1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
11 -- Copyright (C) 1992-2001 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 Rtsfind; use Rtsfind;
42 with Sem_Eval; use Sem_Eval;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sem_Warn; use Sem_Warn;
46 with Sinfo; use Sinfo;
47 with Snames; use Snames;
48 with Stand; use Stand;
49 with Tbuild; use Tbuild;
50 with Ttypes; use Ttypes;
51 with Urealp; use Urealp;
52 with Validsw; use Validsw;
54 package body Checks is
56 -- General note: many of these routines are concerned with generating
57 -- checking code to make sure that constraint error is raised at runtime.
58 -- Clearly this code is only needed if the expander is active, since
59 -- otherwise we will not be generating code or going into the runtime
62 -- We therefore disconnect most of these checks if the expander is
63 -- inactive. This has the additional benefit that we do not need to
64 -- worry about the tree being messed up by previous errors (since errors
65 -- turn off expansion anyway).
67 -- There are a few exceptions to the above rule. For instance routines
68 -- such as Apply_Scalar_Range_Check that do not insert any code can be
69 -- safely called even when the Expander is inactive (but Errors_Detected
70 -- is 0). The benefit of executing this code when expansion is off, is
71 -- the ability to emit constraint error warning for static expressions
72 -- even when we are not generating code.
74 ----------------------------
75 -- Local Subprogram Specs --
76 ----------------------------
78 procedure Apply_Selected_Length_Checks
80 Target_Typ : Entity_Id;
81 Source_Typ : Entity_Id;
83 -- This is the subprogram that does all the work for Apply_Length_Check
84 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
85 -- described for the above routines. The Do_Static flag indicates that
86 -- only a static check is to be done.
88 procedure Apply_Selected_Range_Checks
90 Target_Typ : Entity_Id;
91 Source_Typ : Entity_Id;
93 -- This is the subprogram that does all the work for Apply_Range_Check.
94 -- Expr, Target_Typ and Source_Typ are as described for the above
95 -- routine. The Do_Static flag indicates that only a static check is
98 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
99 -- If a discriminal is used in constraining a prival, Return reference
100 -- to the discriminal of the protected body (which renames the parameter
101 -- of the enclosing protected operation). This clumsy transformation is
102 -- needed because privals are created too late and their actual subtypes
103 -- are not available when analysing the bodies of the protected operations.
104 -- To be cleaned up???
106 function Guard_Access
111 -- In the access type case, guard the test with a test to ensure
112 -- that the access value is non-null, since the checks do not
113 -- not apply to null access values.
115 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
116 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
117 -- Constraint_Error node.
119 function Selected_Length_Checks
121 Target_Typ : Entity_Id;
122 Source_Typ : Entity_Id;
125 -- Like Apply_Selected_Length_Checks, except it doesn't modify
126 -- anything, just returns a list of nodes as described in the spec of
127 -- this package for the Range_Check function.
129 function Selected_Range_Checks
131 Target_Typ : Entity_Id;
132 Source_Typ : Entity_Id;
135 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
136 -- just returns a list of nodes as described in the spec of this package
137 -- for the Range_Check function.
139 ------------------------------
140 -- Access_Checks_Suppressed --
141 ------------------------------
143 function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
145 return Scope_Suppress.Access_Checks
146 or else (Present (E) and then Suppress_Access_Checks (E));
147 end Access_Checks_Suppressed;
149 -------------------------------------
150 -- Accessibility_Checks_Suppressed --
151 -------------------------------------
153 function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
155 return Scope_Suppress.Accessibility_Checks
156 or else (Present (E) and then Suppress_Accessibility_Checks (E));
157 end Accessibility_Checks_Suppressed;
159 -------------------------
160 -- Append_Range_Checks --
161 -------------------------
163 procedure Append_Range_Checks
164 (Checks : Check_Result;
166 Suppress_Typ : Entity_Id;
167 Static_Sloc : Source_Ptr;
170 Internal_Flag_Node : Node_Id := Flag_Node;
171 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
172 Checks_On : constant Boolean :=
173 (not Index_Checks_Suppressed (Suppress_Typ))
175 (not Range_Checks_Suppressed (Suppress_Typ));
178 -- For now we just return if Checks_On is false, however this should
179 -- be enhanced to check for an always True value in the condition
180 -- and to generate a compilation warning???
182 if not Checks_On then
187 exit when No (Checks (J));
189 if Nkind (Checks (J)) = N_Raise_Constraint_Error
190 and then Present (Condition (Checks (J)))
192 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
193 Append_To (Stmts, Checks (J));
194 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
199 (Stmts, Make_Raise_Constraint_Error (Internal_Static_Sloc));
202 end Append_Range_Checks;
204 ------------------------
205 -- Apply_Access_Check --
206 ------------------------
208 procedure Apply_Access_Check (N : Node_Id) is
209 P : constant Node_Id := Prefix (N);
212 if Inside_A_Generic then
216 if Is_Entity_Name (P) then
217 Check_Unset_Reference (P);
220 if Is_Entity_Name (P)
221 and then Access_Checks_Suppressed (Entity (P))
225 elsif Access_Checks_Suppressed (Etype (P)) then
229 Set_Do_Access_Check (N, True);
231 end Apply_Access_Check;
233 -------------------------------
234 -- Apply_Accessibility_Check --
235 -------------------------------
237 procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id) is
238 Loc : constant Source_Ptr := Sloc (N);
239 Param_Ent : constant Entity_Id := Param_Entity (N);
240 Param_Level : Node_Id;
241 Type_Level : Node_Id;
244 if Inside_A_Generic then
247 -- Only apply the run-time check if the access parameter
248 -- has an associated extra access level parameter and
249 -- when the level of the type is less deep than the level
250 -- of the access parameter.
252 elsif Present (Param_Ent)
253 and then Present (Extra_Accessibility (Param_Ent))
254 and then UI_Gt (Object_Access_Level (N),
255 Type_Access_Level (Typ))
256 and then not Accessibility_Checks_Suppressed (Param_Ent)
257 and then not Accessibility_Checks_Suppressed (Typ)
260 New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
263 Make_Integer_Literal (Loc, Type_Access_Level (Typ));
265 -- Raise Program_Error if the accessibility level of the
266 -- the access parameter is deeper than the level of the
267 -- target access type.
270 Make_Raise_Program_Error (Loc,
273 Left_Opnd => Param_Level,
274 Right_Opnd => Type_Level)));
276 Analyze_And_Resolve (N);
278 end Apply_Accessibility_Check;
280 -------------------------------------
281 -- Apply_Arithmetic_Overflow_Check --
282 -------------------------------------
284 -- This routine is called only if the type is an integer type, and
285 -- a software arithmetic overflow check must be performed for op
286 -- (add, subtract, multiply). The check is performed only if
287 -- Software_Overflow_Checking is enabled and Do_Overflow_Check
288 -- is set. In this case we expand the operation into a more complex
289 -- sequence of tests that ensures that overflow is properly caught.
291 procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
292 Loc : constant Source_Ptr := Sloc (N);
293 Typ : constant Entity_Id := Etype (N);
294 Rtyp : constant Entity_Id := Root_Type (Typ);
295 Siz : constant Int := UI_To_Int (Esize (Rtyp));
296 Dsiz : constant Int := Siz * 2;
306 if not Software_Overflow_Checking
307 or else not Do_Overflow_Check (N)
308 or else not Expander_Active
313 -- Nothing to do if the range of the result is known OK
315 Determine_Range (N, OK, Lo, Hi);
317 -- Note in the test below that we assume that if a bound of the
318 -- range is equal to that of the type. That's not quite accurate
319 -- but we do this for the following reasons:
321 -- a) The way that Determine_Range works, it will typically report
322 -- the bounds of the value are the bounds of the type, because
323 -- it either can't tell anything more precise, or does not think
324 -- it is worth the effort to be more precise.
326 -- b) It is very unusual to have a situation in which this would
327 -- generate an unnecessary overflow check (an example would be
328 -- a subtype with a range 0 .. Integer'Last - 1 to which the
329 -- literal value one is added.
331 -- c) The alternative is a lot of special casing in this routine
332 -- which would partially duplicate the Determine_Range processing.
335 and then Lo > Expr_Value (Type_Low_Bound (Typ))
336 and then Hi < Expr_Value (Type_High_Bound (Typ))
341 -- None of the special case optimizations worked, so there is nothing
342 -- for it but to generate the full general case code:
348 -- Typ (Checktyp (x) op Checktyp (y));
350 -- where Typ is the type of the original expression, and Checktyp is
351 -- an integer type of sufficient length to hold the largest possible
354 -- In the case where check type exceeds the size of Long_Long_Integer,
355 -- we use a different approach, expanding to:
357 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
359 -- where xxx is Add, Multiply or Subtract as appropriate
361 -- Find check type if one exists
363 if Dsiz <= Standard_Integer_Size then
364 Ctyp := Standard_Integer;
366 elsif Dsiz <= Standard_Long_Long_Integer_Size then
367 Ctyp := Standard_Long_Long_Integer;
369 -- No check type exists, use runtime call
372 if Nkind (N) = N_Op_Add then
373 Cent := RE_Add_With_Ovflo_Check;
375 elsif Nkind (N) = N_Op_Multiply then
376 Cent := RE_Multiply_With_Ovflo_Check;
379 pragma Assert (Nkind (N) = N_Op_Subtract);
380 Cent := RE_Subtract_With_Ovflo_Check;
385 Make_Function_Call (Loc,
386 Name => New_Reference_To (RTE (Cent), Loc),
387 Parameter_Associations => New_List (
388 OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
389 OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
391 Analyze_And_Resolve (N, Typ);
395 -- If we fall through, we have the case where we do the arithmetic in
396 -- the next higher type and get the check by conversion. In these cases
397 -- Ctyp is set to the type to be used as the check type.
399 Opnod := Relocate_Node (N);
401 Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
404 Set_Etype (Opnd, Ctyp);
405 Set_Analyzed (Opnd, True);
406 Set_Left_Opnd (Opnod, Opnd);
408 Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
411 Set_Etype (Opnd, Ctyp);
412 Set_Analyzed (Opnd, True);
413 Set_Right_Opnd (Opnod, Opnd);
415 -- The type of the operation changes to the base type of the check
416 -- type, and we reset the overflow check indication, since clearly
417 -- no overflow is possible now that we are using a double length
418 -- type. We also set the Analyzed flag to avoid a recursive attempt
419 -- to expand the node.
421 Set_Etype (Opnod, Base_Type (Ctyp));
422 Set_Do_Overflow_Check (Opnod, False);
423 Set_Analyzed (Opnod, True);
425 -- Now build the outer conversion
427 Opnd := OK_Convert_To (Typ, Opnod);
430 Set_Etype (Opnd, Typ);
431 Set_Analyzed (Opnd, True);
432 Set_Do_Overflow_Check (Opnd, True);
435 end Apply_Arithmetic_Overflow_Check;
437 ----------------------------
438 -- Apply_Array_Size_Check --
439 ----------------------------
441 -- Note: Really of course this entre check should be in the backend,
442 -- and perhaps this is not quite the right value, but it is good
443 -- enough to catch the normal cases (and the relevant ACVC tests!)
445 procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id) is
446 Loc : constant Source_Ptr := Sloc (N);
447 Ctyp : constant Entity_Id := Component_Type (Typ);
448 Ent : constant Entity_Id := Defining_Identifier (N);
460 Static : Boolean := True;
461 -- Set false if any index subtye bound is non-static
463 Umark : constant Uintp.Save_Mark := Uintp.Mark;
464 -- We can throw away all the Uint computations here, since they are
465 -- done only to generate boolean test results.
468 -- Size to check against
470 function Is_Address_Or_Import (Decl : Node_Id) return Boolean;
471 -- Determines if Decl is an address clause or Import/Interface pragma
472 -- that references the defining identifier of the current declaration.
474 --------------------------
475 -- Is_Address_Or_Import --
476 --------------------------
478 function Is_Address_Or_Import (Decl : Node_Id) return Boolean is
480 if Nkind (Decl) = N_At_Clause then
481 return Chars (Identifier (Decl)) = Chars (Ent);
483 elsif Nkind (Decl) = N_Attribute_Definition_Clause then
485 Chars (Decl) = Name_Address
487 Nkind (Name (Decl)) = N_Identifier
489 Chars (Name (Decl)) = Chars (Ent);
491 elsif Nkind (Decl) = N_Pragma then
492 if (Chars (Decl) = Name_Import
494 Chars (Decl) = Name_Interface)
495 and then Present (Pragma_Argument_Associations (Decl))
498 F : constant Node_Id :=
499 First (Pragma_Argument_Associations (Decl));
507 Nkind (Expression (Next (F))) = N_Identifier
509 Chars (Expression (Next (F))) = Chars (Ent);
519 end Is_Address_Or_Import;
521 -- Start of processing for Apply_Array_Size_Check
524 if not Expander_Active
525 or else Storage_Checks_Suppressed (Typ)
530 -- It is pointless to insert this check inside an _init_proc, because
531 -- that's too late, we have already built the object to be the right
532 -- size, and if it's too large, too bad!
534 if Inside_Init_Proc then
538 -- Look head for pragma interface/import or address clause applying
539 -- to this entity. If found, we suppress the check entirely. For now
540 -- we only look ahead 20 declarations to stop this becoming too slow
541 -- Note that eventually this whole routine gets moved to gigi.
544 for Ctr in 1 .. 20 loop
548 if Is_Address_Or_Import (Decl) then
553 -- First step is to calculate the maximum number of elements. For this
554 -- calculation, we use the actual size of the subtype if it is static,
555 -- and if a bound of a subtype is non-static, we go to the bound of the
559 Indx := First_Index (Typ);
560 while Present (Indx) loop
561 Xtyp := Etype (Indx);
562 Lo := Type_Low_Bound (Xtyp);
563 Hi := Type_High_Bound (Xtyp);
565 -- If any bound raises constraint error, we will never get this
566 -- far, so there is no need to generate any kind of check.
568 if Raises_Constraint_Error (Lo)
570 Raises_Constraint_Error (Hi)
572 Uintp.Release (Umark);
576 -- Otherwise get bounds values
578 if Is_Static_Expression (Lo) then
579 Lob := Expr_Value (Lo);
581 Lob := Expr_Value (Type_Low_Bound (Base_Type (Xtyp)));
585 if Is_Static_Expression (Hi) then
586 Hib := Expr_Value (Hi);
588 Hib := Expr_Value (Type_High_Bound (Base_Type (Xtyp)));
592 Siz := Siz * UI_Max (Hib - Lob + 1, Uint_0);
596 -- Compute the limit against which we want to check. For subprograms,
597 -- where the array will go on the stack, we use 8*2**24, which (in
598 -- bits) is the size of a 16 megabyte array.
600 if Is_Subprogram (Scope (Ent)) then
601 Check_Siz := Uint_2 ** 27;
603 Check_Siz := Uint_2 ** 31;
606 -- If we have all static bounds and Siz is too large, then we know we
607 -- know we have a storage error right now, so generate message
609 if Static and then Siz >= Check_Siz then
611 Make_Raise_Storage_Error (Loc));
612 Warn_On_Instance := True;
613 Error_Msg_N ("?Storage_Error will be raised at run-time", N);
614 Warn_On_Instance := False;
615 Uintp.Release (Umark);
619 -- Case of component size known at compile time. If the array
620 -- size is definitely in range, then we do not need a check.
622 if Known_Esize (Ctyp)
623 and then Siz * Esize (Ctyp) < Check_Siz
625 Uintp.Release (Umark);
629 -- Here if a dynamic check is required
631 -- What we do is to build an expression for the size of the array,
632 -- which is computed as the 'Size of the array component, times
633 -- the size of each dimension.
635 Uintp.Release (Umark);
638 Make_Attribute_Reference (Loc,
639 Prefix => New_Occurrence_Of (Ctyp, Loc),
640 Attribute_Name => Name_Size);
642 Indx := First_Index (Typ);
644 for J in 1 .. Number_Dimensions (Typ) loop
646 if Sloc (Etype (Indx)) = Sloc (N) then
647 Ensure_Defined (Etype (Indx), N);
651 Make_Op_Multiply (Loc,
654 Make_Attribute_Reference (Loc,
655 Prefix => New_Occurrence_Of (Typ, Loc),
656 Attribute_Name => Name_Length,
657 Expressions => New_List (
658 Make_Integer_Literal (Loc, J))));
663 Make_Raise_Storage_Error (Loc,
668 Make_Integer_Literal (Loc, Check_Siz)));
670 Set_Size_Check_Code (Defining_Identifier (N), Code);
671 Insert_Action (N, Code);
673 end Apply_Array_Size_Check;
675 ----------------------------
676 -- Apply_Constraint_Check --
677 ----------------------------
679 procedure Apply_Constraint_Check
682 No_Sliding : Boolean := False)
684 Desig_Typ : Entity_Id;
687 if Inside_A_Generic then
690 elsif Is_Scalar_Type (Typ) then
691 Apply_Scalar_Range_Check (N, Typ);
693 elsif Is_Array_Type (Typ) then
695 if Is_Constrained (Typ) then
696 Apply_Length_Check (N, Typ);
699 Apply_Range_Check (N, Typ);
702 Apply_Range_Check (N, Typ);
705 elsif (Is_Record_Type (Typ)
706 or else Is_Private_Type (Typ))
707 and then Has_Discriminants (Base_Type (Typ))
708 and then Is_Constrained (Typ)
710 Apply_Discriminant_Check (N, Typ);
712 elsif Is_Access_Type (Typ) then
714 Desig_Typ := Designated_Type (Typ);
716 -- No checks necessary if expression statically null
718 if Nkind (N) = N_Null then
721 -- No sliding possible on access to arrays
723 elsif Is_Array_Type (Desig_Typ) then
724 if Is_Constrained (Desig_Typ) then
725 Apply_Length_Check (N, Typ);
728 Apply_Range_Check (N, Typ);
730 elsif Has_Discriminants (Base_Type (Desig_Typ))
731 and then Is_Constrained (Desig_Typ)
733 Apply_Discriminant_Check (N, Typ);
736 end Apply_Constraint_Check;
738 ------------------------------
739 -- Apply_Discriminant_Check --
740 ------------------------------
742 procedure Apply_Discriminant_Check
745 Lhs : Node_Id := Empty)
747 Loc : constant Source_Ptr := Sloc (N);
748 Do_Access : constant Boolean := Is_Access_Type (Typ);
749 S_Typ : Entity_Id := Etype (N);
753 function Is_Aliased_Unconstrained_Component return Boolean;
754 -- It is possible for an aliased component to have a nominal
755 -- unconstrained subtype (through instantiation). If this is a
756 -- discriminated component assigned in the expansion of an aggregate
757 -- in an initialization, the check must be suppressed. This unusual
758 -- situation requires a predicate of its own (see 7503-008).
760 ----------------------------------------
761 -- Is_Aliased_Unconstrained_Component --
762 ----------------------------------------
764 function Is_Aliased_Unconstrained_Component return Boolean is
769 if Nkind (Lhs) /= N_Selected_Component then
772 Comp := Entity (Selector_Name (Lhs));
773 Pref := Prefix (Lhs);
776 if Ekind (Comp) /= E_Component
777 or else not Is_Aliased (Comp)
782 return not Comes_From_Source (Pref)
784 and then not Is_Constrained (Etype (Comp));
785 end Is_Aliased_Unconstrained_Component;
787 -- Start of processing for Apply_Discriminant_Check
791 T_Typ := Designated_Type (Typ);
796 -- Nothing to do if discriminant checks are suppressed or else no code
797 -- is to be generated
799 if not Expander_Active
800 or else Discriminant_Checks_Suppressed (T_Typ)
805 -- No discriminant checks necessary for access when expression
806 -- is statically Null. This is not only an optimization, this is
807 -- fundamental because otherwise discriminant checks may be generated
808 -- in init procs for types containing an access to a non-frozen yet
809 -- record, causing a deadly forward reference.
811 -- Also, if the expression is of an access type whose designated
812 -- type is incomplete, then the access value must be null and
813 -- we suppress the check.
815 if Nkind (N) = N_Null then
818 elsif Is_Access_Type (S_Typ) then
819 S_Typ := Designated_Type (S_Typ);
821 if Ekind (S_Typ) = E_Incomplete_Type then
826 -- If an assignment target is present, then we need to generate
827 -- the actual subtype if the target is a parameter or aliased
828 -- object with an unconstrained nominal subtype.
831 and then (Present (Param_Entity (Lhs))
832 or else (not Is_Constrained (T_Typ)
833 and then Is_Aliased_View (Lhs)
834 and then not Is_Aliased_Unconstrained_Component))
836 T_Typ := Get_Actual_Subtype (Lhs);
839 -- Nothing to do if the type is unconstrained (this is the case
840 -- where the actual subtype in the RM sense of N is unconstrained
841 -- and no check is required).
843 if not Is_Constrained (T_Typ) then
847 -- Suppress checks if the subtypes are the same.
848 -- the check must be preserved in an assignment to a formal, because
849 -- the constraint is given by the actual.
851 if Nkind (Original_Node (N)) /= N_Allocator
853 or else not Is_Entity_Name (Lhs)
854 or else (Ekind (Entity (Lhs)) /= E_In_Out_Parameter
855 and then Ekind (Entity (Lhs)) /= E_Out_Parameter))
858 or else (Do_Access and then Designated_Type (Typ) = S_Typ))
859 and then not Is_Aliased_View (Lhs)
864 -- We can also eliminate checks on allocators with a subtype mark
865 -- that coincides with the context type. The context type may be a
866 -- subtype without a constraint (common case, a generic actual).
868 elsif Nkind (Original_Node (N)) = N_Allocator
869 and then Is_Entity_Name (Expression (Original_Node (N)))
872 Alloc_Typ : Entity_Id := Entity (Expression (Original_Node (N)));
876 or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
877 and then Is_Entity_Name (
878 Subtype_Indication (Parent (T_Typ)))
879 and then Alloc_Typ = Base_Type (T_Typ))
887 -- See if we have a case where the types are both constrained, and
888 -- all the constraints are constants. In this case, we can do the
889 -- check successfully at compile time.
891 -- we skip this check for the case where the node is a rewritten`
892 -- allocator, because it already carries the context subtype, and
893 -- extracting the discriminants from the aggregate is messy.
895 if Is_Constrained (S_Typ)
896 and then Nkind (Original_Node (N)) /= N_Allocator
906 -- S_Typ may not have discriminants in the case where it is a
907 -- private type completed by a default discriminated type. In
908 -- that case, we need to get the constraints from the
909 -- underlying_type. If the underlying type is unconstrained (i.e.
910 -- has no default discriminants) no check is needed.
912 if Has_Discriminants (S_Typ) then
913 Discr := First_Discriminant (S_Typ);
914 DconS := First_Elmt (Discriminant_Constraint (S_Typ));
917 Discr := First_Discriminant (Underlying_Type (S_Typ));
920 (Discriminant_Constraint (Underlying_Type (S_Typ)));
927 DconT := First_Elmt (Discriminant_Constraint (T_Typ));
929 while Present (Discr) loop
930 ItemS := Node (DconS);
931 ItemT := Node (DconT);
934 not Is_OK_Static_Expression (ItemS)
936 not Is_OK_Static_Expression (ItemT);
938 if Expr_Value (ItemS) /= Expr_Value (ItemT) then
939 if Do_Access then -- needs run-time check.
942 Apply_Compile_Time_Constraint_Error
943 (N, "incorrect value for discriminant&?", Ent => Discr);
950 Next_Discriminant (Discr);
959 -- Here we need a discriminant check. First build the expression
960 -- for the comparisons of the discriminants:
962 -- (n.disc1 /= typ.disc1) or else
963 -- (n.disc2 /= typ.disc2) or else
965 -- (n.discn /= typ.discn)
967 Cond := Build_Discriminant_Checks (N, T_Typ);
969 -- If Lhs is set and is a parameter, then the condition is
970 -- guarded by: lhs'constrained and then (condition built above)
972 if Present (Param_Entity (Lhs)) then
976 Make_Attribute_Reference (Loc,
977 Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
978 Attribute_Name => Name_Constrained),
983 Cond := Guard_Access (Cond, Loc, N);
987 Make_Raise_Constraint_Error (Loc, Condition => Cond));
989 end Apply_Discriminant_Check;
991 ------------------------
992 -- Apply_Divide_Check --
993 ------------------------
995 procedure Apply_Divide_Check (N : Node_Id) is
996 Loc : constant Source_Ptr := Sloc (N);
997 Typ : constant Entity_Id := Etype (N);
998 Left : constant Node_Id := Left_Opnd (N);
999 Right : constant Node_Id := Right_Opnd (N);
1011 and then Software_Overflow_Checking
1013 Determine_Range (Right, ROK, Rlo, Rhi);
1015 -- See if division by zero possible, and if so generate test. This
1016 -- part of the test is not controlled by the -gnato switch.
1018 if Do_Division_Check (N) then
1020 if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
1022 Make_Raise_Constraint_Error (Loc,
1025 Left_Opnd => Duplicate_Subexpr (Right),
1026 Right_Opnd => Make_Integer_Literal (Loc, 0))));
1030 -- Test for extremely annoying case of xxx'First divided by -1
1032 if Do_Overflow_Check (N) then
1034 if Nkind (N) = N_Op_Divide
1035 and then Is_Signed_Integer_Type (Typ)
1037 Determine_Range (Left, LOK, Llo, Lhi);
1038 LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
1040 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
1042 ((not LOK) or else (Llo = LLB))
1045 Make_Raise_Constraint_Error (Loc,
1050 Left_Opnd => Duplicate_Subexpr (Left),
1051 Right_Opnd => Make_Integer_Literal (Loc, LLB)),
1054 Left_Opnd => Duplicate_Subexpr (Right),
1056 Make_Integer_Literal (Loc, -1)))));
1061 end Apply_Divide_Check;
1063 ------------------------
1064 -- Apply_Length_Check --
1065 ------------------------
1067 procedure Apply_Length_Check
1069 Target_Typ : Entity_Id;
1070 Source_Typ : Entity_Id := Empty)
1073 Apply_Selected_Length_Checks
1074 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1075 end Apply_Length_Check;
1077 -----------------------
1078 -- Apply_Range_Check --
1079 -----------------------
1081 procedure Apply_Range_Check
1083 Target_Typ : Entity_Id;
1084 Source_Typ : Entity_Id := Empty)
1087 Apply_Selected_Range_Checks
1088 (Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
1089 end Apply_Range_Check;
1091 ------------------------------
1092 -- Apply_Scalar_Range_Check --
1093 ------------------------------
1095 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check
1096 -- flag off if it is already set on.
1098 procedure Apply_Scalar_Range_Check
1100 Target_Typ : Entity_Id;
1101 Source_Typ : Entity_Id := Empty;
1102 Fixed_Int : Boolean := False)
1104 Parnt : constant Node_Id := Parent (Expr);
1106 Arr : Node_Id := Empty; -- initialize to prevent warning
1107 Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning
1110 Is_Subscr_Ref : Boolean;
1111 -- Set true if Expr is a subscript
1113 Is_Unconstrained_Subscr_Ref : Boolean;
1114 -- Set true if Expr is a subscript of an unconstrained array. In this
1115 -- case we do not attempt to do an analysis of the value against the
1116 -- range of the subscript, since we don't know the actual subtype.
1119 -- Set to True if Expr should be regarded as a real value
1120 -- even though the type of Expr might be discrete.
1122 procedure Bad_Value;
1123 -- Procedure called if value is determined to be out of range
1125 procedure Bad_Value is
1127 Apply_Compile_Time_Constraint_Error
1128 (Expr, "value not in range of}?",
1134 if Inside_A_Generic then
1137 -- Return if check obviously not needed. Note that we do not check
1138 -- for the expander being inactive, since this routine does not
1139 -- insert any code, but it does generate useful warnings sometimes,
1140 -- which we would like even if we are in semantics only mode.
1142 elsif Target_Typ = Any_Type
1143 or else not Is_Scalar_Type (Target_Typ)
1144 or else Raises_Constraint_Error (Expr)
1149 -- Now, see if checks are suppressed
1152 Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
1154 if Is_Subscr_Ref then
1155 Arr := Prefix (Parnt);
1156 Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
1159 if not Do_Range_Check (Expr) then
1161 -- Subscript reference. Check for Index_Checks suppressed
1163 if Is_Subscr_Ref then
1165 -- Check array type and its base type
1167 if Index_Checks_Suppressed (Arr_Typ)
1168 or else Suppress_Index_Checks (Base_Type (Arr_Typ))
1172 -- Check array itself if it is an entity name
1174 elsif Is_Entity_Name (Arr)
1175 and then Suppress_Index_Checks (Entity (Arr))
1179 -- Check expression itself if it is an entity name
1181 elsif Is_Entity_Name (Expr)
1182 and then Suppress_Index_Checks (Entity (Expr))
1187 -- All other cases, check for Range_Checks suppressed
1190 -- Check target type and its base type
1192 if Range_Checks_Suppressed (Target_Typ)
1193 or else Suppress_Range_Checks (Base_Type (Target_Typ))
1197 -- Check expression itself if it is an entity name
1199 elsif Is_Entity_Name (Expr)
1200 and then Suppress_Range_Checks (Entity (Expr))
1204 -- If Expr is part of an assignment statement, then check
1205 -- left side of assignment if it is an entity name.
1207 elsif Nkind (Parnt) = N_Assignment_Statement
1208 and then Is_Entity_Name (Name (Parnt))
1209 and then Suppress_Range_Checks (Entity (Name (Parnt)))
1216 -- Now see if we need a check
1218 if No (Source_Typ) then
1219 S_Typ := Etype (Expr);
1221 S_Typ := Source_Typ;
1224 if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
1228 Is_Unconstrained_Subscr_Ref :=
1229 Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
1231 -- Always do a range check if the source type includes infinities
1232 -- and the target type does not include infinities.
1234 if Is_Floating_Point_Type (S_Typ)
1235 and then Has_Infinities (S_Typ)
1236 and then not Has_Infinities (Target_Typ)
1238 Enable_Range_Check (Expr);
1241 -- Return if we know expression is definitely in the range of
1242 -- the target type as determined by Determine_Range. Right now
1243 -- we only do this for discrete types, and not fixed-point or
1244 -- floating-point types.
1246 -- The additional less-precise tests below catch these cases.
1248 -- Note: skip this if we are given a source_typ, since the point
1249 -- of supplying a Source_Typ is to stop us looking at the expression.
1250 -- could sharpen this test to be out parameters only ???
1252 if Is_Discrete_Type (Target_Typ)
1253 and then Is_Discrete_Type (Etype (Expr))
1254 and then not Is_Unconstrained_Subscr_Ref
1255 and then No (Source_Typ)
1258 Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
1259 Thi : constant Node_Id := Type_High_Bound (Target_Typ);
1264 if Compile_Time_Known_Value (Tlo)
1265 and then Compile_Time_Known_Value (Thi)
1267 Determine_Range (Expr, OK, Lo, Hi);
1271 Lov : constant Uint := Expr_Value (Tlo);
1272 Hiv : constant Uint := Expr_Value (Thi);
1275 if Lo >= Lov and then Hi <= Hiv then
1278 elsif Lov > Hi or else Hiv < Lo then
1289 Is_Floating_Point_Type (S_Typ)
1290 or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
1292 -- Check if we can determine at compile time whether Expr is in the
1293 -- range of the target type. Note that if S_Typ is within the
1294 -- bounds of Target_Typ then this must be the case. This checks is
1295 -- only meaningful if this is not a conversion between integer and
1298 if not Is_Unconstrained_Subscr_Ref
1300 Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
1302 (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
1304 Is_In_Range (Expr, Target_Typ, Fixed_Int, Int_Real))
1308 elsif Is_Out_Of_Range (Expr, Target_Typ, Fixed_Int, Int_Real) then
1312 -- Do not set range checks if they are killed
1314 elsif Nkind (Expr) = N_Unchecked_Type_Conversion
1315 and then Kill_Range_Check (Expr)
1319 -- ??? We only need a runtime check if the target type is constrained
1320 -- (the predefined type Float is not for instance).
1321 -- so the following should really be
1323 -- elsif Is_Constrained (Target_Typ) then
1325 -- but it isn't because certain types do not have the Is_Constrained
1326 -- flag properly set (see 1503-003).
1329 Enable_Range_Check (Expr);
1333 end Apply_Scalar_Range_Check;
1335 ----------------------------------
1336 -- Apply_Selected_Length_Checks --
1337 ----------------------------------
1339 procedure Apply_Selected_Length_Checks
1341 Target_Typ : Entity_Id;
1342 Source_Typ : Entity_Id;
1343 Do_Static : Boolean)
1346 R_Result : Check_Result;
1349 Loc : constant Source_Ptr := Sloc (Ck_Node);
1350 Checks_On : constant Boolean :=
1351 (not Index_Checks_Suppressed (Target_Typ))
1353 (not Length_Checks_Suppressed (Target_Typ));
1356 if not Expander_Active or else not Checks_On then
1361 Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
1363 for J in 1 .. 2 loop
1365 R_Cno := R_Result (J);
1366 exit when No (R_Cno);
1368 -- A length check may mention an Itype which is attached to a
1369 -- subsequent node. At the top level in a package this can cause
1370 -- an order-of-elaboration problem, so we make sure that the itype
1371 -- is referenced now.
1373 if Ekind (Current_Scope) = E_Package
1374 and then Is_Compilation_Unit (Current_Scope)
1376 Ensure_Defined (Target_Typ, Ck_Node);
1378 if Present (Source_Typ) then
1379 Ensure_Defined (Source_Typ, Ck_Node);
1381 elsif Is_Itype (Etype (Ck_Node)) then
1382 Ensure_Defined (Etype (Ck_Node), Ck_Node);
1386 -- If the item is a conditional raise of constraint error,
1387 -- then have a look at what check is being performed and
1390 if Nkind (R_Cno) = N_Raise_Constraint_Error
1391 and then Present (Condition (R_Cno))
1393 Cond := Condition (R_Cno);
1395 if not Has_Dynamic_Length_Check (Ck_Node) then
1396 Insert_Action (Ck_Node, R_Cno);
1398 if not Do_Static then
1399 Set_Has_Dynamic_Length_Check (Ck_Node);
1404 -- Output a warning if the condition is known to be True
1406 if Is_Entity_Name (Cond)
1407 and then Entity (Cond) = Standard_True
1409 Apply_Compile_Time_Constraint_Error
1410 (Ck_Node, "wrong length for array of}?",
1414 -- If we were only doing a static check, or if checks are not
1415 -- on, then we want to delete the check, since it is not needed.
1416 -- We do this by replacing the if statement by a null statement
1418 elsif Do_Static or else not Checks_On then
1419 Rewrite (R_Cno, Make_Null_Statement (Loc));
1423 Install_Static_Check (R_Cno, Loc);
1428 end Apply_Selected_Length_Checks;
1430 ---------------------------------
1431 -- Apply_Selected_Range_Checks --
1432 ---------------------------------
1434 procedure Apply_Selected_Range_Checks
1436 Target_Typ : Entity_Id;
1437 Source_Typ : Entity_Id;
1438 Do_Static : Boolean)
1441 R_Result : Check_Result;
1444 Loc : constant Source_Ptr := Sloc (Ck_Node);
1445 Checks_On : constant Boolean :=
1446 (not Index_Checks_Suppressed (Target_Typ))
1448 (not Range_Checks_Suppressed (Target_Typ));
1451 if not Expander_Active or else not Checks_On then
1456 Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
1458 for J in 1 .. 2 loop
1460 R_Cno := R_Result (J);
1461 exit when No (R_Cno);
1463 -- If the item is a conditional raise of constraint error,
1464 -- then have a look at what check is being performed and
1467 if Nkind (R_Cno) = N_Raise_Constraint_Error
1468 and then Present (Condition (R_Cno))
1470 Cond := Condition (R_Cno);
1472 if not Has_Dynamic_Range_Check (Ck_Node) then
1473 Insert_Action (Ck_Node, R_Cno);
1475 if not Do_Static then
1476 Set_Has_Dynamic_Range_Check (Ck_Node);
1480 -- Output a warning if the condition is known to be True
1482 if Is_Entity_Name (Cond)
1483 and then Entity (Cond) = Standard_True
1485 -- Since an N_Range is technically not an expression, we
1486 -- have to set one of the bounds to C_E and then just flag
1487 -- the N_Range. The warning message will point to the
1488 -- lower bound and complain about a range, which seems OK.
1490 if Nkind (Ck_Node) = N_Range then
1491 Apply_Compile_Time_Constraint_Error
1492 (Low_Bound (Ck_Node), "static range out of bounds of}?",
1496 Set_Raises_Constraint_Error (Ck_Node);
1499 Apply_Compile_Time_Constraint_Error
1500 (Ck_Node, "static value out of range of}?",
1505 -- If we were only doing a static check, or if checks are not
1506 -- on, then we want to delete the check, since it is not needed.
1507 -- We do this by replacing the if statement by a null statement
1509 elsif Do_Static or else not Checks_On then
1510 Rewrite (R_Cno, Make_Null_Statement (Loc));
1514 Install_Static_Check (R_Cno, Loc);
1519 end Apply_Selected_Range_Checks;
1521 -------------------------------
1522 -- Apply_Static_Length_Check --
1523 -------------------------------
1525 procedure Apply_Static_Length_Check
1527 Target_Typ : Entity_Id;
1528 Source_Typ : Entity_Id := Empty)
1531 Apply_Selected_Length_Checks
1532 (Expr, Target_Typ, Source_Typ, Do_Static => True);
1533 end Apply_Static_Length_Check;
1535 -------------------------------------
1536 -- Apply_Subscript_Validity_Checks --
1537 -------------------------------------
1539 procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
1543 pragma Assert (Nkind (Expr) = N_Indexed_Component);
1545 -- Loop through subscripts
1547 Sub := First (Expressions (Expr));
1548 while Present (Sub) loop
1550 -- Check one subscript. Note that we do not worry about
1551 -- enumeration type with holes, since we will convert the
1552 -- value to a Pos value for the subscript, and that convert
1553 -- will do the necessary validity check.
1555 Ensure_Valid (Sub, Holes_OK => True);
1557 -- Move to next subscript
1561 end Apply_Subscript_Validity_Checks;
1563 ----------------------------------
1564 -- Apply_Type_Conversion_Checks --
1565 ----------------------------------
1567 procedure Apply_Type_Conversion_Checks (N : Node_Id) is
1568 Target_Type : constant Entity_Id := Etype (N);
1569 Target_Base : constant Entity_Id := Base_Type (Target_Type);
1571 Expr : constant Node_Id := Expression (N);
1572 Expr_Type : constant Entity_Id := Etype (Expr);
1575 if Inside_A_Generic then
1578 -- Skip these checks if errors detected, there are some nasty
1579 -- situations of incomplete trees that blow things up.
1581 elsif Errors_Detected > 0 then
1584 -- Scalar type conversions of the form Target_Type (Expr) require
1587 -- - First there is an overflow check to insure that Expr is
1588 -- in the base type of Target_Typ (4.6 (28)),
1590 -- - After we know Expr fits into the base type, we must perform a
1591 -- range check to ensure that Expr meets the constraints of the
1594 elsif Is_Scalar_Type (Target_Type) then
1596 Conv_OK : constant Boolean := Conversion_OK (N);
1597 -- If the Conversion_OK flag on the type conversion is set
1598 -- and no floating point type is involved in the type conversion
1599 -- then fixed point values must be read as integral values.
1604 if not Overflow_Checks_Suppressed (Target_Base)
1605 and then not In_Subrange_Of (Expr_Type, Target_Base, Conv_OK)
1607 Set_Do_Overflow_Check (N);
1610 if not Range_Checks_Suppressed (Target_Type)
1611 and then not Range_Checks_Suppressed (Expr_Type)
1613 Apply_Scalar_Range_Check
1614 (Expr, Target_Type, Fixed_Int => Conv_OK);
1618 elsif Comes_From_Source (N)
1619 and then Is_Record_Type (Target_Type)
1620 and then Is_Derived_Type (Target_Type)
1621 and then not Is_Tagged_Type (Target_Type)
1622 and then not Is_Constrained (Target_Type)
1623 and then Present (Girder_Constraint (Target_Type))
1625 -- A unconstrained derived type may have inherited discriminants.
1626 -- Build an actual discriminant constraint list using the girder
1627 -- constraint, to verify that the expression of the parent type
1628 -- satisfies the constraints imposed by the (unconstrained!)
1629 -- derived type. This applies to value conversions, not to view
1630 -- conversions of tagged types.
1633 Loc : constant Source_Ptr := Sloc (N);
1635 Constraint : Elmt_Id;
1636 Discr_Value : Node_Id;
1638 New_Constraints : Elist_Id := New_Elmt_List;
1639 Old_Constraints : Elist_Id := Discriminant_Constraint (Expr_Type);
1642 Constraint := First_Elmt (Girder_Constraint (Target_Type));
1644 while Present (Constraint) loop
1645 Discr_Value := Node (Constraint);
1647 if Is_Entity_Name (Discr_Value)
1648 and then Ekind (Entity (Discr_Value)) = E_Discriminant
1650 Discr := Corresponding_Discriminant (Entity (Discr_Value));
1653 and then Scope (Discr) = Base_Type (Expr_Type)
1655 -- Parent is constrained by new discriminant. Obtain
1656 -- Value of original discriminant in expression. If
1657 -- the new discriminant has been used to constrain more
1658 -- than one of the girder ones, this will provide the
1659 -- required consistency check.
1662 Make_Selected_Component (Loc,
1664 Duplicate_Subexpr (Expr, Name_Req => True),
1666 Make_Identifier (Loc, Chars (Discr))),
1670 -- Discriminant of more remote ancestor ???
1675 -- Derived type definition has an explicit value for
1676 -- this girder discriminant.
1680 (Duplicate_Subexpr (Discr_Value), New_Constraints);
1683 Next_Elmt (Constraint);
1686 -- Use the unconstrained expression type to retrieve the
1687 -- discriminants of the parent, and apply momentarily the
1688 -- discriminant constraint synthesized above.
1690 Set_Discriminant_Constraint (Expr_Type, New_Constraints);
1691 Cond := Build_Discriminant_Checks (Expr, Expr_Type);
1692 Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
1695 Make_Raise_Constraint_Error (Loc, Condition => Cond));
1698 -- should there be other checks here for array types ???
1704 end Apply_Type_Conversion_Checks;
1706 ----------------------------------------------
1707 -- Apply_Universal_Integer_Attribute_Checks --
1708 ----------------------------------------------
1710 procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
1711 Loc : constant Source_Ptr := Sloc (N);
1712 Typ : constant Entity_Id := Etype (N);
1715 if Inside_A_Generic then
1718 -- Nothing to do if checks are suppressed
1720 elsif Range_Checks_Suppressed (Typ)
1721 and then Overflow_Checks_Suppressed (Typ)
1725 -- Nothing to do if the attribute does not come from source. The
1726 -- internal attributes we generate of this type do not need checks,
1727 -- and furthermore the attempt to check them causes some circular
1728 -- elaboration orders when dealing with packed types.
1730 elsif not Comes_From_Source (N) then
1733 -- Otherwise, replace the attribute node with a type conversion
1734 -- node whose expression is the attribute, retyped to universal
1735 -- integer, and whose subtype mark is the target type. The call
1736 -- to analyze this conversion will set range and overflow checks
1737 -- as required for proper detection of an out of range value.
1740 Set_Etype (N, Universal_Integer);
1741 Set_Analyzed (N, True);
1744 Make_Type_Conversion (Loc,
1745 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
1746 Expression => Relocate_Node (N)));
1748 Analyze_And_Resolve (N, Typ);
1752 end Apply_Universal_Integer_Attribute_Checks;
1754 -------------------------------
1755 -- Build_Discriminant_Checks --
1756 -------------------------------
1758 function Build_Discriminant_Checks
1763 Loc : constant Source_Ptr := Sloc (N);
1766 Disc_Ent : Entity_Id;
1771 Disc := First_Elmt (Discriminant_Constraint (T_Typ));
1773 -- For a fully private type, use the discriminants of the parent
1776 if Is_Private_Type (T_Typ)
1777 and then No (Full_View (T_Typ))
1779 Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
1781 Disc_Ent := First_Discriminant (T_Typ);
1784 while Present (Disc) loop
1786 Dval := Node (Disc);
1788 if Nkind (Dval) = N_Identifier
1789 and then Ekind (Entity (Dval)) = E_Discriminant
1791 Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
1793 Dval := Duplicate_Subexpr (Dval);
1796 Evolve_Or_Else (Cond,
1799 Make_Selected_Component (Loc,
1801 Duplicate_Subexpr (N, Name_Req => True),
1803 Make_Identifier (Loc, Chars (Disc_Ent))),
1804 Right_Opnd => Dval));
1807 Next_Discriminant (Disc_Ent);
1811 end Build_Discriminant_Checks;
1813 -----------------------------------
1814 -- Check_Valid_Lvalue_Subscripts --
1815 -----------------------------------
1817 procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
1819 -- Skip this if range checks are suppressed
1821 if Range_Checks_Suppressed (Etype (Expr)) then
1824 -- Only do this check for expressions that come from source. We
1825 -- assume that expander generated assignments explicitly include
1826 -- any necessary checks. Note that this is not just an optimization,
1827 -- it avoids infinite recursions!
1829 elsif not Comes_From_Source (Expr) then
1832 -- For a selected component, check the prefix
1834 elsif Nkind (Expr) = N_Selected_Component then
1835 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
1838 -- Case of indexed component
1840 elsif Nkind (Expr) = N_Indexed_Component then
1841 Apply_Subscript_Validity_Checks (Expr);
1843 -- Prefix may itself be or contain an indexed component, and
1844 -- these subscripts need checking as well
1846 Check_Valid_Lvalue_Subscripts (Prefix (Expr));
1848 end Check_Valid_Lvalue_Subscripts;
1850 ---------------------
1851 -- Determine_Range --
1852 ---------------------
1854 Cache_Size : constant := 2 ** 6;
1855 type Cache_Index is range 0 .. Cache_Size - 1;
1856 -- Determine size of below cache (power of 2 is more efficient!)
1858 Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
1859 Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
1860 Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
1861 -- The above arrays are used to implement a small direct cache
1862 -- for Determine_Range calls. Because of the way Determine_Range
1863 -- recursively traces subexpressions, and because overflow checking
1864 -- calls the routine on the way up the tree, a quadratic behavior
1865 -- can otherwise be encountered in large expressions. The cache
1866 -- entry for node N is stored in the (N mod Cache_Size) entry, and
1867 -- can be validated by checking the actual node value stored there.
1869 procedure Determine_Range
1875 Typ : constant Entity_Id := Etype (N);
1886 Cindex : Cache_Index;
1888 function OK_Operands return Boolean;
1889 -- Used for binary operators. Determines the ranges of the left and
1890 -- right operands, and if they are both OK, returns True, and puts
1891 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left
1897 function OK_Operands return Boolean is
1899 Determine_Range (Left_Opnd (N), OK1, Lo_Left, Hi_Left);
1905 Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
1909 -- Start of processing for Determine_Range
1912 -- Prevent junk warnings by initializing range variables
1919 -- If the type is not discrete, or is undefined, then we can't
1920 -- do anything about determining the range.
1922 if No (Typ) or else not Is_Discrete_Type (Typ)
1923 or else Error_Posted (N)
1929 -- For all other cases, we can determine the range
1933 -- If value is compile time known, then the possible range is the
1934 -- one value that we know this expression definitely has!
1936 if Compile_Time_Known_Value (N) then
1937 Lo := Expr_Value (N);
1942 -- Return if already in the cache
1944 Cindex := Cache_Index (N mod Cache_Size);
1946 if Determine_Range_Cache_N (Cindex) = N then
1947 Lo := Determine_Range_Cache_Lo (Cindex);
1948 Hi := Determine_Range_Cache_Hi (Cindex);
1952 -- Otherwise, start by finding the bounds of the type of the
1953 -- expression, the value cannot be outside this range (if it
1954 -- is, then we have an overflow situation, which is a separate
1955 -- check, we are talking here only about the expression value).
1957 -- We use the actual bound unless it is dynamic, in which case
1958 -- use the corresponding base type bound if possible. If we can't
1961 Bound := Type_Low_Bound (Typ);
1963 if Compile_Time_Known_Value (Bound) then
1964 Lo := Expr_Value (Bound);
1966 elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then
1967 Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
1974 Bound := Type_High_Bound (Typ);
1976 if Compile_Time_Known_Value (Bound) then
1977 Hi := Expr_Value (Bound);
1979 elsif Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then
1980 Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ)));
1988 -- We may be able to refine this value in certain situations. If
1989 -- refinement is possible, then Lor and Hir are set to possibly
1990 -- tighter bounds, and OK1 is set to True.
1994 -- For unary plus, result is limited by range of operand
1997 Determine_Range (Right_Opnd (N), OK1, Lor, Hir);
1999 -- For unary minus, determine range of operand, and negate it
2002 Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
2009 -- For binary addition, get range of each operand and do the
2010 -- addition to get the result range.
2014 Lor := Lo_Left + Lo_Right;
2015 Hir := Hi_Left + Hi_Right;
2018 -- Division is tricky. The only case we consider is where the
2019 -- right operand is a positive constant, and in this case we
2020 -- simply divide the bounds of the left operand
2024 if Lo_Right = Hi_Right
2025 and then Lo_Right > 0
2027 Lor := Lo_Left / Lo_Right;
2028 Hir := Hi_Left / Lo_Right;
2035 -- For binary subtraction, get range of each operand and do
2036 -- the worst case subtraction to get the result range.
2038 when N_Op_Subtract =>
2040 Lor := Lo_Left - Hi_Right;
2041 Hir := Hi_Left - Lo_Right;
2044 -- For MOD, if right operand is a positive constant, then
2045 -- result must be in the allowable range of mod results.
2049 if Lo_Right = Hi_Right then
2050 if Lo_Right > 0 then
2052 Hir := Lo_Right - 1;
2054 elsif Lo_Right < 0 then
2055 Lor := Lo_Right + 1;
2064 -- For REM, if right operand is a positive constant, then
2065 -- result must be in the allowable range of mod results.
2069 if Lo_Right = Hi_Right then
2071 Dval : constant Uint := (abs Lo_Right) - 1;
2074 -- The sign of the result depends on the sign of the
2075 -- dividend (but not on the sign of the divisor, hence
2076 -- the abs operation above).
2096 -- Attribute reference cases
2098 when N_Attribute_Reference =>
2099 case Attribute_Name (N) is
2101 -- For Pos/Val attributes, we can refine the range using the
2102 -- possible range of values of the attribute expression
2104 when Name_Pos | Name_Val =>
2105 Determine_Range (First (Expressions (N)), OK1, Lor, Hir);
2107 -- For Length attribute, use the bounds of the corresponding
2108 -- index type to refine the range.
2112 Atyp : Entity_Id := Etype (Prefix (N));
2120 if Is_Access_Type (Atyp) then
2121 Atyp := Designated_Type (Atyp);
2124 -- For string literal, we know exact value
2126 if Ekind (Atyp) = E_String_Literal_Subtype then
2128 Lo := String_Literal_Length (Atyp);
2129 Hi := String_Literal_Length (Atyp);
2133 -- Otherwise check for expression given
2135 if No (Expressions (N)) then
2139 UI_To_Int (Expr_Value (First (Expressions (N))));
2142 Indx := First_Index (Atyp);
2143 for J in 2 .. Inum loop
2144 Indx := Next_Index (Indx);
2148 (Type_Low_Bound (Etype (Indx)), OK1, LL, LU);
2152 (Type_High_Bound (Etype (Indx)), OK1, UL, UU);
2156 -- The maximum value for Length is the biggest
2157 -- possible gap between the values of the bounds.
2158 -- But of course, this value cannot be negative.
2160 Hir := UI_Max (Uint_0, UU - LL);
2162 -- For constrained arrays, the minimum value for
2163 -- Length is taken from the actual value of the
2164 -- bounds, since the index will be exactly of
2167 if Is_Constrained (Atyp) then
2168 Lor := UI_Max (Uint_0, UL - LU);
2170 -- For an unconstrained array, the minimum value
2171 -- for length is always zero.
2180 -- No special handling for other attributes
2181 -- Probably more opportunities exist here ???
2188 -- For type conversion from one discrete type to another, we
2189 -- can refine the range using the converted value.
2191 when N_Type_Conversion =>
2192 Determine_Range (Expression (N), OK1, Lor, Hir);
2194 -- Nothing special to do for all other expression kinds
2202 -- At this stage, if OK1 is true, then we know that the actual
2203 -- result of the computed expression is in the range Lor .. Hir.
2204 -- We can use this to restrict the possible range of results.
2208 -- If the refined value of the low bound is greater than the
2209 -- type high bound, then reset it to the more restrictive
2210 -- value. However, we do NOT do this for the case of a modular
2211 -- type where the possible upper bound on the value is above the
2212 -- base type high bound, because that means the result could wrap.
2215 and then not (Is_Modular_Integer_Type (Typ)
2216 and then Hir > Hbound)
2221 -- Similarly, if the refined value of the high bound is less
2222 -- than the value so far, then reset it to the more restrictive
2223 -- value. Again, we do not do this if the refined low bound is
2224 -- negative for a modular type, since this would wrap.
2227 and then not (Is_Modular_Integer_Type (Typ)
2228 and then Lor < Uint_0)
2234 -- Set cache entry for future call and we are all done
2236 Determine_Range_Cache_N (Cindex) := N;
2237 Determine_Range_Cache_Lo (Cindex) := Lo;
2238 Determine_Range_Cache_Hi (Cindex) := Hi;
2241 -- If any exception occurs, it means that we have some bug in the compiler
2242 -- possibly triggered by a previous error, or by some unforseen peculiar
2243 -- occurrence. However, this is only an optimization attempt, so there is
2244 -- really no point in crashing the compiler. Instead we just decide, too
2245 -- bad, we can't figure out a range in this case after all.
2250 -- Debug flag K disables this behavior (useful for debugging)
2252 if Debug_Flag_K then
2261 end Determine_Range;
2263 ------------------------------------
2264 -- Discriminant_Checks_Suppressed --
2265 ------------------------------------
2267 function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
2269 return Scope_Suppress.Discriminant_Checks
2270 or else (Present (E) and then Suppress_Discriminant_Checks (E));
2271 end Discriminant_Checks_Suppressed;
2273 --------------------------------
2274 -- Division_Checks_Suppressed --
2275 --------------------------------
2277 function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
2279 return Scope_Suppress.Division_Checks
2280 or else (Present (E) and then Suppress_Division_Checks (E));
2281 end Division_Checks_Suppressed;
2283 -----------------------------------
2284 -- Elaboration_Checks_Suppressed --
2285 -----------------------------------
2287 function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
2289 return Scope_Suppress.Elaboration_Checks
2290 or else (Present (E) and then Suppress_Elaboration_Checks (E));
2291 end Elaboration_Checks_Suppressed;
2293 ------------------------
2294 -- Enable_Range_Check --
2295 ------------------------
2297 procedure Enable_Range_Check (N : Node_Id) is
2299 if Nkind (N) = N_Unchecked_Type_Conversion
2300 and then Kill_Range_Check (N)
2304 Set_Do_Range_Check (N, True);
2306 end Enable_Range_Check;
2312 procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is
2313 Typ : constant Entity_Id := Etype (Expr);
2316 -- Ignore call if we are not doing any validity checking
2318 if not Validity_Checks_On then
2321 -- No check required if expression is from the expander, we assume
2322 -- the expander will generate whatever checks are needed. Note that
2323 -- this is not just an optimization, it avoids infinite recursions!
2325 -- Unchecked conversions must be checked, unless they are initialized
2326 -- scalar values, as in a component assignment in an init_proc.
2328 elsif not Comes_From_Source (Expr)
2329 and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
2330 or else Kill_Range_Check (Expr))
2334 -- No check required if expression is known to have valid value
2336 elsif Expr_Known_Valid (Expr) then
2339 -- No check required if checks off
2341 elsif Range_Checks_Suppressed (Typ) then
2344 -- Ignore case of enumeration with holes where the flag is set not
2345 -- to worry about holes, since no special validity check is needed
2347 elsif Is_Enumeration_Type (Typ)
2348 and then Has_Non_Standard_Rep (Typ)
2353 -- No check required on the left-hand side of an assignment.
2355 elsif Nkind (Parent (Expr)) = N_Assignment_Statement
2356 and then Expr = Name (Parent (Expr))
2360 -- An annoying special case. If this is an out parameter of a scalar
2361 -- type, then the value is not going to be accessed, therefore it is
2362 -- inappropriate to do any validity check at the call site.
2365 -- Only need to worry about scalar types
2367 if Is_Scalar_Type (Typ) then
2377 -- Find actual argument (which may be a parameter association)
2378 -- and the parent of the actual argument (the call statement)
2383 if Nkind (P) = N_Parameter_Association then
2388 -- Only need to worry if we are argument of a procedure
2389 -- call since functions don't have out parameters.
2391 if Nkind (P) = N_Procedure_Call_Statement then
2392 L := Parameter_Associations (P);
2393 E := Entity (Name (P));
2395 -- Only need to worry if there are indeed actuals, and
2396 -- if this could be a procedure call, otherwise we cannot
2397 -- get a match (either we are not an argument, or the
2398 -- mode of the formal is not OUT). This test also filters
2399 -- out the generic case.
2401 if Is_Non_Empty_List (L)
2402 and then Is_Subprogram (E)
2404 -- This is the loop through parameters, looking to
2405 -- see if there is an OUT parameter for which we are
2408 F := First_Formal (E);
2411 while Present (F) loop
2412 if Ekind (F) = E_Out_Parameter and then A = N then
2425 -- If we fall through, a validity check is required. Note that it would
2426 -- not be good to set Do_Range_Check, even in contexts where this is
2427 -- permissible, since this flag causes checking against the target type,
2428 -- not the source type in contexts such as assignments
2430 Insert_Valid_Check (Expr);
2433 ----------------------
2434 -- Expr_Known_Valid --
2435 ----------------------
2437 function Expr_Known_Valid (Expr : Node_Id) return Boolean is
2438 Typ : constant Entity_Id := Etype (Expr);
2441 -- Non-scalar types are always consdered valid, since they never
2442 -- give rise to the issues of erroneous or bounded error behavior
2443 -- that are the concern. In formal reference manual terms the
2444 -- notion of validity only applies to scalar types.
2446 if not Is_Scalar_Type (Typ) then
2449 -- If no validity checking, then everything is considered valid
2451 elsif not Validity_Checks_On then
2454 -- Floating-point types are considered valid unless floating-point
2455 -- validity checks have been specifically turned on.
2457 elsif Is_Floating_Point_Type (Typ)
2458 and then not Validity_Check_Floating_Point
2462 -- If the expression is the value of an object that is known to
2463 -- be valid, then clearly the expression value itself is valid.
2465 elsif Is_Entity_Name (Expr)
2466 and then Is_Known_Valid (Entity (Expr))
2470 -- If the type is one for which all values are known valid, then
2471 -- we are sure that the value is valid except in the slightly odd
2472 -- case where the expression is a reference to a variable whose size
2473 -- has been explicitly set to a value greater than the object size.
2475 elsif Is_Known_Valid (Typ) then
2476 if Is_Entity_Name (Expr)
2477 and then Ekind (Entity (Expr)) = E_Variable
2478 and then Esize (Entity (Expr)) > Esize (Typ)
2485 -- Integer and character literals always have valid values, where
2486 -- appropriate these will be range checked in any case.
2488 elsif Nkind (Expr) = N_Integer_Literal
2490 Nkind (Expr) = N_Character_Literal
2494 -- If we have a type conversion or a qualification of a known valid
2495 -- value, then the result will always be valid.
2497 elsif Nkind (Expr) = N_Type_Conversion
2499 Nkind (Expr) = N_Qualified_Expression
2501 return Expr_Known_Valid (Expression (Expr));
2503 -- The result of any function call or operator is always considered
2504 -- valid, since we assume the necessary checks are done by the call.
2506 elsif Nkind (Expr) in N_Binary_Op
2508 Nkind (Expr) in N_Unary_Op
2510 Nkind (Expr) = N_Function_Call
2514 -- For all other cases, we do not know the expression is valid
2519 end Expr_Known_Valid;
2521 ---------------------
2522 -- Get_Discriminal --
2523 ---------------------
2525 function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
2526 Loc : constant Source_Ptr := Sloc (E);
2531 -- The entity E is the type of a private component of the protected
2532 -- type, or the type of a renaming of that component within a protected
2533 -- operation of that type.
2537 if Ekind (Sc) /= E_Protected_Type then
2540 if Ekind (Sc) /= E_Protected_Type then
2545 D := First_Discriminant (Sc);
2548 and then Chars (D) /= Chars (Bound)
2550 Next_Discriminant (D);
2553 return New_Occurrence_Of (Discriminal (D), Loc);
2554 end Get_Discriminal;
2560 function Guard_Access
2567 if Nkind (Cond) = N_Or_Else then
2568 Set_Paren_Count (Cond, 1);
2571 if Nkind (Ck_Node) = N_Allocator then
2578 Left_Opnd => Duplicate_Subexpr (Ck_Node),
2579 Right_Opnd => Make_Null (Loc)),
2580 Right_Opnd => Cond);
2584 -----------------------------
2585 -- Index_Checks_Suppressed --
2586 -----------------------------
2588 function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
2590 return Scope_Suppress.Index_Checks
2591 or else (Present (E) and then Suppress_Index_Checks (E));
2592 end Index_Checks_Suppressed;
2598 procedure Initialize is
2600 for J in Determine_Range_Cache_N'Range loop
2601 Determine_Range_Cache_N (J) := Empty;
2605 -------------------------
2606 -- Insert_Range_Checks --
2607 -------------------------
2609 procedure Insert_Range_Checks
2610 (Checks : Check_Result;
2612 Suppress_Typ : Entity_Id;
2613 Static_Sloc : Source_Ptr := No_Location;
2614 Flag_Node : Node_Id := Empty;
2615 Do_Before : Boolean := False)
2617 Internal_Flag_Node : Node_Id := Flag_Node;
2618 Internal_Static_Sloc : Source_Ptr := Static_Sloc;
2620 Check_Node : Node_Id;
2621 Checks_On : constant Boolean :=
2622 (not Index_Checks_Suppressed (Suppress_Typ))
2624 (not Range_Checks_Suppressed (Suppress_Typ));
2627 -- For now we just return if Checks_On is false, however this should
2628 -- be enhanced to check for an always True value in the condition
2629 -- and to generate a compilation warning???
2631 if not Expander_Active or else not Checks_On then
2635 if Static_Sloc = No_Location then
2636 Internal_Static_Sloc := Sloc (Node);
2639 if No (Flag_Node) then
2640 Internal_Flag_Node := Node;
2643 for J in 1 .. 2 loop
2644 exit when No (Checks (J));
2646 if Nkind (Checks (J)) = N_Raise_Constraint_Error
2647 and then Present (Condition (Checks (J)))
2649 if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
2650 Check_Node := Checks (J);
2651 Mark_Rewrite_Insertion (Check_Node);
2654 Insert_Before_And_Analyze (Node, Check_Node);
2656 Insert_After_And_Analyze (Node, Check_Node);
2659 Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
2664 Make_Raise_Constraint_Error (Internal_Static_Sloc);
2665 Mark_Rewrite_Insertion (Check_Node);
2668 Insert_Before_And_Analyze (Node, Check_Node);
2670 Insert_After_And_Analyze (Node, Check_Node);
2674 end Insert_Range_Checks;
2676 ------------------------
2677 -- Insert_Valid_Check --
2678 ------------------------
2680 procedure Insert_Valid_Check (Expr : Node_Id) is
2681 Loc : constant Source_Ptr := Sloc (Expr);
2684 -- Do not insert if checks off, or if not checking validity
2686 if Range_Checks_Suppressed (Etype (Expr))
2687 or else (not Validity_Checks_On)
2691 -- Otherwise insert the validity check. Note that we do this with
2692 -- validity checks turned off, to avoid recursion, we do not want
2693 -- validity checks on the validity checking code itself!
2696 Validity_Checks_On := False;
2699 Make_Raise_Constraint_Error (Loc,
2703 Make_Attribute_Reference (Loc,
2705 Duplicate_Subexpr (Expr, Name_Req => True),
2706 Attribute_Name => Name_Valid))),
2707 Suppress => All_Checks);
2708 Validity_Checks_On := True;
2710 end Insert_Valid_Check;
2712 --------------------------
2713 -- Install_Static_Check --
2714 --------------------------
2716 procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
2717 Stat : constant Boolean := Is_Static_Expression (R_Cno);
2718 Typ : constant Entity_Id := Etype (R_Cno);
2721 Rewrite (R_Cno, Make_Raise_Constraint_Error (Loc));
2722 Set_Analyzed (R_Cno);
2723 Set_Etype (R_Cno, Typ);
2724 Set_Raises_Constraint_Error (R_Cno);
2725 Set_Is_Static_Expression (R_Cno, Stat);
2726 end Install_Static_Check;
2728 ------------------------------
2729 -- Length_Checks_Suppressed --
2730 ------------------------------
2732 function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
2734 return Scope_Suppress.Length_Checks
2735 or else (Present (E) and then Suppress_Length_Checks (E));
2736 end Length_Checks_Suppressed;
2738 --------------------------------
2739 -- Overflow_Checks_Suppressed --
2740 --------------------------------
2742 function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
2744 return Scope_Suppress.Overflow_Checks
2745 or else (Present (E) and then Suppress_Overflow_Checks (E));
2746 end Overflow_Checks_Suppressed;
2752 function Range_Check
2754 Target_Typ : Entity_Id;
2755 Source_Typ : Entity_Id := Empty;
2756 Warn_Node : Node_Id := Empty)
2760 return Selected_Range_Checks
2761 (Ck_Node, Target_Typ, Source_Typ, Warn_Node);
2764 -----------------------------
2765 -- Range_Checks_Suppressed --
2766 -----------------------------
2768 function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
2770 -- Note: for now we always suppress range checks on Vax float types,
2771 -- since Gigi does not know how to generate these checks.
2773 return Scope_Suppress.Range_Checks
2774 or else (Present (E) and then Suppress_Range_Checks (E))
2775 or else Vax_Float (E);
2776 end Range_Checks_Suppressed;
2778 ----------------------------
2779 -- Selected_Length_Checks --
2780 ----------------------------
2782 function Selected_Length_Checks
2784 Target_Typ : Entity_Id;
2785 Source_Typ : Entity_Id;
2786 Warn_Node : Node_Id)
2789 Loc : constant Source_Ptr := Sloc (Ck_Node);
2792 Expr_Actual : Node_Id;
2794 Cond : Node_Id := Empty;
2795 Do_Access : Boolean := False;
2796 Wnode : Node_Id := Warn_Node;
2797 Ret_Result : Check_Result := (Empty, Empty);
2798 Num_Checks : Natural := 0;
2800 procedure Add_Check (N : Node_Id);
2801 -- Adds the action given to Ret_Result if N is non-Empty
2803 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
2804 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
2806 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
2807 -- True for equal literals and for nodes that denote the same constant
2808 -- entity, even if its value is not a static constant. This removes
2809 -- some obviously superfluous checks.
2811 function Length_E_Cond
2812 (Exptyp : Entity_Id;
2816 -- Returns expression to compute:
2817 -- Typ'Length /= Exptyp'Length
2819 function Length_N_Cond
2824 -- Returns expression to compute:
2825 -- Typ'Length /= Expr'Length
2831 procedure Add_Check (N : Node_Id) is
2835 -- For now, ignore attempt to place more than 2 checks ???
2837 if Num_Checks = 2 then
2841 pragma Assert (Num_Checks <= 1);
2842 Num_Checks := Num_Checks + 1;
2843 Ret_Result (Num_Checks) := N;
2851 function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
2853 E1 : Entity_Id := E;
2854 Pt : Entity_Id := Scope (Scope (E));
2857 if Ekind (Scope (E)) = E_Record_Type
2858 and then Has_Discriminants (Scope (E))
2860 N := Build_Discriminal_Subtype_Of_Component (E);
2863 Insert_Action (Ck_Node, N);
2864 E1 := Defining_Identifier (N);
2868 if Ekind (E1) = E_String_Literal_Subtype then
2870 Make_Integer_Literal (Loc,
2871 Intval => String_Literal_Length (E1));
2873 elsif Ekind (Pt) = E_Protected_Type
2874 and then Has_Discriminants (Pt)
2875 and then Has_Completion (Pt)
2876 and then not Inside_Init_Proc
2879 -- If the type whose length is needed is a private component
2880 -- constrained by a discriminant, we must expand the 'Length
2881 -- attribute into an explicit computation, using the discriminal
2882 -- of the current protected operation. This is because the actual
2883 -- type of the prival is constructed after the protected opera-
2884 -- tion has been fully expanded.
2887 Indx_Type : Node_Id;
2890 Do_Expand : Boolean := False;
2893 Indx_Type := First_Index (E);
2895 for J in 1 .. Indx - 1 loop
2896 Next_Index (Indx_Type);
2899 Get_Index_Bounds (Indx_Type, Lo, Hi);
2901 if Nkind (Lo) = N_Identifier
2902 and then Ekind (Entity (Lo)) = E_In_Parameter
2904 Lo := Get_Discriminal (E, Lo);
2908 if Nkind (Hi) = N_Identifier
2909 and then Ekind (Entity (Hi)) = E_In_Parameter
2911 Hi := Get_Discriminal (E, Hi);
2916 if not Is_Entity_Name (Lo) then
2917 Lo := Duplicate_Subexpr (Lo);
2920 if not Is_Entity_Name (Hi) then
2921 Lo := Duplicate_Subexpr (Hi);
2927 Make_Op_Subtract (Loc,
2931 Right_Opnd => Make_Integer_Literal (Loc, 1));
2936 Make_Attribute_Reference (Loc,
2937 Attribute_Name => Name_Length,
2939 New_Occurrence_Of (E1, Loc));
2942 Set_Expressions (N, New_List (
2943 Make_Integer_Literal (Loc, Indx)));
2952 Make_Attribute_Reference (Loc,
2953 Attribute_Name => Name_Length,
2955 New_Occurrence_Of (E1, Loc));
2958 Set_Expressions (N, New_List (
2959 Make_Integer_Literal (Loc, Indx)));
2971 function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
2974 Make_Attribute_Reference (Loc,
2975 Attribute_Name => Name_Length,
2977 Duplicate_Subexpr (N, Name_Req => True),
2978 Expressions => New_List (
2979 Make_Integer_Literal (Loc, Indx)));
2987 function Length_E_Cond
2988 (Exptyp : Entity_Id;
2996 Left_Opnd => Get_E_Length (Typ, Indx),
2997 Right_Opnd => Get_E_Length (Exptyp, Indx));
3005 function Length_N_Cond
3014 Left_Opnd => Get_E_Length (Typ, Indx),
3015 Right_Opnd => Get_N_Length (Expr, Indx));
3019 function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
3022 (Nkind (L) = N_Integer_Literal
3023 and then Nkind (R) = N_Integer_Literal
3024 and then Intval (L) = Intval (R))
3028 and then Ekind (Entity (L)) = E_Constant
3029 and then ((Is_Entity_Name (R)
3030 and then Entity (L) = Entity (R))
3032 (Nkind (R) = N_Type_Conversion
3033 and then Is_Entity_Name (Expression (R))
3034 and then Entity (L) = Entity (Expression (R)))))
3038 and then Ekind (Entity (R)) = E_Constant
3039 and then Nkind (L) = N_Type_Conversion
3040 and then Is_Entity_Name (Expression (L))
3041 and then Entity (R) = Entity (Expression (L)));
3044 -- Start of processing for Selected_Length_Checks
3047 if not Expander_Active then
3051 if Target_Typ = Any_Type
3052 or else Target_Typ = Any_Composite
3053 or else Raises_Constraint_Error (Ck_Node)
3062 T_Typ := Target_Typ;
3064 if No (Source_Typ) then
3065 S_Typ := Etype (Ck_Node);
3067 S_Typ := Source_Typ;
3070 if S_Typ = Any_Type or else S_Typ = Any_Composite then
3074 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
3075 S_Typ := Designated_Type (S_Typ);
3076 T_Typ := Designated_Type (T_Typ);
3079 -- A simple optimization
3081 if Nkind (Ck_Node) = N_Null then
3086 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
3087 if Is_Constrained (T_Typ) then
3089 -- The checking code to be generated will freeze the
3090 -- corresponding array type. However, we must freeze the
3091 -- type now, so that the freeze node does not appear within
3092 -- the generated condional expression, but ahead of it.
3094 Freeze_Before (Ck_Node, T_Typ);
3096 Expr_Actual := Get_Referenced_Object (Ck_Node);
3097 Exptyp := Get_Actual_Subtype (Expr_Actual);
3099 if Is_Access_Type (Exptyp) then
3100 Exptyp := Designated_Type (Exptyp);
3103 -- String_Literal case. This needs to be handled specially be-
3104 -- cause no index types are available for string literals. The
3105 -- condition is simply:
3107 -- T_Typ'Length = string-literal-length
3109 if Nkind (Expr_Actual) = N_String_Literal then
3112 Left_Opnd => Get_E_Length (T_Typ, 1),
3114 Make_Integer_Literal (Loc,
3116 String_Literal_Length (Etype (Expr_Actual))));
3118 -- General array case. Here we have a usable actual subtype for
3119 -- the expression, and the condition is built from the two types
3122 -- T_Typ'Length /= Exptyp'Length or else
3123 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
3124 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
3127 elsif Is_Constrained (Exptyp) then
3131 Ndims : Nat := Number_Dimensions (T_Typ);
3142 L_Index := First_Index (T_Typ);
3143 R_Index := First_Index (Exptyp);
3145 for Indx in 1 .. Ndims loop
3146 if not (Nkind (L_Index) = N_Raise_Constraint_Error
3147 or else Nkind (R_Index) = N_Raise_Constraint_Error)
3149 Get_Index_Bounds (L_Index, L_Low, L_High);
3150 Get_Index_Bounds (R_Index, R_Low, R_High);
3152 -- Deal with compile time length check. Note that we
3153 -- skip this in the access case, because the access
3154 -- value may be null, so we cannot know statically.
3157 and then Compile_Time_Known_Value (L_Low)
3158 and then Compile_Time_Known_Value (L_High)
3159 and then Compile_Time_Known_Value (R_Low)
3160 and then Compile_Time_Known_Value (R_High)
3162 if Expr_Value (L_High) >= Expr_Value (L_Low) then
3163 L_Length := Expr_Value (L_High) -
3164 Expr_Value (L_Low) + 1;
3166 L_Length := UI_From_Int (0);
3169 if Expr_Value (R_High) >= Expr_Value (R_Low) then
3170 R_Length := Expr_Value (R_High) -
3171 Expr_Value (R_Low) + 1;
3173 R_Length := UI_From_Int (0);
3176 if L_Length > R_Length then
3178 (Compile_Time_Constraint_Error
3179 (Wnode, "too few elements for}?", T_Typ));
3181 elsif L_Length < R_Length then
3183 (Compile_Time_Constraint_Error
3184 (Wnode, "too many elements for}?", T_Typ));
3187 -- The comparison for an individual index subtype
3188 -- is omitted if the corresponding index subtypes
3189 -- statically match, since the result is known to
3190 -- be true. Note that this test is worth while even
3191 -- though we do static evaluation, because non-static
3192 -- subtypes can statically match.
3195 Subtypes_Statically_Match
3196 (Etype (L_Index), Etype (R_Index))
3199 (Same_Bounds (L_Low, R_Low)
3200 and then Same_Bounds (L_High, R_High))
3203 (Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
3212 -- Handle cases where we do not get a usable actual subtype that
3213 -- is constrained. This happens for example in the function call
3214 -- and explicit dereference cases. In these cases, we have to get
3215 -- the length or range from the expression itself, making sure we
3216 -- do not evaluate it more than once.
3218 -- Here Ck_Node is the original expression, or more properly the
3219 -- result of applying Duplicate_Expr to the original tree,
3220 -- forcing the result to be a name.
3224 Ndims : Nat := Number_Dimensions (T_Typ);
3227 -- Build the condition for the explicit dereference case
3229 for Indx in 1 .. Ndims loop
3231 (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
3238 -- Construct the test and insert into the tree
3240 if Present (Cond) then
3242 Cond := Guard_Access (Cond, Loc, Ck_Node);
3245 Add_Check (Make_Raise_Constraint_Error (Loc, Condition => Cond));
3250 end Selected_Length_Checks;
3252 ---------------------------
3253 -- Selected_Range_Checks --
3254 ---------------------------
3256 function Selected_Range_Checks
3258 Target_Typ : Entity_Id;
3259 Source_Typ : Entity_Id;
3260 Warn_Node : Node_Id)
3263 Loc : constant Source_Ptr := Sloc (Ck_Node);
3266 Expr_Actual : Node_Id;
3268 Cond : Node_Id := Empty;
3269 Do_Access : Boolean := False;
3270 Wnode : Node_Id := Warn_Node;
3271 Ret_Result : Check_Result := (Empty, Empty);
3272 Num_Checks : Integer := 0;
3274 procedure Add_Check (N : Node_Id);
3275 -- Adds the action given to Ret_Result if N is non-Empty
3277 function Discrete_Range_Cond
3281 -- Returns expression to compute:
3282 -- Low_Bound (Expr) < Typ'First
3284 -- High_Bound (Expr) > Typ'Last
3286 function Discrete_Expr_Cond
3290 -- Returns expression to compute:
3295 function Get_E_First_Or_Last
3300 -- Returns expression to compute:
3301 -- E'First or E'Last
3303 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
3304 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
3305 -- Returns expression to compute:
3306 -- N'First or N'Last using Duplicate_Subexpr
3308 function Range_E_Cond
3309 (Exptyp : Entity_Id;
3313 -- Returns expression to compute:
3314 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
3316 function Range_Equal_E_Cond
3317 (Exptyp : Entity_Id;
3321 -- Returns expression to compute:
3322 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
3324 function Range_N_Cond
3329 -- Return expression to compute:
3330 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
3336 procedure Add_Check (N : Node_Id) is
3340 -- For now, ignore attempt to place more than 2 checks ???
3342 if Num_Checks = 2 then
3346 pragma Assert (Num_Checks <= 1);
3347 Num_Checks := Num_Checks + 1;
3348 Ret_Result (Num_Checks) := N;
3352 -------------------------
3353 -- Discrete_Expr_Cond --
3354 -------------------------
3356 function Discrete_Expr_Cond
3367 Convert_To (Base_Type (Typ), Duplicate_Subexpr (Expr)),
3369 Convert_To (Base_Type (Typ),
3370 Get_E_First_Or_Last (Typ, 0, Name_First))),
3375 Convert_To (Base_Type (Typ), Duplicate_Subexpr (Expr)),
3379 Get_E_First_Or_Last (Typ, 0, Name_Last))));
3380 end Discrete_Expr_Cond;
3382 -------------------------
3383 -- Discrete_Range_Cond --
3384 -------------------------
3386 function Discrete_Range_Cond
3391 LB : Node_Id := Low_Bound (Expr);
3392 HB : Node_Id := High_Bound (Expr);
3394 Left_Opnd : Node_Id;
3395 Right_Opnd : Node_Id;
3398 if Nkind (LB) = N_Identifier
3399 and then Ekind (Entity (LB)) = E_Discriminant then
3400 LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
3403 if Nkind (HB) = N_Identifier
3404 and then Ekind (Entity (HB)) = E_Discriminant then
3405 HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
3412 (Base_Type (Typ), Duplicate_Subexpr (LB)),
3416 (Base_Type (Typ), Get_E_First_Or_Last (Typ, 0, Name_First)));
3418 if Base_Type (Typ) = Typ then
3421 elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ)))
3423 Compile_Time_Known_Value (High_Bound (Scalar_Range
3426 if Is_Floating_Point_Type (Typ) then
3427 if Expr_Value_R (High_Bound (Scalar_Range (Typ))) =
3428 Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ))))
3434 if Expr_Value (High_Bound (Scalar_Range (Typ))) =
3435 Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ))))
3446 (Base_Type (Typ), Duplicate_Subexpr (HB)),
3451 Get_E_First_Or_Last (Typ, 0, Name_Last)));
3453 return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
3454 end Discrete_Range_Cond;
3456 -------------------------
3457 -- Get_E_First_Or_Last --
3458 -------------------------
3460 function Get_E_First_Or_Last
3472 if Is_Array_Type (E) then
3473 N := First_Index (E);
3475 for J in 2 .. Indx loop
3480 N := Scalar_Range (E);
3483 if Nkind (N) = N_Subtype_Indication then
3484 LB := Low_Bound (Range_Expression (Constraint (N)));
3485 HB := High_Bound (Range_Expression (Constraint (N)));
3487 elsif Is_Entity_Name (N) then
3488 LB := Type_Low_Bound (Etype (N));
3489 HB := Type_High_Bound (Etype (N));
3492 LB := Low_Bound (N);
3493 HB := High_Bound (N);
3496 if Nam = Name_First then
3502 if Nkind (Bound) = N_Identifier
3503 and then Ekind (Entity (Bound)) = E_Discriminant
3505 return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
3507 elsif Nkind (Bound) = N_Identifier
3508 and then Ekind (Entity (Bound)) = E_In_Parameter
3509 and then not Inside_Init_Proc
3511 return Get_Discriminal (E, Bound);
3513 elsif Nkind (Bound) = N_Integer_Literal then
3514 return Make_Integer_Literal (Loc, Intval (Bound));
3517 return Duplicate_Subexpr (Bound);
3519 end Get_E_First_Or_Last;
3525 function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
3528 Make_Attribute_Reference (Loc,
3529 Attribute_Name => Name_First,
3531 Duplicate_Subexpr (N, Name_Req => True),
3532 Expressions => New_List (
3533 Make_Integer_Literal (Loc, Indx)));
3541 function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
3544 Make_Attribute_Reference (Loc,
3545 Attribute_Name => Name_Last,
3547 Duplicate_Subexpr (N, Name_Req => True),
3548 Expressions => New_List (
3549 Make_Integer_Literal (Loc, Indx)));
3557 function Range_E_Cond
3558 (Exptyp : Entity_Id;
3568 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
3569 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3573 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
3574 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3578 ------------------------
3579 -- Range_Equal_E_Cond --
3580 ------------------------
3582 function Range_Equal_E_Cond
3583 (Exptyp : Entity_Id;
3593 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
3594 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3597 Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
3598 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3599 end Range_Equal_E_Cond;
3605 function Range_N_Cond
3616 Left_Opnd => Get_N_First (Expr, Indx),
3617 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
3621 Left_Opnd => Get_N_Last (Expr, Indx),
3622 Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
3625 -- Start of processing for Selected_Range_Checks
3628 if not Expander_Active then
3632 if Target_Typ = Any_Type
3633 or else Target_Typ = Any_Composite
3634 or else Raises_Constraint_Error (Ck_Node)
3643 T_Typ := Target_Typ;
3645 if No (Source_Typ) then
3646 S_Typ := Etype (Ck_Node);
3648 S_Typ := Source_Typ;
3651 if S_Typ = Any_Type or else S_Typ = Any_Composite then
3655 -- The order of evaluating T_Typ before S_Typ seems to be critical
3656 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
3657 -- in, and since Node can be an N_Range node, it might be invalid.
3658 -- Should there be an assert check somewhere for taking the Etype of
3659 -- an N_Range node ???
3661 if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
3662 S_Typ := Designated_Type (S_Typ);
3663 T_Typ := Designated_Type (T_Typ);
3666 -- A simple optimization
3668 if Nkind (Ck_Node) = N_Null then
3673 -- For an N_Range Node, check for a null range and then if not
3674 -- null generate a range check action.
3676 if Nkind (Ck_Node) = N_Range then
3678 -- There's no point in checking a range against itself
3680 if Ck_Node = Scalar_Range (T_Typ) then
3685 T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
3686 T_HB : constant Node_Id := Type_High_Bound (T_Typ);
3687 LB : constant Node_Id := Low_Bound (Ck_Node);
3688 HB : constant Node_Id := High_Bound (Ck_Node);
3689 Null_Range : Boolean;
3691 Out_Of_Range_L : Boolean;
3692 Out_Of_Range_H : Boolean;
3695 -- Check for case where everything is static and we can
3696 -- do the check at compile time. This is skipped if we
3697 -- have an access type, since the access value may be null.
3699 -- ??? This code can be improved since you only need to know
3700 -- that the two respective bounds (LB & T_LB or HB & T_HB)
3701 -- are known at compile time to emit pertinent messages.
3703 if Compile_Time_Known_Value (LB)
3704 and then Compile_Time_Known_Value (HB)
3705 and then Compile_Time_Known_Value (T_LB)
3706 and then Compile_Time_Known_Value (T_HB)
3707 and then not Do_Access
3709 -- Floating-point case
3711 if Is_Floating_Point_Type (S_Typ) then
3712 Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
3714 (Expr_Value_R (LB) < Expr_Value_R (T_LB))
3716 (Expr_Value_R (LB) > Expr_Value_R (T_HB));
3719 (Expr_Value_R (HB) > Expr_Value_R (T_HB))
3721 (Expr_Value_R (HB) < Expr_Value_R (T_LB));
3723 -- Fixed or discrete type case
3726 Null_Range := Expr_Value (HB) < Expr_Value (LB);
3728 (Expr_Value (LB) < Expr_Value (T_LB))
3730 (Expr_Value (LB) > Expr_Value (T_HB));
3733 (Expr_Value (HB) > Expr_Value (T_HB))
3735 (Expr_Value (HB) < Expr_Value (T_LB));
3738 if not Null_Range then
3739 if Out_Of_Range_L then
3740 if No (Warn_Node) then
3742 (Compile_Time_Constraint_Error
3743 (Low_Bound (Ck_Node),
3744 "static value out of range of}?", T_Typ));
3748 (Compile_Time_Constraint_Error
3750 "static range out of bounds of}?", T_Typ));
3754 if Out_Of_Range_H then
3755 if No (Warn_Node) then
3757 (Compile_Time_Constraint_Error
3758 (High_Bound (Ck_Node),
3759 "static value out of range of}?", T_Typ));
3763 (Compile_Time_Constraint_Error
3765 "static range out of bounds of}?", T_Typ));
3773 LB : Node_Id := Low_Bound (Ck_Node);
3774 HB : Node_Id := High_Bound (Ck_Node);
3778 -- If either bound is a discriminant and we are within
3779 -- the record declaration, it is a use of the discriminant
3780 -- in a constraint of a component, and nothing can be
3781 -- checked here. The check will be emitted within the
3782 -- init_proc. Before then, the discriminal has no real
3785 if Nkind (LB) = N_Identifier
3786 and then Ekind (Entity (LB)) = E_Discriminant
3788 if Current_Scope = Scope (Entity (LB)) then
3792 New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
3796 if Nkind (HB) = N_Identifier
3797 and then Ekind (Entity (HB)) = E_Discriminant
3799 if Current_Scope = Scope (Entity (HB)) then
3803 New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
3807 Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
3808 Set_Paren_Count (Cond, 1);
3814 Left_Opnd => Duplicate_Subexpr (HB),
3815 Right_Opnd => Duplicate_Subexpr (LB)),
3816 Right_Opnd => Cond);
3822 elsif Is_Scalar_Type (S_Typ) then
3824 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
3825 -- except the above simply sets a flag in the node and lets
3826 -- gigi generate the check base on the Etype of the expression.
3827 -- Sometimes, however we want to do a dynamic check against an
3828 -- arbitrary target type, so we do that here.
3830 if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
3831 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
3833 -- For literals, we can tell if the constraint error will be
3834 -- raised at compile time, so we never need a dynamic check, but
3835 -- if the exception will be raised, then post the usual warning,
3836 -- and replace the literal with a raise constraint error
3837 -- expression. As usual, skip this for access types
3839 elsif Compile_Time_Known_Value (Ck_Node)
3840 and then not Do_Access
3843 LB : constant Node_Id := Type_Low_Bound (T_Typ);
3844 UB : constant Node_Id := Type_High_Bound (T_Typ);
3846 Out_Of_Range : Boolean;
3847 Static_Bounds : constant Boolean :=
3848 Compile_Time_Known_Value (LB)
3849 and Compile_Time_Known_Value (UB);
3852 -- Following range tests should use Sem_Eval routine ???
3854 if Static_Bounds then
3855 if Is_Floating_Point_Type (S_Typ) then
3857 (Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
3859 (Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
3861 else -- fixed or discrete type
3863 Expr_Value (Ck_Node) < Expr_Value (LB)
3865 Expr_Value (Ck_Node) > Expr_Value (UB);
3868 -- Bounds of the type are static and the literal is
3869 -- out of range so make a warning message.
3871 if Out_Of_Range then
3872 if No (Warn_Node) then
3874 (Compile_Time_Constraint_Error
3876 "static value out of range of}?", T_Typ));
3880 (Compile_Time_Constraint_Error
3882 "static value out of range of}?", T_Typ));
3887 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
3891 -- Here for the case of a non-static expression, we need a runtime
3892 -- check unless the source type range is guaranteed to be in the
3893 -- range of the target type.
3896 if not In_Subrange_Of (S_Typ, T_Typ) then
3897 Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
3902 if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
3903 if Is_Constrained (T_Typ) then
3905 Expr_Actual := Get_Referenced_Object (Ck_Node);
3906 Exptyp := Get_Actual_Subtype (Expr_Actual);
3908 if Is_Access_Type (Exptyp) then
3909 Exptyp := Designated_Type (Exptyp);
3912 -- String_Literal case. This needs to be handled specially be-
3913 -- cause no index types are available for string literals. The
3914 -- condition is simply:
3916 -- T_Typ'Length = string-literal-length
3918 if Nkind (Expr_Actual) = N_String_Literal then
3921 -- General array case. Here we have a usable actual subtype for
3922 -- the expression, and the condition is built from the two types
3924 -- T_Typ'First < Exptyp'First or else
3925 -- T_Typ'Last > Exptyp'Last or else
3926 -- T_Typ'First(1) < Exptyp'First(1) or else
3927 -- T_Typ'Last(1) > Exptyp'Last(1) or else
3930 elsif Is_Constrained (Exptyp) then
3934 Ndims : Nat := Number_Dimensions (T_Typ);
3942 L_Index := First_Index (T_Typ);
3943 R_Index := First_Index (Exptyp);
3945 for Indx in 1 .. Ndims loop
3946 if not (Nkind (L_Index) = N_Raise_Constraint_Error
3947 or else Nkind (R_Index) = N_Raise_Constraint_Error)
3949 Get_Index_Bounds (L_Index, L_Low, L_High);
3950 Get_Index_Bounds (R_Index, R_Low, R_High);
3952 -- Deal with compile time length check. Note that we
3953 -- skip this in the access case, because the access
3954 -- value may be null, so we cannot know statically.
3957 Subtypes_Statically_Match
3958 (Etype (L_Index), Etype (R_Index))
3960 -- If the target type is constrained then we
3961 -- have to check for exact equality of bounds
3962 -- (required for qualified expressions).
3964 if Is_Constrained (T_Typ) then
3967 Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
3971 (Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
3982 -- Handle cases where we do not get a usable actual subtype that
3983 -- is constrained. This happens for example in the function call
3984 -- and explicit dereference cases. In these cases, we have to get
3985 -- the length or range from the expression itself, making sure we
3986 -- do not evaluate it more than once.
3988 -- Here Ck_Node is the original expression, or more properly the
3989 -- result of applying Duplicate_Expr to the original tree,
3990 -- forcing the result to be a name.
3994 Ndims : Nat := Number_Dimensions (T_Typ);
3997 -- Build the condition for the explicit dereference case
3999 for Indx in 1 .. Ndims loop
4001 (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
4008 -- Generate an Action to check that the bounds of the
4009 -- source value are within the constraints imposed by the
4010 -- target type for a conversion to an unconstrained type.
4013 if Nkind (Parent (Ck_Node)) = N_Type_Conversion then
4015 Opnd_Index : Node_Id;
4016 Targ_Index : Node_Id;
4020 := First_Index (Get_Actual_Subtype (Ck_Node));
4021 Targ_Index := First_Index (T_Typ);
4023 while Opnd_Index /= Empty loop
4024 if Nkind (Opnd_Index) = N_Range then
4026 (Low_Bound (Opnd_Index), Etype (Targ_Index))
4029 (High_Bound (Opnd_Index), Etype (Targ_Index))
4033 elsif Is_Out_Of_Range
4034 (Low_Bound (Opnd_Index), Etype (Targ_Index))
4037 (High_Bound (Opnd_Index), Etype (Targ_Index))
4040 (Compile_Time_Constraint_Error
4041 (Wnode, "value out of range of}?", T_Typ));
4047 (Opnd_Index, Etype (Targ_Index)));
4051 Next_Index (Opnd_Index);
4052 Next_Index (Targ_Index);
4059 -- Construct the test and insert into the tree
4061 if Present (Cond) then
4063 Cond := Guard_Access (Cond, Loc, Ck_Node);
4066 Add_Check (Make_Raise_Constraint_Error (Loc, Condition => Cond));
4071 end Selected_Range_Checks;
4073 -------------------------------
4074 -- Storage_Checks_Suppressed --
4075 -------------------------------
4077 function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
4079 return Scope_Suppress.Storage_Checks
4080 or else (Present (E) and then Suppress_Storage_Checks (E));
4081 end Storage_Checks_Suppressed;
4083 ---------------------------
4084 -- Tag_Checks_Suppressed --
4085 ---------------------------
4087 function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
4089 return Scope_Suppress.Tag_Checks
4090 or else (Present (E) and then Suppress_Tag_Checks (E));
4091 end Tag_Checks_Suppressed;