1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
10 -- Copyright (C) 1992-2002 Free Software Foundation, Inc. --
12 -- GNAT is free software; you can redistribute it and/or modify it under --
13 -- terms of the GNU General Public License as published by the Free Soft- --
14 -- ware Foundation; either version 2, or (at your option) any later ver- --
15 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
18 -- for more details. You should have received a copy of the GNU General --
19 -- Public License distributed with GNAT; see file COPYING. If not, write --
20 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
21 -- MA 02111-1307, USA. --
23 -- GNAT was originally developed by the GNAT team at New York University. --
24 -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
26 ------------------------------------------------------------------------------
28 with Atree
; use Atree
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Errout
; use Errout
;
32 with Exp_Ch2
; use Exp_Ch2
;
33 with Exp_Util
; use Exp_Util
;
34 with Elists
; use Elists
;
35 with Freeze
; use Freeze
;
36 with Nlists
; use Nlists
;
37 with Nmake
; use Nmake
;
39 with Restrict
; use Restrict
;
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 Targparm
; use Targparm
;
50 with Tbuild
; use Tbuild
;
51 with Ttypes
; use Ttypes
;
52 with Urealp
; use Urealp
;
53 with Validsw
; use Validsw
;
55 package body Checks
is
57 -- General note: many of these routines are concerned with generating
58 -- checking code to make sure that constraint error is raised at runtime.
59 -- Clearly this code is only needed if the expander is active, since
60 -- otherwise we will not be generating code or going into the runtime
63 -- We therefore disconnect most of these checks if the expander is
64 -- inactive. This has the additional benefit that we do not need to
65 -- worry about the tree being messed up by previous errors (since errors
66 -- turn off expansion anyway).
68 -- There are a few exceptions to the above rule. For instance routines
69 -- such as Apply_Scalar_Range_Check that do not insert any code can be
70 -- safely called even when the Expander is inactive (but Errors_Detected
71 -- is 0). The benefit of executing this code when expansion is off, is
72 -- the ability to emit constraint error warning for static expressions
73 -- even when we are not generating code.
75 ----------------------------
76 -- Local Subprogram Specs --
77 ----------------------------
79 procedure Apply_Selected_Length_Checks
81 Target_Typ
: Entity_Id
;
82 Source_Typ
: Entity_Id
;
84 -- This is the subprogram that does all the work for Apply_Length_Check
85 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
86 -- described for the above routines. The Do_Static flag indicates that
87 -- only a static check is to be done.
89 procedure Apply_Selected_Range_Checks
91 Target_Typ
: Entity_Id
;
92 Source_Typ
: Entity_Id
;
94 -- This is the subprogram that does all the work for Apply_Range_Check.
95 -- Expr, Target_Typ and Source_Typ are as described for the above
96 -- routine. The Do_Static flag indicates that only a static check is
99 function Get_Discriminal
(E
: Entity_Id
; Bound
: Node_Id
) return Node_Id
;
100 -- If a discriminal is used in constraining a prival, Return reference
101 -- to the discriminal of the protected body (which renames the parameter
102 -- of the enclosing protected operation). This clumsy transformation is
103 -- needed because privals are created too late and their actual subtypes
104 -- are not available when analysing the bodies of the protected operations.
105 -- To be cleaned up???
107 function Guard_Access
112 -- In the access type case, guard the test with a test to ensure
113 -- that the access value is non-null, since the checks do not
114 -- not apply to null access values.
116 procedure Install_Static_Check
(R_Cno
: Node_Id
; Loc
: Source_Ptr
);
117 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
118 -- Constraint_Error node.
120 function Selected_Length_Checks
122 Target_Typ
: Entity_Id
;
123 Source_Typ
: Entity_Id
;
126 -- Like Apply_Selected_Length_Checks, except it doesn't modify
127 -- anything, just returns a list of nodes as described in the spec of
128 -- this package for the Range_Check function.
130 function Selected_Range_Checks
132 Target_Typ
: Entity_Id
;
133 Source_Typ
: Entity_Id
;
136 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
137 -- just returns a list of nodes as described in the spec of this package
138 -- for the Range_Check function.
140 ------------------------------
141 -- Access_Checks_Suppressed --
142 ------------------------------
144 function Access_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
146 return Scope_Suppress
.Access_Checks
147 or else (Present
(E
) and then Suppress_Access_Checks
(E
));
148 end Access_Checks_Suppressed
;
150 -------------------------------------
151 -- Accessibility_Checks_Suppressed --
152 -------------------------------------
154 function Accessibility_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
156 return Scope_Suppress
.Accessibility_Checks
157 or else (Present
(E
) and then Suppress_Accessibility_Checks
(E
));
158 end Accessibility_Checks_Suppressed
;
160 -------------------------
161 -- Append_Range_Checks --
162 -------------------------
164 procedure Append_Range_Checks
165 (Checks
: Check_Result
;
167 Suppress_Typ
: Entity_Id
;
168 Static_Sloc
: Source_Ptr
;
171 Internal_Flag_Node
: Node_Id
:= Flag_Node
;
172 Internal_Static_Sloc
: Source_Ptr
:= Static_Sloc
;
173 Checks_On
: constant Boolean :=
174 (not Index_Checks_Suppressed
(Suppress_Typ
))
176 (not Range_Checks_Suppressed
(Suppress_Typ
));
179 -- For now we just return if Checks_On is false, however this should
180 -- be enhanced to check for an always True value in the condition
181 -- and to generate a compilation warning???
183 if not Checks_On
then
188 exit when No
(Checks
(J
));
190 if Nkind
(Checks
(J
)) = N_Raise_Constraint_Error
191 and then Present
(Condition
(Checks
(J
)))
193 if not Has_Dynamic_Range_Check
(Internal_Flag_Node
) then
194 Append_To
(Stmts
, Checks
(J
));
195 Set_Has_Dynamic_Range_Check
(Internal_Flag_Node
);
201 Make_Raise_Constraint_Error
(Internal_Static_Sloc
,
202 Reason
=> CE_Range_Check_Failed
));
205 end Append_Range_Checks
;
207 ------------------------
208 -- Apply_Access_Check --
209 ------------------------
211 procedure Apply_Access_Check
(N
: Node_Id
) is
212 P
: constant Node_Id
:= Prefix
(N
);
215 if Inside_A_Generic
then
219 if Is_Entity_Name
(P
) then
220 Check_Unset_Reference
(P
);
223 if Is_Entity_Name
(P
)
224 and then Access_Checks_Suppressed
(Entity
(P
))
228 elsif Access_Checks_Suppressed
(Etype
(P
)) then
232 Set_Do_Access_Check
(N
, True);
234 end Apply_Access_Check
;
236 -------------------------------
237 -- Apply_Accessibility_Check --
238 -------------------------------
240 procedure Apply_Accessibility_Check
(N
: Node_Id
; Typ
: Entity_Id
) is
241 Loc
: constant Source_Ptr
:= Sloc
(N
);
242 Param_Ent
: constant Entity_Id
:= Param_Entity
(N
);
243 Param_Level
: Node_Id
;
244 Type_Level
: Node_Id
;
247 if Inside_A_Generic
then
250 -- Only apply the run-time check if the access parameter
251 -- has an associated extra access level parameter and
252 -- when the level of the type is less deep than the level
253 -- of the access parameter.
255 elsif Present
(Param_Ent
)
256 and then Present
(Extra_Accessibility
(Param_Ent
))
257 and then UI_Gt
(Object_Access_Level
(N
),
258 Type_Access_Level
(Typ
))
259 and then not Accessibility_Checks_Suppressed
(Param_Ent
)
260 and then not Accessibility_Checks_Suppressed
(Typ
)
263 New_Occurrence_Of
(Extra_Accessibility
(Param_Ent
), Loc
);
266 Make_Integer_Literal
(Loc
, Type_Access_Level
(Typ
));
268 -- Raise Program_Error if the accessibility level of the
269 -- the access parameter is deeper than the level of the
270 -- target access type.
273 Make_Raise_Program_Error
(Loc
,
276 Left_Opnd
=> Param_Level
,
277 Right_Opnd
=> Type_Level
),
278 Reason
=> PE_Accessibility_Check_Failed
));
280 Analyze_And_Resolve
(N
);
282 end Apply_Accessibility_Check
;
284 ---------------------------
285 -- Apply_Alignment_Check --
286 ---------------------------
288 procedure Apply_Alignment_Check
(E
: Entity_Id
; N
: Node_Id
) is
289 AC
: constant Node_Id
:= Address_Clause
(E
);
294 if No
(AC
) or else Range_Checks_Suppressed
(E
) then
299 Expr
:= Expression
(AC
);
301 if Nkind
(Expr
) = N_Unchecked_Type_Conversion
then
302 Expr
:= Expression
(Expr
);
304 elsif Nkind
(Expr
) = N_Function_Call
305 and then Is_RTE
(Entity
(Name
(Expr
)), RE_To_Address
)
307 Expr
:= First
(Parameter_Associations
(Expr
));
309 if Nkind
(Expr
) = N_Parameter_Association
then
310 Expr
:= Explicit_Actual_Parameter
(Expr
);
314 -- Here Expr is the address value. See if we know that the
315 -- value is unacceptable at compile time.
317 if Compile_Time_Known_Value
(Expr
)
318 and then Known_Alignment
(E
)
320 if Expr_Value
(Expr
) mod Alignment
(E
) /= 0 then
322 Make_Raise_Program_Error
(Loc
,
323 Reason
=> PE_Misaligned_Address_Value
));
325 ("?specified address for& not " &
326 "consistent with alignment", Expr
, E
);
329 -- Here we do not know if the value is acceptable, generate
330 -- code to raise PE if alignment is inappropriate.
333 -- Skip generation of this code if we don't want elab code
335 if not Restrictions
(No_Elaboration_Code
) then
336 Insert_After_And_Analyze
(N
,
337 Make_Raise_Program_Error
(Loc
,
344 (RTE
(RE_Integer_Address
),
345 Duplicate_Subexpr
(Expr
)),
347 Make_Attribute_Reference
(Loc
,
348 Prefix
=> New_Occurrence_Of
(E
, Loc
),
349 Attribute_Name
=> Name_Alignment
)),
350 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
351 Reason
=> PE_Misaligned_Address_Value
),
352 Suppress
=> All_Checks
);
357 end Apply_Alignment_Check
;
359 -------------------------------------
360 -- Apply_Arithmetic_Overflow_Check --
361 -------------------------------------
363 -- This routine is called only if the type is an integer type, and
364 -- a software arithmetic overflow check must be performed for op
365 -- (add, subtract, multiply). The check is performed only if
366 -- Software_Overflow_Checking is enabled and Do_Overflow_Check
367 -- is set. In this case we expand the operation into a more complex
368 -- sequence of tests that ensures that overflow is properly caught.
370 procedure Apply_Arithmetic_Overflow_Check
(N
: Node_Id
) is
371 Loc
: constant Source_Ptr
:= Sloc
(N
);
372 Typ
: constant Entity_Id
:= Etype
(N
);
373 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
374 Siz
: constant Int
:= UI_To_Int
(Esize
(Rtyp
));
375 Dsiz
: constant Int
:= Siz
* 2;
385 if Backend_Overflow_Checks_On_Target
386 or not Do_Overflow_Check
(N
)
387 or not Expander_Active
392 -- Nothing to do if the range of the result is known OK
394 Determine_Range
(N
, OK
, Lo
, Hi
);
396 -- Note in the test below that we assume that if a bound of the
397 -- range is equal to that of the type. That's not quite accurate
398 -- but we do this for the following reasons:
400 -- a) The way that Determine_Range works, it will typically report
401 -- the bounds of the value are the bounds of the type, because
402 -- it either can't tell anything more precise, or does not think
403 -- it is worth the effort to be more precise.
405 -- b) It is very unusual to have a situation in which this would
406 -- generate an unnecessary overflow check (an example would be
407 -- a subtype with a range 0 .. Integer'Last - 1 to which the
408 -- literal value one is added.
410 -- c) The alternative is a lot of special casing in this routine
411 -- which would partially duplicate the Determine_Range processing.
414 and then Lo
> Expr_Value
(Type_Low_Bound
(Typ
))
415 and then Hi
< Expr_Value
(Type_High_Bound
(Typ
))
420 -- None of the special case optimizations worked, so there is nothing
421 -- for it but to generate the full general case code:
427 -- Typ (Checktyp (x) op Checktyp (y));
429 -- where Typ is the type of the original expression, and Checktyp is
430 -- an integer type of sufficient length to hold the largest possible
433 -- In the case where check type exceeds the size of Long_Long_Integer,
434 -- we use a different approach, expanding to:
436 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
438 -- where xxx is Add, Multiply or Subtract as appropriate
440 -- Find check type if one exists
442 if Dsiz
<= Standard_Integer_Size
then
443 Ctyp
:= Standard_Integer
;
445 elsif Dsiz
<= Standard_Long_Long_Integer_Size
then
446 Ctyp
:= Standard_Long_Long_Integer
;
448 -- No check type exists, use runtime call
451 if Nkind
(N
) = N_Op_Add
then
452 Cent
:= RE_Add_With_Ovflo_Check
;
454 elsif Nkind
(N
) = N_Op_Multiply
then
455 Cent
:= RE_Multiply_With_Ovflo_Check
;
458 pragma Assert
(Nkind
(N
) = N_Op_Subtract
);
459 Cent
:= RE_Subtract_With_Ovflo_Check
;
464 Make_Function_Call
(Loc
,
465 Name
=> New_Reference_To
(RTE
(Cent
), Loc
),
466 Parameter_Associations
=> New_List
(
467 OK_Convert_To
(RTE
(RE_Integer_64
), Left_Opnd
(N
)),
468 OK_Convert_To
(RTE
(RE_Integer_64
), Right_Opnd
(N
))))));
470 Analyze_And_Resolve
(N
, Typ
);
474 -- If we fall through, we have the case where we do the arithmetic in
475 -- the next higher type and get the check by conversion. In these cases
476 -- Ctyp is set to the type to be used as the check type.
478 Opnod
:= Relocate_Node
(N
);
480 Opnd
:= OK_Convert_To
(Ctyp
, Left_Opnd
(Opnod
));
483 Set_Etype
(Opnd
, Ctyp
);
484 Set_Analyzed
(Opnd
, True);
485 Set_Left_Opnd
(Opnod
, Opnd
);
487 Opnd
:= OK_Convert_To
(Ctyp
, Right_Opnd
(Opnod
));
490 Set_Etype
(Opnd
, Ctyp
);
491 Set_Analyzed
(Opnd
, True);
492 Set_Right_Opnd
(Opnod
, Opnd
);
494 -- The type of the operation changes to the base type of the check
495 -- type, and we reset the overflow check indication, since clearly
496 -- no overflow is possible now that we are using a double length
497 -- type. We also set the Analyzed flag to avoid a recursive attempt
498 -- to expand the node.
500 Set_Etype
(Opnod
, Base_Type
(Ctyp
));
501 Set_Do_Overflow_Check
(Opnod
, False);
502 Set_Analyzed
(Opnod
, True);
504 -- Now build the outer conversion
506 Opnd
:= OK_Convert_To
(Typ
, Opnod
);
509 Set_Etype
(Opnd
, Typ
);
510 Set_Analyzed
(Opnd
, True);
511 Set_Do_Overflow_Check
(Opnd
, True);
514 end Apply_Arithmetic_Overflow_Check
;
516 ----------------------------
517 -- Apply_Array_Size_Check --
518 ----------------------------
520 -- Note: Really of course this entre check should be in the backend,
521 -- and perhaps this is not quite the right value, but it is good
522 -- enough to catch the normal cases (and the relevant ACVC tests!)
524 procedure Apply_Array_Size_Check
(N
: Node_Id
; Typ
: Entity_Id
) is
525 Loc
: constant Source_Ptr
:= Sloc
(N
);
526 Ctyp
: constant Entity_Id
:= Component_Type
(Typ
);
527 Ent
: constant Entity_Id
:= Defining_Identifier
(N
);
539 Static
: Boolean := True;
540 -- Set false if any index subtye bound is non-static
542 Umark
: constant Uintp
.Save_Mark
:= Uintp
.Mark
;
543 -- We can throw away all the Uint computations here, since they are
544 -- done only to generate boolean test results.
547 -- Size to check against
549 function Is_Address_Or_Import
(Decl
: Node_Id
) return Boolean;
550 -- Determines if Decl is an address clause or Import/Interface pragma
551 -- that references the defining identifier of the current declaration.
553 --------------------------
554 -- Is_Address_Or_Import --
555 --------------------------
557 function Is_Address_Or_Import
(Decl
: Node_Id
) return Boolean is
559 if Nkind
(Decl
) = N_At_Clause
then
560 return Chars
(Identifier
(Decl
)) = Chars
(Ent
);
562 elsif Nkind
(Decl
) = N_Attribute_Definition_Clause
then
564 Chars
(Decl
) = Name_Address
566 Nkind
(Name
(Decl
)) = N_Identifier
568 Chars
(Name
(Decl
)) = Chars
(Ent
);
570 elsif Nkind
(Decl
) = N_Pragma
then
571 if (Chars
(Decl
) = Name_Import
573 Chars
(Decl
) = Name_Interface
)
574 and then Present
(Pragma_Argument_Associations
(Decl
))
577 F
: constant Node_Id
:=
578 First
(Pragma_Argument_Associations
(Decl
));
586 Nkind
(Expression
(Next
(F
))) = N_Identifier
588 Chars
(Expression
(Next
(F
))) = Chars
(Ent
);
598 end Is_Address_Or_Import
;
600 -- Start of processing for Apply_Array_Size_Check
603 if not Expander_Active
604 or else Storage_Checks_Suppressed
(Typ
)
609 -- It is pointless to insert this check inside an _init_proc, because
610 -- that's too late, we have already built the object to be the right
611 -- size, and if it's too large, too bad!
613 if Inside_Init_Proc
then
617 -- Look head for pragma interface/import or address clause applying
618 -- to this entity. If found, we suppress the check entirely. For now
619 -- we only look ahead 20 declarations to stop this becoming too slow
620 -- Note that eventually this whole routine gets moved to gigi.
623 for Ctr
in 1 .. 20 loop
627 if Is_Address_Or_Import
(Decl
) then
632 -- First step is to calculate the maximum number of elements. For this
633 -- calculation, we use the actual size of the subtype if it is static,
634 -- and if a bound of a subtype is non-static, we go to the bound of the
638 Indx
:= First_Index
(Typ
);
639 while Present
(Indx
) loop
640 Xtyp
:= Etype
(Indx
);
641 Lo
:= Type_Low_Bound
(Xtyp
);
642 Hi
:= Type_High_Bound
(Xtyp
);
644 -- If any bound raises constraint error, we will never get this
645 -- far, so there is no need to generate any kind of check.
647 if Raises_Constraint_Error
(Lo
)
649 Raises_Constraint_Error
(Hi
)
651 Uintp
.Release
(Umark
);
655 -- Otherwise get bounds values
657 if Is_Static_Expression
(Lo
) then
658 Lob
:= Expr_Value
(Lo
);
660 Lob
:= Expr_Value
(Type_Low_Bound
(Base_Type
(Xtyp
)));
664 if Is_Static_Expression
(Hi
) then
665 Hib
:= Expr_Value
(Hi
);
667 Hib
:= Expr_Value
(Type_High_Bound
(Base_Type
(Xtyp
)));
671 Siz
:= Siz
* UI_Max
(Hib
- Lob
+ 1, Uint_0
);
675 -- Compute the limit against which we want to check. For subprograms,
676 -- where the array will go on the stack, we use 8*2**24, which (in
677 -- bits) is the size of a 16 megabyte array.
679 if Is_Subprogram
(Scope
(Ent
)) then
680 Check_Siz
:= Uint_2
** 27;
682 Check_Siz
:= Uint_2
** 31;
685 -- If we have all static bounds and Siz is too large, then we know we
686 -- know we have a storage error right now, so generate message
688 if Static
and then Siz
>= Check_Siz
then
690 Make_Raise_Storage_Error
(Loc
,
691 Reason
=> SE_Object_Too_Large
));
692 Warn_On_Instance
:= True;
693 Error_Msg_N
("?Storage_Error will be raised at run-time", N
);
694 Warn_On_Instance
:= False;
695 Uintp
.Release
(Umark
);
699 -- Case of component size known at compile time. If the array
700 -- size is definitely in range, then we do not need a check.
702 if Known_Esize
(Ctyp
)
703 and then Siz
* Esize
(Ctyp
) < Check_Siz
705 Uintp
.Release
(Umark
);
709 -- Here if a dynamic check is required
711 -- What we do is to build an expression for the size of the array,
712 -- which is computed as the 'Size of the array component, times
713 -- the size of each dimension.
715 Uintp
.Release
(Umark
);
718 Make_Attribute_Reference
(Loc
,
719 Prefix
=> New_Occurrence_Of
(Ctyp
, Loc
),
720 Attribute_Name
=> Name_Size
);
722 Indx
:= First_Index
(Typ
);
724 for J
in 1 .. Number_Dimensions
(Typ
) loop
726 if Sloc
(Etype
(Indx
)) = Sloc
(N
) then
727 Ensure_Defined
(Etype
(Indx
), N
);
731 Make_Op_Multiply
(Loc
,
734 Make_Attribute_Reference
(Loc
,
735 Prefix
=> New_Occurrence_Of
(Typ
, Loc
),
736 Attribute_Name
=> Name_Length
,
737 Expressions
=> New_List
(
738 Make_Integer_Literal
(Loc
, J
))));
743 Make_Raise_Storage_Error
(Loc
,
748 Make_Integer_Literal
(Loc
, Check_Siz
)),
749 Reason
=> SE_Object_Too_Large
);
751 Set_Size_Check_Code
(Defining_Identifier
(N
), Code
);
752 Insert_Action
(N
, Code
);
753 end Apply_Array_Size_Check
;
755 ----------------------------
756 -- Apply_Constraint_Check --
757 ----------------------------
759 procedure Apply_Constraint_Check
762 No_Sliding
: Boolean := False)
764 Desig_Typ
: Entity_Id
;
767 if Inside_A_Generic
then
770 elsif Is_Scalar_Type
(Typ
) then
771 Apply_Scalar_Range_Check
(N
, Typ
);
773 elsif Is_Array_Type
(Typ
) then
775 -- A useful optimization: an aggregate with only an Others clause
776 -- always has the right bounds.
778 if Nkind
(N
) = N_Aggregate
779 and then No
(Expressions
(N
))
781 (First
(Choices
(First
(Component_Associations
(N
)))))
787 if Is_Constrained
(Typ
) then
788 Apply_Length_Check
(N
, Typ
);
791 Apply_Range_Check
(N
, Typ
);
794 Apply_Range_Check
(N
, Typ
);
797 elsif (Is_Record_Type
(Typ
)
798 or else Is_Private_Type
(Typ
))
799 and then Has_Discriminants
(Base_Type
(Typ
))
800 and then Is_Constrained
(Typ
)
802 Apply_Discriminant_Check
(N
, Typ
);
804 elsif Is_Access_Type
(Typ
) then
806 Desig_Typ
:= Designated_Type
(Typ
);
808 -- No checks necessary if expression statically null
810 if Nkind
(N
) = N_Null
then
813 -- No sliding possible on access to arrays
815 elsif Is_Array_Type
(Desig_Typ
) then
816 if Is_Constrained
(Desig_Typ
) then
817 Apply_Length_Check
(N
, Typ
);
820 Apply_Range_Check
(N
, Typ
);
822 elsif Has_Discriminants
(Base_Type
(Desig_Typ
))
823 and then Is_Constrained
(Desig_Typ
)
825 Apply_Discriminant_Check
(N
, Typ
);
828 end Apply_Constraint_Check
;
830 ------------------------------
831 -- Apply_Discriminant_Check --
832 ------------------------------
834 procedure Apply_Discriminant_Check
837 Lhs
: Node_Id
:= Empty
)
839 Loc
: constant Source_Ptr
:= Sloc
(N
);
840 Do_Access
: constant Boolean := Is_Access_Type
(Typ
);
841 S_Typ
: Entity_Id
:= Etype
(N
);
845 function Is_Aliased_Unconstrained_Component
return Boolean;
846 -- It is possible for an aliased component to have a nominal
847 -- unconstrained subtype (through instantiation). If this is a
848 -- discriminated component assigned in the expansion of an aggregate
849 -- in an initialization, the check must be suppressed. This unusual
850 -- situation requires a predicate of its own (see 7503-008).
852 ----------------------------------------
853 -- Is_Aliased_Unconstrained_Component --
854 ----------------------------------------
856 function Is_Aliased_Unconstrained_Component
return Boolean is
861 if Nkind
(Lhs
) /= N_Selected_Component
then
864 Comp
:= Entity
(Selector_Name
(Lhs
));
865 Pref
:= Prefix
(Lhs
);
868 if Ekind
(Comp
) /= E_Component
869 or else not Is_Aliased
(Comp
)
874 return not Comes_From_Source
(Pref
)
876 and then not Is_Constrained
(Etype
(Comp
));
877 end Is_Aliased_Unconstrained_Component
;
879 -- Start of processing for Apply_Discriminant_Check
883 T_Typ
:= Designated_Type
(Typ
);
888 -- Nothing to do if discriminant checks are suppressed or else no code
889 -- is to be generated
891 if not Expander_Active
892 or else Discriminant_Checks_Suppressed
(T_Typ
)
897 -- No discriminant checks necessary for access when expression
898 -- is statically Null. This is not only an optimization, this is
899 -- fundamental because otherwise discriminant checks may be generated
900 -- in init procs for types containing an access to a non-frozen yet
901 -- record, causing a deadly forward reference.
903 -- Also, if the expression is of an access type whose designated
904 -- type is incomplete, then the access value must be null and
905 -- we suppress the check.
907 if Nkind
(N
) = N_Null
then
910 elsif Is_Access_Type
(S_Typ
) then
911 S_Typ
:= Designated_Type
(S_Typ
);
913 if Ekind
(S_Typ
) = E_Incomplete_Type
then
918 -- If an assignment target is present, then we need to generate
919 -- the actual subtype if the target is a parameter or aliased
920 -- object with an unconstrained nominal subtype.
923 and then (Present
(Param_Entity
(Lhs
))
924 or else (not Is_Constrained
(T_Typ
)
925 and then Is_Aliased_View
(Lhs
)
926 and then not Is_Aliased_Unconstrained_Component
))
928 T_Typ
:= Get_Actual_Subtype
(Lhs
);
931 -- Nothing to do if the type is unconstrained (this is the case
932 -- where the actual subtype in the RM sense of N is unconstrained
933 -- and no check is required).
935 if not Is_Constrained
(T_Typ
) then
939 -- Suppress checks if the subtypes are the same.
940 -- the check must be preserved in an assignment to a formal, because
941 -- the constraint is given by the actual.
943 if Nkind
(Original_Node
(N
)) /= N_Allocator
945 or else not Is_Entity_Name
(Lhs
)
946 or else (Ekind
(Entity
(Lhs
)) /= E_In_Out_Parameter
947 and then Ekind
(Entity
(Lhs
)) /= E_Out_Parameter
))
950 or else (Do_Access
and then Designated_Type
(Typ
) = S_Typ
))
951 and then not Is_Aliased_View
(Lhs
)
956 -- We can also eliminate checks on allocators with a subtype mark
957 -- that coincides with the context type. The context type may be a
958 -- subtype without a constraint (common case, a generic actual).
960 elsif Nkind
(Original_Node
(N
)) = N_Allocator
961 and then Is_Entity_Name
(Expression
(Original_Node
(N
)))
964 Alloc_Typ
: Entity_Id
:= Entity
(Expression
(Original_Node
(N
)));
968 or else (Nkind
(Parent
(T_Typ
)) = N_Subtype_Declaration
969 and then Is_Entity_Name
(
970 Subtype_Indication
(Parent
(T_Typ
)))
971 and then Alloc_Typ
= Base_Type
(T_Typ
))
979 -- See if we have a case where the types are both constrained, and
980 -- all the constraints are constants. In this case, we can do the
981 -- check successfully at compile time.
983 -- we skip this check for the case where the node is a rewritten`
984 -- allocator, because it already carries the context subtype, and
985 -- extracting the discriminants from the aggregate is messy.
987 if Is_Constrained
(S_Typ
)
988 and then Nkind
(Original_Node
(N
)) /= N_Allocator
998 -- S_Typ may not have discriminants in the case where it is a
999 -- private type completed by a default discriminated type. In
1000 -- that case, we need to get the constraints from the
1001 -- underlying_type. If the underlying type is unconstrained (i.e.
1002 -- has no default discriminants) no check is needed.
1004 if Has_Discriminants
(S_Typ
) then
1005 Discr
:= First_Discriminant
(S_Typ
);
1006 DconS
:= First_Elmt
(Discriminant_Constraint
(S_Typ
));
1009 Discr
:= First_Discriminant
(Underlying_Type
(S_Typ
));
1012 (Discriminant_Constraint
(Underlying_Type
(S_Typ
)));
1019 DconT
:= First_Elmt
(Discriminant_Constraint
(T_Typ
));
1021 while Present
(Discr
) loop
1022 ItemS
:= Node
(DconS
);
1023 ItemT
:= Node
(DconT
);
1026 not Is_OK_Static_Expression
(ItemS
)
1028 not Is_OK_Static_Expression
(ItemT
);
1030 if Expr_Value
(ItemS
) /= Expr_Value
(ItemT
) then
1031 if Do_Access
then -- needs run-time check.
1034 Apply_Compile_Time_Constraint_Error
1035 (N
, "incorrect value for discriminant&?",
1036 CE_Discriminant_Check_Failed
, Ent
=> Discr
);
1043 Next_Discriminant
(Discr
);
1052 -- Here we need a discriminant check. First build the expression
1053 -- for the comparisons of the discriminants:
1055 -- (n.disc1 /= typ.disc1) or else
1056 -- (n.disc2 /= typ.disc2) or else
1058 -- (n.discn /= typ.discn)
1060 Cond
:= Build_Discriminant_Checks
(N
, T_Typ
);
1062 -- If Lhs is set and is a parameter, then the condition is
1063 -- guarded by: lhs'constrained and then (condition built above)
1065 if Present
(Param_Entity
(Lhs
)) then
1069 Make_Attribute_Reference
(Loc
,
1070 Prefix
=> New_Occurrence_Of
(Param_Entity
(Lhs
), Loc
),
1071 Attribute_Name
=> Name_Constrained
),
1072 Right_Opnd
=> Cond
);
1076 Cond
:= Guard_Access
(Cond
, Loc
, N
);
1080 Make_Raise_Constraint_Error
(Loc
,
1082 Reason
=> CE_Discriminant_Check_Failed
));
1084 end Apply_Discriminant_Check
;
1086 ------------------------
1087 -- Apply_Divide_Check --
1088 ------------------------
1090 procedure Apply_Divide_Check
(N
: Node_Id
) is
1091 Loc
: constant Source_Ptr
:= Sloc
(N
);
1092 Typ
: constant Entity_Id
:= Etype
(N
);
1093 Left
: constant Node_Id
:= Left_Opnd
(N
);
1094 Right
: constant Node_Id
:= Right_Opnd
(N
);
1106 and not Backend_Divide_Checks_On_Target
1108 Determine_Range
(Right
, ROK
, Rlo
, Rhi
);
1110 -- See if division by zero possible, and if so generate test. This
1111 -- part of the test is not controlled by the -gnato switch.
1113 if Do_Division_Check
(N
) then
1115 if (not ROK
) or else (Rlo
<= 0 and then 0 <= Rhi
) then
1117 Make_Raise_Constraint_Error
(Loc
,
1120 Left_Opnd
=> Duplicate_Subexpr
(Right
),
1121 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
1122 Reason
=> CE_Divide_By_Zero
));
1126 -- Test for extremely annoying case of xxx'First divided by -1
1128 if Do_Overflow_Check
(N
) then
1130 if Nkind
(N
) = N_Op_Divide
1131 and then Is_Signed_Integer_Type
(Typ
)
1133 Determine_Range
(Left
, LOK
, Llo
, Lhi
);
1134 LLB
:= Expr_Value
(Type_Low_Bound
(Base_Type
(Typ
)));
1136 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
1138 ((not LOK
) or else (Llo
= LLB
))
1141 Make_Raise_Constraint_Error
(Loc
,
1146 Left_Opnd
=> Duplicate_Subexpr
(Left
),
1147 Right_Opnd
=> Make_Integer_Literal
(Loc
, LLB
)),
1150 Left_Opnd
=> Duplicate_Subexpr
(Right
),
1152 Make_Integer_Literal
(Loc
, -1))),
1153 Reason
=> CE_Overflow_Check_Failed
));
1158 end Apply_Divide_Check
;
1160 ------------------------
1161 -- Apply_Length_Check --
1162 ------------------------
1164 procedure Apply_Length_Check
1166 Target_Typ
: Entity_Id
;
1167 Source_Typ
: Entity_Id
:= Empty
)
1170 Apply_Selected_Length_Checks
1171 (Ck_Node
, Target_Typ
, Source_Typ
, Do_Static
=> False);
1172 end Apply_Length_Check
;
1174 -----------------------
1175 -- Apply_Range_Check --
1176 -----------------------
1178 procedure Apply_Range_Check
1180 Target_Typ
: Entity_Id
;
1181 Source_Typ
: Entity_Id
:= Empty
)
1184 Apply_Selected_Range_Checks
1185 (Ck_Node
, Target_Typ
, Source_Typ
, Do_Static
=> False);
1186 end Apply_Range_Check
;
1188 ------------------------------
1189 -- Apply_Scalar_Range_Check --
1190 ------------------------------
1192 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check
1193 -- flag off if it is already set on.
1195 procedure Apply_Scalar_Range_Check
1197 Target_Typ
: Entity_Id
;
1198 Source_Typ
: Entity_Id
:= Empty
;
1199 Fixed_Int
: Boolean := False)
1201 Parnt
: constant Node_Id
:= Parent
(Expr
);
1203 Arr
: Node_Id
:= Empty
; -- initialize to prevent warning
1204 Arr_Typ
: Entity_Id
:= Empty
; -- initialize to prevent warning
1207 Is_Subscr_Ref
: Boolean;
1208 -- Set true if Expr is a subscript
1210 Is_Unconstrained_Subscr_Ref
: Boolean;
1211 -- Set true if Expr is a subscript of an unconstrained array. In this
1212 -- case we do not attempt to do an analysis of the value against the
1213 -- range of the subscript, since we don't know the actual subtype.
1216 -- Set to True if Expr should be regarded as a real value
1217 -- even though the type of Expr might be discrete.
1219 procedure Bad_Value
;
1220 -- Procedure called if value is determined to be out of range
1222 procedure Bad_Value
is
1224 Apply_Compile_Time_Constraint_Error
1225 (Expr
, "value not in range of}?", CE_Range_Check_Failed
,
1231 if Inside_A_Generic
then
1234 -- Return if check obviously not needed. Note that we do not check
1235 -- for the expander being inactive, since this routine does not
1236 -- insert any code, but it does generate useful warnings sometimes,
1237 -- which we would like even if we are in semantics only mode.
1239 elsif Target_Typ
= Any_Type
1240 or else not Is_Scalar_Type
(Target_Typ
)
1241 or else Raises_Constraint_Error
(Expr
)
1246 -- Now, see if checks are suppressed
1249 Is_List_Member
(Expr
) and then Nkind
(Parnt
) = N_Indexed_Component
;
1251 if Is_Subscr_Ref
then
1252 Arr
:= Prefix
(Parnt
);
1253 Arr_Typ
:= Get_Actual_Subtype_If_Available
(Arr
);
1256 if not Do_Range_Check
(Expr
) then
1258 -- Subscript reference. Check for Index_Checks suppressed
1260 if Is_Subscr_Ref
then
1262 -- Check array type and its base type
1264 if Index_Checks_Suppressed
(Arr_Typ
)
1265 or else Suppress_Index_Checks
(Base_Type
(Arr_Typ
))
1269 -- Check array itself if it is an entity name
1271 elsif Is_Entity_Name
(Arr
)
1272 and then Suppress_Index_Checks
(Entity
(Arr
))
1276 -- Check expression itself if it is an entity name
1278 elsif Is_Entity_Name
(Expr
)
1279 and then Suppress_Index_Checks
(Entity
(Expr
))
1284 -- All other cases, check for Range_Checks suppressed
1287 -- Check target type and its base type
1289 if Range_Checks_Suppressed
(Target_Typ
)
1290 or else Suppress_Range_Checks
(Base_Type
(Target_Typ
))
1294 -- Check expression itself if it is an entity name
1296 elsif Is_Entity_Name
(Expr
)
1297 and then Suppress_Range_Checks
(Entity
(Expr
))
1301 -- If Expr is part of an assignment statement, then check
1302 -- left side of assignment if it is an entity name.
1304 elsif Nkind
(Parnt
) = N_Assignment_Statement
1305 and then Is_Entity_Name
(Name
(Parnt
))
1306 and then Suppress_Range_Checks
(Entity
(Name
(Parnt
)))
1313 -- Now see if we need a check
1315 if No
(Source_Typ
) then
1316 S_Typ
:= Etype
(Expr
);
1318 S_Typ
:= Source_Typ
;
1321 if not Is_Scalar_Type
(S_Typ
) or else S_Typ
= Any_Type
then
1325 Is_Unconstrained_Subscr_Ref
:=
1326 Is_Subscr_Ref
and then not Is_Constrained
(Arr_Typ
);
1328 -- Always do a range check if the source type includes infinities
1329 -- and the target type does not include infinities.
1331 if Is_Floating_Point_Type
(S_Typ
)
1332 and then Has_Infinities
(S_Typ
)
1333 and then not Has_Infinities
(Target_Typ
)
1335 Enable_Range_Check
(Expr
);
1338 -- Return if we know expression is definitely in the range of
1339 -- the target type as determined by Determine_Range. Right now
1340 -- we only do this for discrete types, and not fixed-point or
1341 -- floating-point types.
1343 -- The additional less-precise tests below catch these cases.
1345 -- Note: skip this if we are given a source_typ, since the point
1346 -- of supplying a Source_Typ is to stop us looking at the expression.
1347 -- could sharpen this test to be out parameters only ???
1349 if Is_Discrete_Type
(Target_Typ
)
1350 and then Is_Discrete_Type
(Etype
(Expr
))
1351 and then not Is_Unconstrained_Subscr_Ref
1352 and then No
(Source_Typ
)
1355 Tlo
: constant Node_Id
:= Type_Low_Bound
(Target_Typ
);
1356 Thi
: constant Node_Id
:= Type_High_Bound
(Target_Typ
);
1361 if Compile_Time_Known_Value
(Tlo
)
1362 and then Compile_Time_Known_Value
(Thi
)
1364 Determine_Range
(Expr
, OK
, Lo
, Hi
);
1368 Lov
: constant Uint
:= Expr_Value
(Tlo
);
1369 Hiv
: constant Uint
:= Expr_Value
(Thi
);
1372 if Lo
>= Lov
and then Hi
<= Hiv
then
1375 elsif Lov
> Hi
or else Hiv
< Lo
then
1386 Is_Floating_Point_Type
(S_Typ
)
1387 or else (Is_Fixed_Point_Type
(S_Typ
) and then not Fixed_Int
);
1389 -- Check if we can determine at compile time whether Expr is in the
1390 -- range of the target type. Note that if S_Typ is within the
1391 -- bounds of Target_Typ then this must be the case. This checks is
1392 -- only meaningful if this is not a conversion between integer and
1395 if not Is_Unconstrained_Subscr_Ref
1397 Is_Discrete_Type
(S_Typ
) = Is_Discrete_Type
(Target_Typ
)
1399 (In_Subrange_Of
(S_Typ
, Target_Typ
, Fixed_Int
)
1401 Is_In_Range
(Expr
, Target_Typ
, Fixed_Int
, Int_Real
))
1405 elsif Is_Out_Of_Range
(Expr
, Target_Typ
, Fixed_Int
, Int_Real
) then
1409 -- Do not set range checks if they are killed
1411 elsif Nkind
(Expr
) = N_Unchecked_Type_Conversion
1412 and then Kill_Range_Check
(Expr
)
1416 -- ??? We only need a runtime check if the target type is constrained
1417 -- (the predefined type Float is not for instance).
1418 -- so the following should really be
1420 -- elsif Is_Constrained (Target_Typ) then
1422 -- but it isn't because certain types do not have the Is_Constrained
1423 -- flag properly set (see 1503-003).
1426 Enable_Range_Check
(Expr
);
1430 end Apply_Scalar_Range_Check
;
1432 ----------------------------------
1433 -- Apply_Selected_Length_Checks --
1434 ----------------------------------
1436 procedure Apply_Selected_Length_Checks
1438 Target_Typ
: Entity_Id
;
1439 Source_Typ
: Entity_Id
;
1440 Do_Static
: Boolean)
1443 R_Result
: Check_Result
;
1446 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
1447 Checks_On
: constant Boolean :=
1448 (not Index_Checks_Suppressed
(Target_Typ
))
1450 (not Length_Checks_Suppressed
(Target_Typ
));
1453 if not Expander_Active
then
1458 Selected_Length_Checks
(Ck_Node
, Target_Typ
, Source_Typ
, Empty
);
1460 for J
in 1 .. 2 loop
1462 R_Cno
:= R_Result
(J
);
1463 exit when No
(R_Cno
);
1465 -- A length check may mention an Itype which is attached to a
1466 -- subsequent node. At the top level in a package this can cause
1467 -- an order-of-elaboration problem, so we make sure that the itype
1468 -- is referenced now.
1470 if Ekind
(Current_Scope
) = E_Package
1471 and then Is_Compilation_Unit
(Current_Scope
)
1473 Ensure_Defined
(Target_Typ
, Ck_Node
);
1475 if Present
(Source_Typ
) then
1476 Ensure_Defined
(Source_Typ
, Ck_Node
);
1478 elsif Is_Itype
(Etype
(Ck_Node
)) then
1479 Ensure_Defined
(Etype
(Ck_Node
), Ck_Node
);
1483 -- If the item is a conditional raise of constraint error,
1484 -- then have a look at what check is being performed and
1487 if Nkind
(R_Cno
) = N_Raise_Constraint_Error
1488 and then Present
(Condition
(R_Cno
))
1490 Cond
:= Condition
(R_Cno
);
1492 if not Has_Dynamic_Length_Check
(Ck_Node
)
1495 Insert_Action
(Ck_Node
, R_Cno
);
1497 if not Do_Static
then
1498 Set_Has_Dynamic_Length_Check
(Ck_Node
);
1502 -- Output a warning if the condition is known to be True
1504 if Is_Entity_Name
(Cond
)
1505 and then Entity
(Cond
) = Standard_True
1507 Apply_Compile_Time_Constraint_Error
1508 (Ck_Node
, "wrong length for array of}?",
1509 CE_Length_Check_Failed
,
1513 -- If we were only doing a static check, or if checks are not
1514 -- on, then we want to delete the check, since it is not needed.
1515 -- We do this by replacing the if statement by a null statement
1517 elsif Do_Static
or else not Checks_On
then
1518 Rewrite
(R_Cno
, Make_Null_Statement
(Loc
));
1522 Install_Static_Check
(R_Cno
, Loc
);
1527 end Apply_Selected_Length_Checks
;
1529 ---------------------------------
1530 -- Apply_Selected_Range_Checks --
1531 ---------------------------------
1533 procedure Apply_Selected_Range_Checks
1535 Target_Typ
: Entity_Id
;
1536 Source_Typ
: Entity_Id
;
1537 Do_Static
: Boolean)
1540 R_Result
: Check_Result
;
1543 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
1544 Checks_On
: constant Boolean :=
1545 (not Index_Checks_Suppressed
(Target_Typ
))
1547 (not Range_Checks_Suppressed
(Target_Typ
));
1550 if not Expander_Active
or else not Checks_On
then
1555 Selected_Range_Checks
(Ck_Node
, Target_Typ
, Source_Typ
, Empty
);
1557 for J
in 1 .. 2 loop
1559 R_Cno
:= R_Result
(J
);
1560 exit when No
(R_Cno
);
1562 -- If the item is a conditional raise of constraint error,
1563 -- then have a look at what check is being performed and
1566 if Nkind
(R_Cno
) = N_Raise_Constraint_Error
1567 and then Present
(Condition
(R_Cno
))
1569 Cond
:= Condition
(R_Cno
);
1571 if not Has_Dynamic_Range_Check
(Ck_Node
) then
1572 Insert_Action
(Ck_Node
, R_Cno
);
1574 if not Do_Static
then
1575 Set_Has_Dynamic_Range_Check
(Ck_Node
);
1579 -- Output a warning if the condition is known to be True
1581 if Is_Entity_Name
(Cond
)
1582 and then Entity
(Cond
) = Standard_True
1584 -- Since an N_Range is technically not an expression, we
1585 -- have to set one of the bounds to C_E and then just flag
1586 -- the N_Range. The warning message will point to the
1587 -- lower bound and complain about a range, which seems OK.
1589 if Nkind
(Ck_Node
) = N_Range
then
1590 Apply_Compile_Time_Constraint_Error
1591 (Low_Bound
(Ck_Node
), "static range out of bounds of}?",
1592 CE_Range_Check_Failed
,
1596 Set_Raises_Constraint_Error
(Ck_Node
);
1599 Apply_Compile_Time_Constraint_Error
1600 (Ck_Node
, "static value out of range of}?",
1601 CE_Range_Check_Failed
,
1606 -- If we were only doing a static check, or if checks are not
1607 -- on, then we want to delete the check, since it is not needed.
1608 -- We do this by replacing the if statement by a null statement
1610 elsif Do_Static
or else not Checks_On
then
1611 Rewrite
(R_Cno
, Make_Null_Statement
(Loc
));
1615 Install_Static_Check
(R_Cno
, Loc
);
1620 end Apply_Selected_Range_Checks
;
1622 -------------------------------
1623 -- Apply_Static_Length_Check --
1624 -------------------------------
1626 procedure Apply_Static_Length_Check
1628 Target_Typ
: Entity_Id
;
1629 Source_Typ
: Entity_Id
:= Empty
)
1632 Apply_Selected_Length_Checks
1633 (Expr
, Target_Typ
, Source_Typ
, Do_Static
=> True);
1634 end Apply_Static_Length_Check
;
1636 -------------------------------------
1637 -- Apply_Subscript_Validity_Checks --
1638 -------------------------------------
1640 procedure Apply_Subscript_Validity_Checks
(Expr
: Node_Id
) is
1644 pragma Assert
(Nkind
(Expr
) = N_Indexed_Component
);
1646 -- Loop through subscripts
1648 Sub
:= First
(Expressions
(Expr
));
1649 while Present
(Sub
) loop
1651 -- Check one subscript. Note that we do not worry about
1652 -- enumeration type with holes, since we will convert the
1653 -- value to a Pos value for the subscript, and that convert
1654 -- will do the necessary validity check.
1656 Ensure_Valid
(Sub
, Holes_OK
=> True);
1658 -- Move to next subscript
1662 end Apply_Subscript_Validity_Checks
;
1664 ----------------------------------
1665 -- Apply_Type_Conversion_Checks --
1666 ----------------------------------
1668 procedure Apply_Type_Conversion_Checks
(N
: Node_Id
) is
1669 Target_Type
: constant Entity_Id
:= Etype
(N
);
1670 Target_Base
: constant Entity_Id
:= Base_Type
(Target_Type
);
1672 Expr
: constant Node_Id
:= Expression
(N
);
1673 Expr_Type
: constant Entity_Id
:= Etype
(Expr
);
1676 if Inside_A_Generic
then
1679 -- Skip these checks if serious errors detected, there are some nasty
1680 -- situations of incomplete trees that blow things up.
1682 elsif Serious_Errors_Detected
> 0 then
1685 -- Scalar type conversions of the form Target_Type (Expr) require
1688 -- - First there is an overflow check to insure that Expr is
1689 -- in the base type of Target_Typ (4.6 (28)),
1691 -- - After we know Expr fits into the base type, we must perform a
1692 -- range check to ensure that Expr meets the constraints of the
1695 elsif Is_Scalar_Type
(Target_Type
) then
1697 Conv_OK
: constant Boolean := Conversion_OK
(N
);
1698 -- If the Conversion_OK flag on the type conversion is set
1699 -- and no floating point type is involved in the type conversion
1700 -- then fixed point values must be read as integral values.
1705 if not Overflow_Checks_Suppressed
(Target_Base
)
1706 and then not In_Subrange_Of
(Expr_Type
, Target_Base
, Conv_OK
)
1708 Set_Do_Overflow_Check
(N
);
1711 if not Range_Checks_Suppressed
(Target_Type
)
1712 and then not Range_Checks_Suppressed
(Expr_Type
)
1714 Apply_Scalar_Range_Check
1715 (Expr
, Target_Type
, Fixed_Int
=> Conv_OK
);
1719 elsif Comes_From_Source
(N
)
1720 and then Is_Record_Type
(Target_Type
)
1721 and then Is_Derived_Type
(Target_Type
)
1722 and then not Is_Tagged_Type
(Target_Type
)
1723 and then not Is_Constrained
(Target_Type
)
1724 and then Present
(Girder_Constraint
(Target_Type
))
1726 -- A unconstrained derived type may have inherited discriminants.
1727 -- Build an actual discriminant constraint list using the girder
1728 -- constraint, to verify that the expression of the parent type
1729 -- satisfies the constraints imposed by the (unconstrained!)
1730 -- derived type. This applies to value conversions, not to view
1731 -- conversions of tagged types.
1734 Loc
: constant Source_Ptr
:= Sloc
(N
);
1736 Constraint
: Elmt_Id
;
1737 Discr_Value
: Node_Id
;
1739 New_Constraints
: Elist_Id
:= New_Elmt_List
;
1740 Old_Constraints
: Elist_Id
:= Discriminant_Constraint
(Expr_Type
);
1743 Constraint
:= First_Elmt
(Girder_Constraint
(Target_Type
));
1745 while Present
(Constraint
) loop
1746 Discr_Value
:= Node
(Constraint
);
1748 if Is_Entity_Name
(Discr_Value
)
1749 and then Ekind
(Entity
(Discr_Value
)) = E_Discriminant
1751 Discr
:= Corresponding_Discriminant
(Entity
(Discr_Value
));
1754 and then Scope
(Discr
) = Base_Type
(Expr_Type
)
1756 -- Parent is constrained by new discriminant. Obtain
1757 -- Value of original discriminant in expression. If
1758 -- the new discriminant has been used to constrain more
1759 -- than one of the girder ones, this will provide the
1760 -- required consistency check.
1763 Make_Selected_Component
(Loc
,
1765 Duplicate_Subexpr
(Expr
, Name_Req
=> True),
1767 Make_Identifier
(Loc
, Chars
(Discr
))),
1771 -- Discriminant of more remote ancestor ???
1776 -- Derived type definition has an explicit value for
1777 -- this girder discriminant.
1781 (Duplicate_Subexpr
(Discr_Value
), New_Constraints
);
1784 Next_Elmt
(Constraint
);
1787 -- Use the unconstrained expression type to retrieve the
1788 -- discriminants of the parent, and apply momentarily the
1789 -- discriminant constraint synthesized above.
1791 Set_Discriminant_Constraint
(Expr_Type
, New_Constraints
);
1792 Cond
:= Build_Discriminant_Checks
(Expr
, Expr_Type
);
1793 Set_Discriminant_Constraint
(Expr_Type
, Old_Constraints
);
1796 Make_Raise_Constraint_Error
(Loc
,
1798 Reason
=> CE_Discriminant_Check_Failed
));
1801 -- should there be other checks here for array types ???
1807 end Apply_Type_Conversion_Checks
;
1809 ----------------------------------------------
1810 -- Apply_Universal_Integer_Attribute_Checks --
1811 ----------------------------------------------
1813 procedure Apply_Universal_Integer_Attribute_Checks
(N
: Node_Id
) is
1814 Loc
: constant Source_Ptr
:= Sloc
(N
);
1815 Typ
: constant Entity_Id
:= Etype
(N
);
1818 if Inside_A_Generic
then
1821 -- Nothing to do if checks are suppressed
1823 elsif Range_Checks_Suppressed
(Typ
)
1824 and then Overflow_Checks_Suppressed
(Typ
)
1828 -- Nothing to do if the attribute does not come from source. The
1829 -- internal attributes we generate of this type do not need checks,
1830 -- and furthermore the attempt to check them causes some circular
1831 -- elaboration orders when dealing with packed types.
1833 elsif not Comes_From_Source
(N
) then
1836 -- Otherwise, replace the attribute node with a type conversion
1837 -- node whose expression is the attribute, retyped to universal
1838 -- integer, and whose subtype mark is the target type. The call
1839 -- to analyze this conversion will set range and overflow checks
1840 -- as required for proper detection of an out of range value.
1843 Set_Etype
(N
, Universal_Integer
);
1844 Set_Analyzed
(N
, True);
1847 Make_Type_Conversion
(Loc
,
1848 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
1849 Expression
=> Relocate_Node
(N
)));
1851 Analyze_And_Resolve
(N
, Typ
);
1855 end Apply_Universal_Integer_Attribute_Checks
;
1857 -------------------------------
1858 -- Build_Discriminant_Checks --
1859 -------------------------------
1861 function Build_Discriminant_Checks
1866 Loc
: constant Source_Ptr
:= Sloc
(N
);
1869 Disc_Ent
: Entity_Id
;
1874 Disc
:= First_Elmt
(Discriminant_Constraint
(T_Typ
));
1876 -- For a fully private type, use the discriminants of the parent
1879 if Is_Private_Type
(T_Typ
)
1880 and then No
(Full_View
(T_Typ
))
1882 Disc_Ent
:= First_Discriminant
(Etype
(Base_Type
(T_Typ
)));
1884 Disc_Ent
:= First_Discriminant
(T_Typ
);
1887 while Present
(Disc
) loop
1889 Dval
:= Node
(Disc
);
1891 if Nkind
(Dval
) = N_Identifier
1892 and then Ekind
(Entity
(Dval
)) = E_Discriminant
1894 Dval
:= New_Occurrence_Of
(Discriminal
(Entity
(Dval
)), Loc
);
1896 Dval
:= Duplicate_Subexpr
(Dval
);
1899 Evolve_Or_Else
(Cond
,
1902 Make_Selected_Component
(Loc
,
1904 Duplicate_Subexpr
(N
, Name_Req
=> True),
1906 Make_Identifier
(Loc
, Chars
(Disc_Ent
))),
1907 Right_Opnd
=> Dval
));
1910 Next_Discriminant
(Disc_Ent
);
1914 end Build_Discriminant_Checks
;
1916 -----------------------------------
1917 -- Check_Valid_Lvalue_Subscripts --
1918 -----------------------------------
1920 procedure Check_Valid_Lvalue_Subscripts
(Expr
: Node_Id
) is
1922 -- Skip this if range checks are suppressed
1924 if Range_Checks_Suppressed
(Etype
(Expr
)) then
1927 -- Only do this check for expressions that come from source. We
1928 -- assume that expander generated assignments explicitly include
1929 -- any necessary checks. Note that this is not just an optimization,
1930 -- it avoids infinite recursions!
1932 elsif not Comes_From_Source
(Expr
) then
1935 -- For a selected component, check the prefix
1937 elsif Nkind
(Expr
) = N_Selected_Component
then
1938 Check_Valid_Lvalue_Subscripts
(Prefix
(Expr
));
1941 -- Case of indexed component
1943 elsif Nkind
(Expr
) = N_Indexed_Component
then
1944 Apply_Subscript_Validity_Checks
(Expr
);
1946 -- Prefix may itself be or contain an indexed component, and
1947 -- these subscripts need checking as well
1949 Check_Valid_Lvalue_Subscripts
(Prefix
(Expr
));
1951 end Check_Valid_Lvalue_Subscripts
;
1953 ---------------------
1954 -- Determine_Range --
1955 ---------------------
1957 Cache_Size
: constant := 2 ** 10;
1958 type Cache_Index
is range 0 .. Cache_Size
- 1;
1959 -- Determine size of below cache (power of 2 is more efficient!)
1961 Determine_Range_Cache_N
: array (Cache_Index
) of Node_Id
;
1962 Determine_Range_Cache_Lo
: array (Cache_Index
) of Uint
;
1963 Determine_Range_Cache_Hi
: array (Cache_Index
) of Uint
;
1964 -- The above arrays are used to implement a small direct cache
1965 -- for Determine_Range calls. Because of the way Determine_Range
1966 -- recursively traces subexpressions, and because overflow checking
1967 -- calls the routine on the way up the tree, a quadratic behavior
1968 -- can otherwise be encountered in large expressions. The cache
1969 -- entry for node N is stored in the (N mod Cache_Size) entry, and
1970 -- can be validated by checking the actual node value stored there.
1972 procedure Determine_Range
1978 Typ
: constant Entity_Id
:= Etype
(N
);
1982 -- Lo and Hi bounds of left operand
1986 -- Lo and Hi bounds of right (or only) operand
1989 -- Temp variable used to hold a bound node
1992 -- High bound of base type of expression
1996 -- Refined values for low and high bounds, after tightening
1999 -- Used in lower level calls to indicate if call succeeded
2001 Cindex
: Cache_Index
;
2002 -- Used to search cache
2004 function OK_Operands
return Boolean;
2005 -- Used for binary operators. Determines the ranges of the left and
2006 -- right operands, and if they are both OK, returns True, and puts
2007 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left
2013 function OK_Operands
return Boolean is
2015 Determine_Range
(Left_Opnd
(N
), OK1
, Lo_Left
, Hi_Left
);
2021 Determine_Range
(Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
);
2025 -- Start of processing for Determine_Range
2028 -- Prevent junk warnings by initializing range variables
2035 -- If the type is not discrete, or is undefined, then we can't
2036 -- do anything about determining the range.
2038 if No
(Typ
) or else not Is_Discrete_Type
(Typ
)
2039 or else Error_Posted
(N
)
2045 -- For all other cases, we can determine the range
2049 -- If value is compile time known, then the possible range is the
2050 -- one value that we know this expression definitely has!
2052 if Compile_Time_Known_Value
(N
) then
2053 Lo
:= Expr_Value
(N
);
2058 -- Return if already in the cache
2060 Cindex
:= Cache_Index
(N
mod Cache_Size
);
2062 if Determine_Range_Cache_N
(Cindex
) = N
then
2063 Lo
:= Determine_Range_Cache_Lo
(Cindex
);
2064 Hi
:= Determine_Range_Cache_Hi
(Cindex
);
2068 -- Otherwise, start by finding the bounds of the type of the
2069 -- expression, the value cannot be outside this range (if it
2070 -- is, then we have an overflow situation, which is a separate
2071 -- check, we are talking here only about the expression value).
2073 -- We use the actual bound unless it is dynamic, in which case
2074 -- use the corresponding base type bound if possible. If we can't
2075 -- get a bound then we figure we can't determine the range (a
2076 -- peculiar case, that perhaps cannot happen, but there is no
2077 -- point in bombing in this optimization circuit.
2079 -- First the low bound
2081 Bound
:= Type_Low_Bound
(Typ
);
2083 if Compile_Time_Known_Value
(Bound
) then
2084 Lo
:= Expr_Value
(Bound
);
2086 elsif Compile_Time_Known_Value
(Type_Low_Bound
(Base_Type
(Typ
))) then
2087 Lo
:= Expr_Value
(Type_Low_Bound
(Base_Type
(Typ
)));
2094 -- Now the high bound
2096 Bound
:= Type_High_Bound
(Typ
);
2098 -- We need the high bound of the base type later on, and this should
2099 -- always be compile time known. Again, it is not clear that this
2100 -- can ever be false, but no point in bombing.
2102 if Compile_Time_Known_Value
(Type_High_Bound
(Base_Type
(Typ
))) then
2103 Hbound
:= Expr_Value
(Type_High_Bound
(Base_Type
(Typ
)));
2111 -- If we have a static subtype, then that may have a tighter bound
2112 -- so use the upper bound of the subtype instead in this case.
2114 if Compile_Time_Known_Value
(Bound
) then
2115 Hi
:= Expr_Value
(Bound
);
2118 -- We may be able to refine this value in certain situations. If
2119 -- refinement is possible, then Lor and Hir are set to possibly
2120 -- tighter bounds, and OK1 is set to True.
2124 -- For unary plus, result is limited by range of operand
2127 Determine_Range
(Right_Opnd
(N
), OK1
, Lor
, Hir
);
2129 -- For unary minus, determine range of operand, and negate it
2132 Determine_Range
(Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
);
2139 -- For binary addition, get range of each operand and do the
2140 -- addition to get the result range.
2144 Lor
:= Lo_Left
+ Lo_Right
;
2145 Hir
:= Hi_Left
+ Hi_Right
;
2148 -- Division is tricky. The only case we consider is where the
2149 -- right operand is a positive constant, and in this case we
2150 -- simply divide the bounds of the left operand
2154 if Lo_Right
= Hi_Right
2155 and then Lo_Right
> 0
2157 Lor
:= Lo_Left
/ Lo_Right
;
2158 Hir
:= Hi_Left
/ Lo_Right
;
2165 -- For binary subtraction, get range of each operand and do
2166 -- the worst case subtraction to get the result range.
2168 when N_Op_Subtract
=>
2170 Lor
:= Lo_Left
- Hi_Right
;
2171 Hir
:= Hi_Left
- Lo_Right
;
2174 -- For MOD, if right operand is a positive constant, then
2175 -- result must be in the allowable range of mod results.
2179 if Lo_Right
= Hi_Right
then
2180 if Lo_Right
> 0 then
2182 Hir
:= Lo_Right
- 1;
2184 elsif Lo_Right
< 0 then
2185 Lor
:= Lo_Right
+ 1;
2194 -- For REM, if right operand is a positive constant, then
2195 -- result must be in the allowable range of mod results.
2199 if Lo_Right
= Hi_Right
then
2201 Dval
: constant Uint
:= (abs Lo_Right
) - 1;
2204 -- The sign of the result depends on the sign of the
2205 -- dividend (but not on the sign of the divisor, hence
2206 -- the abs operation above).
2226 -- Attribute reference cases
2228 when N_Attribute_Reference
=>
2229 case Attribute_Name
(N
) is
2231 -- For Pos/Val attributes, we can refine the range using the
2232 -- possible range of values of the attribute expression
2234 when Name_Pos | Name_Val
=>
2235 Determine_Range
(First
(Expressions
(N
)), OK1
, Lor
, Hir
);
2237 -- For Length attribute, use the bounds of the corresponding
2238 -- index type to refine the range.
2242 Atyp
: Entity_Id
:= Etype
(Prefix
(N
));
2250 if Is_Access_Type
(Atyp
) then
2251 Atyp
:= Designated_Type
(Atyp
);
2254 -- For string literal, we know exact value
2256 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
2258 Lo
:= String_Literal_Length
(Atyp
);
2259 Hi
:= String_Literal_Length
(Atyp
);
2263 -- Otherwise check for expression given
2265 if No
(Expressions
(N
)) then
2269 UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
2272 Indx
:= First_Index
(Atyp
);
2273 for J
in 2 .. Inum
loop
2274 Indx
:= Next_Index
(Indx
);
2278 (Type_Low_Bound
(Etype
(Indx
)), OK1
, LL
, LU
);
2282 (Type_High_Bound
(Etype
(Indx
)), OK1
, UL
, UU
);
2286 -- The maximum value for Length is the biggest
2287 -- possible gap between the values of the bounds.
2288 -- But of course, this value cannot be negative.
2290 Hir
:= UI_Max
(Uint_0
, UU
- LL
);
2292 -- For constrained arrays, the minimum value for
2293 -- Length is taken from the actual value of the
2294 -- bounds, since the index will be exactly of
2297 if Is_Constrained
(Atyp
) then
2298 Lor
:= UI_Max
(Uint_0
, UL
- LU
);
2300 -- For an unconstrained array, the minimum value
2301 -- for length is always zero.
2310 -- No special handling for other attributes
2311 -- Probably more opportunities exist here ???
2318 -- For type conversion from one discrete type to another, we
2319 -- can refine the range using the converted value.
2321 when N_Type_Conversion
=>
2322 Determine_Range
(Expression
(N
), OK1
, Lor
, Hir
);
2324 -- Nothing special to do for all other expression kinds
2332 -- At this stage, if OK1 is true, then we know that the actual
2333 -- result of the computed expression is in the range Lor .. Hir.
2334 -- We can use this to restrict the possible range of results.
2338 -- If the refined value of the low bound is greater than the
2339 -- type high bound, then reset it to the more restrictive
2340 -- value. However, we do NOT do this for the case of a modular
2341 -- type where the possible upper bound on the value is above the
2342 -- base type high bound, because that means the result could wrap.
2345 and then not (Is_Modular_Integer_Type
(Typ
)
2346 and then Hir
> Hbound
)
2351 -- Similarly, if the refined value of the high bound is less
2352 -- than the value so far, then reset it to the more restrictive
2353 -- value. Again, we do not do this if the refined low bound is
2354 -- negative for a modular type, since this would wrap.
2357 and then not (Is_Modular_Integer_Type
(Typ
)
2358 and then Lor
< Uint_0
)
2364 -- Set cache entry for future call and we are all done
2366 Determine_Range_Cache_N
(Cindex
) := N
;
2367 Determine_Range_Cache_Lo
(Cindex
) := Lo
;
2368 Determine_Range_Cache_Hi
(Cindex
) := Hi
;
2371 -- If any exception occurs, it means that we have some bug in the compiler
2372 -- possibly triggered by a previous error, or by some unforseen peculiar
2373 -- occurrence. However, this is only an optimization attempt, so there is
2374 -- really no point in crashing the compiler. Instead we just decide, too
2375 -- bad, we can't figure out a range in this case after all.
2380 -- Debug flag K disables this behavior (useful for debugging)
2382 if Debug_Flag_K
then
2391 end Determine_Range
;
2393 ------------------------------------
2394 -- Discriminant_Checks_Suppressed --
2395 ------------------------------------
2397 function Discriminant_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2399 return Scope_Suppress
.Discriminant_Checks
2400 or else (Present
(E
) and then Suppress_Discriminant_Checks
(E
));
2401 end Discriminant_Checks_Suppressed
;
2403 --------------------------------
2404 -- Division_Checks_Suppressed --
2405 --------------------------------
2407 function Division_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2409 return Scope_Suppress
.Division_Checks
2410 or else (Present
(E
) and then Suppress_Division_Checks
(E
));
2411 end Division_Checks_Suppressed
;
2413 -----------------------------------
2414 -- Elaboration_Checks_Suppressed --
2415 -----------------------------------
2417 function Elaboration_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2419 return Scope_Suppress
.Elaboration_Checks
2420 or else (Present
(E
) and then Suppress_Elaboration_Checks
(E
));
2421 end Elaboration_Checks_Suppressed
;
2423 ------------------------
2424 -- Enable_Range_Check --
2425 ------------------------
2427 procedure Enable_Range_Check
(N
: Node_Id
) is
2429 if Nkind
(N
) = N_Unchecked_Type_Conversion
2430 and then Kill_Range_Check
(N
)
2434 Set_Do_Range_Check
(N
, True);
2436 end Enable_Range_Check
;
2442 procedure Ensure_Valid
(Expr
: Node_Id
; Holes_OK
: Boolean := False) is
2443 Typ
: constant Entity_Id
:= Etype
(Expr
);
2446 -- Ignore call if we are not doing any validity checking
2448 if not Validity_Checks_On
then
2451 -- No check required if expression is from the expander, we assume
2452 -- the expander will generate whatever checks are needed. Note that
2453 -- this is not just an optimization, it avoids infinite recursions!
2455 -- Unchecked conversions must be checked, unless they are initialized
2456 -- scalar values, as in a component assignment in an init_proc.
2458 elsif not Comes_From_Source
(Expr
)
2459 and then (Nkind
(Expr
) /= N_Unchecked_Type_Conversion
2460 or else Kill_Range_Check
(Expr
))
2464 -- No check required if expression is known to have valid value
2466 elsif Expr_Known_Valid
(Expr
) then
2469 -- No check required if checks off
2471 elsif Range_Checks_Suppressed
(Typ
) then
2474 -- Ignore case of enumeration with holes where the flag is set not
2475 -- to worry about holes, since no special validity check is needed
2477 elsif Is_Enumeration_Type
(Typ
)
2478 and then Has_Non_Standard_Rep
(Typ
)
2483 -- No check required on the left-hand side of an assignment.
2485 elsif Nkind
(Parent
(Expr
)) = N_Assignment_Statement
2486 and then Expr
= Name
(Parent
(Expr
))
2490 -- An annoying special case. If this is an out parameter of a scalar
2491 -- type, then the value is not going to be accessed, therefore it is
2492 -- inappropriate to do any validity check at the call site.
2495 -- Only need to worry about scalar types
2497 if Is_Scalar_Type
(Typ
) then
2507 -- Find actual argument (which may be a parameter association)
2508 -- and the parent of the actual argument (the call statement)
2513 if Nkind
(P
) = N_Parameter_Association
then
2518 -- Only need to worry if we are argument of a procedure
2519 -- call since functions don't have out parameters.
2521 if Nkind
(P
) = N_Procedure_Call_Statement
then
2522 L
:= Parameter_Associations
(P
);
2523 E
:= Entity
(Name
(P
));
2525 -- Only need to worry if there are indeed actuals, and
2526 -- if this could be a procedure call, otherwise we cannot
2527 -- get a match (either we are not an argument, or the
2528 -- mode of the formal is not OUT). This test also filters
2529 -- out the generic case.
2531 if Is_Non_Empty_List
(L
)
2532 and then Is_Subprogram
(E
)
2534 -- This is the loop through parameters, looking to
2535 -- see if there is an OUT parameter for which we are
2538 F
:= First_Formal
(E
);
2541 while Present
(F
) loop
2542 if Ekind
(F
) = E_Out_Parameter
and then A
= N
then
2555 -- If we fall through, a validity check is required. Note that it would
2556 -- not be good to set Do_Range_Check, even in contexts where this is
2557 -- permissible, since this flag causes checking against the target type,
2558 -- not the source type in contexts such as assignments
2560 Insert_Valid_Check
(Expr
);
2563 ----------------------
2564 -- Expr_Known_Valid --
2565 ----------------------
2567 function Expr_Known_Valid
(Expr
: Node_Id
) return Boolean is
2568 Typ
: constant Entity_Id
:= Etype
(Expr
);
2571 -- Non-scalar types are always consdered valid, since they never
2572 -- give rise to the issues of erroneous or bounded error behavior
2573 -- that are the concern. In formal reference manual terms the
2574 -- notion of validity only applies to scalar types.
2576 if not Is_Scalar_Type
(Typ
) then
2579 -- If no validity checking, then everything is considered valid
2581 elsif not Validity_Checks_On
then
2584 -- Floating-point types are considered valid unless floating-point
2585 -- validity checks have been specifically turned on.
2587 elsif Is_Floating_Point_Type
(Typ
)
2588 and then not Validity_Check_Floating_Point
2592 -- If the expression is the value of an object that is known to
2593 -- be valid, then clearly the expression value itself is valid.
2595 elsif Is_Entity_Name
(Expr
)
2596 and then Is_Known_Valid
(Entity
(Expr
))
2600 -- If the type is one for which all values are known valid, then
2601 -- we are sure that the value is valid except in the slightly odd
2602 -- case where the expression is a reference to a variable whose size
2603 -- has been explicitly set to a value greater than the object size.
2605 elsif Is_Known_Valid
(Typ
) then
2606 if Is_Entity_Name
(Expr
)
2607 and then Ekind
(Entity
(Expr
)) = E_Variable
2608 and then Esize
(Entity
(Expr
)) > Esize
(Typ
)
2615 -- Integer and character literals always have valid values, where
2616 -- appropriate these will be range checked in any case.
2618 elsif Nkind
(Expr
) = N_Integer_Literal
2620 Nkind
(Expr
) = N_Character_Literal
2624 -- If we have a type conversion or a qualification of a known valid
2625 -- value, then the result will always be valid.
2627 elsif Nkind
(Expr
) = N_Type_Conversion
2629 Nkind
(Expr
) = N_Qualified_Expression
2631 return Expr_Known_Valid
(Expression
(Expr
));
2633 -- The result of any function call or operator is always considered
2634 -- valid, since we assume the necessary checks are done by the call.
2636 elsif Nkind
(Expr
) in N_Binary_Op
2638 Nkind
(Expr
) in N_Unary_Op
2640 Nkind
(Expr
) = N_Function_Call
2644 -- For all other cases, we do not know the expression is valid
2649 end Expr_Known_Valid
;
2651 ---------------------
2652 -- Get_Discriminal --
2653 ---------------------
2655 function Get_Discriminal
(E
: Entity_Id
; Bound
: Node_Id
) return Node_Id
is
2656 Loc
: constant Source_Ptr
:= Sloc
(E
);
2661 -- The entity E is the type of a private component of the protected
2662 -- type, or the type of a renaming of that component within a protected
2663 -- operation of that type.
2667 if Ekind
(Sc
) /= E_Protected_Type
then
2670 if Ekind
(Sc
) /= E_Protected_Type
then
2675 D
:= First_Discriminant
(Sc
);
2678 and then Chars
(D
) /= Chars
(Bound
)
2680 Next_Discriminant
(D
);
2683 return New_Occurrence_Of
(Discriminal
(D
), Loc
);
2684 end Get_Discriminal
;
2690 function Guard_Access
2697 if Nkind
(Cond
) = N_Or_Else
then
2698 Set_Paren_Count
(Cond
, 1);
2701 if Nkind
(Ck_Node
) = N_Allocator
then
2708 Left_Opnd
=> Duplicate_Subexpr
(Ck_Node
),
2709 Right_Opnd
=> Make_Null
(Loc
)),
2710 Right_Opnd
=> Cond
);
2714 -----------------------------
2715 -- Index_Checks_Suppressed --
2716 -----------------------------
2718 function Index_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2720 return Scope_Suppress
.Index_Checks
2721 or else (Present
(E
) and then Suppress_Index_Checks
(E
));
2722 end Index_Checks_Suppressed
;
2728 procedure Initialize
is
2730 for J
in Determine_Range_Cache_N
'Range loop
2731 Determine_Range_Cache_N
(J
) := Empty
;
2735 -------------------------
2736 -- Insert_Range_Checks --
2737 -------------------------
2739 procedure Insert_Range_Checks
2740 (Checks
: Check_Result
;
2742 Suppress_Typ
: Entity_Id
;
2743 Static_Sloc
: Source_Ptr
:= No_Location
;
2744 Flag_Node
: Node_Id
:= Empty
;
2745 Do_Before
: Boolean := False)
2747 Internal_Flag_Node
: Node_Id
:= Flag_Node
;
2748 Internal_Static_Sloc
: Source_Ptr
:= Static_Sloc
;
2750 Check_Node
: Node_Id
;
2751 Checks_On
: constant Boolean :=
2752 (not Index_Checks_Suppressed
(Suppress_Typ
))
2754 (not Range_Checks_Suppressed
(Suppress_Typ
));
2757 -- For now we just return if Checks_On is false, however this should
2758 -- be enhanced to check for an always True value in the condition
2759 -- and to generate a compilation warning???
2761 if not Expander_Active
or else not Checks_On
then
2765 if Static_Sloc
= No_Location
then
2766 Internal_Static_Sloc
:= Sloc
(Node
);
2769 if No
(Flag_Node
) then
2770 Internal_Flag_Node
:= Node
;
2773 for J
in 1 .. 2 loop
2774 exit when No
(Checks
(J
));
2776 if Nkind
(Checks
(J
)) = N_Raise_Constraint_Error
2777 and then Present
(Condition
(Checks
(J
)))
2779 if not Has_Dynamic_Range_Check
(Internal_Flag_Node
) then
2780 Check_Node
:= Checks
(J
);
2781 Mark_Rewrite_Insertion
(Check_Node
);
2784 Insert_Before_And_Analyze
(Node
, Check_Node
);
2786 Insert_After_And_Analyze
(Node
, Check_Node
);
2789 Set_Has_Dynamic_Range_Check
(Internal_Flag_Node
);
2794 Make_Raise_Constraint_Error
(Internal_Static_Sloc
,
2795 Reason
=> CE_Range_Check_Failed
);
2796 Mark_Rewrite_Insertion
(Check_Node
);
2799 Insert_Before_And_Analyze
(Node
, Check_Node
);
2801 Insert_After_And_Analyze
(Node
, Check_Node
);
2805 end Insert_Range_Checks
;
2807 ------------------------
2808 -- Insert_Valid_Check --
2809 ------------------------
2811 procedure Insert_Valid_Check
(Expr
: Node_Id
) is
2812 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
2816 -- Do not insert if checks off, or if not checking validity
2818 if Range_Checks_Suppressed
(Etype
(Expr
))
2819 or else (not Validity_Checks_On
)
2824 -- If we have a checked conversion, then validity check applies to
2825 -- the expression inside the conversion, not the result, since if
2826 -- the expression inside is valid, then so is the conversion result.
2829 while Nkind
(Exp
) = N_Type_Conversion
loop
2830 Exp
:= Expression
(Exp
);
2833 -- Insert the validity check. Note that we do this with validity
2834 -- checks turned off, to avoid recursion, we do not want validity
2835 -- checks on the validity checking code itself!
2837 Validity_Checks_On
:= False;
2840 Make_Raise_Constraint_Error
(Loc
,
2844 Make_Attribute_Reference
(Loc
,
2846 Duplicate_Subexpr
(Exp
, Name_Req
=> True),
2847 Attribute_Name
=> Name_Valid
)),
2848 Reason
=> CE_Invalid_Data
),
2849 Suppress
=> All_Checks
);
2850 Validity_Checks_On
:= True;
2851 end Insert_Valid_Check
;
2853 --------------------------
2854 -- Install_Static_Check --
2855 --------------------------
2857 procedure Install_Static_Check
(R_Cno
: Node_Id
; Loc
: Source_Ptr
) is
2858 Stat
: constant Boolean := Is_Static_Expression
(R_Cno
);
2859 Typ
: constant Entity_Id
:= Etype
(R_Cno
);
2863 Make_Raise_Constraint_Error
(Loc
,
2864 Reason
=> CE_Range_Check_Failed
));
2865 Set_Analyzed
(R_Cno
);
2866 Set_Etype
(R_Cno
, Typ
);
2867 Set_Raises_Constraint_Error
(R_Cno
);
2868 Set_Is_Static_Expression
(R_Cno
, Stat
);
2869 end Install_Static_Check
;
2871 ------------------------------
2872 -- Length_Checks_Suppressed --
2873 ------------------------------
2875 function Length_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2877 return Scope_Suppress
.Length_Checks
2878 or else (Present
(E
) and then Suppress_Length_Checks
(E
));
2879 end Length_Checks_Suppressed
;
2881 --------------------------------
2882 -- Overflow_Checks_Suppressed --
2883 --------------------------------
2885 function Overflow_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2887 return Scope_Suppress
.Overflow_Checks
2888 or else (Present
(E
) and then Suppress_Overflow_Checks
(E
));
2889 end Overflow_Checks_Suppressed
;
2895 function Range_Check
2897 Target_Typ
: Entity_Id
;
2898 Source_Typ
: Entity_Id
:= Empty
;
2899 Warn_Node
: Node_Id
:= Empty
)
2903 return Selected_Range_Checks
2904 (Ck_Node
, Target_Typ
, Source_Typ
, Warn_Node
);
2907 -----------------------------
2908 -- Range_Checks_Suppressed --
2909 -----------------------------
2911 function Range_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
2913 -- Note: for now we always suppress range checks on Vax float types,
2914 -- since Gigi does not know how to generate these checks.
2916 return Scope_Suppress
.Range_Checks
2917 or else (Present
(E
) and then Suppress_Range_Checks
(E
))
2918 or else Vax_Float
(E
);
2919 end Range_Checks_Suppressed
;
2921 ----------------------------
2922 -- Selected_Length_Checks --
2923 ----------------------------
2925 function Selected_Length_Checks
2927 Target_Typ
: Entity_Id
;
2928 Source_Typ
: Entity_Id
;
2929 Warn_Node
: Node_Id
)
2932 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
2935 Expr_Actual
: Node_Id
;
2937 Cond
: Node_Id
:= Empty
;
2938 Do_Access
: Boolean := False;
2939 Wnode
: Node_Id
:= Warn_Node
;
2940 Ret_Result
: Check_Result
:= (Empty
, Empty
);
2941 Num_Checks
: Natural := 0;
2943 procedure Add_Check
(N
: Node_Id
);
2944 -- Adds the action given to Ret_Result if N is non-Empty
2946 function Get_E_Length
(E
: Entity_Id
; Indx
: Nat
) return Node_Id
;
2947 function Get_N_Length
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
2949 function Same_Bounds
(L
: Node_Id
; R
: Node_Id
) return Boolean;
2950 -- True for equal literals and for nodes that denote the same constant
2951 -- entity, even if its value is not a static constant. This includes the
2952 -- case of a discriminal reference within an init_proc. Removes some
2953 -- obviously superfluous checks.
2955 function Length_E_Cond
2956 (Exptyp
: Entity_Id
;
2960 -- Returns expression to compute:
2961 -- Typ'Length /= Exptyp'Length
2963 function Length_N_Cond
2968 -- Returns expression to compute:
2969 -- Typ'Length /= Expr'Length
2975 procedure Add_Check
(N
: Node_Id
) is
2979 -- For now, ignore attempt to place more than 2 checks ???
2981 if Num_Checks
= 2 then
2985 pragma Assert
(Num_Checks
<= 1);
2986 Num_Checks
:= Num_Checks
+ 1;
2987 Ret_Result
(Num_Checks
) := N
;
2995 function Get_E_Length
(E
: Entity_Id
; Indx
: Nat
) return Node_Id
is
2997 E1
: Entity_Id
:= E
;
2998 Pt
: Entity_Id
:= Scope
(Scope
(E
));
3001 if Ekind
(Scope
(E
)) = E_Record_Type
3002 and then Has_Discriminants
(Scope
(E
))
3004 N
:= Build_Discriminal_Subtype_Of_Component
(E
);
3007 Insert_Action
(Ck_Node
, N
);
3008 E1
:= Defining_Identifier
(N
);
3012 if Ekind
(E1
) = E_String_Literal_Subtype
then
3014 Make_Integer_Literal
(Loc
,
3015 Intval
=> String_Literal_Length
(E1
));
3017 elsif Ekind
(Pt
) = E_Protected_Type
3018 and then Has_Discriminants
(Pt
)
3019 and then Has_Completion
(Pt
)
3020 and then not Inside_Init_Proc
3023 -- If the type whose length is needed is a private component
3024 -- constrained by a discriminant, we must expand the 'Length
3025 -- attribute into an explicit computation, using the discriminal
3026 -- of the current protected operation. This is because the actual
3027 -- type of the prival is constructed after the protected opera-
3028 -- tion has been fully expanded.
3031 Indx_Type
: Node_Id
;
3034 Do_Expand
: Boolean := False;
3037 Indx_Type
:= First_Index
(E
);
3039 for J
in 1 .. Indx
- 1 loop
3040 Next_Index
(Indx_Type
);
3043 Get_Index_Bounds
(Indx_Type
, Lo
, Hi
);
3045 if Nkind
(Lo
) = N_Identifier
3046 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
3048 Lo
:= Get_Discriminal
(E
, Lo
);
3052 if Nkind
(Hi
) = N_Identifier
3053 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
3055 Hi
:= Get_Discriminal
(E
, Hi
);
3060 if not Is_Entity_Name
(Lo
) then
3061 Lo
:= Duplicate_Subexpr
(Lo
);
3064 if not Is_Entity_Name
(Hi
) then
3065 Lo
:= Duplicate_Subexpr
(Hi
);
3071 Make_Op_Subtract
(Loc
,
3075 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
3080 Make_Attribute_Reference
(Loc
,
3081 Attribute_Name
=> Name_Length
,
3083 New_Occurrence_Of
(E1
, Loc
));
3086 Set_Expressions
(N
, New_List
(
3087 Make_Integer_Literal
(Loc
, Indx
)));
3096 Make_Attribute_Reference
(Loc
,
3097 Attribute_Name
=> Name_Length
,
3099 New_Occurrence_Of
(E1
, Loc
));
3102 Set_Expressions
(N
, New_List
(
3103 Make_Integer_Literal
(Loc
, Indx
)));
3115 function Get_N_Length
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
3118 Make_Attribute_Reference
(Loc
,
3119 Attribute_Name
=> Name_Length
,
3121 Duplicate_Subexpr
(N
, Name_Req
=> True),
3122 Expressions
=> New_List
(
3123 Make_Integer_Literal
(Loc
, Indx
)));
3131 function Length_E_Cond
3132 (Exptyp
: Entity_Id
;
3140 Left_Opnd
=> Get_E_Length
(Typ
, Indx
),
3141 Right_Opnd
=> Get_E_Length
(Exptyp
, Indx
));
3149 function Length_N_Cond
3158 Left_Opnd
=> Get_E_Length
(Typ
, Indx
),
3159 Right_Opnd
=> Get_N_Length
(Expr
, Indx
));
3163 function Same_Bounds
(L
: Node_Id
; R
: Node_Id
) return Boolean is
3166 (Nkind
(L
) = N_Integer_Literal
3167 and then Nkind
(R
) = N_Integer_Literal
3168 and then Intval
(L
) = Intval
(R
))
3172 and then Ekind
(Entity
(L
)) = E_Constant
3173 and then ((Is_Entity_Name
(R
)
3174 and then Entity
(L
) = Entity
(R
))
3176 (Nkind
(R
) = N_Type_Conversion
3177 and then Is_Entity_Name
(Expression
(R
))
3178 and then Entity
(L
) = Entity
(Expression
(R
)))))
3182 and then Ekind
(Entity
(R
)) = E_Constant
3183 and then Nkind
(L
) = N_Type_Conversion
3184 and then Is_Entity_Name
(Expression
(L
))
3185 and then Entity
(R
) = Entity
(Expression
(L
)))
3189 and then Is_Entity_Name
(R
)
3190 and then Entity
(L
) = Entity
(R
)
3191 and then Ekind
(Entity
(L
)) = E_In_Parameter
3192 and then Inside_Init_Proc
);
3195 -- Start of processing for Selected_Length_Checks
3198 if not Expander_Active
then
3202 if Target_Typ
= Any_Type
3203 or else Target_Typ
= Any_Composite
3204 or else Raises_Constraint_Error
(Ck_Node
)
3213 T_Typ
:= Target_Typ
;
3215 if No
(Source_Typ
) then
3216 S_Typ
:= Etype
(Ck_Node
);
3218 S_Typ
:= Source_Typ
;
3221 if S_Typ
= Any_Type
or else S_Typ
= Any_Composite
then
3225 if Is_Access_Type
(T_Typ
) and then Is_Access_Type
(S_Typ
) then
3226 S_Typ
:= Designated_Type
(S_Typ
);
3227 T_Typ
:= Designated_Type
(T_Typ
);
3230 -- A simple optimization
3232 if Nkind
(Ck_Node
) = N_Null
then
3237 if Is_Array_Type
(T_Typ
) and then Is_Array_Type
(S_Typ
) then
3238 if Is_Constrained
(T_Typ
) then
3240 -- The checking code to be generated will freeze the
3241 -- corresponding array type. However, we must freeze the
3242 -- type now, so that the freeze node does not appear within
3243 -- the generated condional expression, but ahead of it.
3245 Freeze_Before
(Ck_Node
, T_Typ
);
3247 Expr_Actual
:= Get_Referenced_Object
(Ck_Node
);
3248 Exptyp
:= Get_Actual_Subtype
(Expr_Actual
);
3250 if Is_Access_Type
(Exptyp
) then
3251 Exptyp
:= Designated_Type
(Exptyp
);
3254 -- String_Literal case. This needs to be handled specially be-
3255 -- cause no index types are available for string literals. The
3256 -- condition is simply:
3258 -- T_Typ'Length = string-literal-length
3260 if Nkind
(Expr_Actual
) = N_String_Literal
then
3263 Left_Opnd
=> Get_E_Length
(T_Typ
, 1),
3265 Make_Integer_Literal
(Loc
,
3267 String_Literal_Length
(Etype
(Expr_Actual
))));
3269 -- General array case. Here we have a usable actual subtype for
3270 -- the expression, and the condition is built from the two types
3273 -- T_Typ'Length /= Exptyp'Length or else
3274 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
3275 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
3278 elsif Is_Constrained
(Exptyp
) then
3282 Ndims
: Nat
:= Number_Dimensions
(T_Typ
);
3293 L_Index
:= First_Index
(T_Typ
);
3294 R_Index
:= First_Index
(Exptyp
);
3296 for Indx
in 1 .. Ndims
loop
3297 if not (Nkind
(L_Index
) = N_Raise_Constraint_Error
3299 Nkind
(R_Index
) = N_Raise_Constraint_Error
)
3301 Get_Index_Bounds
(L_Index
, L_Low
, L_High
);
3302 Get_Index_Bounds
(R_Index
, R_Low
, R_High
);
3304 -- Deal with compile time length check. Note that we
3305 -- skip this in the access case, because the access
3306 -- value may be null, so we cannot know statically.
3309 and then Compile_Time_Known_Value
(L_Low
)
3310 and then Compile_Time_Known_Value
(L_High
)
3311 and then Compile_Time_Known_Value
(R_Low
)
3312 and then Compile_Time_Known_Value
(R_High
)
3314 if Expr_Value
(L_High
) >= Expr_Value
(L_Low
) then
3315 L_Length
:= Expr_Value
(L_High
) -
3316 Expr_Value
(L_Low
) + 1;
3318 L_Length
:= UI_From_Int
(0);
3321 if Expr_Value
(R_High
) >= Expr_Value
(R_Low
) then
3322 R_Length
:= Expr_Value
(R_High
) -
3323 Expr_Value
(R_Low
) + 1;
3325 R_Length
:= UI_From_Int
(0);
3328 if L_Length
> R_Length
then
3330 (Compile_Time_Constraint_Error
3331 (Wnode
, "too few elements for}?", T_Typ
));
3333 elsif L_Length
< R_Length
then
3335 (Compile_Time_Constraint_Error
3336 (Wnode
, "too many elements for}?", T_Typ
));
3339 -- The comparison for an individual index subtype
3340 -- is omitted if the corresponding index subtypes
3341 -- statically match, since the result is known to
3342 -- be true. Note that this test is worth while even
3343 -- though we do static evaluation, because non-static
3344 -- subtypes can statically match.
3347 Subtypes_Statically_Match
3348 (Etype
(L_Index
), Etype
(R_Index
))
3351 (Same_Bounds
(L_Low
, R_Low
)
3352 and then Same_Bounds
(L_High
, R_High
))
3355 (Cond
, Length_E_Cond
(Exptyp
, T_Typ
, Indx
));
3364 -- Handle cases where we do not get a usable actual subtype that
3365 -- is constrained. This happens for example in the function call
3366 -- and explicit dereference cases. In these cases, we have to get
3367 -- the length or range from the expression itself, making sure we
3368 -- do not evaluate it more than once.
3370 -- Here Ck_Node is the original expression, or more properly the
3371 -- result of applying Duplicate_Expr to the original tree,
3372 -- forcing the result to be a name.
3376 Ndims
: Nat
:= Number_Dimensions
(T_Typ
);
3379 -- Build the condition for the explicit dereference case
3381 for Indx
in 1 .. Ndims
loop
3383 (Cond
, Length_N_Cond
(Ck_Node
, T_Typ
, Indx
));
3390 -- Construct the test and insert into the tree
3392 if Present
(Cond
) then
3394 Cond
:= Guard_Access
(Cond
, Loc
, Ck_Node
);
3398 (Make_Raise_Constraint_Error
(Loc
,
3400 Reason
=> CE_Length_Check_Failed
));
3404 end Selected_Length_Checks
;
3406 ---------------------------
3407 -- Selected_Range_Checks --
3408 ---------------------------
3410 function Selected_Range_Checks
3412 Target_Typ
: Entity_Id
;
3413 Source_Typ
: Entity_Id
;
3414 Warn_Node
: Node_Id
)
3417 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
3420 Expr_Actual
: Node_Id
;
3422 Cond
: Node_Id
:= Empty
;
3423 Do_Access
: Boolean := False;
3424 Wnode
: Node_Id
:= Warn_Node
;
3425 Ret_Result
: Check_Result
:= (Empty
, Empty
);
3426 Num_Checks
: Integer := 0;
3428 procedure Add_Check
(N
: Node_Id
);
3429 -- Adds the action given to Ret_Result if N is non-Empty
3431 function Discrete_Range_Cond
3435 -- Returns expression to compute:
3436 -- Low_Bound (Expr) < Typ'First
3438 -- High_Bound (Expr) > Typ'Last
3440 function Discrete_Expr_Cond
3444 -- Returns expression to compute:
3449 function Get_E_First_Or_Last
3454 -- Returns expression to compute:
3455 -- E'First or E'Last
3457 function Get_N_First
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
3458 function Get_N_Last
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
3459 -- Returns expression to compute:
3460 -- N'First or N'Last using Duplicate_Subexpr
3462 function Range_E_Cond
3463 (Exptyp
: Entity_Id
;
3467 -- Returns expression to compute:
3468 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
3470 function Range_Equal_E_Cond
3471 (Exptyp
: Entity_Id
;
3475 -- Returns expression to compute:
3476 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
3478 function Range_N_Cond
3483 -- Return expression to compute:
3484 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
3490 procedure Add_Check
(N
: Node_Id
) is
3494 -- For now, ignore attempt to place more than 2 checks ???
3496 if Num_Checks
= 2 then
3500 pragma Assert
(Num_Checks
<= 1);
3501 Num_Checks
:= Num_Checks
+ 1;
3502 Ret_Result
(Num_Checks
) := N
;
3506 -------------------------
3507 -- Discrete_Expr_Cond --
3508 -------------------------
3510 function Discrete_Expr_Cond
3521 Convert_To
(Base_Type
(Typ
), Duplicate_Subexpr
(Expr
)),
3523 Convert_To
(Base_Type
(Typ
),
3524 Get_E_First_Or_Last
(Typ
, 0, Name_First
))),
3529 Convert_To
(Base_Type
(Typ
), Duplicate_Subexpr
(Expr
)),
3533 Get_E_First_Or_Last
(Typ
, 0, Name_Last
))));
3534 end Discrete_Expr_Cond
;
3536 -------------------------
3537 -- Discrete_Range_Cond --
3538 -------------------------
3540 function Discrete_Range_Cond
3545 LB
: Node_Id
:= Low_Bound
(Expr
);
3546 HB
: Node_Id
:= High_Bound
(Expr
);
3548 Left_Opnd
: Node_Id
;
3549 Right_Opnd
: Node_Id
;
3552 if Nkind
(LB
) = N_Identifier
3553 and then Ekind
(Entity
(LB
)) = E_Discriminant
then
3554 LB
:= New_Occurrence_Of
(Discriminal
(Entity
(LB
)), Loc
);
3557 if Nkind
(HB
) = N_Identifier
3558 and then Ekind
(Entity
(HB
)) = E_Discriminant
then
3559 HB
:= New_Occurrence_Of
(Discriminal
(Entity
(HB
)), Loc
);
3566 (Base_Type
(Typ
), Duplicate_Subexpr
(LB
)),
3570 (Base_Type
(Typ
), Get_E_First_Or_Last
(Typ
, 0, Name_First
)));
3572 if Base_Type
(Typ
) = Typ
then
3575 elsif Compile_Time_Known_Value
(High_Bound
(Scalar_Range
(Typ
)))
3577 Compile_Time_Known_Value
(High_Bound
(Scalar_Range
3580 if Is_Floating_Point_Type
(Typ
) then
3581 if Expr_Value_R
(High_Bound
(Scalar_Range
(Typ
))) =
3582 Expr_Value_R
(High_Bound
(Scalar_Range
(Base_Type
(Typ
))))
3588 if Expr_Value
(High_Bound
(Scalar_Range
(Typ
))) =
3589 Expr_Value
(High_Bound
(Scalar_Range
(Base_Type
(Typ
))))
3600 (Base_Type
(Typ
), Duplicate_Subexpr
(HB
)),
3605 Get_E_First_Or_Last
(Typ
, 0, Name_Last
)));
3607 return Make_Or_Else
(Loc
, Left_Opnd
, Right_Opnd
);
3608 end Discrete_Range_Cond
;
3610 -------------------------
3611 -- Get_E_First_Or_Last --
3612 -------------------------
3614 function Get_E_First_Or_Last
3626 if Is_Array_Type
(E
) then
3627 N
:= First_Index
(E
);
3629 for J
in 2 .. Indx
loop
3634 N
:= Scalar_Range
(E
);
3637 if Nkind
(N
) = N_Subtype_Indication
then
3638 LB
:= Low_Bound
(Range_Expression
(Constraint
(N
)));
3639 HB
:= High_Bound
(Range_Expression
(Constraint
(N
)));
3641 elsif Is_Entity_Name
(N
) then
3642 LB
:= Type_Low_Bound
(Etype
(N
));
3643 HB
:= Type_High_Bound
(Etype
(N
));
3646 LB
:= Low_Bound
(N
);
3647 HB
:= High_Bound
(N
);
3650 if Nam
= Name_First
then
3656 if Nkind
(Bound
) = N_Identifier
3657 and then Ekind
(Entity
(Bound
)) = E_Discriminant
3659 return New_Occurrence_Of
(Discriminal
(Entity
(Bound
)), Loc
);
3661 elsif Nkind
(Bound
) = N_Identifier
3662 and then Ekind
(Entity
(Bound
)) = E_In_Parameter
3663 and then not Inside_Init_Proc
3665 return Get_Discriminal
(E
, Bound
);
3667 elsif Nkind
(Bound
) = N_Integer_Literal
then
3668 return Make_Integer_Literal
(Loc
, Intval
(Bound
));
3671 return Duplicate_Subexpr
(Bound
);
3673 end Get_E_First_Or_Last
;
3679 function Get_N_First
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
3682 Make_Attribute_Reference
(Loc
,
3683 Attribute_Name
=> Name_First
,
3685 Duplicate_Subexpr
(N
, Name_Req
=> True),
3686 Expressions
=> New_List
(
3687 Make_Integer_Literal
(Loc
, Indx
)));
3695 function Get_N_Last
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
3698 Make_Attribute_Reference
(Loc
,
3699 Attribute_Name
=> Name_Last
,
3701 Duplicate_Subexpr
(N
, Name_Req
=> True),
3702 Expressions
=> New_List
(
3703 Make_Integer_Literal
(Loc
, Indx
)));
3711 function Range_E_Cond
3712 (Exptyp
: Entity_Id
;
3722 Left_Opnd
=> Get_E_First_Or_Last
(Exptyp
, Indx
, Name_First
),
3723 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_First
)),
3727 Left_Opnd
=> Get_E_First_Or_Last
(Exptyp
, Indx
, Name_Last
),
3728 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_Last
)));
3732 ------------------------
3733 -- Range_Equal_E_Cond --
3734 ------------------------
3736 function Range_Equal_E_Cond
3737 (Exptyp
: Entity_Id
;
3747 Left_Opnd
=> Get_E_First_Or_Last
(Exptyp
, Indx
, Name_First
),
3748 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_First
)),
3751 Left_Opnd
=> Get_E_First_Or_Last
(Exptyp
, Indx
, Name_Last
),
3752 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_Last
)));
3753 end Range_Equal_E_Cond
;
3759 function Range_N_Cond
3770 Left_Opnd
=> Get_N_First
(Expr
, Indx
),
3771 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_First
)),
3775 Left_Opnd
=> Get_N_Last
(Expr
, Indx
),
3776 Right_Opnd
=> Get_E_First_Or_Last
(Typ
, Indx
, Name_Last
)));
3779 -- Start of processing for Selected_Range_Checks
3782 if not Expander_Active
then
3786 if Target_Typ
= Any_Type
3787 or else Target_Typ
= Any_Composite
3788 or else Raises_Constraint_Error
(Ck_Node
)
3797 T_Typ
:= Target_Typ
;
3799 if No
(Source_Typ
) then
3800 S_Typ
:= Etype
(Ck_Node
);
3802 S_Typ
:= Source_Typ
;
3805 if S_Typ
= Any_Type
or else S_Typ
= Any_Composite
then
3809 -- The order of evaluating T_Typ before S_Typ seems to be critical
3810 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
3811 -- in, and since Node can be an N_Range node, it might be invalid.
3812 -- Should there be an assert check somewhere for taking the Etype of
3813 -- an N_Range node ???
3815 if Is_Access_Type
(T_Typ
) and then Is_Access_Type
(S_Typ
) then
3816 S_Typ
:= Designated_Type
(S_Typ
);
3817 T_Typ
:= Designated_Type
(T_Typ
);
3820 -- A simple optimization
3822 if Nkind
(Ck_Node
) = N_Null
then
3827 -- For an N_Range Node, check for a null range and then if not
3828 -- null generate a range check action.
3830 if Nkind
(Ck_Node
) = N_Range
then
3832 -- There's no point in checking a range against itself
3834 if Ck_Node
= Scalar_Range
(T_Typ
) then
3839 T_LB
: constant Node_Id
:= Type_Low_Bound
(T_Typ
);
3840 T_HB
: constant Node_Id
:= Type_High_Bound
(T_Typ
);
3841 LB
: constant Node_Id
:= Low_Bound
(Ck_Node
);
3842 HB
: constant Node_Id
:= High_Bound
(Ck_Node
);
3843 Null_Range
: Boolean;
3845 Out_Of_Range_L
: Boolean;
3846 Out_Of_Range_H
: Boolean;
3849 -- Check for case where everything is static and we can
3850 -- do the check at compile time. This is skipped if we
3851 -- have an access type, since the access value may be null.
3853 -- ??? This code can be improved since you only need to know
3854 -- that the two respective bounds (LB & T_LB or HB & T_HB)
3855 -- are known at compile time to emit pertinent messages.
3857 if Compile_Time_Known_Value
(LB
)
3858 and then Compile_Time_Known_Value
(HB
)
3859 and then Compile_Time_Known_Value
(T_LB
)
3860 and then Compile_Time_Known_Value
(T_HB
)
3861 and then not Do_Access
3863 -- Floating-point case
3865 if Is_Floating_Point_Type
(S_Typ
) then
3866 Null_Range
:= Expr_Value_R
(HB
) < Expr_Value_R
(LB
);
3868 (Expr_Value_R
(LB
) < Expr_Value_R
(T_LB
))
3870 (Expr_Value_R
(LB
) > Expr_Value_R
(T_HB
));
3873 (Expr_Value_R
(HB
) > Expr_Value_R
(T_HB
))
3875 (Expr_Value_R
(HB
) < Expr_Value_R
(T_LB
));
3877 -- Fixed or discrete type case
3880 Null_Range
:= Expr_Value
(HB
) < Expr_Value
(LB
);
3882 (Expr_Value
(LB
) < Expr_Value
(T_LB
))
3884 (Expr_Value
(LB
) > Expr_Value
(T_HB
));
3887 (Expr_Value
(HB
) > Expr_Value
(T_HB
))
3889 (Expr_Value
(HB
) < Expr_Value
(T_LB
));
3892 if not Null_Range
then
3893 if Out_Of_Range_L
then
3894 if No
(Warn_Node
) then
3896 (Compile_Time_Constraint_Error
3897 (Low_Bound
(Ck_Node
),
3898 "static value out of range of}?", T_Typ
));
3902 (Compile_Time_Constraint_Error
3904 "static range out of bounds of}?", T_Typ
));
3908 if Out_Of_Range_H
then
3909 if No
(Warn_Node
) then
3911 (Compile_Time_Constraint_Error
3912 (High_Bound
(Ck_Node
),
3913 "static value out of range of}?", T_Typ
));
3917 (Compile_Time_Constraint_Error
3919 "static range out of bounds of}?", T_Typ
));
3927 LB
: Node_Id
:= Low_Bound
(Ck_Node
);
3928 HB
: Node_Id
:= High_Bound
(Ck_Node
);
3932 -- If either bound is a discriminant and we are within
3933 -- the record declaration, it is a use of the discriminant
3934 -- in a constraint of a component, and nothing can be
3935 -- checked here. The check will be emitted within the
3936 -- init_proc. Before then, the discriminal has no real
3939 if Nkind
(LB
) = N_Identifier
3940 and then Ekind
(Entity
(LB
)) = E_Discriminant
3942 if Current_Scope
= Scope
(Entity
(LB
)) then
3946 New_Occurrence_Of
(Discriminal
(Entity
(LB
)), Loc
);
3950 if Nkind
(HB
) = N_Identifier
3951 and then Ekind
(Entity
(HB
)) = E_Discriminant
3953 if Current_Scope
= Scope
(Entity
(HB
)) then
3957 New_Occurrence_Of
(Discriminal
(Entity
(HB
)), Loc
);
3961 Cond
:= Discrete_Range_Cond
(Ck_Node
, T_Typ
);
3962 Set_Paren_Count
(Cond
, 1);
3968 Left_Opnd
=> Duplicate_Subexpr
(HB
),
3969 Right_Opnd
=> Duplicate_Subexpr
(LB
)),
3970 Right_Opnd
=> Cond
);
3976 elsif Is_Scalar_Type
(S_Typ
) then
3978 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
3979 -- except the above simply sets a flag in the node and lets
3980 -- gigi generate the check base on the Etype of the expression.
3981 -- Sometimes, however we want to do a dynamic check against an
3982 -- arbitrary target type, so we do that here.
3984 if Ekind
(Base_Type
(S_Typ
)) /= Ekind
(Base_Type
(T_Typ
)) then
3985 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
3987 -- For literals, we can tell if the constraint error will be
3988 -- raised at compile time, so we never need a dynamic check, but
3989 -- if the exception will be raised, then post the usual warning,
3990 -- and replace the literal with a raise constraint error
3991 -- expression. As usual, skip this for access types
3993 elsif Compile_Time_Known_Value
(Ck_Node
)
3994 and then not Do_Access
3997 LB
: constant Node_Id
:= Type_Low_Bound
(T_Typ
);
3998 UB
: constant Node_Id
:= Type_High_Bound
(T_Typ
);
4000 Out_Of_Range
: Boolean;
4001 Static_Bounds
: constant Boolean :=
4002 Compile_Time_Known_Value
(LB
)
4003 and Compile_Time_Known_Value
(UB
);
4006 -- Following range tests should use Sem_Eval routine ???
4008 if Static_Bounds
then
4009 if Is_Floating_Point_Type
(S_Typ
) then
4011 (Expr_Value_R
(Ck_Node
) < Expr_Value_R
(LB
))
4013 (Expr_Value_R
(Ck_Node
) > Expr_Value_R
(UB
));
4015 else -- fixed or discrete type
4017 Expr_Value
(Ck_Node
) < Expr_Value
(LB
)
4019 Expr_Value
(Ck_Node
) > Expr_Value
(UB
);
4022 -- Bounds of the type are static and the literal is
4023 -- out of range so make a warning message.
4025 if Out_Of_Range
then
4026 if No
(Warn_Node
) then
4028 (Compile_Time_Constraint_Error
4030 "static value out of range of}?", T_Typ
));
4034 (Compile_Time_Constraint_Error
4036 "static value out of range of}?", T_Typ
));
4041 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
4045 -- Here for the case of a non-static expression, we need a runtime
4046 -- check unless the source type range is guaranteed to be in the
4047 -- range of the target type.
4050 if not In_Subrange_Of
(S_Typ
, T_Typ
) then
4051 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
4056 if Is_Array_Type
(T_Typ
) and then Is_Array_Type
(S_Typ
) then
4057 if Is_Constrained
(T_Typ
) then
4059 Expr_Actual
:= Get_Referenced_Object
(Ck_Node
);
4060 Exptyp
:= Get_Actual_Subtype
(Expr_Actual
);
4062 if Is_Access_Type
(Exptyp
) then
4063 Exptyp
:= Designated_Type
(Exptyp
);
4066 -- String_Literal case. This needs to be handled specially be-
4067 -- cause no index types are available for string literals. The
4068 -- condition is simply:
4070 -- T_Typ'Length = string-literal-length
4072 if Nkind
(Expr_Actual
) = N_String_Literal
then
4075 -- General array case. Here we have a usable actual subtype for
4076 -- the expression, and the condition is built from the two types
4078 -- T_Typ'First < Exptyp'First or else
4079 -- T_Typ'Last > Exptyp'Last or else
4080 -- T_Typ'First(1) < Exptyp'First(1) or else
4081 -- T_Typ'Last(1) > Exptyp'Last(1) or else
4084 elsif Is_Constrained
(Exptyp
) then
4088 Ndims
: Nat
:= Number_Dimensions
(T_Typ
);
4096 L_Index
:= First_Index
(T_Typ
);
4097 R_Index
:= First_Index
(Exptyp
);
4099 for Indx
in 1 .. Ndims
loop
4100 if not (Nkind
(L_Index
) = N_Raise_Constraint_Error
4102 Nkind
(R_Index
) = N_Raise_Constraint_Error
)
4104 Get_Index_Bounds
(L_Index
, L_Low
, L_High
);
4105 Get_Index_Bounds
(R_Index
, R_Low
, R_High
);
4107 -- Deal with compile time length check. Note that we
4108 -- skip this in the access case, because the access
4109 -- value may be null, so we cannot know statically.
4112 Subtypes_Statically_Match
4113 (Etype
(L_Index
), Etype
(R_Index
))
4115 -- If the target type is constrained then we
4116 -- have to check for exact equality of bounds
4117 -- (required for qualified expressions).
4119 if Is_Constrained
(T_Typ
) then
4122 Range_Equal_E_Cond
(Exptyp
, T_Typ
, Indx
));
4126 (Cond
, Range_E_Cond
(Exptyp
, T_Typ
, Indx
));
4137 -- Handle cases where we do not get a usable actual subtype that
4138 -- is constrained. This happens for example in the function call
4139 -- and explicit dereference cases. In these cases, we have to get
4140 -- the length or range from the expression itself, making sure we
4141 -- do not evaluate it more than once.
4143 -- Here Ck_Node is the original expression, or more properly the
4144 -- result of applying Duplicate_Expr to the original tree,
4145 -- forcing the result to be a name.
4149 Ndims
: Nat
:= Number_Dimensions
(T_Typ
);
4152 -- Build the condition for the explicit dereference case
4154 for Indx
in 1 .. Ndims
loop
4156 (Cond
, Range_N_Cond
(Ck_Node
, T_Typ
, Indx
));
4163 -- Generate an Action to check that the bounds of the
4164 -- source value are within the constraints imposed by the
4165 -- target type for a conversion to an unconstrained type.
4168 if Nkind
(Parent
(Ck_Node
)) = N_Type_Conversion
then
4170 Opnd_Index
: Node_Id
;
4171 Targ_Index
: Node_Id
;
4175 := First_Index
(Get_Actual_Subtype
(Ck_Node
));
4176 Targ_Index
:= First_Index
(T_Typ
);
4178 while Opnd_Index
/= Empty
loop
4179 if Nkind
(Opnd_Index
) = N_Range
then
4181 (Low_Bound
(Opnd_Index
), Etype
(Targ_Index
))
4184 (High_Bound
(Opnd_Index
), Etype
(Targ_Index
))
4188 elsif Is_Out_Of_Range
4189 (Low_Bound
(Opnd_Index
), Etype
(Targ_Index
))
4192 (High_Bound
(Opnd_Index
), Etype
(Targ_Index
))
4195 (Compile_Time_Constraint_Error
4196 (Wnode
, "value out of range of}?", T_Typ
));
4202 (Opnd_Index
, Etype
(Targ_Index
)));
4206 Next_Index
(Opnd_Index
);
4207 Next_Index
(Targ_Index
);
4214 -- Construct the test and insert into the tree
4216 if Present
(Cond
) then
4218 Cond
:= Guard_Access
(Cond
, Loc
, Ck_Node
);
4222 (Make_Raise_Constraint_Error
(Loc
,
4224 Reason
=> CE_Range_Check_Failed
));
4228 end Selected_Range_Checks
;
4230 -------------------------------
4231 -- Storage_Checks_Suppressed --
4232 -------------------------------
4234 function Storage_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
4236 return Scope_Suppress
.Storage_Checks
4237 or else (Present
(E
) and then Suppress_Storage_Checks
(E
));
4238 end Storage_Checks_Suppressed
;
4240 ---------------------------
4241 -- Tag_Checks_Suppressed --
4242 ---------------------------
4244 function Tag_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
4246 return Scope_Suppress
.Tag_Checks
4247 or else (Present
(E
) and then Suppress_Tag_Checks
(E
));
4248 end Tag_Checks_Suppressed
;