| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT COMPILER COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S E M _ E V A L -- |
| 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 |
-- This package contains various subprograms involved in compile time |
| 27 |
-- evaluation of expressions and checks for staticness of expressions and |
| 28 |
-- types. It also contains the circuitry for checking for violations of pure |
| 29 |
-- and preelaborated conditions (this naturally goes here, since these rules |
| 30 |
-- involve consideration of staticness). |
| 31 |
|
| 32 |
-- Note: the static evaluation for attributes is found in Sem_Attr even though |
| 33 |
-- logically it belongs here. We have done this so that it is easier to add |
| 34 |
-- new attributes to GNAT. |
| 35 |
|
| 36 |
with Types; use Types; |
| 37 |
with Uintp; use Uintp; |
| 38 |
with Urealp; use Urealp; |
| 39 |
|
| 40 |
package Sem_Eval is |
| 41 |
|
| 42 |
------------------------------------ |
| 43 |
-- Handling of Static Expressions -- |
| 44 |
------------------------------------ |
| 45 |
|
| 46 |
-- This package contains a set of routines that process individual |
| 47 |
-- subexpression nodes with the objective of folding (precomputing) the |
| 48 |
-- value of static expressions that are known at compile time and properly |
| 49 |
-- computing the setting of two flags that appear in every subexpression |
| 50 |
-- node: |
| 51 |
|
| 52 |
-- Is_Static_Expression |
| 53 |
|
| 54 |
-- True for static expressions, as defined in RM-4.9. |
| 55 |
|
| 56 |
-- Raises_Constraint_Error |
| 57 |
|
| 58 |
-- This flag indicates that it is known at compile time that the |
| 59 |
-- evaluation of an expression raises constraint error. If the |
| 60 |
-- expression is static, and this flag is off, then it is also known at |
| 61 |
-- compile time that the expression does not raise constraint error |
| 62 |
-- (i.e. the flag is accurate for static expressions, and conservative |
| 63 |
-- for non-static expressions. |
| 64 |
|
| 65 |
-- See also Is_OK_Static_Expression, which is True for static |
| 66 |
-- expressions that do not raise Constraint_Error. This is used in most |
| 67 |
-- legality checks, because static expressions that raise Constraint_Error |
| 68 |
-- are usually illegal. |
| 69 |
|
| 70 |
-- See also Compile_Time_Known_Value, which is True for an expression whose |
| 71 |
-- value is known at compile time. In this case, the expression is folded |
| 72 |
-- to a literal or to a constant that is itself (recursively) either a |
| 73 |
-- literal or a constant |
| 74 |
|
| 75 |
-- Is_[OK_]Static_Expression are used for legality checks, whereas |
| 76 |
-- Compile_Time_Known_Value is used for optimization purposes. |
| 77 |
|
| 78 |
-- When we are analyzing and evaluating static expressions, we propagate |
| 79 |
-- both flags. Usually if a subexpression raises a Constraint_Error, then |
| 80 |
-- so will its parent expression, and Raise_Constraint_Error will be |
| 81 |
-- propagated to this parent. The exception is conditional cases like |
| 82 |
-- (True or else 1/0 = 0), which results in an expression that has the |
| 83 |
-- Is_Static_Expression flag True, and Raises_Constraint_Error False. Even |
| 84 |
-- though 1/0 would raise an exception, the right operand is never actually |
| 85 |
-- executed, so the expression as a whole does not raise CE. |
| 86 |
|
| 87 |
-- Finally, the case of static predicates. These are applied only to entire |
| 88 |
-- expressions, not to subexpressions, so we do not have the case of having |
| 89 |
-- to propagate this information. We handle this case simply by resetting |
| 90 |
-- the Is_Static_Expression flag if a static predicate fails. Note that we |
| 91 |
-- can't use this simpler approach for the constraint error case because of |
| 92 |
-- the (True or else 1/0 = 0) example discussed above. |
| 93 |
|
| 94 |
------------------------------- |
| 95 |
-- Compile-Time Known Values -- |
| 96 |
------------------------------- |
| 97 |
|
| 98 |
-- For most legality checking purposes the flag Is_Static_Expression |
| 99 |
-- defined in Sinfo should be used. This package also provides a routine |
| 100 |
-- called Is_OK_Static_Expression which in addition of checking that an |
| 101 |
-- expression is static in the RM 4.9 sense, it checks that the expression |
| 102 |
-- does not raise constraint error. In fact for certain legality checks not |
| 103 |
-- only do we need to ascertain that the expression is static, but we must |
| 104 |
-- also ensure that it does not raise constraint error. |
| 105 |
|
| 106 |
-- Neither of Is_Static_Expression and Is_OK_Static_Expression should be |
| 107 |
-- used for compile time evaluation purposes. In fact certain expression |
| 108 |
-- whose value may be known at compile time are not static in the RM 4.9 |
| 109 |
-- sense. A typical example is: |
| 110 |
|
| 111 |
-- C : constant Integer := Record_Type'Size; |
| 112 |
|
| 113 |
-- The expression 'C' is not static in the technical RM sense, but for many |
| 114 |
-- simple record types, the size is in fact known at compile time. When we |
| 115 |
-- are trying to perform compile time constant folding (for instance for |
| 116 |
-- expressions like C + 1), Is_Static_Expression or Is_OK_Static_Expression |
| 117 |
-- are not the right functions to test if folding is possible. Instead, we |
| 118 |
-- use Compile_Time_Known_Value. All static expressions that do not raise |
| 119 |
-- constraint error (i.e. those for which Is_OK_Static_Expression is true) |
| 120 |
-- are known at compile time, but as shown by the above example, there may |
| 121 |
-- be cases of non-static expressions which are known at compile time. |
| 122 |
|
| 123 |
----------------- |
| 124 |
-- Subprograms -- |
| 125 |
----------------- |
| 126 |
|
| 127 |
procedure Check_Expression_Against_Static_Predicate |
| 128 |
(Expr : Node_Id; |
| 129 |
Typ : Entity_Id; |
| 130 |
Static_Failure_Is_Error : Boolean := False); |
| 131 |
-- Determine whether an arbitrary expression satisfies the static predicate |
| 132 |
-- of a type. The routine does nothing if Expr is not known at compile time |
| 133 |
-- or Typ lacks a static predicate; otherwise it may emit a warning if the |
| 134 |
-- expression is prohibited by the predicate, or if Static_Failure_Is_Error |
| 135 |
-- is True then an error will be flagged. If the expression is a static |
| 136 |
-- expression, it fails a predicate that was not explicitly stated to be |
| 137 |
-- a dynamic predicate, and Static_Failure_Is_Error is False, then an |
| 138 |
-- additional warning is given, and the flag Is_Static_Expression is reset |
| 139 |
-- on Expr. |
| 140 |
|
| 141 |
procedure Check_Non_Static_Context (N : Node_Id); |
| 142 |
-- Deals with the special check required for a static expression that |
| 143 |
-- appears in a non-static context, i.e. is not part of a larger static |
| 144 |
-- expression (see RM 4.9(35)), i.e. the value of the expression must be |
| 145 |
-- within the base range of the base type of its expected type. A check is |
| 146 |
-- also made for expressions that are inside the base range, but outside |
| 147 |
-- the range of the expected subtype (this is a warning message rather than |
| 148 |
-- an illegality). |
| 149 |
-- |
| 150 |
-- Note: most cases of non-static context checks are handled within |
| 151 |
-- Sem_Eval itself, including all cases of expressions at the outer level |
| 152 |
-- (i.e. those that are not a subexpression). The outside customers for |
| 153 |
-- this procedure are Sem_Aggr, Sem_Attr (because Eval_Attribute is there) |
| 154 |
-- and Sem_Res (for a special case arising from ranges, see Resolve_Range). |
| 155 |
-- |
| 156 |
-- Note: this procedure is also called by GNATprove on real literals |
| 157 |
-- that are not sub-expressions of static expressions, to convert them to |
| 158 |
-- machine numbers, as GNATprove cannot perform this conversion contrary |
| 159 |
-- to gigi. |
| 160 |
|
| 161 |
procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id); |
| 162 |
-- N is either a string literal, or a constraint error node. In the latter |
| 163 |
-- case, the situation is already dealt with, and the call has no effect. |
| 164 |
-- In the former case, if the target type, Ttyp is constrained, then a |
| 165 |
-- check is made to see if the string literal is of appropriate length. |
| 166 |
|
| 167 |
function Checking_Potentially_Static_Expression return Boolean; |
| 168 |
-- Returns True if the checking for potentially static expressions is |
| 169 |
-- enabled; otherwise returns False. |
| 170 |
|
| 171 |
procedure Set_Checking_Potentially_Static_Expression (Value : Boolean); |
| 172 |
-- Enables checking for potentially static expressions if Value is True, |
| 173 |
-- and disables such checking if Value is False. |
| 174 |
|
| 175 |
type Compare_Result is (LT, LE, EQ, GT, GE, NE, Unknown); |
| 176 |
subtype Compare_GE is Compare_Result range EQ .. GE; |
| 177 |
subtype Compare_LE is Compare_Result range LT .. EQ; |
| 178 |
-- Result subtypes for Compile_Time_Compare subprograms |
| 179 |
|
| 180 |
function Compile_Time_Compare |
| 181 |
(L, R : Node_Id; |
| 182 |
Assume_Valid : Boolean) return Compare_Result; |
| 183 |
pragma Inline (Compile_Time_Compare); |
| 184 |
-- Given two expression nodes, finds out whether it can be determined at |
| 185 |
-- compile time how the runtime values will compare. An Unknown result |
| 186 |
-- means that the result of a comparison cannot be determined at compile |
| 187 |
-- time, otherwise the returned result indicates the known result of the |
| 188 |
-- comparison, given as tightly as possible (i.e. EQ or LT is preferred |
| 189 |
-- returned value to LE). If Assume_Valid is true, the result reflects |
| 190 |
-- the result of assuming that entities involved in the comparison have |
| 191 |
-- valid representations. If Assume_Valid is false, then the base type of |
| 192 |
-- any involved entity is used so that no assumption of validity is made. |
| 193 |
|
| 194 |
function Compile_Time_Compare |
| 195 |
(L, R : Node_Id; |
| 196 |
Diff : access Uint; |
| 197 |
Assume_Valid : Boolean; |
| 198 |
Rec : Boolean := False) return Compare_Result; |
| 199 |
-- This version of Compile_Time_Compare returns extra information if the |
| 200 |
-- result is GT or LT. In these cases, if the magnitude of the difference |
| 201 |
-- can be determined at compile time, this (positive) magnitude is returned |
| 202 |
-- in Diff.all. If the magnitude of the difference cannot be determined |
| 203 |
-- then Diff.all contains No_Uint on return. Rec is a parameter that is set |
| 204 |
-- True for a recursive call from within Compile_Time_Compare to avoid some |
| 205 |
-- infinite recursion cases. It should never be set by a client. |
| 206 |
|
| 207 |
function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean; |
| 208 |
-- If T is an array whose index bounds are all known at compile time, then |
| 209 |
-- True is returned. If T is not an array type, or one or more of its index |
| 210 |
-- bounds is not known at compile time, then False is returned. |
| 211 |
|
| 212 |
function Compile_Time_Known_Value (Op : Node_Id) return Boolean; |
| 213 |
-- Returns true if Op is an expression not raising Constraint_Error whose |
| 214 |
-- value is known at compile time and for which a call to Expr_Value can |
| 215 |
-- be used to determine this value. This is always true if Op is a static |
| 216 |
-- expression, but can also be true for expressions which are technically |
| 217 |
-- non-static but which are in fact known at compile time. Some examples of |
| 218 |
-- such expressions are the static lower bound of a non-static range or the |
| 219 |
-- value of a constant object whose initial value is itself compile time |
| 220 |
-- known in the sense of this routine. Note that this routine is defended |
| 221 |
-- against unanalyzed expressions. Such expressions will not cause a |
| 222 |
-- blowup, they may cause pessimistic (i.e. False) results to be returned. |
| 223 |
-- In general we take a pessimistic view. False does not mean the value |
| 224 |
-- could not be known at compile time, but True means that absolutely |
| 225 |
-- definition it is known at compile time and it is safe to call |
| 226 |
-- Expr_Value[_XX] on the expression Op. |
| 227 |
-- |
| 228 |
-- Note that we don't define precisely the set of expressions that return |
| 229 |
-- True. Callers should not make any assumptions regarding the value that |
| 230 |
-- is returned for non-static expressions. Functional behavior should never |
| 231 |
-- be affected by whether a given non-static expression returns True or |
| 232 |
-- False when this function is called. In other words this is purely for |
| 233 |
-- efficiency optimization purposes. The code generated can often be more |
| 234 |
-- efficient with compile time known values, e.g. range analysis for the |
| 235 |
-- purpose of removing checks is more effective if we know precise bounds. |
| 236 |
|
| 237 |
-- WARNING: There is a matching C declaration of this subprogram in fe.h |
| 238 |
|
| 239 |
function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean; |
| 240 |
-- In the case of configurable run-times, there may be an issue calling |
| 241 |
-- Compile_Time_Known_Value with non-static expressions where the legality |
| 242 |
-- of the program is not well-defined. Consider this example: |
| 243 |
-- |
| 244 |
-- X := B ** C; |
| 245 |
-- |
| 246 |
-- Now if C is compile time known, and has the value 4, then inline code |
| 247 |
-- can be generated at compile time, instead of calling a run-time routine. |
| 248 |
-- That's fine in the normal case, but when we have a configurable run-time |
| 249 |
-- the run-time routine may not be available. This means that the program |
| 250 |
-- will be rejected if C is not known at compile time. We don't want the |
| 251 |
-- legality of a program to depend on how clever the implementation of this |
| 252 |
-- function is. If the run-time in use lacks the exponentiation routine, |
| 253 |
-- then what we say is that exponentiation is permitted if the exponent is |
| 254 |
-- officially static and has a value in the range 0 .. 4. |
| 255 |
-- |
| 256 |
-- In a case like this, we use CRT_Safe_Compile_Time_Known_Value to avoid |
| 257 |
-- this effect. This routine will return False for a non-static expression |
| 258 |
-- if we are in configurable run-time mode, even if the expression would |
| 259 |
-- normally be considered compile-time known. |
| 260 |
|
| 261 |
function Expr_Rep_Value (N : Node_Id) return Uint; |
| 262 |
-- This is identical to Expr_Value, except in the case of enumeration |
| 263 |
-- literals of types for which an enumeration representation clause has |
| 264 |
-- been given, in which case it returns the representation value rather |
| 265 |
-- than the pos value. This is the value that is needed for generating code |
| 266 |
-- sequences, while the Expr_Value value is appropriate for compile time |
| 267 |
-- constraint errors or getting the logical value. Note that this function |
| 268 |
-- does NOT concern itself with biased values, if the caller needs a |
| 269 |
-- properly biased value, the subtraction of the bias must be handled |
| 270 |
-- explicitly. |
| 271 |
|
| 272 |
function Expr_Value (N : Node_Id) return Uint; |
| 273 |
-- Returns the folded value of the expression N. This function is called in |
| 274 |
-- instances where it has already been determined that the expression is |
| 275 |
-- static or its value is compile time known (Compile_Time_Known_Value (N) |
| 276 |
-- returns True). This version is used for integer values, and enumeration |
| 277 |
-- or character literals. In the latter two cases, the value returned is |
| 278 |
-- the Pos value in the relevant enumeration type. It can also be used for |
| 279 |
-- fixed-point values, in which case it returns the corresponding integer |
| 280 |
-- value, but it cannot be used for floating-point values. Finally, it can |
| 281 |
-- also be used for the Null access value, as well as for the result of an |
| 282 |
-- unchecked conversion of the aforementioned handled values. |
| 283 |
|
| 284 |
function Expr_Value_E (N : Node_Id) return Entity_Id; |
| 285 |
-- Returns the folded value of the expression. This function is called in |
| 286 |
-- instances where it has already been determined that the expression is |
| 287 |
-- static or its value known at compile time. This version is used for |
| 288 |
-- enumeration types and returns the corresponding enumeration literal. |
| 289 |
|
| 290 |
function Expr_Value_R (N : Node_Id) return Ureal; |
| 291 |
-- Returns the folded value of the expression. This function is called in |
| 292 |
-- instances where it has already been determined that the expression is |
| 293 |
-- static or its value known at compile time. This version is used for real |
| 294 |
-- values (including both the floating-point and fixed-point cases). In the |
| 295 |
-- case of a fixed-point type, the real value is returned (cf above version |
| 296 |
-- returning Uint). |
| 297 |
|
| 298 |
function Expr_Value_S (N : Node_Id) return Node_Id; |
| 299 |
-- Returns the folded value of the expression. This function is called |
| 300 |
-- in instances where it has already been determined that the expression |
| 301 |
-- is static or its value is known at compile time. This version is used |
| 302 |
-- for string types and returns the corresponding N_String_Literal node. |
| 303 |
|
| 304 |
procedure Eval_Actual (N : Node_Id); |
| 305 |
procedure Eval_Allocator (N : Node_Id); |
| 306 |
procedure Eval_Arithmetic_Op (N : Node_Id); |
| 307 |
procedure Eval_Call (N : Node_Id); |
| 308 |
procedure Eval_Case_Expression (N : Node_Id); |
| 309 |
procedure Eval_Character_Literal (N : Node_Id); |
| 310 |
procedure Eval_Concatenation (N : Node_Id); |
| 311 |
procedure Eval_Entity_Name (N : Node_Id); |
| 312 |
procedure Eval_If_Expression (N : Node_Id); |
| 313 |
procedure Eval_Indexed_Component (N : Node_Id); |
| 314 |
procedure Eval_Integer_Literal (N : Node_Id); |
| 315 |
procedure Eval_Logical_Op (N : Node_Id); |
| 316 |
procedure Eval_Membership_Op (N : Node_Id); |
| 317 |
procedure Eval_Named_Integer (N : Node_Id); |
| 318 |
procedure Eval_Named_Real (N : Node_Id); |
| 319 |
procedure Eval_Op_Expon (N : Node_Id); |
| 320 |
procedure Eval_Op_Not (N : Node_Id); |
| 321 |
procedure Eval_Real_Literal (N : Node_Id); |
| 322 |
procedure Eval_Relational_Op (N : Node_Id); |
| 323 |
procedure Eval_Selected_Component (N : Node_Id); |
| 324 |
procedure Eval_Shift (N : Node_Id); |
| 325 |
procedure Eval_Short_Circuit (N : Node_Id); |
| 326 |
procedure Eval_Slice (N : Node_Id); |
| 327 |
procedure Eval_String_Literal (N : Node_Id); |
| 328 |
procedure Eval_Qualified_Expression (N : Node_Id); |
| 329 |
procedure Eval_Type_Conversion (N : Node_Id); |
| 330 |
procedure Eval_Unary_Op (N : Node_Id); |
| 331 |
procedure Eval_Unchecked_Conversion (N : Node_Id); |
| 332 |
|
| 333 |
procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id); |
| 334 |
-- This procedure is called after it has been determined that Expr is not |
| 335 |
-- static when it is required to be. Msg is the text of a message that |
| 336 |
-- explains the error. This procedure checks if an error is already posted |
| 337 |
-- on Expr, if so, it does nothing unless All_Errors_Mode is set in which |
| 338 |
-- case this flag is ignored. Otherwise the given message is posted using |
| 339 |
-- Error_Msg_F, and then Why_Not_Static is called on Expr to generate |
| 340 |
-- additional messages. The string given as Msg should end with ! to make |
| 341 |
-- it an unconditional message, to ensure that if it is posted, the entire |
| 342 |
-- set of messages is all posted. |
| 343 |
|
| 344 |
procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean); |
| 345 |
-- Rewrite N with a new N_String_Literal node as the result of the compile |
| 346 |
-- time evaluation of the node N. Val is the resulting string value from |
| 347 |
-- the folding operation. The Is_Static_Expression flag is set in the |
| 348 |
-- result node. The result is fully analyzed and resolved. Static indicates |
| 349 |
-- whether the result should be considered static or not (True = consider |
| 350 |
-- static). The point here is that normally all string literals are static, |
| 351 |
-- but if this was the result of some sequence of evaluation where values |
| 352 |
-- were known at compile time but not static, then the result is not |
| 353 |
-- static. The call has no effect if Raises_Constraint_Error (N) is True, |
| 354 |
-- since there is no point in folding if we have an error. |
| 355 |
|
| 356 |
procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean); |
| 357 |
-- Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal) |
| 358 |
-- node as the result of the compile time evaluation of the node N. Val is |
| 359 |
-- the result in the integer case and is the position of the literal in the |
| 360 |
-- literals list for the enumeration case. Is_Static_Expression is set True |
| 361 |
-- in the result node. The result is fully analyzed/resolved. Static |
| 362 |
-- indicates whether the result should be considered static or not (True = |
| 363 |
-- consider static). The point here is that normally all integer literals |
| 364 |
-- are static, but if this was the result of some sequence of evaluation |
| 365 |
-- where values were known at compile time but not static, then the result |
| 366 |
-- is not static. The call has no effect if Raises_Constraint_Error (N) is |
| 367 |
-- True, since there is no point in folding if we have an error. |
| 368 |
|
| 369 |
procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean); |
| 370 |
-- Rewrite N with a new N_Real_Literal node as the result of the compile |
| 371 |
-- time evaluation of the node N. Val is the resulting real value from the |
| 372 |
-- folding operation. The Is_Static_Expression flag is set in the result |
| 373 |
-- node. The result is fully analyzed and result. Static indicates whether |
| 374 |
-- the result should be considered static or not (True = consider static). |
| 375 |
-- The point here is that normally all string literals are static, but if |
| 376 |
-- this was the result of some sequence of evaluation where values were |
| 377 |
-- known at compile time but not static, then the result is not static. |
| 378 |
-- The call has no effect if Raises_Constraint_Error (N) is True, since |
| 379 |
-- there is no point in folding if we have an error. |
| 380 |
|
| 381 |
procedure Fold (N : Node_Id); |
| 382 |
-- Rewrite N with the relevant value if Compile_Time_Known_Value (N) is |
| 383 |
-- True, otherwise a no-op. |
| 384 |
|
| 385 |
function Is_In_Range |
| 386 |
(N : Node_Id; |
| 387 |
Typ : Entity_Id; |
| 388 |
Assume_Valid : Boolean := False; |
| 389 |
Fixed_Int : Boolean := False; |
| 390 |
Int_Real : Boolean := False) return Boolean; |
| 391 |
-- Returns True if it can be guaranteed at compile time that expression |
| 392 |
-- N is known to be in range of the subtype Typ. A result of False does |
| 393 |
-- not mean that the expression is out of range, merely that it cannot be |
| 394 |
-- determined at compile time that it is in range. If Typ is a floating |
| 395 |
-- point type or Int_Real is set, any integer value is treated as though it |
| 396 |
-- was a real value (i.e. the underlying real value is used). In this case |
| 397 |
-- we use the corresponding real value, both for the bounds of Typ, and for |
| 398 |
-- the value of the expression N. If Typ is a fixed type or a discrete type |
| 399 |
-- and Int_Real is False but flag Fixed_Int is True then any fixed-point |
| 400 |
-- value is treated as though it was discrete value (i.e. the underlying |
| 401 |
-- integer value is used). In this case we use the corresponding integer |
| 402 |
-- value, both for the bounds of Typ, and for the value of the expression |
| 403 |
-- N. If Typ is a discrete type and Fixed_Int as well as Int_Real are |
| 404 |
-- false, integer values are used throughout. |
| 405 |
-- |
| 406 |
-- If Assume_Valid is set True, then N is always assumed to contain a valid |
| 407 |
-- value. If Assume_Valid is set False, then N may be invalid (unless there |
| 408 |
-- is some independent way of knowing that it is valid, i.e. either it is |
| 409 |
-- an entity with Is_Known_Valid set, or Assume_No_Invalid_Values is True. |
| 410 |
|
| 411 |
function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean; |
| 412 |
-- Returns True if it can guarantee that Lo .. Hi is a null range |
| 413 |
|
| 414 |
-- WARNING: There is a matching C declaration of this subprogram in fe.h |
| 415 |
|
| 416 |
function Is_OK_Static_Expression (N : Node_Id) return Boolean; |
| 417 |
-- An OK static expression is one that is static in the RM definition sense |
| 418 |
-- and which does not raise constraint error. For most legality checking |
| 419 |
-- purposes you should use Is_Static_Expression. For those legality checks |
| 420 |
-- where the expression N should not raise constraint error use this |
| 421 |
-- routine. This routine is *not* to be used in contexts where the test is |
| 422 |
-- for compile time evaluation purposes. Use Compile_Time_Known_Value |
| 423 |
-- instead (see section on "Compile-Time Known Values" above). |
| 424 |
|
| 425 |
type Staticity is (Static, Not_Static, Invalid); |
| 426 |
|
| 427 |
function Is_OK_Static_Expression_Of_Type |
| 428 |
(Expr : Node_Id; Typ : Entity_Id := Empty) return Staticity; |
| 429 |
-- Return whether Expr is a static expression of the given type (i.e. it |
| 430 |
-- will be analyzed and resolved using this type, which can be any valid |
| 431 |
-- argument to Resolve, e.g. Any_Integer is OK). Includes checking that the |
| 432 |
-- expression does not raise Constraint_Error. |
| 433 |
|
| 434 |
function Is_OK_Static_Range (N : Node_Id) return Boolean; |
| 435 |
-- Determines if range is static, as defined in RM 4.9(26), and also checks |
| 436 |
-- that neither bound of the range raises constraint error, thus ensuring |
| 437 |
-- that both bounds of the range are compile-time evaluable (i.e. do not |
| 438 |
-- raise constraint error). A result of true means that the bounds are |
| 439 |
-- compile time evaluable. A result of false means they are not (either |
| 440 |
-- because the range is not static, or because one or the other bound |
| 441 |
-- raises CE). |
| 442 |
|
| 443 |
function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean; |
| 444 |
-- Determines whether a subtype fits the definition of an Ada static |
| 445 |
-- subtype as given in (RM 4.9(26)) with the additional check that neither |
| 446 |
-- bound raises constraint error (meaning that Expr_Value[_R|S] can be used |
| 447 |
-- on these bounds). |
| 448 |
-- |
| 449 |
-- This differs from Is_Static_Subtype in that it includes the constraint |
| 450 |
-- error checks, which are missing from Is_Static_Subtype. |
| 451 |
|
| 452 |
function Is_Out_Of_Range |
| 453 |
(N : Node_Id; |
| 454 |
Typ : Entity_Id; |
| 455 |
Assume_Valid : Boolean := False; |
| 456 |
Fixed_Int : Boolean := False; |
| 457 |
Int_Real : Boolean := False) return Boolean; |
| 458 |
-- Returns True if it can be guaranteed at compile time that expression is |
| 459 |
-- known to be out of range of the subtype Typ. True is returned if Typ is |
| 460 |
-- a scalar type, and the value of N can be determined to be outside the |
| 461 |
-- range of Typ. A result of False does not mean that the expression is in |
| 462 |
-- range, but rather merely that it cannot be determined at compile time |
| 463 |
-- that it is out of range. The parameters Assume_Valid, Fixed_Int, and |
| 464 |
-- Int_Real are as described for Is_In_Range above. |
| 465 |
|
| 466 |
function Is_Static_Subtype (Typ : Entity_Id) return Boolean; |
| 467 |
-- Determines whether a subtype fits the definition of an Ada static |
| 468 |
-- subtype as given in (RM 4.9(26)). |
| 469 |
-- |
| 470 |
-- This differs from Is_OK_Static_Subtype (which is what must be used by |
| 471 |
-- clients) in that it does not care whether the bounds raise a constraint |
| 472 |
-- error exception or not. Used for checking whether expressions are static |
| 473 |
-- in the 4.9 sense (without worrying about exceptions). |
| 474 |
|
| 475 |
function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean; |
| 476 |
-- This function returns True if the given expression Expr is statically |
| 477 |
-- unevaluated, as defined in (RM 4.9 (32.1-32.6)). |
| 478 |
|
| 479 |
function In_Subrange_Of |
| 480 |
(T1 : Entity_Id; |
| 481 |
T2 : Entity_Id; |
| 482 |
Fixed_Int : Boolean := False) return Boolean; |
| 483 |
-- Returns True if it can be guaranteed at compile time that the range of |
| 484 |
-- values for scalar type T1 are always in the range of scalar type T2. A |
| 485 |
-- result of False does not mean that T1 is not in T2's subrange, only that |
| 486 |
-- it cannot be determined at compile time. Flag Fixed_Int is used as in |
| 487 |
-- routine Is_In_Range above. |
| 488 |
|
| 489 |
function Machine_Number |
| 490 |
(Typ : Entity_Id; |
| 491 |
Val : Ureal; |
| 492 |
N : Node_Id) return Ureal; |
| 493 |
-- Return the machine number of Typ corresponding to the specified Val as |
| 494 |
-- per RM 4.9(38/2). N is a node only used to post warnings. |
| 495 |
|
| 496 |
function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean; |
| 497 |
-- Returns True if it can guarantee that Lo .. Hi is not a null range |
| 498 |
|
| 499 |
function Predicates_Compatible (T1, T2 : Entity_Id) return Boolean; |
| 500 |
-- In Ada 2012, subtypes are statically compatible if the predicates are |
| 501 |
-- compatible as well. This function performs the required check that |
| 502 |
-- predicates are compatible. Split from Subtypes_Statically_Compatible |
| 503 |
-- so that it can be used in specializing error messages. |
| 504 |
|
| 505 |
function Predicates_Match (T1, T2 : Entity_Id) return Boolean; |
| 506 |
-- In Ada 2012, subtypes statically match if their predicates match as |
| 507 |
-- as well. This function performs the required check that predicates |
| 508 |
-- match. Separated out from Subtypes_Statically_Match so that it can |
| 509 |
-- be used in specializing error messages. |
| 510 |
|
| 511 |
function Subtypes_Statically_Compatible |
| 512 |
(T1 : Entity_Id; |
| 513 |
T2 : Entity_Id; |
| 514 |
Formal_Derived_Matching : Boolean := False) return Boolean; |
| 515 |
-- Returns true if the subtypes are unconstrained or the constraint on |
| 516 |
-- on T1 is statically compatible with T2 (as defined by 4.9.1(4)). |
| 517 |
-- Otherwise returns false. Formal_Derived_Matching indicates whether |
| 518 |
-- the type T1 is a generic actual being checked against ancestor T2 |
| 519 |
-- in a formal derived type association. |
| 520 |
|
| 521 |
function Subtypes_Statically_Match |
| 522 |
(T1 : Entity_Id; |
| 523 |
T2 : Entity_Id; |
| 524 |
Formal_Derived_Matching : Boolean := False) return Boolean; |
| 525 |
-- Determine whether two types T1, T2, which have the same base type, |
| 526 |
-- are statically matching subtypes (RM 4.9.1(1-2)). Also includes the |
| 527 |
-- extra GNAT rule that object sizes must match (this can be false for |
| 528 |
-- types that match in the RM sense because of use of 'Object_Size), |
| 529 |
-- except when testing a generic actual T1 against an ancestor T2 in a |
| 530 |
-- formal derived type association (indicated by Formal_Derived_Matching). |
| 531 |
|
| 532 |
procedure Test_Comparison |
| 533 |
(Op : Node_Id; |
| 534 |
Assume_Valid : Boolean; |
| 535 |
True_Result : out Boolean; |
| 536 |
False_Result : out Boolean); |
| 537 |
-- Determine the outcome of evaluating comparison operator Op using routine |
| 538 |
-- Compile_Time_Compare. Assume_Valid should be set when the operands are |
| 539 |
-- to be assumed valid. Flags True_Result and False_Result are set when the |
| 540 |
-- comparison evaluates to True or False respectively. |
| 541 |
|
| 542 |
procedure Why_Not_Static (Expr : Node_Id); |
| 543 |
-- This procedure may be called after generating an error message that |
| 544 |
-- complains that something is non-static. If it finds good reasons, it |
| 545 |
-- generates one or more error messages pointing the appropriate offending |
| 546 |
-- component of the expression. If no good reasons can be figured out, then |
| 547 |
-- no messages are generated. The expectation here is that the caller has |
| 548 |
-- already issued a message complaining that the expression is non-static. |
| 549 |
-- Note that this message should be placed using Error_Msg_F or |
| 550 |
-- Error_Msg_FE, so that it will sort before any messages placed by this |
| 551 |
-- call. Note that it is fine to call Why_Not_Static with something that |
| 552 |
-- is not an expression, and usually this has no effect, but in some cases |
| 553 |
-- (N_Parameter_Association or N_Range), it makes sense for the internal |
| 554 |
-- recursive calls. |
| 555 |
-- |
| 556 |
-- Note that these messages are not continuation messages, instead they are |
| 557 |
-- separate unconditional messages, marked with '!'. The reason for this is |
| 558 |
-- that they can be posted at a different location from the main message as |
| 559 |
-- documented above ("appropriate offending component"), and continuation |
| 560 |
-- messages must always point to the same location as the parent message. |
| 561 |
|
| 562 |
procedure Initialize; |
| 563 |
-- Initializes the internal data structures |
| 564 |
|
| 565 |
private |
| 566 |
-- The Eval routines are all marked inline, since they are called once |
| 567 |
|
| 568 |
pragma Inline (Eval_Actual); |
| 569 |
pragma Inline (Eval_Allocator); |
| 570 |
pragma Inline (Eval_Character_Literal); |
| 571 |
pragma Inline (Eval_If_Expression); |
| 572 |
pragma Inline (Eval_Indexed_Component); |
| 573 |
pragma Inline (Eval_Named_Integer); |
| 574 |
pragma Inline (Eval_Named_Real); |
| 575 |
pragma Inline (Eval_Real_Literal); |
| 576 |
pragma Inline (Eval_Shift); |
| 577 |
pragma Inline (Eval_Slice); |
| 578 |
pragma Inline (Eval_String_Literal); |
| 579 |
pragma Inline (Eval_Unchecked_Conversion); |
| 580 |
|
| 581 |
pragma Inline (Is_OK_Static_Expression); |
| 582 |
pragma Inline (Machine_Number); |
| 583 |
|
| 584 |
end Sem_Eval; |