1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Casing
; use Casing
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Eval_Fat
; use Eval_Fat
;
32 with Exp_Ch11
; use Exp_Ch11
;
33 with Exp_Ch2
; use Exp_Ch2
;
34 with Exp_Ch4
; use Exp_Ch4
;
35 with Exp_Pakd
; use Exp_Pakd
;
36 with Exp_Util
; use Exp_Util
;
37 with Expander
; use Expander
;
38 with Freeze
; use Freeze
;
40 with Nlists
; use Nlists
;
41 with Nmake
; use Nmake
;
43 with Output
; use Output
;
44 with Restrict
; use Restrict
;
45 with Rident
; use Rident
;
46 with Rtsfind
; use Rtsfind
;
48 with Sem_Aux
; use Sem_Aux
;
49 with Sem_Ch3
; use Sem_Ch3
;
50 with Sem_Ch8
; use Sem_Ch8
;
51 with Sem_Eval
; use Sem_Eval
;
52 with Sem_Res
; use Sem_Res
;
53 with Sem_Util
; use Sem_Util
;
54 with Sem_Warn
; use Sem_Warn
;
55 with Sinfo
; use Sinfo
;
56 with Sinput
; use Sinput
;
57 with Snames
; use Snames
;
58 with Sprint
; use Sprint
;
59 with Stand
; use Stand
;
60 with Stringt
; use Stringt
;
61 with Targparm
; use Targparm
;
62 with Tbuild
; use Tbuild
;
63 with Ttypes
; use Ttypes
;
64 with Validsw
; use Validsw
;
66 package body Checks
is
68 -- General note: many of these routines are concerned with generating
69 -- checking code to make sure that constraint error is raised at runtime.
70 -- Clearly this code is only needed if the expander is active, since
71 -- otherwise we will not be generating code or going into the runtime
74 -- We therefore disconnect most of these checks if the expander is
75 -- inactive. This has the additional benefit that we do not need to
76 -- worry about the tree being messed up by previous errors (since errors
77 -- turn off expansion anyway).
79 -- There are a few exceptions to the above rule. For instance routines
80 -- such as Apply_Scalar_Range_Check that do not insert any code can be
81 -- safely called even when the Expander is inactive (but Errors_Detected
82 -- is 0). The benefit of executing this code when expansion is off, is
83 -- the ability to emit constraint error warning for static expressions
84 -- even when we are not generating code.
86 -- The above is modified in gnatprove mode to ensure that proper check
87 -- flags are always placed, even if expansion is off.
89 -------------------------------------
90 -- Suppression of Redundant Checks --
91 -------------------------------------
93 -- This unit implements a limited circuit for removal of redundant
94 -- checks. The processing is based on a tracing of simple sequential
95 -- flow. For any sequence of statements, we save expressions that are
96 -- marked to be checked, and then if the same expression appears later
97 -- with the same check, then under certain circumstances, the second
98 -- check can be suppressed.
100 -- Basically, we can suppress the check if we know for certain that
101 -- the previous expression has been elaborated (together with its
102 -- check), and we know that the exception frame is the same, and that
103 -- nothing has happened to change the result of the exception.
105 -- Let us examine each of these three conditions in turn to describe
106 -- how we ensure that this condition is met.
108 -- First, we need to know for certain that the previous expression has
109 -- been executed. This is done principally by the mechanism of calling
110 -- Conditional_Statements_Begin at the start of any statement sequence
111 -- and Conditional_Statements_End at the end. The End call causes all
112 -- checks remembered since the Begin call to be discarded. This does
113 -- miss a few cases, notably the case of a nested BEGIN-END block with
114 -- no exception handlers. But the important thing is to be conservative.
115 -- The other protection is that all checks are discarded if a label
116 -- is encountered, since then the assumption of sequential execution
117 -- is violated, and we don't know enough about the flow.
119 -- Second, we need to know that the exception frame is the same. We
120 -- do this by killing all remembered checks when we enter a new frame.
121 -- Again, that's over-conservative, but generally the cases we can help
122 -- with are pretty local anyway (like the body of a loop for example).
124 -- Third, we must be sure to forget any checks which are no longer valid.
125 -- This is done by two mechanisms, first the Kill_Checks_Variable call is
126 -- used to note any changes to local variables. We only attempt to deal
127 -- with checks involving local variables, so we do not need to worry
128 -- about global variables. Second, a call to any non-global procedure
129 -- causes us to abandon all stored checks, since such a all may affect
130 -- the values of any local variables.
132 -- The following define the data structures used to deal with remembering
133 -- checks so that redundant checks can be eliminated as described above.
135 -- Right now, the only expressions that we deal with are of the form of
136 -- simple local objects (either declared locally, or IN parameters) or
137 -- such objects plus/minus a compile time known constant. We can do
138 -- more later on if it seems worthwhile, but this catches many simple
139 -- cases in practice.
141 -- The following record type reflects a single saved check. An entry
142 -- is made in the stack of saved checks if and only if the expression
143 -- has been elaborated with the indicated checks.
145 type Saved_Check
is record
147 -- Set True if entry is killed by Kill_Checks
150 -- The entity involved in the expression that is checked
153 -- A compile time value indicating the result of adding or
154 -- subtracting a compile time value. This value is to be
155 -- added to the value of the Entity. A value of zero is
156 -- used for the case of a simple entity reference.
158 Check_Type
: Character;
159 -- This is set to 'R' for a range check (in which case Target_Type
160 -- is set to the target type for the range check) or to 'O' for an
161 -- overflow check (in which case Target_Type is set to Empty).
163 Target_Type
: Entity_Id
;
164 -- Used only if Do_Range_Check is set. Records the target type for
165 -- the check. We need this, because a check is a duplicate only if
166 -- it has the same target type (or more accurately one with a
167 -- range that is smaller or equal to the stored target type of a
171 -- The following table keeps track of saved checks. Rather than use an
172 -- extensible table, we just use a table of fixed size, and we discard
173 -- any saved checks that do not fit. That's very unlikely to happen and
174 -- this is only an optimization in any case.
176 Saved_Checks
: array (Int
range 1 .. 200) of Saved_Check
;
177 -- Array of saved checks
179 Num_Saved_Checks
: Nat
:= 0;
180 -- Number of saved checks
182 -- The following stack keeps track of statement ranges. It is treated
183 -- as a stack. When Conditional_Statements_Begin is called, an entry
184 -- is pushed onto this stack containing the value of Num_Saved_Checks
185 -- at the time of the call. Then when Conditional_Statements_End is
186 -- called, this value is popped off and used to reset Num_Saved_Checks.
188 -- Note: again, this is a fixed length stack with a size that should
189 -- always be fine. If the value of the stack pointer goes above the
190 -- limit, then we just forget all saved checks.
192 Saved_Checks_Stack
: array (Int
range 1 .. 100) of Nat
;
193 Saved_Checks_TOS
: Nat
:= 0;
195 -----------------------
196 -- Local Subprograms --
197 -----------------------
199 procedure Apply_Arithmetic_Overflow_Strict
(N
: Node_Id
);
200 -- Used to apply arithmetic overflow checks for all cases except operators
201 -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we
202 -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a
203 -- signed integer arithmetic operator (but not an if or case expression).
204 -- It is also called for types other than signed integers.
206 procedure Apply_Arithmetic_Overflow_Minimized_Eliminated
(Op
: Node_Id
);
207 -- Used to apply arithmetic overflow checks for the case where the overflow
208 -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer
209 -- arithmetic op (which includes the case of if and case expressions). Note
210 -- that Do_Overflow_Check may or may not be set for node Op. In these modes
211 -- we have work to do even if overflow checking is suppressed.
213 procedure Apply_Division_Check
218 -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies
219 -- division checks as required if the Do_Division_Check flag is set.
220 -- Rlo and Rhi give the possible range of the right operand, these values
221 -- can be referenced and trusted only if ROK is set True.
223 procedure Apply_Float_Conversion_Check
225 Target_Typ
: Entity_Id
);
226 -- The checks on a conversion from a floating-point type to an integer
227 -- type are delicate. They have to be performed before conversion, they
228 -- have to raise an exception when the operand is a NaN, and rounding must
229 -- be taken into account to determine the safe bounds of the operand.
231 procedure Apply_Selected_Length_Checks
233 Target_Typ
: Entity_Id
;
234 Source_Typ
: Entity_Id
;
235 Do_Static
: Boolean);
236 -- This is the subprogram that does all the work for Apply_Length_Check
237 -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
238 -- described for the above routines. The Do_Static flag indicates that
239 -- only a static check is to be done.
241 procedure Apply_Selected_Range_Checks
243 Target_Typ
: Entity_Id
;
244 Source_Typ
: Entity_Id
;
245 Do_Static
: Boolean);
246 -- This is the subprogram that does all the work for Apply_Range_Check.
247 -- Expr, Target_Typ and Source_Typ are as described for the above
248 -- routine. The Do_Static flag indicates that only a static check is
251 type Check_Type
is new Check_Id
range Access_Check
.. Division_Check
;
252 function Check_Needed
(Nod
: Node_Id
; Check
: Check_Type
) return Boolean;
253 -- This function is used to see if an access or division by zero check is
254 -- needed. The check is to be applied to a single variable appearing in the
255 -- source, and N is the node for the reference. If N is not of this form,
256 -- True is returned with no further processing. If N is of the right form,
257 -- then further processing determines if the given Check is needed.
259 -- The particular circuit is to see if we have the case of a check that is
260 -- not needed because it appears in the right operand of a short circuited
261 -- conditional where the left operand guards the check. For example:
263 -- if Var = 0 or else Q / Var > 12 then
267 -- In this example, the division check is not required. At the same time
268 -- we can issue warnings for suspicious use of non-short-circuited forms,
271 -- if Var = 0 or Q / Var > 12 then
277 Check_Type
: Character;
278 Target_Type
: Entity_Id
;
279 Entry_OK
: out Boolean;
283 -- This routine is used by Enable_Range_Check and Enable_Overflow_Check
284 -- to see if a check is of the form for optimization, and if so, to see
285 -- if it has already been performed. Expr is the expression to check,
286 -- and Check_Type is 'R' for a range check, 'O' for an overflow check.
287 -- Target_Type is the target type for a range check, and Empty for an
288 -- overflow check. If the entry is not of the form for optimization,
289 -- then Entry_OK is set to False, and the remaining out parameters
290 -- are undefined. If the entry is OK, then Ent/Ofs are set to the
291 -- entity and offset from the expression. Check_Num is the number of
292 -- a matching saved entry in Saved_Checks, or zero if no such entry
295 function Get_Discriminal
(E
: Entity_Id
; Bound
: Node_Id
) return Node_Id
;
296 -- If a discriminal is used in constraining a prival, Return reference
297 -- to the discriminal of the protected body (which renames the parameter
298 -- of the enclosing protected operation). This clumsy transformation is
299 -- needed because privals are created too late and their actual subtypes
300 -- are not available when analysing the bodies of the protected operations.
301 -- This function is called whenever the bound is an entity and the scope
302 -- indicates a protected operation. If the bound is an in-parameter of
303 -- a protected operation that is not a prival, the function returns the
305 -- To be cleaned up???
307 function Guard_Access
310 Ck_Node
: Node_Id
) return Node_Id
;
311 -- In the access type case, guard the test with a test to ensure
312 -- that the access value is non-null, since the checks do not
313 -- not apply to null access values.
315 procedure Install_Static_Check
(R_Cno
: Node_Id
; Loc
: Source_Ptr
);
316 -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
317 -- Constraint_Error node.
319 function Is_Signed_Integer_Arithmetic_Op
(N
: Node_Id
) return Boolean;
320 -- Returns True if node N is for an arithmetic operation with signed
321 -- integer operands. This includes unary and binary operators, and also
322 -- if and case expression nodes where the dependent expressions are of
323 -- a signed integer type. These are the kinds of nodes for which special
324 -- handling applies in MINIMIZED or ELIMINATED overflow checking mode.
326 function Range_Or_Validity_Checks_Suppressed
327 (Expr
: Node_Id
) return Boolean;
328 -- Returns True if either range or validity checks or both are suppressed
329 -- for the type of the given expression, or, if the expression is the name
330 -- of an entity, if these checks are suppressed for the entity.
332 function Selected_Length_Checks
334 Target_Typ
: Entity_Id
;
335 Source_Typ
: Entity_Id
;
336 Warn_Node
: Node_Id
) return Check_Result
;
337 -- Like Apply_Selected_Length_Checks, except it doesn't modify
338 -- anything, just returns a list of nodes as described in the spec of
339 -- this package for the Range_Check function.
341 function Selected_Range_Checks
343 Target_Typ
: Entity_Id
;
344 Source_Typ
: Entity_Id
;
345 Warn_Node
: Node_Id
) return Check_Result
;
346 -- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
347 -- just returns a list of nodes as described in the spec of this package
348 -- for the Range_Check function.
350 ------------------------------
351 -- Access_Checks_Suppressed --
352 ------------------------------
354 function Access_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
356 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
357 return Is_Check_Suppressed
(E
, Access_Check
);
359 return Scope_Suppress
.Suppress
(Access_Check
);
361 end Access_Checks_Suppressed
;
363 -------------------------------------
364 -- Accessibility_Checks_Suppressed --
365 -------------------------------------
367 function Accessibility_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
369 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
370 return Is_Check_Suppressed
(E
, Accessibility_Check
);
372 return Scope_Suppress
.Suppress
(Accessibility_Check
);
374 end Accessibility_Checks_Suppressed
;
376 -----------------------------
377 -- Activate_Division_Check --
378 -----------------------------
380 procedure Activate_Division_Check
(N
: Node_Id
) is
382 Set_Do_Division_Check
(N
, True);
383 Possible_Local_Raise
(N
, Standard_Constraint_Error
);
384 end Activate_Division_Check
;
386 -----------------------------
387 -- Activate_Overflow_Check --
388 -----------------------------
390 procedure Activate_Overflow_Check
(N
: Node_Id
) is
391 Typ
: constant Entity_Id
:= Etype
(N
);
394 -- Floating-point case. If Etype is not set (this can happen when we
395 -- activate a check on a node that has not yet been analyzed), then
396 -- we assume we do not have a floating-point type (as per our spec).
398 if Present
(Typ
) and then Is_Floating_Point_Type
(Typ
) then
400 -- Ignore call if we have no automatic overflow checks on the target
401 -- and Check_Float_Overflow mode is not set. These are the cases in
402 -- which we expect to generate infinities and NaN's with no check.
404 if not (Machine_Overflows_On_Target
or Check_Float_Overflow
) then
407 -- Ignore for unary operations ("+", "-", abs) since these can never
408 -- result in overflow for floating-point cases.
410 elsif Nkind
(N
) in N_Unary_Op
then
413 -- Otherwise we will set the flag
422 -- Nothing to do for Rem/Mod/Plus (overflow not possible, the check
423 -- for zero-divide is a divide check, not an overflow check).
425 if Nkind_In
(N
, N_Op_Rem
, N_Op_Mod
, N_Op_Plus
) then
430 -- Fall through for cases where we do set the flag
432 Set_Do_Overflow_Check
(N
, True);
433 Possible_Local_Raise
(N
, Standard_Constraint_Error
);
434 end Activate_Overflow_Check
;
436 --------------------------
437 -- Activate_Range_Check --
438 --------------------------
440 procedure Activate_Range_Check
(N
: Node_Id
) is
442 Set_Do_Range_Check
(N
, True);
443 Possible_Local_Raise
(N
, Standard_Constraint_Error
);
444 end Activate_Range_Check
;
446 ---------------------------------
447 -- Alignment_Checks_Suppressed --
448 ---------------------------------
450 function Alignment_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
452 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
453 return Is_Check_Suppressed
(E
, Alignment_Check
);
455 return Scope_Suppress
.Suppress
(Alignment_Check
);
457 end Alignment_Checks_Suppressed
;
459 ----------------------------------
460 -- Allocation_Checks_Suppressed --
461 ----------------------------------
463 -- Note: at the current time there are no calls to this function, because
464 -- the relevant check is in the run-time, so it is not a check that the
465 -- compiler can suppress anyway, but we still have to recognize the check
466 -- name Allocation_Check since it is part of the standard.
468 function Allocation_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
470 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
471 return Is_Check_Suppressed
(E
, Allocation_Check
);
473 return Scope_Suppress
.Suppress
(Allocation_Check
);
475 end Allocation_Checks_Suppressed
;
477 -------------------------
478 -- Append_Range_Checks --
479 -------------------------
481 procedure Append_Range_Checks
482 (Checks
: Check_Result
;
484 Suppress_Typ
: Entity_Id
;
485 Static_Sloc
: Source_Ptr
;
488 Internal_Flag_Node
: constant Node_Id
:= Flag_Node
;
489 Internal_Static_Sloc
: constant Source_Ptr
:= Static_Sloc
;
491 Checks_On
: constant Boolean :=
492 (not Index_Checks_Suppressed
(Suppress_Typ
))
493 or else (not Range_Checks_Suppressed
(Suppress_Typ
));
496 -- For now we just return if Checks_On is false, however this should
497 -- be enhanced to check for an always True value in the condition
498 -- and to generate a compilation warning???
500 if not Checks_On
then
505 exit when No
(Checks
(J
));
507 if Nkind
(Checks
(J
)) = N_Raise_Constraint_Error
508 and then Present
(Condition
(Checks
(J
)))
510 if not Has_Dynamic_Range_Check
(Internal_Flag_Node
) then
511 Append_To
(Stmts
, Checks
(J
));
512 Set_Has_Dynamic_Range_Check
(Internal_Flag_Node
);
518 Make_Raise_Constraint_Error
(Internal_Static_Sloc
,
519 Reason
=> CE_Range_Check_Failed
));
522 end Append_Range_Checks
;
524 ------------------------
525 -- Apply_Access_Check --
526 ------------------------
528 procedure Apply_Access_Check
(N
: Node_Id
) is
529 P
: constant Node_Id
:= Prefix
(N
);
532 -- We do not need checks if we are not generating code (i.e. the
533 -- expander is not active). This is not just an optimization, there
534 -- are cases (e.g. with pragma Debug) where generating the checks
535 -- can cause real trouble).
537 if not Expander_Active
then
541 -- No check if short circuiting makes check unnecessary
543 if not Check_Needed
(P
, Access_Check
) then
547 -- No check if accessing the Offset_To_Top component of a dispatch
548 -- table. They are safe by construction.
550 if Tagged_Type_Expansion
551 and then Present
(Etype
(P
))
552 and then RTU_Loaded
(Ada_Tags
)
553 and then RTE_Available
(RE_Offset_To_Top_Ptr
)
554 and then Etype
(P
) = RTE
(RE_Offset_To_Top_Ptr
)
559 -- Otherwise go ahead and install the check
561 Install_Null_Excluding_Check
(P
);
562 end Apply_Access_Check
;
564 -------------------------------
565 -- Apply_Accessibility_Check --
566 -------------------------------
568 procedure Apply_Accessibility_Check
571 Insert_Node
: Node_Id
)
573 Loc
: constant Source_Ptr
:= Sloc
(N
);
574 Param_Ent
: Entity_Id
:= Param_Entity
(N
);
575 Param_Level
: Node_Id
;
576 Type_Level
: Node_Id
;
579 if Ada_Version
>= Ada_2012
580 and then not Present
(Param_Ent
)
581 and then Is_Entity_Name
(N
)
582 and then Ekind_In
(Entity
(N
), E_Constant
, E_Variable
)
583 and then Present
(Effective_Extra_Accessibility
(Entity
(N
)))
585 Param_Ent
:= Entity
(N
);
586 while Present
(Renamed_Object
(Param_Ent
)) loop
588 -- Renamed_Object must return an Entity_Name here
589 -- because of preceding "Present (E_E_A (...))" test.
591 Param_Ent
:= Entity
(Renamed_Object
(Param_Ent
));
595 if Inside_A_Generic
then
598 -- Only apply the run-time check if the access parameter has an
599 -- associated extra access level parameter and when the level of the
600 -- type is less deep than the level of the access parameter, and
601 -- accessibility checks are not suppressed.
603 elsif Present
(Param_Ent
)
604 and then Present
(Extra_Accessibility
(Param_Ent
))
605 and then UI_Gt
(Object_Access_Level
(N
),
606 Deepest_Type_Access_Level
(Typ
))
607 and then not Accessibility_Checks_Suppressed
(Param_Ent
)
608 and then not Accessibility_Checks_Suppressed
(Typ
)
611 New_Occurrence_Of
(Extra_Accessibility
(Param_Ent
), Loc
);
614 Make_Integer_Literal
(Loc
, Deepest_Type_Access_Level
(Typ
));
616 -- Raise Program_Error if the accessibility level of the access
617 -- parameter is deeper than the level of the target access type.
619 Insert_Action
(Insert_Node
,
620 Make_Raise_Program_Error
(Loc
,
623 Left_Opnd
=> Param_Level
,
624 Right_Opnd
=> Type_Level
),
625 Reason
=> PE_Accessibility_Check_Failed
));
627 Analyze_And_Resolve
(N
);
629 end Apply_Accessibility_Check
;
631 --------------------------------
632 -- Apply_Address_Clause_Check --
633 --------------------------------
635 procedure Apply_Address_Clause_Check
(E
: Entity_Id
; N
: Node_Id
) is
636 pragma Assert
(Nkind
(N
) = N_Freeze_Entity
);
638 AC
: constant Node_Id
:= Address_Clause
(E
);
639 Loc
: constant Source_Ptr
:= Sloc
(AC
);
640 Typ
: constant Entity_Id
:= Etype
(E
);
643 -- Address expression (not necessarily the same as Aexp, for example
644 -- when Aexp is a reference to a constant, in which case Expr gets
645 -- reset to reference the value expression of the constant).
648 -- See if alignment check needed. Note that we never need a check if the
649 -- maximum alignment is one, since the check will always succeed.
651 -- Note: we do not check for checks suppressed here, since that check
652 -- was done in Sem_Ch13 when the address clause was processed. We are
653 -- only called if checks were not suppressed. The reason for this is
654 -- that we have to delay the call to Apply_Alignment_Check till freeze
655 -- time (so that all types etc are elaborated), but we have to check
656 -- the status of check suppressing at the point of the address clause.
659 or else not Check_Address_Alignment
(AC
)
660 or else Maximum_Alignment
= 1
665 -- Obtain expression from address clause
667 Expr
:= Address_Value
(Expression
(AC
));
669 -- See if we know that Expr has an acceptable value at compile time. If
670 -- it hasn't or we don't know, we defer issuing the warning until the
671 -- end of the compilation to take into account back end annotations.
673 if Compile_Time_Known_Value
(Expr
)
674 and then (Known_Alignment
(E
) or else Known_Alignment
(Typ
))
677 AL
: Uint
:= Alignment
(Typ
);
680 -- The object alignment might be more restrictive than the type
683 if Known_Alignment
(E
) then
687 if Expr_Value
(Expr
) mod AL
= 0 then
692 -- If the expression has the form X'Address, then we can find out if the
693 -- object X has an alignment that is compatible with the object E. If it
694 -- hasn't or we don't know, we defer issuing the warning until the end
695 -- of the compilation to take into account back end annotations.
697 elsif Nkind
(Expr
) = N_Attribute_Reference
698 and then Attribute_Name
(Expr
) = Name_Address
700 Has_Compatible_Alignment
(E
, Prefix
(Expr
), False) = Known_Compatible
705 -- Here we do not know if the value is acceptable. Strictly we don't
706 -- have to do anything, since if the alignment is bad, we have an
707 -- erroneous program. However we are allowed to check for erroneous
708 -- conditions and we decide to do this by default if the check is not
711 -- However, don't do the check if elaboration code is unwanted
713 if Restriction_Active
(No_Elaboration_Code
) then
716 -- Generate a check to raise PE if alignment may be inappropriate
719 -- If the original expression is a non-static constant, use the name
720 -- of the constant itself rather than duplicating its initialization
721 -- expression, which was extracted above.
723 -- Note: Expr is empty if the address-clause is applied to in-mode
724 -- actuals (allowed by 13.1(22)).
726 if not Present
(Expr
)
728 (Is_Entity_Name
(Expression
(AC
))
729 and then Ekind
(Entity
(Expression
(AC
))) = E_Constant
730 and then Nkind
(Parent
(Entity
(Expression
(AC
)))) =
731 N_Object_Declaration
)
733 Expr
:= New_Copy_Tree
(Expression
(AC
));
735 Remove_Side_Effects
(Expr
);
738 if No
(Actions
(N
)) then
739 Set_Actions
(N
, New_List
);
742 Prepend_To
(Actions
(N
),
743 Make_Raise_Program_Error
(Loc
,
750 (RTE
(RE_Integer_Address
), Expr
),
752 Make_Attribute_Reference
(Loc
,
753 Prefix
=> New_Occurrence_Of
(E
, Loc
),
754 Attribute_Name
=> Name_Alignment
)),
755 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
756 Reason
=> PE_Misaligned_Address_Value
));
758 Warning_Msg
:= No_Error_Msg
;
759 Analyze
(First
(Actions
(N
)), Suppress
=> All_Checks
);
761 -- If the above raise action generated a warning message (for example
762 -- from Warn_On_Non_Local_Exception mode with the active restriction
763 -- No_Exception_Propagation).
765 if Warning_Msg
/= No_Error_Msg
then
767 -- If the expression has a known at compile time value, then
768 -- once we know the alignment of the type, we can check if the
769 -- exception will be raised or not, and if not, we don't need
770 -- the warning so we will kill the warning later on.
772 if Compile_Time_Known_Value
(Expr
) then
773 Alignment_Warnings
.Append
774 ((E
=> E
, A
=> Expr_Value
(Expr
), W
=> Warning_Msg
));
776 -- Add explanation of the warning generated by the check
780 ("\address value may be incompatible with alignment of "
790 -- If we have some missing run time component in configurable run time
791 -- mode then just skip the check (it is not required in any case).
793 when RE_Not_Available
=>
795 end Apply_Address_Clause_Check
;
797 -------------------------------------
798 -- Apply_Arithmetic_Overflow_Check --
799 -------------------------------------
801 procedure Apply_Arithmetic_Overflow_Check
(N
: Node_Id
) is
803 -- Use old routine in almost all cases (the only case we are treating
804 -- specially is the case of a signed integer arithmetic op with the
805 -- overflow checking mode set to MINIMIZED or ELIMINATED).
807 if Overflow_Check_Mode
= Strict
808 or else not Is_Signed_Integer_Arithmetic_Op
(N
)
810 Apply_Arithmetic_Overflow_Strict
(N
);
812 -- Otherwise use the new routine for the case of a signed integer
813 -- arithmetic op, with Do_Overflow_Check set to True, and the checking
814 -- mode is MINIMIZED or ELIMINATED.
817 Apply_Arithmetic_Overflow_Minimized_Eliminated
(N
);
819 end Apply_Arithmetic_Overflow_Check
;
821 --------------------------------------
822 -- Apply_Arithmetic_Overflow_Strict --
823 --------------------------------------
825 -- This routine is called only if the type is an integer type, and a
826 -- software arithmetic overflow check may be needed for op (add, subtract,
827 -- or multiply). This check is performed only if Software_Overflow_Checking
828 -- is enabled and Do_Overflow_Check is set. In this case we expand the
829 -- operation into a more complex sequence of tests that ensures that
830 -- overflow is properly caught.
832 -- This is used in CHECKED modes. It is identical to the code for this
833 -- cases before the big overflow earthquake, thus ensuring that in this
834 -- modes we have compatible behavior (and reliability) to what was there
835 -- before. It is also called for types other than signed integers, and if
836 -- the Do_Overflow_Check flag is off.
838 -- Note: we also call this routine if we decide in the MINIMIZED case
839 -- to give up and just generate an overflow check without any fuss.
841 procedure Apply_Arithmetic_Overflow_Strict
(N
: Node_Id
) is
842 Loc
: constant Source_Ptr
:= Sloc
(N
);
843 Typ
: constant Entity_Id
:= Etype
(N
);
844 Rtyp
: constant Entity_Id
:= Root_Type
(Typ
);
847 -- Nothing to do if Do_Overflow_Check not set or overflow checks
850 if not Do_Overflow_Check
(N
) then
854 -- An interesting special case. If the arithmetic operation appears as
855 -- the operand of a type conversion:
859 -- and all the following conditions apply:
861 -- arithmetic operation is for a signed integer type
862 -- target type type1 is a static integer subtype
863 -- range of x and y are both included in the range of type1
864 -- range of x op y is included in the range of type1
865 -- size of type1 is at least twice the result size of op
867 -- then we don't do an overflow check in any case. Instead, we transform
868 -- the operation so that we end up with:
870 -- type1 (type1 (x) op type1 (y))
872 -- This avoids intermediate overflow before the conversion. It is
873 -- explicitly permitted by RM 3.5.4(24):
875 -- For the execution of a predefined operation of a signed integer
876 -- type, the implementation need not raise Constraint_Error if the
877 -- result is outside the base range of the type, so long as the
878 -- correct result is produced.
880 -- It's hard to imagine that any programmer counts on the exception
881 -- being raised in this case, and in any case it's wrong coding to
882 -- have this expectation, given the RM permission. Furthermore, other
883 -- Ada compilers do allow such out of range results.
885 -- Note that we do this transformation even if overflow checking is
886 -- off, since this is precisely about giving the "right" result and
887 -- avoiding the need for an overflow check.
889 -- Note: this circuit is partially redundant with respect to the similar
890 -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals
891 -- with cases that do not come through here. We still need the following
892 -- processing even with the Exp_Ch4 code in place, since we want to be
893 -- sure not to generate the arithmetic overflow check in these cases
894 -- (Exp_Ch4 would have a hard time removing them once generated).
896 if Is_Signed_Integer_Type
(Typ
)
897 and then Nkind
(Parent
(N
)) = N_Type_Conversion
899 Conversion_Optimization
: declare
900 Target_Type
: constant Entity_Id
:=
901 Base_Type
(Entity
(Subtype_Mark
(Parent
(N
))));
915 if Is_Integer_Type
(Target_Type
)
916 and then RM_Size
(Root_Type
(Target_Type
)) >= 2 * RM_Size
(Rtyp
)
918 Tlo
:= Expr_Value
(Type_Low_Bound
(Target_Type
));
919 Thi
:= Expr_Value
(Type_High_Bound
(Target_Type
));
922 (Left_Opnd
(N
), LOK
, Llo
, Lhi
, Assume_Valid
=> True);
924 (Right_Opnd
(N
), ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
927 and then Tlo
<= Llo
and then Lhi
<= Thi
928 and then Tlo
<= Rlo
and then Rhi
<= Thi
930 Determine_Range
(N
, VOK
, Vlo
, Vhi
, Assume_Valid
=> True);
932 if VOK
and then Tlo
<= Vlo
and then Vhi
<= Thi
then
933 Rewrite
(Left_Opnd
(N
),
934 Make_Type_Conversion
(Loc
,
935 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
936 Expression
=> Relocate_Node
(Left_Opnd
(N
))));
938 Rewrite
(Right_Opnd
(N
),
939 Make_Type_Conversion
(Loc
,
940 Subtype_Mark
=> New_Occurrence_Of
(Target_Type
, Loc
),
941 Expression
=> Relocate_Node
(Right_Opnd
(N
))));
943 -- Rewrite the conversion operand so that the original
944 -- node is retained, in order to avoid the warning for
945 -- redundant conversions in Resolve_Type_Conversion.
947 Rewrite
(N
, Relocate_Node
(N
));
949 Set_Etype
(N
, Target_Type
);
951 Analyze_And_Resolve
(Left_Opnd
(N
), Target_Type
);
952 Analyze_And_Resolve
(Right_Opnd
(N
), Target_Type
);
954 -- Given that the target type is twice the size of the
955 -- source type, overflow is now impossible, so we can
956 -- safely kill the overflow check and return.
958 Set_Do_Overflow_Check
(N
, False);
963 end Conversion_Optimization
;
966 -- Now see if an overflow check is required
969 Siz
: constant Int
:= UI_To_Int
(Esize
(Rtyp
));
970 Dsiz
: constant Int
:= Siz
* 2;
977 -- Skip check if back end does overflow checks, or the overflow flag
978 -- is not set anyway, or we are not doing code expansion, or the
979 -- parent node is a type conversion whose operand is an arithmetic
980 -- operation on signed integers on which the expander can promote
981 -- later the operands to type Integer (see Expand_N_Type_Conversion).
983 if Backend_Overflow_Checks_On_Target
984 or else not Do_Overflow_Check
(N
)
985 or else not Expander_Active
986 or else (Present
(Parent
(N
))
987 and then Nkind
(Parent
(N
)) = N_Type_Conversion
988 and then Integer_Promotion_Possible
(Parent
(N
)))
993 -- Otherwise, generate the full general code for front end overflow
994 -- detection, which works by doing arithmetic in a larger type:
1000 -- Typ (Checktyp (x) op Checktyp (y));
1002 -- where Typ is the type of the original expression, and Checktyp is
1003 -- an integer type of sufficient length to hold the largest possible
1006 -- If the size of check type exceeds the size of Long_Long_Integer,
1007 -- we use a different approach, expanding to:
1009 -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
1011 -- where xxx is Add, Multiply or Subtract as appropriate
1013 -- Find check type if one exists
1015 if Dsiz
<= Standard_Integer_Size
then
1016 Ctyp
:= Standard_Integer
;
1018 elsif Dsiz
<= Standard_Long_Long_Integer_Size
then
1019 Ctyp
:= Standard_Long_Long_Integer
;
1021 -- No check type exists, use runtime call
1024 if Nkind
(N
) = N_Op_Add
then
1025 Cent
:= RE_Add_With_Ovflo_Check
;
1027 elsif Nkind
(N
) = N_Op_Multiply
then
1028 Cent
:= RE_Multiply_With_Ovflo_Check
;
1031 pragma Assert
(Nkind
(N
) = N_Op_Subtract
);
1032 Cent
:= RE_Subtract_With_Ovflo_Check
;
1037 Make_Function_Call
(Loc
,
1038 Name
=> New_Occurrence_Of
(RTE
(Cent
), Loc
),
1039 Parameter_Associations
=> New_List
(
1040 OK_Convert_To
(RTE
(RE_Integer_64
), Left_Opnd
(N
)),
1041 OK_Convert_To
(RTE
(RE_Integer_64
), Right_Opnd
(N
))))));
1043 Analyze_And_Resolve
(N
, Typ
);
1047 -- If we fall through, we have the case where we do the arithmetic
1048 -- in the next higher type and get the check by conversion. In these
1049 -- cases Ctyp is set to the type to be used as the check type.
1051 Opnod
:= Relocate_Node
(N
);
1053 Opnd
:= OK_Convert_To
(Ctyp
, Left_Opnd
(Opnod
));
1056 Set_Etype
(Opnd
, Ctyp
);
1057 Set_Analyzed
(Opnd
, True);
1058 Set_Left_Opnd
(Opnod
, Opnd
);
1060 Opnd
:= OK_Convert_To
(Ctyp
, Right_Opnd
(Opnod
));
1063 Set_Etype
(Opnd
, Ctyp
);
1064 Set_Analyzed
(Opnd
, True);
1065 Set_Right_Opnd
(Opnod
, Opnd
);
1067 -- The type of the operation changes to the base type of the check
1068 -- type, and we reset the overflow check indication, since clearly no
1069 -- overflow is possible now that we are using a double length type.
1070 -- We also set the Analyzed flag to avoid a recursive attempt to
1073 Set_Etype
(Opnod
, Base_Type
(Ctyp
));
1074 Set_Do_Overflow_Check
(Opnod
, False);
1075 Set_Analyzed
(Opnod
, True);
1077 -- Now build the outer conversion
1079 Opnd
:= OK_Convert_To
(Typ
, Opnod
);
1081 Set_Etype
(Opnd
, Typ
);
1083 -- In the discrete type case, we directly generate the range check
1084 -- for the outer operand. This range check will implement the
1085 -- required overflow check.
1087 if Is_Discrete_Type
(Typ
) then
1089 Generate_Range_Check
1090 (Expression
(N
), Typ
, CE_Overflow_Check_Failed
);
1092 -- For other types, we enable overflow checking on the conversion,
1093 -- after setting the node as analyzed to prevent recursive attempts
1094 -- to expand the conversion node.
1097 Set_Analyzed
(Opnd
, True);
1098 Enable_Overflow_Check
(Opnd
);
1103 when RE_Not_Available
=>
1106 end Apply_Arithmetic_Overflow_Strict
;
1108 ----------------------------------------------------
1109 -- Apply_Arithmetic_Overflow_Minimized_Eliminated --
1110 ----------------------------------------------------
1112 procedure Apply_Arithmetic_Overflow_Minimized_Eliminated
(Op
: Node_Id
) is
1113 pragma Assert
(Is_Signed_Integer_Arithmetic_Op
(Op
));
1115 Loc
: constant Source_Ptr
:= Sloc
(Op
);
1116 P
: constant Node_Id
:= Parent
(Op
);
1118 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
1119 -- Operands and results are of this type when we convert
1121 Result_Type
: constant Entity_Id
:= Etype
(Op
);
1122 -- Original result type
1124 Check_Mode
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
1125 pragma Assert
(Check_Mode
in Minimized_Or_Eliminated
);
1128 -- Ranges of values for result
1131 -- Nothing to do if our parent is one of the following:
1133 -- Another signed integer arithmetic op
1134 -- A membership operation
1135 -- A comparison operation
1137 -- In all these cases, we will process at the higher level (and then
1138 -- this node will be processed during the downwards recursion that
1139 -- is part of the processing in Minimize_Eliminate_Overflows).
1141 if Is_Signed_Integer_Arithmetic_Op
(P
)
1142 or else Nkind
(P
) in N_Membership_Test
1143 or else Nkind
(P
) in N_Op_Compare
1145 -- This is also true for an alternative in a case expression
1147 or else Nkind
(P
) = N_Case_Expression_Alternative
1149 -- This is also true for a range operand in a membership test
1151 or else (Nkind
(P
) = N_Range
1152 and then Nkind
(Parent
(P
)) in N_Membership_Test
)
1154 -- If_Expressions and Case_Expressions are treated as arithmetic
1155 -- ops, but if they appear in an assignment or similar contexts
1156 -- there is no overflow check that starts from that parent node,
1157 -- so apply check now.
1159 if Nkind_In
(P
, N_If_Expression
, N_Case_Expression
)
1160 and then not Is_Signed_Integer_Arithmetic_Op
(Parent
(P
))
1168 -- Otherwise, we have a top level arithmetic operation node, and this
1169 -- is where we commence the special processing for MINIMIZED/ELIMINATED
1170 -- modes. This is the case where we tell the machinery not to move into
1171 -- Bignum mode at this top level (of course the top level operation
1172 -- will still be in Bignum mode if either of its operands are of type
1175 Minimize_Eliminate_Overflows
(Op
, Lo
, Hi
, Top_Level
=> True);
1177 -- That call may but does not necessarily change the result type of Op.
1178 -- It is the job of this routine to undo such changes, so that at the
1179 -- top level, we have the proper type. This "undoing" is a point at
1180 -- which a final overflow check may be applied.
1182 -- If the result type was not fiddled we are all set. We go to base
1183 -- types here because things may have been rewritten to generate the
1184 -- base type of the operand types.
1186 if Base_Type
(Etype
(Op
)) = Base_Type
(Result_Type
) then
1191 elsif Is_RTE
(Etype
(Op
), RE_Bignum
) then
1193 -- We need a sequence that looks like:
1195 -- Rnn : Result_Type;
1198 -- M : Mark_Id := SS_Mark;
1200 -- Rnn := Long_Long_Integer'Base (From_Bignum (Op));
1204 -- This block is inserted (using Insert_Actions), and then the node
1205 -- is replaced with a reference to Rnn.
1207 -- If our parent is a conversion node then there is no point in
1208 -- generating a conversion to Result_Type. Instead, we let the parent
1209 -- handle this. Note that this special case is not just about
1210 -- optimization. Consider
1214 -- X := Long_Long_Integer'Base (A * (B ** C));
1216 -- Now the product may fit in Long_Long_Integer but not in Integer.
1217 -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an
1218 -- overflow exception for this intermediate value.
1221 Blk
: constant Node_Id
:= Make_Bignum_Block
(Loc
);
1222 Rnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'R', Op
);
1228 RHS
:= Convert_From_Bignum
(Op
);
1230 if Nkind
(P
) /= N_Type_Conversion
then
1231 Convert_To_And_Rewrite
(Result_Type
, RHS
);
1232 Rtype
:= Result_Type
;
1234 -- Interesting question, do we need a check on that conversion
1235 -- operation. Answer, not if we know the result is in range.
1236 -- At the moment we are not taking advantage of this. To be
1237 -- looked at later ???
1244 (First
(Statements
(Handled_Statement_Sequence
(Blk
))),
1245 Make_Assignment_Statement
(Loc
,
1246 Name
=> New_Occurrence_Of
(Rnn
, Loc
),
1247 Expression
=> RHS
));
1249 Insert_Actions
(Op
, New_List
(
1250 Make_Object_Declaration
(Loc
,
1251 Defining_Identifier
=> Rnn
,
1252 Object_Definition
=> New_Occurrence_Of
(Rtype
, Loc
)),
1255 Rewrite
(Op
, New_Occurrence_Of
(Rnn
, Loc
));
1256 Analyze_And_Resolve
(Op
);
1259 -- Here we know the result is Long_Long_Integer'Base, or that it has
1260 -- been rewritten because the parent operation is a conversion. See
1261 -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization.
1265 (Etype
(Op
) = LLIB
or else Nkind
(Parent
(Op
)) = N_Type_Conversion
);
1267 -- All we need to do here is to convert the result to the proper
1268 -- result type. As explained above for the Bignum case, we can
1269 -- omit this if our parent is a type conversion.
1271 if Nkind
(P
) /= N_Type_Conversion
then
1272 Convert_To_And_Rewrite
(Result_Type
, Op
);
1275 Analyze_And_Resolve
(Op
);
1277 end Apply_Arithmetic_Overflow_Minimized_Eliminated
;
1279 ----------------------------
1280 -- Apply_Constraint_Check --
1281 ----------------------------
1283 procedure Apply_Constraint_Check
1286 No_Sliding
: Boolean := False)
1288 Desig_Typ
: Entity_Id
;
1291 -- No checks inside a generic (check the instantiations)
1293 if Inside_A_Generic
then
1297 -- Apply required constraint checks
1299 if Is_Scalar_Type
(Typ
) then
1300 Apply_Scalar_Range_Check
(N
, Typ
);
1302 elsif Is_Array_Type
(Typ
) then
1304 -- A useful optimization: an aggregate with only an others clause
1305 -- always has the right bounds.
1307 if Nkind
(N
) = N_Aggregate
1308 and then No
(Expressions
(N
))
1310 (First
(Choices
(First
(Component_Associations
(N
)))))
1316 if Is_Constrained
(Typ
) then
1317 Apply_Length_Check
(N
, Typ
);
1320 Apply_Range_Check
(N
, Typ
);
1323 Apply_Range_Check
(N
, Typ
);
1326 elsif (Is_Record_Type
(Typ
) or else Is_Private_Type
(Typ
))
1327 and then Has_Discriminants
(Base_Type
(Typ
))
1328 and then Is_Constrained
(Typ
)
1330 Apply_Discriminant_Check
(N
, Typ
);
1332 elsif Is_Access_Type
(Typ
) then
1334 Desig_Typ
:= Designated_Type
(Typ
);
1336 -- No checks necessary if expression statically null
1338 if Known_Null
(N
) then
1339 if Can_Never_Be_Null
(Typ
) then
1340 Install_Null_Excluding_Check
(N
);
1343 -- No sliding possible on access to arrays
1345 elsif Is_Array_Type
(Desig_Typ
) then
1346 if Is_Constrained
(Desig_Typ
) then
1347 Apply_Length_Check
(N
, Typ
);
1350 Apply_Range_Check
(N
, Typ
);
1352 elsif Has_Discriminants
(Base_Type
(Desig_Typ
))
1353 and then Is_Constrained
(Desig_Typ
)
1355 Apply_Discriminant_Check
(N
, Typ
);
1358 -- Apply the 2005 Null_Excluding check. Note that we do not apply
1359 -- this check if the constraint node is illegal, as shown by having
1360 -- an error posted. This additional guard prevents cascaded errors
1361 -- and compiler aborts on illegal programs involving Ada 2005 checks.
1363 if Can_Never_Be_Null
(Typ
)
1364 and then not Can_Never_Be_Null
(Etype
(N
))
1365 and then not Error_Posted
(N
)
1367 Install_Null_Excluding_Check
(N
);
1370 end Apply_Constraint_Check
;
1372 ------------------------------
1373 -- Apply_Discriminant_Check --
1374 ------------------------------
1376 procedure Apply_Discriminant_Check
1379 Lhs
: Node_Id
:= Empty
)
1381 Loc
: constant Source_Ptr
:= Sloc
(N
);
1382 Do_Access
: constant Boolean := Is_Access_Type
(Typ
);
1383 S_Typ
: Entity_Id
:= Etype
(N
);
1387 function Denotes_Explicit_Dereference
(Obj
: Node_Id
) return Boolean;
1388 -- A heap object with an indefinite subtype is constrained by its
1389 -- initial value, and assigning to it requires a constraint_check.
1390 -- The target may be an explicit dereference, or a renaming of one.
1392 function Is_Aliased_Unconstrained_Component
return Boolean;
1393 -- It is possible for an aliased component to have a nominal
1394 -- unconstrained subtype (through instantiation). If this is a
1395 -- discriminated component assigned in the expansion of an aggregate
1396 -- in an initialization, the check must be suppressed. This unusual
1397 -- situation requires a predicate of its own.
1399 ----------------------------------
1400 -- Denotes_Explicit_Dereference --
1401 ----------------------------------
1403 function Denotes_Explicit_Dereference
(Obj
: Node_Id
) return Boolean is
1406 Nkind
(Obj
) = N_Explicit_Dereference
1408 (Is_Entity_Name
(Obj
)
1409 and then Present
(Renamed_Object
(Entity
(Obj
)))
1410 and then Nkind
(Renamed_Object
(Entity
(Obj
))) =
1411 N_Explicit_Dereference
);
1412 end Denotes_Explicit_Dereference
;
1414 ----------------------------------------
1415 -- Is_Aliased_Unconstrained_Component --
1416 ----------------------------------------
1418 function Is_Aliased_Unconstrained_Component
return Boolean is
1423 if Nkind
(Lhs
) /= N_Selected_Component
then
1426 Comp
:= Entity
(Selector_Name
(Lhs
));
1427 Pref
:= Prefix
(Lhs
);
1430 if Ekind
(Comp
) /= E_Component
1431 or else not Is_Aliased
(Comp
)
1436 return not Comes_From_Source
(Pref
)
1437 and then In_Instance
1438 and then not Is_Constrained
(Etype
(Comp
));
1439 end Is_Aliased_Unconstrained_Component
;
1441 -- Start of processing for Apply_Discriminant_Check
1445 T_Typ
:= Designated_Type
(Typ
);
1450 -- Nothing to do if discriminant checks are suppressed or else no code
1451 -- is to be generated
1453 if not Expander_Active
1454 or else Discriminant_Checks_Suppressed
(T_Typ
)
1459 -- No discriminant checks necessary for an access when expression is
1460 -- statically Null. This is not only an optimization, it is fundamental
1461 -- because otherwise discriminant checks may be generated in init procs
1462 -- for types containing an access to a not-yet-frozen record, causing a
1463 -- deadly forward reference.
1465 -- Also, if the expression is of an access type whose designated type is
1466 -- incomplete, then the access value must be null and we suppress the
1469 if Known_Null
(N
) then
1472 elsif Is_Access_Type
(S_Typ
) then
1473 S_Typ
:= Designated_Type
(S_Typ
);
1475 if Ekind
(S_Typ
) = E_Incomplete_Type
then
1480 -- If an assignment target is present, then we need to generate the
1481 -- actual subtype if the target is a parameter or aliased object with
1482 -- an unconstrained nominal subtype.
1484 -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual
1485 -- subtype to the parameter and dereference cases, since other aliased
1486 -- objects are unconstrained (unless the nominal subtype is explicitly
1490 and then (Present
(Param_Entity
(Lhs
))
1491 or else (Ada_Version
< Ada_2005
1492 and then not Is_Constrained
(T_Typ
)
1493 and then Is_Aliased_View
(Lhs
)
1494 and then not Is_Aliased_Unconstrained_Component
)
1495 or else (Ada_Version
>= Ada_2005
1496 and then not Is_Constrained
(T_Typ
)
1497 and then Denotes_Explicit_Dereference
(Lhs
)
1498 and then Nkind
(Original_Node
(Lhs
)) /=
1501 T_Typ
:= Get_Actual_Subtype
(Lhs
);
1504 -- Nothing to do if the type is unconstrained (this is the case where
1505 -- the actual subtype in the RM sense of N is unconstrained and no check
1508 if not Is_Constrained
(T_Typ
) then
1511 -- Ada 2005: nothing to do if the type is one for which there is a
1512 -- partial view that is constrained.
1514 elsif Ada_Version
>= Ada_2005
1515 and then Object_Type_Has_Constrained_Partial_View
1516 (Typ
=> Base_Type
(T_Typ
),
1517 Scop
=> Current_Scope
)
1522 -- Nothing to do if the type is an Unchecked_Union
1524 if Is_Unchecked_Union
(Base_Type
(T_Typ
)) then
1528 -- Suppress checks if the subtypes are the same. The check must be
1529 -- preserved in an assignment to a formal, because the constraint is
1530 -- given by the actual.
1532 if Nkind
(Original_Node
(N
)) /= N_Allocator
1534 or else not Is_Entity_Name
(Lhs
)
1535 or else No
(Param_Entity
(Lhs
)))
1538 or else (Do_Access
and then Designated_Type
(Typ
) = S_Typ
))
1539 and then not Is_Aliased_View
(Lhs
)
1544 -- We can also eliminate checks on allocators with a subtype mark that
1545 -- coincides with the context type. The context type may be a subtype
1546 -- without a constraint (common case, a generic actual).
1548 elsif Nkind
(Original_Node
(N
)) = N_Allocator
1549 and then Is_Entity_Name
(Expression
(Original_Node
(N
)))
1552 Alloc_Typ
: constant Entity_Id
:=
1553 Entity
(Expression
(Original_Node
(N
)));
1556 if Alloc_Typ
= T_Typ
1557 or else (Nkind
(Parent
(T_Typ
)) = N_Subtype_Declaration
1558 and then Is_Entity_Name
(
1559 Subtype_Indication
(Parent
(T_Typ
)))
1560 and then Alloc_Typ
= Base_Type
(T_Typ
))
1568 -- See if we have a case where the types are both constrained, and all
1569 -- the constraints are constants. In this case, we can do the check
1570 -- successfully at compile time.
1572 -- We skip this check for the case where the node is rewritten as
1573 -- an allocator, because it already carries the context subtype,
1574 -- and extracting the discriminants from the aggregate is messy.
1576 if Is_Constrained
(S_Typ
)
1577 and then Nkind
(Original_Node
(N
)) /= N_Allocator
1587 -- S_Typ may not have discriminants in the case where it is a
1588 -- private type completed by a default discriminated type. In that
1589 -- case, we need to get the constraints from the underlying type.
1590 -- If the underlying type is unconstrained (i.e. has no default
1591 -- discriminants) no check is needed.
1593 if Has_Discriminants
(S_Typ
) then
1594 Discr
:= First_Discriminant
(S_Typ
);
1595 DconS
:= First_Elmt
(Discriminant_Constraint
(S_Typ
));
1598 Discr
:= First_Discriminant
(Underlying_Type
(S_Typ
));
1601 (Discriminant_Constraint
(Underlying_Type
(S_Typ
)));
1607 -- A further optimization: if T_Typ is derived from S_Typ
1608 -- without imposing a constraint, no check is needed.
1610 if Nkind
(Original_Node
(Parent
(T_Typ
))) =
1611 N_Full_Type_Declaration
1614 Type_Def
: constant Node_Id
:=
1615 Type_Definition
(Original_Node
(Parent
(T_Typ
)));
1617 if Nkind
(Type_Def
) = N_Derived_Type_Definition
1618 and then Is_Entity_Name
(Subtype_Indication
(Type_Def
))
1619 and then Entity
(Subtype_Indication
(Type_Def
)) = S_Typ
1627 -- Constraint may appear in full view of type
1629 if Ekind
(T_Typ
) = E_Private_Subtype
1630 and then Present
(Full_View
(T_Typ
))
1633 First_Elmt
(Discriminant_Constraint
(Full_View
(T_Typ
)));
1636 First_Elmt
(Discriminant_Constraint
(T_Typ
));
1639 while Present
(Discr
) loop
1640 ItemS
:= Node
(DconS
);
1641 ItemT
:= Node
(DconT
);
1643 -- For a discriminated component type constrained by the
1644 -- current instance of an enclosing type, there is no
1645 -- applicable discriminant check.
1647 if Nkind
(ItemT
) = N_Attribute_Reference
1648 and then Is_Access_Type
(Etype
(ItemT
))
1649 and then Is_Entity_Name
(Prefix
(ItemT
))
1650 and then Is_Type
(Entity
(Prefix
(ItemT
)))
1655 -- If the expressions for the discriminants are identical
1656 -- and it is side-effect free (for now just an entity),
1657 -- this may be a shared constraint, e.g. from a subtype
1658 -- without a constraint introduced as a generic actual.
1659 -- Examine other discriminants if any.
1662 and then Is_Entity_Name
(ItemS
)
1666 elsif not Is_OK_Static_Expression
(ItemS
)
1667 or else not Is_OK_Static_Expression
(ItemT
)
1671 elsif Expr_Value
(ItemS
) /= Expr_Value
(ItemT
) then
1672 if Do_Access
then -- needs run-time check.
1675 Apply_Compile_Time_Constraint_Error
1676 (N
, "incorrect value for discriminant&??",
1677 CE_Discriminant_Check_Failed
, Ent
=> Discr
);
1684 Next_Discriminant
(Discr
);
1693 -- Here we need a discriminant check. First build the expression
1694 -- for the comparisons of the discriminants:
1696 -- (n.disc1 /= typ.disc1) or else
1697 -- (n.disc2 /= typ.disc2) or else
1699 -- (n.discn /= typ.discn)
1701 Cond
:= Build_Discriminant_Checks
(N
, T_Typ
);
1703 -- If Lhs is set and is a parameter, then the condition is guarded by:
1704 -- lhs'constrained and then (condition built above)
1706 if Present
(Param_Entity
(Lhs
)) then
1710 Make_Attribute_Reference
(Loc
,
1711 Prefix
=> New_Occurrence_Of
(Param_Entity
(Lhs
), Loc
),
1712 Attribute_Name
=> Name_Constrained
),
1713 Right_Opnd
=> Cond
);
1717 Cond
:= Guard_Access
(Cond
, Loc
, N
);
1721 Make_Raise_Constraint_Error
(Loc
,
1723 Reason
=> CE_Discriminant_Check_Failed
));
1724 end Apply_Discriminant_Check
;
1726 -------------------------
1727 -- Apply_Divide_Checks --
1728 -------------------------
1730 procedure Apply_Divide_Checks
(N
: Node_Id
) is
1731 Loc
: constant Source_Ptr
:= Sloc
(N
);
1732 Typ
: constant Entity_Id
:= Etype
(N
);
1733 Left
: constant Node_Id
:= Left_Opnd
(N
);
1734 Right
: constant Node_Id
:= Right_Opnd
(N
);
1736 Mode
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
1737 -- Current overflow checking mode
1747 pragma Warnings
(Off
, Lhi
);
1748 -- Don't actually use this value
1751 -- If we are operating in MINIMIZED or ELIMINATED mode, and we are
1752 -- operating on signed integer types, then the only thing this routine
1753 -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That
1754 -- procedure will (possibly later on during recursive downward calls),
1755 -- ensure that any needed overflow/division checks are properly applied.
1757 if Mode
in Minimized_Or_Eliminated
1758 and then Is_Signed_Integer_Type
(Typ
)
1760 Apply_Arithmetic_Overflow_Minimized_Eliminated
(N
);
1764 -- Proceed here in SUPPRESSED or CHECKED modes
1767 and then not Backend_Divide_Checks_On_Target
1768 and then Check_Needed
(Right
, Division_Check
)
1770 Determine_Range
(Right
, ROK
, Rlo
, Rhi
, Assume_Valid
=> True);
1772 -- Deal with division check
1774 if Do_Division_Check
(N
)
1775 and then not Division_Checks_Suppressed
(Typ
)
1777 Apply_Division_Check
(N
, Rlo
, Rhi
, ROK
);
1780 -- Deal with overflow check
1782 if Do_Overflow_Check
(N
)
1783 and then not Overflow_Checks_Suppressed
(Etype
(N
))
1785 Set_Do_Overflow_Check
(N
, False);
1787 -- Test for extremely annoying case of xxx'First divided by -1
1788 -- for division of signed integer types (only overflow case).
1790 if Nkind
(N
) = N_Op_Divide
1791 and then Is_Signed_Integer_Type
(Typ
)
1793 Determine_Range
(Left
, LOK
, Llo
, Lhi
, Assume_Valid
=> True);
1794 LLB
:= Expr_Value
(Type_Low_Bound
(Base_Type
(Typ
)));
1796 if ((not ROK
) or else (Rlo
<= (-1) and then (-1) <= Rhi
))
1798 ((not LOK
) or else (Llo
= LLB
))
1801 Make_Raise_Constraint_Error
(Loc
,
1807 Duplicate_Subexpr_Move_Checks
(Left
),
1808 Right_Opnd
=> Make_Integer_Literal
(Loc
, LLB
)),
1812 Left_Opnd
=> Duplicate_Subexpr
(Right
),
1813 Right_Opnd
=> Make_Integer_Literal
(Loc
, -1))),
1815 Reason
=> CE_Overflow_Check_Failed
));
1820 end Apply_Divide_Checks
;
1822 --------------------------
1823 -- Apply_Division_Check --
1824 --------------------------
1826 procedure Apply_Division_Check
1832 pragma Assert
(Do_Division_Check
(N
));
1834 Loc
: constant Source_Ptr
:= Sloc
(N
);
1835 Right
: constant Node_Id
:= Right_Opnd
(N
);
1839 and then not Backend_Divide_Checks_On_Target
1840 and then Check_Needed
(Right
, Division_Check
)
1842 -- See if division by zero possible, and if so generate test. This
1843 -- part of the test is not controlled by the -gnato switch, since
1844 -- it is a Division_Check and not an Overflow_Check.
1846 if Do_Division_Check
(N
) then
1847 Set_Do_Division_Check
(N
, False);
1849 if (not ROK
) or else (Rlo
<= 0 and then 0 <= Rhi
) then
1851 Make_Raise_Constraint_Error
(Loc
,
1854 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(Right
),
1855 Right_Opnd
=> Make_Integer_Literal
(Loc
, 0)),
1856 Reason
=> CE_Divide_By_Zero
));
1860 end Apply_Division_Check
;
1862 ----------------------------------
1863 -- Apply_Float_Conversion_Check --
1864 ----------------------------------
1866 -- Let F and I be the source and target types of the conversion. The RM
1867 -- specifies that a floating-point value X is rounded to the nearest
1868 -- integer, with halfway cases being rounded away from zero. The rounded
1869 -- value of X is checked against I'Range.
1871 -- The catch in the above paragraph is that there is no good way to know
1872 -- whether the round-to-integer operation resulted in overflow. A remedy is
1873 -- to perform a range check in the floating-point domain instead, however:
1875 -- (1) The bounds may not be known at compile time
1876 -- (2) The check must take into account rounding or truncation.
1877 -- (3) The range of type I may not be exactly representable in F.
1878 -- (4) For the rounding case, The end-points I'First - 0.5 and
1879 -- I'Last + 0.5 may or may not be in range, depending on the
1880 -- sign of I'First and I'Last.
1881 -- (5) X may be a NaN, which will fail any comparison
1883 -- The following steps correctly convert X with rounding:
1885 -- (1) If either I'First or I'Last is not known at compile time, use
1886 -- I'Base instead of I in the next three steps and perform a
1887 -- regular range check against I'Range after conversion.
1888 -- (2) If I'First - 0.5 is representable in F then let Lo be that
1889 -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be
1890 -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First).
1891 -- In other words, take one of the closest floating-point numbers
1892 -- (which is an integer value) to I'First, and see if it is in
1894 -- (3) If I'Last + 0.5 is representable in F then let Hi be that value
1895 -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be
1896 -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last).
1897 -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo)
1898 -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi)
1900 -- For the truncating case, replace steps (2) and (3) as follows:
1901 -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK
1902 -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let
1904 -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK
1905 -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let
1908 procedure Apply_Float_Conversion_Check
1910 Target_Typ
: Entity_Id
)
1912 LB
: constant Node_Id
:= Type_Low_Bound
(Target_Typ
);
1913 HB
: constant Node_Id
:= Type_High_Bound
(Target_Typ
);
1914 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
1915 Expr_Type
: constant Entity_Id
:= Base_Type
(Etype
(Ck_Node
));
1916 Target_Base
: constant Entity_Id
:=
1917 Implementation_Base_Type
(Target_Typ
);
1919 Par
: constant Node_Id
:= Parent
(Ck_Node
);
1920 pragma Assert
(Nkind
(Par
) = N_Type_Conversion
);
1921 -- Parent of check node, must be a type conversion
1923 Truncate
: constant Boolean := Float_Truncate
(Par
);
1924 Max_Bound
: constant Uint
:=
1926 (Machine_Radix_Value
(Expr_Type
),
1927 Machine_Mantissa_Value
(Expr_Type
) - 1) - 1;
1929 -- Largest bound, so bound plus or minus half is a machine number of F
1931 Ifirst
, Ilast
: Uint
;
1932 -- Bounds of integer type
1935 -- Bounds to check in floating-point domain
1937 Lo_OK
, Hi_OK
: Boolean;
1938 -- True iff Lo resp. Hi belongs to I'Range
1940 Lo_Chk
, Hi_Chk
: Node_Id
;
1941 -- Expressions that are False iff check fails
1943 Reason
: RT_Exception_Code
;
1946 -- We do not need checks if we are not generating code (i.e. the full
1947 -- expander is not active). In SPARK mode, we specifically don't want
1948 -- the frontend to expand these checks, which are dealt with directly
1949 -- in the formal verification backend.
1951 if not Expander_Active
then
1955 if not Compile_Time_Known_Value
(LB
)
1956 or not Compile_Time_Known_Value
(HB
)
1959 -- First check that the value falls in the range of the base type,
1960 -- to prevent overflow during conversion and then perform a
1961 -- regular range check against the (dynamic) bounds.
1963 pragma Assert
(Target_Base
/= Target_Typ
);
1965 Temp
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', Par
);
1968 Apply_Float_Conversion_Check
(Ck_Node
, Target_Base
);
1969 Set_Etype
(Temp
, Target_Base
);
1971 Insert_Action
(Parent
(Par
),
1972 Make_Object_Declaration
(Loc
,
1973 Defining_Identifier
=> Temp
,
1974 Object_Definition
=> New_Occurrence_Of
(Target_Typ
, Loc
),
1975 Expression
=> New_Copy_Tree
(Par
)),
1976 Suppress
=> All_Checks
);
1979 Make_Raise_Constraint_Error
(Loc
,
1982 Left_Opnd
=> New_Occurrence_Of
(Temp
, Loc
),
1983 Right_Opnd
=> New_Occurrence_Of
(Target_Typ
, Loc
)),
1984 Reason
=> CE_Range_Check_Failed
));
1985 Rewrite
(Par
, New_Occurrence_Of
(Temp
, Loc
));
1991 -- Get the (static) bounds of the target type
1993 Ifirst
:= Expr_Value
(LB
);
1994 Ilast
:= Expr_Value
(HB
);
1996 -- A simple optimization: if the expression is a universal literal,
1997 -- we can do the comparison with the bounds and the conversion to
1998 -- an integer type statically. The range checks are unchanged.
2000 if Nkind
(Ck_Node
) = N_Real_Literal
2001 and then Etype
(Ck_Node
) = Universal_Real
2002 and then Is_Integer_Type
(Target_Typ
)
2003 and then Nkind
(Parent
(Ck_Node
)) = N_Type_Conversion
2006 Int_Val
: constant Uint
:= UR_To_Uint
(Realval
(Ck_Node
));
2009 if Int_Val
<= Ilast
and then Int_Val
>= Ifirst
then
2011 -- Conversion is safe
2013 Rewrite
(Parent
(Ck_Node
),
2014 Make_Integer_Literal
(Loc
, UI_To_Int
(Int_Val
)));
2015 Analyze_And_Resolve
(Parent
(Ck_Node
), Target_Typ
);
2021 -- Check against lower bound
2023 if Truncate
and then Ifirst
> 0 then
2024 Lo
:= Pred
(Expr_Type
, UR_From_Uint
(Ifirst
));
2028 Lo
:= Succ
(Expr_Type
, UR_From_Uint
(Ifirst
- 1));
2031 elsif abs (Ifirst
) < Max_Bound
then
2032 Lo
:= UR_From_Uint
(Ifirst
) - Ureal_Half
;
2033 Lo_OK
:= (Ifirst
> 0);
2036 Lo
:= Machine
(Expr_Type
, UR_From_Uint
(Ifirst
), Round_Even
, Ck_Node
);
2037 Lo_OK
:= (Lo
>= UR_From_Uint
(Ifirst
));
2042 -- Lo_Chk := (X >= Lo)
2044 Lo_Chk
:= Make_Op_Ge
(Loc
,
2045 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Ck_Node
),
2046 Right_Opnd
=> Make_Real_Literal
(Loc
, Lo
));
2049 -- Lo_Chk := (X > Lo)
2051 Lo_Chk
:= Make_Op_Gt
(Loc
,
2052 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Ck_Node
),
2053 Right_Opnd
=> Make_Real_Literal
(Loc
, Lo
));
2056 -- Check against higher bound
2058 if Truncate
and then Ilast
< 0 then
2059 Hi
:= Succ
(Expr_Type
, UR_From_Uint
(Ilast
));
2063 Hi
:= Pred
(Expr_Type
, UR_From_Uint
(Ilast
+ 1));
2066 elsif abs (Ilast
) < Max_Bound
then
2067 Hi
:= UR_From_Uint
(Ilast
) + Ureal_Half
;
2068 Hi_OK
:= (Ilast
< 0);
2070 Hi
:= Machine
(Expr_Type
, UR_From_Uint
(Ilast
), Round_Even
, Ck_Node
);
2071 Hi_OK
:= (Hi
<= UR_From_Uint
(Ilast
));
2076 -- Hi_Chk := (X <= Hi)
2078 Hi_Chk
:= Make_Op_Le
(Loc
,
2079 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Ck_Node
),
2080 Right_Opnd
=> Make_Real_Literal
(Loc
, Hi
));
2083 -- Hi_Chk := (X < Hi)
2085 Hi_Chk
:= Make_Op_Lt
(Loc
,
2086 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Ck_Node
),
2087 Right_Opnd
=> Make_Real_Literal
(Loc
, Hi
));
2090 -- If the bounds of the target type are the same as those of the base
2091 -- type, the check is an overflow check as a range check is not
2092 -- performed in these cases.
2094 if Expr_Value
(Type_Low_Bound
(Target_Base
)) = Ifirst
2095 and then Expr_Value
(Type_High_Bound
(Target_Base
)) = Ilast
2097 Reason
:= CE_Overflow_Check_Failed
;
2099 Reason
:= CE_Range_Check_Failed
;
2102 -- Raise CE if either conditions does not hold
2104 Insert_Action
(Ck_Node
,
2105 Make_Raise_Constraint_Error
(Loc
,
2106 Condition
=> Make_Op_Not
(Loc
, Make_And_Then
(Loc
, Lo_Chk
, Hi_Chk
)),
2108 end Apply_Float_Conversion_Check
;
2110 ------------------------
2111 -- Apply_Length_Check --
2112 ------------------------
2114 procedure Apply_Length_Check
2116 Target_Typ
: Entity_Id
;
2117 Source_Typ
: Entity_Id
:= Empty
)
2120 Apply_Selected_Length_Checks
2121 (Ck_Node
, Target_Typ
, Source_Typ
, Do_Static
=> False);
2122 end Apply_Length_Check
;
2124 -------------------------------------
2125 -- Apply_Parameter_Aliasing_Checks --
2126 -------------------------------------
2128 procedure Apply_Parameter_Aliasing_Checks
2132 Loc
: constant Source_Ptr
:= Sloc
(Call
);
2134 function May_Cause_Aliasing
2135 (Formal_1
: Entity_Id
;
2136 Formal_2
: Entity_Id
) return Boolean;
2137 -- Determine whether two formal parameters can alias each other
2138 -- depending on their modes.
2140 function Original_Actual
(N
: Node_Id
) return Node_Id
;
2141 -- The expander may replace an actual with a temporary for the sake of
2142 -- side effect removal. The temporary may hide a potential aliasing as
2143 -- it does not share the address of the actual. This routine attempts
2144 -- to retrieve the original actual.
2146 procedure Overlap_Check
2147 (Actual_1
: Node_Id
;
2149 Formal_1
: Entity_Id
;
2150 Formal_2
: Entity_Id
;
2151 Check
: in out Node_Id
);
2152 -- Create a check to determine whether Actual_1 overlaps with Actual_2.
2153 -- If detailed exception messages are enabled, the check is augmented to
2154 -- provide information about the names of the corresponding formals. See
2155 -- the body for details. Actual_1 and Actual_2 denote the two actuals to
2156 -- be tested. Formal_1 and Formal_2 denote the corresponding formals.
2157 -- Check contains all and-ed simple tests generated so far or remains
2158 -- unchanged in the case of detailed exception messaged.
2160 ------------------------
2161 -- May_Cause_Aliasing --
2162 ------------------------
2164 function May_Cause_Aliasing
2165 (Formal_1
: Entity_Id
;
2166 Formal_2
: Entity_Id
) return Boolean
2169 -- The following combination cannot lead to aliasing
2171 -- Formal 1 Formal 2
2174 if Ekind
(Formal_1
) = E_In_Parameter
2176 Ekind
(Formal_2
) = E_In_Parameter
2180 -- The following combinations may lead to aliasing
2182 -- Formal 1 Formal 2
2192 end May_Cause_Aliasing
;
2194 ---------------------
2195 -- Original_Actual --
2196 ---------------------
2198 function Original_Actual
(N
: Node_Id
) return Node_Id
is
2200 if Nkind
(N
) = N_Type_Conversion
then
2201 return Expression
(N
);
2203 -- The expander created a temporary to capture the result of a type
2204 -- conversion where the expression is the real actual.
2206 elsif Nkind
(N
) = N_Identifier
2207 and then Present
(Original_Node
(N
))
2208 and then Nkind
(Original_Node
(N
)) = N_Type_Conversion
2210 return Expression
(Original_Node
(N
));
2214 end Original_Actual
;
2220 procedure Overlap_Check
2221 (Actual_1
: Node_Id
;
2223 Formal_1
: Entity_Id
;
2224 Formal_2
: Entity_Id
;
2225 Check
: in out Node_Id
)
2228 ID_Casing
: constant Casing_Type
:=
2229 Identifier_Casing
(Source_Index
(Current_Sem_Unit
));
2233 -- Actual_1'Overlaps_Storage (Actual_2)
2236 Make_Attribute_Reference
(Loc
,
2237 Prefix
=> New_Copy_Tree
(Original_Actual
(Actual_1
)),
2238 Attribute_Name
=> Name_Overlaps_Storage
,
2240 New_List
(New_Copy_Tree
(Original_Actual
(Actual_2
))));
2242 -- Generate the following check when detailed exception messages are
2245 -- if Actual_1'Overlaps_Storage (Actual_2) then
2246 -- raise Program_Error with <detailed message>;
2249 if Exception_Extra_Info
then
2252 -- Do not generate location information for internal calls
2254 if Comes_From_Source
(Call
) then
2255 Store_String_Chars
(Build_Location_String
(Loc
));
2256 Store_String_Char
(' ');
2259 Store_String_Chars
("aliased parameters, actuals for """);
2261 Get_Name_String
(Chars
(Formal_1
));
2262 Set_Casing
(ID_Casing
);
2263 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
2265 Store_String_Chars
(""" and """);
2267 Get_Name_String
(Chars
(Formal_2
));
2268 Set_Casing
(ID_Casing
);
2269 Store_String_Chars
(Name_Buffer
(1 .. Name_Len
));
2271 Store_String_Chars
(""" overlap");
2273 Insert_Action
(Call
,
2274 Make_If_Statement
(Loc
,
2276 Then_Statements
=> New_List
(
2277 Make_Raise_Statement
(Loc
,
2279 New_Occurrence_Of
(Standard_Program_Error
, Loc
),
2280 Expression
=> Make_String_Literal
(Loc
, End_String
)))));
2282 -- Create a sequence of overlapping checks by and-ing them all
2292 Right_Opnd
=> Cond
);
2302 Formal_1
: Entity_Id
;
2303 Formal_2
: Entity_Id
;
2304 Orig_Act_1
: Node_Id
;
2305 Orig_Act_2
: Node_Id
;
2307 -- Start of processing for Apply_Parameter_Aliasing_Checks
2312 Actual_1
:= First_Actual
(Call
);
2313 Formal_1
:= First_Formal
(Subp
);
2314 while Present
(Actual_1
) and then Present
(Formal_1
) loop
2315 Orig_Act_1
:= Original_Actual
(Actual_1
);
2317 -- Ensure that the actual is an object that is not passed by value.
2318 -- Elementary types are always passed by value, therefore actuals of
2319 -- such types cannot lead to aliasing. An aggregate is an object in
2320 -- Ada 2012, but an actual that is an aggregate cannot overlap with
2321 -- another actual. A type that is By_Reference (such as an array of
2322 -- controlled types) is not subject to the check because any update
2323 -- will be done in place and a subsequent read will always see the
2324 -- correct value, see RM 6.2 (12/3).
2326 if Nkind
(Orig_Act_1
) = N_Aggregate
2327 or else (Nkind
(Orig_Act_1
) = N_Qualified_Expression
2328 and then Nkind
(Expression
(Orig_Act_1
)) = N_Aggregate
)
2332 elsif Is_Object_Reference
(Orig_Act_1
)
2333 and then not Is_Elementary_Type
(Etype
(Orig_Act_1
))
2334 and then not Is_By_Reference_Type
(Etype
(Orig_Act_1
))
2336 Actual_2
:= Next_Actual
(Actual_1
);
2337 Formal_2
:= Next_Formal
(Formal_1
);
2338 while Present
(Actual_2
) and then Present
(Formal_2
) loop
2339 Orig_Act_2
:= Original_Actual
(Actual_2
);
2341 -- The other actual we are testing against must also denote
2342 -- a non pass-by-value object. Generate the check only when
2343 -- the mode of the two formals may lead to aliasing.
2345 if Is_Object_Reference
(Orig_Act_2
)
2346 and then not Is_Elementary_Type
(Etype
(Orig_Act_2
))
2347 and then May_Cause_Aliasing
(Formal_1
, Formal_2
)
2350 (Actual_1
=> Actual_1
,
2351 Actual_2
=> Actual_2
,
2352 Formal_1
=> Formal_1
,
2353 Formal_2
=> Formal_2
,
2357 Next_Actual
(Actual_2
);
2358 Next_Formal
(Formal_2
);
2362 Next_Actual
(Actual_1
);
2363 Next_Formal
(Formal_1
);
2366 -- Place a simple check right before the call
2368 if Present
(Check
) and then not Exception_Extra_Info
then
2369 Insert_Action
(Call
,
2370 Make_Raise_Program_Error
(Loc
,
2372 Reason
=> PE_Aliased_Parameters
));
2374 end Apply_Parameter_Aliasing_Checks
;
2376 -------------------------------------
2377 -- Apply_Parameter_Validity_Checks --
2378 -------------------------------------
2380 procedure Apply_Parameter_Validity_Checks
(Subp
: Entity_Id
) is
2381 Subp_Decl
: Node_Id
;
2383 procedure Add_Validity_Check
2384 (Formal
: Entity_Id
;
2386 For_Result
: Boolean := False);
2387 -- Add a single 'Valid[_Scalar] check which verifies the initialization
2388 -- of Formal. Prag_Nam denotes the pre or post condition pragma name.
2389 -- Set flag For_Result when to verify the result of a function.
2391 ------------------------
2392 -- Add_Validity_Check --
2393 ------------------------
2395 procedure Add_Validity_Check
2396 (Formal
: Entity_Id
;
2398 For_Result
: Boolean := False)
2400 procedure Build_Pre_Post_Condition
(Expr
: Node_Id
);
2401 -- Create a pre/postcondition pragma that tests expression Expr
2403 ------------------------------
2404 -- Build_Pre_Post_Condition --
2405 ------------------------------
2407 procedure Build_Pre_Post_Condition
(Expr
: Node_Id
) is
2408 Loc
: constant Source_Ptr
:= Sloc
(Subp
);
2415 Pragma_Identifier
=>
2416 Make_Identifier
(Loc
, Prag_Nam
),
2417 Pragma_Argument_Associations
=> New_List
(
2418 Make_Pragma_Argument_Association
(Loc
,
2419 Chars
=> Name_Check
,
2420 Expression
=> Expr
)));
2422 -- Add a message unless exception messages are suppressed
2424 if not Exception_Locations_Suppressed
then
2425 Append_To
(Pragma_Argument_Associations
(Prag
),
2426 Make_Pragma_Argument_Association
(Loc
,
2427 Chars
=> Name_Message
,
2429 Make_String_Literal
(Loc
,
2431 & Get_Name_String
(Prag_Nam
)
2433 & Build_Location_String
(Loc
))));
2436 -- Insert the pragma in the tree
2438 if Nkind
(Parent
(Subp_Decl
)) = N_Compilation_Unit
then
2439 Add_Global_Declaration
(Prag
);
2442 -- PPC pragmas associated with subprogram bodies must be inserted
2443 -- in the declarative part of the body.
2445 elsif Nkind
(Subp_Decl
) = N_Subprogram_Body
then
2446 Decls
:= Declarations
(Subp_Decl
);
2450 Set_Declarations
(Subp_Decl
, Decls
);
2453 Prepend_To
(Decls
, Prag
);
2456 -- For subprogram declarations insert the PPC pragma right after
2457 -- the declarative node.
2460 Insert_After_And_Analyze
(Subp_Decl
, Prag
);
2462 end Build_Pre_Post_Condition
;
2466 Loc
: constant Source_Ptr
:= Sloc
(Subp
);
2467 Typ
: constant Entity_Id
:= Etype
(Formal
);
2471 -- Start of processing for Add_Validity_Check
2474 -- For scalars, generate 'Valid test
2476 if Is_Scalar_Type
(Typ
) then
2479 -- For any non-scalar with scalar parts, generate 'Valid_Scalars test
2481 elsif Scalar_Part_Present
(Typ
) then
2482 Nam
:= Name_Valid_Scalars
;
2484 -- No test needed for other cases (no scalars to test)
2490 -- Step 1: Create the expression to verify the validity of the
2493 Check
:= New_Occurrence_Of
(Formal
, Loc
);
2495 -- When processing a function result, use 'Result. Generate
2500 Make_Attribute_Reference
(Loc
,
2502 Attribute_Name
=> Name_Result
);
2506 -- Context['Result]'Valid[_Scalars]
2509 Make_Attribute_Reference
(Loc
,
2511 Attribute_Name
=> Nam
);
2513 -- Step 2: Create a pre or post condition pragma
2515 Build_Pre_Post_Condition
(Check
);
2516 end Add_Validity_Check
;
2521 Subp_Spec
: Node_Id
;
2523 -- Start of processing for Apply_Parameter_Validity_Checks
2526 -- Extract the subprogram specification and declaration nodes
2528 Subp_Spec
:= Parent
(Subp
);
2530 if Nkind
(Subp_Spec
) = N_Defining_Program_Unit_Name
then
2531 Subp_Spec
:= Parent
(Subp_Spec
);
2534 Subp_Decl
:= Parent
(Subp_Spec
);
2536 if not Comes_From_Source
(Subp
)
2538 -- Do not process formal subprograms because the corresponding actual
2539 -- will receive the proper checks when the instance is analyzed.
2541 or else Is_Formal_Subprogram
(Subp
)
2543 -- Do not process imported subprograms since pre and postconditions
2544 -- are never verified on routines coming from a different language.
2546 or else Is_Imported
(Subp
)
2547 or else Is_Intrinsic_Subprogram
(Subp
)
2549 -- The PPC pragmas generated by this routine do not correspond to
2550 -- source aspects, therefore they cannot be applied to abstract
2553 or else Nkind
(Subp_Decl
) = N_Abstract_Subprogram_Declaration
2555 -- Do not consider subprogram renaminds because the renamed entity
2556 -- already has the proper PPC pragmas.
2558 or else Nkind
(Subp_Decl
) = N_Subprogram_Renaming_Declaration
2560 -- Do not process null procedures because there is no benefit of
2561 -- adding the checks to a no action routine.
2563 or else (Nkind
(Subp_Spec
) = N_Procedure_Specification
2564 and then Null_Present
(Subp_Spec
))
2569 -- Inspect all the formals applying aliasing and scalar initialization
2570 -- checks where applicable.
2572 Formal
:= First_Formal
(Subp
);
2573 while Present
(Formal
) loop
2575 -- Generate the following scalar initialization checks for each
2576 -- formal parameter:
2578 -- mode IN - Pre => Formal'Valid[_Scalars]
2579 -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars]
2580 -- mode OUT - Post => Formal'Valid[_Scalars]
2582 if Check_Validity_Of_Parameters
then
2583 if Ekind_In
(Formal
, E_In_Parameter
, E_In_Out_Parameter
) then
2584 Add_Validity_Check
(Formal
, Name_Precondition
, False);
2587 if Ekind_In
(Formal
, E_In_Out_Parameter
, E_Out_Parameter
) then
2588 Add_Validity_Check
(Formal
, Name_Postcondition
, False);
2592 Next_Formal
(Formal
);
2595 -- Generate following scalar initialization check for function result:
2597 -- Post => Subp'Result'Valid[_Scalars]
2599 if Check_Validity_Of_Parameters
and then Ekind
(Subp
) = E_Function
then
2600 Add_Validity_Check
(Subp
, Name_Postcondition
, True);
2602 end Apply_Parameter_Validity_Checks
;
2604 ---------------------------
2605 -- Apply_Predicate_Check --
2606 ---------------------------
2608 procedure Apply_Predicate_Check
(N
: Node_Id
; Typ
: Entity_Id
) is
2612 if Predicate_Checks_Suppressed
(Empty
) then
2615 elsif Predicates_Ignored
(Typ
) then
2618 elsif Present
(Predicate_Function
(Typ
)) then
2620 while Present
(S
) and then not Is_Subprogram
(S
) loop
2624 -- A predicate check does not apply within internally generated
2625 -- subprograms, such as TSS functions.
2627 if Within_Internal_Subprogram
then
2630 -- If the check appears within the predicate function itself, it
2631 -- means that the user specified a check whose formal is the
2632 -- predicated subtype itself, rather than some covering type. This
2633 -- is likely to be a common error, and thus deserves a warning.
2635 elsif Present
(S
) and then S
= Predicate_Function
(Typ
) then
2637 ("predicate check includes a function call that "
2638 & "requires a predicate check??", Parent
(N
));
2640 ("\this will result in infinite recursion??", Parent
(N
));
2642 Make_Raise_Storage_Error
(Sloc
(N
),
2643 Reason
=> SE_Infinite_Recursion
));
2645 -- Here for normal case of predicate active
2648 -- If the type has a static predicate and the expression is known
2649 -- at compile time, see if the expression satisfies the predicate.
2651 Check_Expression_Against_Static_Predicate
(N
, Typ
);
2653 if not Expander_Active
then
2657 -- For an entity of the type, generate a call to the predicate
2658 -- function, unless its type is an actual subtype, which is not
2659 -- visible outside of the enclosing subprogram.
2661 if Is_Entity_Name
(N
)
2662 and then not Is_Actual_Subtype
(Typ
)
2665 Make_Predicate_Check
2666 (Typ
, New_Occurrence_Of
(Entity
(N
), Sloc
(N
))));
2668 -- If the expression is not an entity it may have side effects,
2669 -- and the following call will create an object declaration for
2670 -- it. We disable checks during its analysis, to prevent an
2671 -- infinite recursion.
2675 Make_Predicate_Check
2676 (Typ
, Duplicate_Subexpr
(N
)), Suppress
=> All_Checks
);
2680 end Apply_Predicate_Check
;
2682 -----------------------
2683 -- Apply_Range_Check --
2684 -----------------------
2686 procedure Apply_Range_Check
2688 Target_Typ
: Entity_Id
;
2689 Source_Typ
: Entity_Id
:= Empty
)
2692 Apply_Selected_Range_Checks
2693 (Ck_Node
, Target_Typ
, Source_Typ
, Do_Static
=> False);
2694 end Apply_Range_Check
;
2696 ------------------------------
2697 -- Apply_Scalar_Range_Check --
2698 ------------------------------
2700 -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag
2701 -- off if it is already set on.
2703 procedure Apply_Scalar_Range_Check
2705 Target_Typ
: Entity_Id
;
2706 Source_Typ
: Entity_Id
:= Empty
;
2707 Fixed_Int
: Boolean := False)
2709 Parnt
: constant Node_Id
:= Parent
(Expr
);
2711 Arr
: Node_Id
:= Empty
; -- initialize to prevent warning
2712 Arr_Typ
: Entity_Id
:= Empty
; -- initialize to prevent warning
2715 Is_Subscr_Ref
: Boolean;
2716 -- Set true if Expr is a subscript
2718 Is_Unconstrained_Subscr_Ref
: Boolean;
2719 -- Set true if Expr is a subscript of an unconstrained array. In this
2720 -- case we do not attempt to do an analysis of the value against the
2721 -- range of the subscript, since we don't know the actual subtype.
2724 -- Set to True if Expr should be regarded as a real value even though
2725 -- the type of Expr might be discrete.
2727 procedure Bad_Value
(Warn
: Boolean := False);
2728 -- Procedure called if value is determined to be out of range. Warn is
2729 -- True to force a warning instead of an error, even when SPARK_Mode is
2736 procedure Bad_Value
(Warn
: Boolean := False) is
2738 Apply_Compile_Time_Constraint_Error
2739 (Expr
, "value not in range of}??", CE_Range_Check_Failed
,
2745 -- Start of processing for Apply_Scalar_Range_Check
2748 -- Return if check obviously not needed
2751 -- Not needed inside generic
2755 -- Not needed if previous error
2757 or else Target_Typ
= Any_Type
2758 or else Nkind
(Expr
) = N_Error
2760 -- Not needed for non-scalar type
2762 or else not Is_Scalar_Type
(Target_Typ
)
2764 -- Not needed if we know node raises CE already
2766 or else Raises_Constraint_Error
(Expr
)
2771 -- Now, see if checks are suppressed
2774 Is_List_Member
(Expr
) and then Nkind
(Parnt
) = N_Indexed_Component
;
2776 if Is_Subscr_Ref
then
2777 Arr
:= Prefix
(Parnt
);
2778 Arr_Typ
:= Get_Actual_Subtype_If_Available
(Arr
);
2780 if Is_Access_Type
(Arr_Typ
) then
2781 Arr_Typ
:= Designated_Type
(Arr_Typ
);
2785 if not Do_Range_Check
(Expr
) then
2787 -- Subscript reference. Check for Index_Checks suppressed
2789 if Is_Subscr_Ref
then
2791 -- Check array type and its base type
2793 if Index_Checks_Suppressed
(Arr_Typ
)
2794 or else Index_Checks_Suppressed
(Base_Type
(Arr_Typ
))
2798 -- Check array itself if it is an entity name
2800 elsif Is_Entity_Name
(Arr
)
2801 and then Index_Checks_Suppressed
(Entity
(Arr
))
2805 -- Check expression itself if it is an entity name
2807 elsif Is_Entity_Name
(Expr
)
2808 and then Index_Checks_Suppressed
(Entity
(Expr
))
2813 -- All other cases, check for Range_Checks suppressed
2816 -- Check target type and its base type
2818 if Range_Checks_Suppressed
(Target_Typ
)
2819 or else Range_Checks_Suppressed
(Base_Type
(Target_Typ
))
2823 -- Check expression itself if it is an entity name
2825 elsif Is_Entity_Name
(Expr
)
2826 and then Range_Checks_Suppressed
(Entity
(Expr
))
2830 -- If Expr is part of an assignment statement, then check left
2831 -- side of assignment if it is an entity name.
2833 elsif Nkind
(Parnt
) = N_Assignment_Statement
2834 and then Is_Entity_Name
(Name
(Parnt
))
2835 and then Range_Checks_Suppressed
(Entity
(Name
(Parnt
)))
2842 -- Do not set range checks if they are killed
2844 if Nkind
(Expr
) = N_Unchecked_Type_Conversion
2845 and then Kill_Range_Check
(Expr
)
2850 -- Do not set range checks for any values from System.Scalar_Values
2851 -- since the whole idea of such values is to avoid checking them.
2853 if Is_Entity_Name
(Expr
)
2854 and then Is_RTU
(Scope
(Entity
(Expr
)), System_Scalar_Values
)
2859 -- Now see if we need a check
2861 if No
(Source_Typ
) then
2862 S_Typ
:= Etype
(Expr
);
2864 S_Typ
:= Source_Typ
;
2867 if not Is_Scalar_Type
(S_Typ
) or else S_Typ
= Any_Type
then
2871 Is_Unconstrained_Subscr_Ref
:=
2872 Is_Subscr_Ref
and then not Is_Constrained
(Arr_Typ
);
2874 -- Special checks for floating-point type
2876 if Is_Floating_Point_Type
(S_Typ
) then
2878 -- Always do a range check if the source type includes infinities and
2879 -- the target type does not include infinities. We do not do this if
2880 -- range checks are killed.
2881 -- If the expression is a literal and the bounds of the type are
2882 -- static constants it may be possible to optimize the check.
2884 if Has_Infinities
(S_Typ
)
2885 and then not Has_Infinities
(Target_Typ
)
2887 -- If the expression is a literal and the bounds of the type are
2888 -- static constants it may be possible to optimize the check.
2890 if Nkind
(Expr
) = N_Real_Literal
then
2892 Tlo
: constant Node_Id
:= Type_Low_Bound
(Target_Typ
);
2893 Thi
: constant Node_Id
:= Type_High_Bound
(Target_Typ
);
2896 if Compile_Time_Known_Value
(Tlo
)
2897 and then Compile_Time_Known_Value
(Thi
)
2898 and then Expr_Value_R
(Expr
) >= Expr_Value_R
(Tlo
)
2899 and then Expr_Value_R
(Expr
) <= Expr_Value_R
(Thi
)
2903 Enable_Range_Check
(Expr
);
2908 Enable_Range_Check
(Expr
);
2913 -- Return if we know expression is definitely in the range of the target
2914 -- type as determined by Determine_Range. Right now we only do this for
2915 -- discrete types, and not fixed-point or floating-point types.
2917 -- The additional less-precise tests below catch these cases
2919 -- Note: skip this if we are given a source_typ, since the point of
2920 -- supplying a Source_Typ is to stop us looking at the expression.
2921 -- We could sharpen this test to be out parameters only ???
2923 if Is_Discrete_Type
(Target_Typ
)
2924 and then Is_Discrete_Type
(Etype
(Expr
))
2925 and then not Is_Unconstrained_Subscr_Ref
2926 and then No
(Source_Typ
)
2929 Tlo
: constant Node_Id
:= Type_Low_Bound
(Target_Typ
);
2930 Thi
: constant Node_Id
:= Type_High_Bound
(Target_Typ
);
2935 if Compile_Time_Known_Value
(Tlo
)
2936 and then Compile_Time_Known_Value
(Thi
)
2939 Lov
: constant Uint
:= Expr_Value
(Tlo
);
2940 Hiv
: constant Uint
:= Expr_Value
(Thi
);
2943 -- If range is null, we for sure have a constraint error
2944 -- (we don't even need to look at the value involved,
2945 -- since all possible values will raise CE).
2949 -- When SPARK_Mode is On, force a warning instead of
2950 -- an error in that case, as this likely corresponds
2951 -- to deactivated code.
2953 Bad_Value
(Warn
=> SPARK_Mode
= On
);
2955 -- In GNATprove mode, we enable the range check so that
2956 -- GNATprove will issue a message if it cannot be proved.
2958 if GNATprove_Mode
then
2959 Enable_Range_Check
(Expr
);
2965 -- Otherwise determine range of value
2967 Determine_Range
(Expr
, OK
, Lo
, Hi
, Assume_Valid
=> True);
2971 -- If definitely in range, all OK
2973 if Lo
>= Lov
and then Hi
<= Hiv
then
2976 -- If definitely not in range, warn
2978 elsif Lov
> Hi
or else Hiv
< Lo
then
2982 -- Otherwise we don't know
2994 Is_Floating_Point_Type
(S_Typ
)
2995 or else (Is_Fixed_Point_Type
(S_Typ
) and then not Fixed_Int
);
2997 -- Check if we can determine at compile time whether Expr is in the
2998 -- range of the target type. Note that if S_Typ is within the bounds
2999 -- of Target_Typ then this must be the case. This check is meaningful
3000 -- only if this is not a conversion between integer and real types.
3002 if not Is_Unconstrained_Subscr_Ref
3003 and then Is_Discrete_Type
(S_Typ
) = Is_Discrete_Type
(Target_Typ
)
3005 (In_Subrange_Of
(S_Typ
, Target_Typ
, Fixed_Int
)
3007 -- Also check if the expression itself is in the range of the
3008 -- target type if it is a known at compile time value. We skip
3009 -- this test if S_Typ is set since for OUT and IN OUT parameters
3010 -- the Expr itself is not relevant to the checking.
3014 and then Is_In_Range
(Expr
, Target_Typ
,
3015 Assume_Valid
=> True,
3016 Fixed_Int
=> Fixed_Int
,
3017 Int_Real
=> Int_Real
)))
3021 elsif Is_Out_Of_Range
(Expr
, Target_Typ
,
3022 Assume_Valid
=> True,
3023 Fixed_Int
=> Fixed_Int
,
3024 Int_Real
=> Int_Real
)
3029 -- Floating-point case
3030 -- In the floating-point case, we only do range checks if the type is
3031 -- constrained. We definitely do NOT want range checks for unconstrained
3032 -- types, since we want to have infinities, except when
3033 -- Check_Float_Overflow is set.
3035 elsif Is_Floating_Point_Type
(S_Typ
) then
3036 if Is_Constrained
(S_Typ
) or else Check_Float_Overflow
then
3037 Enable_Range_Check
(Expr
);
3040 -- For all other cases we enable a range check unconditionally
3043 Enable_Range_Check
(Expr
);
3046 end Apply_Scalar_Range_Check
;
3048 ----------------------------------
3049 -- Apply_Selected_Length_Checks --
3050 ----------------------------------
3052 procedure Apply_Selected_Length_Checks
3054 Target_Typ
: Entity_Id
;
3055 Source_Typ
: Entity_Id
;
3056 Do_Static
: Boolean)
3059 R_Result
: Check_Result
;
3062 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
3063 Checks_On
: constant Boolean :=
3064 (not Index_Checks_Suppressed
(Target_Typ
))
3065 or else (not Length_Checks_Suppressed
(Target_Typ
));
3068 -- Note: this means that we lose some useful warnings if the expander
3069 -- is not active, and we also lose these warnings in SPARK mode ???
3071 if not Expander_Active
then
3076 Selected_Length_Checks
(Ck_Node
, Target_Typ
, Source_Typ
, Empty
);
3078 for J
in 1 .. 2 loop
3079 R_Cno
:= R_Result
(J
);
3080 exit when No
(R_Cno
);
3082 -- A length check may mention an Itype which is attached to a
3083 -- subsequent node. At the top level in a package this can cause
3084 -- an order-of-elaboration problem, so we make sure that the itype
3085 -- is referenced now.
3087 if Ekind
(Current_Scope
) = E_Package
3088 and then Is_Compilation_Unit
(Current_Scope
)
3090 Ensure_Defined
(Target_Typ
, Ck_Node
);
3092 if Present
(Source_Typ
) then
3093 Ensure_Defined
(Source_Typ
, Ck_Node
);
3095 elsif Is_Itype
(Etype
(Ck_Node
)) then
3096 Ensure_Defined
(Etype
(Ck_Node
), Ck_Node
);
3100 -- If the item is a conditional raise of constraint error, then have
3101 -- a look at what check is being performed and ???
3103 if Nkind
(R_Cno
) = N_Raise_Constraint_Error
3104 and then Present
(Condition
(R_Cno
))
3106 Cond
:= Condition
(R_Cno
);
3108 -- Case where node does not now have a dynamic check
3110 if not Has_Dynamic_Length_Check
(Ck_Node
) then
3112 -- If checks are on, just insert the check
3115 Insert_Action
(Ck_Node
, R_Cno
);
3117 if not Do_Static
then
3118 Set_Has_Dynamic_Length_Check
(Ck_Node
);
3121 -- If checks are off, then analyze the length check after
3122 -- temporarily attaching it to the tree in case the relevant
3123 -- condition can be evaluated at compile time. We still want a
3124 -- compile time warning in this case.
3127 Set_Parent
(R_Cno
, Ck_Node
);
3132 -- Output a warning if the condition is known to be True
3134 if Is_Entity_Name
(Cond
)
3135 and then Entity
(Cond
) = Standard_True
3137 Apply_Compile_Time_Constraint_Error
3138 (Ck_Node
, "wrong length for array of}??",
3139 CE_Length_Check_Failed
,
3143 -- If we were only doing a static check, or if checks are not
3144 -- on, then we want to delete the check, since it is not needed.
3145 -- We do this by replacing the if statement by a null statement
3147 elsif Do_Static
or else not Checks_On
then
3148 Remove_Warning_Messages
(R_Cno
);
3149 Rewrite
(R_Cno
, Make_Null_Statement
(Loc
));
3153 Install_Static_Check
(R_Cno
, Loc
);
3156 end Apply_Selected_Length_Checks
;
3158 ---------------------------------
3159 -- Apply_Selected_Range_Checks --
3160 ---------------------------------
3162 procedure Apply_Selected_Range_Checks
3164 Target_Typ
: Entity_Id
;
3165 Source_Typ
: Entity_Id
;
3166 Do_Static
: Boolean)
3168 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
3169 Checks_On
: constant Boolean :=
3170 not Index_Checks_Suppressed
(Target_Typ
)
3172 not Range_Checks_Suppressed
(Target_Typ
);
3176 R_Result
: Check_Result
;
3179 if not Expander_Active
or not Checks_On
then
3184 Selected_Range_Checks
(Ck_Node
, Target_Typ
, Source_Typ
, Empty
);
3186 for J
in 1 .. 2 loop
3187 R_Cno
:= R_Result
(J
);
3188 exit when No
(R_Cno
);
3190 -- The range check requires runtime evaluation. Depending on what its
3191 -- triggering condition is, the check may be converted into a compile
3192 -- time constraint check.
3194 if Nkind
(R_Cno
) = N_Raise_Constraint_Error
3195 and then Present
(Condition
(R_Cno
))
3197 Cond
:= Condition
(R_Cno
);
3199 -- Insert the range check before the related context. Note that
3200 -- this action analyses the triggering condition.
3202 Insert_Action
(Ck_Node
, R_Cno
);
3204 -- This old code doesn't make sense, why is the context flagged as
3205 -- requiring dynamic range checks now in the middle of generating
3208 if not Do_Static
then
3209 Set_Has_Dynamic_Range_Check
(Ck_Node
);
3212 -- The triggering condition evaluates to True, the range check
3213 -- can be converted into a compile time constraint check.
3215 if Is_Entity_Name
(Cond
)
3216 and then Entity
(Cond
) = Standard_True
3218 -- Since an N_Range is technically not an expression, we have
3219 -- to set one of the bounds to C_E and then just flag the
3220 -- N_Range. The warning message will point to the lower bound
3221 -- and complain about a range, which seems OK.
3223 if Nkind
(Ck_Node
) = N_Range
then
3224 Apply_Compile_Time_Constraint_Error
3225 (Low_Bound
(Ck_Node
),
3226 "static range out of bounds of}??",
3227 CE_Range_Check_Failed
,
3231 Set_Raises_Constraint_Error
(Ck_Node
);
3234 Apply_Compile_Time_Constraint_Error
3236 "static value out of range of}??",
3237 CE_Range_Check_Failed
,
3242 -- If we were only doing a static check, or if checks are not
3243 -- on, then we want to delete the check, since it is not needed.
3244 -- We do this by replacing the if statement by a null statement
3246 -- Why are we even generating checks if checks are turned off ???
3248 elsif Do_Static
or else not Checks_On
then
3249 Remove_Warning_Messages
(R_Cno
);
3250 Rewrite
(R_Cno
, Make_Null_Statement
(Loc
));
3253 -- The range check raises Constraint_Error explicitly
3256 Install_Static_Check
(R_Cno
, Loc
);
3259 end Apply_Selected_Range_Checks
;
3261 -------------------------------
3262 -- Apply_Static_Length_Check --
3263 -------------------------------
3265 procedure Apply_Static_Length_Check
3267 Target_Typ
: Entity_Id
;
3268 Source_Typ
: Entity_Id
:= Empty
)
3271 Apply_Selected_Length_Checks
3272 (Expr
, Target_Typ
, Source_Typ
, Do_Static
=> True);
3273 end Apply_Static_Length_Check
;
3275 -------------------------------------
3276 -- Apply_Subscript_Validity_Checks --
3277 -------------------------------------
3279 procedure Apply_Subscript_Validity_Checks
(Expr
: Node_Id
) is
3283 pragma Assert
(Nkind
(Expr
) = N_Indexed_Component
);
3285 -- Loop through subscripts
3287 Sub
:= First
(Expressions
(Expr
));
3288 while Present
(Sub
) loop
3290 -- Check one subscript. Note that we do not worry about enumeration
3291 -- type with holes, since we will convert the value to a Pos value
3292 -- for the subscript, and that convert will do the necessary validity
3295 Ensure_Valid
(Sub
, Holes_OK
=> True);
3297 -- Move to next subscript
3301 end Apply_Subscript_Validity_Checks
;
3303 ----------------------------------
3304 -- Apply_Type_Conversion_Checks --
3305 ----------------------------------
3307 procedure Apply_Type_Conversion_Checks
(N
: Node_Id
) is
3308 Target_Type
: constant Entity_Id
:= Etype
(N
);
3309 Target_Base
: constant Entity_Id
:= Base_Type
(Target_Type
);
3310 Expr
: constant Node_Id
:= Expression
(N
);
3312 Expr_Type
: constant Entity_Id
:= Underlying_Type
(Etype
(Expr
));
3313 -- Note: if Etype (Expr) is a private type without discriminants, its
3314 -- full view might have discriminants with defaults, so we need the
3315 -- full view here to retrieve the constraints.
3318 if Inside_A_Generic
then
3321 -- Skip these checks if serious errors detected, there are some nasty
3322 -- situations of incomplete trees that blow things up.
3324 elsif Serious_Errors_Detected
> 0 then
3327 -- Never generate discriminant checks for Unchecked_Union types
3329 elsif Present
(Expr_Type
)
3330 and then Is_Unchecked_Union
(Expr_Type
)
3334 -- Scalar type conversions of the form Target_Type (Expr) require a
3335 -- range check if we cannot be sure that Expr is in the base type of
3336 -- Target_Typ and also that Expr is in the range of Target_Typ. These
3337 -- are not quite the same condition from an implementation point of
3338 -- view, but clearly the second includes the first.
3340 elsif Is_Scalar_Type
(Target_Type
) then
3342 Conv_OK
: constant Boolean := Conversion_OK
(N
);
3343 -- If the Conversion_OK flag on the type conversion is set and no
3344 -- floating-point type is involved in the type conversion then
3345 -- fixed-point values must be read as integral values.
3347 Float_To_Int
: constant Boolean :=
3348 Is_Floating_Point_Type
(Expr_Type
)
3349 and then Is_Integer_Type
(Target_Type
);
3352 if not Overflow_Checks_Suppressed
(Target_Base
)
3353 and then not Overflow_Checks_Suppressed
(Target_Type
)
3355 In_Subrange_Of
(Expr_Type
, Target_Base
, Fixed_Int
=> Conv_OK
)
3356 and then not Float_To_Int
3358 Activate_Overflow_Check
(N
);
3361 if not Range_Checks_Suppressed
(Target_Type
)
3362 and then not Range_Checks_Suppressed
(Expr_Type
)
3364 if Float_To_Int
then
3365 Apply_Float_Conversion_Check
(Expr
, Target_Type
);
3367 Apply_Scalar_Range_Check
3368 (Expr
, Target_Type
, Fixed_Int
=> Conv_OK
);
3370 -- If the target type has predicates, we need to indicate
3371 -- the need for a check, even if Determine_Range finds that
3372 -- the value is within bounds. This may be the case e.g for
3373 -- a division with a constant denominator.
3375 if Has_Predicates
(Target_Type
) then
3376 Enable_Range_Check
(Expr
);
3382 elsif Comes_From_Source
(N
)
3383 and then not Discriminant_Checks_Suppressed
(Target_Type
)
3384 and then Is_Record_Type
(Target_Type
)
3385 and then Is_Derived_Type
(Target_Type
)
3386 and then not Is_Tagged_Type
(Target_Type
)
3387 and then not Is_Constrained
(Target_Type
)
3388 and then Present
(Stored_Constraint
(Target_Type
))
3390 -- An unconstrained derived type may have inherited discriminant.
3391 -- Build an actual discriminant constraint list using the stored
3392 -- constraint, to verify that the expression of the parent type
3393 -- satisfies the constraints imposed by the (unconstrained) derived
3394 -- type. This applies to value conversions, not to view conversions
3398 Loc
: constant Source_Ptr
:= Sloc
(N
);
3400 Constraint
: Elmt_Id
;
3401 Discr_Value
: Node_Id
;
3404 New_Constraints
: constant Elist_Id
:= New_Elmt_List
;
3405 Old_Constraints
: constant Elist_Id
:=
3406 Discriminant_Constraint
(Expr_Type
);
3409 Constraint
:= First_Elmt
(Stored_Constraint
(Target_Type
));
3410 while Present
(Constraint
) loop
3411 Discr_Value
:= Node
(Constraint
);
3413 if Is_Entity_Name
(Discr_Value
)
3414 and then Ekind
(Entity
(Discr_Value
)) = E_Discriminant
3416 Discr
:= Corresponding_Discriminant
(Entity
(Discr_Value
));
3419 and then Scope
(Discr
) = Base_Type
(Expr_Type
)
3421 -- Parent is constrained by new discriminant. Obtain
3422 -- Value of original discriminant in expression. If the
3423 -- new discriminant has been used to constrain more than
3424 -- one of the stored discriminants, this will provide the
3425 -- required consistency check.
3428 (Make_Selected_Component
(Loc
,
3430 Duplicate_Subexpr_No_Checks
3431 (Expr
, Name_Req
=> True),
3433 Make_Identifier
(Loc
, Chars
(Discr
))),
3437 -- Discriminant of more remote ancestor ???
3442 -- Derived type definition has an explicit value for this
3443 -- stored discriminant.
3447 (Duplicate_Subexpr_No_Checks
(Discr_Value
),
3451 Next_Elmt
(Constraint
);
3454 -- Use the unconstrained expression type to retrieve the
3455 -- discriminants of the parent, and apply momentarily the
3456 -- discriminant constraint synthesized above.
3458 Set_Discriminant_Constraint
(Expr_Type
, New_Constraints
);
3459 Cond
:= Build_Discriminant_Checks
(Expr
, Expr_Type
);
3460 Set_Discriminant_Constraint
(Expr_Type
, Old_Constraints
);
3463 Make_Raise_Constraint_Error
(Loc
,
3465 Reason
=> CE_Discriminant_Check_Failed
));
3468 -- For arrays, checks are set now, but conversions are applied during
3469 -- expansion, to take into accounts changes of representation. The
3470 -- checks become range checks on the base type or length checks on the
3471 -- subtype, depending on whether the target type is unconstrained or
3472 -- constrained. Note that the range check is put on the expression of a
3473 -- type conversion, while the length check is put on the type conversion
3476 elsif Is_Array_Type
(Target_Type
) then
3477 if Is_Constrained
(Target_Type
) then
3478 Set_Do_Length_Check
(N
);
3480 Set_Do_Range_Check
(Expr
);
3483 end Apply_Type_Conversion_Checks
;
3485 ----------------------------------------------
3486 -- Apply_Universal_Integer_Attribute_Checks --
3487 ----------------------------------------------
3489 procedure Apply_Universal_Integer_Attribute_Checks
(N
: Node_Id
) is
3490 Loc
: constant Source_Ptr
:= Sloc
(N
);
3491 Typ
: constant Entity_Id
:= Etype
(N
);
3494 if Inside_A_Generic
then
3497 -- Nothing to do if checks are suppressed
3499 elsif Range_Checks_Suppressed
(Typ
)
3500 and then Overflow_Checks_Suppressed
(Typ
)
3504 -- Nothing to do if the attribute does not come from source. The
3505 -- internal attributes we generate of this type do not need checks,
3506 -- and furthermore the attempt to check them causes some circular
3507 -- elaboration orders when dealing with packed types.
3509 elsif not Comes_From_Source
(N
) then
3512 -- If the prefix is a selected component that depends on a discriminant
3513 -- the check may improperly expose a discriminant instead of using
3514 -- the bounds of the object itself. Set the type of the attribute to
3515 -- the base type of the context, so that a check will be imposed when
3516 -- needed (e.g. if the node appears as an index).
3518 elsif Nkind
(Prefix
(N
)) = N_Selected_Component
3519 and then Ekind
(Typ
) = E_Signed_Integer_Subtype
3520 and then Depends_On_Discriminant
(Scalar_Range
(Typ
))
3522 Set_Etype
(N
, Base_Type
(Typ
));
3524 -- Otherwise, replace the attribute node with a type conversion node
3525 -- whose expression is the attribute, retyped to universal integer, and
3526 -- whose subtype mark is the target type. The call to analyze this
3527 -- conversion will set range and overflow checks as required for proper
3528 -- detection of an out of range value.
3531 Set_Etype
(N
, Universal_Integer
);
3532 Set_Analyzed
(N
, True);
3535 Make_Type_Conversion
(Loc
,
3536 Subtype_Mark
=> New_Occurrence_Of
(Typ
, Loc
),
3537 Expression
=> Relocate_Node
(N
)));
3539 Analyze_And_Resolve
(N
, Typ
);
3542 end Apply_Universal_Integer_Attribute_Checks
;
3544 -------------------------------------
3545 -- Atomic_Synchronization_Disabled --
3546 -------------------------------------
3548 -- Note: internally Disable/Enable_Atomic_Synchronization is implemented
3549 -- using a bogus check called Atomic_Synchronization. This is to make it
3550 -- more convenient to get exactly the same semantics as [Un]Suppress.
3552 function Atomic_Synchronization_Disabled
(E
: Entity_Id
) return Boolean is
3554 -- If debug flag d.e is set, always return False, i.e. all atomic sync
3555 -- looks enabled, since it is never disabled.
3557 if Debug_Flag_Dot_E
then
3560 -- If debug flag d.d is set then always return True, i.e. all atomic
3561 -- sync looks disabled, since it always tests True.
3563 elsif Debug_Flag_Dot_D
then
3566 -- If entity present, then check result for that entity
3568 elsif Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
3569 return Is_Check_Suppressed
(E
, Atomic_Synchronization
);
3571 -- Otherwise result depends on current scope setting
3574 return Scope_Suppress
.Suppress
(Atomic_Synchronization
);
3576 end Atomic_Synchronization_Disabled
;
3578 -------------------------------
3579 -- Build_Discriminant_Checks --
3580 -------------------------------
3582 function Build_Discriminant_Checks
3584 T_Typ
: Entity_Id
) return Node_Id
3586 Loc
: constant Source_Ptr
:= Sloc
(N
);
3589 Disc_Ent
: Entity_Id
;
3593 function Aggregate_Discriminant_Val
(Disc
: Entity_Id
) return Node_Id
;
3595 ----------------------------------
3596 -- Aggregate_Discriminant_Value --
3597 ----------------------------------
3599 function Aggregate_Discriminant_Val
(Disc
: Entity_Id
) return Node_Id
is
3603 -- The aggregate has been normalized with named associations. We use
3604 -- the Chars field to locate the discriminant to take into account
3605 -- discriminants in derived types, which carry the same name as those
3608 Assoc
:= First
(Component_Associations
(N
));
3609 while Present
(Assoc
) loop
3610 if Chars
(First
(Choices
(Assoc
))) = Chars
(Disc
) then
3611 return Expression
(Assoc
);
3617 -- Discriminant must have been found in the loop above
3619 raise Program_Error
;
3620 end Aggregate_Discriminant_Val
;
3622 -- Start of processing for Build_Discriminant_Checks
3625 -- Loop through discriminants evolving the condition
3628 Disc
:= First_Elmt
(Discriminant_Constraint
(T_Typ
));
3630 -- For a fully private type, use the discriminants of the parent type
3632 if Is_Private_Type
(T_Typ
)
3633 and then No
(Full_View
(T_Typ
))
3635 Disc_Ent
:= First_Discriminant
(Etype
(Base_Type
(T_Typ
)));
3637 Disc_Ent
:= First_Discriminant
(T_Typ
);
3640 while Present
(Disc
) loop
3641 Dval
:= Node
(Disc
);
3643 if Nkind
(Dval
) = N_Identifier
3644 and then Ekind
(Entity
(Dval
)) = E_Discriminant
3646 Dval
:= New_Occurrence_Of
(Discriminal
(Entity
(Dval
)), Loc
);
3648 Dval
:= Duplicate_Subexpr_No_Checks
(Dval
);
3651 -- If we have an Unchecked_Union node, we can infer the discriminants
3654 if Is_Unchecked_Union
(Base_Type
(T_Typ
)) then
3656 Get_Discriminant_Value
(
3657 First_Discriminant
(T_Typ
),
3659 Stored_Constraint
(T_Typ
)));
3661 elsif Nkind
(N
) = N_Aggregate
then
3663 Duplicate_Subexpr_No_Checks
3664 (Aggregate_Discriminant_Val
(Disc_Ent
));
3668 Make_Selected_Component
(Loc
,
3670 Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True),
3671 Selector_Name
=> Make_Identifier
(Loc
, Chars
(Disc_Ent
)));
3673 Set_Is_In_Discriminant_Check
(Dref
);
3676 Evolve_Or_Else
(Cond
,
3679 Right_Opnd
=> Dval
));
3682 Next_Discriminant
(Disc_Ent
);
3686 end Build_Discriminant_Checks
;
3692 function Check_Needed
(Nod
: Node_Id
; Check
: Check_Type
) return Boolean is
3699 function Left_Expression
(Op
: Node_Id
) return Node_Id
;
3700 -- Return the relevant expression from the left operand of the given
3701 -- short circuit form: this is LO itself, except if LO is a qualified
3702 -- expression, a type conversion, or an expression with actions, in
3703 -- which case this is Left_Expression (Expression (LO)).
3705 ---------------------
3706 -- Left_Expression --
3707 ---------------------
3709 function Left_Expression
(Op
: Node_Id
) return Node_Id
is
3710 LE
: Node_Id
:= Left_Opnd
(Op
);
3712 while Nkind_In
(LE
, N_Qualified_Expression
,
3714 N_Expression_With_Actions
)
3716 LE
:= Expression
(LE
);
3720 end Left_Expression
;
3722 -- Start of processing for Check_Needed
3725 -- Always check if not simple entity
3727 if Nkind
(Nod
) not in N_Has_Entity
3728 or else not Comes_From_Source
(Nod
)
3733 -- Look up tree for short circuit
3740 -- Done if out of subexpression (note that we allow generated stuff
3741 -- such as itype declarations in this context, to keep the loop going
3742 -- since we may well have generated such stuff in complex situations.
3743 -- Also done if no parent (probably an error condition, but no point
3744 -- in behaving nasty if we find it).
3747 or else (K
not in N_Subexpr
and then Comes_From_Source
(P
))
3751 -- Or/Or Else case, where test is part of the right operand, or is
3752 -- part of one of the actions associated with the right operand, and
3753 -- the left operand is an equality test.
3755 elsif K
= N_Op_Or
then
3756 exit when N
= Right_Opnd
(P
)
3757 and then Nkind
(Left_Expression
(P
)) = N_Op_Eq
;
3759 elsif K
= N_Or_Else
then
3760 exit when (N
= Right_Opnd
(P
)
3763 and then List_Containing
(N
) = Actions
(P
)))
3764 and then Nkind
(Left_Expression
(P
)) = N_Op_Eq
;
3766 -- Similar test for the And/And then case, where the left operand
3767 -- is an inequality test.
3769 elsif K
= N_Op_And
then
3770 exit when N
= Right_Opnd
(P
)
3771 and then Nkind
(Left_Expression
(P
)) = N_Op_Ne
;
3773 elsif K
= N_And_Then
then
3774 exit when (N
= Right_Opnd
(P
)
3777 and then List_Containing
(N
) = Actions
(P
)))
3778 and then Nkind
(Left_Expression
(P
)) = N_Op_Ne
;
3784 -- If we fall through the loop, then we have a conditional with an
3785 -- appropriate test as its left operand, so look further.
3787 L
:= Left_Expression
(P
);
3789 -- L is an "=" or "/=" operator: extract its operands
3791 R
:= Right_Opnd
(L
);
3794 -- Left operand of test must match original variable
3796 if Nkind
(L
) not in N_Has_Entity
or else Entity
(L
) /= Entity
(Nod
) then
3800 -- Right operand of test must be key value (zero or null)
3803 when Access_Check
=>
3804 if not Known_Null
(R
) then
3808 when Division_Check
=>
3809 if not Compile_Time_Known_Value
(R
)
3810 or else Expr_Value
(R
) /= Uint_0
3816 raise Program_Error
;
3819 -- Here we have the optimizable case, warn if not short-circuited
3821 if K
= N_Op_And
or else K
= N_Op_Or
then
3822 Error_Msg_Warn
:= SPARK_Mode
/= On
;
3825 when Access_Check
=>
3826 if GNATprove_Mode
then
3828 ("Constraint_Error might have been raised (access check)",
3832 ("Constraint_Error may be raised (access check)??",
3836 when Division_Check
=>
3837 if GNATprove_Mode
then
3839 ("Constraint_Error might have been raised (zero divide)",
3843 ("Constraint_Error may be raised (zero divide)??",
3848 raise Program_Error
;
3851 if K
= N_Op_And
then
3852 Error_Msg_N
-- CODEFIX
3853 ("use `AND THEN` instead of AND??", P
);
3855 Error_Msg_N
-- CODEFIX
3856 ("use `OR ELSE` instead of OR??", P
);
3859 -- If not short-circuited, we need the check
3863 -- If short-circuited, we can omit the check
3870 -----------------------------------
3871 -- Check_Valid_Lvalue_Subscripts --
3872 -----------------------------------
3874 procedure Check_Valid_Lvalue_Subscripts
(Expr
: Node_Id
) is
3876 -- Skip this if range checks are suppressed
3878 if Range_Checks_Suppressed
(Etype
(Expr
)) then
3881 -- Only do this check for expressions that come from source. We assume
3882 -- that expander generated assignments explicitly include any necessary
3883 -- checks. Note that this is not just an optimization, it avoids
3884 -- infinite recursions.
3886 elsif not Comes_From_Source
(Expr
) then
3889 -- For a selected component, check the prefix
3891 elsif Nkind
(Expr
) = N_Selected_Component
then
3892 Check_Valid_Lvalue_Subscripts
(Prefix
(Expr
));
3895 -- Case of indexed component
3897 elsif Nkind
(Expr
) = N_Indexed_Component
then
3898 Apply_Subscript_Validity_Checks
(Expr
);
3900 -- Prefix may itself be or contain an indexed component, and these
3901 -- subscripts need checking as well.
3903 Check_Valid_Lvalue_Subscripts
(Prefix
(Expr
));
3905 end Check_Valid_Lvalue_Subscripts
;
3907 ----------------------------------
3908 -- Null_Exclusion_Static_Checks --
3909 ----------------------------------
3911 procedure Null_Exclusion_Static_Checks
(N
: Node_Id
) is
3912 Error_Node
: Node_Id
;
3914 Has_Null
: constant Boolean := Has_Null_Exclusion
(N
);
3915 K
: constant Node_Kind
:= Nkind
(N
);
3920 (Nkind_In
(K
, N_Component_Declaration
,
3921 N_Discriminant_Specification
,
3922 N_Function_Specification
,
3923 N_Object_Declaration
,
3924 N_Parameter_Specification
));
3926 if K
= N_Function_Specification
then
3927 Typ
:= Etype
(Defining_Entity
(N
));
3929 Typ
:= Etype
(Defining_Identifier
(N
));
3933 when N_Component_Declaration
=>
3934 if Present
(Access_Definition
(Component_Definition
(N
))) then
3935 Error_Node
:= Component_Definition
(N
);
3937 Error_Node
:= Subtype_Indication
(Component_Definition
(N
));
3940 when N_Discriminant_Specification
=>
3941 Error_Node
:= Discriminant_Type
(N
);
3943 when N_Function_Specification
=>
3944 Error_Node
:= Result_Definition
(N
);
3946 when N_Object_Declaration
=>
3947 Error_Node
:= Object_Definition
(N
);
3949 when N_Parameter_Specification
=>
3950 Error_Node
:= Parameter_Type
(N
);
3953 raise Program_Error
;
3958 -- Enforce legality rule 3.10 (13): A null exclusion can only be
3959 -- applied to an access [sub]type.
3961 if not Is_Access_Type
(Typ
) then
3963 ("`NOT NULL` allowed only for an access type", Error_Node
);
3965 -- Enforce legality rule RM 3.10(14/1): A null exclusion can only
3966 -- be applied to a [sub]type that does not exclude null already.
3968 elsif Can_Never_Be_Null
(Typ
)
3969 and then Comes_From_Source
(Typ
)
3972 ("`NOT NULL` not allowed (& already excludes null)",
3977 -- Check that null-excluding objects are always initialized, except for
3978 -- deferred constants, for which the expression will appear in the full
3981 if K
= N_Object_Declaration
3982 and then No
(Expression
(N
))
3983 and then not Constant_Present
(N
)
3984 and then not No_Initialization
(N
)
3986 -- Add an expression that assigns null. This node is needed by
3987 -- Apply_Compile_Time_Constraint_Error, which will replace this with
3988 -- a Constraint_Error node.
3990 Set_Expression
(N
, Make_Null
(Sloc
(N
)));
3991 Set_Etype
(Expression
(N
), Etype
(Defining_Identifier
(N
)));
3993 Apply_Compile_Time_Constraint_Error
3994 (N
=> Expression
(N
),
3996 "(Ada 2005) null-excluding objects must be initialized??",
3997 Reason
=> CE_Null_Not_Allowed
);
4000 -- Check that a null-excluding component, formal or object is not being
4001 -- assigned a null value. Otherwise generate a warning message and
4002 -- replace Expression (N) by an N_Constraint_Error node.
4004 if K
/= N_Function_Specification
then
4005 Expr
:= Expression
(N
);
4007 if Present
(Expr
) and then Known_Null
(Expr
) then
4009 when N_Component_Declaration |
4010 N_Discriminant_Specification
=>
4011 Apply_Compile_Time_Constraint_Error
4013 Msg
=> "(Ada 2005) null not allowed "
4014 & "in null-excluding components??",
4015 Reason
=> CE_Null_Not_Allowed
);
4017 when N_Object_Declaration
=>
4018 Apply_Compile_Time_Constraint_Error
4020 Msg
=> "(Ada 2005) null not allowed "
4021 & "in null-excluding objects??",
4022 Reason
=> CE_Null_Not_Allowed
);
4024 when N_Parameter_Specification
=>
4025 Apply_Compile_Time_Constraint_Error
4027 Msg
=> "(Ada 2005) null not allowed "
4028 & "in null-excluding formals??",
4029 Reason
=> CE_Null_Not_Allowed
);
4036 end Null_Exclusion_Static_Checks
;
4038 ----------------------------------
4039 -- Conditional_Statements_Begin --
4040 ----------------------------------
4042 procedure Conditional_Statements_Begin
is
4044 Saved_Checks_TOS
:= Saved_Checks_TOS
+ 1;
4046 -- If stack overflows, kill all checks, that way we know to simply reset
4047 -- the number of saved checks to zero on return. This should never occur
4050 if Saved_Checks_TOS
> Saved_Checks_Stack
'Last then
4053 -- In the normal case, we just make a new stack entry saving the current
4054 -- number of saved checks for a later restore.
4057 Saved_Checks_Stack
(Saved_Checks_TOS
) := Num_Saved_Checks
;
4059 if Debug_Flag_CC
then
4060 w
("Conditional_Statements_Begin: Num_Saved_Checks = ",
4064 end Conditional_Statements_Begin
;
4066 --------------------------------
4067 -- Conditional_Statements_End --
4068 --------------------------------
4070 procedure Conditional_Statements_End
is
4072 pragma Assert
(Saved_Checks_TOS
> 0);
4074 -- If the saved checks stack overflowed, then we killed all checks, so
4075 -- setting the number of saved checks back to zero is correct. This
4076 -- should never occur in practice.
4078 if Saved_Checks_TOS
> Saved_Checks_Stack
'Last then
4079 Num_Saved_Checks
:= 0;
4081 -- In the normal case, restore the number of saved checks from the top
4085 Num_Saved_Checks
:= Saved_Checks_Stack
(Saved_Checks_TOS
);
4087 if Debug_Flag_CC
then
4088 w
("Conditional_Statements_End: Num_Saved_Checks = ",
4093 Saved_Checks_TOS
:= Saved_Checks_TOS
- 1;
4094 end Conditional_Statements_End
;
4096 -------------------------
4097 -- Convert_From_Bignum --
4098 -------------------------
4100 function Convert_From_Bignum
(N
: Node_Id
) return Node_Id
is
4101 Loc
: constant Source_Ptr
:= Sloc
(N
);
4104 pragma Assert
(Is_RTE
(Etype
(N
), RE_Bignum
));
4106 -- Construct call From Bignum
4109 Make_Function_Call
(Loc
,
4111 New_Occurrence_Of
(RTE
(RE_From_Bignum
), Loc
),
4112 Parameter_Associations
=> New_List
(Relocate_Node
(N
)));
4113 end Convert_From_Bignum
;
4115 -----------------------
4116 -- Convert_To_Bignum --
4117 -----------------------
4119 function Convert_To_Bignum
(N
: Node_Id
) return Node_Id
is
4120 Loc
: constant Source_Ptr
:= Sloc
(N
);
4123 -- Nothing to do if Bignum already except call Relocate_Node
4125 if Is_RTE
(Etype
(N
), RE_Bignum
) then
4126 return Relocate_Node
(N
);
4128 -- Otherwise construct call to To_Bignum, converting the operand to the
4129 -- required Long_Long_Integer form.
4132 pragma Assert
(Is_Signed_Integer_Type
(Etype
(N
)));
4134 Make_Function_Call
(Loc
,
4136 New_Occurrence_Of
(RTE
(RE_To_Bignum
), Loc
),
4137 Parameter_Associations
=> New_List
(
4138 Convert_To
(Standard_Long_Long_Integer
, Relocate_Node
(N
))));
4140 end Convert_To_Bignum
;
4142 ---------------------
4143 -- Determine_Range --
4144 ---------------------
4146 Cache_Size
: constant := 2 ** 10;
4147 type Cache_Index
is range 0 .. Cache_Size
- 1;
4148 -- Determine size of below cache (power of 2 is more efficient)
4150 Determine_Range_Cache_N
: array (Cache_Index
) of Node_Id
;
4151 Determine_Range_Cache_V
: array (Cache_Index
) of Boolean;
4152 Determine_Range_Cache_Lo
: array (Cache_Index
) of Uint
;
4153 Determine_Range_Cache_Hi
: array (Cache_Index
) of Uint
;
4154 Determine_Range_Cache_Lo_R
: array (Cache_Index
) of Ureal
;
4155 Determine_Range_Cache_Hi_R
: array (Cache_Index
) of Ureal
;
4156 -- The above arrays are used to implement a small direct cache for
4157 -- Determine_Range and Determine_Range_R calls. Because of the way these
4158 -- subprograms recursively traces subexpressions, and because overflow
4159 -- checking calls the routine on the way up the tree, a quadratic behavior
4160 -- can otherwise be encountered in large expressions. The cache entry for
4161 -- node N is stored in the (N mod Cache_Size) entry, and can be validated
4162 -- by checking the actual node value stored there. The Range_Cache_V array
4163 -- records the setting of Assume_Valid for the cache entry.
4165 procedure Determine_Range
4170 Assume_Valid
: Boolean := False)
4172 Typ
: Entity_Id
:= Etype
(N
);
4173 -- Type to use, may get reset to base type for possibly invalid entity
4177 -- Lo and Hi bounds of left operand
4181 -- Lo and Hi bounds of right (or only) operand
4184 -- Temp variable used to hold a bound node
4187 -- High bound of base type of expression
4191 -- Refined values for low and high bounds, after tightening
4194 -- Used in lower level calls to indicate if call succeeded
4196 Cindex
: Cache_Index
;
4197 -- Used to search cache
4202 function OK_Operands
return Boolean;
4203 -- Used for binary operators. Determines the ranges of the left and
4204 -- right operands, and if they are both OK, returns True, and puts
4205 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left.
4211 function OK_Operands
return Boolean is
4214 (Left_Opnd
(N
), OK1
, Lo_Left
, Hi_Left
, Assume_Valid
);
4221 (Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
, Assume_Valid
);
4225 -- Start of processing for Determine_Range
4228 -- Prevent junk warnings by initializing range variables
4235 -- For temporary constants internally generated to remove side effects
4236 -- we must use the corresponding expression to determine the range of
4237 -- the expression. But note that the expander can also generate
4238 -- constants in other cases, including deferred constants.
4240 if Is_Entity_Name
(N
)
4241 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
4242 and then Ekind
(Entity
(N
)) = E_Constant
4243 and then Is_Internal_Name
(Chars
(Entity
(N
)))
4245 if Present
(Expression
(Parent
(Entity
(N
)))) then
4247 (Expression
(Parent
(Entity
(N
))), OK
, Lo
, Hi
, Assume_Valid
);
4249 elsif Present
(Full_View
(Entity
(N
))) then
4251 (Expression
(Parent
(Full_View
(Entity
(N
)))),
4252 OK
, Lo
, Hi
, Assume_Valid
);
4260 -- If type is not defined, we can't determine its range
4264 -- We don't deal with anything except discrete types
4266 or else not Is_Discrete_Type
(Typ
)
4268 -- Ignore type for which an error has been posted, since range in
4269 -- this case may well be a bogosity deriving from the error. Also
4270 -- ignore if error posted on the reference node.
4272 or else Error_Posted
(N
) or else Error_Posted
(Typ
)
4278 -- For all other cases, we can determine the range
4282 -- If value is compile time known, then the possible range is the one
4283 -- value that we know this expression definitely has.
4285 if Compile_Time_Known_Value
(N
) then
4286 Lo
:= Expr_Value
(N
);
4291 -- Return if already in the cache
4293 Cindex
:= Cache_Index
(N
mod Cache_Size
);
4295 if Determine_Range_Cache_N
(Cindex
) = N
4297 Determine_Range_Cache_V
(Cindex
) = Assume_Valid
4299 Lo
:= Determine_Range_Cache_Lo
(Cindex
);
4300 Hi
:= Determine_Range_Cache_Hi
(Cindex
);
4304 -- Otherwise, start by finding the bounds of the type of the expression,
4305 -- the value cannot be outside this range (if it is, then we have an
4306 -- overflow situation, which is a separate check, we are talking here
4307 -- only about the expression value).
4309 -- First a check, never try to find the bounds of a generic type, since
4310 -- these bounds are always junk values, and it is only valid to look at
4311 -- the bounds in an instance.
4313 if Is_Generic_Type
(Typ
) then
4318 -- First step, change to use base type unless we know the value is valid
4320 if (Is_Entity_Name
(N
) and then Is_Known_Valid
(Entity
(N
)))
4321 or else Assume_No_Invalid_Values
4322 or else Assume_Valid
4326 Typ
:= Underlying_Type
(Base_Type
(Typ
));
4329 -- Retrieve the base type. Handle the case where the base type is a
4330 -- private enumeration type.
4332 Btyp
:= Base_Type
(Typ
);
4334 if Is_Private_Type
(Btyp
) and then Present
(Full_View
(Btyp
)) then
4335 Btyp
:= Full_View
(Btyp
);
4338 -- We use the actual bound unless it is dynamic, in which case use the
4339 -- corresponding base type bound if possible. If we can't get a bound
4340 -- then we figure we can't determine the range (a peculiar case, that
4341 -- perhaps cannot happen, but there is no point in bombing in this
4342 -- optimization circuit.
4344 -- First the low bound
4346 Bound
:= Type_Low_Bound
(Typ
);
4348 if Compile_Time_Known_Value
(Bound
) then
4349 Lo
:= Expr_Value
(Bound
);
4351 elsif Compile_Time_Known_Value
(Type_Low_Bound
(Btyp
)) then
4352 Lo
:= Expr_Value
(Type_Low_Bound
(Btyp
));
4359 -- Now the high bound
4361 Bound
:= Type_High_Bound
(Typ
);
4363 -- We need the high bound of the base type later on, and this should
4364 -- always be compile time known. Again, it is not clear that this
4365 -- can ever be false, but no point in bombing.
4367 if Compile_Time_Known_Value
(Type_High_Bound
(Btyp
)) then
4368 Hbound
:= Expr_Value
(Type_High_Bound
(Btyp
));
4376 -- If we have a static subtype, then that may have a tighter bound so
4377 -- use the upper bound of the subtype instead in this case.
4379 if Compile_Time_Known_Value
(Bound
) then
4380 Hi
:= Expr_Value
(Bound
);
4383 -- We may be able to refine this value in certain situations. If any
4384 -- refinement is possible, then Lor and Hir are set to possibly tighter
4385 -- bounds, and OK1 is set to True.
4389 -- For unary plus, result is limited by range of operand
4393 (Right_Opnd
(N
), OK1
, Lor
, Hir
, Assume_Valid
);
4395 -- For unary minus, determine range of operand, and negate it
4399 (Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
, Assume_Valid
);
4406 -- For binary addition, get range of each operand and do the
4407 -- addition to get the result range.
4411 Lor
:= Lo_Left
+ Lo_Right
;
4412 Hir
:= Hi_Left
+ Hi_Right
;
4415 -- Division is tricky. The only case we consider is where the right
4416 -- operand is a positive constant, and in this case we simply divide
4417 -- the bounds of the left operand
4421 if Lo_Right
= Hi_Right
4422 and then Lo_Right
> 0
4424 Lor
:= Lo_Left
/ Lo_Right
;
4425 Hir
:= Hi_Left
/ Lo_Right
;
4431 -- For binary subtraction, get range of each operand and do the worst
4432 -- case subtraction to get the result range.
4434 when N_Op_Subtract
=>
4436 Lor
:= Lo_Left
- Hi_Right
;
4437 Hir
:= Hi_Left
- Lo_Right
;
4440 -- For MOD, if right operand is a positive constant, then result must
4441 -- be in the allowable range of mod results.
4445 if Lo_Right
= Hi_Right
4446 and then Lo_Right
/= 0
4448 if Lo_Right
> 0 then
4450 Hir
:= Lo_Right
- 1;
4452 else -- Lo_Right < 0
4453 Lor
:= Lo_Right
+ 1;
4462 -- For REM, if right operand is a positive constant, then result must
4463 -- be in the allowable range of mod results.
4467 if Lo_Right
= Hi_Right
4468 and then Lo_Right
/= 0
4471 Dval
: constant Uint
:= (abs Lo_Right
) - 1;
4474 -- The sign of the result depends on the sign of the
4475 -- dividend (but not on the sign of the divisor, hence
4476 -- the abs operation above).
4496 -- Attribute reference cases
4498 when N_Attribute_Reference
=>
4499 case Attribute_Name
(N
) is
4501 -- For Pos/Val attributes, we can refine the range using the
4502 -- possible range of values of the attribute expression.
4504 when Name_Pos | Name_Val
=>
4506 (First
(Expressions
(N
)), OK1
, Lor
, Hir
, Assume_Valid
);
4508 -- For Length attribute, use the bounds of the corresponding
4509 -- index type to refine the range.
4513 Atyp
: Entity_Id
:= Etype
(Prefix
(N
));
4521 if Is_Access_Type
(Atyp
) then
4522 Atyp
:= Designated_Type
(Atyp
);
4525 -- For string literal, we know exact value
4527 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
4529 Lo
:= String_Literal_Length
(Atyp
);
4530 Hi
:= String_Literal_Length
(Atyp
);
4534 -- Otherwise check for expression given
4536 if No
(Expressions
(N
)) then
4540 UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
4543 Indx
:= First_Index
(Atyp
);
4544 for J
in 2 .. Inum
loop
4545 Indx
:= Next_Index
(Indx
);
4548 -- If the index type is a formal type or derived from
4549 -- one, the bounds are not static.
4551 if Is_Generic_Type
(Root_Type
(Etype
(Indx
))) then
4557 (Type_Low_Bound
(Etype
(Indx
)), OK1
, LL
, LU
,
4562 (Type_High_Bound
(Etype
(Indx
)), OK1
, UL
, UU
,
4567 -- The maximum value for Length is the biggest
4568 -- possible gap between the values of the bounds.
4569 -- But of course, this value cannot be negative.
4571 Hir
:= UI_Max
(Uint_0
, UU
- LL
+ 1);
4573 -- For constrained arrays, the minimum value for
4574 -- Length is taken from the actual value of the
4575 -- bounds, since the index will be exactly of this
4578 if Is_Constrained
(Atyp
) then
4579 Lor
:= UI_Max
(Uint_0
, UL
- LU
+ 1);
4581 -- For an unconstrained array, the minimum value
4582 -- for length is always zero.
4591 -- No special handling for other attributes
4592 -- Probably more opportunities exist here???
4599 -- For type conversion from one discrete type to another, we can
4600 -- refine the range using the converted value.
4602 when N_Type_Conversion
=>
4603 Determine_Range
(Expression
(N
), OK1
, Lor
, Hir
, Assume_Valid
);
4605 -- Nothing special to do for all other expression kinds
4613 -- At this stage, if OK1 is true, then we know that the actual result of
4614 -- the computed expression is in the range Lor .. Hir. We can use this
4615 -- to restrict the possible range of results.
4619 -- If the refined value of the low bound is greater than the type
4620 -- low bound, then reset it to the more restrictive value. However,
4621 -- we do NOT do this for the case of a modular type where the
4622 -- possible upper bound on the value is above the base type high
4623 -- bound, because that means the result could wrap.
4626 and then not (Is_Modular_Integer_Type
(Typ
) and then Hir
> Hbound
)
4631 -- Similarly, if the refined value of the high bound is less than the
4632 -- value so far, then reset it to the more restrictive value. Again,
4633 -- we do not do this if the refined low bound is negative for a
4634 -- modular type, since this would wrap.
4637 and then not (Is_Modular_Integer_Type
(Typ
) and then Lor
< Uint_0
)
4643 -- Set cache entry for future call and we are all done
4645 Determine_Range_Cache_N
(Cindex
) := N
;
4646 Determine_Range_Cache_V
(Cindex
) := Assume_Valid
;
4647 Determine_Range_Cache_Lo
(Cindex
) := Lo
;
4648 Determine_Range_Cache_Hi
(Cindex
) := Hi
;
4651 -- If any exception occurs, it means that we have some bug in the compiler,
4652 -- possibly triggered by a previous error, or by some unforeseen peculiar
4653 -- occurrence. However, this is only an optimization attempt, so there is
4654 -- really no point in crashing the compiler. Instead we just decide, too
4655 -- bad, we can't figure out a range in this case after all.
4660 -- Debug flag K disables this behavior (useful for debugging)
4662 if Debug_Flag_K
then
4670 end Determine_Range
;
4672 -----------------------
4673 -- Determine_Range_R --
4674 -----------------------
4676 procedure Determine_Range_R
4681 Assume_Valid
: Boolean := False)
4683 Typ
: Entity_Id
:= Etype
(N
);
4684 -- Type to use, may get reset to base type for possibly invalid entity
4688 -- Lo and Hi bounds of left operand
4692 -- Lo and Hi bounds of right (or only) operand
4695 -- Temp variable used to hold a bound node
4698 -- High bound of base type of expression
4702 -- Refined values for low and high bounds, after tightening
4705 -- Used in lower level calls to indicate if call succeeded
4707 Cindex
: Cache_Index
;
4708 -- Used to search cache
4713 function OK_Operands
return Boolean;
4714 -- Used for binary operators. Determines the ranges of the left and
4715 -- right operands, and if they are both OK, returns True, and puts
4716 -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left.
4718 function Round_Machine
(B
: Ureal
) return Ureal
;
4719 -- B is a real bound. Round it using mode Round_Even.
4725 function OK_Operands
return Boolean is
4728 (Left_Opnd
(N
), OK1
, Lo_Left
, Hi_Left
, Assume_Valid
);
4735 (Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
, Assume_Valid
);
4743 function Round_Machine
(B
: Ureal
) return Ureal
is
4745 return Machine
(Typ
, B
, Round_Even
, N
);
4748 -- Start of processing for Determine_Range_R
4751 -- Prevent junk warnings by initializing range variables
4758 -- For temporary constants internally generated to remove side effects
4759 -- we must use the corresponding expression to determine the range of
4760 -- the expression. But note that the expander can also generate
4761 -- constants in other cases, including deferred constants.
4763 if Is_Entity_Name
(N
)
4764 and then Nkind
(Parent
(Entity
(N
))) = N_Object_Declaration
4765 and then Ekind
(Entity
(N
)) = E_Constant
4766 and then Is_Internal_Name
(Chars
(Entity
(N
)))
4768 if Present
(Expression
(Parent
(Entity
(N
)))) then
4770 (Expression
(Parent
(Entity
(N
))), OK
, Lo
, Hi
, Assume_Valid
);
4772 elsif Present
(Full_View
(Entity
(N
))) then
4774 (Expression
(Parent
(Full_View
(Entity
(N
)))),
4775 OK
, Lo
, Hi
, Assume_Valid
);
4784 -- If type is not defined, we can't determine its range
4788 -- We don't deal with anything except IEEE floating-point types
4790 or else not Is_Floating_Point_Type
(Typ
)
4791 or else Float_Rep
(Typ
) /= IEEE_Binary
4793 -- Ignore type for which an error has been posted, since range in
4794 -- this case may well be a bogosity deriving from the error. Also
4795 -- ignore if error posted on the reference node.
4797 or else Error_Posted
(N
) or else Error_Posted
(Typ
)
4803 -- For all other cases, we can determine the range
4807 -- If value is compile time known, then the possible range is the one
4808 -- value that we know this expression definitely has.
4810 if Compile_Time_Known_Value
(N
) then
4811 Lo
:= Expr_Value_R
(N
);
4816 -- Return if already in the cache
4818 Cindex
:= Cache_Index
(N
mod Cache_Size
);
4820 if Determine_Range_Cache_N
(Cindex
) = N
4822 Determine_Range_Cache_V
(Cindex
) = Assume_Valid
4824 Lo
:= Determine_Range_Cache_Lo_R
(Cindex
);
4825 Hi
:= Determine_Range_Cache_Hi_R
(Cindex
);
4829 -- Otherwise, start by finding the bounds of the type of the expression,
4830 -- the value cannot be outside this range (if it is, then we have an
4831 -- overflow situation, which is a separate check, we are talking here
4832 -- only about the expression value).
4834 -- First a check, never try to find the bounds of a generic type, since
4835 -- these bounds are always junk values, and it is only valid to look at
4836 -- the bounds in an instance.
4838 if Is_Generic_Type
(Typ
) then
4843 -- First step, change to use base type unless we know the value is valid
4845 if (Is_Entity_Name
(N
) and then Is_Known_Valid
(Entity
(N
)))
4846 or else Assume_No_Invalid_Values
4847 or else Assume_Valid
4851 Typ
:= Underlying_Type
(Base_Type
(Typ
));
4854 -- Retrieve the base type. Handle the case where the base type is a
4857 Btyp
:= Base_Type
(Typ
);
4859 if Is_Private_Type
(Btyp
) and then Present
(Full_View
(Btyp
)) then
4860 Btyp
:= Full_View
(Btyp
);
4863 -- We use the actual bound unless it is dynamic, in which case use the
4864 -- corresponding base type bound if possible. If we can't get a bound
4865 -- then we figure we can't determine the range (a peculiar case, that
4866 -- perhaps cannot happen, but there is no point in bombing in this
4867 -- optimization circuit).
4869 -- First the low bound
4871 Bound
:= Type_Low_Bound
(Typ
);
4873 if Compile_Time_Known_Value
(Bound
) then
4874 Lo
:= Expr_Value_R
(Bound
);
4876 elsif Compile_Time_Known_Value
(Type_Low_Bound
(Btyp
)) then
4877 Lo
:= Expr_Value_R
(Type_Low_Bound
(Btyp
));
4884 -- Now the high bound
4886 Bound
:= Type_High_Bound
(Typ
);
4888 -- We need the high bound of the base type later on, and this should
4889 -- always be compile time known. Again, it is not clear that this
4890 -- can ever be false, but no point in bombing.
4892 if Compile_Time_Known_Value
(Type_High_Bound
(Btyp
)) then
4893 Hbound
:= Expr_Value_R
(Type_High_Bound
(Btyp
));
4901 -- If we have a static subtype, then that may have a tighter bound so
4902 -- use the upper bound of the subtype instead in this case.
4904 if Compile_Time_Known_Value
(Bound
) then
4905 Hi
:= Expr_Value_R
(Bound
);
4908 -- We may be able to refine this value in certain situations. If any
4909 -- refinement is possible, then Lor and Hir are set to possibly tighter
4910 -- bounds, and OK1 is set to True.
4914 -- For unary plus, result is limited by range of operand
4918 (Right_Opnd
(N
), OK1
, Lor
, Hir
, Assume_Valid
);
4920 -- For unary minus, determine range of operand, and negate it
4924 (Right_Opnd
(N
), OK1
, Lo_Right
, Hi_Right
, Assume_Valid
);
4931 -- For binary addition, get range of each operand and do the
4932 -- addition to get the result range.
4936 Lor
:= Round_Machine
(Lo_Left
+ Lo_Right
);
4937 Hir
:= Round_Machine
(Hi_Left
+ Hi_Right
);
4940 -- For binary subtraction, get range of each operand and do the worst
4941 -- case subtraction to get the result range.
4943 when N_Op_Subtract
=>
4945 Lor
:= Round_Machine
(Lo_Left
- Hi_Right
);
4946 Hir
:= Round_Machine
(Hi_Left
- Lo_Right
);
4949 -- For multiplication, get range of each operand and do the
4950 -- four multiplications to get the result range.
4952 when N_Op_Multiply
=>
4955 M1
: constant Ureal
:= Round_Machine
(Lo_Left
* Lo_Right
);
4956 M2
: constant Ureal
:= Round_Machine
(Lo_Left
* Hi_Right
);
4957 M3
: constant Ureal
:= Round_Machine
(Hi_Left
* Lo_Right
);
4958 M4
: constant Ureal
:= Round_Machine
(Hi_Left
* Hi_Right
);
4960 Lor
:= UR_Min
(UR_Min
(M1
, M2
), UR_Min
(M3
, M4
));
4961 Hir
:= UR_Max
(UR_Max
(M1
, M2
), UR_Max
(M3
, M4
));
4965 -- For division, consider separately the cases where the right
4966 -- operand is positive or negative. Otherwise, the right operand
4967 -- can be arbitrarily close to zero, so the result is likely to
4968 -- be unbounded in one direction, do not attempt to compute it.
4973 -- Right operand is positive
4975 if Lo_Right
> Ureal_0
then
4977 -- If the low bound of the left operand is negative, obtain
4978 -- the overall low bound by dividing it by the smallest
4979 -- value of the right operand, and otherwise by the largest
4980 -- value of the right operand.
4982 if Lo_Left
< Ureal_0
then
4983 Lor
:= Round_Machine
(Lo_Left
/ Lo_Right
);
4985 Lor
:= Round_Machine
(Lo_Left
/ Hi_Right
);
4988 -- If the high bound of the left operand is negative, obtain
4989 -- the overall high bound by dividing it by the largest
4990 -- value of the right operand, and otherwise by the
4991 -- smallest value of the right operand.
4993 if Hi_Left
< Ureal_0
then
4994 Hir
:= Round_Machine
(Hi_Left
/ Hi_Right
);
4996 Hir
:= Round_Machine
(Hi_Left
/ Lo_Right
);
4999 -- Right operand is negative
5001 elsif Hi_Right
< Ureal_0
then
5003 -- If the low bound of the left operand is negative, obtain
5004 -- the overall low bound by dividing it by the largest
5005 -- value of the right operand, and otherwise by the smallest
5006 -- value of the right operand.
5008 if Lo_Left
< Ureal_0
then
5009 Lor
:= Round_Machine
(Lo_Left
/ Hi_Right
);
5011 Lor
:= Round_Machine
(Lo_Left
/ Lo_Right
);
5014 -- If the high bound of the left operand is negative, obtain
5015 -- the overall high bound by dividing it by the smallest
5016 -- value of the right operand, and otherwise by the
5017 -- largest value of the right operand.
5019 if Hi_Left
< Ureal_0
then
5020 Hir
:= Round_Machine
(Hi_Left
/ Lo_Right
);
5022 Hir
:= Round_Machine
(Hi_Left
/ Hi_Right
);
5030 -- For type conversion from one floating-point type to another, we
5031 -- can refine the range using the converted value.
5033 when N_Type_Conversion
=>
5034 Determine_Range_R
(Expression
(N
), OK1
, Lor
, Hir
, Assume_Valid
);
5036 -- Nothing special to do for all other expression kinds
5044 -- At this stage, if OK1 is true, then we know that the actual result of
5045 -- the computed expression is in the range Lor .. Hir. We can use this
5046 -- to restrict the possible range of results.
5050 -- If the refined value of the low bound is greater than the type
5051 -- low bound, then reset it to the more restrictive value.
5057 -- Similarly, if the refined value of the high bound is less than the
5058 -- value so far, then reset it to the more restrictive value.
5065 -- Set cache entry for future call and we are all done
5067 Determine_Range_Cache_N
(Cindex
) := N
;
5068 Determine_Range_Cache_V
(Cindex
) := Assume_Valid
;
5069 Determine_Range_Cache_Lo_R
(Cindex
) := Lo
;
5070 Determine_Range_Cache_Hi_R
(Cindex
) := Hi
;
5073 -- If any exception occurs, it means that we have some bug in the compiler,
5074 -- possibly triggered by a previous error, or by some unforeseen peculiar
5075 -- occurrence. However, this is only an optimization attempt, so there is
5076 -- really no point in crashing the compiler. Instead we just decide, too
5077 -- bad, we can't figure out a range in this case after all.
5082 -- Debug flag K disables this behavior (useful for debugging)
5084 if Debug_Flag_K
then
5092 end Determine_Range_R
;
5094 ------------------------------------
5095 -- Discriminant_Checks_Suppressed --
5096 ------------------------------------
5098 function Discriminant_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
5101 if Is_Unchecked_Union
(E
) then
5103 elsif Checks_May_Be_Suppressed
(E
) then
5104 return Is_Check_Suppressed
(E
, Discriminant_Check
);
5108 return Scope_Suppress
.Suppress
(Discriminant_Check
);
5109 end Discriminant_Checks_Suppressed
;
5111 --------------------------------
5112 -- Division_Checks_Suppressed --
5113 --------------------------------
5115 function Division_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
5117 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
5118 return Is_Check_Suppressed
(E
, Division_Check
);
5120 return Scope_Suppress
.Suppress
(Division_Check
);
5122 end Division_Checks_Suppressed
;
5124 --------------------------------------
5125 -- Duplicated_Tag_Checks_Suppressed --
5126 --------------------------------------
5128 function Duplicated_Tag_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
5130 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
5131 return Is_Check_Suppressed
(E
, Duplicated_Tag_Check
);
5133 return Scope_Suppress
.Suppress
(Duplicated_Tag_Check
);
5135 end Duplicated_Tag_Checks_Suppressed
;
5137 -----------------------------------
5138 -- Elaboration_Checks_Suppressed --
5139 -----------------------------------
5141 function Elaboration_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
5143 -- The complication in this routine is that if we are in the dynamic
5144 -- model of elaboration, we also check All_Checks, since All_Checks
5145 -- does not set Elaboration_Check explicitly.
5148 if Kill_Elaboration_Checks
(E
) then
5151 elsif Checks_May_Be_Suppressed
(E
) then
5152 if Is_Check_Suppressed
(E
, Elaboration_Check
) then
5154 elsif Dynamic_Elaboration_Checks
then
5155 return Is_Check_Suppressed
(E
, All_Checks
);
5162 if Scope_Suppress
.Suppress
(Elaboration_Check
) then
5164 elsif Dynamic_Elaboration_Checks
then
5165 return Scope_Suppress
.Suppress
(All_Checks
);
5169 end Elaboration_Checks_Suppressed
;
5171 ---------------------------
5172 -- Enable_Overflow_Check --
5173 ---------------------------
5175 procedure Enable_Overflow_Check
(N
: Node_Id
) is
5176 Typ
: constant Entity_Id
:= Base_Type
(Etype
(N
));
5177 Mode
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
5185 Do_Ovflow_Check
: Boolean;
5188 if Debug_Flag_CC
then
5189 w
("Enable_Overflow_Check for node ", Int
(N
));
5190 Write_Str
(" Source location = ");
5195 -- No check if overflow checks suppressed for type of node
5197 if Overflow_Checks_Suppressed
(Etype
(N
)) then
5200 -- Nothing to do for unsigned integer types, which do not overflow
5202 elsif Is_Modular_Integer_Type
(Typ
) then
5206 -- This is the point at which processing for STRICT mode diverges
5207 -- from processing for MINIMIZED/ELIMINATED modes. This divergence is
5208 -- probably more extreme that it needs to be, but what is going on here
5209 -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted
5210 -- to leave the processing for STRICT mode untouched. There were
5211 -- two reasons for this. First it avoided any incompatible change of
5212 -- behavior. Second, it guaranteed that STRICT mode continued to be
5215 -- The big difference is that in STRICT mode there is a fair amount of
5216 -- circuitry to try to avoid setting the Do_Overflow_Check flag if we
5217 -- know that no check is needed. We skip all that in the two new modes,
5218 -- since really overflow checking happens over a whole subtree, and we
5219 -- do the corresponding optimizations later on when applying the checks.
5221 if Mode
in Minimized_Or_Eliminated
then
5222 if not (Overflow_Checks_Suppressed
(Etype
(N
)))
5223 and then not (Is_Entity_Name
(N
)
5224 and then Overflow_Checks_Suppressed
(Entity
(N
)))
5226 Activate_Overflow_Check
(N
);
5229 if Debug_Flag_CC
then
5230 w
("Minimized/Eliminated mode");
5236 -- Remainder of processing is for STRICT case, and is unchanged from
5237 -- earlier versions preceding the addition of MINIMIZED/ELIMINATED.
5239 -- Nothing to do if the range of the result is known OK. We skip this
5240 -- for conversions, since the caller already did the check, and in any
5241 -- case the condition for deleting the check for a type conversion is
5244 if Nkind
(N
) /= N_Type_Conversion
then
5245 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> True);
5247 -- Note in the test below that we assume that the range is not OK
5248 -- if a bound of the range is equal to that of the type. That's not
5249 -- quite accurate but we do this for the following reasons:
5251 -- a) The way that Determine_Range works, it will typically report
5252 -- the bounds of the value as being equal to the bounds of the
5253 -- type, because it either can't tell anything more precise, or
5254 -- does not think it is worth the effort to be more precise.
5256 -- b) It is very unusual to have a situation in which this would
5257 -- generate an unnecessary overflow check (an example would be
5258 -- a subtype with a range 0 .. Integer'Last - 1 to which the
5259 -- literal value one is added).
5261 -- c) The alternative is a lot of special casing in this routine
5262 -- which would partially duplicate Determine_Range processing.
5265 Do_Ovflow_Check
:= True;
5267 -- Note that the following checks are quite deliberately > and <
5268 -- rather than >= and <= as explained above.
5270 if Lo
> Expr_Value
(Type_Low_Bound
(Typ
))
5272 Hi
< Expr_Value
(Type_High_Bound
(Typ
))
5274 Do_Ovflow_Check
:= False;
5276 -- Despite the comments above, it is worth dealing specially with
5277 -- division specially. The only case where integer division can
5278 -- overflow is (largest negative number) / (-1). So we will do
5279 -- an extra range analysis to see if this is possible.
5281 elsif Nkind
(N
) = N_Op_Divide
then
5283 (Left_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
5285 if OK
and then Lo
> Expr_Value
(Type_Low_Bound
(Typ
)) then
5286 Do_Ovflow_Check
:= False;
5290 (Right_Opnd
(N
), OK
, Lo
, Hi
, Assume_Valid
=> True);
5292 if OK
and then (Lo
> Uint_Minus_1
5296 Do_Ovflow_Check
:= False;
5301 -- If no overflow check required, we are done
5303 if not Do_Ovflow_Check
then
5304 if Debug_Flag_CC
then
5305 w
("No overflow check required");
5313 -- If not in optimizing mode, set flag and we are done. We are also done
5314 -- (and just set the flag) if the type is not a discrete type, since it
5315 -- is not worth the effort to eliminate checks for other than discrete
5316 -- types. In addition, we take this same path if we have stored the
5317 -- maximum number of checks possible already (a very unlikely situation,
5318 -- but we do not want to blow up).
5320 if Optimization_Level
= 0
5321 or else not Is_Discrete_Type
(Etype
(N
))
5322 or else Num_Saved_Checks
= Saved_Checks
'Last
5324 Activate_Overflow_Check
(N
);
5326 if Debug_Flag_CC
then
5327 w
("Optimization off");
5333 -- Otherwise evaluate and check the expression
5338 Target_Type
=> Empty
,
5344 if Debug_Flag_CC
then
5345 w
("Called Find_Check");
5349 w
(" Check_Num = ", Chk
);
5350 w
(" Ent = ", Int
(Ent
));
5351 Write_Str
(" Ofs = ");
5356 -- If check is not of form to optimize, then set flag and we are done
5359 Activate_Overflow_Check
(N
);
5363 -- If check is already performed, then return without setting flag
5366 if Debug_Flag_CC
then
5367 w
("Check suppressed!");
5373 -- Here we will make a new entry for the new check
5375 Activate_Overflow_Check
(N
);
5376 Num_Saved_Checks
:= Num_Saved_Checks
+ 1;
5377 Saved_Checks
(Num_Saved_Checks
) :=
5382 Target_Type
=> Empty
);
5384 if Debug_Flag_CC
then
5385 w
("Make new entry, check number = ", Num_Saved_Checks
);
5386 w
(" Entity = ", Int
(Ent
));
5387 Write_Str
(" Offset = ");
5389 w
(" Check_Type = O");
5390 w
(" Target_Type = Empty");
5393 -- If we get an exception, then something went wrong, probably because of
5394 -- an error in the structure of the tree due to an incorrect program. Or
5395 -- it may be a bug in the optimization circuit. In either case the safest
5396 -- thing is simply to set the check flag unconditionally.
5400 Activate_Overflow_Check
(N
);
5402 if Debug_Flag_CC
then
5403 w
(" exception occurred, overflow flag set");
5407 end Enable_Overflow_Check
;
5409 ------------------------
5410 -- Enable_Range_Check --
5411 ------------------------
5413 procedure Enable_Range_Check
(N
: Node_Id
) is
5422 -- Return if unchecked type conversion with range check killed. In this
5423 -- case we never set the flag (that's what Kill_Range_Check is about).
5425 if Nkind
(N
) = N_Unchecked_Type_Conversion
5426 and then Kill_Range_Check
(N
)
5431 -- Do not set range check flag if parent is assignment statement or
5432 -- object declaration with Suppress_Assignment_Checks flag set
5434 if Nkind_In
(Parent
(N
), N_Assignment_Statement
, N_Object_Declaration
)
5435 and then Suppress_Assignment_Checks
(Parent
(N
))
5440 -- Check for various cases where we should suppress the range check
5442 -- No check if range checks suppressed for type of node
5444 if Present
(Etype
(N
)) and then Range_Checks_Suppressed
(Etype
(N
)) then
5447 -- No check if node is an entity name, and range checks are suppressed
5448 -- for this entity, or for the type of this entity.
5450 elsif Is_Entity_Name
(N
)
5451 and then (Range_Checks_Suppressed
(Entity
(N
))
5452 or else Range_Checks_Suppressed
(Etype
(Entity
(N
))))
5456 -- No checks if index of array, and index checks are suppressed for
5457 -- the array object or the type of the array.
5459 elsif Nkind
(Parent
(N
)) = N_Indexed_Component
then
5461 Pref
: constant Node_Id
:= Prefix
(Parent
(N
));
5463 if Is_Entity_Name
(Pref
)
5464 and then Index_Checks_Suppressed
(Entity
(Pref
))
5467 elsif Index_Checks_Suppressed
(Etype
(Pref
)) then
5473 -- Debug trace output
5475 if Debug_Flag_CC
then
5476 w
("Enable_Range_Check for node ", Int
(N
));
5477 Write_Str
(" Source location = ");
5482 -- If not in optimizing mode, set flag and we are done. We are also done
5483 -- (and just set the flag) if the type is not a discrete type, since it
5484 -- is not worth the effort to eliminate checks for other than discrete
5485 -- types. In addition, we take this same path if we have stored the
5486 -- maximum number of checks possible already (a very unlikely situation,
5487 -- but we do not want to blow up).
5489 if Optimization_Level
= 0
5490 or else No
(Etype
(N
))
5491 or else not Is_Discrete_Type
(Etype
(N
))
5492 or else Num_Saved_Checks
= Saved_Checks
'Last
5494 Activate_Range_Check
(N
);
5496 if Debug_Flag_CC
then
5497 w
("Optimization off");
5503 -- Otherwise find out the target type
5507 -- For assignment, use left side subtype
5509 if Nkind
(P
) = N_Assignment_Statement
5510 and then Expression
(P
) = N
5512 Ttyp
:= Etype
(Name
(P
));
5514 -- For indexed component, use subscript subtype
5516 elsif Nkind
(P
) = N_Indexed_Component
then
5523 Atyp
:= Etype
(Prefix
(P
));
5525 if Is_Access_Type
(Atyp
) then
5526 Atyp
:= Designated_Type
(Atyp
);
5528 -- If the prefix is an access to an unconstrained array,
5529 -- perform check unconditionally: it depends on the bounds of
5530 -- an object and we cannot currently recognize whether the test
5531 -- may be redundant.
5533 if not Is_Constrained
(Atyp
) then
5534 Activate_Range_Check
(N
);
5538 -- Ditto if prefix is simply an unconstrained array. We used
5539 -- to think this case was OK, if the prefix was not an explicit
5540 -- dereference, but we have now seen a case where this is not
5541 -- true, so it is safer to just suppress the optimization in this
5542 -- case. The back end is getting better at eliminating redundant
5543 -- checks in any case, so the loss won't be important.
5545 elsif Is_Array_Type
(Atyp
)
5546 and then not Is_Constrained
(Atyp
)
5548 Activate_Range_Check
(N
);
5552 Indx
:= First_Index
(Atyp
);
5553 Subs
:= First
(Expressions
(P
));
5556 Ttyp
:= Etype
(Indx
);
5565 -- For now, ignore all other cases, they are not so interesting
5568 if Debug_Flag_CC
then
5569 w
(" target type not found, flag set");
5572 Activate_Range_Check
(N
);
5576 -- Evaluate and check the expression
5581 Target_Type
=> Ttyp
,
5587 if Debug_Flag_CC
then
5588 w
("Called Find_Check");
5589 w
("Target_Typ = ", Int
(Ttyp
));
5593 w
(" Check_Num = ", Chk
);
5594 w
(" Ent = ", Int
(Ent
));
5595 Write_Str
(" Ofs = ");
5600 -- If check is not of form to optimize, then set flag and we are done
5603 if Debug_Flag_CC
then
5604 w
(" expression not of optimizable type, flag set");
5607 Activate_Range_Check
(N
);
5611 -- If check is already performed, then return without setting flag
5614 if Debug_Flag_CC
then
5615 w
("Check suppressed!");
5621 -- Here we will make a new entry for the new check
5623 Activate_Range_Check
(N
);
5624 Num_Saved_Checks
:= Num_Saved_Checks
+ 1;
5625 Saved_Checks
(Num_Saved_Checks
) :=
5630 Target_Type
=> Ttyp
);
5632 if Debug_Flag_CC
then
5633 w
("Make new entry, check number = ", Num_Saved_Checks
);
5634 w
(" Entity = ", Int
(Ent
));
5635 Write_Str
(" Offset = ");
5637 w
(" Check_Type = R");
5638 w
(" Target_Type = ", Int
(Ttyp
));
5639 pg
(Union_Id
(Ttyp
));
5642 -- If we get an exception, then something went wrong, probably because of
5643 -- an error in the structure of the tree due to an incorrect program. Or
5644 -- it may be a bug in the optimization circuit. In either case the safest
5645 -- thing is simply to set the check flag unconditionally.
5649 Activate_Range_Check
(N
);
5651 if Debug_Flag_CC
then
5652 w
(" exception occurred, range flag set");
5656 end Enable_Range_Check
;
5662 procedure Ensure_Valid
5664 Holes_OK
: Boolean := False;
5665 Related_Id
: Entity_Id
:= Empty
;
5666 Is_Low_Bound
: Boolean := False;
5667 Is_High_Bound
: Boolean := False)
5669 Typ
: constant Entity_Id
:= Etype
(Expr
);
5672 -- Ignore call if we are not doing any validity checking
5674 if not Validity_Checks_On
then
5677 -- Ignore call if range or validity checks suppressed on entity or type
5679 elsif Range_Or_Validity_Checks_Suppressed
(Expr
) then
5682 -- No check required if expression is from the expander, we assume the
5683 -- expander will generate whatever checks are needed. Note that this is
5684 -- not just an optimization, it avoids infinite recursions.
5686 -- Unchecked conversions must be checked, unless they are initialized
5687 -- scalar values, as in a component assignment in an init proc.
5689 -- In addition, we force a check if Force_Validity_Checks is set
5691 elsif not Comes_From_Source
(Expr
)
5692 and then not Force_Validity_Checks
5693 and then (Nkind
(Expr
) /= N_Unchecked_Type_Conversion
5694 or else Kill_Range_Check
(Expr
))
5698 -- No check required if expression is known to have valid value
5700 elsif Expr_Known_Valid
(Expr
) then
5703 -- Ignore case of enumeration with holes where the flag is set not to
5704 -- worry about holes, since no special validity check is needed
5706 elsif Is_Enumeration_Type
(Typ
)
5707 and then Has_Non_Standard_Rep
(Typ
)
5712 -- No check required on the left-hand side of an assignment
5714 elsif Nkind
(Parent
(Expr
)) = N_Assignment_Statement
5715 and then Expr
= Name
(Parent
(Expr
))
5719 -- No check on a universal real constant. The context will eventually
5720 -- convert it to a machine number for some target type, or report an
5723 elsif Nkind
(Expr
) = N_Real_Literal
5724 and then Etype
(Expr
) = Universal_Real
5728 -- If the expression denotes a component of a packed boolean array,
5729 -- no possible check applies. We ignore the old ACATS chestnuts that
5730 -- involve Boolean range True..True.
5732 -- Note: validity checks are generated for expressions that yield a
5733 -- scalar type, when it is possible to create a value that is outside of
5734 -- the type. If this is a one-bit boolean no such value exists. This is
5735 -- an optimization, and it also prevents compiler blowing up during the
5736 -- elaboration of improperly expanded packed array references.
5738 elsif Nkind
(Expr
) = N_Indexed_Component
5739 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Expr
)))
5740 and then Root_Type
(Etype
(Expr
)) = Standard_Boolean
5744 -- For an expression with actions, we want to insert the validity check
5745 -- on the final Expression.
5747 elsif Nkind
(Expr
) = N_Expression_With_Actions
then
5748 Ensure_Valid
(Expression
(Expr
));
5751 -- An annoying special case. If this is an out parameter of a scalar
5752 -- type, then the value is not going to be accessed, therefore it is
5753 -- inappropriate to do any validity check at the call site.
5756 -- Only need to worry about scalar types
5758 if Is_Scalar_Type
(Typ
) then
5768 -- Find actual argument (which may be a parameter association)
5769 -- and the parent of the actual argument (the call statement)
5774 if Nkind
(P
) = N_Parameter_Association
then
5779 -- Only need to worry if we are argument of a procedure call
5780 -- since functions don't have out parameters. If this is an
5781 -- indirect or dispatching call, get signature from the
5784 if Nkind
(P
) = N_Procedure_Call_Statement
then
5785 L
:= Parameter_Associations
(P
);
5787 if Is_Entity_Name
(Name
(P
)) then
5788 E
:= Entity
(Name
(P
));
5790 pragma Assert
(Nkind
(Name
(P
)) = N_Explicit_Dereference
);
5791 E
:= Etype
(Name
(P
));
5794 -- Only need to worry if there are indeed actuals, and if
5795 -- this could be a procedure call, otherwise we cannot get a
5796 -- match (either we are not an argument, or the mode of the
5797 -- formal is not OUT). This test also filters out the
5800 if Is_Non_Empty_List
(L
) and then Is_Subprogram
(E
) then
5802 -- This is the loop through parameters, looking for an
5803 -- OUT parameter for which we are the argument.
5805 F
:= First_Formal
(E
);
5807 while Present
(F
) loop
5808 if Ekind
(F
) = E_Out_Parameter
and then A
= N
then
5821 -- If this is a boolean expression, only its elementary operands need
5822 -- checking: if they are valid, a boolean or short-circuit operation
5823 -- with them will be valid as well.
5825 if Base_Type
(Typ
) = Standard_Boolean
5827 (Nkind
(Expr
) in N_Op
or else Nkind
(Expr
) in N_Short_Circuit
)
5832 -- If we fall through, a validity check is required
5834 Insert_Valid_Check
(Expr
, Related_Id
, Is_Low_Bound
, Is_High_Bound
);
5836 if Is_Entity_Name
(Expr
)
5837 and then Safe_To_Capture_Value
(Expr
, Entity
(Expr
))
5839 Set_Is_Known_Valid
(Entity
(Expr
));
5843 ----------------------
5844 -- Expr_Known_Valid --
5845 ----------------------
5847 function Expr_Known_Valid
(Expr
: Node_Id
) return Boolean is
5848 Typ
: constant Entity_Id
:= Etype
(Expr
);
5851 -- Non-scalar types are always considered valid, since they never give
5852 -- rise to the issues of erroneous or bounded error behavior that are
5853 -- the concern. In formal reference manual terms the notion of validity
5854 -- only applies to scalar types. Note that even when packed arrays are
5855 -- represented using modular types, they are still arrays semantically,
5856 -- so they are also always valid (in particular, the unused bits can be
5857 -- random rubbish without affecting the validity of the array value).
5859 if not Is_Scalar_Type
(Typ
) or else Is_Packed_Array_Impl_Type
(Typ
) then
5862 -- If no validity checking, then everything is considered valid
5864 elsif not Validity_Checks_On
then
5867 -- Floating-point types are considered valid unless floating-point
5868 -- validity checks have been specifically turned on.
5870 elsif Is_Floating_Point_Type
(Typ
)
5871 and then not Validity_Check_Floating_Point
5875 -- If the expression is the value of an object that is known to be
5876 -- valid, then clearly the expression value itself is valid.
5878 elsif Is_Entity_Name
(Expr
)
5879 and then Is_Known_Valid
(Entity
(Expr
))
5881 -- Exclude volatile variables
5883 and then not Treat_As_Volatile
(Entity
(Expr
))
5887 -- References to discriminants are always considered valid. The value
5888 -- of a discriminant gets checked when the object is built. Within the
5889 -- record, we consider it valid, and it is important to do so, since
5890 -- otherwise we can try to generate bogus validity checks which
5891 -- reference discriminants out of scope. Discriminants of concurrent
5892 -- types are excluded for the same reason.
5894 elsif Is_Entity_Name
(Expr
)
5895 and then Denotes_Discriminant
(Expr
, Check_Concurrent
=> True)
5899 -- If the type is one for which all values are known valid, then we are
5900 -- sure that the value is valid except in the slightly odd case where
5901 -- the expression is a reference to a variable whose size has been
5902 -- explicitly set to a value greater than the object size.
5904 elsif Is_Known_Valid
(Typ
) then
5905 if Is_Entity_Name
(Expr
)
5906 and then Ekind
(Entity
(Expr
)) = E_Variable
5907 and then Esize
(Entity
(Expr
)) > Esize
(Typ
)
5914 -- Integer and character literals always have valid values, where
5915 -- appropriate these will be range checked in any case.
5917 elsif Nkind_In
(Expr
, N_Integer_Literal
, N_Character_Literal
) then
5920 -- If we have a type conversion or a qualification of a known valid
5921 -- value, then the result will always be valid.
5923 elsif Nkind_In
(Expr
, N_Type_Conversion
, N_Qualified_Expression
) then
5924 return Expr_Known_Valid
(Expression
(Expr
));
5926 -- Case of expression is a non-floating-point operator. In this case we
5927 -- can assume the result is valid the generated code for the operator
5928 -- will include whatever checks are needed (e.g. range checks) to ensure
5929 -- validity. This assumption does not hold for the floating-point case,
5930 -- since floating-point operators can generate Infinite or NaN results
5931 -- which are considered invalid.
5933 -- Historical note: in older versions, the exemption of floating-point
5934 -- types from this assumption was done only in cases where the parent
5935 -- was an assignment, function call or parameter association. Presumably
5936 -- the idea was that in other contexts, the result would be checked
5937 -- elsewhere, but this list of cases was missing tests (at least the
5938 -- N_Object_Declaration case, as shown by a reported missing validity
5939 -- check), and it is not clear why function calls but not procedure
5940 -- calls were tested for. It really seems more accurate and much
5941 -- safer to recognize that expressions which are the result of a
5942 -- floating-point operator can never be assumed to be valid.
5944 elsif Nkind
(Expr
) in N_Op
and then not Is_Floating_Point_Type
(Typ
) then
5947 -- The result of a membership test is always valid, since it is true or
5948 -- false, there are no other possibilities.
5950 elsif Nkind
(Expr
) in N_Membership_Test
then
5953 -- For all other cases, we do not know the expression is valid
5958 end Expr_Known_Valid
;
5964 procedure Find_Check
5966 Check_Type
: Character;
5967 Target_Type
: Entity_Id
;
5968 Entry_OK
: out Boolean;
5969 Check_Num
: out Nat
;
5970 Ent
: out Entity_Id
;
5973 function Within_Range_Of
5974 (Target_Type
: Entity_Id
;
5975 Check_Type
: Entity_Id
) return Boolean;
5976 -- Given a requirement for checking a range against Target_Type, and
5977 -- and a range Check_Type against which a check has already been made,
5978 -- determines if the check against check type is sufficient to ensure
5979 -- that no check against Target_Type is required.
5981 ---------------------
5982 -- Within_Range_Of --
5983 ---------------------
5985 function Within_Range_Of
5986 (Target_Type
: Entity_Id
;
5987 Check_Type
: Entity_Id
) return Boolean
5990 if Target_Type
= Check_Type
then
5995 Tlo
: constant Node_Id
:= Type_Low_Bound
(Target_Type
);
5996 Thi
: constant Node_Id
:= Type_High_Bound
(Target_Type
);
5997 Clo
: constant Node_Id
:= Type_Low_Bound
(Check_Type
);
5998 Chi
: constant Node_Id
:= Type_High_Bound
(Check_Type
);
6002 or else (Compile_Time_Known_Value
(Tlo
)
6004 Compile_Time_Known_Value
(Clo
)
6006 Expr_Value
(Clo
) >= Expr_Value
(Tlo
)))
6009 or else (Compile_Time_Known_Value
(Thi
)
6011 Compile_Time_Known_Value
(Chi
)
6013 Expr_Value
(Chi
) <= Expr_Value
(Clo
)))
6021 end Within_Range_Of
;
6023 -- Start of processing for Find_Check
6026 -- Establish default, in case no entry is found
6030 -- Case of expression is simple entity reference
6032 if Is_Entity_Name
(Expr
) then
6033 Ent
:= Entity
(Expr
);
6036 -- Case of expression is entity + known constant
6038 elsif Nkind
(Expr
) = N_Op_Add
6039 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
6040 and then Is_Entity_Name
(Left_Opnd
(Expr
))
6042 Ent
:= Entity
(Left_Opnd
(Expr
));
6043 Ofs
:= Expr_Value
(Right_Opnd
(Expr
));
6045 -- Case of expression is entity - known constant
6047 elsif Nkind
(Expr
) = N_Op_Subtract
6048 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
6049 and then Is_Entity_Name
(Left_Opnd
(Expr
))
6051 Ent
:= Entity
(Left_Opnd
(Expr
));
6052 Ofs
:= UI_Negate
(Expr_Value
(Right_Opnd
(Expr
)));
6054 -- Any other expression is not of the right form
6063 -- Come here with expression of appropriate form, check if entity is an
6064 -- appropriate one for our purposes.
6066 if (Ekind
(Ent
) = E_Variable
6067 or else Is_Constant_Object
(Ent
))
6068 and then not Is_Library_Level_Entity
(Ent
)
6076 -- See if there is matching check already
6078 for J
in reverse 1 .. Num_Saved_Checks
loop
6080 SC
: Saved_Check
renames Saved_Checks
(J
);
6082 if SC
.Killed
= False
6083 and then SC
.Entity
= Ent
6084 and then SC
.Offset
= Ofs
6085 and then SC
.Check_Type
= Check_Type
6086 and then Within_Range_Of
(Target_Type
, SC
.Target_Type
)
6094 -- If we fall through entry was not found
6099 ---------------------------------
6100 -- Generate_Discriminant_Check --
6101 ---------------------------------
6103 -- Note: the code for this procedure is derived from the
6104 -- Emit_Discriminant_Check Routine in trans.c.
6106 procedure Generate_Discriminant_Check
(N
: Node_Id
) is
6107 Loc
: constant Source_Ptr
:= Sloc
(N
);
6108 Pref
: constant Node_Id
:= Prefix
(N
);
6109 Sel
: constant Node_Id
:= Selector_Name
(N
);
6111 Orig_Comp
: constant Entity_Id
:=
6112 Original_Record_Component
(Entity
(Sel
));
6113 -- The original component to be checked
6115 Discr_Fct
: constant Entity_Id
:=
6116 Discriminant_Checking_Func
(Orig_Comp
);
6117 -- The discriminant checking function
6120 -- One discriminant to be checked in the type
6122 Real_Discr
: Entity_Id
;
6123 -- Actual discriminant in the call
6125 Pref_Type
: Entity_Id
;
6126 -- Type of relevant prefix (ignoring private/access stuff)
6129 -- List of arguments for function call
6132 -- Keep track of the formal corresponding to the actual we build for
6133 -- each discriminant, in order to be able to perform the necessary type
6137 -- Selected component reference for checking function argument
6140 Pref_Type
:= Etype
(Pref
);
6142 -- Force evaluation of the prefix, so that it does not get evaluated
6143 -- twice (once for the check, once for the actual reference). Such a
6144 -- double evaluation is always a potential source of inefficiency, and
6145 -- is functionally incorrect in the volatile case, or when the prefix
6146 -- may have side effects. A nonvolatile entity or a component of a
6147 -- nonvolatile entity requires no evaluation.
6149 if Is_Entity_Name
(Pref
) then
6150 if Treat_As_Volatile
(Entity
(Pref
)) then
6151 Force_Evaluation
(Pref
, Name_Req
=> True);
6154 elsif Treat_As_Volatile
(Etype
(Pref
)) then
6155 Force_Evaluation
(Pref
, Name_Req
=> True);
6157 elsif Nkind
(Pref
) = N_Selected_Component
6158 and then Is_Entity_Name
(Prefix
(Pref
))
6163 Force_Evaluation
(Pref
, Name_Req
=> True);
6166 -- For a tagged type, use the scope of the original component to
6167 -- obtain the type, because ???
6169 if Is_Tagged_Type
(Scope
(Orig_Comp
)) then
6170 Pref_Type
:= Scope
(Orig_Comp
);
6172 -- For an untagged derived type, use the discriminants of the parent
6173 -- which have been renamed in the derivation, possibly by a one-to-many
6174 -- discriminant constraint. For untagged type, initially get the Etype
6178 if Is_Derived_Type
(Pref_Type
)
6179 and then Number_Discriminants
(Pref_Type
) /=
6180 Number_Discriminants
(Etype
(Base_Type
(Pref_Type
)))
6182 Pref_Type
:= Etype
(Base_Type
(Pref_Type
));
6186 -- We definitely should have a checking function, This routine should
6187 -- not be called if no discriminant checking function is present.
6189 pragma Assert
(Present
(Discr_Fct
));
6191 -- Create the list of the actual parameters for the call. This list
6192 -- is the list of the discriminant fields of the record expression to
6193 -- be discriminant checked.
6196 Formal
:= First_Formal
(Discr_Fct
);
6197 Discr
:= First_Discriminant
(Pref_Type
);
6198 while Present
(Discr
) loop
6200 -- If we have a corresponding discriminant field, and a parent
6201 -- subtype is present, then we want to use the corresponding
6202 -- discriminant since this is the one with the useful value.
6204 if Present
(Corresponding_Discriminant
(Discr
))
6205 and then Ekind
(Pref_Type
) = E_Record_Type
6206 and then Present
(Parent_Subtype
(Pref_Type
))
6208 Real_Discr
:= Corresponding_Discriminant
(Discr
);
6210 Real_Discr
:= Discr
;
6213 -- Construct the reference to the discriminant
6216 Make_Selected_Component
(Loc
,
6218 Unchecked_Convert_To
(Pref_Type
,
6219 Duplicate_Subexpr
(Pref
)),
6220 Selector_Name
=> New_Occurrence_Of
(Real_Discr
, Loc
));
6222 -- Manually analyze and resolve this selected component. We really
6223 -- want it just as it appears above, and do not want the expander
6224 -- playing discriminal games etc with this reference. Then we append
6225 -- the argument to the list we are gathering.
6227 Set_Etype
(Scomp
, Etype
(Real_Discr
));
6228 Set_Analyzed
(Scomp
, True);
6229 Append_To
(Args
, Convert_To
(Etype
(Formal
), Scomp
));
6231 Next_Formal_With_Extras
(Formal
);
6232 Next_Discriminant
(Discr
);
6235 -- Now build and insert the call
6238 Make_Raise_Constraint_Error
(Loc
,
6240 Make_Function_Call
(Loc
,
6241 Name
=> New_Occurrence_Of
(Discr_Fct
, Loc
),
6242 Parameter_Associations
=> Args
),
6243 Reason
=> CE_Discriminant_Check_Failed
));
6244 end Generate_Discriminant_Check
;
6246 ---------------------------
6247 -- Generate_Index_Checks --
6248 ---------------------------
6250 procedure Generate_Index_Checks
(N
: Node_Id
) is
6252 function Entity_Of_Prefix
return Entity_Id
;
6253 -- Returns the entity of the prefix of N (or Empty if not found)
6255 ----------------------
6256 -- Entity_Of_Prefix --
6257 ----------------------
6259 function Entity_Of_Prefix
return Entity_Id
is
6264 while not Is_Entity_Name
(P
) loop
6265 if not Nkind_In
(P
, N_Selected_Component
,
6266 N_Indexed_Component
)
6275 end Entity_Of_Prefix
;
6279 Loc
: constant Source_Ptr
:= Sloc
(N
);
6280 A
: constant Node_Id
:= Prefix
(N
);
6281 A_Ent
: constant Entity_Id
:= Entity_Of_Prefix
;
6284 -- Start of processing for Generate_Index_Checks
6287 -- Ignore call if the prefix is not an array since we have a serious
6288 -- error in the sources. Ignore it also if index checks are suppressed
6289 -- for array object or type.
6291 if not Is_Array_Type
(Etype
(A
))
6292 or else (Present
(A_Ent
) and then Index_Checks_Suppressed
(A_Ent
))
6293 or else Index_Checks_Suppressed
(Etype
(A
))
6297 -- The indexed component we are dealing with contains 'Loop_Entry in its
6298 -- prefix. This case arises when analysis has determined that constructs
6301 -- Prefix'Loop_Entry (Expr)
6302 -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN)
6304 -- require rewriting for error detection purposes. A side effect of this
6305 -- action is the generation of index checks that mention 'Loop_Entry.
6306 -- Delay the generation of the check until 'Loop_Entry has been properly
6307 -- expanded. This is done in Expand_Loop_Entry_Attributes.
6309 elsif Nkind
(Prefix
(N
)) = N_Attribute_Reference
6310 and then Attribute_Name
(Prefix
(N
)) = Name_Loop_Entry
6315 -- Generate a raise of constraint error with the appropriate reason and
6316 -- a condition of the form:
6318 -- Base_Type (Sub) not in Array'Range (Subscript)
6320 -- Note that the reason we generate the conversion to the base type here
6321 -- is that we definitely want the range check to take place, even if it
6322 -- looks like the subtype is OK. Optimization considerations that allow
6323 -- us to omit the check have already been taken into account in the
6324 -- setting of the Do_Range_Check flag earlier on.
6326 Sub
:= First
(Expressions
(N
));
6328 -- Handle string literals
6330 if Ekind
(Etype
(A
)) = E_String_Literal_Subtype
then
6331 if Do_Range_Check
(Sub
) then
6332 Set_Do_Range_Check
(Sub
, False);
6334 -- For string literals we obtain the bounds of the string from the
6335 -- associated subtype.
6338 Make_Raise_Constraint_Error
(Loc
,
6342 Convert_To
(Base_Type
(Etype
(Sub
)),
6343 Duplicate_Subexpr_Move_Checks
(Sub
)),
6345 Make_Attribute_Reference
(Loc
,
6346 Prefix
=> New_Occurrence_Of
(Etype
(A
), Loc
),
6347 Attribute_Name
=> Name_Range
)),
6348 Reason
=> CE_Index_Check_Failed
));
6355 A_Idx
: Node_Id
:= Empty
;
6362 A_Idx
:= First_Index
(Etype
(A
));
6364 while Present
(Sub
) loop
6365 if Do_Range_Check
(Sub
) then
6366 Set_Do_Range_Check
(Sub
, False);
6368 -- Force evaluation except for the case of a simple name of
6369 -- a nonvolatile entity.
6371 if not Is_Entity_Name
(Sub
)
6372 or else Treat_As_Volatile
(Entity
(Sub
))
6374 Force_Evaluation
(Sub
);
6377 if Nkind
(A_Idx
) = N_Range
then
6380 elsif Nkind
(A_Idx
) = N_Identifier
6381 or else Nkind
(A_Idx
) = N_Expanded_Name
6383 A_Range
:= Scalar_Range
(Entity
(A_Idx
));
6385 else pragma Assert
(Nkind
(A_Idx
) = N_Subtype_Indication
);
6386 A_Range
:= Range_Expression
(Constraint
(A_Idx
));
6389 -- For array objects with constant bounds we can generate
6390 -- the index check using the bounds of the type of the index
6393 and then Ekind
(A_Ent
) = E_Variable
6394 and then Is_Constant_Bound
(Low_Bound
(A_Range
))
6395 and then Is_Constant_Bound
(High_Bound
(A_Range
))
6398 Make_Attribute_Reference
(Loc
,
6400 New_Occurrence_Of
(Etype
(A_Idx
), Loc
),
6401 Attribute_Name
=> Name_Range
);
6403 -- For arrays with non-constant bounds we cannot generate
6404 -- the index check using the bounds of the type of the index
6405 -- since it may reference discriminants of some enclosing
6406 -- type. We obtain the bounds directly from the prefix
6413 Num
:= New_List
(Make_Integer_Literal
(Loc
, Ind
));
6417 Make_Attribute_Reference
(Loc
,
6419 Duplicate_Subexpr_Move_Checks
(A
, Name_Req
=> True),
6420 Attribute_Name
=> Name_Range
,
6421 Expressions
=> Num
);
6425 Make_Raise_Constraint_Error
(Loc
,
6429 Convert_To
(Base_Type
(Etype
(Sub
)),
6430 Duplicate_Subexpr_Move_Checks
(Sub
)),
6431 Right_Opnd
=> Range_N
),
6432 Reason
=> CE_Index_Check_Failed
));
6435 A_Idx
:= Next_Index
(A_Idx
);
6441 end Generate_Index_Checks
;
6443 --------------------------
6444 -- Generate_Range_Check --
6445 --------------------------
6447 procedure Generate_Range_Check
6449 Target_Type
: Entity_Id
;
6450 Reason
: RT_Exception_Code
)
6452 Loc
: constant Source_Ptr
:= Sloc
(N
);
6453 Source_Type
: constant Entity_Id
:= Etype
(N
);
6454 Source_Base_Type
: constant Entity_Id
:= Base_Type
(Source_Type
);
6455 Target_Base_Type
: constant Entity_Id
:= Base_Type
(Target_Type
);
6457 procedure Convert_And_Check_Range
;
6458 -- Convert the conversion operand to the target base type and save in
6459 -- a temporary. Then check the converted value against the range of the
6462 -----------------------------
6463 -- Convert_And_Check_Range --
6464 -----------------------------
6466 procedure Convert_And_Check_Range
is
6467 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
6470 -- We make a temporary to hold the value of the converted value
6471 -- (converted to the base type), and then do the test against this
6472 -- temporary. The conversion itself is replaced by an occurrence of
6473 -- Tnn and followed by the explicit range check. Note that checks
6474 -- are suppressed for this code, since we don't want a recursive
6475 -- range check popping up.
6477 -- Tnn : constant Target_Base_Type := Target_Base_Type (N);
6478 -- [constraint_error when Tnn not in Target_Type]
6480 Insert_Actions
(N
, New_List
(
6481 Make_Object_Declaration
(Loc
,
6482 Defining_Identifier
=> Tnn
,
6483 Object_Definition
=> New_Occurrence_Of
(Target_Base_Type
, Loc
),
6484 Constant_Present
=> True,
6486 Make_Type_Conversion
(Loc
,
6487 Subtype_Mark
=> New_Occurrence_Of
(Target_Base_Type
, Loc
),
6488 Expression
=> Duplicate_Subexpr
(N
))),
6490 Make_Raise_Constraint_Error
(Loc
,
6493 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6494 Right_Opnd
=> New_Occurrence_Of
(Target_Type
, Loc
)),
6496 Suppress
=> All_Checks
);
6498 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
6500 -- Set the type of N, because the declaration for Tnn might not
6501 -- be analyzed yet, as is the case if N appears within a record
6502 -- declaration, as a discriminant constraint or expression.
6504 Set_Etype
(N
, Target_Base_Type
);
6505 end Convert_And_Check_Range
;
6507 -- Start of processing for Generate_Range_Check
6510 -- First special case, if the source type is already within the range
6511 -- of the target type, then no check is needed (probably we should have
6512 -- stopped Do_Range_Check from being set in the first place, but better
6513 -- late than never in preventing junk code and junk flag settings.
6515 if In_Subrange_Of
(Source_Type
, Target_Type
)
6517 -- We do NOT apply this if the source node is a literal, since in this
6518 -- case the literal has already been labeled as having the subtype of
6522 (Nkind_In
(N
, N_Integer_Literal
, N_Real_Literal
, N_Character_Literal
)
6525 and then Ekind
(Entity
(N
)) = E_Enumeration_Literal
))
6527 Set_Do_Range_Check
(N
, False);
6531 -- Here a check is needed. If the expander is not active, or if we are
6532 -- in GNATProve mode, then simply set the Do_Range_Check flag and we
6533 -- are done. In both these cases, we just want to see the range check
6534 -- flag set, we do not want to generate the explicit range check code.
6536 if GNATprove_Mode
or else not Expander_Active
then
6537 Set_Do_Range_Check
(N
, True);
6541 -- Here we will generate an explicit range check, so we don't want to
6542 -- set the Do_Range check flag, since the range check is taken care of
6543 -- by the code we will generate.
6545 Set_Do_Range_Check
(N
, False);
6547 -- Force evaluation of the node, so that it does not get evaluated twice
6548 -- (once for the check, once for the actual reference). Such a double
6549 -- evaluation is always a potential source of inefficiency, and is
6550 -- functionally incorrect in the volatile case.
6552 if not Is_Entity_Name
(N
) or else Treat_As_Volatile
(Entity
(N
)) then
6553 Force_Evaluation
(N
);
6556 -- The easiest case is when Source_Base_Type and Target_Base_Type are
6557 -- the same since in this case we can simply do a direct check of the
6558 -- value of N against the bounds of Target_Type.
6560 -- [constraint_error when N not in Target_Type]
6562 -- Note: this is by far the most common case, for example all cases of
6563 -- checks on the RHS of assignments are in this category, but not all
6564 -- cases are like this. Notably conversions can involve two types.
6566 if Source_Base_Type
= Target_Base_Type
then
6568 -- Insert the explicit range check. Note that we suppress checks for
6569 -- this code, since we don't want a recursive range check popping up.
6572 Make_Raise_Constraint_Error
(Loc
,
6575 Left_Opnd
=> Duplicate_Subexpr
(N
),
6576 Right_Opnd
=> New_Occurrence_Of
(Target_Type
, Loc
)),
6578 Suppress
=> All_Checks
);
6580 -- Next test for the case where the target type is within the bounds
6581 -- of the base type of the source type, since in this case we can
6582 -- simply convert these bounds to the base type of T to do the test.
6584 -- [constraint_error when N not in
6585 -- Source_Base_Type (Target_Type'First)
6587 -- Source_Base_Type(Target_Type'Last))]
6589 -- The conversions will always work and need no check
6591 -- Unchecked_Convert_To is used instead of Convert_To to handle the case
6592 -- of converting from an enumeration value to an integer type, such as
6593 -- occurs for the case of generating a range check on Enum'Val(Exp)
6594 -- (which used to be handled by gigi). This is OK, since the conversion
6595 -- itself does not require a check.
6597 elsif In_Subrange_Of
(Target_Type
, Source_Base_Type
) then
6599 -- Insert the explicit range check. Note that we suppress checks for
6600 -- this code, since we don't want a recursive range check popping up.
6602 if Is_Discrete_Type
(Source_Base_Type
)
6604 Is_Discrete_Type
(Target_Base_Type
)
6607 Make_Raise_Constraint_Error
(Loc
,
6610 Left_Opnd
=> Duplicate_Subexpr
(N
),
6615 Unchecked_Convert_To
(Source_Base_Type
,
6616 Make_Attribute_Reference
(Loc
,
6618 New_Occurrence_Of
(Target_Type
, Loc
),
6619 Attribute_Name
=> Name_First
)),
6622 Unchecked_Convert_To
(Source_Base_Type
,
6623 Make_Attribute_Reference
(Loc
,
6625 New_Occurrence_Of
(Target_Type
, Loc
),
6626 Attribute_Name
=> Name_Last
)))),
6628 Suppress
=> All_Checks
);
6630 -- For conversions involving at least one type that is not discrete,
6631 -- first convert to target type and then generate the range check.
6632 -- This avoids problems with values that are close to a bound of the
6633 -- target type that would fail a range check when done in a larger
6634 -- source type before converting but would pass if converted with
6635 -- rounding and then checked (such as in float-to-float conversions).
6638 Convert_And_Check_Range
;
6641 -- Note that at this stage we now that the Target_Base_Type is not in
6642 -- the range of the Source_Base_Type (since even the Target_Type itself
6643 -- is not in this range). It could still be the case that Source_Type is
6644 -- in range of the target base type since we have not checked that case.
6646 -- If that is the case, we can freely convert the source to the target,
6647 -- and then test the target result against the bounds.
6649 elsif In_Subrange_Of
(Source_Type
, Target_Base_Type
) then
6650 Convert_And_Check_Range
;
6652 -- At this stage, we know that we have two scalar types, which are
6653 -- directly convertible, and where neither scalar type has a base
6654 -- range that is in the range of the other scalar type.
6656 -- The only way this can happen is with a signed and unsigned type.
6657 -- So test for these two cases:
6660 -- Case of the source is unsigned and the target is signed
6662 if Is_Unsigned_Type
(Source_Base_Type
)
6663 and then not Is_Unsigned_Type
(Target_Base_Type
)
6665 -- If the source is unsigned and the target is signed, then we
6666 -- know that the source is not shorter than the target (otherwise
6667 -- the source base type would be in the target base type range).
6669 -- In other words, the unsigned type is either the same size as
6670 -- the target, or it is larger. It cannot be smaller.
6673 (Esize
(Source_Base_Type
) >= Esize
(Target_Base_Type
));
6675 -- We only need to check the low bound if the low bound of the
6676 -- target type is non-negative. If the low bound of the target
6677 -- type is negative, then we know that we will fit fine.
6679 -- If the high bound of the target type is negative, then we
6680 -- know we have a constraint error, since we can't possibly
6681 -- have a negative source.
6683 -- With these two checks out of the way, we can do the check
6684 -- using the source type safely
6686 -- This is definitely the most annoying case.
6688 -- [constraint_error
6689 -- when (Target_Type'First >= 0
6691 -- N < Source_Base_Type (Target_Type'First))
6692 -- or else Target_Type'Last < 0
6693 -- or else N > Source_Base_Type (Target_Type'Last)];
6695 -- We turn off all checks since we know that the conversions
6696 -- will work fine, given the guards for negative values.
6699 Make_Raise_Constraint_Error
(Loc
,
6705 Left_Opnd
=> Make_Op_Ge
(Loc
,
6707 Make_Attribute_Reference
(Loc
,
6709 New_Occurrence_Of
(Target_Type
, Loc
),
6710 Attribute_Name
=> Name_First
),
6711 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
6715 Left_Opnd
=> Duplicate_Subexpr
(N
),
6717 Convert_To
(Source_Base_Type
,
6718 Make_Attribute_Reference
(Loc
,
6720 New_Occurrence_Of
(Target_Type
, Loc
),
6721 Attribute_Name
=> Name_First
)))),
6726 Make_Attribute_Reference
(Loc
,
6727 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
6728 Attribute_Name
=> Name_Last
),
6729 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
))),
6733 Left_Opnd
=> Duplicate_Subexpr
(N
),
6735 Convert_To
(Source_Base_Type
,
6736 Make_Attribute_Reference
(Loc
,
6737 Prefix
=> New_Occurrence_Of
(Target_Type
, Loc
),
6738 Attribute_Name
=> Name_Last
)))),
6741 Suppress
=> All_Checks
);
6743 -- Only remaining possibility is that the source is signed and
6744 -- the target is unsigned.
6747 pragma Assert
(not Is_Unsigned_Type
(Source_Base_Type
)
6748 and then Is_Unsigned_Type
(Target_Base_Type
));
6750 -- If the source is signed and the target is unsigned, then we
6751 -- know that the target is not shorter than the source (otherwise
6752 -- the target base type would be in the source base type range).
6754 -- In other words, the unsigned type is either the same size as
6755 -- the target, or it is larger. It cannot be smaller.
6757 -- Clearly we have an error if the source value is negative since
6758 -- no unsigned type can have negative values. If the source type
6759 -- is non-negative, then the check can be done using the target
6762 -- Tnn : constant Target_Base_Type (N) := Target_Type;
6764 -- [constraint_error
6765 -- when N < 0 or else Tnn not in Target_Type];
6767 -- We turn off all checks for the conversion of N to the target
6768 -- base type, since we generate the explicit check to ensure that
6769 -- the value is non-negative
6772 Tnn
: constant Entity_Id
:= Make_Temporary
(Loc
, 'T', N
);
6775 Insert_Actions
(N
, New_List
(
6776 Make_Object_Declaration
(Loc
,
6777 Defining_Identifier
=> Tnn
,
6778 Object_Definition
=>
6779 New_Occurrence_Of
(Target_Base_Type
, Loc
),
6780 Constant_Present
=> True,
6782 Make_Unchecked_Type_Conversion
(Loc
,
6784 New_Occurrence_Of
(Target_Base_Type
, Loc
),
6785 Expression
=> Duplicate_Subexpr
(N
))),
6787 Make_Raise_Constraint_Error
(Loc
,
6792 Left_Opnd
=> Duplicate_Subexpr
(N
),
6793 Right_Opnd
=> Make_Integer_Literal
(Loc
, Uint_0
)),
6797 Left_Opnd
=> New_Occurrence_Of
(Tnn
, Loc
),
6799 New_Occurrence_Of
(Target_Type
, Loc
))),
6802 Suppress
=> All_Checks
);
6804 -- Set the Etype explicitly, because Insert_Actions may have
6805 -- placed the declaration in the freeze list for an enclosing
6806 -- construct, and thus it is not analyzed yet.
6808 Set_Etype
(Tnn
, Target_Base_Type
);
6809 Rewrite
(N
, New_Occurrence_Of
(Tnn
, Loc
));
6813 end Generate_Range_Check
;
6819 function Get_Check_Id
(N
: Name_Id
) return Check_Id
is
6821 -- For standard check name, we can do a direct computation
6823 if N
in First_Check_Name
.. Last_Check_Name
then
6824 return Check_Id
(N
- (First_Check_Name
- 1));
6826 -- For non-standard names added by pragma Check_Name, search table
6829 for J
in All_Checks
+ 1 .. Check_Names
.Last
loop
6830 if Check_Names
.Table
(J
) = N
then
6836 -- No matching name found
6841 ---------------------
6842 -- Get_Discriminal --
6843 ---------------------
6845 function Get_Discriminal
(E
: Entity_Id
; Bound
: Node_Id
) return Node_Id
is
6846 Loc
: constant Source_Ptr
:= Sloc
(E
);
6851 -- The bound can be a bona fide parameter of a protected operation,
6852 -- rather than a prival encoded as an in-parameter.
6854 if No
(Discriminal_Link
(Entity
(Bound
))) then
6858 -- Climb the scope stack looking for an enclosing protected type. If
6859 -- we run out of scopes, return the bound itself.
6862 while Present
(Sc
) loop
6863 if Sc
= Standard_Standard
then
6865 elsif Ekind
(Sc
) = E_Protected_Type
then
6872 D
:= First_Discriminant
(Sc
);
6873 while Present
(D
) loop
6874 if Chars
(D
) = Chars
(Bound
) then
6875 return New_Occurrence_Of
(Discriminal
(D
), Loc
);
6878 Next_Discriminant
(D
);
6882 end Get_Discriminal
;
6884 ----------------------
6885 -- Get_Range_Checks --
6886 ----------------------
6888 function Get_Range_Checks
6890 Target_Typ
: Entity_Id
;
6891 Source_Typ
: Entity_Id
:= Empty
;
6892 Warn_Node
: Node_Id
:= Empty
) return Check_Result
6896 Selected_Range_Checks
(Ck_Node
, Target_Typ
, Source_Typ
, Warn_Node
);
6897 end Get_Range_Checks
;
6903 function Guard_Access
6906 Ck_Node
: Node_Id
) return Node_Id
6909 if Nkind
(Cond
) = N_Or_Else
then
6910 Set_Paren_Count
(Cond
, 1);
6913 if Nkind
(Ck_Node
) = N_Allocator
then
6921 Left_Opnd
=> Duplicate_Subexpr_No_Checks
(Ck_Node
),
6922 Right_Opnd
=> Make_Null
(Loc
)),
6923 Right_Opnd
=> Cond
);
6927 -----------------------------
6928 -- Index_Checks_Suppressed --
6929 -----------------------------
6931 function Index_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
6933 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
6934 return Is_Check_Suppressed
(E
, Index_Check
);
6936 return Scope_Suppress
.Suppress
(Index_Check
);
6938 end Index_Checks_Suppressed
;
6944 procedure Initialize
is
6946 for J
in Determine_Range_Cache_N
'Range loop
6947 Determine_Range_Cache_N
(J
) := Empty
;
6952 for J
in Int
range 1 .. All_Checks
loop
6953 Check_Names
.Append
(Name_Id
(Int
(First_Check_Name
) + J
- 1));
6957 -------------------------
6958 -- Insert_Range_Checks --
6959 -------------------------
6961 procedure Insert_Range_Checks
6962 (Checks
: Check_Result
;
6964 Suppress_Typ
: Entity_Id
;
6965 Static_Sloc
: Source_Ptr
:= No_Location
;
6966 Flag_Node
: Node_Id
:= Empty
;
6967 Do_Before
: Boolean := False)
6969 Internal_Flag_Node
: Node_Id
:= Flag_Node
;
6970 Internal_Static_Sloc
: Source_Ptr
:= Static_Sloc
;
6972 Check_Node
: Node_Id
;
6973 Checks_On
: constant Boolean :=
6974 (not Index_Checks_Suppressed
(Suppress_Typ
))
6975 or else (not Range_Checks_Suppressed
(Suppress_Typ
));
6978 -- For now we just return if Checks_On is false, however this should be
6979 -- enhanced to check for an always True value in the condition and to
6980 -- generate a compilation warning???
6982 if not Expander_Active
or not Checks_On
then
6986 if Static_Sloc
= No_Location
then
6987 Internal_Static_Sloc
:= Sloc
(Node
);
6990 if No
(Flag_Node
) then
6991 Internal_Flag_Node
:= Node
;
6994 for J
in 1 .. 2 loop
6995 exit when No
(Checks
(J
));
6997 if Nkind
(Checks
(J
)) = N_Raise_Constraint_Error
6998 and then Present
(Condition
(Checks
(J
)))
7000 if not Has_Dynamic_Range_Check
(Internal_Flag_Node
) then
7001 Check_Node
:= Checks
(J
);
7002 Mark_Rewrite_Insertion
(Check_Node
);
7005 Insert_Before_And_Analyze
(Node
, Check_Node
);
7007 Insert_After_And_Analyze
(Node
, Check_Node
);
7010 Set_Has_Dynamic_Range_Check
(Internal_Flag_Node
);
7015 Make_Raise_Constraint_Error
(Internal_Static_Sloc
,
7016 Reason
=> CE_Range_Check_Failed
);
7017 Mark_Rewrite_Insertion
(Check_Node
);
7020 Insert_Before_And_Analyze
(Node
, Check_Node
);
7022 Insert_After_And_Analyze
(Node
, Check_Node
);
7026 end Insert_Range_Checks
;
7028 ------------------------
7029 -- Insert_Valid_Check --
7030 ------------------------
7032 procedure Insert_Valid_Check
7034 Related_Id
: Entity_Id
:= Empty
;
7035 Is_Low_Bound
: Boolean := False;
7036 Is_High_Bound
: Boolean := False)
7038 Loc
: constant Source_Ptr
:= Sloc
(Expr
);
7039 Typ
: constant Entity_Id
:= Etype
(Expr
);
7043 -- Do not insert if checks off, or if not checking validity or if
7044 -- expression is known to be valid.
7046 if not Validity_Checks_On
7047 or else Range_Or_Validity_Checks_Suppressed
(Expr
)
7048 or else Expr_Known_Valid
(Expr
)
7053 -- Do not insert checks within a predicate function. This will arise
7054 -- if the current unit and the predicate function are being compiled
7055 -- with validity checks enabled.
7057 if Present
(Predicate_Function
(Typ
))
7058 and then Current_Scope
= Predicate_Function
(Typ
)
7063 -- If the expression is a packed component of a modular type of the
7064 -- right size, the data is always valid.
7066 if Nkind
(Expr
) = N_Selected_Component
7067 and then Present
(Component_Clause
(Entity
(Selector_Name
(Expr
))))
7068 and then Is_Modular_Integer_Type
(Typ
)
7069 and then Modulus
(Typ
) = 2 ** Esize
(Entity
(Selector_Name
(Expr
)))
7074 -- If we have a checked conversion, then validity check applies to
7075 -- the expression inside the conversion, not the result, since if
7076 -- the expression inside is valid, then so is the conversion result.
7079 while Nkind
(Exp
) = N_Type_Conversion
loop
7080 Exp
:= Expression
(Exp
);
7083 -- We are about to insert the validity check for Exp. We save and
7084 -- reset the Do_Range_Check flag over this validity check, and then
7085 -- put it back for the final original reference (Exp may be rewritten).
7088 DRC
: constant Boolean := Do_Range_Check
(Exp
);
7093 Set_Do_Range_Check
(Exp
, False);
7095 -- Force evaluation to avoid multiple reads for atomic/volatile
7097 -- Note: we set Name_Req to False. We used to set it to True, with
7098 -- the thinking that a name is required as the prefix of the 'Valid
7099 -- call, but in fact the check that the prefix of an attribute is
7100 -- a name is in the parser, and we just don't require it here.
7101 -- Moreover, when we set Name_Req to True, that interfered with the
7102 -- checking for Volatile, since we couldn't just capture the value.
7104 if Is_Entity_Name
(Exp
)
7105 and then Is_Volatile
(Entity
(Exp
))
7107 -- Same reasoning as above for setting Name_Req to False
7109 Force_Evaluation
(Exp
, Name_Req
=> False);
7112 -- Build the prefix for the 'Valid call
7115 Duplicate_Subexpr_No_Checks
7118 Related_Id
=> Related_Id
,
7119 Is_Low_Bound
=> Is_Low_Bound
,
7120 Is_High_Bound
=> Is_High_Bound
);
7122 -- A rather specialized test. If PV is an analyzed expression which
7123 -- is an indexed component of a packed array that has not been
7124 -- properly expanded, turn off its Analyzed flag to make sure it
7125 -- gets properly reexpanded. If the prefix is an access value,
7126 -- the dereference will be added later.
7128 -- The reason this arises is that Duplicate_Subexpr_No_Checks did
7129 -- an analyze with the old parent pointer. This may point e.g. to
7130 -- a subprogram call, which deactivates this expansion.
7133 and then Nkind
(PV
) = N_Indexed_Component
7134 and then Is_Array_Type
(Etype
(Prefix
(PV
)))
7135 and then Present
(Packed_Array_Impl_Type
(Etype
(Prefix
(PV
))))
7137 Set_Analyzed
(PV
, False);
7140 -- Build the raise CE node to check for validity. We build a type
7141 -- qualification for the prefix, since it may not be of the form of
7142 -- a name, and we don't care in this context!
7145 Make_Raise_Constraint_Error
(Loc
,
7149 Make_Attribute_Reference
(Loc
,
7151 Attribute_Name
=> Name_Valid
)),
7152 Reason
=> CE_Invalid_Data
);
7154 -- Insert the validity check. Note that we do this with validity
7155 -- checks turned off, to avoid recursion, we do not want validity
7156 -- checks on the validity checking code itself.
7158 Insert_Action
(Expr
, CE
, Suppress
=> Validity_Check
);
7160 -- If the expression is a reference to an element of a bit-packed
7161 -- array, then it is rewritten as a renaming declaration. If the
7162 -- expression is an actual in a call, it has not been expanded,
7163 -- waiting for the proper point at which to do it. The same happens
7164 -- with renamings, so that we have to force the expansion now. This
7165 -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb
7168 if Is_Entity_Name
(Exp
)
7169 and then Nkind
(Parent
(Entity
(Exp
))) =
7170 N_Object_Renaming_Declaration
7173 Old_Exp
: constant Node_Id
:= Name
(Parent
(Entity
(Exp
)));
7175 if Nkind
(Old_Exp
) = N_Indexed_Component
7176 and then Is_Bit_Packed_Array
(Etype
(Prefix
(Old_Exp
)))
7178 Expand_Packed_Element_Reference
(Old_Exp
);
7183 -- Put back the Do_Range_Check flag on the resulting (possibly
7184 -- rewritten) expression.
7186 -- Note: it might be thought that a validity check is not required
7187 -- when a range check is present, but that's not the case, because
7188 -- the back end is allowed to assume for the range check that the
7189 -- operand is within its declared range (an assumption that validity
7190 -- checking is all about NOT assuming).
7192 -- Note: no need to worry about Possible_Local_Raise here, it will
7193 -- already have been called if original node has Do_Range_Check set.
7195 Set_Do_Range_Check
(Exp
, DRC
);
7197 end Insert_Valid_Check
;
7199 -------------------------------------
7200 -- Is_Signed_Integer_Arithmetic_Op --
7201 -------------------------------------
7203 function Is_Signed_Integer_Arithmetic_Op
(N
: Node_Id
) return Boolean is
7206 when N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon |
7207 N_Op_Minus | N_Op_Mod | N_Op_Multiply | N_Op_Plus |
7208 N_Op_Rem | N_Op_Subtract
=>
7209 return Is_Signed_Integer_Type
(Etype
(N
));
7211 when N_If_Expression | N_Case_Expression
=>
7212 return Is_Signed_Integer_Type
(Etype
(N
));
7217 end Is_Signed_Integer_Arithmetic_Op
;
7219 ----------------------------------
7220 -- Install_Null_Excluding_Check --
7221 ----------------------------------
7223 procedure Install_Null_Excluding_Check
(N
: Node_Id
) is
7224 Loc
: constant Source_Ptr
:= Sloc
(Parent
(N
));
7225 Typ
: constant Entity_Id
:= Etype
(N
);
7227 function Safe_To_Capture_In_Parameter_Value
return Boolean;
7228 -- Determines if it is safe to capture Known_Non_Null status for an
7229 -- the entity referenced by node N. The caller ensures that N is indeed
7230 -- an entity name. It is safe to capture the non-null status for an IN
7231 -- parameter when the reference occurs within a declaration that is sure
7232 -- to be executed as part of the declarative region.
7234 procedure Mark_Non_Null
;
7235 -- After installation of check, if the node in question is an entity
7236 -- name, then mark this entity as non-null if possible.
7238 function Safe_To_Capture_In_Parameter_Value
return Boolean is
7239 E
: constant Entity_Id
:= Entity
(N
);
7240 S
: constant Entity_Id
:= Current_Scope
;
7244 if Ekind
(E
) /= E_In_Parameter
then
7248 -- Two initial context checks. We must be inside a subprogram body
7249 -- with declarations and reference must not appear in nested scopes.
7251 if (Ekind
(S
) /= E_Function
and then Ekind
(S
) /= E_Procedure
)
7252 or else Scope
(E
) /= S
7257 S_Par
:= Parent
(Parent
(S
));
7259 if Nkind
(S_Par
) /= N_Subprogram_Body
7260 or else No
(Declarations
(S_Par
))
7270 -- Retrieve the declaration node of N (if any). Note that N
7271 -- may be a part of a complex initialization expression.
7275 while Present
(P
) loop
7277 -- If we have a short circuit form, and we are within the right
7278 -- hand expression, we return false, since the right hand side
7279 -- is not guaranteed to be elaborated.
7281 if Nkind
(P
) in N_Short_Circuit
7282 and then N
= Right_Opnd
(P
)
7287 -- Similarly, if we are in an if expression and not part of the
7288 -- condition, then we return False, since neither the THEN or
7289 -- ELSE dependent expressions will always be elaborated.
7291 if Nkind
(P
) = N_If_Expression
7292 and then N
/= First
(Expressions
(P
))
7297 -- If within a case expression, and not part of the expression,
7298 -- then return False, since a particular dependent expression
7299 -- may not always be elaborated
7301 if Nkind
(P
) = N_Case_Expression
7302 and then N
/= Expression
(P
)
7307 -- While traversing the parent chain, if node N belongs to a
7308 -- statement, then it may never appear in a declarative region.
7310 if Nkind
(P
) in N_Statement_Other_Than_Procedure_Call
7311 or else Nkind
(P
) = N_Procedure_Call_Statement
7316 -- If we are at a declaration, record it and exit
7318 if Nkind
(P
) in N_Declaration
7319 and then Nkind
(P
) not in N_Subprogram_Specification
7332 return List_Containing
(N_Decl
) = Declarations
(S_Par
);
7334 end Safe_To_Capture_In_Parameter_Value
;
7340 procedure Mark_Non_Null
is
7342 -- Only case of interest is if node N is an entity name
7344 if Is_Entity_Name
(N
) then
7346 -- For sure, we want to clear an indication that this is known to
7347 -- be null, since if we get past this check, it definitely is not.
7349 Set_Is_Known_Null
(Entity
(N
), False);
7351 -- We can mark the entity as known to be non-null if either it is
7352 -- safe to capture the value, or in the case of an IN parameter,
7353 -- which is a constant, if the check we just installed is in the
7354 -- declarative region of the subprogram body. In this latter case,
7355 -- a check is decisive for the rest of the body if the expression
7356 -- is sure to be elaborated, since we know we have to elaborate
7357 -- all declarations before executing the body.
7359 -- Couldn't this always be part of Safe_To_Capture_Value ???
7361 if Safe_To_Capture_Value
(N
, Entity
(N
))
7362 or else Safe_To_Capture_In_Parameter_Value
7364 Set_Is_Known_Non_Null
(Entity
(N
));
7369 -- Start of processing for Install_Null_Excluding_Check
7372 pragma Assert
(Is_Access_Type
(Typ
));
7374 -- No check inside a generic, check will be emitted in instance
7376 if Inside_A_Generic
then
7380 -- No check needed if known to be non-null
7382 if Known_Non_Null
(N
) then
7386 -- If known to be null, here is where we generate a compile time check
7388 if Known_Null
(N
) then
7390 -- Avoid generating warning message inside init procs. In SPARK mode
7391 -- we can go ahead and call Apply_Compile_Time_Constraint_Error
7392 -- since it will be turned into an error in any case.
7394 if (not Inside_Init_Proc
or else SPARK_Mode
= On
)
7396 -- Do not emit the warning within a conditional expression,
7397 -- where the expression might not be evaluated, and the warning
7398 -- appear as extraneous noise.
7400 and then not Within_Case_Or_If_Expression
(N
)
7402 Apply_Compile_Time_Constraint_Error
7403 (N
, "null value not allowed here??", CE_Access_Check_Failed
);
7405 -- Remaining cases, where we silently insert the raise
7409 Make_Raise_Constraint_Error
(Loc
,
7410 Reason
=> CE_Access_Check_Failed
));
7417 -- If entity is never assigned, for sure a warning is appropriate
7419 if Is_Entity_Name
(N
) then
7420 Check_Unset_Reference
(N
);
7423 -- No check needed if checks are suppressed on the range. Note that we
7424 -- don't set Is_Known_Non_Null in this case (we could legitimately do
7425 -- so, since the program is erroneous, but we don't like to casually
7426 -- propagate such conclusions from erroneosity).
7428 if Access_Checks_Suppressed
(Typ
) then
7432 -- No check needed for access to concurrent record types generated by
7433 -- the expander. This is not just an optimization (though it does indeed
7434 -- remove junk checks). It also avoids generation of junk warnings.
7436 if Nkind
(N
) in N_Has_Chars
7437 and then Chars
(N
) = Name_uObject
7438 and then Is_Concurrent_Record_Type
7439 (Directly_Designated_Type
(Etype
(N
)))
7444 -- No check needed in interface thunks since the runtime check is
7445 -- already performed at the caller side.
7447 if Is_Thunk
(Current_Scope
) then
7451 -- No check needed for the Get_Current_Excep.all.all idiom generated by
7452 -- the expander within exception handlers, since we know that the value
7453 -- can never be null.
7455 -- Is this really the right way to do this? Normally we generate such
7456 -- code in the expander with checks off, and that's how we suppress this
7457 -- kind of junk check ???
7459 if Nkind
(N
) = N_Function_Call
7460 and then Nkind
(Name
(N
)) = N_Explicit_Dereference
7461 and then Nkind
(Prefix
(Name
(N
))) = N_Identifier
7462 and then Is_RTE
(Entity
(Prefix
(Name
(N
))), RE_Get_Current_Excep
)
7467 -- Otherwise install access check
7470 Make_Raise_Constraint_Error
(Loc
,
7473 Left_Opnd
=> Duplicate_Subexpr_Move_Checks
(N
),
7474 Right_Opnd
=> Make_Null
(Loc
)),
7475 Reason
=> CE_Access_Check_Failed
));
7478 end Install_Null_Excluding_Check
;
7480 --------------------------
7481 -- Install_Static_Check --
7482 --------------------------
7484 procedure Install_Static_Check
(R_Cno
: Node_Id
; Loc
: Source_Ptr
) is
7485 Stat
: constant Boolean := Is_OK_Static_Expression
(R_Cno
);
7486 Typ
: constant Entity_Id
:= Etype
(R_Cno
);
7490 Make_Raise_Constraint_Error
(Loc
,
7491 Reason
=> CE_Range_Check_Failed
));
7492 Set_Analyzed
(R_Cno
);
7493 Set_Etype
(R_Cno
, Typ
);
7494 Set_Raises_Constraint_Error
(R_Cno
);
7495 Set_Is_Static_Expression
(R_Cno
, Stat
);
7497 -- Now deal with possible local raise handling
7499 Possible_Local_Raise
(R_Cno
, Standard_Constraint_Error
);
7500 end Install_Static_Check
;
7502 -------------------------
7503 -- Is_Check_Suppressed --
7504 -------------------------
7506 function Is_Check_Suppressed
(E
: Entity_Id
; C
: Check_Id
) return Boolean is
7507 Ptr
: Suppress_Stack_Entry_Ptr
;
7510 -- First search the local entity suppress stack. We search this from the
7511 -- top of the stack down so that we get the innermost entry that applies
7512 -- to this case if there are nested entries.
7514 Ptr
:= Local_Suppress_Stack_Top
;
7515 while Ptr
/= null loop
7516 if (Ptr
.Entity
= Empty
or else Ptr
.Entity
= E
)
7517 and then (Ptr
.Check
= All_Checks
or else Ptr
.Check
= C
)
7519 return Ptr
.Suppress
;
7525 -- Now search the global entity suppress table for a matching entry.
7526 -- We also search this from the top down so that if there are multiple
7527 -- pragmas for the same entity, the last one applies (not clear what
7528 -- or whether the RM specifies this handling, but it seems reasonable).
7530 Ptr
:= Global_Suppress_Stack_Top
;
7531 while Ptr
/= null loop
7532 if (Ptr
.Entity
= Empty
or else Ptr
.Entity
= E
)
7533 and then (Ptr
.Check
= All_Checks
or else Ptr
.Check
= C
)
7535 return Ptr
.Suppress
;
7541 -- If we did not find a matching entry, then use the normal scope
7542 -- suppress value after all (actually this will be the global setting
7543 -- since it clearly was not overridden at any point). For a predefined
7544 -- check, we test the specific flag. For a user defined check, we check
7545 -- the All_Checks flag. The Overflow flag requires special handling to
7546 -- deal with the General vs Assertion case
7548 if C
= Overflow_Check
then
7549 return Overflow_Checks_Suppressed
(Empty
);
7550 elsif C
in Predefined_Check_Id
then
7551 return Scope_Suppress
.Suppress
(C
);
7553 return Scope_Suppress
.Suppress
(All_Checks
);
7555 end Is_Check_Suppressed
;
7557 ---------------------
7558 -- Kill_All_Checks --
7559 ---------------------
7561 procedure Kill_All_Checks
is
7563 if Debug_Flag_CC
then
7564 w
("Kill_All_Checks");
7567 -- We reset the number of saved checks to zero, and also modify all
7568 -- stack entries for statement ranges to indicate that the number of
7569 -- checks at each level is now zero.
7571 Num_Saved_Checks
:= 0;
7573 -- Note: the Int'Min here avoids any possibility of J being out of
7574 -- range when called from e.g. Conditional_Statements_Begin.
7576 for J
in 1 .. Int
'Min (Saved_Checks_TOS
, Saved_Checks_Stack
'Last) loop
7577 Saved_Checks_Stack
(J
) := 0;
7579 end Kill_All_Checks
;
7585 procedure Kill_Checks
(V
: Entity_Id
) is
7587 if Debug_Flag_CC
then
7588 w
("Kill_Checks for entity", Int
(V
));
7591 for J
in 1 .. Num_Saved_Checks
loop
7592 if Saved_Checks
(J
).Entity
= V
then
7593 if Debug_Flag_CC
then
7594 w
(" Checks killed for saved check ", J
);
7597 Saved_Checks
(J
).Killed
:= True;
7602 ------------------------------
7603 -- Length_Checks_Suppressed --
7604 ------------------------------
7606 function Length_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
7608 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
7609 return Is_Check_Suppressed
(E
, Length_Check
);
7611 return Scope_Suppress
.Suppress
(Length_Check
);
7613 end Length_Checks_Suppressed
;
7615 -----------------------
7616 -- Make_Bignum_Block --
7617 -----------------------
7619 function Make_Bignum_Block
(Loc
: Source_Ptr
) return Node_Id
is
7620 M
: constant Entity_Id
:= Make_Defining_Identifier
(Loc
, Name_uM
);
7623 Make_Block_Statement
(Loc
,
7625 New_List
(Build_SS_Mark_Call
(Loc
, M
)),
7626 Handled_Statement_Sequence
=>
7627 Make_Handled_Sequence_Of_Statements
(Loc
,
7628 Statements
=> New_List
(Build_SS_Release_Call
(Loc
, M
))));
7629 end Make_Bignum_Block
;
7631 ----------------------------------
7632 -- Minimize_Eliminate_Overflows --
7633 ----------------------------------
7635 -- This is a recursive routine that is called at the top of an expression
7636 -- tree to properly process overflow checking for a whole subtree by making
7637 -- recursive calls to process operands. This processing may involve the use
7638 -- of bignum or long long integer arithmetic, which will change the types
7639 -- of operands and results. That's why we can't do this bottom up (since
7640 -- it would interfere with semantic analysis).
7642 -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then
7643 -- the operator expansion routines, as well as the expansion routines for
7644 -- if/case expression, do nothing (for the moment) except call the routine
7645 -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That
7646 -- routine does nothing for non top-level nodes, so at the point where the
7647 -- call is made for the top level node, the entire expression subtree has
7648 -- not been expanded, or processed for overflow. All that has to happen as
7649 -- a result of the top level call to this routine.
7651 -- As noted above, the overflow processing works by making recursive calls
7652 -- for the operands, and figuring out what to do, based on the processing
7653 -- of these operands (e.g. if a bignum operand appears, the parent op has
7654 -- to be done in bignum mode), and the determined ranges of the operands.
7656 -- After possible rewriting of a constituent subexpression node, a call is
7657 -- made to either reexpand the node (if nothing has changed) or reanalyze
7658 -- the node (if it has been modified by the overflow check processing). The
7659 -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid
7660 -- a recursive call into the whole overflow apparatus, an important rule
7661 -- for this call is that the overflow handling mode must be temporarily set
7664 procedure Minimize_Eliminate_Overflows
7668 Top_Level
: Boolean)
7670 Rtyp
: constant Entity_Id
:= Etype
(N
);
7671 pragma Assert
(Is_Signed_Integer_Type
(Rtyp
));
7672 -- Result type, must be a signed integer type
7674 Check_Mode
: constant Overflow_Mode_Type
:= Overflow_Check_Mode
;
7675 pragma Assert
(Check_Mode
in Minimized_Or_Eliminated
);
7677 Loc
: constant Source_Ptr
:= Sloc
(N
);
7680 -- Ranges of values for right operand (operator case)
7683 -- Ranges of values for left operand (operator case)
7685 LLIB
: constant Entity_Id
:= Base_Type
(Standard_Long_Long_Integer
);
7686 -- Operands and results are of this type when we convert
7688 LLLo
: constant Uint
:= Intval
(Type_Low_Bound
(LLIB
));
7689 LLHi
: constant Uint
:= Intval
(Type_High_Bound
(LLIB
));
7690 -- Bounds of Long_Long_Integer
7692 Binary
: constant Boolean := Nkind
(N
) in N_Binary_Op
;
7693 -- Indicates binary operator case
7696 -- Used in call to Determine_Range
7698 Bignum_Operands
: Boolean;
7699 -- Set True if one or more operands is already of type Bignum, meaning
7700 -- that for sure (regardless of Top_Level setting) we are committed to
7701 -- doing the operation in Bignum mode (or in the case of a case or if
7702 -- expression, converting all the dependent expressions to Bignum).
7704 Long_Long_Integer_Operands
: Boolean;
7705 -- Set True if one or more operands is already of type Long_Long_Integer
7706 -- which means that if the result is known to be in the result type
7707 -- range, then we must convert such operands back to the result type.
7709 procedure Reanalyze
(Typ
: Entity_Id
; Suppress
: Boolean := False);
7710 -- This is called when we have modified the node and we therefore need
7711 -- to reanalyze it. It is important that we reset the mode to STRICT for
7712 -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode
7713 -- we would reenter this routine recursively which would not be good.
7714 -- The argument Suppress is set True if we also want to suppress
7715 -- overflow checking for the reexpansion (this is set when we know
7716 -- overflow is not possible). Typ is the type for the reanalysis.
7718 procedure Reexpand
(Suppress
: Boolean := False);
7719 -- This is like Reanalyze, but does not do the Analyze step, it only
7720 -- does a reexpansion. We do this reexpansion in STRICT mode, so that
7721 -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we
7722 -- follow the normal expansion path (e.g. converting A**4 to A**2**2).
7723 -- Note that skipping reanalysis is not just an optimization, testing
7724 -- has showed up several complex cases in which reanalyzing an already
7725 -- analyzed node causes incorrect behavior.
7727 function In_Result_Range
return Boolean;
7728 -- Returns True iff Lo .. Hi are within range of the result type
7730 procedure Max
(A
: in out Uint
; B
: Uint
);
7731 -- If A is No_Uint, sets A to B, else to UI_Max (A, B)
7733 procedure Min
(A
: in out Uint
; B
: Uint
);
7734 -- If A is No_Uint, sets A to B, else to UI_Min (A, B)
7736 ---------------------
7737 -- In_Result_Range --
7738 ---------------------
7740 function In_Result_Range
return Boolean is
7742 if Lo
= No_Uint
or else Hi
= No_Uint
then
7745 elsif Is_OK_Static_Subtype
(Etype
(N
)) then
7746 return Lo
>= Expr_Value
(Type_Low_Bound
(Rtyp
))
7748 Hi
<= Expr_Value
(Type_High_Bound
(Rtyp
));
7751 return Lo
>= Expr_Value
(Type_Low_Bound
(Base_Type
(Rtyp
)))
7753 Hi
<= Expr_Value
(Type_High_Bound
(Base_Type
(Rtyp
)));
7755 end In_Result_Range
;
7761 procedure Max
(A
: in out Uint
; B
: Uint
) is
7763 if A
= No_Uint
or else B
> A
then
7772 procedure Min
(A
: in out Uint
; B
: Uint
) is
7774 if A
= No_Uint
or else B
< A
then
7783 procedure Reanalyze
(Typ
: Entity_Id
; Suppress
: Boolean := False) is
7784 Svg
: constant Overflow_Mode_Type
:=
7785 Scope_Suppress
.Overflow_Mode_General
;
7786 Sva
: constant Overflow_Mode_Type
:=
7787 Scope_Suppress
.Overflow_Mode_Assertions
;
7788 Svo
: constant Boolean :=
7789 Scope_Suppress
.Suppress
(Overflow_Check
);
7792 Scope_Suppress
.Overflow_Mode_General
:= Strict
;
7793 Scope_Suppress
.Overflow_Mode_Assertions
:= Strict
;
7796 Scope_Suppress
.Suppress
(Overflow_Check
) := True;
7799 Analyze_And_Resolve
(N
, Typ
);
7801 Scope_Suppress
.Suppress
(Overflow_Check
) := Svo
;
7802 Scope_Suppress
.Overflow_Mode_General
:= Svg
;
7803 Scope_Suppress
.Overflow_Mode_Assertions
:= Sva
;
7810 procedure Reexpand
(Suppress
: Boolean := False) is
7811 Svg
: constant Overflow_Mode_Type
:=
7812 Scope_Suppress
.Overflow_Mode_General
;
7813 Sva
: constant Overflow_Mode_Type
:=
7814 Scope_Suppress
.Overflow_Mode_Assertions
;
7815 Svo
: constant Boolean :=
7816 Scope_Suppress
.Suppress
(Overflow_Check
);
7819 Scope_Suppress
.Overflow_Mode_General
:= Strict
;
7820 Scope_Suppress
.Overflow_Mode_Assertions
:= Strict
;
7821 Set_Analyzed
(N
, False);
7824 Scope_Suppress
.Suppress
(Overflow_Check
) := True;
7829 Scope_Suppress
.Suppress
(Overflow_Check
) := Svo
;
7830 Scope_Suppress
.Overflow_Mode_General
:= Svg
;
7831 Scope_Suppress
.Overflow_Mode_Assertions
:= Sva
;
7834 -- Start of processing for Minimize_Eliminate_Overflows
7837 -- Case where we do not have a signed integer arithmetic operation
7839 if not Is_Signed_Integer_Arithmetic_Op
(N
) then
7841 -- Use the normal Determine_Range routine to get the range. We
7842 -- don't require operands to be valid, invalid values may result in
7843 -- rubbish results where the result has not been properly checked for
7844 -- overflow, that's fine.
7846 Determine_Range
(N
, OK
, Lo
, Hi
, Assume_Valid
=> False);
7848 -- If Determine_Range did not work (can this in fact happen? Not
7849 -- clear but might as well protect), use type bounds.
7852 Lo
:= Intval
(Type_Low_Bound
(Base_Type
(Etype
(N
))));
7853 Hi
:= Intval
(Type_High_Bound
(Base_Type
(Etype
(N
))));
7856 -- If we don't have a binary operator, all we have to do is to set
7857 -- the Hi/Lo range, so we are done.
7861 -- Processing for if expression
7863 elsif Nkind
(N
) = N_If_Expression
then
7865 Then_DE
: constant Node_Id
:= Next
(First
(Expressions
(N
)));
7866 Else_DE
: constant Node_Id
:= Next
(Then_DE
);
7869 Bignum_Operands
:= False;
7871 Minimize_Eliminate_Overflows
7872 (Then_DE
, Lo
, Hi
, Top_Level
=> False);
7874 if Lo
= No_Uint
then
7875 Bignum_Operands
:= True;
7878 Minimize_Eliminate_Overflows
7879 (Else_DE
, Rlo
, Rhi
, Top_Level
=> False);
7881 if Rlo
= No_Uint
then
7882 Bignum_Operands
:= True;
7884 Long_Long_Integer_Operands
:=
7885 Etype
(Then_DE
) = LLIB
or else Etype
(Else_DE
) = LLIB
;
7891 -- If at least one of our operands is now Bignum, we must rebuild
7892 -- the if expression to use Bignum operands. We will analyze the
7893 -- rebuilt if expression with overflow checks off, since once we
7894 -- are in bignum mode, we are all done with overflow checks.
7896 if Bignum_Operands
then
7898 Make_If_Expression
(Loc
,
7899 Expressions
=> New_List
(
7900 Remove_Head
(Expressions
(N
)),
7901 Convert_To_Bignum
(Then_DE
),
7902 Convert_To_Bignum
(Else_DE
)),
7903 Is_Elsif
=> Is_Elsif
(N
)));
7905 Reanalyze
(RTE
(RE_Bignum
), Suppress
=> True);
7907 -- If we have no Long_Long_Integer operands, then we are in result
7908 -- range, since it means that none of our operands felt the need
7909 -- to worry about overflow (otherwise it would have already been
7910 -- converted to long long integer or bignum). We reexpand to
7911 -- complete the expansion of the if expression (but we do not
7912 -- need to reanalyze).
7914 elsif not Long_Long_Integer_Operands
then
7915 Set_Do_Overflow_Check
(N
, False);
7918 -- Otherwise convert us to long long integer mode. Note that we
7919 -- don't need any further overflow checking at this level.
7922 Convert_To_And_Rewrite
(LLIB
, Then_DE
);
7923 Convert_To_And_Rewrite
(LLIB
, Else_DE
);
7924 Set_Etype
(N
, LLIB
);
7926 -- Now reanalyze with overflow checks off
7928 Set_Do_Overflow_Check
(N
, False);
7929 Reanalyze
(LLIB
, Suppress
=> True);
7935 -- Here for case expression
7937 elsif Nkind
(N
) = N_Case_Expression
then
7938 Bignum_Operands
:= False;
7939 Long_Long_Integer_Operands
:= False;
7945 -- Loop through expressions applying recursive call
7947 Alt
:= First
(Alternatives
(N
));
7948 while Present
(Alt
) loop
7950 Aexp
: constant Node_Id
:= Expression
(Alt
);
7953 Minimize_Eliminate_Overflows
7954 (Aexp
, Lo
, Hi
, Top_Level
=> False);
7956 if Lo
= No_Uint
then
7957 Bignum_Operands
:= True;
7958 elsif Etype
(Aexp
) = LLIB
then
7959 Long_Long_Integer_Operands
:= True;
7966 -- If we have no bignum or long long integer operands, it means
7967 -- that none of our dependent expressions could raise overflow.
7968 -- In this case, we simply return with no changes except for
7969 -- resetting the overflow flag, since we are done with overflow
7970 -- checks for this node. We will reexpand to get the needed
7971 -- expansion for the case expression, but we do not need to
7972 -- reanalyze, since nothing has changed.
7974 if not (Bignum_Operands
or Long_Long_Integer_Operands
) then
7975 Set_Do_Overflow_Check
(N
, False);
7976 Reexpand
(Suppress
=> True);
7978 -- Otherwise we are going to rebuild the case expression using
7979 -- either bignum or long long integer operands throughout.
7988 New_Alts
:= New_List
;
7989 Alt
:= First
(Alternatives
(N
));
7990 while Present
(Alt
) loop
7991 if Bignum_Operands
then
7992 New_Exp
:= Convert_To_Bignum
(Expression
(Alt
));
7993 Rtype
:= RTE
(RE_Bignum
);
7995 New_Exp
:= Convert_To
(LLIB
, Expression
(Alt
));
7999 Append_To
(New_Alts
,
8000 Make_Case_Expression_Alternative
(Sloc
(Alt
),
8002 Discrete_Choices
=> Discrete_Choices
(Alt
),
8003 Expression
=> New_Exp
));
8009 Make_Case_Expression
(Loc
,
8010 Expression
=> Expression
(N
),
8011 Alternatives
=> New_Alts
));
8013 Reanalyze
(Rtype
, Suppress
=> True);
8021 -- If we have an arithmetic operator we make recursive calls on the
8022 -- operands to get the ranges (and to properly process the subtree
8023 -- that lies below us).
8025 Minimize_Eliminate_Overflows
8026 (Right_Opnd
(N
), Rlo
, Rhi
, Top_Level
=> False);
8029 Minimize_Eliminate_Overflows
8030 (Left_Opnd
(N
), Llo
, Lhi
, Top_Level
=> False);
8033 -- Record if we have Long_Long_Integer operands
8035 Long_Long_Integer_Operands
:=
8036 Etype
(Right_Opnd
(N
)) = LLIB
8037 or else (Binary
and then Etype
(Left_Opnd
(N
)) = LLIB
);
8039 -- If either operand is a bignum, then result will be a bignum and we
8040 -- don't need to do any range analysis. As previously discussed we could
8041 -- do range analysis in such cases, but it could mean working with giant
8042 -- numbers at compile time for very little gain (the number of cases
8043 -- in which we could slip back from bignum mode is small).
8045 if Rlo
= No_Uint
or else (Binary
and then Llo
= No_Uint
) then
8048 Bignum_Operands
:= True;
8050 -- Otherwise compute result range
8053 Bignum_Operands
:= False;
8061 Hi
:= UI_Max
(abs Rlo
, abs Rhi
);
8073 -- If the right operand can only be zero, set 0..0
8075 if Rlo
= 0 and then Rhi
= 0 then
8079 -- Possible bounds of division must come from dividing end
8080 -- values of the input ranges (four possibilities), provided
8081 -- zero is not included in the possible values of the right
8084 -- Otherwise, we just consider two intervals of values for
8085 -- the right operand: the interval of negative values (up to
8086 -- -1) and the interval of positive values (starting at 1).
8087 -- Since division by 1 is the identity, and division by -1
8088 -- is negation, we get all possible bounds of division in that
8089 -- case by considering:
8090 -- - all values from the division of end values of input
8092 -- - the end values of the left operand;
8093 -- - the negation of the end values of the left operand.
8097 Mrk
: constant Uintp
.Save_Mark
:= Mark
;
8098 -- Mark so we can release the RR and Ev values
8106 -- Discard extreme values of zero for the divisor, since
8107 -- they will simply result in an exception in any case.
8115 -- Compute possible bounds coming from dividing end
8116 -- values of the input ranges.
8123 Lo
:= UI_Min
(UI_Min
(Ev1
, Ev2
), UI_Min
(Ev3
, Ev4
));
8124 Hi
:= UI_Max
(UI_Max
(Ev1
, Ev2
), UI_Max
(Ev3
, Ev4
));
8126 -- If the right operand can be both negative or positive,
8127 -- include the end values of the left operand in the
8128 -- extreme values, as well as their negation.
8130 if Rlo
< 0 and then Rhi
> 0 then
8137 UI_Min
(UI_Min
(Ev1
, Ev2
), UI_Min
(Ev3
, Ev4
)));
8139 UI_Max
(UI_Max
(Ev1
, Ev2
), UI_Max
(Ev3
, Ev4
)));
8142 -- Release the RR and Ev values
8144 Release_And_Save
(Mrk
, Lo
, Hi
);
8152 -- Discard negative values for the exponent, since they will
8153 -- simply result in an exception in any case.
8161 -- Estimate number of bits in result before we go computing
8162 -- giant useless bounds. Basically the number of bits in the
8163 -- result is the number of bits in the base multiplied by the
8164 -- value of the exponent. If this is big enough that the result
8165 -- definitely won't fit in Long_Long_Integer, switch to bignum
8166 -- mode immediately, and avoid computing giant bounds.
8168 -- The comparison here is approximate, but conservative, it
8169 -- only clicks on cases that are sure to exceed the bounds.
8171 if Num_Bits
(UI_Max
(abs Llo
, abs Lhi
)) * Rhi
+ 1 > 100 then
8175 -- If right operand is zero then result is 1
8182 -- High bound comes either from exponentiation of largest
8183 -- positive value to largest exponent value, or from
8184 -- the exponentiation of most negative value to an
8198 if Rhi
mod 2 = 0 then
8201 Hi2
:= Llo
** (Rhi
- 1);
8207 Hi
:= UI_Max
(Hi1
, Hi2
);
8210 -- Result can only be negative if base can be negative
8213 if Rhi
mod 2 = 0 then
8214 Lo
:= Llo
** (Rhi
- 1);
8219 -- Otherwise low bound is minimum ** minimum
8236 Maxabs
: constant Uint
:= UI_Max
(abs Rlo
, abs Rhi
) - 1;
8237 -- This is the maximum absolute value of the result
8243 -- The result depends only on the sign and magnitude of
8244 -- the right operand, it does not depend on the sign or
8245 -- magnitude of the left operand.
8258 when N_Op_Multiply
=>
8260 -- Possible bounds of multiplication must come from multiplying
8261 -- end values of the input ranges (four possibilities).
8264 Mrk
: constant Uintp
.Save_Mark
:= Mark
;
8265 -- Mark so we can release the Ev values
8267 Ev1
: constant Uint
:= Llo
* Rlo
;
8268 Ev2
: constant Uint
:= Llo
* Rhi
;
8269 Ev3
: constant Uint
:= Lhi
* Rlo
;
8270 Ev4
: constant Uint
:= Lhi
* Rhi
;
8273 Lo
:= UI_Min
(UI_Min
(Ev1
, Ev2
), UI_Min
(Ev3
, Ev4
));
8274 Hi
:= UI_Max
(UI_Max
(Ev1
, Ev2
), UI_Max
(Ev3
, Ev4
));
8276 -- Release the Ev values
8278 Release_And_Save
(Mrk
, Lo
, Hi
);
8281 -- Plus operator (affirmation)
8291 Maxabs
: constant Uint
:= UI_Max
(abs Rlo
, abs Rhi
) - 1;
8292 -- This is the maximum absolute value of the result. Note
8293 -- that the result range does not depend on the sign of the
8300 -- Case of left operand negative, which results in a range
8301 -- of -Maxabs .. 0 for those negative values. If there are
8302 -- no negative values then Lo value of result is always 0.
8308 -- Case of left operand positive
8317 when N_Op_Subtract
=>
8321 -- Nothing else should be possible
8324 raise Program_Error
;
8328 -- Here for the case where we have not rewritten anything (no bignum
8329 -- operands or long long integer operands), and we know the result.
8330 -- If we know we are in the result range, and we do not have Bignum
8331 -- operands or Long_Long_Integer operands, we can just reexpand with
8332 -- overflow checks turned off (since we know we cannot have overflow).
8333 -- As always the reexpansion is required to complete expansion of the
8334 -- operator, but we do not need to reanalyze, and we prevent recursion
8335 -- by suppressing the check.
8337 if not (Bignum_Operands
or Long_Long_Integer_Operands
)
8338 and then In_Result_Range
8340 Set_Do_Overflow_Check
(N
, False);
8341 Reexpand
(Suppress
=> True);
8344 -- Here we know that we are not in the result range, and in the general
8345 -- case we will move into either the Bignum or Long_Long_Integer domain
8346 -- to compute the result. However, there is one exception. If we are
8347 -- at the top level, and we do not have Bignum or Long_Long_Integer
8348 -- operands, we will have to immediately convert the result back to
8349 -- the result type, so there is no point in Bignum/Long_Long_Integer
8353 and then not (Bignum_Operands
or Long_Long_Integer_Operands
)
8355 -- One further refinement. If we are at the top level, but our parent
8356 -- is a type conversion, then go into bignum or long long integer node
8357 -- since the result will be converted to that type directly without
8358 -- going through the result type, and we may avoid an overflow. This
8359 -- is the case for example of Long_Long_Integer (A ** 4), where A is
8360 -- of type Integer, and the result A ** 4 fits in Long_Long_Integer
8361 -- but does not fit in Integer.
8363 and then Nkind
(Parent
(N
)) /= N_Type_Conversion
8365 -- Here keep original types, but we need to complete analysis
8367 -- One subtlety. We can't just go ahead and do an analyze operation
8368 -- here because it will cause recursion into the whole MINIMIZED/
8369 -- ELIMINATED overflow processing which is not what we want. Here
8370 -- we are at the top level, and we need a check against the result
8371 -- mode (i.e. we want to use STRICT mode). So do exactly that.
8372 -- Also, we have not modified the node, so this is a case where
8373 -- we need to reexpand, but not reanalyze.
8378 -- Cases where we do the operation in Bignum mode. This happens either
8379 -- because one of our operands is in Bignum mode already, or because
8380 -- the computed bounds are outside the bounds of Long_Long_Integer,
8381 -- which in some cases can be indicated by Hi and Lo being No_Uint.
8383 -- Note: we could do better here and in some cases switch back from
8384 -- Bignum mode to normal mode, e.g. big mod 2 must be in the range
8385 -- 0 .. 1, but the cases are rare and it is not worth the effort.
8386 -- Failing to do this switching back is only an efficiency issue.
8388 elsif Lo
= No_Uint
or else Lo
< LLLo
or else Hi
> LLHi
then
8390 -- OK, we are definitely outside the range of Long_Long_Integer. The
8391 -- question is whether to move to Bignum mode, or stay in the domain
8392 -- of Long_Long_Integer, signalling that an overflow check is needed.
8394 -- Obviously in MINIMIZED mode we stay with LLI, since we are not in
8395 -- the Bignum business. In ELIMINATED mode, we will normally move
8396 -- into Bignum mode, but there is an exception if neither of our
8397 -- operands is Bignum now, and we are at the top level (Top_Level
8398 -- set True). In this case, there is no point in moving into Bignum
8399 -- mode to prevent overflow if the caller will immediately convert
8400 -- the Bignum value back to LLI with an overflow check. It's more
8401 -- efficient to stay in LLI mode with an overflow check (if needed)
8403 if Check_Mode
= Minimized
8404 or else (Top_Level
and not Bignum_Operands
)
8406 if Do_Overflow_Check
(N
) then
8407 Enable_Overflow_Check
(N
);
8410 -- The result now has to be in Long_Long_Integer mode, so adjust
8411 -- the possible range to reflect this. Note these calls also
8412 -- change No_Uint values from the top level case to LLI bounds.
8417 -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode
8420 pragma Assert
(Check_Mode
= Eliminated
);
8429 Fent
:= RTE
(RE_Big_Abs
);
8432 Fent
:= RTE
(RE_Big_Add
);
8435 Fent
:= RTE
(RE_Big_Div
);
8438 Fent
:= RTE
(RE_Big_Exp
);
8441 Fent
:= RTE
(RE_Big_Neg
);
8444 Fent
:= RTE
(RE_Big_Mod
);
8446 when N_Op_Multiply
=>
8447 Fent
:= RTE
(RE_Big_Mul
);
8450 Fent
:= RTE
(RE_Big_Rem
);
8452 when N_Op_Subtract
=>
8453 Fent
:= RTE
(RE_Big_Sub
);
8455 -- Anything else is an internal error, this includes the
8456 -- N_Op_Plus case, since how can plus cause the result
8457 -- to be out of range if the operand is in range?
8460 raise Program_Error
;
8463 -- Construct argument list for Bignum call, converting our
8464 -- operands to Bignum form if they are not already there.
8469 Append_To
(Args
, Convert_To_Bignum
(Left_Opnd
(N
)));
8472 Append_To
(Args
, Convert_To_Bignum
(Right_Opnd
(N
)));
8474 -- Now rewrite the arithmetic operator with a call to the
8475 -- corresponding bignum function.
8478 Make_Function_Call
(Loc
,
8479 Name
=> New_Occurrence_Of
(Fent
, Loc
),
8480 Parameter_Associations
=> Args
));
8481 Reanalyze
(RTE
(RE_Bignum
), Suppress
=> True);
8483 -- Indicate result is Bignum mode
8491 -- Otherwise we are in range of Long_Long_Integer, so no overflow
8492 -- check is required, at least not yet.
8495 Set_Do_Overflow_Check
(N
, False);
8498 -- Here we are not in Bignum territory, but we may have long long
8499 -- integer operands that need special handling. First a special check:
8500 -- If an exponentiation operator exponent is of type Long_Long_Integer,
8501 -- it means we converted it to prevent overflow, but exponentiation
8502 -- requires a Natural right operand, so convert it back to Natural.
8503 -- This conversion may raise an exception which is fine.
8505 if Nkind
(N
) = N_Op_Expon
and then Etype
(Right_Opnd
(N
)) = LLIB
then
8506 Convert_To_And_Rewrite
(Standard_Natural
, Right_Opnd
(N
));
8509 -- Here we will do the operation in Long_Long_Integer. We do this even
8510 -- if we know an overflow check is required, better to do this in long
8511 -- long integer mode, since we are less likely to overflow.
8513 -- Convert right or only operand to Long_Long_Integer, except that
8514 -- we do not touch the exponentiation right operand.
8516 if Nkind
(N
) /= N_Op_Expon
then
8517 Convert_To_And_Rewrite
(LLIB
, Right_Opnd
(N
));
8520 -- Convert left operand to Long_Long_Integer for binary case
8523 Convert_To_And_Rewrite
(LLIB
, Left_Opnd
(N
));
8526 -- Reset node to unanalyzed
8528 Set_Analyzed
(N
, False);
8529 Set_Etype
(N
, Empty
);
8530 Set_Entity
(N
, Empty
);
8532 -- Now analyze this new node. This reanalysis will complete processing
8533 -- for the node. In particular we will complete the expansion of an
8534 -- exponentiation operator (e.g. changing A ** 2 to A * A), and also
8535 -- we will complete any division checks (since we have not changed the
8536 -- setting of the Do_Division_Check flag).
8538 -- We do this reanalysis in STRICT mode to avoid recursion into the
8539 -- MINIMIZED/ELIMINATED handling, since we are now done with that.
8542 SG
: constant Overflow_Mode_Type
:=
8543 Scope_Suppress
.Overflow_Mode_General
;
8544 SA
: constant Overflow_Mode_Type
:=
8545 Scope_Suppress
.Overflow_Mode_Assertions
;
8548 Scope_Suppress
.Overflow_Mode_General
:= Strict
;
8549 Scope_Suppress
.Overflow_Mode_Assertions
:= Strict
;
8551 if not Do_Overflow_Check
(N
) then
8552 Reanalyze
(LLIB
, Suppress
=> True);
8557 Scope_Suppress
.Overflow_Mode_General
:= SG
;
8558 Scope_Suppress
.Overflow_Mode_Assertions
:= SA
;
8560 end Minimize_Eliminate_Overflows
;
8562 -------------------------
8563 -- Overflow_Check_Mode --
8564 -------------------------
8566 function Overflow_Check_Mode
return Overflow_Mode_Type
is
8568 if In_Assertion_Expr
= 0 then
8569 return Scope_Suppress
.Overflow_Mode_General
;
8571 return Scope_Suppress
.Overflow_Mode_Assertions
;
8573 end Overflow_Check_Mode
;
8575 --------------------------------
8576 -- Overflow_Checks_Suppressed --
8577 --------------------------------
8579 function Overflow_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
8581 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
8582 return Is_Check_Suppressed
(E
, Overflow_Check
);
8584 return Scope_Suppress
.Suppress
(Overflow_Check
);
8586 end Overflow_Checks_Suppressed
;
8588 ---------------------------------
8589 -- Predicate_Checks_Suppressed --
8590 ---------------------------------
8592 function Predicate_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
8594 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
8595 return Is_Check_Suppressed
(E
, Predicate_Check
);
8597 return Scope_Suppress
.Suppress
(Predicate_Check
);
8599 end Predicate_Checks_Suppressed
;
8601 -----------------------------
8602 -- Range_Checks_Suppressed --
8603 -----------------------------
8605 function Range_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
8608 if Kill_Range_Checks
(E
) then
8611 elsif Checks_May_Be_Suppressed
(E
) then
8612 return Is_Check_Suppressed
(E
, Range_Check
);
8616 return Scope_Suppress
.Suppress
(Range_Check
);
8617 end Range_Checks_Suppressed
;
8619 -----------------------------------------
8620 -- Range_Or_Validity_Checks_Suppressed --
8621 -----------------------------------------
8623 -- Note: the coding would be simpler here if we simply made appropriate
8624 -- calls to Range/Validity_Checks_Suppressed, but that would result in
8625 -- duplicated checks which we prefer to avoid.
8627 function Range_Or_Validity_Checks_Suppressed
8628 (Expr
: Node_Id
) return Boolean
8631 -- Immediate return if scope checks suppressed for either check
8633 if Scope_Suppress
.Suppress
(Range_Check
)
8635 Scope_Suppress
.Suppress
(Validity_Check
)
8640 -- If no expression, that's odd, decide that checks are suppressed,
8641 -- since we don't want anyone trying to do checks in this case, which
8642 -- is most likely the result of some other error.
8648 -- Expression is present, so perform suppress checks on type
8651 Typ
: constant Entity_Id
:= Etype
(Expr
);
8653 if Checks_May_Be_Suppressed
(Typ
)
8654 and then (Is_Check_Suppressed
(Typ
, Range_Check
)
8656 Is_Check_Suppressed
(Typ
, Validity_Check
))
8662 -- If expression is an entity name, perform checks on this entity
8664 if Is_Entity_Name
(Expr
) then
8666 Ent
: constant Entity_Id
:= Entity
(Expr
);
8668 if Checks_May_Be_Suppressed
(Ent
) then
8669 return Is_Check_Suppressed
(Ent
, Range_Check
)
8670 or else Is_Check_Suppressed
(Ent
, Validity_Check
);
8675 -- If we fall through, no checks suppressed
8678 end Range_Or_Validity_Checks_Suppressed
;
8684 procedure Remove_Checks
(Expr
: Node_Id
) is
8685 function Process
(N
: Node_Id
) return Traverse_Result
;
8686 -- Process a single node during the traversal
8688 procedure Traverse
is new Traverse_Proc
(Process
);
8689 -- The traversal procedure itself
8695 function Process
(N
: Node_Id
) return Traverse_Result
is
8697 if Nkind
(N
) not in N_Subexpr
then
8701 Set_Do_Range_Check
(N
, False);
8705 Traverse
(Left_Opnd
(N
));
8708 when N_Attribute_Reference
=>
8709 Set_Do_Overflow_Check
(N
, False);
8711 when N_Function_Call
=>
8712 Set_Do_Tag_Check
(N
, False);
8715 Set_Do_Overflow_Check
(N
, False);
8719 Set_Do_Division_Check
(N
, False);
8722 Set_Do_Length_Check
(N
, False);
8725 Set_Do_Division_Check
(N
, False);
8728 Set_Do_Length_Check
(N
, False);
8731 Set_Do_Division_Check
(N
, False);
8734 Set_Do_Length_Check
(N
, False);
8741 Traverse
(Left_Opnd
(N
));
8744 when N_Selected_Component
=>
8745 Set_Do_Discriminant_Check
(N
, False);
8747 when N_Type_Conversion
=>
8748 Set_Do_Length_Check
(N
, False);
8749 Set_Do_Tag_Check
(N
, False);
8750 Set_Do_Overflow_Check
(N
, False);
8759 -- Start of processing for Remove_Checks
8765 ----------------------------
8766 -- Selected_Length_Checks --
8767 ----------------------------
8769 function Selected_Length_Checks
8771 Target_Typ
: Entity_Id
;
8772 Source_Typ
: Entity_Id
;
8773 Warn_Node
: Node_Id
) return Check_Result
8775 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
8778 Expr_Actual
: Node_Id
;
8780 Cond
: Node_Id
:= Empty
;
8781 Do_Access
: Boolean := False;
8782 Wnode
: Node_Id
:= Warn_Node
;
8783 Ret_Result
: Check_Result
:= (Empty
, Empty
);
8784 Num_Checks
: Natural := 0;
8786 procedure Add_Check
(N
: Node_Id
);
8787 -- Adds the action given to Ret_Result if N is non-Empty
8789 function Get_E_Length
(E
: Entity_Id
; Indx
: Nat
) return Node_Id
;
8790 function Get_N_Length
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
8791 -- Comments required ???
8793 function Same_Bounds
(L
: Node_Id
; R
: Node_Id
) return Boolean;
8794 -- True for equal literals and for nodes that denote the same constant
8795 -- entity, even if its value is not a static constant. This includes the
8796 -- case of a discriminal reference within an init proc. Removes some
8797 -- obviously superfluous checks.
8799 function Length_E_Cond
8800 (Exptyp
: Entity_Id
;
8802 Indx
: Nat
) return Node_Id
;
8803 -- Returns expression to compute:
8804 -- Typ'Length /= Exptyp'Length
8806 function Length_N_Cond
8809 Indx
: Nat
) return Node_Id
;
8810 -- Returns expression to compute:
8811 -- Typ'Length /= Expr'Length
8817 procedure Add_Check
(N
: Node_Id
) is
8821 -- For now, ignore attempt to place more than two checks ???
8822 -- This is really worrisome, are we really discarding checks ???
8824 if Num_Checks
= 2 then
8828 pragma Assert
(Num_Checks
<= 1);
8829 Num_Checks
:= Num_Checks
+ 1;
8830 Ret_Result
(Num_Checks
) := N
;
8838 function Get_E_Length
(E
: Entity_Id
; Indx
: Nat
) return Node_Id
is
8839 SE
: constant Entity_Id
:= Scope
(E
);
8841 E1
: Entity_Id
:= E
;
8844 if Ekind
(Scope
(E
)) = E_Record_Type
8845 and then Has_Discriminants
(Scope
(E
))
8847 N
:= Build_Discriminal_Subtype_Of_Component
(E
);
8850 Insert_Action
(Ck_Node
, N
);
8851 E1
:= Defining_Identifier
(N
);
8855 if Ekind
(E1
) = E_String_Literal_Subtype
then
8857 Make_Integer_Literal
(Loc
,
8858 Intval
=> String_Literal_Length
(E1
));
8860 elsif SE
/= Standard_Standard
8861 and then Ekind
(Scope
(SE
)) = E_Protected_Type
8862 and then Has_Discriminants
(Scope
(SE
))
8863 and then Has_Completion
(Scope
(SE
))
8864 and then not Inside_Init_Proc
8866 -- If the type whose length is needed is a private component
8867 -- constrained by a discriminant, we must expand the 'Length
8868 -- attribute into an explicit computation, using the discriminal
8869 -- of the current protected operation. This is because the actual
8870 -- type of the prival is constructed after the protected opera-
8871 -- tion has been fully expanded.
8874 Indx_Type
: Node_Id
;
8877 Do_Expand
: Boolean := False;
8880 Indx_Type
:= First_Index
(E
);
8882 for J
in 1 .. Indx
- 1 loop
8883 Next_Index
(Indx_Type
);
8886 Get_Index_Bounds
(Indx_Type
, Lo
, Hi
);
8888 if Nkind
(Lo
) = N_Identifier
8889 and then Ekind
(Entity
(Lo
)) = E_In_Parameter
8891 Lo
:= Get_Discriminal
(E
, Lo
);
8895 if Nkind
(Hi
) = N_Identifier
8896 and then Ekind
(Entity
(Hi
)) = E_In_Parameter
8898 Hi
:= Get_Discriminal
(E
, Hi
);
8903 if not Is_Entity_Name
(Lo
) then
8904 Lo
:= Duplicate_Subexpr_No_Checks
(Lo
);
8907 if not Is_Entity_Name
(Hi
) then
8908 Lo
:= Duplicate_Subexpr_No_Checks
(Hi
);
8914 Make_Op_Subtract
(Loc
,
8918 Right_Opnd
=> Make_Integer_Literal
(Loc
, 1));
8923 Make_Attribute_Reference
(Loc
,
8924 Attribute_Name
=> Name_Length
,
8926 New_Occurrence_Of
(E1
, Loc
));
8929 Set_Expressions
(N
, New_List
(
8930 Make_Integer_Literal
(Loc
, Indx
)));
8939 Make_Attribute_Reference
(Loc
,
8940 Attribute_Name
=> Name_Length
,
8942 New_Occurrence_Of
(E1
, Loc
));
8945 Set_Expressions
(N
, New_List
(
8946 Make_Integer_Literal
(Loc
, Indx
)));
8957 function Get_N_Length
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
8960 Make_Attribute_Reference
(Loc
,
8961 Attribute_Name
=> Name_Length
,
8963 Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True),
8964 Expressions
=> New_List
(
8965 Make_Integer_Literal
(Loc
, Indx
)));
8972 function Length_E_Cond
8973 (Exptyp
: Entity_Id
;
8975 Indx
: Nat
) return Node_Id
8980 Left_Opnd
=> Get_E_Length
(Typ
, Indx
),
8981 Right_Opnd
=> Get_E_Length
(Exptyp
, Indx
));
8988 function Length_N_Cond
8991 Indx
: Nat
) return Node_Id
8996 Left_Opnd
=> Get_E_Length
(Typ
, Indx
),
8997 Right_Opnd
=> Get_N_Length
(Expr
, Indx
));
9004 function Same_Bounds
(L
: Node_Id
; R
: Node_Id
) return Boolean is
9007 (Nkind
(L
) = N_Integer_Literal
9008 and then Nkind
(R
) = N_Integer_Literal
9009 and then Intval
(L
) = Intval
(R
))
9013 and then Ekind
(Entity
(L
)) = E_Constant
9014 and then ((Is_Entity_Name
(R
)
9015 and then Entity
(L
) = Entity
(R
))
9017 (Nkind
(R
) = N_Type_Conversion
9018 and then Is_Entity_Name
(Expression
(R
))
9019 and then Entity
(L
) = Entity
(Expression
(R
)))))
9023 and then Ekind
(Entity
(R
)) = E_Constant
9024 and then Nkind
(L
) = N_Type_Conversion
9025 and then Is_Entity_Name
(Expression
(L
))
9026 and then Entity
(R
) = Entity
(Expression
(L
)))
9030 and then Is_Entity_Name
(R
)
9031 and then Entity
(L
) = Entity
(R
)
9032 and then Ekind
(Entity
(L
)) = E_In_Parameter
9033 and then Inside_Init_Proc
);
9036 -- Start of processing for Selected_Length_Checks
9039 if not Expander_Active
then
9043 if Target_Typ
= Any_Type
9044 or else Target_Typ
= Any_Composite
9045 or else Raises_Constraint_Error
(Ck_Node
)
9054 T_Typ
:= Target_Typ
;
9056 if No
(Source_Typ
) then
9057 S_Typ
:= Etype
(Ck_Node
);
9059 S_Typ
:= Source_Typ
;
9062 if S_Typ
= Any_Type
or else S_Typ
= Any_Composite
then
9066 if Is_Access_Type
(T_Typ
) and then Is_Access_Type
(S_Typ
) then
9067 S_Typ
:= Designated_Type
(S_Typ
);
9068 T_Typ
:= Designated_Type
(T_Typ
);
9071 -- A simple optimization for the null case
9073 if Known_Null
(Ck_Node
) then
9078 if Is_Array_Type
(T_Typ
) and then Is_Array_Type
(S_Typ
) then
9079 if Is_Constrained
(T_Typ
) then
9081 -- The checking code to be generated will freeze the corresponding
9082 -- array type. However, we must freeze the type now, so that the
9083 -- freeze node does not appear within the generated if expression,
9086 Freeze_Before
(Ck_Node
, T_Typ
);
9088 Expr_Actual
:= Get_Referenced_Object
(Ck_Node
);
9089 Exptyp
:= Get_Actual_Subtype
(Ck_Node
);
9091 if Is_Access_Type
(Exptyp
) then
9092 Exptyp
:= Designated_Type
(Exptyp
);
9095 -- String_Literal case. This needs to be handled specially be-
9096 -- cause no index types are available for string literals. The
9097 -- condition is simply:
9099 -- T_Typ'Length = string-literal-length
9101 if Nkind
(Expr_Actual
) = N_String_Literal
9102 and then Ekind
(Etype
(Expr_Actual
)) = E_String_Literal_Subtype
9106 Left_Opnd
=> Get_E_Length
(T_Typ
, 1),
9108 Make_Integer_Literal
(Loc
,
9110 String_Literal_Length
(Etype
(Expr_Actual
))));
9112 -- General array case. Here we have a usable actual subtype for
9113 -- the expression, and the condition is built from the two types
9116 -- T_Typ'Length /= Exptyp'Length or else
9117 -- T_Typ'Length (2) /= Exptyp'Length (2) or else
9118 -- T_Typ'Length (3) /= Exptyp'Length (3) or else
9121 elsif Is_Constrained
(Exptyp
) then
9123 Ndims
: constant Nat
:= Number_Dimensions
(T_Typ
);
9136 -- At the library level, we need to ensure that the type of
9137 -- the object is elaborated before the check itself is
9138 -- emitted. This is only done if the object is in the
9139 -- current compilation unit, otherwise the type is frozen
9140 -- and elaborated in its unit.
9142 if Is_Itype
(Exptyp
)
9144 Ekind
(Cunit_Entity
(Current_Sem_Unit
)) = E_Package
9146 not In_Package_Body
(Cunit_Entity
(Current_Sem_Unit
))
9147 and then In_Open_Scopes
(Scope
(Exptyp
))
9149 Ref_Node
:= Make_Itype_Reference
(Sloc
(Ck_Node
));
9150 Set_Itype
(Ref_Node
, Exptyp
);
9151 Insert_Action
(Ck_Node
, Ref_Node
);
9154 L_Index
:= First_Index
(T_Typ
);
9155 R_Index
:= First_Index
(Exptyp
);
9157 for Indx
in 1 .. Ndims
loop
9158 if not (Nkind
(L_Index
) = N_Raise_Constraint_Error
9160 Nkind
(R_Index
) = N_Raise_Constraint_Error
)
9162 Get_Index_Bounds
(L_Index
, L_Low
, L_High
);
9163 Get_Index_Bounds
(R_Index
, R_Low
, R_High
);
9165 -- Deal with compile time length check. Note that we
9166 -- skip this in the access case, because the access
9167 -- value may be null, so we cannot know statically.
9170 and then Compile_Time_Known_Value
(L_Low
)
9171 and then Compile_Time_Known_Value
(L_High
)
9172 and then Compile_Time_Known_Value
(R_Low
)
9173 and then Compile_Time_Known_Value
(R_High
)
9175 if Expr_Value
(L_High
) >= Expr_Value
(L_Low
) then
9176 L_Length
:= Expr_Value
(L_High
) -
9177 Expr_Value
(L_Low
) + 1;
9179 L_Length
:= UI_From_Int
(0);
9182 if Expr_Value
(R_High
) >= Expr_Value
(R_Low
) then
9183 R_Length
:= Expr_Value
(R_High
) -
9184 Expr_Value
(R_Low
) + 1;
9186 R_Length
:= UI_From_Int
(0);
9189 if L_Length
> R_Length
then
9191 (Compile_Time_Constraint_Error
9192 (Wnode
, "too few elements for}??", T_Typ
));
9194 elsif L_Length
< R_Length
then
9196 (Compile_Time_Constraint_Error
9197 (Wnode
, "too many elements for}??", T_Typ
));
9200 -- The comparison for an individual index subtype
9201 -- is omitted if the corresponding index subtypes
9202 -- statically match, since the result is known to
9203 -- be true. Note that this test is worth while even
9204 -- though we do static evaluation, because non-static
9205 -- subtypes can statically match.
9208 Subtypes_Statically_Match
9209 (Etype
(L_Index
), Etype
(R_Index
))
9212 (Same_Bounds
(L_Low
, R_Low
)
9213 and then Same_Bounds
(L_High
, R_High
))
9216 (Cond
, Length_E_Cond
(Exptyp
, T_Typ
, Indx
));
9225 -- Handle cases where we do not get a usable actual subtype that
9226 -- is constrained. This happens for example in the function call
9227 -- and explicit dereference cases. In these cases, we have to get
9228 -- the length or range from the expression itself, making sure we
9229 -- do not evaluate it more than once.
9231 -- Here Ck_Node is the original expression, or more properly the
9232 -- result of applying Duplicate_Expr to the original tree, forcing
9233 -- the result to be a name.
9237 Ndims
: constant Nat
:= Number_Dimensions
(T_Typ
);
9240 -- Build the condition for the explicit dereference case
9242 for Indx
in 1 .. Ndims
loop
9244 (Cond
, Length_N_Cond
(Ck_Node
, T_Typ
, Indx
));
9251 -- Construct the test and insert into the tree
9253 if Present
(Cond
) then
9255 Cond
:= Guard_Access
(Cond
, Loc
, Ck_Node
);
9259 (Make_Raise_Constraint_Error
(Loc
,
9261 Reason
=> CE_Length_Check_Failed
));
9265 end Selected_Length_Checks
;
9267 ---------------------------
9268 -- Selected_Range_Checks --
9269 ---------------------------
9271 function Selected_Range_Checks
9273 Target_Typ
: Entity_Id
;
9274 Source_Typ
: Entity_Id
;
9275 Warn_Node
: Node_Id
) return Check_Result
9277 Loc
: constant Source_Ptr
:= Sloc
(Ck_Node
);
9280 Expr_Actual
: Node_Id
;
9282 Cond
: Node_Id
:= Empty
;
9283 Do_Access
: Boolean := False;
9284 Wnode
: Node_Id
:= Warn_Node
;
9285 Ret_Result
: Check_Result
:= (Empty
, Empty
);
9286 Num_Checks
: Integer := 0;
9288 procedure Add_Check
(N
: Node_Id
);
9289 -- Adds the action given to Ret_Result if N is non-Empty
9291 function Discrete_Range_Cond
9293 Typ
: Entity_Id
) return Node_Id
;
9294 -- Returns expression to compute:
9295 -- Low_Bound (Expr) < Typ'First
9297 -- High_Bound (Expr) > Typ'Last
9299 function Discrete_Expr_Cond
9301 Typ
: Entity_Id
) return Node_Id
;
9302 -- Returns expression to compute:
9307 function Get_E_First_Or_Last
9311 Nam
: Name_Id
) return Node_Id
;
9312 -- Returns an attribute reference
9313 -- E'First or E'Last
9314 -- with a source location of Loc.
9316 -- Nam is Name_First or Name_Last, according to which attribute is
9317 -- desired. If Indx is non-zero, it is passed as a literal in the
9318 -- Expressions of the attribute reference (identifying the desired
9319 -- array dimension).
9321 function Get_N_First
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
9322 function Get_N_Last
(N
: Node_Id
; Indx
: Nat
) return Node_Id
;
9323 -- Returns expression to compute:
9324 -- N'First or N'Last using Duplicate_Subexpr_No_Checks
9326 function Range_E_Cond
9327 (Exptyp
: Entity_Id
;
9331 -- Returns expression to compute:
9332 -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
9334 function Range_Equal_E_Cond
9335 (Exptyp
: Entity_Id
;
9337 Indx
: Nat
) return Node_Id
;
9338 -- Returns expression to compute:
9339 -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
9341 function Range_N_Cond
9344 Indx
: Nat
) return Node_Id
;
9345 -- Return expression to compute:
9346 -- Expr'First < Typ'First or else Expr'Last > Typ'Last
9352 procedure Add_Check
(N
: Node_Id
) is
9356 -- For now, ignore attempt to place more than 2 checks ???
9358 if Num_Checks
= 2 then
9362 pragma Assert
(Num_Checks
<= 1);
9363 Num_Checks
:= Num_Checks
+ 1;
9364 Ret_Result
(Num_Checks
) := N
;
9368 -------------------------
9369 -- Discrete_Expr_Cond --
9370 -------------------------
9372 function Discrete_Expr_Cond
9374 Typ
: Entity_Id
) return Node_Id
9382 Convert_To
(Base_Type
(Typ
),
9383 Duplicate_Subexpr_No_Checks
(Expr
)),
9385 Convert_To
(Base_Type
(Typ
),
9386 Get_E_First_Or_Last
(Loc
, Typ
, 0, Name_First
))),
9391 Convert_To
(Base_Type
(Typ
),
9392 Duplicate_Subexpr_No_Checks
(Expr
)),
9396 Get_E_First_Or_Last
(Loc
, Typ
, 0, Name_Last
))));
9397 end Discrete_Expr_Cond
;
9399 -------------------------
9400 -- Discrete_Range_Cond --
9401 -------------------------
9403 function Discrete_Range_Cond
9405 Typ
: Entity_Id
) return Node_Id
9407 LB
: Node_Id
:= Low_Bound
(Expr
);
9408 HB
: Node_Id
:= High_Bound
(Expr
);
9410 Left_Opnd
: Node_Id
;
9411 Right_Opnd
: Node_Id
;
9414 if Nkind
(LB
) = N_Identifier
9415 and then Ekind
(Entity
(LB
)) = E_Discriminant
9417 LB
:= New_Occurrence_Of
(Discriminal
(Entity
(LB
)), Loc
);
9424 (Base_Type
(Typ
), Duplicate_Subexpr_No_Checks
(LB
)),
9429 Get_E_First_Or_Last
(Loc
, Typ
, 0, Name_First
)));
9431 if Nkind
(HB
) = N_Identifier
9432 and then Ekind
(Entity
(HB
)) = E_Discriminant
9434 HB
:= New_Occurrence_Of
(Discriminal
(Entity
(HB
)), Loc
);
9441 (Base_Type
(Typ
), Duplicate_Subexpr_No_Checks
(HB
)),
9446 Get_E_First_Or_Last
(Loc
, Typ
, 0, Name_Last
)));
9448 return Make_Or_Else
(Loc
, Left_Opnd
, Right_Opnd
);
9449 end Discrete_Range_Cond
;
9451 -------------------------
9452 -- Get_E_First_Or_Last --
9453 -------------------------
9455 function Get_E_First_Or_Last
9459 Nam
: Name_Id
) return Node_Id
9464 Exprs
:= New_List
(Make_Integer_Literal
(Loc
, UI_From_Int
(Indx
)));
9469 return Make_Attribute_Reference
(Loc
,
9470 Prefix
=> New_Occurrence_Of
(E
, Loc
),
9471 Attribute_Name
=> Nam
,
9472 Expressions
=> Exprs
);
9473 end Get_E_First_Or_Last
;
9479 function Get_N_First
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
9482 Make_Attribute_Reference
(Loc
,
9483 Attribute_Name
=> Name_First
,
9485 Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True),
9486 Expressions
=> New_List
(
9487 Make_Integer_Literal
(Loc
, Indx
)));
9494 function Get_N_Last
(N
: Node_Id
; Indx
: Nat
) return Node_Id
is
9497 Make_Attribute_Reference
(Loc
,
9498 Attribute_Name
=> Name_Last
,
9500 Duplicate_Subexpr_No_Checks
(N
, Name_Req
=> True),
9501 Expressions
=> New_List
(
9502 Make_Integer_Literal
(Loc
, Indx
)));
9509 function Range_E_Cond
9510 (Exptyp
: Entity_Id
;
9512 Indx
: Nat
) return Node_Id
9520 Get_E_First_Or_Last
(Loc
, Exptyp
, Indx
, Name_First
),
9522 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_First
)),
9527 Get_E_First_Or_Last
(Loc
, Exptyp
, Indx
, Name_Last
),
9529 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_Last
)));
9532 ------------------------
9533 -- Range_Equal_E_Cond --
9534 ------------------------
9536 function Range_Equal_E_Cond
9537 (Exptyp
: Entity_Id
;
9539 Indx
: Nat
) return Node_Id
9547 Get_E_First_Or_Last
(Loc
, Exptyp
, Indx
, Name_First
),
9549 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_First
)),
9554 Get_E_First_Or_Last
(Loc
, Exptyp
, Indx
, Name_Last
),
9556 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_Last
)));
9557 end Range_Equal_E_Cond
;
9563 function Range_N_Cond
9566 Indx
: Nat
) return Node_Id
9574 Get_N_First
(Expr
, Indx
),
9576 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_First
)),
9581 Get_N_Last
(Expr
, Indx
),
9583 Get_E_First_Or_Last
(Loc
, Typ
, Indx
, Name_Last
)));
9586 -- Start of processing for Selected_Range_Checks
9589 if not Expander_Active
then
9593 if Target_Typ
= Any_Type
9594 or else Target_Typ
= Any_Composite
9595 or else Raises_Constraint_Error
(Ck_Node
)
9604 T_Typ
:= Target_Typ
;
9606 if No
(Source_Typ
) then
9607 S_Typ
:= Etype
(Ck_Node
);
9609 S_Typ
:= Source_Typ
;
9612 if S_Typ
= Any_Type
or else S_Typ
= Any_Composite
then
9616 -- The order of evaluating T_Typ before S_Typ seems to be critical
9617 -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
9618 -- in, and since Node can be an N_Range node, it might be invalid.
9619 -- Should there be an assert check somewhere for taking the Etype of
9620 -- an N_Range node ???
9622 if Is_Access_Type
(T_Typ
) and then Is_Access_Type
(S_Typ
) then
9623 S_Typ
:= Designated_Type
(S_Typ
);
9624 T_Typ
:= Designated_Type
(T_Typ
);
9627 -- A simple optimization for the null case
9629 if Known_Null
(Ck_Node
) then
9634 -- For an N_Range Node, check for a null range and then if not
9635 -- null generate a range check action.
9637 if Nkind
(Ck_Node
) = N_Range
then
9639 -- There's no point in checking a range against itself
9641 if Ck_Node
= Scalar_Range
(T_Typ
) then
9646 T_LB
: constant Node_Id
:= Type_Low_Bound
(T_Typ
);
9647 T_HB
: constant Node_Id
:= Type_High_Bound
(T_Typ
);
9648 Known_T_LB
: constant Boolean := Compile_Time_Known_Value
(T_LB
);
9649 Known_T_HB
: constant Boolean := Compile_Time_Known_Value
(T_HB
);
9651 LB
: Node_Id
:= Low_Bound
(Ck_Node
);
9652 HB
: Node_Id
:= High_Bound
(Ck_Node
);
9656 Null_Range
: Boolean;
9657 Out_Of_Range_L
: Boolean;
9658 Out_Of_Range_H
: Boolean;
9661 -- Compute what is known at compile time
9663 if Known_T_LB
and Known_T_HB
then
9664 if Compile_Time_Known_Value
(LB
) then
9667 -- There's no point in checking that a bound is within its
9668 -- own range so pretend that it is known in this case. First
9669 -- deal with low bound.
9671 elsif Ekind
(Etype
(LB
)) = E_Signed_Integer_Subtype
9672 and then Scalar_Range
(Etype
(LB
)) = Scalar_Range
(T_Typ
)
9681 -- Likewise for the high bound
9683 if Compile_Time_Known_Value
(HB
) then
9686 elsif Ekind
(Etype
(HB
)) = E_Signed_Integer_Subtype
9687 and then Scalar_Range
(Etype
(HB
)) = Scalar_Range
(T_Typ
)
9696 -- Check for case where everything is static and we can do the
9697 -- check at compile time. This is skipped if we have an access
9698 -- type, since the access value may be null.
9700 -- ??? This code can be improved since you only need to know that
9701 -- the two respective bounds (LB & T_LB or HB & T_HB) are known at
9702 -- compile time to emit pertinent messages.
9704 if Known_T_LB
and Known_T_HB
and Known_LB
and Known_HB
9707 -- Floating-point case
9709 if Is_Floating_Point_Type
(S_Typ
) then
9710 Null_Range
:= Expr_Value_R
(HB
) < Expr_Value_R
(LB
);
9712 (Expr_Value_R
(LB
) < Expr_Value_R
(T_LB
))
9714 (Expr_Value_R
(LB
) > Expr_Value_R
(T_HB
));
9717 (Expr_Value_R
(HB
) > Expr_Value_R
(T_HB
))
9719 (Expr_Value_R
(HB
) < Expr_Value_R
(T_LB
));
9721 -- Fixed or discrete type case
9724 Null_Range
:= Expr_Value
(HB
) < Expr_Value
(LB
);
9726 (Expr_Value
(LB
) < Expr_Value
(T_LB
))
9728 (Expr_Value
(LB
) > Expr_Value
(T_HB
));
9731 (Expr_Value
(HB
) > Expr_Value
(T_HB
))
9733 (Expr_Value
(HB
) < Expr_Value
(T_LB
));
9736 if not Null_Range
then
9737 if Out_Of_Range_L
then
9738 if No
(Warn_Node
) then
9740 (Compile_Time_Constraint_Error
9741 (Low_Bound
(Ck_Node
),
9742 "static value out of range of}??", T_Typ
));
9746 (Compile_Time_Constraint_Error
9748 "static range out of bounds of}??", T_Typ
));
9752 if Out_Of_Range_H
then
9753 if No
(Warn_Node
) then
9755 (Compile_Time_Constraint_Error
9756 (High_Bound
(Ck_Node
),
9757 "static value out of range of}??", T_Typ
));
9761 (Compile_Time_Constraint_Error
9763 "static range out of bounds of}??", T_Typ
));
9770 LB
: Node_Id
:= Low_Bound
(Ck_Node
);
9771 HB
: Node_Id
:= High_Bound
(Ck_Node
);
9774 -- If either bound is a discriminant and we are within the
9775 -- record declaration, it is a use of the discriminant in a
9776 -- constraint of a component, and nothing can be checked
9777 -- here. The check will be emitted within the init proc.
9778 -- Before then, the discriminal has no real meaning.
9779 -- Similarly, if the entity is a discriminal, there is no
9780 -- check to perform yet.
9782 -- The same holds within a discriminated synchronized type,
9783 -- where the discriminant may constrain a component or an
9786 if Nkind
(LB
) = N_Identifier
9787 and then Denotes_Discriminant
(LB
, True)
9789 if Current_Scope
= Scope
(Entity
(LB
))
9790 or else Is_Concurrent_Type
(Current_Scope
)
9791 or else Ekind
(Entity
(LB
)) /= E_Discriminant
9796 New_Occurrence_Of
(Discriminal
(Entity
(LB
)), Loc
);
9800 if Nkind
(HB
) = N_Identifier
9801 and then Denotes_Discriminant
(HB
, True)
9803 if Current_Scope
= Scope
(Entity
(HB
))
9804 or else Is_Concurrent_Type
(Current_Scope
)
9805 or else Ekind
(Entity
(HB
)) /= E_Discriminant
9810 New_Occurrence_Of
(Discriminal
(Entity
(HB
)), Loc
);
9814 Cond
:= Discrete_Range_Cond
(Ck_Node
, T_Typ
);
9815 Set_Paren_Count
(Cond
, 1);
9822 Convert_To
(Base_Type
(Etype
(HB
)),
9823 Duplicate_Subexpr_No_Checks
(HB
)),
9825 Convert_To
(Base_Type
(Etype
(LB
)),
9826 Duplicate_Subexpr_No_Checks
(LB
))),
9827 Right_Opnd
=> Cond
);
9832 elsif Is_Scalar_Type
(S_Typ
) then
9834 -- This somewhat duplicates what Apply_Scalar_Range_Check does,
9835 -- except the above simply sets a flag in the node and lets
9836 -- gigi generate the check base on the Etype of the expression.
9837 -- Sometimes, however we want to do a dynamic check against an
9838 -- arbitrary target type, so we do that here.
9840 if Ekind
(Base_Type
(S_Typ
)) /= Ekind
(Base_Type
(T_Typ
)) then
9841 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
9843 -- For literals, we can tell if the constraint error will be
9844 -- raised at compile time, so we never need a dynamic check, but
9845 -- if the exception will be raised, then post the usual warning,
9846 -- and replace the literal with a raise constraint error
9847 -- expression. As usual, skip this for access types
9849 elsif Compile_Time_Known_Value
(Ck_Node
) and then not Do_Access
then
9851 LB
: constant Node_Id
:= Type_Low_Bound
(T_Typ
);
9852 UB
: constant Node_Id
:= Type_High_Bound
(T_Typ
);
9854 Out_Of_Range
: Boolean;
9855 Static_Bounds
: constant Boolean :=
9856 Compile_Time_Known_Value
(LB
)
9857 and Compile_Time_Known_Value
(UB
);
9860 -- Following range tests should use Sem_Eval routine ???
9862 if Static_Bounds
then
9863 if Is_Floating_Point_Type
(S_Typ
) then
9865 (Expr_Value_R
(Ck_Node
) < Expr_Value_R
(LB
))
9867 (Expr_Value_R
(Ck_Node
) > Expr_Value_R
(UB
));
9869 -- Fixed or discrete type
9873 Expr_Value
(Ck_Node
) < Expr_Value
(LB
)
9875 Expr_Value
(Ck_Node
) > Expr_Value
(UB
);
9878 -- Bounds of the type are static and the literal is out of
9879 -- range so output a warning message.
9881 if Out_Of_Range
then
9882 if No
(Warn_Node
) then
9884 (Compile_Time_Constraint_Error
9886 "static value out of range of}??", T_Typ
));
9890 (Compile_Time_Constraint_Error
9892 "static value out of range of}??", T_Typ
));
9897 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
9901 -- Here for the case of a non-static expression, we need a runtime
9902 -- check unless the source type range is guaranteed to be in the
9903 -- range of the target type.
9906 if not In_Subrange_Of
(S_Typ
, T_Typ
) then
9907 Cond
:= Discrete_Expr_Cond
(Ck_Node
, T_Typ
);
9912 if Is_Array_Type
(T_Typ
) and then Is_Array_Type
(S_Typ
) then
9913 if Is_Constrained
(T_Typ
) then
9915 Expr_Actual
:= Get_Referenced_Object
(Ck_Node
);
9916 Exptyp
:= Get_Actual_Subtype
(Expr_Actual
);
9918 if Is_Access_Type
(Exptyp
) then
9919 Exptyp
:= Designated_Type
(Exptyp
);
9922 -- String_Literal case. This needs to be handled specially be-
9923 -- cause no index types are available for string literals. The
9924 -- condition is simply:
9926 -- T_Typ'Length = string-literal-length
9928 if Nkind
(Expr_Actual
) = N_String_Literal
then
9931 -- General array case. Here we have a usable actual subtype for
9932 -- the expression, and the condition is built from the two types
9934 -- T_Typ'First < Exptyp'First or else
9935 -- T_Typ'Last > Exptyp'Last or else
9936 -- T_Typ'First(1) < Exptyp'First(1) or else
9937 -- T_Typ'Last(1) > Exptyp'Last(1) or else
9940 elsif Is_Constrained
(Exptyp
) then
9942 Ndims
: constant Nat
:= Number_Dimensions
(T_Typ
);
9948 L_Index
:= First_Index
(T_Typ
);
9949 R_Index
:= First_Index
(Exptyp
);
9951 for Indx
in 1 .. Ndims
loop
9952 if not (Nkind
(L_Index
) = N_Raise_Constraint_Error
9954 Nkind
(R_Index
) = N_Raise_Constraint_Error
)
9956 -- Deal with compile time length check. Note that we
9957 -- skip this in the access case, because the access
9958 -- value may be null, so we cannot know statically.
9961 Subtypes_Statically_Match
9962 (Etype
(L_Index
), Etype
(R_Index
))
9964 -- If the target type is constrained then we
9965 -- have to check for exact equality of bounds
9966 -- (required for qualified expressions).
9968 if Is_Constrained
(T_Typ
) then
9971 Range_Equal_E_Cond
(Exptyp
, T_Typ
, Indx
));
9974 (Cond
, Range_E_Cond
(Exptyp
, T_Typ
, Indx
));
9984 -- Handle cases where we do not get a usable actual subtype that
9985 -- is constrained. This happens for example in the function call
9986 -- and explicit dereference cases. In these cases, we have to get
9987 -- the length or range from the expression itself, making sure we
9988 -- do not evaluate it more than once.
9990 -- Here Ck_Node is the original expression, or more properly the
9991 -- result of applying Duplicate_Expr to the original tree,
9992 -- forcing the result to be a name.
9996 Ndims
: constant Nat
:= Number_Dimensions
(T_Typ
);
9999 -- Build the condition for the explicit dereference case
10001 for Indx
in 1 .. Ndims
loop
10003 (Cond
, Range_N_Cond
(Ck_Node
, T_Typ
, Indx
));
10009 -- For a conversion to an unconstrained array type, generate an
10010 -- Action to check that the bounds of the source value are within
10011 -- the constraints imposed by the target type (RM 4.6(38)). No
10012 -- check is needed for a conversion to an access to unconstrained
10013 -- array type, as 4.6(24.15/2) requires the designated subtypes
10014 -- of the two access types to statically match.
10016 if Nkind
(Parent
(Ck_Node
)) = N_Type_Conversion
10017 and then not Do_Access
10020 Opnd_Index
: Node_Id
;
10021 Targ_Index
: Node_Id
;
10022 Opnd_Range
: Node_Id
;
10025 Opnd_Index
:= First_Index
(Get_Actual_Subtype
(Ck_Node
));
10026 Targ_Index
:= First_Index
(T_Typ
);
10027 while Present
(Opnd_Index
) loop
10029 -- If the index is a range, use its bounds. If it is an
10030 -- entity (as will be the case if it is a named subtype
10031 -- or an itype created for a slice) retrieve its range.
10033 if Is_Entity_Name
(Opnd_Index
)
10034 and then Is_Type
(Entity
(Opnd_Index
))
10036 Opnd_Range
:= Scalar_Range
(Entity
(Opnd_Index
));
10038 Opnd_Range
:= Opnd_Index
;
10041 if Nkind
(Opnd_Range
) = N_Range
then
10043 (Low_Bound
(Opnd_Range
), Etype
(Targ_Index
),
10044 Assume_Valid
=> True)
10047 (High_Bound
(Opnd_Range
), Etype
(Targ_Index
),
10048 Assume_Valid
=> True)
10052 -- If null range, no check needed
10055 Compile_Time_Known_Value
(High_Bound
(Opnd_Range
))
10057 Compile_Time_Known_Value
(Low_Bound
(Opnd_Range
))
10059 Expr_Value
(High_Bound
(Opnd_Range
)) <
10060 Expr_Value
(Low_Bound
(Opnd_Range
))
10064 elsif Is_Out_Of_Range
10065 (Low_Bound
(Opnd_Range
), Etype
(Targ_Index
),
10066 Assume_Valid
=> True)
10069 (High_Bound
(Opnd_Range
), Etype
(Targ_Index
),
10070 Assume_Valid
=> True)
10073 (Compile_Time_Constraint_Error
10074 (Wnode
, "value out of range of}??", T_Typ
));
10079 Discrete_Range_Cond
10080 (Opnd_Range
, Etype
(Targ_Index
)));
10084 Next_Index
(Opnd_Index
);
10085 Next_Index
(Targ_Index
);
10092 -- Construct the test and insert into the tree
10094 if Present
(Cond
) then
10096 Cond
:= Guard_Access
(Cond
, Loc
, Ck_Node
);
10100 (Make_Raise_Constraint_Error
(Loc
,
10102 Reason
=> CE_Range_Check_Failed
));
10106 end Selected_Range_Checks
;
10108 -------------------------------
10109 -- Storage_Checks_Suppressed --
10110 -------------------------------
10112 function Storage_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
10114 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
10115 return Is_Check_Suppressed
(E
, Storage_Check
);
10117 return Scope_Suppress
.Suppress
(Storage_Check
);
10119 end Storage_Checks_Suppressed
;
10121 ---------------------------
10122 -- Tag_Checks_Suppressed --
10123 ---------------------------
10125 function Tag_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
10128 and then Checks_May_Be_Suppressed
(E
)
10130 return Is_Check_Suppressed
(E
, Tag_Check
);
10132 return Scope_Suppress
.Suppress
(Tag_Check
);
10134 end Tag_Checks_Suppressed
;
10136 ---------------------------------------
10137 -- Validate_Alignment_Check_Warnings --
10138 ---------------------------------------
10140 procedure Validate_Alignment_Check_Warnings
is
10142 for J
in Alignment_Warnings
.First
.. Alignment_Warnings
.Last
loop
10144 AWR
: Alignment_Warnings_Record
10145 renames Alignment_Warnings
.Table
(J
);
10147 if Known_Alignment
(AWR
.E
)
10148 and then AWR
.A
mod Alignment
(AWR
.E
) = 0
10150 Delete_Warning_And_Continuations
(AWR
.W
);
10154 end Validate_Alignment_Check_Warnings
;
10156 --------------------------
10157 -- Validity_Check_Range --
10158 --------------------------
10160 procedure Validity_Check_Range
10162 Related_Id
: Entity_Id
:= Empty
)
10165 if Validity_Checks_On
and Validity_Check_Operands
then
10166 if Nkind
(N
) = N_Range
then
10168 (Expr
=> Low_Bound
(N
),
10169 Related_Id
=> Related_Id
,
10170 Is_Low_Bound
=> True);
10173 (Expr
=> High_Bound
(N
),
10174 Related_Id
=> Related_Id
,
10175 Is_High_Bound
=> True);
10178 end Validity_Check_Range
;
10180 --------------------------------
10181 -- Validity_Checks_Suppressed --
10182 --------------------------------
10184 function Validity_Checks_Suppressed
(E
: Entity_Id
) return Boolean is
10186 if Present
(E
) and then Checks_May_Be_Suppressed
(E
) then
10187 return Is_Check_Suppressed
(E
, Validity_Check
);
10189 return Scope_Suppress
.Suppress
(Validity_Check
);
10191 end Validity_Checks_Suppressed
;