| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- C H E C K S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 12 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 13 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 14 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 15 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 16 |
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- |
| 17 |
-- for more details. You should have received a copy of the GNU General -- |
| 18 |
-- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
| 19 |
-- http://www.gnu.org/licenses for a complete copy of the license. -- |
| 20 |
-- -- |
| 21 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 22 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 23 |
-- -- |
| 24 |
------------------------------------------------------------------------------ |
| 25 |
|
| 26 |
with Atree; use Atree; |
| 27 |
with Debug; use Debug; |
| 28 |
with Einfo; use Einfo; |
| 29 |
with Einfo.Entities; use Einfo.Entities; |
| 30 |
with Einfo.Utils; use Einfo.Utils; |
| 31 |
with Elists; use Elists; |
| 32 |
with Eval_Fat; use Eval_Fat; |
| 33 |
with Exp_Ch11; use Exp_Ch11; |
| 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; |
| 39 |
with Lib; use Lib; |
| 40 |
with Nlists; use Nlists; |
| 41 |
with Nmake; use Nmake; |
| 42 |
with Opt; use Opt; |
| 43 |
with Output; use Output; |
| 44 |
with Restrict; use Restrict; |
| 45 |
with Rident; use Rident; |
| 46 |
with Rtsfind; use Rtsfind; |
| 47 |
with Sem; use Sem; |
| 48 |
with Sem_Aux; use Sem_Aux; |
| 49 |
with Sem_Ch3; use Sem_Ch3; |
| 50 |
with Sem_Ch8; use Sem_Ch8; |
| 51 |
with Sem_Cat; use Sem_Cat; |
| 52 |
with Sem_Disp; use Sem_Disp; |
| 53 |
with Sem_Elab; use Sem_Elab; |
| 54 |
with Sem_Eval; use Sem_Eval; |
| 55 |
with Sem_Mech; use Sem_Mech; |
| 56 |
with Sem_Res; use Sem_Res; |
| 57 |
with Sem_Util; use Sem_Util; |
| 58 |
with Sem_Warn; use Sem_Warn; |
| 59 |
with Sinfo; use Sinfo; |
| 60 |
with Sinfo.Nodes; use Sinfo.Nodes; |
| 61 |
with Sinfo.Utils; use Sinfo.Utils; |
| 62 |
with Sinput; use Sinput; |
| 63 |
with Snames; use Snames; |
| 64 |
with Sprint; use Sprint; |
| 65 |
with Stand; use Stand; |
| 66 |
with Stringt; use Stringt; |
| 67 |
with Targparm; use Targparm; |
| 68 |
with Tbuild; use Tbuild; |
| 69 |
with Ttypes; use Ttypes; |
| 70 |
with Validsw; use Validsw; |
| 71 |
|
| 72 |
package body Checks is |
| 73 |
|
| 74 |
-- General note: many of these routines are concerned with generating |
| 75 |
-- checking code to make sure that constraint error is raised at runtime. |
| 76 |
-- Clearly this code is only needed if the expander is active, since |
| 77 |
-- otherwise we will not be generating code or going into the runtime |
| 78 |
-- execution anyway. |
| 79 |
|
| 80 |
-- We therefore disconnect most of these checks if the expander is |
| 81 |
-- inactive. This has the additional benefit that we do not need to |
| 82 |
-- worry about the tree being messed up by previous errors (since errors |
| 83 |
-- turn off expansion anyway). |
| 84 |
|
| 85 |
-- There are a few exceptions to the above rule. For instance routines |
| 86 |
-- such as Apply_Scalar_Range_Check that do not insert any code can be |
| 87 |
-- safely called even when the Expander is inactive (but Errors_Detected |
| 88 |
-- is 0). The benefit of executing this code when expansion is off, is |
| 89 |
-- the ability to emit constraint error warnings for static expressions |
| 90 |
-- even when we are not generating code. |
| 91 |
|
| 92 |
-- The above is modified in gnatprove mode to ensure that proper check |
| 93 |
-- flags are always placed, even if expansion is off. |
| 94 |
|
| 95 |
------------------------------------- |
| 96 |
-- Suppression of Redundant Checks -- |
| 97 |
------------------------------------- |
| 98 |
|
| 99 |
-- This unit implements a limited circuit for removal of redundant |
| 100 |
-- checks. The processing is based on a tracing of simple sequential |
| 101 |
-- flow. For any sequence of statements, we save expressions that are |
| 102 |
-- marked to be checked, and then if the same expression appears later |
| 103 |
-- with the same check, then under certain circumstances, the second |
| 104 |
-- check can be suppressed. |
| 105 |
|
| 106 |
-- Basically, we can suppress the check if we know for certain that |
| 107 |
-- the previous expression has been elaborated (together with its |
| 108 |
-- check), and we know that the exception frame is the same, and that |
| 109 |
-- nothing has happened to change the result of the exception. |
| 110 |
|
| 111 |
-- Let us examine each of these three conditions in turn to describe |
| 112 |
-- how we ensure that this condition is met. |
| 113 |
|
| 114 |
-- First, we need to know for certain that the previous expression has |
| 115 |
-- been executed. This is done principally by the mechanism of calling |
| 116 |
-- Conditional_Statements_Begin at the start of any statement sequence |
| 117 |
-- and Conditional_Statements_End at the end. The End call causes all |
| 118 |
-- checks remembered since the Begin call to be discarded. This does |
| 119 |
-- miss a few cases, notably the case of a nested BEGIN-END block with |
| 120 |
-- no exception handlers. But the important thing is to be conservative. |
| 121 |
-- The other protection is that all checks are discarded if a label |
| 122 |
-- is encountered, since then the assumption of sequential execution |
| 123 |
-- is violated, and we don't know enough about the flow. |
| 124 |
|
| 125 |
-- Second, we need to know that the exception frame is the same. We |
| 126 |
-- do this by killing all remembered checks when we enter a new frame. |
| 127 |
-- Again, that's over-conservative, but generally the cases we can help |
| 128 |
-- with are pretty local anyway (like the body of a loop for example). |
| 129 |
|
| 130 |
-- Third, we must be sure to forget any checks which are no longer valid. |
| 131 |
-- This is done by two mechanisms, first the Kill_Checks_Variable call is |
| 132 |
-- used to note any changes to local variables. We only attempt to deal |
| 133 |
-- with checks involving local variables, so we do not need to worry |
| 134 |
-- about global variables. Second, a call to any non-global procedure |
| 135 |
-- causes us to abandon all stored checks, since such a all may affect |
| 136 |
-- the values of any local variables. |
| 137 |
|
| 138 |
-- The following define the data structures used to deal with remembering |
| 139 |
-- checks so that redundant checks can be eliminated as described above. |
| 140 |
|
| 141 |
-- Right now, the only expressions that we deal with are of the form of |
| 142 |
-- simple local objects (either declared locally, or IN parameters) or |
| 143 |
-- such objects plus/minus a compile time known constant. We can do |
| 144 |
-- more later on if it seems worthwhile, but this catches many simple |
| 145 |
-- cases in practice. |
| 146 |
|
| 147 |
-- The following record type reflects a single saved check. An entry |
| 148 |
-- is made in the stack of saved checks if and only if the expression |
| 149 |
-- has been elaborated with the indicated checks. |
| 150 |
|
| 151 |
type Saved_Check is record |
| 152 |
Killed : Boolean; |
| 153 |
-- Set True if entry is killed by Kill_Checks |
| 154 |
|
| 155 |
Entity : Entity_Id; |
| 156 |
-- The entity involved in the expression that is checked |
| 157 |
|
| 158 |
Offset : Uint; |
| 159 |
-- A compile time value indicating the result of adding or |
| 160 |
-- subtracting a compile time value. This value is to be |
| 161 |
-- added to the value of the Entity. A value of zero is |
| 162 |
-- used for the case of a simple entity reference. |
| 163 |
|
| 164 |
Check_Type : Character; |
| 165 |
-- This is set to 'R' for a range check (in which case Target_Type |
| 166 |
-- is set to the target type for the range check) or to 'O' for an |
| 167 |
-- overflow check (in which case Target_Type is set to Empty). |
| 168 |
|
| 169 |
Target_Type : Entity_Id; |
| 170 |
-- Used only if Do_Range_Check is set. Records the target type for |
| 171 |
-- the check. We need this, because a check is a duplicate only if |
| 172 |
-- it has the same target type (or more accurately one with a |
| 173 |
-- range that is smaller or equal to the stored target type of a |
| 174 |
-- saved check). |
| 175 |
end record; |
| 176 |
|
| 177 |
-- The following table keeps track of saved checks. Rather than use an |
| 178 |
-- extensible table, we just use a table of fixed size, and we discard |
| 179 |
-- any saved checks that do not fit. That's very unlikely to happen and |
| 180 |
-- this is only an optimization in any case. |
| 181 |
|
| 182 |
Saved_Checks : array (Int range 1 .. 200) of Saved_Check; |
| 183 |
-- Array of saved checks |
| 184 |
|
| 185 |
Num_Saved_Checks : Nat := 0; |
| 186 |
-- Number of saved checks |
| 187 |
|
| 188 |
-- The following stack keeps track of statement ranges. It is treated |
| 189 |
-- as a stack. When Conditional_Statements_Begin is called, an entry |
| 190 |
-- is pushed onto this stack containing the value of Num_Saved_Checks |
| 191 |
-- at the time of the call. Then when Conditional_Statements_End is |
| 192 |
-- called, this value is popped off and used to reset Num_Saved_Checks. |
| 193 |
|
| 194 |
-- Note: again, this is a fixed length stack with a size that should |
| 195 |
-- always be fine. If the value of the stack pointer goes above the |
| 196 |
-- limit, then we just forget all saved checks. |
| 197 |
|
| 198 |
Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; |
| 199 |
Saved_Checks_TOS : Nat := 0; |
| 200 |
|
| 201 |
----------------------- |
| 202 |
-- Local Subprograms -- |
| 203 |
----------------------- |
| 204 |
|
| 205 |
procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
| 206 |
-- Used to apply arithmetic overflow checks for all cases except operators |
| 207 |
-- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
| 208 |
-- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
| 209 |
-- signed integer arithmetic operator (but not an if or case expression). |
| 210 |
-- It is also called for types other than signed integers. |
| 211 |
|
| 212 |
procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); |
| 213 |
-- Used to apply arithmetic overflow checks for the case where the overflow |
| 214 |
-- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
| 215 |
-- arithmetic op (which includes the case of if and case expressions). Note |
| 216 |
-- that Do_Overflow_Check may or may not be set for node Op. In these modes |
| 217 |
-- we have work to do even if overflow checking is suppressed. |
| 218 |
|
| 219 |
procedure Apply_Division_Check |
| 220 |
(N : Node_Id; |
| 221 |
Rlo : Uint; |
| 222 |
Rhi : Uint; |
| 223 |
ROK : Boolean); |
| 224 |
-- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies |
| 225 |
-- division checks as required if the Do_Division_Check flag is set. |
| 226 |
-- Rlo and Rhi give the possible range of the right operand, these values |
| 227 |
-- can be referenced and trusted only if ROK is set True. |
| 228 |
|
| 229 |
procedure Apply_Float_Conversion_Check |
| 230 |
(Expr : Node_Id; |
| 231 |
Target_Typ : Entity_Id); |
| 232 |
-- The checks on a conversion from a floating-point type to an integer |
| 233 |
-- type are delicate. They have to be performed before conversion, they |
| 234 |
-- have to raise an exception when the operand is a NaN, and rounding must |
| 235 |
-- be taken into account to determine the safe bounds of the operand. |
| 236 |
|
| 237 |
procedure Apply_Selected_Length_Checks |
| 238 |
(Expr : Node_Id; |
| 239 |
Target_Typ : Entity_Id; |
| 240 |
Source_Typ : Entity_Id; |
| 241 |
Do_Static : Boolean); |
| 242 |
-- This is the subprogram that does all the work for Apply_Length_Check |
| 243 |
-- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as |
| 244 |
-- described for the above routines. The Do_Static flag indicates that |
| 245 |
-- only a static check is to be done. |
| 246 |
|
| 247 |
procedure Compute_Range_For_Arithmetic_Op |
| 248 |
(Op : Node_Kind; |
| 249 |
Lo_Left : Uint; |
| 250 |
Hi_Left : Uint; |
| 251 |
Lo_Right : Uint; |
| 252 |
Hi_Right : Uint; |
| 253 |
OK : out Boolean; |
| 254 |
Lo : out Uint; |
| 255 |
Hi : out Uint); |
| 256 |
-- Given an integer arithmetical operation Op and the range of values of |
| 257 |
-- its operand(s), try to compute a conservative estimate of the possible |
| 258 |
-- range of values for the result of the operation. Thus if OK is True on |
| 259 |
-- return, the result is known to lie in the range Lo .. Hi (inclusive). |
| 260 |
-- If OK is false, both Lo and Hi are set to No_Uint. |
| 261 |
|
| 262 |
type Check_Type is new Check_Id range Access_Check .. Division_Check; |
| 263 |
function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
| 264 |
-- This function is used to see if an access or division by zero check is |
| 265 |
-- needed. The check is to be applied to a single variable appearing in the |
| 266 |
-- source, and N is the node for the reference. If N is not of this form, |
| 267 |
-- True is returned with no further processing. If N is of the right form, |
| 268 |
-- then further processing determines if the given Check is needed. |
| 269 |
-- |
| 270 |
-- The particular circuit is to see if we have the case of a check that is |
| 271 |
-- not needed because it appears in the right operand of a short circuited |
| 272 |
-- conditional where the left operand guards the check. For example: |
| 273 |
-- |
| 274 |
-- if Var = 0 or else Q / Var > 12 then |
| 275 |
-- ... |
| 276 |
-- end if; |
| 277 |
-- |
| 278 |
-- In this example, the division check is not required. At the same time |
| 279 |
-- we can issue warnings for suspicious use of non-short-circuited forms, |
| 280 |
-- such as: |
| 281 |
-- |
| 282 |
-- if Var = 0 or Q / Var > 12 then |
| 283 |
-- ... |
| 284 |
-- end if; |
| 285 |
|
| 286 |
procedure Find_Check |
| 287 |
(Expr : Node_Id; |
| 288 |
Check_Type : Character; |
| 289 |
Target_Type : Entity_Id; |
| 290 |
Entry_OK : out Boolean; |
| 291 |
Check_Num : out Nat; |
| 292 |
Ent : out Entity_Id; |
| 293 |
Ofs : out Uint); |
| 294 |
-- This routine is used by Enable_Range_Check and Enable_Overflow_Check |
| 295 |
-- to see if a check is of the form for optimization, and if so, to see |
| 296 |
-- if it has already been performed. Expr is the expression to check, |
| 297 |
-- and Check_Type is 'R' for a range check, 'O' for an overflow check. |
| 298 |
-- Target_Type is the target type for a range check, and Empty for an |
| 299 |
-- overflow check. If the entry is not of the form for optimization, |
| 300 |
-- then Entry_OK is set to False, and the remaining out parameters |
| 301 |
-- are undefined. If the entry is OK, then Ent/Ofs are set to the |
| 302 |
-- entity and offset from the expression. Check_Num is the number of |
| 303 |
-- a matching saved entry in Saved_Checks, or zero if no such entry |
| 304 |
-- is located. |
| 305 |
|
| 306 |
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
| 307 |
-- If a discriminal is used in constraining a prival, Return reference |
| 308 |
-- to the discriminal of the protected body (which renames the parameter |
| 309 |
-- of the enclosing protected operation). This clumsy transformation is |
| 310 |
-- needed because privals are created too late and their actual subtypes |
| 311 |
-- are not available when analysing the bodies of the protected operations. |
| 312 |
-- This function is called whenever the bound is an entity and the scope |
| 313 |
-- indicates a protected operation. If the bound is an in-parameter of |
| 314 |
-- a protected operation that is not a prival, the function returns the |
| 315 |
-- bound itself. |
| 316 |
-- To be cleaned up??? |
| 317 |
|
| 318 |
function Guard_Access |
| 319 |
(Cond : Node_Id; |
| 320 |
Loc : Source_Ptr; |
| 321 |
Expr : Node_Id) return Node_Id; |
| 322 |
-- In the access type case, guard the test with a test to ensure |
| 323 |
-- that the access value is non-null, since the checks do not |
| 324 |
-- not apply to null access values. |
| 325 |
|
| 326 |
procedure Install_Static_Check |
| 327 |
(R_Cno : Node_Id; Loc : Source_Ptr; Reason : RT_Exception_Code); |
| 328 |
-- Called by Apply_{Length,Range}_Checks to rewrite the tree with the |
| 329 |
-- Constraint_Error node. |
| 330 |
|
| 331 |
function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
| 332 |
-- Returns True if node N is for an arithmetic operation with signed |
| 333 |
-- integer operands. This includes unary and binary operators (including |
| 334 |
-- comparison operators), and also if and case expression nodes which |
| 335 |
-- yield a value of a signed integer type. |
| 336 |
-- These are the kinds of nodes for which special handling applies in |
| 337 |
-- MINIMIZED or ELIMINATED overflow checking mode. |
| 338 |
|
| 339 |
function Range_Or_Validity_Checks_Suppressed |
| 340 |
(Expr : Node_Id) return Boolean; |
| 341 |
-- Returns True if either range or validity checks or both are suppressed |
| 342 |
-- for the type of the given expression, or, if the expression is the name |
| 343 |
-- of an entity, if these checks are suppressed for the entity. |
| 344 |
|
| 345 |
function Selected_Length_Checks |
| 346 |
(Expr : Node_Id; |
| 347 |
Target_Typ : Entity_Id; |
| 348 |
Source_Typ : Entity_Id; |
| 349 |
Warn_Node : Node_Id) return Check_Result; |
| 350 |
-- Like Apply_Selected_Length_Checks, except it doesn't modify |
| 351 |
-- anything, just returns a list of nodes as described in the spec of |
| 352 |
-- this package for the Get_Range_Checks function. |
| 353 |
-- ??? In fact it does construct the test and insert it into the tree, |
| 354 |
-- and insert actions in various ways (calling Insert_Action directly |
| 355 |
-- in particular) so we do not call it in GNATprove mode, contrary to |
| 356 |
-- Selected_Range_Checks. |
| 357 |
|
| 358 |
function Selected_Range_Checks |
| 359 |
(Expr : Node_Id; |
| 360 |
Target_Typ : Entity_Id; |
| 361 |
Source_Typ : Entity_Id; |
| 362 |
Warn_Node : Node_Id) return Check_Result; |
| 363 |
-- Like Apply_Range_Check, except it does not modify anything, just |
| 364 |
-- returns a list of nodes as described in the spec of this package |
| 365 |
-- for the Get_Range_Checks function. |
| 366 |
|
| 367 |
------------------------------ |
| 368 |
-- Access_Checks_Suppressed -- |
| 369 |
------------------------------ |
| 370 |
|
| 371 |
function Access_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 372 |
begin |
| 373 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 374 |
return Is_Check_Suppressed (E, Access_Check); |
| 375 |
else |
| 376 |
return Scope_Suppress.Suppress (Access_Check); |
| 377 |
end if; |
| 378 |
end Access_Checks_Suppressed; |
| 379 |
|
| 380 |
------------------------------------- |
| 381 |
-- Accessibility_Checks_Suppressed -- |
| 382 |
------------------------------------- |
| 383 |
|
| 384 |
function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 385 |
begin |
| 386 |
if No_Dynamic_Accessibility_Checks_Enabled (E) then |
| 387 |
return True; |
| 388 |
|
| 389 |
elsif Present (E) and then Checks_May_Be_Suppressed (E) then |
| 390 |
return Is_Check_Suppressed (E, Accessibility_Check); |
| 391 |
|
| 392 |
else |
| 393 |
return Scope_Suppress.Suppress (Accessibility_Check); |
| 394 |
end if; |
| 395 |
end Accessibility_Checks_Suppressed; |
| 396 |
|
| 397 |
----------------------------- |
| 398 |
-- Activate_Division_Check -- |
| 399 |
----------------------------- |
| 400 |
|
| 401 |
procedure Activate_Division_Check (N : Node_Id) is |
| 402 |
begin |
| 403 |
Set_Do_Division_Check (N, True); |
| 404 |
Possible_Local_Raise (N, Standard_Constraint_Error); |
| 405 |
end Activate_Division_Check; |
| 406 |
|
| 407 |
----------------------------- |
| 408 |
-- Activate_Overflow_Check -- |
| 409 |
----------------------------- |
| 410 |
|
| 411 |
procedure Activate_Overflow_Check (N : Node_Id) is |
| 412 |
Typ : constant Entity_Id := Etype (N); |
| 413 |
|
| 414 |
begin |
| 415 |
-- Floating-point case. If Etype is not set (this can happen when we |
| 416 |
-- activate a check on a node that has not yet been analyzed), then |
| 417 |
-- we assume we do not have a floating-point type (as per our spec). |
| 418 |
|
| 419 |
if Present (Typ) and then Is_Floating_Point_Type (Typ) then |
| 420 |
|
| 421 |
-- Ignore call if we have no automatic overflow checks on the target |
| 422 |
-- and Check_Float_Overflow mode is not set. These are the cases in |
| 423 |
-- which we expect to generate infinities and NaN's with no check. |
| 424 |
|
| 425 |
if not (Machine_Overflows_On_Target or Check_Float_Overflow) then |
| 426 |
return; |
| 427 |
|
| 428 |
-- Ignore for unary operations ("+", "-", abs) since these can never |
| 429 |
-- result in overflow for floating-point cases. |
| 430 |
|
| 431 |
elsif Nkind (N) in N_Unary_Op then |
| 432 |
return; |
| 433 |
|
| 434 |
-- Otherwise we will set the flag |
| 435 |
|
| 436 |
else |
| 437 |
null; |
| 438 |
end if; |
| 439 |
|
| 440 |
-- Discrete case |
| 441 |
|
| 442 |
else |
| 443 |
-- Nothing to do for Rem/Mod/Plus (overflow not possible, the check |
| 444 |
-- for zero-divide is a divide check, not an overflow check). |
| 445 |
|
| 446 |
if Nkind (N) in N_Op_Rem | N_Op_Mod | N_Op_Plus then |
| 447 |
return; |
| 448 |
end if; |
| 449 |
end if; |
| 450 |
|
| 451 |
-- Fall through for cases where we do set the flag |
| 452 |
|
| 453 |
Set_Do_Overflow_Check (N); |
| 454 |
Possible_Local_Raise (N, Standard_Constraint_Error); |
| 455 |
end Activate_Overflow_Check; |
| 456 |
|
| 457 |
-------------------------- |
| 458 |
-- Activate_Range_Check -- |
| 459 |
-------------------------- |
| 460 |
|
| 461 |
procedure Activate_Range_Check (N : Node_Id) is |
| 462 |
begin |
| 463 |
Set_Do_Range_Check (N); |
| 464 |
Possible_Local_Raise (N, Standard_Constraint_Error); |
| 465 |
end Activate_Range_Check; |
| 466 |
|
| 467 |
--------------------------------- |
| 468 |
-- Alignment_Checks_Suppressed -- |
| 469 |
--------------------------------- |
| 470 |
|
| 471 |
function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 472 |
begin |
| 473 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 474 |
return Is_Check_Suppressed (E, Alignment_Check); |
| 475 |
else |
| 476 |
return Scope_Suppress.Suppress (Alignment_Check); |
| 477 |
end if; |
| 478 |
end Alignment_Checks_Suppressed; |
| 479 |
|
| 480 |
---------------------------------- |
| 481 |
-- Allocation_Checks_Suppressed -- |
| 482 |
---------------------------------- |
| 483 |
|
| 484 |
-- Note: at the current time there are no calls to this function, because |
| 485 |
-- the relevant check is in the run-time, so it is not a check that the |
| 486 |
-- compiler can suppress anyway, but we still have to recognize the check |
| 487 |
-- name Allocation_Check since it is part of the standard. |
| 488 |
|
| 489 |
function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 490 |
begin |
| 491 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 492 |
return Is_Check_Suppressed (E, Allocation_Check); |
| 493 |
else |
| 494 |
return Scope_Suppress.Suppress (Allocation_Check); |
| 495 |
end if; |
| 496 |
end Allocation_Checks_Suppressed; |
| 497 |
|
| 498 |
------------------------- |
| 499 |
-- Append_Range_Checks -- |
| 500 |
------------------------- |
| 501 |
|
| 502 |
procedure Append_Range_Checks |
| 503 |
(Checks : Check_Result; |
| 504 |
Stmts : List_Id; |
| 505 |
Suppress_Typ : Entity_Id; |
| 506 |
Static_Sloc : Source_Ptr) |
| 507 |
is |
| 508 |
Checks_On : constant Boolean := |
| 509 |
not Index_Checks_Suppressed (Suppress_Typ) |
| 510 |
or else |
| 511 |
not Range_Checks_Suppressed (Suppress_Typ); |
| 512 |
|
| 513 |
begin |
| 514 |
-- For now we just return if Checks_On is false, however this could be |
| 515 |
-- enhanced to check for an always True value in the condition and to |
| 516 |
-- generate a compilation warning. |
| 517 |
|
| 518 |
if not Checks_On then |
| 519 |
return; |
| 520 |
end if; |
| 521 |
|
| 522 |
for J in 1 .. 2 loop |
| 523 |
exit when No (Checks (J)); |
| 524 |
|
| 525 |
if Nkind (Checks (J)) = N_Raise_Constraint_Error |
| 526 |
and then Present (Condition (Checks (J))) |
| 527 |
then |
| 528 |
Append_To (Stmts, Checks (J)); |
| 529 |
else |
| 530 |
Append_To |
| 531 |
(Stmts, |
| 532 |
Make_Raise_Constraint_Error (Static_Sloc, |
| 533 |
Reason => CE_Range_Check_Failed)); |
| 534 |
end if; |
| 535 |
end loop; |
| 536 |
end Append_Range_Checks; |
| 537 |
|
| 538 |
------------------------ |
| 539 |
-- Apply_Access_Check -- |
| 540 |
------------------------ |
| 541 |
|
| 542 |
procedure Apply_Access_Check (N : Node_Id) is |
| 543 |
P : constant Node_Id := Prefix (N); |
| 544 |
|
| 545 |
begin |
| 546 |
-- We do not need checks if we are not generating code (i.e. the |
| 547 |
-- expander is not active). This is not just an optimization, there |
| 548 |
-- are cases (e.g. with pragma Debug) where generating the checks |
| 549 |
-- can cause real trouble. |
| 550 |
|
| 551 |
if not Expander_Active then |
| 552 |
return; |
| 553 |
end if; |
| 554 |
|
| 555 |
-- No check if short circuiting makes check unnecessary |
| 556 |
|
| 557 |
if not Check_Needed (P, Access_Check) then |
| 558 |
return; |
| 559 |
end if; |
| 560 |
|
| 561 |
-- No check if accessing the Offset_To_Top component of a dispatch |
| 562 |
-- table. They are safe by construction. |
| 563 |
|
| 564 |
if Tagged_Type_Expansion |
| 565 |
and then Present (Etype (P)) |
| 566 |
and then Is_RTE (Etype (P), RE_Offset_To_Top_Ptr) |
| 567 |
then |
| 568 |
return; |
| 569 |
end if; |
| 570 |
|
| 571 |
-- Otherwise go ahead and install the check |
| 572 |
|
| 573 |
Install_Null_Excluding_Check (P); |
| 574 |
end Apply_Access_Check; |
| 575 |
|
| 576 |
-------------------------------- |
| 577 |
-- Apply_Address_Clause_Check -- |
| 578 |
-------------------------------- |
| 579 |
|
| 580 |
procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is |
| 581 |
pragma Assert (Nkind (N) = N_Freeze_Entity); |
| 582 |
|
| 583 |
AC : constant Node_Id := Address_Clause (E); |
| 584 |
Loc : constant Source_Ptr := Sloc (AC); |
| 585 |
Typ : constant Entity_Id := Etype (E); |
| 586 |
|
| 587 |
Expr : Node_Id; |
| 588 |
-- Address expression (not necessarily the same as Aexp, for example |
| 589 |
-- when Aexp is a reference to a constant, in which case Expr gets |
| 590 |
-- reset to reference the value expression of the constant). |
| 591 |
|
| 592 |
begin |
| 593 |
-- See if alignment check needed. Note that we never need a check if the |
| 594 |
-- maximum alignment is one, since the check will always succeed. |
| 595 |
|
| 596 |
-- Note: we do not check for checks suppressed here, since that check |
| 597 |
-- was done in Sem_Ch13 when the address clause was processed. We are |
| 598 |
-- only called if checks were not suppressed. The reason for this is |
| 599 |
-- that we have to delay the call to Apply_Alignment_Check till freeze |
| 600 |
-- time (so that all types etc are elaborated), but we have to check |
| 601 |
-- the status of check suppressing at the point of the address clause. |
| 602 |
|
| 603 |
if No (AC) |
| 604 |
or else not Check_Address_Alignment (AC) |
| 605 |
or else Maximum_Alignment = 1 |
| 606 |
then |
| 607 |
return; |
| 608 |
end if; |
| 609 |
|
| 610 |
-- Obtain expression from address clause |
| 611 |
|
| 612 |
Expr := Address_Value (Expression (AC)); |
| 613 |
|
| 614 |
-- See if we know that Expr has an acceptable value at compile time. If |
| 615 |
-- it hasn't or we don't know, we defer issuing the warning until the |
| 616 |
-- end of the compilation to take into account back end annotations. |
| 617 |
|
| 618 |
if Compile_Time_Known_Value (Expr) |
| 619 |
and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
| 620 |
then |
| 621 |
declare |
| 622 |
AL : Uint := Alignment (Typ); |
| 623 |
|
| 624 |
begin |
| 625 |
-- The object alignment might be more restrictive than the type |
| 626 |
-- alignment. |
| 627 |
|
| 628 |
if Known_Alignment (E) then |
| 629 |
AL := Alignment (E); |
| 630 |
end if; |
| 631 |
|
| 632 |
if Expr_Value (Expr) mod AL = 0 then |
| 633 |
return; |
| 634 |
end if; |
| 635 |
end; |
| 636 |
|
| 637 |
-- If the expression has the form X'Address, then we can find out if the |
| 638 |
-- object X has an alignment that is compatible with the object E. If it |
| 639 |
-- hasn't or we don't know, we defer issuing the warning until the end |
| 640 |
-- of the compilation to take into account back end annotations. |
| 641 |
|
| 642 |
elsif Nkind (Expr) = N_Attribute_Reference |
| 643 |
and then Attribute_Name (Expr) = Name_Address |
| 644 |
and then |
| 645 |
Has_Compatible_Alignment (E, Prefix (Expr), False) = Known_Compatible |
| 646 |
then |
| 647 |
return; |
| 648 |
end if; |
| 649 |
|
| 650 |
-- Here we do not know if the value is acceptable. Strictly we don't |
| 651 |
-- have to do anything, since if the alignment is bad, we have an |
| 652 |
-- erroneous program. However we are allowed to check for erroneous |
| 653 |
-- conditions and we decide to do this by default if the check is not |
| 654 |
-- suppressed. |
| 655 |
|
| 656 |
-- However, don't do the check if elaboration code is unwanted |
| 657 |
|
| 658 |
if Restriction_Active (No_Elaboration_Code) then |
| 659 |
return; |
| 660 |
|
| 661 |
-- Generate a check to raise PE if alignment may be inappropriate |
| 662 |
|
| 663 |
else |
| 664 |
-- If the original expression is a nonstatic constant, use the name |
| 665 |
-- of the constant itself rather than duplicating its initialization |
| 666 |
-- expression, which was extracted above. |
| 667 |
|
| 668 |
-- Note: Expr is empty if the address-clause is applied to in-mode |
| 669 |
-- actuals (allowed by 13.1(22)). |
| 670 |
|
| 671 |
if No (Expr) |
| 672 |
or else |
| 673 |
(Is_Entity_Name (Expression (AC)) |
| 674 |
and then Ekind (Entity (Expression (AC))) = E_Constant |
| 675 |
and then Nkind (Parent (Entity (Expression (AC)))) = |
| 676 |
N_Object_Declaration) |
| 677 |
then |
| 678 |
Expr := New_Copy_Tree (Expression (AC)); |
| 679 |
else |
| 680 |
Remove_Side_Effects (Expr); |
| 681 |
end if; |
| 682 |
|
| 683 |
if No (Actions (N)) then |
| 684 |
Set_Actions (N, New_List); |
| 685 |
end if; |
| 686 |
|
| 687 |
Prepend_To (Actions (N), |
| 688 |
Make_Raise_Program_Error (Loc, |
| 689 |
Condition => |
| 690 |
Make_Op_Ne (Loc, |
| 691 |
Left_Opnd => |
| 692 |
Make_Op_Mod (Loc, |
| 693 |
Left_Opnd => |
| 694 |
Unchecked_Convert_To |
| 695 |
(RTE (RE_Integer_Address), Expr), |
| 696 |
Right_Opnd => |
| 697 |
Make_Attribute_Reference (Loc, |
| 698 |
Prefix => New_Occurrence_Of (E, Loc), |
| 699 |
Attribute_Name => Name_Alignment)), |
| 700 |
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), |
| 701 |
Reason => PE_Misaligned_Address_Value)); |
| 702 |
|
| 703 |
Warning_Msg := No_Error_Msg; |
| 704 |
Analyze (First (Actions (N)), Suppress => All_Checks); |
| 705 |
|
| 706 |
-- If the above raise action generated a warning message (for example |
| 707 |
-- from Warn_On_Non_Local_Exception mode with the active restriction |
| 708 |
-- No_Exception_Propagation). |
| 709 |
|
| 710 |
if Warning_Msg /= No_Error_Msg then |
| 711 |
|
| 712 |
-- If the expression has a known at compile time value, then |
| 713 |
-- once we know the alignment of the type, we can check if the |
| 714 |
-- exception will be raised or not, and if not, we don't need |
| 715 |
-- the warning so we will kill the warning later on. |
| 716 |
|
| 717 |
if Compile_Time_Known_Value (Expr) then |
| 718 |
Alignment_Warnings.Append |
| 719 |
((E => E, |
| 720 |
A => Expr_Value (Expr), |
| 721 |
P => Empty, |
| 722 |
W => Warning_Msg)); |
| 723 |
|
| 724 |
-- Likewise if the expression is of the form X'Address |
| 725 |
|
| 726 |
elsif Nkind (Expr) = N_Attribute_Reference |
| 727 |
and then Attribute_Name (Expr) = Name_Address |
| 728 |
then |
| 729 |
Alignment_Warnings.Append |
| 730 |
((E => E, |
| 731 |
A => No_Uint, |
| 732 |
P => Prefix (Expr), |
| 733 |
W => Warning_Msg)); |
| 734 |
|
| 735 |
-- Add explanation of the warning generated by the check |
| 736 |
|
| 737 |
else |
| 738 |
Error_Msg_N |
| 739 |
("\address value may be incompatible with alignment of " |
| 740 |
& "object?.x?", AC); |
| 741 |
end if; |
| 742 |
end if; |
| 743 |
|
| 744 |
return; |
| 745 |
end if; |
| 746 |
|
| 747 |
exception |
| 748 |
|
| 749 |
-- If we have some missing run time component in configurable run time |
| 750 |
-- mode then just skip the check (it is not required in any case). |
| 751 |
|
| 752 |
when RE_Not_Available => |
| 753 |
return; |
| 754 |
end Apply_Address_Clause_Check; |
| 755 |
|
| 756 |
------------------------------------- |
| 757 |
-- Apply_Arithmetic_Overflow_Check -- |
| 758 |
------------------------------------- |
| 759 |
|
| 760 |
procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
| 761 |
begin |
| 762 |
-- Use old routine in almost all cases (the only case we are treating |
| 763 |
-- specially is the case of a signed integer arithmetic op with the |
| 764 |
-- overflow checking mode set to MINIMIZED or ELIMINATED). |
| 765 |
|
| 766 |
if Overflow_Check_Mode = Strict |
| 767 |
or else not Is_Signed_Integer_Arithmetic_Op (N) |
| 768 |
then |
| 769 |
Apply_Arithmetic_Overflow_Strict (N); |
| 770 |
|
| 771 |
-- Otherwise use the new routine for the case of a signed integer |
| 772 |
-- arithmetic op, with Do_Overflow_Check set to True, and the checking |
| 773 |
-- mode is MINIMIZED or ELIMINATED. |
| 774 |
|
| 775 |
else |
| 776 |
Apply_Arithmetic_Overflow_Minimized_Eliminated (N); |
| 777 |
end if; |
| 778 |
end Apply_Arithmetic_Overflow_Check; |
| 779 |
|
| 780 |
-------------------------------------- |
| 781 |
-- Apply_Arithmetic_Overflow_Strict -- |
| 782 |
-------------------------------------- |
| 783 |
|
| 784 |
-- This routine is called only if the type is an integer type and an |
| 785 |
-- arithmetic overflow check may be needed for op (add, subtract, or |
| 786 |
-- multiply). This check is performed if Backend_Overflow_Checks_On_Target |
| 787 |
-- is not enabled and Do_Overflow_Check is set. In this case we expand the |
| 788 |
-- operation into a more complex sequence of tests that ensures that |
| 789 |
-- overflow is properly caught. |
| 790 |
|
| 791 |
-- This is used in CHECKED modes. It is identical to the code for this |
| 792 |
-- cases before the big overflow earthquake, thus ensuring that in this |
| 793 |
-- modes we have compatible behavior (and reliability) to what was there |
| 794 |
-- before. It is also called for types other than signed integers, and if |
| 795 |
-- the Do_Overflow_Check flag is off. |
| 796 |
|
| 797 |
-- Note: we also call this routine if we decide in the MINIMIZED case |
| 798 |
-- to give up and just generate an overflow check without any fuss. |
| 799 |
|
| 800 |
procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
| 801 |
Loc : constant Source_Ptr := Sloc (N); |
| 802 |
Typ : constant Entity_Id := Etype (N); |
| 803 |
Rtyp : constant Entity_Id := Root_Type (Typ); |
| 804 |
|
| 805 |
begin |
| 806 |
-- Nothing to do if Do_Overflow_Check not set or overflow checks |
| 807 |
-- suppressed. |
| 808 |
|
| 809 |
if not Do_Overflow_Check (N) then |
| 810 |
return; |
| 811 |
end if; |
| 812 |
|
| 813 |
-- An interesting special case. If the arithmetic operation appears as |
| 814 |
-- the operand of a type conversion: |
| 815 |
|
| 816 |
-- type1 (x op y) |
| 817 |
|
| 818 |
-- and all the following conditions apply: |
| 819 |
|
| 820 |
-- arithmetic operation is for a signed integer type |
| 821 |
-- target type type1 is a static integer subtype |
| 822 |
-- range of x and y are both included in the range of type1 |
| 823 |
-- range of x op y is included in the range of type1 |
| 824 |
-- size of type1 is at least twice the result size of op |
| 825 |
|
| 826 |
-- then we don't do an overflow check in any case. Instead, we transform |
| 827 |
-- the operation so that we end up with: |
| 828 |
|
| 829 |
-- type1 (type1 (x) op type1 (y)) |
| 830 |
|
| 831 |
-- This avoids intermediate overflow before the conversion. It is |
| 832 |
-- explicitly permitted by RM 3.5.4(24): |
| 833 |
|
| 834 |
-- For the execution of a predefined operation of a signed integer |
| 835 |
-- type, the implementation need not raise Constraint_Error if the |
| 836 |
-- result is outside the base range of the type, so long as the |
| 837 |
-- correct result is produced. |
| 838 |
|
| 839 |
-- It's hard to imagine that any programmer counts on the exception |
| 840 |
-- being raised in this case, and in any case it's wrong coding to |
| 841 |
-- have this expectation, given the RM permission. Furthermore, other |
| 842 |
-- Ada compilers do allow such out of range results. |
| 843 |
|
| 844 |
-- Note that we do this transformation even if overflow checking is |
| 845 |
-- off, since this is precisely about giving the "right" result and |
| 846 |
-- avoiding the need for an overflow check. |
| 847 |
|
| 848 |
-- Note: this circuit is partially redundant with respect to the similar |
| 849 |
-- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals |
| 850 |
-- with cases that do not come through here. We still need the following |
| 851 |
-- processing even with the Exp_Ch4 code in place, since we want to be |
| 852 |
-- sure not to generate the arithmetic overflow check in these cases |
| 853 |
-- (Exp_Ch4 would have a hard time removing them once generated). |
| 854 |
|
| 855 |
if Is_Signed_Integer_Type (Typ) |
| 856 |
and then Nkind (Parent (N)) = N_Type_Conversion |
| 857 |
then |
| 858 |
Conversion_Optimization : declare |
| 859 |
Target_Type : constant Entity_Id := |
| 860 |
Base_Type (Entity (Subtype_Mark (Parent (N)))); |
| 861 |
|
| 862 |
Llo, Lhi : Uint; |
| 863 |
Rlo, Rhi : Uint; |
| 864 |
LOK, ROK : Boolean; |
| 865 |
|
| 866 |
Vlo : Uint; |
| 867 |
Vhi : Uint; |
| 868 |
VOK : Boolean; |
| 869 |
|
| 870 |
Tlo : Uint; |
| 871 |
Thi : Uint; |
| 872 |
|
| 873 |
begin |
| 874 |
if Is_Integer_Type (Target_Type) |
| 875 |
and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) |
| 876 |
then |
| 877 |
Tlo := Expr_Value (Type_Low_Bound (Target_Type)); |
| 878 |
Thi := Expr_Value (Type_High_Bound (Target_Type)); |
| 879 |
|
| 880 |
Determine_Range |
| 881 |
(Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); |
| 882 |
Determine_Range |
| 883 |
(Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); |
| 884 |
|
| 885 |
if (LOK and ROK) |
| 886 |
and then Tlo <= Llo and then Lhi <= Thi |
| 887 |
and then Tlo <= Rlo and then Rhi <= Thi |
| 888 |
then |
| 889 |
Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
| 890 |
|
| 891 |
if VOK and then Tlo <= Vlo and then Vhi <= Thi then |
| 892 |
-- Rewrite the conversion operand so that the original |
| 893 |
-- node is retained, in order to avoid the warning for |
| 894 |
-- redundant conversions in Resolve_Type_Conversion. |
| 895 |
|
| 896 |
declare |
| 897 |
Op : constant Node_Id := New_Op_Node (Nkind (N), Loc); |
| 898 |
begin |
| 899 |
Set_Left_Opnd (Op, |
| 900 |
Make_Type_Conversion (Loc, |
| 901 |
Subtype_Mark => |
| 902 |
New_Occurrence_Of (Target_Type, Loc), |
| 903 |
Expression => Relocate_Node (Left_Opnd (N)))); |
| 904 |
Set_Right_Opnd (Op, |
| 905 |
Make_Type_Conversion (Loc, |
| 906 |
Subtype_Mark => |
| 907 |
New_Occurrence_Of (Target_Type, Loc), |
| 908 |
Expression => Relocate_Node (Right_Opnd (N)))); |
| 909 |
|
| 910 |
Rewrite (N, Op); |
| 911 |
end; |
| 912 |
|
| 913 |
Set_Etype (N, Target_Type); |
| 914 |
|
| 915 |
Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
| 916 |
Analyze_And_Resolve (Right_Opnd (N), Target_Type); |
| 917 |
|
| 918 |
-- Given that the target type is twice the size of the |
| 919 |
-- source type, overflow is now impossible, so we can |
| 920 |
-- safely kill the overflow check and return. |
| 921 |
|
| 922 |
Set_Do_Overflow_Check (N, False); |
| 923 |
return; |
| 924 |
end if; |
| 925 |
end if; |
| 926 |
end if; |
| 927 |
end Conversion_Optimization; |
| 928 |
end if; |
| 929 |
|
| 930 |
-- Now see if an overflow check is required |
| 931 |
|
| 932 |
declare |
| 933 |
Dsiz : constant Uint := 2 * Esize (Rtyp); |
| 934 |
Opnod : Node_Id; |
| 935 |
Ctyp : Entity_Id; |
| 936 |
Opnd : Node_Id; |
| 937 |
Cent : RE_Id; |
| 938 |
|
| 939 |
begin |
| 940 |
-- Skip check if back end does overflow checks, or the overflow flag |
| 941 |
-- is not set anyway, or we are not doing code expansion, or the |
| 942 |
-- parent node is a type conversion whose operand is an arithmetic |
| 943 |
-- operation on signed integers on which the expander can promote |
| 944 |
-- later the operands to type Integer (see Expand_N_Type_Conversion). |
| 945 |
|
| 946 |
if Backend_Overflow_Checks_On_Target |
| 947 |
or else not Do_Overflow_Check (N) |
| 948 |
or else not Expander_Active |
| 949 |
or else (Present (Parent (N)) |
| 950 |
and then Nkind (Parent (N)) = N_Type_Conversion |
| 951 |
and then Integer_Promotion_Possible (Parent (N))) |
| 952 |
then |
| 953 |
return; |
| 954 |
end if; |
| 955 |
|
| 956 |
-- Otherwise, generate the full general code for front end overflow |
| 957 |
-- detection, which works by doing arithmetic in a larger type: |
| 958 |
|
| 959 |
-- x op y |
| 960 |
|
| 961 |
-- is expanded into |
| 962 |
|
| 963 |
-- Typ (Checktyp (x) op Checktyp (y)); |
| 964 |
|
| 965 |
-- where Typ is the type of the original expression, and Checktyp is |
| 966 |
-- an integer type of sufficient length to hold the largest possible |
| 967 |
-- result. |
| 968 |
|
| 969 |
-- If the size of the check type exceeds the maximum integer size, |
| 970 |
-- we use a different approach, expanding to: |
| 971 |
|
| 972 |
-- typ (xxx_With_Ovflo_Check (Integer_NN (x), Integer_NN (y))) |
| 973 |
|
| 974 |
-- where xxx is Add, Multiply or Subtract as appropriate |
| 975 |
|
| 976 |
-- Find check type if one exists |
| 977 |
|
| 978 |
if Dsiz <= System_Max_Integer_Size then |
| 979 |
Ctyp := Integer_Type_For (Dsiz, Uns => False); |
| 980 |
|
| 981 |
-- No check type exists, use runtime call |
| 982 |
|
| 983 |
else |
| 984 |
if System_Max_Integer_Size = 64 then |
| 985 |
Ctyp := RTE (RE_Integer_64); |
| 986 |
else |
| 987 |
Ctyp := RTE (RE_Integer_128); |
| 988 |
end if; |
| 989 |
|
| 990 |
if Nkind (N) = N_Op_Add then |
| 991 |
if System_Max_Integer_Size = 64 then |
| 992 |
Cent := RE_Add_With_Ovflo_Check64; |
| 993 |
else |
| 994 |
Cent := RE_Add_With_Ovflo_Check128; |
| 995 |
end if; |
| 996 |
|
| 997 |
elsif Nkind (N) = N_Op_Subtract then |
| 998 |
if System_Max_Integer_Size = 64 then |
| 999 |
Cent := RE_Subtract_With_Ovflo_Check64; |
| 1000 |
else |
| 1001 |
Cent := RE_Subtract_With_Ovflo_Check128; |
| 1002 |
end if; |
| 1003 |
|
| 1004 |
else pragma Assert (Nkind (N) = N_Op_Multiply); |
| 1005 |
if System_Max_Integer_Size = 64 then |
| 1006 |
Cent := RE_Multiply_With_Ovflo_Check64; |
| 1007 |
else |
| 1008 |
Cent := RE_Multiply_With_Ovflo_Check128; |
| 1009 |
end if; |
| 1010 |
end if; |
| 1011 |
|
| 1012 |
Rewrite (N, |
| 1013 |
OK_Convert_To (Typ, |
| 1014 |
Make_Function_Call (Loc, |
| 1015 |
Name => New_Occurrence_Of (RTE (Cent), Loc), |
| 1016 |
Parameter_Associations => New_List ( |
| 1017 |
OK_Convert_To (Ctyp, Left_Opnd (N)), |
| 1018 |
OK_Convert_To (Ctyp, Right_Opnd (N)))))); |
| 1019 |
|
| 1020 |
Analyze_And_Resolve (N, Typ); |
| 1021 |
return; |
| 1022 |
end if; |
| 1023 |
|
| 1024 |
-- If we fall through, we have the case where we do the arithmetic |
| 1025 |
-- in the next higher type and get the check by conversion. In these |
| 1026 |
-- cases Ctyp is set to the type to be used as the check type. |
| 1027 |
|
| 1028 |
Opnod := Relocate_Node (N); |
| 1029 |
|
| 1030 |
Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
| 1031 |
|
| 1032 |
Analyze (Opnd); |
| 1033 |
Set_Etype (Opnd, Ctyp); |
| 1034 |
Set_Analyzed (Opnd, True); |
| 1035 |
Set_Left_Opnd (Opnod, Opnd); |
| 1036 |
|
| 1037 |
Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
| 1038 |
|
| 1039 |
Analyze (Opnd); |
| 1040 |
Set_Etype (Opnd, Ctyp); |
| 1041 |
Set_Analyzed (Opnd, True); |
| 1042 |
Set_Right_Opnd (Opnod, Opnd); |
| 1043 |
|
| 1044 |
-- The type of the operation changes to the base type of the check |
| 1045 |
-- type, and we reset the overflow check indication, since clearly no |
| 1046 |
-- overflow is possible now that we are using a double length type. |
| 1047 |
-- We also set the Analyzed flag to avoid a recursive attempt to |
| 1048 |
-- expand the node. |
| 1049 |
|
| 1050 |
Set_Etype (Opnod, Base_Type (Ctyp)); |
| 1051 |
Set_Do_Overflow_Check (Opnod, False); |
| 1052 |
Set_Analyzed (Opnod, True); |
| 1053 |
|
| 1054 |
-- Now build the outer conversion |
| 1055 |
|
| 1056 |
Opnd := OK_Convert_To (Typ, Opnod); |
| 1057 |
Analyze (Opnd); |
| 1058 |
Set_Etype (Opnd, Typ); |
| 1059 |
|
| 1060 |
-- In the discrete type case, we directly generate the range check |
| 1061 |
-- for the outer operand. This range check will implement the |
| 1062 |
-- required overflow check. |
| 1063 |
|
| 1064 |
if Is_Discrete_Type (Typ) then |
| 1065 |
Rewrite (N, Opnd); |
| 1066 |
Generate_Range_Check |
| 1067 |
(Expression (N), Typ, CE_Overflow_Check_Failed); |
| 1068 |
|
| 1069 |
-- For other types, we enable overflow checking on the conversion, |
| 1070 |
-- after setting the node as analyzed to prevent recursive attempts |
| 1071 |
-- to expand the conversion node. |
| 1072 |
|
| 1073 |
else |
| 1074 |
Set_Analyzed (Opnd, True); |
| 1075 |
Enable_Overflow_Check (Opnd); |
| 1076 |
Rewrite (N, Opnd); |
| 1077 |
end if; |
| 1078 |
|
| 1079 |
exception |
| 1080 |
when RE_Not_Available => |
| 1081 |
return; |
| 1082 |
end; |
| 1083 |
end Apply_Arithmetic_Overflow_Strict; |
| 1084 |
|
| 1085 |
---------------------------------------------------- |
| 1086 |
-- Apply_Arithmetic_Overflow_Minimized_Eliminated -- |
| 1087 |
---------------------------------------------------- |
| 1088 |
|
| 1089 |
procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is |
| 1090 |
pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); |
| 1091 |
|
| 1092 |
Loc : constant Source_Ptr := Sloc (Op); |
| 1093 |
P : constant Node_Id := Parent (Op); |
| 1094 |
|
| 1095 |
LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
| 1096 |
-- Operands and results are of this type when we convert |
| 1097 |
|
| 1098 |
Result_Type : constant Entity_Id := Etype (Op); |
| 1099 |
-- Original result type |
| 1100 |
|
| 1101 |
Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
| 1102 |
pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
| 1103 |
|
| 1104 |
Lo, Hi : Uint; |
| 1105 |
-- Ranges of values for result |
| 1106 |
|
| 1107 |
begin |
| 1108 |
-- Nothing to do if our parent is one of the following: |
| 1109 |
|
| 1110 |
-- Another signed integer arithmetic op |
| 1111 |
-- A membership operation |
| 1112 |
-- A comparison operation |
| 1113 |
|
| 1114 |
-- In all these cases, we will process at the higher level (and then |
| 1115 |
-- this node will be processed during the downwards recursion that |
| 1116 |
-- is part of the processing in Minimize_Eliminate_Overflows). |
| 1117 |
|
| 1118 |
if Is_Signed_Integer_Arithmetic_Op (P) |
| 1119 |
or else Nkind (P) in N_Membership_Test |
| 1120 |
or else Nkind (P) in N_Op_Compare |
| 1121 |
|
| 1122 |
-- This is also true for an alternative in a case expression |
| 1123 |
|
| 1124 |
or else Nkind (P) = N_Case_Expression_Alternative |
| 1125 |
|
| 1126 |
-- This is also true for a range operand in a membership test |
| 1127 |
|
| 1128 |
or else (Nkind (P) = N_Range |
| 1129 |
and then Nkind (Parent (P)) in N_Membership_Test) |
| 1130 |
then |
| 1131 |
-- If_Expressions and Case_Expressions are treated as arithmetic |
| 1132 |
-- ops, but if they appear in an assignment or similar contexts |
| 1133 |
-- there is no overflow check that starts from that parent node, |
| 1134 |
-- so apply check now. |
| 1135 |
-- Similarly, if these expressions are nested, we should go on. |
| 1136 |
|
| 1137 |
if Nkind (P) in N_If_Expression | N_Case_Expression |
| 1138 |
and then not Is_Signed_Integer_Arithmetic_Op (Parent (P)) |
| 1139 |
then |
| 1140 |
null; |
| 1141 |
elsif Nkind (P) in N_If_Expression | N_Case_Expression |
| 1142 |
and then Nkind (Op) in N_If_Expression | N_Case_Expression |
| 1143 |
then |
| 1144 |
null; |
| 1145 |
else |
| 1146 |
return; |
| 1147 |
end if; |
| 1148 |
end if; |
| 1149 |
|
| 1150 |
-- Otherwise, we have a top level arithmetic operation node, and this |
| 1151 |
-- is where we commence the special processing for MINIMIZED/ELIMINATED |
| 1152 |
-- modes. This is the case where we tell the machinery not to move into |
| 1153 |
-- Bignum mode at this top level (of course the top level operation |
| 1154 |
-- will still be in Bignum mode if either of its operands are of type |
| 1155 |
-- Bignum). |
| 1156 |
|
| 1157 |
Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
| 1158 |
|
| 1159 |
-- That call may but does not necessarily change the result type of Op. |
| 1160 |
-- It is the job of this routine to undo such changes, so that at the |
| 1161 |
-- top level, we have the proper type. This "undoing" is a point at |
| 1162 |
-- which a final overflow check may be applied. |
| 1163 |
|
| 1164 |
-- If the result type was not fiddled we are all set. We go to base |
| 1165 |
-- types here because things may have been rewritten to generate the |
| 1166 |
-- base type of the operand types. |
| 1167 |
|
| 1168 |
if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
| 1169 |
return; |
| 1170 |
|
| 1171 |
-- Bignum case |
| 1172 |
|
| 1173 |
elsif Is_RTE (Etype (Op), RE_Bignum) then |
| 1174 |
|
| 1175 |
-- We need a sequence that looks like: |
| 1176 |
|
| 1177 |
-- Rnn : Result_Type; |
| 1178 |
|
| 1179 |
-- declare |
| 1180 |
-- M : Mark_Id := SS_Mark; |
| 1181 |
-- begin |
| 1182 |
-- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
| 1183 |
-- SS_Release (M); |
| 1184 |
-- end; |
| 1185 |
|
| 1186 |
-- This block is inserted (using Insert_Actions), and then the node |
| 1187 |
-- is replaced with a reference to Rnn. |
| 1188 |
|
| 1189 |
-- If our parent is a conversion node then there is no point in |
| 1190 |
-- generating a conversion to Result_Type. Instead, we let the parent |
| 1191 |
-- handle this. Note that this special case is not just about |
| 1192 |
-- optimization. Consider |
| 1193 |
|
| 1194 |
-- A,B,C : Integer; |
| 1195 |
-- ... |
| 1196 |
-- X := Long_Long_Integer'Base (A * (B ** C)); |
| 1197 |
|
| 1198 |
-- Now the product may fit in Long_Long_Integer but not in Integer. |
| 1199 |
-- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
| 1200 |
-- overflow exception for this intermediate value. |
| 1201 |
|
| 1202 |
declare |
| 1203 |
Blk : constant Node_Id := Make_Bignum_Block (Loc); |
| 1204 |
Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
| 1205 |
RHS : Node_Id; |
| 1206 |
|
| 1207 |
Rtype : Entity_Id; |
| 1208 |
|
| 1209 |
begin |
| 1210 |
RHS := Convert_From_Bignum (Op); |
| 1211 |
|
| 1212 |
if Nkind (P) /= N_Type_Conversion then |
| 1213 |
Convert_To_And_Rewrite (Result_Type, RHS); |
| 1214 |
Rtype := Result_Type; |
| 1215 |
|
| 1216 |
-- Interesting question, do we need a check on that conversion |
| 1217 |
-- operation. Answer, not if we know the result is in range. |
| 1218 |
-- At the moment we are not taking advantage of this. To be |
| 1219 |
-- looked at later ??? |
| 1220 |
|
| 1221 |
else |
| 1222 |
Rtype := LLIB; |
| 1223 |
end if; |
| 1224 |
|
| 1225 |
Insert_Before |
| 1226 |
(First (Statements (Handled_Statement_Sequence (Blk))), |
| 1227 |
Make_Assignment_Statement (Loc, |
| 1228 |
Name => New_Occurrence_Of (Rnn, Loc), |
| 1229 |
Expression => RHS)); |
| 1230 |
|
| 1231 |
Insert_Actions (Op, New_List ( |
| 1232 |
Make_Object_Declaration (Loc, |
| 1233 |
Defining_Identifier => Rnn, |
| 1234 |
Object_Definition => New_Occurrence_Of (Rtype, Loc)), |
| 1235 |
Blk)); |
| 1236 |
|
| 1237 |
Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); |
| 1238 |
Analyze_And_Resolve (Op); |
| 1239 |
end; |
| 1240 |
|
| 1241 |
-- Here we know the result is Long_Long_Integer'Base, or that it has |
| 1242 |
-- been rewritten because the parent operation is a conversion. See |
| 1243 |
-- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
| 1244 |
|
| 1245 |
else |
| 1246 |
pragma Assert |
| 1247 |
(Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); |
| 1248 |
|
| 1249 |
-- All we need to do here is to convert the result to the proper |
| 1250 |
-- result type. As explained above for the Bignum case, we can |
| 1251 |
-- omit this if our parent is a type conversion. |
| 1252 |
|
| 1253 |
if Nkind (P) /= N_Type_Conversion then |
| 1254 |
Convert_To_And_Rewrite (Result_Type, Op); |
| 1255 |
end if; |
| 1256 |
|
| 1257 |
Analyze_And_Resolve (Op); |
| 1258 |
end if; |
| 1259 |
end Apply_Arithmetic_Overflow_Minimized_Eliminated; |
| 1260 |
|
| 1261 |
---------------------------- |
| 1262 |
-- Apply_Constraint_Check -- |
| 1263 |
---------------------------- |
| 1264 |
|
| 1265 |
procedure Apply_Constraint_Check |
| 1266 |
(N : Node_Id; |
| 1267 |
Typ : Entity_Id; |
| 1268 |
No_Sliding : Boolean := False) |
| 1269 |
is |
| 1270 |
Desig_Typ : Entity_Id; |
| 1271 |
|
| 1272 |
begin |
| 1273 |
-- No checks inside a generic (check the instantiations) |
| 1274 |
|
| 1275 |
if Inside_A_Generic then |
| 1276 |
return; |
| 1277 |
end if; |
| 1278 |
|
| 1279 |
-- Apply required constraint checks |
| 1280 |
|
| 1281 |
if Is_Scalar_Type (Typ) then |
| 1282 |
Apply_Scalar_Range_Check (N, Typ); |
| 1283 |
|
| 1284 |
elsif Is_Array_Type (Typ) then |
| 1285 |
|
| 1286 |
-- A useful optimization: an aggregate with only an others clause |
| 1287 |
-- always has the right bounds. |
| 1288 |
|
| 1289 |
if Nkind (N) = N_Aggregate |
| 1290 |
and then No (Expressions (N)) |
| 1291 |
and then Nkind (First (Component_Associations (N))) = |
| 1292 |
N_Component_Association |
| 1293 |
and then Nkind |
| 1294 |
(First (Choices (First (Component_Associations (N))))) |
| 1295 |
= N_Others_Choice |
| 1296 |
then |
| 1297 |
return; |
| 1298 |
end if; |
| 1299 |
|
| 1300 |
if Is_Constrained (Typ) then |
| 1301 |
Apply_Length_Check (N, Typ); |
| 1302 |
|
| 1303 |
if No_Sliding then |
| 1304 |
Apply_Range_Check (N, Typ); |
| 1305 |
end if; |
| 1306 |
else |
| 1307 |
Apply_Range_Check (N, Typ); |
| 1308 |
end if; |
| 1309 |
|
| 1310 |
elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
| 1311 |
and then Has_Discriminants (Base_Type (Typ)) |
| 1312 |
and then Is_Constrained (Typ) |
| 1313 |
then |
| 1314 |
Apply_Discriminant_Check (N, Typ); |
| 1315 |
|
| 1316 |
elsif Is_Access_Type (Typ) then |
| 1317 |
|
| 1318 |
Desig_Typ := Designated_Type (Typ); |
| 1319 |
|
| 1320 |
-- No checks necessary if expression statically null |
| 1321 |
|
| 1322 |
if Known_Null (N) then |
| 1323 |
if Can_Never_Be_Null (Typ) then |
| 1324 |
Install_Null_Excluding_Check (N); |
| 1325 |
end if; |
| 1326 |
|
| 1327 |
-- No sliding possible on access to arrays |
| 1328 |
|
| 1329 |
elsif Is_Array_Type (Desig_Typ) then |
| 1330 |
if Is_Constrained (Desig_Typ) then |
| 1331 |
Apply_Length_Check (N, Typ); |
| 1332 |
end if; |
| 1333 |
|
| 1334 |
Apply_Range_Check (N, Typ); |
| 1335 |
|
| 1336 |
-- Do not install a discriminant check for a constrained subtype |
| 1337 |
-- created for an unconstrained nominal type because the subtype |
| 1338 |
-- has the correct constraints by construction. |
| 1339 |
|
| 1340 |
elsif Has_Discriminants (Base_Type (Desig_Typ)) |
| 1341 |
and then Is_Constrained (Desig_Typ) |
| 1342 |
and then not Is_Constr_Subt_For_U_Nominal (Desig_Typ) |
| 1343 |
then |
| 1344 |
Apply_Discriminant_Check (N, Typ); |
| 1345 |
end if; |
| 1346 |
|
| 1347 |
-- Apply the 2005 Null_Excluding check. Note that we do not apply |
| 1348 |
-- this check if the constraint node is illegal, as shown by having |
| 1349 |
-- an error posted. This additional guard prevents cascaded errors |
| 1350 |
-- and compiler aborts on illegal programs involving Ada 2005 checks. |
| 1351 |
|
| 1352 |
if Can_Never_Be_Null (Typ) |
| 1353 |
and then not Can_Never_Be_Null (Etype (N)) |
| 1354 |
and then not Error_Posted (N) |
| 1355 |
then |
| 1356 |
Install_Null_Excluding_Check (N); |
| 1357 |
end if; |
| 1358 |
end if; |
| 1359 |
end Apply_Constraint_Check; |
| 1360 |
|
| 1361 |
------------------------------ |
| 1362 |
-- Apply_Discriminant_Check -- |
| 1363 |
------------------------------ |
| 1364 |
|
| 1365 |
procedure Apply_Discriminant_Check |
| 1366 |
(N : Node_Id; |
| 1367 |
Typ : Entity_Id; |
| 1368 |
Lhs : Node_Id := Empty) |
| 1369 |
is |
| 1370 |
Loc : constant Source_Ptr := Sloc (N); |
| 1371 |
Do_Access : constant Boolean := Is_Access_Type (Typ); |
| 1372 |
S_Typ : Entity_Id := Etype (N); |
| 1373 |
Cond : Node_Id; |
| 1374 |
T_Typ : Entity_Id; |
| 1375 |
|
| 1376 |
function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
| 1377 |
-- A heap object with an indefinite subtype is constrained by its |
| 1378 |
-- initial value, and assigning to it requires a constraint_check. |
| 1379 |
-- The target may be an explicit dereference, or a renaming of one. |
| 1380 |
|
| 1381 |
function Is_Aliased_Unconstrained_Component return Boolean; |
| 1382 |
-- It is possible for an aliased component to have a nominal |
| 1383 |
-- unconstrained subtype (through instantiation). If this is a |
| 1384 |
-- discriminated component assigned in the expansion of an aggregate |
| 1385 |
-- in an initialization, the check must be suppressed. This unusual |
| 1386 |
-- situation requires a predicate of its own. |
| 1387 |
|
| 1388 |
---------------------------------- |
| 1389 |
-- Denotes_Explicit_Dereference -- |
| 1390 |
---------------------------------- |
| 1391 |
|
| 1392 |
function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is |
| 1393 |
begin |
| 1394 |
if Is_Entity_Name (Obj) then |
| 1395 |
return Present (Renamed_Object (Entity (Obj))) |
| 1396 |
and then |
| 1397 |
Denotes_Explicit_Dereference (Renamed_Object (Entity (Obj))); |
| 1398 |
|
| 1399 |
-- This routine uses the rules of the language so we need to exclude |
| 1400 |
-- rewritten constructs that introduce artificial dereferences. |
| 1401 |
|
| 1402 |
elsif Nkind (Obj) = N_Explicit_Dereference then |
| 1403 |
return not Is_Captured_Function_Call (Obj) |
| 1404 |
and then not |
| 1405 |
(Nkind (Parent (Obj)) = N_Object_Renaming_Declaration |
| 1406 |
and then Is_Return_Object (Defining_Entity (Parent (Obj)))); |
| 1407 |
|
| 1408 |
else |
| 1409 |
return False; |
| 1410 |
end if; |
| 1411 |
end Denotes_Explicit_Dereference; |
| 1412 |
|
| 1413 |
---------------------------------------- |
| 1414 |
-- Is_Aliased_Unconstrained_Component -- |
| 1415 |
---------------------------------------- |
| 1416 |
|
| 1417 |
function Is_Aliased_Unconstrained_Component return Boolean is |
| 1418 |
Comp : Entity_Id; |
| 1419 |
Pref : Node_Id; |
| 1420 |
|
| 1421 |
begin |
| 1422 |
if Nkind (Lhs) /= N_Selected_Component then |
| 1423 |
return False; |
| 1424 |
else |
| 1425 |
Comp := Entity (Selector_Name (Lhs)); |
| 1426 |
Pref := Prefix (Lhs); |
| 1427 |
end if; |
| 1428 |
|
| 1429 |
if Ekind (Comp) /= E_Component |
| 1430 |
or else not Is_Aliased (Comp) |
| 1431 |
then |
| 1432 |
return False; |
| 1433 |
end if; |
| 1434 |
|
| 1435 |
return not Comes_From_Source (Pref) |
| 1436 |
and then In_Instance |
| 1437 |
and then not Is_Constrained (Etype (Comp)); |
| 1438 |
end Is_Aliased_Unconstrained_Component; |
| 1439 |
|
| 1440 |
-- Start of processing for Apply_Discriminant_Check |
| 1441 |
|
| 1442 |
begin |
| 1443 |
if Do_Access then |
| 1444 |
T_Typ := Designated_Type (Typ); |
| 1445 |
else |
| 1446 |
T_Typ := Typ; |
| 1447 |
end if; |
| 1448 |
|
| 1449 |
-- If the expression is a function call that returns a limited object |
| 1450 |
-- it cannot be copied. It is not clear how to perform the proper |
| 1451 |
-- discriminant check in this case because the discriminant value must |
| 1452 |
-- be retrieved from the constructed object itself. |
| 1453 |
|
| 1454 |
if Nkind (N) = N_Function_Call |
| 1455 |
and then Is_Limited_Type (Typ) |
| 1456 |
and then Is_Entity_Name (Name (N)) |
| 1457 |
and then Returns_By_Ref (Entity (Name (N))) |
| 1458 |
then |
| 1459 |
return; |
| 1460 |
end if; |
| 1461 |
|
| 1462 |
-- Only apply checks when generating code and discriminant checks are |
| 1463 |
-- not suppressed. In GNATprove mode, we do not apply the checks, but we |
| 1464 |
-- still analyze the expression to possibly issue errors on SPARK code |
| 1465 |
-- when a run-time error can be detected at compile time. |
| 1466 |
|
| 1467 |
if not GNATprove_Mode then |
| 1468 |
if not Expander_Active |
| 1469 |
or else Discriminant_Checks_Suppressed (T_Typ) |
| 1470 |
then |
| 1471 |
return; |
| 1472 |
end if; |
| 1473 |
end if; |
| 1474 |
|
| 1475 |
-- No discriminant checks necessary for an access when expression is |
| 1476 |
-- statically Null. This is not only an optimization, it is fundamental |
| 1477 |
-- because otherwise discriminant checks may be generated in init procs |
| 1478 |
-- for types containing an access to a not-yet-frozen record, causing a |
| 1479 |
-- deadly forward reference. |
| 1480 |
|
| 1481 |
-- Also, if the expression is of an access type whose designated type is |
| 1482 |
-- incomplete, then the access value must be null and we suppress the |
| 1483 |
-- check. |
| 1484 |
|
| 1485 |
if Known_Null (N) then |
| 1486 |
return; |
| 1487 |
|
| 1488 |
elsif Is_Access_Type (S_Typ) then |
| 1489 |
S_Typ := Designated_Type (S_Typ); |
| 1490 |
|
| 1491 |
if Ekind (S_Typ) = E_Incomplete_Type then |
| 1492 |
return; |
| 1493 |
end if; |
| 1494 |
end if; |
| 1495 |
|
| 1496 |
-- If an assignment target is present, then we need to generate the |
| 1497 |
-- actual subtype if the target is a parameter or aliased object with |
| 1498 |
-- an unconstrained nominal subtype. |
| 1499 |
|
| 1500 |
-- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual |
| 1501 |
-- subtype to the parameter and dereference cases, since other aliased |
| 1502 |
-- objects are unconstrained (unless the nominal subtype is explicitly |
| 1503 |
-- constrained). |
| 1504 |
|
| 1505 |
if Present (Lhs) |
| 1506 |
and then (Present (Param_Entity (Lhs)) |
| 1507 |
or else (Ada_Version < Ada_2005 |
| 1508 |
and then not Is_Constrained (T_Typ) |
| 1509 |
and then Is_Aliased_View (Lhs) |
| 1510 |
and then not Is_Aliased_Unconstrained_Component) |
| 1511 |
or else (Ada_Version >= Ada_2005 |
| 1512 |
and then not Is_Constrained (T_Typ) |
| 1513 |
and then Denotes_Explicit_Dereference (Lhs))) |
| 1514 |
then |
| 1515 |
T_Typ := Get_Actual_Subtype (Lhs); |
| 1516 |
end if; |
| 1517 |
|
| 1518 |
-- Nothing to do if the type is unconstrained (this is the case where |
| 1519 |
-- the actual subtype in the RM sense of N is unconstrained and no check |
| 1520 |
-- is required). |
| 1521 |
|
| 1522 |
if not Is_Constrained (T_Typ) then |
| 1523 |
return; |
| 1524 |
|
| 1525 |
-- Ada 2005: nothing to do if the type is one for which there is a |
| 1526 |
-- partial view that is constrained. |
| 1527 |
|
| 1528 |
elsif Ada_Version >= Ada_2005 |
| 1529 |
and then Object_Type_Has_Constrained_Partial_View |
| 1530 |
(Typ => Base_Type (T_Typ), |
| 1531 |
Scop => Current_Scope) |
| 1532 |
then |
| 1533 |
return; |
| 1534 |
end if; |
| 1535 |
|
| 1536 |
-- Nothing to do if the type is an Unchecked_Union |
| 1537 |
|
| 1538 |
if Is_Unchecked_Union (Base_Type (T_Typ)) then |
| 1539 |
return; |
| 1540 |
end if; |
| 1541 |
|
| 1542 |
-- Suppress checks if the subtypes are the same and constrained. The |
| 1543 |
-- check must be preserved in an assignment to a formal, because the |
| 1544 |
-- constraint is given by the actual. |
| 1545 |
|
| 1546 |
if Nkind (Original_Node (N)) /= N_Allocator |
| 1547 |
and then (if Do_Access then Designated_Type (Typ) else Typ) = S_Typ |
| 1548 |
and then Is_Constrained (S_Typ) |
| 1549 |
and then (No (Lhs) |
| 1550 |
or else not Is_Entity_Name (Lhs) |
| 1551 |
or else No (Param_Entity (Lhs))) |
| 1552 |
then |
| 1553 |
return; |
| 1554 |
|
| 1555 |
-- We can also eliminate checks on allocators with a subtype mark that |
| 1556 |
-- coincides with the context type. The context type may be a subtype |
| 1557 |
-- without a constraint (common case, a generic actual). |
| 1558 |
|
| 1559 |
elsif Nkind (Original_Node (N)) = N_Allocator |
| 1560 |
and then Is_Entity_Name (Expression (Original_Node (N))) |
| 1561 |
then |
| 1562 |
declare |
| 1563 |
Alloc_Typ : constant Entity_Id := |
| 1564 |
Entity (Expression (Original_Node (N))); |
| 1565 |
|
| 1566 |
begin |
| 1567 |
if Alloc_Typ = T_Typ |
| 1568 |
or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration |
| 1569 |
and then Is_Entity_Name ( |
| 1570 |
Subtype_Indication (Parent (T_Typ))) |
| 1571 |
and then Alloc_Typ = Base_Type (T_Typ)) |
| 1572 |
|
| 1573 |
then |
| 1574 |
return; |
| 1575 |
end if; |
| 1576 |
end; |
| 1577 |
end if; |
| 1578 |
|
| 1579 |
-- See if we have a case where the types are both constrained, and all |
| 1580 |
-- the constraints are constants. In this case, we can do the check |
| 1581 |
-- successfully at compile time. |
| 1582 |
|
| 1583 |
-- We skip this check for the case where the node is rewritten as |
| 1584 |
-- an allocator, because it already carries the context subtype, |
| 1585 |
-- and extracting the discriminants from the aggregate is messy. |
| 1586 |
|
| 1587 |
if Is_Constrained (S_Typ) |
| 1588 |
and then Nkind (Original_Node (N)) /= N_Allocator |
| 1589 |
then |
| 1590 |
declare |
| 1591 |
DconT : Elmt_Id; |
| 1592 |
Discr : Entity_Id; |
| 1593 |
DconS : Elmt_Id; |
| 1594 |
ItemS : Node_Id; |
| 1595 |
ItemT : Node_Id; |
| 1596 |
|
| 1597 |
begin |
| 1598 |
-- S_Typ may not have discriminants in the case where it is a |
| 1599 |
-- private type completed by a default discriminated type. In that |
| 1600 |
-- case, we need to get the constraints from the underlying type. |
| 1601 |
-- If the underlying type is unconstrained (i.e. has no default |
| 1602 |
-- discriminants) no check is needed. |
| 1603 |
|
| 1604 |
if Has_Discriminants (S_Typ) then |
| 1605 |
Discr := First_Discriminant (S_Typ); |
| 1606 |
DconS := First_Elmt (Discriminant_Constraint (S_Typ)); |
| 1607 |
|
| 1608 |
else |
| 1609 |
Discr := First_Discriminant (Underlying_Type (S_Typ)); |
| 1610 |
DconS := |
| 1611 |
First_Elmt |
| 1612 |
(Discriminant_Constraint (Underlying_Type (S_Typ))); |
| 1613 |
|
| 1614 |
if No (DconS) then |
| 1615 |
return; |
| 1616 |
end if; |
| 1617 |
|
| 1618 |
-- A further optimization: if T_Typ is derived from S_Typ |
| 1619 |
-- without imposing a constraint, no check is needed. |
| 1620 |
|
| 1621 |
if Nkind (Original_Node (Parent (T_Typ))) = |
| 1622 |
N_Full_Type_Declaration |
| 1623 |
then |
| 1624 |
declare |
| 1625 |
Type_Def : constant Node_Id := |
| 1626 |
Type_Definition (Original_Node (Parent (T_Typ))); |
| 1627 |
begin |
| 1628 |
if Nkind (Type_Def) = N_Derived_Type_Definition |
| 1629 |
and then Is_Entity_Name (Subtype_Indication (Type_Def)) |
| 1630 |
and then Entity (Subtype_Indication (Type_Def)) = S_Typ |
| 1631 |
then |
| 1632 |
return; |
| 1633 |
end if; |
| 1634 |
end; |
| 1635 |
end if; |
| 1636 |
end if; |
| 1637 |
|
| 1638 |
-- Constraint may appear in full view of type |
| 1639 |
|
| 1640 |
if Ekind (T_Typ) = E_Private_Subtype |
| 1641 |
and then Present (Full_View (T_Typ)) |
| 1642 |
then |
| 1643 |
DconT := |
| 1644 |
First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
| 1645 |
else |
| 1646 |
DconT := |
| 1647 |
First_Elmt (Discriminant_Constraint (T_Typ)); |
| 1648 |
end if; |
| 1649 |
|
| 1650 |
while Present (Discr) loop |
| 1651 |
ItemS := Node (DconS); |
| 1652 |
ItemT := Node (DconT); |
| 1653 |
|
| 1654 |
-- For a discriminated component type constrained by the |
| 1655 |
-- current instance of an enclosing type, there is no |
| 1656 |
-- applicable discriminant check. |
| 1657 |
|
| 1658 |
if Nkind (ItemT) = N_Attribute_Reference |
| 1659 |
and then Is_Access_Type (Etype (ItemT)) |
| 1660 |
and then Is_Entity_Name (Prefix (ItemT)) |
| 1661 |
and then Is_Type (Entity (Prefix (ItemT))) |
| 1662 |
then |
| 1663 |
return; |
| 1664 |
end if; |
| 1665 |
|
| 1666 |
-- If the expressions for the discriminants are identical |
| 1667 |
-- and it is side-effect-free (for now just an entity), |
| 1668 |
-- this may be a shared constraint, e.g. from a subtype |
| 1669 |
-- without a constraint introduced as a generic actual. |
| 1670 |
-- Examine other discriminants if any. |
| 1671 |
|
| 1672 |
if ItemS = ItemT |
| 1673 |
and then Is_Entity_Name (ItemS) |
| 1674 |
then |
| 1675 |
null; |
| 1676 |
|
| 1677 |
elsif not Is_OK_Static_Expression (ItemS) |
| 1678 |
or else not Is_OK_Static_Expression (ItemT) |
| 1679 |
then |
| 1680 |
exit; |
| 1681 |
|
| 1682 |
elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
| 1683 |
if Do_Access then -- needs run-time check. |
| 1684 |
exit; |
| 1685 |
else |
| 1686 |
Apply_Compile_Time_Constraint_Error |
| 1687 |
(N, "incorrect value for discriminant&??", |
| 1688 |
CE_Discriminant_Check_Failed, Ent => Discr); |
| 1689 |
return; |
| 1690 |
end if; |
| 1691 |
end if; |
| 1692 |
|
| 1693 |
Next_Elmt (DconS); |
| 1694 |
Next_Elmt (DconT); |
| 1695 |
Next_Discriminant (Discr); |
| 1696 |
end loop; |
| 1697 |
|
| 1698 |
if No (Discr) then |
| 1699 |
return; |
| 1700 |
end if; |
| 1701 |
end; |
| 1702 |
end if; |
| 1703 |
|
| 1704 |
-- In GNATprove mode, we do not apply the checks |
| 1705 |
|
| 1706 |
if GNATprove_Mode then |
| 1707 |
return; |
| 1708 |
end if; |
| 1709 |
|
| 1710 |
-- Here we need a discriminant check. First build the expression |
| 1711 |
-- for the comparisons of the discriminants: |
| 1712 |
|
| 1713 |
-- (n.disc1 /= typ.disc1) or else |
| 1714 |
-- (n.disc2 /= typ.disc2) or else |
| 1715 |
-- ... |
| 1716 |
-- (n.discn /= typ.discn) |
| 1717 |
|
| 1718 |
Cond := Build_Discriminant_Checks (N, T_Typ); |
| 1719 |
|
| 1720 |
-- If Lhs is set and is a parameter, then the condition is guarded by: |
| 1721 |
-- lhs'constrained and then (condition built above) |
| 1722 |
|
| 1723 |
if Present (Param_Entity (Lhs)) then |
| 1724 |
Cond := |
| 1725 |
Make_And_Then (Loc, |
| 1726 |
Left_Opnd => |
| 1727 |
Make_Attribute_Reference (Loc, |
| 1728 |
Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), |
| 1729 |
Attribute_Name => Name_Constrained), |
| 1730 |
Right_Opnd => Cond); |
| 1731 |
end if; |
| 1732 |
|
| 1733 |
if Do_Access then |
| 1734 |
Cond := Guard_Access (Cond, Loc, N); |
| 1735 |
end if; |
| 1736 |
|
| 1737 |
Insert_Action (N, |
| 1738 |
Make_Raise_Constraint_Error (Loc, |
| 1739 |
Condition => Cond, |
| 1740 |
Reason => CE_Discriminant_Check_Failed)); |
| 1741 |
end Apply_Discriminant_Check; |
| 1742 |
|
| 1743 |
------------------------- |
| 1744 |
-- Apply_Divide_Checks -- |
| 1745 |
------------------------- |
| 1746 |
|
| 1747 |
procedure Apply_Divide_Checks (N : Node_Id) is |
| 1748 |
Loc : constant Source_Ptr := Sloc (N); |
| 1749 |
Typ : constant Entity_Id := Etype (N); |
| 1750 |
Left : constant Node_Id := Left_Opnd (N); |
| 1751 |
Right : constant Node_Id := Right_Opnd (N); |
| 1752 |
|
| 1753 |
Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
| 1754 |
-- Current overflow checking mode |
| 1755 |
|
| 1756 |
LLB : Uint; |
| 1757 |
Llo : Uint; |
| 1758 |
Lhi : Uint; |
| 1759 |
LOK : Boolean; |
| 1760 |
Rlo : Uint; |
| 1761 |
Rhi : Uint; |
| 1762 |
ROK : Boolean; |
| 1763 |
|
| 1764 |
pragma Warnings (Off, Lhi); |
| 1765 |
-- Don't actually use this value |
| 1766 |
|
| 1767 |
begin |
| 1768 |
-- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
| 1769 |
-- operating on signed integer types, then the only thing this routine |
| 1770 |
-- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That |
| 1771 |
-- procedure will (possibly later on during recursive downward calls), |
| 1772 |
-- ensure that any needed overflow/division checks are properly applied. |
| 1773 |
|
| 1774 |
if Mode in Minimized_Or_Eliminated |
| 1775 |
and then Is_Signed_Integer_Type (Typ) |
| 1776 |
then |
| 1777 |
Apply_Arithmetic_Overflow_Minimized_Eliminated (N); |
| 1778 |
return; |
| 1779 |
end if; |
| 1780 |
|
| 1781 |
-- Proceed here in SUPPRESSED or CHECKED modes |
| 1782 |
|
| 1783 |
if Expander_Active |
| 1784 |
and then not Backend_Divide_Checks_On_Target |
| 1785 |
and then Check_Needed (Right, Division_Check) |
| 1786 |
then |
| 1787 |
Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
| 1788 |
|
| 1789 |
-- Deal with division check |
| 1790 |
|
| 1791 |
if Do_Division_Check (N) |
| 1792 |
and then not Division_Checks_Suppressed (Typ) |
| 1793 |
then |
| 1794 |
Apply_Division_Check (N, Rlo, Rhi, ROK); |
| 1795 |
end if; |
| 1796 |
|
| 1797 |
-- Deal with overflow check |
| 1798 |
|
| 1799 |
if Do_Overflow_Check (N) |
| 1800 |
and then not Overflow_Checks_Suppressed (Etype (N)) |
| 1801 |
then |
| 1802 |
Set_Do_Overflow_Check (N, False); |
| 1803 |
|
| 1804 |
-- Test for extremely annoying case of xxx'First divided by -1 |
| 1805 |
-- for division of signed integer types (only overflow case). |
| 1806 |
|
| 1807 |
if Nkind (N) = N_Op_Divide |
| 1808 |
and then Is_Signed_Integer_Type (Typ) |
| 1809 |
then |
| 1810 |
Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
| 1811 |
LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
| 1812 |
|
| 1813 |
if (not ROK or else (Rlo <= (-1) and then (-1) <= Rhi)) |
| 1814 |
and then |
| 1815 |
(not LOK or else Llo = LLB) |
| 1816 |
then |
| 1817 |
-- Ensure that expressions are not evaluated twice (once |
| 1818 |
-- for their runtime checks and once for their regular |
| 1819 |
-- computation). |
| 1820 |
|
| 1821 |
Force_Evaluation (Left, Mode => Strict); |
| 1822 |
Force_Evaluation (Right, Mode => Strict); |
| 1823 |
|
| 1824 |
Insert_Action (N, |
| 1825 |
Make_Raise_Constraint_Error (Loc, |
| 1826 |
Condition => |
| 1827 |
Make_And_Then (Loc, |
| 1828 |
Left_Opnd => |
| 1829 |
Make_Op_Eq (Loc, |
| 1830 |
Left_Opnd => |
| 1831 |
Duplicate_Subexpr_Move_Checks (Left), |
| 1832 |
Right_Opnd => Make_Integer_Literal (Loc, LLB)), |
| 1833 |
|
| 1834 |
Right_Opnd => |
| 1835 |
Make_Op_Eq (Loc, |
| 1836 |
Left_Opnd => Duplicate_Subexpr (Right), |
| 1837 |
Right_Opnd => Make_Integer_Literal (Loc, -1))), |
| 1838 |
|
| 1839 |
Reason => CE_Overflow_Check_Failed)); |
| 1840 |
end if; |
| 1841 |
end if; |
| 1842 |
end if; |
| 1843 |
end if; |
| 1844 |
end Apply_Divide_Checks; |
| 1845 |
|
| 1846 |
-------------------------- |
| 1847 |
-- Apply_Division_Check -- |
| 1848 |
-------------------------- |
| 1849 |
|
| 1850 |
procedure Apply_Division_Check |
| 1851 |
(N : Node_Id; |
| 1852 |
Rlo : Uint; |
| 1853 |
Rhi : Uint; |
| 1854 |
ROK : Boolean) |
| 1855 |
is |
| 1856 |
pragma Assert (Do_Division_Check (N)); |
| 1857 |
|
| 1858 |
Loc : constant Source_Ptr := Sloc (N); |
| 1859 |
Right : constant Node_Id := Right_Opnd (N); |
| 1860 |
Opnd : Node_Id; |
| 1861 |
|
| 1862 |
begin |
| 1863 |
if Expander_Active |
| 1864 |
and then not Backend_Divide_Checks_On_Target |
| 1865 |
and then Check_Needed (Right, Division_Check) |
| 1866 |
|
| 1867 |
-- See if division by zero possible, and if so generate test. This |
| 1868 |
-- part of the test is not controlled by the -gnato switch, since it |
| 1869 |
-- is a Division_Check and not an Overflow_Check. |
| 1870 |
|
| 1871 |
and then Do_Division_Check (N) |
| 1872 |
then |
| 1873 |
Set_Do_Division_Check (N, False); |
| 1874 |
|
| 1875 |
if not ROK or else (Rlo <= 0 and then 0 <= Rhi) then |
| 1876 |
if Is_Floating_Point_Type (Etype (N)) then |
| 1877 |
Opnd := Make_Real_Literal (Loc, Ureal_0); |
| 1878 |
else |
| 1879 |
Opnd := Make_Integer_Literal (Loc, 0); |
| 1880 |
end if; |
| 1881 |
|
| 1882 |
Insert_Action (N, |
| 1883 |
Make_Raise_Constraint_Error (Loc, |
| 1884 |
Condition => |
| 1885 |
Make_Op_Eq (Loc, |
| 1886 |
Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), |
| 1887 |
Right_Opnd => Opnd), |
| 1888 |
Reason => CE_Divide_By_Zero)); |
| 1889 |
end if; |
| 1890 |
end if; |
| 1891 |
end Apply_Division_Check; |
| 1892 |
|
| 1893 |
---------------------------------- |
| 1894 |
-- Apply_Float_Conversion_Check -- |
| 1895 |
---------------------------------- |
| 1896 |
|
| 1897 |
-- Let F and I be the source and target types of the conversion. The RM |
| 1898 |
-- specifies that a floating-point value X is rounded to the nearest |
| 1899 |
-- integer, with halfway cases being rounded away from zero. The rounded |
| 1900 |
-- value of X is checked against I'Range. |
| 1901 |
|
| 1902 |
-- The catch in the above paragraph is that there is no good way to know |
| 1903 |
-- whether the round-to-integer operation resulted in overflow. A remedy is |
| 1904 |
-- to perform a range check in the floating-point domain instead, however: |
| 1905 |
|
| 1906 |
-- (1) The bounds may not be known at compile time |
| 1907 |
-- (2) The check must take into account rounding or truncation. |
| 1908 |
-- (3) The range of type I may not be exactly representable in F. |
| 1909 |
-- (4) For the rounding case, the end-points I'First - 0.5 and |
| 1910 |
-- I'Last + 0.5 may or may not be in range, depending on the |
| 1911 |
-- sign of I'First and I'Last. |
| 1912 |
-- (5) X may be a NaN, which will fail any comparison |
| 1913 |
|
| 1914 |
-- The following steps correctly convert X with rounding: |
| 1915 |
|
| 1916 |
-- (1) If either I'First or I'Last is not known at compile time, use |
| 1917 |
-- I'Base instead of I in the next three steps and perform a |
| 1918 |
-- regular range check against I'Range after conversion. |
| 1919 |
-- (2) If I'First - 0.5 is representable in F then let Lo be that |
| 1920 |
-- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be |
| 1921 |
-- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
| 1922 |
-- In other words, take one of the closest floating-point numbers |
| 1923 |
-- (which is an integer value) to I'First, and see if it is in |
| 1924 |
-- range or not. |
| 1925 |
-- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
| 1926 |
-- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be |
| 1927 |
-- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
| 1928 |
-- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
| 1929 |
-- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) |
| 1930 |
|
| 1931 |
-- For the truncating case, replace steps (2) and (3) as follows: |
| 1932 |
-- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK |
| 1933 |
-- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let |
| 1934 |
-- Lo_OK be True. |
| 1935 |
-- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK |
| 1936 |
-- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let |
| 1937 |
-- Hi_OK be True. |
| 1938 |
|
| 1939 |
procedure Apply_Float_Conversion_Check |
| 1940 |
(Expr : Node_Id; |
| 1941 |
Target_Typ : Entity_Id) |
| 1942 |
is |
| 1943 |
LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
| 1944 |
HB : constant Node_Id := Type_High_Bound (Target_Typ); |
| 1945 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 1946 |
Expr_Type : constant Entity_Id := Base_Type (Etype (Expr)); |
| 1947 |
Target_Base : constant Entity_Id := |
| 1948 |
Implementation_Base_Type (Target_Typ); |
| 1949 |
|
| 1950 |
Par : constant Node_Id := Parent (Expr); |
| 1951 |
pragma Assert (Nkind (Par) = N_Type_Conversion); |
| 1952 |
-- Parent of check node, must be a type conversion |
| 1953 |
|
| 1954 |
Truncate : constant Boolean := Float_Truncate (Par); |
| 1955 |
Max_Bound : constant Uint := |
| 1956 |
UI_Expon |
| 1957 |
(Machine_Radix_Value (Expr_Type), |
| 1958 |
Machine_Mantissa_Value (Expr_Type) - 1) - 1; |
| 1959 |
|
| 1960 |
-- Largest bound, so bound plus or minus half is a machine number of F |
| 1961 |
|
| 1962 |
Ifirst, Ilast : Uint; |
| 1963 |
-- Bounds of integer type |
| 1964 |
|
| 1965 |
Lo, Hi : Ureal; |
| 1966 |
-- Bounds to check in floating-point domain |
| 1967 |
|
| 1968 |
Lo_OK, Hi_OK : Boolean; |
| 1969 |
-- True iff Lo resp. Hi belongs to I'Range |
| 1970 |
|
| 1971 |
Lo_Chk, Hi_Chk : Node_Id; |
| 1972 |
-- Expressions that are False iff check fails |
| 1973 |
|
| 1974 |
Reason : RT_Exception_Code; |
| 1975 |
|
| 1976 |
begin |
| 1977 |
-- We do not need checks if we are not generating code (i.e. the full |
| 1978 |
-- expander is not active). In SPARK mode, we specifically don't want |
| 1979 |
-- the frontend to expand these checks, which are dealt with directly |
| 1980 |
-- in the formal verification backend. |
| 1981 |
|
| 1982 |
if not Expander_Active then |
| 1983 |
return; |
| 1984 |
end if; |
| 1985 |
|
| 1986 |
-- Here we will generate an explicit range check, so we don't want to |
| 1987 |
-- set the Do_Range check flag, since the range check is taken care of |
| 1988 |
-- by the code we will generate. |
| 1989 |
|
| 1990 |
Set_Do_Range_Check (Expr, False); |
| 1991 |
|
| 1992 |
if not Compile_Time_Known_Value (LB) |
| 1993 |
or not Compile_Time_Known_Value (HB) |
| 1994 |
then |
| 1995 |
declare |
| 1996 |
-- First check that the value falls in the range of the base type, |
| 1997 |
-- to prevent overflow during conversion and then perform a |
| 1998 |
-- regular range check against the (dynamic) bounds. |
| 1999 |
|
| 2000 |
pragma Assert (Target_Base /= Target_Typ); |
| 2001 |
|
| 2002 |
Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
| 2003 |
|
| 2004 |
begin |
| 2005 |
Apply_Float_Conversion_Check (Expr, Target_Base); |
| 2006 |
Set_Etype (Temp, Target_Base); |
| 2007 |
|
| 2008 |
-- Note: Previously the declaration was inserted above the parent |
| 2009 |
-- of the conversion, apparently as a small optimization for the |
| 2010 |
-- subequent traversal in Insert_Actions. Unfortunately a similar |
| 2011 |
-- optimization takes place in Insert_Actions, assuming that the |
| 2012 |
-- insertion point must be above the expression that creates |
| 2013 |
-- actions. This is not correct in the presence of conditional |
| 2014 |
-- expressions, where the insertion must be in the list of actions |
| 2015 |
-- attached to the current alternative. |
| 2016 |
|
| 2017 |
Insert_Action (Par, |
| 2018 |
Make_Object_Declaration (Loc, |
| 2019 |
Defining_Identifier => Temp, |
| 2020 |
Object_Definition => New_Occurrence_Of (Target_Typ, Loc), |
| 2021 |
Expression => New_Copy_Tree (Par)), |
| 2022 |
Suppress => All_Checks); |
| 2023 |
|
| 2024 |
Insert_Action (Par, |
| 2025 |
Make_Raise_Constraint_Error (Loc, |
| 2026 |
Condition => |
| 2027 |
Make_Not_In (Loc, |
| 2028 |
Left_Opnd => New_Occurrence_Of (Temp, Loc), |
| 2029 |
Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), |
| 2030 |
Reason => CE_Range_Check_Failed)); |
| 2031 |
Rewrite (Par, New_Occurrence_Of (Temp, Loc)); |
| 2032 |
|
| 2033 |
return; |
| 2034 |
end; |
| 2035 |
end if; |
| 2036 |
|
| 2037 |
-- Get the (static) bounds of the target type |
| 2038 |
|
| 2039 |
Ifirst := Expr_Value (LB); |
| 2040 |
Ilast := Expr_Value (HB); |
| 2041 |
|
| 2042 |
-- A simple optimization: if the expression is a universal literal, |
| 2043 |
-- we can do the comparison with the bounds and the conversion to |
| 2044 |
-- an integer type statically. The range checks are unchanged. |
| 2045 |
|
| 2046 |
if Nkind (Expr) = N_Real_Literal |
| 2047 |
and then Etype (Expr) = Universal_Real |
| 2048 |
and then Is_Integer_Type (Target_Typ) |
| 2049 |
then |
| 2050 |
declare |
| 2051 |
Int_Val : constant Uint := UR_To_Uint (Realval (Expr)); |
| 2052 |
|
| 2053 |
begin |
| 2054 |
if Int_Val <= Ilast and then Int_Val >= Ifirst then |
| 2055 |
|
| 2056 |
-- Conversion is safe |
| 2057 |
|
| 2058 |
Rewrite (Parent (Expr), |
| 2059 |
Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); |
| 2060 |
Analyze_And_Resolve (Parent (Expr), Target_Typ); |
| 2061 |
return; |
| 2062 |
end if; |
| 2063 |
end; |
| 2064 |
end if; |
| 2065 |
|
| 2066 |
-- Check against lower bound |
| 2067 |
|
| 2068 |
if Truncate and then Ifirst > 0 then |
| 2069 |
Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); |
| 2070 |
Lo_OK := False; |
| 2071 |
|
| 2072 |
elsif Truncate then |
| 2073 |
Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); |
| 2074 |
Lo_OK := True; |
| 2075 |
|
| 2076 |
elsif abs Ifirst < Max_Bound then |
| 2077 |
Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
| 2078 |
Lo_OK := (Ifirst > 0); |
| 2079 |
|
| 2080 |
else |
| 2081 |
Lo := Machine_Number (Expr_Type, UR_From_Uint (Ifirst), Expr); |
| 2082 |
Lo_OK := (Lo >= UR_From_Uint (Ifirst)); |
| 2083 |
end if; |
| 2084 |
|
| 2085 |
-- Saturate the lower bound to that of the expression's type, because |
| 2086 |
-- we do not want to create an out-of-range value but we still need to |
| 2087 |
-- do a comparison to catch NaNs. |
| 2088 |
|
| 2089 |
if Lo < Expr_Value_R (Type_Low_Bound (Expr_Type)) then |
| 2090 |
Lo := Expr_Value_R (Type_Low_Bound (Expr_Type)); |
| 2091 |
Lo_OK := True; |
| 2092 |
end if; |
| 2093 |
|
| 2094 |
if Lo_OK then |
| 2095 |
|
| 2096 |
-- Lo_Chk := (X >= Lo) |
| 2097 |
|
| 2098 |
Lo_Chk := Make_Op_Ge (Loc, |
| 2099 |
Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
| 2100 |
Right_Opnd => Make_Real_Literal (Loc, Lo)); |
| 2101 |
|
| 2102 |
else |
| 2103 |
-- Lo_Chk := (X > Lo) |
| 2104 |
|
| 2105 |
Lo_Chk := Make_Op_Gt (Loc, |
| 2106 |
Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
| 2107 |
Right_Opnd => Make_Real_Literal (Loc, Lo)); |
| 2108 |
end if; |
| 2109 |
|
| 2110 |
-- Check against higher bound |
| 2111 |
|
| 2112 |
if Truncate and then Ilast < 0 then |
| 2113 |
Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); |
| 2114 |
Hi_OK := False; |
| 2115 |
|
| 2116 |
elsif Truncate then |
| 2117 |
Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); |
| 2118 |
Hi_OK := True; |
| 2119 |
|
| 2120 |
elsif abs Ilast < Max_Bound then |
| 2121 |
Hi := UR_From_Uint (Ilast) + Ureal_Half; |
| 2122 |
Hi_OK := (Ilast < 0); |
| 2123 |
else |
| 2124 |
Hi := Machine_Number (Expr_Type, UR_From_Uint (Ilast), Expr); |
| 2125 |
Hi_OK := (Hi <= UR_From_Uint (Ilast)); |
| 2126 |
end if; |
| 2127 |
|
| 2128 |
-- Saturate the higher bound to that of the expression's type, because |
| 2129 |
-- we do not want to create an out-of-range value but we still need to |
| 2130 |
-- do a comparison to catch NaNs. |
| 2131 |
|
| 2132 |
if Hi > Expr_Value_R (Type_High_Bound (Expr_Type)) then |
| 2133 |
Hi := Expr_Value_R (Type_High_Bound (Expr_Type)); |
| 2134 |
Hi_OK := True; |
| 2135 |
end if; |
| 2136 |
|
| 2137 |
if Hi_OK then |
| 2138 |
|
| 2139 |
-- Hi_Chk := (X <= Hi) |
| 2140 |
|
| 2141 |
Hi_Chk := Make_Op_Le (Loc, |
| 2142 |
Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
| 2143 |
Right_Opnd => Make_Real_Literal (Loc, Hi)); |
| 2144 |
|
| 2145 |
else |
| 2146 |
-- Hi_Chk := (X < Hi) |
| 2147 |
|
| 2148 |
Hi_Chk := Make_Op_Lt (Loc, |
| 2149 |
Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
| 2150 |
Right_Opnd => Make_Real_Literal (Loc, Hi)); |
| 2151 |
end if; |
| 2152 |
|
| 2153 |
-- If the bounds of the target type are the same as those of the base |
| 2154 |
-- type, the check is an overflow check as a range check is not |
| 2155 |
-- performed in these cases. |
| 2156 |
|
| 2157 |
if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst |
| 2158 |
and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast |
| 2159 |
then |
| 2160 |
Reason := CE_Overflow_Check_Failed; |
| 2161 |
else |
| 2162 |
Reason := CE_Range_Check_Failed; |
| 2163 |
end if; |
| 2164 |
|
| 2165 |
-- Raise CE if either conditions does not hold |
| 2166 |
|
| 2167 |
Insert_Action (Expr, |
| 2168 |
Make_Raise_Constraint_Error (Loc, |
| 2169 |
Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
| 2170 |
Reason => Reason)); |
| 2171 |
end Apply_Float_Conversion_Check; |
| 2172 |
|
| 2173 |
------------------------ |
| 2174 |
-- Apply_Length_Check -- |
| 2175 |
------------------------ |
| 2176 |
|
| 2177 |
procedure Apply_Length_Check |
| 2178 |
(Expr : Node_Id; |
| 2179 |
Target_Typ : Entity_Id; |
| 2180 |
Source_Typ : Entity_Id := Empty) |
| 2181 |
is |
| 2182 |
begin |
| 2183 |
Apply_Selected_Length_Checks |
| 2184 |
(Expr, Target_Typ, Source_Typ, Do_Static => False); |
| 2185 |
end Apply_Length_Check; |
| 2186 |
|
| 2187 |
-------------------------------------- |
| 2188 |
-- Apply_Length_Check_On_Assignment -- |
| 2189 |
-------------------------------------- |
| 2190 |
|
| 2191 |
procedure Apply_Length_Check_On_Assignment |
| 2192 |
(Expr : Node_Id; |
| 2193 |
Target_Typ : Entity_Id; |
| 2194 |
Target : Node_Id; |
| 2195 |
Source_Typ : Entity_Id := Empty) |
| 2196 |
is |
| 2197 |
Assign : constant Node_Id := Parent (Target); |
| 2198 |
|
| 2199 |
begin |
| 2200 |
-- Do not apply length checks if parent is still an assignment statement |
| 2201 |
-- with Suppress_Assignment_Checks flag set. |
| 2202 |
|
| 2203 |
if Nkind (Assign) = N_Assignment_Statement |
| 2204 |
and then Suppress_Assignment_Checks (Assign) |
| 2205 |
then |
| 2206 |
return; |
| 2207 |
end if; |
| 2208 |
|
| 2209 |
-- No check is needed for the initialization of an object whose |
| 2210 |
-- nominal subtype is unconstrained. |
| 2211 |
|
| 2212 |
if Is_Constr_Subt_For_U_Nominal (Target_Typ) |
| 2213 |
and then Nkind (Parent (Assign)) = N_Freeze_Entity |
| 2214 |
and then Is_Entity_Name (Target) |
| 2215 |
and then Entity (Target) = Entity (Parent (Assign)) |
| 2216 |
then |
| 2217 |
return; |
| 2218 |
end if; |
| 2219 |
|
| 2220 |
Apply_Selected_Length_Checks |
| 2221 |
(Expr, Target_Typ, Source_Typ, Do_Static => False); |
| 2222 |
end Apply_Length_Check_On_Assignment; |
| 2223 |
|
| 2224 |
------------------------------------- |
| 2225 |
-- Apply_Parameter_Aliasing_Checks -- |
| 2226 |
------------------------------------- |
| 2227 |
|
| 2228 |
procedure Apply_Parameter_Aliasing_Checks |
| 2229 |
(Call : Node_Id; |
| 2230 |
Subp : Entity_Id) |
| 2231 |
is |
| 2232 |
Loc : constant Source_Ptr := Sloc (Call); |
| 2233 |
|
| 2234 |
function Parameter_Passing_Mechanism_Specified |
| 2235 |
(Typ : Entity_Id) |
| 2236 |
return Boolean; |
| 2237 |
-- Returns True if parameter-passing mechanism is specified for type Typ |
| 2238 |
|
| 2239 |
function May_Cause_Aliasing |
| 2240 |
(Formal_1 : Entity_Id; |
| 2241 |
Formal_2 : Entity_Id) return Boolean; |
| 2242 |
-- Determine whether two formal parameters can alias each other |
| 2243 |
-- depending on their modes. |
| 2244 |
|
| 2245 |
function Original_Actual (N : Node_Id) return Node_Id; |
| 2246 |
-- The expander may replace an actual with a temporary for the sake of |
| 2247 |
-- side effect removal. The temporary may hide a potential aliasing as |
| 2248 |
-- it does not share the address of the actual. This routine attempts |
| 2249 |
-- to retrieve the original actual. |
| 2250 |
|
| 2251 |
procedure Overlap_Check |
| 2252 |
(Actual_1 : Node_Id; |
| 2253 |
Actual_2 : Node_Id; |
| 2254 |
Formal_1 : Entity_Id; |
| 2255 |
Formal_2 : Entity_Id; |
| 2256 |
Check : in out Node_Id); |
| 2257 |
-- Create a check to determine whether Actual_1 overlaps with Actual_2. |
| 2258 |
-- If detailed exception messages are enabled, the check is augmented to |
| 2259 |
-- provide information about the names of the corresponding formals. See |
| 2260 |
-- the body for details. Actual_1 and Actual_2 denote the two actuals to |
| 2261 |
-- be tested. Formal_1 and Formal_2 denote the corresponding formals. |
| 2262 |
-- Check contains all and-ed simple tests generated so far or remains |
| 2263 |
-- unchanged in the case of detailed exception messaged. |
| 2264 |
|
| 2265 |
------------------------------------------- |
| 2266 |
-- Parameter_Passing_Mechanism_Specified -- |
| 2267 |
------------------------------------------- |
| 2268 |
|
| 2269 |
function Parameter_Passing_Mechanism_Specified |
| 2270 |
(Typ : Entity_Id) |
| 2271 |
return Boolean |
| 2272 |
is |
| 2273 |
begin |
| 2274 |
return Is_Elementary_Type (Typ) |
| 2275 |
or else Is_By_Reference_Type (Typ); |
| 2276 |
end Parameter_Passing_Mechanism_Specified; |
| 2277 |
|
| 2278 |
------------------------ |
| 2279 |
-- May_Cause_Aliasing -- |
| 2280 |
------------------------ |
| 2281 |
|
| 2282 |
function May_Cause_Aliasing |
| 2283 |
(Formal_1 : Entity_Id; |
| 2284 |
Formal_2 : Entity_Id) return Boolean |
| 2285 |
is |
| 2286 |
begin |
| 2287 |
-- The following combination cannot lead to aliasing |
| 2288 |
|
| 2289 |
-- Formal 1 Formal 2 |
| 2290 |
-- IN IN |
| 2291 |
|
| 2292 |
if Ekind (Formal_1) = E_In_Parameter |
| 2293 |
and then |
| 2294 |
Ekind (Formal_2) = E_In_Parameter |
| 2295 |
then |
| 2296 |
return False; |
| 2297 |
|
| 2298 |
-- The following combinations may lead to aliasing |
| 2299 |
|
| 2300 |
-- Formal 1 Formal 2 |
| 2301 |
-- IN OUT |
| 2302 |
-- IN IN OUT |
| 2303 |
-- OUT IN |
| 2304 |
-- OUT IN OUT |
| 2305 |
-- OUT OUT |
| 2306 |
|
| 2307 |
else |
| 2308 |
return True; |
| 2309 |
end if; |
| 2310 |
end May_Cause_Aliasing; |
| 2311 |
|
| 2312 |
--------------------- |
| 2313 |
-- Original_Actual -- |
| 2314 |
--------------------- |
| 2315 |
|
| 2316 |
function Original_Actual (N : Node_Id) return Node_Id is |
| 2317 |
begin |
| 2318 |
if Nkind (N) = N_Type_Conversion then |
| 2319 |
return Expression (N); |
| 2320 |
|
| 2321 |
-- The expander created a temporary to capture the result of a type |
| 2322 |
-- conversion where the expression is the real actual. |
| 2323 |
|
| 2324 |
elsif Nkind (N) = N_Identifier |
| 2325 |
and then Present (Original_Node (N)) |
| 2326 |
and then Nkind (Original_Node (N)) = N_Type_Conversion |
| 2327 |
then |
| 2328 |
return Expression (Original_Node (N)); |
| 2329 |
end if; |
| 2330 |
|
| 2331 |
return N; |
| 2332 |
end Original_Actual; |
| 2333 |
|
| 2334 |
------------------- |
| 2335 |
-- Overlap_Check -- |
| 2336 |
------------------- |
| 2337 |
|
| 2338 |
procedure Overlap_Check |
| 2339 |
(Actual_1 : Node_Id; |
| 2340 |
Actual_2 : Node_Id; |
| 2341 |
Formal_1 : Entity_Id; |
| 2342 |
Formal_2 : Entity_Id; |
| 2343 |
Check : in out Node_Id) |
| 2344 |
is |
| 2345 |
Cond : Node_Id; |
| 2346 |
Formal_Name : Bounded_String; |
| 2347 |
|
| 2348 |
begin |
| 2349 |
-- Generate: |
| 2350 |
-- Actual_1'Overlaps_Storage (Actual_2) |
| 2351 |
|
| 2352 |
Cond := |
| 2353 |
Make_Attribute_Reference (Loc, |
| 2354 |
Prefix => New_Copy_Tree (Original_Actual (Actual_1)), |
| 2355 |
Attribute_Name => Name_Overlaps_Storage, |
| 2356 |
Expressions => |
| 2357 |
New_List (New_Copy_Tree (Original_Actual (Actual_2)))); |
| 2358 |
|
| 2359 |
-- Generate the following check when detailed exception messages are |
| 2360 |
-- enabled: |
| 2361 |
|
| 2362 |
-- if Actual_1'Overlaps_Storage (Actual_2) then |
| 2363 |
-- raise Program_Error with <detailed message>; |
| 2364 |
-- end if; |
| 2365 |
|
| 2366 |
if Exception_Extra_Info then |
| 2367 |
Start_String; |
| 2368 |
|
| 2369 |
-- Do not generate location information for internal calls |
| 2370 |
|
| 2371 |
if Comes_From_Source (Call) then |
| 2372 |
Store_String_Chars (Build_Location_String (Loc)); |
| 2373 |
Store_String_Char (' '); |
| 2374 |
end if; |
| 2375 |
|
| 2376 |
Store_String_Chars ("aliased parameters, actuals for """); |
| 2377 |
|
| 2378 |
Append (Formal_Name, Chars (Formal_1)); |
| 2379 |
Adjust_Name_Case (Formal_Name, Sloc (Formal_1)); |
| 2380 |
Store_String_Chars (To_String (Formal_Name)); |
| 2381 |
|
| 2382 |
Store_String_Chars (""" and """); |
| 2383 |
|
| 2384 |
Formal_Name.Length := 0; |
| 2385 |
|
| 2386 |
Append (Formal_Name, Chars (Formal_2)); |
| 2387 |
Adjust_Name_Case (Formal_Name, Sloc (Formal_2)); |
| 2388 |
Store_String_Chars (To_String (Formal_Name)); |
| 2389 |
|
| 2390 |
Store_String_Chars (""" overlap"); |
| 2391 |
|
| 2392 |
Insert_Action (Call, |
| 2393 |
Make_If_Statement (Loc, |
| 2394 |
Condition => Cond, |
| 2395 |
Then_Statements => New_List ( |
| 2396 |
Make_Raise_Statement (Loc, |
| 2397 |
Name => |
| 2398 |
New_Occurrence_Of (Standard_Program_Error, Loc), |
| 2399 |
Expression => Make_String_Literal (Loc, End_String))))); |
| 2400 |
|
| 2401 |
-- Create a sequence of overlapping checks by and-ing them all |
| 2402 |
-- together. |
| 2403 |
|
| 2404 |
else |
| 2405 |
if No (Check) then |
| 2406 |
Check := Cond; |
| 2407 |
else |
| 2408 |
Check := |
| 2409 |
Make_And_Then (Loc, |
| 2410 |
Left_Opnd => Check, |
| 2411 |
Right_Opnd => Cond); |
| 2412 |
end if; |
| 2413 |
end if; |
| 2414 |
end Overlap_Check; |
| 2415 |
|
| 2416 |
-- Local variables |
| 2417 |
|
| 2418 |
Actual_1 : Node_Id; |
| 2419 |
Actual_2 : Node_Id; |
| 2420 |
Check : Node_Id; |
| 2421 |
Formal_1 : Entity_Id; |
| 2422 |
Formal_2 : Entity_Id; |
| 2423 |
Orig_Act_1 : Node_Id; |
| 2424 |
Orig_Act_2 : Node_Id; |
| 2425 |
|
| 2426 |
-- Start of processing for Apply_Parameter_Aliasing_Checks |
| 2427 |
|
| 2428 |
begin |
| 2429 |
Check := Empty; |
| 2430 |
|
| 2431 |
Actual_1 := First_Actual (Call); |
| 2432 |
Formal_1 := First_Formal (Subp); |
| 2433 |
while Present (Actual_1) and then Present (Formal_1) loop |
| 2434 |
Orig_Act_1 := Original_Actual (Actual_1); |
| 2435 |
|
| 2436 |
if Is_Name_Reference (Orig_Act_1) then |
| 2437 |
Actual_2 := Next_Actual (Actual_1); |
| 2438 |
Formal_2 := Next_Formal (Formal_1); |
| 2439 |
while Present (Actual_2) and then Present (Formal_2) loop |
| 2440 |
Orig_Act_2 := Original_Actual (Actual_2); |
| 2441 |
|
| 2442 |
-- Generate the check only when the mode of the two formals may |
| 2443 |
-- lead to aliasing. |
| 2444 |
|
| 2445 |
if Is_Name_Reference (Orig_Act_2) |
| 2446 |
and then May_Cause_Aliasing (Formal_1, Formal_2) |
| 2447 |
then |
| 2448 |
|
| 2449 |
-- The aliasing check only applies when some of the formals |
| 2450 |
-- have their passing mechanism unspecified; RM 6.2 (12/3). |
| 2451 |
|
| 2452 |
if Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_1)) |
| 2453 |
and then |
| 2454 |
Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_2)) |
| 2455 |
then |
| 2456 |
null; |
| 2457 |
else |
| 2458 |
Remove_Side_Effects (Actual_1); |
| 2459 |
Remove_Side_Effects (Actual_2); |
| 2460 |
|
| 2461 |
Overlap_Check |
| 2462 |
(Actual_1 => Actual_1, |
| 2463 |
Actual_2 => Actual_2, |
| 2464 |
Formal_1 => Formal_1, |
| 2465 |
Formal_2 => Formal_2, |
| 2466 |
Check => Check); |
| 2467 |
end if; |
| 2468 |
end if; |
| 2469 |
|
| 2470 |
Next_Actual (Actual_2); |
| 2471 |
Next_Formal (Formal_2); |
| 2472 |
end loop; |
| 2473 |
end if; |
| 2474 |
|
| 2475 |
Next_Actual (Actual_1); |
| 2476 |
Next_Formal (Formal_1); |
| 2477 |
end loop; |
| 2478 |
|
| 2479 |
-- Place a simple check right before the call |
| 2480 |
|
| 2481 |
if Present (Check) and then not Exception_Extra_Info then |
| 2482 |
Insert_Action (Call, |
| 2483 |
Make_Raise_Program_Error (Loc, |
| 2484 |
Condition => Check, |
| 2485 |
Reason => PE_Aliased_Parameters)); |
| 2486 |
end if; |
| 2487 |
end Apply_Parameter_Aliasing_Checks; |
| 2488 |
|
| 2489 |
------------------------------------- |
| 2490 |
-- Apply_Parameter_Validity_Checks -- |
| 2491 |
------------------------------------- |
| 2492 |
|
| 2493 |
procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is |
| 2494 |
Subp_Decl : Node_Id; |
| 2495 |
|
| 2496 |
procedure Add_Validity_Check |
| 2497 |
(Formal : Entity_Id; |
| 2498 |
Prag_Nam : Name_Id; |
| 2499 |
For_Result : Boolean := False); |
| 2500 |
-- Add a single 'Valid[_Scalars] check which verifies the initialization |
| 2501 |
-- of Formal. Prag_Nam denotes the pre or post condition pragma name. |
| 2502 |
-- Set flag For_Result when to verify the result of a function. |
| 2503 |
|
| 2504 |
------------------------ |
| 2505 |
-- Add_Validity_Check -- |
| 2506 |
------------------------ |
| 2507 |
|
| 2508 |
procedure Add_Validity_Check |
| 2509 |
(Formal : Entity_Id; |
| 2510 |
Prag_Nam : Name_Id; |
| 2511 |
For_Result : Boolean := False) |
| 2512 |
is |
| 2513 |
procedure Build_Pre_Post_Condition (Expr : Node_Id); |
| 2514 |
-- Create a pre/postcondition pragma that tests expression Expr |
| 2515 |
|
| 2516 |
------------------------------ |
| 2517 |
-- Build_Pre_Post_Condition -- |
| 2518 |
------------------------------ |
| 2519 |
|
| 2520 |
procedure Build_Pre_Post_Condition (Expr : Node_Id) is |
| 2521 |
Loc : constant Source_Ptr := Sloc (Subp); |
| 2522 |
Decls : List_Id; |
| 2523 |
Prag : Node_Id; |
| 2524 |
|
| 2525 |
begin |
| 2526 |
Prag := |
| 2527 |
Make_Pragma (Loc, |
| 2528 |
Chars => Prag_Nam, |
| 2529 |
Pragma_Argument_Associations => New_List ( |
| 2530 |
Make_Pragma_Argument_Association (Loc, |
| 2531 |
Chars => Name_Check, |
| 2532 |
Expression => Expr))); |
| 2533 |
|
| 2534 |
-- The check is enabled unconditionally |
| 2535 |
|
| 2536 |
Set_Is_Checked (Prag); |
| 2537 |
|
| 2538 |
-- Add a message unless exception messages are suppressed |
| 2539 |
|
| 2540 |
if not Exception_Locations_Suppressed then |
| 2541 |
Append_To (Pragma_Argument_Associations (Prag), |
| 2542 |
Make_Pragma_Argument_Association (Loc, |
| 2543 |
Chars => Name_Message, |
| 2544 |
Expression => |
| 2545 |
Make_String_Literal (Loc, |
| 2546 |
Strval => "failed " |
| 2547 |
& Get_Name_String (Prag_Nam) |
| 2548 |
& " from " |
| 2549 |
& Build_Location_String (Loc)))); |
| 2550 |
end if; |
| 2551 |
|
| 2552 |
-- Insert the pragma in the tree |
| 2553 |
|
| 2554 |
if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then |
| 2555 |
Add_Global_Declaration (Prag); |
| 2556 |
Analyze (Prag); |
| 2557 |
|
| 2558 |
-- PPC pragmas associated with subprogram bodies must be inserted |
| 2559 |
-- in the declarative part of the body. |
| 2560 |
|
| 2561 |
elsif Nkind (Subp_Decl) = N_Subprogram_Body then |
| 2562 |
Decls := Declarations (Subp_Decl); |
| 2563 |
|
| 2564 |
if No (Decls) then |
| 2565 |
Decls := New_List; |
| 2566 |
Set_Declarations (Subp_Decl, Decls); |
| 2567 |
end if; |
| 2568 |
|
| 2569 |
Prepend_To (Decls, Prag); |
| 2570 |
Analyze (Prag); |
| 2571 |
|
| 2572 |
-- For subprogram declarations insert the PPC pragma right after |
| 2573 |
-- the declarative node. |
| 2574 |
|
| 2575 |
else |
| 2576 |
Insert_After_And_Analyze (Subp_Decl, Prag); |
| 2577 |
end if; |
| 2578 |
end Build_Pre_Post_Condition; |
| 2579 |
|
| 2580 |
-- Local variables |
| 2581 |
|
| 2582 |
Loc : constant Source_Ptr := Sloc (Subp); |
| 2583 |
Typ : constant Entity_Id := Etype (Formal); |
| 2584 |
Check : Node_Id; |
| 2585 |
Nam : Name_Id; |
| 2586 |
|
| 2587 |
-- Start of processing for Add_Validity_Check |
| 2588 |
|
| 2589 |
begin |
| 2590 |
-- For scalars, generate 'Valid test |
| 2591 |
|
| 2592 |
if Is_Scalar_Type (Typ) then |
| 2593 |
Nam := Name_Valid; |
| 2594 |
|
| 2595 |
-- For non-scalars with scalar parts, generate 'Valid_Scalars test, |
| 2596 |
-- except for unchecked unions since we cannot know where they are. |
| 2597 |
|
| 2598 |
elsif Scalar_Part_Present (Typ) |
| 2599 |
and then not Is_Unchecked_Union (Typ) |
| 2600 |
then |
| 2601 |
Nam := Name_Valid_Scalars; |
| 2602 |
|
| 2603 |
-- No test needed for other cases (no scalars to test) |
| 2604 |
|
| 2605 |
else |
| 2606 |
return; |
| 2607 |
end if; |
| 2608 |
|
| 2609 |
-- Step 1: Create the expression to verify the validity of the |
| 2610 |
-- context. |
| 2611 |
|
| 2612 |
Check := New_Occurrence_Of (Formal, Loc); |
| 2613 |
|
| 2614 |
-- When processing a function result, use 'Result. Generate |
| 2615 |
-- Context'Result |
| 2616 |
|
| 2617 |
if For_Result then |
| 2618 |
Check := |
| 2619 |
Make_Attribute_Reference (Loc, |
| 2620 |
Prefix => Check, |
| 2621 |
Attribute_Name => Name_Result); |
| 2622 |
end if; |
| 2623 |
|
| 2624 |
-- Generate: |
| 2625 |
-- Context['Result]'Valid[_Scalars] |
| 2626 |
|
| 2627 |
Check := |
| 2628 |
Make_Attribute_Reference (Loc, |
| 2629 |
Prefix => Check, |
| 2630 |
Attribute_Name => Nam); |
| 2631 |
|
| 2632 |
-- Step 2: Create a pre or post condition pragma |
| 2633 |
|
| 2634 |
Build_Pre_Post_Condition (Check); |
| 2635 |
end Add_Validity_Check; |
| 2636 |
|
| 2637 |
-- Local variables |
| 2638 |
|
| 2639 |
Formal : Entity_Id; |
| 2640 |
Subp_Spec : Node_Id; |
| 2641 |
|
| 2642 |
-- Start of processing for Apply_Parameter_Validity_Checks |
| 2643 |
|
| 2644 |
begin |
| 2645 |
-- Extract the subprogram specification and declaration nodes |
| 2646 |
|
| 2647 |
Subp_Spec := Parent (Subp); |
| 2648 |
|
| 2649 |
if No (Subp_Spec) then |
| 2650 |
return; |
| 2651 |
end if; |
| 2652 |
|
| 2653 |
if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
| 2654 |
Subp_Spec := Parent (Subp_Spec); |
| 2655 |
end if; |
| 2656 |
|
| 2657 |
Subp_Decl := Parent (Subp_Spec); |
| 2658 |
|
| 2659 |
if not Comes_From_Source (Subp) |
| 2660 |
|
| 2661 |
-- Do not process formal subprograms because the corresponding actual |
| 2662 |
-- will receive the proper checks when the instance is analyzed. |
| 2663 |
|
| 2664 |
or else Is_Formal_Subprogram (Subp) |
| 2665 |
|
| 2666 |
-- Do not process imported subprograms since pre and postconditions |
| 2667 |
-- are never verified on routines coming from a different language. |
| 2668 |
|
| 2669 |
or else Is_Imported (Subp) |
| 2670 |
or else Is_Intrinsic_Subprogram (Subp) |
| 2671 |
|
| 2672 |
-- The PPC pragmas generated by this routine do not correspond to |
| 2673 |
-- source aspects, therefore they cannot be applied to abstract |
| 2674 |
-- subprograms. |
| 2675 |
|
| 2676 |
or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
| 2677 |
|
| 2678 |
-- Do not consider subprogram renaminds because the renamed entity |
| 2679 |
-- already has the proper PPC pragmas. |
| 2680 |
|
| 2681 |
or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration |
| 2682 |
|
| 2683 |
-- Do not process null procedures because there is no benefit of |
| 2684 |
-- adding the checks to a no action routine. |
| 2685 |
|
| 2686 |
or else (Nkind (Subp_Spec) = N_Procedure_Specification |
| 2687 |
and then Null_Present (Subp_Spec)) |
| 2688 |
then |
| 2689 |
return; |
| 2690 |
end if; |
| 2691 |
|
| 2692 |
-- Apply scalar initialization checks to formals where applicable |
| 2693 |
|
| 2694 |
Formal := First_Formal (Subp); |
| 2695 |
while Present (Formal) loop |
| 2696 |
|
| 2697 |
-- Generate the following scalar initialization checks for each |
| 2698 |
-- formal parameter: |
| 2699 |
|
| 2700 |
-- mode IN - Pre => Formal'Valid[_Scalars] |
| 2701 |
-- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] |
| 2702 |
-- mode OUT - Post => Formal'Valid[_Scalars] |
| 2703 |
|
| 2704 |
if Ekind (Formal) in E_In_Parameter | E_In_Out_Parameter then |
| 2705 |
Add_Validity_Check (Formal, Name_Precondition, False); |
| 2706 |
end if; |
| 2707 |
|
| 2708 |
if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then |
| 2709 |
Add_Validity_Check (Formal, Name_Postcondition, False); |
| 2710 |
end if; |
| 2711 |
|
| 2712 |
Next_Formal (Formal); |
| 2713 |
end loop; |
| 2714 |
|
| 2715 |
-- Generate following scalar initialization check for function result: |
| 2716 |
|
| 2717 |
-- Post => Subp'Result'Valid[_Scalars] |
| 2718 |
|
| 2719 |
if Ekind (Subp) = E_Function then |
| 2720 |
Add_Validity_Check (Subp, Name_Postcondition, True); |
| 2721 |
end if; |
| 2722 |
end Apply_Parameter_Validity_Checks; |
| 2723 |
|
| 2724 |
--------------------------- |
| 2725 |
-- Apply_Predicate_Check -- |
| 2726 |
--------------------------- |
| 2727 |
|
| 2728 |
procedure Apply_Predicate_Check |
| 2729 |
(N : Node_Id; |
| 2730 |
Typ : Entity_Id; |
| 2731 |
Deref : Boolean := False; |
| 2732 |
Fun : Entity_Id := Empty) |
| 2733 |
is |
| 2734 |
Loc : constant Source_Ptr := Sloc (N); |
| 2735 |
Check_Disabled : constant Boolean := |
| 2736 |
not Predicate_Enabled (Typ) |
| 2737 |
or else not Predicate_Check_In_Scope (N); |
| 2738 |
|
| 2739 |
Expr : Node_Id; |
| 2740 |
Par : Node_Id; |
| 2741 |
S : Entity_Id; |
| 2742 |
|
| 2743 |
begin |
| 2744 |
S := Current_Scope; |
| 2745 |
while Present (S) and then not Is_Subprogram (S) loop |
| 2746 |
S := Scope (S); |
| 2747 |
end loop; |
| 2748 |
|
| 2749 |
-- If the check appears within the predicate function itself, it means |
| 2750 |
-- that the user specified a check whose formal is the predicated |
| 2751 |
-- subtype itself, rather than some covering type. This is likely to be |
| 2752 |
-- a common error, and thus deserves a warning. We want to emit this |
| 2753 |
-- warning even if predicate checking is disabled (in which case the |
| 2754 |
-- warning is still useful even if it is not strictly accurate). |
| 2755 |
|
| 2756 |
if Present (S) and then S = Predicate_Function (Typ) then |
| 2757 |
Error_Msg_NE |
| 2758 |
("predicate check includes a call to& that requires a " |
| 2759 |
& "predicate check??", Parent (N), Fun); |
| 2760 |
Error_Msg_N |
| 2761 |
("\this will result in infinite recursion??", Parent (N)); |
| 2762 |
|
| 2763 |
if Is_First_Subtype (Typ) then |
| 2764 |
Error_Msg_NE |
| 2765 |
("\use an explicit subtype of& to carry the predicate", |
| 2766 |
Parent (N), Typ); |
| 2767 |
end if; |
| 2768 |
|
| 2769 |
if not Check_Disabled then |
| 2770 |
Insert_Action (N, |
| 2771 |
Make_Raise_Storage_Error (Loc, |
| 2772 |
Reason => SE_Infinite_Recursion)); |
| 2773 |
return; |
| 2774 |
end if; |
| 2775 |
end if; |
| 2776 |
|
| 2777 |
if Check_Disabled then |
| 2778 |
return; |
| 2779 |
end if; |
| 2780 |
|
| 2781 |
-- Normal case of predicate active |
| 2782 |
|
| 2783 |
-- If the expression is an IN parameter, the predicate will have |
| 2784 |
-- been applied at the point of call. An additional check would |
| 2785 |
-- be redundant, or will lead to out-of-scope references if the |
| 2786 |
-- call appears within an aspect specification for a precondition. |
| 2787 |
|
| 2788 |
-- However, if the reference is within the body of the subprogram |
| 2789 |
-- that declares the formal, the predicate can safely be applied, |
| 2790 |
-- which may be necessary for a nested call whose formal has a |
| 2791 |
-- different predicate. |
| 2792 |
|
| 2793 |
if Is_Entity_Name (N) |
| 2794 |
and then Ekind (Entity (N)) = E_In_Parameter |
| 2795 |
then |
| 2796 |
declare |
| 2797 |
In_Body : Boolean := False; |
| 2798 |
P : Node_Id := Parent (N); |
| 2799 |
|
| 2800 |
begin |
| 2801 |
while Present (P) loop |
| 2802 |
if Nkind (P) = N_Subprogram_Body |
| 2803 |
and then |
| 2804 |
((Present (Corresponding_Spec (P)) |
| 2805 |
and then |
| 2806 |
Corresponding_Spec (P) = Scope (Entity (N))) |
| 2807 |
or else |
| 2808 |
Defining_Unit_Name (Specification (P)) = |
| 2809 |
Scope (Entity (N))) |
| 2810 |
then |
| 2811 |
In_Body := True; |
| 2812 |
exit; |
| 2813 |
end if; |
| 2814 |
|
| 2815 |
P := Parent (P); |
| 2816 |
end loop; |
| 2817 |
|
| 2818 |
if not In_Body then |
| 2819 |
return; |
| 2820 |
end if; |
| 2821 |
end; |
| 2822 |
end if; |
| 2823 |
|
| 2824 |
-- If the type has a static predicate and the expression is known |
| 2825 |
-- at compile time, see if the expression satisfies the predicate. |
| 2826 |
|
| 2827 |
Check_Expression_Against_Static_Predicate (N, Typ); |
| 2828 |
|
| 2829 |
if not Expander_Active then |
| 2830 |
return; |
| 2831 |
end if; |
| 2832 |
|
| 2833 |
Par := Parent (N); |
| 2834 |
if Nkind (Par) = N_Qualified_Expression then |
| 2835 |
Par := Parent (Par); |
| 2836 |
end if; |
| 2837 |
|
| 2838 |
-- Try to avoid creating a temporary if the expression is an aggregate |
| 2839 |
|
| 2840 |
if Nkind (N) in N_Aggregate | N_Extension_Aggregate then |
| 2841 |
|
| 2842 |
-- If the expression is an aggregate in an assignment, apply the |
| 2843 |
-- check to the LHS after the assignment, rather than create a |
| 2844 |
-- redundant temporary. This is only necessary in rare cases |
| 2845 |
-- of array types (including strings) initialized with an |
| 2846 |
-- aggregate with an "others" clause, either coming from source |
| 2847 |
-- or generated by an Initialize_Scalars pragma. |
| 2848 |
|
| 2849 |
if Nkind (Par) = N_Assignment_Statement then |
| 2850 |
Insert_Action_After (Par, |
| 2851 |
Make_Predicate_Check |
| 2852 |
(Typ, Duplicate_Subexpr (Name (Par)))); |
| 2853 |
return; |
| 2854 |
|
| 2855 |
-- Similarly, if the expression is an aggregate in an object |
| 2856 |
-- declaration, apply it to the object after the declaration. |
| 2857 |
|
| 2858 |
-- This is only necessary in cases of tagged extensions |
| 2859 |
-- initialized with an aggregate with an "others => <>" clause, |
| 2860 |
-- when the subtypes of LHS and RHS do not statically match or |
| 2861 |
-- when we know the object's type will be rewritten later. |
| 2862 |
-- The condition for the later is copied from the |
| 2863 |
-- Analyze_Object_Declaration procedure when it actually builds the |
| 2864 |
-- subtype. |
| 2865 |
|
| 2866 |
elsif Nkind (Par) = N_Object_Declaration then |
| 2867 |
if Subtypes_Statically_Match |
| 2868 |
(Etype (Defining_Identifier (Par)), Typ) |
| 2869 |
and then (Nkind (N) = N_Extension_Aggregate |
| 2870 |
or else (Is_Definite_Subtype (Typ) |
| 2871 |
and then Build_Default_Subtype_OK (Typ))) |
| 2872 |
then |
| 2873 |
Insert_Action_After (Par, |
| 2874 |
Make_Predicate_Check (Typ, |
| 2875 |
New_Occurrence_Of (Defining_Identifier (Par), Loc))); |
| 2876 |
return; |
| 2877 |
end if; |
| 2878 |
|
| 2879 |
end if; |
| 2880 |
end if; |
| 2881 |
|
| 2882 |
-- For an entity of the type, generate a call to the predicate |
| 2883 |
-- function, unless its type is an actual subtype, which is not |
| 2884 |
-- visible outside of the enclosing subprogram. |
| 2885 |
|
| 2886 |
if Is_Entity_Name (N) and then not Is_Actual_Subtype (Typ) then |
| 2887 |
Expr := New_Occurrence_Of (Entity (N), Loc); |
| 2888 |
|
| 2889 |
-- If the expression is not an entity, it may have side effects |
| 2890 |
|
| 2891 |
else |
| 2892 |
Expr := Duplicate_Subexpr (N); |
| 2893 |
end if; |
| 2894 |
|
| 2895 |
-- Make the dereference if requested |
| 2896 |
|
| 2897 |
if Deref then |
| 2898 |
Expr := Make_Explicit_Dereference (Loc, Prefix => Expr); |
| 2899 |
|
| 2900 |
-- Preserve Comes_From_Source for Predicate_Check_In_Scope |
| 2901 |
|
| 2902 |
Preserve_Comes_From_Source (Expr, N); |
| 2903 |
end if; |
| 2904 |
|
| 2905 |
-- Disable checks to prevent an infinite recursion |
| 2906 |
|
| 2907 |
Insert_Action |
| 2908 |
(N, Make_Predicate_Check (Typ, Expr), Suppress => All_Checks); |
| 2909 |
end Apply_Predicate_Check; |
| 2910 |
|
| 2911 |
----------------------- |
| 2912 |
-- Apply_Raise_Check -- |
| 2913 |
----------------------- |
| 2914 |
|
| 2915 |
procedure Apply_Raise_Check (N : Node_Id) is |
| 2916 |
Loc : constant Source_Ptr := Sloc (N); |
| 2917 |
|
| 2918 |
Block : Node_Id; |
| 2919 |
Block_Id : Entity_Id; |
| 2920 |
HSS : Node_Id; |
| 2921 |
Spec_Id : Entity_Id; |
| 2922 |
|
| 2923 |
begin |
| 2924 |
pragma Assert (Nkind (N) = N_Subprogram_Body); |
| 2925 |
|
| 2926 |
if Present (Corresponding_Spec (N)) then |
| 2927 |
Spec_Id := Corresponding_Spec (N); |
| 2928 |
else |
| 2929 |
Spec_Id := Defining_Entity (N); |
| 2930 |
end if; |
| 2931 |
|
| 2932 |
-- Return immediately if the check is not needed or is suppressed |
| 2933 |
|
| 2934 |
if not No_Raise (Spec_Id) or else Raise_Checks_Suppressed (Spec_Id) then |
| 2935 |
return; |
| 2936 |
end if; |
| 2937 |
|
| 2938 |
-- Build a block using the declarations, statements and At_End procedure |
| 2939 |
-- from the subprogram body. |
| 2940 |
|
| 2941 |
Block_Id := New_Internal_Entity (E_Block, Spec_Id, Loc, 'B'); |
| 2942 |
Set_Etype (Block_Id, Standard_Void_Type); |
| 2943 |
Set_Scope (Block_Id, Spec_Id); |
| 2944 |
|
| 2945 |
Block := |
| 2946 |
Make_Block_Statement (Loc, |
| 2947 |
Identifier => New_Occurrence_Of (Block_Id, Loc), |
| 2948 |
Declarations => Declarations (N), |
| 2949 |
Handled_Statement_Sequence => Handled_Statement_Sequence (N), |
| 2950 |
At_End_Proc => At_End_Proc (N)); |
| 2951 |
|
| 2952 |
Set_Parent (Block_Id, Block); |
| 2953 |
|
| 2954 |
-- Wrap the block in a sequence of statements with an Others handler |
| 2955 |
-- and attach it directly to the subprogram body. Generate: |
| 2956 |
-- |
| 2957 |
-- begin |
| 2958 |
-- Bnn : |
| 2959 |
-- ... |
| 2960 |
-- end Bnn; |
| 2961 |
-- exception |
| 2962 |
-- when others => |
| 2963 |
-- [program_error "raise check failed"] |
| 2964 |
-- end |
| 2965 |
|
| 2966 |
HSS := |
| 2967 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 2968 |
Statements => New_List (Block), |
| 2969 |
Exception_Handlers => New_List ( |
| 2970 |
Make_Exception_Handler (Loc, |
| 2971 |
Exception_Choices => New_List (Make_Others_Choice (Loc)), |
| 2972 |
Statements => New_List ( |
| 2973 |
Make_Raise_Program_Error (Loc, |
| 2974 |
Reason => PE_Raise_Check_Failed))))); |
| 2975 |
|
| 2976 |
Set_Declarations (N, No_List); |
| 2977 |
Set_Handled_Statement_Sequence (N, HSS); |
| 2978 |
Set_At_End_Proc (N, Empty); |
| 2979 |
|
| 2980 |
Analyze (HSS); |
| 2981 |
end Apply_Raise_Check; |
| 2982 |
|
| 2983 |
----------------------- |
| 2984 |
-- Apply_Range_Check -- |
| 2985 |
----------------------- |
| 2986 |
|
| 2987 |
procedure Apply_Range_Check |
| 2988 |
(Expr : Node_Id; |
| 2989 |
Target_Typ : Entity_Id; |
| 2990 |
Source_Typ : Entity_Id := Empty; |
| 2991 |
Insert_Node : Node_Id := Empty) |
| 2992 |
is |
| 2993 |
Checks_On : constant Boolean := |
| 2994 |
not Index_Checks_Suppressed (Target_Typ) |
| 2995 |
or else |
| 2996 |
not Range_Checks_Suppressed (Target_Typ); |
| 2997 |
|
| 2998 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 2999 |
|
| 3000 |
Cond : Node_Id; |
| 3001 |
R_Cno : Node_Id; |
| 3002 |
R_Result : Check_Result; |
| 3003 |
|
| 3004 |
begin |
| 3005 |
-- Only apply checks when generating code. In GNATprove mode, we do not |
| 3006 |
-- apply the checks, but we still call Selected_Range_Checks to possibly |
| 3007 |
-- issue errors on SPARK code when a run-time error can be detected at |
| 3008 |
-- compile time. |
| 3009 |
|
| 3010 |
if not GNATprove_Mode then |
| 3011 |
if not Expander_Active or not Checks_On then |
| 3012 |
return; |
| 3013 |
end if; |
| 3014 |
end if; |
| 3015 |
|
| 3016 |
R_Result := |
| 3017 |
Selected_Range_Checks (Expr, Target_Typ, Source_Typ, Insert_Node); |
| 3018 |
|
| 3019 |
if GNATprove_Mode then |
| 3020 |
return; |
| 3021 |
end if; |
| 3022 |
|
| 3023 |
for J in 1 .. 2 loop |
| 3024 |
R_Cno := R_Result (J); |
| 3025 |
exit when No (R_Cno); |
| 3026 |
|
| 3027 |
-- The range check requires runtime evaluation. Depending on what its |
| 3028 |
-- triggering condition is, the check may be converted into a compile |
| 3029 |
-- time constraint check. |
| 3030 |
|
| 3031 |
if Nkind (R_Cno) = N_Raise_Constraint_Error |
| 3032 |
and then Present (Condition (R_Cno)) |
| 3033 |
then |
| 3034 |
Cond := Condition (R_Cno); |
| 3035 |
|
| 3036 |
-- Insert the range check before the related context. Note that |
| 3037 |
-- this action analyses the triggering condition. |
| 3038 |
|
| 3039 |
if Present (Insert_Node) then |
| 3040 |
Insert_Action (Insert_Node, R_Cno); |
| 3041 |
else |
| 3042 |
Insert_Action (Expr, R_Cno); |
| 3043 |
end if; |
| 3044 |
|
| 3045 |
-- The triggering condition evaluates to True, the range check |
| 3046 |
-- can be converted into a compile time constraint check. |
| 3047 |
|
| 3048 |
if Is_Entity_Name (Cond) |
| 3049 |
and then Entity (Cond) = Standard_True |
| 3050 |
then |
| 3051 |
-- Since an N_Range is technically not an expression, we have |
| 3052 |
-- to set one of the bounds to C_E and then just flag the |
| 3053 |
-- N_Range. The warning message will point to the lower bound |
| 3054 |
-- and complain about a range, which seems OK. |
| 3055 |
|
| 3056 |
if Nkind (Expr) = N_Range then |
| 3057 |
Apply_Compile_Time_Constraint_Error |
| 3058 |
(Low_Bound (Expr), |
| 3059 |
"static range out of bounds of}??", |
| 3060 |
CE_Range_Check_Failed, |
| 3061 |
Ent => Target_Typ, |
| 3062 |
Typ => Target_Typ); |
| 3063 |
|
| 3064 |
Set_Raises_Constraint_Error (Expr); |
| 3065 |
|
| 3066 |
else |
| 3067 |
Apply_Compile_Time_Constraint_Error |
| 3068 |
(Expr, |
| 3069 |
"static value out of range of}??", |
| 3070 |
CE_Range_Check_Failed, |
| 3071 |
Ent => Target_Typ, |
| 3072 |
Typ => Target_Typ); |
| 3073 |
end if; |
| 3074 |
end if; |
| 3075 |
|
| 3076 |
-- The range check raises Constraint_Error explicitly |
| 3077 |
|
| 3078 |
elsif Present (Insert_Node) then |
| 3079 |
R_Cno := |
| 3080 |
Make_Raise_Constraint_Error (Sloc (Insert_Node), |
| 3081 |
Reason => CE_Range_Check_Failed); |
| 3082 |
|
| 3083 |
Insert_Action (Insert_Node, R_Cno); |
| 3084 |
|
| 3085 |
else |
| 3086 |
Install_Static_Check (R_Cno, Loc, CE_Range_Check_Failed); |
| 3087 |
end if; |
| 3088 |
end loop; |
| 3089 |
end Apply_Range_Check; |
| 3090 |
|
| 3091 |
------------------------------ |
| 3092 |
-- Apply_Scalar_Range_Check -- |
| 3093 |
------------------------------ |
| 3094 |
|
| 3095 |
-- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
| 3096 |
-- off if it is already set on. |
| 3097 |
|
| 3098 |
procedure Apply_Scalar_Range_Check |
| 3099 |
(Expr : Node_Id; |
| 3100 |
Target_Typ : Entity_Id; |
| 3101 |
Source_Typ : Entity_Id := Empty; |
| 3102 |
Fixed_Int : Boolean := False) |
| 3103 |
is |
| 3104 |
Parnt : constant Node_Id := Parent (Expr); |
| 3105 |
S_Typ : Entity_Id; |
| 3106 |
Arr : Node_Id := Empty; -- initialize to prevent warning |
| 3107 |
Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning |
| 3108 |
|
| 3109 |
Is_Subscr_Ref : Boolean; |
| 3110 |
-- Set true if Expr is a subscript |
| 3111 |
|
| 3112 |
Is_Unconstrained_Subscr_Ref : Boolean; |
| 3113 |
-- Set true if Expr is a subscript of an unconstrained array. In this |
| 3114 |
-- case we do not attempt to do an analysis of the value against the |
| 3115 |
-- range of the subscript, since we don't know the actual subtype. |
| 3116 |
|
| 3117 |
Int_Real : Boolean; |
| 3118 |
-- Set to True if Expr should be regarded as a real value even though |
| 3119 |
-- the type of Expr might be discrete. |
| 3120 |
|
| 3121 |
procedure Bad_Value (Warn : Boolean := False); |
| 3122 |
-- Procedure called if value is determined to be out of range. Warn is |
| 3123 |
-- True to force a warning instead of an error, even when SPARK_Mode is |
| 3124 |
-- On. |
| 3125 |
|
| 3126 |
--------------- |
| 3127 |
-- Bad_Value -- |
| 3128 |
--------------- |
| 3129 |
|
| 3130 |
procedure Bad_Value (Warn : Boolean := False) is |
| 3131 |
begin |
| 3132 |
Apply_Compile_Time_Constraint_Error |
| 3133 |
(Expr, "value not in range of}??", CE_Range_Check_Failed, |
| 3134 |
Ent => Target_Typ, |
| 3135 |
Typ => Target_Typ, |
| 3136 |
Warn => Warn); |
| 3137 |
end Bad_Value; |
| 3138 |
|
| 3139 |
-- Start of processing for Apply_Scalar_Range_Check |
| 3140 |
|
| 3141 |
begin |
| 3142 |
-- Return if check obviously not needed |
| 3143 |
|
| 3144 |
if |
| 3145 |
-- Not needed inside generic |
| 3146 |
|
| 3147 |
Inside_A_Generic |
| 3148 |
|
| 3149 |
-- Not needed if previous error |
| 3150 |
|
| 3151 |
or else Target_Typ = Any_Type |
| 3152 |
or else Nkind (Expr) = N_Error |
| 3153 |
|
| 3154 |
-- Not needed for non-scalar type |
| 3155 |
|
| 3156 |
or else not Is_Scalar_Type (Target_Typ) |
| 3157 |
|
| 3158 |
-- Not needed if we know node raises CE already |
| 3159 |
|
| 3160 |
or else Raises_Constraint_Error (Expr) |
| 3161 |
then |
| 3162 |
return; |
| 3163 |
end if; |
| 3164 |
|
| 3165 |
-- Now, see if checks are suppressed |
| 3166 |
|
| 3167 |
Is_Subscr_Ref := |
| 3168 |
Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; |
| 3169 |
|
| 3170 |
if Is_Subscr_Ref then |
| 3171 |
Arr := Prefix (Parnt); |
| 3172 |
Arr_Typ := Get_Actual_Subtype_If_Available (Arr); |
| 3173 |
|
| 3174 |
if Is_Access_Type (Arr_Typ) then |
| 3175 |
Arr_Typ := Designated_Type (Arr_Typ); |
| 3176 |
end if; |
| 3177 |
end if; |
| 3178 |
|
| 3179 |
if not Do_Range_Check (Expr) then |
| 3180 |
|
| 3181 |
-- Subscript reference. Check for Index_Checks suppressed |
| 3182 |
|
| 3183 |
if Is_Subscr_Ref then |
| 3184 |
|
| 3185 |
-- Check array type and its base type |
| 3186 |
|
| 3187 |
if Index_Checks_Suppressed (Arr_Typ) |
| 3188 |
or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
| 3189 |
then |
| 3190 |
return; |
| 3191 |
|
| 3192 |
-- Check array itself if it is an entity name |
| 3193 |
|
| 3194 |
elsif Is_Entity_Name (Arr) |
| 3195 |
and then Index_Checks_Suppressed (Entity (Arr)) |
| 3196 |
then |
| 3197 |
return; |
| 3198 |
|
| 3199 |
-- Check expression itself if it is an entity name |
| 3200 |
|
| 3201 |
elsif Is_Entity_Name (Expr) |
| 3202 |
and then Index_Checks_Suppressed (Entity (Expr)) |
| 3203 |
then |
| 3204 |
return; |
| 3205 |
end if; |
| 3206 |
|
| 3207 |
-- All other cases, check for Range_Checks suppressed |
| 3208 |
|
| 3209 |
else |
| 3210 |
-- Check target type and its base type |
| 3211 |
|
| 3212 |
if Range_Checks_Suppressed (Target_Typ) |
| 3213 |
or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
| 3214 |
then |
| 3215 |
return; |
| 3216 |
|
| 3217 |
-- Check expression itself if it is an entity name |
| 3218 |
|
| 3219 |
elsif Is_Entity_Name (Expr) |
| 3220 |
and then Range_Checks_Suppressed (Entity (Expr)) |
| 3221 |
then |
| 3222 |
return; |
| 3223 |
|
| 3224 |
-- If Expr is part of an assignment statement, then check left |
| 3225 |
-- side of assignment if it is an entity name. |
| 3226 |
|
| 3227 |
elsif Nkind (Parnt) = N_Assignment_Statement |
| 3228 |
and then Is_Entity_Name (Name (Parnt)) |
| 3229 |
and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
| 3230 |
then |
| 3231 |
return; |
| 3232 |
end if; |
| 3233 |
end if; |
| 3234 |
end if; |
| 3235 |
|
| 3236 |
-- Do not set range checks if they are killed |
| 3237 |
|
| 3238 |
if Nkind (Expr) = N_Unchecked_Type_Conversion |
| 3239 |
and then Kill_Range_Check (Expr) |
| 3240 |
then |
| 3241 |
return; |
| 3242 |
end if; |
| 3243 |
|
| 3244 |
-- Do not set range checks for any values from System.Scalar_Values |
| 3245 |
-- since the whole idea of such values is to avoid checking them. |
| 3246 |
|
| 3247 |
if Is_Entity_Name (Expr) |
| 3248 |
and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) |
| 3249 |
then |
| 3250 |
return; |
| 3251 |
end if; |
| 3252 |
|
| 3253 |
-- Now see if we need a check |
| 3254 |
|
| 3255 |
if No (Source_Typ) then |
| 3256 |
S_Typ := Etype (Expr); |
| 3257 |
else |
| 3258 |
S_Typ := Source_Typ; |
| 3259 |
end if; |
| 3260 |
|
| 3261 |
if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then |
| 3262 |
return; |
| 3263 |
end if; |
| 3264 |
|
| 3265 |
Is_Unconstrained_Subscr_Ref := |
| 3266 |
Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); |
| 3267 |
|
| 3268 |
-- Special checks for floating-point type |
| 3269 |
|
| 3270 |
if Is_Floating_Point_Type (S_Typ) then |
| 3271 |
|
| 3272 |
-- Always do a range check if the source type includes infinities and |
| 3273 |
-- the target type does not include infinities. We do not do this if |
| 3274 |
-- range checks are killed. |
| 3275 |
-- If the expression is a literal and the bounds of the type are |
| 3276 |
-- static constants it may be possible to optimize the check. |
| 3277 |
|
| 3278 |
if Has_Infinities (S_Typ) |
| 3279 |
and then not Has_Infinities (Target_Typ) |
| 3280 |
then |
| 3281 |
-- If the expression is a literal and the bounds of the type are |
| 3282 |
-- static constants it may be possible to optimize the check. |
| 3283 |
|
| 3284 |
if Nkind (Expr) = N_Real_Literal then |
| 3285 |
declare |
| 3286 |
Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); |
| 3287 |
Thi : constant Node_Id := Type_High_Bound (Target_Typ); |
| 3288 |
|
| 3289 |
begin |
| 3290 |
if Compile_Time_Known_Value (Tlo) |
| 3291 |
and then Compile_Time_Known_Value (Thi) |
| 3292 |
and then Expr_Value_R (Expr) >= Expr_Value_R (Tlo) |
| 3293 |
and then Expr_Value_R (Expr) <= Expr_Value_R (Thi) |
| 3294 |
then |
| 3295 |
return; |
| 3296 |
else |
| 3297 |
Enable_Range_Check (Expr); |
| 3298 |
end if; |
| 3299 |
end; |
| 3300 |
|
| 3301 |
else |
| 3302 |
Enable_Range_Check (Expr); |
| 3303 |
end if; |
| 3304 |
end if; |
| 3305 |
end if; |
| 3306 |
|
| 3307 |
-- Return if we know expression is definitely in the range of the target |
| 3308 |
-- type as determined by Determine_Range_To_Discrete. Right now we only |
| 3309 |
-- do this for discrete target types, i.e. neither for fixed-point nor |
| 3310 |
-- for floating-point types. But the additional less precise tests below |
| 3311 |
-- catch these cases. |
| 3312 |
|
| 3313 |
-- Note: skip this if we are given a source_typ, since the point of |
| 3314 |
-- supplying a Source_Typ is to stop us looking at the expression. |
| 3315 |
-- We could sharpen this test to be out parameters only ??? |
| 3316 |
|
| 3317 |
if Is_Discrete_Type (Target_Typ) |
| 3318 |
and then not Is_Unconstrained_Subscr_Ref |
| 3319 |
and then No (Source_Typ) |
| 3320 |
then |
| 3321 |
declare |
| 3322 |
Thi : constant Node_Id := Type_High_Bound (Target_Typ); |
| 3323 |
Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); |
| 3324 |
|
| 3325 |
begin |
| 3326 |
if Compile_Time_Known_Value (Tlo) |
| 3327 |
and then Compile_Time_Known_Value (Thi) |
| 3328 |
then |
| 3329 |
declare |
| 3330 |
OK : Boolean := False; -- initialize to prevent warning |
| 3331 |
Hiv : constant Uint := Expr_Value (Thi); |
| 3332 |
Lov : constant Uint := Expr_Value (Tlo); |
| 3333 |
Hi : Uint := No_Uint; |
| 3334 |
Lo : Uint := No_Uint; |
| 3335 |
|
| 3336 |
begin |
| 3337 |
-- If range is null, we for sure have a constraint error (we |
| 3338 |
-- don't even need to look at the value involved, since all |
| 3339 |
-- possible values will raise CE). |
| 3340 |
|
| 3341 |
if Lov > Hiv then |
| 3342 |
|
| 3343 |
-- When SPARK_Mode is On, force a warning instead of |
| 3344 |
-- an error in that case, as this likely corresponds |
| 3345 |
-- to deactivated code. |
| 3346 |
|
| 3347 |
Bad_Value (Warn => SPARK_Mode = On); |
| 3348 |
|
| 3349 |
return; |
| 3350 |
end if; |
| 3351 |
|
| 3352 |
-- Otherwise determine range of value |
| 3353 |
|
| 3354 |
Determine_Range_To_Discrete |
| 3355 |
(Expr, OK, Lo, Hi, Fixed_Int, Assume_Valid => True); |
| 3356 |
|
| 3357 |
if OK then |
| 3358 |
|
| 3359 |
-- If definitely in range, all OK |
| 3360 |
|
| 3361 |
if Lo >= Lov and then Hi <= Hiv then |
| 3362 |
return; |
| 3363 |
|
| 3364 |
-- If definitely not in range, warn |
| 3365 |
|
| 3366 |
elsif Lov > Hi or else Hiv < Lo then |
| 3367 |
|
| 3368 |
-- Ignore out of range values for System.Priority in |
| 3369 |
-- CodePeer mode since the actual target compiler may |
| 3370 |
-- provide a wider range. |
| 3371 |
|
| 3372 |
if not CodePeer_Mode |
| 3373 |
or else not Is_RTE (Target_Typ, RE_Priority) |
| 3374 |
then |
| 3375 |
Bad_Value; |
| 3376 |
end if; |
| 3377 |
|
| 3378 |
return; |
| 3379 |
|
| 3380 |
-- Otherwise we don't know |
| 3381 |
|
| 3382 |
else |
| 3383 |
null; |
| 3384 |
end if; |
| 3385 |
end if; |
| 3386 |
end; |
| 3387 |
end if; |
| 3388 |
end; |
| 3389 |
end if; |
| 3390 |
|
| 3391 |
Int_Real := |
| 3392 |
Is_Floating_Point_Type (S_Typ) |
| 3393 |
or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); |
| 3394 |
|
| 3395 |
-- Check if we can determine at compile time whether Expr is in the |
| 3396 |
-- range of the target type. Note that if S_Typ is within the bounds |
| 3397 |
-- of Target_Typ then this must be the case. This check is meaningful |
| 3398 |
-- only if this is not a conversion between integer and real types, |
| 3399 |
-- unless for a fixed-point type if Fixed_Int is set. |
| 3400 |
|
| 3401 |
if not Is_Unconstrained_Subscr_Ref |
| 3402 |
and then (Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
| 3403 |
or else (Fixed_Int and then Is_Discrete_Type (Target_Typ))) |
| 3404 |
and then |
| 3405 |
(In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
| 3406 |
|
| 3407 |
-- Also check if the expression itself is in the range of the |
| 3408 |
-- target type if it is a known at compile time value. We skip |
| 3409 |
-- this test if S_Typ is set since for OUT and IN OUT parameters |
| 3410 |
-- the Expr itself is not relevant to the checking. |
| 3411 |
|
| 3412 |
or else |
| 3413 |
(No (Source_Typ) |
| 3414 |
and then Is_In_Range (Expr, Target_Typ, |
| 3415 |
Assume_Valid => True, |
| 3416 |
Fixed_Int => Fixed_Int, |
| 3417 |
Int_Real => Int_Real))) |
| 3418 |
then |
| 3419 |
return; |
| 3420 |
|
| 3421 |
elsif Is_Out_Of_Range (Expr, Target_Typ, |
| 3422 |
Assume_Valid => True, |
| 3423 |
Fixed_Int => Fixed_Int, |
| 3424 |
Int_Real => Int_Real) |
| 3425 |
then |
| 3426 |
Bad_Value; |
| 3427 |
return; |
| 3428 |
|
| 3429 |
-- Floating-point case |
| 3430 |
-- In the floating-point case, we only do range checks if the type is |
| 3431 |
-- constrained. We definitely do NOT want range checks for unconstrained |
| 3432 |
-- types, since we want to have infinities, except when |
| 3433 |
-- Check_Float_Overflow is set. |
| 3434 |
|
| 3435 |
elsif Is_Floating_Point_Type (S_Typ) then |
| 3436 |
if Is_Constrained (S_Typ) or else Check_Float_Overflow then |
| 3437 |
Enable_Range_Check (Expr); |
| 3438 |
end if; |
| 3439 |
|
| 3440 |
-- For all other cases we enable a range check unconditionally |
| 3441 |
|
| 3442 |
else |
| 3443 |
Enable_Range_Check (Expr); |
| 3444 |
return; |
| 3445 |
end if; |
| 3446 |
end Apply_Scalar_Range_Check; |
| 3447 |
|
| 3448 |
---------------------------------- |
| 3449 |
-- Apply_Selected_Length_Checks -- |
| 3450 |
---------------------------------- |
| 3451 |
|
| 3452 |
procedure Apply_Selected_Length_Checks |
| 3453 |
(Expr : Node_Id; |
| 3454 |
Target_Typ : Entity_Id; |
| 3455 |
Source_Typ : Entity_Id; |
| 3456 |
Do_Static : Boolean) |
| 3457 |
is |
| 3458 |
Checks_On : constant Boolean := |
| 3459 |
not Index_Checks_Suppressed (Target_Typ) |
| 3460 |
or else |
| 3461 |
not Length_Checks_Suppressed (Target_Typ); |
| 3462 |
|
| 3463 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 3464 |
|
| 3465 |
Cond : Node_Id; |
| 3466 |
R_Cno : Node_Id; |
| 3467 |
R_Result : Check_Result; |
| 3468 |
|
| 3469 |
begin |
| 3470 |
-- Only apply checks when generating code |
| 3471 |
|
| 3472 |
-- Note: this means that we lose some useful warnings if the expander |
| 3473 |
-- is not active. |
| 3474 |
|
| 3475 |
if not Expander_Active then |
| 3476 |
return; |
| 3477 |
end if; |
| 3478 |
|
| 3479 |
R_Result := |
| 3480 |
Selected_Length_Checks (Expr, Target_Typ, Source_Typ, Empty); |
| 3481 |
|
| 3482 |
for J in 1 .. 2 loop |
| 3483 |
R_Cno := R_Result (J); |
| 3484 |
exit when No (R_Cno); |
| 3485 |
|
| 3486 |
-- A length check may mention an Itype which is attached to a |
| 3487 |
-- subsequent node. At the top level in a package this can cause |
| 3488 |
-- an order-of-elaboration problem, so we make sure that the itype |
| 3489 |
-- is referenced now. |
| 3490 |
|
| 3491 |
if Ekind (Current_Scope) = E_Package |
| 3492 |
and then Is_Compilation_Unit (Current_Scope) |
| 3493 |
then |
| 3494 |
Ensure_Defined (Target_Typ, Expr); |
| 3495 |
|
| 3496 |
if Present (Source_Typ) then |
| 3497 |
Ensure_Defined (Source_Typ, Expr); |
| 3498 |
|
| 3499 |
elsif Is_Itype (Etype (Expr)) then |
| 3500 |
Ensure_Defined (Etype (Expr), Expr); |
| 3501 |
end if; |
| 3502 |
end if; |
| 3503 |
|
| 3504 |
if Nkind (R_Cno) = N_Raise_Constraint_Error |
| 3505 |
and then Present (Condition (R_Cno)) |
| 3506 |
then |
| 3507 |
Cond := Condition (R_Cno); |
| 3508 |
|
| 3509 |
-- Case where node does not now have a dynamic check |
| 3510 |
|
| 3511 |
if not Has_Dynamic_Length_Check (Expr) then |
| 3512 |
|
| 3513 |
-- If checks are on, just insert the check |
| 3514 |
|
| 3515 |
if Checks_On then |
| 3516 |
Insert_Action (Expr, R_Cno); |
| 3517 |
|
| 3518 |
if not Do_Static then |
| 3519 |
Set_Has_Dynamic_Length_Check (Expr); |
| 3520 |
end if; |
| 3521 |
|
| 3522 |
-- If checks are off, then analyze the length check after |
| 3523 |
-- temporarily attaching it to the tree in case the relevant |
| 3524 |
-- condition can be evaluated at compile time. We still want a |
| 3525 |
-- compile time warning in this case. |
| 3526 |
|
| 3527 |
else |
| 3528 |
Set_Parent (R_Cno, Expr); |
| 3529 |
Analyze (R_Cno); |
| 3530 |
end if; |
| 3531 |
end if; |
| 3532 |
|
| 3533 |
-- Output a warning if the condition is known to be True |
| 3534 |
|
| 3535 |
if Is_Entity_Name (Cond) |
| 3536 |
and then Entity (Cond) = Standard_True |
| 3537 |
then |
| 3538 |
Apply_Compile_Time_Constraint_Error |
| 3539 |
(Expr, "wrong length for array of}??", |
| 3540 |
CE_Length_Check_Failed, |
| 3541 |
Ent => Target_Typ, |
| 3542 |
Typ => Target_Typ); |
| 3543 |
|
| 3544 |
-- If we were only doing a static check, or if checks are not |
| 3545 |
-- on, then we want to delete the check, since it is not needed. |
| 3546 |
-- We do this by replacing the if statement by a null statement |
| 3547 |
|
| 3548 |
elsif Do_Static or else not Checks_On then |
| 3549 |
Remove_Warning_Messages (R_Cno); |
| 3550 |
Rewrite (R_Cno, Make_Null_Statement (Loc)); |
| 3551 |
end if; |
| 3552 |
|
| 3553 |
else |
| 3554 |
Install_Static_Check (R_Cno, Loc, CE_Length_Check_Failed); |
| 3555 |
end if; |
| 3556 |
end loop; |
| 3557 |
end Apply_Selected_Length_Checks; |
| 3558 |
|
| 3559 |
------------------------------- |
| 3560 |
-- Apply_Static_Length_Check -- |
| 3561 |
------------------------------- |
| 3562 |
|
| 3563 |
procedure Apply_Static_Length_Check |
| 3564 |
(Expr : Node_Id; |
| 3565 |
Target_Typ : Entity_Id; |
| 3566 |
Source_Typ : Entity_Id := Empty) |
| 3567 |
is |
| 3568 |
begin |
| 3569 |
Apply_Selected_Length_Checks |
| 3570 |
(Expr, Target_Typ, Source_Typ, Do_Static => True); |
| 3571 |
end Apply_Static_Length_Check; |
| 3572 |
|
| 3573 |
------------------------------------- |
| 3574 |
-- Apply_Subscript_Validity_Checks -- |
| 3575 |
------------------------------------- |
| 3576 |
|
| 3577 |
procedure Apply_Subscript_Validity_Checks |
| 3578 |
(Expr : Node_Id; |
| 3579 |
No_Check_Needed : Dimension_Set := Empty_Dimension_Set) is |
| 3580 |
Sub : Node_Id; |
| 3581 |
|
| 3582 |
Dimension : Pos := 1; |
| 3583 |
begin |
| 3584 |
pragma Assert (Nkind (Expr) = N_Indexed_Component); |
| 3585 |
|
| 3586 |
-- Loop through subscripts |
| 3587 |
|
| 3588 |
Sub := First (Expressions (Expr)); |
| 3589 |
while Present (Sub) loop |
| 3590 |
|
| 3591 |
-- Check one subscript. Note that we do not worry about enumeration |
| 3592 |
-- type with holes, since we will convert the value to a Pos value |
| 3593 |
-- for the subscript, and that convert will do the necessary validity |
| 3594 |
-- check. |
| 3595 |
|
| 3596 |
if No_Check_Needed = Empty_Dimension_Set |
| 3597 |
or else not No_Check_Needed.Elements (Dimension) |
| 3598 |
then |
| 3599 |
Ensure_Valid (Sub, Holes_OK => True); |
| 3600 |
end if; |
| 3601 |
|
| 3602 |
-- Move to next subscript |
| 3603 |
|
| 3604 |
Next (Sub); |
| 3605 |
Dimension := Dimension + 1; |
| 3606 |
end loop; |
| 3607 |
end Apply_Subscript_Validity_Checks; |
| 3608 |
|
| 3609 |
---------------------------------- |
| 3610 |
-- Apply_Type_Conversion_Checks -- |
| 3611 |
---------------------------------- |
| 3612 |
|
| 3613 |
procedure Apply_Type_Conversion_Checks (N : Node_Id) is |
| 3614 |
Target_Type : constant Entity_Id := Etype (N); |
| 3615 |
Target_Base : constant Entity_Id := Base_Type (Target_Type); |
| 3616 |
Expr : constant Node_Id := Expression (N); |
| 3617 |
|
| 3618 |
Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); |
| 3619 |
-- Note: if Etype (Expr) is a private type without discriminants, its |
| 3620 |
-- full view might have discriminants with defaults, so we need the |
| 3621 |
-- full view here to retrieve the constraints. |
| 3622 |
|
| 3623 |
procedure Make_Discriminant_Constraint_Check |
| 3624 |
(Target_Type : Entity_Id; |
| 3625 |
Expr_Type : Entity_Id); |
| 3626 |
-- Generate a discriminant check based on the target type and expression |
| 3627 |
-- type for Expr. |
| 3628 |
|
| 3629 |
---------------------------------------- |
| 3630 |
-- Make_Discriminant_Constraint_Check -- |
| 3631 |
---------------------------------------- |
| 3632 |
|
| 3633 |
procedure Make_Discriminant_Constraint_Check |
| 3634 |
(Target_Type : Entity_Id; |
| 3635 |
Expr_Type : Entity_Id) |
| 3636 |
is |
| 3637 |
Loc : constant Source_Ptr := Sloc (N); |
| 3638 |
Cond : Node_Id; |
| 3639 |
Constraint : Elmt_Id; |
| 3640 |
Discr_Value : Node_Id; |
| 3641 |
Discr : Entity_Id; |
| 3642 |
|
| 3643 |
New_Constraints : constant Elist_Id := New_Elmt_List; |
| 3644 |
Old_Constraints : constant Elist_Id := |
| 3645 |
Discriminant_Constraint (Expr_Type); |
| 3646 |
|
| 3647 |
begin |
| 3648 |
-- Build an actual discriminant constraint list using the stored |
| 3649 |
-- constraint, to verify that the expression of the parent type |
| 3650 |
-- satisfies the constraints imposed by the (unconstrained) derived |
| 3651 |
-- type. This applies to value conversions, not to view conversions |
| 3652 |
-- of tagged types. |
| 3653 |
|
| 3654 |
Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
| 3655 |
while Present (Constraint) loop |
| 3656 |
Discr_Value := Node (Constraint); |
| 3657 |
|
| 3658 |
if Is_Entity_Name (Discr_Value) |
| 3659 |
and then Ekind (Entity (Discr_Value)) = E_Discriminant |
| 3660 |
then |
| 3661 |
Discr := Corresponding_Discriminant (Entity (Discr_Value)); |
| 3662 |
|
| 3663 |
if Present (Discr) |
| 3664 |
and then Scope (Discr) = Base_Type (Expr_Type) |
| 3665 |
then |
| 3666 |
-- Parent is constrained by new discriminant. Obtain |
| 3667 |
-- Value of original discriminant in expression. If the |
| 3668 |
-- new discriminant has been used to constrain more than |
| 3669 |
-- one of the stored discriminants, this will provide the |
| 3670 |
-- required consistency check. |
| 3671 |
|
| 3672 |
Append_Elmt |
| 3673 |
(Make_Selected_Component (Loc, |
| 3674 |
Prefix => |
| 3675 |
Duplicate_Subexpr_No_Checks |
| 3676 |
(Expr, Name_Req => True), |
| 3677 |
Selector_Name => |
| 3678 |
Make_Identifier (Loc, Chars (Discr))), |
| 3679 |
New_Constraints); |
| 3680 |
|
| 3681 |
else |
| 3682 |
-- Discriminant of more remote ancestor ??? |
| 3683 |
|
| 3684 |
return; |
| 3685 |
end if; |
| 3686 |
|
| 3687 |
-- Derived type definition has an explicit value for this |
| 3688 |
-- stored discriminant. |
| 3689 |
|
| 3690 |
else |
| 3691 |
Append_Elmt |
| 3692 |
(Duplicate_Subexpr_No_Checks (Discr_Value), |
| 3693 |
New_Constraints); |
| 3694 |
end if; |
| 3695 |
|
| 3696 |
Next_Elmt (Constraint); |
| 3697 |
end loop; |
| 3698 |
|
| 3699 |
-- Use the unconstrained expression type to retrieve the |
| 3700 |
-- discriminants of the parent, and apply momentarily the |
| 3701 |
-- discriminant constraint synthesized above. |
| 3702 |
|
| 3703 |
-- Note: We use Expr_Type instead of Target_Type since the number of |
| 3704 |
-- actual discriminants may be different due to the presence of |
| 3705 |
-- stored discriminants and cause Build_Discriminant_Checks to fail. |
| 3706 |
|
| 3707 |
Set_Discriminant_Constraint (Expr_Type, New_Constraints); |
| 3708 |
Cond := Build_Discriminant_Checks (Expr, Expr_Type); |
| 3709 |
Set_Discriminant_Constraint (Expr_Type, Old_Constraints); |
| 3710 |
|
| 3711 |
-- Conversion between access types requires that we check for null |
| 3712 |
-- before checking discriminants. |
| 3713 |
|
| 3714 |
if Is_Access_Type (Etype (Expr)) then |
| 3715 |
Cond := Make_And_Then (Loc, |
| 3716 |
Left_Opnd => |
| 3717 |
Make_Op_Ne (Loc, |
| 3718 |
Left_Opnd => |
| 3719 |
Duplicate_Subexpr_No_Checks |
| 3720 |
(Expr, Name_Req => True), |
| 3721 |
Right_Opnd => Make_Null (Loc)), |
| 3722 |
Right_Opnd => Cond); |
| 3723 |
end if; |
| 3724 |
|
| 3725 |
Insert_Action (N, |
| 3726 |
Make_Raise_Constraint_Error (Loc, |
| 3727 |
Condition => Cond, |
| 3728 |
Reason => CE_Discriminant_Check_Failed)); |
| 3729 |
end Make_Discriminant_Constraint_Check; |
| 3730 |
|
| 3731 |
-- Start of processing for Apply_Type_Conversion_Checks |
| 3732 |
|
| 3733 |
begin |
| 3734 |
if Inside_A_Generic then |
| 3735 |
return; |
| 3736 |
|
| 3737 |
-- Skip these checks if serious errors detected, there are some nasty |
| 3738 |
-- situations of incomplete trees that blow things up. |
| 3739 |
|
| 3740 |
elsif Serious_Errors_Detected > 0 then |
| 3741 |
return; |
| 3742 |
|
| 3743 |
-- Never generate discriminant checks for Unchecked_Union types |
| 3744 |
|
| 3745 |
elsif Present (Expr_Type) |
| 3746 |
and then Is_Unchecked_Union (Expr_Type) |
| 3747 |
then |
| 3748 |
return; |
| 3749 |
|
| 3750 |
-- Scalar type conversions of the form Target_Type (Expr) require a |
| 3751 |
-- range check if we cannot be sure that Expr is in the base type of |
| 3752 |
-- Target_Typ and also that Expr is in the range of Target_Typ. These |
| 3753 |
-- are not quite the same condition from an implementation point of |
| 3754 |
-- view, but clearly the second includes the first. |
| 3755 |
|
| 3756 |
elsif Is_Scalar_Type (Target_Type) then |
| 3757 |
declare |
| 3758 |
Conv_OK : constant Boolean := Conversion_OK (N); |
| 3759 |
-- If the Conversion_OK flag on the type conversion is set and no |
| 3760 |
-- floating-point type is involved in the type conversion then |
| 3761 |
-- fixed-point values must be read as integral values. |
| 3762 |
|
| 3763 |
Float_To_Int : constant Boolean := |
| 3764 |
Is_Floating_Point_Type (Expr_Type) |
| 3765 |
and then Is_Integer_Type (Target_Type); |
| 3766 |
|
| 3767 |
begin |
| 3768 |
if not Overflow_Checks_Suppressed (Target_Base) |
| 3769 |
and then not Overflow_Checks_Suppressed (Target_Type) |
| 3770 |
and then not |
| 3771 |
In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
| 3772 |
and then not Float_To_Int |
| 3773 |
then |
| 3774 |
-- A small optimization: the attribute 'Pos applied to an |
| 3775 |
-- enumeration type has a known range, even though its type is |
| 3776 |
-- Universal_Integer. So in numeric conversions it is usually |
| 3777 |
-- within range of the target integer type. Use the static |
| 3778 |
-- bounds of the base types to check. Disable this optimization |
| 3779 |
-- in case of a descendant of a generic formal discrete type, |
| 3780 |
-- because we don't necessarily know the upper bound yet. |
| 3781 |
|
| 3782 |
if Nkind (Expr) = N_Attribute_Reference |
| 3783 |
and then Attribute_Name (Expr) = Name_Pos |
| 3784 |
and then Is_Enumeration_Type (Etype (Prefix (Expr))) |
| 3785 |
and then |
| 3786 |
not Is_Generic_Type (Root_Type (Etype (Prefix (Expr)))) |
| 3787 |
and then Is_Integer_Type (Target_Type) |
| 3788 |
then |
| 3789 |
declare |
| 3790 |
Enum_T : constant Entity_Id := |
| 3791 |
Root_Type (Etype (Prefix (Expr))); |
| 3792 |
Int_T : constant Entity_Id := Base_Type (Target_Type); |
| 3793 |
Last_I : constant Uint := |
| 3794 |
Intval (High_Bound (Scalar_Range (Int_T))); |
| 3795 |
Last_E : Uint; |
| 3796 |
|
| 3797 |
begin |
| 3798 |
-- Character types have no explicit literals, so we use |
| 3799 |
-- the known number of characters in the type. |
| 3800 |
|
| 3801 |
if Root_Type (Enum_T) = Standard_Character then |
| 3802 |
Last_E := UI_From_Int (255); |
| 3803 |
|
| 3804 |
elsif Enum_T = Standard_Wide_Character |
| 3805 |
or else Enum_T = Standard_Wide_Wide_Character |
| 3806 |
then |
| 3807 |
Last_E := UI_From_Int (65535); |
| 3808 |
|
| 3809 |
else |
| 3810 |
Last_E := |
| 3811 |
Enumeration_Pos |
| 3812 |
(Entity (High_Bound (Scalar_Range (Enum_T)))); |
| 3813 |
end if; |
| 3814 |
|
| 3815 |
if Last_E > Last_I then |
| 3816 |
Activate_Overflow_Check (N); |
| 3817 |
end if; |
| 3818 |
end; |
| 3819 |
else |
| 3820 |
Activate_Overflow_Check (N); |
| 3821 |
end if; |
| 3822 |
end if; |
| 3823 |
|
| 3824 |
if not Range_Checks_Suppressed (Target_Type) |
| 3825 |
and then not Range_Checks_Suppressed (Expr_Type) |
| 3826 |
then |
| 3827 |
if Float_To_Int |
| 3828 |
and then not GNATprove_Mode |
| 3829 |
then |
| 3830 |
Apply_Float_Conversion_Check (Expr, Target_Type); |
| 3831 |
else |
| 3832 |
-- Raw conversions involving fixed-point types are expanded |
| 3833 |
-- separately and do not need a Range_Check flag yet, except |
| 3834 |
-- in GNATprove_Mode where this expansion is not performed. |
| 3835 |
-- This does not apply to conversion where fixed-point types |
| 3836 |
-- are treated as integers, which are precisely generated by |
| 3837 |
-- this expansion. |
| 3838 |
|
| 3839 |
if GNATprove_Mode |
| 3840 |
or else Conv_OK |
| 3841 |
or else (not Is_Fixed_Point_Type (Expr_Type) |
| 3842 |
and then not Is_Fixed_Point_Type (Target_Type)) |
| 3843 |
then |
| 3844 |
Apply_Scalar_Range_Check |
| 3845 |
(Expr, Target_Type, Fixed_Int => Conv_OK); |
| 3846 |
|
| 3847 |
else |
| 3848 |
Set_Do_Range_Check (Expr, False); |
| 3849 |
end if; |
| 3850 |
|
| 3851 |
-- If the target type has predicates, we need to indicate |
| 3852 |
-- the need for a check, even if Determine_Range finds that |
| 3853 |
-- the value is within bounds. This may be the case e.g for |
| 3854 |
-- a division with a constant denominator. |
| 3855 |
|
| 3856 |
if Has_Predicates (Target_Type) then |
| 3857 |
Enable_Range_Check (Expr); |
| 3858 |
end if; |
| 3859 |
end if; |
| 3860 |
end if; |
| 3861 |
end; |
| 3862 |
|
| 3863 |
-- Generate discriminant constraint checks for access types on the |
| 3864 |
-- designated target type's stored constraints. |
| 3865 |
|
| 3866 |
-- Do we need to generate subtype predicate checks here as well ??? |
| 3867 |
|
| 3868 |
elsif Comes_From_Source (N) |
| 3869 |
and then Ekind (Target_Type) = E_General_Access_Type |
| 3870 |
|
| 3871 |
-- Check that both of the designated types have known discriminants, |
| 3872 |
-- and that such checks on the target type are not suppressed. |
| 3873 |
|
| 3874 |
and then Has_Discriminants (Directly_Designated_Type (Target_Type)) |
| 3875 |
and then Has_Discriminants (Directly_Designated_Type (Expr_Type)) |
| 3876 |
and then not Discriminant_Checks_Suppressed |
| 3877 |
(Directly_Designated_Type (Target_Type)) |
| 3878 |
|
| 3879 |
-- Verify the designated type of the target has stored constraints |
| 3880 |
|
| 3881 |
and then Present |
| 3882 |
(Stored_Constraint (Directly_Designated_Type (Target_Type))) |
| 3883 |
then |
| 3884 |
Make_Discriminant_Constraint_Check |
| 3885 |
(Target_Type => Directly_Designated_Type (Target_Type), |
| 3886 |
Expr_Type => Directly_Designated_Type (Expr_Type)); |
| 3887 |
|
| 3888 |
-- Create discriminant checks for the Target_Type's stored constraints |
| 3889 |
|
| 3890 |
elsif Comes_From_Source (N) |
| 3891 |
and then not Discriminant_Checks_Suppressed (Target_Type) |
| 3892 |
and then Is_Record_Type (Target_Type) |
| 3893 |
and then Is_Derived_Type (Target_Type) |
| 3894 |
and then not Is_Tagged_Type (Target_Type) |
| 3895 |
and then not Is_Constrained (Target_Type) |
| 3896 |
and then Present (Stored_Constraint (Target_Type)) |
| 3897 |
then |
| 3898 |
Make_Discriminant_Constraint_Check (Target_Type, Expr_Type); |
| 3899 |
|
| 3900 |
-- For arrays, checks are set now, but conversions are applied during |
| 3901 |
-- expansion, to take into accounts changes of representation. The |
| 3902 |
-- checks become range checks on the base type or length checks on the |
| 3903 |
-- subtype, depending on whether the target type is unconstrained or |
| 3904 |
-- constrained. Note that the range check is put on the expression of a |
| 3905 |
-- type conversion, while the length check is put on the type conversion |
| 3906 |
-- itself. |
| 3907 |
|
| 3908 |
elsif Is_Array_Type (Target_Type) then |
| 3909 |
if Is_Constrained (Target_Type) then |
| 3910 |
Set_Do_Length_Check (N); |
| 3911 |
else |
| 3912 |
Set_Do_Range_Check (Expr); |
| 3913 |
end if; |
| 3914 |
end if; |
| 3915 |
end Apply_Type_Conversion_Checks; |
| 3916 |
|
| 3917 |
---------------------------------------------- |
| 3918 |
-- Apply_Universal_Integer_Attribute_Checks -- |
| 3919 |
---------------------------------------------- |
| 3920 |
|
| 3921 |
procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is |
| 3922 |
Loc : constant Source_Ptr := Sloc (N); |
| 3923 |
Typ : constant Entity_Id := Etype (N); |
| 3924 |
|
| 3925 |
begin |
| 3926 |
if Inside_A_Generic then |
| 3927 |
return; |
| 3928 |
|
| 3929 |
-- Nothing to do if the result type is universal integer |
| 3930 |
|
| 3931 |
elsif Typ = Universal_Integer then |
| 3932 |
return; |
| 3933 |
|
| 3934 |
-- Nothing to do if checks are suppressed |
| 3935 |
|
| 3936 |
elsif Range_Checks_Suppressed (Typ) |
| 3937 |
and then Overflow_Checks_Suppressed (Typ) |
| 3938 |
then |
| 3939 |
return; |
| 3940 |
|
| 3941 |
-- Nothing to do if the attribute does not come from source. The |
| 3942 |
-- internal attributes we generate of this type do not need checks, |
| 3943 |
-- and furthermore the attempt to check them causes some circular |
| 3944 |
-- elaboration orders when dealing with packed types. |
| 3945 |
|
| 3946 |
elsif not Comes_From_Source (N) then |
| 3947 |
return; |
| 3948 |
|
| 3949 |
-- If the prefix is a selected component that depends on a discriminant |
| 3950 |
-- the check may improperly expose a discriminant instead of using |
| 3951 |
-- the bounds of the object itself. Set the type of the attribute to |
| 3952 |
-- the base type of the context, so that a check will be imposed when |
| 3953 |
-- needed (e.g. if the node appears as an index). |
| 3954 |
|
| 3955 |
elsif Nkind (Prefix (N)) = N_Selected_Component |
| 3956 |
and then Ekind (Typ) = E_Signed_Integer_Subtype |
| 3957 |
and then Depends_On_Discriminant (Scalar_Range (Typ)) |
| 3958 |
then |
| 3959 |
Set_Etype (N, Base_Type (Typ)); |
| 3960 |
|
| 3961 |
-- Otherwise, replace the attribute node with a type conversion node |
| 3962 |
-- whose expression is the attribute, retyped to universal integer, and |
| 3963 |
-- whose subtype mark is the target type. The call to analyze this |
| 3964 |
-- conversion will set range and overflow checks as required for proper |
| 3965 |
-- detection of an out of range value. |
| 3966 |
|
| 3967 |
else |
| 3968 |
Set_Etype (N, Universal_Integer); |
| 3969 |
Set_Analyzed (N, True); |
| 3970 |
|
| 3971 |
Rewrite (N, |
| 3972 |
Make_Type_Conversion (Loc, |
| 3973 |
Subtype_Mark => New_Occurrence_Of (Typ, Loc), |
| 3974 |
Expression => Relocate_Node (N))); |
| 3975 |
|
| 3976 |
Analyze_And_Resolve (N, Typ); |
| 3977 |
return; |
| 3978 |
end if; |
| 3979 |
end Apply_Universal_Integer_Attribute_Checks; |
| 3980 |
|
| 3981 |
------------------------------------- |
| 3982 |
-- Atomic_Synchronization_Disabled -- |
| 3983 |
------------------------------------- |
| 3984 |
|
| 3985 |
-- Note: internally Disable/Enable_Atomic_Synchronization is implemented |
| 3986 |
-- using a pseudo-check called _Atomic_Synchronization. This is to make it |
| 3987 |
-- more convenient to get the same placement and scope rules as |
| 3988 |
-- [Un]Suppress. The check name has a leading underscore to make |
| 3989 |
-- it reserved by the implementation. |
| 3990 |
|
| 3991 |
function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is |
| 3992 |
begin |
| 3993 |
-- If debug flag d.e is set, always return False, i.e. all atomic sync |
| 3994 |
-- looks enabled, since it is never disabled. |
| 3995 |
|
| 3996 |
if Debug_Flag_Dot_E then |
| 3997 |
return False; |
| 3998 |
|
| 3999 |
-- If debug flag d.d is set then always return True, i.e. all atomic |
| 4000 |
-- sync looks disabled, since it always tests True. |
| 4001 |
|
| 4002 |
elsif Debug_Flag_Dot_D then |
| 4003 |
return True; |
| 4004 |
|
| 4005 |
-- If entity present, then check result for that entity |
| 4006 |
|
| 4007 |
elsif Present (E) and then Checks_May_Be_Suppressed (E) then |
| 4008 |
return Is_Check_Suppressed (E, Atomic_Synchronization); |
| 4009 |
|
| 4010 |
-- Otherwise result depends on current scope setting |
| 4011 |
|
| 4012 |
else |
| 4013 |
return Scope_Suppress.Suppress (Atomic_Synchronization); |
| 4014 |
end if; |
| 4015 |
end Atomic_Synchronization_Disabled; |
| 4016 |
|
| 4017 |
------------------------------- |
| 4018 |
-- Build_Discriminant_Checks -- |
| 4019 |
------------------------------- |
| 4020 |
|
| 4021 |
function Build_Discriminant_Checks |
| 4022 |
(N : Node_Id; |
| 4023 |
T_Typ : Entity_Id) return Node_Id |
| 4024 |
is |
| 4025 |
Loc : constant Source_Ptr := Sloc (N); |
| 4026 |
Cond : Node_Id; |
| 4027 |
Disc : Elmt_Id; |
| 4028 |
Disc_Ent : Entity_Id; |
| 4029 |
Dref : Node_Id; |
| 4030 |
Dval : Node_Id; |
| 4031 |
|
| 4032 |
function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
| 4033 |
|
| 4034 |
function Replace_Current_Instance |
| 4035 |
(N : Node_Id) return Traverse_Result; |
| 4036 |
-- Replace a reference to the current instance of the type with the |
| 4037 |
-- corresponding _init formal of the initialization procedure. Note: |
| 4038 |
-- this function relies on us currently being within the initialization |
| 4039 |
-- procedure. |
| 4040 |
|
| 4041 |
-------------------------------- |
| 4042 |
-- Aggregate_Discriminant_Val -- |
| 4043 |
-------------------------------- |
| 4044 |
|
| 4045 |
function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is |
| 4046 |
Assoc : Node_Id; |
| 4047 |
|
| 4048 |
begin |
| 4049 |
-- The aggregate has been normalized with named associations. We use |
| 4050 |
-- the Chars field to locate the discriminant to take into account |
| 4051 |
-- discriminants in derived types, which carry the same name as those |
| 4052 |
-- in the parent. |
| 4053 |
|
| 4054 |
Assoc := First (Component_Associations (N)); |
| 4055 |
while Present (Assoc) loop |
| 4056 |
if Chars (First (Choices (Assoc))) = Chars (Disc) then |
| 4057 |
return Expression (Assoc); |
| 4058 |
else |
| 4059 |
Next (Assoc); |
| 4060 |
end if; |
| 4061 |
end loop; |
| 4062 |
|
| 4063 |
-- Discriminant must have been found in the loop above |
| 4064 |
|
| 4065 |
raise Program_Error; |
| 4066 |
end Aggregate_Discriminant_Val; |
| 4067 |
|
| 4068 |
------------------------------ |
| 4069 |
-- Replace_Current_Instance -- |
| 4070 |
------------------------------ |
| 4071 |
|
| 4072 |
function Replace_Current_Instance |
| 4073 |
(N : Node_Id) return Traverse_Result is |
| 4074 |
begin |
| 4075 |
if Is_Entity_Name (N) |
| 4076 |
and then Etype (N) = Entity (N) |
| 4077 |
then |
| 4078 |
Rewrite (N, |
| 4079 |
New_Occurrence_Of (First_Formal (Current_Subprogram), Loc)); |
| 4080 |
end if; |
| 4081 |
|
| 4082 |
return OK; |
| 4083 |
end Replace_Current_Instance; |
| 4084 |
|
| 4085 |
procedure Search_And_Replace_Current_Instance is new |
| 4086 |
Traverse_Proc (Replace_Current_Instance); |
| 4087 |
|
| 4088 |
-- Start of processing for Build_Discriminant_Checks |
| 4089 |
|
| 4090 |
begin |
| 4091 |
-- Loop through discriminants evolving the condition |
| 4092 |
|
| 4093 |
Cond := Empty; |
| 4094 |
Disc := First_Elmt (Discriminant_Constraint (T_Typ)); |
| 4095 |
|
| 4096 |
-- For a fully private type, use the discriminants of the parent type |
| 4097 |
|
| 4098 |
if Is_Private_Type (T_Typ) |
| 4099 |
and then No (Full_View (T_Typ)) |
| 4100 |
then |
| 4101 |
Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); |
| 4102 |
else |
| 4103 |
Disc_Ent := First_Discriminant (T_Typ); |
| 4104 |
end if; |
| 4105 |
|
| 4106 |
while Present (Disc) loop |
| 4107 |
Dval := Node (Disc); |
| 4108 |
|
| 4109 |
if Nkind (Dval) = N_Identifier |
| 4110 |
and then Ekind (Entity (Dval)) = E_Discriminant |
| 4111 |
then |
| 4112 |
Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); |
| 4113 |
else |
| 4114 |
Dval := Duplicate_Subexpr_No_Checks (Dval); |
| 4115 |
end if; |
| 4116 |
|
| 4117 |
-- Replace references to the current instance of the type with the |
| 4118 |
-- corresponding _init formal of the initialization procedure. |
| 4119 |
|
| 4120 |
if Within_Init_Proc then |
| 4121 |
Search_And_Replace_Current_Instance (Dval); |
| 4122 |
end if; |
| 4123 |
|
| 4124 |
-- If we have an Unchecked_Union node, we can infer the discriminants |
| 4125 |
-- of the node. |
| 4126 |
|
| 4127 |
if Is_Unchecked_Union (Base_Type (T_Typ)) then |
| 4128 |
Dref := New_Copy ( |
| 4129 |
Get_Discriminant_Value ( |
| 4130 |
First_Discriminant (T_Typ), |
| 4131 |
T_Typ, |
| 4132 |
Stored_Constraint (T_Typ))); |
| 4133 |
|
| 4134 |
elsif Nkind (N) = N_Aggregate then |
| 4135 |
Dref := |
| 4136 |
Duplicate_Subexpr_No_Checks |
| 4137 |
(Aggregate_Discriminant_Val (Disc_Ent)); |
| 4138 |
|
| 4139 |
elsif Is_Access_Type (Etype (N)) then |
| 4140 |
Dref := |
| 4141 |
Make_Selected_Component (Loc, |
| 4142 |
Prefix => |
| 4143 |
Make_Explicit_Dereference (Loc, |
| 4144 |
Duplicate_Subexpr_No_Checks (N, Name_Req => True)), |
| 4145 |
Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
| 4146 |
|
| 4147 |
Set_Is_In_Discriminant_Check (Dref); |
| 4148 |
else |
| 4149 |
Dref := |
| 4150 |
Make_Selected_Component (Loc, |
| 4151 |
Prefix => |
| 4152 |
Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
| 4153 |
Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
| 4154 |
|
| 4155 |
Set_Is_In_Discriminant_Check (Dref); |
| 4156 |
end if; |
| 4157 |
|
| 4158 |
Evolve_Or_Else (Cond, |
| 4159 |
Make_Op_Ne (Loc, |
| 4160 |
Left_Opnd => Dref, |
| 4161 |
Right_Opnd => Dval)); |
| 4162 |
|
| 4163 |
Next_Elmt (Disc); |
| 4164 |
Next_Discriminant (Disc_Ent); |
| 4165 |
end loop; |
| 4166 |
|
| 4167 |
return Cond; |
| 4168 |
end Build_Discriminant_Checks; |
| 4169 |
|
| 4170 |
------------------ |
| 4171 |
-- Check_Needed -- |
| 4172 |
------------------ |
| 4173 |
|
| 4174 |
function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is |
| 4175 |
N : Node_Id; |
| 4176 |
P : Node_Id; |
| 4177 |
K : Node_Kind; |
| 4178 |
L : Node_Id; |
| 4179 |
R : Node_Id; |
| 4180 |
|
| 4181 |
function Left_Expression (Op : Node_Id) return Node_Id; |
| 4182 |
-- Return the relevant expression from the left operand of the given |
| 4183 |
-- short circuit form: this is LO itself, except if LO is a qualified |
| 4184 |
-- expression, a type conversion, or an expression with actions, in |
| 4185 |
-- which case this is Left_Expression (Expression (LO)). |
| 4186 |
|
| 4187 |
--------------------- |
| 4188 |
-- Left_Expression -- |
| 4189 |
--------------------- |
| 4190 |
|
| 4191 |
function Left_Expression (Op : Node_Id) return Node_Id is |
| 4192 |
LE : Node_Id := Left_Opnd (Op); |
| 4193 |
begin |
| 4194 |
while Nkind (LE) in N_Qualified_Expression |
| 4195 |
| N_Type_Conversion |
| 4196 |
| N_Expression_With_Actions |
| 4197 |
loop |
| 4198 |
LE := Expression (LE); |
| 4199 |
end loop; |
| 4200 |
|
| 4201 |
return LE; |
| 4202 |
end Left_Expression; |
| 4203 |
|
| 4204 |
-- Start of processing for Check_Needed |
| 4205 |
|
| 4206 |
begin |
| 4207 |
-- Always check if not simple entity |
| 4208 |
|
| 4209 |
if Nkind (Nod) not in N_Has_Entity |
| 4210 |
or else not Comes_From_Source (Nod) |
| 4211 |
then |
| 4212 |
return True; |
| 4213 |
end if; |
| 4214 |
|
| 4215 |
-- Look up tree for short circuit |
| 4216 |
|
| 4217 |
N := Nod; |
| 4218 |
loop |
| 4219 |
P := Parent (N); |
| 4220 |
K := Nkind (P); |
| 4221 |
|
| 4222 |
-- Done if out of subexpression (note that we allow generated stuff |
| 4223 |
-- such as itype declarations in this context, to keep the loop going |
| 4224 |
-- since we may well have generated such stuff in complex situations. |
| 4225 |
-- Also done if no parent (probably an error condition, but no point |
| 4226 |
-- in behaving nasty if we find it). |
| 4227 |
|
| 4228 |
if No (P) |
| 4229 |
or else (K not in N_Subexpr and then Comes_From_Source (P)) |
| 4230 |
then |
| 4231 |
return True; |
| 4232 |
|
| 4233 |
-- Or/Or Else case, where test is part of the right operand, or is |
| 4234 |
-- part of one of the actions associated with the right operand, and |
| 4235 |
-- the left operand is an equality test. |
| 4236 |
|
| 4237 |
elsif K = N_Op_Or then |
| 4238 |
exit when N = Right_Opnd (P) |
| 4239 |
and then Nkind (Left_Expression (P)) = N_Op_Eq; |
| 4240 |
|
| 4241 |
elsif K = N_Or_Else then |
| 4242 |
exit when (N = Right_Opnd (P) |
| 4243 |
or else |
| 4244 |
(Is_List_Member (N) |
| 4245 |
and then List_Containing (N) = Actions (P))) |
| 4246 |
and then Nkind (Left_Expression (P)) = N_Op_Eq; |
| 4247 |
|
| 4248 |
-- Similar test for the And/And then case, where the left operand |
| 4249 |
-- is an inequality test. |
| 4250 |
|
| 4251 |
elsif K = N_Op_And then |
| 4252 |
exit when N = Right_Opnd (P) |
| 4253 |
and then Nkind (Left_Expression (P)) = N_Op_Ne; |
| 4254 |
|
| 4255 |
elsif K = N_And_Then then |
| 4256 |
exit when (N = Right_Opnd (P) |
| 4257 |
or else |
| 4258 |
(Is_List_Member (N) |
| 4259 |
and then List_Containing (N) = Actions (P))) |
| 4260 |
and then Nkind (Left_Expression (P)) = N_Op_Ne; |
| 4261 |
end if; |
| 4262 |
|
| 4263 |
N := P; |
| 4264 |
end loop; |
| 4265 |
|
| 4266 |
-- If we fall through the loop, then we have a conditional with an |
| 4267 |
-- appropriate test as its left operand, so look further. |
| 4268 |
|
| 4269 |
L := Left_Expression (P); |
| 4270 |
|
| 4271 |
-- L is an "=" or "/=" operator: extract its operands |
| 4272 |
|
| 4273 |
R := Right_Opnd (L); |
| 4274 |
L := Left_Opnd (L); |
| 4275 |
|
| 4276 |
-- Left operand of test must match original variable |
| 4277 |
|
| 4278 |
if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
| 4279 |
return True; |
| 4280 |
end if; |
| 4281 |
|
| 4282 |
-- Right operand of test must be key value (zero or null) |
| 4283 |
|
| 4284 |
case Check is |
| 4285 |
when Access_Check => |
| 4286 |
if not Known_Null (R) then |
| 4287 |
return True; |
| 4288 |
end if; |
| 4289 |
|
| 4290 |
when Division_Check => |
| 4291 |
if not Compile_Time_Known_Value (R) |
| 4292 |
or else Expr_Value (R) /= Uint_0 |
| 4293 |
then |
| 4294 |
return True; |
| 4295 |
end if; |
| 4296 |
|
| 4297 |
when others => |
| 4298 |
raise Program_Error; |
| 4299 |
end case; |
| 4300 |
|
| 4301 |
-- Here we have the optimizable case, warn if not short-circuited |
| 4302 |
|
| 4303 |
if K = N_Op_And or else K = N_Op_Or then |
| 4304 |
Error_Msg_Warn := SPARK_Mode /= On; |
| 4305 |
|
| 4306 |
case Check is |
| 4307 |
when Access_Check => |
| 4308 |
if GNATprove_Mode then |
| 4309 |
Error_Msg_N |
| 4310 |
("Constraint_Error might have been raised (access check)", |
| 4311 |
Parent (Nod)); |
| 4312 |
else |
| 4313 |
Error_Msg_N |
| 4314 |
("Constraint_Error may be raised (access check)??", |
| 4315 |
Parent (Nod)); |
| 4316 |
end if; |
| 4317 |
|
| 4318 |
when Division_Check => |
| 4319 |
if GNATprove_Mode then |
| 4320 |
Error_Msg_N |
| 4321 |
("Constraint_Error might have been raised (zero divide)", |
| 4322 |
Parent (Nod)); |
| 4323 |
else |
| 4324 |
Error_Msg_N |
| 4325 |
("Constraint_Error may be raised (zero divide)??", |
| 4326 |
Parent (Nod)); |
| 4327 |
end if; |
| 4328 |
|
| 4329 |
when others => |
| 4330 |
raise Program_Error; |
| 4331 |
end case; |
| 4332 |
|
| 4333 |
if K = N_Op_And then |
| 4334 |
Error_Msg_N -- CODEFIX |
| 4335 |
("use `AND THEN` instead of AND??", P); |
| 4336 |
else |
| 4337 |
Error_Msg_N -- CODEFIX |
| 4338 |
("use `OR ELSE` instead of OR??", P); |
| 4339 |
end if; |
| 4340 |
|
| 4341 |
-- If not short-circuited, we need the check |
| 4342 |
|
| 4343 |
return True; |
| 4344 |
|
| 4345 |
-- If short-circuited, we can omit the check |
| 4346 |
|
| 4347 |
else |
| 4348 |
return False; |
| 4349 |
end if; |
| 4350 |
end Check_Needed; |
| 4351 |
|
| 4352 |
----------------------------------- |
| 4353 |
-- Check_Valid_Lvalue_Subscripts -- |
| 4354 |
----------------------------------- |
| 4355 |
|
| 4356 |
procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is |
| 4357 |
begin |
| 4358 |
-- Skip this if range checks are suppressed |
| 4359 |
|
| 4360 |
if Range_Checks_Suppressed (Etype (Expr)) then |
| 4361 |
return; |
| 4362 |
|
| 4363 |
-- Only do this check for expressions that come from source. We assume |
| 4364 |
-- that expander generated assignments explicitly include any necessary |
| 4365 |
-- checks. Note that this is not just an optimization, it avoids |
| 4366 |
-- infinite recursions. |
| 4367 |
|
| 4368 |
elsif not Comes_From_Source (Expr) then |
| 4369 |
return; |
| 4370 |
|
| 4371 |
-- For a selected component, check the prefix |
| 4372 |
|
| 4373 |
elsif Nkind (Expr) = N_Selected_Component then |
| 4374 |
Check_Valid_Lvalue_Subscripts (Prefix (Expr)); |
| 4375 |
return; |
| 4376 |
|
| 4377 |
-- Case of indexed component |
| 4378 |
|
| 4379 |
elsif Nkind (Expr) = N_Indexed_Component then |
| 4380 |
Apply_Subscript_Validity_Checks (Expr); |
| 4381 |
|
| 4382 |
-- Prefix may itself be or contain an indexed component, and these |
| 4383 |
-- subscripts need checking as well. |
| 4384 |
|
| 4385 |
Check_Valid_Lvalue_Subscripts (Prefix (Expr)); |
| 4386 |
end if; |
| 4387 |
end Check_Valid_Lvalue_Subscripts; |
| 4388 |
|
| 4389 |
---------------------------------- |
| 4390 |
-- Null_Exclusion_Static_Checks -- |
| 4391 |
---------------------------------- |
| 4392 |
|
| 4393 |
procedure Null_Exclusion_Static_Checks |
| 4394 |
(N : Node_Id; |
| 4395 |
Comp : Node_Id := Empty; |
| 4396 |
Array_Comp : Boolean := False) |
| 4397 |
is |
| 4398 |
Has_Null : constant Boolean := Has_Null_Exclusion (N); |
| 4399 |
Kind : constant Node_Kind := Nkind (N); |
| 4400 |
Error_Nod : Node_Id; |
| 4401 |
Expr : Node_Id; |
| 4402 |
Typ : Entity_Id; |
| 4403 |
|
| 4404 |
begin |
| 4405 |
pragma Assert |
| 4406 |
(Kind in N_Component_Declaration |
| 4407 |
| N_Discriminant_Specification |
| 4408 |
| N_Function_Specification |
| 4409 |
| N_Object_Declaration |
| 4410 |
| N_Parameter_Specification); |
| 4411 |
|
| 4412 |
if Kind = N_Function_Specification then |
| 4413 |
Typ := Etype (Defining_Entity (N)); |
| 4414 |
else |
| 4415 |
Typ := Etype (Defining_Identifier (N)); |
| 4416 |
end if; |
| 4417 |
|
| 4418 |
case Kind is |
| 4419 |
when N_Component_Declaration => |
| 4420 |
if Present (Access_Definition (Component_Definition (N))) then |
| 4421 |
Error_Nod := Component_Definition (N); |
| 4422 |
else |
| 4423 |
Error_Nod := Subtype_Indication (Component_Definition (N)); |
| 4424 |
end if; |
| 4425 |
|
| 4426 |
when N_Discriminant_Specification => |
| 4427 |
Error_Nod := Discriminant_Type (N); |
| 4428 |
|
| 4429 |
when N_Function_Specification => |
| 4430 |
Error_Nod := Result_Definition (N); |
| 4431 |
|
| 4432 |
when N_Object_Declaration => |
| 4433 |
Error_Nod := Object_Definition (N); |
| 4434 |
|
| 4435 |
when N_Parameter_Specification => |
| 4436 |
Error_Nod := Parameter_Type (N); |
| 4437 |
|
| 4438 |
when others => |
| 4439 |
raise Program_Error; |
| 4440 |
end case; |
| 4441 |
|
| 4442 |
if Has_Null then |
| 4443 |
|
| 4444 |
-- Enforce legality rule 3.10 (13): A null exclusion can only be |
| 4445 |
-- applied to an access [sub]type. |
| 4446 |
|
| 4447 |
if not Is_Access_Type (Typ) then |
| 4448 |
Error_Msg_N |
| 4449 |
("`NOT NULL` allowed only for an access type", Error_Nod); |
| 4450 |
|
| 4451 |
-- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
| 4452 |
-- be applied to a [sub]type that does not exclude null already. |
| 4453 |
|
| 4454 |
elsif Can_Never_Be_Null (Typ) and then Comes_From_Source (Typ) then |
| 4455 |
Error_Msg_NE |
| 4456 |
("`NOT NULL` not allowed (& already excludes null)", |
| 4457 |
Error_Nod, Typ); |
| 4458 |
end if; |
| 4459 |
end if; |
| 4460 |
|
| 4461 |
-- Check that null-excluding objects are always initialized, except for |
| 4462 |
-- deferred constants, for which the expression will appear in the full |
| 4463 |
-- declaration. |
| 4464 |
|
| 4465 |
if Kind = N_Object_Declaration |
| 4466 |
and then No (Expression (N)) |
| 4467 |
and then not Constant_Present (N) |
| 4468 |
and then not No_Initialization (N) |
| 4469 |
then |
| 4470 |
if Present (Comp) then |
| 4471 |
|
| 4472 |
-- Specialize the warning message to indicate that we are dealing |
| 4473 |
-- with an uninitialized composite object that has a defaulted |
| 4474 |
-- null-excluding component. |
| 4475 |
|
| 4476 |
Error_Msg_Name_1 := Chars (Defining_Identifier (Comp)); |
| 4477 |
Error_Msg_Name_2 := Chars (Defining_Identifier (N)); |
| 4478 |
|
| 4479 |
Discard_Node |
| 4480 |
(Compile_Time_Constraint_Error |
| 4481 |
(N => N, |
| 4482 |
Msg => |
| 4483 |
"(Ada 2005) null-excluding component % of object % must " |
| 4484 |
& "be initialized??", |
| 4485 |
Ent => Defining_Identifier (Comp))); |
| 4486 |
|
| 4487 |
-- This is a case of an array with null-excluding components, so |
| 4488 |
-- indicate that in the warning. |
| 4489 |
|
| 4490 |
elsif Array_Comp then |
| 4491 |
Discard_Node |
| 4492 |
(Compile_Time_Constraint_Error |
| 4493 |
(N => N, |
| 4494 |
Msg => |
| 4495 |
"(Ada 2005) null-excluding array components must " |
| 4496 |
& "be initialized??", |
| 4497 |
Ent => Defining_Identifier (N))); |
| 4498 |
|
| 4499 |
-- Normal case of object of a null-excluding access type |
| 4500 |
|
| 4501 |
else |
| 4502 |
-- Add an expression that assigns null. This node is needed by |
| 4503 |
-- Apply_Compile_Time_Constraint_Error, which will replace this |
| 4504 |
-- with a Constraint_Error node. |
| 4505 |
|
| 4506 |
Set_Expression (N, Make_Null (Sloc (N))); |
| 4507 |
Set_Etype (Expression (N), Etype (Defining_Identifier (N))); |
| 4508 |
|
| 4509 |
Apply_Compile_Time_Constraint_Error |
| 4510 |
(N => Expression (N), |
| 4511 |
Msg => |
| 4512 |
"(Ada 2005) null-excluding objects must be initialized??", |
| 4513 |
Reason => CE_Null_Not_Allowed); |
| 4514 |
end if; |
| 4515 |
end if; |
| 4516 |
|
| 4517 |
-- Check that a null-excluding component, formal or object is not being |
| 4518 |
-- assigned a null value. Otherwise generate a warning message and |
| 4519 |
-- replace Expression (N) by an N_Constraint_Error node. |
| 4520 |
|
| 4521 |
if Kind /= N_Function_Specification then |
| 4522 |
Expr := Expression (N); |
| 4523 |
|
| 4524 |
if Present (Expr) and then Known_Null (Expr) then |
| 4525 |
case Kind is |
| 4526 |
when N_Component_Declaration |
| 4527 |
| N_Discriminant_Specification |
| 4528 |
=> |
| 4529 |
Apply_Compile_Time_Constraint_Error |
| 4530 |
(N => Expr, |
| 4531 |
Msg => |
| 4532 |
"(Ada 2005) NULL not allowed in null-excluding " |
| 4533 |
& "components??", |
| 4534 |
Reason => CE_Null_Not_Allowed); |
| 4535 |
|
| 4536 |
when N_Object_Declaration => |
| 4537 |
Apply_Compile_Time_Constraint_Error |
| 4538 |
(N => Expr, |
| 4539 |
Msg => |
| 4540 |
"(Ada 2005) NULL not allowed in null-excluding " |
| 4541 |
& "objects??", |
| 4542 |
Reason => CE_Null_Not_Allowed); |
| 4543 |
|
| 4544 |
when N_Parameter_Specification => |
| 4545 |
Apply_Compile_Time_Constraint_Error |
| 4546 |
(N => Expr, |
| 4547 |
Msg => |
| 4548 |
"(Ada 2005) NULL not allowed in null-excluding " |
| 4549 |
& "formals??", |
| 4550 |
Reason => CE_Null_Not_Allowed); |
| 4551 |
|
| 4552 |
when others => |
| 4553 |
null; |
| 4554 |
end case; |
| 4555 |
end if; |
| 4556 |
end if; |
| 4557 |
end Null_Exclusion_Static_Checks; |
| 4558 |
|
| 4559 |
------------------------------------- |
| 4560 |
-- Compute_Range_For_Arithmetic_Op -- |
| 4561 |
------------------------------------- |
| 4562 |
|
| 4563 |
procedure Compute_Range_For_Arithmetic_Op |
| 4564 |
(Op : Node_Kind; |
| 4565 |
Lo_Left : Uint; |
| 4566 |
Hi_Left : Uint; |
| 4567 |
Lo_Right : Uint; |
| 4568 |
Hi_Right : Uint; |
| 4569 |
OK : out Boolean; |
| 4570 |
Lo : out Uint; |
| 4571 |
Hi : out Uint) |
| 4572 |
is |
| 4573 |
-- Use local variables for possible adjustments |
| 4574 |
|
| 4575 |
Llo : Uint renames Lo_Left; |
| 4576 |
Lhi : Uint renames Hi_Left; |
| 4577 |
Rlo : Uint := Lo_Right; |
| 4578 |
Rhi : Uint := Hi_Right; |
| 4579 |
|
| 4580 |
begin |
| 4581 |
-- We will compute a range for the result in almost all cases |
| 4582 |
|
| 4583 |
OK := True; |
| 4584 |
|
| 4585 |
case Op is |
| 4586 |
|
| 4587 |
-- Absolute value |
| 4588 |
|
| 4589 |
when N_Op_Abs => |
| 4590 |
Lo := Uint_0; |
| 4591 |
Hi := UI_Max (abs Rlo, abs Rhi); |
| 4592 |
|
| 4593 |
-- Addition |
| 4594 |
|
| 4595 |
when N_Op_Add => |
| 4596 |
Lo := Llo + Rlo; |
| 4597 |
Hi := Lhi + Rhi; |
| 4598 |
|
| 4599 |
-- Division |
| 4600 |
|
| 4601 |
when N_Op_Divide => |
| 4602 |
|
| 4603 |
-- If the right operand can only be zero, set 0..0 |
| 4604 |
|
| 4605 |
if Rlo = 0 and then Rhi = 0 then |
| 4606 |
Lo := Uint_0; |
| 4607 |
Hi := Uint_0; |
| 4608 |
|
| 4609 |
-- Possible bounds of division must come from dividing end |
| 4610 |
-- values of the input ranges (four possibilities), provided |
| 4611 |
-- zero is not included in the possible values of the right |
| 4612 |
-- operand. |
| 4613 |
|
| 4614 |
-- Otherwise, we just consider two intervals of values for |
| 4615 |
-- the right operand: the interval of negative values (up to |
| 4616 |
-- -1) and the interval of positive values (starting at 1). |
| 4617 |
-- Since division by 1 is the identity, and division by -1 |
| 4618 |
-- is negation, we get all possible bounds of division in that |
| 4619 |
-- case by considering: |
| 4620 |
-- - all values from the division of end values of input |
| 4621 |
-- ranges; |
| 4622 |
-- - the end values of the left operand; |
| 4623 |
-- - the negation of the end values of the left operand. |
| 4624 |
|
| 4625 |
else |
| 4626 |
declare |
| 4627 |
Mrk : constant Uintp.Save_Mark := Mark; |
| 4628 |
-- Mark so we can release the RR and Ev values |
| 4629 |
|
| 4630 |
Ev1 : Uint; |
| 4631 |
Ev2 : Uint; |
| 4632 |
Ev3 : Uint; |
| 4633 |
Ev4 : Uint; |
| 4634 |
|
| 4635 |
begin |
| 4636 |
-- Discard extreme values of zero for the divisor, since |
| 4637 |
-- they will simply result in an exception in any case. |
| 4638 |
|
| 4639 |
if Rlo = 0 then |
| 4640 |
Rlo := Uint_1; |
| 4641 |
elsif Rhi = 0 then |
| 4642 |
Rhi := -Uint_1; |
| 4643 |
end if; |
| 4644 |
|
| 4645 |
-- Compute possible bounds coming from dividing end |
| 4646 |
-- values of the input ranges. |
| 4647 |
|
| 4648 |
Ev1 := Llo / Rlo; |
| 4649 |
Ev2 := Llo / Rhi; |
| 4650 |
Ev3 := Lhi / Rlo; |
| 4651 |
Ev4 := Lhi / Rhi; |
| 4652 |
|
| 4653 |
Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
| 4654 |
Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); |
| 4655 |
|
| 4656 |
-- If the right operand can be both negative or positive, |
| 4657 |
-- include the end values of the left operand in the |
| 4658 |
-- extreme values, as well as their negation. |
| 4659 |
|
| 4660 |
if Rlo < 0 and then Rhi > 0 then |
| 4661 |
Ev1 := Llo; |
| 4662 |
Ev2 := -Llo; |
| 4663 |
Ev3 := Lhi; |
| 4664 |
Ev4 := -Lhi; |
| 4665 |
|
| 4666 |
Lo := UI_Min (Lo, |
| 4667 |
UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); |
| 4668 |
Hi := UI_Max (Hi, |
| 4669 |
UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); |
| 4670 |
end if; |
| 4671 |
|
| 4672 |
-- Release the RR and Ev values |
| 4673 |
|
| 4674 |
Release_And_Save (Mrk, Lo, Hi); |
| 4675 |
end; |
| 4676 |
end if; |
| 4677 |
|
| 4678 |
-- Exponentiation |
| 4679 |
|
| 4680 |
when N_Op_Expon => |
| 4681 |
|
| 4682 |
-- Discard negative values for the exponent, since they will |
| 4683 |
-- simply result in an exception in any case. |
| 4684 |
|
| 4685 |
if Rhi < 0 then |
| 4686 |
Rhi := Uint_0; |
| 4687 |
elsif Rlo < 0 then |
| 4688 |
Rlo := Uint_0; |
| 4689 |
end if; |
| 4690 |
|
| 4691 |
-- Estimate number of bits in result before we go computing |
| 4692 |
-- giant useless bounds. Basically the number of bits in the |
| 4693 |
-- result is the number of bits in the base multiplied by the |
| 4694 |
-- value of the exponent. If this is big enough that the result |
| 4695 |
-- definitely won't fit in Long_Long_Integer, return immediately |
| 4696 |
-- and avoid computing giant bounds. |
| 4697 |
|
| 4698 |
-- The comparison here is approximate, but conservative, it |
| 4699 |
-- only clicks on cases that are sure to exceed the bounds. |
| 4700 |
|
| 4701 |
if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then |
| 4702 |
Lo := No_Uint; |
| 4703 |
Hi := No_Uint; |
| 4704 |
OK := False; |
| 4705 |
return; |
| 4706 |
|
| 4707 |
-- If right operand is zero then result is 1 |
| 4708 |
|
| 4709 |
elsif Rhi = 0 then |
| 4710 |
Lo := Uint_1; |
| 4711 |
Hi := Uint_1; |
| 4712 |
|
| 4713 |
else |
| 4714 |
-- High bound comes either from exponentiation of largest |
| 4715 |
-- positive value to largest exponent value, or from |
| 4716 |
-- the exponentiation of most negative value to an |
| 4717 |
-- even exponent. |
| 4718 |
|
| 4719 |
declare |
| 4720 |
Hi1, Hi2 : Uint; |
| 4721 |
|
| 4722 |
begin |
| 4723 |
if Lhi > 0 then |
| 4724 |
Hi1 := Lhi ** Rhi; |
| 4725 |
else |
| 4726 |
Hi1 := Uint_0; |
| 4727 |
end if; |
| 4728 |
|
| 4729 |
if Llo < 0 then |
| 4730 |
if Rhi mod 2 = 0 then |
| 4731 |
Hi2 := Llo ** Rhi; |
| 4732 |
else |
| 4733 |
Hi2 := Llo ** (Rhi - 1); |
| 4734 |
end if; |
| 4735 |
else |
| 4736 |
Hi2 := Uint_0; |
| 4737 |
end if; |
| 4738 |
|
| 4739 |
Hi := UI_Max (Hi1, Hi2); |
| 4740 |
end; |
| 4741 |
|
| 4742 |
-- Result can only be negative if base can be negative |
| 4743 |
|
| 4744 |
if Llo < 0 then |
| 4745 |
if Rhi mod 2 = 0 then |
| 4746 |
Lo := Llo ** (Rhi - 1); |
| 4747 |
else |
| 4748 |
Lo := Llo ** Rhi; |
| 4749 |
end if; |
| 4750 |
|
| 4751 |
-- Otherwise low bound is minimum ** minimum |
| 4752 |
|
| 4753 |
else |
| 4754 |
Lo := Llo ** Rlo; |
| 4755 |
end if; |
| 4756 |
end if; |
| 4757 |
|
| 4758 |
-- Negation |
| 4759 |
|
| 4760 |
when N_Op_Minus => |
| 4761 |
Lo := -Rhi; |
| 4762 |
Hi := -Rlo; |
| 4763 |
|
| 4764 |
-- Mod |
| 4765 |
|
| 4766 |
when N_Op_Mod => |
| 4767 |
declare |
| 4768 |
Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
| 4769 |
-- This is the maximum absolute value of the result |
| 4770 |
|
| 4771 |
begin |
| 4772 |
Lo := Uint_0; |
| 4773 |
Hi := Uint_0; |
| 4774 |
|
| 4775 |
-- The result depends only on the sign and magnitude of |
| 4776 |
-- the right operand, it does not depend on the sign or |
| 4777 |
-- magnitude of the left operand. |
| 4778 |
|
| 4779 |
if Rlo < 0 then |
| 4780 |
Lo := -Maxabs; |
| 4781 |
end if; |
| 4782 |
|
| 4783 |
if Rhi > 0 then |
| 4784 |
Hi := Maxabs; |
| 4785 |
end if; |
| 4786 |
end; |
| 4787 |
|
| 4788 |
-- Multiplication |
| 4789 |
|
| 4790 |
when N_Op_Multiply => |
| 4791 |
|
| 4792 |
-- Possible bounds of multiplication must come from multiplying |
| 4793 |
-- end values of the input ranges (four possibilities). |
| 4794 |
|
| 4795 |
declare |
| 4796 |
Mrk : constant Uintp.Save_Mark := Mark; |
| 4797 |
-- Mark so we can release the Ev values |
| 4798 |
|
| 4799 |
Ev1 : constant Uint := Llo * Rlo; |
| 4800 |
Ev2 : constant Uint := Llo * Rhi; |
| 4801 |
Ev3 : constant Uint := Lhi * Rlo; |
| 4802 |
Ev4 : constant Uint := Lhi * Rhi; |
| 4803 |
|
| 4804 |
begin |
| 4805 |
Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
| 4806 |
Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); |
| 4807 |
|
| 4808 |
-- Release the Ev values |
| 4809 |
|
| 4810 |
Release_And_Save (Mrk, Lo, Hi); |
| 4811 |
end; |
| 4812 |
|
| 4813 |
-- Plus operator (affirmation) |
| 4814 |
|
| 4815 |
when N_Op_Plus => |
| 4816 |
Lo := Rlo; |
| 4817 |
Hi := Rhi; |
| 4818 |
|
| 4819 |
-- Remainder |
| 4820 |
|
| 4821 |
when N_Op_Rem => |
| 4822 |
declare |
| 4823 |
Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
| 4824 |
-- This is the maximum absolute value of the result. Note |
| 4825 |
-- that the result range does not depend on the sign of the |
| 4826 |
-- right operand. |
| 4827 |
|
| 4828 |
begin |
| 4829 |
Lo := Uint_0; |
| 4830 |
Hi := Uint_0; |
| 4831 |
|
| 4832 |
-- Case of left operand negative, which results in a range |
| 4833 |
-- of -Maxabs .. 0 for those negative values. If there are |
| 4834 |
-- no negative values then Lo value of result is always 0. |
| 4835 |
|
| 4836 |
if Llo < 0 then |
| 4837 |
Lo := -Maxabs; |
| 4838 |
end if; |
| 4839 |
|
| 4840 |
-- Case of left operand positive |
| 4841 |
|
| 4842 |
if Lhi > 0 then |
| 4843 |
Hi := Maxabs; |
| 4844 |
end if; |
| 4845 |
end; |
| 4846 |
|
| 4847 |
-- Subtract |
| 4848 |
|
| 4849 |
when N_Op_Subtract => |
| 4850 |
Lo := Llo - Rhi; |
| 4851 |
Hi := Lhi - Rlo; |
| 4852 |
|
| 4853 |
-- Nothing else should be possible |
| 4854 |
|
| 4855 |
when others => |
| 4856 |
raise Program_Error; |
| 4857 |
end case; |
| 4858 |
end Compute_Range_For_Arithmetic_Op; |
| 4859 |
|
| 4860 |
---------------------------------- |
| 4861 |
-- Conditional_Statements_Begin -- |
| 4862 |
---------------------------------- |
| 4863 |
|
| 4864 |
procedure Conditional_Statements_Begin is |
| 4865 |
begin |
| 4866 |
Saved_Checks_TOS := Saved_Checks_TOS + 1; |
| 4867 |
|
| 4868 |
-- If stack overflows, kill all checks, that way we know to simply reset |
| 4869 |
-- the number of saved checks to zero on return. This should never occur |
| 4870 |
-- in practice. |
| 4871 |
|
| 4872 |
if Saved_Checks_TOS > Saved_Checks_Stack'Last then |
| 4873 |
Kill_All_Checks; |
| 4874 |
|
| 4875 |
-- In the normal case, we just make a new stack entry saving the current |
| 4876 |
-- number of saved checks for a later restore. |
| 4877 |
|
| 4878 |
else |
| 4879 |
Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; |
| 4880 |
|
| 4881 |
if Debug_Flag_CC then |
| 4882 |
w ("Conditional_Statements_Begin: Num_Saved_Checks = ", |
| 4883 |
Num_Saved_Checks); |
| 4884 |
end if; |
| 4885 |
end if; |
| 4886 |
end Conditional_Statements_Begin; |
| 4887 |
|
| 4888 |
-------------------------------- |
| 4889 |
-- Conditional_Statements_End -- |
| 4890 |
-------------------------------- |
| 4891 |
|
| 4892 |
procedure Conditional_Statements_End is |
| 4893 |
begin |
| 4894 |
pragma Assert (Saved_Checks_TOS > 0); |
| 4895 |
|
| 4896 |
-- If the saved checks stack overflowed, then we killed all checks, so |
| 4897 |
-- setting the number of saved checks back to zero is correct. This |
| 4898 |
-- should never occur in practice. |
| 4899 |
|
| 4900 |
if Saved_Checks_TOS > Saved_Checks_Stack'Last then |
| 4901 |
Num_Saved_Checks := 0; |
| 4902 |
|
| 4903 |
-- In the normal case, restore the number of saved checks from the top |
| 4904 |
-- stack entry. |
| 4905 |
|
| 4906 |
else |
| 4907 |
Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); |
| 4908 |
|
| 4909 |
if Debug_Flag_CC then |
| 4910 |
w ("Conditional_Statements_End: Num_Saved_Checks = ", |
| 4911 |
Num_Saved_Checks); |
| 4912 |
end if; |
| 4913 |
end if; |
| 4914 |
|
| 4915 |
Saved_Checks_TOS := Saved_Checks_TOS - 1; |
| 4916 |
end Conditional_Statements_End; |
| 4917 |
|
| 4918 |
------------------------- |
| 4919 |
-- Convert_From_Bignum -- |
| 4920 |
------------------------- |
| 4921 |
|
| 4922 |
function Convert_From_Bignum (N : Node_Id) return Node_Id is |
| 4923 |
Loc : constant Source_Ptr := Sloc (N); |
| 4924 |
|
| 4925 |
begin |
| 4926 |
pragma Assert (Is_RTE (Etype (N), RE_Bignum)); |
| 4927 |
|
| 4928 |
-- Construct call From Bignum |
| 4929 |
|
| 4930 |
return |
| 4931 |
Make_Function_Call (Loc, |
| 4932 |
Name => |
| 4933 |
New_Occurrence_Of (RTE (RE_From_Bignum), Loc), |
| 4934 |
Parameter_Associations => New_List (Relocate_Node (N))); |
| 4935 |
end Convert_From_Bignum; |
| 4936 |
|
| 4937 |
----------------------- |
| 4938 |
-- Convert_To_Bignum -- |
| 4939 |
----------------------- |
| 4940 |
|
| 4941 |
function Convert_To_Bignum (N : Node_Id) return Node_Id is |
| 4942 |
Loc : constant Source_Ptr := Sloc (N); |
| 4943 |
|
| 4944 |
begin |
| 4945 |
-- Nothing to do if Bignum already except call Relocate_Node |
| 4946 |
|
| 4947 |
if Is_RTE (Etype (N), RE_Bignum) then |
| 4948 |
return Relocate_Node (N); |
| 4949 |
|
| 4950 |
-- Otherwise construct call to To_Bignum, converting the operand to the |
| 4951 |
-- required Long_Long_Integer form. |
| 4952 |
|
| 4953 |
else |
| 4954 |
pragma Assert (Is_Signed_Integer_Type (Etype (N))); |
| 4955 |
return |
| 4956 |
Make_Function_Call (Loc, |
| 4957 |
Name => |
| 4958 |
New_Occurrence_Of (RTE (RE_To_Bignum), Loc), |
| 4959 |
Parameter_Associations => New_List ( |
| 4960 |
Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); |
| 4961 |
end if; |
| 4962 |
end Convert_To_Bignum; |
| 4963 |
|
| 4964 |
--------------------- |
| 4965 |
-- Determine_Range -- |
| 4966 |
--------------------- |
| 4967 |
|
| 4968 |
Cache_Size : constant := 2 ** 10; |
| 4969 |
type Cache_Index is range 0 .. Cache_Size - 1; |
| 4970 |
-- Determine size of below cache (power of 2 is more efficient) |
| 4971 |
|
| 4972 |
Determine_Range_Cache_N : array (Cache_Index) of Node_Id; |
| 4973 |
Determine_Range_Cache_O : array (Cache_Index) of Node_Id; |
| 4974 |
Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
| 4975 |
Determine_Range_Cache_Lo : array (Cache_Index) of Uint; |
| 4976 |
Determine_Range_Cache_Hi : array (Cache_Index) of Uint; |
| 4977 |
Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal; |
| 4978 |
Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal; |
| 4979 |
-- The above arrays are used to implement a small direct cache for |
| 4980 |
-- Determine_Range and Determine_Range_R calls. Because of the way these |
| 4981 |
-- subprograms recursively traces subexpressions, and because overflow |
| 4982 |
-- checking calls the routine on the way up the tree, a quadratic behavior |
| 4983 |
-- can otherwise be encountered in large expressions. The cache entry for |
| 4984 |
-- node N is stored in the (N mod Cache_Size) entry, and can be validated |
| 4985 |
-- by checking the actual node value stored there. The Range_Cache_O array |
| 4986 |
-- records the setting of Original_Node (N) so that the cache entry does |
| 4987 |
-- not become stale when the node N is rewritten. The Range_Cache_V array |
| 4988 |
-- records the setting of Assume_Valid for the cache entry. |
| 4989 |
|
| 4990 |
procedure Determine_Range |
| 4991 |
(N : Node_Id; |
| 4992 |
OK : out Boolean; |
| 4993 |
Lo : out Uint; |
| 4994 |
Hi : out Uint; |
| 4995 |
Assume_Valid : Boolean := False) |
| 4996 |
is |
| 4997 |
Kind : constant Node_Kind := Nkind (N); |
| 4998 |
-- Kind of node |
| 4999 |
|
| 5000 |
function Half_Address_Space return Uint; |
| 5001 |
-- The size of half the total addressable memory space in storage units |
| 5002 |
-- (minus one, so that the size fits in a signed integer whose size is |
| 5003 |
-- System_Address_Size, which helps in various cases). |
| 5004 |
|
| 5005 |
------------------------ |
| 5006 |
-- Half_Address_Space -- |
| 5007 |
------------------------ |
| 5008 |
|
| 5009 |
function Half_Address_Space return Uint is |
| 5010 |
begin |
| 5011 |
return Uint_2 ** (System_Address_Size - 1) - 1; |
| 5012 |
end Half_Address_Space; |
| 5013 |
|
| 5014 |
-- Local variables |
| 5015 |
|
| 5016 |
Typ : Entity_Id := Etype (N); |
| 5017 |
-- Type to use, may get reset to base type for possibly invalid entity |
| 5018 |
|
| 5019 |
Lo_Left : Uint := No_Uint; |
| 5020 |
Hi_Left : Uint := No_Uint; |
| 5021 |
-- Lo and Hi bounds of left operand |
| 5022 |
|
| 5023 |
Lo_Right : Uint := No_Uint; |
| 5024 |
Hi_Right : Uint := No_Uint; |
| 5025 |
-- Lo and Hi bounds of right (or only) operand |
| 5026 |
|
| 5027 |
Bound : Node_Id; |
| 5028 |
-- Temp variable used to hold a bound node |
| 5029 |
|
| 5030 |
Hbound : Uint; |
| 5031 |
-- High bound of base type of expression |
| 5032 |
|
| 5033 |
Lor : Uint; |
| 5034 |
Hir : Uint; |
| 5035 |
-- Refined values for low and high bounds, after tightening |
| 5036 |
|
| 5037 |
OK1 : Boolean; |
| 5038 |
-- Used in lower level calls to indicate if call succeeded |
| 5039 |
|
| 5040 |
Cindex : Cache_Index; |
| 5041 |
-- Used to search cache |
| 5042 |
|
| 5043 |
Btyp : Entity_Id; |
| 5044 |
-- Base type |
| 5045 |
|
| 5046 |
-- Start of processing for Determine_Range |
| 5047 |
|
| 5048 |
begin |
| 5049 |
-- Prevent junk warnings by initializing range variables |
| 5050 |
|
| 5051 |
Lo := No_Uint; |
| 5052 |
Hi := No_Uint; |
| 5053 |
Lor := No_Uint; |
| 5054 |
Hir := No_Uint; |
| 5055 |
|
| 5056 |
-- For temporary constants internally generated to remove side effects |
| 5057 |
-- we must use the corresponding expression to determine the range of |
| 5058 |
-- the expression. But note that the expander can also generate |
| 5059 |
-- constants in other cases, including deferred constants. |
| 5060 |
|
| 5061 |
if Is_Entity_Name (N) |
| 5062 |
and then Nkind (Parent (Entity (N))) = N_Object_Declaration |
| 5063 |
and then Ekind (Entity (N)) = E_Constant |
| 5064 |
and then Is_Internal_Name (Chars (Entity (N))) |
| 5065 |
then |
| 5066 |
if Present (Expression (Parent (Entity (N)))) then |
| 5067 |
Determine_Range |
| 5068 |
(Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); |
| 5069 |
|
| 5070 |
elsif Present (Full_View (Entity (N))) then |
| 5071 |
Determine_Range |
| 5072 |
(Expression (Parent (Full_View (Entity (N)))), |
| 5073 |
OK, Lo, Hi, Assume_Valid); |
| 5074 |
|
| 5075 |
else |
| 5076 |
OK := False; |
| 5077 |
end if; |
| 5078 |
return; |
| 5079 |
end if; |
| 5080 |
|
| 5081 |
-- If type is not defined, we can't determine its range |
| 5082 |
|
| 5083 |
if No (Typ) |
| 5084 |
|
| 5085 |
-- We don't deal with anything except discrete types |
| 5086 |
|
| 5087 |
or else not Is_Discrete_Type (Typ) |
| 5088 |
|
| 5089 |
-- Don't deal with enumerated types with non-standard representation |
| 5090 |
|
| 5091 |
or else (Is_Enumeration_Type (Typ) |
| 5092 |
and then Present (Enum_Pos_To_Rep |
| 5093 |
(Implementation_Base_Type (Typ)))) |
| 5094 |
|
| 5095 |
-- Ignore type for which an error has been posted, since range in |
| 5096 |
-- this case may well be a bogosity deriving from the error. Also |
| 5097 |
-- ignore if error posted on the reference node. |
| 5098 |
|
| 5099 |
or else Error_Posted (N) or else Error_Posted (Typ) |
| 5100 |
then |
| 5101 |
OK := False; |
| 5102 |
return; |
| 5103 |
end if; |
| 5104 |
|
| 5105 |
-- For all other cases, we can determine the range |
| 5106 |
|
| 5107 |
OK := True; |
| 5108 |
|
| 5109 |
-- If value is compile time known, then the possible range is the one |
| 5110 |
-- value that we know this expression definitely has. |
| 5111 |
|
| 5112 |
if Compile_Time_Known_Value (N) then |
| 5113 |
Lo := Expr_Value (N); |
| 5114 |
Hi := Lo; |
| 5115 |
return; |
| 5116 |
end if; |
| 5117 |
|
| 5118 |
-- Return if already in the cache |
| 5119 |
|
| 5120 |
Cindex := Cache_Index (N mod Cache_Size); |
| 5121 |
|
| 5122 |
if Determine_Range_Cache_N (Cindex) = N |
| 5123 |
and then |
| 5124 |
Determine_Range_Cache_O (Cindex) = Original_Node (N) |
| 5125 |
and then |
| 5126 |
Determine_Range_Cache_V (Cindex) = Assume_Valid |
| 5127 |
then |
| 5128 |
Lo := Determine_Range_Cache_Lo (Cindex); |
| 5129 |
Hi := Determine_Range_Cache_Hi (Cindex); |
| 5130 |
return; |
| 5131 |
end if; |
| 5132 |
|
| 5133 |
-- Otherwise, start by finding the bounds of the type of the expression, |
| 5134 |
-- the value cannot be outside this range (if it is, then we have an |
| 5135 |
-- overflow situation, which is a separate check, we are talking here |
| 5136 |
-- only about the expression value). |
| 5137 |
|
| 5138 |
-- First a check, never try to find the bounds of a generic type, since |
| 5139 |
-- these bounds are always junk values, and it is only valid to look at |
| 5140 |
-- the bounds in an instance. |
| 5141 |
|
| 5142 |
if Is_Generic_Type (Typ) then |
| 5143 |
OK := False; |
| 5144 |
return; |
| 5145 |
end if; |
| 5146 |
|
| 5147 |
-- First step, change to use base type unless we know the value is valid |
| 5148 |
|
| 5149 |
if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
| 5150 |
or else Assume_No_Invalid_Values |
| 5151 |
or else Assume_Valid |
| 5152 |
then |
| 5153 |
-- If this is a known valid constant with a nonstatic value, it may |
| 5154 |
-- have inherited a narrower subtype from its initial value; use this |
| 5155 |
-- saved subtype (see sem_ch3.adb). |
| 5156 |
|
| 5157 |
if Is_Entity_Name (N) |
| 5158 |
and then Ekind (Entity (N)) = E_Constant |
| 5159 |
and then Present (Actual_Subtype (Entity (N))) |
| 5160 |
then |
| 5161 |
Typ := Actual_Subtype (Entity (N)); |
| 5162 |
end if; |
| 5163 |
|
| 5164 |
else |
| 5165 |
Typ := Underlying_Type (Base_Type (Typ)); |
| 5166 |
end if; |
| 5167 |
|
| 5168 |
-- Retrieve the base type. Handle the case where the base type is a |
| 5169 |
-- private enumeration type. |
| 5170 |
|
| 5171 |
Btyp := Base_Type (Typ); |
| 5172 |
|
| 5173 |
if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then |
| 5174 |
Btyp := Full_View (Btyp); |
| 5175 |
end if; |
| 5176 |
|
| 5177 |
-- We use the actual bound unless it is dynamic, in which case use the |
| 5178 |
-- corresponding base type bound if possible. If we can't get a bound |
| 5179 |
-- then we figure we can't determine the range (a peculiar case, that |
| 5180 |
-- perhaps cannot happen, but there is no point in bombing in this |
| 5181 |
-- optimization circuit). |
| 5182 |
|
| 5183 |
-- First the low bound |
| 5184 |
|
| 5185 |
Bound := Type_Low_Bound (Typ); |
| 5186 |
|
| 5187 |
if Compile_Time_Known_Value (Bound) then |
| 5188 |
Lo := Expr_Value (Bound); |
| 5189 |
|
| 5190 |
elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
| 5191 |
Lo := Expr_Value (Type_Low_Bound (Btyp)); |
| 5192 |
|
| 5193 |
else |
| 5194 |
OK := False; |
| 5195 |
return; |
| 5196 |
end if; |
| 5197 |
|
| 5198 |
-- Now the high bound |
| 5199 |
|
| 5200 |
Bound := Type_High_Bound (Typ); |
| 5201 |
|
| 5202 |
-- We need the high bound of the base type later on, and this should |
| 5203 |
-- always be compile time known. Again, it is not clear that this |
| 5204 |
-- can ever be false, but no point in bombing. |
| 5205 |
|
| 5206 |
if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
| 5207 |
Hbound := Expr_Value (Type_High_Bound (Btyp)); |
| 5208 |
Hi := Hbound; |
| 5209 |
|
| 5210 |
else |
| 5211 |
OK := False; |
| 5212 |
return; |
| 5213 |
end if; |
| 5214 |
|
| 5215 |
-- If we have a static subtype, then that may have a tighter bound so |
| 5216 |
-- use the upper bound of the subtype instead in this case. |
| 5217 |
|
| 5218 |
if Compile_Time_Known_Value (Bound) then |
| 5219 |
Hi := Expr_Value (Bound); |
| 5220 |
end if; |
| 5221 |
|
| 5222 |
-- We may be able to refine this value in certain situations. If any |
| 5223 |
-- refinement is possible, then Lor and Hir are set to possibly tighter |
| 5224 |
-- bounds, and OK1 is set to True. |
| 5225 |
|
| 5226 |
case Kind is |
| 5227 |
|
| 5228 |
-- Unary operation case |
| 5229 |
|
| 5230 |
when N_Op_Abs |
| 5231 |
| N_Op_Minus |
| 5232 |
| N_Op_Plus |
| 5233 |
=> |
| 5234 |
Determine_Range |
| 5235 |
(Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); |
| 5236 |
|
| 5237 |
if OK1 then |
| 5238 |
Compute_Range_For_Arithmetic_Op |
| 5239 |
(Kind, Lo_Left, Hi_Left, Lo_Right, Hi_Right, OK1, Lor, Hir); |
| 5240 |
end if; |
| 5241 |
|
| 5242 |
-- Binary operation case |
| 5243 |
|
| 5244 |
when N_Op_Add |
| 5245 |
| N_Op_Divide |
| 5246 |
| N_Op_Expon |
| 5247 |
| N_Op_Mod |
| 5248 |
| N_Op_Multiply |
| 5249 |
| N_Op_Rem |
| 5250 |
| N_Op_Subtract |
| 5251 |
=> |
| 5252 |
Determine_Range |
| 5253 |
(Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); |
| 5254 |
|
| 5255 |
if OK1 then |
| 5256 |
Determine_Range |
| 5257 |
(Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); |
| 5258 |
end if; |
| 5259 |
|
| 5260 |
if OK1 then |
| 5261 |
Compute_Range_For_Arithmetic_Op |
| 5262 |
(Kind, Lo_Left, Hi_Left, Lo_Right, Hi_Right, OK1, Lor, Hir); |
| 5263 |
end if; |
| 5264 |
|
| 5265 |
-- Attribute reference cases |
| 5266 |
|
| 5267 |
when N_Attribute_Reference => |
| 5268 |
case Get_Attribute_Id (Attribute_Name (N)) is |
| 5269 |
|
| 5270 |
-- For Min/Max attributes, we can refine the range using the |
| 5271 |
-- possible range of values of the attribute expressions. |
| 5272 |
|
| 5273 |
when Attribute_Min |
| 5274 |
| Attribute_Max |
| 5275 |
=> |
| 5276 |
Determine_Range |
| 5277 |
(First (Expressions (N)), |
| 5278 |
OK1, Lo_Left, Hi_Left, Assume_Valid); |
| 5279 |
|
| 5280 |
if OK1 then |
| 5281 |
Determine_Range |
| 5282 |
(Next (First (Expressions (N))), |
| 5283 |
OK1, Lo_Right, Hi_Right, Assume_Valid); |
| 5284 |
end if; |
| 5285 |
|
| 5286 |
if OK1 then |
| 5287 |
Lor := UI_Min (Lo_Left, Lo_Right); |
| 5288 |
Hir := UI_Max (Hi_Left, Hi_Right); |
| 5289 |
end if; |
| 5290 |
|
| 5291 |
-- For Pos/Val attributes, we can refine the range using the |
| 5292 |
-- possible range of values of the attribute expression. |
| 5293 |
|
| 5294 |
when Attribute_Pos |
| 5295 |
| Attribute_Val |
| 5296 |
=> |
| 5297 |
Determine_Range |
| 5298 |
(First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); |
| 5299 |
|
| 5300 |
-- For Length and Range_Length attributes, use the bounds of |
| 5301 |
-- the (corresponding index) type to refine the range. |
| 5302 |
|
| 5303 |
when Attribute_Length |
| 5304 |
| Attribute_Range_Length |
| 5305 |
=> |
| 5306 |
declare |
| 5307 |
Ptyp : Entity_Id; |
| 5308 |
Ityp : Entity_Id; |
| 5309 |
|
| 5310 |
LL, LU : Uint; |
| 5311 |
UL, UU : Uint; |
| 5312 |
|
| 5313 |
begin |
| 5314 |
Ptyp := Etype (Prefix (N)); |
| 5315 |
if Is_Access_Type (Ptyp) then |
| 5316 |
Ptyp := Designated_Type (Ptyp); |
| 5317 |
end if; |
| 5318 |
|
| 5319 |
-- For string literal, we know exact value |
| 5320 |
|
| 5321 |
if Ekind (Ptyp) = E_String_Literal_Subtype then |
| 5322 |
OK := True; |
| 5323 |
Lo := String_Literal_Length (Ptyp); |
| 5324 |
Hi := String_Literal_Length (Ptyp); |
| 5325 |
return; |
| 5326 |
end if; |
| 5327 |
|
| 5328 |
if Is_Array_Type (Ptyp) then |
| 5329 |
Ityp := Get_Index_Subtype (N); |
| 5330 |
else |
| 5331 |
Ityp := Ptyp; |
| 5332 |
end if; |
| 5333 |
|
| 5334 |
-- If the (index) type is a formal type or derived from |
| 5335 |
-- one, the bounds are not static. |
| 5336 |
|
| 5337 |
if Is_Generic_Type (Root_Type (Ityp)) then |
| 5338 |
OK := False; |
| 5339 |
return; |
| 5340 |
end if; |
| 5341 |
|
| 5342 |
Determine_Range |
| 5343 |
(Type_Low_Bound (Ityp), OK1, LL, LU, Assume_Valid); |
| 5344 |
|
| 5345 |
if OK1 then |
| 5346 |
Determine_Range |
| 5347 |
(Type_High_Bound (Ityp), OK1, UL, UU, Assume_Valid); |
| 5348 |
|
| 5349 |
if OK1 then |
| 5350 |
-- The maximum value for Length is the biggest |
| 5351 |
-- possible gap between the values of the bounds. |
| 5352 |
-- But of course, this value cannot be negative. |
| 5353 |
|
| 5354 |
Hir := UI_Max (Uint_0, UU - LL + 1); |
| 5355 |
|
| 5356 |
-- For a constrained array, the minimum value for |
| 5357 |
-- Length is taken from the actual value of the |
| 5358 |
-- bounds, since the index will be exactly of this |
| 5359 |
-- subtype. |
| 5360 |
|
| 5361 |
if Is_Constrained (Ptyp) then |
| 5362 |
Lor := UI_Max (Uint_0, UL - LU + 1); |
| 5363 |
|
| 5364 |
-- For an unconstrained array, the minimum value |
| 5365 |
-- for length is always zero. |
| 5366 |
|
| 5367 |
else |
| 5368 |
Lor := Uint_0; |
| 5369 |
end if; |
| 5370 |
end if; |
| 5371 |
end if; |
| 5372 |
|
| 5373 |
-- Small optimization: the maximum size in storage units |
| 5374 |
-- an object can have with GNAT is half of the address |
| 5375 |
-- space, so we can bound the length of an array declared |
| 5376 |
-- in Interfaces (or its children) because its component |
| 5377 |
-- size is at least the storage unit and it is meant to |
| 5378 |
-- be used to interface actual array objects. |
| 5379 |
|
| 5380 |
if Is_Array_Type (Ptyp) then |
| 5381 |
declare |
| 5382 |
S : constant Entity_Id := Scope (Base_Type (Ptyp)); |
| 5383 |
begin |
| 5384 |
if Is_RTU (S, Interfaces) |
| 5385 |
or else (S /= Standard_Standard |
| 5386 |
and then Is_RTU (Scope (S), Interfaces)) |
| 5387 |
then |
| 5388 |
Hir := UI_Min (Hir, Half_Address_Space); |
| 5389 |
end if; |
| 5390 |
end; |
| 5391 |
end if; |
| 5392 |
end; |
| 5393 |
|
| 5394 |
-- The maximum default alignment is quite low, but GNAT accepts |
| 5395 |
-- alignment clauses that are fairly large, but not as large as |
| 5396 |
-- the maximum size of objects, see below. |
| 5397 |
|
| 5398 |
when Attribute_Alignment => |
| 5399 |
Lor := Uint_0; |
| 5400 |
Hir := Half_Address_Space; |
| 5401 |
OK1 := True; |
| 5402 |
|
| 5403 |
-- The attribute should have been folded if a component clause |
| 5404 |
-- was specified, so we assume there is none. |
| 5405 |
|
| 5406 |
when Attribute_Bit |
| 5407 |
| Attribute_First_Bit |
| 5408 |
=> |
| 5409 |
Lor := Uint_0; |
| 5410 |
Hir := UI_From_Int (System_Storage_Unit - 1); |
| 5411 |
OK1 := True; |
| 5412 |
|
| 5413 |
-- Likewise about the component clause. Note that Last_Bit |
| 5414 |
-- yields -1 for a field of size 0 if First_Bit is 0. |
| 5415 |
|
| 5416 |
when Attribute_Last_Bit => |
| 5417 |
Lor := Uint_Minus_1; |
| 5418 |
Hir := Hi; |
| 5419 |
OK1 := True; |
| 5420 |
|
| 5421 |
-- Likewise about the component clause for Position. The |
| 5422 |
-- maximum size in storage units that an object can have |
| 5423 |
-- with GNAT is half of the address space. |
| 5424 |
|
| 5425 |
when Attribute_Max_Size_In_Storage_Elements |
| 5426 |
| Attribute_Position |
| 5427 |
=> |
| 5428 |
Lor := Uint_0; |
| 5429 |
Hir := Half_Address_Space; |
| 5430 |
OK1 := True; |
| 5431 |
|
| 5432 |
-- These attributes yield a nonnegative value (we do not set |
| 5433 |
-- the maximum value because it is too large to be useful). |
| 5434 |
|
| 5435 |
when Attribute_Bit_Position |
| 5436 |
| Attribute_Component_Size |
| 5437 |
| Attribute_Object_Size |
| 5438 |
| Attribute_Size |
| 5439 |
| Attribute_Value_Size |
| 5440 |
=> |
| 5441 |
Lor := Uint_0; |
| 5442 |
Hir := Hi; |
| 5443 |
OK1 := True; |
| 5444 |
|
| 5445 |
-- The maximum size is the sum of twice the size of the largest |
| 5446 |
-- integer for every dimension, rounded up to the next multiple |
| 5447 |
-- of the maximum alignment, but we add instead of rounding. |
| 5448 |
|
| 5449 |
when Attribute_Descriptor_Size => |
| 5450 |
declare |
| 5451 |
Max_Align : constant Pos := |
| 5452 |
Maximum_Alignment * System_Storage_Unit; |
| 5453 |
Max_Size : constant Uint := |
| 5454 |
2 * Esize (Universal_Integer); |
| 5455 |
Ndims : constant Pos := |
| 5456 |
Number_Dimensions (Etype (Prefix (N))); |
| 5457 |
begin |
| 5458 |
Lor := Uint_0; |
| 5459 |
Hir := Max_Size * Ndims + Max_Align; |
| 5460 |
OK1 := True; |
| 5461 |
end; |
| 5462 |
|
| 5463 |
-- No special handling for other attributes for now |
| 5464 |
|
| 5465 |
when others => |
| 5466 |
OK1 := False; |
| 5467 |
|
| 5468 |
end case; |
| 5469 |
|
| 5470 |
when N_Type_Conversion => |
| 5471 |
-- For a type conversion, we can try to refine the range using the |
| 5472 |
-- converted value. |
| 5473 |
|
| 5474 |
Determine_Range_To_Discrete |
| 5475 |
(Expression (N), OK1, Lor, Hir, Conversion_OK (N), Assume_Valid); |
| 5476 |
|
| 5477 |
-- Nothing special to do for all other expression kinds |
| 5478 |
|
| 5479 |
when others => |
| 5480 |
OK1 := False; |
| 5481 |
Lor := No_Uint; |
| 5482 |
Hir := No_Uint; |
| 5483 |
end case; |
| 5484 |
|
| 5485 |
-- At this stage, if OK1 is true, then we know that the actual result of |
| 5486 |
-- the computed expression is in the range Lor .. Hir. We can use this |
| 5487 |
-- to restrict the possible range of results. |
| 5488 |
|
| 5489 |
if OK1 then |
| 5490 |
|
| 5491 |
-- If the refined value of the low bound is greater than the type |
| 5492 |
-- low bound, then reset it to the more restrictive value. However, |
| 5493 |
-- we do NOT do this for the case of a modular type where the |
| 5494 |
-- possible upper bound on the value is above the base type high |
| 5495 |
-- bound, because that means the result could wrap. |
| 5496 |
-- Same applies for the lower bound if it is negative. |
| 5497 |
|
| 5498 |
if Is_Modular_Integer_Type (Typ) then |
| 5499 |
if Lor > Lo and then Hir <= Hbound then |
| 5500 |
Lo := Lor; |
| 5501 |
end if; |
| 5502 |
|
| 5503 |
if Hir < Hi and then Lor >= Uint_0 then |
| 5504 |
Hi := Hir; |
| 5505 |
end if; |
| 5506 |
|
| 5507 |
else |
| 5508 |
if Lor > Hi or else Hir < Lo then |
| 5509 |
|
| 5510 |
-- If the ranges are disjoint, return the computed range. |
| 5511 |
|
| 5512 |
-- The current range-constraining logic would require returning |
| 5513 |
-- the base type's bounds. However, this would miss an |
| 5514 |
-- opportunity to warn about out-of-range values for some cases |
| 5515 |
-- (e.g. when type's upper bound is equal to base type upper |
| 5516 |
-- bound). |
| 5517 |
|
| 5518 |
-- The alternative of always returning the computed values, |
| 5519 |
-- even when ranges are intersecting, has unwanted effects |
| 5520 |
-- (mainly useless constraint checks are inserted) in the |
| 5521 |
-- Enable_Overflow_Check and Apply_Scalar_Range_Check as these |
| 5522 |
-- bounds have a special interpretation. |
| 5523 |
|
| 5524 |
Lo := Lor; |
| 5525 |
Hi := Hir; |
| 5526 |
else |
| 5527 |
|
| 5528 |
-- If the ranges Lor .. Hir and Lo .. Hi intersect, try to |
| 5529 |
-- refine the returned range. |
| 5530 |
|
| 5531 |
if Lor > Lo then |
| 5532 |
Lo := Lor; |
| 5533 |
end if; |
| 5534 |
|
| 5535 |
if Hir < Hi then |
| 5536 |
Hi := Hir; |
| 5537 |
end if; |
| 5538 |
end if; |
| 5539 |
end if; |
| 5540 |
end if; |
| 5541 |
|
| 5542 |
-- Set cache entry for future call and we are all done |
| 5543 |
|
| 5544 |
Determine_Range_Cache_N (Cindex) := N; |
| 5545 |
Determine_Range_Cache_O (Cindex) := Original_Node (N); |
| 5546 |
Determine_Range_Cache_V (Cindex) := Assume_Valid; |
| 5547 |
Determine_Range_Cache_Lo (Cindex) := Lo; |
| 5548 |
Determine_Range_Cache_Hi (Cindex) := Hi; |
| 5549 |
return; |
| 5550 |
|
| 5551 |
-- If any exception occurs, it means that we have some bug in the compiler, |
| 5552 |
-- possibly triggered by a previous error, or by some unforeseen peculiar |
| 5553 |
-- occurrence. However, this is only an optimization attempt, so there is |
| 5554 |
-- really no point in crashing the compiler. Instead we just decide, too |
| 5555 |
-- bad, we can't figure out a range in this case after all. |
| 5556 |
|
| 5557 |
exception |
| 5558 |
when others => |
| 5559 |
|
| 5560 |
-- Debug flag K disables this behavior (useful for debugging) |
| 5561 |
|
| 5562 |
if Debug_Flag_K then |
| 5563 |
raise; |
| 5564 |
else |
| 5565 |
OK := False; |
| 5566 |
Lo := No_Uint; |
| 5567 |
Hi := No_Uint; |
| 5568 |
return; |
| 5569 |
end if; |
| 5570 |
end Determine_Range; |
| 5571 |
|
| 5572 |
----------------------- |
| 5573 |
-- Determine_Range_R -- |
| 5574 |
----------------------- |
| 5575 |
|
| 5576 |
procedure Determine_Range_R |
| 5577 |
(N : Node_Id; |
| 5578 |
OK : out Boolean; |
| 5579 |
Lo : out Ureal; |
| 5580 |
Hi : out Ureal; |
| 5581 |
Assume_Valid : Boolean := False) |
| 5582 |
is |
| 5583 |
Typ : Entity_Id := Etype (N); |
| 5584 |
-- Type to use, may get reset to base type for possibly invalid entity |
| 5585 |
|
| 5586 |
Lo_Left : Ureal; |
| 5587 |
Hi_Left : Ureal; |
| 5588 |
-- Lo and Hi bounds of left operand |
| 5589 |
|
| 5590 |
Lo_Right : Ureal := No_Ureal; |
| 5591 |
Hi_Right : Ureal := No_Ureal; |
| 5592 |
-- Lo and Hi bounds of right (or only) operand |
| 5593 |
|
| 5594 |
Bound : Node_Id; |
| 5595 |
-- Temp variable used to hold a bound node |
| 5596 |
|
| 5597 |
Hbound : Ureal; |
| 5598 |
-- High bound of base type of expression |
| 5599 |
|
| 5600 |
Lor : Ureal; |
| 5601 |
Hir : Ureal; |
| 5602 |
-- Refined values for low and high bounds, after tightening |
| 5603 |
|
| 5604 |
OK1 : Boolean; |
| 5605 |
-- Used in lower level calls to indicate if call succeeded |
| 5606 |
|
| 5607 |
Cindex : Cache_Index; |
| 5608 |
-- Used to search cache |
| 5609 |
|
| 5610 |
Btyp : Entity_Id; |
| 5611 |
-- Base type |
| 5612 |
|
| 5613 |
function OK_Operands return Boolean; |
| 5614 |
-- Used for binary operators. Determines the ranges of the left and |
| 5615 |
-- right operands, and if they are both OK, returns True, and puts |
| 5616 |
-- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
| 5617 |
|
| 5618 |
function Round_Machine (B : Ureal) return Ureal; |
| 5619 |
-- B is a real bound. Round it to the nearest machine number. |
| 5620 |
|
| 5621 |
----------------- |
| 5622 |
-- OK_Operands -- |
| 5623 |
----------------- |
| 5624 |
|
| 5625 |
function OK_Operands return Boolean is |
| 5626 |
begin |
| 5627 |
Determine_Range_R |
| 5628 |
(Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); |
| 5629 |
|
| 5630 |
if not OK1 then |
| 5631 |
return False; |
| 5632 |
end if; |
| 5633 |
|
| 5634 |
Determine_Range_R |
| 5635 |
(Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); |
| 5636 |
return OK1; |
| 5637 |
end OK_Operands; |
| 5638 |
|
| 5639 |
------------------- |
| 5640 |
-- Round_Machine -- |
| 5641 |
------------------- |
| 5642 |
|
| 5643 |
function Round_Machine (B : Ureal) return Ureal is |
| 5644 |
begin |
| 5645 |
return Machine_Number (Typ, B, N); |
| 5646 |
end Round_Machine; |
| 5647 |
|
| 5648 |
-- Start of processing for Determine_Range_R |
| 5649 |
|
| 5650 |
begin |
| 5651 |
-- Prevent junk warnings by initializing range variables |
| 5652 |
|
| 5653 |
Lo := No_Ureal; |
| 5654 |
Hi := No_Ureal; |
| 5655 |
Lor := No_Ureal; |
| 5656 |
Hir := No_Ureal; |
| 5657 |
|
| 5658 |
-- For temporary constants internally generated to remove side effects |
| 5659 |
-- we must use the corresponding expression to determine the range of |
| 5660 |
-- the expression. But note that the expander can also generate |
| 5661 |
-- constants in other cases, including deferred constants. |
| 5662 |
|
| 5663 |
if Is_Entity_Name (N) |
| 5664 |
and then Nkind (Parent (Entity (N))) = N_Object_Declaration |
| 5665 |
and then Ekind (Entity (N)) = E_Constant |
| 5666 |
and then Is_Internal_Name (Chars (Entity (N))) |
| 5667 |
then |
| 5668 |
if Present (Expression (Parent (Entity (N)))) then |
| 5669 |
Determine_Range_R |
| 5670 |
(Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); |
| 5671 |
|
| 5672 |
elsif Present (Full_View (Entity (N))) then |
| 5673 |
Determine_Range_R |
| 5674 |
(Expression (Parent (Full_View (Entity (N)))), |
| 5675 |
OK, Lo, Hi, Assume_Valid); |
| 5676 |
|
| 5677 |
else |
| 5678 |
OK := False; |
| 5679 |
end if; |
| 5680 |
|
| 5681 |
return; |
| 5682 |
end if; |
| 5683 |
|
| 5684 |
-- If type is not defined, we can't determine its range |
| 5685 |
|
| 5686 |
pragma Warnings (Off, "condition can only be True if invalid"); |
| 5687 |
-- Otherwise the compiler warns on the check of Float_Rep below, because |
| 5688 |
-- there is only one value (see types.ads). |
| 5689 |
|
| 5690 |
if No (Typ) |
| 5691 |
|
| 5692 |
-- We don't deal with anything except IEEE floating-point types |
| 5693 |
|
| 5694 |
or else not Is_Floating_Point_Type (Typ) |
| 5695 |
or else Float_Rep (Typ) /= IEEE_Binary |
| 5696 |
|
| 5697 |
-- Ignore type for which an error has been posted, since range in |
| 5698 |
-- this case may well be a bogosity deriving from the error. Also |
| 5699 |
-- ignore if error posted on the reference node. |
| 5700 |
|
| 5701 |
or else Error_Posted (N) or else Error_Posted (Typ) |
| 5702 |
then |
| 5703 |
pragma Warnings (On, "condition can only be True if invalid"); |
| 5704 |
OK := False; |
| 5705 |
return; |
| 5706 |
end if; |
| 5707 |
|
| 5708 |
-- For all other cases, we can determine the range |
| 5709 |
|
| 5710 |
OK := True; |
| 5711 |
|
| 5712 |
-- If value is compile time known, then the possible range is the one |
| 5713 |
-- value that we know this expression definitely has. |
| 5714 |
|
| 5715 |
if Compile_Time_Known_Value (N) then |
| 5716 |
Lo := Expr_Value_R (N); |
| 5717 |
Hi := Lo; |
| 5718 |
return; |
| 5719 |
end if; |
| 5720 |
|
| 5721 |
-- Return if already in the cache |
| 5722 |
|
| 5723 |
Cindex := Cache_Index (N mod Cache_Size); |
| 5724 |
|
| 5725 |
if Determine_Range_Cache_N (Cindex) = N |
| 5726 |
and then |
| 5727 |
Determine_Range_Cache_O (Cindex) = Original_Node (N) |
| 5728 |
and then |
| 5729 |
Determine_Range_Cache_V (Cindex) = Assume_Valid |
| 5730 |
then |
| 5731 |
Lo := Determine_Range_Cache_Lo_R (Cindex); |
| 5732 |
Hi := Determine_Range_Cache_Hi_R (Cindex); |
| 5733 |
return; |
| 5734 |
end if; |
| 5735 |
|
| 5736 |
-- Otherwise, start by finding the bounds of the type of the expression, |
| 5737 |
-- the value cannot be outside this range (if it is, then we have an |
| 5738 |
-- overflow situation, which is a separate check, we are talking here |
| 5739 |
-- only about the expression value). |
| 5740 |
|
| 5741 |
-- First a check, never try to find the bounds of a generic type, since |
| 5742 |
-- these bounds are always junk values, and it is only valid to look at |
| 5743 |
-- the bounds in an instance. |
| 5744 |
|
| 5745 |
if Is_Generic_Type (Typ) then |
| 5746 |
OK := False; |
| 5747 |
return; |
| 5748 |
end if; |
| 5749 |
|
| 5750 |
-- First step, change to use base type unless we know the value is valid |
| 5751 |
|
| 5752 |
if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
| 5753 |
or else Assume_No_Invalid_Values |
| 5754 |
or else Assume_Valid |
| 5755 |
then |
| 5756 |
null; |
| 5757 |
else |
| 5758 |
Typ := Underlying_Type (Base_Type (Typ)); |
| 5759 |
end if; |
| 5760 |
|
| 5761 |
-- Retrieve the base type. Handle the case where the base type is a |
| 5762 |
-- private type. |
| 5763 |
|
| 5764 |
Btyp := Base_Type (Typ); |
| 5765 |
|
| 5766 |
if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then |
| 5767 |
Btyp := Full_View (Btyp); |
| 5768 |
end if; |
| 5769 |
|
| 5770 |
-- We use the actual bound unless it is dynamic, in which case use the |
| 5771 |
-- corresponding base type bound if possible. If we can't get a bound |
| 5772 |
-- then we figure we can't determine the range (a peculiar case, that |
| 5773 |
-- perhaps cannot happen, but there is no point in bombing in this |
| 5774 |
-- optimization circuit). |
| 5775 |
|
| 5776 |
-- First the low bound |
| 5777 |
|
| 5778 |
Bound := Type_Low_Bound (Typ); |
| 5779 |
|
| 5780 |
if Compile_Time_Known_Value (Bound) then |
| 5781 |
Lo := Expr_Value_R (Bound); |
| 5782 |
|
| 5783 |
elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
| 5784 |
Lo := Expr_Value_R (Type_Low_Bound (Btyp)); |
| 5785 |
|
| 5786 |
else |
| 5787 |
OK := False; |
| 5788 |
return; |
| 5789 |
end if; |
| 5790 |
|
| 5791 |
-- Now the high bound |
| 5792 |
|
| 5793 |
Bound := Type_High_Bound (Typ); |
| 5794 |
|
| 5795 |
-- We need the high bound of the base type later on, and this should |
| 5796 |
-- always be compile time known. Again, it is not clear that this |
| 5797 |
-- can ever be false, but no point in bombing. |
| 5798 |
|
| 5799 |
if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
| 5800 |
Hbound := Expr_Value_R (Type_High_Bound (Btyp)); |
| 5801 |
Hi := Hbound; |
| 5802 |
|
| 5803 |
else |
| 5804 |
OK := False; |
| 5805 |
return; |
| 5806 |
end if; |
| 5807 |
|
| 5808 |
-- If we have a static subtype, then that may have a tighter bound so |
| 5809 |
-- use the upper bound of the subtype instead in this case. |
| 5810 |
|
| 5811 |
if Compile_Time_Known_Value (Bound) then |
| 5812 |
Hi := Expr_Value_R (Bound); |
| 5813 |
end if; |
| 5814 |
|
| 5815 |
-- We may be able to refine this value in certain situations. If any |
| 5816 |
-- refinement is possible, then Lor and Hir are set to possibly tighter |
| 5817 |
-- bounds, and OK1 is set to True. |
| 5818 |
|
| 5819 |
case Nkind (N) is |
| 5820 |
|
| 5821 |
-- For unary plus, result is limited by range of operand |
| 5822 |
|
| 5823 |
when N_Op_Plus => |
| 5824 |
Determine_Range_R |
| 5825 |
(Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); |
| 5826 |
|
| 5827 |
-- For unary minus, determine range of operand, and negate it |
| 5828 |
|
| 5829 |
when N_Op_Minus => |
| 5830 |
Determine_Range_R |
| 5831 |
(Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); |
| 5832 |
|
| 5833 |
if OK1 then |
| 5834 |
Lor := -Hi_Right; |
| 5835 |
Hir := -Lo_Right; |
| 5836 |
end if; |
| 5837 |
|
| 5838 |
-- For binary addition, get range of each operand and do the |
| 5839 |
-- addition to get the result range. |
| 5840 |
|
| 5841 |
when N_Op_Add => |
| 5842 |
if OK_Operands then |
| 5843 |
Lor := Round_Machine (Lo_Left + Lo_Right); |
| 5844 |
Hir := Round_Machine (Hi_Left + Hi_Right); |
| 5845 |
end if; |
| 5846 |
|
| 5847 |
-- For binary subtraction, get range of each operand and do the worst |
| 5848 |
-- case subtraction to get the result range. |
| 5849 |
|
| 5850 |
when N_Op_Subtract => |
| 5851 |
if OK_Operands then |
| 5852 |
Lor := Round_Machine (Lo_Left - Hi_Right); |
| 5853 |
Hir := Round_Machine (Hi_Left - Lo_Right); |
| 5854 |
end if; |
| 5855 |
|
| 5856 |
-- For multiplication, get range of each operand and do the |
| 5857 |
-- four multiplications to get the result range. |
| 5858 |
|
| 5859 |
when N_Op_Multiply => |
| 5860 |
if OK_Operands then |
| 5861 |
declare |
| 5862 |
M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right); |
| 5863 |
M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right); |
| 5864 |
M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right); |
| 5865 |
M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right); |
| 5866 |
|
| 5867 |
begin |
| 5868 |
Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4)); |
| 5869 |
Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4)); |
| 5870 |
end; |
| 5871 |
end if; |
| 5872 |
|
| 5873 |
-- For division, consider separately the cases where the right |
| 5874 |
-- operand is positive or negative. Otherwise, the right operand |
| 5875 |
-- can be arbitrarily close to zero, so the result is likely to |
| 5876 |
-- be unbounded in one direction, do not attempt to compute it. |
| 5877 |
|
| 5878 |
when N_Op_Divide => |
| 5879 |
if OK_Operands then |
| 5880 |
|
| 5881 |
-- Right operand is positive |
| 5882 |
|
| 5883 |
if Lo_Right > Ureal_0 then |
| 5884 |
|
| 5885 |
-- If the low bound of the left operand is negative, obtain |
| 5886 |
-- the overall low bound by dividing it by the smallest |
| 5887 |
-- value of the right operand, and otherwise by the largest |
| 5888 |
-- value of the right operand. |
| 5889 |
|
| 5890 |
if Lo_Left < Ureal_0 then |
| 5891 |
Lor := Round_Machine (Lo_Left / Lo_Right); |
| 5892 |
else |
| 5893 |
Lor := Round_Machine (Lo_Left / Hi_Right); |
| 5894 |
end if; |
| 5895 |
|
| 5896 |
-- If the high bound of the left operand is negative, obtain |
| 5897 |
-- the overall high bound by dividing it by the largest |
| 5898 |
-- value of the right operand, and otherwise by the |
| 5899 |
-- smallest value of the right operand. |
| 5900 |
|
| 5901 |
if Hi_Left < Ureal_0 then |
| 5902 |
Hir := Round_Machine (Hi_Left / Hi_Right); |
| 5903 |
else |
| 5904 |
Hir := Round_Machine (Hi_Left / Lo_Right); |
| 5905 |
end if; |
| 5906 |
|
| 5907 |
-- Right operand is negative |
| 5908 |
|
| 5909 |
elsif Hi_Right < Ureal_0 then |
| 5910 |
|
| 5911 |
-- If the low bound of the left operand is negative, obtain |
| 5912 |
-- the overall low bound by dividing it by the largest |
| 5913 |
-- value of the right operand, and otherwise by the smallest |
| 5914 |
-- value of the right operand. |
| 5915 |
|
| 5916 |
if Lo_Left < Ureal_0 then |
| 5917 |
Lor := Round_Machine (Lo_Left / Hi_Right); |
| 5918 |
else |
| 5919 |
Lor := Round_Machine (Lo_Left / Lo_Right); |
| 5920 |
end if; |
| 5921 |
|
| 5922 |
-- If the high bound of the left operand is negative, obtain |
| 5923 |
-- the overall high bound by dividing it by the smallest |
| 5924 |
-- value of the right operand, and otherwise by the |
| 5925 |
-- largest value of the right operand. |
| 5926 |
|
| 5927 |
if Hi_Left < Ureal_0 then |
| 5928 |
Hir := Round_Machine (Hi_Left / Lo_Right); |
| 5929 |
else |
| 5930 |
Hir := Round_Machine (Hi_Left / Hi_Right); |
| 5931 |
end if; |
| 5932 |
|
| 5933 |
else |
| 5934 |
OK1 := False; |
| 5935 |
end if; |
| 5936 |
end if; |
| 5937 |
|
| 5938 |
when N_Type_Conversion => |
| 5939 |
|
| 5940 |
-- For type conversion from one floating-point type to another, we |
| 5941 |
-- can refine the range using the converted value. |
| 5942 |
|
| 5943 |
if Is_Floating_Point_Type (Etype (Expression (N))) then |
| 5944 |
Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid); |
| 5945 |
|
| 5946 |
-- When converting an integer to a floating-point type, determine |
| 5947 |
-- the range in integer first, and then convert the bounds. |
| 5948 |
|
| 5949 |
elsif Is_Discrete_Type (Etype (Expression (N))) then |
| 5950 |
declare |
| 5951 |
Hir_Int : Uint; |
| 5952 |
Lor_Int : Uint; |
| 5953 |
|
| 5954 |
begin |
| 5955 |
Determine_Range |
| 5956 |
(Expression (N), OK1, Lor_Int, Hir_Int, Assume_Valid); |
| 5957 |
|
| 5958 |
if OK1 then |
| 5959 |
Lor := Round_Machine (UR_From_Uint (Lor_Int)); |
| 5960 |
Hir := Round_Machine (UR_From_Uint (Hir_Int)); |
| 5961 |
end if; |
| 5962 |
end; |
| 5963 |
|
| 5964 |
else |
| 5965 |
OK1 := False; |
| 5966 |
end if; |
| 5967 |
|
| 5968 |
-- Nothing special to do for all other expression kinds |
| 5969 |
|
| 5970 |
when others => |
| 5971 |
OK1 := False; |
| 5972 |
Lor := No_Ureal; |
| 5973 |
Hir := No_Ureal; |
| 5974 |
end case; |
| 5975 |
|
| 5976 |
-- At this stage, if OK1 is true, then we know that the actual result of |
| 5977 |
-- the computed expression is in the range Lor .. Hir. We can use this |
| 5978 |
-- to restrict the possible range of results. |
| 5979 |
|
| 5980 |
if OK1 then |
| 5981 |
|
| 5982 |
-- If the refined value of the low bound is greater than the type |
| 5983 |
-- low bound, then reset it to the more restrictive value. |
| 5984 |
|
| 5985 |
if Lor > Lo then |
| 5986 |
Lo := Lor; |
| 5987 |
end if; |
| 5988 |
|
| 5989 |
-- Similarly, if the refined value of the high bound is less than the |
| 5990 |
-- value so far, then reset it to the more restrictive value. |
| 5991 |
|
| 5992 |
if Hir < Hi then |
| 5993 |
Hi := Hir; |
| 5994 |
end if; |
| 5995 |
end if; |
| 5996 |
|
| 5997 |
-- Set cache entry for future call and we are all done |
| 5998 |
|
| 5999 |
Determine_Range_Cache_N (Cindex) := N; |
| 6000 |
Determine_Range_Cache_O (Cindex) := Original_Node (N); |
| 6001 |
Determine_Range_Cache_V (Cindex) := Assume_Valid; |
| 6002 |
Determine_Range_Cache_Lo_R (Cindex) := Lo; |
| 6003 |
Determine_Range_Cache_Hi_R (Cindex) := Hi; |
| 6004 |
return; |
| 6005 |
|
| 6006 |
-- If any exception occurs, it means that we have some bug in the compiler, |
| 6007 |
-- possibly triggered by a previous error, or by some unforeseen peculiar |
| 6008 |
-- occurrence. However, this is only an optimization attempt, so there is |
| 6009 |
-- really no point in crashing the compiler. Instead we just decide, too |
| 6010 |
-- bad, we can't figure out a range in this case after all. |
| 6011 |
|
| 6012 |
exception |
| 6013 |
when others => |
| 6014 |
|
| 6015 |
-- Debug flag K disables this behavior (useful for debugging) |
| 6016 |
|
| 6017 |
if Debug_Flag_K then |
| 6018 |
raise; |
| 6019 |
else |
| 6020 |
OK := False; |
| 6021 |
Lo := No_Ureal; |
| 6022 |
Hi := No_Ureal; |
| 6023 |
return; |
| 6024 |
end if; |
| 6025 |
end Determine_Range_R; |
| 6026 |
|
| 6027 |
--------------------------------- |
| 6028 |
-- Determine_Range_To_Discrete -- |
| 6029 |
--------------------------------- |
| 6030 |
|
| 6031 |
procedure Determine_Range_To_Discrete |
| 6032 |
(N : Node_Id; |
| 6033 |
OK : out Boolean; |
| 6034 |
Lo : out Uint; |
| 6035 |
Hi : out Uint; |
| 6036 |
Fixed_Int : Boolean := False; |
| 6037 |
Assume_Valid : Boolean := False) |
| 6038 |
is |
| 6039 |
Typ : constant Entity_Id := Etype (N); |
| 6040 |
|
| 6041 |
begin |
| 6042 |
-- For a discrete type, simply defer to Determine_Range |
| 6043 |
|
| 6044 |
if Is_Discrete_Type (Typ) then |
| 6045 |
Determine_Range (N, OK, Lo, Hi, Assume_Valid); |
| 6046 |
|
| 6047 |
-- For a fixed point type treated as an integer, we can determine the |
| 6048 |
-- range using the Corresponding_Integer_Value of the bounds of the |
| 6049 |
-- type or base type. This is done by the calls to Expr_Value below. |
| 6050 |
|
| 6051 |
elsif Is_Fixed_Point_Type (Typ) and then Fixed_Int then |
| 6052 |
declare |
| 6053 |
Btyp, Ftyp : Entity_Id; |
| 6054 |
Bound : Node_Id; |
| 6055 |
|
| 6056 |
begin |
| 6057 |
if Assume_Valid then |
| 6058 |
Ftyp := Typ; |
| 6059 |
else |
| 6060 |
Ftyp := Underlying_Type (Base_Type (Typ)); |
| 6061 |
end if; |
| 6062 |
|
| 6063 |
Btyp := Base_Type (Ftyp); |
| 6064 |
|
| 6065 |
-- First the low bound |
| 6066 |
|
| 6067 |
Bound := Type_Low_Bound (Ftyp); |
| 6068 |
|
| 6069 |
if Compile_Time_Known_Value (Bound) then |
| 6070 |
Lo := Expr_Value (Bound); |
| 6071 |
else |
| 6072 |
Lo := Expr_Value (Type_Low_Bound (Btyp)); |
| 6073 |
end if; |
| 6074 |
|
| 6075 |
-- Then the high bound |
| 6076 |
|
| 6077 |
Bound := Type_High_Bound (Ftyp); |
| 6078 |
|
| 6079 |
if Compile_Time_Known_Value (Bound) then |
| 6080 |
Hi := Expr_Value (Bound); |
| 6081 |
else |
| 6082 |
Hi := Expr_Value (Type_High_Bound (Btyp)); |
| 6083 |
end if; |
| 6084 |
|
| 6085 |
OK := True; |
| 6086 |
end; |
| 6087 |
|
| 6088 |
-- For a floating-point type, we can determine the range in real first, |
| 6089 |
-- and then convert the bounds using UR_To_Uint, which correctly rounds |
| 6090 |
-- away from zero when half way between two integers, as required by |
| 6091 |
-- normal Ada 95 rounding semantics. But this is only possible because |
| 6092 |
-- GNATprove's analysis rules out the possibility of a NaN or infinite. |
| 6093 |
|
| 6094 |
elsif GNATprove_Mode and then Is_Floating_Point_Type (Typ) then |
| 6095 |
declare |
| 6096 |
Lo_Real, Hi_Real : Ureal; |
| 6097 |
|
| 6098 |
begin |
| 6099 |
Determine_Range_R (N, OK, Lo_Real, Hi_Real, Assume_Valid); |
| 6100 |
|
| 6101 |
if OK then |
| 6102 |
Lo := UR_To_Uint (Lo_Real); |
| 6103 |
Hi := UR_To_Uint (Hi_Real); |
| 6104 |
else |
| 6105 |
Lo := No_Uint; |
| 6106 |
Hi := No_Uint; |
| 6107 |
end if; |
| 6108 |
end; |
| 6109 |
|
| 6110 |
else |
| 6111 |
Lo := No_Uint; |
| 6112 |
Hi := No_Uint; |
| 6113 |
OK := False; |
| 6114 |
end if; |
| 6115 |
end Determine_Range_To_Discrete; |
| 6116 |
|
| 6117 |
------------------------------------ |
| 6118 |
-- Discriminant_Checks_Suppressed -- |
| 6119 |
------------------------------------ |
| 6120 |
|
| 6121 |
function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 6122 |
begin |
| 6123 |
if Present (E) then |
| 6124 |
if Is_Unchecked_Union (E) then |
| 6125 |
return True; |
| 6126 |
elsif Checks_May_Be_Suppressed (E) then |
| 6127 |
return Is_Check_Suppressed (E, Discriminant_Check); |
| 6128 |
end if; |
| 6129 |
end if; |
| 6130 |
|
| 6131 |
return Scope_Suppress.Suppress (Discriminant_Check); |
| 6132 |
end Discriminant_Checks_Suppressed; |
| 6133 |
|
| 6134 |
-------------------------------- |
| 6135 |
-- Division_Checks_Suppressed -- |
| 6136 |
-------------------------------- |
| 6137 |
|
| 6138 |
function Division_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 6139 |
begin |
| 6140 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 6141 |
return Is_Check_Suppressed (E, Division_Check); |
| 6142 |
else |
| 6143 |
return Scope_Suppress.Suppress (Division_Check); |
| 6144 |
end if; |
| 6145 |
end Division_Checks_Suppressed; |
| 6146 |
|
| 6147 |
-------------------------------------- |
| 6148 |
-- Duplicated_Tag_Checks_Suppressed -- |
| 6149 |
-------------------------------------- |
| 6150 |
|
| 6151 |
function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 6152 |
begin |
| 6153 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 6154 |
return Is_Check_Suppressed (E, Duplicated_Tag_Check); |
| 6155 |
else |
| 6156 |
return Scope_Suppress.Suppress (Duplicated_Tag_Check); |
| 6157 |
end if; |
| 6158 |
end Duplicated_Tag_Checks_Suppressed; |
| 6159 |
|
| 6160 |
----------------------------------- |
| 6161 |
-- Elaboration_Checks_Suppressed -- |
| 6162 |
----------------------------------- |
| 6163 |
|
| 6164 |
function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 6165 |
begin |
| 6166 |
-- The complication in this routine is that if we are in the dynamic |
| 6167 |
-- model of elaboration, we also check All_Checks, since All_Checks |
| 6168 |
-- does not set Elaboration_Check explicitly. |
| 6169 |
|
| 6170 |
if Present (E) then |
| 6171 |
if Kill_Elaboration_Checks (E) then |
| 6172 |
return True; |
| 6173 |
|
| 6174 |
elsif Checks_May_Be_Suppressed (E) then |
| 6175 |
if Is_Check_Suppressed (E, Elaboration_Check) then |
| 6176 |
return True; |
| 6177 |
|
| 6178 |
elsif Dynamic_Elaboration_Checks then |
| 6179 |
return Is_Check_Suppressed (E, All_Checks); |
| 6180 |
|
| 6181 |
else |
| 6182 |
return False; |
| 6183 |
end if; |
| 6184 |
end if; |
| 6185 |
end if; |
| 6186 |
|
| 6187 |
if Scope_Suppress.Suppress (Elaboration_Check) then |
| 6188 |
return True; |
| 6189 |
|
| 6190 |
elsif Dynamic_Elaboration_Checks then |
| 6191 |
return Scope_Suppress.Suppress (All_Checks); |
| 6192 |
|
| 6193 |
else |
| 6194 |
return False; |
| 6195 |
end if; |
| 6196 |
end Elaboration_Checks_Suppressed; |
| 6197 |
|
| 6198 |
--------------------------- |
| 6199 |
-- Enable_Overflow_Check -- |
| 6200 |
--------------------------- |
| 6201 |
|
| 6202 |
procedure Enable_Overflow_Check (N : Node_Id) is |
| 6203 |
Typ : constant Entity_Id := Base_Type (Etype (N)); |
| 6204 |
Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
| 6205 |
Chk : Nat; |
| 6206 |
OK : Boolean; |
| 6207 |
Ent : Entity_Id; |
| 6208 |
Ofs : Uint; |
| 6209 |
Lo : Uint; |
| 6210 |
Hi : Uint; |
| 6211 |
|
| 6212 |
Do_Ovflow_Check : Boolean; |
| 6213 |
|
| 6214 |
begin |
| 6215 |
if Debug_Flag_CC then |
| 6216 |
w ("Enable_Overflow_Check for node ", Int (N)); |
| 6217 |
Write_Str (" Source location = "); |
| 6218 |
wl (Sloc (N)); |
| 6219 |
pg (Union_Id (N)); |
| 6220 |
end if; |
| 6221 |
|
| 6222 |
-- No check if overflow checks suppressed for type of node |
| 6223 |
|
| 6224 |
if Overflow_Checks_Suppressed (Etype (N)) then |
| 6225 |
return; |
| 6226 |
|
| 6227 |
-- Nothing to do for unsigned integer types, which do not overflow |
| 6228 |
|
| 6229 |
elsif Is_Modular_Integer_Type (Typ) then |
| 6230 |
return; |
| 6231 |
end if; |
| 6232 |
|
| 6233 |
-- This is the point at which processing for STRICT mode diverges |
| 6234 |
-- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
| 6235 |
-- probably more extreme that it needs to be, but what is going on here |
| 6236 |
-- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted |
| 6237 |
-- to leave the processing for STRICT mode untouched. There were |
| 6238 |
-- two reasons for this. First it avoided any incompatible change of |
| 6239 |
-- behavior. Second, it guaranteed that STRICT mode continued to be |
| 6240 |
-- legacy reliable. |
| 6241 |
|
| 6242 |
-- The big difference is that in STRICT mode there is a fair amount of |
| 6243 |
-- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
| 6244 |
-- know that no check is needed. We skip all that in the two new modes, |
| 6245 |
-- since really overflow checking happens over a whole subtree, and we |
| 6246 |
-- do the corresponding optimizations later on when applying the checks. |
| 6247 |
|
| 6248 |
if Mode in Minimized_Or_Eliminated then |
| 6249 |
if not Overflow_Checks_Suppressed (Etype (N)) |
| 6250 |
and then not (Is_Entity_Name (N) |
| 6251 |
and then Overflow_Checks_Suppressed (Entity (N))) |
| 6252 |
then |
| 6253 |
Activate_Overflow_Check (N); |
| 6254 |
end if; |
| 6255 |
|
| 6256 |
if Debug_Flag_CC then |
| 6257 |
w ("Minimized/Eliminated mode"); |
| 6258 |
end if; |
| 6259 |
|
| 6260 |
return; |
| 6261 |
end if; |
| 6262 |
|
| 6263 |
-- Remainder of processing is for STRICT case, and is unchanged from |
| 6264 |
-- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
| 6265 |
|
| 6266 |
-- Nothing to do if the range of the result is known OK. We skip this |
| 6267 |
-- for conversions, since the caller already did the check, and in any |
| 6268 |
-- case the condition for deleting the check for a type conversion is |
| 6269 |
-- different. |
| 6270 |
|
| 6271 |
if Nkind (N) /= N_Type_Conversion then |
| 6272 |
Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
| 6273 |
|
| 6274 |
-- Note in the test below that we assume that the range is not OK |
| 6275 |
-- if a bound of the range is equal to that of the type. That's not |
| 6276 |
-- quite accurate but we do this for the following reasons: |
| 6277 |
|
| 6278 |
-- a) The way that Determine_Range works, it will typically report |
| 6279 |
-- the bounds of the value as being equal to the bounds of the |
| 6280 |
-- type, because it either can't tell anything more precise, or |
| 6281 |
-- does not think it is worth the effort to be more precise. |
| 6282 |
|
| 6283 |
-- b) It is very unusual to have a situation in which this would |
| 6284 |
-- generate an unnecessary overflow check (an example would be |
| 6285 |
-- a subtype with a range 0 .. Integer'Last - 1 to which the |
| 6286 |
-- literal value one is added). |
| 6287 |
|
| 6288 |
-- c) The alternative is a lot of special casing in this routine |
| 6289 |
-- which would partially duplicate Determine_Range processing. |
| 6290 |
|
| 6291 |
if OK then |
| 6292 |
Do_Ovflow_Check := True; |
| 6293 |
|
| 6294 |
-- Note that the following checks are quite deliberately > and < |
| 6295 |
-- rather than >= and <= as explained above. |
| 6296 |
|
| 6297 |
if Lo > Expr_Value (Type_Low_Bound (Typ)) |
| 6298 |
and then |
| 6299 |
Hi < Expr_Value (Type_High_Bound (Typ)) |
| 6300 |
then |
| 6301 |
Do_Ovflow_Check := False; |
| 6302 |
|
| 6303 |
-- Despite the comments above, it is worth dealing specially with |
| 6304 |
-- division. The only case where integer division can overflow is |
| 6305 |
-- (largest negative number) / (-1). So we will do an extra range |
| 6306 |
-- analysis to see if this is possible. |
| 6307 |
|
| 6308 |
elsif Nkind (N) = N_Op_Divide then |
| 6309 |
Determine_Range |
| 6310 |
(Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); |
| 6311 |
|
| 6312 |
if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then |
| 6313 |
Do_Ovflow_Check := False; |
| 6314 |
|
| 6315 |
else |
| 6316 |
Determine_Range |
| 6317 |
(Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); |
| 6318 |
|
| 6319 |
if OK and then (Lo > Uint_Minus_1 |
| 6320 |
or else |
| 6321 |
Hi < Uint_Minus_1) |
| 6322 |
then |
| 6323 |
Do_Ovflow_Check := False; |
| 6324 |
end if; |
| 6325 |
end if; |
| 6326 |
|
| 6327 |
-- Likewise for Abs/Minus, the only case where the operation can |
| 6328 |
-- overflow is when the operand is the largest negative number. |
| 6329 |
|
| 6330 |
elsif Nkind (N) in N_Op_Abs | N_Op_Minus then |
| 6331 |
Determine_Range |
| 6332 |
(Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); |
| 6333 |
|
| 6334 |
if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then |
| 6335 |
Do_Ovflow_Check := False; |
| 6336 |
end if; |
| 6337 |
end if; |
| 6338 |
|
| 6339 |
-- If no overflow check required, we are done |
| 6340 |
|
| 6341 |
if not Do_Ovflow_Check then |
| 6342 |
if Debug_Flag_CC then |
| 6343 |
w ("No overflow check required"); |
| 6344 |
end if; |
| 6345 |
|
| 6346 |
return; |
| 6347 |
end if; |
| 6348 |
end if; |
| 6349 |
end if; |
| 6350 |
|
| 6351 |
-- If not in optimizing mode, set flag and we are done. We are also done |
| 6352 |
-- (and just set the flag) if the type is not a discrete type, since it |
| 6353 |
-- is not worth the effort to eliminate checks for other than discrete |
| 6354 |
-- types. In addition, we take this same path if we have stored the |
| 6355 |
-- maximum number of checks possible already (a very unlikely situation, |
| 6356 |
-- but we do not want to blow up). |
| 6357 |
|
| 6358 |
if Optimization_Level = 0 |
| 6359 |
or else not Is_Discrete_Type (Etype (N)) |
| 6360 |
or else Num_Saved_Checks = Saved_Checks'Last |
| 6361 |
then |
| 6362 |
Activate_Overflow_Check (N); |
| 6363 |
|
| 6364 |
if Debug_Flag_CC then |
| 6365 |
w ("Optimization off"); |
| 6366 |
end if; |
| 6367 |
|
| 6368 |
return; |
| 6369 |
end if; |
| 6370 |
|
| 6371 |
-- Otherwise evaluate and check the expression |
| 6372 |
|
| 6373 |
Find_Check |
| 6374 |
(Expr => N, |
| 6375 |
Check_Type => 'O', |
| 6376 |
Target_Type => Empty, |
| 6377 |
Entry_OK => OK, |
| 6378 |
Check_Num => Chk, |
| 6379 |
Ent => Ent, |
| 6380 |
Ofs => Ofs); |
| 6381 |
|
| 6382 |
if Debug_Flag_CC then |
| 6383 |
w ("Called Find_Check"); |
| 6384 |
w (" OK = ", OK); |
| 6385 |
|
| 6386 |
if OK then |
| 6387 |
w (" Check_Num = ", Chk); |
| 6388 |
w (" Ent = ", Int (Ent)); |
| 6389 |
Write_Str (" Ofs = "); |
| 6390 |
pid (Ofs); |
| 6391 |
end if; |
| 6392 |
end if; |
| 6393 |
|
| 6394 |
-- If check is not of form to optimize, then set flag and we are done |
| 6395 |
|
| 6396 |
if not OK then |
| 6397 |
Activate_Overflow_Check (N); |
| 6398 |
return; |
| 6399 |
end if; |
| 6400 |
|
| 6401 |
-- If check is already performed, then return without setting flag |
| 6402 |
|
| 6403 |
if Chk /= 0 then |
| 6404 |
if Debug_Flag_CC then |
| 6405 |
w ("Check suppressed!"); |
| 6406 |
end if; |
| 6407 |
|
| 6408 |
return; |
| 6409 |
end if; |
| 6410 |
|
| 6411 |
-- Here we will make a new entry for the new check |
| 6412 |
|
| 6413 |
Activate_Overflow_Check (N); |
| 6414 |
Num_Saved_Checks := Num_Saved_Checks + 1; |
| 6415 |
Saved_Checks (Num_Saved_Checks) := |
| 6416 |
(Killed => False, |
| 6417 |
Entity => Ent, |
| 6418 |
Offset => Ofs, |
| 6419 |
Check_Type => 'O', |
| 6420 |
Target_Type => Empty); |
| 6421 |
|
| 6422 |
if Debug_Flag_CC then |
| 6423 |
w ("Make new entry, check number = ", Num_Saved_Checks); |
| 6424 |
w (" Entity = ", Int (Ent)); |
| 6425 |
Write_Str (" Offset = "); |
| 6426 |
pid (Ofs); |
| 6427 |
w (" Check_Type = O"); |
| 6428 |
w (" Target_Type = Empty"); |
| 6429 |
end if; |
| 6430 |
|
| 6431 |
-- If we get an exception, then something went wrong, probably because of |
| 6432 |
-- an error in the structure of the tree due to an incorrect program. Or |
| 6433 |
-- it may be a bug in the optimization circuit. In either case the safest |
| 6434 |
-- thing is simply to set the check flag unconditionally. |
| 6435 |
|
| 6436 |
exception |
| 6437 |
when others => |
| 6438 |
Activate_Overflow_Check (N); |
| 6439 |
|
| 6440 |
if Debug_Flag_CC then |
| 6441 |
w (" exception occurred, overflow flag set"); |
| 6442 |
end if; |
| 6443 |
|
| 6444 |
return; |
| 6445 |
end Enable_Overflow_Check; |
| 6446 |
|
| 6447 |
------------------------ |
| 6448 |
-- Enable_Range_Check -- |
| 6449 |
------------------------ |
| 6450 |
|
| 6451 |
procedure Enable_Range_Check (N : Node_Id) is |
| 6452 |
Chk : Nat; |
| 6453 |
OK : Boolean; |
| 6454 |
Ent : Entity_Id; |
| 6455 |
Ofs : Uint; |
| 6456 |
Ttyp : Entity_Id; |
| 6457 |
P : Node_Id; |
| 6458 |
|
| 6459 |
begin |
| 6460 |
-- Return if unchecked type conversion with range check killed. In this |
| 6461 |
-- case we never set the flag (that's what Kill_Range_Check is about). |
| 6462 |
|
| 6463 |
if Nkind (N) = N_Unchecked_Type_Conversion |
| 6464 |
and then Kill_Range_Check (N) |
| 6465 |
then |
| 6466 |
return; |
| 6467 |
end if; |
| 6468 |
|
| 6469 |
-- Do not set range check flag if parent is assignment statement or |
| 6470 |
-- object declaration with Suppress_Assignment_Checks flag set. |
| 6471 |
|
| 6472 |
if Nkind (Parent (N)) in N_Assignment_Statement | N_Object_Declaration |
| 6473 |
and then Suppress_Assignment_Checks (Parent (N)) |
| 6474 |
then |
| 6475 |
return; |
| 6476 |
end if; |
| 6477 |
|
| 6478 |
-- Check for various cases where we should suppress the range check |
| 6479 |
|
| 6480 |
-- No check if range checks suppressed for type of node |
| 6481 |
|
| 6482 |
if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
| 6483 |
return; |
| 6484 |
|
| 6485 |
-- No check if node is an entity name, and range checks are suppressed |
| 6486 |
-- for this entity, or for the type of this entity. |
| 6487 |
|
| 6488 |
elsif Is_Entity_Name (N) |
| 6489 |
and then (Range_Checks_Suppressed (Entity (N)) |
| 6490 |
or else Range_Checks_Suppressed (Etype (Entity (N)))) |
| 6491 |
then |
| 6492 |
return; |
| 6493 |
|
| 6494 |
-- No checks if index of array, and index checks are suppressed for |
| 6495 |
-- the array object or the type of the array. |
| 6496 |
|
| 6497 |
elsif Nkind (Parent (N)) = N_Indexed_Component then |
| 6498 |
declare |
| 6499 |
Pref : constant Node_Id := Prefix (Parent (N)); |
| 6500 |
begin |
| 6501 |
if Is_Entity_Name (Pref) |
| 6502 |
and then Index_Checks_Suppressed (Entity (Pref)) |
| 6503 |
then |
| 6504 |
return; |
| 6505 |
elsif Index_Checks_Suppressed (Etype (Pref)) then |
| 6506 |
return; |
| 6507 |
end if; |
| 6508 |
end; |
| 6509 |
end if; |
| 6510 |
|
| 6511 |
-- Debug trace output |
| 6512 |
|
| 6513 |
if Debug_Flag_CC then |
| 6514 |
w ("Enable_Range_Check for node ", Int (N)); |
| 6515 |
Write_Str (" Source location = "); |
| 6516 |
wl (Sloc (N)); |
| 6517 |
pg (Union_Id (N)); |
| 6518 |
end if; |
| 6519 |
|
| 6520 |
-- If not in optimizing mode, set flag and we are done. We are also done |
| 6521 |
-- (and just set the flag) if the type is not a discrete type, since it |
| 6522 |
-- is not worth the effort to eliminate checks for other than discrete |
| 6523 |
-- types. In addition, we take this same path if we have stored the |
| 6524 |
-- maximum number of checks possible already (a very unlikely situation, |
| 6525 |
-- but we do not want to blow up). |
| 6526 |
|
| 6527 |
if Optimization_Level = 0 |
| 6528 |
or else No (Etype (N)) |
| 6529 |
or else not Is_Discrete_Type (Etype (N)) |
| 6530 |
or else Num_Saved_Checks = Saved_Checks'Last |
| 6531 |
then |
| 6532 |
Activate_Range_Check (N); |
| 6533 |
|
| 6534 |
if Debug_Flag_CC then |
| 6535 |
w ("Optimization off"); |
| 6536 |
end if; |
| 6537 |
|
| 6538 |
return; |
| 6539 |
end if; |
| 6540 |
|
| 6541 |
-- Otherwise find out the target type |
| 6542 |
|
| 6543 |
P := Parent (N); |
| 6544 |
|
| 6545 |
-- For assignment, use left side subtype |
| 6546 |
|
| 6547 |
if Nkind (P) = N_Assignment_Statement |
| 6548 |
and then Expression (P) = N |
| 6549 |
then |
| 6550 |
Ttyp := Etype (Name (P)); |
| 6551 |
|
| 6552 |
-- For indexed component, use subscript subtype |
| 6553 |
|
| 6554 |
elsif Nkind (P) = N_Indexed_Component then |
| 6555 |
declare |
| 6556 |
Atyp : Entity_Id; |
| 6557 |
Indx : Node_Id; |
| 6558 |
Subs : Node_Id; |
| 6559 |
|
| 6560 |
begin |
| 6561 |
Atyp := Etype (Prefix (P)); |
| 6562 |
|
| 6563 |
if Is_Access_Type (Atyp) then |
| 6564 |
Atyp := Designated_Type (Atyp); |
| 6565 |
|
| 6566 |
-- If the prefix is an access to an unconstrained array, |
| 6567 |
-- perform check unconditionally: it depends on the bounds of |
| 6568 |
-- an object and we cannot currently recognize whether the test |
| 6569 |
-- may be redundant. |
| 6570 |
|
| 6571 |
if not Is_Constrained (Atyp) then |
| 6572 |
Activate_Range_Check (N); |
| 6573 |
return; |
| 6574 |
end if; |
| 6575 |
|
| 6576 |
-- Ditto if prefix is simply an unconstrained array. We used |
| 6577 |
-- to think this case was OK, if the prefix was not an explicit |
| 6578 |
-- dereference, but we have now seen a case where this is not |
| 6579 |
-- true, so it is safer to just suppress the optimization in this |
| 6580 |
-- case. The back end is getting better at eliminating redundant |
| 6581 |
-- checks in any case, so the loss won't be important. |
| 6582 |
|
| 6583 |
elsif Is_Array_Type (Atyp) |
| 6584 |
and then not Is_Constrained (Atyp) |
| 6585 |
then |
| 6586 |
Activate_Range_Check (N); |
| 6587 |
return; |
| 6588 |
end if; |
| 6589 |
|
| 6590 |
Indx := First_Index (Atyp); |
| 6591 |
Subs := First (Expressions (P)); |
| 6592 |
loop |
| 6593 |
if Subs = N then |
| 6594 |
Ttyp := Etype (Indx); |
| 6595 |
exit; |
| 6596 |
end if; |
| 6597 |
|
| 6598 |
Next_Index (Indx); |
| 6599 |
Next (Subs); |
| 6600 |
end loop; |
| 6601 |
end; |
| 6602 |
|
| 6603 |
-- For now, ignore all other cases, they are not so interesting |
| 6604 |
|
| 6605 |
else |
| 6606 |
if Debug_Flag_CC then |
| 6607 |
w (" target type not found, flag set"); |
| 6608 |
end if; |
| 6609 |
|
| 6610 |
Activate_Range_Check (N); |
| 6611 |
return; |
| 6612 |
end if; |
| 6613 |
|
| 6614 |
-- Evaluate and check the expression |
| 6615 |
|
| 6616 |
Find_Check |
| 6617 |
(Expr => N, |
| 6618 |
Check_Type => 'R', |
| 6619 |
Target_Type => Ttyp, |
| 6620 |
Entry_OK => OK, |
| 6621 |
Check_Num => Chk, |
| 6622 |
Ent => Ent, |
| 6623 |
Ofs => Ofs); |
| 6624 |
|
| 6625 |
if Debug_Flag_CC then |
| 6626 |
w ("Called Find_Check"); |
| 6627 |
w ("Target_Typ = ", Int (Ttyp)); |
| 6628 |
w (" OK = ", OK); |
| 6629 |
|
| 6630 |
if OK then |
| 6631 |
w (" Check_Num = ", Chk); |
| 6632 |
w (" Ent = ", Int (Ent)); |
| 6633 |
Write_Str (" Ofs = "); |
| 6634 |
pid (Ofs); |
| 6635 |
end if; |
| 6636 |
end if; |
| 6637 |
|
| 6638 |
-- If check is not of form to optimize, then set flag and we are done |
| 6639 |
|
| 6640 |
if not OK then |
| 6641 |
if Debug_Flag_CC then |
| 6642 |
w (" expression not of optimizable type, flag set"); |
| 6643 |
end if; |
| 6644 |
|
| 6645 |
Activate_Range_Check (N); |
| 6646 |
return; |
| 6647 |
end if; |
| 6648 |
|
| 6649 |
-- If check is already performed, then return without setting flag |
| 6650 |
|
| 6651 |
if Chk /= 0 then |
| 6652 |
if Debug_Flag_CC then |
| 6653 |
w ("Check suppressed!"); |
| 6654 |
end if; |
| 6655 |
|
| 6656 |
return; |
| 6657 |
end if; |
| 6658 |
|
| 6659 |
-- Here we will make a new entry for the new check |
| 6660 |
|
| 6661 |
Activate_Range_Check (N); |
| 6662 |
Num_Saved_Checks := Num_Saved_Checks + 1; |
| 6663 |
Saved_Checks (Num_Saved_Checks) := |
| 6664 |
(Killed => False, |
| 6665 |
Entity => Ent, |
| 6666 |
Offset => Ofs, |
| 6667 |
Check_Type => 'R', |
| 6668 |
Target_Type => Ttyp); |
| 6669 |
|
| 6670 |
if Debug_Flag_CC then |
| 6671 |
w ("Make new entry, check number = ", Num_Saved_Checks); |
| 6672 |
w (" Entity = ", Int (Ent)); |
| 6673 |
Write_Str (" Offset = "); |
| 6674 |
pid (Ofs); |
| 6675 |
w (" Check_Type = R"); |
| 6676 |
w (" Target_Type = ", Int (Ttyp)); |
| 6677 |
pg (Union_Id (Ttyp)); |
| 6678 |
end if; |
| 6679 |
|
| 6680 |
-- If we get an exception, then something went wrong, probably because of |
| 6681 |
-- an error in the structure of the tree due to an incorrect program. Or |
| 6682 |
-- it may be a bug in the optimization circuit. In either case the safest |
| 6683 |
-- thing is simply to set the check flag unconditionally. |
| 6684 |
|
| 6685 |
exception |
| 6686 |
when others => |
| 6687 |
Activate_Range_Check (N); |
| 6688 |
|
| 6689 |
if Debug_Flag_CC then |
| 6690 |
w (" exception occurred, range flag set"); |
| 6691 |
end if; |
| 6692 |
|
| 6693 |
return; |
| 6694 |
end Enable_Range_Check; |
| 6695 |
|
| 6696 |
------------------ |
| 6697 |
-- Ensure_Valid -- |
| 6698 |
------------------ |
| 6699 |
|
| 6700 |
procedure Ensure_Valid |
| 6701 |
(Expr : Node_Id; |
| 6702 |
Holes_OK : Boolean := False; |
| 6703 |
Related_Id : Entity_Id := Empty; |
| 6704 |
Is_Low_Bound : Boolean := False; |
| 6705 |
Is_High_Bound : Boolean := False) |
| 6706 |
is |
| 6707 |
Typ : constant Entity_Id := Etype (Expr); |
| 6708 |
|
| 6709 |
begin |
| 6710 |
-- Ignore call if we are not doing any validity checking |
| 6711 |
|
| 6712 |
if not Validity_Checks_On then |
| 6713 |
return; |
| 6714 |
|
| 6715 |
-- Ignore call if range or validity checks suppressed on entity or type |
| 6716 |
|
| 6717 |
elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
| 6718 |
return; |
| 6719 |
|
| 6720 |
-- No check required if expression is from the expander, we assume the |
| 6721 |
-- expander will generate whatever checks are needed. Note that this is |
| 6722 |
-- not just an optimization, it avoids infinite recursions. |
| 6723 |
|
| 6724 |
-- Unchecked conversions must be checked, unless they are initialized |
| 6725 |
-- scalar values, as in a component assignment in an init proc. |
| 6726 |
|
| 6727 |
-- In addition, we force a check if Force_Validity_Checks is set |
| 6728 |
|
| 6729 |
elsif not Comes_From_Source (Expr) |
| 6730 |
and then not |
| 6731 |
(Nkind (Expr) = N_Identifier |
| 6732 |
and then Present (Renamed_Entity_Or_Object (Entity (Expr))) |
| 6733 |
and then |
| 6734 |
Comes_From_Source (Renamed_Entity_Or_Object (Entity (Expr)))) |
| 6735 |
and then not Force_Validity_Checks |
| 6736 |
and then (Nkind (Expr) /= N_Unchecked_Type_Conversion |
| 6737 |
or else Kill_Range_Check (Expr)) |
| 6738 |
then |
| 6739 |
return; |
| 6740 |
|
| 6741 |
-- No check required if expression is known to have valid value |
| 6742 |
|
| 6743 |
elsif Expr_Known_Valid (Expr) then |
| 6744 |
return; |
| 6745 |
|
| 6746 |
-- No check needed within a generated predicate function. Validity |
| 6747 |
-- of input value will have been checked earlier. |
| 6748 |
|
| 6749 |
elsif Ekind (Current_Scope) = E_Function |
| 6750 |
and then Is_Predicate_Function (Current_Scope) |
| 6751 |
then |
| 6752 |
return; |
| 6753 |
|
| 6754 |
-- Ignore case of enumeration with holes where the flag is set not to |
| 6755 |
-- worry about holes, since no special validity check is needed |
| 6756 |
|
| 6757 |
elsif Is_Enumeration_Type (Typ) |
| 6758 |
and then Has_Non_Standard_Rep (Typ) |
| 6759 |
and then Holes_OK |
| 6760 |
then |
| 6761 |
return; |
| 6762 |
|
| 6763 |
-- No check required on the left-hand side of an assignment |
| 6764 |
|
| 6765 |
elsif Nkind (Parent (Expr)) = N_Assignment_Statement |
| 6766 |
and then Expr = Name (Parent (Expr)) |
| 6767 |
then |
| 6768 |
return; |
| 6769 |
|
| 6770 |
-- No check on a universal real constant. The context will eventually |
| 6771 |
-- convert it to a machine number for some target type, or report an |
| 6772 |
-- illegality. |
| 6773 |
|
| 6774 |
elsif Nkind (Expr) = N_Real_Literal |
| 6775 |
and then Etype (Expr) = Universal_Real |
| 6776 |
then |
| 6777 |
return; |
| 6778 |
|
| 6779 |
-- If the expression denotes a component of a packed boolean array, |
| 6780 |
-- no possible check applies. We ignore the old ACATS chestnuts that |
| 6781 |
-- involve Boolean range True..True. |
| 6782 |
|
| 6783 |
-- Note: validity checks are generated for expressions that yield a |
| 6784 |
-- scalar type, when it is possible to create a value that is outside of |
| 6785 |
-- the type. If this is a one-bit boolean no such value exists. This is |
| 6786 |
-- an optimization, and it also prevents compiler blowing up during the |
| 6787 |
-- elaboration of improperly expanded packed array references. |
| 6788 |
|
| 6789 |
elsif Nkind (Expr) = N_Indexed_Component |
| 6790 |
and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) |
| 6791 |
and then Root_Type (Etype (Expr)) = Standard_Boolean |
| 6792 |
then |
| 6793 |
return; |
| 6794 |
|
| 6795 |
-- For an expression with actions, we want to insert the validity check |
| 6796 |
-- on the final Expression. |
| 6797 |
|
| 6798 |
elsif Nkind (Expr) = N_Expression_With_Actions then |
| 6799 |
Ensure_Valid (Expression (Expr)); |
| 6800 |
return; |
| 6801 |
|
| 6802 |
-- An annoying special case. If this is an out parameter of a scalar |
| 6803 |
-- type, then the value is not going to be accessed, therefore it is |
| 6804 |
-- inappropriate to do any validity check at the call site. Likewise |
| 6805 |
-- if the parameter is passed by reference. |
| 6806 |
|
| 6807 |
else |
| 6808 |
-- Only need to worry about scalar types |
| 6809 |
|
| 6810 |
if Is_Scalar_Type (Typ) then |
| 6811 |
declare |
| 6812 |
Formal : Entity_Id; |
| 6813 |
Call : Node_Id; |
| 6814 |
|
| 6815 |
begin |
| 6816 |
Find_Actual (Expr, Formal, Call); |
| 6817 |
|
| 6818 |
if Present (Formal) |
| 6819 |
and then |
| 6820 |
(Ekind (Formal) = E_Out_Parameter |
| 6821 |
or else Mechanism (Formal) = By_Reference) |
| 6822 |
then |
| 6823 |
return; |
| 6824 |
end if; |
| 6825 |
end; |
| 6826 |
end if; |
| 6827 |
end if; |
| 6828 |
|
| 6829 |
-- If we fall through, a validity check is required |
| 6830 |
|
| 6831 |
Insert_Valid_Check (Expr, Related_Id, Is_Low_Bound, Is_High_Bound); |
| 6832 |
|
| 6833 |
if Is_Entity_Name (Expr) |
| 6834 |
and then Safe_To_Capture_Value (Expr, Entity (Expr)) |
| 6835 |
then |
| 6836 |
Set_Is_Known_Valid (Entity (Expr)); |
| 6837 |
end if; |
| 6838 |
end Ensure_Valid; |
| 6839 |
|
| 6840 |
---------------------- |
| 6841 |
-- Expr_Known_Valid -- |
| 6842 |
---------------------- |
| 6843 |
|
| 6844 |
function Expr_Known_Valid (Expr : Node_Id) return Boolean is |
| 6845 |
Typ : constant Entity_Id := Validated_View (Etype (Expr)); |
| 6846 |
|
| 6847 |
begin |
| 6848 |
-- Non-scalar types are always considered valid, since they never give |
| 6849 |
-- rise to the issues of erroneous or bounded error behavior that are |
| 6850 |
-- the concern. In formal reference manual terms the notion of validity |
| 6851 |
-- only applies to scalar types. Note that even when packed arrays are |
| 6852 |
-- represented using modular types, they are still arrays semantically, |
| 6853 |
-- so they are also always valid (in particular, the unused bits can be |
| 6854 |
-- random rubbish without affecting the validity of the array value). |
| 6855 |
|
| 6856 |
if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then |
| 6857 |
return True; |
| 6858 |
|
| 6859 |
-- If no validity checking, then everything is considered valid |
| 6860 |
|
| 6861 |
elsif not Validity_Checks_On then |
| 6862 |
return True; |
| 6863 |
|
| 6864 |
-- Floating-point types are considered valid unless floating-point |
| 6865 |
-- validity checks have been specifically turned on. |
| 6866 |
|
| 6867 |
elsif Is_Floating_Point_Type (Typ) |
| 6868 |
and then not Validity_Check_Floating_Point |
| 6869 |
then |
| 6870 |
return True; |
| 6871 |
|
| 6872 |
elsif Is_Static_Expression (Expr) then |
| 6873 |
return True; |
| 6874 |
|
| 6875 |
-- If the expression is the value of an object that is known to be |
| 6876 |
-- valid, then clearly the expression value itself is valid. |
| 6877 |
|
| 6878 |
elsif Is_Entity_Name (Expr) |
| 6879 |
and then Is_Known_Valid (Entity (Expr)) |
| 6880 |
|
| 6881 |
-- Exclude volatile variables |
| 6882 |
|
| 6883 |
and then not Treat_As_Volatile (Entity (Expr)) |
| 6884 |
then |
| 6885 |
return True; |
| 6886 |
|
| 6887 |
-- References to discriminants are always considered valid. The value |
| 6888 |
-- of a discriminant gets checked when the object is built. Within the |
| 6889 |
-- record, we consider it valid, and it is important to do so, since |
| 6890 |
-- otherwise we can try to generate bogus validity checks which |
| 6891 |
-- reference discriminants out of scope. Discriminants of concurrent |
| 6892 |
-- types are excluded for the same reason. |
| 6893 |
|
| 6894 |
elsif Is_Entity_Name (Expr) |
| 6895 |
and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
| 6896 |
then |
| 6897 |
return True; |
| 6898 |
|
| 6899 |
-- If the type is one for which all values are known valid, then we are |
| 6900 |
-- sure that the value is valid except in the slightly odd case where |
| 6901 |
-- the expression is a reference to a variable whose size has been |
| 6902 |
-- explicitly set to a value greater than the object size. |
| 6903 |
|
| 6904 |
elsif Is_Known_Valid (Typ) then |
| 6905 |
if Is_Entity_Name (Expr) |
| 6906 |
and then Ekind (Entity (Expr)) = E_Variable |
| 6907 |
and then Known_Esize (Entity (Expr)) |
| 6908 |
and then Esize (Entity (Expr)) > Esize (Typ) |
| 6909 |
then |
| 6910 |
return False; |
| 6911 |
else |
| 6912 |
return True; |
| 6913 |
end if; |
| 6914 |
|
| 6915 |
-- Integer and character literals always have valid values, where |
| 6916 |
-- appropriate these will be range checked in any case. |
| 6917 |
|
| 6918 |
elsif Nkind (Expr) in N_Integer_Literal | N_Character_Literal then |
| 6919 |
return True; |
| 6920 |
|
| 6921 |
-- If we have a type conversion or a qualification of a known valid |
| 6922 |
-- value, then the result will always be valid. |
| 6923 |
|
| 6924 |
elsif Nkind (Expr) in N_Type_Conversion | N_Qualified_Expression then |
| 6925 |
return Expr_Known_Valid (Expression (Expr)); |
| 6926 |
|
| 6927 |
-- Case of expression is a non-floating-point operator. In this case we |
| 6928 |
-- can assume the result is valid the generated code for the operator |
| 6929 |
-- will include whatever checks are needed (e.g. range checks) to ensure |
| 6930 |
-- validity. This assumption does not hold for the floating-point case, |
| 6931 |
-- since floating-point operators can generate Infinite or NaN results |
| 6932 |
-- which are considered invalid. |
| 6933 |
|
| 6934 |
-- Historical note: in older versions, the exemption of floating-point |
| 6935 |
-- types from this assumption was done only in cases where the parent |
| 6936 |
-- was an assignment, function call or parameter association. Presumably |
| 6937 |
-- the idea was that in other contexts, the result would be checked |
| 6938 |
-- elsewhere, but this list of cases was missing tests (at least the |
| 6939 |
-- N_Object_Declaration case, as shown by a reported missing validity |
| 6940 |
-- check), and it is not clear why function calls but not procedure |
| 6941 |
-- calls were tested for. It really seems more accurate and much |
| 6942 |
-- safer to recognize that expressions which are the result of a |
| 6943 |
-- floating-point operator can never be assumed to be valid. |
| 6944 |
|
| 6945 |
elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then |
| 6946 |
return True; |
| 6947 |
|
| 6948 |
-- The result of a membership test is always valid, since it is true or |
| 6949 |
-- false, there are no other possibilities; same for short-circuit |
| 6950 |
-- operators. |
| 6951 |
|
| 6952 |
elsif Nkind (Expr) in N_Membership_Test | N_Short_Circuit then |
| 6953 |
return True; |
| 6954 |
|
| 6955 |
-- For all other cases, we do not know the expression is valid |
| 6956 |
|
| 6957 |
else |
| 6958 |
return False; |
| 6959 |
end if; |
| 6960 |
end Expr_Known_Valid; |
| 6961 |
|
| 6962 |
---------------- |
| 6963 |
-- Find_Check -- |
| 6964 |
---------------- |
| 6965 |
|
| 6966 |
procedure Find_Check |
| 6967 |
(Expr : Node_Id; |
| 6968 |
Check_Type : Character; |
| 6969 |
Target_Type : Entity_Id; |
| 6970 |
Entry_OK : out Boolean; |
| 6971 |
Check_Num : out Nat; |
| 6972 |
Ent : out Entity_Id; |
| 6973 |
Ofs : out Uint) |
| 6974 |
is |
| 6975 |
function Within_Range_Of |
| 6976 |
(Target_Type : Entity_Id; |
| 6977 |
Check_Type : Entity_Id) return Boolean; |
| 6978 |
-- Given a requirement for checking a range against Target_Type, and |
| 6979 |
-- and a range Check_Type against which a check has already been made, |
| 6980 |
-- determines if the check against check type is sufficient to ensure |
| 6981 |
-- that no check against Target_Type is required. |
| 6982 |
|
| 6983 |
--------------------- |
| 6984 |
-- Within_Range_Of -- |
| 6985 |
--------------------- |
| 6986 |
|
| 6987 |
function Within_Range_Of |
| 6988 |
(Target_Type : Entity_Id; |
| 6989 |
Check_Type : Entity_Id) return Boolean |
| 6990 |
is |
| 6991 |
begin |
| 6992 |
if Target_Type = Check_Type then |
| 6993 |
return True; |
| 6994 |
|
| 6995 |
else |
| 6996 |
declare |
| 6997 |
Tlo : constant Node_Id := Type_Low_Bound (Target_Type); |
| 6998 |
Thi : constant Node_Id := Type_High_Bound (Target_Type); |
| 6999 |
Clo : constant Node_Id := Type_Low_Bound (Check_Type); |
| 7000 |
Chi : constant Node_Id := Type_High_Bound (Check_Type); |
| 7001 |
|
| 7002 |
begin |
| 7003 |
if (Tlo = Clo |
| 7004 |
or else (Compile_Time_Known_Value (Tlo) |
| 7005 |
and then |
| 7006 |
Compile_Time_Known_Value (Clo) |
| 7007 |
and then |
| 7008 |
Expr_Value (Clo) >= Expr_Value (Tlo))) |
| 7009 |
and then |
| 7010 |
(Thi = Chi |
| 7011 |
or else (Compile_Time_Known_Value (Thi) |
| 7012 |
and then |
| 7013 |
Compile_Time_Known_Value (Chi) |
| 7014 |
and then |
| 7015 |
Expr_Value (Chi) <= Expr_Value (Clo))) |
| 7016 |
then |
| 7017 |
return True; |
| 7018 |
else |
| 7019 |
return False; |
| 7020 |
end if; |
| 7021 |
end; |
| 7022 |
end if; |
| 7023 |
end Within_Range_Of; |
| 7024 |
|
| 7025 |
-- Start of processing for Find_Check |
| 7026 |
|
| 7027 |
begin |
| 7028 |
-- Establish default, in case no entry is found |
| 7029 |
|
| 7030 |
Check_Num := 0; |
| 7031 |
|
| 7032 |
-- Case of expression is simple entity reference |
| 7033 |
|
| 7034 |
if Is_Entity_Name (Expr) then |
| 7035 |
Ent := Entity (Expr); |
| 7036 |
Ofs := Uint_0; |
| 7037 |
|
| 7038 |
-- Case of expression is entity + known constant |
| 7039 |
|
| 7040 |
elsif Nkind (Expr) = N_Op_Add |
| 7041 |
and then Compile_Time_Known_Value (Right_Opnd (Expr)) |
| 7042 |
and then Is_Entity_Name (Left_Opnd (Expr)) |
| 7043 |
then |
| 7044 |
Ent := Entity (Left_Opnd (Expr)); |
| 7045 |
Ofs := Expr_Value (Right_Opnd (Expr)); |
| 7046 |
|
| 7047 |
-- Case of expression is entity - known constant |
| 7048 |
|
| 7049 |
elsif Nkind (Expr) = N_Op_Subtract |
| 7050 |
and then Compile_Time_Known_Value (Right_Opnd (Expr)) |
| 7051 |
and then Is_Entity_Name (Left_Opnd (Expr)) |
| 7052 |
then |
| 7053 |
Ent := Entity (Left_Opnd (Expr)); |
| 7054 |
Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); |
| 7055 |
|
| 7056 |
-- Any other expression is not of the right form |
| 7057 |
|
| 7058 |
else |
| 7059 |
Ent := Empty; |
| 7060 |
Ofs := Uint_0; |
| 7061 |
Entry_OK := False; |
| 7062 |
return; |
| 7063 |
end if; |
| 7064 |
|
| 7065 |
-- Come here with expression of appropriate form, check if entity is an |
| 7066 |
-- appropriate one for our purposes. |
| 7067 |
|
| 7068 |
if (Ekind (Ent) = E_Variable |
| 7069 |
or else Is_Constant_Object (Ent)) |
| 7070 |
and then not Is_Library_Level_Entity (Ent) |
| 7071 |
then |
| 7072 |
Entry_OK := True; |
| 7073 |
else |
| 7074 |
Entry_OK := False; |
| 7075 |
return; |
| 7076 |
end if; |
| 7077 |
|
| 7078 |
-- See if there is matching check already |
| 7079 |
|
| 7080 |
for J in reverse 1 .. Num_Saved_Checks loop |
| 7081 |
declare |
| 7082 |
SC : Saved_Check renames Saved_Checks (J); |
| 7083 |
begin |
| 7084 |
if SC.Killed = False |
| 7085 |
and then SC.Entity = Ent |
| 7086 |
and then SC.Offset = Ofs |
| 7087 |
and then SC.Check_Type = Check_Type |
| 7088 |
and then Within_Range_Of (Target_Type, SC.Target_Type) |
| 7089 |
then |
| 7090 |
Check_Num := J; |
| 7091 |
return; |
| 7092 |
end if; |
| 7093 |
end; |
| 7094 |
end loop; |
| 7095 |
|
| 7096 |
-- If we fall through entry was not found |
| 7097 |
|
| 7098 |
return; |
| 7099 |
end Find_Check; |
| 7100 |
|
| 7101 |
--------------------------------- |
| 7102 |
-- Generate_Discriminant_Check -- |
| 7103 |
--------------------------------- |
| 7104 |
|
| 7105 |
procedure Generate_Discriminant_Check (N : Node_Id) is |
| 7106 |
Loc : constant Source_Ptr := Sloc (N); |
| 7107 |
Pref : constant Node_Id := Prefix (N); |
| 7108 |
Sel : constant Node_Id := Selector_Name (N); |
| 7109 |
|
| 7110 |
Orig_Comp : constant Entity_Id := |
| 7111 |
Original_Record_Component (Entity (Sel)); |
| 7112 |
-- The original component to be checked |
| 7113 |
|
| 7114 |
Discr_Fct : constant Entity_Id := |
| 7115 |
Discriminant_Checking_Func (Orig_Comp); |
| 7116 |
-- The discriminant checking function |
| 7117 |
|
| 7118 |
Discr : Entity_Id; |
| 7119 |
-- One discriminant to be checked in the type |
| 7120 |
|
| 7121 |
Real_Discr : Entity_Id; |
| 7122 |
-- Actual discriminant in the call |
| 7123 |
|
| 7124 |
Pref_Type : Entity_Id; |
| 7125 |
-- Type of relevant prefix (ignoring private/access stuff) |
| 7126 |
|
| 7127 |
Args : List_Id; |
| 7128 |
-- List of arguments for function call |
| 7129 |
|
| 7130 |
Formal : Entity_Id; |
| 7131 |
-- Keep track of the formal corresponding to the actual we build for |
| 7132 |
-- each discriminant, in order to be able to perform the necessary type |
| 7133 |
-- conversions. |
| 7134 |
|
| 7135 |
Scomp : Node_Id; |
| 7136 |
-- Selected component reference for checking function argument |
| 7137 |
|
| 7138 |
begin |
| 7139 |
Pref_Type := Etype (Pref); |
| 7140 |
|
| 7141 |
-- Force evaluation of the prefix, so that it does not get evaluated |
| 7142 |
-- twice (once for the check, once for the actual reference). Such a |
| 7143 |
-- double evaluation is always a potential source of inefficiency, and |
| 7144 |
-- is functionally incorrect in the volatile case, or when the prefix |
| 7145 |
-- may have side effects. A nonvolatile entity or a component of a |
| 7146 |
-- nonvolatile entity requires no evaluation. |
| 7147 |
|
| 7148 |
if Is_Entity_Name (Pref) then |
| 7149 |
if Treat_As_Volatile (Entity (Pref)) then |
| 7150 |
Force_Evaluation (Pref, Name_Req => True); |
| 7151 |
end if; |
| 7152 |
|
| 7153 |
elsif Treat_As_Volatile (Etype (Pref)) then |
| 7154 |
Force_Evaluation (Pref, Name_Req => True); |
| 7155 |
|
| 7156 |
elsif Nkind (Pref) = N_Selected_Component |
| 7157 |
and then Is_Entity_Name (Prefix (Pref)) |
| 7158 |
then |
| 7159 |
null; |
| 7160 |
|
| 7161 |
else |
| 7162 |
Force_Evaluation (Pref, Name_Req => True); |
| 7163 |
end if; |
| 7164 |
|
| 7165 |
-- For a tagged type, use the scope of the original component to |
| 7166 |
-- obtain the type, because ??? |
| 7167 |
|
| 7168 |
if Is_Tagged_Type (Scope (Orig_Comp)) then |
| 7169 |
Pref_Type := Scope (Orig_Comp); |
| 7170 |
|
| 7171 |
-- For an untagged derived type, use the discriminants of the parent |
| 7172 |
-- which have been renamed in the derivation, possibly by a one-to-many |
| 7173 |
-- discriminant constraint. For untagged type, initially get the Etype |
| 7174 |
-- of the prefix |
| 7175 |
|
| 7176 |
else |
| 7177 |
if Is_Derived_Type (Pref_Type) |
| 7178 |
and then Number_Discriminants (Pref_Type) /= |
| 7179 |
Number_Discriminants (Etype (Base_Type (Pref_Type))) |
| 7180 |
then |
| 7181 |
Pref_Type := Etype (Base_Type (Pref_Type)); |
| 7182 |
end if; |
| 7183 |
end if; |
| 7184 |
|
| 7185 |
-- We definitely should have a checking function, This routine should |
| 7186 |
-- not be called if no discriminant checking function is present. |
| 7187 |
|
| 7188 |
pragma Assert (Present (Discr_Fct)); |
| 7189 |
|
| 7190 |
-- Create the list of the actual parameters for the call. This list |
| 7191 |
-- is the list of the discriminant fields of the record expression to |
| 7192 |
-- be discriminant checked. |
| 7193 |
|
| 7194 |
Args := New_List; |
| 7195 |
Formal := First_Formal (Discr_Fct); |
| 7196 |
Discr := First_Discriminant (Pref_Type); |
| 7197 |
while Present (Discr) loop |
| 7198 |
|
| 7199 |
-- If we have a corresponding discriminant field, and a parent |
| 7200 |
-- subtype is present, then we want to use the corresponding |
| 7201 |
-- discriminant since this is the one with the useful value. |
| 7202 |
|
| 7203 |
if Present (Corresponding_Discriminant (Discr)) |
| 7204 |
and then Ekind (Pref_Type) = E_Record_Type |
| 7205 |
and then Present (Parent_Subtype (Pref_Type)) |
| 7206 |
then |
| 7207 |
Real_Discr := Corresponding_Discriminant (Discr); |
| 7208 |
else |
| 7209 |
Real_Discr := Discr; |
| 7210 |
end if; |
| 7211 |
|
| 7212 |
-- Construct the reference to the discriminant |
| 7213 |
|
| 7214 |
Scomp := |
| 7215 |
Make_Selected_Component (Loc, |
| 7216 |
Prefix => |
| 7217 |
Unchecked_Convert_To (Pref_Type, |
| 7218 |
Duplicate_Subexpr (Pref)), |
| 7219 |
Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); |
| 7220 |
|
| 7221 |
-- Manually analyze and resolve this selected component. We really |
| 7222 |
-- want it just as it appears above, and do not want the expander |
| 7223 |
-- playing discriminal games etc with this reference. Then we append |
| 7224 |
-- the argument to the list we are gathering. |
| 7225 |
|
| 7226 |
Set_Etype (Scomp, Etype (Real_Discr)); |
| 7227 |
Set_Analyzed (Scomp, True); |
| 7228 |
Append_To (Args, Convert_To (Etype (Formal), Scomp)); |
| 7229 |
|
| 7230 |
Next_Formal_With_Extras (Formal); |
| 7231 |
Next_Discriminant (Discr); |
| 7232 |
end loop; |
| 7233 |
|
| 7234 |
-- Now build and insert the call |
| 7235 |
|
| 7236 |
Insert_Action (N, |
| 7237 |
Make_Raise_Constraint_Error (Loc, |
| 7238 |
Condition => |
| 7239 |
Make_Function_Call (Loc, |
| 7240 |
Name => New_Occurrence_Of (Discr_Fct, Loc), |
| 7241 |
Parameter_Associations => Args), |
| 7242 |
Reason => CE_Discriminant_Check_Failed)); |
| 7243 |
end Generate_Discriminant_Check; |
| 7244 |
|
| 7245 |
--------------------------- |
| 7246 |
-- Generate_Index_Checks -- |
| 7247 |
--------------------------- |
| 7248 |
|
| 7249 |
procedure Generate_Index_Checks |
| 7250 |
(N : Node_Id; |
| 7251 |
Checks_Generated : out Dimension_Set) |
| 7252 |
is |
| 7253 |
|
| 7254 |
function Entity_Of_Prefix return Entity_Id; |
| 7255 |
-- Returns the entity of the prefix of N (or Empty if not found) |
| 7256 |
|
| 7257 |
---------------------- |
| 7258 |
-- Entity_Of_Prefix -- |
| 7259 |
---------------------- |
| 7260 |
|
| 7261 |
function Entity_Of_Prefix return Entity_Id is |
| 7262 |
P : Node_Id; |
| 7263 |
|
| 7264 |
begin |
| 7265 |
P := Prefix (N); |
| 7266 |
while not Is_Entity_Name (P) loop |
| 7267 |
if Nkind (P) not in N_Selected_Component | N_Indexed_Component then |
| 7268 |
return Empty; |
| 7269 |
end if; |
| 7270 |
|
| 7271 |
P := Prefix (P); |
| 7272 |
end loop; |
| 7273 |
|
| 7274 |
return Entity (P); |
| 7275 |
end Entity_Of_Prefix; |
| 7276 |
|
| 7277 |
-- Local variables |
| 7278 |
|
| 7279 |
Loc : constant Source_Ptr := Sloc (N); |
| 7280 |
A : constant Node_Id := Prefix (N); |
| 7281 |
A_Ent : constant Entity_Id := Entity_Of_Prefix; |
| 7282 |
|
| 7283 |
Expr : Node_Id; |
| 7284 |
|
| 7285 |
-- Start of processing for Generate_Index_Checks |
| 7286 |
|
| 7287 |
begin |
| 7288 |
Checks_Generated.Elements := (others => False); |
| 7289 |
|
| 7290 |
-- Ignore call if the prefix is not an array since we have a serious |
| 7291 |
-- error in the sources. Ignore it also if index checks are suppressed |
| 7292 |
-- for array object or type. |
| 7293 |
|
| 7294 |
if not Is_Array_Type (Etype (A)) |
| 7295 |
or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
| 7296 |
or else Index_Checks_Suppressed (Etype (A)) |
| 7297 |
then |
| 7298 |
return; |
| 7299 |
|
| 7300 |
-- The indexed component we are dealing with contains 'Loop_Entry in its |
| 7301 |
-- prefix. This case arises when analysis has determined that constructs |
| 7302 |
-- such as |
| 7303 |
|
| 7304 |
-- Prefix'Loop_Entry (Expr) |
| 7305 |
-- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) |
| 7306 |
|
| 7307 |
-- require rewriting for error detection purposes. A side effect of this |
| 7308 |
-- action is the generation of index checks that mention 'Loop_Entry. |
| 7309 |
-- Delay the generation of the check until 'Loop_Entry has been properly |
| 7310 |
-- expanded. This is done in Expand_Loop_Entry_Attributes. |
| 7311 |
|
| 7312 |
elsif Is_Attribute_Loop_Entry (Prefix (N)) then |
| 7313 |
return; |
| 7314 |
end if; |
| 7315 |
|
| 7316 |
-- Generate a raise of constraint error with the appropriate reason and |
| 7317 |
-- a condition of the form: |
| 7318 |
|
| 7319 |
-- Base_Type (Sub) not in Array'Range (Subscript) |
| 7320 |
|
| 7321 |
-- Note that the reason we generate the conversion to the base type here |
| 7322 |
-- is that we definitely want the range check to take place, even if it |
| 7323 |
-- looks like the subtype is OK. Optimization considerations that allow |
| 7324 |
-- us to omit the check have already been taken into account in the |
| 7325 |
-- setting of the Do_Range_Check flag earlier on. |
| 7326 |
|
| 7327 |
Expr := First (Expressions (N)); |
| 7328 |
|
| 7329 |
-- Handle string literals |
| 7330 |
|
| 7331 |
if Ekind (Etype (A)) = E_String_Literal_Subtype then |
| 7332 |
if Do_Range_Check (Expr) then |
| 7333 |
Set_Do_Range_Check (Expr, False); |
| 7334 |
|
| 7335 |
-- For string literals we obtain the bounds of the string from the |
| 7336 |
-- associated subtype. |
| 7337 |
|
| 7338 |
Insert_Action (N, |
| 7339 |
Make_Raise_Constraint_Error (Loc, |
| 7340 |
Condition => |
| 7341 |
Make_Not_In (Loc, |
| 7342 |
Left_Opnd => |
| 7343 |
Convert_To (Base_Type (Etype (Expr)), |
| 7344 |
Duplicate_Subexpr_Move_Checks (Expr)), |
| 7345 |
Right_Opnd => |
| 7346 |
Make_Attribute_Reference (Loc, |
| 7347 |
Prefix => New_Occurrence_Of (Etype (A), Loc), |
| 7348 |
Attribute_Name => Name_Range)), |
| 7349 |
Reason => CE_Index_Check_Failed)); |
| 7350 |
|
| 7351 |
Checks_Generated.Elements (1) := True; |
| 7352 |
end if; |
| 7353 |
|
| 7354 |
-- General case |
| 7355 |
|
| 7356 |
else |
| 7357 |
declare |
| 7358 |
A_Idx : Node_Id; |
| 7359 |
A_Range : Node_Id; |
| 7360 |
Ind : Pos; |
| 7361 |
Num : List_Id; |
| 7362 |
Range_N : Node_Id; |
| 7363 |
Stmt : Node_Id; |
| 7364 |
Sub : Node_Id; |
| 7365 |
|
| 7366 |
begin |
| 7367 |
A_Idx := First_Index (Etype (A)); |
| 7368 |
Ind := 1; |
| 7369 |
while Present (Expr) loop |
| 7370 |
if Nkind (Expr) = N_Expression_With_Actions then |
| 7371 |
Sub := Expression (Expr); |
| 7372 |
else |
| 7373 |
Sub := Expr; |
| 7374 |
end if; |
| 7375 |
|
| 7376 |
if Do_Range_Check (Sub) then |
| 7377 |
Set_Do_Range_Check (Sub, False); |
| 7378 |
|
| 7379 |
-- Force evaluation except for the case of a simple name of |
| 7380 |
-- a nonvolatile entity. |
| 7381 |
|
| 7382 |
if not Is_Entity_Name (Sub) |
| 7383 |
or else Treat_As_Volatile (Entity (Sub)) |
| 7384 |
then |
| 7385 |
Force_Evaluation (Sub); |
| 7386 |
end if; |
| 7387 |
|
| 7388 |
if Nkind (A_Idx) = N_Range then |
| 7389 |
A_Range := A_Idx; |
| 7390 |
|
| 7391 |
elsif Nkind (A_Idx) in N_Identifier | N_Expanded_Name then |
| 7392 |
A_Range := Scalar_Range (Entity (A_Idx)); |
| 7393 |
|
| 7394 |
if Nkind (A_Range) = N_Subtype_Indication then |
| 7395 |
A_Range := Range_Expression (Constraint (A_Range)); |
| 7396 |
end if; |
| 7397 |
|
| 7398 |
else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); |
| 7399 |
A_Range := Range_Expression (Constraint (A_Idx)); |
| 7400 |
end if; |
| 7401 |
|
| 7402 |
-- For array objects with constant bounds we can generate |
| 7403 |
-- the index check using the bounds of the type of the index |
| 7404 |
|
| 7405 |
if Present (A_Ent) |
| 7406 |
and then Ekind (A_Ent) = E_Variable |
| 7407 |
and then Is_Constant_Bound (Low_Bound (A_Range)) |
| 7408 |
and then Is_Constant_Bound (High_Bound (A_Range)) |
| 7409 |
then |
| 7410 |
Range_N := |
| 7411 |
Make_Attribute_Reference (Loc, |
| 7412 |
Prefix => |
| 7413 |
New_Occurrence_Of (Etype (A_Idx), Loc), |
| 7414 |
Attribute_Name => Name_Range); |
| 7415 |
|
| 7416 |
-- For arrays with non-constant bounds we cannot generate |
| 7417 |
-- the index check using the bounds of the type of the index |
| 7418 |
-- since it may reference discriminants of some enclosing |
| 7419 |
-- type. We obtain the bounds directly from the prefix |
| 7420 |
-- object. |
| 7421 |
|
| 7422 |
else |
| 7423 |
if Ind = 1 then |
| 7424 |
Num := No_List; |
| 7425 |
else |
| 7426 |
Num := New_List (Make_Integer_Literal (Loc, Ind)); |
| 7427 |
end if; |
| 7428 |
|
| 7429 |
Range_N := |
| 7430 |
Make_Attribute_Reference (Loc, |
| 7431 |
Prefix => |
| 7432 |
Duplicate_Subexpr_Move_Checks (A, Name_Req => True), |
| 7433 |
Attribute_Name => Name_Range, |
| 7434 |
Expressions => Num); |
| 7435 |
end if; |
| 7436 |
|
| 7437 |
Stmt := |
| 7438 |
Make_Raise_Constraint_Error (Loc, |
| 7439 |
Condition => |
| 7440 |
Make_Not_In (Loc, |
| 7441 |
Left_Opnd => |
| 7442 |
Convert_To (Base_Type (Etype (Sub)), |
| 7443 |
Duplicate_Subexpr_Move_Checks (Sub)), |
| 7444 |
Right_Opnd => Range_N), |
| 7445 |
Reason => CE_Index_Check_Failed); |
| 7446 |
|
| 7447 |
if Nkind (Expr) = N_Expression_With_Actions then |
| 7448 |
Append_To (Actions (Expr), Stmt); |
| 7449 |
Analyze (Stmt); |
| 7450 |
else |
| 7451 |
Insert_Action (Expr, Stmt); |
| 7452 |
end if; |
| 7453 |
|
| 7454 |
Checks_Generated.Elements (Ind) := True; |
| 7455 |
end if; |
| 7456 |
|
| 7457 |
Next_Index (A_Idx); |
| 7458 |
Ind := Ind + 1; |
| 7459 |
Next (Expr); |
| 7460 |
end loop; |
| 7461 |
end; |
| 7462 |
end if; |
| 7463 |
end Generate_Index_Checks; |
| 7464 |
|
| 7465 |
-------------------------- |
| 7466 |
-- Generate_Range_Check -- |
| 7467 |
-------------------------- |
| 7468 |
|
| 7469 |
procedure Generate_Range_Check |
| 7470 |
(N : Node_Id; |
| 7471 |
Target_Type : Entity_Id; |
| 7472 |
Reason : RT_Exception_Code) |
| 7473 |
is |
| 7474 |
Loc : constant Source_Ptr := Sloc (N); |
| 7475 |
Source_Type : constant Entity_Id := Etype (N); |
| 7476 |
Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); |
| 7477 |
Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); |
| 7478 |
|
| 7479 |
procedure Convert_And_Check_Range (Suppress : Check_Id); |
| 7480 |
-- Convert N to the target base type and save the result in a temporary. |
| 7481 |
-- The action is analyzed using the default checks as modified by the |
| 7482 |
-- given Suppress argument. Then check the converted value against the |
| 7483 |
-- range of the target subtype. |
| 7484 |
|
| 7485 |
function Is_Single_Attribute_Reference (N : Node_Id) return Boolean; |
| 7486 |
-- Return True if N is an expression that contains a single attribute |
| 7487 |
-- reference, possibly as operand among only integer literal operands. |
| 7488 |
|
| 7489 |
----------------------------- |
| 7490 |
-- Convert_And_Check_Range -- |
| 7491 |
----------------------------- |
| 7492 |
|
| 7493 |
procedure Convert_And_Check_Range (Suppress : Check_Id) is |
| 7494 |
Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
| 7495 |
Conv_N : Node_Id; |
| 7496 |
|
| 7497 |
begin |
| 7498 |
-- For enumeration types with non-standard representation this is a |
| 7499 |
-- direct conversion from the enumeration type to the target integer |
| 7500 |
-- type, which is treated by the back end as a normal integer type |
| 7501 |
-- conversion, treating the enumeration type as an integer, which is |
| 7502 |
-- exactly what we want. We set Conversion_OK to make sure that the |
| 7503 |
-- analyzer does not complain about what otherwise might be an |
| 7504 |
-- illegal conversion. |
| 7505 |
|
| 7506 |
if Is_Enumeration_Type (Source_Base_Type) |
| 7507 |
and then Present (Enum_Pos_To_Rep (Source_Base_Type)) |
| 7508 |
and then Is_Integer_Type (Target_Base_Type) |
| 7509 |
then |
| 7510 |
Conv_N := OK_Convert_To (Target_Base_Type, Duplicate_Subexpr (N)); |
| 7511 |
else |
| 7512 |
Conv_N := Convert_To (Target_Base_Type, Duplicate_Subexpr (N)); |
| 7513 |
end if; |
| 7514 |
|
| 7515 |
-- We make a temporary to hold the value of the conversion to the |
| 7516 |
-- target base type, and then do the test against this temporary. |
| 7517 |
-- N itself is replaced by an occurrence of Tnn and followed by |
| 7518 |
-- the explicit range check. |
| 7519 |
|
| 7520 |
-- Tnn : constant Target_Base_Type := Target_Base_Type (N); |
| 7521 |
-- [constraint_error when Tnn not in Target_Type] |
| 7522 |
-- Tnn |
| 7523 |
|
| 7524 |
Insert_Actions (N, New_List ( |
| 7525 |
Make_Object_Declaration (Loc, |
| 7526 |
Defining_Identifier => Tnn, |
| 7527 |
Object_Definition => New_Occurrence_Of (Target_Base_Type, Loc), |
| 7528 |
Constant_Present => True, |
| 7529 |
Expression => Conv_N), |
| 7530 |
|
| 7531 |
Make_Raise_Constraint_Error (Loc, |
| 7532 |
Condition => |
| 7533 |
Make_Not_In (Loc, |
| 7534 |
Left_Opnd => New_Occurrence_Of (Tnn, Loc), |
| 7535 |
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), |
| 7536 |
Reason => Reason)), |
| 7537 |
Suppress => Suppress); |
| 7538 |
|
| 7539 |
Rewrite (N, New_Occurrence_Of (Tnn, Loc)); |
| 7540 |
|
| 7541 |
-- Set the type of N, because the declaration for Tnn might not |
| 7542 |
-- be analyzed yet, as is the case if N appears within a record |
| 7543 |
-- declaration, as a discriminant constraint or expression. |
| 7544 |
|
| 7545 |
Set_Etype (N, Target_Base_Type); |
| 7546 |
end Convert_And_Check_Range; |
| 7547 |
|
| 7548 |
------------------------------------- |
| 7549 |
-- Is_Single_Attribute_Reference -- |
| 7550 |
------------------------------------- |
| 7551 |
|
| 7552 |
function Is_Single_Attribute_Reference (N : Node_Id) return Boolean is |
| 7553 |
begin |
| 7554 |
if Nkind (N) = N_Attribute_Reference then |
| 7555 |
return True; |
| 7556 |
|
| 7557 |
elsif Nkind (N) in N_Binary_Op then |
| 7558 |
if Nkind (Right_Opnd (N)) = N_Integer_Literal then |
| 7559 |
return Is_Single_Attribute_Reference (Left_Opnd (N)); |
| 7560 |
|
| 7561 |
elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then |
| 7562 |
return Is_Single_Attribute_Reference (Right_Opnd (N)); |
| 7563 |
|
| 7564 |
else |
| 7565 |
return False; |
| 7566 |
end if; |
| 7567 |
|
| 7568 |
else |
| 7569 |
return False; |
| 7570 |
end if; |
| 7571 |
end Is_Single_Attribute_Reference; |
| 7572 |
|
| 7573 |
-- Start of processing for Generate_Range_Check |
| 7574 |
|
| 7575 |
begin |
| 7576 |
-- First special case, if the source type is already within the range |
| 7577 |
-- of the target type, then no check is needed (probably we should have |
| 7578 |
-- stopped Do_Range_Check from being set in the first place, but better |
| 7579 |
-- late than never in preventing junk code and junk flag settings). |
| 7580 |
|
| 7581 |
if In_Subrange_Of (Source_Type, Target_Type) |
| 7582 |
|
| 7583 |
-- We do NOT apply this if the source node is a literal, since in this |
| 7584 |
-- case the literal has already been labeled as having the subtype of |
| 7585 |
-- the target. |
| 7586 |
|
| 7587 |
and then not |
| 7588 |
(Nkind (N) in |
| 7589 |
N_Integer_Literal | N_Real_Literal | N_Character_Literal |
| 7590 |
or else |
| 7591 |
(Is_Entity_Name (N) |
| 7592 |
and then Ekind (Entity (N)) = E_Enumeration_Literal)) |
| 7593 |
then |
| 7594 |
Set_Do_Range_Check (N, False); |
| 7595 |
return; |
| 7596 |
end if; |
| 7597 |
|
| 7598 |
-- Here a check is needed. If the expander is not active (which is also |
| 7599 |
-- the case in GNATprove mode), then simply set the Do_Range_Check flag |
| 7600 |
-- and we are done. We just want to see the range check flag set, we do |
| 7601 |
-- not want to generate the explicit range check code. |
| 7602 |
|
| 7603 |
if not Expander_Active then |
| 7604 |
Set_Do_Range_Check (N); |
| 7605 |
return; |
| 7606 |
end if; |
| 7607 |
|
| 7608 |
-- Here we will generate an explicit range check, so we don't want to |
| 7609 |
-- set the Do_Range check flag, since the range check is taken care of |
| 7610 |
-- by the code we will generate. |
| 7611 |
|
| 7612 |
Set_Do_Range_Check (N, False); |
| 7613 |
|
| 7614 |
-- Force evaluation of the node, so that it does not get evaluated twice |
| 7615 |
-- (once for the check, once for the actual reference). Such a double |
| 7616 |
-- evaluation is always a potential source of inefficiency, and is |
| 7617 |
-- functionally incorrect in the volatile case. |
| 7618 |
|
| 7619 |
-- We skip the evaluation of attribute references because, after these |
| 7620 |
-- runtime checks are generated, the expander may need to rewrite this |
| 7621 |
-- node (for example, see Attribute_Max_Size_In_Storage_Elements in |
| 7622 |
-- Expand_N_Attribute_Reference) and, in many cases, their return type |
| 7623 |
-- is universal integer, which is a very large type for a temporary. |
| 7624 |
|
| 7625 |
if not Is_Single_Attribute_Reference (N) |
| 7626 |
and then (not Is_Entity_Name (N) |
| 7627 |
or else Treat_As_Volatile (Entity (N))) |
| 7628 |
then |
| 7629 |
Force_Evaluation (N, Mode => Strict); |
| 7630 |
end if; |
| 7631 |
|
| 7632 |
-- The easiest case is when Source_Base_Type and Target_Base_Type are |
| 7633 |
-- the same since in this case we can simply do a direct check of the |
| 7634 |
-- value of N against the bounds of Target_Type. |
| 7635 |
|
| 7636 |
-- [constraint_error when N not in Target_Type] |
| 7637 |
|
| 7638 |
-- Note: this is by far the most common case, for example all cases of |
| 7639 |
-- checks on the RHS of assignments are in this category, but not all |
| 7640 |
-- cases are like this. Notably conversions can involve two types. |
| 7641 |
|
| 7642 |
if Source_Base_Type = Target_Base_Type then |
| 7643 |
|
| 7644 |
-- Insert the explicit range check. Note that we suppress checks for |
| 7645 |
-- this code, since we don't want a recursive range check popping up. |
| 7646 |
|
| 7647 |
Insert_Action (N, |
| 7648 |
Make_Raise_Constraint_Error (Loc, |
| 7649 |
Condition => |
| 7650 |
Make_Not_In (Loc, |
| 7651 |
Left_Opnd => Duplicate_Subexpr (N), |
| 7652 |
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), |
| 7653 |
Reason => Reason), |
| 7654 |
Suppress => All_Checks); |
| 7655 |
|
| 7656 |
-- Next test for the case where the target type is within the bounds |
| 7657 |
-- of the base type of the source type, since in this case we can |
| 7658 |
-- simply convert the bounds of the target type to this base type |
| 7659 |
-- to do the test. |
| 7660 |
|
| 7661 |
-- [constraint_error when N not in |
| 7662 |
-- Source_Base_Type (Target_Type'First) |
| 7663 |
-- .. |
| 7664 |
-- Source_Base_Type(Target_Type'Last))] |
| 7665 |
|
| 7666 |
-- The conversions will always work and need no check |
| 7667 |
|
| 7668 |
-- Unchecked_Convert_To is used instead of Convert_To to handle the case |
| 7669 |
-- of converting from an enumeration value to an integer type, such as |
| 7670 |
-- occurs for the case of generating a range check on Enum'Val(Exp) |
| 7671 |
-- (which used to be handled by gigi). This is OK, since the conversion |
| 7672 |
-- itself does not require a check. |
| 7673 |
|
| 7674 |
elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
| 7675 |
|
| 7676 |
-- Insert the explicit range check. Note that we suppress checks for |
| 7677 |
-- this code, since we don't want a recursive range check popping up. |
| 7678 |
|
| 7679 |
if Is_Discrete_Type (Source_Base_Type) |
| 7680 |
and then |
| 7681 |
Is_Discrete_Type (Target_Base_Type) |
| 7682 |
then |
| 7683 |
Insert_Action (N, |
| 7684 |
Make_Raise_Constraint_Error (Loc, |
| 7685 |
Condition => |
| 7686 |
Make_Not_In (Loc, |
| 7687 |
Left_Opnd => Duplicate_Subexpr (N), |
| 7688 |
|
| 7689 |
Right_Opnd => |
| 7690 |
Make_Range (Loc, |
| 7691 |
Low_Bound => |
| 7692 |
Unchecked_Convert_To (Source_Base_Type, |
| 7693 |
Make_Attribute_Reference (Loc, |
| 7694 |
Prefix => |
| 7695 |
New_Occurrence_Of (Target_Type, Loc), |
| 7696 |
Attribute_Name => Name_First)), |
| 7697 |
|
| 7698 |
High_Bound => |
| 7699 |
Unchecked_Convert_To (Source_Base_Type, |
| 7700 |
Make_Attribute_Reference (Loc, |
| 7701 |
Prefix => |
| 7702 |
New_Occurrence_Of (Target_Type, Loc), |
| 7703 |
Attribute_Name => Name_Last)))), |
| 7704 |
Reason => Reason), |
| 7705 |
Suppress => All_Checks); |
| 7706 |
|
| 7707 |
-- For conversions involving at least one type that is not discrete, |
| 7708 |
-- first convert to the target base type and then generate the range |
| 7709 |
-- check. This avoids problems with values that are close to a bound |
| 7710 |
-- of the target type that would fail a range check when done in a |
| 7711 |
-- larger source type before converting but pass if converted with |
| 7712 |
-- rounding and then checked (such as in float-to-float conversions). |
| 7713 |
|
| 7714 |
-- Note that overflow checks are not suppressed for this code because |
| 7715 |
-- we do not know whether the source type is in range of the target |
| 7716 |
-- base type (unlike in the next case below). |
| 7717 |
|
| 7718 |
else |
| 7719 |
Convert_And_Check_Range (Suppress => Range_Check); |
| 7720 |
end if; |
| 7721 |
|
| 7722 |
-- Note that at this stage we know that the Target_Base_Type is not in |
| 7723 |
-- the range of the Source_Base_Type (since even the Target_Type itself |
| 7724 |
-- is not in this range). It could still be the case that Source_Type is |
| 7725 |
-- in range of the target base type since we have not checked that case. |
| 7726 |
|
| 7727 |
-- If that is the case, we can freely convert the source to the target, |
| 7728 |
-- and then test the target result against the bounds. Note that checks |
| 7729 |
-- are suppressed for this code, since we don't want a recursive range |
| 7730 |
-- check popping up. |
| 7731 |
|
| 7732 |
elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
| 7733 |
Convert_And_Check_Range (Suppress => All_Checks); |
| 7734 |
|
| 7735 |
-- At this stage, we know that we have two scalar types, which are |
| 7736 |
-- directly convertible, and where neither scalar type has a base |
| 7737 |
-- range that is in the range of the other scalar type. |
| 7738 |
|
| 7739 |
-- The only way this can happen is with a signed and unsigned type. |
| 7740 |
-- So test for these two cases: |
| 7741 |
|
| 7742 |
else |
| 7743 |
-- Case of the source is unsigned and the target is signed |
| 7744 |
|
| 7745 |
if Is_Unsigned_Type (Source_Base_Type) |
| 7746 |
and then not Is_Unsigned_Type (Target_Base_Type) |
| 7747 |
then |
| 7748 |
-- If the source is unsigned and the target is signed, then we |
| 7749 |
-- know that the source is not shorter than the target (otherwise |
| 7750 |
-- the source base type would be in the target base type range). |
| 7751 |
|
| 7752 |
-- In other words, the unsigned type is either the same size as |
| 7753 |
-- the target, or it is larger. It cannot be smaller. |
| 7754 |
|
| 7755 |
pragma Assert |
| 7756 |
(Esize (Source_Base_Type) >= Esize (Target_Base_Type)); |
| 7757 |
|
| 7758 |
-- We only need to check the low bound if the low bound of the |
| 7759 |
-- target type is non-negative. If the low bound of the target |
| 7760 |
-- type is negative, then we know that we will fit fine. |
| 7761 |
|
| 7762 |
-- If the high bound of the target type is negative, then we |
| 7763 |
-- know we have a constraint error, since we can't possibly |
| 7764 |
-- have a negative source. |
| 7765 |
|
| 7766 |
-- With these two checks out of the way, we can do the check |
| 7767 |
-- using the source type safely |
| 7768 |
|
| 7769 |
-- This is definitely the most annoying case. |
| 7770 |
|
| 7771 |
-- [constraint_error |
| 7772 |
-- when (Target_Type'First >= 0 |
| 7773 |
-- and then |
| 7774 |
-- N < Source_Base_Type (Target_Type'First)) |
| 7775 |
-- or else Target_Type'Last < 0 |
| 7776 |
-- or else N > Source_Base_Type (Target_Type'Last)]; |
| 7777 |
|
| 7778 |
-- We turn off all checks since we know that the conversions |
| 7779 |
-- will work fine, given the guards for negative values. |
| 7780 |
|
| 7781 |
Insert_Action (N, |
| 7782 |
Make_Raise_Constraint_Error (Loc, |
| 7783 |
Condition => |
| 7784 |
Make_Or_Else (Loc, |
| 7785 |
Make_Or_Else (Loc, |
| 7786 |
Left_Opnd => |
| 7787 |
Make_And_Then (Loc, |
| 7788 |
Left_Opnd => Make_Op_Ge (Loc, |
| 7789 |
Left_Opnd => |
| 7790 |
Make_Attribute_Reference (Loc, |
| 7791 |
Prefix => |
| 7792 |
New_Occurrence_Of (Target_Type, Loc), |
| 7793 |
Attribute_Name => Name_First), |
| 7794 |
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), |
| 7795 |
|
| 7796 |
Right_Opnd => |
| 7797 |
Make_Op_Lt (Loc, |
| 7798 |
Left_Opnd => Duplicate_Subexpr (N), |
| 7799 |
Right_Opnd => |
| 7800 |
Convert_To (Source_Base_Type, |
| 7801 |
Make_Attribute_Reference (Loc, |
| 7802 |
Prefix => |
| 7803 |
New_Occurrence_Of (Target_Type, Loc), |
| 7804 |
Attribute_Name => Name_First)))), |
| 7805 |
|
| 7806 |
Right_Opnd => |
| 7807 |
Make_Op_Lt (Loc, |
| 7808 |
Left_Opnd => |
| 7809 |
Make_Attribute_Reference (Loc, |
| 7810 |
Prefix => New_Occurrence_Of (Target_Type, Loc), |
| 7811 |
Attribute_Name => Name_Last), |
| 7812 |
Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), |
| 7813 |
|
| 7814 |
Right_Opnd => |
| 7815 |
Make_Op_Gt (Loc, |
| 7816 |
Left_Opnd => Duplicate_Subexpr (N), |
| 7817 |
Right_Opnd => |
| 7818 |
Convert_To (Source_Base_Type, |
| 7819 |
Make_Attribute_Reference (Loc, |
| 7820 |
Prefix => New_Occurrence_Of (Target_Type, Loc), |
| 7821 |
Attribute_Name => Name_Last)))), |
| 7822 |
|
| 7823 |
Reason => Reason), |
| 7824 |
Suppress => All_Checks); |
| 7825 |
|
| 7826 |
-- Only remaining possibility is that the source is signed and |
| 7827 |
-- the target is unsigned. |
| 7828 |
|
| 7829 |
else |
| 7830 |
pragma Assert (not Is_Unsigned_Type (Source_Base_Type) |
| 7831 |
and then Is_Unsigned_Type (Target_Base_Type)); |
| 7832 |
|
| 7833 |
-- If the source is signed and the target is unsigned, then we |
| 7834 |
-- know that the target is not shorter than the source (otherwise |
| 7835 |
-- the target base type would be in the source base type range). |
| 7836 |
|
| 7837 |
-- In other words, the unsigned type is either the same size as |
| 7838 |
-- the target, or it is larger. It cannot be smaller. |
| 7839 |
|
| 7840 |
-- Clearly we have an error if the source value is negative since |
| 7841 |
-- no unsigned type can have negative values. If the source type |
| 7842 |
-- is non-negative, then the check can be done using the target |
| 7843 |
-- type. |
| 7844 |
|
| 7845 |
-- Tnn : constant Target_Base_Type (N) := Target_Type; |
| 7846 |
|
| 7847 |
-- [constraint_error |
| 7848 |
-- when N < 0 or else Tnn not in Target_Type]; |
| 7849 |
|
| 7850 |
-- We turn off all checks for the conversion of N to the target |
| 7851 |
-- base type, since we generate the explicit check to ensure that |
| 7852 |
-- the value is non-negative |
| 7853 |
|
| 7854 |
declare |
| 7855 |
Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
| 7856 |
|
| 7857 |
begin |
| 7858 |
Insert_Actions (N, New_List ( |
| 7859 |
Make_Object_Declaration (Loc, |
| 7860 |
Defining_Identifier => Tnn, |
| 7861 |
Object_Definition => |
| 7862 |
New_Occurrence_Of (Target_Base_Type, Loc), |
| 7863 |
Constant_Present => True, |
| 7864 |
Expression => |
| 7865 |
Unchecked_Convert_To |
| 7866 |
(Target_Base_Type, Duplicate_Subexpr (N))), |
| 7867 |
|
| 7868 |
Make_Raise_Constraint_Error (Loc, |
| 7869 |
Condition => |
| 7870 |
Make_Or_Else (Loc, |
| 7871 |
Left_Opnd => |
| 7872 |
Make_Op_Lt (Loc, |
| 7873 |
Left_Opnd => Duplicate_Subexpr (N), |
| 7874 |
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), |
| 7875 |
|
| 7876 |
Right_Opnd => |
| 7877 |
Make_Not_In (Loc, |
| 7878 |
Left_Opnd => New_Occurrence_Of (Tnn, Loc), |
| 7879 |
Right_Opnd => |
| 7880 |
New_Occurrence_Of (Target_Type, Loc))), |
| 7881 |
|
| 7882 |
Reason => Reason)), |
| 7883 |
Suppress => All_Checks); |
| 7884 |
|
| 7885 |
-- Set the Etype explicitly, because Insert_Actions may have |
| 7886 |
-- placed the declaration in the freeze list for an enclosing |
| 7887 |
-- construct, and thus it is not analyzed yet. |
| 7888 |
|
| 7889 |
Set_Etype (Tnn, Target_Base_Type); |
| 7890 |
Rewrite (N, New_Occurrence_Of (Tnn, Loc)); |
| 7891 |
end; |
| 7892 |
end if; |
| 7893 |
end if; |
| 7894 |
end Generate_Range_Check; |
| 7895 |
|
| 7896 |
------------------ |
| 7897 |
-- Get_Check_Id -- |
| 7898 |
------------------ |
| 7899 |
|
| 7900 |
function Get_Check_Id (N : Name_Id) return Check_Id is |
| 7901 |
begin |
| 7902 |
-- For standard check name, we can do a direct computation |
| 7903 |
|
| 7904 |
if N in First_Check_Name .. Last_Check_Name then |
| 7905 |
return Check_Id (N - (First_Check_Name - 1)); |
| 7906 |
|
| 7907 |
-- For non-standard names added by pragma Check_Name, search table |
| 7908 |
|
| 7909 |
else |
| 7910 |
for J in All_Checks + 1 .. Check_Names.Last loop |
| 7911 |
if Check_Names.Table (J) = N then |
| 7912 |
return J; |
| 7913 |
end if; |
| 7914 |
end loop; |
| 7915 |
end if; |
| 7916 |
|
| 7917 |
-- No matching name found |
| 7918 |
|
| 7919 |
return No_Check_Id; |
| 7920 |
end Get_Check_Id; |
| 7921 |
|
| 7922 |
--------------------- |
| 7923 |
-- Get_Discriminal -- |
| 7924 |
--------------------- |
| 7925 |
|
| 7926 |
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is |
| 7927 |
Loc : constant Source_Ptr := Sloc (E); |
| 7928 |
D : Entity_Id; |
| 7929 |
Sc : Entity_Id; |
| 7930 |
|
| 7931 |
begin |
| 7932 |
-- The bound can be a bona fide parameter of a protected operation, |
| 7933 |
-- rather than a prival encoded as an in-parameter. |
| 7934 |
|
| 7935 |
if No (Discriminal_Link (Entity (Bound))) then |
| 7936 |
return Bound; |
| 7937 |
end if; |
| 7938 |
|
| 7939 |
-- Climb the scope stack looking for an enclosing protected type. If |
| 7940 |
-- we run out of scopes, return the bound itself. |
| 7941 |
|
| 7942 |
Sc := Scope (E); |
| 7943 |
while Present (Sc) loop |
| 7944 |
if Sc = Standard_Standard then |
| 7945 |
return Bound; |
| 7946 |
elsif Ekind (Sc) = E_Protected_Type then |
| 7947 |
exit; |
| 7948 |
end if; |
| 7949 |
|
| 7950 |
Sc := Scope (Sc); |
| 7951 |
end loop; |
| 7952 |
|
| 7953 |
D := First_Discriminant (Sc); |
| 7954 |
while Present (D) loop |
| 7955 |
if Chars (D) = Chars (Bound) then |
| 7956 |
return New_Occurrence_Of (Discriminal (D), Loc); |
| 7957 |
end if; |
| 7958 |
|
| 7959 |
Next_Discriminant (D); |
| 7960 |
end loop; |
| 7961 |
|
| 7962 |
return Bound; |
| 7963 |
end Get_Discriminal; |
| 7964 |
|
| 7965 |
---------------------- |
| 7966 |
-- Get_Range_Checks -- |
| 7967 |
---------------------- |
| 7968 |
|
| 7969 |
function Get_Range_Checks |
| 7970 |
(Expr : Node_Id; |
| 7971 |
Target_Typ : Entity_Id; |
| 7972 |
Source_Typ : Entity_Id := Empty; |
| 7973 |
Warn_Node : Node_Id := Empty) return Check_Result |
| 7974 |
is |
| 7975 |
begin |
| 7976 |
return |
| 7977 |
Selected_Range_Checks (Expr, Target_Typ, Source_Typ, Warn_Node); |
| 7978 |
end Get_Range_Checks; |
| 7979 |
|
| 7980 |
------------------ |
| 7981 |
-- Guard_Access -- |
| 7982 |
------------------ |
| 7983 |
|
| 7984 |
function Guard_Access |
| 7985 |
(Cond : Node_Id; |
| 7986 |
Loc : Source_Ptr; |
| 7987 |
Expr : Node_Id) return Node_Id |
| 7988 |
is |
| 7989 |
begin |
| 7990 |
if Nkind (Cond) = N_Or_Else then |
| 7991 |
Set_Paren_Count (Cond, 1); |
| 7992 |
end if; |
| 7993 |
|
| 7994 |
if Nkind (Expr) = N_Allocator then |
| 7995 |
return Cond; |
| 7996 |
|
| 7997 |
else |
| 7998 |
return |
| 7999 |
Make_And_Then (Loc, |
| 8000 |
Left_Opnd => |
| 8001 |
Make_Op_Ne (Loc, |
| 8002 |
Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
| 8003 |
Right_Opnd => Make_Null (Loc)), |
| 8004 |
Right_Opnd => Cond); |
| 8005 |
end if; |
| 8006 |
end Guard_Access; |
| 8007 |
|
| 8008 |
----------------------------- |
| 8009 |
-- Index_Checks_Suppressed -- |
| 8010 |
----------------------------- |
| 8011 |
|
| 8012 |
function Index_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 8013 |
begin |
| 8014 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 8015 |
return Is_Check_Suppressed (E, Index_Check); |
| 8016 |
else |
| 8017 |
return Scope_Suppress.Suppress (Index_Check); |
| 8018 |
end if; |
| 8019 |
end Index_Checks_Suppressed; |
| 8020 |
|
| 8021 |
---------------- |
| 8022 |
-- Initialize -- |
| 8023 |
---------------- |
| 8024 |
|
| 8025 |
procedure Initialize is |
| 8026 |
begin |
| 8027 |
for J in Determine_Range_Cache_N'Range loop |
| 8028 |
Determine_Range_Cache_N (J) := Empty; |
| 8029 |
end loop; |
| 8030 |
|
| 8031 |
Check_Names.Init; |
| 8032 |
|
| 8033 |
for J in Int range 1 .. All_Checks loop |
| 8034 |
Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); |
| 8035 |
end loop; |
| 8036 |
end Initialize; |
| 8037 |
|
| 8038 |
------------------------- |
| 8039 |
-- Insert_Range_Checks -- |
| 8040 |
------------------------- |
| 8041 |
|
| 8042 |
procedure Insert_Range_Checks |
| 8043 |
(Checks : Check_Result; |
| 8044 |
Node : Node_Id; |
| 8045 |
Suppress_Typ : Entity_Id; |
| 8046 |
Static_Sloc : Source_Ptr; |
| 8047 |
Do_Before : Boolean := False) |
| 8048 |
is |
| 8049 |
Checks_On : constant Boolean := |
| 8050 |
not Index_Checks_Suppressed (Suppress_Typ) |
| 8051 |
or else |
| 8052 |
not Range_Checks_Suppressed (Suppress_Typ); |
| 8053 |
|
| 8054 |
Check_Node : Node_Id; |
| 8055 |
|
| 8056 |
begin |
| 8057 |
-- For now we just return if Checks_On is false, however this should be |
| 8058 |
-- enhanced to check for an always True value in the condition and to |
| 8059 |
-- generate a compilation warning. |
| 8060 |
|
| 8061 |
if not Expander_Active or not Checks_On then |
| 8062 |
return; |
| 8063 |
end if; |
| 8064 |
|
| 8065 |
for J in 1 .. 2 loop |
| 8066 |
exit when No (Checks (J)); |
| 8067 |
|
| 8068 |
if Nkind (Checks (J)) = N_Raise_Constraint_Error |
| 8069 |
and then Present (Condition (Checks (J))) |
| 8070 |
then |
| 8071 |
Check_Node := Checks (J); |
| 8072 |
else |
| 8073 |
Check_Node := |
| 8074 |
Make_Raise_Constraint_Error (Static_Sloc, |
| 8075 |
Reason => CE_Range_Check_Failed); |
| 8076 |
end if; |
| 8077 |
|
| 8078 |
Mark_Rewrite_Insertion (Check_Node); |
| 8079 |
|
| 8080 |
if Do_Before then |
| 8081 |
Insert_Before_And_Analyze (Node, Check_Node); |
| 8082 |
else |
| 8083 |
Insert_After_And_Analyze (Node, Check_Node); |
| 8084 |
end if; |
| 8085 |
end loop; |
| 8086 |
end Insert_Range_Checks; |
| 8087 |
|
| 8088 |
------------------------ |
| 8089 |
-- Insert_Valid_Check -- |
| 8090 |
------------------------ |
| 8091 |
|
| 8092 |
procedure Insert_Valid_Check |
| 8093 |
(Expr : Node_Id; |
| 8094 |
Related_Id : Entity_Id := Empty; |
| 8095 |
Is_Low_Bound : Boolean := False; |
| 8096 |
Is_High_Bound : Boolean := False) |
| 8097 |
is |
| 8098 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 8099 |
Typ : Entity_Id := Etype (Expr); |
| 8100 |
Exp : Node_Id; |
| 8101 |
|
| 8102 |
begin |
| 8103 |
-- Do not insert if checks off, or if not checking validity or if |
| 8104 |
-- expression is known to be valid. |
| 8105 |
|
| 8106 |
if not Validity_Checks_On |
| 8107 |
or else Range_Or_Validity_Checks_Suppressed (Expr) |
| 8108 |
or else Expr_Known_Valid (Expr) |
| 8109 |
then |
| 8110 |
return; |
| 8111 |
|
| 8112 |
-- Do not insert checks within a predicate function. This will arise |
| 8113 |
-- if the current unit and the predicate function are being compiled |
| 8114 |
-- with validity checks enabled. |
| 8115 |
|
| 8116 |
elsif Present (Predicate_Function (Typ)) |
| 8117 |
and then Current_Scope = Predicate_Function (Typ) |
| 8118 |
then |
| 8119 |
return; |
| 8120 |
|
| 8121 |
-- If the expression is a packed component of a modular type of the |
| 8122 |
-- right size, the data is always valid. |
| 8123 |
|
| 8124 |
elsif Nkind (Expr) = N_Selected_Component |
| 8125 |
and then Present (Component_Clause (Entity (Selector_Name (Expr)))) |
| 8126 |
and then Is_Modular_Integer_Type (Typ) |
| 8127 |
and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) |
| 8128 |
then |
| 8129 |
return; |
| 8130 |
|
| 8131 |
-- Do not generate a validity check when inside a generic unit as this |
| 8132 |
-- is an expansion activity. |
| 8133 |
|
| 8134 |
elsif Inside_A_Generic then |
| 8135 |
return; |
| 8136 |
end if; |
| 8137 |
|
| 8138 |
-- Entities declared in Lock_free protected types must be treated as |
| 8139 |
-- volatile, and we must inhibit validity checks to prevent improper |
| 8140 |
-- constant folding. |
| 8141 |
|
| 8142 |
if Is_Entity_Name (Expr) |
| 8143 |
and then Is_Subprogram (Scope (Entity (Expr))) |
| 8144 |
and then Present (Protected_Subprogram (Scope (Entity (Expr)))) |
| 8145 |
and then Uses_Lock_Free |
| 8146 |
(Scope (Protected_Subprogram (Scope (Entity (Expr))))) |
| 8147 |
then |
| 8148 |
return; |
| 8149 |
end if; |
| 8150 |
|
| 8151 |
-- If we have a checked conversion, then validity check applies to |
| 8152 |
-- the expression inside the conversion, not the result, since if |
| 8153 |
-- the expression inside is valid, then so is the conversion result. |
| 8154 |
|
| 8155 |
Exp := Expr; |
| 8156 |
while Nkind (Exp) = N_Type_Conversion loop |
| 8157 |
Exp := Expression (Exp); |
| 8158 |
end loop; |
| 8159 |
Typ := Etype (Exp); |
| 8160 |
|
| 8161 |
-- Do not generate a check for a variable which already validates the |
| 8162 |
-- value of an assignable object. |
| 8163 |
|
| 8164 |
if Is_Validation_Variable_Reference (Exp) then |
| 8165 |
return; |
| 8166 |
end if; |
| 8167 |
|
| 8168 |
declare |
| 8169 |
Decl : Node_Id; |
| 8170 |
CE : Node_Id; |
| 8171 |
PV : Node_Id; |
| 8172 |
Var_Id : Entity_Id; |
| 8173 |
|
| 8174 |
begin |
| 8175 |
-- If the expression denotes an assignable object, capture its value |
| 8176 |
-- in a variable and replace the original expression by the variable. |
| 8177 |
-- This approach has several effects: |
| 8178 |
|
| 8179 |
-- 1) The evaluation of the object results in only one read in the |
| 8180 |
-- case where the object is atomic or volatile. |
| 8181 |
|
| 8182 |
-- Var ... := Object; -- read |
| 8183 |
|
| 8184 |
-- 2) The captured value is the one verified by attribute 'Valid. |
| 8185 |
-- As a result the object is not evaluated again, which would |
| 8186 |
-- result in an unwanted read in the case where the object is |
| 8187 |
-- atomic or volatile. |
| 8188 |
|
| 8189 |
-- if not Var'Valid then -- OK, no read of Object |
| 8190 |
|
| 8191 |
-- if not Object'Valid then -- Wrong, extra read of Object |
| 8192 |
|
| 8193 |
-- 3) The captured value replaces the original object reference. |
| 8194 |
-- As a result the object is not evaluated again, in the same |
| 8195 |
-- vein as 2). |
| 8196 |
|
| 8197 |
-- ... Var ... -- OK, no read of Object |
| 8198 |
|
| 8199 |
-- ... Object ... -- Wrong, extra read of Object |
| 8200 |
|
| 8201 |
-- 4) The use of a variable to capture the value of the object |
| 8202 |
-- allows the propagation of any changes back to the original |
| 8203 |
-- object. |
| 8204 |
|
| 8205 |
-- procedure Call (Val : in out ...); |
| 8206 |
|
| 8207 |
-- Var : ... := Object; -- read Object |
| 8208 |
-- if not Var'Valid then -- validity check |
| 8209 |
-- Call (Var); -- modify Var |
| 8210 |
-- Object := Var; -- update Object |
| 8211 |
|
| 8212 |
if Is_Variable (Exp) then |
| 8213 |
Var_Id := Make_Temporary (Loc, 'T', Exp); |
| 8214 |
|
| 8215 |
-- Because we could be dealing with a transient scope which would |
| 8216 |
-- cause our object declaration to remain unanalyzed we must do |
| 8217 |
-- some manual decoration. |
| 8218 |
|
| 8219 |
Mutate_Ekind (Var_Id, E_Variable); |
| 8220 |
Set_Etype (Var_Id, Typ); |
| 8221 |
|
| 8222 |
Decl := |
| 8223 |
Make_Object_Declaration (Loc, |
| 8224 |
Defining_Identifier => Var_Id, |
| 8225 |
Object_Definition => New_Occurrence_Of (Typ, Loc), |
| 8226 |
Expression => New_Copy_Tree (Exp)); |
| 8227 |
|
| 8228 |
-- We might be validity-checking object whose type is declared as |
| 8229 |
-- limited but completion is a scalar type. We need to explicitly |
| 8230 |
-- flag its assignment as OK, as otherwise it would be rejected by |
| 8231 |
-- the language rules. |
| 8232 |
|
| 8233 |
Set_Assignment_OK (Decl); |
| 8234 |
|
| 8235 |
Insert_Action (Exp, Decl, Suppress => Validity_Check); |
| 8236 |
|
| 8237 |
Set_Validated_Object (Var_Id, New_Copy_Tree (Exp)); |
| 8238 |
|
| 8239 |
Rewrite (Exp, New_Occurrence_Of (Var_Id, Loc)); |
| 8240 |
|
| 8241 |
-- Move the Do_Range_Check flag over to the new Exp so it doesn't |
| 8242 |
-- get lost and doesn't leak elsewhere. |
| 8243 |
|
| 8244 |
if Do_Range_Check (Validated_Object (Var_Id)) then |
| 8245 |
Set_Do_Range_Check (Exp); |
| 8246 |
Set_Do_Range_Check (Validated_Object (Var_Id), False); |
| 8247 |
end if; |
| 8248 |
|
| 8249 |
-- In case of a type conversion, an expansion of the expr may be |
| 8250 |
-- needed (eg. fixed-point as actual). |
| 8251 |
|
| 8252 |
if Exp /= Expr then |
| 8253 |
pragma Assert (Nkind (Expr) = N_Type_Conversion); |
| 8254 |
Analyze_And_Resolve (Expr); |
| 8255 |
end if; |
| 8256 |
|
| 8257 |
PV := New_Occurrence_Of (Var_Id, Loc); |
| 8258 |
|
| 8259 |
-- Otherwise the expression does not denote a variable. Force its |
| 8260 |
-- evaluation by capturing its value in a constant. Generate: |
| 8261 |
|
| 8262 |
-- Temp : constant ... := Exp; |
| 8263 |
|
| 8264 |
else |
| 8265 |
Force_Evaluation |
| 8266 |
(Exp => Exp, |
| 8267 |
Related_Id => Related_Id, |
| 8268 |
Is_Low_Bound => Is_Low_Bound, |
| 8269 |
Is_High_Bound => Is_High_Bound); |
| 8270 |
|
| 8271 |
PV := New_Copy_Tree (Exp); |
| 8272 |
end if; |
| 8273 |
|
| 8274 |
-- A rather specialized test. If PV is an analyzed expression which |
| 8275 |
-- is an indexed component of a packed array that has not been |
| 8276 |
-- properly expanded, turn off its Analyzed flag to make sure it |
| 8277 |
-- gets properly reexpanded. If the prefix is an access value, |
| 8278 |
-- the dereference will be added later. |
| 8279 |
|
| 8280 |
-- The reason this arises is that Duplicate_Subexpr_No_Checks did |
| 8281 |
-- an analyze with the old parent pointer. This may point e.g. to |
| 8282 |
-- a subprogram call, which deactivates this expansion. |
| 8283 |
|
| 8284 |
if Analyzed (PV) |
| 8285 |
and then Nkind (PV) = N_Indexed_Component |
| 8286 |
and then Is_Array_Type (Etype (Prefix (PV))) |
| 8287 |
and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV)))) |
| 8288 |
then |
| 8289 |
Set_Analyzed (PV, False); |
| 8290 |
end if; |
| 8291 |
|
| 8292 |
-- Build the raise CE node to check for validity. We build a type |
| 8293 |
-- qualification for the prefix, since it may not be of the form of |
| 8294 |
-- a name, and we don't care in this context! |
| 8295 |
|
| 8296 |
CE := |
| 8297 |
Make_Raise_Constraint_Error (Loc, |
| 8298 |
Condition => |
| 8299 |
Make_Op_Not (Loc, |
| 8300 |
Right_Opnd => |
| 8301 |
Make_Attribute_Reference (Loc, |
| 8302 |
Prefix => PV, |
| 8303 |
Attribute_Name => Name_Valid)), |
| 8304 |
Reason => CE_Invalid_Data); |
| 8305 |
|
| 8306 |
-- Insert the validity check. Note that we do this with validity |
| 8307 |
-- checks turned off, to avoid recursion, we do not want validity |
| 8308 |
-- checks on the validity checking code itself. |
| 8309 |
|
| 8310 |
Insert_Action (Expr, CE, Suppress => Validity_Check); |
| 8311 |
|
| 8312 |
-- If the expression is a reference to an element of a bit-packed |
| 8313 |
-- array, then it is rewritten as a renaming declaration. If the |
| 8314 |
-- expression is an actual in a call, it has not been expanded, |
| 8315 |
-- waiting for the proper point at which to do it. The same happens |
| 8316 |
-- with renamings, so that we have to force the expansion now. This |
| 8317 |
-- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb |
| 8318 |
-- and exp_ch6.adb. |
| 8319 |
|
| 8320 |
if Is_Entity_Name (Exp) |
| 8321 |
and then Nkind (Parent (Entity (Exp))) = |
| 8322 |
N_Object_Renaming_Declaration |
| 8323 |
then |
| 8324 |
declare |
| 8325 |
Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); |
| 8326 |
begin |
| 8327 |
if Nkind (Old_Exp) = N_Indexed_Component |
| 8328 |
and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) |
| 8329 |
then |
| 8330 |
Expand_Packed_Element_Reference (Old_Exp); |
| 8331 |
end if; |
| 8332 |
end; |
| 8333 |
end if; |
| 8334 |
end; |
| 8335 |
end Insert_Valid_Check; |
| 8336 |
|
| 8337 |
------------------------------------- |
| 8338 |
-- Is_Signed_Integer_Arithmetic_Op -- |
| 8339 |
------------------------------------- |
| 8340 |
|
| 8341 |
function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is |
| 8342 |
begin |
| 8343 |
case Nkind (N) is |
| 8344 |
when N_Op_Abs |
| 8345 |
| N_Op_Add |
| 8346 |
| N_Op_Divide |
| 8347 |
| N_Op_Expon |
| 8348 |
| N_Op_Minus |
| 8349 |
| N_Op_Mod |
| 8350 |
| N_Op_Multiply |
| 8351 |
| N_Op_Plus |
| 8352 |
| N_Op_Rem |
| 8353 |
| N_Op_Subtract |
| 8354 |
=> |
| 8355 |
return Is_Signed_Integer_Type (Etype (N)); |
| 8356 |
|
| 8357 |
when N_Op_Compare => |
| 8358 |
return Is_Signed_Integer_Type (Etype (Left_Opnd (N))); |
| 8359 |
|
| 8360 |
when N_Case_Expression |
| 8361 |
| N_If_Expression |
| 8362 |
=> |
| 8363 |
return Is_Signed_Integer_Type (Etype (N)); |
| 8364 |
|
| 8365 |
when others => |
| 8366 |
return False; |
| 8367 |
end case; |
| 8368 |
end Is_Signed_Integer_Arithmetic_Op; |
| 8369 |
|
| 8370 |
---------------------------------- |
| 8371 |
-- Install_Null_Excluding_Check -- |
| 8372 |
---------------------------------- |
| 8373 |
|
| 8374 |
procedure Install_Null_Excluding_Check (N : Node_Id) is |
| 8375 |
Loc : constant Source_Ptr := Sloc (Parent (N)); |
| 8376 |
Typ : constant Entity_Id := Etype (N); |
| 8377 |
|
| 8378 |
procedure Mark_Non_Null; |
| 8379 |
-- After installation of check, if the node in question is an entity |
| 8380 |
-- name, then mark this entity as non-null if possible. |
| 8381 |
|
| 8382 |
------------------- |
| 8383 |
-- Mark_Non_Null -- |
| 8384 |
------------------- |
| 8385 |
|
| 8386 |
procedure Mark_Non_Null is |
| 8387 |
begin |
| 8388 |
-- Only case of interest is if node N is an entity name |
| 8389 |
|
| 8390 |
if Is_Entity_Name (N) then |
| 8391 |
|
| 8392 |
-- For sure, we want to clear an indication that this is known to |
| 8393 |
-- be null, since if we get past this check, it definitely is not. |
| 8394 |
|
| 8395 |
Set_Is_Known_Null (Entity (N), False); |
| 8396 |
|
| 8397 |
-- We can mark the entity as known to be non-null if it is safe to |
| 8398 |
-- capture the value. |
| 8399 |
|
| 8400 |
if Safe_To_Capture_Value (N, Entity (N)) then |
| 8401 |
Set_Is_Known_Non_Null (Entity (N)); |
| 8402 |
end if; |
| 8403 |
end if; |
| 8404 |
end Mark_Non_Null; |
| 8405 |
|
| 8406 |
-- Start of processing for Install_Null_Excluding_Check |
| 8407 |
|
| 8408 |
begin |
| 8409 |
-- No need to add null-excluding checks when the tree may not be fully |
| 8410 |
-- decorated. |
| 8411 |
|
| 8412 |
if Serious_Errors_Detected > 0 then |
| 8413 |
return; |
| 8414 |
end if; |
| 8415 |
|
| 8416 |
pragma Assert (Is_Access_Type (Typ)); |
| 8417 |
|
| 8418 |
-- No check inside a generic, check will be emitted in instance |
| 8419 |
|
| 8420 |
if Inside_A_Generic then |
| 8421 |
return; |
| 8422 |
|
| 8423 |
-- No check during preanalysis |
| 8424 |
|
| 8425 |
elsif Preanalysis_Active then |
| 8426 |
return; |
| 8427 |
|
| 8428 |
-- No check needed if known to be non-null |
| 8429 |
|
| 8430 |
elsif Known_Non_Null (N) then |
| 8431 |
return; |
| 8432 |
end if; |
| 8433 |
|
| 8434 |
-- If known to be null, here is where we generate a compile time check |
| 8435 |
|
| 8436 |
if Known_Null (N) then |
| 8437 |
|
| 8438 |
-- Avoid generating warning message inside init procs. In SPARK mode |
| 8439 |
-- we can go ahead and call Apply_Compile_Time_Constraint_Error |
| 8440 |
-- since it will be turned into an error in any case. |
| 8441 |
|
| 8442 |
if (not Inside_Init_Proc or else SPARK_Mode = On) |
| 8443 |
|
| 8444 |
-- Do not emit the warning within a conditional expression, |
| 8445 |
-- where the expression might not be evaluated, and the warning |
| 8446 |
-- appear as extraneous noise. |
| 8447 |
|
| 8448 |
and then not Within_Conditional_Expression (N) |
| 8449 |
then |
| 8450 |
Apply_Compile_Time_Constraint_Error |
| 8451 |
(N, "null value not allowed here??", CE_Access_Check_Failed); |
| 8452 |
|
| 8453 |
-- Remaining cases, where we silently insert the raise |
| 8454 |
|
| 8455 |
else |
| 8456 |
Insert_Action (N, |
| 8457 |
Make_Raise_Constraint_Error (Loc, |
| 8458 |
Reason => CE_Access_Check_Failed)); |
| 8459 |
end if; |
| 8460 |
|
| 8461 |
Mark_Non_Null; |
| 8462 |
return; |
| 8463 |
end if; |
| 8464 |
|
| 8465 |
-- If entity is never assigned, for sure a warning is appropriate |
| 8466 |
|
| 8467 |
if Is_Entity_Name (N) then |
| 8468 |
Check_Unset_Reference (N); |
| 8469 |
end if; |
| 8470 |
|
| 8471 |
-- No check needed if checks are suppressed on the range. Note that we |
| 8472 |
-- don't set Is_Known_Non_Null in this case (we could legitimately do |
| 8473 |
-- so, since the program is erroneous, but we don't like to casually |
| 8474 |
-- propagate such conclusions from erroneosity). |
| 8475 |
|
| 8476 |
if Access_Checks_Suppressed (Typ) then |
| 8477 |
return; |
| 8478 |
end if; |
| 8479 |
|
| 8480 |
-- No check needed for access to concurrent record types generated by |
| 8481 |
-- the expander. This is not just an optimization (though it does indeed |
| 8482 |
-- remove junk checks). It also avoids generation of junk warnings. |
| 8483 |
|
| 8484 |
if Nkind (N) in N_Has_Chars |
| 8485 |
and then Chars (N) = Name_uObject |
| 8486 |
and then Is_Concurrent_Record_Type |
| 8487 |
(Directly_Designated_Type (Etype (N))) |
| 8488 |
then |
| 8489 |
return; |
| 8490 |
end if; |
| 8491 |
|
| 8492 |
-- No check needed in interface thunks since the runtime check is |
| 8493 |
-- already performed at the caller side. |
| 8494 |
|
| 8495 |
if Is_Thunk (Current_Scope) then |
| 8496 |
return; |
| 8497 |
end if; |
| 8498 |
|
| 8499 |
-- In GNATprove mode, we do not apply the check |
| 8500 |
|
| 8501 |
if GNATprove_Mode then |
| 8502 |
return; |
| 8503 |
end if; |
| 8504 |
|
| 8505 |
-- Otherwise install access check |
| 8506 |
|
| 8507 |
Insert_Action (N, |
| 8508 |
Make_Raise_Constraint_Error (Loc, |
| 8509 |
Condition => |
| 8510 |
Make_Op_Eq (Loc, |
| 8511 |
Left_Opnd => Duplicate_Subexpr_Move_Checks (N), |
| 8512 |
Right_Opnd => Make_Null (Loc)), |
| 8513 |
Reason => CE_Access_Check_Failed)); |
| 8514 |
|
| 8515 |
-- Mark the entity of N "non-null" except when assertions are enabled - |
| 8516 |
-- since expansion becomes much more complicated (especially when it |
| 8517 |
-- comes to contracts) due to the generation of wrappers and wholesale |
| 8518 |
-- moving of declarations and statements which may happen. |
| 8519 |
|
| 8520 |
-- Additionally, it is assumed that extra checks will exist with |
| 8521 |
-- assertions enabled so some potentially redundant checks are |
| 8522 |
-- acceptable. |
| 8523 |
|
| 8524 |
if not Assertions_Enabled then |
| 8525 |
Mark_Non_Null; |
| 8526 |
end if; |
| 8527 |
end Install_Null_Excluding_Check; |
| 8528 |
|
| 8529 |
----------------------------------------- |
| 8530 |
-- Install_Primitive_Elaboration_Check -- |
| 8531 |
----------------------------------------- |
| 8532 |
|
| 8533 |
procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id) is |
| 8534 |
function Within_Compilation_Unit_Instance |
| 8535 |
(Subp_Id : Entity_Id) return Boolean; |
| 8536 |
-- Determine whether subprogram Subp_Id appears within an instance which |
| 8537 |
-- acts as a compilation unit. |
| 8538 |
|
| 8539 |
-------------------------------------- |
| 8540 |
-- Within_Compilation_Unit_Instance -- |
| 8541 |
-------------------------------------- |
| 8542 |
|
| 8543 |
function Within_Compilation_Unit_Instance |
| 8544 |
(Subp_Id : Entity_Id) return Boolean |
| 8545 |
is |
| 8546 |
Pack : Entity_Id; |
| 8547 |
|
| 8548 |
begin |
| 8549 |
-- Examine the scope chain looking for a compilation-unit-level |
| 8550 |
-- instance. |
| 8551 |
|
| 8552 |
Pack := Scope (Subp_Id); |
| 8553 |
while Present (Pack) and then Pack /= Standard_Standard loop |
| 8554 |
if Ekind (Pack) = E_Package |
| 8555 |
and then Is_Generic_Instance (Pack) |
| 8556 |
and then Nkind (Parent (Unit_Declaration_Node (Pack))) = |
| 8557 |
N_Compilation_Unit |
| 8558 |
then |
| 8559 |
return True; |
| 8560 |
end if; |
| 8561 |
|
| 8562 |
Pack := Scope (Pack); |
| 8563 |
end loop; |
| 8564 |
|
| 8565 |
return False; |
| 8566 |
end Within_Compilation_Unit_Instance; |
| 8567 |
|
| 8568 |
-- Local declarations |
| 8569 |
|
| 8570 |
Context : constant Node_Id := Parent (Subp_Body); |
| 8571 |
Loc : constant Source_Ptr := Sloc (Subp_Body); |
| 8572 |
Subp_Id : constant Entity_Id := Unique_Defining_Entity (Subp_Body); |
| 8573 |
Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id); |
| 8574 |
|
| 8575 |
Decls : List_Id; |
| 8576 |
Flag_Id : Entity_Id; |
| 8577 |
Set_Ins : Node_Id; |
| 8578 |
Set_Stmt : Node_Id; |
| 8579 |
Tag_Typ : Entity_Id; |
| 8580 |
|
| 8581 |
-- Start of processing for Install_Primitive_Elaboration_Check |
| 8582 |
|
| 8583 |
begin |
| 8584 |
-- Do not generate an elaboration check in compilation modes where |
| 8585 |
-- expansion is not desirable. |
| 8586 |
|
| 8587 |
if GNATprove_Mode then |
| 8588 |
return; |
| 8589 |
|
| 8590 |
-- No check during preanalysis |
| 8591 |
|
| 8592 |
elsif Preanalysis_Active then |
| 8593 |
return; |
| 8594 |
|
| 8595 |
-- Do not generate an elaboration check if all checks have been |
| 8596 |
-- suppressed. |
| 8597 |
|
| 8598 |
elsif Suppress_Checks then |
| 8599 |
return; |
| 8600 |
|
| 8601 |
-- Do not generate an elaboration check if the related subprogram is |
| 8602 |
-- not subject to elaboration checks. |
| 8603 |
|
| 8604 |
elsif Elaboration_Checks_Suppressed (Subp_Id) then |
| 8605 |
return; |
| 8606 |
|
| 8607 |
-- Do not generate an elaboration check if such code is not desirable |
| 8608 |
|
| 8609 |
elsif Restriction_Active (No_Elaboration_Code) then |
| 8610 |
return; |
| 8611 |
|
| 8612 |
-- If pragma Pure or Preelaborate applies, then these elaboration checks |
| 8613 |
-- cannot fail, so do not generate them. |
| 8614 |
|
| 8615 |
elsif In_Preelaborated_Unit then |
| 8616 |
return; |
| 8617 |
|
| 8618 |
-- Do not generate an elaboration check if exceptions cannot be used, |
| 8619 |
-- caught, or propagated. |
| 8620 |
|
| 8621 |
elsif not Exceptions_OK then |
| 8622 |
return; |
| 8623 |
|
| 8624 |
-- Do not consider subprograms that are compilation units, because they |
| 8625 |
-- cannot be the target of a dispatching call. |
| 8626 |
|
| 8627 |
elsif Nkind (Context) = N_Compilation_Unit then |
| 8628 |
return; |
| 8629 |
|
| 8630 |
-- Do not consider anything other than nonabstract library-level source |
| 8631 |
-- primitives. |
| 8632 |
|
| 8633 |
elsif not |
| 8634 |
(Comes_From_Source (Subp_Id) |
| 8635 |
and then Is_Library_Level_Entity (Subp_Id) |
| 8636 |
and then Is_Primitive (Subp_Id) |
| 8637 |
and then not Is_Abstract_Subprogram (Subp_Id)) |
| 8638 |
then |
| 8639 |
return; |
| 8640 |
|
| 8641 |
-- Do not consider inlined primitives, because once the body is inlined |
| 8642 |
-- the reference to the elaboration flag will be out of place and will |
| 8643 |
-- result in an undefined symbol. |
| 8644 |
|
| 8645 |
elsif Is_Inlined (Subp_Id) or else Has_Pragma_Inline (Subp_Id) then |
| 8646 |
return; |
| 8647 |
|
| 8648 |
-- Do not generate a duplicate elaboration check. This happens only in |
| 8649 |
-- the case of primitives completed by an expression function, as the |
| 8650 |
-- corresponding body is apparently analyzed and expanded twice. |
| 8651 |
|
| 8652 |
elsif Analyzed (Subp_Body) then |
| 8653 |
return; |
| 8654 |
|
| 8655 |
-- Do not consider primitives that occur within an instance that is a |
| 8656 |
-- compilation unit. Such an instance defines its spec and body out of |
| 8657 |
-- order (body is first) within the tree, which causes the reference to |
| 8658 |
-- the elaboration flag to appear as an undefined symbol. |
| 8659 |
|
| 8660 |
elsif Within_Compilation_Unit_Instance (Subp_Id) then |
| 8661 |
return; |
| 8662 |
end if; |
| 8663 |
|
| 8664 |
Tag_Typ := Find_Dispatching_Type (Subp_Id); |
| 8665 |
|
| 8666 |
-- Only tagged primitives may be the target of a dispatching call |
| 8667 |
|
| 8668 |
if No (Tag_Typ) then |
| 8669 |
return; |
| 8670 |
|
| 8671 |
-- Do not consider finalization-related primitives, because they may |
| 8672 |
-- need to be called while elaboration is taking place. |
| 8673 |
|
| 8674 |
elsif Is_Controlled (Tag_Typ) |
| 8675 |
and then (Is_Controlled_Procedure (Subp_Id, Name_Adjust) |
| 8676 |
or else Is_Controlled_Procedure (Subp_Id, Name_Finalize) |
| 8677 |
or else Is_Controlled_Procedure (Subp_Id, Name_Initialize)) |
| 8678 |
then |
| 8679 |
return; |
| 8680 |
end if; |
| 8681 |
|
| 8682 |
-- Create the declaration of the elaboration flag. The name carries a |
| 8683 |
-- unique counter in case of name overloading. |
| 8684 |
|
| 8685 |
Flag_Id := |
| 8686 |
Make_Defining_Identifier (Loc, |
| 8687 |
Chars => New_External_Name (Chars (Subp_Id), 'E', -1)); |
| 8688 |
Set_Is_Frozen (Flag_Id); |
| 8689 |
|
| 8690 |
-- Insert the declaration of the elaboration flag in front of the |
| 8691 |
-- primitive spec and analyze it in the proper context. |
| 8692 |
|
| 8693 |
Push_Scope (Scope (Subp_Id)); |
| 8694 |
|
| 8695 |
-- Generate: |
| 8696 |
-- E : Boolean := False; |
| 8697 |
|
| 8698 |
Insert_Action (Subp_Decl, |
| 8699 |
Make_Object_Declaration (Loc, |
| 8700 |
Defining_Identifier => Flag_Id, |
| 8701 |
Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), |
| 8702 |
Expression => New_Occurrence_Of (Standard_False, Loc))); |
| 8703 |
Pop_Scope; |
| 8704 |
|
| 8705 |
-- Prevent the compiler from optimizing the elaboration check by killing |
| 8706 |
-- the current value of the flag and the associated assignment. |
| 8707 |
|
| 8708 |
Set_Current_Value (Flag_Id, Empty); |
| 8709 |
Set_Last_Assignment (Flag_Id, Empty); |
| 8710 |
|
| 8711 |
-- Add a check at the top of the body declarations to ensure that the |
| 8712 |
-- elaboration flag has been set. |
| 8713 |
|
| 8714 |
Decls := Declarations (Subp_Body); |
| 8715 |
|
| 8716 |
if No (Decls) then |
| 8717 |
Decls := New_List; |
| 8718 |
Set_Declarations (Subp_Body, Decls); |
| 8719 |
end if; |
| 8720 |
|
| 8721 |
-- Generate: |
| 8722 |
-- if not F then |
| 8723 |
-- raise Program_Error with "access before elaboration"; |
| 8724 |
-- end if; |
| 8725 |
|
| 8726 |
Prepend_To (Decls, |
| 8727 |
Make_Raise_Program_Error (Loc, |
| 8728 |
Condition => |
| 8729 |
Make_Op_Not (Loc, |
| 8730 |
Right_Opnd => New_Occurrence_Of (Flag_Id, Loc)), |
| 8731 |
Reason => PE_Access_Before_Elaboration)); |
| 8732 |
|
| 8733 |
Analyze (First (Decls)); |
| 8734 |
|
| 8735 |
-- Set the elaboration flag once the body has been elaborated. Insert |
| 8736 |
-- the statement after the subprogram stub when the primitive body is |
| 8737 |
-- a subunit. |
| 8738 |
|
| 8739 |
if Nkind (Context) = N_Subunit then |
| 8740 |
Set_Ins := Corresponding_Stub (Context); |
| 8741 |
else |
| 8742 |
Set_Ins := Subp_Body; |
| 8743 |
end if; |
| 8744 |
|
| 8745 |
-- Generate: |
| 8746 |
-- E := True; |
| 8747 |
|
| 8748 |
Set_Stmt := |
| 8749 |
Make_Assignment_Statement (Loc, |
| 8750 |
Name => New_Occurrence_Of (Flag_Id, Loc), |
| 8751 |
Expression => New_Occurrence_Of (Standard_True, Loc)); |
| 8752 |
|
| 8753 |
-- Mark the assignment statement as elaboration code. This allows the |
| 8754 |
-- early call region mechanism (see Sem_Elab) to properly ignore such |
| 8755 |
-- assignments even though they are non-preelaborable code. |
| 8756 |
|
| 8757 |
Set_Is_Elaboration_Code (Set_Stmt); |
| 8758 |
|
| 8759 |
Insert_After_And_Analyze (Set_Ins, Set_Stmt); |
| 8760 |
end Install_Primitive_Elaboration_Check; |
| 8761 |
|
| 8762 |
-------------------------- |
| 8763 |
-- Install_Static_Check -- |
| 8764 |
-------------------------- |
| 8765 |
|
| 8766 |
procedure Install_Static_Check |
| 8767 |
(R_Cno : Node_Id; Loc : Source_Ptr; Reason : RT_Exception_Code) |
| 8768 |
is |
| 8769 |
Stat : constant Boolean := Is_OK_Static_Expression (R_Cno); |
| 8770 |
Typ : constant Entity_Id := Etype (R_Cno); |
| 8771 |
|
| 8772 |
begin |
| 8773 |
Rewrite (R_Cno, |
| 8774 |
Make_Raise_Constraint_Error (Loc, |
| 8775 |
Reason => Reason)); |
| 8776 |
Set_Analyzed (R_Cno); |
| 8777 |
Set_Etype (R_Cno, Typ); |
| 8778 |
Set_Raises_Constraint_Error (R_Cno); |
| 8779 |
Set_Is_Static_Expression (R_Cno, Stat); |
| 8780 |
|
| 8781 |
-- Now deal with possible local raise handling |
| 8782 |
|
| 8783 |
Possible_Local_Raise (R_Cno, Standard_Constraint_Error); |
| 8784 |
end Install_Static_Check; |
| 8785 |
|
| 8786 |
------------------------- |
| 8787 |
-- Is_Check_Suppressed -- |
| 8788 |
------------------------- |
| 8789 |
|
| 8790 |
function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is |
| 8791 |
Ptr : Suppress_Stack_Entry_Ptr; |
| 8792 |
|
| 8793 |
begin |
| 8794 |
-- First search the local entity suppress stack. We search this from the |
| 8795 |
-- top of the stack down so that we get the innermost entry that applies |
| 8796 |
-- to this case if there are nested entries. |
| 8797 |
|
| 8798 |
Ptr := Local_Suppress_Stack_Top; |
| 8799 |
while Ptr /= null loop |
| 8800 |
if (Ptr.Entity = Empty or else Ptr.Entity = E) |
| 8801 |
and then (Ptr.Check = All_Checks or else Ptr.Check = C) |
| 8802 |
then |
| 8803 |
return Ptr.Suppress; |
| 8804 |
end if; |
| 8805 |
|
| 8806 |
Ptr := Ptr.Prev; |
| 8807 |
end loop; |
| 8808 |
|
| 8809 |
-- Now search the global entity suppress table for a matching entry. |
| 8810 |
-- We also search this from the top down so that if there are multiple |
| 8811 |
-- pragmas for the same entity, the last one applies (not clear what |
| 8812 |
-- or whether the RM specifies this handling, but it seems reasonable). |
| 8813 |
|
| 8814 |
Ptr := Global_Suppress_Stack_Top; |
| 8815 |
while Ptr /= null loop |
| 8816 |
if (Ptr.Entity = Empty or else Ptr.Entity = E) |
| 8817 |
and then (Ptr.Check = All_Checks or else Ptr.Check = C) |
| 8818 |
then |
| 8819 |
return Ptr.Suppress; |
| 8820 |
end if; |
| 8821 |
|
| 8822 |
Ptr := Ptr.Prev; |
| 8823 |
end loop; |
| 8824 |
|
| 8825 |
-- If we did not find a matching entry, then use the normal scope |
| 8826 |
-- suppress value after all (actually this will be the global setting |
| 8827 |
-- since it clearly was not overridden at any point). For a predefined |
| 8828 |
-- check, we test the specific flag. For a user defined check, we check |
| 8829 |
-- the All_Checks flag. The Overflow flag requires special handling to |
| 8830 |
-- deal with the General vs Assertion case. |
| 8831 |
|
| 8832 |
if C = Overflow_Check then |
| 8833 |
return Overflow_Checks_Suppressed (Empty); |
| 8834 |
|
| 8835 |
elsif C in Predefined_Check_Id then |
| 8836 |
return Scope_Suppress.Suppress (C); |
| 8837 |
|
| 8838 |
else |
| 8839 |
return Scope_Suppress.Suppress (All_Checks); |
| 8840 |
end if; |
| 8841 |
end Is_Check_Suppressed; |
| 8842 |
|
| 8843 |
--------------------- |
| 8844 |
-- Kill_All_Checks -- |
| 8845 |
--------------------- |
| 8846 |
|
| 8847 |
procedure Kill_All_Checks is |
| 8848 |
begin |
| 8849 |
if Debug_Flag_CC then |
| 8850 |
w ("Kill_All_Checks"); |
| 8851 |
end if; |
| 8852 |
|
| 8853 |
-- We reset the number of saved checks to zero, and also modify all |
| 8854 |
-- stack entries for statement ranges to indicate that the number of |
| 8855 |
-- checks at each level is now zero. |
| 8856 |
|
| 8857 |
Num_Saved_Checks := 0; |
| 8858 |
|
| 8859 |
-- Note: the Int'Min here avoids any possibility of J being out of |
| 8860 |
-- range when called from e.g. Conditional_Statements_Begin. |
| 8861 |
|
| 8862 |
for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop |
| 8863 |
Saved_Checks_Stack (J) := 0; |
| 8864 |
end loop; |
| 8865 |
end Kill_All_Checks; |
| 8866 |
|
| 8867 |
----------------- |
| 8868 |
-- Kill_Checks -- |
| 8869 |
----------------- |
| 8870 |
|
| 8871 |
procedure Kill_Checks (V : Entity_Id) is |
| 8872 |
begin |
| 8873 |
if Debug_Flag_CC then |
| 8874 |
w ("Kill_Checks for entity", Int (V)); |
| 8875 |
end if; |
| 8876 |
|
| 8877 |
for J in 1 .. Num_Saved_Checks loop |
| 8878 |
if Saved_Checks (J).Entity = V then |
| 8879 |
if Debug_Flag_CC then |
| 8880 |
w (" Checks killed for saved check ", J); |
| 8881 |
end if; |
| 8882 |
|
| 8883 |
Saved_Checks (J).Killed := True; |
| 8884 |
end if; |
| 8885 |
end loop; |
| 8886 |
end Kill_Checks; |
| 8887 |
|
| 8888 |
------------------------------ |
| 8889 |
-- Length_Checks_Suppressed -- |
| 8890 |
------------------------------ |
| 8891 |
|
| 8892 |
function Length_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 8893 |
begin |
| 8894 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 8895 |
return Is_Check_Suppressed (E, Length_Check); |
| 8896 |
else |
| 8897 |
return Scope_Suppress.Suppress (Length_Check); |
| 8898 |
end if; |
| 8899 |
end Length_Checks_Suppressed; |
| 8900 |
|
| 8901 |
----------------------- |
| 8902 |
-- Make_Bignum_Block -- |
| 8903 |
----------------------- |
| 8904 |
|
| 8905 |
function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is |
| 8906 |
M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); |
| 8907 |
begin |
| 8908 |
return |
| 8909 |
Make_Block_Statement (Loc, |
| 8910 |
Declarations => |
| 8911 |
New_List (Build_SS_Mark_Call (Loc, M)), |
| 8912 |
Handled_Statement_Sequence => |
| 8913 |
Make_Handled_Sequence_Of_Statements (Loc, |
| 8914 |
Statements => New_List (Build_SS_Release_Call (Loc, M)))); |
| 8915 |
end Make_Bignum_Block; |
| 8916 |
|
| 8917 |
---------------------------------- |
| 8918 |
-- Minimize_Eliminate_Overflows -- |
| 8919 |
---------------------------------- |
| 8920 |
|
| 8921 |
-- This is a recursive routine that is called at the top of an expression |
| 8922 |
-- tree to properly process overflow checking for a whole subtree by making |
| 8923 |
-- recursive calls to process operands. This processing may involve the use |
| 8924 |
-- of bignum or long long integer arithmetic, which will change the types |
| 8925 |
-- of operands and results. That's why we can't do this bottom up (since |
| 8926 |
-- it would interfere with semantic analysis). |
| 8927 |
|
| 8928 |
-- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
| 8929 |
-- the operator expansion routines, as well as the expansion routines for |
| 8930 |
-- if/case expression, do nothing (for the moment) except call the routine |
| 8931 |
-- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That |
| 8932 |
-- routine does nothing for non top-level nodes, so at the point where the |
| 8933 |
-- call is made for the top level node, the entire expression subtree has |
| 8934 |
-- not been expanded, or processed for overflow. All that has to happen as |
| 8935 |
-- a result of the top level call to this routine. |
| 8936 |
|
| 8937 |
-- As noted above, the overflow processing works by making recursive calls |
| 8938 |
-- for the operands, and figuring out what to do, based on the processing |
| 8939 |
-- of these operands (e.g. if a bignum operand appears, the parent op has |
| 8940 |
-- to be done in bignum mode), and the determined ranges of the operands. |
| 8941 |
|
| 8942 |
-- After possible rewriting of a constituent subexpression node, a call is |
| 8943 |
-- made to either reexpand the node (if nothing has changed) or reanalyze |
| 8944 |
-- the node (if it has been modified by the overflow check processing). The |
| 8945 |
-- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid |
| 8946 |
-- a recursive call into the whole overflow apparatus, an important rule |
| 8947 |
-- for this call is that the overflow handling mode must be temporarily set |
| 8948 |
-- to STRICT. |
| 8949 |
|
| 8950 |
procedure Minimize_Eliminate_Overflows |
| 8951 |
(N : Node_Id; |
| 8952 |
Lo : out Uint; |
| 8953 |
Hi : out Uint; |
| 8954 |
Top_Level : Boolean) |
| 8955 |
is |
| 8956 |
Rtyp : constant Entity_Id := Etype (N); |
| 8957 |
pragma Assert (Is_Signed_Integer_Type (Rtyp)); |
| 8958 |
-- Result type, must be a signed integer type |
| 8959 |
|
| 8960 |
Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
| 8961 |
pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
| 8962 |
|
| 8963 |
Loc : constant Source_Ptr := Sloc (N); |
| 8964 |
|
| 8965 |
Rlo, Rhi : Uint; |
| 8966 |
-- Ranges of values for right operand (operator case) |
| 8967 |
|
| 8968 |
Llo : Uint := No_Uint; -- initialize to prevent warning |
| 8969 |
Lhi : Uint := No_Uint; -- initialize to prevent warning |
| 8970 |
-- Ranges of values for left operand (operator case) |
| 8971 |
|
| 8972 |
LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
| 8973 |
-- Operands and results are of this type when we convert |
| 8974 |
|
| 8975 |
LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
| 8976 |
LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); |
| 8977 |
-- Bounds of Long_Long_Integer |
| 8978 |
|
| 8979 |
Binary : constant Boolean := Nkind (N) in N_Binary_Op; |
| 8980 |
-- Indicates binary operator case |
| 8981 |
|
| 8982 |
OK : Boolean; |
| 8983 |
-- Used in call to Determine_Range |
| 8984 |
|
| 8985 |
Bignum_Operands : Boolean; |
| 8986 |
-- Set True if one or more operands is already of type Bignum, meaning |
| 8987 |
-- that for sure (regardless of Top_Level setting) we are committed to |
| 8988 |
-- doing the operation in Bignum mode (or in the case of a case or if |
| 8989 |
-- expression, converting all the dependent expressions to Bignum). |
| 8990 |
|
| 8991 |
Long_Long_Integer_Operands : Boolean; |
| 8992 |
-- Set True if one or more operands is already of type Long_Long_Integer |
| 8993 |
-- which means that if the result is known to be in the result type |
| 8994 |
-- range, then we must convert such operands back to the result type. |
| 8995 |
|
| 8996 |
procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); |
| 8997 |
-- This is called when we have modified the node and we therefore need |
| 8998 |
-- to reanalyze it. It is important that we reset the mode to STRICT for |
| 8999 |
-- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode |
| 9000 |
-- we would reenter this routine recursively which would not be good. |
| 9001 |
-- The argument Suppress is set True if we also want to suppress |
| 9002 |
-- overflow checking for the reexpansion (this is set when we know |
| 9003 |
-- overflow is not possible). Typ is the type for the reanalysis. |
| 9004 |
|
| 9005 |
procedure Reexpand (Suppress : Boolean := False); |
| 9006 |
-- This is like Reanalyze, but does not do the Analyze step, it only |
| 9007 |
-- does a reexpansion. We do this reexpansion in STRICT mode, so that |
| 9008 |
-- instead of reentering the MINIMIZED/ELIMINATED mode processing, we |
| 9009 |
-- follow the normal expansion path (e.g. converting A**4 to A**2**2). |
| 9010 |
-- Note that skipping reanalysis is not just an optimization, testing |
| 9011 |
-- has showed up several complex cases in which reanalyzing an already |
| 9012 |
-- analyzed node causes incorrect behavior. |
| 9013 |
|
| 9014 |
function In_Result_Range return Boolean; |
| 9015 |
-- Returns True iff Lo .. Hi are within range of the result type |
| 9016 |
|
| 9017 |
procedure Max (A : in out Uint; B : Uint); |
| 9018 |
-- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
| 9019 |
|
| 9020 |
procedure Min (A : in out Uint; B : Uint); |
| 9021 |
-- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
| 9022 |
|
| 9023 |
--------------------- |
| 9024 |
-- In_Result_Range -- |
| 9025 |
--------------------- |
| 9026 |
|
| 9027 |
function In_Result_Range return Boolean is |
| 9028 |
begin |
| 9029 |
if No (Lo) or else No (Hi) then |
| 9030 |
return False; |
| 9031 |
|
| 9032 |
elsif Is_OK_Static_Subtype (Etype (N)) then |
| 9033 |
return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
| 9034 |
and then |
| 9035 |
Hi <= Expr_Value (Type_High_Bound (Rtyp)); |
| 9036 |
|
| 9037 |
else |
| 9038 |
return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) |
| 9039 |
and then |
| 9040 |
Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); |
| 9041 |
end if; |
| 9042 |
end In_Result_Range; |
| 9043 |
|
| 9044 |
--------- |
| 9045 |
-- Max -- |
| 9046 |
--------- |
| 9047 |
|
| 9048 |
procedure Max (A : in out Uint; B : Uint) is |
| 9049 |
begin |
| 9050 |
if No (A) or else B > A then |
| 9051 |
A := B; |
| 9052 |
end if; |
| 9053 |
end Max; |
| 9054 |
|
| 9055 |
--------- |
| 9056 |
-- Min -- |
| 9057 |
--------- |
| 9058 |
|
| 9059 |
procedure Min (A : in out Uint; B : Uint) is |
| 9060 |
begin |
| 9061 |
if No (A) or else B < A then |
| 9062 |
A := B; |
| 9063 |
end if; |
| 9064 |
end Min; |
| 9065 |
|
| 9066 |
--------------- |
| 9067 |
-- Reanalyze -- |
| 9068 |
--------------- |
| 9069 |
|
| 9070 |
procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is |
| 9071 |
Svg : constant Overflow_Mode_Type := |
| 9072 |
Scope_Suppress.Overflow_Mode_General; |
| 9073 |
Sva : constant Overflow_Mode_Type := |
| 9074 |
Scope_Suppress.Overflow_Mode_Assertions; |
| 9075 |
Svo : constant Boolean := |
| 9076 |
Scope_Suppress.Suppress (Overflow_Check); |
| 9077 |
|
| 9078 |
begin |
| 9079 |
Scope_Suppress.Overflow_Mode_General := Strict; |
| 9080 |
Scope_Suppress.Overflow_Mode_Assertions := Strict; |
| 9081 |
|
| 9082 |
if Suppress then |
| 9083 |
Scope_Suppress.Suppress (Overflow_Check) := True; |
| 9084 |
end if; |
| 9085 |
|
| 9086 |
Analyze_And_Resolve (N, Typ); |
| 9087 |
|
| 9088 |
Scope_Suppress.Suppress (Overflow_Check) := Svo; |
| 9089 |
Scope_Suppress.Overflow_Mode_General := Svg; |
| 9090 |
Scope_Suppress.Overflow_Mode_Assertions := Sva; |
| 9091 |
end Reanalyze; |
| 9092 |
|
| 9093 |
-------------- |
| 9094 |
-- Reexpand -- |
| 9095 |
-------------- |
| 9096 |
|
| 9097 |
procedure Reexpand (Suppress : Boolean := False) is |
| 9098 |
Svg : constant Overflow_Mode_Type := |
| 9099 |
Scope_Suppress.Overflow_Mode_General; |
| 9100 |
Sva : constant Overflow_Mode_Type := |
| 9101 |
Scope_Suppress.Overflow_Mode_Assertions; |
| 9102 |
Svo : constant Boolean := |
| 9103 |
Scope_Suppress.Suppress (Overflow_Check); |
| 9104 |
|
| 9105 |
begin |
| 9106 |
Scope_Suppress.Overflow_Mode_General := Strict; |
| 9107 |
Scope_Suppress.Overflow_Mode_Assertions := Strict; |
| 9108 |
Set_Analyzed (N, False); |
| 9109 |
|
| 9110 |
if Suppress then |
| 9111 |
Scope_Suppress.Suppress (Overflow_Check) := True; |
| 9112 |
end if; |
| 9113 |
|
| 9114 |
Expand (N); |
| 9115 |
|
| 9116 |
Scope_Suppress.Suppress (Overflow_Check) := Svo; |
| 9117 |
Scope_Suppress.Overflow_Mode_General := Svg; |
| 9118 |
Scope_Suppress.Overflow_Mode_Assertions := Sva; |
| 9119 |
end Reexpand; |
| 9120 |
|
| 9121 |
-- Start of processing for Minimize_Eliminate_Overflows |
| 9122 |
|
| 9123 |
begin |
| 9124 |
-- Default initialize Lo and Hi since these are not guaranteed to be |
| 9125 |
-- set otherwise. |
| 9126 |
|
| 9127 |
Lo := No_Uint; |
| 9128 |
Hi := No_Uint; |
| 9129 |
|
| 9130 |
-- Case where we do not have a signed integer arithmetic operation |
| 9131 |
|
| 9132 |
if not Is_Signed_Integer_Arithmetic_Op (N) then |
| 9133 |
|
| 9134 |
-- Use the normal Determine_Range routine to get the range. We |
| 9135 |
-- don't require operands to be valid, invalid values may result in |
| 9136 |
-- rubbish results where the result has not been properly checked for |
| 9137 |
-- overflow, that's fine. |
| 9138 |
|
| 9139 |
Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); |
| 9140 |
|
| 9141 |
-- If Determine_Range did not work (can this in fact happen? Not |
| 9142 |
-- clear but might as well protect), use type bounds. |
| 9143 |
|
| 9144 |
if not OK then |
| 9145 |
Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); |
| 9146 |
Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); |
| 9147 |
end if; |
| 9148 |
|
| 9149 |
-- If we don't have a binary operator, all we have to do is to set |
| 9150 |
-- the Hi/Lo range, so we are done. |
| 9151 |
|
| 9152 |
return; |
| 9153 |
|
| 9154 |
-- Processing for if expression |
| 9155 |
|
| 9156 |
elsif Nkind (N) = N_If_Expression then |
| 9157 |
declare |
| 9158 |
Then_DE : constant Node_Id := Next (First (Expressions (N))); |
| 9159 |
Else_DE : constant Node_Id := Next (Then_DE); |
| 9160 |
|
| 9161 |
begin |
| 9162 |
Bignum_Operands := False; |
| 9163 |
|
| 9164 |
Minimize_Eliminate_Overflows |
| 9165 |
(Then_DE, Lo, Hi, Top_Level => False); |
| 9166 |
|
| 9167 |
if No (Lo) then |
| 9168 |
Bignum_Operands := True; |
| 9169 |
end if; |
| 9170 |
|
| 9171 |
Minimize_Eliminate_Overflows |
| 9172 |
(Else_DE, Rlo, Rhi, Top_Level => False); |
| 9173 |
|
| 9174 |
if No (Rlo) then |
| 9175 |
Bignum_Operands := True; |
| 9176 |
else |
| 9177 |
Long_Long_Integer_Operands := |
| 9178 |
Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; |
| 9179 |
|
| 9180 |
Min (Lo, Rlo); |
| 9181 |
Max (Hi, Rhi); |
| 9182 |
end if; |
| 9183 |
|
| 9184 |
-- If at least one of our operands is now Bignum, we must rebuild |
| 9185 |
-- the if expression to use Bignum operands. We will analyze the |
| 9186 |
-- rebuilt if expression with overflow checks off, since once we |
| 9187 |
-- are in bignum mode, we are all done with overflow checks. |
| 9188 |
|
| 9189 |
if Bignum_Operands then |
| 9190 |
Rewrite (N, |
| 9191 |
Make_If_Expression (Loc, |
| 9192 |
Expressions => New_List ( |
| 9193 |
Remove_Head (Expressions (N)), |
| 9194 |
Convert_To_Bignum (Then_DE), |
| 9195 |
Convert_To_Bignum (Else_DE)), |
| 9196 |
Is_Elsif => Is_Elsif (N))); |
| 9197 |
|
| 9198 |
Reanalyze (RTE (RE_Bignum), Suppress => True); |
| 9199 |
|
| 9200 |
-- If we have no Long_Long_Integer operands, then we are in result |
| 9201 |
-- range, since it means that none of our operands felt the need |
| 9202 |
-- to worry about overflow (otherwise it would have already been |
| 9203 |
-- converted to long long integer or bignum). We reexpand to |
| 9204 |
-- complete the expansion of the if expression (but we do not |
| 9205 |
-- need to reanalyze). |
| 9206 |
|
| 9207 |
elsif not Long_Long_Integer_Operands then |
| 9208 |
Set_Do_Overflow_Check (N, False); |
| 9209 |
Reexpand; |
| 9210 |
|
| 9211 |
-- Otherwise convert us to long long integer mode. Note that we |
| 9212 |
-- don't need any further overflow checking at this level. |
| 9213 |
|
| 9214 |
else |
| 9215 |
Convert_To_And_Rewrite (LLIB, Then_DE); |
| 9216 |
Convert_To_And_Rewrite (LLIB, Else_DE); |
| 9217 |
Set_Etype (N, LLIB); |
| 9218 |
|
| 9219 |
-- Now reanalyze with overflow checks off |
| 9220 |
|
| 9221 |
Set_Do_Overflow_Check (N, False); |
| 9222 |
Reanalyze (LLIB, Suppress => True); |
| 9223 |
end if; |
| 9224 |
end; |
| 9225 |
|
| 9226 |
return; |
| 9227 |
|
| 9228 |
-- Here for case expression |
| 9229 |
|
| 9230 |
elsif Nkind (N) = N_Case_Expression then |
| 9231 |
Bignum_Operands := False; |
| 9232 |
Long_Long_Integer_Operands := False; |
| 9233 |
|
| 9234 |
declare |
| 9235 |
Alt : Node_Id; |
| 9236 |
|
| 9237 |
begin |
| 9238 |
-- Loop through expressions applying recursive call |
| 9239 |
|
| 9240 |
Alt := First (Alternatives (N)); |
| 9241 |
while Present (Alt) loop |
| 9242 |
declare |
| 9243 |
Aexp : constant Node_Id := Expression (Alt); |
| 9244 |
|
| 9245 |
begin |
| 9246 |
Minimize_Eliminate_Overflows |
| 9247 |
(Aexp, Lo, Hi, Top_Level => False); |
| 9248 |
|
| 9249 |
if No (Lo) then |
| 9250 |
Bignum_Operands := True; |
| 9251 |
elsif Etype (Aexp) = LLIB then |
| 9252 |
Long_Long_Integer_Operands := True; |
| 9253 |
end if; |
| 9254 |
end; |
| 9255 |
|
| 9256 |
Next (Alt); |
| 9257 |
end loop; |
| 9258 |
|
| 9259 |
-- If we have no bignum or long long integer operands, it means |
| 9260 |
-- that none of our dependent expressions could raise overflow. |
| 9261 |
-- In this case, we simply return with no changes except for |
| 9262 |
-- resetting the overflow flag, since we are done with overflow |
| 9263 |
-- checks for this node. We will reexpand to get the needed |
| 9264 |
-- expansion for the case expression, but we do not need to |
| 9265 |
-- reanalyze, since nothing has changed. |
| 9266 |
|
| 9267 |
if not (Bignum_Operands or Long_Long_Integer_Operands) then |
| 9268 |
Set_Do_Overflow_Check (N, False); |
| 9269 |
Reexpand (Suppress => True); |
| 9270 |
|
| 9271 |
-- Otherwise we are going to rebuild the case expression using |
| 9272 |
-- either bignum or long long integer operands throughout. |
| 9273 |
|
| 9274 |
else |
| 9275 |
declare |
| 9276 |
Rtype : Entity_Id := Empty; |
| 9277 |
New_Alts : List_Id; |
| 9278 |
New_Exp : Node_Id; |
| 9279 |
|
| 9280 |
begin |
| 9281 |
New_Alts := New_List; |
| 9282 |
Alt := First (Alternatives (N)); |
| 9283 |
while Present (Alt) loop |
| 9284 |
if Bignum_Operands then |
| 9285 |
New_Exp := Convert_To_Bignum (Expression (Alt)); |
| 9286 |
Rtype := RTE (RE_Bignum); |
| 9287 |
else |
| 9288 |
New_Exp := Convert_To (LLIB, Expression (Alt)); |
| 9289 |
Rtype := LLIB; |
| 9290 |
end if; |
| 9291 |
|
| 9292 |
Append_To (New_Alts, |
| 9293 |
Make_Case_Expression_Alternative (Sloc (Alt), |
| 9294 |
Discrete_Choices => Discrete_Choices (Alt), |
| 9295 |
Expression => New_Exp)); |
| 9296 |
|
| 9297 |
Next (Alt); |
| 9298 |
end loop; |
| 9299 |
|
| 9300 |
Rewrite (N, |
| 9301 |
Make_Case_Expression (Loc, |
| 9302 |
Expression => Expression (N), |
| 9303 |
Alternatives => New_Alts)); |
| 9304 |
|
| 9305 |
pragma Assert (Present (Rtype)); |
| 9306 |
Reanalyze (Rtype, Suppress => True); |
| 9307 |
end; |
| 9308 |
end if; |
| 9309 |
end; |
| 9310 |
|
| 9311 |
return; |
| 9312 |
end if; |
| 9313 |
|
| 9314 |
-- If we have an arithmetic operator we make recursive calls on the |
| 9315 |
-- operands to get the ranges (and to properly process the subtree |
| 9316 |
-- that lies below us). |
| 9317 |
|
| 9318 |
Minimize_Eliminate_Overflows |
| 9319 |
(Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
| 9320 |
|
| 9321 |
if Binary then |
| 9322 |
Minimize_Eliminate_Overflows |
| 9323 |
(Left_Opnd (N), Llo, Lhi, Top_Level => False); |
| 9324 |
end if; |
| 9325 |
|
| 9326 |
-- Record if we have Long_Long_Integer operands |
| 9327 |
|
| 9328 |
Long_Long_Integer_Operands := |
| 9329 |
Etype (Right_Opnd (N)) = LLIB |
| 9330 |
or else (Binary and then Etype (Left_Opnd (N)) = LLIB); |
| 9331 |
|
| 9332 |
-- If either operand is a bignum, then result will be a bignum and we |
| 9333 |
-- don't need to do any range analysis. As previously discussed we could |
| 9334 |
-- do range analysis in such cases, but it could mean working with giant |
| 9335 |
-- numbers at compile time for very little gain (the number of cases |
| 9336 |
-- in which we could slip back from bignum mode is small). |
| 9337 |
|
| 9338 |
if No (Rlo) or else (Binary and then No (Llo)) then |
| 9339 |
Lo := No_Uint; |
| 9340 |
Hi := No_Uint; |
| 9341 |
Bignum_Operands := True; |
| 9342 |
|
| 9343 |
-- Otherwise compute result range |
| 9344 |
|
| 9345 |
else |
| 9346 |
Compute_Range_For_Arithmetic_Op |
| 9347 |
(Nkind (N), Llo, Lhi, Rlo, Rhi, OK, Lo, Hi); |
| 9348 |
Bignum_Operands := False; |
| 9349 |
end if; |
| 9350 |
|
| 9351 |
-- Here for the case where we have not rewritten anything (no bignum |
| 9352 |
-- operands or long long integer operands), and we know the result. |
| 9353 |
-- If we know we are in the result range, and we do not have Bignum |
| 9354 |
-- operands or Long_Long_Integer operands, we can just reexpand with |
| 9355 |
-- overflow checks turned off (since we know we cannot have overflow). |
| 9356 |
-- As always the reexpansion is required to complete expansion of the |
| 9357 |
-- operator, but we do not need to reanalyze, and we prevent recursion |
| 9358 |
-- by suppressing the check. |
| 9359 |
|
| 9360 |
if not (Bignum_Operands or Long_Long_Integer_Operands) |
| 9361 |
and then In_Result_Range |
| 9362 |
then |
| 9363 |
Set_Do_Overflow_Check (N, False); |
| 9364 |
Reexpand (Suppress => True); |
| 9365 |
return; |
| 9366 |
|
| 9367 |
-- Here we know that we are not in the result range, and in the general |
| 9368 |
-- case we will move into either the Bignum or Long_Long_Integer domain |
| 9369 |
-- to compute the result. However, there is one exception. If we are |
| 9370 |
-- at the top level, and we do not have Bignum or Long_Long_Integer |
| 9371 |
-- operands, we will have to immediately convert the result back to |
| 9372 |
-- the result type, so there is no point in Bignum/Long_Long_Integer |
| 9373 |
-- fiddling. |
| 9374 |
|
| 9375 |
elsif Top_Level |
| 9376 |
and then not (Bignum_Operands or Long_Long_Integer_Operands) |
| 9377 |
|
| 9378 |
-- One further refinement. If we are at the top level, but our parent |
| 9379 |
-- is a type conversion, then go into bignum or long long integer node |
| 9380 |
-- since the result will be converted to that type directly without |
| 9381 |
-- going through the result type, and we may avoid an overflow. This |
| 9382 |
-- is the case for example of Long_Long_Integer (A ** 4), where A is |
| 9383 |
-- of type Integer, and the result A ** 4 fits in Long_Long_Integer |
| 9384 |
-- but does not fit in Integer. |
| 9385 |
|
| 9386 |
and then Nkind (Parent (N)) /= N_Type_Conversion |
| 9387 |
then |
| 9388 |
-- Here keep original types, but we need to complete analysis |
| 9389 |
|
| 9390 |
-- One subtlety. We can't just go ahead and do an analyze operation |
| 9391 |
-- here because it will cause recursion into the whole MINIMIZED/ |
| 9392 |
-- ELIMINATED overflow processing which is not what we want. Here |
| 9393 |
-- we are at the top level, and we need a check against the result |
| 9394 |
-- mode (i.e. we want to use STRICT mode). So do exactly that. |
| 9395 |
-- Also, we have not modified the node, so this is a case where |
| 9396 |
-- we need to reexpand, but not reanalyze. |
| 9397 |
|
| 9398 |
Reexpand; |
| 9399 |
return; |
| 9400 |
|
| 9401 |
-- Cases where we do the operation in Bignum mode. This happens either |
| 9402 |
-- because one of our operands is in Bignum mode already, or because |
| 9403 |
-- the computed bounds are outside the bounds of Long_Long_Integer, |
| 9404 |
-- which in some cases can be indicated by Hi and Lo being No_Uint. |
| 9405 |
|
| 9406 |
-- Note: we could do better here and in some cases switch back from |
| 9407 |
-- Bignum mode to normal mode, e.g. big mod 2 must be in the range |
| 9408 |
-- 0 .. 1, but the cases are rare and it is not worth the effort. |
| 9409 |
-- Failing to do this switching back is only an efficiency issue. |
| 9410 |
|
| 9411 |
elsif No (Lo) or else Lo < LLLo or else Hi > LLHi then |
| 9412 |
|
| 9413 |
-- OK, we are definitely outside the range of Long_Long_Integer. The |
| 9414 |
-- question is whether to move to Bignum mode, or stay in the domain |
| 9415 |
-- of Long_Long_Integer, signalling that an overflow check is needed. |
| 9416 |
|
| 9417 |
-- Obviously in MINIMIZED mode we stay with LLI, since we are not in |
| 9418 |
-- the Bignum business. In ELIMINATED mode, we will normally move |
| 9419 |
-- into Bignum mode, but there is an exception if neither of our |
| 9420 |
-- operands is Bignum now, and we are at the top level (Top_Level |
| 9421 |
-- set True). In this case, there is no point in moving into Bignum |
| 9422 |
-- mode to prevent overflow if the caller will immediately convert |
| 9423 |
-- the Bignum value back to LLI with an overflow check. It's more |
| 9424 |
-- efficient to stay in LLI mode with an overflow check (if needed) |
| 9425 |
|
| 9426 |
if Check_Mode = Minimized |
| 9427 |
or else (Top_Level and not Bignum_Operands) |
| 9428 |
then |
| 9429 |
if Do_Overflow_Check (N) then |
| 9430 |
Enable_Overflow_Check (N); |
| 9431 |
end if; |
| 9432 |
|
| 9433 |
-- The result now has to be in Long_Long_Integer mode, so adjust |
| 9434 |
-- the possible range to reflect this. Note these calls also |
| 9435 |
-- change No_Uint values from the top level case to LLI bounds. |
| 9436 |
|
| 9437 |
Max (Lo, LLLo); |
| 9438 |
Min (Hi, LLHi); |
| 9439 |
|
| 9440 |
-- Otherwise we are in ELIMINATED mode and we switch to Bignum mode |
| 9441 |
|
| 9442 |
else |
| 9443 |
pragma Assert (Check_Mode = Eliminated); |
| 9444 |
|
| 9445 |
declare |
| 9446 |
Fent : Entity_Id; |
| 9447 |
Args : List_Id; |
| 9448 |
|
| 9449 |
begin |
| 9450 |
case Nkind (N) is |
| 9451 |
when N_Op_Abs => |
| 9452 |
Fent := RTE (RE_Big_Abs); |
| 9453 |
|
| 9454 |
when N_Op_Add => |
| 9455 |
Fent := RTE (RE_Big_Add); |
| 9456 |
|
| 9457 |
when N_Op_Divide => |
| 9458 |
Fent := RTE (RE_Big_Div); |
| 9459 |
|
| 9460 |
when N_Op_Expon => |
| 9461 |
Fent := RTE (RE_Big_Exp); |
| 9462 |
|
| 9463 |
when N_Op_Minus => |
| 9464 |
Fent := RTE (RE_Big_Neg); |
| 9465 |
|
| 9466 |
when N_Op_Mod => |
| 9467 |
Fent := RTE (RE_Big_Mod); |
| 9468 |
|
| 9469 |
when N_Op_Multiply => |
| 9470 |
Fent := RTE (RE_Big_Mul); |
| 9471 |
|
| 9472 |
when N_Op_Rem => |
| 9473 |
Fent := RTE (RE_Big_Rem); |
| 9474 |
|
| 9475 |
when N_Op_Subtract => |
| 9476 |
Fent := RTE (RE_Big_Sub); |
| 9477 |
|
| 9478 |
-- Anything else is an internal error, this includes the |
| 9479 |
-- N_Op_Plus case, since how can plus cause the result |
| 9480 |
-- to be out of range if the operand is in range? |
| 9481 |
|
| 9482 |
when others => |
| 9483 |
raise Program_Error; |
| 9484 |
end case; |
| 9485 |
|
| 9486 |
-- Construct argument list for Bignum call, converting our |
| 9487 |
-- operands to Bignum form if they are not already there. |
| 9488 |
|
| 9489 |
Args := New_List; |
| 9490 |
|
| 9491 |
if Binary then |
| 9492 |
Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); |
| 9493 |
end if; |
| 9494 |
|
| 9495 |
Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); |
| 9496 |
|
| 9497 |
-- Now rewrite the arithmetic operator with a call to the |
| 9498 |
-- corresponding bignum function. |
| 9499 |
|
| 9500 |
Rewrite (N, |
| 9501 |
Make_Function_Call (Loc, |
| 9502 |
Name => New_Occurrence_Of (Fent, Loc), |
| 9503 |
Parameter_Associations => Args)); |
| 9504 |
Reanalyze (RTE (RE_Bignum), Suppress => True); |
| 9505 |
|
| 9506 |
-- Indicate result is Bignum mode |
| 9507 |
|
| 9508 |
Lo := No_Uint; |
| 9509 |
Hi := No_Uint; |
| 9510 |
return; |
| 9511 |
end; |
| 9512 |
end if; |
| 9513 |
|
| 9514 |
-- Otherwise we are in range of Long_Long_Integer, so no overflow |
| 9515 |
-- check is required, at least not yet. |
| 9516 |
|
| 9517 |
else |
| 9518 |
Set_Do_Overflow_Check (N, False); |
| 9519 |
end if; |
| 9520 |
|
| 9521 |
-- Here we are not in Bignum territory, but we may have long long |
| 9522 |
-- integer operands that need special handling. First a special check: |
| 9523 |
-- If an exponentiation operator exponent is of type Long_Long_Integer, |
| 9524 |
-- it means we converted it to prevent overflow, but exponentiation |
| 9525 |
-- requires a Natural right operand, so convert it back to Natural. |
| 9526 |
-- This conversion may raise an exception which is fine. |
| 9527 |
|
| 9528 |
if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
| 9529 |
Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); |
| 9530 |
end if; |
| 9531 |
|
| 9532 |
-- Here we will do the operation in Long_Long_Integer. We do this even |
| 9533 |
-- if we know an overflow check is required, better to do this in long |
| 9534 |
-- long integer mode, since we are less likely to overflow. |
| 9535 |
|
| 9536 |
-- Convert right or only operand to Long_Long_Integer, except that |
| 9537 |
-- we do not touch the exponentiation right operand. |
| 9538 |
|
| 9539 |
if Nkind (N) /= N_Op_Expon then |
| 9540 |
Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); |
| 9541 |
end if; |
| 9542 |
|
| 9543 |
-- Convert left operand to Long_Long_Integer for binary case |
| 9544 |
|
| 9545 |
if Binary then |
| 9546 |
Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); |
| 9547 |
end if; |
| 9548 |
|
| 9549 |
-- Reset node to unanalyzed |
| 9550 |
|
| 9551 |
Set_Analyzed (N, False); |
| 9552 |
Set_Etype (N, Empty); |
| 9553 |
Set_Entity (N, Empty); |
| 9554 |
|
| 9555 |
-- Now analyze this new node. This reanalysis will complete processing |
| 9556 |
-- for the node. In particular we will complete the expansion of an |
| 9557 |
-- exponentiation operator (e.g. changing A ** 2 to A * A), and also |
| 9558 |
-- we will complete any division checks (since we have not changed the |
| 9559 |
-- setting of the Do_Division_Check flag). |
| 9560 |
|
| 9561 |
-- We do this reanalysis in STRICT mode to avoid recursion into the |
| 9562 |
-- MINIMIZED/ELIMINATED handling, since we are now done with that. |
| 9563 |
|
| 9564 |
declare |
| 9565 |
SG : constant Overflow_Mode_Type := |
| 9566 |
Scope_Suppress.Overflow_Mode_General; |
| 9567 |
SA : constant Overflow_Mode_Type := |
| 9568 |
Scope_Suppress.Overflow_Mode_Assertions; |
| 9569 |
|
| 9570 |
begin |
| 9571 |
Scope_Suppress.Overflow_Mode_General := Strict; |
| 9572 |
Scope_Suppress.Overflow_Mode_Assertions := Strict; |
| 9573 |
|
| 9574 |
if not Do_Overflow_Check (N) then |
| 9575 |
Reanalyze (LLIB, Suppress => True); |
| 9576 |
else |
| 9577 |
Reanalyze (LLIB); |
| 9578 |
end if; |
| 9579 |
|
| 9580 |
Scope_Suppress.Overflow_Mode_General := SG; |
| 9581 |
Scope_Suppress.Overflow_Mode_Assertions := SA; |
| 9582 |
end; |
| 9583 |
end Minimize_Eliminate_Overflows; |
| 9584 |
|
| 9585 |
------------------------- |
| 9586 |
-- Overflow_Check_Mode -- |
| 9587 |
------------------------- |
| 9588 |
|
| 9589 |
function Overflow_Check_Mode return Overflow_Mode_Type is |
| 9590 |
begin |
| 9591 |
if In_Assertion_Expr = 0 then |
| 9592 |
return Scope_Suppress.Overflow_Mode_General; |
| 9593 |
else |
| 9594 |
return Scope_Suppress.Overflow_Mode_Assertions; |
| 9595 |
end if; |
| 9596 |
end Overflow_Check_Mode; |
| 9597 |
|
| 9598 |
-------------------------------- |
| 9599 |
-- Overflow_Checks_Suppressed -- |
| 9600 |
-------------------------------- |
| 9601 |
|
| 9602 |
function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 9603 |
begin |
| 9604 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 9605 |
return Is_Check_Suppressed (E, Overflow_Check); |
| 9606 |
else |
| 9607 |
return Scope_Suppress.Suppress (Overflow_Check); |
| 9608 |
end if; |
| 9609 |
end Overflow_Checks_Suppressed; |
| 9610 |
|
| 9611 |
--------------------------------- |
| 9612 |
-- Predicate_Checks_Suppressed -- |
| 9613 |
--------------------------------- |
| 9614 |
|
| 9615 |
function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 9616 |
begin |
| 9617 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 9618 |
return Is_Check_Suppressed (E, Predicate_Check); |
| 9619 |
else |
| 9620 |
return Scope_Suppress.Suppress (Predicate_Check); |
| 9621 |
end if; |
| 9622 |
end Predicate_Checks_Suppressed; |
| 9623 |
|
| 9624 |
----------------------------- |
| 9625 |
-- Raise_Checks_Suppressed -- |
| 9626 |
----------------------------- |
| 9627 |
|
| 9628 |
function Raise_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 9629 |
begin |
| 9630 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 9631 |
return Is_Check_Suppressed (E, Raise_Check); |
| 9632 |
else |
| 9633 |
return Scope_Suppress.Suppress (Raise_Check); |
| 9634 |
end if; |
| 9635 |
end Raise_Checks_Suppressed; |
| 9636 |
|
| 9637 |
----------------------------- |
| 9638 |
-- Range_Checks_Suppressed -- |
| 9639 |
----------------------------- |
| 9640 |
|
| 9641 |
function Range_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 9642 |
begin |
| 9643 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 9644 |
return Is_Check_Suppressed (E, Range_Check); |
| 9645 |
else |
| 9646 |
return Scope_Suppress.Suppress (Range_Check); |
| 9647 |
end if; |
| 9648 |
end Range_Checks_Suppressed; |
| 9649 |
|
| 9650 |
----------------------------------------- |
| 9651 |
-- Range_Or_Validity_Checks_Suppressed -- |
| 9652 |
----------------------------------------- |
| 9653 |
|
| 9654 |
-- Note: the coding would be simpler here if we simply made appropriate |
| 9655 |
-- calls to Range/Validity_Checks_Suppressed, but that would result in |
| 9656 |
-- duplicated checks which we prefer to avoid. |
| 9657 |
|
| 9658 |
function Range_Or_Validity_Checks_Suppressed |
| 9659 |
(Expr : Node_Id) return Boolean |
| 9660 |
is |
| 9661 |
begin |
| 9662 |
-- Immediate return if scope checks suppressed for either check |
| 9663 |
|
| 9664 |
if Scope_Suppress.Suppress (Range_Check) |
| 9665 |
or |
| 9666 |
Scope_Suppress.Suppress (Validity_Check) |
| 9667 |
then |
| 9668 |
return True; |
| 9669 |
end if; |
| 9670 |
|
| 9671 |
-- If no expression, that's odd, decide that checks are suppressed, |
| 9672 |
-- since we don't want anyone trying to do checks in this case, which |
| 9673 |
-- is most likely the result of some other error. |
| 9674 |
|
| 9675 |
if No (Expr) then |
| 9676 |
return True; |
| 9677 |
end if; |
| 9678 |
|
| 9679 |
-- Expression is present, so perform suppress checks on type |
| 9680 |
|
| 9681 |
declare |
| 9682 |
Typ : constant Entity_Id := Etype (Expr); |
| 9683 |
begin |
| 9684 |
if Checks_May_Be_Suppressed (Typ) |
| 9685 |
and then (Is_Check_Suppressed (Typ, Range_Check) |
| 9686 |
or else |
| 9687 |
Is_Check_Suppressed (Typ, Validity_Check)) |
| 9688 |
then |
| 9689 |
return True; |
| 9690 |
end if; |
| 9691 |
end; |
| 9692 |
|
| 9693 |
-- If expression is an entity name, perform checks on this entity |
| 9694 |
|
| 9695 |
if Is_Entity_Name (Expr) then |
| 9696 |
declare |
| 9697 |
Ent : constant Entity_Id := Entity (Expr); |
| 9698 |
begin |
| 9699 |
if Checks_May_Be_Suppressed (Ent) then |
| 9700 |
return Is_Check_Suppressed (Ent, Range_Check) |
| 9701 |
or else Is_Check_Suppressed (Ent, Validity_Check); |
| 9702 |
end if; |
| 9703 |
end; |
| 9704 |
end if; |
| 9705 |
|
| 9706 |
-- If we fall through, no checks suppressed |
| 9707 |
|
| 9708 |
return False; |
| 9709 |
end Range_Or_Validity_Checks_Suppressed; |
| 9710 |
|
| 9711 |
------------------- |
| 9712 |
-- Remove_Checks -- |
| 9713 |
------------------- |
| 9714 |
|
| 9715 |
procedure Remove_Checks (Expr : Node_Id) is |
| 9716 |
function Process (N : Node_Id) return Traverse_Result; |
| 9717 |
-- Process a single node during the traversal |
| 9718 |
|
| 9719 |
procedure Traverse is new Traverse_Proc (Process); |
| 9720 |
-- The traversal procedure itself |
| 9721 |
|
| 9722 |
------------- |
| 9723 |
-- Process -- |
| 9724 |
------------- |
| 9725 |
|
| 9726 |
function Process (N : Node_Id) return Traverse_Result is |
| 9727 |
begin |
| 9728 |
if Nkind (N) not in N_Subexpr then |
| 9729 |
return Skip; |
| 9730 |
end if; |
| 9731 |
|
| 9732 |
Set_Do_Range_Check (N, False); |
| 9733 |
|
| 9734 |
case Nkind (N) is |
| 9735 |
when N_Attribute_Reference => |
| 9736 |
Set_Do_Overflow_Check (N, False); |
| 9737 |
|
| 9738 |
when N_Op => |
| 9739 |
Set_Do_Overflow_Check (N, False); |
| 9740 |
|
| 9741 |
case Nkind (N) is |
| 9742 |
when N_Op_Divide |
| 9743 |
| N_Op_Mod |
| 9744 |
| N_Op_Rem |
| 9745 |
=> |
| 9746 |
Set_Do_Division_Check (N, False); |
| 9747 |
|
| 9748 |
when N_Op_And |
| 9749 |
| N_Op_Or |
| 9750 |
| N_Op_Xor |
| 9751 |
=> |
| 9752 |
Set_Do_Length_Check (N, False); |
| 9753 |
|
| 9754 |
when others => |
| 9755 |
null; |
| 9756 |
end case; |
| 9757 |
|
| 9758 |
when N_Selected_Component => |
| 9759 |
Set_Do_Discriminant_Check (N, False); |
| 9760 |
|
| 9761 |
when N_Short_Circuit => |
| 9762 |
Traverse (Left_Opnd (N)); |
| 9763 |
return Skip; |
| 9764 |
|
| 9765 |
when N_Type_Conversion => |
| 9766 |
Set_Do_Length_Check (N, False); |
| 9767 |
Set_Do_Overflow_Check (N, False); |
| 9768 |
|
| 9769 |
when others => |
| 9770 |
null; |
| 9771 |
end case; |
| 9772 |
|
| 9773 |
return OK; |
| 9774 |
end Process; |
| 9775 |
|
| 9776 |
-- Start of processing for Remove_Checks |
| 9777 |
|
| 9778 |
begin |
| 9779 |
Traverse (Expr); |
| 9780 |
end Remove_Checks; |
| 9781 |
|
| 9782 |
---------------------------- |
| 9783 |
-- Selected_Length_Checks -- |
| 9784 |
---------------------------- |
| 9785 |
|
| 9786 |
function Selected_Length_Checks |
| 9787 |
(Expr : Node_Id; |
| 9788 |
Target_Typ : Entity_Id; |
| 9789 |
Source_Typ : Entity_Id; |
| 9790 |
Warn_Node : Node_Id) return Check_Result |
| 9791 |
is |
| 9792 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 9793 |
S_Typ : Entity_Id; |
| 9794 |
T_Typ : Entity_Id; |
| 9795 |
Expr_Actual : Node_Id; |
| 9796 |
Exptyp : Entity_Id; |
| 9797 |
Cond : Node_Id := Empty; |
| 9798 |
Do_Access : Boolean := False; |
| 9799 |
Wnode : Node_Id := Warn_Node; |
| 9800 |
Ret_Result : Check_Result := (Empty, Empty); |
| 9801 |
Num_Checks : Natural := 0; |
| 9802 |
|
| 9803 |
procedure Add_Check (N : Node_Id); |
| 9804 |
-- Adds the action given to Ret_Result if N is non-Empty |
| 9805 |
|
| 9806 |
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; |
| 9807 |
-- Return E'Length (Indx) |
| 9808 |
|
| 9809 |
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; |
| 9810 |
-- Return N'Length (Indx) |
| 9811 |
|
| 9812 |
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; |
| 9813 |
-- True for equal literals and for nodes that denote the same constant |
| 9814 |
-- entity, even if its value is not a static constant. This includes the |
| 9815 |
-- case of a discriminal reference within an init proc. Removes some |
| 9816 |
-- obviously superfluous checks. |
| 9817 |
|
| 9818 |
function Length_E_Cond |
| 9819 |
(Exptyp : Entity_Id; |
| 9820 |
Typ : Entity_Id; |
| 9821 |
Indx : Nat) return Node_Id; |
| 9822 |
-- Returns expression to compute: |
| 9823 |
-- Typ'Length /= Exptyp'Length |
| 9824 |
|
| 9825 |
function Length_N_Cond |
| 9826 |
(Exp : Node_Id; |
| 9827 |
Typ : Entity_Id; |
| 9828 |
Indx : Nat) return Node_Id; |
| 9829 |
-- Returns expression to compute: |
| 9830 |
-- Typ'Length /= Exp'Length |
| 9831 |
|
| 9832 |
function Length_Mismatch_Info_Message |
| 9833 |
(Left_Element_Count : Unat; |
| 9834 |
Right_Element_Count : Unat) return String; |
| 9835 |
-- Returns a message indicating how many elements were expected |
| 9836 |
-- (Left_Element_Count) and how many were found (Right_Element_Count). |
| 9837 |
|
| 9838 |
--------------- |
| 9839 |
-- Add_Check -- |
| 9840 |
--------------- |
| 9841 |
|
| 9842 |
procedure Add_Check (N : Node_Id) is |
| 9843 |
begin |
| 9844 |
if Present (N) then |
| 9845 |
|
| 9846 |
-- We do not support inserting more than 2 checks on the same |
| 9847 |
-- node. If this happens it means we have already added an |
| 9848 |
-- unconditional raise, so we can skip the other checks safely |
| 9849 |
-- since N will always raise an exception. |
| 9850 |
|
| 9851 |
if Num_Checks = 2 then |
| 9852 |
return; |
| 9853 |
end if; |
| 9854 |
|
| 9855 |
pragma Assert (Num_Checks <= 1); |
| 9856 |
Num_Checks := Num_Checks + 1; |
| 9857 |
Ret_Result (Num_Checks) := N; |
| 9858 |
end if; |
| 9859 |
end Add_Check; |
| 9860 |
|
| 9861 |
------------------ |
| 9862 |
-- Get_E_Length -- |
| 9863 |
------------------ |
| 9864 |
|
| 9865 |
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is |
| 9866 |
SE : constant Entity_Id := Scope (E); |
| 9867 |
N : Node_Id; |
| 9868 |
E1 : Entity_Id := E; |
| 9869 |
|
| 9870 |
begin |
| 9871 |
if Ekind (Scope (E)) = E_Record_Type |
| 9872 |
and then Has_Discriminants (Scope (E)) |
| 9873 |
then |
| 9874 |
-- If the expression is a selected component, in other words, |
| 9875 |
-- has a prefix, then build an actual subtype from the prefix. |
| 9876 |
-- Otherwise, build an actual subtype from the discriminal. |
| 9877 |
|
| 9878 |
if Nkind (Expr) = N_Selected_Component then |
| 9879 |
N := Build_Actual_Subtype_Of_Component (E, Expr); |
| 9880 |
else |
| 9881 |
N := Build_Discriminal_Subtype_Of_Component (E); |
| 9882 |
end if; |
| 9883 |
|
| 9884 |
if Present (N) then |
| 9885 |
Insert_Action (Expr, N); |
| 9886 |
E1 := Defining_Identifier (N); |
| 9887 |
end if; |
| 9888 |
end if; |
| 9889 |
|
| 9890 |
if Ekind (E1) = E_String_Literal_Subtype then |
| 9891 |
return |
| 9892 |
Make_Integer_Literal (Loc, |
| 9893 |
Intval => String_Literal_Length (E1)); |
| 9894 |
|
| 9895 |
elsif SE /= Standard_Standard |
| 9896 |
and then Ekind (Scope (SE)) = E_Protected_Type |
| 9897 |
and then Has_Discriminants (Scope (SE)) |
| 9898 |
and then Has_Completion (Scope (SE)) |
| 9899 |
and then not Inside_Init_Proc |
| 9900 |
then |
| 9901 |
-- If the type whose length is needed is a private component |
| 9902 |
-- constrained by a discriminant, we must expand the 'Length |
| 9903 |
-- attribute into an explicit computation, using the discriminal |
| 9904 |
-- of the current protected operation. This is because the actual |
| 9905 |
-- type of the prival is constructed after the protected opera- |
| 9906 |
-- tion has been fully expanded. |
| 9907 |
|
| 9908 |
declare |
| 9909 |
Indx_Type : Node_Id; |
| 9910 |
Bounds : Range_Nodes; |
| 9911 |
Do_Expand : Boolean := False; |
| 9912 |
|
| 9913 |
begin |
| 9914 |
Indx_Type := First_Index (E); |
| 9915 |
|
| 9916 |
for J in 1 .. Indx - 1 loop |
| 9917 |
Next_Index (Indx_Type); |
| 9918 |
end loop; |
| 9919 |
|
| 9920 |
Bounds := Get_Index_Bounds (Indx_Type); |
| 9921 |
|
| 9922 |
if Nkind (Bounds.First) = N_Identifier |
| 9923 |
and then Ekind (Entity (Bounds.First)) = E_In_Parameter |
| 9924 |
then |
| 9925 |
Bounds.First := Get_Discriminal (E, Bounds.First); |
| 9926 |
Do_Expand := True; |
| 9927 |
end if; |
| 9928 |
|
| 9929 |
if Nkind (Bounds.Last) = N_Identifier |
| 9930 |
and then Ekind (Entity (Bounds.Last)) = E_In_Parameter |
| 9931 |
then |
| 9932 |
Bounds.Last := Get_Discriminal (E, Bounds.Last); |
| 9933 |
Do_Expand := True; |
| 9934 |
end if; |
| 9935 |
|
| 9936 |
if Do_Expand then |
| 9937 |
if not Is_Entity_Name (Bounds.First) then |
| 9938 |
Bounds.First := |
| 9939 |
Duplicate_Subexpr_No_Checks (Bounds.First); |
| 9940 |
end if; |
| 9941 |
|
| 9942 |
if not Is_Entity_Name (Bounds.Last) then |
| 9943 |
Bounds.First := Duplicate_Subexpr_No_Checks (Bounds.Last); |
| 9944 |
end if; |
| 9945 |
|
| 9946 |
N := |
| 9947 |
Make_Op_Add (Loc, |
| 9948 |
Left_Opnd => |
| 9949 |
Make_Op_Subtract (Loc, |
| 9950 |
Left_Opnd => Bounds.Last, |
| 9951 |
Right_Opnd => Bounds.First), |
| 9952 |
|
| 9953 |
Right_Opnd => Make_Integer_Literal (Loc, 1)); |
| 9954 |
return N; |
| 9955 |
|
| 9956 |
else |
| 9957 |
N := |
| 9958 |
Make_Attribute_Reference (Loc, |
| 9959 |
Attribute_Name => Name_Length, |
| 9960 |
Prefix => |
| 9961 |
New_Occurrence_Of (E1, Loc)); |
| 9962 |
|
| 9963 |
if Indx > 1 then |
| 9964 |
Set_Expressions (N, New_List ( |
| 9965 |
Make_Integer_Literal (Loc, Indx))); |
| 9966 |
end if; |
| 9967 |
|
| 9968 |
return N; |
| 9969 |
end if; |
| 9970 |
end; |
| 9971 |
|
| 9972 |
else |
| 9973 |
N := |
| 9974 |
Make_Attribute_Reference (Loc, |
| 9975 |
Attribute_Name => Name_Length, |
| 9976 |
Prefix => |
| 9977 |
New_Occurrence_Of (E1, Loc)); |
| 9978 |
|
| 9979 |
if Indx > 1 then |
| 9980 |
Set_Expressions (N, New_List ( |
| 9981 |
Make_Integer_Literal (Loc, Indx))); |
| 9982 |
end if; |
| 9983 |
|
| 9984 |
return N; |
| 9985 |
end if; |
| 9986 |
end Get_E_Length; |
| 9987 |
|
| 9988 |
------------------ |
| 9989 |
-- Get_N_Length -- |
| 9990 |
------------------ |
| 9991 |
|
| 9992 |
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is |
| 9993 |
begin |
| 9994 |
return |
| 9995 |
Make_Attribute_Reference (Loc, |
| 9996 |
Attribute_Name => Name_Length, |
| 9997 |
Prefix => |
| 9998 |
Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
| 9999 |
Expressions => New_List ( |
| 10000 |
Make_Integer_Literal (Loc, Indx))); |
| 10001 |
end Get_N_Length; |
| 10002 |
|
| 10003 |
------------------- |
| 10004 |
-- Length_E_Cond -- |
| 10005 |
------------------- |
| 10006 |
|
| 10007 |
function Length_E_Cond |
| 10008 |
(Exptyp : Entity_Id; |
| 10009 |
Typ : Entity_Id; |
| 10010 |
Indx : Nat) return Node_Id |
| 10011 |
is |
| 10012 |
begin |
| 10013 |
return |
| 10014 |
Make_Op_Ne (Loc, |
| 10015 |
Left_Opnd => Get_E_Length (Typ, Indx), |
| 10016 |
Right_Opnd => Get_E_Length (Exptyp, Indx)); |
| 10017 |
end Length_E_Cond; |
| 10018 |
|
| 10019 |
------------------- |
| 10020 |
-- Length_N_Cond -- |
| 10021 |
------------------- |
| 10022 |
|
| 10023 |
function Length_N_Cond |
| 10024 |
(Exp : Node_Id; |
| 10025 |
Typ : Entity_Id; |
| 10026 |
Indx : Nat) return Node_Id |
| 10027 |
is |
| 10028 |
begin |
| 10029 |
return |
| 10030 |
Make_Op_Ne (Loc, |
| 10031 |
Left_Opnd => Get_E_Length (Typ, Indx), |
| 10032 |
Right_Opnd => Get_N_Length (Exp, Indx)); |
| 10033 |
end Length_N_Cond; |
| 10034 |
|
| 10035 |
---------------------------------- |
| 10036 |
-- Length_Mismatch_Info_Message -- |
| 10037 |
---------------------------------- |
| 10038 |
|
| 10039 |
function Length_Mismatch_Info_Message |
| 10040 |
(Left_Element_Count : Unat; |
| 10041 |
Right_Element_Count : Unat) return String |
| 10042 |
is |
| 10043 |
|
| 10044 |
function Plural_Vs_Singular_Ending (Count : Unat) return String; |
| 10045 |
-- Returns an empty string if Count is 1; otherwise returns "s" |
| 10046 |
|
| 10047 |
function Plural_Vs_Singular_Ending (Count : Unat) return String is |
| 10048 |
begin |
| 10049 |
if Count = 1 then |
| 10050 |
return ""; |
| 10051 |
else |
| 10052 |
return "s"; |
| 10053 |
end if; |
| 10054 |
end Plural_Vs_Singular_Ending; |
| 10055 |
|
| 10056 |
begin |
| 10057 |
return "expected " |
| 10058 |
& UI_Image (Left_Element_Count, Format => Decimal) |
| 10059 |
& " element" |
| 10060 |
& Plural_Vs_Singular_Ending (Left_Element_Count) |
| 10061 |
& "; found " |
| 10062 |
& UI_Image (Right_Element_Count, Format => Decimal) |
| 10063 |
& " element" |
| 10064 |
& Plural_Vs_Singular_Ending (Right_Element_Count); |
| 10065 |
-- "Format => Decimal" above is needed because otherwise UI_Image |
| 10066 |
-- can sometimes return a hexadecimal number 16#...#, but "#" means |
| 10067 |
-- something special to Errout. A previous version used the default |
| 10068 |
-- Auto, which was essentially the same bug as documented here: |
| 10069 |
-- https://xkcd.com/327/ . |
| 10070 |
end Length_Mismatch_Info_Message; |
| 10071 |
|
| 10072 |
----------------- |
| 10073 |
-- Same_Bounds -- |
| 10074 |
----------------- |
| 10075 |
|
| 10076 |
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
| 10077 |
begin |
| 10078 |
return |
| 10079 |
(Nkind (L) = N_Integer_Literal |
| 10080 |
and then Nkind (R) = N_Integer_Literal |
| 10081 |
and then Intval (L) = Intval (R)) |
| 10082 |
|
| 10083 |
or else |
| 10084 |
(Is_Entity_Name (L) |
| 10085 |
and then Ekind (Entity (L)) = E_Constant |
| 10086 |
and then ((Is_Entity_Name (R) |
| 10087 |
and then Entity (L) = Entity (R)) |
| 10088 |
or else |
| 10089 |
(Nkind (R) = N_Type_Conversion |
| 10090 |
and then Is_Entity_Name (Expression (R)) |
| 10091 |
and then Entity (L) = Entity (Expression (R))))) |
| 10092 |
|
| 10093 |
or else |
| 10094 |
(Is_Entity_Name (R) |
| 10095 |
and then Ekind (Entity (R)) = E_Constant |
| 10096 |
and then Nkind (L) = N_Type_Conversion |
| 10097 |
and then Is_Entity_Name (Expression (L)) |
| 10098 |
and then Entity (R) = Entity (Expression (L))) |
| 10099 |
|
| 10100 |
or else |
| 10101 |
(Is_Entity_Name (L) |
| 10102 |
and then Is_Entity_Name (R) |
| 10103 |
and then Entity (L) = Entity (R) |
| 10104 |
and then Ekind (Entity (L)) = E_In_Parameter |
| 10105 |
and then Inside_Init_Proc); |
| 10106 |
end Same_Bounds; |
| 10107 |
|
| 10108 |
-- Start of processing for Selected_Length_Checks |
| 10109 |
|
| 10110 |
begin |
| 10111 |
-- Checks will be applied only when generating code |
| 10112 |
|
| 10113 |
if not Expander_Active then |
| 10114 |
return Ret_Result; |
| 10115 |
end if; |
| 10116 |
|
| 10117 |
if Target_Typ = Any_Type |
| 10118 |
or else Target_Typ = Any_Composite |
| 10119 |
or else Raises_Constraint_Error (Expr) |
| 10120 |
then |
| 10121 |
return Ret_Result; |
| 10122 |
end if; |
| 10123 |
|
| 10124 |
if No (Wnode) then |
| 10125 |
Wnode := Expr; |
| 10126 |
end if; |
| 10127 |
|
| 10128 |
T_Typ := Target_Typ; |
| 10129 |
|
| 10130 |
if No (Source_Typ) then |
| 10131 |
S_Typ := Etype (Expr); |
| 10132 |
else |
| 10133 |
S_Typ := Source_Typ; |
| 10134 |
end if; |
| 10135 |
|
| 10136 |
if S_Typ = Any_Type or else S_Typ = Any_Composite then |
| 10137 |
return Ret_Result; |
| 10138 |
end if; |
| 10139 |
|
| 10140 |
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then |
| 10141 |
S_Typ := Designated_Type (S_Typ); |
| 10142 |
T_Typ := Designated_Type (T_Typ); |
| 10143 |
Do_Access := True; |
| 10144 |
|
| 10145 |
-- A simple optimization for the null case |
| 10146 |
|
| 10147 |
if Known_Null (Expr) then |
| 10148 |
return Ret_Result; |
| 10149 |
end if; |
| 10150 |
end if; |
| 10151 |
|
| 10152 |
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then |
| 10153 |
if Is_Constrained (T_Typ) then |
| 10154 |
|
| 10155 |
-- The checking code to be generated will freeze the corresponding |
| 10156 |
-- array type. However, we must freeze the type now, so that the |
| 10157 |
-- freeze node does not appear within the generated if expression, |
| 10158 |
-- but ahead of it. |
| 10159 |
|
| 10160 |
Freeze_Before (Expr, T_Typ); |
| 10161 |
|
| 10162 |
Expr_Actual := Get_Referenced_Object (Expr); |
| 10163 |
Exptyp := Get_Actual_Subtype (Expr); |
| 10164 |
|
| 10165 |
if Is_Access_Type (Exptyp) then |
| 10166 |
Exptyp := Designated_Type (Exptyp); |
| 10167 |
end if; |
| 10168 |
|
| 10169 |
-- String_Literal case. This needs to be handled specially be- |
| 10170 |
-- cause no index types are available for string literals. The |
| 10171 |
-- condition is simply: |
| 10172 |
|
| 10173 |
-- T_Typ'Length = string-literal-length |
| 10174 |
|
| 10175 |
-- The above also applies to the External_Initializer case. |
| 10176 |
|
| 10177 |
if Nkind (Expr_Actual) in N_String_Literal | N_External_Initializer |
| 10178 |
and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype |
| 10179 |
then |
| 10180 |
Cond := |
| 10181 |
Make_Op_Ne (Loc, |
| 10182 |
Left_Opnd => Get_E_Length (T_Typ, 1), |
| 10183 |
Right_Opnd => |
| 10184 |
Make_Integer_Literal (Loc, |
| 10185 |
Intval => |
| 10186 |
String_Literal_Length (Etype (Expr_Actual)))); |
| 10187 |
|
| 10188 |
-- General array case. Here we have a usable actual subtype for |
| 10189 |
-- the expression, and the condition is built from the two types |
| 10190 |
-- (Do_Length): |
| 10191 |
|
| 10192 |
-- T_Typ'Length /= Exptyp'Length or else |
| 10193 |
-- T_Typ'Length (2) /= Exptyp'Length (2) or else |
| 10194 |
-- T_Typ'Length (3) /= Exptyp'Length (3) or else |
| 10195 |
-- ... |
| 10196 |
|
| 10197 |
elsif Is_Constrained (Exptyp) then |
| 10198 |
declare |
| 10199 |
Ndims : constant Nat := Number_Dimensions (T_Typ); |
| 10200 |
|
| 10201 |
L_Index : Node_Id; |
| 10202 |
R_Index : Node_Id; |
| 10203 |
L_Bounds : Range_Nodes; |
| 10204 |
R_Bounds : Range_Nodes; |
| 10205 |
L_Length : Uint; |
| 10206 |
R_Length : Uint; |
| 10207 |
Ref_Node : Node_Id; |
| 10208 |
|
| 10209 |
begin |
| 10210 |
-- At the library level, we need to ensure that the type of |
| 10211 |
-- the object is elaborated before the check itself is |
| 10212 |
-- emitted. This is only done if the object is in the |
| 10213 |
-- current compilation unit, otherwise the type is frozen |
| 10214 |
-- and elaborated in its unit. |
| 10215 |
|
| 10216 |
if Is_Itype (Exptyp) |
| 10217 |
and then |
| 10218 |
Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package |
| 10219 |
and then |
| 10220 |
not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) |
| 10221 |
and then In_Open_Scopes (Scope (Exptyp)) |
| 10222 |
then |
| 10223 |
Ref_Node := Make_Itype_Reference (Sloc (Expr)); |
| 10224 |
Set_Itype (Ref_Node, Exptyp); |
| 10225 |
Insert_Action (Expr, Ref_Node); |
| 10226 |
end if; |
| 10227 |
|
| 10228 |
L_Index := First_Index (T_Typ); |
| 10229 |
R_Index := First_Index (Exptyp); |
| 10230 |
|
| 10231 |
for Indx in 1 .. Ndims loop |
| 10232 |
if not (Nkind (L_Index) = N_Raise_Constraint_Error |
| 10233 |
or else |
| 10234 |
Nkind (R_Index) = N_Raise_Constraint_Error) |
| 10235 |
then |
| 10236 |
L_Bounds := Get_Index_Bounds (L_Index); |
| 10237 |
R_Bounds := Get_Index_Bounds (R_Index); |
| 10238 |
|
| 10239 |
-- Deal with compile time length check. Note that we |
| 10240 |
-- skip this in the access case, because the access |
| 10241 |
-- value may be null, so we cannot know statically. |
| 10242 |
|
| 10243 |
if not Do_Access |
| 10244 |
and then Compile_Time_Known_Value (L_Bounds.First) |
| 10245 |
and then Compile_Time_Known_Value (L_Bounds.Last) |
| 10246 |
and then Compile_Time_Known_Value (R_Bounds.First) |
| 10247 |
and then Compile_Time_Known_Value (R_Bounds.Last) |
| 10248 |
then |
| 10249 |
if Expr_Value (L_Bounds.Last) >= |
| 10250 |
Expr_Value (L_Bounds.First) |
| 10251 |
then |
| 10252 |
L_Length := Expr_Value (L_Bounds.Last) - |
| 10253 |
Expr_Value (L_Bounds.First) + 1; |
| 10254 |
else |
| 10255 |
L_Length := UI_From_Int (0); |
| 10256 |
end if; |
| 10257 |
|
| 10258 |
if Expr_Value (R_Bounds.Last) >= |
| 10259 |
Expr_Value (R_Bounds.First) |
| 10260 |
then |
| 10261 |
R_Length := Expr_Value (R_Bounds.Last) - |
| 10262 |
Expr_Value (R_Bounds.First) + 1; |
| 10263 |
else |
| 10264 |
R_Length := UI_From_Int (0); |
| 10265 |
end if; |
| 10266 |
|
| 10267 |
if L_Length > R_Length then |
| 10268 |
Add_Check |
| 10269 |
(Compile_Time_Constraint_Error |
| 10270 |
(Wnode, "too few elements for}!!??", T_Typ, |
| 10271 |
Extra_Msg => Length_Mismatch_Info_Message |
| 10272 |
(L_Length, R_Length))); |
| 10273 |
|
| 10274 |
elsif L_Length < R_Length then |
| 10275 |
Add_Check |
| 10276 |
(Compile_Time_Constraint_Error |
| 10277 |
(Wnode, "too many elements for}!!??", T_Typ, |
| 10278 |
Extra_Msg => Length_Mismatch_Info_Message |
| 10279 |
(L_Length, R_Length))); |
| 10280 |
end if; |
| 10281 |
|
| 10282 |
-- The comparison for an individual index subtype |
| 10283 |
-- is omitted if the corresponding index subtypes |
| 10284 |
-- statically match, since the result is known to |
| 10285 |
-- be true. Note that this test is worth while even |
| 10286 |
-- though we do static evaluation, because non-static |
| 10287 |
-- subtypes can statically match. |
| 10288 |
|
| 10289 |
elsif not |
| 10290 |
Subtypes_Statically_Match |
| 10291 |
(Etype (L_Index), Etype (R_Index)) |
| 10292 |
|
| 10293 |
and then not |
| 10294 |
(Same_Bounds (L_Bounds.First, R_Bounds.First) |
| 10295 |
and then |
| 10296 |
Same_Bounds (L_Bounds.Last, R_Bounds.Last)) |
| 10297 |
then |
| 10298 |
Evolve_Or_Else |
| 10299 |
(Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); |
| 10300 |
end if; |
| 10301 |
|
| 10302 |
Next (L_Index); |
| 10303 |
Next (R_Index); |
| 10304 |
end if; |
| 10305 |
end loop; |
| 10306 |
end; |
| 10307 |
|
| 10308 |
-- Handle cases where we do not get a usable actual subtype that |
| 10309 |
-- is constrained. This happens for example in the function call |
| 10310 |
-- and explicit dereference cases. In these cases, we have to get |
| 10311 |
-- the length or range from the expression itself, making sure we |
| 10312 |
-- do not evaluate it more than once. |
| 10313 |
|
| 10314 |
-- Here Expr is the original expression, or more properly the |
| 10315 |
-- result of applying Duplicate_Expr to the original tree, forcing |
| 10316 |
-- the result to be a name. |
| 10317 |
|
| 10318 |
else |
| 10319 |
declare |
| 10320 |
Ndims : constant Pos := Number_Dimensions (T_Typ); |
| 10321 |
|
| 10322 |
begin |
| 10323 |
-- Build the condition for the explicit dereference case |
| 10324 |
|
| 10325 |
for Indx in 1 .. Ndims loop |
| 10326 |
Evolve_Or_Else |
| 10327 |
(Cond, Length_N_Cond (Expr, T_Typ, Indx)); |
| 10328 |
end loop; |
| 10329 |
end; |
| 10330 |
end if; |
| 10331 |
end if; |
| 10332 |
end if; |
| 10333 |
|
| 10334 |
-- Construct the test and insert into the tree |
| 10335 |
|
| 10336 |
if Present (Cond) then |
| 10337 |
if Do_Access then |
| 10338 |
Cond := Guard_Access (Cond, Loc, Expr); |
| 10339 |
end if; |
| 10340 |
|
| 10341 |
Add_Check |
| 10342 |
(Make_Raise_Constraint_Error (Loc, |
| 10343 |
Condition => Cond, |
| 10344 |
Reason => CE_Length_Check_Failed)); |
| 10345 |
end if; |
| 10346 |
|
| 10347 |
return Ret_Result; |
| 10348 |
end Selected_Length_Checks; |
| 10349 |
|
| 10350 |
--------------------------- |
| 10351 |
-- Selected_Range_Checks -- |
| 10352 |
--------------------------- |
| 10353 |
|
| 10354 |
function Selected_Range_Checks |
| 10355 |
(Expr : Node_Id; |
| 10356 |
Target_Typ : Entity_Id; |
| 10357 |
Source_Typ : Entity_Id; |
| 10358 |
Warn_Node : Node_Id) return Check_Result |
| 10359 |
is |
| 10360 |
Loc : constant Source_Ptr := Sloc (Expr); |
| 10361 |
S_Typ : Entity_Id; |
| 10362 |
T_Typ : Entity_Id; |
| 10363 |
Expr_Actual : Node_Id; |
| 10364 |
Exptyp : Entity_Id; |
| 10365 |
Cond : Node_Id := Empty; |
| 10366 |
Do_Access : Boolean := False; |
| 10367 |
Wnode : Node_Id := Warn_Node; |
| 10368 |
Ret_Result : Check_Result := (Empty, Empty); |
| 10369 |
Num_Checks : Natural := 0; |
| 10370 |
|
| 10371 |
procedure Add_Check (N : Node_Id); |
| 10372 |
-- Adds the action given to Ret_Result if N is non-Empty |
| 10373 |
|
| 10374 |
function Discrete_Range_Cond |
| 10375 |
(Exp : Node_Id; |
| 10376 |
Typ : Entity_Id) return Node_Id; |
| 10377 |
-- Returns expression to compute: |
| 10378 |
-- Low_Bound (Exp) < Typ'First |
| 10379 |
-- or else |
| 10380 |
-- High_Bound (Exp) > Typ'Last |
| 10381 |
|
| 10382 |
function Discrete_Expr_Cond |
| 10383 |
(Exp : Node_Id; |
| 10384 |
Typ : Entity_Id) return Node_Id; |
| 10385 |
-- Returns expression to compute: |
| 10386 |
-- Exp < Typ'First |
| 10387 |
-- or else |
| 10388 |
-- Exp > Typ'Last |
| 10389 |
|
| 10390 |
function Get_E_First_Or_Last |
| 10391 |
(Loc : Source_Ptr; |
| 10392 |
E : Entity_Id; |
| 10393 |
Indx : Nat; |
| 10394 |
Nam : Name_Id) return Node_Id; |
| 10395 |
-- Returns an attribute reference |
| 10396 |
-- E'First or E'Last |
| 10397 |
-- with a source location of Loc. |
| 10398 |
-- |
| 10399 |
-- Nam is Name_First or Name_Last, according to which attribute is |
| 10400 |
-- desired. If Indx is non-zero, it is passed as a literal in the |
| 10401 |
-- Expressions of the attribute reference (identifying the desired |
| 10402 |
-- array dimension). |
| 10403 |
|
| 10404 |
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; |
| 10405 |
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; |
| 10406 |
-- Returns expression to compute: |
| 10407 |
-- N'First or N'Last using Duplicate_Subexpr_No_Checks |
| 10408 |
|
| 10409 |
function Is_Cond_Expr_Ge (N : Node_Id; V : Node_Id) return Boolean; |
| 10410 |
function Is_Cond_Expr_Le (N : Node_Id; V : Node_Id) return Boolean; |
| 10411 |
-- Return True if N is a conditional expression whose dependent |
| 10412 |
-- expressions are all known and greater/lower than or equal to V. |
| 10413 |
|
| 10414 |
function Range_E_Cond |
| 10415 |
(Exptyp : Entity_Id; |
| 10416 |
Typ : Entity_Id; |
| 10417 |
Indx : Nat) |
| 10418 |
return Node_Id; |
| 10419 |
-- Returns expression to compute: |
| 10420 |
-- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last |
| 10421 |
|
| 10422 |
function Range_Equal_E_Cond |
| 10423 |
(Exptyp : Entity_Id; |
| 10424 |
Typ : Entity_Id; |
| 10425 |
Indx : Nat) return Node_Id; |
| 10426 |
-- Returns expression to compute: |
| 10427 |
-- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last |
| 10428 |
|
| 10429 |
function Range_N_Cond |
| 10430 |
(Exp : Node_Id; |
| 10431 |
Typ : Entity_Id; |
| 10432 |
Indx : Nat) return Node_Id; |
| 10433 |
-- Return expression to compute: |
| 10434 |
-- Exp'First < Typ'First or else Exp'Last > Typ'Last |
| 10435 |
|
| 10436 |
function "<" (Left, Right : Node_Id) return Boolean |
| 10437 |
is (if Is_Floating_Point_Type (S_Typ) |
| 10438 |
then Expr_Value_R (Left) < Expr_Value_R (Right) |
| 10439 |
else Expr_Value (Left) < Expr_Value (Right)); |
| 10440 |
function "<=" (Left, Right : Node_Id) return Boolean |
| 10441 |
is (if Is_Floating_Point_Type (S_Typ) |
| 10442 |
then Expr_Value_R (Left) <= Expr_Value_R (Right) |
| 10443 |
else Expr_Value (Left) <= Expr_Value (Right)); |
| 10444 |
-- Convenience comparison functions of integer or floating point values |
| 10445 |
|
| 10446 |
--------------- |
| 10447 |
-- Add_Check -- |
| 10448 |
--------------- |
| 10449 |
|
| 10450 |
procedure Add_Check (N : Node_Id) is |
| 10451 |
begin |
| 10452 |
if Present (N) then |
| 10453 |
|
| 10454 |
-- We do not support inserting more than 2 checks on the same |
| 10455 |
-- node. If this happens it means we have already added an |
| 10456 |
-- unconditional raise, so we can skip the other checks safely |
| 10457 |
-- since N will always raise an exception. |
| 10458 |
|
| 10459 |
if Num_Checks = 2 then |
| 10460 |
return; |
| 10461 |
end if; |
| 10462 |
|
| 10463 |
pragma Assert (Num_Checks <= 1); |
| 10464 |
Num_Checks := Num_Checks + 1; |
| 10465 |
Ret_Result (Num_Checks) := N; |
| 10466 |
end if; |
| 10467 |
end Add_Check; |
| 10468 |
|
| 10469 |
------------------------- |
| 10470 |
-- Discrete_Expr_Cond -- |
| 10471 |
------------------------- |
| 10472 |
|
| 10473 |
function Discrete_Expr_Cond |
| 10474 |
(Exp : Node_Id; |
| 10475 |
Typ : Entity_Id) return Node_Id |
| 10476 |
is |
| 10477 |
begin |
| 10478 |
return |
| 10479 |
Make_Or_Else (Loc, |
| 10480 |
Left_Opnd => |
| 10481 |
Make_Op_Lt (Loc, |
| 10482 |
Left_Opnd => |
| 10483 |
Convert_To (Base_Type (Typ), |
| 10484 |
Duplicate_Subexpr_No_Checks (Exp)), |
| 10485 |
Right_Opnd => |
| 10486 |
Convert_To (Base_Type (Typ), |
| 10487 |
Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
| 10488 |
|
| 10489 |
Right_Opnd => |
| 10490 |
Make_Op_Gt (Loc, |
| 10491 |
Left_Opnd => |
| 10492 |
Convert_To (Base_Type (Typ), |
| 10493 |
Duplicate_Subexpr_No_Checks (Exp)), |
| 10494 |
Right_Opnd => |
| 10495 |
Convert_To |
| 10496 |
(Base_Type (Typ), |
| 10497 |
Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
| 10498 |
end Discrete_Expr_Cond; |
| 10499 |
|
| 10500 |
------------------------- |
| 10501 |
-- Discrete_Range_Cond -- |
| 10502 |
------------------------- |
| 10503 |
|
| 10504 |
function Discrete_Range_Cond |
| 10505 |
(Exp : Node_Id; |
| 10506 |
Typ : Entity_Id) return Node_Id |
| 10507 |
is |
| 10508 |
LB : Node_Id := Low_Bound (Exp); |
| 10509 |
HB : Node_Id := High_Bound (Exp); |
| 10510 |
|
| 10511 |
Left_Opnd : Node_Id; |
| 10512 |
Right_Opnd : Node_Id; |
| 10513 |
|
| 10514 |
begin |
| 10515 |
if Nkind (LB) = N_Identifier |
| 10516 |
and then Ekind (Entity (LB)) = E_Discriminant |
| 10517 |
then |
| 10518 |
LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
| 10519 |
end if; |
| 10520 |
|
| 10521 |
-- If the index type has a fixed lower bound, then we require an |
| 10522 |
-- exact match of the range's lower bound against that fixed lower |
| 10523 |
-- bound. |
| 10524 |
|
| 10525 |
if Is_Fixed_Lower_Bound_Index_Subtype (Typ) then |
| 10526 |
Left_Opnd := |
| 10527 |
Make_Op_Ne (Loc, |
| 10528 |
Left_Opnd => |
| 10529 |
Convert_To |
| 10530 |
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
| 10531 |
|
| 10532 |
Right_Opnd => |
| 10533 |
Convert_To |
| 10534 |
(Base_Type (Typ), |
| 10535 |
Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); |
| 10536 |
|
| 10537 |
-- Otherwise we do the expected less-than comparison |
| 10538 |
|
| 10539 |
else |
| 10540 |
Left_Opnd := |
| 10541 |
Make_Op_Lt (Loc, |
| 10542 |
Left_Opnd => |
| 10543 |
Convert_To |
| 10544 |
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
| 10545 |
|
| 10546 |
Right_Opnd => |
| 10547 |
Convert_To |
| 10548 |
(Base_Type (Typ), |
| 10549 |
Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); |
| 10550 |
end if; |
| 10551 |
|
| 10552 |
if Nkind (HB) = N_Identifier |
| 10553 |
and then Ekind (Entity (HB)) = E_Discriminant |
| 10554 |
then |
| 10555 |
HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
| 10556 |
end if; |
| 10557 |
|
| 10558 |
Right_Opnd := |
| 10559 |
Make_Op_Gt (Loc, |
| 10560 |
Left_Opnd => |
| 10561 |
Convert_To |
| 10562 |
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
| 10563 |
|
| 10564 |
Right_Opnd => |
| 10565 |
Convert_To |
| 10566 |
(Base_Type (Typ), |
| 10567 |
Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
| 10568 |
|
| 10569 |
return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); |
| 10570 |
end Discrete_Range_Cond; |
| 10571 |
|
| 10572 |
------------------------- |
| 10573 |
-- Get_E_First_Or_Last -- |
| 10574 |
------------------------- |
| 10575 |
|
| 10576 |
function Get_E_First_Or_Last |
| 10577 |
(Loc : Source_Ptr; |
| 10578 |
E : Entity_Id; |
| 10579 |
Indx : Nat; |
| 10580 |
Nam : Name_Id) return Node_Id |
| 10581 |
is |
| 10582 |
Exprs : List_Id; |
| 10583 |
begin |
| 10584 |
if Indx > 0 then |
| 10585 |
Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); |
| 10586 |
else |
| 10587 |
Exprs := No_List; |
| 10588 |
end if; |
| 10589 |
|
| 10590 |
return Make_Attribute_Reference (Loc, |
| 10591 |
Prefix => New_Occurrence_Of (E, Loc), |
| 10592 |
Attribute_Name => Nam, |
| 10593 |
Expressions => Exprs); |
| 10594 |
end Get_E_First_Or_Last; |
| 10595 |
|
| 10596 |
----------------- |
| 10597 |
-- Get_N_First -- |
| 10598 |
----------------- |
| 10599 |
|
| 10600 |
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is |
| 10601 |
begin |
| 10602 |
return |
| 10603 |
Make_Attribute_Reference (Loc, |
| 10604 |
Attribute_Name => Name_First, |
| 10605 |
Prefix => |
| 10606 |
Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
| 10607 |
Expressions => New_List ( |
| 10608 |
Make_Integer_Literal (Loc, Indx))); |
| 10609 |
end Get_N_First; |
| 10610 |
|
| 10611 |
---------------- |
| 10612 |
-- Get_N_Last -- |
| 10613 |
---------------- |
| 10614 |
|
| 10615 |
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is |
| 10616 |
begin |
| 10617 |
return |
| 10618 |
Make_Attribute_Reference (Loc, |
| 10619 |
Attribute_Name => Name_Last, |
| 10620 |
Prefix => |
| 10621 |
Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
| 10622 |
Expressions => New_List ( |
| 10623 |
Make_Integer_Literal (Loc, Indx))); |
| 10624 |
end Get_N_Last; |
| 10625 |
|
| 10626 |
--------------------- |
| 10627 |
-- Is_Cond_Expr_Ge -- |
| 10628 |
--------------------- |
| 10629 |
|
| 10630 |
function Is_Cond_Expr_Ge (N : Node_Id; V : Node_Id) return Boolean is |
| 10631 |
begin |
| 10632 |
-- Only if expressions are relevant for the time being |
| 10633 |
|
| 10634 |
if Nkind (N) = N_If_Expression then |
| 10635 |
declare |
| 10636 |
Cond : constant Node_Id := First (Expressions (N)); |
| 10637 |
Thenx : constant Node_Id := Next (Cond); |
| 10638 |
Elsex : constant Node_Id := Next (Thenx); |
| 10639 |
|
| 10640 |
begin |
| 10641 |
return Compile_Time_Known_Value (Thenx) |
| 10642 |
and then V <= Thenx |
| 10643 |
and then |
| 10644 |
((Compile_Time_Known_Value (Elsex) and then V <= Elsex) |
| 10645 |
or else Is_Cond_Expr_Ge (Elsex, V)); |
| 10646 |
end; |
| 10647 |
|
| 10648 |
else |
| 10649 |
return False; |
| 10650 |
end if; |
| 10651 |
end Is_Cond_Expr_Ge; |
| 10652 |
|
| 10653 |
--------------------- |
| 10654 |
-- Is_Cond_Expr_Le -- |
| 10655 |
--------------------- |
| 10656 |
|
| 10657 |
function Is_Cond_Expr_Le (N : Node_Id; V : Node_Id) return Boolean is |
| 10658 |
begin |
| 10659 |
-- Only if expressions are relevant for the time being |
| 10660 |
|
| 10661 |
if Nkind (N) = N_If_Expression then |
| 10662 |
declare |
| 10663 |
Cond : constant Node_Id := First (Expressions (N)); |
| 10664 |
Thenx : constant Node_Id := Next (Cond); |
| 10665 |
Elsex : constant Node_Id := Next (Thenx); |
| 10666 |
|
| 10667 |
begin |
| 10668 |
return Compile_Time_Known_Value (Thenx) |
| 10669 |
and then Thenx <= V |
| 10670 |
and then |
| 10671 |
((Compile_Time_Known_Value (Elsex) and then Elsex <= V) |
| 10672 |
or else Is_Cond_Expr_Le (Elsex, V)); |
| 10673 |
end; |
| 10674 |
|
| 10675 |
else |
| 10676 |
return False; |
| 10677 |
end if; |
| 10678 |
end Is_Cond_Expr_Le; |
| 10679 |
|
| 10680 |
------------------ |
| 10681 |
-- Range_E_Cond -- |
| 10682 |
------------------ |
| 10683 |
|
| 10684 |
function Range_E_Cond |
| 10685 |
(Exptyp : Entity_Id; |
| 10686 |
Typ : Entity_Id; |
| 10687 |
Indx : Nat) return Node_Id |
| 10688 |
is |
| 10689 |
begin |
| 10690 |
return |
| 10691 |
Make_Or_Else (Loc, |
| 10692 |
Left_Opnd => |
| 10693 |
Make_Op_Lt (Loc, |
| 10694 |
Left_Opnd => |
| 10695 |
Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), |
| 10696 |
Right_Opnd => |
| 10697 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), |
| 10698 |
|
| 10699 |
Right_Opnd => |
| 10700 |
Make_Op_Gt (Loc, |
| 10701 |
Left_Opnd => |
| 10702 |
Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), |
| 10703 |
Right_Opnd => |
| 10704 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); |
| 10705 |
end Range_E_Cond; |
| 10706 |
|
| 10707 |
------------------------ |
| 10708 |
-- Range_Equal_E_Cond -- |
| 10709 |
------------------------ |
| 10710 |
|
| 10711 |
function Range_Equal_E_Cond |
| 10712 |
(Exptyp : Entity_Id; |
| 10713 |
Typ : Entity_Id; |
| 10714 |
Indx : Nat) return Node_Id |
| 10715 |
is |
| 10716 |
begin |
| 10717 |
return |
| 10718 |
Make_Or_Else (Loc, |
| 10719 |
Left_Opnd => |
| 10720 |
Make_Op_Ne (Loc, |
| 10721 |
Left_Opnd => |
| 10722 |
Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), |
| 10723 |
Right_Opnd => |
| 10724 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), |
| 10725 |
|
| 10726 |
Right_Opnd => |
| 10727 |
Make_Op_Ne (Loc, |
| 10728 |
Left_Opnd => |
| 10729 |
Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), |
| 10730 |
Right_Opnd => |
| 10731 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); |
| 10732 |
end Range_Equal_E_Cond; |
| 10733 |
|
| 10734 |
------------------ |
| 10735 |
-- Range_N_Cond -- |
| 10736 |
------------------ |
| 10737 |
|
| 10738 |
function Range_N_Cond |
| 10739 |
(Exp : Node_Id; |
| 10740 |
Typ : Entity_Id; |
| 10741 |
Indx : Nat) return Node_Id |
| 10742 |
is |
| 10743 |
begin |
| 10744 |
return |
| 10745 |
Make_Or_Else (Loc, |
| 10746 |
Left_Opnd => |
| 10747 |
Make_Op_Lt (Loc, |
| 10748 |
Left_Opnd => |
| 10749 |
Get_N_First (Exp, Indx), |
| 10750 |
Right_Opnd => |
| 10751 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), |
| 10752 |
|
| 10753 |
Right_Opnd => |
| 10754 |
Make_Op_Gt (Loc, |
| 10755 |
Left_Opnd => |
| 10756 |
Get_N_Last (Exp, Indx), |
| 10757 |
Right_Opnd => |
| 10758 |
Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); |
| 10759 |
end Range_N_Cond; |
| 10760 |
|
| 10761 |
-- Start of processing for Selected_Range_Checks |
| 10762 |
|
| 10763 |
begin |
| 10764 |
-- Checks will be applied only when generating code. In GNATprove mode, |
| 10765 |
-- we do not apply the checks, but we still call Selected_Range_Checks |
| 10766 |
-- outside of generics to possibly issue errors on SPARK code when a |
| 10767 |
-- run-time error can be detected at compile time. |
| 10768 |
|
| 10769 |
if Inside_A_Generic or (not GNATprove_Mode and not Expander_Active) then |
| 10770 |
return Ret_Result; |
| 10771 |
end if; |
| 10772 |
|
| 10773 |
if Target_Typ = Any_Type |
| 10774 |
or else Target_Typ = Any_Composite |
| 10775 |
or else Raises_Constraint_Error (Expr) |
| 10776 |
then |
| 10777 |
return Ret_Result; |
| 10778 |
end if; |
| 10779 |
|
| 10780 |
if No (Wnode) then |
| 10781 |
Wnode := Expr; |
| 10782 |
end if; |
| 10783 |
|
| 10784 |
T_Typ := Target_Typ; |
| 10785 |
|
| 10786 |
if No (Source_Typ) then |
| 10787 |
S_Typ := Etype (Expr); |
| 10788 |
else |
| 10789 |
S_Typ := Source_Typ; |
| 10790 |
end if; |
| 10791 |
|
| 10792 |
if S_Typ = Any_Type or else S_Typ = Any_Composite then |
| 10793 |
return Ret_Result; |
| 10794 |
end if; |
| 10795 |
|
| 10796 |
-- The order of evaluating T_Typ before S_Typ seems to be critical |
| 10797 |
-- because S_Typ can be derived from Etype (Expr), if it's not passed |
| 10798 |
-- in, and since Node can be an N_Range node, it might be invalid. |
| 10799 |
-- Should there be an assert check somewhere for taking the Etype of |
| 10800 |
-- an N_Range node ??? |
| 10801 |
|
| 10802 |
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then |
| 10803 |
S_Typ := Designated_Type (S_Typ); |
| 10804 |
T_Typ := Designated_Type (T_Typ); |
| 10805 |
Do_Access := True; |
| 10806 |
|
| 10807 |
-- A simple optimization for the null case |
| 10808 |
|
| 10809 |
if Known_Null (Expr) then |
| 10810 |
return Ret_Result; |
| 10811 |
end if; |
| 10812 |
end if; |
| 10813 |
|
| 10814 |
-- For an N_Range Node, check for a null range and then if not |
| 10815 |
-- null generate a range check action. |
| 10816 |
|
| 10817 |
if Nkind (Expr) = N_Range then |
| 10818 |
|
| 10819 |
-- There's no point in checking a range against itself |
| 10820 |
|
| 10821 |
if Expr = Scalar_Range (T_Typ) then |
| 10822 |
return Ret_Result; |
| 10823 |
end if; |
| 10824 |
|
| 10825 |
declare |
| 10826 |
T_LB : constant Node_Id := Type_Low_Bound (T_Typ); |
| 10827 |
T_HB : constant Node_Id := Type_High_Bound (T_Typ); |
| 10828 |
Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
| 10829 |
Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); |
| 10830 |
|
| 10831 |
LB : Node_Id := Low_Bound (Expr); |
| 10832 |
HB : Node_Id := High_Bound (Expr); |
| 10833 |
Known_LB : Boolean := False; |
| 10834 |
Known_HB : Boolean := False; |
| 10835 |
Check_Added : Boolean := False; |
| 10836 |
|
| 10837 |
Out_Of_Range_L : Boolean := False; |
| 10838 |
Out_Of_Range_H : Boolean := False; |
| 10839 |
|
| 10840 |
begin |
| 10841 |
-- Compute what is known at compile time |
| 10842 |
|
| 10843 |
if Known_T_LB and Known_T_HB then |
| 10844 |
if Compile_Time_Known_Value (LB) then |
| 10845 |
Known_LB := True; |
| 10846 |
|
| 10847 |
-- There's no point in checking that a bound is within its |
| 10848 |
-- own range so pretend that it is known in this case. First |
| 10849 |
-- deal with low bound. |
| 10850 |
|
| 10851 |
elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype |
| 10852 |
and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) |
| 10853 |
then |
| 10854 |
LB := T_LB; |
| 10855 |
Known_LB := True; |
| 10856 |
|
| 10857 |
-- Similarly; deal with the case where the low bound is a |
| 10858 |
-- conditional expression whose result is greater than or |
| 10859 |
-- equal to the target low bound. |
| 10860 |
|
| 10861 |
elsif Is_Cond_Expr_Ge (LB, T_LB) then |
| 10862 |
LB := T_LB; |
| 10863 |
Known_LB := True; |
| 10864 |
end if; |
| 10865 |
|
| 10866 |
-- Likewise for the high bound |
| 10867 |
|
| 10868 |
if Compile_Time_Known_Value (HB) then |
| 10869 |
Known_HB := True; |
| 10870 |
|
| 10871 |
elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype |
| 10872 |
and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) |
| 10873 |
then |
| 10874 |
HB := T_HB; |
| 10875 |
Known_HB := True; |
| 10876 |
|
| 10877 |
elsif Is_Cond_Expr_Le (HB, T_HB) then |
| 10878 |
HB := T_HB; |
| 10879 |
Known_HB := True; |
| 10880 |
end if; |
| 10881 |
end if; |
| 10882 |
|
| 10883 |
-- Check for the simple cases where we can do the check at |
| 10884 |
-- compile time. This is skipped if we have an access type, since |
| 10885 |
-- the access value may be null. |
| 10886 |
|
| 10887 |
if not Do_Access and then Not_Null_Range (LB, HB) then |
| 10888 |
if Known_LB then |
| 10889 |
if Known_T_LB then |
| 10890 |
Out_Of_Range_L := LB < T_LB; |
| 10891 |
end if; |
| 10892 |
|
| 10893 |
if Known_T_HB and not Out_Of_Range_L then |
| 10894 |
Out_Of_Range_L := T_HB < LB; |
| 10895 |
end if; |
| 10896 |
|
| 10897 |
if Out_Of_Range_L then |
| 10898 |
if No (Warn_Node) then |
| 10899 |
Add_Check |
| 10900 |
(Compile_Time_Constraint_Error |
| 10901 |
(Low_Bound (Expr), |
| 10902 |
"static value out of range of}??", T_Typ)); |
| 10903 |
Check_Added := True; |
| 10904 |
|
| 10905 |
else |
| 10906 |
Add_Check |
| 10907 |
(Compile_Time_Constraint_Error |
| 10908 |
(Wnode, |
| 10909 |
"static range out of bounds of}??", T_Typ)); |
| 10910 |
Check_Added := True; |
| 10911 |
end if; |
| 10912 |
end if; |
| 10913 |
end if; |
| 10914 |
|
| 10915 |
-- Flag the case of a fixed-lower-bound index where the static |
| 10916 |
-- bounds are not equal. |
| 10917 |
|
| 10918 |
if not Check_Added |
| 10919 |
and then Is_Fixed_Lower_Bound_Index_Subtype (T_Typ) |
| 10920 |
and then Known_LB |
| 10921 |
and then Known_T_LB |
| 10922 |
and then Expr_Value (LB) /= Expr_Value (T_LB) |
| 10923 |
then |
| 10924 |
Add_Check |
| 10925 |
(Compile_Time_Constraint_Error |
| 10926 |
((if Present (Warn_Node) |
| 10927 |
then Warn_Node else Low_Bound (Expr)), |
| 10928 |
"static value does not equal lower bound of}??", |
| 10929 |
T_Typ)); |
| 10930 |
Check_Added := True; |
| 10931 |
end if; |
| 10932 |
|
| 10933 |
if Known_HB then |
| 10934 |
if Known_T_HB then |
| 10935 |
Out_Of_Range_H := T_HB < HB; |
| 10936 |
end if; |
| 10937 |
|
| 10938 |
if Known_T_LB and not Out_Of_Range_H then |
| 10939 |
Out_Of_Range_H := HB < T_LB; |
| 10940 |
end if; |
| 10941 |
|
| 10942 |
if Out_Of_Range_H then |
| 10943 |
if No (Warn_Node) then |
| 10944 |
Add_Check |
| 10945 |
(Compile_Time_Constraint_Error |
| 10946 |
(High_Bound (Expr), |
| 10947 |
"static value out of range of}??", T_Typ)); |
| 10948 |
Check_Added := True; |
| 10949 |
|
| 10950 |
else |
| 10951 |
Add_Check |
| 10952 |
(Compile_Time_Constraint_Error |
| 10953 |
(Wnode, |
| 10954 |
"static range out of bounds of}??", T_Typ)); |
| 10955 |
Check_Added := True; |
| 10956 |
end if; |
| 10957 |
end if; |
| 10958 |
end if; |
| 10959 |
end if; |
| 10960 |
|
| 10961 |
-- Check for the case where not everything is static |
| 10962 |
|
| 10963 |
if not Check_Added |
| 10964 |
and then |
| 10965 |
(Do_Access |
| 10966 |
or else not Known_T_LB |
| 10967 |
or else not Known_LB |
| 10968 |
or else not Known_T_HB |
| 10969 |
or else not Known_HB) |
| 10970 |
then |
| 10971 |
declare |
| 10972 |
LB : Node_Id := Low_Bound (Expr); |
| 10973 |
HB : Node_Id := High_Bound (Expr); |
| 10974 |
|
| 10975 |
begin |
| 10976 |
-- If either bound is a discriminant and we are within the |
| 10977 |
-- record declaration, it is a use of the discriminant in a |
| 10978 |
-- constraint of a component, and nothing can be checked |
| 10979 |
-- here. The check will be emitted within the init proc. |
| 10980 |
-- Before then, the discriminal has no real meaning. |
| 10981 |
-- Similarly, if the entity is a discriminal, there is no |
| 10982 |
-- check to perform yet. |
| 10983 |
|
| 10984 |
-- The same holds within a discriminated synchronized type, |
| 10985 |
-- where the discriminant may constrain a component or an |
| 10986 |
-- entry family. |
| 10987 |
|
| 10988 |
if Nkind (LB) = N_Identifier |
| 10989 |
and then Denotes_Discriminant (LB, True) |
| 10990 |
then |
| 10991 |
if Current_Scope = Scope (Entity (LB)) |
| 10992 |
or else Is_Concurrent_Type (Current_Scope) |
| 10993 |
or else Ekind (Entity (LB)) /= E_Discriminant |
| 10994 |
then |
| 10995 |
return Ret_Result; |
| 10996 |
else |
| 10997 |
LB := |
| 10998 |
New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
| 10999 |
end if; |
| 11000 |
end if; |
| 11001 |
|
| 11002 |
if Nkind (HB) = N_Identifier |
| 11003 |
and then Denotes_Discriminant (HB, True) |
| 11004 |
then |
| 11005 |
if Current_Scope = Scope (Entity (HB)) |
| 11006 |
or else Is_Concurrent_Type (Current_Scope) |
| 11007 |
or else Ekind (Entity (HB)) /= E_Discriminant |
| 11008 |
then |
| 11009 |
return Ret_Result; |
| 11010 |
else |
| 11011 |
HB := |
| 11012 |
New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
| 11013 |
end if; |
| 11014 |
end if; |
| 11015 |
|
| 11016 |
Cond := Discrete_Range_Cond (Expr, T_Typ); |
| 11017 |
Set_Paren_Count (Cond, 1); |
| 11018 |
|
| 11019 |
Cond := |
| 11020 |
Make_And_Then (Loc, |
| 11021 |
Left_Opnd => |
| 11022 |
Make_Op_Ge (Loc, |
| 11023 |
Left_Opnd => |
| 11024 |
Convert_To (Base_Type (Etype (HB)), |
| 11025 |
Duplicate_Subexpr_No_Checks (HB)), |
| 11026 |
Right_Opnd => |
| 11027 |
Convert_To (Base_Type (Etype (LB)), |
| 11028 |
Duplicate_Subexpr_No_Checks (LB))), |
| 11029 |
Right_Opnd => Cond); |
| 11030 |
end; |
| 11031 |
end if; |
| 11032 |
end; |
| 11033 |
|
| 11034 |
elsif Is_Scalar_Type (S_Typ) then |
| 11035 |
|
| 11036 |
-- This somewhat duplicates what Apply_Scalar_Range_Check does, |
| 11037 |
-- except the above simply sets a flag in the node and lets the |
| 11038 |
-- check be generated based on the Etype of the expression. |
| 11039 |
-- Sometimes, however we want to do a dynamic check against an |
| 11040 |
-- arbitrary target type, so we do that here. |
| 11041 |
|
| 11042 |
if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then |
| 11043 |
Cond := Discrete_Expr_Cond (Expr, T_Typ); |
| 11044 |
|
| 11045 |
-- For literals, we can tell if the constraint error will be |
| 11046 |
-- raised at compile time, so we never need a dynamic check, but |
| 11047 |
-- if the exception will be raised, then post the usual warning, |
| 11048 |
-- and replace the literal with a raise constraint error |
| 11049 |
-- expression. As usual, skip this for access types |
| 11050 |
|
| 11051 |
elsif Compile_Time_Known_Value (Expr) and then not Do_Access then |
| 11052 |
if Is_Out_Of_Range (Expr, T_Typ) then |
| 11053 |
|
| 11054 |
-- Bounds of the type are static and the literal is out of |
| 11055 |
-- range so output a warning message. |
| 11056 |
|
| 11057 |
if No (Warn_Node) then |
| 11058 |
Add_Check |
| 11059 |
(Compile_Time_Constraint_Error |
| 11060 |
(Expr, "static value out of range of}??", T_Typ)); |
| 11061 |
|
| 11062 |
else |
| 11063 |
Add_Check |
| 11064 |
(Compile_Time_Constraint_Error |
| 11065 |
(Wnode, "static value out of range of}??", T_Typ)); |
| 11066 |
end if; |
| 11067 |
else |
| 11068 |
Cond := Discrete_Expr_Cond (Expr, T_Typ); |
| 11069 |
end if; |
| 11070 |
|
| 11071 |
-- Here for the case of a non-static expression, we need a runtime |
| 11072 |
-- check unless the source type range is guaranteed to be in the |
| 11073 |
-- range of the target type. |
| 11074 |
|
| 11075 |
else |
| 11076 |
if not In_Subrange_Of (S_Typ, T_Typ) then |
| 11077 |
Cond := Discrete_Expr_Cond (Expr, T_Typ); |
| 11078 |
end if; |
| 11079 |
end if; |
| 11080 |
end if; |
| 11081 |
|
| 11082 |
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then |
| 11083 |
if Is_Constrained (T_Typ) then |
| 11084 |
Expr_Actual := Get_Referenced_Object (Expr); |
| 11085 |
Exptyp := Get_Actual_Subtype (Expr_Actual); |
| 11086 |
|
| 11087 |
if Is_Access_Type (Exptyp) then |
| 11088 |
Exptyp := Designated_Type (Exptyp); |
| 11089 |
end if; |
| 11090 |
|
| 11091 |
-- String_Literal case. This needs to be handled specially be- |
| 11092 |
-- cause no index types are available for string literals. The |
| 11093 |
-- condition is simply: |
| 11094 |
|
| 11095 |
-- T_Typ'Length = string-literal-length |
| 11096 |
|
| 11097 |
if Nkind (Expr_Actual) = N_String_Literal then |
| 11098 |
null; |
| 11099 |
|
| 11100 |
-- General array case. Here we have a usable actual subtype for |
| 11101 |
-- the expression, and the condition is built from the two types |
| 11102 |
|
| 11103 |
-- T_Typ'First < Exptyp'First or else |
| 11104 |
-- T_Typ'Last > Exptyp'Last or else |
| 11105 |
-- T_Typ'First(1) < Exptyp'First(1) or else |
| 11106 |
-- T_Typ'Last(1) > Exptyp'Last(1) or else |
| 11107 |
-- ... |
| 11108 |
|
| 11109 |
elsif Is_Constrained (Exptyp) then |
| 11110 |
declare |
| 11111 |
Ndims : constant Pos := Number_Dimensions (T_Typ); |
| 11112 |
|
| 11113 |
L_Index : Node_Id; |
| 11114 |
R_Index : Node_Id; |
| 11115 |
|
| 11116 |
begin |
| 11117 |
L_Index := First_Index (T_Typ); |
| 11118 |
R_Index := First_Index (Exptyp); |
| 11119 |
|
| 11120 |
for Indx in 1 .. Ndims loop |
| 11121 |
if not (Nkind (L_Index) = N_Raise_Constraint_Error |
| 11122 |
or else |
| 11123 |
Nkind (R_Index) = N_Raise_Constraint_Error) |
| 11124 |
then |
| 11125 |
-- Deal with compile time length check. Note that we |
| 11126 |
-- skip this in the access case, because the access |
| 11127 |
-- value may be null, so we cannot know statically. |
| 11128 |
|
| 11129 |
if not |
| 11130 |
Subtypes_Statically_Match |
| 11131 |
(Etype (L_Index), Etype (R_Index)) |
| 11132 |
then |
| 11133 |
-- If the target type is constrained then we |
| 11134 |
-- have to check for exact equality of bounds |
| 11135 |
-- (required for qualified expressions). |
| 11136 |
|
| 11137 |
if Is_Constrained (T_Typ) then |
| 11138 |
Evolve_Or_Else |
| 11139 |
(Cond, |
| 11140 |
Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); |
| 11141 |
else |
| 11142 |
Evolve_Or_Else |
| 11143 |
(Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); |
| 11144 |
end if; |
| 11145 |
end if; |
| 11146 |
|
| 11147 |
Next (L_Index); |
| 11148 |
Next (R_Index); |
| 11149 |
end if; |
| 11150 |
end loop; |
| 11151 |
end; |
| 11152 |
|
| 11153 |
-- Handle cases where we do not get a usable actual subtype that |
| 11154 |
-- is constrained. This happens for example in the function call |
| 11155 |
-- and explicit dereference cases. In these cases, we have to get |
| 11156 |
-- the length or range from the expression itself, making sure we |
| 11157 |
-- do not evaluate it more than once. |
| 11158 |
|
| 11159 |
-- Here Expr is the original expression, or more properly the |
| 11160 |
-- result of applying Duplicate_Expr to the original tree, |
| 11161 |
-- forcing the result to be a name. |
| 11162 |
|
| 11163 |
else |
| 11164 |
declare |
| 11165 |
Ndims : constant Pos := Number_Dimensions (T_Typ); |
| 11166 |
|
| 11167 |
begin |
| 11168 |
-- Build the condition for the explicit dereference case |
| 11169 |
|
| 11170 |
for Indx in 1 .. Ndims loop |
| 11171 |
Evolve_Or_Else |
| 11172 |
(Cond, Range_N_Cond (Expr, T_Typ, Indx)); |
| 11173 |
end loop; |
| 11174 |
end; |
| 11175 |
end if; |
| 11176 |
|
| 11177 |
-- If the context is a qualified_expression where the subtype is |
| 11178 |
-- an unconstrained array subtype with fixed-lower-bound indexes, |
| 11179 |
-- then consistency checks must be done between the lower bounds |
| 11180 |
-- of any such indexes and the corresponding lower bounds of the |
| 11181 |
-- qualified array object. |
| 11182 |
|
| 11183 |
elsif Is_Fixed_Lower_Bound_Array_Subtype (T_Typ) |
| 11184 |
and then Nkind (Parent (Expr)) = N_Qualified_Expression |
| 11185 |
and then not Do_Access |
| 11186 |
then |
| 11187 |
declare |
| 11188 |
Ndims : constant Pos := Number_Dimensions (T_Typ); |
| 11189 |
|
| 11190 |
Qual_Index : Node_Id; |
| 11191 |
Expr_Index : Node_Id; |
| 11192 |
|
| 11193 |
begin |
| 11194 |
Expr_Actual := Get_Referenced_Object (Expr); |
| 11195 |
Exptyp := Get_Actual_Subtype (Expr_Actual); |
| 11196 |
|
| 11197 |
Qual_Index := First_Index (T_Typ); |
| 11198 |
Expr_Index := First_Index (Exptyp); |
| 11199 |
|
| 11200 |
for Indx in 1 .. Ndims loop |
| 11201 |
if Nkind (Expr_Index) /= N_Raise_Constraint_Error then |
| 11202 |
|
| 11203 |
-- If this index of the qualifying array subtype has |
| 11204 |
-- a fixed lower bound, then apply a check that the |
| 11205 |
-- corresponding lower bound of the array expression |
| 11206 |
-- is equal to it. |
| 11207 |
|
| 11208 |
if Is_Fixed_Lower_Bound_Index_Subtype (Etype (Qual_Index)) |
| 11209 |
then |
| 11210 |
Evolve_Or_Else |
| 11211 |
(Cond, |
| 11212 |
Make_Op_Ne (Loc, |
| 11213 |
Left_Opnd => |
| 11214 |
Get_E_First_Or_Last |
| 11215 |
(Loc, Exptyp, Indx, Name_First), |
| 11216 |
Right_Opnd => |
| 11217 |
New_Copy_Tree |
| 11218 |
(Type_Low_Bound (Etype (Qual_Index))))); |
| 11219 |
end if; |
| 11220 |
|
| 11221 |
Next (Qual_Index); |
| 11222 |
Next (Expr_Index); |
| 11223 |
end if; |
| 11224 |
end loop; |
| 11225 |
end; |
| 11226 |
|
| 11227 |
else |
| 11228 |
-- For a conversion to an unconstrained array type, generate an |
| 11229 |
-- Action to check that the bounds of the source value are within |
| 11230 |
-- the constraints imposed by the target type (RM 4.6(38)). No |
| 11231 |
-- check is needed for a conversion to an access to unconstrained |
| 11232 |
-- array type, as 4.6(24.15/2) requires the designated subtypes |
| 11233 |
-- of the two access types to statically match. |
| 11234 |
|
| 11235 |
if Nkind (Parent (Expr)) = N_Type_Conversion |
| 11236 |
and then not Do_Access |
| 11237 |
then |
| 11238 |
declare |
| 11239 |
Opnd_Index : Node_Id; |
| 11240 |
Targ_Index : Node_Id; |
| 11241 |
Opnd_Range : Node_Id; |
| 11242 |
|
| 11243 |
begin |
| 11244 |
Opnd_Index := First_Index (Get_Actual_Subtype (Expr)); |
| 11245 |
Targ_Index := First_Index (T_Typ); |
| 11246 |
while Present (Opnd_Index) loop |
| 11247 |
|
| 11248 |
-- If the index is a range, use its bounds. If it is an |
| 11249 |
-- entity (as will be the case if it is a named subtype |
| 11250 |
-- or an itype created for a slice) retrieve its range. |
| 11251 |
|
| 11252 |
if Is_Entity_Name (Opnd_Index) |
| 11253 |
and then Is_Type (Entity (Opnd_Index)) |
| 11254 |
then |
| 11255 |
Opnd_Range := Scalar_Range (Entity (Opnd_Index)); |
| 11256 |
else |
| 11257 |
Opnd_Range := Opnd_Index; |
| 11258 |
end if; |
| 11259 |
|
| 11260 |
if Nkind (Opnd_Range) = N_Range then |
| 11261 |
if Is_In_Range |
| 11262 |
(Low_Bound (Opnd_Range), Etype (Targ_Index), |
| 11263 |
Assume_Valid => True) |
| 11264 |
and then |
| 11265 |
Is_In_Range |
| 11266 |
(High_Bound (Opnd_Range), Etype (Targ_Index), |
| 11267 |
Assume_Valid => True) |
| 11268 |
then |
| 11269 |
null; |
| 11270 |
|
| 11271 |
-- If null range, no check needed |
| 11272 |
|
| 11273 |
elsif |
| 11274 |
Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
| 11275 |
and then |
| 11276 |
Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
| 11277 |
and then |
| 11278 |
Expr_Value (High_Bound (Opnd_Range)) < |
| 11279 |
Expr_Value (Low_Bound (Opnd_Range)) |
| 11280 |
then |
| 11281 |
null; |
| 11282 |
|
| 11283 |
elsif Is_Out_Of_Range |
| 11284 |
(Low_Bound (Opnd_Range), Etype (Targ_Index), |
| 11285 |
Assume_Valid => True) |
| 11286 |
or else |
| 11287 |
Is_Out_Of_Range |
| 11288 |
(High_Bound (Opnd_Range), Etype (Targ_Index), |
| 11289 |
Assume_Valid => True) |
| 11290 |
then |
| 11291 |
Add_Check |
| 11292 |
(Compile_Time_Constraint_Error |
| 11293 |
(Wnode, "value out of range of}??", T_Typ)); |
| 11294 |
|
| 11295 |
else |
| 11296 |
Evolve_Or_Else |
| 11297 |
(Cond, |
| 11298 |
Discrete_Range_Cond |
| 11299 |
(Opnd_Range, Etype (Targ_Index))); |
| 11300 |
end if; |
| 11301 |
end if; |
| 11302 |
|
| 11303 |
Next_Index (Opnd_Index); |
| 11304 |
Next_Index (Targ_Index); |
| 11305 |
end loop; |
| 11306 |
end; |
| 11307 |
end if; |
| 11308 |
end if; |
| 11309 |
end if; |
| 11310 |
|
| 11311 |
-- Construct the test and insert into the tree |
| 11312 |
|
| 11313 |
if Present (Cond) then |
| 11314 |
if Do_Access then |
| 11315 |
Cond := Guard_Access (Cond, Loc, Expr); |
| 11316 |
end if; |
| 11317 |
|
| 11318 |
Add_Check |
| 11319 |
(Make_Raise_Constraint_Error (Loc, |
| 11320 |
Condition => Cond, |
| 11321 |
Reason => CE_Range_Check_Failed)); |
| 11322 |
end if; |
| 11323 |
|
| 11324 |
return Ret_Result; |
| 11325 |
end Selected_Range_Checks; |
| 11326 |
|
| 11327 |
------------------------------- |
| 11328 |
-- Storage_Checks_Suppressed -- |
| 11329 |
------------------------------- |
| 11330 |
|
| 11331 |
function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 11332 |
begin |
| 11333 |
if Present (E) and then Checks_May_Be_Suppressed (E) then |
| 11334 |
return Is_Check_Suppressed (E, Storage_Check); |
| 11335 |
else |
| 11336 |
return Scope_Suppress.Suppress (Storage_Check); |
| 11337 |
end if; |
| 11338 |
end Storage_Checks_Suppressed; |
| 11339 |
|
| 11340 |
--------------------------- |
| 11341 |
-- Tag_Checks_Suppressed -- |
| 11342 |
--------------------------- |
| 11343 |
|
| 11344 |
function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is |
| 11345 |
begin |
| 11346 |
if Present (E) |
| 11347 |
and then Checks_May_Be_Suppressed (E) |
| 11348 |
then |
| 11349 |
return Is_Check_Suppressed (E, Tag_Check); |
| 11350 |
else |
| 11351 |
return Scope_Suppress.Suppress (Tag_Check); |
| 11352 |
end if; |
| 11353 |
end Tag_Checks_Suppressed; |
| 11354 |
|
| 11355 |
--------------------------------------- |
| 11356 |
-- Validate_Alignment_Check_Warnings -- |
| 11357 |
--------------------------------------- |
| 11358 |
|
| 11359 |
procedure Validate_Alignment_Check_Warnings is |
| 11360 |
begin |
| 11361 |
for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop |
| 11362 |
declare |
| 11363 |
AWR : Alignment_Warnings_Record |
| 11364 |
renames Alignment_Warnings.Table (J); |
| 11365 |
begin |
| 11366 |
if Known_Alignment (AWR.E) |
| 11367 |
and then ((Present (AWR.A) |
| 11368 |
and then AWR.A mod Alignment (AWR.E) = 0) |
| 11369 |
or else (Present (AWR.P) |
| 11370 |
and then Has_Compatible_Alignment |
| 11371 |
(AWR.E, AWR.P, True) = |
| 11372 |
Known_Compatible)) |
| 11373 |
then |
| 11374 |
Delete_Warning_And_Continuations (AWR.W); |
| 11375 |
end if; |
| 11376 |
end; |
| 11377 |
end loop; |
| 11378 |
end Validate_Alignment_Check_Warnings; |
| 11379 |
|
| 11380 |
-------------------------- |
| 11381 |
-- Validity_Check_Range -- |
| 11382 |
-------------------------- |
| 11383 |
|
| 11384 |
procedure Validity_Check_Range |
| 11385 |
(N : Node_Id; |
| 11386 |
Related_Id : Entity_Id := Empty) is |
| 11387 |
begin |
| 11388 |
if Validity_Checks_On and Validity_Check_Operands then |
| 11389 |
if Nkind (N) = N_Range then |
| 11390 |
Ensure_Valid |
| 11391 |
(Expr => Low_Bound (N), |
| 11392 |
Related_Id => Related_Id, |
| 11393 |
Is_Low_Bound => True); |
| 11394 |
|
| 11395 |
Ensure_Valid |
| 11396 |
(Expr => High_Bound (N), |
| 11397 |
Related_Id => Related_Id, |
| 11398 |
Is_High_Bound => True); |
| 11399 |
end if; |
| 11400 |
end if; |
| 11401 |
end Validity_Check_Range; |
| 11402 |
|
| 11403 |
end Checks; |