| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- C H E C K S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 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 |
-- Package containing routines used to deal with run-time checks. These |
| 27 |
-- routines are used both by the semantics and by the expander. In some |
| 28 |
-- cases, checks are enabled simply by setting a flag for the back end, |
| 29 |
-- and in other cases the code for the check is expanded. |
| 30 |
|
| 31 |
-- The approach used for range and length checks, in regards to suppressed |
| 32 |
-- checks, is to attempt to detect at compilation time that a constraint |
| 33 |
-- error will occur. If this is detected a warning or error is issued and the |
| 34 |
-- offending expression or statement replaced with a constraint error node. |
| 35 |
-- This always occurs whether checks are suppressed or not. Dynamic range |
| 36 |
-- checks are, of course, not inserted if checks are suppressed. |
| 37 |
|
| 38 |
with Errout; use Errout; |
| 39 |
with Namet; use Namet; |
| 40 |
with Table; |
| 41 |
with Types; use Types; |
| 42 |
with Uintp; use Uintp; |
| 43 |
with Urealp; use Urealp; |
| 44 |
|
| 45 |
package Checks is |
| 46 |
|
| 47 |
type Bit_Vector is array (Pos range <>) of Boolean; |
| 48 |
type Dimension_Set (Dimensions : Nat) is |
| 49 |
record |
| 50 |
Elements : Bit_Vector (1 .. Dimensions); |
| 51 |
end record; |
| 52 |
Empty_Dimension_Set : constant Dimension_Set := |
| 53 |
(Dimensions => 0, Elements => (others => <>)); |
| 54 |
|
| 55 |
procedure Initialize; |
| 56 |
-- Called for each new main source program, to initialize internal |
| 57 |
-- variables used in the package body of the Checks unit. |
| 58 |
|
| 59 |
function Access_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 60 |
function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 61 |
function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 62 |
function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 63 |
function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean; |
| 64 |
function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 65 |
function Division_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 66 |
function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 67 |
function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 68 |
function Index_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 69 |
function Length_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 70 |
function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 71 |
function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 72 |
function Raise_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 73 |
function Range_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 74 |
function Storage_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 75 |
function Tag_Checks_Suppressed (E : Entity_Id) return Boolean; |
| 76 |
-- These functions check to see if the named check is suppressed, either |
| 77 |
-- by an active scope suppress setting, or because the check has been |
| 78 |
-- specifically suppressed for the given entity. If no entity is relevant |
| 79 |
-- for the current check, then Empty is used as an argument. Note: the |
| 80 |
-- reason we insist on specifying Empty is to force the caller to think |
| 81 |
-- about whether there is any relevant entity that should be checked. |
| 82 |
|
| 83 |
function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean; |
| 84 |
-- This function is called if Checks_May_Be_Suppressed (E) is True to |
| 85 |
-- determine whether check C is suppressed either on the entity E or |
| 86 |
-- as the result of a scope suppress pragma. If Checks_May_Be_Suppressed |
| 87 |
-- is False, then the status of the check can be determined simply by |
| 88 |
-- examining Scope_Suppress, so this routine is not called in that case. |
| 89 |
|
| 90 |
function Overflow_Check_Mode return Overflow_Mode_Type; |
| 91 |
-- Returns current overflow checking mode, taking into account whether |
| 92 |
-- we are inside an assertion expression and the assertion policy. |
| 93 |
|
| 94 |
----------------------------------------- |
| 95 |
-- Control of Alignment Check Warnings -- |
| 96 |
----------------------------------------- |
| 97 |
|
| 98 |
-- When we have address clauses, there is an issue of whether the address |
| 99 |
-- specified is appropriate to the alignment. In the general case where the |
| 100 |
-- address is dynamic, we generate a check and a possible warning (this |
| 101 |
-- warning occurs for example if we have a restricted runtime with the |
| 102 |
-- restriction No_Exception_Propagation). We also issue this warning in |
| 103 |
-- the case where the address is static, but we don't know the alignment |
| 104 |
-- at the time we process the address clause. In such a case, we issue the |
| 105 |
-- warning, but we may be able to find out later (after the back end has |
| 106 |
-- annotated the actual alignment chosen) that the warning was not needed. |
| 107 |
|
| 108 |
-- To deal with deleting these potentially annoying warnings, we save the |
| 109 |
-- warning information in a table, and then delete the warnings in the |
| 110 |
-- post compilation validation stage if we can tell that the check would |
| 111 |
-- never fail (in general the back end will also optimize away the check |
| 112 |
-- in such cases). |
| 113 |
|
| 114 |
-- Table used to record information |
| 115 |
|
| 116 |
type Alignment_Warnings_Record is record |
| 117 |
E : Entity_Id; |
| 118 |
-- Entity whose alignment possibly warrants a warning |
| 119 |
|
| 120 |
A : Uint; |
| 121 |
-- Compile time known value of address clause for which the alignment |
| 122 |
-- is to be checked once we know the alignment. |
| 123 |
|
| 124 |
P : Node_Id; |
| 125 |
-- Prefix of address clause when it is of the form X'Address |
| 126 |
|
| 127 |
W : Error_Msg_Id; |
| 128 |
-- Id of warning message we might delete |
| 129 |
end record; |
| 130 |
|
| 131 |
package Alignment_Warnings is new Table.Table ( |
| 132 |
Table_Component_Type => Alignment_Warnings_Record, |
| 133 |
Table_Index_Type => Int, |
| 134 |
Table_Low_Bound => 0, |
| 135 |
Table_Initial => 10, |
| 136 |
Table_Increment => 200, |
| 137 |
Table_Name => "Alignment_Warnings"); |
| 138 |
|
| 139 |
procedure Validate_Alignment_Check_Warnings; |
| 140 |
-- This routine is called after back annotation of type data to delete any |
| 141 |
-- alignment warnings that turn out to be false alarms, based on knowing |
| 142 |
-- the actual alignment, and a compile-time known alignment value. |
| 143 |
|
| 144 |
------------------------------------------- |
| 145 |
-- Procedures to Activate Checking Flags -- |
| 146 |
------------------------------------------- |
| 147 |
|
| 148 |
procedure Activate_Division_Check (N : Node_Id); |
| 149 |
pragma Inline (Activate_Division_Check); |
| 150 |
-- Sets Do_Division_Check flag in node N, and handles possible local raise. |
| 151 |
-- Always call this routine rather than calling Set_Do_Division_Check to |
| 152 |
-- set an explicit value of True, to ensure handling the local raise case. |
| 153 |
|
| 154 |
procedure Activate_Overflow_Check (N : Node_Id); |
| 155 |
pragma Inline (Activate_Overflow_Check); |
| 156 |
-- Sets Do_Overflow_Check flag in node N, and handles possible local raise. |
| 157 |
-- Always call this routine rather than calling Set_Do_Overflow_Check to |
| 158 |
-- set an explicit value of True, to ensure handling the local raise case. |
| 159 |
-- Note that for discrete types, this call has no effect for MOD, REM, and |
| 160 |
-- unary "+" for which overflow is never possible in any case. |
| 161 |
-- |
| 162 |
-- Note: for the discrete-type case, it is legitimate to call this routine |
| 163 |
-- on an unanalyzed node where the Etype field is not set. However, for the |
| 164 |
-- floating-point case, Etype must be set (to a floating-point type). |
| 165 |
-- |
| 166 |
-- For floating-point, we set the flag if we have automatic overflow checks |
| 167 |
-- on the target, or if Check_Float_Overflow mode is set. For the floating- |
| 168 |
-- point case, we ignore all the unary operators ("+", "-", and abs) since |
| 169 |
-- none of these can result in overflow. If there are no overflow checks on |
| 170 |
-- the target, and Check_Float_Overflow mode is not set, then the call has |
| 171 |
-- no effect, since in such cases we want to generate NaN's and infinities. |
| 172 |
|
| 173 |
procedure Activate_Range_Check (N : Node_Id); |
| 174 |
pragma Inline (Activate_Range_Check); |
| 175 |
-- Sets Do_Range_Check flag in node N, and handles possible local raise. |
| 176 |
-- Always call this routine rather than calling Set_Do_Range_Check to |
| 177 |
-- set an explicit value of True, to ensure handling the local raise case. |
| 178 |
|
| 179 |
-------------------------------- |
| 180 |
-- Procedures to Apply Checks -- |
| 181 |
-------------------------------- |
| 182 |
|
| 183 |
-- General note on following checks. These checks are always active if |
| 184 |
-- Expander_Active and not Inside_A_Generic. They are inactive and have |
| 185 |
-- no effect Inside_A_Generic. In the case where not Expander_Active |
| 186 |
-- and not Inside_A_Generic, most of them are inactive, but some of them |
| 187 |
-- operate anyway since they may generate useful compile time warnings. |
| 188 |
|
| 189 |
procedure Apply_Access_Check (N : Node_Id); |
| 190 |
-- Determines whether an expression node requires a run-time access |
| 191 |
-- check and if so inserts the appropriate run-time check. |
| 192 |
|
| 193 |
procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id); |
| 194 |
-- E is the entity for an object which has an address clause. If checks |
| 195 |
-- are enabled, then this procedure generates a check that the specified |
| 196 |
-- address has an alignment consistent with the alignment of the object, |
| 197 |
-- raising PE if this is not the case. The resulting check (if one is |
| 198 |
-- generated) is prepended to the Actions list of N_Freeze_Entity node N. |
| 199 |
-- Note that the check references E'Alignment, so it cannot be emitted |
| 200 |
-- before N (its freeze node), otherwise this would cause an illegal |
| 201 |
-- access before elaboration error in gigi. For the case of a clear overlay |
| 202 |
-- situation, we also check that the size of the overlaying object is not |
| 203 |
-- larger than the overlaid object. |
| 204 |
|
| 205 |
procedure Apply_Arithmetic_Overflow_Check (N : Node_Id); |
| 206 |
-- Handle overflow checking for an arithmetic operator. Also handles the |
| 207 |
-- cases of ELIMINATED and MINIMIZED overflow checking mode. If the mode |
| 208 |
-- is one of the latter two, then this routine can also be called with |
| 209 |
-- an if or case expression node to make sure that we properly handle |
| 210 |
-- overflow checking for dependent expressions. This routine handles |
| 211 |
-- front end vs back end overflow checks (in the front end case it expands |
| 212 |
-- the necessary check). Note that divide is handled separately using |
| 213 |
-- Apply_Divide_Checks. Node N may or may not have Do_Overflow_Check. |
| 214 |
-- In STRICT mode, there is nothing to do if this flag is off, but in |
| 215 |
-- MINIMIZED/ELIMINATED mode we still have to deal with possible use |
| 216 |
-- of doing operations in Long_Long_Integer or Bignum mode. |
| 217 |
|
| 218 |
procedure Apply_Constraint_Check |
| 219 |
(N : Node_Id; |
| 220 |
Typ : Entity_Id; |
| 221 |
No_Sliding : Boolean := False); |
| 222 |
-- Top-level procedure, calls all the others depending on the class of |
| 223 |
-- Typ. Checks that expression N satisfies the constraint of type Typ. |
| 224 |
-- No_Sliding is only relevant for constrained array types, if set to |
| 225 |
-- True, it checks that indexes are in range. |
| 226 |
|
| 227 |
procedure Apply_Discriminant_Check |
| 228 |
(N : Node_Id; |
| 229 |
Typ : Entity_Id; |
| 230 |
Lhs : Node_Id := Empty); |
| 231 |
-- Given an expression N of a discriminated type, or of an access type |
| 232 |
-- whose designated type is a discriminanted type, generates a check to |
| 233 |
-- ensure that the expression can be converted to the subtype given as |
| 234 |
-- the second parameter. Lhs is empty except in the case of assignments, |
| 235 |
-- where the target object may be needed to determine the subtype to |
| 236 |
-- check against (such as the cases of unconstrained formal parameters |
| 237 |
-- and unconstrained aliased objects). For the case of unconstrained |
| 238 |
-- formals, the check is performed only if the corresponding actual is |
| 239 |
-- constrained, i.e., whether Lhs'Constrained is True. |
| 240 |
|
| 241 |
procedure Apply_Divide_Checks (N : Node_Id); |
| 242 |
-- The node kind is N_Op_Divide, N_Op_Mod, or N_Op_Rem if either of the |
| 243 |
-- flags Do_Division_Check or Do_Overflow_Check is set, then this routine |
| 244 |
-- ensures that the appropriate checks are made. Note that overflow can |
| 245 |
-- occur in the signed case for the case of the largest negative number |
| 246 |
-- divided by minus one. This procedure only applies to Integer types. |
| 247 |
|
| 248 |
procedure Apply_Parameter_Aliasing_Checks |
| 249 |
(Call : Node_Id; |
| 250 |
Subp : Entity_Id); |
| 251 |
-- Given a subprogram call Call, add a check to verify that none of the |
| 252 |
-- actuals overlap. Subp denotes the subprogram being called. |
| 253 |
|
| 254 |
procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id); |
| 255 |
-- Given a subprogram Subp, add both a pre and post condition pragmas that |
| 256 |
-- verify the proper initialization of scalars in parameters and function |
| 257 |
-- results. |
| 258 |
|
| 259 |
procedure Apply_Predicate_Check |
| 260 |
(N : Node_Id; |
| 261 |
Typ : Entity_Id; |
| 262 |
Deref : Boolean := False; |
| 263 |
Fun : Entity_Id := Empty); |
| 264 |
-- N is an expression to which a predicate check may need to be applied for |
| 265 |
-- Typ if Typ has a predicate function, after dereference if Deref is True. |
| 266 |
-- When N is an actual in a call, Fun is the function being called, which |
| 267 |
-- is used to generate a warning if the call leads to infinite recursion. |
| 268 |
|
| 269 |
procedure Apply_Raise_Check (N : Node_Id); |
| 270 |
-- N is an N_Subprogram_Body node. Apply a raise check to N if its entity |
| 271 |
-- is subject to the GNAT aspect/pragma No_Raise. |
| 272 |
|
| 273 |
procedure Apply_Type_Conversion_Checks (N : Node_Id); |
| 274 |
-- N is an N_Type_Conversion node. A type conversion actually involves |
| 275 |
-- two sorts of checks. The first check is the checks that ensures that |
| 276 |
-- the operand in the type conversion fits onto the base type of the |
| 277 |
-- subtype it is being converted to (see RM 4.6 (28)-(50)). The second |
| 278 |
-- check is there to ensure that once the operand has been converted to |
| 279 |
-- a value of the target type, this converted value meets the |
| 280 |
-- constraints imposed by the target subtype (see RM 4.6 (51)). |
| 281 |
|
| 282 |
procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id); |
| 283 |
-- The argument N is an attribute reference node intended for processing |
| 284 |
-- by gigi. The attribute is one that returns a universal integer, but |
| 285 |
-- the attribute reference node is currently typed with the expected |
| 286 |
-- result type. This routine deals with range and overflow checks needed |
| 287 |
-- to make sure that the universal result is in range. |
| 288 |
|
| 289 |
function Build_Discriminant_Checks |
| 290 |
(N : Node_Id; |
| 291 |
T_Typ : Entity_Id) |
| 292 |
return Node_Id; |
| 293 |
-- Subsidiary routine for Apply_Discriminant_Check. Builds the expression |
| 294 |
-- that compares discriminants of the expression with discriminants of the |
| 295 |
-- type. Also used directly for membership tests (see Exp_Ch4.Expand_N_In). |
| 296 |
|
| 297 |
function Convert_From_Bignum (N : Node_Id) return Node_Id; |
| 298 |
-- Returns result of converting node N from Bignum. The returned value is |
| 299 |
-- not analyzed, the caller takes responsibility for this. Node N must be |
| 300 |
-- a subexpression node of type Bignum. The result is Long_Long_Integer. |
| 301 |
|
| 302 |
function Convert_To_Bignum (N : Node_Id) return Node_Id; |
| 303 |
-- Returns result of converting node N to Bignum. The returned value is not |
| 304 |
-- analyzed, the caller takes responsibility for this. Node N must be a |
| 305 |
-- subexpression node of a signed integer type or Bignum type (if it is |
| 306 |
-- already a Bignum, the returned value is Relocate_Node (N)). |
| 307 |
|
| 308 |
procedure Determine_Range |
| 309 |
(N : Node_Id; |
| 310 |
OK : out Boolean; |
| 311 |
Lo : out Uint; |
| 312 |
Hi : out Uint; |
| 313 |
Assume_Valid : Boolean := False); |
| 314 |
-- N is a node for a subexpression. If N is of a discrete type with no |
| 315 |
-- error indications, and no other peculiarities (e.g. missing Etype), |
| 316 |
-- then OK is True on return, and Lo and Hi are set to a conservative |
| 317 |
-- estimate of the possible range of values of N. Thus if OK is True on |
| 318 |
-- return, the value of the subexpression N is known to lie in the range |
| 319 |
-- Lo .. Hi (inclusive). For enumeration and character literals the values |
| 320 |
-- returned are the Pos value in the relevant enumeration type. If the |
| 321 |
-- expression is not of a discrete type, or some kind of error condition |
| 322 |
-- is detected, then OK is False on exit, and Lo/Hi are set to No_Uint. |
| 323 |
-- Thus the significance of OK being False on return is that no useful |
| 324 |
-- information is available on the range of the expression. Assume_Valid |
| 325 |
-- determines whether the processing is allowed to assume that values are |
| 326 |
-- in range of their subtypes. If it is set to True, then this assumption |
| 327 |
-- is valid, if False, then processing is done using base types to allow |
| 328 |
-- invalid values. |
| 329 |
|
| 330 |
procedure Determine_Range_R |
| 331 |
(N : Node_Id; |
| 332 |
OK : out Boolean; |
| 333 |
Lo : out Ureal; |
| 334 |
Hi : out Ureal; |
| 335 |
Assume_Valid : Boolean := False); |
| 336 |
-- Similar to Determine_Range, but for a node N of floating-point type. OK |
| 337 |
-- is True on return only for IEEE floating-point types and only if we do |
| 338 |
-- not have to worry about extended precision (i.e. on the x86, we must be |
| 339 |
-- using -msse2 -mfpmath=sse). At the current time, this is used only in |
| 340 |
-- GNATprove, though we could consider using it more generally in future. |
| 341 |
-- For that to happen, the possibility of arguments of infinite or NaN |
| 342 |
-- value should be taken into account, which is not the case currently. |
| 343 |
|
| 344 |
procedure Determine_Range_To_Discrete |
| 345 |
(N : Node_Id; |
| 346 |
OK : out Boolean; |
| 347 |
Lo : out Uint; |
| 348 |
Hi : out Uint; |
| 349 |
Fixed_Int : Boolean := False; |
| 350 |
Assume_Valid : Boolean := False); |
| 351 |
-- Similar to Determine_Range, but attempts to return a discrete range even |
| 352 |
-- if N is not of a discrete type by doing a conversion. The Fixed_Int flag |
| 353 |
-- if set causes any fixed-point values to be treated as though they were |
| 354 |
-- discrete values (i.e. the underlying integer value is used), in which |
| 355 |
-- case no conversion is needed. At the current time, this is used only for |
| 356 |
-- discrete types, for fixed-point types if Fixed_Int is set, and also for |
| 357 |
-- floating-point types in GNATprove, see Determine_Range_R above. |
| 358 |
|
| 359 |
procedure Install_Null_Excluding_Check (N : Node_Id); |
| 360 |
-- Determines whether an access node requires a run-time access check and |
| 361 |
-- if so inserts the appropriate run-time check. |
| 362 |
|
| 363 |
procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id); |
| 364 |
-- Insert a check to ensure that subprogram body Subp_Body has been |
| 365 |
-- properly elaborated. The check is installed only when Subp_Body is the |
| 366 |
-- body of a nonabstract library-level primitive of a tagged type. Further |
| 367 |
-- restrictions may apply, see the body for details. |
| 368 |
|
| 369 |
function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id; |
| 370 |
-- This function is used by top level overflow checking routines to do a |
| 371 |
-- mark/release operation on the secondary stack around bignum operations. |
| 372 |
-- The block created looks like: |
| 373 |
-- |
| 374 |
-- declare |
| 375 |
-- M : Mark_Id := SS_Mark; |
| 376 |
-- begin |
| 377 |
-- SS_Release (M); |
| 378 |
-- end; |
| 379 |
-- |
| 380 |
-- The idea is that the caller will insert any needed extra declarations |
| 381 |
-- after the declaration of M, and any needed statements (in particular |
| 382 |
-- the bignum operations) before the call to SS_Release, and then do an |
| 383 |
-- Insert_Action of the whole block (it is returned unanalyzed). The Loc |
| 384 |
-- parameter is used to supply Sloc values for the constructed tree. |
| 385 |
|
| 386 |
procedure Minimize_Eliminate_Overflows |
| 387 |
(N : Node_Id; |
| 388 |
Lo : out Uint; |
| 389 |
Hi : out Uint; |
| 390 |
Top_Level : Boolean); |
| 391 |
-- This is the main routine for handling MINIMIZED and ELIMINATED overflow |
| 392 |
-- processing. On entry N is a node whose result is a signed integer |
| 393 |
-- subtype. The Do_Overflow_Check flag may or may not be set on N. If the |
| 394 |
-- node is an arithmetic operation, then a range analysis is carried out, |
| 395 |
-- and there are three possibilities: |
| 396 |
-- |
| 397 |
-- The node is left unchanged (apart from expansion of an exponentiation |
| 398 |
-- operation). This happens if the routine can determine that the result |
| 399 |
-- is definitely in range. The Do_Overflow_Check flag is turned off in |
| 400 |
-- this case. |
| 401 |
-- |
| 402 |
-- The node is transformed into an arithmetic operation with a result |
| 403 |
-- type of Long_Long_Integer. |
| 404 |
-- |
| 405 |
-- The node is transformed into a function call that calls an appropriate |
| 406 |
-- function in the System.Bignums package to compute a Bignum result. |
| 407 |
-- |
| 408 |
-- In the first two cases, Lo and Hi are set to the bounds of the possible |
| 409 |
-- range of results, computed as accurately as possible. In the third case |
| 410 |
-- Lo and Hi are set to No_Uint (there are some cases where we could get an |
| 411 |
-- advantage from keeping result ranges for Bignum values, but it could use |
| 412 |
-- a lot of space and is very unlikely to be valuable). |
| 413 |
-- |
| 414 |
-- If the node is not an arithmetic operation, then it is unchanged but |
| 415 |
-- Lo and Hi are still set (to the bounds of the result subtype if nothing |
| 416 |
-- better can be determined). |
| 417 |
-- |
| 418 |
-- Note: this function is recursive, if called with an arithmetic operator, |
| 419 |
-- recursive calls are made to process the operands using this procedure. |
| 420 |
-- So we end up doing things top down. Nothing happens to an arithmetic |
| 421 |
-- expression until this procedure is called on the top level node and |
| 422 |
-- then the recursive calls process all the children. We have to do it |
| 423 |
-- this way. If we try to do it bottom up in natural expansion order, then |
| 424 |
-- there are two problems. First, where do we stash the bounds, and more |
| 425 |
-- importantly, semantic processing will be messed up. Consider A+B+C where |
| 426 |
-- A,B,C are all of type integer, if we processed A+B before doing semantic |
| 427 |
-- analysis of the addition of this result to C, that addition could end up |
| 428 |
-- with a Long_Long_Integer left operand and an Integer right operand, and |
| 429 |
-- we would get a semantic error. |
| 430 |
-- |
| 431 |
-- The routine is called in three situations if we are operating in either |
| 432 |
-- MINIMIZED or ELIMINATED modes. |
| 433 |
-- |
| 434 |
-- Overflow processing applied to the top node of an expression tree when |
| 435 |
-- that node is an arithmetic operator. In this case the result is |
| 436 |
-- converted to the appropriate result type (there is special processing |
| 437 |
-- when the parent is a conversion, see body for details). |
| 438 |
-- |
| 439 |
-- Overflow processing applied to the operands of a comparison operation. |
| 440 |
-- In this case, the comparison is done on the result Long_Long_Integer |
| 441 |
-- or Bignum values, without raising any exceptions. |
| 442 |
-- |
| 443 |
-- Overflow processing applied to the left operand of a membership test. |
| 444 |
-- In this case no exception is raised if a Long_Long_Integer or Bignum |
| 445 |
-- result is outside the range of the type of that left operand (it is |
| 446 |
-- just that the result of IN is false in that case). |
| 447 |
-- |
| 448 |
-- Note that if Bignum values appear, the caller must take care of doing |
| 449 |
-- the appropriate mark/release operations on the secondary stack. |
| 450 |
-- |
| 451 |
-- Top_Level is used to avoid inefficient unnecessary transitions into the |
| 452 |
-- Bignum domain. If Top_Level is True, it means that the caller will have |
| 453 |
-- to convert any Bignum value back to Long_Long_Integer, possibly checking |
| 454 |
-- that the value is in range. This is the normal case for a top level |
| 455 |
-- operator in a subexpression. There is no point in going into Bignum mode |
| 456 |
-- to avoid an overflow just so we can check for overflow the next moment. |
| 457 |
-- For calls from comparisons and membership tests, and for all recursive |
| 458 |
-- calls, we do want to transition into the Bignum domain if necessary. |
| 459 |
-- Note that this setting is only relevant in ELIMINATED mode. |
| 460 |
|
| 461 |
------------------------------------------------------- |
| 462 |
-- Control and Optimization of Range/Overflow Checks -- |
| 463 |
------------------------------------------------------- |
| 464 |
|
| 465 |
-- Range checks are controlled by the Do_Range_Check flag. The front end |
| 466 |
-- is responsible for setting this flag in relevant nodes. Originally the |
| 467 |
-- back end generated all the corresponding range checks, but later on we |
| 468 |
-- decided to generate all the range checks in the front end and this is |
| 469 |
-- the current situation. |
| 470 |
|
| 471 |
-- Overflow checks are similarly controlled by the Do_Overflow_Check flag. |
| 472 |
-- The difference here is that if back end overflow checks are inactive |
| 473 |
-- (Backend_Overflow_Checks_On_Target set False), then the actual overflow |
| 474 |
-- checks are generated by the front end, but if back end overflow checks |
| 475 |
-- are active (Backend_Overflow_Checks_On_Target set True), then the back |
| 476 |
-- end does generate the checks. |
| 477 |
|
| 478 |
-- The following two routines are used to set these flags, they allow |
| 479 |
-- for the possibility of eliminating checks. Checks can be eliminated |
| 480 |
-- if an identical check has already been performed. |
| 481 |
|
| 482 |
procedure Enable_Overflow_Check (N : Node_Id); |
| 483 |
-- First this routine determines if an overflow check is needed by doing |
| 484 |
-- an appropriate range check. If a check is not needed, then the call |
| 485 |
-- has no effect. If a check is needed then this routine sets the flag |
| 486 |
-- Do_Overflow_Check in node N to True, unless it can be determined that |
| 487 |
-- the check is not needed. The only condition under which this is the |
| 488 |
-- case is if there was an identical check earlier on. |
| 489 |
|
| 490 |
procedure Enable_Range_Check (N : Node_Id); |
| 491 |
-- Set Do_Range_Check flag in node N True, unless it can be determined |
| 492 |
-- that the check is not needed. The only condition under which this is |
| 493 |
-- the case is if there was an identical check earlier on. This routine |
| 494 |
-- is not responsible for doing range analysis to determine whether or |
| 495 |
-- not such a check is needed -- the caller is expected to do this. The |
| 496 |
-- one other case in which the request to set the flag is ignored is |
| 497 |
-- when Kill_Range_Check is set in an N_Unchecked_Conversion node. |
| 498 |
|
| 499 |
-- The following routines are used to keep track of processing sequences |
| 500 |
-- of statements (e.g. the THEN statements of an IF statement). A check |
| 501 |
-- that appears within such a sequence can eliminate an identical check |
| 502 |
-- within this sequence of statements. However, after the end of the |
| 503 |
-- sequence of statements, such a check is no longer of interest, since |
| 504 |
-- it may not have been executed. |
| 505 |
|
| 506 |
procedure Conditional_Statements_Begin; |
| 507 |
-- This call marks the start of processing of a sequence of statements. |
| 508 |
-- Every call to this procedure must be followed by a matching call to |
| 509 |
-- Conditional_Statements_End. |
| 510 |
|
| 511 |
procedure Conditional_Statements_End; |
| 512 |
-- This call removes from consideration all saved checks since the |
| 513 |
-- corresponding call to Conditional_Statements_Begin. These two |
| 514 |
-- procedures operate in a stack like manner. |
| 515 |
|
| 516 |
-- The mechanism for optimizing checks works by remembering checks |
| 517 |
-- that have already been made, but certain conditions, for example |
| 518 |
-- an assignment to a variable involved in a check, may mean that the |
| 519 |
-- remembered check is no longer valid, in the sense that if the same |
| 520 |
-- expression appears again, another check is required because the |
| 521 |
-- value may have changed. |
| 522 |
|
| 523 |
-- The following routines are used to note conditions which may render |
| 524 |
-- some or all of the stored and remembered checks to be invalidated. |
| 525 |
|
| 526 |
procedure Kill_Checks (V : Entity_Id); |
| 527 |
-- This procedure records an assignment or other condition that causes |
| 528 |
-- the value of the variable to be changed, invalidating any stored |
| 529 |
-- checks that reference the value. Note that all such checks must |
| 530 |
-- be discarded, even if they are not in the current statement range. |
| 531 |
|
| 532 |
procedure Kill_All_Checks; |
| 533 |
-- This procedure kills all remembered checks |
| 534 |
|
| 535 |
----------------------------- |
| 536 |
-- Length and Range Checks -- |
| 537 |
----------------------------- |
| 538 |
|
| 539 |
-- In the following procedures, there are three arguments which have |
| 540 |
-- a common meaning as follows: |
| 541 |
|
| 542 |
-- Expr The expression to be checked. If a check is required, |
| 543 |
-- the appropriate flag will be placed on this node. Whether |
| 544 |
-- this node is further examined depends on the setting of |
| 545 |
-- the parameter Source_Typ, as described below. |
| 546 |
|
| 547 |
-- Target_Typ The target type on which the check is to be based. For |
| 548 |
-- example, if we have a scalar range check, then the check |
| 549 |
-- is that we are in range of this type. |
| 550 |
|
| 551 |
-- Source_Typ Normally Empty, but can be set to a type, in which case |
| 552 |
-- this type is used for the check, see below. |
| 553 |
|
| 554 |
-- The checks operate in one of two modes: |
| 555 |
|
| 556 |
-- If Source_Typ is Empty, then the node Expr is examined, at the very |
| 557 |
-- least to get the source subtype. In addition for some of the checks, |
| 558 |
-- the actual form of the node may be examined. For example, a node of |
| 559 |
-- type Integer whose actual form is an Integer conversion from a type |
| 560 |
-- with range 0 .. 3 can be determined to have a value in range 0 .. 3. |
| 561 |
|
| 562 |
-- If Source_Typ is given, then nothing can be assumed about the Expr, |
| 563 |
-- and indeed its contents are not examined. In this case the check is |
| 564 |
-- based on the assumption that Expr can be an arbitrary value of the |
| 565 |
-- given Source_Typ. |
| 566 |
|
| 567 |
-- Currently, the only case in which a Source_Typ is explicitly supplied |
| 568 |
-- is for the case of Out and In_Out parameters, where, for the conversion |
| 569 |
-- on return (the Out direction), the types must be reversed. This is |
| 570 |
-- handled by the caller. |
| 571 |
|
| 572 |
procedure Apply_Length_Check |
| 573 |
(Expr : Node_Id; |
| 574 |
Target_Typ : Entity_Id; |
| 575 |
Source_Typ : Entity_Id := Empty); |
| 576 |
-- This procedure builds a sequence of declarations to do a length check |
| 577 |
-- that checks if the lengths of the two arrays Target_Typ and source type |
| 578 |
-- are the same. The resulting actions are inserted at Node using a call |
| 579 |
-- to Insert_Actions. |
| 580 |
-- |
| 581 |
-- For access types, the Directly_Designated_Type is retrieved and |
| 582 |
-- processing continues as enumerated above, with a guard against null |
| 583 |
-- values. |
| 584 |
-- |
| 585 |
-- Note: calls to Apply_Length_Check currently never supply an explicit |
| 586 |
-- Source_Typ parameter, but Apply_Length_Check takes this parameter and |
| 587 |
-- processes it as described above for consistency with the other routines |
| 588 |
-- in this section. |
| 589 |
|
| 590 |
procedure Apply_Length_Check_On_Assignment |
| 591 |
(Expr : Node_Id; |
| 592 |
Target_Typ : Entity_Id; |
| 593 |
Target : Node_Id; |
| 594 |
Source_Typ : Entity_Id := Empty); |
| 595 |
-- Similar to Apply_Length_Check, but takes the target of an assignment for |
| 596 |
-- which the check is to be done. Used to filter out specific cases where |
| 597 |
-- the check is superfluous. |
| 598 |
|
| 599 |
procedure Apply_Static_Length_Check |
| 600 |
(Expr : Node_Id; |
| 601 |
Target_Typ : Entity_Id; |
| 602 |
Source_Typ : Entity_Id := Empty); |
| 603 |
-- Tries to determine statically whether the two array types source type |
| 604 |
-- and Target_Typ have the same length. If it can be determined at compile |
| 605 |
-- time that they do not, then an N_Raise_Constraint_Error node replaces |
| 606 |
-- Expr, and a warning message is issued. |
| 607 |
|
| 608 |
procedure Apply_Range_Check |
| 609 |
(Expr : Node_Id; |
| 610 |
Target_Typ : Entity_Id; |
| 611 |
Source_Typ : Entity_Id := Empty; |
| 612 |
Insert_Node : Node_Id := Empty); |
| 613 |
-- For a Node of kind N_Range, constructs a range check action that tests |
| 614 |
-- first that the range is not null and then that the range is contained in |
| 615 |
-- the Target_Typ range. |
| 616 |
-- |
| 617 |
-- For scalar types, constructs a range check action that first tests that |
| 618 |
-- the expression is contained in the Target_Typ range. The difference |
| 619 |
-- between this and Apply_Scalar_Range_Check is that the latter generates |
| 620 |
-- the actual checking code against the Etype of the expression. |
| 621 |
-- |
| 622 |
-- For constrained array types, construct series of range check actions |
| 623 |
-- to check that each Expr range is properly contained in the range of |
| 624 |
-- Target_Typ. |
| 625 |
-- |
| 626 |
-- For a type conversion to an unconstrained array type, constructs a range |
| 627 |
-- check action to check that the bounds of the source type are within the |
| 628 |
-- constraints imposed by the Target_Typ. |
| 629 |
-- |
| 630 |
-- For access types, the Directly_Designated_Type is retrieved and |
| 631 |
-- processing continues as enumerated above, with a guard against null |
| 632 |
-- values. |
| 633 |
-- |
| 634 |
-- The source type is used by type conversions to unconstrained array |
| 635 |
-- types to retrieve the corresponding bounds. |
| 636 |
|
| 637 |
-- Insert_Node indicates the node where the check should be inserted. |
| 638 |
-- If it is empty, then the check is inserted directly at Expr instead. |
| 639 |
|
| 640 |
procedure Apply_Scalar_Range_Check |
| 641 |
(Expr : Node_Id; |
| 642 |
Target_Typ : Entity_Id; |
| 643 |
Source_Typ : Entity_Id := Empty; |
| 644 |
Fixed_Int : Boolean := False); |
| 645 |
-- For scalar types, determines whether an expression node should be |
| 646 |
-- flagged as needing a run-time range check. If the node requires such a |
| 647 |
-- check, the Do_Range_Check flag is turned on. The Fixed_Int flag if set |
| 648 |
-- causes any fixed-point values to be treated as though they were discrete |
| 649 |
-- values (i.e. the underlying integer value is used). |
| 650 |
|
| 651 |
type Check_Result is private; |
| 652 |
-- Type used to return result of Get_Range_Checks call, for later use in |
| 653 |
-- call to Insert_Range_Checks procedure. |
| 654 |
|
| 655 |
function Get_Range_Checks |
| 656 |
(Expr : Node_Id; |
| 657 |
Target_Typ : Entity_Id; |
| 658 |
Source_Typ : Entity_Id := Empty; |
| 659 |
Warn_Node : Node_Id := Empty) return Check_Result; |
| 660 |
-- Like Apply_Range_Check, except it does not modify anything. Instead |
| 661 |
-- it returns an encapsulated result of the check operations for later |
| 662 |
-- use in a call to Insert_Range_Checks. If Warn_Node is non-empty, its |
| 663 |
-- Sloc is used, in the static case, for the generated warning or error. |
| 664 |
-- Additionally, it is used rather than Expr (or Low/High_Bound of Expr) |
| 665 |
-- in constructing the check. |
| 666 |
|
| 667 |
procedure Append_Range_Checks |
| 668 |
(Checks : Check_Result; |
| 669 |
Stmts : List_Id; |
| 670 |
Suppress_Typ : Entity_Id; |
| 671 |
Static_Sloc : Source_Ptr); |
| 672 |
-- Called to append range checks as returned by a call to Get_Range_Checks. |
| 673 |
-- Stmts is a list to which either the dynamic check is appended or the |
| 674 |
-- raise Constraint_Error statement is appended (for static checks). |
| 675 |
-- Suppress_Typ is the type to check to determine if checks are suppressed. |
| 676 |
-- Static_Sloc is the Sloc at which the raise CE node points. |
| 677 |
|
| 678 |
procedure Insert_Range_Checks |
| 679 |
(Checks : Check_Result; |
| 680 |
Node : Node_Id; |
| 681 |
Suppress_Typ : Entity_Id; |
| 682 |
Static_Sloc : Source_Ptr; |
| 683 |
Do_Before : Boolean := False); |
| 684 |
-- Called to insert range checks as returned by a call to Get_Range_Checks. |
| 685 |
-- Node is the node after which either the dynamic check is inserted or |
| 686 |
-- the raise Constraint_Error statement is inserted (for static checks). |
| 687 |
-- Suppress_Typ is the type to check to determine if checks are suppressed. |
| 688 |
-- Static_Sloc is the Sloc at which the raise CE node points. Normally the |
| 689 |
-- checks are inserted after Node; if Do_Before is True, they are before. |
| 690 |
|
| 691 |
----------------------- |
| 692 |
-- Expander Routines -- |
| 693 |
----------------------- |
| 694 |
|
| 695 |
-- In most cases, the processing for range checks done by semantic analysis |
| 696 |
-- only results in setting the Do_Range_Check flag, rather than actually |
| 697 |
-- generating checks. The following routines must be called later on in the |
| 698 |
-- expansion process upon seeing the Do_Range_Check flag; they generate the |
| 699 |
-- actual checks and reset the flag. The remaining cases where range checks |
| 700 |
-- are still directly generated during semantic analysis occur as part of |
| 701 |
-- the processing of constraints in (sub)type and object declarations. |
| 702 |
|
| 703 |
procedure Generate_Range_Check |
| 704 |
(N : Node_Id; |
| 705 |
Target_Type : Entity_Id; |
| 706 |
Reason : RT_Exception_Code); |
| 707 |
-- This procedure is called to actually generate and insert a range check. |
| 708 |
-- A check is generated to ensure that the value of N lies within the range |
| 709 |
-- of the target type. Note that the base type of N may be different from |
| 710 |
-- the base type of the target type. This happens in the conversion case. |
| 711 |
-- The Reason parameter is the exception code to be used for the exception |
| 712 |
-- if raised. |
| 713 |
-- |
| 714 |
-- Note: if the expander is not active, or if we are in GNATprove mode, |
| 715 |
-- then we do not generate explicit range checks. Instead we just turn the |
| 716 |
-- Do_Range_Check flag on, since in these cases that's what we want to see |
| 717 |
-- in the tree (GNATprove in particular depends on this flag being set). If |
| 718 |
-- we generate the actual range checks, then we make sure the flag is off |
| 719 |
-- afterward, since the code we generate takes complete care of the checks. |
| 720 |
-- |
| 721 |
-- Historical note: We used to just pass on the Do_Range_Check flag to the |
| 722 |
-- back end to generate the check, but now in code-generation mode we never |
| 723 |
-- have this flag set, since the front end takes care of the check. The |
| 724 |
-- normal processing flow now is that the analyzer typically turns on the |
| 725 |
-- Do_Range_Check flag, and if it is set, this routine is called, which |
| 726 |
-- turns the flag off in code-generation mode. |
| 727 |
|
| 728 |
procedure Generate_Index_Checks |
| 729 |
(N : Node_Id; |
| 730 |
Checks_Generated : out Dimension_Set); |
| 731 |
-- This procedure is called to generate index checks on the subscripts for |
| 732 |
-- the indexed component node N. Each subscript expression is examined, and |
| 733 |
-- if the Do_Range_Check flag is set, an appropriate index check is |
| 734 |
-- generated and the flag is reset. |
| 735 |
-- The out-mode parameter Checks_Generated indicates the dimensions for |
| 736 |
-- which checks were generated. Checks_Generated.Dimensions must match |
| 737 |
-- the number of dimensions of the array type. |
| 738 |
|
| 739 |
-- Similarly, we set the flag Do_Discriminant_Check in the semantic |
| 740 |
-- analysis to indicate that a discriminant check is required for selected |
| 741 |
-- component of a discriminated type. The following routine is called from |
| 742 |
-- the expander to actually generate the call. |
| 743 |
|
| 744 |
procedure Generate_Discriminant_Check (N : Node_Id); |
| 745 |
-- N is a selected component for which a discriminant check is required to |
| 746 |
-- make sure that the discriminants have appropriate values for the |
| 747 |
-- selection. This is done by calling the appropriate discriminant checking |
| 748 |
-- routine for the selector. |
| 749 |
|
| 750 |
----------------------- |
| 751 |
-- Validity Checking -- |
| 752 |
----------------------- |
| 753 |
|
| 754 |
-- In (RM 13.9.1(9-11)) we have the following rules on invalid values |
| 755 |
|
| 756 |
-- If the representation of a scalar object does not represent value of |
| 757 |
-- the object's subtype (perhaps because the object was not initialized), |
| 758 |
-- the object is said to have an invalid representation. It is a bounded |
| 759 |
-- error to evaluate the value of such an object. If the error is |
| 760 |
-- detected, either Constraint_Error or Program_Error is raised. |
| 761 |
-- Otherwise, execution continues using the invalid representation. The |
| 762 |
-- rules of the language outside this subclause assume that all objects |
| 763 |
-- have valid representations. The semantics of operations on invalid |
| 764 |
-- representations are as follows: |
| 765 |
-- |
| 766 |
-- 10 If the representation of the object represents a value of the |
| 767 |
-- object's type, the value of the type is used. |
| 768 |
-- |
| 769 |
-- 11 If the representation of the object does not represent a value |
| 770 |
-- of the object's type, the semantics of operations on such |
| 771 |
-- representations is implementation-defined, but does not by |
| 772 |
-- itself lead to erroneous or unpredictable execution, or to |
| 773 |
-- other objects becoming abnormal. |
| 774 |
|
| 775 |
-- We quote the rules in full here since they are quite delicate. |
| 776 |
-- (???The rules quoted here are obsolete; see the GNAT User's Guide for a |
| 777 |
-- description of all the -gnatV switches.) Most of the time, we can just |
| 778 |
-- compute away with wrong values, and get a possibly wrong result, which |
| 779 |
-- is well within the range of allowed implementation defined behavior. The |
| 780 |
-- two tricky cases are subscripted array assignments, where we don't want |
| 781 |
-- to do wild stores, and case statements where we don't want to do wild |
| 782 |
-- jumps. |
| 783 |
|
| 784 |
-- In GNAT, we control validity checking with a switch -gnatV that can take |
| 785 |
-- three parameters, n/d/f for None/Default/Full. These modes have the |
| 786 |
-- following meanings: |
| 787 |
|
| 788 |
-- None (no validity checking) |
| 789 |
|
| 790 |
-- In this mode, there is no specific checking for invalid values |
| 791 |
-- and the code generator assumes that all stored values are always |
| 792 |
-- within the bounds of the object subtype. The consequences are as |
| 793 |
-- follows: |
| 794 |
|
| 795 |
-- For case statements, an out of range invalid value will cause |
| 796 |
-- Constraint_Error to be raised, or an arbitrary one of the case |
| 797 |
-- alternatives will be executed. Wild jumps cannot result even |
| 798 |
-- in this mode, since we always do a range check |
| 799 |
|
| 800 |
-- For subscripted array assignments, wild stores can result in |
| 801 |
-- overwriting arbitrary memory locations. |
| 802 |
|
| 803 |
-- Default (default standard RM-compatible validity checking) |
| 804 |
|
| 805 |
-- In this mode, which is the default, minimal validity checking is |
| 806 |
-- performed to ensure no erroneous behavior as follows: |
| 807 |
|
| 808 |
-- For case statements, an out of range invalid value will cause |
| 809 |
-- Constraint_Error to be raised. |
| 810 |
|
| 811 |
-- For subscripted array assignments, invalid out of range |
| 812 |
-- subscript values will cause Constraint_Error to be raised. |
| 813 |
|
| 814 |
-- Full (Full validity checking) |
| 815 |
|
| 816 |
-- In this mode, the protections guaranteed by the standard mode are |
| 817 |
-- in place, and the following additional checks are made: |
| 818 |
|
| 819 |
-- For every assignment, the right side is checked for validity |
| 820 |
|
| 821 |
-- For every call, IN and IN OUT parameters are checked for validity |
| 822 |
|
| 823 |
-- For every subscripted array reference, both for stores and loads, |
| 824 |
-- all subscripts are checked for validity. |
| 825 |
|
| 826 |
-- These checks are not required by the RM, but will in practice |
| 827 |
-- improve the detection of uninitialized variables, particularly |
| 828 |
-- if used in conjunction with pragma Normalize_Scalars. |
| 829 |
|
| 830 |
-- In the above description, we talk about performing validity checks, |
| 831 |
-- but we don't actually generate a check in a case where the compiler |
| 832 |
-- can be sure that the value is valid. Note that this assurance must |
| 833 |
-- be achieved without assuming that any uninitialized value lies within |
| 834 |
-- the range of its type. The following are cases in which values are |
| 835 |
-- known to be valid. The flag Is_Known_Valid is used to keep track of |
| 836 |
-- some of these cases. |
| 837 |
|
| 838 |
-- If all possible stored values are valid, then any uninitialized |
| 839 |
-- value must be valid. |
| 840 |
|
| 841 |
-- Literals, including enumeration literals, are clearly always valid |
| 842 |
|
| 843 |
-- Constants are always assumed valid, with a validity check being |
| 844 |
-- performed on the initializing value where necessary to ensure that |
| 845 |
-- this is the case. |
| 846 |
|
| 847 |
-- For variables, the status is set to known valid if there is an |
| 848 |
-- initializing expression. Again a check is made on the initializing |
| 849 |
-- value if necessary to ensure that this assumption is valid. The |
| 850 |
-- status can change as a result of local assignments to a variable. |
| 851 |
-- If a known valid value is unconditionally assigned, then we mark |
| 852 |
-- the left side as known valid. If a value is assigned that is not |
| 853 |
-- known to be valid, then we mark the left side as invalid. This |
| 854 |
-- kind of processing does NOT apply to non-local variables since we |
| 855 |
-- are not following the flow graph (more properly the flow of actual |
| 856 |
-- processing only corresponds to the flow graph for local assignments). |
| 857 |
-- For non-local variables, we preserve the current setting, i.e. a |
| 858 |
-- validity check is performed when assigning to a known valid global. |
| 859 |
|
| 860 |
-- Note: no validity checking is required if range checks are suppressed |
| 861 |
-- regardless of the setting of the validity checking mode. |
| 862 |
|
| 863 |
-- The following procedures are used in handling validity checking |
| 864 |
|
| 865 |
procedure Apply_Subscript_Validity_Checks |
| 866 |
(Expr : Node_Id; |
| 867 |
No_Check_Needed : Dimension_Set := Empty_Dimension_Set); |
| 868 |
-- Expr is the node for an indexed component. If validity checking and |
| 869 |
-- range checking are enabled, each subscript for this indexed component |
| 870 |
-- whose dimension does not belong to the No_Check_Needed set is checked |
| 871 |
-- for validity. No_Check_Needed.Dimensions must match the number of |
| 872 |
-- dimensions of the array type or be zero. |
| 873 |
|
| 874 |
procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id); |
| 875 |
-- Expr is a lvalue, i.e. an expression representing the target of an |
| 876 |
-- assignment. This procedure checks for this expression involving an |
| 877 |
-- assignment to an array value. We have to be sure that all the subscripts |
| 878 |
-- in such a case are valid, since according to the rules in (RM |
| 879 |
-- 13.9.1(9-11)) such assignments are not permitted to result in erroneous |
| 880 |
-- behavior in the case of invalid subscript values. |
| 881 |
|
| 882 |
procedure Ensure_Valid |
| 883 |
(Expr : Node_Id; |
| 884 |
Holes_OK : Boolean := False; |
| 885 |
Related_Id : Entity_Id := Empty; |
| 886 |
Is_Low_Bound : Boolean := False; |
| 887 |
Is_High_Bound : Boolean := False); |
| 888 |
-- Ensure that Expr represents a valid value of its type. If this type |
| 889 |
-- is not a scalar type, then the call has no effect, since validity |
| 890 |
-- is only an issue for scalar types. The effect of this call is to |
| 891 |
-- check if the value is known valid, if so, nothing needs to be done. |
| 892 |
-- If this is not known, then either Expr is set to be range checked, |
| 893 |
-- or specific checking code is inserted so that an exception is raised |
| 894 |
-- if the value is not valid. |
| 895 |
-- |
| 896 |
-- The optional argument Holes_OK indicates whether it is necessary to |
| 897 |
-- worry about enumeration types with non-standard representations leading |
| 898 |
-- to "holes" in the range of possible representations. If Holes_OK is |
| 899 |
-- True, then such values are assumed valid (this is used when the caller |
| 900 |
-- will make a separate check for this case anyway). If Holes_OK is False, |
| 901 |
-- then this case is checked, and code is inserted to ensure that Expr is |
| 902 |
-- valid, raising Constraint_Error if the value is not valid. |
| 903 |
-- |
| 904 |
-- Related_Id denotes the entity of the context where Expr appears. Flags |
| 905 |
-- Is_Low_Bound and Is_High_Bound specify whether the expression to check |
| 906 |
-- is the low or the high bound of a range. These three optional arguments |
| 907 |
-- signal Remove_Side_Effects to create an external symbol of the form |
| 908 |
-- Chars (Related_Id)_FIRST/_LAST. For suggested use of these parameters |
| 909 |
-- see the warning in the body of Sem_Ch3.Process_Range_Expr_In_Decl. |
| 910 |
|
| 911 |
function Expr_Known_Valid (Expr : Node_Id) return Boolean; |
| 912 |
-- This function tests it the value of Expr is known to be valid in the |
| 913 |
-- sense of RM 13.9.1(9-11). In the case of GNAT, it is only discrete types |
| 914 |
-- which are a concern, since for non-discrete types we simply continue |
| 915 |
-- computation with invalid values, which does not lead to erroneous |
| 916 |
-- behavior. Thus Expr_Known_Valid always returns True if the type of Expr |
| 917 |
-- is non-discrete. For discrete types the value returned is True only if |
| 918 |
-- it can be determined that the value is Valid. Otherwise False is |
| 919 |
-- returned. |
| 920 |
|
| 921 |
procedure Insert_Valid_Check |
| 922 |
(Expr : Node_Id; |
| 923 |
Related_Id : Entity_Id := Empty; |
| 924 |
Is_Low_Bound : Boolean := False; |
| 925 |
Is_High_Bound : Boolean := False); |
| 926 |
-- Inserts code that will check for the value of Expr being valid, in the |
| 927 |
-- sense of the 'Valid attribute returning True. Constraint_Error will be |
| 928 |
-- raised if the value is not valid. |
| 929 |
-- |
| 930 |
-- Related_Id denotes the entity of the context where Expr appears. Flags |
| 931 |
-- Is_Low_Bound and Is_High_Bound specify whether the expression to check |
| 932 |
-- is the low or the high bound of a range. These three optional arguments |
| 933 |
-- signal Remove_Side_Effects to create an external symbol of the form |
| 934 |
-- Chars (Related_Id)_FIRST/_LAST. For suggested use of these parameters |
| 935 |
-- see the warning in the body of Sem_Ch3.Process_Range_Expr_In_Decl. |
| 936 |
|
| 937 |
procedure Null_Exclusion_Static_Checks |
| 938 |
(N : Node_Id; |
| 939 |
Comp : Node_Id := Empty; |
| 940 |
Array_Comp : Boolean := False); |
| 941 |
-- Ada 2005 (AI-231): Test for and warn on null-excluding objects or |
| 942 |
-- components that will raise an exception due to initialization by null. |
| 943 |
-- |
| 944 |
-- When a value for Comp is supplied (as in the case of an uninitialized |
| 945 |
-- null-excluding component within a composite object), a reported warning |
| 946 |
-- will indicate the offending component instead of the object itself. |
| 947 |
-- Array_Comp being True indicates an array object with null-excluding |
| 948 |
-- components, and any reported warning will indicate that. |
| 949 |
|
| 950 |
procedure Remove_Checks (Expr : Node_Id); |
| 951 |
-- Remove all checks from Expr except those that are only executed |
| 952 |
-- conditionally (on the right side of And Then/Or Else. This call |
| 953 |
-- removes only embedded checks (Do_Range_Check, Do_Overflow_Check). |
| 954 |
|
| 955 |
procedure Validity_Check_Range |
| 956 |
(N : Node_Id; |
| 957 |
Related_Id : Entity_Id := Empty); |
| 958 |
-- If N is an N_Range node, then Ensure_Valid is called on its bounds, if |
| 959 |
-- validity checking of operands is enabled. Related_Id denotes the entity |
| 960 |
-- of the context where N appears. |
| 961 |
|
| 962 |
----------------------------- |
| 963 |
-- Handling of Check Names -- |
| 964 |
----------------------------- |
| 965 |
|
| 966 |
-- The following table contains Name_Id's for recognized checks. The first |
| 967 |
-- entries (corresponding to the values of the subtype Predefined_Check_Id) |
| 968 |
-- contain the Name_Id values for the checks that are predefined, including |
| 969 |
-- All_Checks (see Types). Remaining entries are those that are introduced |
| 970 |
-- by pragma Check_Names. |
| 971 |
|
| 972 |
package Check_Names is new Table.Table ( |
| 973 |
Table_Component_Type => Name_Id, |
| 974 |
Table_Index_Type => Check_Id, |
| 975 |
Table_Low_Bound => 1, |
| 976 |
Table_Initial => 30, |
| 977 |
Table_Increment => 200, |
| 978 |
Table_Name => "Name_Check_Names"); |
| 979 |
|
| 980 |
function Get_Check_Id (N : Name_Id) return Check_Id; |
| 981 |
-- Function to search above table for matching name. If found returns the |
| 982 |
-- corresponding Check_Id value in the range 1 .. Check_Name.Last. If not |
| 983 |
-- found returns No_Check_Id. |
| 984 |
|
| 985 |
private |
| 986 |
|
| 987 |
type Check_Result is array (Positive range 1 .. 2) of Node_Id; |
| 988 |
-- There are two cases for the result returned by Get_Range_Checks: |
| 989 |
-- |
| 990 |
-- For the static case the result is one or two nodes that should cause |
| 991 |
-- a Constraint_Error. Typically these will include Expr itself or the |
| 992 |
-- direct descendants of Expr, such as Low/High_Bound (Expr)). It is the |
| 993 |
-- responsibility of the caller to rewrite and substitute the nodes with |
| 994 |
-- N_Raise_Constraint_Error nodes. |
| 995 |
-- |
| 996 |
-- For the non-static case a single N_Raise_Constraint_Error node with a |
| 997 |
-- non-empty Condition field is returned. |
| 998 |
-- |
| 999 |
-- Unused entries in Check_Result, if any, are simply set to Empty For |
| 1000 |
-- external clients, the required processing on this result is achieved |
| 1001 |
-- using the Insert_Range_Checks routine. |
| 1002 |
|
| 1003 |
pragma Inline (Apply_Length_Check); |
| 1004 |
pragma Inline (Apply_Range_Check); |
| 1005 |
pragma Inline (Apply_Static_Length_Check); |
| 1006 |
end Checks; |