Fix typo in t-dimode
[official-gcc.git] / gcc / ada / sem_eval.adb
blob7270172dce96316e0bbbc88c98ea0e361503ebef
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ E V A L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2021, 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 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Einfo.Entities; use Einfo.Entities;
32 with Einfo.Utils; use Einfo.Utils;
33 with Elists; use Elists;
34 with Errout; use Errout;
35 with Eval_Fat; use Eval_Fat;
36 with Exp_Util; use Exp_Util;
37 with Freeze; use Freeze;
38 with Lib; use Lib;
39 with Namet; use Namet;
40 with Nmake; use Nmake;
41 with Nlists; use Nlists;
42 with Opt; use Opt;
43 with Par_SCO; use Par_SCO;
44 with Rtsfind; use Rtsfind;
45 with Sem; use Sem;
46 with Sem_Aux; use Sem_Aux;
47 with Sem_Cat; use Sem_Cat;
48 with Sem_Ch3; use Sem_Ch3;
49 with Sem_Ch6; use Sem_Ch6;
50 with Sem_Ch8; use Sem_Ch8;
51 with Sem_Elab; use Sem_Elab;
52 with Sem_Res; use Sem_Res;
53 with Sem_Util; use Sem_Util;
54 with Sem_Type; use Sem_Type;
55 with Sem_Warn; use Sem_Warn;
56 with Sinfo; use Sinfo;
57 with Sinfo.Nodes; use Sinfo.Nodes;
58 with Sinfo.Utils; use Sinfo.Utils;
59 with Snames; use Snames;
60 with Stand; use Stand;
61 with Stringt; use Stringt;
62 with Tbuild; use Tbuild;
64 package body Sem_Eval is
66 -----------------------------------------
67 -- Handling of Compile Time Evaluation --
68 -----------------------------------------
70 -- The compile time evaluation of expressions is distributed over several
71 -- Eval_xxx procedures. These procedures are called immediately after
72 -- a subexpression is resolved and is therefore accomplished in a bottom
73 -- up fashion. The flags are synthesized using the following approach.
75 -- Is_Static_Expression is determined by following the rules in
76 -- RM-4.9. This involves testing the Is_Static_Expression flag of
77 -- the operands in many cases.
79 -- Raises_Constraint_Error is usually set if any of the operands have
80 -- the flag set or if an attempt to compute the value of the current
81 -- expression results in Constraint_Error.
83 -- The general approach is as follows. First compute Is_Static_Expression.
84 -- If the node is not static, then the flag is left off in the node and
85 -- we are all done. Otherwise for a static node, we test if any of the
86 -- operands will raise Constraint_Error, and if so, propagate the flag
87 -- Raises_Constraint_Error to the result node and we are done (since the
88 -- error was already posted at a lower level).
90 -- For the case of a static node whose operands do not raise constraint
91 -- error, we attempt to evaluate the node. If this evaluation succeeds,
92 -- then the node is replaced by the result of this computation. If the
93 -- evaluation raises Constraint_Error, then we rewrite the node with
94 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
95 -- to post appropriate error messages.
97 ----------------
98 -- Local Data --
99 ----------------
101 type Bits is array (Nat range <>) of Boolean;
102 -- Used to convert unsigned (modular) values for folding logical ops
104 -- The following declarations are used to maintain a cache of nodes that
105 -- have compile-time-known values. The cache is maintained only for
106 -- discrete types (the most common case), and is populated by calls to
107 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
108 -- since it is possible for the status to change (in particular it is
109 -- possible for a node to get replaced by a Constraint_Error node).
111 CV_Bits : constant := 5;
112 -- Number of low order bits of Node_Id value used to reference entries
113 -- in the cache table.
115 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
116 -- Size of cache for compile time values
118 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
120 type CV_Entry is record
121 N : Node_Id'Base;
122 -- We use 'Base here, in case we want to add a predicate to Node_Id
123 V : Uint;
124 end record;
126 type Match_Result is (Match, No_Match, Non_Static);
127 -- Result returned from functions that test for a matching result. If the
128 -- operands are not OK_Static then Non_Static will be returned. Otherwise
129 -- Match/No_Match is returned depending on whether the match succeeds.
131 type CV_Cache_Array is array (CV_Range) of CV_Entry;
133 CV_Cache : CV_Cache_Array;
134 -- This is the actual cache, with entries consisting of node/value pairs,
135 -- and the impossible value Node_High_Bound used for unset entries.
137 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
138 -- Range membership may either be statically known to be in range or out
139 -- of range, or not statically known. Used for Test_In_Range below.
141 Checking_For_Potentially_Static_Expression : Boolean := False;
142 -- Global flag that is set True during Analyze_Static_Expression_Function
143 -- in order to verify that the result expression of a static expression
144 -- function is a potentially static function (see RM2022 6.8(5.3)).
146 -----------------------
147 -- Local Subprograms --
148 -----------------------
150 procedure Check_Non_Static_Context_For_Overflow
151 (N : Node_Id;
152 Stat : Boolean;
153 Result : Uint);
154 -- For a signed integer type, check non-static overflow in Result when
155 -- Stat is False. This applies also inside inlined code, where the static
156 -- property may be an effect of the inlining, which should not be allowed
157 -- to remove run-time checks (whether during compilation, or even more
158 -- crucially in the special inlining-for-proof in GNATprove mode).
160 function Choice_Matches
161 (Expr : Node_Id;
162 Choice : Node_Id) return Match_Result;
163 -- Determines whether given value Expr matches the given Choice. The Expr
164 -- can be of discrete, real, or string type and must be a compile time
165 -- known value (it is an error to make the call if these conditions are
166 -- not met). The choice can be a range, subtype name, subtype indication,
167 -- or expression. The returned result is Non_Static if Choice is not
168 -- OK_Static, otherwise either Match or No_Match is returned depending
169 -- on whether Choice matches Expr. This is used for case expression
170 -- alternatives, and also for membership tests. In each case, more
171 -- possibilities are tested than the syntax allows (e.g. membership allows
172 -- subtype indications and non-discrete types, and case allows an OTHERS
173 -- choice), but it does not matter, since we have already done a full
174 -- semantic and syntax check of the construct, so the extra possibilities
175 -- just will not arise for correct expressions.
177 -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g
178 -- a reference to a type, one of whose bounds raises Constraint_Error, then
179 -- it also sets the Raises_Constraint_Error flag on the Choice itself.
181 function Choices_Match
182 (Expr : Node_Id;
183 Choices : List_Id) return Match_Result;
184 -- This function applies Choice_Matches to each element of Choices. If the
185 -- result is No_Match, then it continues and checks the next element. If
186 -- the result is Match or Non_Static, this result is immediately given
187 -- as the result without checking the rest of the list. Expr can be of
188 -- discrete, real, or string type and must be a compile-time-known value
189 -- (it is an error to make the call if these conditions are not met).
191 procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id);
192 -- Evaluate a call N to an intrinsic subprogram E.
194 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
195 -- Check whether an arithmetic operation with universal operands which is a
196 -- rewritten function call with an explicit scope indication is ambiguous:
197 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
198 -- type declared in P and the context does not impose a type on the result
199 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
200 -- error and return Empty, else return the result type of the operator.
202 procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id);
203 -- Rewrite N as a constant dummy value in the relevant type if possible.
205 procedure Fold_Shift
206 (N : Node_Id;
207 Left : Node_Id;
208 Right : Node_Id;
209 Op : Node_Kind;
210 Static : Boolean := False;
211 Check_Elab : Boolean := False);
212 -- Rewrite N as the result of evaluating Left <shift op> Right if possible.
213 -- Op represents the shift operation.
214 -- Static indicates whether the resulting node should be marked static.
215 -- Check_Elab indicates whether checks for elaboration calls should be
216 -- inserted when relevant.
218 function From_Bits (B : Bits; T : Entity_Id) return Uint;
219 -- Converts a bit string of length B'Length to a Uint value to be used for
220 -- a target of type T, which is a modular type. This procedure includes the
221 -- necessary reduction by the modulus in the case of a nonbinary modulus
222 -- (for a binary modulus, the bit string is the right length any way so all
223 -- is well).
225 function Get_String_Val (N : Node_Id) return Node_Id;
226 -- Given a tree node for a folded string or character value, returns the
227 -- corresponding string literal or character literal (one of the two must
228 -- be available, or the operand would not have been marked as foldable in
229 -- the earlier analysis of the operation).
231 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean;
232 -- Given a choice (from a case expression or membership test), returns
233 -- True if the choice is static and does not raise a Constraint_Error.
235 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean;
236 -- Given a choice list (from a case expression or membership test), return
237 -- True if all choices are static in the sense of Is_OK_Static_Choice.
239 function Is_Static_Choice (Choice : Node_Id) return Boolean;
240 -- Given a choice (from a case expression or membership test), returns
241 -- True if the choice is static. No test is made for raising of constraint
242 -- error, so this function is used only for legality tests.
244 function Is_Static_Choice_List (Choices : List_Id) return Boolean;
245 -- Given a choice list (from a case expression or membership test), return
246 -- True if all choices are static in the sense of Is_Static_Choice.
248 function Is_Static_Range (N : Node_Id) return Boolean;
249 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
250 -- argument is an N_Range node (but note that the semantic analysis of
251 -- equivalent range attribute references already turned them into the
252 -- equivalent range). This differs from Is_OK_Static_Range (which is what
253 -- must be used by clients) in that it does not care whether the bounds
254 -- raise Constraint_Error or not. Used for checking whether expressions are
255 -- static in the 4.9 sense (without worrying about exceptions).
257 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
258 -- Bits represents the number of bits in an integer value to be computed
259 -- (but the value has not been computed yet). If this value in Bits is
260 -- reasonable, a result of True is returned, with the implication that the
261 -- caller should go ahead and complete the calculation. If the value in
262 -- Bits is unreasonably large, then an error is posted on node N, and
263 -- False is returned (and the caller skips the proposed calculation).
265 procedure Out_Of_Range (N : Node_Id);
266 -- This procedure is called if it is determined that node N, which appears
267 -- in a non-static context, is a compile-time-known value which is outside
268 -- its range, i.e. the range of Etype. This is used in contexts where
269 -- this is an illegality if N is static, and should generate a warning
270 -- otherwise.
272 function Real_Or_String_Static_Predicate_Matches
273 (Val : Node_Id;
274 Typ : Entity_Id) return Boolean;
275 -- This is the function used to evaluate real or string static predicates.
276 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
277 -- represents the value to be tested against the predicate. Typ is the
278 -- type with the predicate, from which the predicate expression can be
279 -- extracted. The result returned is True if the given value satisfies
280 -- the predicate.
282 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
283 -- N and Exp are nodes representing an expression, Exp is known to raise
284 -- CE. N is rewritten in term of Exp in the optimal way.
286 function String_Type_Len (Stype : Entity_Id) return Uint;
287 -- Given a string type, determines the length of the index type, or, if
288 -- this index type is non-static, the length of the base type of this index
289 -- type. Note that if the string type is itself static, then the index type
290 -- is static, so the second case applies only if the string type passed is
291 -- non-static.
293 function Test (Cond : Boolean) return Uint;
294 pragma Inline (Test);
295 -- This function simply returns the appropriate Boolean'Pos value
296 -- corresponding to the value of Cond as a universal integer. It is
297 -- used for producing the result of the static evaluation of the
298 -- logical operators
300 procedure Test_Expression_Is_Foldable
301 (N : Node_Id;
302 Op1 : Node_Id;
303 Stat : out Boolean;
304 Fold : out Boolean);
305 -- Tests to see if expression N whose single operand is Op1 is foldable,
306 -- i.e. the operand value is known at compile time. If the operation is
307 -- foldable, then Fold is True on return, and Stat indicates whether the
308 -- result is static (i.e. the operand was static). Note that it is quite
309 -- possible for Fold to be True, and Stat to be False, since there are
310 -- cases in which we know the value of an operand even though it is not
311 -- technically static (e.g. the static lower bound of a range whose upper
312 -- bound is non-static).
314 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes
315 -- a call to Check_Non_Static_Context on the operand. If Fold is False on
316 -- return, then all processing is complete, and the caller should return,
317 -- since there is nothing else to do.
319 -- If Stat is set True on return, then Is_Static_Expression is also set
320 -- true in node N. There are some cases where this is over-enthusiastic,
321 -- e.g. in the two operand case below, for string comparison, the result is
322 -- not static even though the two operands are static. In such cases, the
323 -- caller must reset the Is_Static_Expression flag in N.
325 -- If Fold and Stat are both set to False then this routine performs also
326 -- the following extra actions:
328 -- If either operand is Any_Type then propagate it to result to prevent
329 -- cascaded errors.
331 -- If some operand raises Constraint_Error, then replace the node N
332 -- with the raise Constraint_Error node. This replacement inherits the
333 -- Is_Static_Expression flag from the operands.
335 procedure Test_Expression_Is_Foldable
336 (N : Node_Id;
337 Op1 : Node_Id;
338 Op2 : Node_Id;
339 Stat : out Boolean;
340 Fold : out Boolean;
341 CRT_Safe : Boolean := False);
342 -- Same processing, except applies to an expression N with two operands
343 -- Op1 and Op2. The result is static only if both operands are static. If
344 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
345 -- for the tests that the two operands are known at compile time. See
346 -- spec of this routine for further details.
348 function Test_In_Range
349 (N : Node_Id;
350 Typ : Entity_Id;
351 Assume_Valid : Boolean;
352 Fixed_Int : Boolean;
353 Int_Real : Boolean) return Range_Membership;
354 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
355 -- or Out_Of_Range if it can be guaranteed at compile time that expression
356 -- N is known to be in or out of range of the subtype Typ. If not compile
357 -- time known, Unknown is returned. See documentation of Is_In_Range for
358 -- complete description of parameters.
360 procedure To_Bits (U : Uint; B : out Bits);
361 -- Converts a Uint value to a bit string of length B'Length
363 -----------------------------------------------
364 -- Check_Expression_Against_Static_Predicate --
365 -----------------------------------------------
367 procedure Check_Expression_Against_Static_Predicate
368 (Expr : Node_Id;
369 Typ : Entity_Id;
370 Static_Failure_Is_Error : Boolean := False)
372 begin
373 -- Nothing to do if expression is not known at compile time, or the
374 -- type has no static predicate set (will be the case for all non-scalar
375 -- types, so no need to make a special test for that).
377 if not (Has_Static_Predicate (Typ)
378 and then Compile_Time_Known_Value (Expr))
379 then
380 return;
381 end if;
383 -- Here we have a static predicate (note that it could have arisen from
384 -- an explicitly specified Dynamic_Predicate whose expression met the
385 -- rules for being predicate-static). If the expression is known at
386 -- compile time and obeys the predicate, then it is static and must be
387 -- labeled as such, which matters e.g. for case statements. The original
388 -- expression may be a type conversion of a variable with a known value,
389 -- which might otherwise not be marked static.
391 -- Case of real static predicate
393 if Is_Real_Type (Typ) then
394 if Real_Or_String_Static_Predicate_Matches
395 (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)),
396 Typ => Typ)
397 then
398 Set_Is_Static_Expression (Expr);
399 return;
400 end if;
402 -- Case of string static predicate
404 elsif Is_String_Type (Typ) then
405 if Real_Or_String_Static_Predicate_Matches
406 (Val => Expr_Value_S (Expr), Typ => Typ)
407 then
408 Set_Is_Static_Expression (Expr);
409 return;
410 end if;
412 -- Case of discrete static predicate
414 else
415 pragma Assert (Is_Discrete_Type (Typ));
417 -- If static predicate matches, nothing to do
419 if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then
420 Set_Is_Static_Expression (Expr);
421 return;
422 end if;
423 end if;
425 -- Here we know that the predicate will fail
427 -- Special case of static expression failing a predicate (other than one
428 -- that was explicitly specified with a Dynamic_Predicate aspect). If
429 -- the expression comes from a qualified_expression or type_conversion
430 -- this is an error (Static_Failure_Is_Error); otherwise we only issue
431 -- a warning and the expression is no longer considered static.
433 if Is_Static_Expression (Expr)
434 and then not Has_Dynamic_Predicate_Aspect (Typ)
435 then
436 if Static_Failure_Is_Error then
437 Error_Msg_NE
438 ("static expression fails static predicate check on &",
439 Expr, Typ);
441 else
442 Error_Msg_NE
443 ("??static expression fails static predicate check on &",
444 Expr, Typ);
445 Error_Msg_N
446 ("\??expression is no longer considered static", Expr);
448 Set_Is_Static_Expression (Expr, False);
449 end if;
451 -- In all other cases, this is just a warning that a test will fail.
452 -- It does not matter if the expression is static or not, or if the
453 -- predicate comes from a dynamic predicate aspect or not.
455 else
456 Error_Msg_NE
457 ("??expression fails predicate check on &", Expr, Typ);
459 -- Force a check here, which is potentially a redundant check, but
460 -- this ensures a check will be done in cases where the expression
461 -- is folded, and since this is definitely a failure, extra checks
462 -- are OK.
464 if Predicate_Enabled (Typ) then
465 Insert_Action (Expr,
466 Make_Predicate_Check
467 (Typ, Duplicate_Subexpr (Expr)), Suppress => All_Checks);
468 end if;
469 end if;
470 end Check_Expression_Against_Static_Predicate;
472 ------------------------------
473 -- Check_Non_Static_Context --
474 ------------------------------
476 procedure Check_Non_Static_Context (N : Node_Id) is
477 T : constant Entity_Id := Etype (N);
478 Checks_On : constant Boolean :=
479 not Index_Checks_Suppressed (T)
480 and not Range_Checks_Suppressed (T);
482 begin
483 -- Ignore cases of non-scalar types, error types, or universal real
484 -- types that have no usable bounds.
486 if T = Any_Type
487 or else not Is_Scalar_Type (T)
488 or else T = Universal_Fixed
489 or else T = Universal_Real
490 then
491 return;
492 end if;
494 -- At this stage we have a scalar type. If we have an expression that
495 -- raises CE, then we already issued a warning or error msg so there is
496 -- nothing more to be done in this routine.
498 if Raises_Constraint_Error (N) then
499 return;
500 end if;
502 -- Now we have a scalar type which is not marked as raising a constraint
503 -- error exception. The main purpose of this routine is to deal with
504 -- static expressions appearing in a non-static context. That means
505 -- that if we do not have a static expression then there is not much
506 -- to do. The one case that we deal with here is that if we have a
507 -- floating-point value that is out of range, then we post a warning
508 -- that an infinity will result.
510 if not Is_Static_Expression (N) then
511 if Is_Floating_Point_Type (T) then
512 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
513 Error_Msg_N
514 ("??float value out of range, infinity will be generated", N);
516 -- The literal may be the result of constant-folding of a non-
517 -- static subexpression of a larger expression (e.g. a conversion
518 -- of a non-static variable whose value happens to be known). At
519 -- this point we must reduce the value of the subexpression to a
520 -- machine number (RM 4.9 (38/2)).
522 elsif Nkind (N) = N_Real_Literal
523 and then Nkind (Parent (N)) in N_Subexpr
524 then
525 Rewrite (N, New_Copy (N));
526 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
527 Set_Is_Machine_Number (N);
528 end if;
529 end if;
531 return;
532 end if;
534 -- Here we have the case of outer level static expression of scalar
535 -- type, where the processing of this procedure is needed.
537 -- For real types, this is where we convert the value to a machine
538 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
539 -- need to do this if the parent is a constant declaration, since in
540 -- other cases, gigi should do the necessary conversion correctly, but
541 -- experimentation shows that this is not the case on all machines, in
542 -- particular if we do not convert all literals to machine values in
543 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
544 -- and SGI/Irix.
546 -- This conversion is always done by GNATprove on real literals in
547 -- non-static expressions, by calling Check_Non_Static_Context from
548 -- gnat2why, as GNATprove cannot do the conversion later contrary
549 -- to gigi. The frontend computes the information about which
550 -- expressions are static, which is used by gnat2why to call
551 -- Check_Non_Static_Context on exactly those real literals that are
552 -- not subexpressions of static expressions.
554 if Nkind (N) = N_Real_Literal
555 and then not Is_Machine_Number (N)
556 and then not Is_Generic_Type (Etype (N))
557 and then Etype (N) /= Universal_Real
558 then
559 -- Check that value is in bounds before converting to machine
560 -- number, so as not to lose case where value overflows in the
561 -- least significant bit or less. See B490001.
563 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
564 Out_Of_Range (N);
565 return;
566 end if;
568 -- Note: we have to copy the node, to avoid problems with conformance
569 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
571 Rewrite (N, New_Copy (N));
573 if not Is_Floating_Point_Type (T) then
574 Set_Realval
575 (N, Corresponding_Integer_Value (N) * Small_Value (T));
577 elsif not UR_Is_Zero (Realval (N)) then
578 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
579 Set_Is_Machine_Number (N);
580 end if;
582 end if;
584 -- Check for out of range universal integer. This is a non-static
585 -- context, so the integer value must be in range of the runtime
586 -- representation of universal integers.
588 -- We do this only within an expression, because that is the only
589 -- case in which non-static universal integer values can occur, and
590 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
591 -- called in contexts like the expression of a number declaration where
592 -- we certainly want to allow out of range values.
594 -- We inhibit the warning when expansion is disabled, because the
595 -- preanalysis of a range of a 64-bit modular type may appear to
596 -- violate the constraint on non-static Universal_Integer. If there
597 -- is a true overflow it will be diagnosed during full analysis.
599 if Etype (N) = Universal_Integer
600 and then Nkind (N) = N_Integer_Literal
601 and then Nkind (Parent (N)) in N_Subexpr
602 and then Expander_Active
603 and then
604 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
605 or else
606 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
607 then
608 Apply_Compile_Time_Constraint_Error
609 (N, "non-static universal integer value out of range<<",
610 CE_Range_Check_Failed);
612 -- Check out of range of base type
614 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
615 Out_Of_Range (N);
617 -- Give a warning or error on the value outside the subtype. A warning
618 -- is omitted if the expression appears in a range that could be null
619 -- (warnings are handled elsewhere for this case).
621 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
622 if Is_In_Range (N, T, Assume_Valid => True) then
623 null;
625 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
626 -- Ignore out of range values for System.Priority in CodePeer
627 -- mode since the actual target compiler may provide a wider
628 -- range.
630 if CodePeer_Mode and then Is_RTE (T, RE_Priority) then
631 Set_Do_Range_Check (N, False);
633 -- Determine if the out-of-range violation constitutes a warning
634 -- or an error based on context, according to RM 4.9 (34/3).
636 elsif Nkind (Original_Node (N)) in
637 N_Type_Conversion | N_Qualified_Expression
638 and then Comes_From_Source (Original_Node (N))
639 then
640 Apply_Compile_Time_Constraint_Error
641 (N, "value not in range of}", CE_Range_Check_Failed);
642 else
643 Apply_Compile_Time_Constraint_Error
644 (N, "value not in range of}<<", CE_Range_Check_Failed);
645 end if;
647 elsif Checks_On then
648 Enable_Range_Check (N);
650 else
651 Set_Do_Range_Check (N, False);
652 end if;
653 end if;
654 end Check_Non_Static_Context;
656 -------------------------------------------
657 -- Check_Non_Static_Context_For_Overflow --
658 -------------------------------------------
660 procedure Check_Non_Static_Context_For_Overflow
661 (N : Node_Id;
662 Stat : Boolean;
663 Result : Uint)
665 begin
666 if (not Stat or else In_Inlined_Body)
667 and then Is_Signed_Integer_Type (Etype (N))
668 then
669 declare
670 BT : constant Entity_Id := Base_Type (Etype (N));
671 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
672 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
673 begin
674 if Result < Lo or else Result > Hi then
675 Apply_Compile_Time_Constraint_Error
676 (N, "value not in range of }??",
677 CE_Overflow_Check_Failed,
678 Ent => BT);
679 end if;
680 end;
681 end if;
682 end Check_Non_Static_Context_For_Overflow;
684 ---------------------------------
685 -- Check_String_Literal_Length --
686 ---------------------------------
688 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
689 begin
690 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
691 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
692 then
693 Apply_Compile_Time_Constraint_Error
694 (N, "string length wrong for}??",
695 CE_Length_Check_Failed,
696 Ent => Ttype,
697 Typ => Ttype);
698 end if;
699 end if;
700 end Check_String_Literal_Length;
702 --------------------------------------------
703 -- Checking_Potentially_Static_Expression --
704 --------------------------------------------
706 function Checking_Potentially_Static_Expression return Boolean is
707 begin
708 return Checking_For_Potentially_Static_Expression;
709 end Checking_Potentially_Static_Expression;
711 --------------------
712 -- Choice_Matches --
713 --------------------
715 function Choice_Matches
716 (Expr : Node_Id;
717 Choice : Node_Id) return Match_Result
719 Etyp : constant Entity_Id := Etype (Expr);
720 Val : Uint;
721 ValR : Ureal;
722 ValS : Node_Id;
724 begin
725 pragma Assert (Compile_Time_Known_Value (Expr));
726 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
728 if not Is_OK_Static_Choice (Choice) then
729 Set_Raises_Constraint_Error (Choice);
730 return Non_Static;
732 -- When the choice denotes a subtype with a static predictate, check the
733 -- expression against the predicate values. Different procedures apply
734 -- to discrete and non-discrete types.
736 elsif (Nkind (Choice) = N_Subtype_Indication
737 or else (Is_Entity_Name (Choice)
738 and then Is_Type (Entity (Choice))))
739 and then Has_Predicates (Etype (Choice))
740 and then Has_Static_Predicate (Etype (Choice))
741 then
742 if Is_Discrete_Type (Etype (Choice)) then
743 return
744 Choices_Match
745 (Expr, Static_Discrete_Predicate (Etype (Choice)));
747 elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice))
748 then
749 return Match;
751 else
752 return No_Match;
753 end if;
755 -- Discrete type case only
757 elsif Is_Discrete_Type (Etyp) then
758 Val := Expr_Value (Expr);
760 if Nkind (Choice) = N_Range then
761 if Val >= Expr_Value (Low_Bound (Choice))
762 and then
763 Val <= Expr_Value (High_Bound (Choice))
764 then
765 return Match;
766 else
767 return No_Match;
768 end if;
770 elsif Nkind (Choice) = N_Subtype_Indication
771 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
772 then
773 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
774 and then
775 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
776 then
777 return Match;
778 else
779 return No_Match;
780 end if;
782 elsif Nkind (Choice) = N_Others_Choice then
783 return Match;
785 else
786 if Val = Expr_Value (Choice) then
787 return Match;
788 else
789 return No_Match;
790 end if;
791 end if;
793 -- Real type case
795 elsif Is_Real_Type (Etyp) then
796 ValR := Expr_Value_R (Expr);
798 if Nkind (Choice) = N_Range then
799 if ValR >= Expr_Value_R (Low_Bound (Choice))
800 and then
801 ValR <= Expr_Value_R (High_Bound (Choice))
802 then
803 return Match;
804 else
805 return No_Match;
806 end if;
808 elsif Nkind (Choice) = N_Subtype_Indication
809 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
810 then
811 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
812 and then
813 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
814 then
815 return Match;
816 else
817 return No_Match;
818 end if;
820 else
821 if ValR = Expr_Value_R (Choice) then
822 return Match;
823 else
824 return No_Match;
825 end if;
826 end if;
828 -- String type cases
830 else
831 pragma Assert (Is_String_Type (Etyp));
832 ValS := Expr_Value_S (Expr);
834 if Nkind (Choice) = N_Subtype_Indication
835 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
836 then
837 if not Is_Constrained (Etype (Choice)) then
838 return Match;
840 else
841 declare
842 Typlen : constant Uint :=
843 String_Type_Len (Etype (Choice));
844 Strlen : constant Uint :=
845 UI_From_Int (String_Length (Strval (ValS)));
846 begin
847 if Typlen = Strlen then
848 return Match;
849 else
850 return No_Match;
851 end if;
852 end;
853 end if;
855 else
856 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
857 then
858 return Match;
859 else
860 return No_Match;
861 end if;
862 end if;
863 end if;
864 end Choice_Matches;
866 -------------------
867 -- Choices_Match --
868 -------------------
870 function Choices_Match
871 (Expr : Node_Id;
872 Choices : List_Id) return Match_Result
874 Choice : Node_Id;
875 Result : Match_Result;
877 begin
878 Choice := First (Choices);
879 while Present (Choice) loop
880 Result := Choice_Matches (Expr, Choice);
882 if Result /= No_Match then
883 return Result;
884 end if;
886 Next (Choice);
887 end loop;
889 return No_Match;
890 end Choices_Match;
892 --------------------------
893 -- Compile_Time_Compare --
894 --------------------------
896 function Compile_Time_Compare
897 (L, R : Node_Id;
898 Assume_Valid : Boolean) return Compare_Result
900 Discard : aliased Uint;
901 begin
902 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
903 end Compile_Time_Compare;
905 function Compile_Time_Compare
906 (L, R : Node_Id;
907 Diff : access Uint;
908 Assume_Valid : Boolean;
909 Rec : Boolean := False) return Compare_Result
911 Ltyp : Entity_Id := Etype (L);
912 Rtyp : Entity_Id := Etype (R);
914 Discard : aliased Uint;
916 procedure Compare_Decompose
917 (N : Node_Id;
918 R : out Node_Id;
919 V : out Uint);
920 -- This procedure decomposes the node N into an expression node and a
921 -- signed offset, so that the value of N is equal to the value of R plus
922 -- the value V (which may be negative). If no such decomposition is
923 -- possible, then on return R is a copy of N, and V is set to zero.
925 function Compare_Fixup (N : Node_Id) return Node_Id;
926 -- This function deals with replacing 'Last and 'First references with
927 -- their corresponding type bounds, which we then can compare. The
928 -- argument is the original node, the result is the identity, unless we
929 -- have a 'Last/'First reference in which case the value returned is the
930 -- appropriate type bound.
932 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
933 -- Even if the context does not assume that values are valid, some
934 -- simple cases can be recognized.
936 function Is_Same_Value (L, R : Node_Id) return Boolean;
937 -- Returns True iff L and R represent expressions that definitely have
938 -- identical (but not necessarily compile-time-known) values Indeed the
939 -- caller is expected to have already dealt with the cases of compile
940 -- time known values, so these are not tested here.
942 -----------------------
943 -- Compare_Decompose --
944 -----------------------
946 procedure Compare_Decompose
947 (N : Node_Id;
948 R : out Node_Id;
949 V : out Uint)
951 begin
952 if Nkind (N) = N_Op_Add
953 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
954 then
955 R := Left_Opnd (N);
956 V := Intval (Right_Opnd (N));
957 return;
959 elsif Nkind (N) = N_Op_Subtract
960 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
961 then
962 R := Left_Opnd (N);
963 V := UI_Negate (Intval (Right_Opnd (N)));
964 return;
966 elsif Nkind (N) = N_Attribute_Reference then
967 if Attribute_Name (N) = Name_Succ then
968 R := First (Expressions (N));
969 V := Uint_1;
970 return;
972 elsif Attribute_Name (N) = Name_Pred then
973 R := First (Expressions (N));
974 V := Uint_Minus_1;
975 return;
976 end if;
977 end if;
979 R := N;
980 V := Uint_0;
981 end Compare_Decompose;
983 -------------------
984 -- Compare_Fixup --
985 -------------------
987 function Compare_Fixup (N : Node_Id) return Node_Id is
988 Indx : Node_Id;
989 Xtyp : Entity_Id;
990 Subs : Nat;
992 begin
993 -- Fixup only required for First/Last attribute reference
995 if Nkind (N) = N_Attribute_Reference
996 and then Attribute_Name (N) in Name_First | Name_Last
997 then
998 Xtyp := Etype (Prefix (N));
1000 -- If we have no type, then just abandon the attempt to do
1001 -- a fixup, this is probably the result of some other error.
1003 if No (Xtyp) then
1004 return N;
1005 end if;
1007 -- Dereference an access type
1009 if Is_Access_Type (Xtyp) then
1010 Xtyp := Designated_Type (Xtyp);
1011 end if;
1013 -- If we don't have an array type at this stage, something is
1014 -- peculiar, e.g. another error, and we abandon the attempt at
1015 -- a fixup.
1017 if not Is_Array_Type (Xtyp) then
1018 return N;
1019 end if;
1021 -- Ignore unconstrained array, since bounds are not meaningful
1023 if not Is_Constrained (Xtyp) then
1024 return N;
1025 end if;
1027 if Ekind (Xtyp) = E_String_Literal_Subtype then
1028 if Attribute_Name (N) = Name_First then
1029 return String_Literal_Low_Bound (Xtyp);
1030 else
1031 return
1032 Make_Integer_Literal (Sloc (N),
1033 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
1034 String_Literal_Length (Xtyp));
1035 end if;
1036 end if;
1038 -- Find correct index type
1040 Indx := First_Index (Xtyp);
1042 if Present (Expressions (N)) then
1043 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
1045 for J in 2 .. Subs loop
1046 Next_Index (Indx);
1047 end loop;
1048 end if;
1050 Xtyp := Etype (Indx);
1052 if Attribute_Name (N) = Name_First then
1053 return Type_Low_Bound (Xtyp);
1054 else
1055 return Type_High_Bound (Xtyp);
1056 end if;
1057 end if;
1059 return N;
1060 end Compare_Fixup;
1062 ----------------------------
1063 -- Is_Known_Valid_Operand --
1064 ----------------------------
1066 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
1067 begin
1068 return (Is_Entity_Name (Opnd)
1069 and then
1070 (Is_Known_Valid (Entity (Opnd))
1071 or else Ekind (Entity (Opnd)) = E_In_Parameter
1072 or else
1073 (Is_Object (Entity (Opnd))
1074 and then Present (Current_Value (Entity (Opnd))))))
1075 or else Is_OK_Static_Expression (Opnd);
1076 end Is_Known_Valid_Operand;
1078 -------------------
1079 -- Is_Same_Value --
1080 -------------------
1082 function Is_Same_Value (L, R : Node_Id) return Boolean is
1083 Lf : constant Node_Id := Compare_Fixup (L);
1084 Rf : constant Node_Id := Compare_Fixup (R);
1086 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean;
1087 -- An attribute reference to Loop_Entry may have been rewritten into
1088 -- its prefix as a way to avoid generating a constant for that
1089 -- attribute when the corresponding pragma is ignored. These nodes
1090 -- should be ignored when deciding if they can be equal to one
1091 -- another.
1093 function Is_Same_Subscript (L, R : List_Id) return Boolean;
1094 -- L, R are the Expressions values from two attribute nodes for First
1095 -- or Last attributes. Either may be set to No_List if no expressions
1096 -- are present (indicating subscript 1). The result is True if both
1097 -- expressions represent the same subscript (note one case is where
1098 -- one subscript is missing and the other is explicitly set to 1).
1100 -----------------------------
1101 -- Is_Rewritten_Loop_Entry --
1102 -----------------------------
1104 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean is
1105 Orig_N : constant Node_Id := Original_Node (N);
1106 begin
1107 return Orig_N /= N
1108 and then Nkind (Orig_N) = N_Attribute_Reference
1109 and then Get_Attribute_Id (Attribute_Name (Orig_N)) =
1110 Attribute_Loop_Entry;
1111 end Is_Rewritten_Loop_Entry;
1113 -----------------------
1114 -- Is_Same_Subscript --
1115 -----------------------
1117 function Is_Same_Subscript (L, R : List_Id) return Boolean is
1118 begin
1119 if L = No_List then
1120 if R = No_List then
1121 return True;
1122 else
1123 return Expr_Value (First (R)) = Uint_1;
1124 end if;
1126 else
1127 if R = No_List then
1128 return Expr_Value (First (L)) = Uint_1;
1129 else
1130 return Expr_Value (First (L)) = Expr_Value (First (R));
1131 end if;
1132 end if;
1133 end Is_Same_Subscript;
1135 -- Start of processing for Is_Same_Value
1137 begin
1138 -- Loop_Entry nodes rewritten into their prefix inside ignored
1139 -- pragmas should never lead to a decision of equality.
1141 if Is_Rewritten_Loop_Entry (Lf)
1142 or else Is_Rewritten_Loop_Entry (Rf)
1143 then
1144 return False;
1146 -- Values are the same if they refer to the same entity and the
1147 -- entity is nonvolatile.
1149 elsif Nkind (Lf) in N_Identifier | N_Expanded_Name
1150 and then Nkind (Rf) in N_Identifier | N_Expanded_Name
1151 and then Entity (Lf) = Entity (Rf)
1153 -- If the entity is a discriminant, the two expressions may be
1154 -- bounds of components of objects of the same discriminated type.
1155 -- The values of the discriminants are not static, and therefore
1156 -- the result is unknown.
1158 and then Ekind (Entity (Lf)) /= E_Discriminant
1159 and then Present (Entity (Lf))
1161 -- This does not however apply to Float types, since we may have
1162 -- two NaN values and they should never compare equal.
1164 and then not Is_Floating_Point_Type (Etype (L))
1165 and then not Is_Volatile_Reference (L)
1166 and then not Is_Volatile_Reference (R)
1167 then
1168 return True;
1170 -- Or if they are compile-time-known and identical
1172 elsif Compile_Time_Known_Value (Lf)
1173 and then
1174 Compile_Time_Known_Value (Rf)
1175 and then Expr_Value (Lf) = Expr_Value (Rf)
1176 then
1177 return True;
1179 -- False if Nkind of the two nodes is different for remaining cases
1181 elsif Nkind (Lf) /= Nkind (Rf) then
1182 return False;
1184 -- True if both 'First or 'Last values applying to the same entity
1185 -- (first and last don't change even if value does). Note that we
1186 -- need this even with the calls to Compare_Fixup, to handle the
1187 -- case of unconstrained array attributes where Compare_Fixup
1188 -- cannot find useful bounds.
1190 elsif Nkind (Lf) = N_Attribute_Reference
1191 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1192 and then Attribute_Name (Lf) in Name_First | Name_Last
1193 and then Nkind (Prefix (Lf)) in N_Identifier | N_Expanded_Name
1194 and then Nkind (Prefix (Rf)) in N_Identifier | N_Expanded_Name
1195 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1196 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1197 then
1198 return True;
1200 -- True if the same selected component from the same record
1202 elsif Nkind (Lf) = N_Selected_Component
1203 and then Selector_Name (Lf) = Selector_Name (Rf)
1204 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1205 then
1206 return True;
1208 -- True if the same unary operator applied to the same operand
1210 elsif Nkind (Lf) in N_Unary_Op
1211 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1212 then
1213 return True;
1215 -- True if the same binary operator applied to the same operands
1217 elsif Nkind (Lf) in N_Binary_Op
1218 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1219 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1220 then
1221 return True;
1223 -- All other cases, we can't tell, so return False
1225 else
1226 return False;
1227 end if;
1228 end Is_Same_Value;
1230 -- Start of processing for Compile_Time_Compare
1232 begin
1233 Diff.all := No_Uint;
1235 -- In preanalysis mode, always return Unknown unless the expression
1236 -- is static. It is too early to be thinking we know the result of a
1237 -- comparison, save that judgment for the full analysis. This is
1238 -- particularly important in the case of pre and postconditions, which
1239 -- otherwise can be prematurely collapsed into having True or False
1240 -- conditions when this is inappropriate.
1242 if not (Full_Analysis
1243 or else (Is_OK_Static_Expression (L)
1244 and then
1245 Is_OK_Static_Expression (R)))
1246 then
1247 return Unknown;
1248 end if;
1250 -- If either operand could raise Constraint_Error, then we cannot
1251 -- know the result at compile time (since CE may be raised).
1253 if not (Cannot_Raise_Constraint_Error (L)
1254 and then
1255 Cannot_Raise_Constraint_Error (R))
1256 then
1257 return Unknown;
1258 end if;
1260 -- Identical operands are most certainly equal
1262 if L = R then
1263 return EQ;
1264 end if;
1266 -- If expressions have no types, then do not attempt to determine if
1267 -- they are the same, since something funny is going on. One case in
1268 -- which this happens is during generic template analysis, when bounds
1269 -- are not fully analyzed.
1271 if No (Ltyp) or else No (Rtyp) then
1272 return Unknown;
1273 end if;
1275 -- These get reset to the base type for the case of entities where
1276 -- Is_Known_Valid is not set. This takes care of handling possible
1277 -- invalid representations using the value of the base type, in
1278 -- accordance with RM 13.9.1(10).
1280 Ltyp := Underlying_Type (Ltyp);
1281 Rtyp := Underlying_Type (Rtyp);
1283 -- Same rationale as above, but for Underlying_Type instead of Etype
1285 if No (Ltyp) or else No (Rtyp) then
1286 return Unknown;
1287 end if;
1289 -- We do not attempt comparisons for packed arrays represented as
1290 -- modular types, where the semantics of comparison is quite different.
1292 if Is_Packed_Array_Impl_Type (Ltyp)
1293 and then Is_Modular_Integer_Type (Ltyp)
1294 then
1295 return Unknown;
1297 -- For access types, the only time we know the result at compile time
1298 -- (apart from identical operands, which we handled already) is if we
1299 -- know one operand is null and the other is not, or both operands are
1300 -- known null.
1302 elsif Is_Access_Type (Ltyp) then
1303 if Known_Null (L) then
1304 if Known_Null (R) then
1305 return EQ;
1306 elsif Known_Non_Null (R) then
1307 return NE;
1308 else
1309 return Unknown;
1310 end if;
1312 elsif Known_Non_Null (L) and then Known_Null (R) then
1313 return NE;
1315 else
1316 return Unknown;
1317 end if;
1319 -- Case where comparison involves two compile-time-known values
1321 elsif Compile_Time_Known_Value (L)
1322 and then
1323 Compile_Time_Known_Value (R)
1324 then
1325 -- For the floating-point case, we have to be a little careful, since
1326 -- at compile time we are dealing with universal exact values, but at
1327 -- runtime, these will be in non-exact target form. That's why the
1328 -- returned results are LE and GE below instead of LT and GT.
1330 if Is_Floating_Point_Type (Ltyp)
1331 or else
1332 Is_Floating_Point_Type (Rtyp)
1333 then
1334 declare
1335 Lo : constant Ureal := Expr_Value_R (L);
1336 Hi : constant Ureal := Expr_Value_R (R);
1337 begin
1338 if Lo < Hi then
1339 return LE;
1340 elsif Lo = Hi then
1341 return EQ;
1342 else
1343 return GE;
1344 end if;
1345 end;
1347 -- For string types, we have two string literals and we proceed to
1348 -- compare them using the Ada style dictionary string comparison.
1350 elsif not Is_Scalar_Type (Ltyp) then
1351 declare
1352 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1353 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1354 Llen : constant Nat := String_Length (Lstring);
1355 Rlen : constant Nat := String_Length (Rstring);
1357 begin
1358 for J in 1 .. Nat'Min (Llen, Rlen) loop
1359 declare
1360 LC : constant Char_Code := Get_String_Char (Lstring, J);
1361 RC : constant Char_Code := Get_String_Char (Rstring, J);
1362 begin
1363 if LC < RC then
1364 return LT;
1365 elsif LC > RC then
1366 return GT;
1367 end if;
1368 end;
1369 end loop;
1371 if Llen < Rlen then
1372 return LT;
1373 elsif Llen > Rlen then
1374 return GT;
1375 else
1376 return EQ;
1377 end if;
1378 end;
1380 -- For remaining scalar cases we know exactly (note that this does
1381 -- include the fixed-point case, where we know the run time integer
1382 -- values now).
1384 else
1385 declare
1386 Lo : constant Uint := Expr_Value (L);
1387 Hi : constant Uint := Expr_Value (R);
1388 begin
1389 if Lo < Hi then
1390 Diff.all := Hi - Lo;
1391 return LT;
1392 elsif Lo = Hi then
1393 return EQ;
1394 else
1395 Diff.all := Lo - Hi;
1396 return GT;
1397 end if;
1398 end;
1399 end if;
1401 -- Cases where at least one operand is not known at compile time
1403 else
1404 -- Remaining checks apply only for discrete types
1406 if not Is_Discrete_Type (Ltyp)
1407 or else
1408 not Is_Discrete_Type (Rtyp)
1409 then
1410 return Unknown;
1411 end if;
1413 -- Defend against generic types, or actually any expressions that
1414 -- contain a reference to a generic type from within a generic
1415 -- template. We don't want to do any range analysis of such
1416 -- expressions for two reasons. First, the bounds of a generic type
1417 -- itself are junk and cannot be used for any kind of analysis.
1418 -- Second, we may have a case where the range at run time is indeed
1419 -- known, but we don't want to do compile time analysis in the
1420 -- template based on that range since in an instance the value may be
1421 -- static, and able to be elaborated without reference to the bounds
1422 -- of types involved. As an example, consider:
1424 -- (F'Pos (F'Last) + 1) > Integer'Last
1426 -- The expression on the left side of > is Universal_Integer and thus
1427 -- acquires the type Integer for evaluation at run time, and at run
1428 -- time it is true that this condition is always False, but within
1429 -- an instance F may be a type with a static range greater than the
1430 -- range of Integer, and the expression statically evaluates to True.
1432 if References_Generic_Formal_Type (L)
1433 or else
1434 References_Generic_Formal_Type (R)
1435 then
1436 return Unknown;
1437 end if;
1439 -- Replace types by base types for the case of values which are not
1440 -- known to have valid representations. This takes care of properly
1441 -- dealing with invalid representations.
1443 if not Assume_Valid then
1444 if not (Is_Entity_Name (L)
1445 and then (Is_Known_Valid (Entity (L))
1446 or else Assume_No_Invalid_Values))
1447 then
1448 Ltyp := Underlying_Type (Base_Type (Ltyp));
1449 end if;
1451 if not (Is_Entity_Name (R)
1452 and then (Is_Known_Valid (Entity (R))
1453 or else Assume_No_Invalid_Values))
1454 then
1455 Rtyp := Underlying_Type (Base_Type (Rtyp));
1456 end if;
1457 end if;
1459 -- First attempt is to decompose the expressions to extract a
1460 -- constant offset resulting from the use of any of the forms:
1462 -- expr + literal
1463 -- expr - literal
1464 -- typ'Succ (expr)
1465 -- typ'Pred (expr)
1467 -- Then we see if the two expressions are the same value, and if so
1468 -- the result is obtained by comparing the offsets.
1470 -- Note: the reason we do this test first is that it returns only
1471 -- decisive results (with diff set), where other tests, like the
1472 -- range test, may not be as so decisive. Consider for example
1473 -- J .. J + 1. This code can conclude LT with a difference of 1,
1474 -- even if the range of J is not known.
1476 declare
1477 Lnode : Node_Id;
1478 Loffs : Uint;
1479 Rnode : Node_Id;
1480 Roffs : Uint;
1482 begin
1483 Compare_Decompose (L, Lnode, Loffs);
1484 Compare_Decompose (R, Rnode, Roffs);
1486 if Is_Same_Value (Lnode, Rnode) then
1487 if Loffs = Roffs then
1488 return EQ;
1489 end if;
1491 -- When the offsets are not equal, we can go farther only if
1492 -- the types are not modular (e.g. X < X + 1 is False if X is
1493 -- the largest number).
1495 if not Is_Modular_Integer_Type (Ltyp)
1496 and then not Is_Modular_Integer_Type (Rtyp)
1497 then
1498 if Loffs < Roffs then
1499 Diff.all := Roffs - Loffs;
1500 return LT;
1501 else
1502 Diff.all := Loffs - Roffs;
1503 return GT;
1504 end if;
1505 end if;
1506 end if;
1507 end;
1509 -- Next, try range analysis and see if operand ranges are disjoint
1511 declare
1512 LOK, ROK : Boolean;
1513 LLo, LHi : Uint;
1514 RLo, RHi : Uint;
1516 Single : Boolean;
1517 -- True if each range is a single point
1519 begin
1520 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1521 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1523 if LOK and ROK then
1524 Single := (LLo = LHi) and then (RLo = RHi);
1526 if LHi < RLo then
1527 if Single and Assume_Valid then
1528 Diff.all := RLo - LLo;
1529 end if;
1531 return LT;
1533 elsif RHi < LLo then
1534 if Single and Assume_Valid then
1535 Diff.all := LLo - RLo;
1536 end if;
1538 return GT;
1540 elsif Single and then LLo = RLo then
1542 -- If the range includes a single literal and we can assume
1543 -- validity then the result is known even if an operand is
1544 -- not static.
1546 if Assume_Valid then
1547 return EQ;
1548 else
1549 return Unknown;
1550 end if;
1552 elsif LHi = RLo then
1553 return LE;
1555 elsif RHi = LLo then
1556 return GE;
1558 elsif not Is_Known_Valid_Operand (L)
1559 and then not Assume_Valid
1560 then
1561 if Is_Same_Value (L, R) then
1562 return EQ;
1563 else
1564 return Unknown;
1565 end if;
1566 end if;
1568 -- If the range of either operand cannot be determined, nothing
1569 -- further can be inferred.
1571 else
1572 return Unknown;
1573 end if;
1574 end;
1576 -- Here is where we check for comparisons against maximum bounds of
1577 -- types, where we know that no value can be outside the bounds of
1578 -- the subtype. Note that this routine is allowed to assume that all
1579 -- expressions are within their subtype bounds. Callers wishing to
1580 -- deal with possibly invalid values must in any case take special
1581 -- steps (e.g. conversions to larger types) to avoid this kind of
1582 -- optimization, which is always considered to be valid. We do not
1583 -- attempt this optimization with generic types, since the type
1584 -- bounds may not be meaningful in this case.
1586 -- We are in danger of an infinite recursion here. It does not seem
1587 -- useful to go more than one level deep, so the parameter Rec is
1588 -- used to protect ourselves against this infinite recursion.
1590 if not Rec then
1592 -- See if we can get a decisive check against one operand and a
1593 -- bound of the other operand (four possible tests here). Note
1594 -- that we avoid testing junk bounds of a generic type.
1596 if not Is_Generic_Type (Rtyp) then
1597 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1598 Discard'Access,
1599 Assume_Valid, Rec => True)
1601 when LT => return LT;
1602 when LE => return LE;
1603 when EQ => return LE;
1604 when others => null;
1605 end case;
1607 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1608 Discard'Access,
1609 Assume_Valid, Rec => True)
1611 when GT => return GT;
1612 when GE => return GE;
1613 when EQ => return GE;
1614 when others => null;
1615 end case;
1616 end if;
1618 if not Is_Generic_Type (Ltyp) then
1619 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1620 Discard'Access,
1621 Assume_Valid, Rec => True)
1623 when GT => return GT;
1624 when GE => return GE;
1625 when EQ => return GE;
1626 when others => null;
1627 end case;
1629 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1630 Discard'Access,
1631 Assume_Valid, Rec => True)
1633 when LT => return LT;
1634 when LE => return LE;
1635 when EQ => return LE;
1636 when others => null;
1637 end case;
1638 end if;
1639 end if;
1641 -- Next attempt is to see if we have an entity compared with a
1642 -- compile-time-known value, where there is a current value
1643 -- conditional for the entity which can tell us the result.
1645 declare
1646 Var : Node_Id;
1647 -- Entity variable (left operand)
1649 Val : Uint;
1650 -- Value (right operand)
1652 Inv : Boolean;
1653 -- If False, we have reversed the operands
1655 Op : Node_Kind;
1656 -- Comparison operator kind from Get_Current_Value_Condition call
1658 Opn : Node_Id;
1659 -- Value from Get_Current_Value_Condition call
1661 Opv : Uint;
1662 -- Value of Opn
1664 Result : Compare_Result;
1665 -- Known result before inversion
1667 begin
1668 if Is_Entity_Name (L)
1669 and then Compile_Time_Known_Value (R)
1670 then
1671 Var := L;
1672 Val := Expr_Value (R);
1673 Inv := False;
1675 elsif Is_Entity_Name (R)
1676 and then Compile_Time_Known_Value (L)
1677 then
1678 Var := R;
1679 Val := Expr_Value (L);
1680 Inv := True;
1682 -- That was the last chance at finding a compile time result
1684 else
1685 return Unknown;
1686 end if;
1688 Get_Current_Value_Condition (Var, Op, Opn);
1690 -- That was the last chance, so if we got nothing return
1692 if No (Opn) then
1693 return Unknown;
1694 end if;
1696 Opv := Expr_Value (Opn);
1698 -- We got a comparison, so we might have something interesting
1700 -- Convert LE to LT and GE to GT, just so we have fewer cases
1702 if Op = N_Op_Le then
1703 Op := N_Op_Lt;
1704 Opv := Opv + 1;
1706 elsif Op = N_Op_Ge then
1707 Op := N_Op_Gt;
1708 Opv := Opv - 1;
1709 end if;
1711 -- Deal with equality case
1713 if Op = N_Op_Eq then
1714 if Val = Opv then
1715 Result := EQ;
1716 elsif Opv < Val then
1717 Result := LT;
1718 else
1719 Result := GT;
1720 end if;
1722 -- Deal with inequality case
1724 elsif Op = N_Op_Ne then
1725 if Val = Opv then
1726 Result := NE;
1727 else
1728 return Unknown;
1729 end if;
1731 -- Deal with greater than case
1733 elsif Op = N_Op_Gt then
1734 if Opv >= Val then
1735 Result := GT;
1736 elsif Opv = Val - 1 then
1737 Result := GE;
1738 else
1739 return Unknown;
1740 end if;
1742 -- Deal with less than case
1744 else pragma Assert (Op = N_Op_Lt);
1745 if Opv <= Val then
1746 Result := LT;
1747 elsif Opv = Val + 1 then
1748 Result := LE;
1749 else
1750 return Unknown;
1751 end if;
1752 end if;
1754 -- Deal with inverting result
1756 if Inv then
1757 case Result is
1758 when GT => return LT;
1759 when GE => return LE;
1760 when LT => return GT;
1761 when LE => return GE;
1762 when others => return Result;
1763 end case;
1764 end if;
1766 return Result;
1767 end;
1768 end if;
1769 end Compile_Time_Compare;
1771 -------------------------------
1772 -- Compile_Time_Known_Bounds --
1773 -------------------------------
1775 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1776 Indx : Node_Id;
1777 Typ : Entity_Id;
1779 begin
1780 if T = Any_Composite or else not Is_Array_Type (T) then
1781 return False;
1782 end if;
1784 Indx := First_Index (T);
1785 while Present (Indx) loop
1786 Typ := Underlying_Type (Etype (Indx));
1788 -- Never look at junk bounds of a generic type
1790 if Is_Generic_Type (Typ) then
1791 return False;
1792 end if;
1794 -- Otherwise check bounds for compile-time-known
1796 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1797 return False;
1798 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1799 return False;
1800 else
1801 Next_Index (Indx);
1802 end if;
1803 end loop;
1805 return True;
1806 end Compile_Time_Known_Bounds;
1808 ------------------------------
1809 -- Compile_Time_Known_Value --
1810 ------------------------------
1812 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1813 K : constant Node_Kind := Nkind (Op);
1814 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1816 begin
1817 -- Never known at compile time if bad type or raises Constraint_Error
1818 -- or empty (latter case occurs only as a result of a previous error).
1820 if No (Op) then
1821 Check_Error_Detected;
1822 return False;
1824 elsif Op = Error
1825 or else Etype (Op) = Any_Type
1826 or else Raises_Constraint_Error (Op)
1827 then
1828 return False;
1829 end if;
1831 -- If we have an entity name, then see if it is the name of a constant
1832 -- and if so, test the corresponding constant value, or the name of an
1833 -- enumeration literal, which is always a constant.
1835 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1836 declare
1837 Ent : constant Entity_Id := Entity (Op);
1838 Val : Node_Id;
1840 begin
1841 -- Never known at compile time if it is a packed array value. We
1842 -- might want to try to evaluate these at compile time one day,
1843 -- but we do not make that attempt now.
1845 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1846 return False;
1848 elsif Ekind (Ent) = E_Enumeration_Literal then
1849 return True;
1851 elsif Ekind (Ent) = E_Constant then
1852 Val := Constant_Value (Ent);
1854 if Present (Val) then
1856 -- Guard against an illegal deferred constant whose full
1857 -- view is initialized with a reference to itself. Treat
1858 -- this case as a value not known at compile time.
1860 if Is_Entity_Name (Val) and then Entity (Val) = Ent then
1861 return False;
1862 else
1863 return Compile_Time_Known_Value (Val);
1864 end if;
1866 -- Otherwise, the constant does not have a compile-time-known
1867 -- value.
1869 else
1870 return False;
1871 end if;
1872 end if;
1873 end;
1875 -- We have a value, see if it is compile-time-known
1877 else
1878 -- Integer literals are worth storing in the cache
1880 if K = N_Integer_Literal then
1881 CV_Ent.N := Op;
1882 CV_Ent.V := Intval (Op);
1883 return True;
1885 -- Other literals and NULL are known at compile time
1887 elsif K in
1888 N_Character_Literal | N_Real_Literal | N_String_Literal | N_Null
1889 then
1890 return True;
1892 -- Evaluate static discriminants, to eliminate dead paths and
1893 -- redundant discriminant checks.
1895 elsif Is_Static_Discriminant_Component (Op) then
1896 return True;
1897 end if;
1898 end if;
1900 -- If we fall through, not known at compile time
1902 return False;
1904 -- If we get an exception while trying to do this test, then some error
1905 -- has occurred, and we simply say that the value is not known after all
1907 exception
1908 when others =>
1909 -- With debug flag K we will get an exception unless an error has
1910 -- already occurred (useful for debugging).
1912 if Debug_Flag_K then
1913 Check_Error_Detected;
1914 end if;
1916 return False;
1917 end Compile_Time_Known_Value;
1919 --------------------------------------
1920 -- Compile_Time_Known_Value_Or_Aggr --
1921 --------------------------------------
1923 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1924 begin
1925 -- If we have an entity name, then see if it is the name of a constant
1926 -- and if so, test the corresponding constant value, or the name of
1927 -- an enumeration literal, which is always a constant.
1929 if Is_Entity_Name (Op) then
1930 declare
1931 E : constant Entity_Id := Entity (Op);
1932 V : Node_Id;
1934 begin
1935 if Ekind (E) = E_Enumeration_Literal then
1936 return True;
1938 elsif Ekind (E) /= E_Constant then
1939 return False;
1941 else
1942 V := Constant_Value (E);
1943 return Present (V)
1944 and then Compile_Time_Known_Value_Or_Aggr (V);
1945 end if;
1946 end;
1948 -- We have a value, see if it is compile-time-known
1950 else
1951 if Compile_Time_Known_Value (Op) then
1952 return True;
1954 elsif Nkind (Op) = N_Aggregate then
1956 if Present (Expressions (Op)) then
1957 declare
1958 Expr : Node_Id;
1959 begin
1960 Expr := First (Expressions (Op));
1961 while Present (Expr) loop
1962 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1963 return False;
1964 else
1965 Next (Expr);
1966 end if;
1967 end loop;
1968 end;
1969 end if;
1971 if Present (Component_Associations (Op)) then
1972 declare
1973 Cass : Node_Id;
1975 begin
1976 Cass := First (Component_Associations (Op));
1977 while Present (Cass) loop
1978 if not
1979 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1980 then
1981 return False;
1982 end if;
1984 Next (Cass);
1985 end loop;
1986 end;
1987 end if;
1989 return True;
1991 elsif Nkind (Op) = N_Qualified_Expression then
1992 return Compile_Time_Known_Value_Or_Aggr (Expression (Op));
1994 -- All other types of values are not known at compile time
1996 else
1997 return False;
1998 end if;
2000 end if;
2001 end Compile_Time_Known_Value_Or_Aggr;
2003 ---------------------------------------
2004 -- CRT_Safe_Compile_Time_Known_Value --
2005 ---------------------------------------
2007 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
2008 begin
2009 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
2010 and then not Is_OK_Static_Expression (Op)
2011 then
2012 return False;
2013 else
2014 return Compile_Time_Known_Value (Op);
2015 end if;
2016 end CRT_Safe_Compile_Time_Known_Value;
2018 -----------------
2019 -- Eval_Actual --
2020 -----------------
2022 -- This is only called for actuals of functions that are not predefined
2023 -- operators (which have already been rewritten as operators at this
2024 -- stage), so the call can never be folded, and all that needs doing for
2025 -- the actual is to do the check for a non-static context.
2027 procedure Eval_Actual (N : Node_Id) is
2028 begin
2029 Check_Non_Static_Context (N);
2030 end Eval_Actual;
2032 --------------------
2033 -- Eval_Allocator --
2034 --------------------
2036 -- Allocators are never static, so all we have to do is to do the
2037 -- check for a non-static context if an expression is present.
2039 procedure Eval_Allocator (N : Node_Id) is
2040 Expr : constant Node_Id := Expression (N);
2041 begin
2042 if Nkind (Expr) = N_Qualified_Expression then
2043 Check_Non_Static_Context (Expression (Expr));
2044 end if;
2045 end Eval_Allocator;
2047 ------------------------
2048 -- Eval_Arithmetic_Op --
2049 ------------------------
2051 -- Arithmetic operations are static functions, so the result is static
2052 -- if both operands are static (RM 4.9(7), 4.9(20)).
2054 procedure Eval_Arithmetic_Op (N : Node_Id) is
2055 Left : constant Node_Id := Left_Opnd (N);
2056 Right : constant Node_Id := Right_Opnd (N);
2057 Ltype : constant Entity_Id := Etype (Left);
2058 Rtype : constant Entity_Id := Etype (Right);
2059 Otype : Entity_Id := Empty;
2060 Stat : Boolean;
2061 Fold : Boolean;
2063 begin
2064 -- If not foldable we are done
2066 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2068 if not Fold then
2069 return;
2070 end if;
2072 -- Otherwise attempt to fold
2074 if Is_Universal_Numeric_Type (Etype (Left))
2075 and then
2076 Is_Universal_Numeric_Type (Etype (Right))
2077 then
2078 Otype := Find_Universal_Operator_Type (N);
2079 end if;
2081 -- Fold for cases where both operands are of integer type
2083 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
2084 declare
2085 Left_Int : constant Uint := Expr_Value (Left);
2086 Right_Int : constant Uint := Expr_Value (Right);
2087 Result : Uint;
2089 begin
2090 case Nkind (N) is
2091 when N_Op_Add =>
2092 Result := Left_Int + Right_Int;
2094 when N_Op_Subtract =>
2095 Result := Left_Int - Right_Int;
2097 when N_Op_Multiply =>
2098 if OK_Bits
2099 (N, UI_From_Int
2100 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
2101 then
2102 Result := Left_Int * Right_Int;
2103 else
2104 Result := Left_Int;
2105 end if;
2107 when N_Op_Divide =>
2109 -- The exception Constraint_Error is raised by integer
2110 -- division, rem and mod if the right operand is zero.
2112 if Right_Int = 0 then
2114 -- When SPARK_Mode is On, force a warning instead of
2115 -- an error in that case, as this likely corresponds
2116 -- to deactivated code.
2118 Apply_Compile_Time_Constraint_Error
2119 (N, "division by zero", CE_Divide_By_Zero,
2120 Warn => not Stat or SPARK_Mode = On);
2121 return;
2123 -- Otherwise we can do the division
2125 else
2126 Result := Left_Int / Right_Int;
2127 end if;
2129 when N_Op_Mod =>
2131 -- The exception Constraint_Error is raised by integer
2132 -- division, rem and mod if the right operand is zero.
2134 if Right_Int = 0 then
2136 -- When SPARK_Mode is On, force a warning instead of
2137 -- an error in that case, as this likely corresponds
2138 -- to deactivated code.
2140 Apply_Compile_Time_Constraint_Error
2141 (N, "mod with zero divisor", CE_Divide_By_Zero,
2142 Warn => not Stat or SPARK_Mode = On);
2143 return;
2145 else
2146 Result := Left_Int mod Right_Int;
2147 end if;
2149 when N_Op_Rem =>
2151 -- The exception Constraint_Error is raised by integer
2152 -- division, rem and mod if the right operand is zero.
2154 if Right_Int = 0 then
2156 -- When SPARK_Mode is On, force a warning instead of
2157 -- an error in that case, as this likely corresponds
2158 -- to deactivated code.
2160 Apply_Compile_Time_Constraint_Error
2161 (N, "rem with zero divisor", CE_Divide_By_Zero,
2162 Warn => not Stat or SPARK_Mode = On);
2163 return;
2165 else
2166 Result := Left_Int rem Right_Int;
2167 end if;
2169 when others =>
2170 raise Program_Error;
2171 end case;
2173 -- Adjust the result by the modulus if the type is a modular type
2175 if Is_Modular_Integer_Type (Ltype) then
2176 Result := Result mod Modulus (Ltype);
2177 end if;
2179 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
2181 -- If we get here we can fold the result
2183 Fold_Uint (N, Result, Stat);
2184 end;
2186 -- Cases where at least one operand is a real. We handle the cases of
2187 -- both reals, or mixed/real integer cases (the latter happen only for
2188 -- divide and multiply, and the result is always real).
2190 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2191 declare
2192 Left_Real : Ureal;
2193 Right_Real : Ureal;
2194 Result : Ureal;
2196 begin
2197 if Is_Real_Type (Ltype) then
2198 Left_Real := Expr_Value_R (Left);
2199 else
2200 Left_Real := UR_From_Uint (Expr_Value (Left));
2201 end if;
2203 if Is_Real_Type (Rtype) then
2204 Right_Real := Expr_Value_R (Right);
2205 else
2206 Right_Real := UR_From_Uint (Expr_Value (Right));
2207 end if;
2209 if Nkind (N) = N_Op_Add then
2210 Result := Left_Real + Right_Real;
2212 elsif Nkind (N) = N_Op_Subtract then
2213 Result := Left_Real - Right_Real;
2215 elsif Nkind (N) = N_Op_Multiply then
2216 Result := Left_Real * Right_Real;
2218 else pragma Assert (Nkind (N) = N_Op_Divide);
2219 if UR_Is_Zero (Right_Real) then
2220 Apply_Compile_Time_Constraint_Error
2221 (N, "division by zero", CE_Divide_By_Zero);
2222 return;
2223 end if;
2225 Result := Left_Real / Right_Real;
2226 end if;
2228 Fold_Ureal (N, Result, Stat);
2229 end;
2230 end if;
2232 -- If the operator was resolved to a specific type, make sure that type
2233 -- is frozen even if the expression is folded into a literal (which has
2234 -- a universal type).
2236 if Present (Otype) then
2237 Freeze_Before (N, Otype);
2238 end if;
2239 end Eval_Arithmetic_Op;
2241 ----------------------------
2242 -- Eval_Character_Literal --
2243 ----------------------------
2245 -- Nothing to be done
2247 procedure Eval_Character_Literal (N : Node_Id) is
2248 pragma Warnings (Off, N);
2249 begin
2250 null;
2251 end Eval_Character_Literal;
2253 ---------------
2254 -- Eval_Call --
2255 ---------------
2257 -- Static function calls are either calls to predefined operators
2258 -- with static arguments, or calls to functions that rename a literal.
2259 -- Only the latter case is handled here, predefined operators are
2260 -- constant-folded elsewhere.
2262 -- If the function is itself inherited the literal of the parent type must
2263 -- be explicitly converted to the return type of the function.
2265 procedure Eval_Call (N : Node_Id) is
2266 Loc : constant Source_Ptr := Sloc (N);
2267 Typ : constant Entity_Id := Etype (N);
2268 Lit : Entity_Id;
2270 begin
2271 if Nkind (N) = N_Function_Call
2272 and then No (Parameter_Associations (N))
2273 and then Is_Entity_Name (Name (N))
2274 and then Present (Alias (Entity (Name (N))))
2275 and then Is_Enumeration_Type (Base_Type (Typ))
2276 then
2277 Lit := Ultimate_Alias (Entity (Name (N)));
2279 if Ekind (Lit) = E_Enumeration_Literal then
2280 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2281 Rewrite
2282 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2283 else
2284 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2285 end if;
2287 Resolve (N, Typ);
2288 end if;
2290 elsif Nkind (N) = N_Function_Call
2291 and then Is_Entity_Name (Name (N))
2292 and then Is_Intrinsic_Subprogram (Entity (Name (N)))
2293 then
2294 Eval_Intrinsic_Call (N, Entity (Name (N)));
2296 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2297 -- is enabled and we have a call to a static function, substitute a
2298 -- static value for the call, to allow folding the expression. This
2299 -- supports checking the requirement of RM 6.8(5.3/5) in
2300 -- Analyze_Expression_Function.
2302 elsif Checking_Potentially_Static_Expression
2303 and then Is_Static_Function_Call (N)
2304 then
2305 Fold_Dummy (N, Typ);
2306 end if;
2307 end Eval_Call;
2309 --------------------------
2310 -- Eval_Case_Expression --
2311 --------------------------
2313 -- A conditional expression is static if all its conditions and dependent
2314 -- expressions are static. Note that we do not care if the dependent
2315 -- expressions raise CE, except for the one that will be selected.
2317 procedure Eval_Case_Expression (N : Node_Id) is
2318 Alt : Node_Id;
2319 Choice : Node_Id;
2321 begin
2322 Set_Is_Static_Expression (N, False);
2324 if Error_Posted (Expression (N))
2325 or else not Is_Static_Expression (Expression (N))
2326 then
2327 Check_Non_Static_Context (Expression (N));
2328 return;
2329 end if;
2331 -- First loop, make sure all the alternatives are static expressions
2332 -- none of which raise Constraint_Error. We make the Constraint_Error
2333 -- check because part of the legality condition for a correct static
2334 -- case expression is that the cases are covered, like any other case
2335 -- expression. And we can't do that if any of the conditions raise an
2336 -- exception, so we don't even try to evaluate if that is the case.
2338 Alt := First (Alternatives (N));
2339 while Present (Alt) loop
2341 -- The expression must be static, but we don't care at this stage
2342 -- if it raises Constraint_Error (the alternative might not match,
2343 -- in which case the expression is statically unevaluated anyway).
2345 if not Is_Static_Expression (Expression (Alt)) then
2346 Check_Non_Static_Context (Expression (Alt));
2347 return;
2348 end if;
2350 -- The choices of a case always have to be static, and cannot raise
2351 -- an exception. If this condition is not met, then the expression
2352 -- is plain illegal, so just abandon evaluation attempts. No need
2353 -- to check non-static context when we have something illegal anyway.
2355 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2356 return;
2357 end if;
2359 Next (Alt);
2360 end loop;
2362 -- OK, if the above loop gets through it means that all choices are OK
2363 -- static (don't raise exceptions), so the whole case is static, and we
2364 -- can find the matching alternative.
2366 Set_Is_Static_Expression (N);
2368 -- Now to deal with propagating a possible Constraint_Error
2370 -- If the selecting expression raises CE, propagate and we are done
2372 if Raises_Constraint_Error (Expression (N)) then
2373 Set_Raises_Constraint_Error (N);
2375 -- Otherwise we need to check the alternatives to find the matching
2376 -- one. CE's in other than the matching one are not relevant. But we
2377 -- do need to check the matching one. Unlike the first loop, we do not
2378 -- have to go all the way through, when we find the matching one, quit.
2380 else
2381 Alt := First (Alternatives (N));
2382 Search : loop
2384 -- We must find a match among the alternatives. If not, this must
2385 -- be due to other errors, so just ignore, leaving as non-static.
2387 if No (Alt) then
2388 Set_Is_Static_Expression (N, False);
2389 return;
2390 end if;
2392 -- Otherwise loop through choices of this alternative
2394 Choice := First (Discrete_Choices (Alt));
2395 while Present (Choice) loop
2397 -- If we find a matching choice, then the Expression of this
2398 -- alternative replaces N (Raises_Constraint_Error flag is
2399 -- included, so we don't have to special case that).
2401 if Choice_Matches (Expression (N), Choice) = Match then
2402 Rewrite (N, Relocate_Node (Expression (Alt)));
2403 return;
2404 end if;
2406 Next (Choice);
2407 end loop;
2409 Next (Alt);
2410 end loop Search;
2411 end if;
2412 end Eval_Case_Expression;
2414 ------------------------
2415 -- Eval_Concatenation --
2416 ------------------------
2418 -- Concatenation is a static function, so the result is static if both
2419 -- operands are static (RM 4.9(7), 4.9(21)).
2421 procedure Eval_Concatenation (N : Node_Id) is
2422 Left : constant Node_Id := Left_Opnd (N);
2423 Right : constant Node_Id := Right_Opnd (N);
2424 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2425 Stat : Boolean;
2426 Fold : Boolean;
2428 begin
2429 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2430 -- non-static context.
2432 if Ada_Version = Ada_83
2433 and then Comes_From_Source (N)
2434 then
2435 Check_Non_Static_Context (Left);
2436 Check_Non_Static_Context (Right);
2437 return;
2438 end if;
2440 -- If not foldable we are done. In principle concatenation that yields
2441 -- any string type is static (i.e. an array type of character types).
2442 -- However, character types can include enumeration literals, and
2443 -- concatenation in that case cannot be described by a literal, so we
2444 -- only consider the operation static if the result is an array of
2445 -- (a descendant of) a predefined character type.
2447 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2449 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2450 Set_Is_Static_Expression (N, False);
2451 return;
2452 end if;
2454 -- Compile time string concatenation
2456 -- ??? Note that operands that are aggregates can be marked as static,
2457 -- so we should attempt at a later stage to fold concatenations with
2458 -- such aggregates.
2460 declare
2461 Left_Str : constant Node_Id := Get_String_Val (Left);
2462 Left_Len : Nat;
2463 Right_Str : constant Node_Id := Get_String_Val (Right);
2464 Folded_Val : String_Id := No_String;
2466 begin
2467 -- Establish new string literal, and store left operand. We make
2468 -- sure to use the special Start_String that takes an operand if
2469 -- the left operand is a string literal. Since this is optimized
2470 -- in the case where that is the most recently created string
2471 -- literal, we ensure efficient time/space behavior for the
2472 -- case of a concatenation of a series of string literals.
2474 if Nkind (Left_Str) = N_String_Literal then
2475 Left_Len := String_Length (Strval (Left_Str));
2477 -- If the left operand is the empty string, and the right operand
2478 -- is a string literal (the case of "" & "..."), the result is the
2479 -- value of the right operand. This optimization is important when
2480 -- Is_Folded_In_Parser, to avoid copying an enormous right
2481 -- operand.
2483 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2484 Folded_Val := Strval (Right_Str);
2485 else
2486 Start_String (Strval (Left_Str));
2487 end if;
2489 else
2490 Start_String;
2491 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2492 Left_Len := 1;
2493 end if;
2495 -- Now append the characters of the right operand, unless we
2496 -- optimized the "" & "..." case above.
2498 if Nkind (Right_Str) = N_String_Literal then
2499 if Left_Len /= 0 then
2500 Store_String_Chars (Strval (Right_Str));
2501 Folded_Val := End_String;
2502 end if;
2503 else
2504 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2505 Folded_Val := End_String;
2506 end if;
2508 Set_Is_Static_Expression (N, Stat);
2510 -- If left operand is the empty string, the result is the
2511 -- right operand, including its bounds if anomalous.
2513 if Left_Len = 0
2514 and then Is_Array_Type (Etype (Right))
2515 and then Etype (Right) /= Any_String
2516 then
2517 Set_Etype (N, Etype (Right));
2518 end if;
2520 Fold_Str (N, Folded_Val, Static => Stat);
2521 end;
2522 end Eval_Concatenation;
2524 ----------------------
2525 -- Eval_Entity_Name --
2526 ----------------------
2528 -- This procedure is used for identifiers and expanded names other than
2529 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2530 -- static if they denote a static constant (RM 4.9(6)) or if the name
2531 -- denotes an enumeration literal (RM 4.9(22)).
2533 procedure Eval_Entity_Name (N : Node_Id) is
2534 Def_Id : constant Entity_Id := Entity (N);
2535 Val : Node_Id;
2537 begin
2538 -- Enumeration literals are always considered to be constants
2539 -- and cannot raise Constraint_Error (RM 4.9(22)).
2541 if Ekind (Def_Id) = E_Enumeration_Literal then
2542 Set_Is_Static_Expression (N);
2543 return;
2545 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2546 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2547 -- it does not violate 10.2.1(8) here, since this is not a variable.
2549 elsif Ekind (Def_Id) = E_Constant then
2551 -- Deferred constants must always be treated as nonstatic outside the
2552 -- scope of their full view.
2554 if Present (Full_View (Def_Id))
2555 and then not In_Open_Scopes (Scope (Def_Id))
2556 then
2557 Val := Empty;
2558 else
2559 Val := Constant_Value (Def_Id);
2560 end if;
2562 if Present (Val) then
2563 Set_Is_Static_Expression
2564 (N, Is_Static_Expression (Val)
2565 and then Is_Static_Subtype (Etype (Def_Id)));
2566 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2568 if not Is_Static_Expression (N)
2569 and then not Is_Generic_Type (Etype (N))
2570 then
2571 Validate_Static_Object_Name (N);
2572 end if;
2574 -- Mark constant condition in SCOs
2576 if Generate_SCO
2577 and then Comes_From_Source (N)
2578 and then Is_Boolean_Type (Etype (Def_Id))
2579 and then Compile_Time_Known_Value (N)
2580 then
2581 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2582 end if;
2584 return;
2585 end if;
2587 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2588 -- is enabled and we have a reference to a formal parameter of mode in,
2589 -- substitute a static value for the reference, to allow folding the
2590 -- expression. This supports checking the requirement of RM 6.8(5.3/5)
2591 -- in Analyze_Expression_Function.
2593 elsif Ekind (Def_Id) = E_In_Parameter
2594 and then Checking_Potentially_Static_Expression
2595 and then Is_Static_Function (Scope (Def_Id))
2596 then
2597 Fold_Dummy (N, Etype (Def_Id));
2598 end if;
2600 -- Fall through if the name is not static
2602 Validate_Static_Object_Name (N);
2603 end Eval_Entity_Name;
2605 ------------------------
2606 -- Eval_If_Expression --
2607 ------------------------
2609 -- We can fold to a static expression if the condition and both dependent
2610 -- expressions are static. Otherwise, the only required processing is to do
2611 -- the check for non-static context for the then and else expressions.
2613 procedure Eval_If_Expression (N : Node_Id) is
2614 Condition : constant Node_Id := First (Expressions (N));
2615 Then_Expr : constant Node_Id := Next (Condition);
2616 Else_Expr : constant Node_Id := Next (Then_Expr);
2617 Result : Node_Id;
2618 Non_Result : Node_Id;
2620 Rstat : constant Boolean :=
2621 Is_Static_Expression (Condition)
2622 and then
2623 Is_Static_Expression (Then_Expr)
2624 and then
2625 Is_Static_Expression (Else_Expr);
2626 -- True if result is static
2628 begin
2629 -- If result not static, nothing to do, otherwise set static result
2631 if not Rstat then
2632 return;
2633 else
2634 Set_Is_Static_Expression (N);
2635 end if;
2637 -- If any operand is Any_Type, just propagate to result and do not try
2638 -- to fold, this prevents cascaded errors.
2640 if Etype (Condition) = Any_Type or else
2641 Etype (Then_Expr) = Any_Type or else
2642 Etype (Else_Expr) = Any_Type
2643 then
2644 Set_Etype (N, Any_Type);
2645 Set_Is_Static_Expression (N, False);
2646 return;
2647 end if;
2649 -- If condition raises Constraint_Error then we have already signaled
2650 -- an error, and we just propagate to the result and do not fold.
2652 if Raises_Constraint_Error (Condition) then
2653 Set_Raises_Constraint_Error (N);
2654 return;
2655 end if;
2657 -- Static case where we can fold. Note that we don't try to fold cases
2658 -- where the condition is known at compile time, but the result is
2659 -- non-static. This avoids possible cases of infinite recursion where
2660 -- the expander puts in a redundant test and we remove it. Instead we
2661 -- deal with these cases in the expander.
2663 -- Select result operand
2665 if Is_True (Expr_Value (Condition)) then
2666 Result := Then_Expr;
2667 Non_Result := Else_Expr;
2668 else
2669 Result := Else_Expr;
2670 Non_Result := Then_Expr;
2671 end if;
2673 -- Note that it does not matter if the non-result operand raises a
2674 -- Constraint_Error, but if the result raises Constraint_Error then we
2675 -- replace the node with a raise Constraint_Error. This will properly
2676 -- propagate Raises_Constraint_Error since this flag is set in Result.
2678 if Raises_Constraint_Error (Result) then
2679 Rewrite_In_Raise_CE (N, Result);
2680 Check_Non_Static_Context (Non_Result);
2682 -- Otherwise the result operand replaces the original node
2684 else
2685 Rewrite (N, Relocate_Node (Result));
2686 Set_Is_Static_Expression (N);
2687 end if;
2688 end Eval_If_Expression;
2690 ----------------------------
2691 -- Eval_Indexed_Component --
2692 ----------------------------
2694 -- Indexed components are never static, so we need to perform the check
2695 -- for non-static context on the index values. Then, we check if the
2696 -- value can be obtained at compile time, even though it is non-static.
2698 procedure Eval_Indexed_Component (N : Node_Id) is
2699 Expr : Node_Id;
2701 begin
2702 -- Check for non-static context on index values
2704 Expr := First (Expressions (N));
2705 while Present (Expr) loop
2706 Check_Non_Static_Context (Expr);
2707 Next (Expr);
2708 end loop;
2710 -- If the indexed component appears in an object renaming declaration
2711 -- then we do not want to try to evaluate it, since in this case we
2712 -- need the identity of the array element.
2714 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2715 return;
2717 -- Similarly if the indexed component appears as the prefix of an
2718 -- attribute we don't want to evaluate it, because at least for
2719 -- some cases of attributes we need the identify (e.g. Access, Size).
2721 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2722 return;
2723 end if;
2725 -- Note: there are other cases, such as the left side of an assignment,
2726 -- or an OUT parameter for a call, where the replacement results in the
2727 -- illegal use of a constant, But these cases are illegal in the first
2728 -- place, so the replacement, though silly, is harmless.
2730 -- Now see if this is a constant array reference
2732 if List_Length (Expressions (N)) = 1
2733 and then Is_Entity_Name (Prefix (N))
2734 and then Ekind (Entity (Prefix (N))) = E_Constant
2735 and then Present (Constant_Value (Entity (Prefix (N))))
2736 then
2737 declare
2738 Loc : constant Source_Ptr := Sloc (N);
2739 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2740 Sub : constant Node_Id := First (Expressions (N));
2742 Atyp : Entity_Id;
2743 -- Type of array
2745 Lin : Nat;
2746 -- Linear one's origin subscript value for array reference
2748 Lbd : Node_Id;
2749 -- Lower bound of the first array index
2751 Elm : Node_Id;
2752 -- Value from constant array
2754 begin
2755 Atyp := Etype (Arr);
2757 if Is_Access_Type (Atyp) then
2758 Atyp := Designated_Type (Atyp);
2759 end if;
2761 -- If we have an array type (we should have but perhaps there are
2762 -- error cases where this is not the case), then see if we can do
2763 -- a constant evaluation of the array reference.
2765 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2766 if Ekind (Atyp) = E_String_Literal_Subtype then
2767 Lbd := String_Literal_Low_Bound (Atyp);
2768 else
2769 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2770 end if;
2772 if Compile_Time_Known_Value (Sub)
2773 and then Nkind (Arr) = N_Aggregate
2774 and then Compile_Time_Known_Value (Lbd)
2775 and then Is_Discrete_Type (Component_Type (Atyp))
2776 then
2777 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2779 if List_Length (Expressions (Arr)) >= Lin then
2780 Elm := Pick (Expressions (Arr), Lin);
2782 -- If the resulting expression is compile-time-known,
2783 -- then we can rewrite the indexed component with this
2784 -- value, being sure to mark the result as non-static.
2785 -- We also reset the Sloc, in case this generates an
2786 -- error later on (e.g. 136'Access).
2788 if Compile_Time_Known_Value (Elm) then
2789 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2790 Set_Is_Static_Expression (N, False);
2791 Set_Sloc (N, Loc);
2792 end if;
2793 end if;
2795 -- We can also constant-fold if the prefix is a string literal.
2796 -- This will be useful in an instantiation or an inlining.
2798 elsif Compile_Time_Known_Value (Sub)
2799 and then Nkind (Arr) = N_String_Literal
2800 and then Compile_Time_Known_Value (Lbd)
2801 and then Expr_Value (Lbd) = 1
2802 and then Expr_Value (Sub) <=
2803 String_Literal_Length (Etype (Arr))
2804 then
2805 declare
2806 C : constant Char_Code :=
2807 Get_String_Char (Strval (Arr),
2808 UI_To_Int (Expr_Value (Sub)));
2809 begin
2810 Set_Character_Literal_Name (C);
2812 Elm :=
2813 Make_Character_Literal (Loc,
2814 Chars => Name_Find,
2815 Char_Literal_Value => UI_From_CC (C));
2816 Set_Etype (Elm, Component_Type (Atyp));
2817 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2818 Set_Is_Static_Expression (N, False);
2819 end;
2820 end if;
2821 end if;
2822 end;
2823 end if;
2824 end Eval_Indexed_Component;
2826 --------------------------
2827 -- Eval_Integer_Literal --
2828 --------------------------
2830 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2831 -- as static by the analyzer. The reason we did it that early is to allow
2832 -- the possibility of turning off the Is_Static_Expression flag after
2833 -- analysis, but before resolution, when integer literals are generated in
2834 -- the expander that do not correspond to static expressions.
2836 procedure Eval_Integer_Literal (N : Node_Id) is
2837 function In_Any_Integer_Context (K : Node_Kind) return Boolean;
2838 -- If the literal is resolved with a specific type in a context where
2839 -- the expected type is Any_Integer, there are no range checks on the
2840 -- literal. By the time the literal is evaluated, it carries the type
2841 -- imposed by the enclosing expression, and we must recover the context
2842 -- to determine that Any_Integer is meant.
2844 ----------------------------
2845 -- In_Any_Integer_Context --
2846 ----------------------------
2848 function In_Any_Integer_Context (K : Node_Kind) return Boolean is
2849 begin
2850 -- Any_Integer also appears in digits specifications for real types,
2851 -- but those have bounds smaller that those of any integer base type,
2852 -- so we can safely ignore these cases.
2854 return K in N_Attribute_Definition_Clause
2855 | N_Modular_Type_Definition
2856 | N_Number_Declaration
2857 | N_Signed_Integer_Type_Definition;
2858 end In_Any_Integer_Context;
2860 -- Local variables
2862 PK : constant Node_Kind := Nkind (Parent (N));
2863 Typ : constant Entity_Id := Etype (N);
2865 -- Start of processing for Eval_Integer_Literal
2867 begin
2868 -- If the literal appears in a non-expression context, then it is
2869 -- certainly appearing in a non-static context, so check it. This is
2870 -- actually a redundant check, since Check_Non_Static_Context would
2871 -- check it, but it seems worthwhile to optimize out the call.
2873 -- Additionally, when the literal appears within an if or case
2874 -- expression it must be checked as well. However, due to the literal
2875 -- appearing within a conditional statement, expansion greatly changes
2876 -- the nature of its context and performing some of the checks within
2877 -- Check_Non_Static_Context on an expanded literal may lead to spurious
2878 -- and misleading warnings.
2880 if (PK not in N_Subexpr
2881 or else (PK in N_Case_Expression_Alternative | N_If_Expression
2882 and then
2883 Comes_From_Source (N)))
2884 and then not In_Any_Integer_Context (PK)
2885 then
2886 Check_Non_Static_Context (N);
2887 end if;
2889 -- Modular integer literals must be in their base range
2891 if Is_Modular_Integer_Type (Typ)
2892 and then Is_Out_Of_Range (N, Base_Type (Typ), Assume_Valid => True)
2893 then
2894 Out_Of_Range (N);
2895 end if;
2896 end Eval_Integer_Literal;
2898 -------------------------
2899 -- Eval_Intrinsic_Call --
2900 -------------------------
2902 procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
2904 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind);
2905 -- Evaluate an intrinsic shift call N on the given subprogram E.
2906 -- Op is the kind for the shift node.
2908 ----------------
2909 -- Eval_Shift --
2910 ----------------
2912 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind) is
2913 Left : constant Node_Id := First_Actual (N);
2914 Right : constant Node_Id := Next_Actual (Left);
2915 Static : constant Boolean := Is_Static_Function (E);
2917 begin
2918 if Static then
2919 if Checking_Potentially_Static_Expression then
2920 Fold_Dummy (N, Etype (N));
2921 return;
2922 end if;
2923 end if;
2925 Fold_Shift
2926 (N, Left, Right, Op, Static => Static, Check_Elab => not Static);
2927 end Eval_Shift;
2929 Nam : Name_Id;
2931 begin
2932 -- Nothing to do if the intrinsic is handled by the back end.
2934 if Present (Interface_Name (E)) then
2935 return;
2936 end if;
2938 -- Intrinsic calls as part of a static function is a language extension.
2940 if Checking_Potentially_Static_Expression
2941 and then not Extensions_Allowed
2942 then
2943 return;
2944 end if;
2946 -- If we have a renaming, expand the call to the original operation,
2947 -- which must itself be intrinsic, since renaming requires matching
2948 -- conventions and this has already been checked.
2950 if Present (Alias (E)) then
2951 Eval_Intrinsic_Call (N, Alias (E));
2952 return;
2953 end if;
2955 -- If the intrinsic subprogram is generic, gets its original name
2957 if Present (Parent (E))
2958 and then Present (Generic_Parent (Parent (E)))
2959 then
2960 Nam := Chars (Generic_Parent (Parent (E)));
2961 else
2962 Nam := Chars (E);
2963 end if;
2965 case Nam is
2966 when Name_Shift_Left =>
2967 Eval_Shift (N, E, N_Op_Shift_Left);
2968 when Name_Shift_Right =>
2969 Eval_Shift (N, E, N_Op_Shift_Right);
2970 when Name_Shift_Right_Arithmetic =>
2971 Eval_Shift (N, E, N_Op_Shift_Right_Arithmetic);
2972 when others =>
2973 null;
2974 end case;
2975 end Eval_Intrinsic_Call;
2977 ---------------------
2978 -- Eval_Logical_Op --
2979 ---------------------
2981 -- Logical operations are static functions, so the result is potentially
2982 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2984 procedure Eval_Logical_Op (N : Node_Id) is
2985 Left : constant Node_Id := Left_Opnd (N);
2986 Right : constant Node_Id := Right_Opnd (N);
2987 Left_Int : Uint := No_Uint;
2988 Right_Int : Uint := No_Uint;
2989 Stat : Boolean;
2990 Fold : Boolean;
2992 begin
2993 -- If not foldable we are done
2995 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2997 if not Fold then
2998 return;
2999 end if;
3001 -- Compile time evaluation of logical operation
3003 if Is_Modular_Integer_Type (Etype (N)) then
3004 Left_Int := Expr_Value (Left);
3005 Right_Int := Expr_Value (Right);
3007 declare
3008 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
3009 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
3011 begin
3012 To_Bits (Left_Int, Left_Bits);
3013 To_Bits (Right_Int, Right_Bits);
3015 -- Note: should really be able to use array ops instead of
3016 -- these loops, but they break the build with a cryptic error
3017 -- during the bind of gnat1 likely due to a wrong computation
3018 -- of a date or checksum.
3020 if Nkind (N) = N_Op_And then
3021 for J in Left_Bits'Range loop
3022 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
3023 end loop;
3025 elsif Nkind (N) = N_Op_Or then
3026 for J in Left_Bits'Range loop
3027 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
3028 end loop;
3030 else
3031 pragma Assert (Nkind (N) = N_Op_Xor);
3033 for J in Left_Bits'Range loop
3034 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
3035 end loop;
3036 end if;
3038 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
3039 end;
3041 else
3042 pragma Assert (Is_Boolean_Type (Etype (N)));
3044 if Compile_Time_Known_Value (Left)
3045 and then Compile_Time_Known_Value (Right)
3046 then
3047 Right_Int := Expr_Value (Right);
3048 Left_Int := Expr_Value (Left);
3049 end if;
3051 if Nkind (N) = N_Op_And then
3053 -- If Left or Right are not compile time known values it means
3054 -- that the result is always False as per
3055 -- Test_Expression_Is_Foldable.
3056 -- Note that in this case, both Right_Int and Left_Int are set
3057 -- to No_Uint, so need to test for both.
3059 if No (Right_Int) then
3060 Fold_Uint (N, Uint_0, Stat);
3061 else
3062 Fold_Uint (N,
3063 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
3064 end if;
3065 elsif Nkind (N) = N_Op_Or then
3067 -- If Left or Right are not compile time known values it means
3068 -- that the result is always True. as per
3069 -- Test_Expression_Is_Foldable.
3070 -- Note that in this case, both Right_Int and Left_Int are set
3071 -- to No_Uint, so need to test for both.
3073 if No (Right_Int) then
3074 Fold_Uint (N, Uint_1, Stat);
3075 else
3076 Fold_Uint (N,
3077 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
3078 end if;
3079 else
3080 pragma Assert (Nkind (N) = N_Op_Xor);
3081 Fold_Uint (N,
3082 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
3083 end if;
3084 end if;
3085 end Eval_Logical_Op;
3087 ------------------------
3088 -- Eval_Membership_Op --
3089 ------------------------
3091 -- A membership test is potentially static if the expression is static, and
3092 -- the range is a potentially static range, or is a subtype mark denoting a
3093 -- static subtype (RM 4.9(12)).
3095 procedure Eval_Membership_Op (N : Node_Id) is
3096 Alts : constant List_Id := Alternatives (N);
3097 Choice : constant Node_Id := Right_Opnd (N);
3098 Expr : constant Node_Id := Left_Opnd (N);
3099 Result : Match_Result;
3101 begin
3102 -- Ignore if error in either operand, except to make sure that Any_Type
3103 -- is properly propagated to avoid junk cascaded errors.
3105 if Etype (Expr) = Any_Type
3106 or else (Present (Choice) and then Etype (Choice) = Any_Type)
3107 then
3108 Set_Etype (N, Any_Type);
3109 return;
3110 end if;
3112 -- If left operand non-static, then nothing to do
3114 if not Is_Static_Expression (Expr) then
3115 return;
3116 end if;
3118 -- If choice is non-static, left operand is in non-static context
3120 if (Present (Choice) and then not Is_Static_Choice (Choice))
3121 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
3122 then
3123 Check_Non_Static_Context (Expr);
3124 return;
3125 end if;
3127 -- Otherwise we definitely have a static expression
3129 Set_Is_Static_Expression (N);
3131 -- If left operand raises Constraint_Error, propagate and we are done
3133 if Raises_Constraint_Error (Expr) then
3134 Set_Raises_Constraint_Error (N, True);
3136 -- See if we match
3138 else
3139 if Present (Choice) then
3140 Result := Choice_Matches (Expr, Choice);
3141 else
3142 Result := Choices_Match (Expr, Alts);
3143 end if;
3145 -- If result is Non_Static, it means that we raise Constraint_Error,
3146 -- since we already tested that the operands were themselves static.
3148 if Result = Non_Static then
3149 Set_Raises_Constraint_Error (N);
3151 -- Otherwise we have our result (flipped if NOT IN case)
3153 else
3154 Fold_Uint
3155 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
3156 Warn_On_Known_Condition (N);
3157 end if;
3158 end if;
3159 end Eval_Membership_Op;
3161 ------------------------
3162 -- Eval_Named_Integer --
3163 ------------------------
3165 procedure Eval_Named_Integer (N : Node_Id) is
3166 begin
3167 Fold_Uint (N,
3168 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
3169 end Eval_Named_Integer;
3171 ---------------------
3172 -- Eval_Named_Real --
3173 ---------------------
3175 procedure Eval_Named_Real (N : Node_Id) is
3176 begin
3177 Fold_Ureal (N,
3178 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
3179 end Eval_Named_Real;
3181 -------------------
3182 -- Eval_Op_Expon --
3183 -------------------
3185 -- Exponentiation is a static functions, so the result is potentially
3186 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
3188 procedure Eval_Op_Expon (N : Node_Id) is
3189 Left : constant Node_Id := Left_Opnd (N);
3190 Right : constant Node_Id := Right_Opnd (N);
3191 Stat : Boolean;
3192 Fold : Boolean;
3194 begin
3195 -- If not foldable we are done
3197 Test_Expression_Is_Foldable
3198 (N, Left, Right, Stat, Fold, CRT_Safe => True);
3200 -- Return if not foldable
3202 if not Fold then
3203 return;
3204 end if;
3206 if Configurable_Run_Time_Mode and not Stat then
3207 return;
3208 end if;
3210 -- Fold exponentiation operation
3212 declare
3213 Right_Int : constant Uint := Expr_Value (Right);
3215 begin
3216 -- Integer case
3218 if Is_Integer_Type (Etype (Left)) then
3219 declare
3220 Left_Int : constant Uint := Expr_Value (Left);
3221 Result : Uint;
3223 begin
3224 -- Exponentiation of an integer raises Constraint_Error for a
3225 -- negative exponent (RM 4.5.6).
3227 if Right_Int < 0 then
3228 Apply_Compile_Time_Constraint_Error
3229 (N, "integer exponent negative", CE_Range_Check_Failed,
3230 Warn => not Stat);
3231 return;
3233 else
3234 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
3235 Result := Left_Int ** Right_Int;
3236 else
3237 Result := Left_Int;
3238 end if;
3240 if Is_Modular_Integer_Type (Etype (N)) then
3241 Result := Result mod Modulus (Etype (N));
3242 end if;
3244 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
3246 Fold_Uint (N, Result, Stat);
3247 end if;
3248 end;
3250 -- Real case
3252 else
3253 declare
3254 Left_Real : constant Ureal := Expr_Value_R (Left);
3256 begin
3257 -- Cannot have a zero base with a negative exponent
3259 if UR_Is_Zero (Left_Real) then
3261 if Right_Int < 0 then
3262 Apply_Compile_Time_Constraint_Error
3263 (N, "zero ** negative integer", CE_Range_Check_Failed,
3264 Warn => not Stat);
3265 return;
3266 else
3267 Fold_Ureal (N, Ureal_0, Stat);
3268 end if;
3270 else
3271 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
3272 end if;
3273 end;
3274 end if;
3275 end;
3276 end Eval_Op_Expon;
3278 -----------------
3279 -- Eval_Op_Not --
3280 -----------------
3282 -- The not operation is a static function, so the result is potentially
3283 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3285 procedure Eval_Op_Not (N : Node_Id) is
3286 Right : constant Node_Id := Right_Opnd (N);
3287 Stat : Boolean;
3288 Fold : Boolean;
3290 begin
3291 -- If not foldable we are done
3293 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3295 if not Fold then
3296 return;
3297 end if;
3299 -- Fold not operation
3301 declare
3302 Rint : constant Uint := Expr_Value (Right);
3303 Typ : constant Entity_Id := Etype (N);
3305 begin
3306 -- Negation is equivalent to subtracting from the modulus minus one.
3307 -- For a binary modulus this is equivalent to the ones-complement of
3308 -- the original value. For a nonbinary modulus this is an arbitrary
3309 -- but consistent definition.
3311 if Is_Modular_Integer_Type (Typ) then
3312 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3313 else pragma Assert (Is_Boolean_Type (Typ));
3314 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3315 end if;
3317 Set_Is_Static_Expression (N, Stat);
3318 end;
3319 end Eval_Op_Not;
3321 -------------------------------
3322 -- Eval_Qualified_Expression --
3323 -------------------------------
3325 -- A qualified expression is potentially static if its subtype mark denotes
3326 -- a static subtype and its expression is potentially static (RM 4.9 (10)).
3328 procedure Eval_Qualified_Expression (N : Node_Id) is
3329 Operand : constant Node_Id := Expression (N);
3330 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3332 Stat : Boolean;
3333 Fold : Boolean;
3334 Hex : Boolean;
3336 begin
3337 -- Can only fold if target is string or scalar and subtype is static.
3338 -- Also, do not fold if our parent is an allocator (this is because the
3339 -- qualified expression is really part of the syntactic structure of an
3340 -- allocator, and we do not want to end up with something that
3341 -- corresponds to "new 1" where the 1 is the result of folding a
3342 -- qualified expression).
3344 if not Is_Static_Subtype (Target_Type)
3345 or else Nkind (Parent (N)) = N_Allocator
3346 then
3347 Check_Non_Static_Context (Operand);
3349 -- If operand is known to raise Constraint_Error, set the flag on the
3350 -- expression so it does not get optimized away.
3352 if Nkind (Operand) = N_Raise_Constraint_Error then
3353 Set_Raises_Constraint_Error (N);
3354 end if;
3356 return;
3358 -- Also return if a semantic error has been posted on the node, as we
3359 -- don't want to fold in that case (for GNATprove, the node might lead
3360 -- to Constraint_Error but won't have been replaced with a raise node
3361 -- or marked as raising CE).
3363 elsif Error_Posted (N) then
3364 return;
3365 end if;
3367 -- If not foldable we are done
3369 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3371 if not Fold then
3372 return;
3374 -- Don't try fold if target type has Constraint_Error bounds
3376 elsif not Is_OK_Static_Subtype (Target_Type) then
3377 Set_Raises_Constraint_Error (N);
3378 return;
3379 end if;
3381 -- Fold the result of qualification
3383 if Is_Discrete_Type (Target_Type) then
3385 -- Save Print_In_Hex indication
3387 Hex := Nkind (Operand) = N_Integer_Literal
3388 and then Print_In_Hex (Operand);
3390 Fold_Uint (N, Expr_Value (Operand), Stat);
3392 -- Preserve Print_In_Hex indication
3394 if Hex and then Nkind (N) = N_Integer_Literal then
3395 Set_Print_In_Hex (N);
3396 end if;
3398 elsif Is_Real_Type (Target_Type) then
3399 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3401 else
3402 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3404 if not Stat then
3405 Set_Is_Static_Expression (N, False);
3406 else
3407 Check_String_Literal_Length (N, Target_Type);
3408 end if;
3410 return;
3411 end if;
3413 -- The expression may be foldable but not static
3415 Set_Is_Static_Expression (N, Stat);
3417 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3418 Out_Of_Range (N);
3419 end if;
3420 end Eval_Qualified_Expression;
3422 -----------------------
3423 -- Eval_Real_Literal --
3424 -----------------------
3426 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3427 -- as static by the analyzer. The reason we did it that early is to allow
3428 -- the possibility of turning off the Is_Static_Expression flag after
3429 -- analysis, but before resolution, when integer literals are generated
3430 -- in the expander that do not correspond to static expressions.
3432 procedure Eval_Real_Literal (N : Node_Id) is
3433 PK : constant Node_Kind := Nkind (Parent (N));
3435 begin
3436 -- If the literal appears in a non-expression context and not as part of
3437 -- a number declaration, then it is appearing in a non-static context,
3438 -- so check it.
3440 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3441 Check_Non_Static_Context (N);
3442 end if;
3443 end Eval_Real_Literal;
3445 ------------------------
3446 -- Eval_Relational_Op --
3447 ------------------------
3449 -- Relational operations are static functions, so the result is static if
3450 -- both operands are static (RM 4.9(7), 4.9(20)), except that up to Ada
3451 -- 2012, for strings the result is never static, even if the operands are.
3452 -- The string case was relaxed in Ada 2022, see AI12-0201.
3454 -- However, for internally generated nodes, we allow string equality and
3455 -- inequality to be static. This is because we rewrite A in "ABC" as an
3456 -- equality test A = "ABC", and the former is definitely static.
3458 procedure Eval_Relational_Op (N : Node_Id) is
3459 Left : constant Node_Id := Left_Opnd (N);
3460 Right : constant Node_Id := Right_Opnd (N);
3462 procedure Decompose_Expr
3463 (Expr : Node_Id;
3464 Ent : out Entity_Id;
3465 Kind : out Character;
3466 Cons : out Uint;
3467 Orig : Boolean := True);
3468 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3469 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3470 -- simple entity, and Cons is the value of K. If the expression is not
3471 -- of the required form, Ent is set to Empty.
3473 -- Orig indicates whether Expr is the original expression to consider,
3474 -- or if we are handling a subexpression (e.g. recursive call to
3475 -- Decompose_Expr).
3477 procedure Fold_General_Op (Is_Static : Boolean);
3478 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3479 -- be set when the operator denotes a static expression.
3481 procedure Fold_Static_Real_Op;
3482 -- Attempt to fold static real type relational operator N
3484 function Static_Length (Expr : Node_Id) return Uint;
3485 -- If Expr is an expression for a constrained array whose length is
3486 -- known at compile time, return the non-negative length, otherwise
3487 -- return -1.
3489 --------------------
3490 -- Decompose_Expr --
3491 --------------------
3493 procedure Decompose_Expr
3494 (Expr : Node_Id;
3495 Ent : out Entity_Id;
3496 Kind : out Character;
3497 Cons : out Uint;
3498 Orig : Boolean := True)
3500 Exp : Node_Id;
3502 begin
3503 -- Assume that the expression does not meet the expected form
3505 Cons := No_Uint;
3506 Ent := Empty;
3507 Kind := '?';
3509 if Nkind (Expr) = N_Op_Add
3510 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3511 then
3512 Exp := Left_Opnd (Expr);
3513 Cons := Expr_Value (Right_Opnd (Expr));
3515 elsif Nkind (Expr) = N_Op_Subtract
3516 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3517 then
3518 Exp := Left_Opnd (Expr);
3519 Cons := -Expr_Value (Right_Opnd (Expr));
3521 -- If the bound is a constant created to remove side effects, recover
3522 -- the original expression to see if it has one of the recognizable
3523 -- forms.
3525 elsif Nkind (Expr) = N_Identifier
3526 and then not Comes_From_Source (Entity (Expr))
3527 and then Ekind (Entity (Expr)) = E_Constant
3528 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3529 then
3530 Exp := Expression (Parent (Entity (Expr)));
3531 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3533 -- If original expression includes an entity, create a reference
3534 -- to it for use below.
3536 if Present (Ent) then
3537 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3538 else
3539 return;
3540 end if;
3542 else
3543 -- Only consider the case of X + 0 for a full expression, and
3544 -- not when recursing, otherwise we may end up with evaluating
3545 -- expressions not known at compile time to 0.
3547 if Orig then
3548 Exp := Expr;
3549 Cons := Uint_0;
3550 else
3551 return;
3552 end if;
3553 end if;
3555 -- At this stage Exp is set to the potential X
3557 if Nkind (Exp) = N_Attribute_Reference then
3558 if Attribute_Name (Exp) = Name_First then
3559 Kind := 'F';
3560 elsif Attribute_Name (Exp) = Name_Last then
3561 Kind := 'L';
3562 else
3563 return;
3564 end if;
3566 Exp := Prefix (Exp);
3568 else
3569 Kind := 'E';
3570 end if;
3572 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3573 Ent := Entity (Exp);
3574 end if;
3575 end Decompose_Expr;
3577 ---------------------
3578 -- Fold_General_Op --
3579 ---------------------
3581 procedure Fold_General_Op (Is_Static : Boolean) is
3582 CR : constant Compare_Result :=
3583 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3585 Result : Boolean;
3587 begin
3588 if CR = Unknown then
3589 return;
3590 end if;
3592 case Nkind (N) is
3593 when N_Op_Eq =>
3594 if CR = EQ then
3595 Result := True;
3596 elsif CR = NE or else CR = GT or else CR = LT then
3597 Result := False;
3598 else
3599 return;
3600 end if;
3602 when N_Op_Ge =>
3603 if CR = GT or else CR = EQ or else CR = GE then
3604 Result := True;
3605 elsif CR = LT then
3606 Result := False;
3607 else
3608 return;
3609 end if;
3611 when N_Op_Gt =>
3612 if CR = GT then
3613 Result := True;
3614 elsif CR = EQ or else CR = LT or else CR = LE then
3615 Result := False;
3616 else
3617 return;
3618 end if;
3620 when N_Op_Le =>
3621 if CR = LT or else CR = EQ or else CR = LE then
3622 Result := True;
3623 elsif CR = GT then
3624 Result := False;
3625 else
3626 return;
3627 end if;
3629 when N_Op_Lt =>
3630 if CR = LT then
3631 Result := True;
3632 elsif CR = EQ or else CR = GT or else CR = GE then
3633 Result := False;
3634 else
3635 return;
3636 end if;
3638 when N_Op_Ne =>
3639 if CR = NE or else CR = GT or else CR = LT then
3640 Result := True;
3641 elsif CR = EQ then
3642 Result := False;
3643 else
3644 return;
3645 end if;
3647 when others =>
3648 raise Program_Error;
3649 end case;
3651 -- Determine the potential outcome of the relation assuming the
3652 -- operands are valid and emit a warning when the relation yields
3653 -- True or False only in the presence of invalid values.
3655 Warn_On_Constant_Valid_Condition (N);
3657 Fold_Uint (N, Test (Result), Is_Static);
3658 end Fold_General_Op;
3660 -------------------------
3661 -- Fold_Static_Real_Op --
3662 -------------------------
3664 procedure Fold_Static_Real_Op is
3665 Left_Real : constant Ureal := Expr_Value_R (Left);
3666 Right_Real : constant Ureal := Expr_Value_R (Right);
3667 Result : Boolean;
3669 begin
3670 case Nkind (N) is
3671 when N_Op_Eq => Result := (Left_Real = Right_Real);
3672 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3673 when N_Op_Gt => Result := (Left_Real > Right_Real);
3674 when N_Op_Le => Result := (Left_Real <= Right_Real);
3675 when N_Op_Lt => Result := (Left_Real < Right_Real);
3676 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3677 when others => raise Program_Error;
3678 end case;
3680 Fold_Uint (N, Test (Result), True);
3681 end Fold_Static_Real_Op;
3683 -------------------
3684 -- Static_Length --
3685 -------------------
3687 function Static_Length (Expr : Node_Id) return Uint is
3688 Cons1 : Uint;
3689 Cons2 : Uint;
3690 Ent1 : Entity_Id;
3691 Ent2 : Entity_Id;
3692 Kind1 : Character;
3693 Kind2 : Character;
3694 Typ : Entity_Id;
3696 begin
3697 -- First easy case string literal
3699 if Nkind (Expr) = N_String_Literal then
3700 return UI_From_Int (String_Length (Strval (Expr)));
3702 -- With frontend inlining as performed in GNATprove mode, a variable
3703 -- may be inserted that has a string literal subtype. Deal with this
3704 -- specially as for the previous case.
3706 elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then
3707 return String_Literal_Length (Etype (Expr));
3709 -- Second easy case, not constrained subtype, so no length
3711 elsif not Is_Constrained (Etype (Expr)) then
3712 return Uint_Minus_1;
3713 end if;
3715 -- General case
3717 Typ := Etype (First_Index (Etype (Expr)));
3719 -- The simple case, both bounds are known at compile time
3721 if Is_Discrete_Type (Typ)
3722 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3723 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3724 then
3725 return
3726 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3727 Expr_Value (Type_Low_Bound (Typ)) + 1);
3728 end if;
3730 -- A more complex case, where the bounds are of the form X [+/- K1]
3731 -- .. X [+/- K2]), where X is an expression that is either A'First or
3732 -- A'Last (with A an entity name), or X is an entity name, and the
3733 -- two X's are the same and K1 and K2 are known at compile time, in
3734 -- this case, the length can also be computed at compile time, even
3735 -- though the bounds are not known. A common case of this is e.g.
3736 -- (X'First .. X'First+5).
3738 Decompose_Expr
3739 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3740 Decompose_Expr
3741 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3743 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3744 return Cons2 - Cons1 + 1;
3745 else
3746 return Uint_Minus_1;
3747 end if;
3748 end Static_Length;
3750 -- Local variables
3752 Left_Typ : constant Entity_Id := Etype (Left);
3753 Right_Typ : constant Entity_Id := Etype (Right);
3754 Fold : Boolean;
3755 Left_Len : Uint;
3756 Op_Typ : Entity_Id := Empty;
3757 Right_Len : Uint;
3759 Is_Static_Expression : Boolean;
3761 -- Start of processing for Eval_Relational_Op
3763 begin
3764 -- One special case to deal with first. If we can tell that the result
3765 -- will be false because the lengths of one or more index subtypes are
3766 -- compile-time known and different, then we can replace the entire
3767 -- result by False. We only do this for one-dimensional arrays, because
3768 -- the case of multidimensional arrays is rare and too much trouble. If
3769 -- one of the operands is an illegal aggregate, its type might still be
3770 -- an arbitrary composite type, so nothing to do.
3772 if Is_Array_Type (Left_Typ)
3773 and then Left_Typ /= Any_Composite
3774 and then Number_Dimensions (Left_Typ) = 1
3775 and then Nkind (N) in N_Op_Eq | N_Op_Ne
3776 then
3777 if Raises_Constraint_Error (Left)
3778 or else
3779 Raises_Constraint_Error (Right)
3780 then
3781 return;
3782 end if;
3784 -- OK, we have the case where we may be able to do this fold
3786 Left_Len := Static_Length (Left);
3787 Right_Len := Static_Length (Right);
3789 if Left_Len /= Uint_Minus_1
3790 and then Right_Len /= Uint_Minus_1
3791 and then Left_Len /= Right_Len
3792 then
3793 -- AI12-0201: comparison of string is static in Ada 2022
3795 Fold_Uint
3797 Test (Nkind (N) = N_Op_Ne),
3798 Static => Ada_Version >= Ada_2022
3799 and then Is_String_Type (Left_Typ));
3800 Warn_On_Known_Condition (N);
3801 return;
3802 end if;
3803 end if;
3805 -- General case
3807 -- Initialize the value of Is_Static_Expression. The value of Fold
3808 -- returned by Test_Expression_Is_Foldable is not needed since, even
3809 -- when some operand is a variable, we can still perform the static
3810 -- evaluation of the expression in some cases (for example, for a
3811 -- variable of a subtype of Integer we statically know that any value
3812 -- stored in such variable is smaller than Integer'Last).
3814 Test_Expression_Is_Foldable
3815 (N, Left, Right, Is_Static_Expression, Fold);
3817 -- Comparisons of scalars can give static results.
3818 -- In addition starting with Ada 2022 (AI12-0201), comparison of strings
3819 -- can also give static results, and as noted above, we also allow for
3820 -- earlier Ada versions internally generated equality and inequality for
3821 -- strings.
3822 -- The Comes_From_Source test below isn't correct and will accept
3823 -- some cases that are illegal in Ada 2012 and before. Now that Ada
3824 -- 2022 has relaxed the rules, this doesn't really matter.
3826 if Is_String_Type (Left_Typ) then
3827 if Ada_Version < Ada_2022
3828 and then (Comes_From_Source (N)
3829 or else Nkind (N) not in N_Op_Eq | N_Op_Ne)
3830 then
3831 Is_Static_Expression := False;
3832 Set_Is_Static_Expression (N, False);
3833 end if;
3835 elsif not Is_Scalar_Type (Left_Typ) then
3836 Is_Static_Expression := False;
3837 Set_Is_Static_Expression (N, False);
3838 end if;
3840 -- For operators on universal numeric types called as functions with an
3841 -- explicit scope, determine appropriate specific numeric type, and
3842 -- diagnose possible ambiguity.
3844 if Is_Universal_Numeric_Type (Left_Typ)
3845 and then
3846 Is_Universal_Numeric_Type (Right_Typ)
3847 then
3848 Op_Typ := Find_Universal_Operator_Type (N);
3849 end if;
3851 -- Attempt to fold the relational operator
3853 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3854 Fold_Static_Real_Op;
3855 else
3856 Fold_General_Op (Is_Static_Expression);
3857 end if;
3859 -- For the case of a folded relational operator on a specific numeric
3860 -- type, freeze the operand type now.
3862 if Present (Op_Typ) then
3863 Freeze_Before (N, Op_Typ);
3864 end if;
3866 Warn_On_Known_Condition (N);
3867 end Eval_Relational_Op;
3869 -----------------------------
3870 -- Eval_Selected_Component --
3871 -----------------------------
3873 procedure Eval_Selected_Component (N : Node_Id) is
3874 Node : Node_Id;
3875 Comp : Node_Id;
3876 C : Node_Id;
3877 Nam : Name_Id;
3879 begin
3880 -- If an attribute reference or a LHS, nothing to do.
3881 -- Also do not fold if N is an [in] out subprogram parameter.
3882 -- Fold will perform the other relevant tests.
3884 if Nkind (Parent (N)) /= N_Attribute_Reference
3885 and then Is_LHS (N) = No
3886 and then not Is_Actual_Out_Or_In_Out_Parameter (N)
3887 then
3888 -- Simplify a selected_component on an aggregate by extracting
3889 -- the field directly.
3891 Node := Unqualify (Prefix (N));
3893 if Nkind (Node) = N_Aggregate
3894 and then Compile_Time_Known_Aggregate (Node)
3895 then
3896 Comp := First (Component_Associations (Node));
3897 Nam := Chars (Selector_Name (N));
3899 while Present (Comp) loop
3900 C := First (Choices (Comp));
3902 while Present (C) loop
3903 if Chars (C) = Nam then
3904 Rewrite (N, Relocate_Node (Expression (Comp)));
3905 return;
3906 end if;
3908 Next (C);
3909 end loop;
3911 Next (Comp);
3912 end loop;
3913 else
3914 Fold (N);
3915 end if;
3916 end if;
3917 end Eval_Selected_Component;
3919 ----------------
3920 -- Eval_Shift --
3921 ----------------
3923 procedure Eval_Shift (N : Node_Id) is
3924 begin
3925 -- This procedure is only called for compiler generated code (e.g.
3926 -- packed arrays), so there is nothing to do except attempting to fold
3927 -- the expression.
3929 Fold_Shift (N, Left_Opnd (N), Right_Opnd (N), Nkind (N));
3930 end Eval_Shift;
3932 ------------------------
3933 -- Eval_Short_Circuit --
3934 ------------------------
3936 -- A short circuit operation is potentially static if both operands are
3937 -- potentially static (RM 4.9 (13)).
3939 procedure Eval_Short_Circuit (N : Node_Id) is
3940 Kind : constant Node_Kind := Nkind (N);
3941 Left : constant Node_Id := Left_Opnd (N);
3942 Right : constant Node_Id := Right_Opnd (N);
3943 Left_Int : Uint;
3945 Rstat : constant Boolean :=
3946 Is_Static_Expression (Left)
3947 and then
3948 Is_Static_Expression (Right);
3950 begin
3951 -- Short circuit operations are never static in Ada 83
3953 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3954 Check_Non_Static_Context (Left);
3955 Check_Non_Static_Context (Right);
3956 return;
3957 end if;
3959 -- Now look at the operands, we can't quite use the normal call to
3960 -- Test_Expression_Is_Foldable here because short circuit operations
3961 -- are a special case, they can still be foldable, even if the right
3962 -- operand raises Constraint_Error.
3964 -- If either operand is Any_Type, just propagate to result and do not
3965 -- try to fold, this prevents cascaded errors.
3967 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3968 Set_Etype (N, Any_Type);
3969 return;
3971 -- If left operand raises Constraint_Error, then replace node N with
3972 -- the raise Constraint_Error node, and we are obviously not foldable.
3973 -- Is_Static_Expression is set from the two operands in the normal way,
3974 -- and we check the right operand if it is in a non-static context.
3976 elsif Raises_Constraint_Error (Left) then
3977 if not Rstat then
3978 Check_Non_Static_Context (Right);
3979 end if;
3981 Rewrite_In_Raise_CE (N, Left);
3982 Set_Is_Static_Expression (N, Rstat);
3983 return;
3985 -- If the result is not static, then we won't in any case fold
3987 elsif not Rstat then
3988 Check_Non_Static_Context (Left);
3989 Check_Non_Static_Context (Right);
3990 return;
3991 end if;
3993 -- Here the result is static, note that, unlike the normal processing
3994 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3995 -- the right operand raises Constraint_Error, that's because it is not
3996 -- significant if the left operand is decisive.
3998 Set_Is_Static_Expression (N);
4000 -- It does not matter if the right operand raises Constraint_Error if
4001 -- it will not be evaluated. So deal specially with the cases where
4002 -- the right operand is not evaluated. Note that we will fold these
4003 -- cases even if the right operand is non-static, which is fine, but
4004 -- of course in these cases the result is not potentially static.
4006 Left_Int := Expr_Value (Left);
4008 if (Kind = N_And_Then and then Is_False (Left_Int))
4009 or else
4010 (Kind = N_Or_Else and then Is_True (Left_Int))
4011 then
4012 Fold_Uint (N, Left_Int, Rstat);
4013 return;
4014 end if;
4016 -- If first operand not decisive, then it does matter if the right
4017 -- operand raises Constraint_Error, since it will be evaluated, so
4018 -- we simply replace the node with the right operand. Note that this
4019 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
4020 -- (both are set to True in Right).
4022 if Raises_Constraint_Error (Right) then
4023 Rewrite_In_Raise_CE (N, Right);
4024 Check_Non_Static_Context (Left);
4025 return;
4026 end if;
4028 -- Otherwise the result depends on the right operand
4030 Fold_Uint (N, Expr_Value (Right), Rstat);
4031 return;
4032 end Eval_Short_Circuit;
4034 ----------------
4035 -- Eval_Slice --
4036 ----------------
4038 -- Slices can never be static, so the only processing required is to check
4039 -- for non-static context if an explicit range is given.
4041 procedure Eval_Slice (N : Node_Id) is
4042 Drange : constant Node_Id := Discrete_Range (N);
4043 Name : constant Node_Id := Prefix (N);
4045 begin
4046 if Nkind (Drange) = N_Range then
4047 Check_Non_Static_Context (Low_Bound (Drange));
4048 Check_Non_Static_Context (High_Bound (Drange));
4049 end if;
4051 -- A slice of the form A (subtype), when the subtype is the index of
4052 -- the type of A, is redundant, the slice can be replaced with A, and
4053 -- this is worth a warning.
4055 if Is_Entity_Name (Name) then
4056 declare
4057 E : constant Entity_Id := Entity (Name);
4058 T : constant Entity_Id := Etype (E);
4060 begin
4061 if Is_Object (E)
4062 and then Is_Array_Type (T)
4063 and then Is_Entity_Name (Drange)
4064 then
4065 if Is_Entity_Name (Original_Node (First_Index (T)))
4066 and then Entity (Original_Node (First_Index (T)))
4067 = Entity (Drange)
4068 then
4069 if Warn_On_Redundant_Constructs then
4070 Error_Msg_N ("redundant slice denotes whole array?r?", N);
4071 end if;
4073 -- The following might be a useful optimization???
4075 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
4076 end if;
4077 end if;
4078 end;
4079 end if;
4080 end Eval_Slice;
4082 -------------------------
4083 -- Eval_String_Literal --
4084 -------------------------
4086 procedure Eval_String_Literal (N : Node_Id) is
4087 Typ : constant Entity_Id := Etype (N);
4088 Bas : constant Entity_Id := Base_Type (Typ);
4089 Xtp : Entity_Id;
4090 Len : Nat;
4091 Lo : Node_Id;
4093 begin
4094 -- Nothing to do if error type (handles cases like default expressions
4095 -- or generics where we have not yet fully resolved the type).
4097 if Bas = Any_Type or else Bas = Any_String then
4098 return;
4099 end if;
4101 -- String literals are static if the subtype is static (RM 4.9(2)), so
4102 -- reset the static expression flag (it was set unconditionally in
4103 -- Analyze_String_Literal) if the subtype is non-static. We tell if
4104 -- the subtype is static by looking at the lower bound.
4106 if Ekind (Typ) = E_String_Literal_Subtype then
4107 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
4108 Set_Is_Static_Expression (N, False);
4109 return;
4110 end if;
4112 -- Here if Etype of string literal is normal Etype (not yet possible,
4113 -- but may be possible in future).
4115 elsif not Is_OK_Static_Expression
4116 (Type_Low_Bound (Etype (First_Index (Typ))))
4117 then
4118 Set_Is_Static_Expression (N, False);
4119 return;
4120 end if;
4122 -- If original node was a type conversion, then result if non-static
4123 -- up to Ada 2012. AI12-0201 changes that with Ada 2022.
4125 if Nkind (Original_Node (N)) = N_Type_Conversion
4126 and then Ada_Version <= Ada_2012
4127 then
4128 Set_Is_Static_Expression (N, False);
4129 return;
4130 end if;
4132 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
4133 -- if its bounds are outside the index base type and this index type is
4134 -- static. This can happen in only two ways. Either the string literal
4135 -- is too long, or it is null, and the lower bound is type'First. Either
4136 -- way it is the upper bound that is out of range of the index type.
4138 if Ada_Version >= Ada_95 then
4139 if Is_Standard_String_Type (Bas) then
4140 Xtp := Standard_Positive;
4141 else
4142 Xtp := Etype (First_Index (Bas));
4143 end if;
4145 if Ekind (Typ) = E_String_Literal_Subtype then
4146 Lo := String_Literal_Low_Bound (Typ);
4147 else
4148 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
4149 end if;
4151 -- Check for string too long
4153 Len := String_Length (Strval (N));
4155 if Len > String_Type_Len (Bas) then
4157 -- Issue message. Note that this message is a warning if the
4158 -- string literal is not marked as static (happens in some cases
4159 -- of folding strings known at compile time, but not static).
4160 -- Furthermore in such cases, we reword the message, since there
4161 -- is no string literal in the source program.
4163 if Is_Static_Expression (N) then
4164 Apply_Compile_Time_Constraint_Error
4165 (N, "string literal too long for}", CE_Length_Check_Failed,
4166 Ent => Bas,
4167 Typ => First_Subtype (Bas));
4168 else
4169 Apply_Compile_Time_Constraint_Error
4170 (N, "string value too long for}", CE_Length_Check_Failed,
4171 Ent => Bas,
4172 Typ => First_Subtype (Bas),
4173 Warn => True);
4174 end if;
4176 -- Test for null string not allowed
4178 elsif Len = 0
4179 and then not Is_Generic_Type (Xtp)
4180 and then
4181 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
4182 then
4183 -- Same specialization of message
4185 if Is_Static_Expression (N) then
4186 Apply_Compile_Time_Constraint_Error
4187 (N, "null string literal not allowed for}",
4188 CE_Length_Check_Failed,
4189 Ent => Bas,
4190 Typ => First_Subtype (Bas));
4191 else
4192 Apply_Compile_Time_Constraint_Error
4193 (N, "null string value not allowed for}",
4194 CE_Length_Check_Failed,
4195 Ent => Bas,
4196 Typ => First_Subtype (Bas),
4197 Warn => True);
4198 end if;
4199 end if;
4200 end if;
4201 end Eval_String_Literal;
4203 --------------------------
4204 -- Eval_Type_Conversion --
4205 --------------------------
4207 -- A type conversion is potentially static if its subtype mark is for a
4208 -- static scalar subtype, and its operand expression is potentially static
4209 -- (RM 4.9(10)).
4210 -- Also add support for static string types.
4212 procedure Eval_Type_Conversion (N : Node_Id) is
4213 Operand : constant Node_Id := Expression (N);
4214 Source_Type : constant Entity_Id := Etype (Operand);
4215 Target_Type : constant Entity_Id := Etype (N);
4217 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
4218 -- Returns true if type T is an integer type, or if it is a fixed-point
4219 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
4220 -- on the conversion node).
4222 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
4223 -- Returns true if type T is a floating-point type, or if it is a
4224 -- fixed-point type that is not to be treated as an integer (i.e. the
4225 -- flag Conversion_OK is not set on the conversion node).
4227 ------------------------------
4228 -- To_Be_Treated_As_Integer --
4229 ------------------------------
4231 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
4232 begin
4233 return
4234 Is_Integer_Type (T)
4235 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
4236 end To_Be_Treated_As_Integer;
4238 ---------------------------
4239 -- To_Be_Treated_As_Real --
4240 ---------------------------
4242 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
4243 begin
4244 return
4245 Is_Floating_Point_Type (T)
4246 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
4247 end To_Be_Treated_As_Real;
4249 -- Local variables
4251 Fold : Boolean;
4252 Stat : Boolean;
4254 -- Start of processing for Eval_Type_Conversion
4256 begin
4257 -- Cannot fold if target type is non-static or if semantic error
4259 if not Is_Static_Subtype (Target_Type) then
4260 Check_Non_Static_Context (Operand);
4261 return;
4262 elsif Error_Posted (N) then
4263 return;
4264 end if;
4266 -- If not foldable we are done
4268 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
4270 if not Fold then
4271 return;
4273 -- Don't try fold if target type has Constraint_Error bounds
4275 elsif not Is_OK_Static_Subtype (Target_Type) then
4276 Set_Raises_Constraint_Error (N);
4277 return;
4278 end if;
4280 -- Remaining processing depends on operand types. Note that in the
4281 -- following type test, fixed-point counts as real unless the flag
4282 -- Conversion_OK is set, in which case it counts as integer.
4284 -- Fold conversion, case of string type. The result is static starting
4285 -- with Ada 2022 (AI12-0201).
4287 if Is_String_Type (Target_Type) then
4288 Fold_Str
4290 Strval (Get_String_Val (Operand)),
4291 Static => Ada_Version >= Ada_2022);
4292 return;
4294 -- Fold conversion, case of integer target type
4296 elsif To_Be_Treated_As_Integer (Target_Type) then
4297 declare
4298 Result : Uint;
4300 begin
4301 -- Integer to integer conversion
4303 if To_Be_Treated_As_Integer (Source_Type) then
4304 Result := Expr_Value (Operand);
4306 -- Real to integer conversion
4308 elsif To_Be_Treated_As_Real (Source_Type) then
4309 Result := UR_To_Uint (Expr_Value_R (Operand));
4311 -- Enumeration to integer conversion, aka 'Enum_Rep
4313 else
4314 Result := Expr_Rep_Value (Operand);
4315 end if;
4317 -- If fixed-point type (Conversion_OK must be set), then the
4318 -- result is logically an integer, but we must replace the
4319 -- conversion with the corresponding real literal, since the
4320 -- type from a semantic point of view is still fixed-point.
4322 if Is_Fixed_Point_Type (Target_Type) then
4323 Fold_Ureal
4324 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
4326 -- Otherwise result is integer literal
4328 else
4329 Fold_Uint (N, Result, Stat);
4330 end if;
4331 end;
4333 -- Fold conversion, case of real target type
4335 elsif To_Be_Treated_As_Real (Target_Type) then
4336 declare
4337 Result : Ureal;
4339 begin
4340 if To_Be_Treated_As_Real (Source_Type) then
4341 Result := Expr_Value_R (Operand);
4342 else
4343 Result := UR_From_Uint (Expr_Value (Operand));
4344 end if;
4346 Fold_Ureal (N, Result, Stat);
4347 end;
4349 -- Enumeration types
4351 else
4352 Fold_Uint (N, Expr_Value (Operand), Stat);
4353 end if;
4355 -- If the target is a static floating-point subtype, then its bounds
4356 -- are machine numbers so we must consider the machine-rounded value.
4358 if Is_Floating_Point_Type (Target_Type)
4359 and then Nkind (N) = N_Real_Literal
4360 and then not Is_Machine_Number (N)
4361 then
4362 declare
4363 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
4364 Hi : constant Node_Id := Type_High_Bound (Target_Type);
4365 Valr : constant Ureal :=
4366 Machine_Number (Target_Type, Expr_Value_R (N), N);
4367 begin
4368 if Valr < Expr_Value_R (Lo) or else Valr > Expr_Value_R (Hi) then
4369 Out_Of_Range (N);
4370 end if;
4371 end;
4373 elsif Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
4374 Out_Of_Range (N);
4375 end if;
4376 end Eval_Type_Conversion;
4378 -------------------
4379 -- Eval_Unary_Op --
4380 -------------------
4382 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4383 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4385 procedure Eval_Unary_Op (N : Node_Id) is
4386 Right : constant Node_Id := Right_Opnd (N);
4387 Otype : Entity_Id := Empty;
4388 Stat : Boolean;
4389 Fold : Boolean;
4391 begin
4392 -- If not foldable we are done
4394 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
4396 if not Fold then
4397 return;
4398 end if;
4400 if Is_Universal_Numeric_Type (Etype (Right)) then
4401 Otype := Find_Universal_Operator_Type (N);
4402 end if;
4404 -- Fold for integer case
4406 if Is_Integer_Type (Etype (N)) then
4407 declare
4408 Rint : constant Uint := Expr_Value (Right);
4409 Result : Uint;
4411 begin
4412 -- In the case of modular unary plus and abs there is no need
4413 -- to adjust the result of the operation since if the original
4414 -- operand was in bounds the result will be in the bounds of the
4415 -- modular type. However, in the case of modular unary minus the
4416 -- result may go out of the bounds of the modular type and needs
4417 -- adjustment.
4419 if Nkind (N) = N_Op_Plus then
4420 Result := Rint;
4422 elsif Nkind (N) = N_Op_Minus then
4423 if Is_Modular_Integer_Type (Etype (N)) then
4424 Result := (-Rint) mod Modulus (Etype (N));
4425 else
4426 Result := (-Rint);
4427 end if;
4429 else
4430 pragma Assert (Nkind (N) = N_Op_Abs);
4431 Result := abs Rint;
4432 end if;
4434 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
4436 Fold_Uint (N, Result, Stat);
4437 end;
4439 -- Fold for real case
4441 elsif Is_Real_Type (Etype (N)) then
4442 declare
4443 Rreal : constant Ureal := Expr_Value_R (Right);
4444 Result : Ureal;
4446 begin
4447 if Nkind (N) = N_Op_Plus then
4448 Result := Rreal;
4449 elsif Nkind (N) = N_Op_Minus then
4450 Result := UR_Negate (Rreal);
4451 else
4452 pragma Assert (Nkind (N) = N_Op_Abs);
4453 Result := abs Rreal;
4454 end if;
4456 Fold_Ureal (N, Result, Stat);
4457 end;
4458 end if;
4460 -- If the operator was resolved to a specific type, make sure that type
4461 -- is frozen even if the expression is folded into a literal (which has
4462 -- a universal type).
4464 if Present (Otype) then
4465 Freeze_Before (N, Otype);
4466 end if;
4467 end Eval_Unary_Op;
4469 -------------------------------
4470 -- Eval_Unchecked_Conversion --
4471 -------------------------------
4473 -- Unchecked conversions can never be static, so the only required
4474 -- processing is to check for a non-static context for the operand.
4476 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4477 Target_Type : constant Entity_Id := Etype (N);
4478 Operand : constant Node_Id := Expression (N);
4479 Operand_Type : constant Entity_Id := Etype (Operand);
4481 begin
4482 Check_Non_Static_Context (Operand);
4484 -- If we have a conversion of a compile time known value to a target
4485 -- type and the value is in range of the target type, then we can simply
4486 -- replace the construct by an integer literal of the correct type. We
4487 -- only apply this to discrete types being converted. Possibly it may
4488 -- apply in other cases, but it is too much trouble to worry about.
4490 -- Note that we do not do this transformation if the Kill_Range_Check
4491 -- flag is set, since then the value may be outside the expected range.
4492 -- This happens in the Normalize_Scalars case.
4494 -- We also skip this if either the target or operand type is biased
4495 -- because in this case, the unchecked conversion is supposed to
4496 -- preserve the bit pattern, not the integer value.
4498 if Is_Integer_Type (Target_Type)
4499 and then not Has_Biased_Representation (Target_Type)
4500 and then Is_Discrete_Type (Operand_Type)
4501 and then not Has_Biased_Representation (Operand_Type)
4502 and then Compile_Time_Known_Value (Operand)
4503 and then not Kill_Range_Check (N)
4504 then
4505 declare
4506 Val : constant Uint := Expr_Rep_Value (Operand);
4508 begin
4509 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
4510 and then
4511 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
4512 and then
4513 Val >= Expr_Value (Type_Low_Bound (Target_Type))
4514 and then
4515 Val <= Expr_Value (Type_High_Bound (Target_Type))
4516 then
4517 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
4519 -- If Address is the target type, just set the type to avoid a
4520 -- spurious type error on the literal when Address is a visible
4521 -- integer type.
4523 if Is_Descendant_Of_Address (Target_Type) then
4524 Set_Etype (N, Target_Type);
4525 else
4526 Analyze_And_Resolve (N, Target_Type);
4527 end if;
4529 return;
4530 end if;
4531 end;
4532 end if;
4533 end Eval_Unchecked_Conversion;
4535 --------------------
4536 -- Expr_Rep_Value --
4537 --------------------
4539 function Expr_Rep_Value (N : Node_Id) return Uint is
4540 Kind : constant Node_Kind := Nkind (N);
4541 Ent : Entity_Id;
4543 begin
4544 if Is_Entity_Name (N) then
4545 Ent := Entity (N);
4547 -- An enumeration literal that was either in the source or created
4548 -- as a result of static evaluation.
4550 if Ekind (Ent) = E_Enumeration_Literal then
4551 return Enumeration_Rep (Ent);
4553 -- A user defined static constant
4555 else
4556 pragma Assert (Ekind (Ent) = E_Constant);
4557 return Expr_Rep_Value (Constant_Value (Ent));
4558 end if;
4560 -- An integer literal that was either in the source or created as a
4561 -- result of static evaluation.
4563 elsif Kind = N_Integer_Literal then
4564 return Intval (N);
4566 -- A real literal for a fixed-point type. This must be the fixed-point
4567 -- case, either the literal is of a fixed-point type, or it is a bound
4568 -- of a fixed-point type, with type universal real. In either case we
4569 -- obtain the desired value from Corresponding_Integer_Value.
4571 elsif Kind = N_Real_Literal then
4572 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4573 return Corresponding_Integer_Value (N);
4575 -- The NULL access value
4577 elsif Kind = N_Null then
4578 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4579 or else Error_Posted (N));
4580 return Uint_0;
4582 -- Character literal
4584 elsif Kind = N_Character_Literal then
4585 Ent := Entity (N);
4587 -- Since Character literals of type Standard.Character don't have any
4588 -- defining character literals built for them, they do not have their
4589 -- Entity set, so just use their Char code. Otherwise for user-
4590 -- defined character literals use their Pos value as usual which is
4591 -- the same as the Rep value.
4593 if No (Ent) then
4594 return Char_Literal_Value (N);
4595 else
4596 return Enumeration_Rep (Ent);
4597 end if;
4599 -- Unchecked conversion, which can come from System'To_Address (X)
4600 -- where X is a static integer expression. Recursively evaluate X.
4602 elsif Kind = N_Unchecked_Type_Conversion then
4603 return Expr_Rep_Value (Expression (N));
4605 -- Static discriminant value
4607 elsif Is_Static_Discriminant_Component (N) then
4608 return Expr_Rep_Value
4609 (Get_Discriminant_Value
4610 (Entity (Selector_Name (N)),
4611 Etype (Prefix (N)),
4612 Discriminant_Constraint (Etype (Prefix (N)))));
4614 else
4615 raise Program_Error;
4616 end if;
4617 end Expr_Rep_Value;
4619 ----------------
4620 -- Expr_Value --
4621 ----------------
4623 function Expr_Value (N : Node_Id) return Uint is
4624 Kind : constant Node_Kind := Nkind (N);
4625 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4626 Ent : Entity_Id;
4627 Val : Uint;
4629 begin
4630 -- If already in cache, then we know it's compile-time-known and we can
4631 -- return the value that was previously stored in the cache since
4632 -- compile-time-known values cannot change.
4634 if CV_Ent.N = N then
4635 return CV_Ent.V;
4636 end if;
4638 -- Otherwise proceed to test value
4640 if Is_Entity_Name (N) then
4641 Ent := Entity (N);
4643 -- An enumeration literal that was either in the source or created as
4644 -- a result of static evaluation.
4646 if Ekind (Ent) = E_Enumeration_Literal then
4647 Val := Enumeration_Pos (Ent);
4649 -- A user defined static constant
4651 else
4652 pragma Assert (Ekind (Ent) = E_Constant);
4653 Val := Expr_Value (Constant_Value (Ent));
4654 end if;
4656 -- An integer literal that was either in the source or created as a
4657 -- result of static evaluation.
4659 elsif Kind = N_Integer_Literal then
4660 Val := Intval (N);
4662 -- A real literal for a fixed-point type. This must be the fixed-point
4663 -- case, either the literal is of a fixed-point type, or it is a bound
4664 -- of a fixed-point type, with type universal real. In either case we
4665 -- obtain the desired value from Corresponding_Integer_Value.
4667 elsif Kind = N_Real_Literal then
4668 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4669 Val := Corresponding_Integer_Value (N);
4671 -- The NULL access value
4673 elsif Kind = N_Null then
4674 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4675 or else Error_Posted (N));
4676 Val := Uint_0;
4678 -- Character literal
4680 elsif Kind = N_Character_Literal then
4681 Ent := Entity (N);
4683 -- Since Character literals of type Standard.Character don't
4684 -- have any defining character literals built for them, they
4685 -- do not have their Entity set, so just use their Char
4686 -- code. Otherwise for user-defined character literals use
4687 -- their Pos value as usual.
4689 if No (Ent) then
4690 Val := Char_Literal_Value (N);
4691 else
4692 Val := Enumeration_Pos (Ent);
4693 end if;
4695 -- Unchecked conversion, which can come from System'To_Address (X)
4696 -- where X is a static integer expression. Recursively evaluate X.
4698 elsif Kind = N_Unchecked_Type_Conversion then
4699 Val := Expr_Value (Expression (N));
4701 -- Static discriminant value
4703 elsif Is_Static_Discriminant_Component (N) then
4704 Val := Expr_Value
4705 (Get_Discriminant_Value
4706 (Entity (Selector_Name (N)),
4707 Etype (Prefix (N)),
4708 Discriminant_Constraint (Etype (Prefix (N)))));
4710 else
4711 raise Program_Error;
4712 end if;
4714 -- Come here with Val set to value to be returned, set cache
4716 CV_Ent.N := N;
4717 CV_Ent.V := Val;
4718 return Val;
4719 end Expr_Value;
4721 ------------------
4722 -- Expr_Value_E --
4723 ------------------
4725 function Expr_Value_E (N : Node_Id) return Entity_Id is
4726 Ent : constant Entity_Id := Entity (N);
4727 begin
4728 if Ekind (Ent) = E_Enumeration_Literal then
4729 return Ent;
4730 else
4731 pragma Assert (Ekind (Ent) = E_Constant);
4733 -- We may be dealing with a enumerated character type constant, so
4734 -- handle that case here.
4736 if Nkind (Constant_Value (Ent)) = N_Character_Literal then
4737 return Ent;
4738 else
4739 return Expr_Value_E (Constant_Value (Ent));
4740 end if;
4741 end if;
4742 end Expr_Value_E;
4744 ------------------
4745 -- Expr_Value_R --
4746 ------------------
4748 function Expr_Value_R (N : Node_Id) return Ureal is
4749 Kind : constant Node_Kind := Nkind (N);
4750 Ent : Entity_Id;
4752 begin
4753 if Kind = N_Real_Literal then
4754 return Realval (N);
4756 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4757 Ent := Entity (N);
4758 pragma Assert (Ekind (Ent) = E_Constant);
4759 return Expr_Value_R (Constant_Value (Ent));
4761 elsif Kind = N_Integer_Literal then
4762 return UR_From_Uint (Expr_Value (N));
4764 -- Here, we have a node that cannot be interpreted as a compile time
4765 -- constant. That is definitely an error.
4767 else
4768 raise Program_Error;
4769 end if;
4770 end Expr_Value_R;
4772 ------------------
4773 -- Expr_Value_S --
4774 ------------------
4776 function Expr_Value_S (N : Node_Id) return Node_Id is
4777 begin
4778 if Nkind (N) = N_String_Literal then
4779 return N;
4780 else
4781 pragma Assert (Ekind (Entity (N)) = E_Constant);
4782 return Expr_Value_S (Constant_Value (Entity (N)));
4783 end if;
4784 end Expr_Value_S;
4786 ----------------------------------
4787 -- Find_Universal_Operator_Type --
4788 ----------------------------------
4790 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4791 PN : constant Node_Id := Parent (N);
4792 Call : constant Node_Id := Original_Node (N);
4793 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4795 Is_Fix : constant Boolean :=
4796 Nkind (N) in N_Binary_Op
4797 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4798 -- A mixed-mode operation in this context indicates the presence of
4799 -- fixed-point type in the designated package.
4801 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4802 -- Case where N is a relational (or membership) operator (else it is an
4803 -- arithmetic one).
4805 In_Membership : constant Boolean :=
4806 Nkind (PN) in N_Membership_Test
4807 and then
4808 Nkind (Right_Opnd (PN)) = N_Range
4809 and then
4810 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4811 and then
4812 Is_Universal_Numeric_Type
4813 (Etype (Low_Bound (Right_Opnd (PN))))
4814 and then
4815 Is_Universal_Numeric_Type
4816 (Etype (High_Bound (Right_Opnd (PN))));
4817 -- Case where N is part of a membership test with a universal range
4819 E : Entity_Id;
4820 Pack : Entity_Id;
4821 Typ1 : Entity_Id := Empty;
4822 Priv_E : Entity_Id;
4824 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4825 -- Check whether one operand is a mixed-mode operation that requires the
4826 -- presence of a fixed-point type. Given that all operands are universal
4827 -- and have been constant-folded, retrieve the original function call.
4829 ---------------------------
4830 -- Is_Mixed_Mode_Operand --
4831 ---------------------------
4833 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4834 Onod : constant Node_Id := Original_Node (Op);
4835 begin
4836 return Nkind (Onod) = N_Function_Call
4837 and then Present (Next_Actual (First_Actual (Onod)))
4838 and then Etype (First_Actual (Onod)) /=
4839 Etype (Next_Actual (First_Actual (Onod)));
4840 end Is_Mixed_Mode_Operand;
4842 -- Start of processing for Find_Universal_Operator_Type
4844 begin
4845 if Nkind (Call) /= N_Function_Call
4846 or else Nkind (Name (Call)) /= N_Expanded_Name
4847 then
4848 return Empty;
4850 -- There are several cases where the context does not imply the type of
4851 -- the operands:
4852 -- - the universal expression appears in a type conversion;
4853 -- - the expression is a relational operator applied to universal
4854 -- operands;
4855 -- - the expression is a membership test with a universal operand
4856 -- and a range with universal bounds.
4858 elsif Nkind (Parent (N)) = N_Type_Conversion
4859 or else Is_Relational
4860 or else In_Membership
4861 then
4862 Pack := Entity (Prefix (Name (Call)));
4864 -- If the prefix is a package declared elsewhere, iterate over its
4865 -- visible entities, otherwise iterate over all declarations in the
4866 -- designated scope.
4868 if Ekind (Pack) = E_Package
4869 and then not In_Open_Scopes (Pack)
4870 then
4871 Priv_E := First_Private_Entity (Pack);
4872 else
4873 Priv_E := Empty;
4874 end if;
4876 Typ1 := Empty;
4877 E := First_Entity (Pack);
4878 while Present (E) and then E /= Priv_E loop
4879 if Is_Numeric_Type (E)
4880 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4881 and then Comes_From_Source (E)
4882 and then Is_Integer_Type (E) = Is_Int
4883 and then (Nkind (N) in N_Unary_Op
4884 or else Is_Relational
4885 or else Is_Fixed_Point_Type (E) = Is_Fix)
4886 then
4887 if No (Typ1) then
4888 Typ1 := E;
4890 -- Before emitting an error, check for the presence of a
4891 -- mixed-mode operation that specifies a fixed point type.
4893 elsif Is_Relational
4894 and then
4895 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4896 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4897 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4899 then
4900 if Is_Fixed_Point_Type (E) then
4901 Typ1 := E;
4902 end if;
4904 else
4905 -- More than one type of the proper class declared in P
4907 Error_Msg_N ("ambiguous operation", N);
4908 Error_Msg_Sloc := Sloc (Typ1);
4909 Error_Msg_N ("\possible interpretation (inherited)#", N);
4910 Error_Msg_Sloc := Sloc (E);
4911 Error_Msg_N ("\possible interpretation (inherited)#", N);
4912 return Empty;
4913 end if;
4914 end if;
4916 Next_Entity (E);
4917 end loop;
4918 end if;
4920 return Typ1;
4921 end Find_Universal_Operator_Type;
4923 --------------------------
4924 -- Flag_Non_Static_Expr --
4925 --------------------------
4927 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4928 begin
4929 if Error_Posted (Expr) and then not All_Errors_Mode then
4930 return;
4931 else
4932 Error_Msg_F (Msg, Expr);
4933 Why_Not_Static (Expr);
4934 end if;
4935 end Flag_Non_Static_Expr;
4937 ----------
4938 -- Fold --
4939 ----------
4941 procedure Fold (N : Node_Id) is
4942 Typ : constant Entity_Id := Etype (N);
4943 begin
4944 -- If not known at compile time or if already a literal, nothing to do
4946 if Nkind (N) in N_Numeric_Or_String_Literal
4947 or else not Compile_Time_Known_Value (N)
4948 then
4949 null;
4951 elsif Is_Discrete_Type (Typ) then
4952 Fold_Uint (N, Expr_Value (N), Static => Is_Static_Expression (N));
4954 elsif Is_Real_Type (Typ) then
4955 Fold_Ureal (N, Expr_Value_R (N), Static => Is_Static_Expression (N));
4957 elsif Is_String_Type (Typ) then
4958 Fold_Str
4959 (N, Strval (Expr_Value_S (N)), Static => Is_Static_Expression (N));
4960 end if;
4961 end Fold;
4963 ----------------
4964 -- Fold_Dummy --
4965 ----------------
4967 procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id) is
4968 begin
4969 if Is_Integer_Type (Typ) then
4970 Fold_Uint (N, Uint_1, Static => True);
4972 elsif Is_Real_Type (Typ) then
4973 Fold_Ureal (N, Ureal_1, Static => True);
4975 elsif Is_Enumeration_Type (Typ) then
4976 Fold_Uint
4978 Expr_Value (Type_Low_Bound (Base_Type (Typ))),
4979 Static => True);
4981 elsif Is_String_Type (Typ) then
4982 Fold_Str
4984 Strval (Make_String_Literal (Sloc (N), "")),
4985 Static => True);
4986 end if;
4987 end Fold_Dummy;
4989 ----------------
4990 -- Fold_Shift --
4991 ----------------
4993 procedure Fold_Shift
4994 (N : Node_Id;
4995 Left : Node_Id;
4996 Right : Node_Id;
4997 Op : Node_Kind;
4998 Static : Boolean := False;
4999 Check_Elab : Boolean := False)
5001 Typ : constant Entity_Id := Base_Type (Etype (Left));
5003 procedure Check_Elab_Call;
5004 -- Add checks related to calls in elaboration code
5006 ---------------------
5007 -- Check_Elab_Call --
5008 ---------------------
5010 procedure Check_Elab_Call is
5011 begin
5012 if Check_Elab then
5013 if Legacy_Elaboration_Checks then
5014 Check_Elab_Call (N);
5015 end if;
5017 Build_Call_Marker (N);
5018 end if;
5019 end Check_Elab_Call;
5021 Modulus, Val : Uint;
5023 begin
5024 if Compile_Time_Known_Value (Left)
5025 and then Compile_Time_Known_Value (Right)
5026 then
5027 pragma Assert (not Non_Binary_Modulus (Typ));
5029 if Op = N_Op_Shift_Left then
5030 Check_Elab_Call;
5032 if Is_Modular_Integer_Type (Typ) then
5033 Modulus := Einfo.Entities.Modulus (Typ);
5034 else
5035 Modulus := Uint_2 ** RM_Size (Typ);
5036 end if;
5038 -- Fold Shift_Left (X, Y) by computing
5039 -- (X * 2**Y) rem modulus [- Modulus]
5041 Val := (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right)))
5042 rem Modulus;
5044 if Is_Modular_Integer_Type (Typ)
5045 or else Val < Modulus / Uint_2
5046 then
5047 Fold_Uint (N, Val, Static => Static);
5048 else
5049 Fold_Uint (N, Val - Modulus, Static => Static);
5050 end if;
5052 elsif Op = N_Op_Shift_Right then
5053 Check_Elab_Call;
5055 -- X >> 0 is a no-op
5057 if Expr_Value (Right) = Uint_0 then
5058 Fold_Uint (N, Expr_Value (Left), Static => Static);
5059 else
5060 if Is_Modular_Integer_Type (Typ) then
5061 Modulus := Einfo.Entities.Modulus (Typ);
5062 else
5063 Modulus := Uint_2 ** RM_Size (Typ);
5064 end if;
5066 -- Fold X >> Y by computing (X [+ Modulus]) / 2**Y
5067 -- Note that after a Shift_Right operation (with Y > 0), the
5068 -- result is always positive, even if the original operand was
5069 -- negative.
5071 declare
5072 M : Unat;
5073 begin
5074 if Expr_Value (Left) >= Uint_0 then
5075 M := Uint_0;
5076 else
5077 M := Modulus;
5078 end if;
5080 Fold_Uint
5082 (Expr_Value (Left) + M) / (Uint_2 ** Expr_Value (Right)),
5083 Static => Static);
5084 end;
5085 end if;
5086 elsif Op = N_Op_Shift_Right_Arithmetic then
5087 Check_Elab_Call;
5089 declare
5090 Two_Y : constant Uint := Uint_2 ** Expr_Value (Right);
5091 begin
5092 if Is_Modular_Integer_Type (Typ) then
5093 Modulus := Einfo.Entities.Modulus (Typ);
5094 else
5095 Modulus := Uint_2 ** RM_Size (Typ);
5096 end if;
5098 -- X / 2**Y if X if positive or a small enough modular integer
5100 if (Is_Modular_Integer_Type (Typ)
5101 and then Expr_Value (Left) < Modulus / Uint_2)
5102 or else
5103 (not Is_Modular_Integer_Type (Typ)
5104 and then Expr_Value (Left) >= 0)
5105 then
5106 Fold_Uint (N, Expr_Value (Left) / Two_Y, Static => Static);
5108 -- -1 (aka all 1's) if Y is larger than the number of bits
5109 -- available or if X = -1.
5111 elsif Two_Y > Modulus
5112 or else Expr_Value (Left) = Uint_Minus_1
5113 then
5114 if Is_Modular_Integer_Type (Typ) then
5115 Fold_Uint (N, Modulus - Uint_1, Static => Static);
5116 else
5117 Fold_Uint (N, Uint_Minus_1, Static => Static);
5118 end if;
5120 -- Large modular integer, compute via multiply/divide the
5121 -- following: X >> Y + (1 << Y - 1) << (RM_Size - Y)
5123 elsif Is_Modular_Integer_Type (Typ) then
5124 Fold_Uint
5126 (Expr_Value (Left)) / Two_Y
5127 + (Two_Y - Uint_1)
5128 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right)),
5129 Static => Static);
5131 -- Negative signed integer, compute via multiple/divide the
5132 -- following:
5133 -- (Modulus + X) >> Y + (1 << Y - 1) << (RM_Size - Y) - Modulus
5135 else
5136 Fold_Uint
5138 (Modulus + Expr_Value (Left)) / Two_Y
5139 + (Two_Y - Uint_1)
5140 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right))
5141 - Modulus,
5142 Static => Static);
5143 end if;
5144 end;
5145 end if;
5146 end if;
5147 end Fold_Shift;
5149 --------------
5150 -- Fold_Str --
5151 --------------
5153 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
5154 Loc : constant Source_Ptr := Sloc (N);
5155 Typ : constant Entity_Id := Etype (N);
5157 begin
5158 if Raises_Constraint_Error (N) then
5159 Set_Is_Static_Expression (N, Static);
5160 return;
5161 end if;
5163 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
5165 -- We now have the literal with the right value, both the actual type
5166 -- and the expected type of this literal are taken from the expression
5167 -- that was evaluated. So now we do the Analyze and Resolve.
5169 -- Note that we have to reset Is_Static_Expression both after the
5170 -- analyze step (because Resolve will evaluate the literal, which
5171 -- will cause semantic errors if it is marked as static), and after
5172 -- the Resolve step (since Resolve in some cases resets this flag).
5174 Analyze (N);
5175 Set_Is_Static_Expression (N, Static);
5176 Set_Etype (N, Typ);
5177 Resolve (N);
5178 Set_Is_Static_Expression (N, Static);
5179 end Fold_Str;
5181 ---------------
5182 -- Fold_Uint --
5183 ---------------
5185 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
5186 Loc : constant Source_Ptr := Sloc (N);
5187 Typ : Entity_Id := Etype (N);
5188 Ent : Entity_Id;
5190 begin
5191 if Raises_Constraint_Error (N) then
5192 Set_Is_Static_Expression (N, Static);
5193 return;
5194 end if;
5196 -- If we are folding a named number, retain the entity in the literal
5197 -- in the original tree.
5199 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
5200 Ent := Entity (N);
5201 else
5202 Ent := Empty;
5203 end if;
5205 if Is_Private_Type (Typ) then
5206 Typ := Full_View (Typ);
5207 end if;
5209 -- For a result of type integer, substitute an N_Integer_Literal node
5210 -- for the result of the compile time evaluation of the expression.
5211 -- Set a link to the original named number when not in a generic context
5212 -- for reference in the original tree.
5214 if Is_Integer_Type (Typ) then
5215 Rewrite (N, Make_Integer_Literal (Loc, Val));
5216 Set_Original_Entity (N, Ent);
5218 -- Otherwise we have an enumeration type, and we substitute either
5219 -- an N_Identifier or N_Character_Literal to represent the enumeration
5220 -- literal corresponding to the given value, which must always be in
5221 -- range, because appropriate tests have already been made for this.
5223 else pragma Assert (Is_Enumeration_Type (Typ));
5224 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
5225 end if;
5227 -- We now have the literal with the right value, both the actual type
5228 -- and the expected type of this literal are taken from the expression
5229 -- that was evaluated. So now we do the Analyze and Resolve.
5231 -- Note that we have to reset Is_Static_Expression both after the
5232 -- analyze step (because Resolve will evaluate the literal, which
5233 -- will cause semantic errors if it is marked as static), and after
5234 -- the Resolve step (since Resolve in some cases sets this flag).
5236 Analyze (N);
5237 Set_Is_Static_Expression (N, Static);
5238 Set_Etype (N, Typ);
5239 Resolve (N);
5240 Set_Is_Static_Expression (N, Static);
5241 end Fold_Uint;
5243 ----------------
5244 -- Fold_Ureal --
5245 ----------------
5247 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
5248 Loc : constant Source_Ptr := Sloc (N);
5249 Typ : constant Entity_Id := Etype (N);
5250 Ent : Entity_Id;
5252 begin
5253 if Raises_Constraint_Error (N) then
5254 Set_Is_Static_Expression (N, Static);
5255 return;
5256 end if;
5258 -- If we are folding a named number, retain the entity in the literal
5259 -- in the original tree.
5261 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
5262 Ent := Entity (N);
5263 else
5264 Ent := Empty;
5265 end if;
5267 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
5269 -- Set link to original named number
5271 Set_Original_Entity (N, Ent);
5273 -- We now have the literal with the right value, both the actual type
5274 -- and the expected type of this literal are taken from the expression
5275 -- that was evaluated. So now we do the Analyze and Resolve.
5277 -- Note that we have to reset Is_Static_Expression both after the
5278 -- analyze step (because Resolve will evaluate the literal, which
5279 -- will cause semantic errors if it is marked as static), and after
5280 -- the Resolve step (since Resolve in some cases sets this flag).
5282 -- We mark the node as analyzed so that its type is not erased by
5283 -- calling Analyze_Real_Literal.
5285 Analyze (N);
5286 Set_Is_Static_Expression (N, Static);
5287 Set_Etype (N, Typ);
5288 Resolve (N);
5289 Set_Analyzed (N);
5290 Set_Is_Static_Expression (N, Static);
5291 end Fold_Ureal;
5293 ---------------
5294 -- From_Bits --
5295 ---------------
5297 function From_Bits (B : Bits; T : Entity_Id) return Uint is
5298 V : Uint := Uint_0;
5300 begin
5301 for J in 0 .. B'Last loop
5302 if B (J) then
5303 V := V + 2 ** J;
5304 end if;
5305 end loop;
5307 if Non_Binary_Modulus (T) then
5308 V := V mod Modulus (T);
5309 end if;
5311 return V;
5312 end From_Bits;
5314 --------------------
5315 -- Get_String_Val --
5316 --------------------
5318 function Get_String_Val (N : Node_Id) return Node_Id is
5319 begin
5320 if Nkind (N) in N_String_Literal | N_Character_Literal then
5321 return N;
5322 else
5323 pragma Assert (Is_Entity_Name (N));
5324 return Get_String_Val (Constant_Value (Entity (N)));
5325 end if;
5326 end Get_String_Val;
5328 ----------------
5329 -- Initialize --
5330 ----------------
5332 procedure Initialize is
5333 begin
5334 CV_Cache := (others => (Node_High_Bound, Uint_0));
5335 end Initialize;
5337 --------------------
5338 -- In_Subrange_Of --
5339 --------------------
5341 function In_Subrange_Of
5342 (T1 : Entity_Id;
5343 T2 : Entity_Id;
5344 Fixed_Int : Boolean := False) return Boolean
5346 L1 : Node_Id;
5347 H1 : Node_Id;
5349 L2 : Node_Id;
5350 H2 : Node_Id;
5352 begin
5353 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
5354 return True;
5356 -- Never in range if both types are not scalar. Don't know if this can
5357 -- actually happen, but just in case.
5359 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
5360 return False;
5362 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
5363 -- definitely not compatible with T2.
5365 elsif Is_Floating_Point_Type (T1)
5366 and then Has_Infinities (T1)
5367 and then Is_Floating_Point_Type (T2)
5368 and then not Has_Infinities (T2)
5369 then
5370 return False;
5372 else
5373 L1 := Type_Low_Bound (T1);
5374 H1 := Type_High_Bound (T1);
5376 L2 := Type_Low_Bound (T2);
5377 H2 := Type_High_Bound (T2);
5379 -- Check bounds to see if comparison possible at compile time
5381 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
5382 and then
5383 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
5384 then
5385 return True;
5386 end if;
5388 -- If bounds not comparable at compile time, then the bounds of T2
5389 -- must be compile-time-known or we cannot answer the query.
5391 if not Compile_Time_Known_Value (L2)
5392 or else not Compile_Time_Known_Value (H2)
5393 then
5394 return False;
5395 end if;
5397 -- If the bounds of T1 are know at compile time then use these
5398 -- ones, otherwise use the bounds of the base type (which are of
5399 -- course always static).
5401 if not Compile_Time_Known_Value (L1) then
5402 L1 := Type_Low_Bound (Base_Type (T1));
5403 end if;
5405 if not Compile_Time_Known_Value (H1) then
5406 H1 := Type_High_Bound (Base_Type (T1));
5407 end if;
5409 -- Fixed point types should be considered as such only if
5410 -- flag Fixed_Int is set to False.
5412 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
5413 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
5414 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
5415 then
5416 return
5417 Expr_Value_R (L2) <= Expr_Value_R (L1)
5418 and then
5419 Expr_Value_R (H2) >= Expr_Value_R (H1);
5421 else
5422 return
5423 Expr_Value (L2) <= Expr_Value (L1)
5424 and then
5425 Expr_Value (H2) >= Expr_Value (H1);
5427 end if;
5428 end if;
5430 -- If any exception occurs, it means that we have some bug in the compiler
5431 -- possibly triggered by a previous error, or by some unforeseen peculiar
5432 -- occurrence. However, this is only an optimization attempt, so there is
5433 -- really no point in crashing the compiler. Instead we just decide, too
5434 -- bad, we can't figure out the answer in this case after all.
5436 exception
5437 when others =>
5438 -- With debug flag K we will get an exception unless an error has
5439 -- already occurred (useful for debugging).
5441 if Debug_Flag_K then
5442 Check_Error_Detected;
5443 end if;
5445 return False;
5446 end In_Subrange_Of;
5448 -----------------
5449 -- Is_In_Range --
5450 -----------------
5452 function Is_In_Range
5453 (N : Node_Id;
5454 Typ : Entity_Id;
5455 Assume_Valid : Boolean := False;
5456 Fixed_Int : Boolean := False;
5457 Int_Real : Boolean := False) return Boolean
5459 begin
5460 return
5461 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
5462 end Is_In_Range;
5464 -------------------
5465 -- Is_Null_Range --
5466 -------------------
5468 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5469 begin
5470 if Compile_Time_Known_Value (Lo)
5471 and then Compile_Time_Known_Value (Hi)
5472 then
5473 declare
5474 Typ : Entity_Id := Etype (Lo);
5475 begin
5476 -- When called from the frontend, as part of the analysis of
5477 -- potentially static expressions, Typ will be the full view of a
5478 -- type with all the info needed to answer this query. When called
5479 -- from the backend, for example to know whether a range of a loop
5480 -- is null, Typ might be a private type and we need to explicitly
5481 -- switch to its corresponding full view to access the same info.
5483 if Is_Incomplete_Or_Private_Type (Typ)
5484 and then Present (Full_View (Typ))
5485 then
5486 Typ := Full_View (Typ);
5487 end if;
5489 if Is_Discrete_Type (Typ) then
5490 return Expr_Value (Lo) > Expr_Value (Hi);
5491 else pragma Assert (Is_Real_Type (Typ));
5492 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
5493 end if;
5494 end;
5495 else
5496 return False;
5497 end if;
5498 end Is_Null_Range;
5500 -------------------------
5501 -- Is_OK_Static_Choice --
5502 -------------------------
5504 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
5505 begin
5506 -- Check various possibilities for choice
5508 -- Note: for membership tests, we test more cases than are possible
5509 -- (in particular subtype indication), but it doesn't matter because
5510 -- it just won't occur (we have already done a syntax check).
5512 if Nkind (Choice) = N_Others_Choice then
5513 return True;
5515 elsif Nkind (Choice) = N_Range then
5516 return Is_OK_Static_Range (Choice);
5518 elsif Nkind (Choice) = N_Subtype_Indication
5519 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5520 then
5521 return Is_OK_Static_Subtype (Etype (Choice));
5523 else
5524 return Is_OK_Static_Expression (Choice);
5525 end if;
5526 end Is_OK_Static_Choice;
5528 ------------------------------
5529 -- Is_OK_Static_Choice_List --
5530 ------------------------------
5532 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
5533 Choice : Node_Id;
5535 begin
5536 if not Is_Static_Choice_List (Choices) then
5537 return False;
5538 end if;
5540 Choice := First (Choices);
5541 while Present (Choice) loop
5542 if not Is_OK_Static_Choice (Choice) then
5543 Set_Raises_Constraint_Error (Choice);
5544 return False;
5545 end if;
5547 Next (Choice);
5548 end loop;
5550 return True;
5551 end Is_OK_Static_Choice_List;
5553 -----------------------------
5554 -- Is_OK_Static_Expression --
5555 -----------------------------
5557 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
5558 begin
5559 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
5560 end Is_OK_Static_Expression;
5562 ------------------------
5563 -- Is_OK_Static_Range --
5564 ------------------------
5566 -- A static range is a range whose bounds are static expressions, or a
5567 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5568 -- We have already converted range attribute references, so we get the
5569 -- "or" part of this rule without needing a special test.
5571 function Is_OK_Static_Range (N : Node_Id) return Boolean is
5572 begin
5573 return Is_OK_Static_Expression (Low_Bound (N))
5574 and then Is_OK_Static_Expression (High_Bound (N));
5575 end Is_OK_Static_Range;
5577 --------------------------
5578 -- Is_OK_Static_Subtype --
5579 --------------------------
5581 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
5582 -- neither bound raises Constraint_Error when evaluated.
5584 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
5585 Base_T : constant Entity_Id := Base_Type (Typ);
5586 Anc_Subt : Entity_Id;
5588 begin
5589 -- First a quick check on the non static subtype flag. As described
5590 -- in further detail in Einfo, this flag is not decisive in all cases,
5591 -- but if it is set, then the subtype is definitely non-static.
5593 if Is_Non_Static_Subtype (Typ) then
5594 return False;
5595 end if;
5597 -- Then, check if the subtype is strictly static. This takes care of
5598 -- checking for generics and predicates.
5600 if not Is_Static_Subtype (Typ) then
5601 return False;
5602 end if;
5604 -- String types
5606 if Is_String_Type (Typ) then
5607 return
5608 Ekind (Typ) = E_String_Literal_Subtype
5609 or else
5610 (Is_OK_Static_Subtype (Component_Type (Typ))
5611 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
5613 -- Scalar types
5615 elsif Is_Scalar_Type (Typ) then
5616 if Base_T = Typ then
5617 return True;
5619 else
5620 Anc_Subt := Ancestor_Subtype (Typ);
5622 if No (Anc_Subt) then
5623 Anc_Subt := Base_T;
5624 end if;
5626 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
5627 -- Get_Type_{Low,High}_Bound.
5629 return Is_OK_Static_Subtype (Anc_Subt)
5630 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
5631 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
5632 end if;
5634 -- Types other than string and scalar types are never static
5636 else
5637 return False;
5638 end if;
5639 end Is_OK_Static_Subtype;
5641 ---------------------
5642 -- Is_Out_Of_Range --
5643 ---------------------
5645 function Is_Out_Of_Range
5646 (N : Node_Id;
5647 Typ : Entity_Id;
5648 Assume_Valid : Boolean := False;
5649 Fixed_Int : Boolean := False;
5650 Int_Real : Boolean := False) return Boolean
5652 begin
5653 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
5654 Out_Of_Range;
5655 end Is_Out_Of_Range;
5657 ----------------------
5658 -- Is_Static_Choice --
5659 ----------------------
5661 function Is_Static_Choice (Choice : Node_Id) return Boolean is
5662 begin
5663 -- Check various possibilities for choice
5665 -- Note: for membership tests, we test more cases than are possible
5666 -- (in particular subtype indication), but it doesn't matter because
5667 -- it just won't occur (we have already done a syntax check).
5669 if Nkind (Choice) = N_Others_Choice then
5670 return True;
5672 elsif Nkind (Choice) = N_Range then
5673 return Is_Static_Range (Choice);
5675 elsif Nkind (Choice) = N_Subtype_Indication
5676 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5677 then
5678 return Is_Static_Subtype (Etype (Choice));
5680 else
5681 return Is_Static_Expression (Choice);
5682 end if;
5683 end Is_Static_Choice;
5685 ---------------------------
5686 -- Is_Static_Choice_List --
5687 ---------------------------
5689 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
5690 Choice : Node_Id;
5692 begin
5693 Choice := First (Choices);
5694 while Present (Choice) loop
5695 if not Is_Static_Choice (Choice) then
5696 return False;
5697 end if;
5699 Next (Choice);
5700 end loop;
5702 return True;
5703 end Is_Static_Choice_List;
5705 ---------------------
5706 -- Is_Static_Range --
5707 ---------------------
5709 -- A static range is a range whose bounds are static expressions, or a
5710 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5711 -- We have already converted range attribute references, so we get the
5712 -- "or" part of this rule without needing a special test.
5714 function Is_Static_Range (N : Node_Id) return Boolean is
5715 begin
5716 return Is_Static_Expression (Low_Bound (N))
5717 and then
5718 Is_Static_Expression (High_Bound (N));
5719 end Is_Static_Range;
5721 -----------------------
5722 -- Is_Static_Subtype --
5723 -----------------------
5725 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5727 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
5728 Base_T : constant Entity_Id := Base_Type (Typ);
5729 Anc_Subt : Entity_Id;
5731 begin
5732 -- First a quick check on the non static subtype flag. As described
5733 -- in further detail in Einfo, this flag is not decisive in all cases,
5734 -- but if it is set, then the subtype is definitely non-static.
5736 if Is_Non_Static_Subtype (Typ) then
5737 return False;
5738 end if;
5740 Anc_Subt := Ancestor_Subtype (Typ);
5742 if Anc_Subt = Empty then
5743 Anc_Subt := Base_T;
5744 end if;
5746 if Is_Generic_Type (Root_Type (Base_T))
5747 or else Is_Generic_Actual_Type (Base_T)
5748 then
5749 return False;
5751 -- If there is a dynamic predicate for the type (declared or inherited)
5752 -- the expression is not static.
5754 elsif Has_Dynamic_Predicate_Aspect (Typ)
5755 or else (Is_Derived_Type (Typ)
5756 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5757 or else (Has_Aspect (Typ, Aspect_Predicate)
5758 and then not Has_Static_Predicate (Typ))
5759 then
5760 return False;
5762 -- String types
5764 elsif Is_String_Type (Typ) then
5765 return
5766 Ekind (Typ) = E_String_Literal_Subtype
5767 or else (Is_Static_Subtype (Component_Type (Typ))
5768 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5770 -- Scalar types
5772 elsif Is_Scalar_Type (Typ) then
5773 if Base_T = Typ then
5774 return True;
5776 else
5777 return Is_Static_Subtype (Anc_Subt)
5778 and then Is_Static_Expression (Type_Low_Bound (Typ))
5779 and then Is_Static_Expression (Type_High_Bound (Typ));
5780 end if;
5782 -- Types other than string and scalar types are never static
5784 else
5785 return False;
5786 end if;
5787 end Is_Static_Subtype;
5789 -------------------------------
5790 -- Is_Statically_Unevaluated --
5791 -------------------------------
5793 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5794 function Check_Case_Expr_Alternative
5795 (CEA : Node_Id) return Match_Result;
5796 -- We have a message emanating from the Expression of a case expression
5797 -- alternative. We examine this alternative, as follows:
5799 -- If the selecting expression of the parent case is non-static, or
5800 -- if any of the discrete choices of the given case alternative are
5801 -- non-static or raise Constraint_Error, return Non_Static.
5803 -- Otherwise check if the selecting expression matches any of the given
5804 -- discrete choices. If so, the alternative is executed and we return
5805 -- Match, otherwise, the alternative can never be executed, and so we
5806 -- return No_Match.
5808 ---------------------------------
5809 -- Check_Case_Expr_Alternative --
5810 ---------------------------------
5812 function Check_Case_Expr_Alternative
5813 (CEA : Node_Id) return Match_Result
5815 Case_Exp : constant Node_Id := Parent (CEA);
5816 Choice : Node_Id;
5817 Prev_CEA : Node_Id;
5819 begin
5820 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5822 -- Check that selecting expression is static
5824 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5825 return Non_Static;
5826 end if;
5828 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5829 return Non_Static;
5830 end if;
5832 -- All choices are now known to be static. Now see if alternative
5833 -- matches one of the choices.
5835 Choice := First (Discrete_Choices (CEA));
5836 while Present (Choice) loop
5838 -- Check various possibilities for choice, returning Match if we
5839 -- find the selecting value matches any of the choices. Note that
5840 -- we know we are the last choice, so we don't have to keep going.
5842 if Nkind (Choice) = N_Others_Choice then
5844 -- Others choice is a bit annoying, it matches if none of the
5845 -- previous alternatives matches (note that we know we are the
5846 -- last alternative in this case, so we can just go backwards
5847 -- from us to see if any previous one matches).
5849 Prev_CEA := Prev (CEA);
5850 while Present (Prev_CEA) loop
5851 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5852 return No_Match;
5853 end if;
5855 Prev (Prev_CEA);
5856 end loop;
5858 return Match;
5860 -- Else we have a normal static choice
5862 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5863 return Match;
5864 end if;
5866 -- If we fall through, it means that the discrete choice did not
5867 -- match the selecting expression, so continue.
5869 Next (Choice);
5870 end loop;
5872 -- If we get through that loop then all choices were static, and none
5873 -- of them matched the selecting expression. So return No_Match.
5875 return No_Match;
5876 end Check_Case_Expr_Alternative;
5878 -- Local variables
5880 P : Node_Id;
5881 OldP : Node_Id;
5882 Choice : Node_Id;
5884 -- Start of processing for Is_Statically_Unevaluated
5886 begin
5887 -- The (32.x) references here are from RM section 4.9
5889 -- (32.1) An expression is statically unevaluated if it is part of ...
5891 -- This means we have to climb the tree looking for one of the cases
5893 P := Expr;
5894 loop
5895 OldP := P;
5896 P := Parent (P);
5898 -- (32.2) The right operand of a static short-circuit control form
5899 -- whose value is determined by its left operand.
5901 -- AND THEN with False as left operand
5903 if Nkind (P) = N_And_Then
5904 and then Compile_Time_Known_Value (Left_Opnd (P))
5905 and then Is_False (Expr_Value (Left_Opnd (P)))
5906 then
5907 return True;
5909 -- OR ELSE with True as left operand
5911 elsif Nkind (P) = N_Or_Else
5912 and then Compile_Time_Known_Value (Left_Opnd (P))
5913 and then Is_True (Expr_Value (Left_Opnd (P)))
5914 then
5915 return True;
5917 -- (32.3) A dependent_expression of an if_expression whose associated
5918 -- condition is static and equals False.
5920 elsif Nkind (P) = N_If_Expression then
5921 declare
5922 Cond : constant Node_Id := First (Expressions (P));
5923 Texp : constant Node_Id := Next (Cond);
5924 Fexp : constant Node_Id := Next (Texp);
5926 begin
5927 if Compile_Time_Known_Value (Cond) then
5929 -- Condition is True and we are in the right operand
5931 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5932 return True;
5934 -- Condition is False and we are in the left operand
5936 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5937 return True;
5938 end if;
5939 end if;
5940 end;
5942 -- (32.4) A condition or dependent_expression of an if_expression
5943 -- where the condition corresponding to at least one preceding
5944 -- dependent_expression of the if_expression is static and equals
5945 -- True.
5947 -- This refers to cases like
5949 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5951 -- But we expand elsif's out anyway, so the above looks like:
5953 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5955 -- So for us this is caught by the above check for the 32.3 case.
5957 -- (32.5) A dependent_expression of a case_expression whose
5958 -- selecting_expression is static and whose value is not covered
5959 -- by the corresponding discrete_choice_list.
5961 elsif Nkind (P) = N_Case_Expression_Alternative then
5963 -- First, we have to be in the expression to suppress messages.
5964 -- If we are within one of the choices, we want the message.
5966 if OldP = Expression (P) then
5968 -- Statically unevaluated if alternative does not match
5970 if Check_Case_Expr_Alternative (P) = No_Match then
5971 return True;
5972 end if;
5973 end if;
5975 -- (32.6) A choice_expression (or a simple_expression of a range
5976 -- that occurs as a membership_choice of a membership_choice_list)
5977 -- of a static membership test that is preceded in the enclosing
5978 -- membership_choice_list by another item whose individual
5979 -- membership test (see (RM 4.5.2)) statically yields True.
5981 elsif Nkind (P) in N_Membership_Test then
5983 -- Only possibly unevaluated if simple expression is static
5985 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5986 null;
5988 -- All members of the choice list must be static
5990 elsif (Present (Right_Opnd (P))
5991 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5992 or else (Present (Alternatives (P))
5993 and then
5994 not Is_OK_Static_Choice_List (Alternatives (P)))
5995 then
5996 null;
5998 -- If expression is the one and only alternative, then it is
5999 -- definitely not statically unevaluated, so we only have to
6000 -- test the case where there are alternatives present.
6002 elsif Present (Alternatives (P)) then
6004 -- Look for previous matching Choice
6006 Choice := First (Alternatives (P));
6007 while Present (Choice) loop
6009 -- If we reached us and no previous choices matched, this
6010 -- is not the case where we are statically unevaluated.
6012 exit when OldP = Choice;
6014 -- If a previous choice matches, then that is the case where
6015 -- we know our choice is statically unevaluated.
6017 if Choice_Matches (Left_Opnd (P), Choice) = Match then
6018 return True;
6019 end if;
6021 Next (Choice);
6022 end loop;
6024 -- If we fall through the loop, we were not one of the choices,
6025 -- we must have been the expression, so that is not covered by
6026 -- this rule, and we keep going.
6028 null;
6029 end if;
6030 end if;
6032 -- OK, not statically unevaluated at this level, see if we should
6033 -- keep climbing to look for a higher level reason.
6035 -- Special case for component association in aggregates, where
6036 -- we want to keep climbing up to the parent aggregate.
6038 if Nkind (P) = N_Component_Association
6039 and then Nkind (Parent (P)) = N_Aggregate
6040 then
6041 null;
6043 -- All done if not still within subexpression
6045 else
6046 exit when Nkind (P) not in N_Subexpr;
6047 end if;
6048 end loop;
6050 -- If we fall through the loop, not one of the cases covered!
6052 return False;
6053 end Is_Statically_Unevaluated;
6055 --------------------
6056 -- Machine_Number --
6057 --------------------
6059 -- Historical note: RM 4.9(38) originally specified biased rounding but
6060 -- this has been modified by AI-268 to prevent confusing differences in
6061 -- rounding between static and nonstatic expressions. This AI specifies
6062 -- that the effect of such rounding is implementation-dependent instead,
6063 -- and in GNAT we round to nearest even to match the run-time behavior.
6064 -- Note that this applies to floating-point literals, not fixed-point
6065 -- ones, even though their representation is also a universal real.
6067 function Machine_Number
6068 (Typ : Entity_Id;
6069 Val : Ureal;
6070 N : Node_Id) return Ureal
6072 begin
6073 return Machine (Typ, Val, Round_Even, N);
6074 end Machine_Number;
6076 --------------------
6077 -- Not_Null_Range --
6078 --------------------
6080 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
6081 begin
6082 if Compile_Time_Known_Value (Lo)
6083 and then Compile_Time_Known_Value (Hi)
6084 then
6085 declare
6086 Typ : Entity_Id := Etype (Lo);
6087 begin
6088 -- When called from the frontend, as part of the analysis of
6089 -- potentially static expressions, Typ will be the full view of a
6090 -- type with all the info needed to answer this query. When called
6091 -- from the backend, for example to know whether a range of a loop
6092 -- is null, Typ might be a private type and we need to explicitly
6093 -- switch to its corresponding full view to access the same info.
6095 if Is_Incomplete_Or_Private_Type (Typ)
6096 and then Present (Full_View (Typ))
6097 then
6098 Typ := Full_View (Typ);
6099 end if;
6101 if Is_Discrete_Type (Typ) then
6102 return Expr_Value (Lo) <= Expr_Value (Hi);
6103 else pragma Assert (Is_Real_Type (Typ));
6104 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
6105 end if;
6106 end;
6107 else
6108 return False;
6109 end if;
6111 end Not_Null_Range;
6113 -------------
6114 -- OK_Bits --
6115 -------------
6117 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
6118 begin
6119 -- We allow a maximum of 500,000 bits which seems a reasonable limit
6121 if Bits < 500_000 then
6122 return True;
6124 -- Error if this maximum is exceeded
6126 else
6127 Error_Msg_N ("static value too large, capacity exceeded", N);
6128 return False;
6129 end if;
6130 end OK_Bits;
6132 ------------------
6133 -- Out_Of_Range --
6134 ------------------
6136 procedure Out_Of_Range (N : Node_Id) is
6137 begin
6138 -- If we have the static expression case, then this is an illegality
6139 -- in Ada 95 mode, except that in an instance, we never generate an
6140 -- error (if the error is legitimate, it was already diagnosed in the
6141 -- template).
6143 if Is_Static_Expression (N)
6144 and then not In_Instance
6145 and then not In_Inlined_Body
6146 and then Ada_Version >= Ada_95
6147 then
6148 -- No message if we are statically unevaluated
6150 if Is_Statically_Unevaluated (N) then
6151 null;
6153 -- The expression to compute the length of a packed array is attached
6154 -- to the array type itself, and deserves a separate message.
6156 elsif Nkind (Parent (N)) = N_Defining_Identifier
6157 and then Is_Array_Type (Parent (N))
6158 and then Present (Packed_Array_Impl_Type (Parent (N)))
6159 and then Present (First_Rep_Item (Parent (N)))
6160 then
6161 Error_Msg_N
6162 ("length of packed array must not exceed Integer''Last",
6163 First_Rep_Item (Parent (N)));
6164 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
6166 -- All cases except the special array case.
6167 -- No message if we are dealing with System.Priority values in
6168 -- CodePeer mode where the target runtime may have more priorities.
6170 elsif not CodePeer_Mode
6171 or else not Is_RTE (Etype (N), RE_Priority)
6172 then
6173 -- Determine if the out-of-range violation constitutes a warning
6174 -- or an error based on context, according to RM 4.9 (34/3).
6176 if Nkind (Original_Node (N)) = N_Type_Conversion
6177 and then not Comes_From_Source (Original_Node (N))
6178 then
6179 Apply_Compile_Time_Constraint_Error
6180 (N, "value not in range of}??", CE_Range_Check_Failed);
6181 else
6182 Apply_Compile_Time_Constraint_Error
6183 (N, "value not in range of}", CE_Range_Check_Failed);
6184 end if;
6185 end if;
6187 -- Here we generate a warning for the Ada 83 case, or when we are in an
6188 -- instance, or when we have a non-static expression case.
6190 else
6191 Apply_Compile_Time_Constraint_Error
6192 (N, "value not in range of}??", CE_Range_Check_Failed);
6193 end if;
6194 end Out_Of_Range;
6196 ---------------------------
6197 -- Predicates_Compatible --
6198 ---------------------------
6200 function Predicates_Compatible (T1, T2 : Entity_Id) return Boolean is
6202 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean;
6203 -- Return True if the rep item for Nam is either absent on T2 or also
6204 -- applies to T1.
6206 -------------------------------
6207 -- T2_Rep_Item_Applies_To_T1 --
6208 -------------------------------
6210 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean is
6211 Rep_Item : constant Node_Id := Get_Rep_Item (T2, Nam);
6213 begin
6214 return No (Rep_Item) or else Get_Rep_Item (T1, Nam) = Rep_Item;
6215 end T2_Rep_Item_Applies_To_T1;
6217 -- Start of processing for Predicates_Compatible
6219 begin
6220 if Ada_Version < Ada_2012 then
6221 return True;
6223 -- If T2 has no predicates, there is no compatibility issue
6225 elsif not Has_Predicates (T2) then
6226 return True;
6228 -- T2 has predicates, if T1 has none then we defer to the static check
6230 elsif not Has_Predicates (T1) then
6231 null;
6233 -- Both T2 and T1 have predicates, check that all predicates that apply
6234 -- to T2 apply also to T1 (RM 4.9.1(9/3)).
6236 elsif T2_Rep_Item_Applies_To_T1 (Name_Static_Predicate)
6237 and then T2_Rep_Item_Applies_To_T1 (Name_Dynamic_Predicate)
6238 and then T2_Rep_Item_Applies_To_T1 (Name_Predicate)
6239 then
6240 return True;
6241 end if;
6243 -- Implement the static check prescribed by RM 4.9.1(10/3)
6245 if Is_Static_Subtype (T1) and then Is_Static_Subtype (T2) then
6246 -- We just need to query Interval_Lists for discrete types
6248 if Is_Discrete_Type (T1) and then Is_Discrete_Type (T2) then
6249 declare
6250 Interval_List1 : constant Interval_Lists.Discrete_Interval_List
6251 := Interval_Lists.Type_Intervals (T1);
6252 Interval_List2 : constant Interval_Lists.Discrete_Interval_List
6253 := Interval_Lists.Type_Intervals (T2);
6254 begin
6255 return Interval_Lists.Is_Subset (Interval_List1, Interval_List2)
6256 and then not (Has_Predicates (T1)
6257 and then not Predicate_Checks_Suppressed (T2)
6258 and then Predicate_Checks_Suppressed (T1));
6259 end;
6261 else
6262 -- ??? Need to implement Interval_Lists for real types
6264 return False;
6265 end if;
6267 -- If either subtype is not static, the predicates are not compatible
6269 else
6270 return False;
6271 end if;
6272 end Predicates_Compatible;
6274 ----------------------
6275 -- Predicates_Match --
6276 ----------------------
6278 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
6280 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean;
6281 -- Return True if T1 and T2 have the same rep item for Nam
6283 ------------------------
6284 -- Have_Same_Rep_Item --
6285 ------------------------
6287 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean is
6288 begin
6289 return Get_Rep_Item (T1, Nam) = Get_Rep_Item (T2, Nam);
6290 end Have_Same_Rep_Item;
6292 -- Start of processing for Predicates_Match
6294 begin
6295 if Ada_Version < Ada_2012 then
6296 return True;
6298 -- If T2 has no predicates, match if and only if T1 has none
6300 elsif not Has_Predicates (T2) then
6301 return not Has_Predicates (T1);
6303 -- T2 has predicates, no match if T1 has none
6305 elsif not Has_Predicates (T1) then
6306 return False;
6308 -- Both T2 and T1 have predicates, check that they all come
6309 -- from the same declarations.
6311 else
6312 return Have_Same_Rep_Item (Name_Static_Predicate)
6313 and then Have_Same_Rep_Item (Name_Dynamic_Predicate)
6314 and then Have_Same_Rep_Item (Name_Predicate);
6315 end if;
6316 end Predicates_Match;
6318 ---------------------------------------------
6319 -- Real_Or_String_Static_Predicate_Matches --
6320 ---------------------------------------------
6322 function Real_Or_String_Static_Predicate_Matches
6323 (Val : Node_Id;
6324 Typ : Entity_Id) return Boolean
6326 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
6327 -- The predicate expression from the type
6329 Pfun : constant Entity_Id := Predicate_Function (Typ);
6330 -- The entity for the predicate function
6332 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
6333 -- The name of the formal of the predicate function. Occurrences of the
6334 -- type name in Expr have been rewritten as references to this formal,
6335 -- and it has a unique name, so we can identify references by this name.
6337 Copy : Node_Id;
6338 -- Copy of the predicate function tree
6340 function Process (N : Node_Id) return Traverse_Result;
6341 -- Function used to process nodes during the traversal in which we will
6342 -- find occurrences of the entity name, and replace such occurrences
6343 -- by a real literal with the value to be tested.
6345 procedure Traverse is new Traverse_Proc (Process);
6346 -- The actual traversal procedure
6348 -------------
6349 -- Process --
6350 -------------
6352 function Process (N : Node_Id) return Traverse_Result is
6353 begin
6354 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
6355 declare
6356 Nod : constant Node_Id := New_Copy (Val);
6357 begin
6358 Set_Sloc (Nod, Sloc (N));
6359 Rewrite (N, Nod);
6360 return Skip;
6361 end;
6363 -- The predicate function may contain string-comparison operations
6364 -- that have been converted into calls to run-time array-comparison
6365 -- routines. To evaluate the predicate statically, we recover the
6366 -- original comparison operation and replace the occurrence of the
6367 -- formal by the static string value. The actuals of the generated
6368 -- call are of the form X'Address.
6370 elsif Nkind (N) in N_Op_Compare
6371 and then Nkind (Left_Opnd (N)) = N_Function_Call
6372 then
6373 declare
6374 C : constant Node_Id := Left_Opnd (N);
6375 F : constant Node_Id := First (Parameter_Associations (C));
6376 L : constant Node_Id := Prefix (F);
6377 R : constant Node_Id := Prefix (Next (F));
6379 begin
6380 -- If an operand is an entity name, it is the formal of the
6381 -- predicate function, so replace it with the string value.
6382 -- It may be either operand in the call. The other operand
6383 -- is a static string from the original predicate.
6385 if Is_Entity_Name (L) then
6386 Rewrite (Left_Opnd (N), New_Copy (Val));
6387 Rewrite (Right_Opnd (N), New_Copy (R));
6389 else
6390 Rewrite (Left_Opnd (N), New_Copy (L));
6391 Rewrite (Right_Opnd (N), New_Copy (Val));
6392 end if;
6394 return Skip;
6395 end;
6397 else
6398 return OK;
6399 end if;
6400 end Process;
6402 -- Start of processing for Real_Or_String_Static_Predicate_Matches
6404 begin
6405 -- First deal with special case of inherited predicate, where the
6406 -- predicate expression looks like:
6408 -- xxPredicate (typ (Ent)) and then Expr
6410 -- where Expr is the predicate expression for this level, and the
6411 -- left operand is the call to evaluate the inherited predicate.
6413 if Nkind (Expr) = N_And_Then
6414 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
6415 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
6416 then
6417 -- OK we have the inherited case, so make a call to evaluate the
6418 -- inherited predicate. If that fails, so do we!
6420 if not
6421 Real_Or_String_Static_Predicate_Matches
6422 (Val => Val,
6423 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
6424 then
6425 return False;
6426 end if;
6428 -- Use the right operand for the continued processing
6430 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
6432 -- Case where call to predicate function appears on its own (this means
6433 -- that the predicate at this level is just inherited from the parent).
6435 elsif Nkind (Expr) = N_Function_Call then
6436 declare
6437 Typ : constant Entity_Id :=
6438 Etype (First_Formal (Entity (Name (Expr))));
6440 begin
6441 -- If the inherited predicate is dynamic, just ignore it. We can't
6442 -- go trying to evaluate a dynamic predicate as a static one!
6444 if Has_Dynamic_Predicate_Aspect (Typ) then
6445 return True;
6447 -- Otherwise inherited predicate is static, check for match
6449 else
6450 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
6451 end if;
6452 end;
6454 -- If not just an inherited predicate, copy whole expression
6456 else
6457 Copy := Copy_Separate_Tree (Expr);
6458 end if;
6460 -- Now we replace occurrences of the entity by the value
6462 Traverse (Copy);
6464 -- And analyze the resulting static expression to see if it is True
6466 Analyze_And_Resolve (Copy, Standard_Boolean);
6467 return Is_True (Expr_Value (Copy));
6468 end Real_Or_String_Static_Predicate_Matches;
6470 -------------------------
6471 -- Rewrite_In_Raise_CE --
6472 -------------------------
6474 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
6475 Stat : constant Boolean := Is_Static_Expression (N);
6476 Typ : constant Entity_Id := Etype (N);
6478 begin
6479 -- If we want to raise CE in the condition of a N_Raise_CE node, we
6480 -- can just clear the condition if the reason is appropriate. We do
6481 -- not do this operation if the parent has a reason other than range
6482 -- check failed, because otherwise we would change the reason.
6484 if Present (Parent (N))
6485 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
6486 and then Reason (Parent (N)) =
6487 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
6488 then
6489 Set_Condition (Parent (N), Empty);
6491 -- Else build an explicit N_Raise_CE
6493 else
6494 if Nkind (Exp) = N_Raise_Constraint_Error then
6495 Rewrite (N,
6496 Make_Raise_Constraint_Error (Sloc (Exp),
6497 Reason => Reason (Exp)));
6498 else
6499 Rewrite (N,
6500 Make_Raise_Constraint_Error (Sloc (Exp),
6501 Reason => CE_Range_Check_Failed));
6502 end if;
6504 Set_Raises_Constraint_Error (N);
6505 Set_Etype (N, Typ);
6506 end if;
6508 -- Set proper flags in result
6510 Set_Raises_Constraint_Error (N, True);
6511 Set_Is_Static_Expression (N, Stat);
6512 end Rewrite_In_Raise_CE;
6514 ------------------------------------------------
6515 -- Set_Checking_Potentially_Static_Expression --
6516 ------------------------------------------------
6518 procedure Set_Checking_Potentially_Static_Expression (Value : Boolean) is
6519 begin
6520 -- Verify that we only start/stop checking for a potentially static
6521 -- expression and do not start or stop it twice in a row.
6523 pragma Assert (Checking_For_Potentially_Static_Expression /= Value);
6525 Checking_For_Potentially_Static_Expression := Value;
6526 end Set_Checking_Potentially_Static_Expression;
6528 ---------------------
6529 -- String_Type_Len --
6530 ---------------------
6532 function String_Type_Len (Stype : Entity_Id) return Uint is
6533 NT : constant Entity_Id := Etype (First_Index (Stype));
6534 T : Entity_Id;
6536 begin
6537 if Is_OK_Static_Subtype (NT) then
6538 T := NT;
6539 else
6540 T := Base_Type (NT);
6541 end if;
6543 return Expr_Value (Type_High_Bound (T)) -
6544 Expr_Value (Type_Low_Bound (T)) + 1;
6545 end String_Type_Len;
6547 ------------------------------------
6548 -- Subtypes_Statically_Compatible --
6549 ------------------------------------
6551 function Subtypes_Statically_Compatible
6552 (T1 : Entity_Id;
6553 T2 : Entity_Id;
6554 Formal_Derived_Matching : Boolean := False) return Boolean
6556 begin
6557 -- A type is always statically compatible with itself
6559 if T1 = T2 then
6560 return True;
6562 -- Not compatible if predicates are not compatible
6564 elsif not Predicates_Compatible (T1, T2) then
6565 return False;
6567 -- Scalar types
6569 elsif Is_Scalar_Type (T1) then
6571 -- Definitely compatible if we match
6573 if Subtypes_Statically_Match (T1, T2) then
6574 return True;
6576 -- A scalar subtype S1 is compatible with S2 if their bounds
6577 -- are static and compatible, even if S1 has dynamic predicates
6578 -- and is thus non-static. Predicate compatibility has been
6579 -- checked above.
6581 elsif not Is_Static_Range (Scalar_Range (T1))
6582 or else not Is_Static_Range (Scalar_Range (T2))
6583 then
6584 return False;
6586 -- Base types must match, but we don't check that (should we???) but
6587 -- we do at least check that both types are real, or both types are
6588 -- not real.
6590 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
6591 return False;
6593 -- Here we check the bounds
6595 else
6596 declare
6597 LB1 : constant Node_Id := Type_Low_Bound (T1);
6598 HB1 : constant Node_Id := Type_High_Bound (T1);
6599 LB2 : constant Node_Id := Type_Low_Bound (T2);
6600 HB2 : constant Node_Id := Type_High_Bound (T2);
6602 begin
6603 if Is_Real_Type (T1) then
6604 return
6605 Expr_Value_R (LB1) > Expr_Value_R (HB1)
6606 or else
6607 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
6608 and then Expr_Value_R (HB1) <= Expr_Value_R (HB2));
6610 else
6611 return
6612 Expr_Value (LB1) > Expr_Value (HB1)
6613 or else
6614 (Expr_Value (LB2) <= Expr_Value (LB1)
6615 and then Expr_Value (HB1) <= Expr_Value (HB2));
6616 end if;
6617 end;
6618 end if;
6620 -- Access types
6622 elsif Is_Access_Type (T1) then
6623 return
6624 (not Is_Constrained (T2)
6625 or else Subtypes_Statically_Match
6626 (Designated_Type (T1), Designated_Type (T2)))
6627 and then not (Can_Never_Be_Null (T2)
6628 and then not Can_Never_Be_Null (T1));
6630 -- Private types without discriminants can be handled specially.
6631 -- Predicate matching has been checked above.
6633 elsif Is_Private_Type (T1)
6634 and then not Has_Discriminants (T1)
6635 then
6636 return not Has_Discriminants (T2);
6638 -- All other cases
6640 else
6641 return
6642 (Is_Composite_Type (T1) and then not Is_Constrained (T2))
6643 or else Subtypes_Statically_Match
6644 (T1, T2, Formal_Derived_Matching);
6645 end if;
6646 end Subtypes_Statically_Compatible;
6648 -------------------------------
6649 -- Subtypes_Statically_Match --
6650 -------------------------------
6652 -- Subtypes statically match if they have statically matching constraints
6653 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
6654 -- they are the same identical constraint, or if they are static and the
6655 -- values match (RM 4.9.1(1)).
6657 -- In addition, in GNAT, the object size (Esize) values of the types must
6658 -- match if they are set (unless checking an actual for a formal derived
6659 -- type). The use of 'Object_Size can cause this to be false even if the
6660 -- types would otherwise match in the Ada 95 RM sense, but this deviation
6661 -- is adopted by AI12-059 which introduces Object_Size in Ada 2022.
6663 function Subtypes_Statically_Match
6664 (T1 : Entity_Id;
6665 T2 : Entity_Id;
6666 Formal_Derived_Matching : Boolean := False) return Boolean
6668 begin
6669 -- A type always statically matches itself
6671 if T1 = T2 then
6672 return True;
6674 -- No match if sizes different (from use of 'Object_Size). This test
6675 -- is excluded if Formal_Derived_Matching is True, as the base types
6676 -- can be different in that case and typically have different sizes.
6678 elsif not Formal_Derived_Matching
6679 and then Known_Static_Esize (T1)
6680 and then Known_Static_Esize (T2)
6681 and then Esize (T1) /= Esize (T2)
6682 then
6683 return False;
6685 -- No match if predicates do not match
6687 elsif not Predicates_Match (T1, T2) then
6688 return False;
6690 -- Scalar types
6692 elsif Is_Scalar_Type (T1) then
6694 -- Base types must be the same
6696 if Base_Type (T1) /= Base_Type (T2) then
6697 return False;
6698 end if;
6700 -- A constrained numeric subtype never matches an unconstrained
6701 -- subtype, i.e. both types must be constrained or unconstrained.
6703 -- To understand the requirement for this test, see RM 4.9.1(1).
6704 -- As is made clear in RM 3.5.4(11), type Integer, for example is
6705 -- a constrained subtype with constraint bounds matching the bounds
6706 -- of its corresponding unconstrained base type. In this situation,
6707 -- Integer and Integer'Base do not statically match, even though
6708 -- they have the same bounds.
6710 -- We only apply this test to types in Standard and types that appear
6711 -- in user programs. That way, we do not have to be too careful about
6712 -- setting Is_Constrained right for Itypes.
6714 if Is_Numeric_Type (T1)
6715 and then (Is_Constrained (T1) /= Is_Constrained (T2))
6716 and then (Scope (T1) = Standard_Standard
6717 or else Comes_From_Source (T1))
6718 and then (Scope (T2) = Standard_Standard
6719 or else Comes_From_Source (T2))
6720 then
6721 return False;
6723 -- A generic scalar type does not statically match its base type
6724 -- (AI-311). In this case we make sure that the formals, which are
6725 -- first subtypes of their bases, are constrained.
6727 elsif Is_Generic_Type (T1)
6728 and then Is_Generic_Type (T2)
6729 and then (Is_Constrained (T1) /= Is_Constrained (T2))
6730 then
6731 return False;
6732 end if;
6734 -- If there was an error in either range, then just assume the types
6735 -- statically match to avoid further junk errors.
6737 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
6738 or else Error_Posted (Scalar_Range (T1))
6739 or else Error_Posted (Scalar_Range (T2))
6740 then
6741 return True;
6742 end if;
6744 -- Otherwise both types have bounds that can be compared
6746 declare
6747 LB1 : constant Node_Id := Type_Low_Bound (T1);
6748 HB1 : constant Node_Id := Type_High_Bound (T1);
6749 LB2 : constant Node_Id := Type_Low_Bound (T2);
6750 HB2 : constant Node_Id := Type_High_Bound (T2);
6752 begin
6753 -- If the bounds are the same tree node, then match (common case)
6755 if LB1 = LB2 and then HB1 = HB2 then
6756 return True;
6758 -- Otherwise bounds must be static and identical value
6760 else
6761 if not Is_OK_Static_Subtype (T1)
6762 or else
6763 not Is_OK_Static_Subtype (T2)
6764 then
6765 return False;
6767 elsif Is_Real_Type (T1) then
6768 return
6769 Expr_Value_R (LB1) = Expr_Value_R (LB2)
6770 and then
6771 Expr_Value_R (HB1) = Expr_Value_R (HB2);
6773 else
6774 return
6775 Expr_Value (LB1) = Expr_Value (LB2)
6776 and then
6777 Expr_Value (HB1) = Expr_Value (HB2);
6778 end if;
6779 end if;
6780 end;
6782 -- Type with discriminants
6784 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
6786 -- Handle derivations of private subtypes. For example S1 statically
6787 -- matches the full view of T1 in the following example:
6789 -- type T1(<>) is new Root with private;
6790 -- subtype S1 is new T1;
6791 -- overriding proc P1 (P : S1);
6792 -- private
6793 -- type T1 (D : Disc) is new Root with ...
6795 if Ekind (T2) = E_Record_Subtype_With_Private
6796 and then not Has_Discriminants (T2)
6797 and then Partial_View_Has_Unknown_Discr (T1)
6798 and then Etype (T2) = T1
6799 then
6800 return True;
6802 elsif Ekind (T1) = E_Record_Subtype_With_Private
6803 and then not Has_Discriminants (T1)
6804 and then Partial_View_Has_Unknown_Discr (T2)
6805 and then Etype (T1) = T2
6806 then
6807 return True;
6809 -- Because of view exchanges in multiple instantiations, conformance
6810 -- checking might try to match a partial view of a type with no
6811 -- discriminants with a full view that has defaulted discriminants.
6812 -- In such a case, use the discriminant constraint of the full view,
6813 -- which must exist because we know that the two subtypes have the
6814 -- same base type.
6816 elsif Has_Discriminants (T1) /= Has_Discriminants (T2) then
6817 if In_Instance then
6818 if Is_Private_Type (T2)
6819 and then Present (Full_View (T2))
6820 and then Has_Discriminants (Full_View (T2))
6821 then
6822 return Subtypes_Statically_Match (T1, Full_View (T2));
6824 elsif Is_Private_Type (T1)
6825 and then Present (Full_View (T1))
6826 and then Has_Discriminants (Full_View (T1))
6827 then
6828 return Subtypes_Statically_Match (Full_View (T1), T2);
6830 else
6831 return False;
6832 end if;
6833 else
6834 return False;
6835 end if;
6836 end if;
6838 declare
6840 function Original_Discriminant_Constraint
6841 (Typ : Entity_Id) return Elist_Id;
6842 -- Returns Typ's discriminant constraint, or if the constraint
6843 -- is inherited from an ancestor type, then climbs the parent
6844 -- types to locate and return the constraint farthest up the
6845 -- parent chain that Typ's constraint is ultimately inherited
6846 -- from (stopping before a parent that doesn't impose a constraint
6847 -- or a parent that has new discriminants). This ensures a proper
6848 -- result from the equality comparison of Elist_Ids below (as
6849 -- otherwise, derived types that inherit constraints may appear
6850 -- to be unequal, because each level of derivation can have its
6851 -- own copy of the constraint).
6853 function Original_Discriminant_Constraint
6854 (Typ : Entity_Id) return Elist_Id
6856 begin
6857 if not Has_Discriminants (Typ) then
6858 return No_Elist;
6860 -- If Typ is not a derived type, then directly return the
6861 -- its constraint.
6863 elsif not Is_Derived_Type (Typ) then
6864 return Discriminant_Constraint (Typ);
6866 -- If the parent type doesn't have discriminants, doesn't
6867 -- have a constraint, or has new discriminants, then stop
6868 -- and return Typ's constraint.
6870 elsif not Has_Discriminants (Etype (Typ))
6872 -- No constraint on the parent type
6874 or else not Present (Discriminant_Constraint (Etype (Typ)))
6875 or else Is_Empty_Elmt_List
6876 (Discriminant_Constraint (Etype (Typ)))
6878 -- The parent type defines new discriminants
6880 or else
6881 (Is_Base_Type (Etype (Typ))
6882 and then Present (Discriminant_Specifications
6883 (Parent (Etype (Typ)))))
6884 then
6885 return Discriminant_Constraint (Typ);
6887 -- Otherwise, make a recursive call on the parent type
6889 else
6890 return Original_Discriminant_Constraint (Etype (Typ));
6891 end if;
6892 end Original_Discriminant_Constraint;
6894 -- Local variables
6896 DL1 : constant Elist_Id := Original_Discriminant_Constraint (T1);
6897 DL2 : constant Elist_Id := Original_Discriminant_Constraint (T2);
6899 DA1 : Elmt_Id;
6900 DA2 : Elmt_Id;
6902 begin
6903 if DL1 = DL2 then
6904 return True;
6905 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
6906 return False;
6907 end if;
6909 -- Now loop through the discriminant constraints
6911 -- Note: the guard here seems necessary, since it is possible at
6912 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
6914 if Present (DL1) and then Present (DL2) then
6915 DA1 := First_Elmt (DL1);
6916 DA2 := First_Elmt (DL2);
6917 while Present (DA1) loop
6918 declare
6919 Expr1 : constant Node_Id := Node (DA1);
6920 Expr2 : constant Node_Id := Node (DA2);
6922 begin
6923 if not Is_OK_Static_Expression (Expr1)
6924 or else not Is_OK_Static_Expression (Expr2)
6925 then
6926 return False;
6928 -- If either expression raised a Constraint_Error,
6929 -- consider the expressions as matching, since this
6930 -- helps to prevent cascading errors.
6932 elsif Raises_Constraint_Error (Expr1)
6933 or else Raises_Constraint_Error (Expr2)
6934 then
6935 null;
6937 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
6938 return False;
6939 end if;
6940 end;
6942 Next_Elmt (DA1);
6943 Next_Elmt (DA2);
6944 end loop;
6945 end if;
6946 end;
6948 return True;
6950 -- A definite type does not match an indefinite or classwide type.
6951 -- However, a generic type with unknown discriminants may be
6952 -- instantiated with a type with no discriminants, and conformance
6953 -- checking on an inherited operation may compare the actual with the
6954 -- subtype that renames it in the instance.
6956 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
6957 then
6958 return
6959 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
6961 -- Array type
6963 elsif Is_Array_Type (T1) then
6965 -- If either subtype is unconstrained then both must be, and if both
6966 -- are unconstrained then no further checking is needed.
6968 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
6969 return not (Is_Constrained (T1) or else Is_Constrained (T2));
6970 end if;
6972 -- Both subtypes are constrained, so check that the index subtypes
6973 -- statically match.
6975 declare
6976 Index1 : Node_Id := First_Index (T1);
6977 Index2 : Node_Id := First_Index (T2);
6979 begin
6980 while Present (Index1) loop
6981 if not
6982 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6983 then
6984 return False;
6985 end if;
6987 Next_Index (Index1);
6988 Next_Index (Index2);
6989 end loop;
6991 return True;
6992 end;
6994 elsif Is_Access_Type (T1) then
6995 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6996 return False;
6998 elsif Ekind (T1) in E_Access_Subprogram_Type
6999 | E_Anonymous_Access_Subprogram_Type
7000 then
7001 return
7002 Subtype_Conformant
7003 (Designated_Type (T1),
7004 Designated_Type (T2));
7005 else
7006 return
7007 Subtypes_Statically_Match
7008 (Designated_Type (T1),
7009 Designated_Type (T2))
7010 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
7011 end if;
7013 -- All other types definitely match
7015 else
7016 return True;
7017 end if;
7018 end Subtypes_Statically_Match;
7020 ----------
7021 -- Test --
7022 ----------
7024 function Test (Cond : Boolean) return Uint is
7025 begin
7026 if Cond then
7027 return Uint_1;
7028 else
7029 return Uint_0;
7030 end if;
7031 end Test;
7033 ---------------------
7034 -- Test_Comparison --
7035 ---------------------
7037 procedure Test_Comparison
7038 (Op : Node_Id;
7039 Assume_Valid : Boolean;
7040 True_Result : out Boolean;
7041 False_Result : out Boolean)
7043 Left : constant Node_Id := Left_Opnd (Op);
7044 Left_Typ : constant Entity_Id := Etype (Left);
7045 Orig_Op : constant Node_Id := Original_Node (Op);
7047 procedure Replacement_Warning (Msg : String);
7048 -- Emit a warning on a comparison that can be replaced by '='
7050 -------------------------
7051 -- Replacement_Warning --
7052 -------------------------
7054 procedure Replacement_Warning (Msg : String) is
7055 begin
7056 if Constant_Condition_Warnings
7057 and then Comes_From_Source (Orig_Op)
7058 and then Is_Integer_Type (Left_Typ)
7059 and then not Error_Posted (Op)
7060 and then not Has_Warnings_Off (Left_Typ)
7061 and then not In_Instance
7062 then
7063 Error_Msg_N (Msg, Op);
7064 end if;
7065 end Replacement_Warning;
7067 -- Local variables
7069 Res : constant Compare_Result :=
7070 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
7072 -- Start of processing for Test_Comparison
7074 begin
7075 case N_Op_Compare (Nkind (Op)) is
7076 when N_Op_Eq =>
7077 True_Result := Res = EQ;
7078 False_Result := Res = LT or else Res = GT or else Res = NE;
7080 when N_Op_Ge =>
7081 True_Result := Res in Compare_GE;
7082 False_Result := Res = LT;
7084 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
7085 Replacement_Warning
7086 ("can never be greater than, could replace by ""'=""?c?");
7087 end if;
7089 when N_Op_Gt =>
7090 True_Result := Res = GT;
7091 False_Result := Res in Compare_LE;
7093 when N_Op_Le =>
7094 True_Result := Res in Compare_LE;
7095 False_Result := Res = GT;
7097 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
7098 Replacement_Warning
7099 ("can never be less than, could replace by ""'=""?c?");
7100 end if;
7102 when N_Op_Lt =>
7103 True_Result := Res = LT;
7104 False_Result := Res in Compare_GE;
7106 when N_Op_Ne =>
7107 True_Result := Res = NE or else Res = GT or else Res = LT;
7108 False_Result := Res = EQ;
7109 end case;
7110 end Test_Comparison;
7112 ---------------------------------
7113 -- Test_Expression_Is_Foldable --
7114 ---------------------------------
7116 -- One operand case
7118 procedure Test_Expression_Is_Foldable
7119 (N : Node_Id;
7120 Op1 : Node_Id;
7121 Stat : out Boolean;
7122 Fold : out Boolean)
7124 begin
7125 Stat := False;
7126 Fold := False;
7128 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7129 return;
7130 end if;
7132 -- If operand is Any_Type, just propagate to result and do not
7133 -- try to fold, this prevents cascaded errors.
7135 if Etype (Op1) = Any_Type then
7136 Set_Etype (N, Any_Type);
7137 return;
7139 -- If operand raises Constraint_Error, then replace node N with the
7140 -- raise Constraint_Error node, and we are obviously not foldable.
7141 -- Note that this replacement inherits the Is_Static_Expression flag
7142 -- from the operand.
7144 elsif Raises_Constraint_Error (Op1) then
7145 Rewrite_In_Raise_CE (N, Op1);
7146 return;
7148 -- If the operand is not static, then the result is not static, and
7149 -- all we have to do is to check the operand since it is now known
7150 -- to appear in a non-static context.
7152 elsif not Is_Static_Expression (Op1) then
7153 Check_Non_Static_Context (Op1);
7154 Fold := Compile_Time_Known_Value (Op1);
7155 return;
7157 -- An expression of a formal modular type is not foldable because
7158 -- the modulus is unknown.
7160 elsif Is_Modular_Integer_Type (Etype (Op1))
7161 and then Is_Generic_Type (Etype (Op1))
7162 then
7163 Check_Non_Static_Context (Op1);
7164 return;
7166 -- Here we have the case of an operand whose type is OK, which is
7167 -- static, and which does not raise Constraint_Error, we can fold.
7169 else
7170 Set_Is_Static_Expression (N);
7171 Fold := True;
7172 Stat := True;
7173 end if;
7174 end Test_Expression_Is_Foldable;
7176 -- Two operand case
7178 procedure Test_Expression_Is_Foldable
7179 (N : Node_Id;
7180 Op1 : Node_Id;
7181 Op2 : Node_Id;
7182 Stat : out Boolean;
7183 Fold : out Boolean;
7184 CRT_Safe : Boolean := False)
7186 Rstat : constant Boolean := Is_Static_Expression (Op1)
7187 and then
7188 Is_Static_Expression (Op2);
7190 begin
7191 Stat := False;
7192 Fold := False;
7194 -- Inhibit folding if -gnatd.f flag set
7196 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7197 return;
7198 end if;
7200 -- If either operand is Any_Type, just propagate to result and
7201 -- do not try to fold, this prevents cascaded errors.
7203 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
7204 Set_Etype (N, Any_Type);
7205 return;
7207 -- If left operand raises Constraint_Error, then replace node N with the
7208 -- Raise_Constraint_Error node, and we are obviously not foldable.
7209 -- Is_Static_Expression is set from the two operands in the normal way,
7210 -- and we check the right operand if it is in a non-static context.
7212 elsif Raises_Constraint_Error (Op1) then
7213 if not Rstat then
7214 Check_Non_Static_Context (Op2);
7215 end if;
7217 Rewrite_In_Raise_CE (N, Op1);
7218 Set_Is_Static_Expression (N, Rstat);
7219 return;
7221 -- Similar processing for the case of the right operand. Note that we
7222 -- don't use this routine for the short-circuit case, so we do not have
7223 -- to worry about that special case here.
7225 elsif Raises_Constraint_Error (Op2) then
7226 if not Rstat then
7227 Check_Non_Static_Context (Op1);
7228 end if;
7230 Rewrite_In_Raise_CE (N, Op2);
7231 Set_Is_Static_Expression (N, Rstat);
7232 return;
7234 -- Exclude expressions of a generic modular type, as above
7236 elsif Is_Modular_Integer_Type (Etype (Op1))
7237 and then Is_Generic_Type (Etype (Op1))
7238 then
7239 Check_Non_Static_Context (Op1);
7240 return;
7242 -- If result is not static, then check non-static contexts on operands
7243 -- since one of them may be static and the other one may not be static.
7245 elsif not Rstat then
7246 Check_Non_Static_Context (Op1);
7247 Check_Non_Static_Context (Op2);
7249 if CRT_Safe then
7250 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
7251 and then CRT_Safe_Compile_Time_Known_Value (Op2);
7252 else
7253 Fold := Compile_Time_Known_Value (Op1)
7254 and then Compile_Time_Known_Value (Op2);
7255 end if;
7257 if not Fold
7258 and then not Is_Modular_Integer_Type (Etype (N))
7259 then
7260 case Nkind (N) is
7261 when N_Op_And =>
7263 -- (False and XXX) = (XXX and False) = False
7265 Fold :=
7266 (Compile_Time_Known_Value (Op1)
7267 and then Is_False (Expr_Value (Op1))
7268 and then Side_Effect_Free (Op2))
7269 or else (Compile_Time_Known_Value (Op2)
7270 and then Is_False (Expr_Value (Op2))
7271 and then Side_Effect_Free (Op1));
7273 when N_Op_Or =>
7275 -- (True and XXX) = (XXX and True) = True
7277 Fold :=
7278 (Compile_Time_Known_Value (Op1)
7279 and then Is_True (Expr_Value (Op1))
7280 and then Side_Effect_Free (Op2))
7281 or else (Compile_Time_Known_Value (Op2)
7282 and then Is_True (Expr_Value (Op2))
7283 and then Side_Effect_Free (Op1));
7285 when others => null;
7286 end case;
7287 end if;
7289 return;
7291 -- Else result is static and foldable. Both operands are static, and
7292 -- neither raises Constraint_Error, so we can definitely fold.
7294 else
7295 Set_Is_Static_Expression (N);
7296 Fold := True;
7297 Stat := True;
7298 return;
7299 end if;
7300 end Test_Expression_Is_Foldable;
7302 -------------------
7303 -- Test_In_Range --
7304 -------------------
7306 function Test_In_Range
7307 (N : Node_Id;
7308 Typ : Entity_Id;
7309 Assume_Valid : Boolean;
7310 Fixed_Int : Boolean;
7311 Int_Real : Boolean) return Range_Membership
7313 Val : Uint;
7314 Valr : Ureal;
7316 pragma Warnings (Off, Assume_Valid);
7317 -- For now Assume_Valid is unreferenced since the current implementation
7318 -- always returns Unknown if N is not a compile-time-known value, but we
7319 -- keep the parameter to allow for future enhancements in which we try
7320 -- to get the information in the variable case as well.
7322 begin
7323 -- If an error was posted on expression, then return Unknown, we do not
7324 -- want cascaded errors based on some false analysis of a junk node.
7326 if Error_Posted (N) then
7327 return Unknown;
7329 -- Expression that raises Constraint_Error is an odd case. We certainly
7330 -- do not want to consider it to be in range. It might make sense to
7331 -- consider it always out of range, but this causes incorrect error
7332 -- messages about static expressions out of range. So we just return
7333 -- Unknown, which is always safe.
7335 elsif Raises_Constraint_Error (N) then
7336 return Unknown;
7338 -- Universal types have no range limits, so always in range
7340 elsif Is_Universal_Numeric_Type (Typ) then
7341 return In_Range;
7343 -- Never known if not scalar type. Don't know if this can actually
7344 -- happen, but our spec allows it, so we must check.
7346 elsif not Is_Scalar_Type (Typ) then
7347 return Unknown;
7349 -- Never known if this is a generic type, since the bounds of generic
7350 -- types are junk. Note that if we only checked for static expressions
7351 -- (instead of compile-time-known values) below, we would not need this
7352 -- check, because values of a generic type can never be static, but they
7353 -- can be known at compile time.
7355 elsif Is_Generic_Type (Typ) then
7356 return Unknown;
7358 -- Case of a known compile time value, where we can check if it is in
7359 -- the bounds of the given type.
7361 elsif Compile_Time_Known_Value (N) then
7362 declare
7363 Lo : constant Node_Id := Type_Low_Bound (Typ);
7364 Hi : constant Node_Id := Type_High_Bound (Typ);
7365 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
7366 HB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
7368 begin
7369 -- Fixed point types should be considered as such only if flag
7370 -- Fixed_Int is set to False.
7372 if Is_Floating_Point_Type (Typ)
7373 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
7374 or else Int_Real
7375 then
7376 Valr := Expr_Value_R (N);
7378 if LB_Known and HB_Known then
7379 if Valr >= Expr_Value_R (Lo)
7380 and then
7381 Valr <= Expr_Value_R (Hi)
7382 then
7383 return In_Range;
7384 else
7385 return Out_Of_Range;
7386 end if;
7388 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
7389 or else
7390 (HB_Known and then Valr > Expr_Value_R (Hi))
7391 then
7392 return Out_Of_Range;
7394 else
7395 return Unknown;
7396 end if;
7398 else
7399 Val := Expr_Value (N);
7401 if LB_Known and HB_Known then
7402 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
7403 then
7404 return In_Range;
7405 else
7406 return Out_Of_Range;
7407 end if;
7409 elsif (LB_Known and then Val < Expr_Value (Lo))
7410 or else
7411 (HB_Known and then Val > Expr_Value (Hi))
7412 then
7413 return Out_Of_Range;
7415 else
7416 return Unknown;
7417 end if;
7418 end if;
7419 end;
7421 -- Here for value not known at compile time. Case of expression subtype
7422 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
7423 -- In this case we know it is in range without knowing its value.
7425 elsif Assume_Valid
7426 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
7427 then
7428 return In_Range;
7430 -- Another special case. For signed integer types, if the target type
7431 -- has Is_Known_Valid set, and the source type does not have a larger
7432 -- size, then the source value must be in range. We exclude biased
7433 -- types, because they bizarrely can generate out of range values.
7435 elsif Is_Signed_Integer_Type (Etype (N))
7436 and then Is_Known_Valid (Typ)
7437 and then Esize (Etype (N)) <= Esize (Typ)
7438 and then not Has_Biased_Representation (Etype (N))
7439 then
7440 return In_Range;
7442 -- For all other cases, result is unknown
7444 else
7445 return Unknown;
7446 end if;
7447 end Test_In_Range;
7449 --------------
7450 -- To_Bits --
7451 --------------
7453 procedure To_Bits (U : Uint; B : out Bits) is
7454 begin
7455 for J in 0 .. B'Last loop
7456 B (J) := (U / (2 ** J)) mod 2 /= 0;
7457 end loop;
7458 end To_Bits;
7460 --------------------
7461 -- Why_Not_Static --
7462 --------------------
7464 procedure Why_Not_Static (Expr : Node_Id) is
7465 N : constant Node_Id := Original_Node (Expr);
7466 Typ : Entity_Id := Empty;
7467 E : Entity_Id;
7468 Alt : Node_Id;
7469 Exp : Node_Id;
7471 procedure Why_Not_Static_List (L : List_Id);
7472 -- A version that can be called on a list of expressions. Finds all
7473 -- non-static violations in any element of the list.
7475 -------------------------
7476 -- Why_Not_Static_List --
7477 -------------------------
7479 procedure Why_Not_Static_List (L : List_Id) is
7480 N : Node_Id;
7481 begin
7482 if Is_Non_Empty_List (L) then
7483 N := First (L);
7484 while Present (N) loop
7485 Why_Not_Static (N);
7486 Next (N);
7487 end loop;
7488 end if;
7489 end Why_Not_Static_List;
7491 -- Start of processing for Why_Not_Static
7493 begin
7494 -- Ignore call on error or empty node
7496 if No (Expr) or else Nkind (Expr) = N_Error then
7497 return;
7498 end if;
7500 -- Preprocessing for sub expressions
7502 if Nkind (Expr) in N_Subexpr then
7504 -- Nothing to do if expression is static
7506 if Is_OK_Static_Expression (Expr) then
7507 return;
7508 end if;
7510 -- Test for Constraint_Error raised
7512 if Raises_Constraint_Error (Expr) then
7514 -- Special case membership to find out which piece to flag
7516 if Nkind (N) in N_Membership_Test then
7517 if Raises_Constraint_Error (Left_Opnd (N)) then
7518 Why_Not_Static (Left_Opnd (N));
7519 return;
7521 elsif Present (Right_Opnd (N))
7522 and then Raises_Constraint_Error (Right_Opnd (N))
7523 then
7524 Why_Not_Static (Right_Opnd (N));
7525 return;
7527 else
7528 pragma Assert (Present (Alternatives (N)));
7530 Alt := First (Alternatives (N));
7531 while Present (Alt) loop
7532 if Raises_Constraint_Error (Alt) then
7533 Why_Not_Static (Alt);
7534 return;
7535 else
7536 Next (Alt);
7537 end if;
7538 end loop;
7539 end if;
7541 -- Special case a range to find out which bound to flag
7543 elsif Nkind (N) = N_Range then
7544 if Raises_Constraint_Error (Low_Bound (N)) then
7545 Why_Not_Static (Low_Bound (N));
7546 return;
7548 elsif Raises_Constraint_Error (High_Bound (N)) then
7549 Why_Not_Static (High_Bound (N));
7550 return;
7551 end if;
7553 -- Special case attribute to see which part to flag
7555 elsif Nkind (N) = N_Attribute_Reference then
7556 if Raises_Constraint_Error (Prefix (N)) then
7557 Why_Not_Static (Prefix (N));
7558 return;
7559 end if;
7561 if Present (Expressions (N)) then
7562 Exp := First (Expressions (N));
7563 while Present (Exp) loop
7564 if Raises_Constraint_Error (Exp) then
7565 Why_Not_Static (Exp);
7566 return;
7567 end if;
7569 Next (Exp);
7570 end loop;
7571 end if;
7573 -- Special case a subtype name
7575 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
7576 Error_Msg_NE
7577 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
7578 return;
7579 end if;
7581 -- End of special cases
7583 Error_Msg_N
7584 ("!expression raises exception, cannot be static (RM 4.9(34))",
7586 return;
7587 end if;
7589 -- If no type, then something is pretty wrong, so ignore
7591 Typ := Etype (Expr);
7593 if No (Typ) then
7594 return;
7595 end if;
7597 -- Type must be scalar or string type (but allow Bignum, since this
7598 -- is really a scalar type from our point of view in this diagnosis).
7600 if not Is_Scalar_Type (Typ)
7601 and then not Is_String_Type (Typ)
7602 and then not Is_RTE (Typ, RE_Bignum)
7603 then
7604 Error_Msg_N
7605 ("!static expression must have scalar or string type " &
7606 "(RM 4.9(2))", N);
7607 return;
7608 end if;
7609 end if;
7611 -- If we got through those checks, test particular node kind
7613 case Nkind (N) is
7615 -- Entity name
7617 when N_Expanded_Name
7618 | N_Identifier
7619 | N_Operator_Symbol
7621 E := Entity (N);
7623 if Is_Named_Number (E) then
7624 null;
7626 elsif Ekind (E) = E_Constant then
7628 -- One case we can give a better message is when we have a
7629 -- string literal created by concatenating an aggregate with
7630 -- an others expression.
7632 Entity_Case : declare
7633 CV : constant Node_Id := Constant_Value (E);
7634 CO : constant Node_Id := Original_Node (CV);
7636 function Is_Aggregate (N : Node_Id) return Boolean;
7637 -- See if node N came from an others aggregate, if so
7638 -- return True and set Error_Msg_Sloc to aggregate.
7640 ------------------
7641 -- Is_Aggregate --
7642 ------------------
7644 function Is_Aggregate (N : Node_Id) return Boolean is
7645 begin
7646 if Nkind (Original_Node (N)) = N_Aggregate then
7647 Error_Msg_Sloc := Sloc (Original_Node (N));
7648 return True;
7650 elsif Is_Entity_Name (N)
7651 and then Ekind (Entity (N)) = E_Constant
7652 and then
7653 Nkind (Original_Node (Constant_Value (Entity (N)))) =
7654 N_Aggregate
7655 then
7656 Error_Msg_Sloc :=
7657 Sloc (Original_Node (Constant_Value (Entity (N))));
7658 return True;
7660 else
7661 return False;
7662 end if;
7663 end Is_Aggregate;
7665 -- Start of processing for Entity_Case
7667 begin
7668 if Is_Aggregate (CV)
7669 or else (Nkind (CO) = N_Op_Concat
7670 and then (Is_Aggregate (Left_Opnd (CO))
7671 or else
7672 Is_Aggregate (Right_Opnd (CO))))
7673 then
7674 Error_Msg_N ("!aggregate (#) is never static", N);
7676 elsif No (CV) or else not Is_Static_Expression (CV) then
7677 Error_Msg_NE
7678 ("!& is not a static constant (RM 4.9(5))", N, E);
7679 end if;
7680 end Entity_Case;
7682 elsif Is_Type (E) then
7683 Error_Msg_NE
7684 ("!& is not a static subtype (RM 4.9(26))", N, E);
7686 else
7687 Error_Msg_NE
7688 ("!& is not static constant or named number "
7689 & "(RM 4.9(5))", N, E);
7690 end if;
7692 -- Binary operator
7694 when N_Binary_Op
7695 | N_Membership_Test
7696 | N_Short_Circuit
7698 if Nkind (N) in N_Op_Shift then
7699 Error_Msg_N
7700 ("!shift functions are never static (RM 4.9(6,18))", N);
7701 else
7702 Why_Not_Static (Left_Opnd (N));
7703 Why_Not_Static (Right_Opnd (N));
7704 end if;
7706 -- Unary operator
7708 when N_Unary_Op =>
7709 Why_Not_Static (Right_Opnd (N));
7711 -- Attribute reference
7713 when N_Attribute_Reference =>
7714 Why_Not_Static_List (Expressions (N));
7716 E := Etype (Prefix (N));
7718 if E = Standard_Void_Type then
7719 return;
7720 end if;
7722 -- Special case non-scalar'Size since this is a common error
7724 if Attribute_Name (N) = Name_Size then
7725 Error_Msg_N
7726 ("!size attribute is only static for static scalar type "
7727 & "(RM 4.9(7,8))", N);
7729 -- Flag array cases
7731 elsif Is_Array_Type (E) then
7732 if Attribute_Name (N)
7733 not in Name_First | Name_Last | Name_Length
7734 then
7735 Error_Msg_N
7736 ("!static array attribute must be Length, First, or Last "
7737 & "(RM 4.9(8))", N);
7739 -- Since we know the expression is not-static (we already
7740 -- tested for this, must mean array is not static).
7742 else
7743 Error_Msg_N
7744 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
7745 end if;
7747 return;
7749 -- Special case generic types, since again this is a common source
7750 -- of confusion.
7752 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
7753 Error_Msg_N
7754 ("!attribute of generic type is never static "
7755 & "(RM 4.9(7,8))", N);
7757 elsif Is_OK_Static_Subtype (E) then
7758 null;
7760 elsif Is_Scalar_Type (E) then
7761 Error_Msg_N
7762 ("!prefix type for attribute is not static scalar subtype "
7763 & "(RM 4.9(7))", N);
7765 else
7766 Error_Msg_N
7767 ("!static attribute must apply to array/scalar type "
7768 & "(RM 4.9(7,8))", N);
7769 end if;
7771 -- String literal
7773 when N_String_Literal =>
7774 Error_Msg_N
7775 ("!subtype of string literal is non-static (RM 4.9(4))", N);
7777 -- Explicit dereference
7779 when N_Explicit_Dereference =>
7780 Error_Msg_N
7781 ("!explicit dereference is never static (RM 4.9)", N);
7783 -- Function call
7785 when N_Function_Call =>
7786 Why_Not_Static_List (Parameter_Associations (N));
7788 -- Complain about non-static function call unless we have Bignum
7789 -- which means that the underlying expression is really some
7790 -- scalar arithmetic operation.
7792 if not Is_RTE (Typ, RE_Bignum) then
7793 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
7794 end if;
7796 -- Parameter assocation (test actual parameter)
7798 when N_Parameter_Association =>
7799 Why_Not_Static (Explicit_Actual_Parameter (N));
7801 -- Indexed component
7803 when N_Indexed_Component =>
7804 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
7806 -- Procedure call
7808 when N_Procedure_Call_Statement =>
7809 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
7811 -- Qualified expression (test expression)
7813 when N_Qualified_Expression =>
7814 Why_Not_Static (Expression (N));
7816 -- Aggregate
7818 when N_Aggregate
7819 | N_Extension_Aggregate
7821 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
7823 -- Range
7825 when N_Range =>
7826 Why_Not_Static (Low_Bound (N));
7827 Why_Not_Static (High_Bound (N));
7829 -- Range constraint, test range expression
7831 when N_Range_Constraint =>
7832 Why_Not_Static (Range_Expression (N));
7834 -- Subtype indication, test constraint
7836 when N_Subtype_Indication =>
7837 Why_Not_Static (Constraint (N));
7839 -- Selected component
7841 when N_Selected_Component =>
7842 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
7844 -- Slice
7846 when N_Slice =>
7847 Error_Msg_N ("!slice is never static (RM 4.9)", N);
7849 when N_Type_Conversion =>
7850 Why_Not_Static (Expression (N));
7852 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
7853 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
7854 then
7855 Error_Msg_N
7856 ("!static conversion requires static scalar subtype result "
7857 & "(RM 4.9(9))", N);
7858 end if;
7860 -- Unchecked type conversion
7862 when N_Unchecked_Type_Conversion =>
7863 Error_Msg_N
7864 ("!unchecked type conversion is never static (RM 4.9)", N);
7866 -- All other cases, no reason to give
7868 when others =>
7869 null;
7870 end case;
7871 end Why_Not_Static;
7873 end Sem_Eval;