Fix mixed input kind permute optimization
[official-gcc.git] / gcc / ada / sem_eval.adb
blob03006b63070a8c888eb111db93cd3a7cd13eef5d
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-2024, 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_Aggr; use Sem_Aggr;
47 with Sem_Aux; use Sem_Aux;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch6; use Sem_Ch6;
51 with Sem_Ch8; use Sem_Ch8;
52 with Sem_Elab; use Sem_Elab;
53 with Sem_Res; use Sem_Res;
54 with Sem_Util; use Sem_Util;
55 with Sem_Type; use Sem_Type;
56 with Sem_Warn; use Sem_Warn;
57 with Sinfo; use Sinfo;
58 with Sinfo.Nodes; use Sinfo.Nodes;
59 with Sinfo.Utils; use Sinfo.Utils;
60 with Snames; use Snames;
61 with Stand; use Stand;
62 with Stringt; use Stringt;
63 with Tbuild; use Tbuild;
64 with Warnsw; use Warnsw;
66 package body Sem_Eval is
68 -----------------------------------------
69 -- Handling of Compile Time Evaluation --
70 -----------------------------------------
72 -- The compile time evaluation of expressions is distributed over several
73 -- Eval_xxx procedures. These procedures are called immediately after
74 -- a subexpression is resolved and is therefore accomplished in a bottom
75 -- up fashion. The flags are synthesized using the following approach.
77 -- Is_Static_Expression is determined by following the rules in
78 -- RM-4.9. This involves testing the Is_Static_Expression flag of
79 -- the operands in many cases.
81 -- Raises_Constraint_Error is usually set if any of the operands have
82 -- the flag set or if an attempt to compute the value of the current
83 -- expression results in Constraint_Error.
85 -- The general approach is as follows. First compute Is_Static_Expression.
86 -- If the node is not static, then the flag is left off in the node and
87 -- we are all done. Otherwise for a static node, we test if any of the
88 -- operands will raise Constraint_Error, and if so, propagate the flag
89 -- Raises_Constraint_Error to the result node and we are done (since the
90 -- error was already posted at a lower level).
92 -- For the case of a static node whose operands do not raise constraint
93 -- error, we attempt to evaluate the node. If this evaluation succeeds,
94 -- then the node is replaced by the result of this computation. If the
95 -- evaluation raises Constraint_Error, then we rewrite the node with
96 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
97 -- to post appropriate error messages.
99 ----------------
100 -- Local Data --
101 ----------------
103 type Bits is array (Nat range <>) of Boolean;
104 -- Used to convert unsigned (modular) values for folding logical ops
106 -- The following declarations are used to maintain a cache of nodes that
107 -- have compile-time-known values. The cache is maintained only for
108 -- discrete types (the most common case), and is populated by calls to
109 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
110 -- since it is possible for the status to change (in particular it is
111 -- possible for a node to get replaced by a Constraint_Error node).
113 CV_Bits : constant := 5;
114 -- Number of low order bits of Node_Id value used to reference entries
115 -- in the cache table.
117 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
118 -- Size of cache for compile time values
120 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
122 type CV_Entry is record
123 N : Node_Id'Base;
124 -- We use 'Base here, in case we want to add a predicate to Node_Id
125 V : Uint;
126 end record;
128 type Match_Result is (Match, No_Match, Non_Static);
129 -- Result returned from functions that test for a matching result. If the
130 -- operands are not OK_Static then Non_Static will be returned. Otherwise
131 -- Match/No_Match is returned depending on whether the match succeeds.
133 type CV_Cache_Array is array (CV_Range) of CV_Entry;
135 CV_Cache : CV_Cache_Array;
136 -- This is the actual cache, with entries consisting of node/value pairs,
137 -- and the impossible value Node_High_Bound used for unset entries.
139 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
140 -- Range membership may either be statically known to be in range or out
141 -- of range, or not statically known. Used for Test_In_Range below.
143 Checking_For_Potentially_Static_Expression : Boolean := False;
144 -- Global flag that is set True during Analyze_Static_Expression_Function
145 -- in order to verify that the result expression of a static expression
146 -- function is a potentially static function (see RM2022 6.8(5.3)).
148 -----------------------
149 -- Local Subprograms --
150 -----------------------
152 procedure Check_Non_Static_Context_For_Overflow
153 (N : Node_Id;
154 Stat : Boolean;
155 Result : Uint);
156 -- For a signed integer type, check non-static overflow in Result when
157 -- Stat is False. This applies also inside inlined code, where the static
158 -- property may be an effect of the inlining, which should not be allowed
159 -- to remove run-time checks (whether during compilation, or even more
160 -- crucially in the special inlining-for-proof in GNATprove mode).
162 function Choice_Matches
163 (Expr : Node_Id;
164 Choice : Node_Id) return Match_Result;
165 -- Determines whether given value Expr matches the given Choice. The Expr
166 -- can be of discrete, real, or string type and must be a compile time
167 -- known value (it is an error to make the call if these conditions are
168 -- not met). The choice can be a range, subtype name, subtype indication,
169 -- or expression. The returned result is Non_Static if Choice is not
170 -- OK_Static, otherwise either Match or No_Match is returned depending
171 -- on whether Choice matches Expr. This is used for case expression
172 -- alternatives, and also for membership tests. In each case, more
173 -- possibilities are tested than the syntax allows (e.g. membership allows
174 -- subtype indications and non-discrete types, and case allows an OTHERS
175 -- choice), but it does not matter, since we have already done a full
176 -- semantic and syntax check of the construct, so the extra possibilities
177 -- just will not arise for correct expressions.
179 -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g
180 -- a reference to a type, one of whose bounds raises Constraint_Error, then
181 -- it also sets the Raises_Constraint_Error flag on the Choice itself.
183 function Choices_Match
184 (Expr : Node_Id;
185 Choices : List_Id) return Match_Result;
186 -- This function applies Choice_Matches to each element of Choices. If the
187 -- result is No_Match, then it continues and checks the next element. If
188 -- the result is Match or Non_Static, this result is immediately given
189 -- as the result without checking the rest of the list. Expr can be of
190 -- discrete, real, or string type and must be a compile-time-known value
191 -- (it is an error to make the call if these conditions are not met).
193 procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id);
194 -- Evaluate a call N to an intrinsic subprogram E.
196 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
197 -- Check whether an arithmetic operation with universal operands which is a
198 -- rewritten function call with an explicit scope indication is ambiguous:
199 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
200 -- type declared in P and the context does not impose a type on the result
201 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
202 -- error and return Empty, else return the result type of the operator.
204 procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id);
205 -- Rewrite N as a constant dummy value in the relevant type if possible.
207 procedure Fold_Shift
208 (N : Node_Id;
209 Left : Node_Id;
210 Right : Node_Id;
211 Op : Node_Kind;
212 Static : Boolean := False;
213 Check_Elab : Boolean := False);
214 -- Rewrite N as the result of evaluating Left <shift op> Right if possible.
215 -- Op represents the shift operation.
216 -- Static indicates whether the resulting node should be marked static.
217 -- Check_Elab indicates whether checks for elaboration calls should be
218 -- inserted when relevant.
220 function From_Bits (B : Bits; T : Entity_Id) return Uint;
221 -- Converts a bit string of length B'Length to a Uint value to be used for
222 -- a target of type T, which is a modular type. This procedure includes the
223 -- necessary reduction by the modulus in the case of a nonbinary modulus
224 -- (for a binary modulus, the bit string is the right length any way so all
225 -- is well).
227 function Get_String_Val (N : Node_Id) return Node_Id;
228 -- Given a tree node for a folded string or character value, returns the
229 -- corresponding string literal or character literal (one of the two must
230 -- be available, or the operand would not have been marked as foldable in
231 -- the earlier analysis of the operation).
233 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean;
234 -- Given a choice (from a case expression or membership test), returns
235 -- True if the choice is static and does not raise a Constraint_Error.
237 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean;
238 -- Given a choice list (from a case expression or membership test), return
239 -- True if all choices are static in the sense of Is_OK_Static_Choice.
241 function Is_Static_Choice (Choice : Node_Id) return Boolean;
242 -- Given a choice (from a case expression or membership test), returns
243 -- True if the choice is static. No test is made for raising of constraint
244 -- error, so this function is used only for legality tests.
246 function Is_Static_Choice_List (Choices : List_Id) return Boolean;
247 -- Given a choice list (from a case expression or membership test), return
248 -- True if all choices are static in the sense of Is_Static_Choice.
250 function Is_Static_Range (N : Node_Id) return Boolean;
251 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
252 -- argument is an N_Range node (but note that the semantic analysis of
253 -- equivalent range attribute references already turned them into the
254 -- equivalent range). This differs from Is_OK_Static_Range (which is what
255 -- must be used by clients) in that it does not care whether the bounds
256 -- raise Constraint_Error or not. Used for checking whether expressions are
257 -- static in the 4.9 sense (without worrying about exceptions).
259 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
260 -- Bits represents the number of bits in an integer value to be computed
261 -- (but the value has not been computed yet). If this value in Bits is
262 -- reasonable, a result of True is returned, with the implication that the
263 -- caller should go ahead and complete the calculation. If the value in
264 -- Bits is unreasonably large, then an error is posted on node N, and
265 -- False is returned (and the caller skips the proposed calculation).
267 procedure Out_Of_Range (N : Node_Id);
268 -- This procedure is called if it is determined that node N, which appears
269 -- in a non-static context, is a compile-time-known value which is outside
270 -- its range, i.e. the range of Etype. This is used in contexts where
271 -- this is an illegality if N is static, and should generate a warning
272 -- otherwise.
274 function Real_Or_String_Static_Predicate_Matches
275 (Val : Node_Id;
276 Typ : Entity_Id) return Boolean;
277 -- This is the function used to evaluate real or string static predicates.
278 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
279 -- represents the value to be tested against the predicate. Typ is the
280 -- type with the predicate, from which the predicate expression can be
281 -- extracted. The result returned is True if the given value satisfies
282 -- the predicate.
284 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
285 -- N and Exp are nodes representing an expression, Exp is known to raise
286 -- CE. N is rewritten in term of Exp in the optimal way.
288 function String_Type_Len (Stype : Entity_Id) return Uint;
289 -- Given a string type, determines the length of the index type, or, if
290 -- this index type is non-static, the length of the base type of this index
291 -- type. Note that if the string type is itself static, then the index type
292 -- is static, so the second case applies only if the string type passed is
293 -- non-static.
295 function Test (Cond : Boolean) return Uint;
296 pragma Inline (Test);
297 -- This function simply returns the appropriate Boolean'Pos value
298 -- corresponding to the value of Cond as a universal integer. It is
299 -- used for producing the result of the static evaluation of the
300 -- logical operators
302 procedure Test_Expression_Is_Foldable
303 (N : Node_Id;
304 Op1 : Node_Id;
305 Stat : out Boolean;
306 Fold : out Boolean);
307 -- Tests to see if expression N whose single operand is Op1 is foldable,
308 -- i.e. the operand value is known at compile time. If the operation is
309 -- foldable, then Fold is True on return, and Stat indicates whether the
310 -- result is static (i.e. the operand was static). Note that it is quite
311 -- possible for Fold to be True, and Stat to be False, since there are
312 -- cases in which we know the value of an operand even though it is not
313 -- technically static (e.g. the static lower bound of a range whose upper
314 -- bound is non-static).
316 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes
317 -- a call to Check_Non_Static_Context on the operand. If Fold is False on
318 -- return, then all processing is complete, and the caller should return,
319 -- since there is nothing else to do.
321 -- If Stat is set True on return, then Is_Static_Expression is also set
322 -- true in node N. There are some cases where this is over-enthusiastic,
323 -- e.g. in the two operand case below, for string comparison, the result is
324 -- not static even though the two operands are static. In such cases, the
325 -- caller must reset the Is_Static_Expression flag in N.
327 -- If Fold and Stat are both set to False then this routine performs also
328 -- the following extra actions:
330 -- If either operand is Any_Type then propagate it to result to prevent
331 -- cascaded errors.
333 -- If some operand raises Constraint_Error, then replace the node N
334 -- with the raise Constraint_Error node. This replacement inherits the
335 -- Is_Static_Expression flag from the operands.
337 procedure Test_Expression_Is_Foldable
338 (N : Node_Id;
339 Op1 : Node_Id;
340 Op2 : Node_Id;
341 Stat : out Boolean;
342 Fold : out Boolean;
343 CRT_Safe : Boolean := False);
344 -- Same processing, except applies to an expression N with two operands
345 -- Op1 and Op2. The result is static only if both operands are static. If
346 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
347 -- for the tests that the two operands are known at compile time. See
348 -- spec of this routine for further details.
350 function Test_In_Range
351 (N : Node_Id;
352 Typ : Entity_Id;
353 Assume_Valid : Boolean;
354 Fixed_Int : Boolean;
355 Int_Real : Boolean) return Range_Membership;
356 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
357 -- or Out_Of_Range if it can be guaranteed at compile time that expression
358 -- N is known to be in or out of range of the subtype Typ. If not compile
359 -- time known, Unknown is returned. See documentation of Is_In_Range for
360 -- complete description of parameters.
362 procedure To_Bits (U : Uint; B : out Bits);
363 -- Converts a Uint value to a bit string of length B'Length
365 -----------------------------------------------
366 -- Check_Expression_Against_Static_Predicate --
367 -----------------------------------------------
369 procedure Check_Expression_Against_Static_Predicate
370 (Expr : Node_Id;
371 Typ : Entity_Id;
372 Static_Failure_Is_Error : Boolean := False)
374 begin
375 -- Nothing to do if expression is not known at compile time, or the
376 -- type has no static predicate set (will be the case for all non-scalar
377 -- types, so no need to make a special test for that).
379 if not (Has_Static_Predicate (Typ)
380 and then Compile_Time_Known_Value (Expr))
381 then
382 return;
383 end if;
385 -- Here we have a static predicate (note that it could have arisen from
386 -- an explicitly specified Dynamic_Predicate whose expression met the
387 -- rules for being predicate-static). If the expression is known at
388 -- compile time and obeys the predicate, then it is static and must be
389 -- labeled as such, which matters e.g. for case statements. The original
390 -- expression may be a type conversion of a variable with a known value,
391 -- which might otherwise not be marked static.
393 -- Case of real static predicate
395 if Is_Real_Type (Typ) then
396 if Real_Or_String_Static_Predicate_Matches
397 (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)),
398 Typ => Typ)
399 then
400 Set_Is_Static_Expression (Expr);
401 return;
402 end if;
404 -- Case of string static predicate
406 elsif Is_String_Type (Typ) then
407 if Real_Or_String_Static_Predicate_Matches
408 (Val => Expr_Value_S (Expr), Typ => Typ)
409 then
410 Set_Is_Static_Expression (Expr);
411 return;
412 end if;
414 -- Case of discrete static predicate
416 else
417 pragma Assert (Is_Discrete_Type (Typ));
419 -- If static predicate matches, nothing to do
421 if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then
422 Set_Is_Static_Expression (Expr);
423 return;
424 end if;
425 end if;
427 -- Here we know that the predicate will fail
429 -- Special case of static expression failing a predicate (other than one
430 -- that was explicitly specified with a Dynamic_Predicate aspect). If
431 -- the expression comes from a qualified_expression or type_conversion
432 -- this is an error (Static_Failure_Is_Error); otherwise we only issue
433 -- a warning and the expression is no longer considered static.
435 if Is_Static_Expression (Expr)
436 and then not Has_Dynamic_Predicate_Aspect (Typ)
437 and then not Has_Ghost_Predicate_Aspect (Typ)
438 then
439 if Static_Failure_Is_Error then
440 Error_Msg_NE
441 ("static expression fails static predicate check on &",
442 Expr, Typ);
444 else
445 Error_Msg_NE
446 ("??static expression fails static predicate check on &",
447 Expr, Typ);
448 Error_Msg_N
449 ("\??expression is no longer considered static", Expr);
451 Set_Is_Static_Expression (Expr, False);
452 end if;
454 -- In all other cases, this is just a warning that a test will fail.
455 -- It does not matter if the expression is static or not, or if the
456 -- predicate comes from a dynamic predicate aspect or not.
458 else
459 Error_Msg_NE
460 ("??expression fails predicate check on &", Expr, Typ);
462 -- Force a check here, which is potentially a redundant check, but
463 -- this ensures a check will be done in cases where the expression
464 -- is folded, and since this is definitely a failure, extra checks
465 -- are OK.
467 if Predicate_Enabled (Typ) then
468 Insert_Action (Expr,
469 Make_Predicate_Check
470 (Typ, Duplicate_Subexpr (Expr)), Suppress => All_Checks);
471 end if;
472 end if;
473 end Check_Expression_Against_Static_Predicate;
475 ------------------------------
476 -- Check_Non_Static_Context --
477 ------------------------------
479 procedure Check_Non_Static_Context (N : Node_Id) is
480 T : constant Entity_Id := Etype (N);
481 Checks_On : constant Boolean :=
482 not Index_Checks_Suppressed (T)
483 and not Range_Checks_Suppressed (T);
485 begin
486 -- Ignore cases of non-scalar types, error types, or universal real
487 -- types that have no usable bounds.
489 if T = Any_Type
490 or else not Is_Scalar_Type (T)
491 or else T = Universal_Fixed
492 or else T = Universal_Real
493 then
494 return;
495 end if;
497 -- At this stage we have a scalar type. If we have an expression that
498 -- raises CE, then we already issued a warning or error msg so there is
499 -- nothing more to be done in this routine.
501 if Raises_Constraint_Error (N) then
502 return;
503 end if;
505 -- Now we have a scalar type which is not marked as raising a constraint
506 -- error exception. The main purpose of this routine is to deal with
507 -- static expressions appearing in a non-static context. That means
508 -- that if we do not have a static expression then there is not much
509 -- to do. The one case that we deal with here is that if we have a
510 -- floating-point value that is out of range, then we post a warning
511 -- that an infinity will result.
513 if not Is_Static_Expression (N) then
514 if Is_Floating_Point_Type (T) then
515 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
516 Error_Msg_N
517 ("??float value out of range, infinity will be generated", N);
519 -- The literal may be the result of constant-folding of a non-
520 -- static subexpression of a larger expression (e.g. a conversion
521 -- of a non-static variable whose value happens to be known). At
522 -- this point we must reduce the value of the subexpression to a
523 -- machine number (RM 4.9 (38/2)).
525 elsif Nkind (N) = N_Real_Literal
526 and then Nkind (Parent (N)) in N_Subexpr
527 then
528 Rewrite (N, New_Copy (N));
529 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
530 Set_Is_Machine_Number (N);
531 end if;
532 end if;
534 return;
535 end if;
537 -- Here we have the case of outer level static expression of scalar
538 -- type, where the processing of this procedure is needed.
540 -- For real types, this is where we convert the value to a machine
541 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
542 -- need to do this if the parent is a constant declaration, since in
543 -- other cases, gigi should do the necessary conversion correctly, but
544 -- experimentation shows that this is not the case on all machines, in
545 -- particular if we do not convert all literals to machine values in
546 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
547 -- and SGI/Irix.
549 -- This conversion is always done by GNATprove on real literals in
550 -- non-static expressions, by calling Check_Non_Static_Context from
551 -- gnat2why, as GNATprove cannot do the conversion later contrary
552 -- to gigi. The frontend computes the information about which
553 -- expressions are static, which is used by gnat2why to call
554 -- Check_Non_Static_Context on exactly those real literals that are
555 -- not subexpressions of static expressions.
557 if Nkind (N) = N_Real_Literal
558 and then not Is_Machine_Number (N)
559 and then not Is_Generic_Type (Etype (N))
560 and then Etype (N) /= Universal_Real
561 then
562 -- Check that value is in bounds before converting to machine
563 -- number, so as not to lose case where value overflows in the
564 -- least significant bit or less. See B490001.
566 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
567 Out_Of_Range (N);
568 return;
569 end if;
571 -- Note: we have to copy the node, to avoid problems with conformance
572 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
574 Rewrite (N, New_Copy (N));
576 if not Is_Floating_Point_Type (T) then
577 Set_Realval
578 (N, Corresponding_Integer_Value (N) * Small_Value (T));
580 elsif not UR_Is_Zero (Realval (N)) then
581 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
582 Set_Is_Machine_Number (N);
583 end if;
585 end if;
587 -- Check for out of range universal integer. This is a non-static
588 -- context, so the integer value must be in range of the runtime
589 -- representation of universal integers.
591 -- We do this only within an expression, because that is the only
592 -- case in which non-static universal integer values can occur, and
593 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
594 -- called in contexts like the expression of a number declaration where
595 -- we certainly want to allow out of range values.
597 -- We inhibit the warning when expansion is disabled, because the
598 -- preanalysis of a range of a 64-bit modular type may appear to
599 -- violate the constraint on non-static Universal_Integer. If there
600 -- is a true overflow it will be diagnosed during full analysis.
602 if Etype (N) = Universal_Integer
603 and then Nkind (N) = N_Integer_Literal
604 and then Nkind (Parent (N)) in N_Subexpr
605 and then Expander_Active
606 and then
607 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
608 or else
609 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
610 then
611 Apply_Compile_Time_Constraint_Error
612 (N, "non-static universal integer value out of range<<",
613 CE_Range_Check_Failed);
615 -- Check out of range of base type
617 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
618 Out_Of_Range (N);
620 -- Give a warning or error on the value outside the subtype. A warning
621 -- is omitted if the expression appears in a range that could be null
622 -- (warnings are handled elsewhere for this case).
624 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
625 if Is_In_Range (N, T, Assume_Valid => True) then
626 null;
628 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
629 -- Ignore out of range values for System.Priority in CodePeer
630 -- mode since the actual target compiler may provide a wider
631 -- range.
633 if CodePeer_Mode and then Is_RTE (T, RE_Priority) then
634 Set_Do_Range_Check (N, False);
636 -- Determine if the out-of-range violation constitutes a warning
637 -- or an error based on context, according to RM 4.9 (34/3).
639 elsif Nkind (Original_Node (N)) in
640 N_Type_Conversion | N_Qualified_Expression
641 and then Comes_From_Source (Original_Node (N))
642 then
643 Apply_Compile_Time_Constraint_Error
644 (N, "value not in range of}", CE_Range_Check_Failed);
645 else
646 Apply_Compile_Time_Constraint_Error
647 (N, "value not in range of}<<", CE_Range_Check_Failed);
648 end if;
650 elsif Checks_On then
651 Enable_Range_Check (N);
653 else
654 Set_Do_Range_Check (N, False);
655 end if;
656 end if;
657 end Check_Non_Static_Context;
659 -------------------------------------------
660 -- Check_Non_Static_Context_For_Overflow --
661 -------------------------------------------
663 procedure Check_Non_Static_Context_For_Overflow
664 (N : Node_Id;
665 Stat : Boolean;
666 Result : Uint)
668 begin
669 if (not Stat or else In_Inlined_Body)
670 and then Is_Signed_Integer_Type (Etype (N))
671 then
672 declare
673 BT : constant Entity_Id := Base_Type (Etype (N));
674 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
675 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
676 begin
677 if Result < Lo or else Result > Hi then
678 Apply_Compile_Time_Constraint_Error
679 (N, "value not in range of }??",
680 CE_Overflow_Check_Failed,
681 Ent => BT);
682 end if;
683 end;
684 end if;
685 end Check_Non_Static_Context_For_Overflow;
687 ---------------------------------
688 -- Check_String_Literal_Length --
689 ---------------------------------
691 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
692 begin
693 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
694 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
695 then
696 Apply_Compile_Time_Constraint_Error
697 (N, "string length wrong for}??",
698 CE_Length_Check_Failed,
699 Ent => Ttype,
700 Typ => Ttype);
701 end if;
702 end if;
703 end Check_String_Literal_Length;
705 --------------------------------------------
706 -- Checking_Potentially_Static_Expression --
707 --------------------------------------------
709 function Checking_Potentially_Static_Expression return Boolean is
710 begin
711 return Checking_For_Potentially_Static_Expression;
712 end Checking_Potentially_Static_Expression;
714 --------------------
715 -- Choice_Matches --
716 --------------------
718 function Choice_Matches
719 (Expr : Node_Id;
720 Choice : Node_Id) return Match_Result
722 Etyp : constant Entity_Id := Etype (Expr);
723 Val : Uint;
724 ValR : Ureal;
725 ValS : Node_Id;
727 begin
728 pragma Assert (Compile_Time_Known_Value (Expr));
729 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
731 if not Is_OK_Static_Choice (Choice) then
732 Set_Raises_Constraint_Error (Choice);
733 return Non_Static;
735 -- When the choice denotes a subtype with a static predictate, check the
736 -- expression against the predicate values. Different procedures apply
737 -- to discrete and non-discrete types.
739 elsif (Nkind (Choice) = N_Subtype_Indication
740 or else (Is_Entity_Name (Choice)
741 and then Is_Type (Entity (Choice))))
742 and then Has_Predicates (Etype (Choice))
743 and then Has_Static_Predicate (Etype (Choice))
744 then
745 if Is_Discrete_Type (Etype (Choice)) then
746 return
747 Choices_Match
748 (Expr, Static_Discrete_Predicate (Etype (Choice)));
750 elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice))
751 then
752 return Match;
754 else
755 return No_Match;
756 end if;
758 -- Discrete type case only
760 elsif Is_Discrete_Type (Etyp) then
761 Val := Expr_Value (Expr);
763 if Nkind (Choice) = N_Range then
764 if Val >= Expr_Value (Low_Bound (Choice))
765 and then
766 Val <= Expr_Value (High_Bound (Choice))
767 then
768 return Match;
769 else
770 return No_Match;
771 end if;
773 elsif Nkind (Choice) = N_Subtype_Indication
774 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
775 then
776 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
777 and then
778 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
779 then
780 return Match;
781 else
782 return No_Match;
783 end if;
785 elsif Nkind (Choice) = N_Others_Choice then
786 return Match;
788 else
789 if Val = Expr_Value (Choice) then
790 return Match;
791 else
792 return No_Match;
793 end if;
794 end if;
796 -- Real type case
798 elsif Is_Real_Type (Etyp) then
799 ValR := Expr_Value_R (Expr);
801 if Nkind (Choice) = N_Range then
802 if ValR >= Expr_Value_R (Low_Bound (Choice))
803 and then
804 ValR <= Expr_Value_R (High_Bound (Choice))
805 then
806 return Match;
807 else
808 return No_Match;
809 end if;
811 elsif Nkind (Choice) = N_Subtype_Indication
812 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
813 then
814 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
815 and then
816 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
817 then
818 return Match;
819 else
820 return No_Match;
821 end if;
823 else
824 if ValR = Expr_Value_R (Choice) then
825 return Match;
826 else
827 return No_Match;
828 end if;
829 end if;
831 -- String type cases
833 else
834 pragma Assert (Is_String_Type (Etyp));
835 ValS := Expr_Value_S (Expr);
837 if Nkind (Choice) = N_Subtype_Indication
838 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
839 then
840 if not Is_Constrained (Etype (Choice)) then
841 return Match;
843 else
844 declare
845 Typlen : constant Uint :=
846 String_Type_Len (Etype (Choice));
847 Strlen : constant Uint :=
848 UI_From_Int (String_Length (Strval (ValS)));
849 begin
850 if Typlen = Strlen then
851 return Match;
852 else
853 return No_Match;
854 end if;
855 end;
856 end if;
858 else
859 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
860 then
861 return Match;
862 else
863 return No_Match;
864 end if;
865 end if;
866 end if;
867 end Choice_Matches;
869 -------------------
870 -- Choices_Match --
871 -------------------
873 function Choices_Match
874 (Expr : Node_Id;
875 Choices : List_Id) return Match_Result
877 Choice : Node_Id;
878 Result : Match_Result;
880 begin
881 Choice := First (Choices);
882 while Present (Choice) loop
883 Result := Choice_Matches (Expr, Choice);
885 if Result /= No_Match then
886 return Result;
887 end if;
889 Next (Choice);
890 end loop;
892 return No_Match;
893 end Choices_Match;
895 --------------------------
896 -- Compile_Time_Compare --
897 --------------------------
899 function Compile_Time_Compare
900 (L, R : Node_Id;
901 Assume_Valid : Boolean) return Compare_Result
903 Discard : aliased Uint;
904 begin
905 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
906 end Compile_Time_Compare;
908 function Compile_Time_Compare
909 (L, R : Node_Id;
910 Diff : access Uint;
911 Assume_Valid : Boolean;
912 Rec : Boolean := False) return Compare_Result
914 Ltyp : Entity_Id := Etype (L);
915 Rtyp : Entity_Id := Etype (R);
917 Discard : aliased Uint;
919 procedure Compare_Decompose
920 (N : Node_Id;
921 R : out Node_Id;
922 V : out Uint);
923 -- This procedure decomposes the node N into an expression node and a
924 -- signed offset, so that the value of N is equal to the value of R plus
925 -- the value V (which may be negative). If no such decomposition is
926 -- possible, then on return R is a copy of N, and V is set to zero.
928 function Compare_Fixup (N : Node_Id) return Node_Id;
929 -- This function deals with replacing 'Last and 'First references with
930 -- their corresponding type bounds, which we then can compare. The
931 -- argument is the original node, the result is the identity, unless we
932 -- have a 'Last/'First reference in which case the value returned is the
933 -- appropriate type bound.
935 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
936 -- Even if the context does not assume that values are valid, some
937 -- simple cases can be recognized.
939 function Is_Same_Value (L, R : Node_Id) return Boolean;
940 -- Returns True iff L and R represent expressions that definitely have
941 -- identical (but not necessarily compile-time-known) values Indeed the
942 -- caller is expected to have already dealt with the cases of compile
943 -- time known values, so these are not tested here.
945 -----------------------
946 -- Compare_Decompose --
947 -----------------------
949 procedure Compare_Decompose
950 (N : Node_Id;
951 R : out Node_Id;
952 V : out Uint)
954 begin
955 if Nkind (N) = N_Op_Add
956 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
957 then
958 R := Left_Opnd (N);
959 V := Intval (Right_Opnd (N));
960 return;
962 elsif Nkind (N) = N_Op_Subtract
963 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
964 then
965 R := Left_Opnd (N);
966 V := UI_Negate (Intval (Right_Opnd (N)));
967 return;
969 elsif Nkind (N) = N_Attribute_Reference then
970 if Attribute_Name (N) = Name_Succ then
971 R := First (Expressions (N));
972 V := Uint_1;
973 return;
975 elsif Attribute_Name (N) = Name_Pred then
976 R := First (Expressions (N));
977 V := Uint_Minus_1;
978 return;
979 end if;
980 end if;
982 R := N;
983 V := Uint_0;
984 end Compare_Decompose;
986 -------------------
987 -- Compare_Fixup --
988 -------------------
990 function Compare_Fixup (N : Node_Id) return Node_Id is
991 Indx : Node_Id;
992 Xtyp : Entity_Id;
993 Subs : Nat;
995 begin
996 -- Fixup only required for First/Last attribute reference
998 if Nkind (N) = N_Attribute_Reference
999 and then Attribute_Name (N) in Name_First | Name_Last
1000 then
1001 Xtyp := Etype (Prefix (N));
1003 -- If we have no type, then just abandon the attempt to do
1004 -- a fixup, this is probably the result of some other error.
1006 if No (Xtyp) then
1007 return N;
1008 end if;
1010 -- Dereference an access type
1012 if Is_Access_Type (Xtyp) then
1013 Xtyp := Designated_Type (Xtyp);
1014 end if;
1016 -- If we don't have an array type at this stage, something is
1017 -- peculiar, e.g. another error, and we abandon the attempt at
1018 -- a fixup.
1020 if not Is_Array_Type (Xtyp) then
1021 return N;
1022 end if;
1024 -- Ignore unconstrained array, since bounds are not meaningful
1026 if not Is_Constrained (Xtyp) then
1027 return N;
1028 end if;
1030 if Ekind (Xtyp) = E_String_Literal_Subtype then
1031 if Attribute_Name (N) = Name_First then
1032 return String_Literal_Low_Bound (Xtyp);
1033 else
1034 return
1035 Make_Integer_Literal (Sloc (N),
1036 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
1037 String_Literal_Length (Xtyp));
1038 end if;
1039 end if;
1041 -- Find correct index type
1043 Indx := First_Index (Xtyp);
1045 if Present (Expressions (N)) then
1046 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
1048 for J in 2 .. Subs loop
1049 Next_Index (Indx);
1050 end loop;
1051 end if;
1053 Xtyp := Etype (Indx);
1055 if Attribute_Name (N) = Name_First then
1056 return Type_Low_Bound (Xtyp);
1057 else
1058 return Type_High_Bound (Xtyp);
1059 end if;
1060 end if;
1062 return N;
1063 end Compare_Fixup;
1065 ----------------------------
1066 -- Is_Known_Valid_Operand --
1067 ----------------------------
1069 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
1070 begin
1071 return (Is_Entity_Name (Opnd)
1072 and then
1073 (Is_Known_Valid (Entity (Opnd))
1074 or else Ekind (Entity (Opnd)) = E_In_Parameter
1075 or else
1076 (Is_Object (Entity (Opnd))
1077 and then Present (Current_Value (Entity (Opnd))))))
1078 or else Is_OK_Static_Expression (Opnd);
1079 end Is_Known_Valid_Operand;
1081 -------------------
1082 -- Is_Same_Value --
1083 -------------------
1085 function Is_Same_Value (L, R : Node_Id) return Boolean is
1086 Lf : constant Node_Id := Compare_Fixup (L);
1087 Rf : constant Node_Id := Compare_Fixup (R);
1089 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean;
1090 -- An attribute reference to Loop_Entry may have been rewritten into
1091 -- its prefix as a way to avoid generating a constant for that
1092 -- attribute when the corresponding pragma is ignored. These nodes
1093 -- should be ignored when deciding if they can be equal to one
1094 -- another.
1096 function Is_Same_Subscript (L, R : List_Id) return Boolean;
1097 -- L, R are the Expressions values from two attribute nodes for First
1098 -- or Last attributes. Either may be set to No_List if no expressions
1099 -- are present (indicating subscript 1). The result is True if both
1100 -- expressions represent the same subscript (note one case is where
1101 -- one subscript is missing and the other is explicitly set to 1).
1103 -----------------------------
1104 -- Is_Rewritten_Loop_Entry --
1105 -----------------------------
1107 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean is
1108 Orig_N : constant Node_Id := Original_Node (N);
1109 begin
1110 return Orig_N /= N
1111 and then Nkind (Orig_N) = N_Attribute_Reference
1112 and then Get_Attribute_Id (Attribute_Name (Orig_N)) =
1113 Attribute_Loop_Entry;
1114 end Is_Rewritten_Loop_Entry;
1116 -----------------------
1117 -- Is_Same_Subscript --
1118 -----------------------
1120 function Is_Same_Subscript (L, R : List_Id) return Boolean is
1121 begin
1122 if L = No_List then
1123 if R = No_List then
1124 return True;
1125 else
1126 return Expr_Value (First (R)) = Uint_1;
1127 end if;
1129 else
1130 if R = No_List then
1131 return Expr_Value (First (L)) = Uint_1;
1132 else
1133 return Expr_Value (First (L)) = Expr_Value (First (R));
1134 end if;
1135 end if;
1136 end Is_Same_Subscript;
1138 -- Start of processing for Is_Same_Value
1140 begin
1141 -- Loop_Entry nodes rewritten into their prefix inside ignored
1142 -- pragmas should never lead to a decision of equality.
1144 if Is_Rewritten_Loop_Entry (Lf)
1145 or else Is_Rewritten_Loop_Entry (Rf)
1146 then
1147 return False;
1149 -- Values are the same if they refer to the same entity and the
1150 -- entity is nonvolatile.
1152 elsif Nkind (Lf) in N_Identifier | N_Expanded_Name
1153 and then Nkind (Rf) in N_Identifier | N_Expanded_Name
1154 and then Entity (Lf) = Entity (Rf)
1156 -- If the entity is a discriminant, the two expressions may be
1157 -- bounds of components of objects of the same discriminated type.
1158 -- The values of the discriminants are not static, and therefore
1159 -- the result is unknown.
1161 and then Ekind (Entity (Lf)) /= E_Discriminant
1162 and then Present (Entity (Lf))
1164 -- This does not however apply to Float types, since we may have
1165 -- two NaN values and they should never compare equal.
1167 and then not Is_Floating_Point_Type (Etype (L))
1168 and then not Is_Volatile_Reference (L)
1169 and then not Is_Volatile_Reference (R)
1170 then
1171 return True;
1173 -- Or if they are compile-time-known and identical
1175 elsif Compile_Time_Known_Value (Lf)
1176 and then
1177 Compile_Time_Known_Value (Rf)
1178 and then Expr_Value (Lf) = Expr_Value (Rf)
1179 then
1180 return True;
1182 -- False if Nkind of the two nodes is different for remaining cases
1184 elsif Nkind (Lf) /= Nkind (Rf) then
1185 return False;
1187 -- True if both 'First or 'Last values applying to the same entity
1188 -- (first and last don't change even if value does). Note that we
1189 -- need this even with the calls to Compare_Fixup, to handle the
1190 -- case of unconstrained array attributes where Compare_Fixup
1191 -- cannot find useful bounds.
1193 elsif Nkind (Lf) = N_Attribute_Reference
1194 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1195 and then Attribute_Name (Lf) in Name_First | Name_Last
1196 and then Nkind (Prefix (Lf)) in N_Identifier | N_Expanded_Name
1197 and then Nkind (Prefix (Rf)) in N_Identifier | N_Expanded_Name
1198 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1199 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1200 then
1201 return True;
1203 -- True if the same selected component from the same record
1205 elsif Nkind (Lf) = N_Selected_Component
1206 and then Selector_Name (Lf) = Selector_Name (Rf)
1207 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1208 then
1209 return True;
1211 -- True if the same unary operator applied to the same operand
1213 elsif Nkind (Lf) in N_Unary_Op
1214 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1215 then
1216 return True;
1218 -- True if the same binary operator applied to the same operands
1220 elsif Nkind (Lf) in N_Binary_Op
1221 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1222 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1223 then
1224 return True;
1226 -- All other cases, we can't tell, so return False
1228 else
1229 return False;
1230 end if;
1231 end Is_Same_Value;
1233 -- Start of processing for Compile_Time_Compare
1235 begin
1236 Diff.all := No_Uint;
1238 -- In preanalysis mode, always return Unknown unless the expression
1239 -- is static. It is too early to be thinking we know the result of a
1240 -- comparison, save that judgment for the full analysis. This is
1241 -- particularly important in the case of pre and postconditions, which
1242 -- otherwise can be prematurely collapsed into having True or False
1243 -- conditions when this is inappropriate.
1245 if not (Full_Analysis
1246 or else (Is_OK_Static_Expression (L)
1247 and then
1248 Is_OK_Static_Expression (R)))
1249 then
1250 return Unknown;
1251 end if;
1253 -- If either operand could raise Constraint_Error, then we cannot
1254 -- know the result at compile time (since CE may be raised).
1256 if not (Cannot_Raise_Constraint_Error (L)
1257 and then
1258 Cannot_Raise_Constraint_Error (R))
1259 then
1260 return Unknown;
1261 end if;
1263 -- Identical operands are most certainly equal
1265 if L = R then
1266 return EQ;
1267 end if;
1269 -- If expressions have no types, then do not attempt to determine if
1270 -- they are the same, since something funny is going on. One case in
1271 -- which this happens is during generic template analysis, when bounds
1272 -- are not fully analyzed.
1274 if No (Ltyp) or else No (Rtyp) then
1275 return Unknown;
1276 end if;
1278 -- These get reset to the base type for the case of entities where
1279 -- Is_Known_Valid is not set. This takes care of handling possible
1280 -- invalid representations using the value of the base type, in
1281 -- accordance with RM 13.9.1(10).
1283 Ltyp := Underlying_Type (Ltyp);
1284 Rtyp := Underlying_Type (Rtyp);
1286 -- Same rationale as above, but for Underlying_Type instead of Etype
1288 if No (Ltyp) or else No (Rtyp) then
1289 return Unknown;
1290 end if;
1292 -- We do not attempt comparisons for packed arrays represented as
1293 -- modular types, where the semantics of comparison is quite different.
1295 if Is_Packed_Array_Impl_Type (Ltyp)
1296 and then Is_Modular_Integer_Type (Ltyp)
1297 then
1298 return Unknown;
1300 -- For access types, the only time we know the result at compile time
1301 -- (apart from identical operands, which we handled already) is if we
1302 -- know one operand is null and the other is not, or both operands are
1303 -- known null.
1305 elsif Is_Access_Type (Ltyp) then
1306 if Known_Null (L) then
1307 if Known_Null (R) then
1308 return EQ;
1309 elsif Known_Non_Null (R) then
1310 return NE;
1311 else
1312 return Unknown;
1313 end if;
1315 elsif Known_Non_Null (L) and then Known_Null (R) then
1316 return NE;
1318 else
1319 return Unknown;
1320 end if;
1322 -- Case where comparison involves two compile-time-known values
1324 elsif Compile_Time_Known_Value (L)
1325 and then
1326 Compile_Time_Known_Value (R)
1327 then
1328 -- For the floating-point case, we have to be a little careful, since
1329 -- at compile time we are dealing with universal exact values, but at
1330 -- runtime, these will be in non-exact target form. That's why the
1331 -- returned results are LE and GE below instead of LT and GT.
1333 if Is_Floating_Point_Type (Ltyp)
1334 or else
1335 Is_Floating_Point_Type (Rtyp)
1336 then
1337 declare
1338 Lo : constant Ureal := Expr_Value_R (L);
1339 Hi : constant Ureal := Expr_Value_R (R);
1340 begin
1341 if Lo < Hi then
1342 return LE;
1343 elsif Lo = Hi then
1344 return EQ;
1345 else
1346 return GE;
1347 end if;
1348 end;
1350 -- For string types, we have two string literals and we proceed to
1351 -- compare them using the Ada style dictionary string comparison.
1353 elsif not Is_Scalar_Type (Ltyp) then
1354 declare
1355 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1356 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1357 Llen : constant Nat := String_Length (Lstring);
1358 Rlen : constant Nat := String_Length (Rstring);
1360 begin
1361 for J in 1 .. Nat'Min (Llen, Rlen) loop
1362 declare
1363 LC : constant Char_Code := Get_String_Char (Lstring, J);
1364 RC : constant Char_Code := Get_String_Char (Rstring, J);
1365 begin
1366 if LC < RC then
1367 return LT;
1368 elsif LC > RC then
1369 return GT;
1370 end if;
1371 end;
1372 end loop;
1374 if Llen < Rlen then
1375 return LT;
1376 elsif Llen > Rlen then
1377 return GT;
1378 else
1379 return EQ;
1380 end if;
1381 end;
1383 -- For remaining scalar cases we know exactly (note that this does
1384 -- include the fixed-point case, where we know the run time integer
1385 -- values now).
1387 else
1388 declare
1389 Lo : constant Uint := Expr_Value (L);
1390 Hi : constant Uint := Expr_Value (R);
1391 begin
1392 if Lo < Hi then
1393 Diff.all := Hi - Lo;
1394 return LT;
1395 elsif Lo = Hi then
1396 return EQ;
1397 else
1398 Diff.all := Lo - Hi;
1399 return GT;
1400 end if;
1401 end;
1402 end if;
1404 -- Cases where at least one operand is not known at compile time
1406 else
1407 -- Remaining checks apply only for discrete types
1409 if not Is_Discrete_Type (Ltyp)
1410 or else
1411 not Is_Discrete_Type (Rtyp)
1412 then
1413 return Unknown;
1414 end if;
1416 -- Defend against generic types, or actually any expressions that
1417 -- contain a reference to a generic type from within a generic
1418 -- template. We don't want to do any range analysis of such
1419 -- expressions for two reasons. First, the bounds of a generic type
1420 -- itself are junk and cannot be used for any kind of analysis.
1421 -- Second, we may have a case where the range at run time is indeed
1422 -- known, but we don't want to do compile time analysis in the
1423 -- template based on that range since in an instance the value may be
1424 -- static, and able to be elaborated without reference to the bounds
1425 -- of types involved. As an example, consider:
1427 -- (F'Pos (F'Last) + 1) > Integer'Last
1429 -- The expression on the left side of > is Universal_Integer and thus
1430 -- acquires the type Integer for evaluation at run time, and at run
1431 -- time it is true that this condition is always False, but within
1432 -- an instance F may be a type with a static range greater than the
1433 -- range of Integer, and the expression statically evaluates to True.
1435 if References_Generic_Formal_Type (L)
1436 or else
1437 References_Generic_Formal_Type (R)
1438 then
1439 return Unknown;
1440 end if;
1442 -- Replace types by base types for the case of values which are not
1443 -- known to have valid representations. This takes care of properly
1444 -- dealing with invalid representations.
1446 if not Assume_Valid then
1447 if not (Is_Entity_Name (L)
1448 and then (Is_Known_Valid (Entity (L))
1449 or else Assume_No_Invalid_Values))
1450 then
1451 Ltyp := Underlying_Type (Base_Type (Ltyp));
1452 end if;
1454 if not (Is_Entity_Name (R)
1455 and then (Is_Known_Valid (Entity (R))
1456 or else Assume_No_Invalid_Values))
1457 then
1458 Rtyp := Underlying_Type (Base_Type (Rtyp));
1459 end if;
1460 end if;
1462 -- First attempt is to decompose the expressions to extract a
1463 -- constant offset resulting from the use of any of the forms:
1465 -- expr + literal
1466 -- expr - literal
1467 -- typ'Succ (expr)
1468 -- typ'Pred (expr)
1470 -- Then we see if the two expressions are the same value, and if so
1471 -- the result is obtained by comparing the offsets.
1473 -- Note: the reason we do this test first is that it returns only
1474 -- decisive results (with diff set), where other tests, like the
1475 -- range test, may not be as so decisive. Consider for example
1476 -- J .. J + 1. This code can conclude LT with a difference of 1,
1477 -- even if the range of J is not known.
1479 declare
1480 Lnode : Node_Id;
1481 Loffs : Uint;
1482 Rnode : Node_Id;
1483 Roffs : Uint;
1485 begin
1486 Compare_Decompose (L, Lnode, Loffs);
1487 Compare_Decompose (R, Rnode, Roffs);
1489 if Is_Same_Value (Lnode, Rnode) then
1490 if Loffs = Roffs then
1491 return EQ;
1492 end if;
1494 -- When the offsets are not equal, we can go farther only if
1495 -- the types are not modular (e.g. X < X + 1 is False if X is
1496 -- the largest number).
1498 if not Is_Modular_Integer_Type (Ltyp)
1499 and then not Is_Modular_Integer_Type (Rtyp)
1500 then
1501 if Loffs < Roffs then
1502 Diff.all := Roffs - Loffs;
1503 return LT;
1504 else
1505 Diff.all := Loffs - Roffs;
1506 return GT;
1507 end if;
1508 end if;
1509 end if;
1510 end;
1512 -- Next, try range analysis and see if operand ranges are disjoint
1514 declare
1515 LOK, ROK : Boolean;
1516 LLo, LHi : Uint;
1517 RLo, RHi : Uint;
1519 Single : Boolean;
1520 -- True if each range is a single point
1522 begin
1523 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1524 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1526 if LOK and ROK then
1527 Single := LLo = LHi and then RLo = RHi;
1529 if LHi < RLo then
1530 if Single and Assume_Valid then
1531 Diff.all := RLo - LLo;
1532 end if;
1534 return LT;
1536 elsif RHi < LLo then
1537 if Single and Assume_Valid then
1538 Diff.all := LLo - RLo;
1539 end if;
1541 return GT;
1543 elsif Single and then LLo = RLo then
1545 -- If the range includes a single literal and we can assume
1546 -- validity then the result is known even if an operand is
1547 -- not static.
1549 if Assume_Valid then
1550 return EQ;
1551 else
1552 return Unknown;
1553 end if;
1555 elsif LHi = RLo then
1556 return LE;
1558 elsif RHi = LLo then
1559 return GE;
1561 elsif not Is_Known_Valid_Operand (L)
1562 and then not Assume_Valid
1563 then
1564 if Is_Same_Value (L, R) then
1565 return EQ;
1566 else
1567 return Unknown;
1568 end if;
1569 end if;
1571 -- If the range of either operand cannot be determined, nothing
1572 -- further can be inferred.
1574 else
1575 return Unknown;
1576 end if;
1577 end;
1579 -- Here is where we check for comparisons against maximum bounds of
1580 -- types, where we know that no value can be outside the bounds of
1581 -- the subtype. Note that this routine is allowed to assume that all
1582 -- expressions are within their subtype bounds. Callers wishing to
1583 -- deal with possibly invalid values must in any case take special
1584 -- steps (e.g. conversions to larger types) to avoid this kind of
1585 -- optimization, which is always considered to be valid. We do not
1586 -- attempt this optimization with generic types, since the type
1587 -- bounds may not be meaningful in this case.
1589 -- We are in danger of an infinite recursion here. It does not seem
1590 -- useful to go more than one level deep, so the parameter Rec is
1591 -- used to protect ourselves against this infinite recursion.
1593 if not Rec then
1595 -- See if we can get a decisive check against one operand and a
1596 -- bound of the other operand (four possible tests here). Note
1597 -- that we avoid testing junk bounds of a generic type.
1599 if not Is_Generic_Type (Rtyp) then
1600 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1601 Discard'Access,
1602 Assume_Valid, Rec => True)
1604 when LT => return LT;
1605 when LE => return LE;
1606 when EQ => return LE;
1607 when others => null;
1608 end case;
1610 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1611 Discard'Access,
1612 Assume_Valid, Rec => True)
1614 when GT => return GT;
1615 when GE => return GE;
1616 when EQ => return GE;
1617 when others => null;
1618 end case;
1619 end if;
1621 if not Is_Generic_Type (Ltyp) then
1622 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1623 Discard'Access,
1624 Assume_Valid, Rec => True)
1626 when GT => return GT;
1627 when GE => return GE;
1628 when EQ => return GE;
1629 when others => null;
1630 end case;
1632 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1633 Discard'Access,
1634 Assume_Valid, Rec => True)
1636 when LT => return LT;
1637 when LE => return LE;
1638 when EQ => return LE;
1639 when others => null;
1640 end case;
1641 end if;
1642 end if;
1644 -- Next attempt is to see if we have an entity compared with a
1645 -- compile-time-known value, where there is a current value
1646 -- conditional for the entity which can tell us the result.
1648 declare
1649 Var : Node_Id;
1650 -- Entity variable (left operand)
1652 Val : Uint;
1653 -- Value (right operand)
1655 Inv : Boolean;
1656 -- If False, we have reversed the operands
1658 Op : Node_Kind;
1659 -- Comparison operator kind from Get_Current_Value_Condition call
1661 Opn : Node_Id;
1662 -- Value from Get_Current_Value_Condition call
1664 Opv : Uint;
1665 -- Value of Opn
1667 Result : Compare_Result;
1668 -- Known result before inversion
1670 begin
1671 if Is_Entity_Name (L)
1672 and then Compile_Time_Known_Value (R)
1673 then
1674 Var := L;
1675 Val := Expr_Value (R);
1676 Inv := False;
1678 elsif Is_Entity_Name (R)
1679 and then Compile_Time_Known_Value (L)
1680 then
1681 Var := R;
1682 Val := Expr_Value (L);
1683 Inv := True;
1685 -- That was the last chance at finding a compile time result
1687 else
1688 return Unknown;
1689 end if;
1691 Get_Current_Value_Condition (Var, Op, Opn);
1693 -- That was the last chance, so if we got nothing return
1695 if No (Opn) then
1696 return Unknown;
1697 end if;
1699 Opv := Expr_Value (Opn);
1701 -- We got a comparison, so we might have something interesting
1703 -- Convert LE to LT and GE to GT, just so we have fewer cases
1705 if Op = N_Op_Le then
1706 Op := N_Op_Lt;
1707 Opv := Opv + 1;
1709 elsif Op = N_Op_Ge then
1710 Op := N_Op_Gt;
1711 Opv := Opv - 1;
1712 end if;
1714 -- Deal with equality case
1716 if Op = N_Op_Eq then
1717 if Val = Opv then
1718 Result := EQ;
1719 elsif Opv < Val then
1720 Result := LT;
1721 else
1722 Result := GT;
1723 end if;
1725 -- Deal with inequality case
1727 elsif Op = N_Op_Ne then
1728 if Val = Opv then
1729 Result := NE;
1730 else
1731 return Unknown;
1732 end if;
1734 -- Deal with greater than case
1736 elsif Op = N_Op_Gt then
1737 if Opv >= Val then
1738 Result := GT;
1739 elsif Opv = Val - 1 then
1740 Result := GE;
1741 else
1742 return Unknown;
1743 end if;
1745 -- Deal with less than case
1747 else pragma Assert (Op = N_Op_Lt);
1748 if Opv <= Val then
1749 Result := LT;
1750 elsif Opv = Val + 1 then
1751 Result := LE;
1752 else
1753 return Unknown;
1754 end if;
1755 end if;
1757 -- Deal with inverting result
1759 if Inv then
1760 case Result is
1761 when GT => return LT;
1762 when GE => return LE;
1763 when LT => return GT;
1764 when LE => return GE;
1765 when others => return Result;
1766 end case;
1767 end if;
1769 return Result;
1770 end;
1771 end if;
1772 end Compile_Time_Compare;
1774 -------------------------------
1775 -- Compile_Time_Known_Bounds --
1776 -------------------------------
1778 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1779 Indx : Node_Id;
1780 Typ : Entity_Id;
1782 begin
1783 if T = Any_Composite or else not Is_Array_Type (T) then
1784 return False;
1785 end if;
1787 Indx := First_Index (T);
1788 while Present (Indx) loop
1789 Typ := Underlying_Type (Etype (Indx));
1791 -- Never look at junk bounds of a generic type
1793 if Is_Generic_Type (Typ) then
1794 return False;
1795 end if;
1797 -- Otherwise check bounds for compile-time-known
1799 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1800 return False;
1801 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1802 return False;
1803 else
1804 Next_Index (Indx);
1805 end if;
1806 end loop;
1808 return True;
1809 end Compile_Time_Known_Bounds;
1811 ------------------------------
1812 -- Compile_Time_Known_Value --
1813 ------------------------------
1815 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1816 K : constant Node_Kind := Nkind (Op);
1817 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1819 begin
1820 -- Never known at compile time if bad type or raises Constraint_Error
1821 -- or empty (which can occur as a result of a previous error or in the
1822 -- case of e.g. an imported constant).
1824 if No (Op) then
1825 return False;
1827 elsif Op = Error
1828 or else Nkind (Op) not in N_Has_Etype
1829 or else Etype (Op) = Any_Type
1830 or else Raises_Constraint_Error (Op)
1831 then
1832 return False;
1833 end if;
1835 -- If we have an entity name, then see if it is the name of a constant
1836 -- and if so, test the corresponding constant value, or the name of an
1837 -- enumeration literal, which is always a constant.
1839 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1840 declare
1841 Ent : constant Entity_Id := Entity (Op);
1842 Val : Node_Id;
1844 begin
1845 -- Never known at compile time if it is a packed array value. We
1846 -- might want to try to evaluate these at compile time one day,
1847 -- but we do not make that attempt now.
1849 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1850 return False;
1852 elsif Ekind (Ent) = E_Enumeration_Literal then
1853 return True;
1855 elsif Ekind (Ent) = E_Constant then
1856 Val := Constant_Value (Ent);
1858 if Present (Val) then
1860 -- Guard against an illegal deferred constant whose full
1861 -- view is initialized with a reference to itself. Treat
1862 -- this case as a value not known at compile time.
1864 if Is_Entity_Name (Val) and then Entity (Val) = Ent then
1865 return False;
1866 else
1867 return Compile_Time_Known_Value (Val);
1868 end if;
1870 -- Otherwise, the constant does not have a compile-time-known
1871 -- value.
1873 else
1874 return False;
1875 end if;
1876 end if;
1877 end;
1879 -- We have a value, see if it is compile-time-known
1881 else
1882 -- Integer literals are worth storing in the cache
1884 if K = N_Integer_Literal then
1885 CV_Ent.N := Op;
1886 CV_Ent.V := Intval (Op);
1887 return True;
1889 -- Other literals and NULL are known at compile time
1891 elsif K in
1892 N_Character_Literal | N_Real_Literal | N_String_Literal | N_Null
1893 then
1894 return True;
1896 -- Evaluate static discriminants, to eliminate dead paths and
1897 -- redundant discriminant checks.
1899 elsif Is_Static_Discriminant_Component (Op) then
1900 return True;
1901 end if;
1902 end if;
1904 -- If we fall through, not known at compile time
1906 return False;
1908 -- If we get an exception while trying to do this test, then some error
1909 -- has occurred, and we simply say that the value is not known after all
1911 exception
1912 when others =>
1913 -- With debug flag K we will get an exception unless an error has
1914 -- already occurred (useful for debugging).
1916 if Debug_Flag_K then
1917 Check_Error_Detected;
1918 end if;
1920 return False;
1921 end Compile_Time_Known_Value;
1923 ---------------------------------------
1924 -- CRT_Safe_Compile_Time_Known_Value --
1925 ---------------------------------------
1927 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1928 begin
1929 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1930 and then not Is_OK_Static_Expression (Op)
1931 then
1932 return False;
1933 else
1934 return Compile_Time_Known_Value (Op);
1935 end if;
1936 end CRT_Safe_Compile_Time_Known_Value;
1938 -----------------
1939 -- Eval_Actual --
1940 -----------------
1942 -- This is only called for actuals of functions that are not predefined
1943 -- operators (which have already been rewritten as operators at this
1944 -- stage), so the call can never be folded, and all that needs doing for
1945 -- the actual is to do the check for a non-static context.
1947 procedure Eval_Actual (N : Node_Id) is
1948 begin
1949 Check_Non_Static_Context (N);
1950 end Eval_Actual;
1952 --------------------
1953 -- Eval_Allocator --
1954 --------------------
1956 -- Allocators are never static, so all we have to do is to do the
1957 -- check for a non-static context if an expression is present.
1959 procedure Eval_Allocator (N : Node_Id) is
1960 Expr : constant Node_Id := Expression (N);
1961 begin
1962 if Nkind (Expr) = N_Qualified_Expression then
1963 Check_Non_Static_Context (Expression (Expr));
1964 end if;
1965 end Eval_Allocator;
1967 ------------------------
1968 -- Eval_Arithmetic_Op --
1969 ------------------------
1971 -- Arithmetic operations are static functions, so the result is static
1972 -- if both operands are static (RM 4.9(7), 4.9(20)).
1974 procedure Eval_Arithmetic_Op (N : Node_Id) is
1975 Left : constant Node_Id := Left_Opnd (N);
1976 Right : constant Node_Id := Right_Opnd (N);
1977 Ltype : constant Entity_Id := Etype (Left);
1978 Rtype : constant Entity_Id := Etype (Right);
1979 Otype : Entity_Id := Empty;
1980 Stat : Boolean;
1981 Fold : Boolean;
1983 begin
1984 -- If not foldable we are done
1986 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1988 if not Fold then
1989 return;
1990 end if;
1992 -- Otherwise attempt to fold
1994 if Is_Universal_Numeric_Type (Etype (Left))
1995 and then
1996 Is_Universal_Numeric_Type (Etype (Right))
1997 then
1998 Otype := Find_Universal_Operator_Type (N);
1999 end if;
2001 -- Fold for cases where both operands are of integer type
2003 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
2004 declare
2005 Left_Int : constant Uint := Expr_Value (Left);
2006 Right_Int : constant Uint := Expr_Value (Right);
2007 Result : Uint;
2009 begin
2010 case Nkind (N) is
2011 when N_Op_Add =>
2012 Result := Left_Int + Right_Int;
2014 when N_Op_Subtract =>
2015 Result := Left_Int - Right_Int;
2017 when N_Op_Multiply =>
2018 if OK_Bits
2019 (N, UI_From_Int
2020 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
2021 then
2022 Result := Left_Int * Right_Int;
2023 else
2024 Result := Left_Int;
2025 end if;
2027 when N_Op_Divide =>
2029 -- The exception Constraint_Error is raised by integer
2030 -- division, rem and mod if the right operand is zero.
2032 if Right_Int = 0 then
2034 -- When SPARK_Mode is On, force a warning instead of
2035 -- an error in that case, as this likely corresponds
2036 -- to deactivated code.
2038 Apply_Compile_Time_Constraint_Error
2039 (N, "division by zero", CE_Divide_By_Zero,
2040 Loc => Sloc (Right),
2041 Warn => not Stat or SPARK_Mode = On);
2042 return;
2044 -- Otherwise we can do the division
2046 else
2047 Result := Left_Int / Right_Int;
2048 end if;
2050 when N_Op_Mod =>
2052 -- The exception Constraint_Error is raised by integer
2053 -- division, rem and mod if the right operand is zero.
2055 if Right_Int = 0 then
2057 -- When SPARK_Mode is On, force a warning instead of
2058 -- an error in that case, as this likely corresponds
2059 -- to deactivated code.
2061 Apply_Compile_Time_Constraint_Error
2062 (N, "mod with zero divisor", CE_Divide_By_Zero,
2063 Loc => Sloc (Right),
2064 Warn => not Stat or SPARK_Mode = On);
2065 return;
2067 else
2068 Result := Left_Int mod Right_Int;
2069 end if;
2071 when N_Op_Rem =>
2073 -- The exception Constraint_Error is raised by integer
2074 -- division, rem and mod if the right operand is zero.
2076 if Right_Int = 0 then
2078 -- When SPARK_Mode is On, force a warning instead of
2079 -- an error in that case, as this likely corresponds
2080 -- to deactivated code.
2082 Apply_Compile_Time_Constraint_Error
2083 (N, "rem with zero divisor", CE_Divide_By_Zero,
2084 Loc => Sloc (Right),
2085 Warn => not Stat or SPARK_Mode = On);
2086 return;
2088 else
2089 Result := Left_Int rem Right_Int;
2090 end if;
2092 when others =>
2093 raise Program_Error;
2094 end case;
2096 -- Adjust the result by the modulus if the type is a modular type
2098 if Is_Modular_Integer_Type (Ltype) then
2099 Result := Result mod Modulus (Ltype);
2100 end if;
2102 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
2104 -- If we get here we can fold the result
2106 Fold_Uint (N, Result, Stat);
2107 end;
2109 -- Cases where at least one operand is a real. We handle the cases of
2110 -- both reals, or mixed/real integer cases (the latter happen only for
2111 -- divide and multiply, and the result is always real).
2113 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2114 declare
2115 Left_Real : Ureal;
2116 Right_Real : Ureal;
2117 Result : Ureal;
2119 begin
2120 if Is_Real_Type (Ltype) then
2121 Left_Real := Expr_Value_R (Left);
2122 else
2123 Left_Real := UR_From_Uint (Expr_Value (Left));
2124 end if;
2126 if Is_Real_Type (Rtype) then
2127 Right_Real := Expr_Value_R (Right);
2128 else
2129 Right_Real := UR_From_Uint (Expr_Value (Right));
2130 end if;
2132 if Nkind (N) = N_Op_Add then
2133 Result := Left_Real + Right_Real;
2135 elsif Nkind (N) = N_Op_Subtract then
2136 Result := Left_Real - Right_Real;
2138 elsif Nkind (N) = N_Op_Multiply then
2139 Result := Left_Real * Right_Real;
2141 else pragma Assert (Nkind (N) = N_Op_Divide);
2142 if UR_Is_Zero (Right_Real) then
2143 Apply_Compile_Time_Constraint_Error
2144 (N, "division by zero", CE_Divide_By_Zero,
2145 Loc => Sloc (Right));
2146 return;
2147 end if;
2149 Result := Left_Real / Right_Real;
2150 end if;
2152 Fold_Ureal (N, Result, Stat);
2153 end;
2154 end if;
2156 -- If the operator was resolved to a specific type, make sure that type
2157 -- is frozen even if the expression is folded into a literal (which has
2158 -- a universal type).
2160 if Present (Otype) then
2161 Freeze_Before (N, Otype);
2162 end if;
2163 end Eval_Arithmetic_Op;
2165 ----------------------------
2166 -- Eval_Character_Literal --
2167 ----------------------------
2169 -- Nothing to be done
2171 procedure Eval_Character_Literal (N : Node_Id) is
2172 pragma Warnings (Off, N);
2173 begin
2174 null;
2175 end Eval_Character_Literal;
2177 ---------------
2178 -- Eval_Call --
2179 ---------------
2181 -- Static function calls are either calls to predefined operators
2182 -- with static arguments, or calls to functions that rename a literal.
2183 -- Only the latter case is handled here, predefined operators are
2184 -- constant-folded elsewhere.
2186 -- If the function is itself inherited the literal of the parent type must
2187 -- be explicitly converted to the return type of the function.
2189 procedure Eval_Call (N : Node_Id) is
2190 Loc : constant Source_Ptr := Sloc (N);
2191 Typ : constant Entity_Id := Etype (N);
2192 Lit : Entity_Id;
2194 begin
2195 if Nkind (N) = N_Function_Call
2196 and then No (Parameter_Associations (N))
2197 and then Is_Entity_Name (Name (N))
2198 and then Present (Alias (Entity (Name (N))))
2199 and then Is_Enumeration_Type (Base_Type (Typ))
2200 then
2201 Lit := Ultimate_Alias (Entity (Name (N)));
2203 if Ekind (Lit) = E_Enumeration_Literal then
2204 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2205 Rewrite
2206 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2207 else
2208 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2209 end if;
2211 Resolve (N, Typ);
2212 end if;
2214 elsif Nkind (N) = N_Function_Call
2215 and then Is_Entity_Name (Name (N))
2216 and then Is_Intrinsic_Subprogram (Entity (Name (N)))
2217 then
2218 Eval_Intrinsic_Call (N, Entity (Name (N)));
2220 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2221 -- is enabled and we have a call to a static function, substitute a
2222 -- static value for the call, to allow folding the expression. This
2223 -- supports checking the requirement of RM 6.8(5.3/5) in
2224 -- Analyze_Expression_Function.
2226 elsif Checking_Potentially_Static_Expression
2227 and then Is_Static_Function_Call (N)
2228 then
2229 Fold_Dummy (N, Typ);
2230 end if;
2231 end Eval_Call;
2233 --------------------------
2234 -- Eval_Case_Expression --
2235 --------------------------
2237 -- A conditional expression is static if all its conditions and dependent
2238 -- expressions are static. Note that we do not care if the dependent
2239 -- expressions raise CE, except for the one that will be selected.
2241 procedure Eval_Case_Expression (N : Node_Id) is
2242 Alt : Node_Id;
2243 Choice : Node_Id;
2245 begin
2246 Set_Is_Static_Expression (N, False);
2248 if Error_Posted (Expression (N))
2249 or else not Is_Static_Expression (Expression (N))
2250 then
2251 Check_Non_Static_Context (Expression (N));
2252 return;
2253 end if;
2255 -- First loop, make sure all the alternatives are static expressions
2256 -- none of which raise Constraint_Error. We make the Constraint_Error
2257 -- check because part of the legality condition for a correct static
2258 -- case expression is that the cases are covered, like any other case
2259 -- expression. And we can't do that if any of the conditions raise an
2260 -- exception, so we don't even try to evaluate if that is the case.
2262 Alt := First (Alternatives (N));
2263 while Present (Alt) loop
2265 -- The expression must be static, but we don't care at this stage
2266 -- if it raises Constraint_Error (the alternative might not match,
2267 -- in which case the expression is statically unevaluated anyway).
2269 if not Is_Static_Expression (Expression (Alt)) then
2270 Check_Non_Static_Context (Expression (Alt));
2271 return;
2272 end if;
2274 -- The choices of a case always have to be static, and cannot raise
2275 -- an exception. If this condition is not met, then the expression
2276 -- is plain illegal, so just abandon evaluation attempts. No need
2277 -- to check non-static context when we have something illegal anyway.
2279 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2280 return;
2281 end if;
2283 Next (Alt);
2284 end loop;
2286 -- OK, if the above loop gets through it means that all choices are OK
2287 -- static (don't raise exceptions), so the whole case is static, and we
2288 -- can find the matching alternative.
2290 Set_Is_Static_Expression (N);
2292 -- Now to deal with propagating a possible Constraint_Error
2294 -- If the selecting expression raises CE, propagate and we are done
2296 if Raises_Constraint_Error (Expression (N)) then
2297 Set_Raises_Constraint_Error (N);
2299 -- Otherwise we need to check the alternatives to find the matching
2300 -- one. CE's in other than the matching one are not relevant. But we
2301 -- do need to check the matching one. Unlike the first loop, we do not
2302 -- have to go all the way through, when we find the matching one, quit.
2304 else
2305 Alt := First (Alternatives (N));
2306 Search : loop
2308 -- We must find a match among the alternatives. If not, this must
2309 -- be due to other errors, so just ignore, leaving as non-static.
2311 if No (Alt) then
2312 Set_Is_Static_Expression (N, False);
2313 return;
2314 end if;
2316 -- Otherwise loop through choices of this alternative
2318 Choice := First (Discrete_Choices (Alt));
2319 while Present (Choice) loop
2321 -- If we find a matching choice, then the Expression of this
2322 -- alternative replaces N (Raises_Constraint_Error flag is
2323 -- included, so we don't have to special case that).
2325 if Choice_Matches (Expression (N), Choice) = Match then
2326 Rewrite (N, Relocate_Node (Expression (Alt)));
2327 return;
2328 end if;
2330 Next (Choice);
2331 end loop;
2333 Next (Alt);
2334 end loop Search;
2335 end if;
2336 end Eval_Case_Expression;
2338 ------------------------
2339 -- Eval_Concatenation --
2340 ------------------------
2342 -- Concatenation is a static function, so the result is static if both
2343 -- operands are static (RM 4.9(7), 4.9(21)).
2345 procedure Eval_Concatenation (N : Node_Id) is
2346 Left : constant Node_Id := Left_Opnd (N);
2347 Right : constant Node_Id := Right_Opnd (N);
2348 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2349 Stat : Boolean;
2350 Fold : Boolean;
2352 begin
2353 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2354 -- non-static context.
2356 if Ada_Version = Ada_83
2357 and then Comes_From_Source (N)
2358 then
2359 Check_Non_Static_Context (Left);
2360 Check_Non_Static_Context (Right);
2361 return;
2362 end if;
2364 -- If not foldable we are done. In principle concatenation that yields
2365 -- any string type is static (i.e. an array type of character types).
2366 -- However, character types can include enumeration literals, and
2367 -- concatenation in that case cannot be described by a literal, so we
2368 -- only consider the operation static if the result is an array of
2369 -- (a descendant of) a predefined character type.
2371 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2373 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2374 Set_Is_Static_Expression (N, False);
2375 return;
2376 end if;
2378 -- Compile time string concatenation
2380 -- ??? Note that operands that are aggregates can be marked as static,
2381 -- so we should attempt at a later stage to fold concatenations with
2382 -- such aggregates.
2384 declare
2385 Left_Str : constant Node_Id := Get_String_Val (Left);
2386 Left_Len : Nat;
2387 Right_Str : constant Node_Id := Get_String_Val (Right);
2388 Folded_Val : String_Id := No_String;
2390 begin
2391 -- Establish new string literal, and store left operand. We make
2392 -- sure to use the special Start_String that takes an operand if
2393 -- the left operand is a string literal. Since this is optimized
2394 -- in the case where that is the most recently created string
2395 -- literal, we ensure efficient time/space behavior for the
2396 -- case of a concatenation of a series of string literals.
2398 if Nkind (Left_Str) = N_String_Literal then
2399 Left_Len := String_Length (Strval (Left_Str));
2401 -- If the left operand is the empty string, and the right operand
2402 -- is a string literal (the case of "" & "..."), the result is the
2403 -- value of the right operand. This optimization is important when
2404 -- Is_Folded_In_Parser, to avoid copying an enormous right
2405 -- operand.
2407 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2408 Folded_Val := Strval (Right_Str);
2409 else
2410 Start_String (Strval (Left_Str));
2411 end if;
2413 else
2414 Start_String;
2415 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2416 Left_Len := 1;
2417 end if;
2419 -- Now append the characters of the right operand, unless we
2420 -- optimized the "" & "..." case above.
2422 if Nkind (Right_Str) = N_String_Literal then
2423 if Left_Len /= 0 then
2424 Store_String_Chars (Strval (Right_Str));
2425 Folded_Val := End_String;
2426 end if;
2427 else
2428 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2429 Folded_Val := End_String;
2430 end if;
2432 Set_Is_Static_Expression (N, Stat);
2434 -- If left operand is the empty string, the result is the
2435 -- right operand, including its bounds if anomalous.
2437 if Left_Len = 0
2438 and then Is_Array_Type (Etype (Right))
2439 and then Etype (Right) /= Any_String
2440 then
2441 Set_Etype (N, Etype (Right));
2442 end if;
2444 Fold_Str (N, Folded_Val, Static => Stat);
2445 end;
2446 end Eval_Concatenation;
2448 ----------------------
2449 -- Eval_Entity_Name --
2450 ----------------------
2452 -- This procedure is used for identifiers and expanded names other than
2453 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2454 -- static if they denote a static constant (RM 4.9(6)) or if the name
2455 -- denotes an enumeration literal (RM 4.9(22)).
2457 procedure Eval_Entity_Name (N : Node_Id) is
2458 Def_Id : constant Entity_Id := Entity (N);
2459 Val : Node_Id;
2461 begin
2462 -- Enumeration literals are always considered to be constants
2463 -- and cannot raise Constraint_Error (RM 4.9(22)).
2465 if Ekind (Def_Id) = E_Enumeration_Literal then
2466 Set_Is_Static_Expression (N);
2467 return;
2469 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2470 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2471 -- it does not violate 10.2.1(8) here, since this is not a variable.
2473 elsif Ekind (Def_Id) = E_Constant then
2475 -- Deferred constants must always be treated as nonstatic outside the
2476 -- scope of their full view.
2478 if Present (Full_View (Def_Id))
2479 and then not In_Open_Scopes (Scope (Def_Id))
2480 then
2481 Val := Empty;
2482 else
2483 Val := Constant_Value (Def_Id);
2484 end if;
2486 if Present (Val) then
2487 Set_Is_Static_Expression
2488 (N, Is_Static_Expression (Val)
2489 and then Is_Static_Subtype (Etype (Def_Id)));
2490 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2492 if not Is_Static_Expression (N)
2493 and then not Is_Generic_Type (Etype (N))
2494 then
2495 Validate_Static_Object_Name (N);
2496 end if;
2498 -- Mark constant condition in SCOs
2500 if Generate_SCO
2501 and then Comes_From_Source (N)
2502 and then Is_Boolean_Type (Etype (Def_Id))
2503 and then Compile_Time_Known_Value (N)
2504 then
2505 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2506 end if;
2508 return;
2509 end if;
2511 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2512 -- is enabled and we have a reference to a formal parameter of mode in,
2513 -- substitute a static value for the reference, to allow folding the
2514 -- expression. This supports checking the requirement of RM 6.8(5.3/5)
2515 -- in Analyze_Expression_Function.
2517 elsif Ekind (Def_Id) = E_In_Parameter
2518 and then Checking_Potentially_Static_Expression
2519 and then Is_Static_Function (Scope (Def_Id))
2520 then
2521 Fold_Dummy (N, Etype (Def_Id));
2522 end if;
2524 -- Fall through if the name is not static
2526 Validate_Static_Object_Name (N);
2527 end Eval_Entity_Name;
2529 ------------------------
2530 -- Eval_If_Expression --
2531 ------------------------
2533 -- We can fold to a static expression if the condition and both dependent
2534 -- expressions are static. Otherwise, the only required processing is to do
2535 -- the check for non-static context for the then and else expressions.
2537 procedure Eval_If_Expression (N : Node_Id) is
2538 Condition : constant Node_Id := First (Expressions (N));
2539 Then_Expr : constant Node_Id := Next (Condition);
2540 Else_Expr : constant Node_Id := Next (Then_Expr);
2541 Result : Node_Id;
2542 Non_Result : Node_Id;
2544 Rstat : constant Boolean :=
2545 Is_Static_Expression (Condition)
2546 and then
2547 Is_Static_Expression (Then_Expr)
2548 and then
2549 Is_Static_Expression (Else_Expr);
2550 -- True if result is static
2552 begin
2553 -- If result not static, nothing to do, otherwise set static result
2555 if not Rstat then
2556 return;
2557 else
2558 Set_Is_Static_Expression (N);
2559 end if;
2561 -- If any operand is Any_Type, just propagate to result and do not try
2562 -- to fold, this prevents cascaded errors.
2564 if Etype (Condition) = Any_Type or else
2565 Etype (Then_Expr) = Any_Type or else
2566 Etype (Else_Expr) = Any_Type
2567 then
2568 Set_Etype (N, Any_Type);
2569 Set_Is_Static_Expression (N, False);
2570 return;
2571 end if;
2573 -- If condition raises Constraint_Error then we have already signaled
2574 -- an error, and we just propagate to the result and do not fold.
2576 if Raises_Constraint_Error (Condition) then
2577 Set_Raises_Constraint_Error (N);
2578 return;
2579 end if;
2581 -- Static case where we can fold. Note that we don't try to fold cases
2582 -- where the condition is known at compile time, but the result is
2583 -- non-static. This avoids possible cases of infinite recursion where
2584 -- the expander puts in a redundant test and we remove it. Instead we
2585 -- deal with these cases in the expander.
2587 -- Select result operand
2589 if Is_True (Expr_Value (Condition)) then
2590 Result := Then_Expr;
2591 Non_Result := Else_Expr;
2592 else
2593 Result := Else_Expr;
2594 Non_Result := Then_Expr;
2595 end if;
2597 -- Note that it does not matter if the non-result operand raises a
2598 -- Constraint_Error, but if the result raises Constraint_Error then we
2599 -- replace the node with a raise Constraint_Error. This will properly
2600 -- propagate Raises_Constraint_Error since this flag is set in Result.
2602 if Raises_Constraint_Error (Result) then
2603 Rewrite_In_Raise_CE (N, Result);
2604 Check_Non_Static_Context (Non_Result);
2606 -- Otherwise the result operand replaces the original node
2608 else
2609 Rewrite (N, Relocate_Node (Result));
2610 Set_Is_Static_Expression (N);
2611 end if;
2612 end Eval_If_Expression;
2614 ----------------------------
2615 -- Eval_Indexed_Component --
2616 ----------------------------
2618 -- Indexed components are never static, so we need to perform the check
2619 -- for non-static context on the index values. Then, we check if the
2620 -- value can be obtained at compile time, even though it is non-static.
2622 procedure Eval_Indexed_Component (N : Node_Id) is
2623 Expr : Node_Id;
2625 begin
2626 -- Check for non-static context on index values
2628 Expr := First (Expressions (N));
2629 while Present (Expr) loop
2630 Check_Non_Static_Context (Expr);
2631 Next (Expr);
2632 end loop;
2634 -- If the indexed component appears in an object renaming declaration
2635 -- then we do not want to try to evaluate it, since in this case we
2636 -- need the identity of the array element.
2638 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2639 return;
2641 -- Similarly if the indexed component appears as the prefix of an
2642 -- attribute we don't want to evaluate it, because at least for
2643 -- some cases of attributes we need the identify (e.g. Access, Size).
2645 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2646 return;
2647 end if;
2649 -- Note: there are other cases, such as the left side of an assignment,
2650 -- or an OUT parameter for a call, where the replacement results in the
2651 -- illegal use of a constant, But these cases are illegal in the first
2652 -- place, so the replacement, though silly, is harmless.
2654 -- Now see if this is a constant array reference
2656 if List_Length (Expressions (N)) = 1
2657 and then Is_Entity_Name (Prefix (N))
2658 and then Ekind (Entity (Prefix (N))) = E_Constant
2659 and then Present (Constant_Value (Entity (Prefix (N))))
2660 then
2661 declare
2662 Loc : constant Source_Ptr := Sloc (N);
2663 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2664 Sub : constant Node_Id := First (Expressions (N));
2666 Atyp : Entity_Id;
2667 -- Type of array
2669 Lin : Nat;
2670 -- Linear one's origin subscript value for array reference
2672 Lbd : Node_Id;
2673 -- Lower bound of the first array index
2675 Elm : Node_Id;
2676 -- Value from constant array
2678 begin
2679 Atyp := Etype (Arr);
2681 if Is_Access_Type (Atyp) then
2682 Atyp := Designated_Type (Atyp);
2683 end if;
2685 -- If we have an array type (we should have but perhaps there are
2686 -- error cases where this is not the case), then see if we can do
2687 -- a constant evaluation of the array reference.
2689 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2690 if Ekind (Atyp) = E_String_Literal_Subtype then
2691 Lbd := String_Literal_Low_Bound (Atyp);
2692 else
2693 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2694 end if;
2696 if Compile_Time_Known_Value (Sub)
2697 and then Nkind (Arr) = N_Aggregate
2698 and then Compile_Time_Known_Value (Lbd)
2699 and then Is_Discrete_Type (Component_Type (Atyp))
2700 then
2701 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2703 if List_Length (Expressions (Arr)) >= Lin then
2704 Elm := Pick (Expressions (Arr), Lin);
2706 -- If the resulting expression is compile-time-known,
2707 -- then we can rewrite the indexed component with this
2708 -- value, being sure to mark the result as non-static.
2709 -- We also reset the Sloc, in case this generates an
2710 -- error later on (e.g. 136'Access).
2712 if Compile_Time_Known_Value (Elm) then
2713 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2714 Set_Is_Static_Expression (N, False);
2715 Set_Sloc (N, Loc);
2716 end if;
2717 end if;
2719 -- We can also constant-fold if the prefix is a string literal.
2720 -- This will be useful in an instantiation or an inlining.
2722 elsif Compile_Time_Known_Value (Sub)
2723 and then Nkind (Arr) = N_String_Literal
2724 and then Compile_Time_Known_Value (Lbd)
2725 and then Expr_Value (Lbd) = 1
2726 and then Expr_Value (Sub) <=
2727 String_Literal_Length (Etype (Arr))
2728 then
2729 declare
2730 C : constant Char_Code :=
2731 Get_String_Char (Strval (Arr),
2732 UI_To_Int (Expr_Value (Sub)));
2733 begin
2734 Set_Character_Literal_Name (C);
2736 Elm :=
2737 Make_Character_Literal (Loc,
2738 Chars => Name_Find,
2739 Char_Literal_Value => UI_From_CC (C));
2740 Set_Etype (Elm, Component_Type (Atyp));
2741 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2742 Set_Is_Static_Expression (N, False);
2743 end;
2744 end if;
2745 end if;
2746 end;
2747 end if;
2748 end Eval_Indexed_Component;
2750 --------------------------
2751 -- Eval_Integer_Literal --
2752 --------------------------
2754 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2755 -- as static by the analyzer. The reason we did it that early is to allow
2756 -- the possibility of turning off the Is_Static_Expression flag after
2757 -- analysis, but before resolution, when integer literals are generated in
2758 -- the expander that do not correspond to static expressions.
2760 procedure Eval_Integer_Literal (N : Node_Id) is
2761 function In_Any_Integer_Context (K : Node_Kind) return Boolean;
2762 -- If the literal is resolved with a specific type in a context where
2763 -- the expected type is Any_Integer, there are no range checks on the
2764 -- literal. By the time the literal is evaluated, it carries the type
2765 -- imposed by the enclosing expression, and we must recover the context
2766 -- to determine that Any_Integer is meant.
2768 ----------------------------
2769 -- In_Any_Integer_Context --
2770 ----------------------------
2772 function In_Any_Integer_Context (K : Node_Kind) return Boolean is
2773 begin
2774 -- Any_Integer also appears in digits specifications for real types,
2775 -- but those have bounds smaller that those of any integer base type,
2776 -- so we can safely ignore these cases.
2778 return K in N_Attribute_Definition_Clause
2779 | N_Modular_Type_Definition
2780 | N_Number_Declaration
2781 | N_Signed_Integer_Type_Definition;
2782 end In_Any_Integer_Context;
2784 -- Local variables
2786 PK : constant Node_Kind := Nkind (Parent (N));
2787 Typ : constant Entity_Id := Etype (N);
2789 -- Start of processing for Eval_Integer_Literal
2791 begin
2792 -- If the literal appears in a non-expression context, then it is
2793 -- certainly appearing in a non-static context, so check it. This is
2794 -- actually a redundant check, since Check_Non_Static_Context would
2795 -- check it, but it seems worthwhile to optimize out the call.
2797 -- Additionally, when the literal appears within an if or case
2798 -- expression it must be checked as well. However, due to the literal
2799 -- appearing within a conditional statement, expansion greatly changes
2800 -- the nature of its context and performing some of the checks within
2801 -- Check_Non_Static_Context on an expanded literal may lead to spurious
2802 -- and misleading warnings.
2804 if (PK not in N_Case_Expression_Alternative | N_Subexpr
2805 or else (PK in N_Case_Expression_Alternative | N_If_Expression
2806 and then
2807 Comes_From_Source (N)))
2808 and then not In_Any_Integer_Context (PK)
2809 then
2810 Check_Non_Static_Context (N);
2811 end if;
2813 -- Modular integer literals must be in their base range
2815 if Is_Modular_Integer_Type (Typ)
2816 and then Is_Out_Of_Range (N, Base_Type (Typ), Assume_Valid => True)
2817 then
2818 Out_Of_Range (N);
2819 end if;
2820 end Eval_Integer_Literal;
2822 -------------------------
2823 -- Eval_Intrinsic_Call --
2824 -------------------------
2826 procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
2828 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind);
2829 -- Evaluate an intrinsic shift call N on the given subprogram E.
2830 -- Op is the kind for the shift node.
2832 ----------------
2833 -- Eval_Shift --
2834 ----------------
2836 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind) is
2837 Left : constant Node_Id := First_Actual (N);
2838 Right : constant Node_Id := Next_Actual (Left);
2839 Static : constant Boolean := Is_Static_Function (E);
2841 begin
2842 if Static then
2843 if Checking_Potentially_Static_Expression then
2844 Fold_Dummy (N, Etype (N));
2845 return;
2846 end if;
2847 end if;
2849 Fold_Shift
2850 (N, Left, Right, Op, Static => Static, Check_Elab => not Static);
2851 end Eval_Shift;
2853 Nam : Name_Id;
2855 begin
2856 -- Nothing to do if the intrinsic is handled by the back end.
2858 if Present (Interface_Name (E)) then
2859 return;
2860 end if;
2862 -- Intrinsic calls as part of a static function is a (core)
2863 -- language extension.
2865 if Checking_Potentially_Static_Expression
2866 and then not Core_Extensions_Allowed
2867 then
2868 return;
2869 end if;
2871 -- If we have a renaming, expand the call to the original operation,
2872 -- which must itself be intrinsic, since renaming requires matching
2873 -- conventions and this has already been checked.
2875 if Present (Alias (E)) then
2876 Eval_Intrinsic_Call (N, Alias (E));
2877 return;
2878 end if;
2880 -- If the intrinsic subprogram is generic, gets its original name
2882 if Present (Parent (E))
2883 and then Present (Generic_Parent (Parent (E)))
2884 then
2885 Nam := Chars (Generic_Parent (Parent (E)));
2886 else
2887 Nam := Chars (E);
2888 end if;
2890 case Nam is
2891 when Name_Shift_Left =>
2892 Eval_Shift (N, E, N_Op_Shift_Left);
2893 when Name_Shift_Right =>
2894 Eval_Shift (N, E, N_Op_Shift_Right);
2895 when Name_Shift_Right_Arithmetic =>
2896 Eval_Shift (N, E, N_Op_Shift_Right_Arithmetic);
2897 when others =>
2898 null;
2899 end case;
2900 end Eval_Intrinsic_Call;
2902 ---------------------
2903 -- Eval_Logical_Op --
2904 ---------------------
2906 -- Logical operations are static functions, so the result is potentially
2907 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2909 procedure Eval_Logical_Op (N : Node_Id) is
2910 Left : constant Node_Id := Left_Opnd (N);
2911 Right : constant Node_Id := Right_Opnd (N);
2912 Left_Int : Uint := No_Uint;
2913 Right_Int : Uint := No_Uint;
2914 Stat : Boolean;
2915 Fold : Boolean;
2917 begin
2918 -- If not foldable we are done
2920 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2922 if not Fold then
2923 return;
2924 end if;
2926 -- Compile time evaluation of logical operation
2928 if Is_Modular_Integer_Type (Etype (N)) then
2929 Left_Int := Expr_Value (Left);
2930 Right_Int := Expr_Value (Right);
2932 declare
2933 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2934 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2936 begin
2937 To_Bits (Left_Int, Left_Bits);
2938 To_Bits (Right_Int, Right_Bits);
2940 -- Note: should really be able to use array ops instead of
2941 -- these loops, but they break the build with a cryptic error
2942 -- during the bind of gnat1 likely due to a wrong computation
2943 -- of a date or checksum.
2945 if Nkind (N) = N_Op_And then
2946 for J in Left_Bits'Range loop
2947 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2948 end loop;
2950 elsif Nkind (N) = N_Op_Or then
2951 for J in Left_Bits'Range loop
2952 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2953 end loop;
2955 else
2956 pragma Assert (Nkind (N) = N_Op_Xor);
2958 for J in Left_Bits'Range loop
2959 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2960 end loop;
2961 end if;
2963 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2964 end;
2966 else
2967 pragma Assert (Is_Boolean_Type (Etype (N)));
2969 if Compile_Time_Known_Value (Left)
2970 and then Compile_Time_Known_Value (Right)
2971 then
2972 Right_Int := Expr_Value (Right);
2973 Left_Int := Expr_Value (Left);
2974 end if;
2976 if Nkind (N) = N_Op_And then
2978 -- If Left or Right are not compile time known values it means
2979 -- that the result is always False as per
2980 -- Test_Expression_Is_Foldable.
2981 -- Note that in this case, both Right_Int and Left_Int are set
2982 -- to No_Uint, so need to test for both.
2984 if No (Right_Int) then
2985 Fold_Uint (N, Uint_0, Stat);
2986 else
2987 Fold_Uint (N,
2988 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2989 end if;
2990 elsif Nkind (N) = N_Op_Or then
2992 -- If Left or Right are not compile time known values it means
2993 -- that the result is always True. as per
2994 -- Test_Expression_Is_Foldable.
2995 -- Note that in this case, both Right_Int and Left_Int are set
2996 -- to No_Uint, so need to test for both.
2998 if No (Right_Int) then
2999 Fold_Uint (N, Uint_1, Stat);
3000 else
3001 Fold_Uint (N,
3002 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
3003 end if;
3004 else
3005 pragma Assert (Nkind (N) = N_Op_Xor);
3006 Fold_Uint (N,
3007 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
3008 end if;
3009 end if;
3010 end Eval_Logical_Op;
3012 ------------------------
3013 -- Eval_Membership_Op --
3014 ------------------------
3016 -- A membership test is potentially static if the expression is static, and
3017 -- the range is a potentially static range, or is a subtype mark denoting a
3018 -- static subtype (RM 4.9(12)).
3020 procedure Eval_Membership_Op (N : Node_Id) is
3021 Alts : constant List_Id := Alternatives (N);
3022 Choice : constant Node_Id := Right_Opnd (N);
3023 Expr : constant Node_Id := Left_Opnd (N);
3024 Result : Match_Result;
3026 begin
3027 -- Ignore if error in either operand, except to make sure that Any_Type
3028 -- is properly propagated to avoid junk cascaded errors.
3030 if Etype (Expr) = Any_Type
3031 or else (Present (Choice) and then Etype (Choice) = Any_Type)
3032 then
3033 Set_Etype (N, Any_Type);
3034 return;
3035 end if;
3037 -- If left operand non-static, then nothing to do
3039 if not Is_Static_Expression (Expr) then
3040 return;
3041 end if;
3043 -- If choice is non-static, left operand is in non-static context
3045 if (Present (Choice) and then not Is_Static_Choice (Choice))
3046 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
3047 then
3048 Check_Non_Static_Context (Expr);
3049 return;
3050 end if;
3052 -- Otherwise we definitely have a static expression
3054 Set_Is_Static_Expression (N);
3056 -- If left operand raises Constraint_Error, propagate and we are done
3058 if Raises_Constraint_Error (Expr) then
3059 Set_Raises_Constraint_Error (N, True);
3061 -- See if we match
3063 else
3064 if Present (Choice) then
3065 Result := Choice_Matches (Expr, Choice);
3066 else
3067 Result := Choices_Match (Expr, Alts);
3068 end if;
3070 -- If result is Non_Static, it means that we raise Constraint_Error,
3071 -- since we already tested that the operands were themselves static.
3073 if Result = Non_Static then
3074 Set_Raises_Constraint_Error (N);
3076 -- Otherwise we have our result (flipped if NOT IN case)
3078 else
3079 Fold_Uint
3080 (N, Test (Result = Match xor Nkind (N) = N_Not_In), True);
3081 Warn_On_Known_Condition (N);
3082 end if;
3083 end if;
3084 end Eval_Membership_Op;
3086 ------------------------
3087 -- Eval_Named_Integer --
3088 ------------------------
3090 procedure Eval_Named_Integer (N : Node_Id) is
3091 begin
3092 Fold_Uint (N,
3093 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
3094 end Eval_Named_Integer;
3096 ---------------------
3097 -- Eval_Named_Real --
3098 ---------------------
3100 procedure Eval_Named_Real (N : Node_Id) is
3101 begin
3102 Fold_Ureal (N,
3103 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
3104 end Eval_Named_Real;
3106 -------------------
3107 -- Eval_Op_Expon --
3108 -------------------
3110 -- Exponentiation is a static functions, so the result is potentially
3111 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
3113 procedure Eval_Op_Expon (N : Node_Id) is
3114 Left : constant Node_Id := Left_Opnd (N);
3115 Right : constant Node_Id := Right_Opnd (N);
3116 Stat : Boolean;
3117 Fold : Boolean;
3119 begin
3120 -- If not foldable we are done
3122 Test_Expression_Is_Foldable
3123 (N, Left, Right, Stat, Fold, CRT_Safe => True);
3125 -- Return if not foldable
3127 if not Fold then
3128 return;
3129 end if;
3131 if Configurable_Run_Time_Mode and not Stat then
3132 return;
3133 end if;
3135 -- Fold exponentiation operation
3137 declare
3138 Right_Int : constant Uint := Expr_Value (Right);
3140 begin
3141 -- Integer case
3143 if Is_Integer_Type (Etype (Left)) then
3144 declare
3145 Left_Int : constant Uint := Expr_Value (Left);
3146 Result : Uint;
3148 begin
3149 -- Exponentiation of an integer raises Constraint_Error for a
3150 -- negative exponent (RM 4.5.6).
3152 if Right_Int < 0 then
3153 Apply_Compile_Time_Constraint_Error
3154 (N, "integer exponent negative", CE_Range_Check_Failed,
3155 Warn => not Stat);
3156 return;
3158 else
3159 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
3160 Result := Left_Int ** Right_Int;
3161 else
3162 Result := Left_Int;
3163 end if;
3165 if Is_Modular_Integer_Type (Etype (N)) then
3166 Result := Result mod Modulus (Etype (N));
3167 end if;
3169 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
3171 Fold_Uint (N, Result, Stat);
3172 end if;
3173 end;
3175 -- Real case
3177 else
3178 declare
3179 Left_Real : constant Ureal := Expr_Value_R (Left);
3181 begin
3182 -- Cannot have a zero base with a negative exponent
3184 if UR_Is_Zero (Left_Real) then
3186 if Right_Int < 0 then
3187 Apply_Compile_Time_Constraint_Error
3188 (N, "zero ** negative integer", CE_Range_Check_Failed,
3189 Warn => not Stat);
3190 return;
3191 else
3192 Fold_Ureal (N, Ureal_0, Stat);
3193 end if;
3195 else
3196 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
3197 end if;
3198 end;
3199 end if;
3200 end;
3201 end Eval_Op_Expon;
3203 -----------------
3204 -- Eval_Op_Not --
3205 -----------------
3207 -- The not operation is a static function, so the result is potentially
3208 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3210 procedure Eval_Op_Not (N : Node_Id) is
3211 Right : constant Node_Id := Right_Opnd (N);
3212 Stat : Boolean;
3213 Fold : Boolean;
3215 begin
3216 -- If not foldable we are done
3218 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3220 if not Fold then
3221 return;
3222 end if;
3224 -- Fold not operation
3226 declare
3227 Rint : constant Uint := Expr_Value (Right);
3228 Typ : constant Entity_Id := Etype (N);
3230 begin
3231 -- Negation is equivalent to subtracting from the modulus minus one.
3232 -- For a binary modulus this is equivalent to the ones-complement of
3233 -- the original value. For a nonbinary modulus this is an arbitrary
3234 -- but consistent definition.
3236 if Is_Modular_Integer_Type (Typ) then
3237 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3238 else pragma Assert (Is_Boolean_Type (Typ));
3239 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3240 end if;
3242 Set_Is_Static_Expression (N, Stat);
3243 end;
3244 end Eval_Op_Not;
3246 -------------------------------
3247 -- Eval_Qualified_Expression --
3248 -------------------------------
3250 -- A qualified expression is potentially static if its subtype mark denotes
3251 -- a static subtype and its expression is potentially static (RM 4.9 (10)).
3253 procedure Eval_Qualified_Expression (N : Node_Id) is
3254 Operand : constant Node_Id := Expression (N);
3255 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3257 Stat : Boolean;
3258 Fold : Boolean;
3259 Hex : Boolean;
3261 begin
3262 -- Can only fold if target is string or scalar and subtype is static.
3263 -- Also, do not fold if our parent is an allocator (this is because the
3264 -- qualified expression is really part of the syntactic structure of an
3265 -- allocator, and we do not want to end up with something that
3266 -- corresponds to "new 1" where the 1 is the result of folding a
3267 -- qualified expression).
3269 if not Is_Static_Subtype (Target_Type)
3270 or else Nkind (Parent (N)) = N_Allocator
3271 then
3272 Check_Non_Static_Context (Operand);
3274 -- If operand is known to raise Constraint_Error, set the flag on the
3275 -- expression so it does not get optimized away.
3277 if Nkind (Operand) = N_Raise_Constraint_Error then
3278 Set_Raises_Constraint_Error (N);
3279 end if;
3281 return;
3283 -- Also return if a semantic error has been posted on the node, as we
3284 -- don't want to fold in that case (for GNATprove, the node might lead
3285 -- to Constraint_Error but won't have been replaced with a raise node
3286 -- or marked as raising CE).
3288 elsif Error_Posted (N) then
3289 return;
3290 end if;
3292 -- If not foldable we are done
3294 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3296 if not Fold then
3297 return;
3299 -- Don't try fold if target type has Constraint_Error bounds
3301 elsif not Is_OK_Static_Subtype (Target_Type) then
3302 Set_Raises_Constraint_Error (N);
3303 return;
3304 end if;
3306 -- Fold the result of qualification
3308 if Is_Discrete_Type (Target_Type) then
3310 -- Save Print_In_Hex indication
3312 Hex := Nkind (Operand) = N_Integer_Literal
3313 and then Print_In_Hex (Operand);
3315 Fold_Uint (N, Expr_Value (Operand), Stat);
3317 -- Preserve Print_In_Hex indication
3319 if Hex and then Nkind (N) = N_Integer_Literal then
3320 Set_Print_In_Hex (N);
3321 end if;
3323 elsif Is_Real_Type (Target_Type) then
3324 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3326 else
3327 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3329 if not Stat then
3330 Set_Is_Static_Expression (N, False);
3331 else
3332 Check_String_Literal_Length (N, Target_Type);
3333 end if;
3335 return;
3336 end if;
3338 -- The expression may be foldable but not static
3340 Set_Is_Static_Expression (N, Stat);
3342 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3343 Out_Of_Range (N);
3344 end if;
3345 end Eval_Qualified_Expression;
3347 -----------------------
3348 -- Eval_Real_Literal --
3349 -----------------------
3351 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3352 -- as static by the analyzer. The reason we did it that early is to allow
3353 -- the possibility of turning off the Is_Static_Expression flag after
3354 -- analysis, but before resolution, when integer literals are generated
3355 -- in the expander that do not correspond to static expressions.
3357 procedure Eval_Real_Literal (N : Node_Id) is
3358 PK : constant Node_Kind := Nkind (Parent (N));
3360 begin
3361 -- If the literal appears in a non-expression context and not as part of
3362 -- a number declaration, then it is appearing in a non-static context,
3363 -- so check it.
3365 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3366 Check_Non_Static_Context (N);
3367 end if;
3368 end Eval_Real_Literal;
3370 ------------------------
3371 -- Eval_Relational_Op --
3372 ------------------------
3374 -- Relational operations are static functions, so the result is static if
3375 -- both operands are static (RM 4.9(7), 4.9(20)), except that up to Ada
3376 -- 2012, for strings the result is never static, even if the operands are.
3377 -- The string case was relaxed in Ada 2022, see AI12-0201.
3379 -- However, for internally generated nodes, we allow string equality and
3380 -- inequality to be static. This is because we rewrite A in "ABC" as an
3381 -- equality test A = "ABC", and the former is definitely static.
3383 procedure Eval_Relational_Op (N : Node_Id) is
3384 Left : constant Node_Id := Left_Opnd (N);
3385 Right : constant Node_Id := Right_Opnd (N);
3387 procedure Decompose_Expr
3388 (Expr : Node_Id;
3389 Ent : out Entity_Id;
3390 Kind : out Character;
3391 Cons : out Uint;
3392 Orig : Boolean := True);
3393 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3394 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3395 -- simple entity, and Cons is the value of K. If the expression is not
3396 -- of the required form, Ent is set to Empty.
3398 -- Orig indicates whether Expr is the original expression to consider,
3399 -- or if we are handling a subexpression (e.g. recursive call to
3400 -- Decompose_Expr).
3402 procedure Fold_General_Op (Is_Static : Boolean);
3403 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3404 -- be set when the operator denotes a static expression.
3406 procedure Fold_Static_Real_Op;
3407 -- Attempt to fold static real type relational operator N
3409 function Static_Length (Expr : Node_Id) return Uint;
3410 -- If Expr is an expression for a constrained array whose length is
3411 -- known at compile time, return the non-negative length, otherwise
3412 -- return -1.
3414 --------------------
3415 -- Decompose_Expr --
3416 --------------------
3418 procedure Decompose_Expr
3419 (Expr : Node_Id;
3420 Ent : out Entity_Id;
3421 Kind : out Character;
3422 Cons : out Uint;
3423 Orig : Boolean := True)
3425 Exp : Node_Id;
3427 begin
3428 -- Assume that the expression does not meet the expected form
3430 Cons := No_Uint;
3431 Ent := Empty;
3432 Kind := '?';
3434 if Nkind (Expr) = N_Op_Add
3435 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3436 then
3437 Exp := Left_Opnd (Expr);
3438 Cons := Expr_Value (Right_Opnd (Expr));
3440 elsif Nkind (Expr) = N_Op_Subtract
3441 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3442 then
3443 Exp := Left_Opnd (Expr);
3444 Cons := -Expr_Value (Right_Opnd (Expr));
3446 -- If the bound is a constant created to remove side effects, recover
3447 -- the original expression to see if it has one of the recognizable
3448 -- forms.
3450 elsif Nkind (Expr) = N_Identifier
3451 and then not Comes_From_Source (Entity (Expr))
3452 and then Ekind (Entity (Expr)) = E_Constant
3453 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3454 then
3455 Exp := Expression (Parent (Entity (Expr)));
3456 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3458 -- If original expression includes an entity, create a reference
3459 -- to it for use below.
3461 if Present (Ent) then
3462 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3463 else
3464 return;
3465 end if;
3467 else
3468 -- Only consider the case of X + 0 for a full expression, and
3469 -- not when recursing, otherwise we may end up with evaluating
3470 -- expressions not known at compile time to 0.
3472 if Orig then
3473 Exp := Expr;
3474 Cons := Uint_0;
3475 else
3476 return;
3477 end if;
3478 end if;
3480 -- At this stage Exp is set to the potential X
3482 if Nkind (Exp) = N_Attribute_Reference then
3483 if Attribute_Name (Exp) = Name_First then
3484 Kind := 'F';
3485 elsif Attribute_Name (Exp) = Name_Last then
3486 Kind := 'L';
3487 else
3488 return;
3489 end if;
3491 Exp := Prefix (Exp);
3493 else
3494 Kind := 'E';
3495 end if;
3497 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3498 Ent := Entity (Exp);
3499 end if;
3500 end Decompose_Expr;
3502 ---------------------
3503 -- Fold_General_Op --
3504 ---------------------
3506 procedure Fold_General_Op (Is_Static : Boolean) is
3507 CR : constant Compare_Result :=
3508 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3510 Result : Boolean;
3512 begin
3513 if CR = Unknown then
3514 return;
3515 end if;
3517 case Nkind (N) is
3518 when N_Op_Eq =>
3519 if CR = EQ then
3520 Result := True;
3521 elsif CR = NE or else CR = GT or else CR = LT then
3522 Result := False;
3523 else
3524 return;
3525 end if;
3527 when N_Op_Ge =>
3528 if CR = GT or else CR = EQ or else CR = GE then
3529 Result := True;
3530 elsif CR = LT then
3531 Result := False;
3532 else
3533 return;
3534 end if;
3536 when N_Op_Gt =>
3537 if CR = GT then
3538 Result := True;
3539 elsif CR = EQ or else CR = LT or else CR = LE then
3540 Result := False;
3541 else
3542 return;
3543 end if;
3545 when N_Op_Le =>
3546 if CR = LT or else CR = EQ or else CR = LE then
3547 Result := True;
3548 elsif CR = GT then
3549 Result := False;
3550 else
3551 return;
3552 end if;
3554 when N_Op_Lt =>
3555 if CR = LT then
3556 Result := True;
3557 elsif CR = EQ or else CR = GT or else CR = GE then
3558 Result := False;
3559 else
3560 return;
3561 end if;
3563 when N_Op_Ne =>
3564 if CR = NE or else CR = GT or else CR = LT then
3565 Result := True;
3566 elsif CR = EQ then
3567 Result := False;
3568 else
3569 return;
3570 end if;
3572 when others =>
3573 raise Program_Error;
3574 end case;
3576 -- Determine the potential outcome of the relation assuming the
3577 -- operands are valid and emit a warning when the relation yields
3578 -- True or False only in the presence of invalid values.
3580 Warn_On_Constant_Valid_Condition (N);
3582 Fold_Uint (N, Test (Result), Is_Static);
3583 end Fold_General_Op;
3585 -------------------------
3586 -- Fold_Static_Real_Op --
3587 -------------------------
3589 procedure Fold_Static_Real_Op is
3590 Left_Real : constant Ureal := Expr_Value_R (Left);
3591 Right_Real : constant Ureal := Expr_Value_R (Right);
3592 Result : Boolean;
3594 begin
3595 case Nkind (N) is
3596 when N_Op_Eq => Result := (Left_Real = Right_Real);
3597 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3598 when N_Op_Gt => Result := (Left_Real > Right_Real);
3599 when N_Op_Le => Result := (Left_Real <= Right_Real);
3600 when N_Op_Lt => Result := (Left_Real < Right_Real);
3601 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3602 when others => raise Program_Error;
3603 end case;
3605 Fold_Uint (N, Test (Result), True);
3606 end Fold_Static_Real_Op;
3608 -------------------
3609 -- Static_Length --
3610 -------------------
3612 function Static_Length (Expr : Node_Id) return Uint is
3613 Cons1 : Uint;
3614 Cons2 : Uint;
3615 Ent1 : Entity_Id;
3616 Ent2 : Entity_Id;
3617 Kind1 : Character;
3618 Kind2 : Character;
3619 Typ : Entity_Id;
3621 begin
3622 -- First easy case string literal
3624 if Nkind (Expr) = N_String_Literal then
3625 return UI_From_Int (String_Length (Strval (Expr)));
3627 -- With frontend inlining as performed in GNATprove mode, a variable
3628 -- may be inserted that has a string literal subtype. Deal with this
3629 -- specially as for the previous case.
3631 elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then
3632 return String_Literal_Length (Etype (Expr));
3634 -- Second easy case, not constrained subtype, so no length
3636 elsif not Is_Constrained (Etype (Expr)) then
3637 return Uint_Minus_1;
3638 end if;
3640 -- General case
3642 Typ := Etype (First_Index (Etype (Expr)));
3644 -- The simple case, both bounds are known at compile time
3646 if Is_Discrete_Type (Typ)
3647 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3648 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3649 then
3650 return
3651 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3652 Expr_Value (Type_Low_Bound (Typ)) + 1);
3653 end if;
3655 -- A more complex case, where the bounds are of the form X [+/- K1]
3656 -- .. X [+/- K2]), where X is an expression that is either A'First or
3657 -- A'Last (with A an entity name), or X is an entity name, and the
3658 -- two X's are the same and K1 and K2 are known at compile time, in
3659 -- this case, the length can also be computed at compile time, even
3660 -- though the bounds are not known. A common case of this is e.g.
3661 -- (X'First .. X'First+5).
3663 Decompose_Expr
3664 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3665 Decompose_Expr
3666 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3668 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3669 return Cons2 - Cons1 + 1;
3670 else
3671 return Uint_Minus_1;
3672 end if;
3673 end Static_Length;
3675 -- Local variables
3677 Left_Typ : constant Entity_Id := Etype (Left);
3678 Right_Typ : constant Entity_Id := Etype (Right);
3679 Fold : Boolean;
3680 Left_Len : Uint;
3681 Op_Typ : Entity_Id := Empty;
3682 Right_Len : Uint;
3684 Is_Static_Expression : Boolean;
3686 -- Start of processing for Eval_Relational_Op
3688 begin
3689 -- One special case to deal with first. If we can tell that the result
3690 -- will be false because the lengths of one or more index subtypes are
3691 -- compile-time known and different, then we can replace the entire
3692 -- result by False. We only do this for one-dimensional arrays, because
3693 -- the case of multidimensional arrays is rare and too much trouble. If
3694 -- one of the operands is an illegal aggregate, its type might still be
3695 -- an arbitrary composite type, so nothing to do.
3697 if Is_Array_Type (Left_Typ)
3698 and then Left_Typ /= Any_Composite
3699 and then Number_Dimensions (Left_Typ) = 1
3700 and then Nkind (N) in N_Op_Eq | N_Op_Ne
3701 then
3702 if Raises_Constraint_Error (Left)
3703 or else
3704 Raises_Constraint_Error (Right)
3705 then
3706 return;
3707 end if;
3709 -- OK, we have the case where we may be able to do this fold
3711 Left_Len := Static_Length (Left);
3712 Right_Len := Static_Length (Right);
3714 if Left_Len /= Uint_Minus_1
3715 and then Right_Len /= Uint_Minus_1
3716 and then Left_Len /= Right_Len
3717 then
3718 -- AI12-0201: comparison of string is static in Ada 2022
3720 Fold_Uint
3722 Test (Nkind (N) = N_Op_Ne),
3723 Static => Ada_Version >= Ada_2022
3724 and then Is_String_Type (Left_Typ));
3725 Warn_On_Known_Condition (N);
3726 return;
3727 end if;
3728 end if;
3730 -- General case
3732 -- Initialize the value of Is_Static_Expression. The value of Fold
3733 -- returned by Test_Expression_Is_Foldable is not needed since, even
3734 -- when some operand is a variable, we can still perform the static
3735 -- evaluation of the expression in some cases (for example, for a
3736 -- variable of a subtype of Integer we statically know that any value
3737 -- stored in such variable is smaller than Integer'Last).
3739 Test_Expression_Is_Foldable
3740 (N, Left, Right, Is_Static_Expression, Fold);
3742 -- Comparisons of scalars can give static results.
3743 -- In addition starting with Ada 2022 (AI12-0201), comparison of strings
3744 -- can also give static results, and as noted above, we also allow for
3745 -- earlier Ada versions internally generated equality and inequality for
3746 -- strings.
3747 -- The Comes_From_Source test below isn't correct and will accept
3748 -- some cases that are illegal in Ada 2012 and before. Now that Ada
3749 -- 2022 has relaxed the rules, this doesn't really matter.
3751 if Is_String_Type (Left_Typ) then
3752 if Ada_Version < Ada_2022
3753 and then (Comes_From_Source (N)
3754 or else Nkind (N) not in N_Op_Eq | N_Op_Ne)
3755 then
3756 Is_Static_Expression := False;
3757 Set_Is_Static_Expression (N, False);
3758 end if;
3760 elsif not Is_Scalar_Type (Left_Typ) then
3761 Is_Static_Expression := False;
3762 Set_Is_Static_Expression (N, False);
3763 end if;
3765 -- For operators on universal numeric types called as functions with an
3766 -- explicit scope, determine appropriate specific numeric type, and
3767 -- diagnose possible ambiguity.
3769 if Is_Universal_Numeric_Type (Left_Typ)
3770 and then
3771 Is_Universal_Numeric_Type (Right_Typ)
3772 then
3773 Op_Typ := Find_Universal_Operator_Type (N);
3774 end if;
3776 -- Attempt to fold the relational operator
3778 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3779 Fold_Static_Real_Op;
3780 else
3781 Fold_General_Op (Is_Static_Expression);
3782 end if;
3784 -- For the case of a folded relational operator on a specific numeric
3785 -- type, freeze the operand type now.
3787 if Present (Op_Typ) then
3788 Freeze_Before (N, Op_Typ);
3789 end if;
3791 Warn_On_Known_Condition (N);
3792 end Eval_Relational_Op;
3794 -----------------------------
3795 -- Eval_Selected_Component --
3796 -----------------------------
3798 procedure Eval_Selected_Component (N : Node_Id) is
3799 Node : Node_Id;
3800 Comp : Node_Id;
3801 C : Node_Id;
3802 Nam : Name_Id;
3804 begin
3805 -- If an attribute reference or a LHS, nothing to do.
3806 -- Also do not fold if N is an [in] out subprogram parameter.
3807 -- Fold will perform the other relevant tests.
3809 if Nkind (Parent (N)) /= N_Attribute_Reference
3810 and then not Known_To_Be_Assigned (N)
3811 and then not Is_Actual_Out_Or_In_Out_Parameter (N)
3812 then
3813 -- Simplify a selected_component on an aggregate by extracting
3814 -- the field directly.
3816 Node := Unqualify (Prefix (N));
3818 if Nkind (Node) = N_Aggregate
3819 and then Compile_Time_Known_Aggregate (Node)
3820 then
3821 Comp := First (Component_Associations (Node));
3822 Nam := Chars (Selector_Name (N));
3824 while Present (Comp) loop
3825 C := First (Choices (Comp));
3827 while Present (C) loop
3828 if Chars (C) = Nam then
3829 Rewrite (N, Relocate_Node (Expression (Comp)));
3830 return;
3831 end if;
3833 Next (C);
3834 end loop;
3836 Next (Comp);
3837 end loop;
3838 else
3839 Fold (N);
3840 end if;
3841 end if;
3842 end Eval_Selected_Component;
3844 ----------------
3845 -- Eval_Shift --
3846 ----------------
3848 procedure Eval_Shift (N : Node_Id) is
3849 begin
3850 -- This procedure is only called for compiler generated code (e.g.
3851 -- packed arrays), so there is nothing to do except attempting to fold
3852 -- the expression.
3854 Fold_Shift (N, Left_Opnd (N), Right_Opnd (N), Nkind (N));
3855 end Eval_Shift;
3857 ------------------------
3858 -- Eval_Short_Circuit --
3859 ------------------------
3861 -- A short circuit operation is potentially static if both operands are
3862 -- potentially static (RM 4.9 (13)).
3864 procedure Eval_Short_Circuit (N : Node_Id) is
3865 Kind : constant Node_Kind := Nkind (N);
3866 Left : constant Node_Id := Left_Opnd (N);
3867 Right : constant Node_Id := Right_Opnd (N);
3868 Left_Int : Uint;
3870 Rstat : constant Boolean :=
3871 Is_Static_Expression (Left)
3872 and then
3873 Is_Static_Expression (Right);
3875 begin
3876 -- Short circuit operations are never static in Ada 83
3878 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3879 Check_Non_Static_Context (Left);
3880 Check_Non_Static_Context (Right);
3881 return;
3882 end if;
3884 -- Now look at the operands, we can't quite use the normal call to
3885 -- Test_Expression_Is_Foldable here because short circuit operations
3886 -- are a special case, they can still be foldable, even if the right
3887 -- operand raises Constraint_Error.
3889 -- If either operand is Any_Type, just propagate to result and do not
3890 -- try to fold, this prevents cascaded errors.
3892 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3893 Set_Etype (N, Any_Type);
3894 return;
3896 -- If left operand raises Constraint_Error, then replace node N with
3897 -- the raise Constraint_Error node, and we are obviously not foldable.
3898 -- Is_Static_Expression is set from the two operands in the normal way,
3899 -- and we check the right operand if it is in a non-static context.
3901 elsif Raises_Constraint_Error (Left) then
3902 if not Rstat then
3903 Check_Non_Static_Context (Right);
3904 end if;
3906 Rewrite_In_Raise_CE (N, Left);
3907 Set_Is_Static_Expression (N, Rstat);
3908 return;
3910 -- If the result is not static, then we won't in any case fold
3912 elsif not Rstat then
3913 Check_Non_Static_Context (Left);
3914 Check_Non_Static_Context (Right);
3915 return;
3916 end if;
3918 -- Here the result is static, note that, unlike the normal processing
3919 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3920 -- the right operand raises Constraint_Error, that's because it is not
3921 -- significant if the left operand is decisive.
3923 Set_Is_Static_Expression (N);
3925 -- It does not matter if the right operand raises Constraint_Error if
3926 -- it will not be evaluated. So deal specially with the cases where
3927 -- the right operand is not evaluated. Note that we will fold these
3928 -- cases even if the right operand is non-static, which is fine, but
3929 -- of course in these cases the result is not potentially static.
3931 Left_Int := Expr_Value (Left);
3933 if (Kind = N_And_Then and then Is_False (Left_Int))
3934 or else
3935 (Kind = N_Or_Else and then Is_True (Left_Int))
3936 then
3937 Fold_Uint (N, Left_Int, Rstat);
3938 return;
3939 end if;
3941 -- If first operand not decisive, then it does matter if the right
3942 -- operand raises Constraint_Error, since it will be evaluated, so
3943 -- we simply replace the node with the right operand. Note that this
3944 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3945 -- (both are set to True in Right).
3947 if Raises_Constraint_Error (Right) then
3948 Rewrite_In_Raise_CE (N, Right);
3949 Check_Non_Static_Context (Left);
3950 return;
3951 end if;
3953 -- Otherwise the result depends on the right operand
3955 Fold_Uint (N, Expr_Value (Right), Rstat);
3956 return;
3957 end Eval_Short_Circuit;
3959 ----------------
3960 -- Eval_Slice --
3961 ----------------
3963 -- Slices can never be static, so the only processing required is to check
3964 -- for non-static context if an explicit range is given.
3966 procedure Eval_Slice (N : Node_Id) is
3967 Drange : constant Node_Id := Discrete_Range (N);
3968 Name : constant Node_Id := Prefix (N);
3970 begin
3971 if Nkind (Drange) = N_Range then
3972 Check_Non_Static_Context (Low_Bound (Drange));
3973 Check_Non_Static_Context (High_Bound (Drange));
3974 end if;
3976 -- A slice of the form A (subtype), when the subtype is the index of
3977 -- the type of A, is redundant, the slice can be replaced with A, and
3978 -- this is worth a warning.
3980 if Is_Entity_Name (Name) then
3981 declare
3982 E : constant Entity_Id := Entity (Name);
3983 T : constant Entity_Id := Etype (E);
3985 begin
3986 if Is_Object (E)
3987 and then Is_Array_Type (T)
3988 and then Is_Entity_Name (Drange)
3989 then
3990 if Is_Entity_Name (Original_Node (First_Index (T)))
3991 and then Entity (Original_Node (First_Index (T)))
3992 = Entity (Drange)
3993 then
3994 if Warn_On_Redundant_Constructs then
3995 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3996 end if;
3998 -- The following might be a useful optimization???
4000 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
4001 end if;
4002 end if;
4003 end;
4004 end if;
4005 end Eval_Slice;
4007 -------------------------
4008 -- Eval_String_Literal --
4009 -------------------------
4011 procedure Eval_String_Literal (N : Node_Id) is
4012 Typ : constant Entity_Id := Etype (N);
4013 Bas : constant Entity_Id := Base_Type (Typ);
4014 Xtp : Entity_Id;
4015 Len : Nat;
4016 Lo : Node_Id;
4018 begin
4019 -- Nothing to do if error type (handles cases like default expressions
4020 -- or generics where we have not yet fully resolved the type).
4022 if Bas = Any_Type or else Bas = Any_String then
4023 return;
4024 end if;
4026 -- String literals are static if the subtype is static (RM 4.9(2)), so
4027 -- reset the static expression flag (it was set unconditionally in
4028 -- Analyze_String_Literal) if the subtype is non-static. We tell if
4029 -- the subtype is static by looking at the lower bound.
4031 if Ekind (Typ) = E_String_Literal_Subtype then
4032 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
4033 Set_Is_Static_Expression (N, False);
4034 return;
4035 end if;
4037 -- Here if Etype of string literal is normal Etype (not yet possible,
4038 -- but may be possible in future).
4040 elsif not Is_OK_Static_Expression
4041 (Type_Low_Bound (Etype (First_Index (Typ))))
4042 then
4043 Set_Is_Static_Expression (N, False);
4044 return;
4045 end if;
4047 -- If original node was a type conversion, then result if non-static
4048 -- up to Ada 2012. AI12-0201 changes that with Ada 2022.
4050 if Nkind (Original_Node (N)) = N_Type_Conversion
4051 and then Ada_Version <= Ada_2012
4052 then
4053 Set_Is_Static_Expression (N, False);
4054 return;
4055 end if;
4057 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
4058 -- if its bounds are outside the index base type and this index type is
4059 -- static. This can happen in only two ways. Either the string literal
4060 -- is too long, or it is null, and the lower bound is type'First. Either
4061 -- way it is the upper bound that is out of range of the index type.
4063 if Ada_Version >= Ada_95 then
4064 if Is_Standard_String_Type (Bas) then
4065 Xtp := Standard_Positive;
4066 else
4067 Xtp := Etype (First_Index (Bas));
4068 end if;
4070 if Ekind (Typ) = E_String_Literal_Subtype then
4071 Lo := String_Literal_Low_Bound (Typ);
4072 else
4073 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
4074 end if;
4076 -- Check for string too long
4078 Len := String_Length (Strval (N));
4080 if Len > String_Type_Len (Bas) then
4082 -- Issue message. Note that this message is a warning if the
4083 -- string literal is not marked as static (happens in some cases
4084 -- of folding strings known at compile time, but not static).
4085 -- Furthermore in such cases, we reword the message, since there
4086 -- is no string literal in the source program.
4088 if Is_Static_Expression (N) then
4089 Apply_Compile_Time_Constraint_Error
4090 (N, "string literal too long for}", CE_Length_Check_Failed,
4091 Ent => Bas,
4092 Typ => First_Subtype (Bas));
4093 else
4094 Apply_Compile_Time_Constraint_Error
4095 (N, "string value too long for}", CE_Length_Check_Failed,
4096 Ent => Bas,
4097 Typ => First_Subtype (Bas),
4098 Warn => True);
4099 end if;
4101 -- Test for null string not allowed
4103 elsif Len = 0
4104 and then not Is_Generic_Type (Xtp)
4105 and then
4106 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
4107 then
4108 -- Same specialization of message
4110 if Is_Static_Expression (N) then
4111 Apply_Compile_Time_Constraint_Error
4112 (N, "null string literal not allowed for}",
4113 CE_Length_Check_Failed,
4114 Ent => Bas,
4115 Typ => First_Subtype (Bas));
4116 else
4117 Apply_Compile_Time_Constraint_Error
4118 (N, "null string value not allowed for}",
4119 CE_Length_Check_Failed,
4120 Ent => Bas,
4121 Typ => First_Subtype (Bas),
4122 Warn => True);
4123 end if;
4124 end if;
4125 end if;
4126 end Eval_String_Literal;
4128 --------------------------
4129 -- Eval_Type_Conversion --
4130 --------------------------
4132 -- A type conversion is potentially static if its subtype mark is for a
4133 -- static scalar subtype, and its operand expression is potentially static
4134 -- (RM 4.9(10)).
4135 -- Also add support for static string types.
4137 procedure Eval_Type_Conversion (N : Node_Id) is
4138 Operand : constant Node_Id := Expression (N);
4139 Source_Type : constant Entity_Id := Etype (Operand);
4140 Target_Type : constant Entity_Id := Etype (N);
4142 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
4143 -- Returns true if type T is an integer type, or if it is a fixed-point
4144 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
4145 -- on the conversion node).
4147 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
4148 -- Returns true if type T is a floating-point type, or if it is a
4149 -- fixed-point type that is not to be treated as an integer (i.e. the
4150 -- flag Conversion_OK is not set on the conversion node).
4152 ------------------------------
4153 -- To_Be_Treated_As_Integer --
4154 ------------------------------
4156 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
4157 begin
4158 return
4159 Is_Integer_Type (T)
4160 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
4161 end To_Be_Treated_As_Integer;
4163 ---------------------------
4164 -- To_Be_Treated_As_Real --
4165 ---------------------------
4167 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
4168 begin
4169 return
4170 Is_Floating_Point_Type (T)
4171 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
4172 end To_Be_Treated_As_Real;
4174 -- Local variables
4176 Fold : Boolean;
4177 Stat : Boolean;
4179 -- Start of processing for Eval_Type_Conversion
4181 begin
4182 -- Cannot fold if target type is non-static or if semantic error
4184 if not Is_Static_Subtype (Target_Type) then
4185 Check_Non_Static_Context (Operand);
4186 return;
4187 elsif Error_Posted (N) then
4188 return;
4189 end if;
4191 -- If not foldable we are done
4193 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
4195 if not Fold then
4196 return;
4198 -- Don't try fold if target type has Constraint_Error bounds
4200 elsif not Is_OK_Static_Subtype (Target_Type) then
4201 Set_Raises_Constraint_Error (N);
4202 return;
4203 end if;
4205 -- Remaining processing depends on operand types. Note that in the
4206 -- following type test, fixed-point counts as real unless the flag
4207 -- Conversion_OK is set, in which case it counts as integer.
4209 -- Fold conversion, case of string type. The result is static starting
4210 -- with Ada 2022 (AI12-0201).
4212 if Is_String_Type (Target_Type) then
4213 Fold_Str
4215 Strval (Get_String_Val (Operand)),
4216 Static => Ada_Version >= Ada_2022);
4217 return;
4219 -- Fold conversion, case of integer target type
4221 elsif To_Be_Treated_As_Integer (Target_Type) then
4222 declare
4223 Result : Uint;
4225 begin
4226 -- Integer to integer conversion
4228 if To_Be_Treated_As_Integer (Source_Type) then
4229 Result := Expr_Value (Operand);
4231 -- Real to integer conversion
4233 elsif To_Be_Treated_As_Real (Source_Type) then
4234 Result := UR_To_Uint (Expr_Value_R (Operand));
4236 -- Enumeration to integer conversion, aka 'Enum_Rep
4238 else
4239 Result := Expr_Rep_Value (Operand);
4240 end if;
4242 -- If fixed-point type (Conversion_OK must be set), then the
4243 -- result is logically an integer, but we must replace the
4244 -- conversion with the corresponding real literal, since the
4245 -- type from a semantic point of view is still fixed-point.
4247 if Is_Fixed_Point_Type (Target_Type) then
4248 Fold_Ureal
4249 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
4251 -- Otherwise result is integer literal
4253 else
4254 Fold_Uint (N, Result, Stat);
4255 end if;
4256 end;
4258 -- Fold conversion, case of real target type
4260 elsif To_Be_Treated_As_Real (Target_Type) then
4261 declare
4262 Result : Ureal;
4264 begin
4265 if To_Be_Treated_As_Real (Source_Type) then
4266 Result := Expr_Value_R (Operand);
4267 else
4268 Result := UR_From_Uint (Expr_Value (Operand));
4269 end if;
4271 Fold_Ureal (N, Result, Stat);
4272 end;
4274 -- Enumeration types
4276 else
4277 Fold_Uint (N, Expr_Value (Operand), Stat);
4278 end if;
4280 -- If the target is a static floating-point subtype, then its bounds
4281 -- are machine numbers so we must consider the machine-rounded value.
4283 if Is_Floating_Point_Type (Target_Type)
4284 and then Nkind (N) = N_Real_Literal
4285 and then not Is_Machine_Number (N)
4286 then
4287 declare
4288 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
4289 Hi : constant Node_Id := Type_High_Bound (Target_Type);
4290 Valr : constant Ureal :=
4291 Machine_Number (Target_Type, Expr_Value_R (N), N);
4292 begin
4293 if Valr < Expr_Value_R (Lo) or else Valr > Expr_Value_R (Hi) then
4294 Out_Of_Range (N);
4295 end if;
4296 end;
4298 elsif Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
4299 Out_Of_Range (N);
4300 end if;
4301 end Eval_Type_Conversion;
4303 -------------------
4304 -- Eval_Unary_Op --
4305 -------------------
4307 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4308 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4310 procedure Eval_Unary_Op (N : Node_Id) is
4311 Right : constant Node_Id := Right_Opnd (N);
4312 Otype : Entity_Id := Empty;
4313 Stat : Boolean;
4314 Fold : Boolean;
4316 begin
4317 -- If not foldable we are done
4319 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
4321 if not Fold then
4322 return;
4323 end if;
4325 if Is_Universal_Numeric_Type (Etype (Right)) then
4326 Otype := Find_Universal_Operator_Type (N);
4327 end if;
4329 -- Fold for integer case
4331 if Is_Integer_Type (Etype (N)) then
4332 declare
4333 Rint : constant Uint := Expr_Value (Right);
4334 Result : Uint;
4336 begin
4337 -- In the case of modular unary plus and abs there is no need
4338 -- to adjust the result of the operation since if the original
4339 -- operand was in bounds the result will be in the bounds of the
4340 -- modular type. However, in the case of modular unary minus the
4341 -- result may go out of the bounds of the modular type and needs
4342 -- adjustment.
4344 if Nkind (N) = N_Op_Plus then
4345 Result := Rint;
4347 elsif Nkind (N) = N_Op_Minus then
4348 if Is_Modular_Integer_Type (Etype (N)) then
4349 Result := (-Rint) mod Modulus (Etype (N));
4350 else
4351 Result := (-Rint);
4352 end if;
4354 else
4355 pragma Assert (Nkind (N) = N_Op_Abs);
4356 Result := abs Rint;
4357 end if;
4359 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
4361 Fold_Uint (N, Result, Stat);
4362 end;
4364 -- Fold for real case
4366 elsif Is_Real_Type (Etype (N)) then
4367 declare
4368 Rreal : constant Ureal := Expr_Value_R (Right);
4369 Result : Ureal;
4371 begin
4372 if Nkind (N) = N_Op_Plus then
4373 Result := Rreal;
4374 elsif Nkind (N) = N_Op_Minus then
4375 Result := UR_Negate (Rreal);
4376 else
4377 pragma Assert (Nkind (N) = N_Op_Abs);
4378 Result := abs Rreal;
4379 end if;
4381 Fold_Ureal (N, Result, Stat);
4382 end;
4383 end if;
4385 -- If the operator was resolved to a specific type, make sure that type
4386 -- is frozen even if the expression is folded into a literal (which has
4387 -- a universal type).
4389 if Present (Otype) then
4390 Freeze_Before (N, Otype);
4391 end if;
4392 end Eval_Unary_Op;
4394 -------------------------------
4395 -- Eval_Unchecked_Conversion --
4396 -------------------------------
4398 -- Unchecked conversions can never be static, so the only required
4399 -- processing is to check for a non-static context for the operand.
4401 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4402 Target_Type : constant Entity_Id := Etype (N);
4403 Operand : constant Node_Id := Expression (N);
4404 Operand_Type : constant Entity_Id := Etype (Operand);
4406 begin
4407 Check_Non_Static_Context (Operand);
4409 -- If we have a conversion of a compile time known value to a target
4410 -- type and the value is in range of the target type, then we can simply
4411 -- replace the construct by an integer literal of the correct type. We
4412 -- only apply this to discrete types being converted. Possibly it may
4413 -- apply in other cases, but it is too much trouble to worry about.
4415 -- Note that we do not do this transformation if the Kill_Range_Check
4416 -- flag is set, since then the value may be outside the expected range.
4417 -- This happens in the Normalize_Scalars case.
4419 -- We also skip this if either the target or operand type is biased
4420 -- because in this case, the unchecked conversion is supposed to
4421 -- preserve the bit pattern, not the integer value.
4423 if Is_Integer_Type (Target_Type)
4424 and then not Has_Biased_Representation (Target_Type)
4425 and then Is_Discrete_Type (Operand_Type)
4426 and then not Has_Biased_Representation (Operand_Type)
4427 and then Compile_Time_Known_Value (Operand)
4428 and then not Kill_Range_Check (N)
4429 then
4430 declare
4431 Val : constant Uint := Expr_Rep_Value (Operand);
4433 begin
4434 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
4435 and then
4436 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
4437 and then
4438 Val >= Expr_Value (Type_Low_Bound (Target_Type))
4439 and then
4440 Val <= Expr_Value (Type_High_Bound (Target_Type))
4441 then
4442 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
4444 -- If Address is the target type, just set the type to avoid a
4445 -- spurious type error on the literal when Address is a visible
4446 -- integer type.
4448 if Is_Descendant_Of_Address (Target_Type) then
4449 Set_Etype (N, Target_Type);
4450 else
4451 Analyze_And_Resolve (N, Target_Type);
4452 end if;
4454 return;
4455 end if;
4456 end;
4457 end if;
4458 end Eval_Unchecked_Conversion;
4460 --------------------
4461 -- Expr_Rep_Value --
4462 --------------------
4464 function Expr_Rep_Value (N : Node_Id) return Uint is
4465 Kind : constant Node_Kind := Nkind (N);
4466 Ent : Entity_Id;
4468 begin
4469 if Is_Entity_Name (N) then
4470 Ent := Entity (N);
4472 -- An enumeration literal that was either in the source or created
4473 -- as a result of static evaluation.
4475 if Ekind (Ent) = E_Enumeration_Literal then
4476 return Enumeration_Rep (Ent);
4478 -- A user defined static constant
4480 else
4481 pragma Assert (Ekind (Ent) = E_Constant);
4482 return Expr_Rep_Value (Constant_Value (Ent));
4483 end if;
4485 -- An integer literal that was either in the source or created as a
4486 -- result of static evaluation.
4488 elsif Kind = N_Integer_Literal then
4489 return Intval (N);
4491 -- A real literal for a fixed-point type. This must be the fixed-point
4492 -- case, either the literal is of a fixed-point type, or it is a bound
4493 -- of a fixed-point type, with type universal real. In either case we
4494 -- obtain the desired value from Corresponding_Integer_Value.
4496 elsif Kind = N_Real_Literal then
4497 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4498 return Corresponding_Integer_Value (N);
4500 -- The NULL access value
4502 elsif Kind = N_Null then
4503 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4504 or else Error_Posted (N));
4505 return Uint_0;
4507 -- Character literal
4509 elsif Kind = N_Character_Literal then
4510 Ent := Entity (N);
4512 -- Since Character literals of type Standard.Character don't have any
4513 -- defining character literals built for them, they do not have their
4514 -- Entity set, so just use their Char code. Otherwise for user-
4515 -- defined character literals use their Pos value as usual which is
4516 -- the same as the Rep value.
4518 if No (Ent) then
4519 return Char_Literal_Value (N);
4520 else
4521 return Enumeration_Rep (Ent);
4522 end if;
4524 -- Unchecked conversion, which can come from System'To_Address (X)
4525 -- where X is a static integer expression. Recursively evaluate X.
4527 elsif Kind = N_Unchecked_Type_Conversion then
4528 return Expr_Rep_Value (Expression (N));
4530 -- Static discriminant value
4532 elsif Is_Static_Discriminant_Component (N) then
4533 return Expr_Rep_Value
4534 (Get_Discriminant_Value
4535 (Entity (Selector_Name (N)),
4536 Etype (Prefix (N)),
4537 Discriminant_Constraint (Etype (Prefix (N)))));
4539 else
4540 raise Program_Error;
4541 end if;
4542 end Expr_Rep_Value;
4544 ----------------
4545 -- Expr_Value --
4546 ----------------
4548 function Expr_Value (N : Node_Id) return Uint is
4549 Kind : constant Node_Kind := Nkind (N);
4550 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4551 Ent : Entity_Id;
4552 Val : Uint;
4554 begin
4555 -- If already in cache, then we know it's compile-time-known and we can
4556 -- return the value that was previously stored in the cache since
4557 -- compile-time-known values cannot change.
4559 if CV_Ent.N = N then
4560 return CV_Ent.V;
4561 end if;
4563 -- Otherwise proceed to test value
4565 if Is_Entity_Name (N) then
4566 Ent := Entity (N);
4568 -- An enumeration literal that was either in the source or created as
4569 -- a result of static evaluation.
4571 if Ekind (Ent) = E_Enumeration_Literal then
4572 Val := Enumeration_Pos (Ent);
4574 -- A user defined static constant
4576 else
4577 pragma Assert (Ekind (Ent) = E_Constant);
4578 Val := Expr_Value (Constant_Value (Ent));
4579 end if;
4581 -- An integer literal that was either in the source or created as a
4582 -- result of static evaluation.
4584 elsif Kind = N_Integer_Literal then
4585 Val := Intval (N);
4587 -- A real literal for a fixed-point type. This must be the fixed-point
4588 -- case, either the literal is of a fixed-point type, or it is a bound
4589 -- of a fixed-point type, with type universal real. In either case we
4590 -- obtain the desired value from Corresponding_Integer_Value.
4592 elsif Kind = N_Real_Literal then
4593 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4594 Val := Corresponding_Integer_Value (N);
4596 -- The NULL access value
4598 elsif Kind = N_Null then
4599 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4600 or else Error_Posted (N));
4601 Val := Uint_0;
4603 -- Character literal
4605 elsif Kind = N_Character_Literal then
4606 Ent := Entity (N);
4608 -- Since Character literals of type Standard.Character don't
4609 -- have any defining character literals built for them, they
4610 -- do not have their Entity set, so just use their Char
4611 -- code. Otherwise for user-defined character literals use
4612 -- their Pos value as usual.
4614 if No (Ent) then
4615 Val := Char_Literal_Value (N);
4616 else
4617 Val := Enumeration_Pos (Ent);
4618 end if;
4620 -- Unchecked conversion, which can come from System'To_Address (X)
4621 -- where X is a static integer expression. Recursively evaluate X.
4623 elsif Kind = N_Unchecked_Type_Conversion then
4624 Val := Expr_Value (Expression (N));
4626 -- Static discriminant value
4628 elsif Is_Static_Discriminant_Component (N) then
4629 Val := Expr_Value
4630 (Get_Discriminant_Value
4631 (Entity (Selector_Name (N)),
4632 Etype (Prefix (N)),
4633 Discriminant_Constraint (Etype (Prefix (N)))));
4635 else
4636 raise Program_Error;
4637 end if;
4639 -- Come here with Val set to value to be returned, set cache
4641 CV_Ent.N := N;
4642 CV_Ent.V := Val;
4643 return Val;
4644 end Expr_Value;
4646 ------------------
4647 -- Expr_Value_E --
4648 ------------------
4650 function Expr_Value_E (N : Node_Id) return Entity_Id is
4651 Ent : constant Entity_Id := Entity (N);
4652 begin
4653 if Ekind (Ent) = E_Enumeration_Literal then
4654 return Ent;
4655 else
4656 pragma Assert (Ekind (Ent) = E_Constant);
4658 -- We may be dealing with a enumerated character type constant, so
4659 -- handle that case here.
4661 if Nkind (Constant_Value (Ent)) = N_Character_Literal then
4662 return Ent;
4663 else
4664 return Expr_Value_E (Constant_Value (Ent));
4665 end if;
4666 end if;
4667 end Expr_Value_E;
4669 ------------------
4670 -- Expr_Value_R --
4671 ------------------
4673 function Expr_Value_R (N : Node_Id) return Ureal is
4674 Kind : constant Node_Kind := Nkind (N);
4675 Ent : Entity_Id;
4677 begin
4678 if Kind = N_Real_Literal then
4679 return Realval (N);
4681 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4682 Ent := Entity (N);
4683 pragma Assert (Ekind (Ent) = E_Constant);
4684 return Expr_Value_R (Constant_Value (Ent));
4686 elsif Kind = N_Integer_Literal then
4687 return UR_From_Uint (Expr_Value (N));
4689 -- Here, we have a node that cannot be interpreted as a compile time
4690 -- constant. That is definitely an error.
4692 else
4693 raise Program_Error;
4694 end if;
4695 end Expr_Value_R;
4697 ------------------
4698 -- Expr_Value_S --
4699 ------------------
4701 function Expr_Value_S (N : Node_Id) return Node_Id is
4702 begin
4703 if Nkind (N) = N_String_Literal then
4704 return N;
4705 else
4706 pragma Assert (Ekind (Entity (N)) = E_Constant);
4707 return Expr_Value_S (Constant_Value (Entity (N)));
4708 end if;
4709 end Expr_Value_S;
4711 ----------------------------------
4712 -- Find_Universal_Operator_Type --
4713 ----------------------------------
4715 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4716 PN : constant Node_Id := Parent (N);
4717 Call : constant Node_Id := Original_Node (N);
4718 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4720 Is_Fix : constant Boolean :=
4721 Nkind (N) in N_Binary_Op
4722 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4723 -- A mixed-mode operation in this context indicates the presence of
4724 -- fixed-point type in the designated package.
4726 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4727 -- Case where N is a relational (or membership) operator (else it is an
4728 -- arithmetic one).
4730 In_Membership : constant Boolean :=
4731 Nkind (PN) in N_Membership_Test
4732 and then
4733 Nkind (Right_Opnd (PN)) = N_Range
4734 and then
4735 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4736 and then
4737 Is_Universal_Numeric_Type
4738 (Etype (Low_Bound (Right_Opnd (PN))))
4739 and then
4740 Is_Universal_Numeric_Type
4741 (Etype (High_Bound (Right_Opnd (PN))));
4742 -- Case where N is part of a membership test with a universal range
4744 E : Entity_Id;
4745 Pack : Entity_Id;
4746 Typ1 : Entity_Id := Empty;
4747 Priv_E : Entity_Id;
4749 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4750 -- Check whether one operand is a mixed-mode operation that requires the
4751 -- presence of a fixed-point type. Given that all operands are universal
4752 -- and have been constant-folded, retrieve the original function call.
4754 ---------------------------
4755 -- Is_Mixed_Mode_Operand --
4756 ---------------------------
4758 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4759 Onod : constant Node_Id := Original_Node (Op);
4760 begin
4761 return Nkind (Onod) = N_Function_Call
4762 and then Present (Next_Actual (First_Actual (Onod)))
4763 and then Etype (First_Actual (Onod)) /=
4764 Etype (Next_Actual (First_Actual (Onod)));
4765 end Is_Mixed_Mode_Operand;
4767 -- Start of processing for Find_Universal_Operator_Type
4769 begin
4770 if Nkind (Call) /= N_Function_Call
4771 or else Nkind (Name (Call)) /= N_Expanded_Name
4772 then
4773 return Empty;
4775 -- There are several cases where the context does not imply the type of
4776 -- the operands:
4777 -- - the universal expression appears in a type conversion;
4778 -- - the expression is a relational operator applied to universal
4779 -- operands;
4780 -- - the expression is a membership test with a universal operand
4781 -- and a range with universal bounds.
4783 elsif Nkind (Parent (N)) = N_Type_Conversion
4784 or else Is_Relational
4785 or else In_Membership
4786 then
4787 Pack := Entity (Prefix (Name (Call)));
4789 -- If the prefix is a package declared elsewhere, iterate over its
4790 -- visible entities, otherwise iterate over all declarations in the
4791 -- designated scope.
4793 if Ekind (Pack) = E_Package
4794 and then not In_Open_Scopes (Pack)
4795 then
4796 Priv_E := First_Private_Entity (Pack);
4797 else
4798 Priv_E := Empty;
4799 end if;
4801 Typ1 := Empty;
4802 E := First_Entity (Pack);
4803 while Present (E) and then E /= Priv_E loop
4804 if Is_Numeric_Type (E)
4805 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4806 and then Comes_From_Source (E)
4807 and then Is_Integer_Type (E) = Is_Int
4808 and then (Nkind (N) in N_Unary_Op
4809 or else Is_Relational
4810 or else Is_Fixed_Point_Type (E) = Is_Fix)
4811 then
4812 if No (Typ1) then
4813 Typ1 := E;
4815 -- Before emitting an error, check for the presence of a
4816 -- mixed-mode operation that specifies a fixed point type.
4818 elsif Is_Relational
4819 and then
4820 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4821 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4822 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4824 then
4825 if Is_Fixed_Point_Type (E) then
4826 Typ1 := E;
4827 end if;
4829 else
4830 -- More than one type of the proper class declared in P
4832 Error_Msg_N ("ambiguous operation", N);
4833 Error_Msg_Sloc := Sloc (Typ1);
4834 Error_Msg_N ("\possible interpretation (inherited)#", N);
4835 Error_Msg_Sloc := Sloc (E);
4836 Error_Msg_N ("\possible interpretation (inherited)#", N);
4837 return Empty;
4838 end if;
4839 end if;
4841 Next_Entity (E);
4842 end loop;
4843 end if;
4845 return Typ1;
4846 end Find_Universal_Operator_Type;
4848 --------------------------
4849 -- Flag_Non_Static_Expr --
4850 --------------------------
4852 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4853 begin
4854 if Error_Posted (Expr) and then not All_Errors_Mode then
4855 return;
4856 else
4857 Error_Msg_F (Msg, Expr);
4858 Why_Not_Static (Expr);
4859 end if;
4860 end Flag_Non_Static_Expr;
4862 ----------
4863 -- Fold --
4864 ----------
4866 procedure Fold (N : Node_Id) is
4867 Typ : constant Entity_Id := Etype (N);
4868 begin
4869 -- If not known at compile time or if already a literal, nothing to do
4871 if Nkind (N) in N_Numeric_Or_String_Literal
4872 or else not Compile_Time_Known_Value (N)
4873 then
4874 null;
4876 elsif Is_Discrete_Type (Typ) then
4877 Fold_Uint (N, Expr_Value (N), Static => Is_Static_Expression (N));
4879 elsif Is_Real_Type (Typ) then
4880 Fold_Ureal (N, Expr_Value_R (N), Static => Is_Static_Expression (N));
4882 elsif Is_String_Type (Typ) then
4883 Fold_Str
4884 (N, Strval (Expr_Value_S (N)), Static => Is_Static_Expression (N));
4885 end if;
4886 end Fold;
4888 ----------------
4889 -- Fold_Dummy --
4890 ----------------
4892 procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id) is
4893 begin
4894 if Is_Integer_Type (Typ) then
4895 Fold_Uint (N, Uint_1, Static => True);
4897 elsif Is_Real_Type (Typ) then
4898 Fold_Ureal (N, Ureal_1, Static => True);
4900 elsif Is_Enumeration_Type (Typ) then
4901 Fold_Uint
4903 Expr_Value (Type_Low_Bound (Base_Type (Typ))),
4904 Static => True);
4906 elsif Is_String_Type (Typ) then
4907 Fold_Str
4909 Strval (Make_String_Literal (Sloc (N), "")),
4910 Static => True);
4911 end if;
4912 end Fold_Dummy;
4914 ----------------
4915 -- Fold_Shift --
4916 ----------------
4918 procedure Fold_Shift
4919 (N : Node_Id;
4920 Left : Node_Id;
4921 Right : Node_Id;
4922 Op : Node_Kind;
4923 Static : Boolean := False;
4924 Check_Elab : Boolean := False)
4926 Typ : constant Entity_Id := Base_Type (Etype (Left));
4928 procedure Check_Elab_Call;
4929 -- Add checks related to calls in elaboration code
4931 ---------------------
4932 -- Check_Elab_Call --
4933 ---------------------
4935 procedure Check_Elab_Call is
4936 begin
4937 if Check_Elab then
4938 if Legacy_Elaboration_Checks then
4939 Check_Elab_Call (N);
4940 end if;
4942 Build_Call_Marker (N);
4943 end if;
4944 end Check_Elab_Call;
4946 Modulus, Val : Uint;
4948 begin
4949 if Compile_Time_Known_Value (Left)
4950 and then Compile_Time_Known_Value (Right)
4951 then
4952 pragma Assert (not Non_Binary_Modulus (Typ));
4954 if Op = N_Op_Shift_Left then
4955 Check_Elab_Call;
4957 if Is_Modular_Integer_Type (Typ) then
4958 Modulus := Einfo.Entities.Modulus (Typ);
4959 else
4960 Modulus := Uint_2 ** RM_Size (Typ);
4961 end if;
4963 -- Fold Shift_Left (X, Y) by computing
4964 -- (X * 2**Y) rem modulus [- Modulus]
4966 Val := (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right)))
4967 rem Modulus;
4969 if Is_Modular_Integer_Type (Typ)
4970 or else Val < Modulus / Uint_2
4971 then
4972 Fold_Uint (N, Val, Static => Static);
4973 else
4974 Fold_Uint (N, Val - Modulus, Static => Static);
4975 end if;
4977 elsif Op = N_Op_Shift_Right then
4978 Check_Elab_Call;
4980 -- X >> 0 is a no-op
4982 if Expr_Value (Right) = Uint_0 then
4983 Fold_Uint (N, Expr_Value (Left), Static => Static);
4984 else
4985 if Is_Modular_Integer_Type (Typ) then
4986 Modulus := Einfo.Entities.Modulus (Typ);
4987 else
4988 Modulus := Uint_2 ** RM_Size (Typ);
4989 end if;
4991 -- Fold X >> Y by computing (X [+ Modulus]) / 2**Y
4992 -- Note that after a Shift_Right operation (with Y > 0), the
4993 -- result is always positive, even if the original operand was
4994 -- negative.
4996 declare
4997 M : Unat;
4998 begin
4999 if Expr_Value (Left) >= Uint_0 then
5000 M := Uint_0;
5001 else
5002 M := Modulus;
5003 end if;
5005 Fold_Uint
5007 (Expr_Value (Left) + M) / (Uint_2 ** Expr_Value (Right)),
5008 Static => Static);
5009 end;
5010 end if;
5011 elsif Op = N_Op_Shift_Right_Arithmetic then
5012 Check_Elab_Call;
5014 declare
5015 Two_Y : constant Uint := Uint_2 ** Expr_Value (Right);
5016 begin
5017 if Is_Modular_Integer_Type (Typ) then
5018 Modulus := Einfo.Entities.Modulus (Typ);
5019 else
5020 Modulus := Uint_2 ** RM_Size (Typ);
5021 end if;
5023 -- X / 2**Y if X if positive or a small enough modular integer
5025 if (Is_Modular_Integer_Type (Typ)
5026 and then Expr_Value (Left) < Modulus / Uint_2)
5027 or else
5028 (not Is_Modular_Integer_Type (Typ)
5029 and then Expr_Value (Left) >= 0)
5030 then
5031 Fold_Uint (N, Expr_Value (Left) / Two_Y, Static => Static);
5033 -- -1 (aka all 1's) if Y is larger than the number of bits
5034 -- available or if X = -1.
5036 elsif Two_Y > Modulus
5037 or else Expr_Value (Left) = Uint_Minus_1
5038 then
5039 if Is_Modular_Integer_Type (Typ) then
5040 Fold_Uint (N, Modulus - Uint_1, Static => Static);
5041 else
5042 Fold_Uint (N, Uint_Minus_1, Static => Static);
5043 end if;
5045 -- Large modular integer, compute via multiply/divide the
5046 -- following: X >> Y + (1 << Y - 1) << (RM_Size - Y)
5048 elsif Is_Modular_Integer_Type (Typ) then
5049 Fold_Uint
5051 (Expr_Value (Left)) / Two_Y
5052 + (Two_Y - Uint_1)
5053 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right)),
5054 Static => Static);
5056 -- Negative signed integer, compute via multiple/divide the
5057 -- following:
5058 -- (Modulus + X) >> Y + (1 << Y - 1) << (RM_Size - Y) - Modulus
5060 else
5061 Fold_Uint
5063 (Modulus + Expr_Value (Left)) / Two_Y
5064 + (Two_Y - Uint_1)
5065 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right))
5066 - Modulus,
5067 Static => Static);
5068 end if;
5069 end;
5070 end if;
5071 end if;
5072 end Fold_Shift;
5074 --------------
5075 -- Fold_Str --
5076 --------------
5078 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
5079 Loc : constant Source_Ptr := Sloc (N);
5080 Typ : constant Entity_Id := Etype (N);
5082 begin
5083 if Raises_Constraint_Error (N) then
5084 Set_Is_Static_Expression (N, Static);
5085 return;
5086 end if;
5088 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
5090 -- We now have the literal with the right value, both the actual type
5091 -- and the expected type of this literal are taken from the expression
5092 -- that was evaluated. So now we do the Analyze and Resolve.
5094 -- Note that we have to reset Is_Static_Expression both after the
5095 -- analyze step (because Resolve will evaluate the literal, which
5096 -- will cause semantic errors if it is marked as static), and after
5097 -- the Resolve step (since Resolve in some cases resets this flag).
5099 Analyze (N);
5100 Set_Is_Static_Expression (N, Static);
5101 Set_Etype (N, Typ);
5102 Resolve (N);
5103 Set_Is_Static_Expression (N, Static);
5104 end Fold_Str;
5106 ---------------
5107 -- Fold_Uint --
5108 ---------------
5110 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
5111 Loc : constant Source_Ptr := Sloc (N);
5112 Typ : Entity_Id := Etype (N);
5113 Ent : Entity_Id;
5115 begin
5116 if Raises_Constraint_Error (N) then
5117 Set_Is_Static_Expression (N, Static);
5118 return;
5119 end if;
5121 -- If we are folding a named number, retain the entity in the literal
5122 -- in the original tree.
5124 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
5125 Ent := Entity (N);
5126 else
5127 Ent := Empty;
5128 end if;
5130 if Is_Private_Type (Typ) then
5131 Typ := Full_View (Typ);
5132 end if;
5134 -- For a result of type integer, substitute an N_Integer_Literal node
5135 -- for the result of the compile time evaluation of the expression.
5136 -- Set a link to the original named number when not in a generic context
5137 -- for reference in the original tree.
5139 if Is_Integer_Type (Typ) then
5140 Rewrite (N, Make_Integer_Literal (Loc, Val));
5141 Set_Original_Entity (N, Ent);
5143 -- Otherwise we have an enumeration type, and we substitute either
5144 -- an N_Identifier or N_Character_Literal to represent the enumeration
5145 -- literal corresponding to the given value, which must always be in
5146 -- range, because appropriate tests have already been made for this.
5148 else pragma Assert (Is_Enumeration_Type (Typ));
5149 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
5150 end if;
5152 -- We now have the literal with the right value, both the actual type
5153 -- and the expected type of this literal are taken from the expression
5154 -- that was evaluated. So now we do the Analyze and Resolve.
5156 -- Note that we have to reset Is_Static_Expression both after the
5157 -- analyze step (because Resolve will evaluate the literal, which
5158 -- will cause semantic errors if it is marked as static), and after
5159 -- the Resolve step (since Resolve in some cases sets this flag).
5161 Analyze (N);
5162 Set_Is_Static_Expression (N, Static);
5163 Set_Etype (N, Typ);
5164 Resolve (N);
5165 Set_Is_Static_Expression (N, Static);
5166 end Fold_Uint;
5168 ----------------
5169 -- Fold_Ureal --
5170 ----------------
5172 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
5173 Loc : constant Source_Ptr := Sloc (N);
5174 Typ : constant Entity_Id := Etype (N);
5175 Ent : Entity_Id;
5177 begin
5178 if Raises_Constraint_Error (N) then
5179 Set_Is_Static_Expression (N, Static);
5180 return;
5181 end if;
5183 -- If we are folding a named number, retain the entity in the literal
5184 -- in the original tree.
5186 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
5187 Ent := Entity (N);
5188 else
5189 Ent := Empty;
5190 end if;
5192 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
5194 -- Set link to original named number
5196 Set_Original_Entity (N, Ent);
5198 -- We now have the literal with the right value, both the actual type
5199 -- and the expected type of this literal are taken from the expression
5200 -- that was evaluated. So now we do the Analyze and Resolve.
5202 -- Note that we have to reset Is_Static_Expression both after the
5203 -- analyze step (because Resolve will evaluate the literal, which
5204 -- will cause semantic errors if it is marked as static), and after
5205 -- the Resolve step (since Resolve in some cases sets this flag).
5207 -- We mark the node as analyzed so that its type is not erased by
5208 -- calling Analyze_Real_Literal.
5210 Analyze (N);
5211 Set_Is_Static_Expression (N, Static);
5212 Set_Etype (N, Typ);
5213 Resolve (N);
5214 Set_Analyzed (N);
5215 Set_Is_Static_Expression (N, Static);
5216 end Fold_Ureal;
5218 ---------------
5219 -- From_Bits --
5220 ---------------
5222 function From_Bits (B : Bits; T : Entity_Id) return Uint is
5223 V : Uint := Uint_0;
5225 begin
5226 for J in 0 .. B'Last loop
5227 if B (J) then
5228 V := V + 2 ** J;
5229 end if;
5230 end loop;
5232 if Non_Binary_Modulus (T) then
5233 V := V mod Modulus (T);
5234 end if;
5236 return V;
5237 end From_Bits;
5239 --------------------
5240 -- Get_String_Val --
5241 --------------------
5243 function Get_String_Val (N : Node_Id) return Node_Id is
5244 begin
5245 if Nkind (N) in N_String_Literal | N_Character_Literal then
5246 return N;
5247 else
5248 pragma Assert (Is_Entity_Name (N));
5249 return Get_String_Val (Constant_Value (Entity (N)));
5250 end if;
5251 end Get_String_Val;
5253 ----------------
5254 -- Initialize --
5255 ----------------
5257 procedure Initialize is
5258 begin
5259 CV_Cache := (others => (Node_High_Bound, Uint_0));
5260 end Initialize;
5262 --------------------
5263 -- In_Subrange_Of --
5264 --------------------
5266 function In_Subrange_Of
5267 (T1 : Entity_Id;
5268 T2 : Entity_Id;
5269 Fixed_Int : Boolean := False) return Boolean
5271 L1 : Node_Id;
5272 H1 : Node_Id;
5274 L2 : Node_Id;
5275 H2 : Node_Id;
5277 begin
5278 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
5279 return True;
5281 -- Never in range if both types are not scalar. Don't know if this can
5282 -- actually happen, but just in case.
5284 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
5285 return False;
5287 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
5288 -- definitely not compatible with T2.
5290 elsif Is_Floating_Point_Type (T1)
5291 and then Has_Infinities (T1)
5292 and then Is_Floating_Point_Type (T2)
5293 and then not Has_Infinities (T2)
5294 then
5295 return False;
5297 else
5298 L1 := Type_Low_Bound (T1);
5299 H1 := Type_High_Bound (T1);
5301 L2 := Type_Low_Bound (T2);
5302 H2 := Type_High_Bound (T2);
5304 -- Check bounds to see if comparison possible at compile time
5306 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
5307 and then
5308 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
5309 then
5310 return True;
5311 end if;
5313 -- If bounds not comparable at compile time, then the bounds of T2
5314 -- must be compile-time-known or we cannot answer the query.
5316 if not Compile_Time_Known_Value (L2)
5317 or else not Compile_Time_Known_Value (H2)
5318 then
5319 return False;
5320 end if;
5322 -- If the bounds of T1 are know at compile time then use these
5323 -- ones, otherwise use the bounds of the base type (which are of
5324 -- course always static).
5326 if not Compile_Time_Known_Value (L1) then
5327 L1 := Type_Low_Bound (Base_Type (T1));
5328 end if;
5330 if not Compile_Time_Known_Value (H1) then
5331 H1 := Type_High_Bound (Base_Type (T1));
5332 end if;
5334 -- Fixed point types should be considered as such only if
5335 -- flag Fixed_Int is set to False.
5337 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
5338 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
5339 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
5340 then
5341 return
5342 Expr_Value_R (L2) <= Expr_Value_R (L1)
5343 and then
5344 Expr_Value_R (H2) >= Expr_Value_R (H1);
5346 else
5347 return
5348 Expr_Value (L2) <= Expr_Value (L1)
5349 and then
5350 Expr_Value (H2) >= Expr_Value (H1);
5352 end if;
5353 end if;
5355 -- If any exception occurs, it means that we have some bug in the compiler
5356 -- possibly triggered by a previous error, or by some unforeseen peculiar
5357 -- occurrence. However, this is only an optimization attempt, so there is
5358 -- really no point in crashing the compiler. Instead we just decide, too
5359 -- bad, we can't figure out the answer in this case after all.
5361 exception
5362 when others =>
5363 -- With debug flag K we will get an exception unless an error has
5364 -- already occurred (useful for debugging).
5366 if Debug_Flag_K then
5367 Check_Error_Detected;
5368 end if;
5370 return False;
5371 end In_Subrange_Of;
5373 -----------------
5374 -- Is_In_Range --
5375 -----------------
5377 function Is_In_Range
5378 (N : Node_Id;
5379 Typ : Entity_Id;
5380 Assume_Valid : Boolean := False;
5381 Fixed_Int : Boolean := False;
5382 Int_Real : Boolean := False) return Boolean
5384 begin
5385 return
5386 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
5387 end Is_In_Range;
5389 -------------------
5390 -- Is_Null_Range --
5391 -------------------
5393 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5394 begin
5395 if Compile_Time_Known_Value (Lo)
5396 and then Compile_Time_Known_Value (Hi)
5397 then
5398 declare
5399 Typ : Entity_Id := Etype (Lo);
5400 begin
5401 -- When called from the frontend, as part of the analysis of
5402 -- potentially static expressions, Typ will be the full view of a
5403 -- type with all the info needed to answer this query. When called
5404 -- from the backend, for example to know whether a range of a loop
5405 -- is null, Typ might be a private type and we need to explicitly
5406 -- switch to its corresponding full view to access the same info.
5408 if Is_Incomplete_Or_Private_Type (Typ)
5409 and then Present (Full_View (Typ))
5410 then
5411 Typ := Full_View (Typ);
5412 end if;
5414 if Is_Discrete_Type (Typ) then
5415 return Expr_Value (Lo) > Expr_Value (Hi);
5416 else pragma Assert (Is_Real_Type (Typ));
5417 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
5418 end if;
5419 end;
5421 else
5422 return Compile_Time_Compare (Lo, Hi, Assume_Valid => False) = GT;
5423 end if;
5424 end Is_Null_Range;
5426 -------------------------
5427 -- Is_OK_Static_Choice --
5428 -------------------------
5430 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
5431 begin
5432 -- Check various possibilities for choice
5434 -- Note: for membership tests, we test more cases than are possible
5435 -- (in particular subtype indication), but it doesn't matter because
5436 -- it just won't occur (we have already done a syntax check).
5438 if Nkind (Choice) = N_Others_Choice then
5439 return True;
5441 elsif Nkind (Choice) = N_Range then
5442 return Is_OK_Static_Range (Choice);
5444 elsif Nkind (Choice) = N_Subtype_Indication
5445 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5446 then
5447 return Is_OK_Static_Subtype (Etype (Choice));
5449 else
5450 return Is_OK_Static_Expression (Choice);
5451 end if;
5452 end Is_OK_Static_Choice;
5454 ------------------------------
5455 -- Is_OK_Static_Choice_List --
5456 ------------------------------
5458 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
5459 Choice : Node_Id;
5461 begin
5462 if not Is_Static_Choice_List (Choices) then
5463 return False;
5464 end if;
5466 Choice := First (Choices);
5467 while Present (Choice) loop
5468 if not Is_OK_Static_Choice (Choice) then
5469 Set_Raises_Constraint_Error (Choice);
5470 return False;
5471 end if;
5473 Next (Choice);
5474 end loop;
5476 return True;
5477 end Is_OK_Static_Choice_List;
5479 -----------------------------
5480 -- Is_OK_Static_Expression --
5481 -----------------------------
5483 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
5484 begin
5485 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
5486 end Is_OK_Static_Expression;
5488 ------------------------
5489 -- Is_OK_Static_Range --
5490 ------------------------
5492 -- A static range is a range whose bounds are static expressions, or a
5493 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5494 -- We have already converted range attribute references, so we get the
5495 -- "or" part of this rule without needing a special test.
5497 function Is_OK_Static_Range (N : Node_Id) return Boolean is
5498 begin
5499 return Is_OK_Static_Expression (Low_Bound (N))
5500 and then Is_OK_Static_Expression (High_Bound (N));
5501 end Is_OK_Static_Range;
5503 --------------------------
5504 -- Is_OK_Static_Subtype --
5505 --------------------------
5507 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
5508 -- neither bound raises Constraint_Error when evaluated.
5510 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
5511 Base_T : constant Entity_Id := Base_Type (Typ);
5512 Anc_Subt : Entity_Id;
5514 begin
5515 -- First a quick check on the non static subtype flag. As described
5516 -- in further detail in Einfo, this flag is not decisive in all cases,
5517 -- but if it is set, then the subtype is definitely non-static.
5519 if Is_Non_Static_Subtype (Typ) then
5520 return False;
5521 end if;
5523 -- Then, check if the subtype is strictly static. This takes care of
5524 -- checking for generics and predicates.
5526 if not Is_Static_Subtype (Typ) then
5527 return False;
5528 end if;
5530 -- String types
5532 if Is_String_Type (Typ) then
5533 return
5534 Ekind (Typ) = E_String_Literal_Subtype
5535 or else
5536 (Is_OK_Static_Subtype (Component_Type (Typ))
5537 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
5539 -- Scalar types
5541 elsif Is_Scalar_Type (Typ) then
5542 if Base_T = Typ then
5543 return True;
5545 else
5546 Anc_Subt := Ancestor_Subtype (Typ);
5548 if No (Anc_Subt) then
5549 Anc_Subt := Base_T;
5550 end if;
5552 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
5553 -- Get_Type_{Low,High}_Bound.
5555 return Is_OK_Static_Subtype (Anc_Subt)
5556 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
5557 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
5558 end if;
5560 -- Types other than string and scalar types are never static
5562 else
5563 return False;
5564 end if;
5565 end Is_OK_Static_Subtype;
5567 ---------------------
5568 -- Is_Out_Of_Range --
5569 ---------------------
5571 function Is_Out_Of_Range
5572 (N : Node_Id;
5573 Typ : Entity_Id;
5574 Assume_Valid : Boolean := False;
5575 Fixed_Int : Boolean := False;
5576 Int_Real : Boolean := False) return Boolean
5578 begin
5579 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
5580 Out_Of_Range;
5581 end Is_Out_Of_Range;
5583 ----------------------
5584 -- Is_Static_Choice --
5585 ----------------------
5587 function Is_Static_Choice (Choice : Node_Id) return Boolean is
5588 begin
5589 -- Check various possibilities for choice
5591 -- Note: for membership tests, we test more cases than are possible
5592 -- (in particular subtype indication), but it doesn't matter because
5593 -- it just won't occur (we have already done a syntax check).
5595 if Nkind (Choice) = N_Others_Choice then
5596 return True;
5598 elsif Nkind (Choice) = N_Range then
5599 return Is_Static_Range (Choice);
5601 elsif Nkind (Choice) = N_Subtype_Indication
5602 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5603 then
5604 return Is_Static_Subtype (Etype (Choice));
5606 else
5607 return Is_Static_Expression (Choice);
5608 end if;
5609 end Is_Static_Choice;
5611 ---------------------------
5612 -- Is_Static_Choice_List --
5613 ---------------------------
5615 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
5616 Choice : Node_Id;
5618 begin
5619 Choice := First (Choices);
5620 while Present (Choice) loop
5621 if not Is_Static_Choice (Choice) then
5622 return False;
5623 end if;
5625 Next (Choice);
5626 end loop;
5628 return True;
5629 end Is_Static_Choice_List;
5631 ---------------------
5632 -- Is_Static_Range --
5633 ---------------------
5635 -- A static range is a range whose bounds are static expressions, or a
5636 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5637 -- We have already converted range attribute references, so we get the
5638 -- "or" part of this rule without needing a special test.
5640 function Is_Static_Range (N : Node_Id) return Boolean is
5641 begin
5642 return Is_Static_Expression (Low_Bound (N))
5643 and then
5644 Is_Static_Expression (High_Bound (N));
5645 end Is_Static_Range;
5647 -----------------------
5648 -- Is_Static_Subtype --
5649 -----------------------
5651 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5653 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
5654 Base_T : constant Entity_Id := Base_Type (Typ);
5655 Anc_Subt : Entity_Id;
5657 begin
5658 -- First a quick check on the non static subtype flag. As described
5659 -- in further detail in Einfo, this flag is not decisive in all cases,
5660 -- but if it is set, then the subtype is definitely non-static.
5662 if Is_Non_Static_Subtype (Typ) then
5663 return False;
5664 end if;
5666 Anc_Subt := Ancestor_Subtype (Typ);
5668 if Anc_Subt = Empty then
5669 Anc_Subt := Base_T;
5670 end if;
5672 if Is_Generic_Type (Root_Type (Base_T))
5673 or else Is_Generic_Actual_Type (Base_T)
5674 then
5675 return False;
5677 -- If there is a non-static predicate for the type (declared or
5678 -- inherited) the expression is not static.
5680 elsif Has_Dynamic_Predicate_Aspect (Typ)
5681 or else (Is_Derived_Type (Typ)
5682 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5683 or else Has_Ghost_Predicate_Aspect (Typ)
5684 or else (Is_Derived_Type (Typ)
5685 and then Has_Aspect (Typ, Aspect_Ghost_Predicate))
5686 or else (Has_Aspect (Typ, Aspect_Predicate)
5687 and then not Has_Static_Predicate (Typ))
5688 then
5689 return False;
5691 -- String types
5693 elsif Is_String_Type (Typ) then
5694 return
5695 Ekind (Typ) = E_String_Literal_Subtype
5696 or else (Is_Static_Subtype (Component_Type (Typ))
5697 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5699 -- Scalar types
5701 elsif Is_Scalar_Type (Typ) then
5702 if Base_T = Typ then
5703 return True;
5705 else
5706 return Is_Static_Subtype (Anc_Subt)
5707 and then Is_Static_Expression (Type_Low_Bound (Typ))
5708 and then Is_Static_Expression (Type_High_Bound (Typ));
5709 end if;
5711 -- Types other than string and scalar types are never static
5713 else
5714 return False;
5715 end if;
5716 end Is_Static_Subtype;
5718 -------------------------------
5719 -- Is_Statically_Unevaluated --
5720 -------------------------------
5722 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5723 function Check_Case_Expr_Alternative
5724 (CEA : Node_Id) return Match_Result;
5725 -- We have a message emanating from the Expression of a case expression
5726 -- alternative. We examine this alternative, as follows:
5728 -- If the selecting expression of the parent case is non-static, or
5729 -- if any of the discrete choices of the given case alternative are
5730 -- non-static or raise Constraint_Error, return Non_Static.
5732 -- Otherwise check if the selecting expression matches any of the given
5733 -- discrete choices. If so, the alternative is executed and we return
5734 -- Match, otherwise, the alternative can never be executed, and so we
5735 -- return No_Match.
5737 ---------------------------------
5738 -- Check_Case_Expr_Alternative --
5739 ---------------------------------
5741 function Check_Case_Expr_Alternative
5742 (CEA : Node_Id) return Match_Result
5744 Case_Exp : constant Node_Id := Parent (CEA);
5745 Choice : Node_Id;
5746 Prev_CEA : Node_Id;
5748 begin
5749 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5751 -- Check that selecting expression is static
5753 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5754 return Non_Static;
5755 end if;
5757 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5758 return Non_Static;
5759 end if;
5761 -- All choices are now known to be static. Now see if alternative
5762 -- matches one of the choices.
5764 Choice := First (Discrete_Choices (CEA));
5765 while Present (Choice) loop
5767 -- Check various possibilities for choice, returning Match if we
5768 -- find the selecting value matches any of the choices. Note that
5769 -- we know we are the last choice, so we don't have to keep going.
5771 if Nkind (Choice) = N_Others_Choice then
5773 -- Others choice is a bit annoying, it matches if none of the
5774 -- previous alternatives matches (note that we know we are the
5775 -- last alternative in this case, so we can just go backwards
5776 -- from us to see if any previous one matches).
5778 Prev_CEA := Prev (CEA);
5779 while Present (Prev_CEA) loop
5780 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5781 return No_Match;
5782 end if;
5784 Prev (Prev_CEA);
5785 end loop;
5787 return Match;
5789 -- Else we have a normal static choice
5791 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5792 return Match;
5793 end if;
5795 -- If we fall through, it means that the discrete choice did not
5796 -- match the selecting expression, so continue.
5798 Next (Choice);
5799 end loop;
5801 -- If we get through that loop then all choices were static, and none
5802 -- of them matched the selecting expression. So return No_Match.
5804 return No_Match;
5805 end Check_Case_Expr_Alternative;
5807 -- Local variables
5809 P : Node_Id;
5810 OldP : Node_Id;
5811 Choice : Node_Id;
5813 -- Start of processing for Is_Statically_Unevaluated
5815 begin
5816 -- The (32.x) references here are from RM section 4.9
5818 -- (32.1) An expression is statically unevaluated if it is part of ...
5820 -- This means we have to climb the tree looking for one of the cases
5822 P := Expr;
5823 loop
5824 OldP := P;
5825 P := Parent (P);
5827 -- (32.2) The right operand of a static short-circuit control form
5828 -- whose value is determined by its left operand.
5830 -- AND THEN with False as left operand
5832 if Nkind (P) = N_And_Then
5833 and then Compile_Time_Known_Value (Left_Opnd (P))
5834 and then Is_False (Expr_Value (Left_Opnd (P)))
5835 then
5836 return True;
5838 -- OR ELSE with True as left operand
5840 elsif Nkind (P) = N_Or_Else
5841 and then Compile_Time_Known_Value (Left_Opnd (P))
5842 and then Is_True (Expr_Value (Left_Opnd (P)))
5843 then
5844 return True;
5846 -- (32.3) A dependent_expression of an if_expression whose associated
5847 -- condition is static and equals False.
5849 elsif Nkind (P) = N_If_Expression then
5850 declare
5851 Cond : constant Node_Id := First (Expressions (P));
5852 Texp : constant Node_Id := Next (Cond);
5853 Fexp : constant Node_Id := Next (Texp);
5855 begin
5856 if Compile_Time_Known_Value (Cond) then
5858 -- Condition is True and we are in the right operand
5860 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5861 return True;
5863 -- Condition is False and we are in the left operand
5865 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5866 return True;
5867 end if;
5868 end if;
5869 end;
5871 -- (32.4) A condition or dependent_expression of an if_expression
5872 -- where the condition corresponding to at least one preceding
5873 -- dependent_expression of the if_expression is static and equals
5874 -- True.
5876 -- This refers to cases like
5878 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5880 -- But we expand elsif's out anyway, so the above looks like:
5882 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5884 -- So for us this is caught by the above check for the 32.3 case.
5886 -- (32.5) A dependent_expression of a case_expression whose
5887 -- selecting_expression is static and whose value is not covered
5888 -- by the corresponding discrete_choice_list.
5890 elsif Nkind (P) = N_Case_Expression_Alternative then
5892 -- First, we have to be in the expression to suppress messages.
5893 -- If we are within one of the choices, we want the message.
5895 if OldP = Expression (P) then
5897 -- Statically unevaluated if alternative does not match
5899 if Check_Case_Expr_Alternative (P) = No_Match then
5900 return True;
5901 end if;
5902 end if;
5904 -- (32.6) A choice_expression (or a simple_expression of a range
5905 -- that occurs as a membership_choice of a membership_choice_list)
5906 -- of a static membership test that is preceded in the enclosing
5907 -- membership_choice_list by another item whose individual
5908 -- membership test (see (RM 4.5.2)) statically yields True.
5910 elsif Nkind (P) in N_Membership_Test then
5912 -- Only possibly unevaluated if simple expression is static
5914 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5915 null;
5917 -- All members of the choice list must be static
5919 elsif (Present (Right_Opnd (P))
5920 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5921 or else (Present (Alternatives (P))
5922 and then
5923 not Is_OK_Static_Choice_List (Alternatives (P)))
5924 then
5925 null;
5927 -- If expression is the one and only alternative, then it is
5928 -- definitely not statically unevaluated, so we only have to
5929 -- test the case where there are alternatives present.
5931 elsif Present (Alternatives (P)) then
5933 -- Look for previous matching Choice
5935 Choice := First (Alternatives (P));
5936 while Present (Choice) loop
5938 -- If we reached us and no previous choices matched, this
5939 -- is not the case where we are statically unevaluated.
5941 exit when OldP = Choice;
5943 -- If a previous choice matches, then that is the case where
5944 -- we know our choice is statically unevaluated.
5946 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5947 return True;
5948 end if;
5950 Next (Choice);
5951 end loop;
5953 -- If we fall through the loop, we were not one of the choices,
5954 -- we must have been the expression, so that is not covered by
5955 -- this rule, and we keep going.
5957 null;
5958 end if;
5959 end if;
5961 -- OK, not statically unevaluated at this level, see if we should
5962 -- keep climbing to look for a higher level reason.
5964 -- Special case for component association in aggregates, where
5965 -- we want to keep climbing up to the parent aggregate.
5967 if Nkind (P) = N_Component_Association
5968 and then Nkind (Parent (P)) = N_Aggregate
5969 then
5970 null;
5972 -- All done if not still within subexpression
5974 else
5975 exit when Nkind (P) not in N_Subexpr;
5976 end if;
5977 end loop;
5979 -- If we fall through the loop, not one of the cases covered!
5981 return False;
5982 end Is_Statically_Unevaluated;
5984 --------------------
5985 -- Machine_Number --
5986 --------------------
5988 -- Historical note: RM 4.9(38) originally specified biased rounding but
5989 -- this has been modified by AI-268 to prevent confusing differences in
5990 -- rounding between static and nonstatic expressions. This AI specifies
5991 -- that the effect of such rounding is implementation-dependent instead,
5992 -- and in GNAT we round to nearest even to match the run-time behavior.
5993 -- Note that this applies to floating-point literals, not fixed-point
5994 -- ones, even though their representation is also a universal real.
5996 function Machine_Number
5997 (Typ : Entity_Id;
5998 Val : Ureal;
5999 N : Node_Id) return Ureal
6001 begin
6002 return Machine (Typ, Val, Round_Even, N);
6003 end Machine_Number;
6005 --------------------
6006 -- Not_Null_Range --
6007 --------------------
6009 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
6010 begin
6011 if Compile_Time_Known_Value (Lo)
6012 and then Compile_Time_Known_Value (Hi)
6013 then
6014 declare
6015 Typ : Entity_Id := Etype (Lo);
6016 begin
6017 -- When called from the frontend, as part of the analysis of
6018 -- potentially static expressions, Typ will be the full view of a
6019 -- type with all the info needed to answer this query. When called
6020 -- from the backend, for example to know whether a range of a loop
6021 -- is null, Typ might be a private type and we need to explicitly
6022 -- switch to its corresponding full view to access the same info.
6024 if Is_Incomplete_Or_Private_Type (Typ)
6025 and then Present (Full_View (Typ))
6026 then
6027 Typ := Full_View (Typ);
6028 end if;
6030 if Is_Discrete_Type (Typ) then
6031 return Expr_Value (Lo) <= Expr_Value (Hi);
6032 else pragma Assert (Is_Real_Type (Typ));
6033 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
6034 end if;
6035 end;
6037 else
6038 return
6039 Compile_Time_Compare (Lo, Hi, Assume_Valid => False) in Compare_LE;
6040 end if;
6041 end Not_Null_Range;
6043 -------------
6044 -- OK_Bits --
6045 -------------
6047 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
6048 begin
6049 -- We allow a maximum of 500,000 bits which seems a reasonable limit
6051 if Bits < 500_000 then
6052 return True;
6054 -- Error if this maximum is exceeded
6056 else
6057 Error_Msg_N ("static value too large, capacity exceeded", N);
6058 return False;
6059 end if;
6060 end OK_Bits;
6062 ------------------
6063 -- Out_Of_Range --
6064 ------------------
6066 procedure Out_Of_Range (N : Node_Id) is
6068 -- If the FE conjures up an expression that would normally be
6069 -- an illegal static expression (e.g., an integer literal with
6070 -- a value outside of its base subtype), we don't want to
6071 -- flag it as illegal; we only want a warning in such cases.
6073 function Force_Warning return Boolean is
6074 (if Comes_From_Source (Original_Node (N)) then False
6075 elsif Nkind (Original_Node (N)) = N_Type_Conversion then True
6076 else Is_Null_Array_Aggregate_High_Bound (N));
6077 begin
6078 -- If we have the static expression case, then this is an illegality
6079 -- in Ada 95 mode, except that in an instance, we never generate an
6080 -- error (if the error is legitimate, it was already diagnosed in the
6081 -- template).
6083 if Is_Static_Expression (N)
6084 and then not In_Instance
6085 and then not In_Inlined_Body
6086 and then Ada_Version >= Ada_95
6087 then
6088 -- No message if we are statically unevaluated
6090 if Is_Statically_Unevaluated (N) then
6091 null;
6093 -- The expression to compute the length of a packed array is attached
6094 -- to the array type itself, and deserves a separate message.
6096 elsif Nkind (Parent (N)) = N_Defining_Identifier
6097 and then Is_Array_Type (Parent (N))
6098 and then Present (Packed_Array_Impl_Type (Parent (N)))
6099 and then Present (First_Rep_Item (Parent (N)))
6100 then
6101 Error_Msg_N
6102 ("length of packed array must not exceed Integer''Last",
6103 First_Rep_Item (Parent (N)));
6104 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
6106 -- All cases except the special array case.
6107 -- No message if we are dealing with System.Priority values in
6108 -- CodePeer mode where the target runtime may have more priorities.
6110 elsif not CodePeer_Mode
6111 or else not Is_RTE (Etype (N), RE_Priority)
6112 then
6113 -- Determine if the out-of-range violation constitutes a warning
6114 -- or an error based on context, according to RM 4.9 (34/3).
6116 if Force_Warning then
6117 Apply_Compile_Time_Constraint_Error
6118 (N, "value not in range of}??", CE_Range_Check_Failed);
6119 else
6120 Apply_Compile_Time_Constraint_Error
6121 (N, "value not in range of}", CE_Range_Check_Failed);
6122 end if;
6123 end if;
6125 -- Here we generate a warning for the Ada 83 case, or when we are in an
6126 -- instance, or when we have a non-static expression case.
6128 else
6129 Apply_Compile_Time_Constraint_Error
6130 (N, "value not in range of}??", CE_Range_Check_Failed);
6131 end if;
6132 end Out_Of_Range;
6134 ---------------------------
6135 -- Predicates_Compatible --
6136 ---------------------------
6138 function Predicates_Compatible (T1, T2 : Entity_Id) return Boolean is
6140 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean;
6141 -- Return True if the rep item for Nam is either absent on T2 or also
6142 -- applies to T1.
6144 -------------------------------
6145 -- T2_Rep_Item_Applies_To_T1 --
6146 -------------------------------
6148 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean is
6149 Rep_Item : constant Node_Id := Get_Rep_Item (T2, Nam);
6151 begin
6152 return No (Rep_Item) or else Get_Rep_Item (T1, Nam) = Rep_Item;
6153 end T2_Rep_Item_Applies_To_T1;
6155 -- Start of processing for Predicates_Compatible
6157 begin
6158 if Ada_Version < Ada_2012 then
6159 return True;
6161 -- If T2 has no predicates, there is no compatibility issue
6163 elsif not Has_Predicates (T2) then
6164 return True;
6166 -- T2 has predicates, if T1 has none then we defer to the static check
6168 elsif not Has_Predicates (T1) then
6169 null;
6171 -- Both T2 and T1 have predicates, check that all predicates that apply
6172 -- to T2 apply also to T1 (RM 4.9.1(9/3)).
6174 elsif T2_Rep_Item_Applies_To_T1 (Name_Static_Predicate)
6175 and then T2_Rep_Item_Applies_To_T1 (Name_Dynamic_Predicate)
6176 and then T2_Rep_Item_Applies_To_T1 (Name_Predicate)
6177 then
6178 return True;
6179 end if;
6181 -- Implement the static check prescribed by RM 4.9.1(10/3)
6183 if Is_Static_Subtype (T1) and then Is_Static_Subtype (T2) then
6184 -- We just need to query Interval_Lists for discrete types
6186 if Is_Discrete_Type (T1) and then Is_Discrete_Type (T2) then
6187 declare
6188 Interval_List1 : constant Interval_Lists.Discrete_Interval_List
6189 := Interval_Lists.Type_Intervals (T1);
6190 Interval_List2 : constant Interval_Lists.Discrete_Interval_List
6191 := Interval_Lists.Type_Intervals (T2);
6192 begin
6193 return Interval_Lists.Is_Subset (Interval_List1, Interval_List2)
6194 and then not (Has_Predicates (T1)
6195 and then not Predicate_Checks_Suppressed (T2)
6196 and then Predicate_Checks_Suppressed (T1));
6197 end;
6199 else
6200 -- ??? Need to implement Interval_Lists for real types
6202 return False;
6203 end if;
6205 -- If either subtype is not static, the predicates are not compatible
6207 else
6208 return False;
6209 end if;
6210 end Predicates_Compatible;
6212 ----------------------
6213 -- Predicates_Match --
6214 ----------------------
6216 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
6218 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean;
6219 -- Return True if T1 and T2 have the same rep item for Nam
6221 ------------------------
6222 -- Have_Same_Rep_Item --
6223 ------------------------
6225 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean is
6226 begin
6227 return Get_Rep_Item (T1, Nam) = Get_Rep_Item (T2, Nam);
6228 end Have_Same_Rep_Item;
6230 -- Start of processing for Predicates_Match
6232 begin
6233 if Ada_Version < Ada_2012 then
6234 return True;
6236 -- If T2 has no predicates, match if and only if T1 has none
6238 elsif not Has_Predicates (T2) then
6239 return not Has_Predicates (T1);
6241 -- T2 has predicates, no match if T1 has none
6243 elsif not Has_Predicates (T1) then
6244 return False;
6246 -- Both T2 and T1 have predicates, check that they all come
6247 -- from the same declarations.
6249 else
6250 return Have_Same_Rep_Item (Name_Static_Predicate)
6251 and then Have_Same_Rep_Item (Name_Dynamic_Predicate)
6252 and then Have_Same_Rep_Item (Name_Predicate);
6253 end if;
6254 end Predicates_Match;
6256 ---------------------------------------------
6257 -- Real_Or_String_Static_Predicate_Matches --
6258 ---------------------------------------------
6260 function Real_Or_String_Static_Predicate_Matches
6261 (Val : Node_Id;
6262 Typ : Entity_Id) return Boolean
6264 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
6265 -- The predicate expression from the type
6267 Pfun : constant Entity_Id := Predicate_Function (Typ);
6268 -- The entity for the predicate function
6270 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
6271 -- The name of the formal of the predicate function. Occurrences of the
6272 -- type name in Expr have been rewritten as references to this formal,
6273 -- and it has a unique name, so we can identify references by this name.
6275 Copy : Node_Id;
6276 -- Copy of the predicate function tree
6278 function Process (N : Node_Id) return Traverse_Result;
6279 -- Function used to process nodes during the traversal in which we will
6280 -- find occurrences of the entity name, and replace such occurrences
6281 -- by a real literal with the value to be tested.
6283 procedure Traverse is new Traverse_Proc (Process);
6284 -- The actual traversal procedure
6286 -------------
6287 -- Process --
6288 -------------
6290 function Process (N : Node_Id) return Traverse_Result is
6291 begin
6292 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
6293 declare
6294 Nod : constant Node_Id := New_Copy (Val);
6295 begin
6296 Set_Sloc (Nod, Sloc (N));
6297 Rewrite (N, Nod);
6298 return Skip;
6299 end;
6301 -- The predicate function may contain string-comparison operations
6302 -- that have been converted into calls to run-time array-comparison
6303 -- routines. To evaluate the predicate statically, we recover the
6304 -- original comparison operation and replace the occurrence of the
6305 -- formal by the static string value. The actuals of the generated
6306 -- call are of the form X'Address.
6308 elsif Nkind (N) in N_Op_Compare
6309 and then Nkind (Left_Opnd (N)) = N_Function_Call
6310 then
6311 declare
6312 C : constant Node_Id := Left_Opnd (N);
6313 F : constant Node_Id := First (Parameter_Associations (C));
6314 L : constant Node_Id := Prefix (F);
6315 R : constant Node_Id := Prefix (Next (F));
6317 begin
6318 -- If an operand is an entity name, it is the formal of the
6319 -- predicate function, so replace it with the string value.
6320 -- It may be either operand in the call. The other operand
6321 -- is a static string from the original predicate.
6323 if Is_Entity_Name (L) then
6324 Rewrite (Left_Opnd (N), New_Copy (Val));
6325 Rewrite (Right_Opnd (N), New_Copy (R));
6327 else
6328 Rewrite (Left_Opnd (N), New_Copy (L));
6329 Rewrite (Right_Opnd (N), New_Copy (Val));
6330 end if;
6332 return Skip;
6333 end;
6335 else
6336 return OK;
6337 end if;
6338 end Process;
6340 -- Start of processing for Real_Or_String_Static_Predicate_Matches
6342 begin
6343 -- First deal with special case of inherited predicate, where the
6344 -- predicate expression looks like:
6346 -- xxPredicate (typ (Ent)) and then Expr
6348 -- where Expr is the predicate expression for this level, and the
6349 -- left operand is the call to evaluate the inherited predicate.
6351 if Nkind (Expr) = N_And_Then
6352 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
6353 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
6354 then
6355 -- OK we have the inherited case, so make a call to evaluate the
6356 -- inherited predicate. If that fails, so do we!
6358 if not
6359 Real_Or_String_Static_Predicate_Matches
6360 (Val => Val,
6361 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
6362 then
6363 return False;
6364 end if;
6366 -- Use the right operand for the continued processing
6368 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
6370 -- Case where call to predicate function appears on its own (this means
6371 -- that the predicate at this level is just inherited from the parent).
6373 elsif Nkind (Expr) = N_Function_Call then
6374 declare
6375 Typ : constant Entity_Id :=
6376 Etype (First_Formal (Entity (Name (Expr))));
6378 begin
6379 -- If the inherited predicate is not static, just ignore it. We
6380 -- can't go trying to evaluate a dynamic predicate as a static
6381 -- one!
6383 if Has_Dynamic_Predicate_Aspect (Typ)
6384 or else Has_Ghost_Predicate_Aspect (Typ)
6385 then
6386 return True;
6388 -- Otherwise inherited predicate is static, check for match
6390 else
6391 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
6392 end if;
6393 end;
6395 -- If not just an inherited predicate, copy whole expression
6397 else
6398 Copy := Copy_Separate_Tree (Expr);
6399 end if;
6401 -- Now we replace occurrences of the entity by the value
6403 Traverse (Copy);
6405 -- And analyze the resulting static expression to see if it is True
6407 Analyze_And_Resolve (Copy, Standard_Boolean);
6408 return Is_True (Expr_Value (Copy));
6409 end Real_Or_String_Static_Predicate_Matches;
6411 -------------------------
6412 -- Rewrite_In_Raise_CE --
6413 -------------------------
6415 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
6416 Stat : constant Boolean := Is_Static_Expression (N);
6417 Typ : constant Entity_Id := Etype (N);
6419 begin
6420 -- If we want to raise CE in the condition of a N_Raise_CE node, we
6421 -- can just clear the condition if the reason is appropriate. We do
6422 -- not do this operation if the parent has a reason other than range
6423 -- check failed, because otherwise we would change the reason.
6425 if Present (Parent (N))
6426 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
6427 and then Reason (Parent (N)) =
6428 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
6429 then
6430 Set_Condition (Parent (N), Empty);
6432 -- Else build an explicit N_Raise_CE
6434 else
6435 if Nkind (Exp) = N_Raise_Constraint_Error then
6436 Rewrite (N,
6437 Make_Raise_Constraint_Error (Sloc (Exp),
6438 Reason => Reason (Exp)));
6439 else
6440 Rewrite (N,
6441 Make_Raise_Constraint_Error (Sloc (Exp),
6442 Reason => CE_Range_Check_Failed));
6443 end if;
6445 Set_Raises_Constraint_Error (N);
6446 Set_Etype (N, Typ);
6447 end if;
6449 -- Set proper flags in result
6451 Set_Raises_Constraint_Error (N, True);
6452 Set_Is_Static_Expression (N, Stat);
6453 end Rewrite_In_Raise_CE;
6455 ------------------------------------------------
6456 -- Set_Checking_Potentially_Static_Expression --
6457 ------------------------------------------------
6459 procedure Set_Checking_Potentially_Static_Expression (Value : Boolean) is
6460 begin
6461 -- Verify that we only start/stop checking for a potentially static
6462 -- expression and do not start or stop it twice in a row.
6464 pragma Assert (Checking_For_Potentially_Static_Expression /= Value);
6466 Checking_For_Potentially_Static_Expression := Value;
6467 end Set_Checking_Potentially_Static_Expression;
6469 ---------------------
6470 -- String_Type_Len --
6471 ---------------------
6473 function String_Type_Len (Stype : Entity_Id) return Uint is
6474 NT : constant Entity_Id := Etype (First_Index (Stype));
6475 T : Entity_Id;
6477 begin
6478 if Is_OK_Static_Subtype (NT) then
6479 T := NT;
6480 else
6481 T := Base_Type (NT);
6482 end if;
6484 return Expr_Value (Type_High_Bound (T)) -
6485 Expr_Value (Type_Low_Bound (T)) + 1;
6486 end String_Type_Len;
6488 ------------------------------------
6489 -- Subtypes_Statically_Compatible --
6490 ------------------------------------
6492 function Subtypes_Statically_Compatible
6493 (T1 : Entity_Id;
6494 T2 : Entity_Id;
6495 Formal_Derived_Matching : Boolean := False) return Boolean
6497 begin
6498 -- A type is always statically compatible with itself
6500 if T1 = T2 then
6501 return True;
6503 -- Not compatible if predicates are not compatible
6505 elsif not Predicates_Compatible (T1, T2) then
6506 return False;
6508 -- Scalar types
6510 elsif Is_Scalar_Type (T1) and then Is_Scalar_Type (T2) then
6512 -- Definitely compatible if we match
6514 if Subtypes_Statically_Match (T1, T2) then
6515 return True;
6517 -- A scalar subtype S1 is compatible with S2 if their bounds
6518 -- are static and compatible, even if S1 has dynamic predicates
6519 -- and is thus non-static. Predicate compatibility has been
6520 -- checked above.
6522 elsif not Is_Static_Range (Scalar_Range (T1))
6523 or else not Is_Static_Range (Scalar_Range (T2))
6524 then
6525 return False;
6527 -- Base types must match, but we don't check that (should we???) but
6528 -- we do at least check that both types are real, or both types are
6529 -- not real.
6531 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
6532 return False;
6534 -- Here we check the bounds
6536 else
6537 declare
6538 LB1 : constant Node_Id := Type_Low_Bound (T1);
6539 HB1 : constant Node_Id := Type_High_Bound (T1);
6540 LB2 : constant Node_Id := Type_Low_Bound (T2);
6541 HB2 : constant Node_Id := Type_High_Bound (T2);
6543 begin
6544 if Is_Real_Type (T1) then
6545 return
6546 Expr_Value_R (LB1) > Expr_Value_R (HB1)
6547 or else
6548 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
6549 and then Expr_Value_R (HB1) <= Expr_Value_R (HB2));
6551 else
6552 return
6553 Expr_Value (LB1) > Expr_Value (HB1)
6554 or else
6555 (Expr_Value (LB2) <= Expr_Value (LB1)
6556 and then Expr_Value (HB1) <= Expr_Value (HB2));
6557 end if;
6558 end;
6559 end if;
6561 -- Access types
6563 elsif Is_Access_Type (T1) and then Is_Access_Type (T2) then
6564 return
6565 (not Is_Constrained (T2)
6566 or else Subtypes_Statically_Match
6567 (Designated_Type (T1), Designated_Type (T2)))
6568 and then not (Can_Never_Be_Null (T2)
6569 and then not Can_Never_Be_Null (T1));
6571 -- Private types without discriminants can be handled specially.
6572 -- Predicate matching has been checked above.
6574 elsif Is_Private_Type (T1)
6575 and then not Has_Discriminants (T1)
6576 then
6577 return not Has_Discriminants (T2);
6579 -- All other cases
6581 else
6582 return
6583 (Is_Composite_Type (T1) and then not Is_Constrained (T2))
6584 or else Subtypes_Statically_Match
6585 (T1, T2, Formal_Derived_Matching);
6586 end if;
6587 end Subtypes_Statically_Compatible;
6589 -------------------------------
6590 -- Subtypes_Statically_Match --
6591 -------------------------------
6593 -- Subtypes statically match if they have statically matching constraints
6594 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
6595 -- they are the same identical constraint, or if they are static and the
6596 -- values match (RM 4.9.1(1)).
6598 -- In addition, in GNAT, the object size (Esize) values of the types must
6599 -- match if they are set (unless checking an actual for a formal derived
6600 -- type). The use of 'Object_Size can cause this to be false even if the
6601 -- types would otherwise match in the Ada 95 RM sense, but this deviation
6602 -- is adopted by AI12-059 which introduces Object_Size in Ada 2022.
6604 function Subtypes_Statically_Match
6605 (T1 : Entity_Id;
6606 T2 : Entity_Id;
6607 Formal_Derived_Matching : Boolean := False) return Boolean
6609 begin
6610 -- A type always statically matches itself
6612 if T1 = T2 then
6613 return True;
6615 -- No match if sizes different (from use of 'Object_Size). This test
6616 -- is excluded if Formal_Derived_Matching is True, as the base types
6617 -- can be different in that case and typically have different sizes.
6619 elsif not Formal_Derived_Matching
6620 and then Known_Static_Esize (T1)
6621 and then Known_Static_Esize (T2)
6622 and then Esize (T1) /= Esize (T2)
6623 then
6624 return False;
6626 -- No match if predicates do not match
6628 elsif not Predicates_Match (T1, T2) then
6629 return False;
6631 -- Scalar types
6633 elsif Is_Scalar_Type (T1) then
6635 -- Base types must be the same
6637 if Base_Type (T1) /= Base_Type (T2) then
6638 return False;
6639 end if;
6641 -- A constrained numeric subtype never matches an unconstrained
6642 -- subtype, i.e. both types must be constrained or unconstrained.
6644 -- To understand the requirement for this test, see RM 4.9.1(1).
6645 -- As is made clear in RM 3.5.4(11), type Integer, for example is
6646 -- a constrained subtype with constraint bounds matching the bounds
6647 -- of its corresponding unconstrained base type. In this situation,
6648 -- Integer and Integer'Base do not statically match, even though
6649 -- they have the same bounds.
6651 -- We only apply this test to types in Standard and types that appear
6652 -- in user programs. That way, we do not have to be too careful about
6653 -- setting Is_Constrained right for Itypes.
6655 if Is_Numeric_Type (T1)
6656 and then Is_Constrained (T1) /= Is_Constrained (T2)
6657 and then (Scope (T1) = Standard_Standard
6658 or else Comes_From_Source (T1))
6659 and then (Scope (T2) = Standard_Standard
6660 or else Comes_From_Source (T2))
6661 then
6662 return False;
6664 -- A generic scalar type does not statically match its base type
6665 -- (AI-311). In this case we make sure that the formals, which are
6666 -- first subtypes of their bases, are constrained.
6668 elsif Is_Generic_Type (T1)
6669 and then Is_Generic_Type (T2)
6670 and then Is_Constrained (T1) /= Is_Constrained (T2)
6671 then
6672 return False;
6673 end if;
6675 -- If there was an error in either range, then just assume the types
6676 -- statically match to avoid further junk errors.
6678 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
6679 or else Error_Posted (Scalar_Range (T1))
6680 or else Error_Posted (Scalar_Range (T2))
6681 then
6682 return True;
6683 end if;
6685 -- Otherwise both types have bounds that can be compared
6687 declare
6688 LB1 : constant Node_Id := Type_Low_Bound (T1);
6689 HB1 : constant Node_Id := Type_High_Bound (T1);
6690 LB2 : constant Node_Id := Type_Low_Bound (T2);
6691 HB2 : constant Node_Id := Type_High_Bound (T2);
6693 begin
6694 -- If the bounds are the same tree node, then match (common case)
6696 if LB1 = LB2 and then HB1 = HB2 then
6697 return True;
6699 -- Otherwise bounds must be static and identical value
6701 else
6702 if not Is_OK_Static_Subtype (T1)
6703 or else
6704 not Is_OK_Static_Subtype (T2)
6705 then
6706 return False;
6708 elsif Is_Real_Type (T1) then
6709 return
6710 Expr_Value_R (LB1) = Expr_Value_R (LB2)
6711 and then
6712 Expr_Value_R (HB1) = Expr_Value_R (HB2);
6714 else
6715 return
6716 Expr_Value (LB1) = Expr_Value (LB2)
6717 and then
6718 Expr_Value (HB1) = Expr_Value (HB2);
6719 end if;
6720 end if;
6721 end;
6723 -- Type with discriminants
6725 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
6727 -- Handle derivations of private subtypes. For example S1 statically
6728 -- matches the full view of T1 in the following example:
6730 -- type T1(<>) is new Root with private;
6731 -- subtype S1 is new T1;
6732 -- overriding proc P1 (P : S1);
6733 -- private
6734 -- type T1 (D : Disc) is new Root with ...
6736 if Ekind (T2) = E_Record_Subtype_With_Private
6737 and then not Has_Discriminants (T2)
6738 and then Partial_View_Has_Unknown_Discr (T1)
6739 and then Etype (T2) = T1
6740 then
6741 return True;
6743 elsif Ekind (T1) = E_Record_Subtype_With_Private
6744 and then not Has_Discriminants (T1)
6745 and then Partial_View_Has_Unknown_Discr (T2)
6746 and then Etype (T1) = T2
6747 then
6748 return True;
6750 -- Because of view exchanges in multiple instantiations, conformance
6751 -- checking might try to match a partial view of a type with no
6752 -- discriminants with a full view that has defaulted discriminants.
6753 -- In such a case, use the discriminant constraint of the full view,
6754 -- which must exist because we know that the two subtypes have the
6755 -- same base type.
6757 elsif Has_Discriminants (T1) /= Has_Discriminants (T2) then
6758 if In_Instance then
6759 if Is_Private_Type (T2)
6760 and then Present (Full_View (T2))
6761 and then Has_Discriminants (Full_View (T2))
6762 then
6763 return Subtypes_Statically_Match (T1, Full_View (T2));
6765 elsif Is_Private_Type (T1)
6766 and then Present (Full_View (T1))
6767 and then Has_Discriminants (Full_View (T1))
6768 then
6769 return Subtypes_Statically_Match (Full_View (T1), T2);
6771 else
6772 return False;
6773 end if;
6774 else
6775 return False;
6776 end if;
6777 end if;
6779 declare
6781 function Original_Discriminant_Constraint
6782 (Typ : Entity_Id) return Elist_Id;
6783 -- Returns Typ's discriminant constraint, or if the constraint
6784 -- is inherited from an ancestor type, then climbs the parent
6785 -- types to locate and return the constraint farthest up the
6786 -- parent chain that Typ's constraint is ultimately inherited
6787 -- from (stopping before a parent that doesn't impose a constraint
6788 -- or a parent that has new discriminants). This ensures a proper
6789 -- result from the equality comparison of Elist_Ids below (as
6790 -- otherwise, derived types that inherit constraints may appear
6791 -- to be unequal, because each level of derivation can have its
6792 -- own copy of the constraint).
6794 function Original_Discriminant_Constraint
6795 (Typ : Entity_Id) return Elist_Id
6797 begin
6798 if not Has_Discriminants (Typ) then
6799 return No_Elist;
6801 -- If Typ is not a derived type, then directly return the
6802 -- its constraint.
6804 elsif not Is_Derived_Type (Typ) then
6805 return Discriminant_Constraint (Typ);
6807 -- If the parent type doesn't have discriminants, doesn't
6808 -- have a constraint, or has new discriminants, then stop
6809 -- and return Typ's constraint.
6811 elsif not Has_Discriminants (Etype (Typ))
6813 -- No constraint on the parent type
6815 or else No (Discriminant_Constraint (Etype (Typ)))
6816 or else Is_Empty_Elmt_List
6817 (Discriminant_Constraint (Etype (Typ)))
6819 -- The parent type defines new discriminants
6821 or else
6822 (Is_Base_Type (Etype (Typ))
6823 and then Present (Discriminant_Specifications
6824 (Parent (Etype (Typ)))))
6825 then
6826 return Discriminant_Constraint (Typ);
6828 -- Otherwise, make a recursive call on the parent type
6830 else
6831 return Original_Discriminant_Constraint (Etype (Typ));
6832 end if;
6833 end Original_Discriminant_Constraint;
6835 -- Local variables
6837 DL1 : constant Elist_Id := Original_Discriminant_Constraint (T1);
6838 DL2 : constant Elist_Id := Original_Discriminant_Constraint (T2);
6840 DA1 : Elmt_Id;
6841 DA2 : Elmt_Id;
6843 begin
6844 if DL1 = DL2 then
6845 return True;
6846 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
6847 return False;
6848 end if;
6850 -- Now loop through the discriminant constraints
6852 -- Note: the guard here seems necessary, since it is possible at
6853 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
6855 if Present (DL1) and then Present (DL2) then
6856 DA1 := First_Elmt (DL1);
6857 DA2 := First_Elmt (DL2);
6858 while Present (DA1) loop
6859 declare
6860 Expr1 : constant Node_Id := Node (DA1);
6861 Expr2 : constant Node_Id := Node (DA2);
6863 begin
6864 if not Is_OK_Static_Expression (Expr1)
6865 or else not Is_OK_Static_Expression (Expr2)
6866 then
6867 return False;
6869 -- If either expression raised a Constraint_Error,
6870 -- consider the expressions as matching, since this
6871 -- helps to prevent cascading errors.
6873 elsif Raises_Constraint_Error (Expr1)
6874 or else Raises_Constraint_Error (Expr2)
6875 then
6876 null;
6878 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
6879 return False;
6880 end if;
6881 end;
6883 Next_Elmt (DA1);
6884 Next_Elmt (DA2);
6885 end loop;
6886 end if;
6887 end;
6889 return True;
6891 -- A definite type does not match an indefinite or classwide type.
6892 -- However, a generic type with unknown discriminants may be
6893 -- instantiated with a type with no discriminants, and conformance
6894 -- checking on an inherited operation may compare the actual with the
6895 -- subtype that renames it in the instance.
6897 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
6898 then
6899 return
6900 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
6902 -- Array type
6904 elsif Is_Array_Type (T1) then
6906 -- If either subtype is unconstrained then both must be, and if both
6907 -- are unconstrained then no further checking is needed.
6909 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
6910 return not (Is_Constrained (T1) or else Is_Constrained (T2));
6911 end if;
6913 -- Both subtypes are constrained, so check that the index subtypes
6914 -- statically match.
6916 declare
6917 Index1 : Node_Id := First_Index (T1);
6918 Index2 : Node_Id := First_Index (T2);
6920 begin
6921 while Present (Index1) loop
6922 if not
6923 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6924 then
6925 return False;
6926 end if;
6928 Next_Index (Index1);
6929 Next_Index (Index2);
6930 end loop;
6932 return True;
6933 end;
6935 elsif Is_Access_Type (T1) then
6936 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6937 return False;
6939 elsif Ekind (T1) in E_Access_Subprogram_Type
6940 | E_Anonymous_Access_Subprogram_Type
6941 then
6942 return
6943 Subtype_Conformant
6944 (Designated_Type (T1),
6945 Designated_Type (T2));
6946 else
6947 return
6948 Subtypes_Statically_Match
6949 (Designated_Type (T1),
6950 Designated_Type (T2))
6951 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
6952 end if;
6954 -- All other types definitely match
6956 else
6957 return True;
6958 end if;
6959 end Subtypes_Statically_Match;
6961 ----------
6962 -- Test --
6963 ----------
6965 function Test (Cond : Boolean) return Uint is
6966 begin
6967 if Cond then
6968 return Uint_1;
6969 else
6970 return Uint_0;
6971 end if;
6972 end Test;
6974 ---------------------
6975 -- Test_Comparison --
6976 ---------------------
6978 procedure Test_Comparison
6979 (Op : Node_Id;
6980 Assume_Valid : Boolean;
6981 True_Result : out Boolean;
6982 False_Result : out Boolean)
6984 Left : constant Node_Id := Left_Opnd (Op);
6985 Left_Typ : constant Entity_Id := Etype (Left);
6986 Orig_Op : constant Node_Id := Original_Node (Op);
6988 procedure Replacement_Warning (Msg : String);
6989 -- Emit a warning on a comparison that can be replaced by '='
6991 -------------------------
6992 -- Replacement_Warning --
6993 -------------------------
6995 procedure Replacement_Warning (Msg : String) is
6996 begin
6997 if Constant_Condition_Warnings
6998 and then Comes_From_Source (Orig_Op)
6999 and then Is_Integer_Type (Left_Typ)
7000 and then not Error_Posted (Op)
7001 and then not Has_Warnings_Off (Left_Typ)
7002 and then not In_Instance
7003 then
7004 Error_Msg_N (Msg, Op);
7005 end if;
7006 end Replacement_Warning;
7008 -- Local variables
7010 Res : constant Compare_Result :=
7011 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
7013 -- Start of processing for Test_Comparison
7015 begin
7016 case N_Op_Compare (Nkind (Op)) is
7017 when N_Op_Eq =>
7018 True_Result := Res = EQ;
7019 False_Result := Res = LT or else Res = GT or else Res = NE;
7021 when N_Op_Ge =>
7022 True_Result := Res in Compare_GE;
7023 False_Result := Res = LT;
7025 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
7026 Replacement_Warning
7027 ("can never be greater than, could replace by ""'=""?c?");
7028 end if;
7030 when N_Op_Gt =>
7031 True_Result := Res = GT;
7032 False_Result := Res in Compare_LE;
7034 when N_Op_Le =>
7035 True_Result := Res in Compare_LE;
7036 False_Result := Res = GT;
7038 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
7039 Replacement_Warning
7040 ("can never be less than, could replace by ""'=""?c?");
7041 end if;
7043 when N_Op_Lt =>
7044 True_Result := Res = LT;
7045 False_Result := Res in Compare_GE;
7047 when N_Op_Ne =>
7048 True_Result := Res = NE or else Res = GT or else Res = LT;
7049 False_Result := Res = EQ;
7050 end case;
7051 end Test_Comparison;
7053 ---------------------------------
7054 -- Test_Expression_Is_Foldable --
7055 ---------------------------------
7057 -- One operand case
7059 procedure Test_Expression_Is_Foldable
7060 (N : Node_Id;
7061 Op1 : Node_Id;
7062 Stat : out Boolean;
7063 Fold : out Boolean)
7065 begin
7066 Stat := False;
7067 Fold := False;
7069 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7070 return;
7071 end if;
7073 -- If operand is Any_Type, just propagate to result and do not
7074 -- try to fold, this prevents cascaded errors.
7076 if Etype (Op1) = Any_Type then
7077 Set_Etype (N, Any_Type);
7078 return;
7080 -- If operand raises Constraint_Error, then replace node N with the
7081 -- raise Constraint_Error node, and we are obviously not foldable.
7082 -- Note that this replacement inherits the Is_Static_Expression flag
7083 -- from the operand.
7085 elsif Raises_Constraint_Error (Op1) then
7086 Rewrite_In_Raise_CE (N, Op1);
7087 return;
7089 -- If the operand is not static, then the result is not static, and
7090 -- all we have to do is to check the operand since it is now known
7091 -- to appear in a non-static context.
7093 elsif not Is_Static_Expression (Op1) then
7094 Check_Non_Static_Context (Op1);
7095 Fold := Compile_Time_Known_Value (Op1);
7096 return;
7098 -- An expression of a formal modular type is not foldable because
7099 -- the modulus is unknown.
7101 elsif Is_Modular_Integer_Type (Etype (Op1))
7102 and then Is_Generic_Type (Etype (Op1))
7103 then
7104 Check_Non_Static_Context (Op1);
7105 return;
7107 -- Here we have the case of an operand whose type is OK, which is
7108 -- static, and which does not raise Constraint_Error, we can fold.
7110 else
7111 Set_Is_Static_Expression (N);
7112 Fold := True;
7113 Stat := True;
7114 end if;
7115 end Test_Expression_Is_Foldable;
7117 -- Two operand case
7119 procedure Test_Expression_Is_Foldable
7120 (N : Node_Id;
7121 Op1 : Node_Id;
7122 Op2 : Node_Id;
7123 Stat : out Boolean;
7124 Fold : out Boolean;
7125 CRT_Safe : Boolean := False)
7127 Rstat : constant Boolean := Is_Static_Expression (Op1)
7128 and then
7129 Is_Static_Expression (Op2);
7131 begin
7132 Stat := False;
7133 Fold := False;
7135 -- Inhibit folding if -gnatd.f flag set
7137 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7138 return;
7139 end if;
7141 -- If either operand is Any_Type, just propagate to result and
7142 -- do not try to fold, this prevents cascaded errors.
7144 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
7145 Set_Etype (N, Any_Type);
7146 return;
7148 -- If left operand raises Constraint_Error, then replace node N with the
7149 -- Raise_Constraint_Error node, and we are obviously not foldable.
7150 -- Is_Static_Expression is set from the two operands in the normal way,
7151 -- and we check the right operand if it is in a non-static context.
7153 elsif Raises_Constraint_Error (Op1) then
7154 if not Rstat then
7155 Check_Non_Static_Context (Op2);
7156 end if;
7158 Rewrite_In_Raise_CE (N, Op1);
7159 Set_Is_Static_Expression (N, Rstat);
7160 return;
7162 -- Similar processing for the case of the right operand. Note that we
7163 -- don't use this routine for the short-circuit case, so we do not have
7164 -- to worry about that special case here.
7166 elsif Raises_Constraint_Error (Op2) then
7167 if not Rstat then
7168 Check_Non_Static_Context (Op1);
7169 end if;
7171 Rewrite_In_Raise_CE (N, Op2);
7172 Set_Is_Static_Expression (N, Rstat);
7173 return;
7175 -- Exclude expressions of a generic modular type, as above
7177 elsif Is_Modular_Integer_Type (Etype (Op1))
7178 and then Is_Generic_Type (Etype (Op1))
7179 then
7180 Check_Non_Static_Context (Op1);
7181 return;
7183 -- If result is not static, then check non-static contexts on operands
7184 -- since one of them may be static and the other one may not be static.
7186 elsif not Rstat then
7187 Check_Non_Static_Context (Op1);
7188 Check_Non_Static_Context (Op2);
7190 if CRT_Safe then
7191 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
7192 and then CRT_Safe_Compile_Time_Known_Value (Op2);
7193 else
7194 Fold := Compile_Time_Known_Value (Op1)
7195 and then Compile_Time_Known_Value (Op2);
7196 end if;
7198 if not Fold
7199 and then not Is_Modular_Integer_Type (Etype (N))
7200 then
7201 case Nkind (N) is
7202 when N_Op_And =>
7204 -- (False and XXX) = (XXX and False) = False
7206 Fold :=
7207 (Compile_Time_Known_Value (Op1)
7208 and then Is_False (Expr_Value (Op1))
7209 and then Side_Effect_Free (Op2))
7210 or else (Compile_Time_Known_Value (Op2)
7211 and then Is_False (Expr_Value (Op2))
7212 and then Side_Effect_Free (Op1));
7214 when N_Op_Or =>
7216 -- (True and XXX) = (XXX and True) = True
7218 Fold :=
7219 (Compile_Time_Known_Value (Op1)
7220 and then Is_True (Expr_Value (Op1))
7221 and then Side_Effect_Free (Op2))
7222 or else (Compile_Time_Known_Value (Op2)
7223 and then Is_True (Expr_Value (Op2))
7224 and then Side_Effect_Free (Op1));
7226 when others => null;
7227 end case;
7228 end if;
7230 return;
7232 -- Else result is static and foldable. Both operands are static, and
7233 -- neither raises Constraint_Error, so we can definitely fold.
7235 else
7236 Set_Is_Static_Expression (N);
7237 Fold := True;
7238 Stat := True;
7239 return;
7240 end if;
7241 end Test_Expression_Is_Foldable;
7243 -------------------
7244 -- Test_In_Range --
7245 -------------------
7247 function Test_In_Range
7248 (N : Node_Id;
7249 Typ : Entity_Id;
7250 Assume_Valid : Boolean;
7251 Fixed_Int : Boolean;
7252 Int_Real : Boolean) return Range_Membership
7254 Val : Uint;
7255 Valr : Ureal;
7257 pragma Warnings (Off, Assume_Valid);
7258 -- For now Assume_Valid is unreferenced since the current implementation
7259 -- always returns Unknown if N is not a compile-time-known value, but we
7260 -- keep the parameter to allow for future enhancements in which we try
7261 -- to get the information in the variable case as well.
7263 begin
7264 -- If an error was posted on expression, then return Unknown, we do not
7265 -- want cascaded errors based on some false analysis of a junk node.
7267 if Error_Posted (N) then
7268 return Unknown;
7270 -- Expression that raises Constraint_Error is an odd case. We certainly
7271 -- do not want to consider it to be in range. It might make sense to
7272 -- consider it always out of range, but this causes incorrect error
7273 -- messages about static expressions out of range. So we just return
7274 -- Unknown, which is always safe.
7276 elsif Raises_Constraint_Error (N) then
7277 return Unknown;
7279 -- Universal types have no range limits, so always in range
7281 elsif Is_Universal_Numeric_Type (Typ) then
7282 return In_Range;
7284 -- Never known if not scalar type. Don't know if this can actually
7285 -- happen, but our spec allows it, so we must check.
7287 elsif not Is_Scalar_Type (Typ) then
7288 return Unknown;
7290 -- Never known if this is a generic type, since the bounds of generic
7291 -- types are junk. Note that if we only checked for static expressions
7292 -- (instead of compile-time-known values) below, we would not need this
7293 -- check, because values of a generic type can never be static, but they
7294 -- can be known at compile time.
7296 elsif Is_Generic_Type (Typ) then
7297 return Unknown;
7299 -- Case of a known compile time value, where we can check if it is in
7300 -- the bounds of the given type.
7302 elsif Compile_Time_Known_Value (N) then
7303 declare
7304 Lo : constant Node_Id := Type_Low_Bound (Typ);
7305 Hi : constant Node_Id := Type_High_Bound (Typ);
7306 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
7307 HB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
7309 begin
7310 -- Fixed point types should be considered as such only if flag
7311 -- Fixed_Int is set to False.
7313 if Is_Floating_Point_Type (Typ)
7314 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
7315 or else Int_Real
7316 then
7317 Valr := Expr_Value_R (N);
7319 if LB_Known and HB_Known then
7320 if Valr >= Expr_Value_R (Lo)
7321 and then
7322 Valr <= Expr_Value_R (Hi)
7323 then
7324 return In_Range;
7325 else
7326 return Out_Of_Range;
7327 end if;
7329 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
7330 or else
7331 (HB_Known and then Valr > Expr_Value_R (Hi))
7332 then
7333 return Out_Of_Range;
7335 else
7336 return Unknown;
7337 end if;
7339 else
7340 Val := Expr_Value (N);
7342 if LB_Known and HB_Known then
7343 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
7344 then
7345 return In_Range;
7346 else
7347 return Out_Of_Range;
7348 end if;
7350 elsif (LB_Known and then Val < Expr_Value (Lo))
7351 or else
7352 (HB_Known and then Val > Expr_Value (Hi))
7353 then
7354 return Out_Of_Range;
7356 else
7357 return Unknown;
7358 end if;
7359 end if;
7360 end;
7362 -- Here for value not known at compile time. Case of expression subtype
7363 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
7364 -- In this case we know it is in range without knowing its value.
7366 elsif Assume_Valid
7367 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
7368 then
7369 return In_Range;
7371 -- Another special case. For signed integer types, if the target type
7372 -- has Is_Known_Valid set, and the source type does not have a larger
7373 -- size, then the source value must be in range. We exclude biased
7374 -- types, because they bizarrely can generate out of range values.
7376 elsif Is_Signed_Integer_Type (Etype (N))
7377 and then Is_Known_Valid (Typ)
7378 and then Esize (Etype (N)) <= Esize (Typ)
7379 and then not Has_Biased_Representation (Etype (N))
7380 then
7381 return In_Range;
7383 -- For all other cases, result is unknown
7385 else
7386 return Unknown;
7387 end if;
7388 end Test_In_Range;
7390 --------------
7391 -- To_Bits --
7392 --------------
7394 procedure To_Bits (U : Uint; B : out Bits) is
7395 begin
7396 for J in 0 .. B'Last loop
7397 B (J) := (U / (2 ** J)) mod 2 /= 0;
7398 end loop;
7399 end To_Bits;
7401 --------------------
7402 -- Why_Not_Static --
7403 --------------------
7405 procedure Why_Not_Static (Expr : Node_Id) is
7406 N : constant Node_Id := Original_Node (Expr);
7407 Typ : Entity_Id := Empty;
7408 E : Entity_Id;
7409 Alt : Node_Id;
7410 Exp : Node_Id;
7412 procedure Why_Not_Static_List (L : List_Id);
7413 -- A version that can be called on a list of expressions. Finds all
7414 -- non-static violations in any element of the list.
7416 -------------------------
7417 -- Why_Not_Static_List --
7418 -------------------------
7420 procedure Why_Not_Static_List (L : List_Id) is
7421 N : Node_Id;
7422 begin
7423 N := First (L);
7424 while Present (N) loop
7425 Why_Not_Static (N);
7426 Next (N);
7427 end loop;
7428 end Why_Not_Static_List;
7430 -- Start of processing for Why_Not_Static
7432 begin
7433 -- Ignore call on error or empty node
7435 if No (Expr) or else Nkind (Expr) = N_Error then
7436 return;
7437 end if;
7439 -- Preprocessing for sub expressions
7441 if Nkind (Expr) in N_Subexpr then
7443 -- Nothing to do if expression is static
7445 if Is_OK_Static_Expression (Expr) then
7446 return;
7447 end if;
7449 -- Test for Constraint_Error raised
7451 if Raises_Constraint_Error (Expr) then
7453 -- Special case membership to find out which piece to flag
7455 if Nkind (N) in N_Membership_Test then
7456 if Raises_Constraint_Error (Left_Opnd (N)) then
7457 Why_Not_Static (Left_Opnd (N));
7458 return;
7460 elsif Present (Right_Opnd (N))
7461 and then Raises_Constraint_Error (Right_Opnd (N))
7462 then
7463 Why_Not_Static (Right_Opnd (N));
7464 return;
7466 else
7467 pragma Assert (Present (Alternatives (N)));
7469 Alt := First (Alternatives (N));
7470 while Present (Alt) loop
7471 if Raises_Constraint_Error (Alt) then
7472 Why_Not_Static (Alt);
7473 return;
7474 else
7475 Next (Alt);
7476 end if;
7477 end loop;
7478 end if;
7480 -- Special case a range to find out which bound to flag
7482 elsif Nkind (N) = N_Range then
7483 if Raises_Constraint_Error (Low_Bound (N)) then
7484 Why_Not_Static (Low_Bound (N));
7485 return;
7487 elsif Raises_Constraint_Error (High_Bound (N)) then
7488 Why_Not_Static (High_Bound (N));
7489 return;
7490 end if;
7492 -- Special case attribute to see which part to flag
7494 elsif Nkind (N) = N_Attribute_Reference then
7495 if Raises_Constraint_Error (Prefix (N)) then
7496 Why_Not_Static (Prefix (N));
7497 return;
7498 end if;
7500 Exp := First (Expressions (N));
7501 while Present (Exp) loop
7502 if Raises_Constraint_Error (Exp) then
7503 Why_Not_Static (Exp);
7504 return;
7505 end if;
7507 Next (Exp);
7508 end loop;
7510 -- Special case a subtype name
7512 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
7513 Error_Msg_NE
7514 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
7515 return;
7516 end if;
7518 -- End of special cases
7520 Error_Msg_N
7521 ("!expression raises exception, cannot be static (RM 4.9(34))",
7523 return;
7524 end if;
7526 -- If no type, then something is pretty wrong, so ignore
7528 Typ := Etype (Expr);
7530 if No (Typ) then
7531 return;
7532 end if;
7534 -- Type must be scalar or string type (but allow Bignum, since this
7535 -- is really a scalar type from our point of view in this diagnosis).
7537 if not Is_Scalar_Type (Typ)
7538 and then not Is_String_Type (Typ)
7539 and then not Is_RTE (Typ, RE_Bignum)
7540 then
7541 Error_Msg_N
7542 ("!static expression must have scalar or string type " &
7543 "(RM 4.9(2))", N);
7544 return;
7545 end if;
7546 end if;
7548 -- If we got through those checks, test particular node kind
7550 case Nkind (N) is
7552 -- Entity name
7554 when N_Expanded_Name
7555 | N_Identifier
7556 | N_Operator_Symbol
7558 E := Entity (N);
7560 if Is_Named_Number (E) then
7561 null;
7563 elsif Ekind (E) = E_Constant then
7565 -- One case we can give a better message is when we have a
7566 -- string literal created by concatenating an aggregate with
7567 -- an others expression.
7569 Entity_Case : declare
7570 CV : constant Node_Id := Constant_Value (E);
7571 CO : constant Node_Id := Original_Node (CV);
7573 function Is_Aggregate (N : Node_Id) return Boolean;
7574 -- See if node N came from an others aggregate, if so
7575 -- return True and set Error_Msg_Sloc to aggregate.
7577 ------------------
7578 -- Is_Aggregate --
7579 ------------------
7581 function Is_Aggregate (N : Node_Id) return Boolean is
7582 begin
7583 if Nkind (Original_Node (N)) = N_Aggregate then
7584 Error_Msg_Sloc := Sloc (Original_Node (N));
7585 return True;
7587 elsif Is_Entity_Name (N)
7588 and then Ekind (Entity (N)) = E_Constant
7589 and then
7590 Nkind (Original_Node (Constant_Value (Entity (N)))) =
7591 N_Aggregate
7592 then
7593 Error_Msg_Sloc :=
7594 Sloc (Original_Node (Constant_Value (Entity (N))));
7595 return True;
7597 else
7598 return False;
7599 end if;
7600 end Is_Aggregate;
7602 -- Start of processing for Entity_Case
7604 begin
7605 if Is_Aggregate (CV)
7606 or else (Nkind (CO) = N_Op_Concat
7607 and then (Is_Aggregate (Left_Opnd (CO))
7608 or else
7609 Is_Aggregate (Right_Opnd (CO))))
7610 then
7611 Error_Msg_N ("!aggregate (#) is never static", N);
7613 elsif No (CV) or else not Is_Static_Expression (CV) then
7614 Error_Msg_NE
7615 ("!& is not a static constant (RM 4.9(5))", N, E);
7616 end if;
7617 end Entity_Case;
7619 elsif Is_Type (E) then
7620 Error_Msg_NE
7621 ("!& is not a static subtype (RM 4.9(26))", N, E);
7623 elsif E /= Any_Id then
7624 Error_Msg_NE
7625 ("!& is not static constant or named number "
7626 & "(RM 4.9(5))", N, E);
7627 end if;
7629 -- Binary operator
7631 when N_Binary_Op
7632 | N_Membership_Test
7633 | N_Short_Circuit
7635 if Nkind (N) in N_Op_Shift then
7636 Error_Msg_N
7637 ("!shift functions are never static (RM 4.9(6,18))", N);
7638 else
7639 Why_Not_Static (Left_Opnd (N));
7640 Why_Not_Static (Right_Opnd (N));
7641 end if;
7643 -- Unary operator
7645 when N_Unary_Op =>
7646 Why_Not_Static (Right_Opnd (N));
7648 -- Attribute reference
7650 when N_Attribute_Reference =>
7651 Why_Not_Static_List (Expressions (N));
7653 E := Etype (Prefix (N));
7655 if E = Standard_Void_Type then
7656 return;
7657 end if;
7659 -- Special case non-scalar'Size since this is a common error
7661 if Attribute_Name (N) = Name_Size then
7662 Error_Msg_N
7663 ("!size attribute is only static for static scalar type "
7664 & "(RM 4.9(7,8))", N);
7666 -- Flag array cases
7668 elsif Is_Array_Type (E) then
7669 if Attribute_Name (N)
7670 not in Name_First | Name_Last | Name_Length
7671 then
7672 Error_Msg_N
7673 ("!static array attribute must be Length, First, or Last "
7674 & "(RM 4.9(8))", N);
7676 -- Since we know the expression is not-static (we already
7677 -- tested for this, must mean array is not static).
7679 else
7680 Error_Msg_N
7681 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
7682 end if;
7684 return;
7686 -- Special case generic types, since again this is a common source
7687 -- of confusion.
7689 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
7690 Error_Msg_N
7691 ("!attribute of generic type is never static "
7692 & "(RM 4.9(7,8))", N);
7694 elsif Is_OK_Static_Subtype (E) then
7695 null;
7697 elsif Is_Scalar_Type (E) then
7698 Error_Msg_N
7699 ("!prefix type for attribute is not static scalar subtype "
7700 & "(RM 4.9(7))", N);
7702 else
7703 Error_Msg_N
7704 ("!static attribute must apply to array/scalar type "
7705 & "(RM 4.9(7,8))", N);
7706 end if;
7708 -- String literal
7710 when N_String_Literal =>
7711 Error_Msg_N
7712 ("!subtype of string literal is non-static (RM 4.9(4))", N);
7714 -- Explicit dereference
7716 when N_Explicit_Dereference =>
7717 Error_Msg_N
7718 ("!explicit dereference is never static (RM 4.9)", N);
7720 -- Function call
7722 when N_Function_Call =>
7723 Why_Not_Static_List (Parameter_Associations (N));
7725 -- Complain about non-static function call unless we have Bignum
7726 -- which means that the underlying expression is really some
7727 -- scalar arithmetic operation.
7729 if not Is_RTE (Typ, RE_Bignum) then
7730 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
7731 end if;
7733 -- Parameter assocation (test actual parameter)
7735 when N_Parameter_Association =>
7736 Why_Not_Static (Explicit_Actual_Parameter (N));
7738 -- Indexed component
7740 when N_Indexed_Component =>
7741 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
7743 -- Procedure call
7745 when N_Procedure_Call_Statement =>
7746 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
7748 -- Qualified expression (test expression)
7750 when N_Qualified_Expression =>
7751 Why_Not_Static (Expression (N));
7753 -- Aggregate
7755 when N_Aggregate
7756 | N_Extension_Aggregate
7758 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
7760 -- Range
7762 when N_Range =>
7763 Why_Not_Static (Low_Bound (N));
7764 Why_Not_Static (High_Bound (N));
7766 -- Range constraint, test range expression
7768 when N_Range_Constraint =>
7769 Why_Not_Static (Range_Expression (N));
7771 -- Subtype indication, test constraint
7773 when N_Subtype_Indication =>
7774 Why_Not_Static (Constraint (N));
7776 -- Selected component
7778 when N_Selected_Component =>
7779 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
7781 -- Slice
7783 when N_Slice =>
7784 Error_Msg_N ("!slice is never static (RM 4.9)", N);
7786 when N_Type_Conversion =>
7787 Why_Not_Static (Expression (N));
7789 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
7790 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
7791 then
7792 Error_Msg_N
7793 ("!static conversion requires static scalar subtype result "
7794 & "(RM 4.9(9))", N);
7795 end if;
7797 -- Unchecked type conversion
7799 when N_Unchecked_Type_Conversion =>
7800 Error_Msg_N
7801 ("!unchecked type conversion is never static (RM 4.9)", N);
7803 -- All other cases, no reason to give
7805 when others =>
7806 null;
7807 end case;
7808 end Why_Not_Static;
7810 end Sem_Eval;