Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_eval.adb
blob24cd9e1aeeebb898034a81c0420d69a1f17fa560
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-2023, 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 then
438 if Static_Failure_Is_Error then
439 Error_Msg_NE
440 ("static expression fails static predicate check on &",
441 Expr, Typ);
443 else
444 Error_Msg_NE
445 ("??static expression fails static predicate check on &",
446 Expr, Typ);
447 Error_Msg_N
448 ("\??expression is no longer considered static", Expr);
450 Set_Is_Static_Expression (Expr, False);
451 end if;
453 -- In all other cases, this is just a warning that a test will fail.
454 -- It does not matter if the expression is static or not, or if the
455 -- predicate comes from a dynamic predicate aspect or not.
457 else
458 Error_Msg_NE
459 ("??expression fails predicate check on &", Expr, Typ);
461 -- Force a check here, which is potentially a redundant check, but
462 -- this ensures a check will be done in cases where the expression
463 -- is folded, and since this is definitely a failure, extra checks
464 -- are OK.
466 if Predicate_Enabled (Typ) then
467 Insert_Action (Expr,
468 Make_Predicate_Check
469 (Typ, Duplicate_Subexpr (Expr)), Suppress => All_Checks);
470 end if;
471 end if;
472 end Check_Expression_Against_Static_Predicate;
474 ------------------------------
475 -- Check_Non_Static_Context --
476 ------------------------------
478 procedure Check_Non_Static_Context (N : Node_Id) is
479 T : constant Entity_Id := Etype (N);
480 Checks_On : constant Boolean :=
481 not Index_Checks_Suppressed (T)
482 and not Range_Checks_Suppressed (T);
484 begin
485 -- Ignore cases of non-scalar types, error types, or universal real
486 -- types that have no usable bounds.
488 if T = Any_Type
489 or else not Is_Scalar_Type (T)
490 or else T = Universal_Fixed
491 or else T = Universal_Real
492 then
493 return;
494 end if;
496 -- At this stage we have a scalar type. If we have an expression that
497 -- raises CE, then we already issued a warning or error msg so there is
498 -- nothing more to be done in this routine.
500 if Raises_Constraint_Error (N) then
501 return;
502 end if;
504 -- Now we have a scalar type which is not marked as raising a constraint
505 -- error exception. The main purpose of this routine is to deal with
506 -- static expressions appearing in a non-static context. That means
507 -- that if we do not have a static expression then there is not much
508 -- to do. The one case that we deal with here is that if we have a
509 -- floating-point value that is out of range, then we post a warning
510 -- that an infinity will result.
512 if not Is_Static_Expression (N) then
513 if Is_Floating_Point_Type (T) then
514 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
515 Error_Msg_N
516 ("??float value out of range, infinity will be generated", N);
518 -- The literal may be the result of constant-folding of a non-
519 -- static subexpression of a larger expression (e.g. a conversion
520 -- of a non-static variable whose value happens to be known). At
521 -- this point we must reduce the value of the subexpression to a
522 -- machine number (RM 4.9 (38/2)).
524 elsif Nkind (N) = N_Real_Literal
525 and then Nkind (Parent (N)) in N_Subexpr
526 then
527 Rewrite (N, New_Copy (N));
528 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
529 Set_Is_Machine_Number (N);
530 end if;
531 end if;
533 return;
534 end if;
536 -- Here we have the case of outer level static expression of scalar
537 -- type, where the processing of this procedure is needed.
539 -- For real types, this is where we convert the value to a machine
540 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
541 -- need to do this if the parent is a constant declaration, since in
542 -- other cases, gigi should do the necessary conversion correctly, but
543 -- experimentation shows that this is not the case on all machines, in
544 -- particular if we do not convert all literals to machine values in
545 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
546 -- and SGI/Irix.
548 -- This conversion is always done by GNATprove on real literals in
549 -- non-static expressions, by calling Check_Non_Static_Context from
550 -- gnat2why, as GNATprove cannot do the conversion later contrary
551 -- to gigi. The frontend computes the information about which
552 -- expressions are static, which is used by gnat2why to call
553 -- Check_Non_Static_Context on exactly those real literals that are
554 -- not subexpressions of static expressions.
556 if Nkind (N) = N_Real_Literal
557 and then not Is_Machine_Number (N)
558 and then not Is_Generic_Type (Etype (N))
559 and then Etype (N) /= Universal_Real
560 then
561 -- Check that value is in bounds before converting to machine
562 -- number, so as not to lose case where value overflows in the
563 -- least significant bit or less. See B490001.
565 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
566 Out_Of_Range (N);
567 return;
568 end if;
570 -- Note: we have to copy the node, to avoid problems with conformance
571 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
573 Rewrite (N, New_Copy (N));
575 if not Is_Floating_Point_Type (T) then
576 Set_Realval
577 (N, Corresponding_Integer_Value (N) * Small_Value (T));
579 elsif not UR_Is_Zero (Realval (N)) then
580 Set_Realval (N, Machine_Number (Base_Type (T), Realval (N), N));
581 Set_Is_Machine_Number (N);
582 end if;
584 end if;
586 -- Check for out of range universal integer. This is a non-static
587 -- context, so the integer value must be in range of the runtime
588 -- representation of universal integers.
590 -- We do this only within an expression, because that is the only
591 -- case in which non-static universal integer values can occur, and
592 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
593 -- called in contexts like the expression of a number declaration where
594 -- we certainly want to allow out of range values.
596 -- We inhibit the warning when expansion is disabled, because the
597 -- preanalysis of a range of a 64-bit modular type may appear to
598 -- violate the constraint on non-static Universal_Integer. If there
599 -- is a true overflow it will be diagnosed during full analysis.
601 if Etype (N) = Universal_Integer
602 and then Nkind (N) = N_Integer_Literal
603 and then Nkind (Parent (N)) in N_Subexpr
604 and then Expander_Active
605 and then
606 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
607 or else
608 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
609 then
610 Apply_Compile_Time_Constraint_Error
611 (N, "non-static universal integer value out of range<<",
612 CE_Range_Check_Failed);
614 -- Check out of range of base type
616 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
617 Out_Of_Range (N);
619 -- Give a warning or error on the value outside the subtype. A warning
620 -- is omitted if the expression appears in a range that could be null
621 -- (warnings are handled elsewhere for this case).
623 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
624 if Is_In_Range (N, T, Assume_Valid => True) then
625 null;
627 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
628 -- Ignore out of range values for System.Priority in CodePeer
629 -- mode since the actual target compiler may provide a wider
630 -- range.
632 if CodePeer_Mode and then Is_RTE (T, RE_Priority) then
633 Set_Do_Range_Check (N, False);
635 -- Determine if the out-of-range violation constitutes a warning
636 -- or an error based on context, according to RM 4.9 (34/3).
638 elsif Nkind (Original_Node (N)) in
639 N_Type_Conversion | N_Qualified_Expression
640 and then Comes_From_Source (Original_Node (N))
641 then
642 Apply_Compile_Time_Constraint_Error
643 (N, "value not in range of}", CE_Range_Check_Failed);
644 else
645 Apply_Compile_Time_Constraint_Error
646 (N, "value not in range of}<<", CE_Range_Check_Failed);
647 end if;
649 elsif Checks_On then
650 Enable_Range_Check (N);
652 else
653 Set_Do_Range_Check (N, False);
654 end if;
655 end if;
656 end Check_Non_Static_Context;
658 -------------------------------------------
659 -- Check_Non_Static_Context_For_Overflow --
660 -------------------------------------------
662 procedure Check_Non_Static_Context_For_Overflow
663 (N : Node_Id;
664 Stat : Boolean;
665 Result : Uint)
667 begin
668 if (not Stat or else In_Inlined_Body)
669 and then Is_Signed_Integer_Type (Etype (N))
670 then
671 declare
672 BT : constant Entity_Id := Base_Type (Etype (N));
673 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
674 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
675 begin
676 if Result < Lo or else Result > Hi then
677 Apply_Compile_Time_Constraint_Error
678 (N, "value not in range of }??",
679 CE_Overflow_Check_Failed,
680 Ent => BT);
681 end if;
682 end;
683 end if;
684 end Check_Non_Static_Context_For_Overflow;
686 ---------------------------------
687 -- Check_String_Literal_Length --
688 ---------------------------------
690 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
691 begin
692 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
693 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
694 then
695 Apply_Compile_Time_Constraint_Error
696 (N, "string length wrong for}??",
697 CE_Length_Check_Failed,
698 Ent => Ttype,
699 Typ => Ttype);
700 end if;
701 end if;
702 end Check_String_Literal_Length;
704 --------------------------------------------
705 -- Checking_Potentially_Static_Expression --
706 --------------------------------------------
708 function Checking_Potentially_Static_Expression return Boolean is
709 begin
710 return Checking_For_Potentially_Static_Expression;
711 end Checking_Potentially_Static_Expression;
713 --------------------
714 -- Choice_Matches --
715 --------------------
717 function Choice_Matches
718 (Expr : Node_Id;
719 Choice : Node_Id) return Match_Result
721 Etyp : constant Entity_Id := Etype (Expr);
722 Val : Uint;
723 ValR : Ureal;
724 ValS : Node_Id;
726 begin
727 pragma Assert (Compile_Time_Known_Value (Expr));
728 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
730 if not Is_OK_Static_Choice (Choice) then
731 Set_Raises_Constraint_Error (Choice);
732 return Non_Static;
734 -- When the choice denotes a subtype with a static predictate, check the
735 -- expression against the predicate values. Different procedures apply
736 -- to discrete and non-discrete types.
738 elsif (Nkind (Choice) = N_Subtype_Indication
739 or else (Is_Entity_Name (Choice)
740 and then Is_Type (Entity (Choice))))
741 and then Has_Predicates (Etype (Choice))
742 and then Has_Static_Predicate (Etype (Choice))
743 then
744 if Is_Discrete_Type (Etype (Choice)) then
745 return
746 Choices_Match
747 (Expr, Static_Discrete_Predicate (Etype (Choice)));
749 elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice))
750 then
751 return Match;
753 else
754 return No_Match;
755 end if;
757 -- Discrete type case only
759 elsif Is_Discrete_Type (Etyp) then
760 Val := Expr_Value (Expr);
762 if Nkind (Choice) = N_Range then
763 if Val >= Expr_Value (Low_Bound (Choice))
764 and then
765 Val <= Expr_Value (High_Bound (Choice))
766 then
767 return Match;
768 else
769 return No_Match;
770 end if;
772 elsif Nkind (Choice) = N_Subtype_Indication
773 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
774 then
775 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
776 and then
777 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
778 then
779 return Match;
780 else
781 return No_Match;
782 end if;
784 elsif Nkind (Choice) = N_Others_Choice then
785 return Match;
787 else
788 if Val = Expr_Value (Choice) then
789 return Match;
790 else
791 return No_Match;
792 end if;
793 end if;
795 -- Real type case
797 elsif Is_Real_Type (Etyp) then
798 ValR := Expr_Value_R (Expr);
800 if Nkind (Choice) = N_Range then
801 if ValR >= Expr_Value_R (Low_Bound (Choice))
802 and then
803 ValR <= Expr_Value_R (High_Bound (Choice))
804 then
805 return Match;
806 else
807 return No_Match;
808 end if;
810 elsif Nkind (Choice) = N_Subtype_Indication
811 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
812 then
813 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
814 and then
815 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
816 then
817 return Match;
818 else
819 return No_Match;
820 end if;
822 else
823 if ValR = Expr_Value_R (Choice) then
824 return Match;
825 else
826 return No_Match;
827 end if;
828 end if;
830 -- String type cases
832 else
833 pragma Assert (Is_String_Type (Etyp));
834 ValS := Expr_Value_S (Expr);
836 if Nkind (Choice) = N_Subtype_Indication
837 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
838 then
839 if not Is_Constrained (Etype (Choice)) then
840 return Match;
842 else
843 declare
844 Typlen : constant Uint :=
845 String_Type_Len (Etype (Choice));
846 Strlen : constant Uint :=
847 UI_From_Int (String_Length (Strval (ValS)));
848 begin
849 if Typlen = Strlen then
850 return Match;
851 else
852 return No_Match;
853 end if;
854 end;
855 end if;
857 else
858 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
859 then
860 return Match;
861 else
862 return No_Match;
863 end if;
864 end if;
865 end if;
866 end Choice_Matches;
868 -------------------
869 -- Choices_Match --
870 -------------------
872 function Choices_Match
873 (Expr : Node_Id;
874 Choices : List_Id) return Match_Result
876 Choice : Node_Id;
877 Result : Match_Result;
879 begin
880 Choice := First (Choices);
881 while Present (Choice) loop
882 Result := Choice_Matches (Expr, Choice);
884 if Result /= No_Match then
885 return Result;
886 end if;
888 Next (Choice);
889 end loop;
891 return No_Match;
892 end Choices_Match;
894 --------------------------
895 -- Compile_Time_Compare --
896 --------------------------
898 function Compile_Time_Compare
899 (L, R : Node_Id;
900 Assume_Valid : Boolean) return Compare_Result
902 Discard : aliased Uint;
903 begin
904 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
905 end Compile_Time_Compare;
907 function Compile_Time_Compare
908 (L, R : Node_Id;
909 Diff : access Uint;
910 Assume_Valid : Boolean;
911 Rec : Boolean := False) return Compare_Result
913 Ltyp : Entity_Id := Etype (L);
914 Rtyp : Entity_Id := Etype (R);
916 Discard : aliased Uint;
918 procedure Compare_Decompose
919 (N : Node_Id;
920 R : out Node_Id;
921 V : out Uint);
922 -- This procedure decomposes the node N into an expression node and a
923 -- signed offset, so that the value of N is equal to the value of R plus
924 -- the value V (which may be negative). If no such decomposition is
925 -- possible, then on return R is a copy of N, and V is set to zero.
927 function Compare_Fixup (N : Node_Id) return Node_Id;
928 -- This function deals with replacing 'Last and 'First references with
929 -- their corresponding type bounds, which we then can compare. The
930 -- argument is the original node, the result is the identity, unless we
931 -- have a 'Last/'First reference in which case the value returned is the
932 -- appropriate type bound.
934 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
935 -- Even if the context does not assume that values are valid, some
936 -- simple cases can be recognized.
938 function Is_Same_Value (L, R : Node_Id) return Boolean;
939 -- Returns True iff L and R represent expressions that definitely have
940 -- identical (but not necessarily compile-time-known) values Indeed the
941 -- caller is expected to have already dealt with the cases of compile
942 -- time known values, so these are not tested here.
944 -----------------------
945 -- Compare_Decompose --
946 -----------------------
948 procedure Compare_Decompose
949 (N : Node_Id;
950 R : out Node_Id;
951 V : out Uint)
953 begin
954 if Nkind (N) = N_Op_Add
955 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
956 then
957 R := Left_Opnd (N);
958 V := Intval (Right_Opnd (N));
959 return;
961 elsif Nkind (N) = N_Op_Subtract
962 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
963 then
964 R := Left_Opnd (N);
965 V := UI_Negate (Intval (Right_Opnd (N)));
966 return;
968 elsif Nkind (N) = N_Attribute_Reference then
969 if Attribute_Name (N) = Name_Succ then
970 R := First (Expressions (N));
971 V := Uint_1;
972 return;
974 elsif Attribute_Name (N) = Name_Pred then
975 R := First (Expressions (N));
976 V := Uint_Minus_1;
977 return;
978 end if;
979 end if;
981 R := N;
982 V := Uint_0;
983 end Compare_Decompose;
985 -------------------
986 -- Compare_Fixup --
987 -------------------
989 function Compare_Fixup (N : Node_Id) return Node_Id is
990 Indx : Node_Id;
991 Xtyp : Entity_Id;
992 Subs : Nat;
994 begin
995 -- Fixup only required for First/Last attribute reference
997 if Nkind (N) = N_Attribute_Reference
998 and then Attribute_Name (N) in Name_First | Name_Last
999 then
1000 Xtyp := Etype (Prefix (N));
1002 -- If we have no type, then just abandon the attempt to do
1003 -- a fixup, this is probably the result of some other error.
1005 if No (Xtyp) then
1006 return N;
1007 end if;
1009 -- Dereference an access type
1011 if Is_Access_Type (Xtyp) then
1012 Xtyp := Designated_Type (Xtyp);
1013 end if;
1015 -- If we don't have an array type at this stage, something is
1016 -- peculiar, e.g. another error, and we abandon the attempt at
1017 -- a fixup.
1019 if not Is_Array_Type (Xtyp) then
1020 return N;
1021 end if;
1023 -- Ignore unconstrained array, since bounds are not meaningful
1025 if not Is_Constrained (Xtyp) then
1026 return N;
1027 end if;
1029 if Ekind (Xtyp) = E_String_Literal_Subtype then
1030 if Attribute_Name (N) = Name_First then
1031 return String_Literal_Low_Bound (Xtyp);
1032 else
1033 return
1034 Make_Integer_Literal (Sloc (N),
1035 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
1036 String_Literal_Length (Xtyp));
1037 end if;
1038 end if;
1040 -- Find correct index type
1042 Indx := First_Index (Xtyp);
1044 if Present (Expressions (N)) then
1045 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
1047 for J in 2 .. Subs loop
1048 Next_Index (Indx);
1049 end loop;
1050 end if;
1052 Xtyp := Etype (Indx);
1054 if Attribute_Name (N) = Name_First then
1055 return Type_Low_Bound (Xtyp);
1056 else
1057 return Type_High_Bound (Xtyp);
1058 end if;
1059 end if;
1061 return N;
1062 end Compare_Fixup;
1064 ----------------------------
1065 -- Is_Known_Valid_Operand --
1066 ----------------------------
1068 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
1069 begin
1070 return (Is_Entity_Name (Opnd)
1071 and then
1072 (Is_Known_Valid (Entity (Opnd))
1073 or else Ekind (Entity (Opnd)) = E_In_Parameter
1074 or else
1075 (Is_Object (Entity (Opnd))
1076 and then Present (Current_Value (Entity (Opnd))))))
1077 or else Is_OK_Static_Expression (Opnd);
1078 end Is_Known_Valid_Operand;
1080 -------------------
1081 -- Is_Same_Value --
1082 -------------------
1084 function Is_Same_Value (L, R : Node_Id) return Boolean is
1085 Lf : constant Node_Id := Compare_Fixup (L);
1086 Rf : constant Node_Id := Compare_Fixup (R);
1088 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean;
1089 -- An attribute reference to Loop_Entry may have been rewritten into
1090 -- its prefix as a way to avoid generating a constant for that
1091 -- attribute when the corresponding pragma is ignored. These nodes
1092 -- should be ignored when deciding if they can be equal to one
1093 -- another.
1095 function Is_Same_Subscript (L, R : List_Id) return Boolean;
1096 -- L, R are the Expressions values from two attribute nodes for First
1097 -- or Last attributes. Either may be set to No_List if no expressions
1098 -- are present (indicating subscript 1). The result is True if both
1099 -- expressions represent the same subscript (note one case is where
1100 -- one subscript is missing and the other is explicitly set to 1).
1102 -----------------------------
1103 -- Is_Rewritten_Loop_Entry --
1104 -----------------------------
1106 function Is_Rewritten_Loop_Entry (N : Node_Id) return Boolean is
1107 Orig_N : constant Node_Id := Original_Node (N);
1108 begin
1109 return Orig_N /= N
1110 and then Nkind (Orig_N) = N_Attribute_Reference
1111 and then Get_Attribute_Id (Attribute_Name (Orig_N)) =
1112 Attribute_Loop_Entry;
1113 end Is_Rewritten_Loop_Entry;
1115 -----------------------
1116 -- Is_Same_Subscript --
1117 -----------------------
1119 function Is_Same_Subscript (L, R : List_Id) return Boolean is
1120 begin
1121 if L = No_List then
1122 if R = No_List then
1123 return True;
1124 else
1125 return Expr_Value (First (R)) = Uint_1;
1126 end if;
1128 else
1129 if R = No_List then
1130 return Expr_Value (First (L)) = Uint_1;
1131 else
1132 return Expr_Value (First (L)) = Expr_Value (First (R));
1133 end if;
1134 end if;
1135 end Is_Same_Subscript;
1137 -- Start of processing for Is_Same_Value
1139 begin
1140 -- Loop_Entry nodes rewritten into their prefix inside ignored
1141 -- pragmas should never lead to a decision of equality.
1143 if Is_Rewritten_Loop_Entry (Lf)
1144 or else Is_Rewritten_Loop_Entry (Rf)
1145 then
1146 return False;
1148 -- Values are the same if they refer to the same entity and the
1149 -- entity is nonvolatile.
1151 elsif Nkind (Lf) in N_Identifier | N_Expanded_Name
1152 and then Nkind (Rf) in N_Identifier | N_Expanded_Name
1153 and then Entity (Lf) = Entity (Rf)
1155 -- If the entity is a discriminant, the two expressions may be
1156 -- bounds of components of objects of the same discriminated type.
1157 -- The values of the discriminants are not static, and therefore
1158 -- the result is unknown.
1160 and then Ekind (Entity (Lf)) /= E_Discriminant
1161 and then Present (Entity (Lf))
1163 -- This does not however apply to Float types, since we may have
1164 -- two NaN values and they should never compare equal.
1166 and then not Is_Floating_Point_Type (Etype (L))
1167 and then not Is_Volatile_Reference (L)
1168 and then not Is_Volatile_Reference (R)
1169 then
1170 return True;
1172 -- Or if they are compile-time-known and identical
1174 elsif Compile_Time_Known_Value (Lf)
1175 and then
1176 Compile_Time_Known_Value (Rf)
1177 and then Expr_Value (Lf) = Expr_Value (Rf)
1178 then
1179 return True;
1181 -- False if Nkind of the two nodes is different for remaining cases
1183 elsif Nkind (Lf) /= Nkind (Rf) then
1184 return False;
1186 -- True if both 'First or 'Last values applying to the same entity
1187 -- (first and last don't change even if value does). Note that we
1188 -- need this even with the calls to Compare_Fixup, to handle the
1189 -- case of unconstrained array attributes where Compare_Fixup
1190 -- cannot find useful bounds.
1192 elsif Nkind (Lf) = N_Attribute_Reference
1193 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1194 and then Attribute_Name (Lf) in Name_First | Name_Last
1195 and then Nkind (Prefix (Lf)) in N_Identifier | N_Expanded_Name
1196 and then Nkind (Prefix (Rf)) in N_Identifier | N_Expanded_Name
1197 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1198 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1199 then
1200 return True;
1202 -- True if the same selected component from the same record
1204 elsif Nkind (Lf) = N_Selected_Component
1205 and then Selector_Name (Lf) = Selector_Name (Rf)
1206 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1207 then
1208 return True;
1210 -- True if the same unary operator applied to the same operand
1212 elsif Nkind (Lf) in N_Unary_Op
1213 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1214 then
1215 return True;
1217 -- True if the same binary operator applied to the same operands
1219 elsif Nkind (Lf) in N_Binary_Op
1220 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1221 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1222 then
1223 return True;
1225 -- All other cases, we can't tell, so return False
1227 else
1228 return False;
1229 end if;
1230 end Is_Same_Value;
1232 -- Start of processing for Compile_Time_Compare
1234 begin
1235 Diff.all := No_Uint;
1237 -- In preanalysis mode, always return Unknown unless the expression
1238 -- is static. It is too early to be thinking we know the result of a
1239 -- comparison, save that judgment for the full analysis. This is
1240 -- particularly important in the case of pre and postconditions, which
1241 -- otherwise can be prematurely collapsed into having True or False
1242 -- conditions when this is inappropriate.
1244 if not (Full_Analysis
1245 or else (Is_OK_Static_Expression (L)
1246 and then
1247 Is_OK_Static_Expression (R)))
1248 then
1249 return Unknown;
1250 end if;
1252 -- If either operand could raise Constraint_Error, then we cannot
1253 -- know the result at compile time (since CE may be raised).
1255 if not (Cannot_Raise_Constraint_Error (L)
1256 and then
1257 Cannot_Raise_Constraint_Error (R))
1258 then
1259 return Unknown;
1260 end if;
1262 -- Identical operands are most certainly equal
1264 if L = R then
1265 return EQ;
1266 end if;
1268 -- If expressions have no types, then do not attempt to determine if
1269 -- they are the same, since something funny is going on. One case in
1270 -- which this happens is during generic template analysis, when bounds
1271 -- are not fully analyzed.
1273 if No (Ltyp) or else No (Rtyp) then
1274 return Unknown;
1275 end if;
1277 -- These get reset to the base type for the case of entities where
1278 -- Is_Known_Valid is not set. This takes care of handling possible
1279 -- invalid representations using the value of the base type, in
1280 -- accordance with RM 13.9.1(10).
1282 Ltyp := Underlying_Type (Ltyp);
1283 Rtyp := Underlying_Type (Rtyp);
1285 -- Same rationale as above, but for Underlying_Type instead of Etype
1287 if No (Ltyp) or else No (Rtyp) then
1288 return Unknown;
1289 end if;
1291 -- We do not attempt comparisons for packed arrays represented as
1292 -- modular types, where the semantics of comparison is quite different.
1294 if Is_Packed_Array_Impl_Type (Ltyp)
1295 and then Is_Modular_Integer_Type (Ltyp)
1296 then
1297 return Unknown;
1299 -- For access types, the only time we know the result at compile time
1300 -- (apart from identical operands, which we handled already) is if we
1301 -- know one operand is null and the other is not, or both operands are
1302 -- known null.
1304 elsif Is_Access_Type (Ltyp) then
1305 if Known_Null (L) then
1306 if Known_Null (R) then
1307 return EQ;
1308 elsif Known_Non_Null (R) then
1309 return NE;
1310 else
1311 return Unknown;
1312 end if;
1314 elsif Known_Non_Null (L) and then Known_Null (R) then
1315 return NE;
1317 else
1318 return Unknown;
1319 end if;
1321 -- Case where comparison involves two compile-time-known values
1323 elsif Compile_Time_Known_Value (L)
1324 and then
1325 Compile_Time_Known_Value (R)
1326 then
1327 -- For the floating-point case, we have to be a little careful, since
1328 -- at compile time we are dealing with universal exact values, but at
1329 -- runtime, these will be in non-exact target form. That's why the
1330 -- returned results are LE and GE below instead of LT and GT.
1332 if Is_Floating_Point_Type (Ltyp)
1333 or else
1334 Is_Floating_Point_Type (Rtyp)
1335 then
1336 declare
1337 Lo : constant Ureal := Expr_Value_R (L);
1338 Hi : constant Ureal := Expr_Value_R (R);
1339 begin
1340 if Lo < Hi then
1341 return LE;
1342 elsif Lo = Hi then
1343 return EQ;
1344 else
1345 return GE;
1346 end if;
1347 end;
1349 -- For string types, we have two string literals and we proceed to
1350 -- compare them using the Ada style dictionary string comparison.
1352 elsif not Is_Scalar_Type (Ltyp) then
1353 declare
1354 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1355 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1356 Llen : constant Nat := String_Length (Lstring);
1357 Rlen : constant Nat := String_Length (Rstring);
1359 begin
1360 for J in 1 .. Nat'Min (Llen, Rlen) loop
1361 declare
1362 LC : constant Char_Code := Get_String_Char (Lstring, J);
1363 RC : constant Char_Code := Get_String_Char (Rstring, J);
1364 begin
1365 if LC < RC then
1366 return LT;
1367 elsif LC > RC then
1368 return GT;
1369 end if;
1370 end;
1371 end loop;
1373 if Llen < Rlen then
1374 return LT;
1375 elsif Llen > Rlen then
1376 return GT;
1377 else
1378 return EQ;
1379 end if;
1380 end;
1382 -- For remaining scalar cases we know exactly (note that this does
1383 -- include the fixed-point case, where we know the run time integer
1384 -- values now).
1386 else
1387 declare
1388 Lo : constant Uint := Expr_Value (L);
1389 Hi : constant Uint := Expr_Value (R);
1390 begin
1391 if Lo < Hi then
1392 Diff.all := Hi - Lo;
1393 return LT;
1394 elsif Lo = Hi then
1395 return EQ;
1396 else
1397 Diff.all := Lo - Hi;
1398 return GT;
1399 end if;
1400 end;
1401 end if;
1403 -- Cases where at least one operand is not known at compile time
1405 else
1406 -- Remaining checks apply only for discrete types
1408 if not Is_Discrete_Type (Ltyp)
1409 or else
1410 not Is_Discrete_Type (Rtyp)
1411 then
1412 return Unknown;
1413 end if;
1415 -- Defend against generic types, or actually any expressions that
1416 -- contain a reference to a generic type from within a generic
1417 -- template. We don't want to do any range analysis of such
1418 -- expressions for two reasons. First, the bounds of a generic type
1419 -- itself are junk and cannot be used for any kind of analysis.
1420 -- Second, we may have a case where the range at run time is indeed
1421 -- known, but we don't want to do compile time analysis in the
1422 -- template based on that range since in an instance the value may be
1423 -- static, and able to be elaborated without reference to the bounds
1424 -- of types involved. As an example, consider:
1426 -- (F'Pos (F'Last) + 1) > Integer'Last
1428 -- The expression on the left side of > is Universal_Integer and thus
1429 -- acquires the type Integer for evaluation at run time, and at run
1430 -- time it is true that this condition is always False, but within
1431 -- an instance F may be a type with a static range greater than the
1432 -- range of Integer, and the expression statically evaluates to True.
1434 if References_Generic_Formal_Type (L)
1435 or else
1436 References_Generic_Formal_Type (R)
1437 then
1438 return Unknown;
1439 end if;
1441 -- Replace types by base types for the case of values which are not
1442 -- known to have valid representations. This takes care of properly
1443 -- dealing with invalid representations.
1445 if not Assume_Valid then
1446 if not (Is_Entity_Name (L)
1447 and then (Is_Known_Valid (Entity (L))
1448 or else Assume_No_Invalid_Values))
1449 then
1450 Ltyp := Underlying_Type (Base_Type (Ltyp));
1451 end if;
1453 if not (Is_Entity_Name (R)
1454 and then (Is_Known_Valid (Entity (R))
1455 or else Assume_No_Invalid_Values))
1456 then
1457 Rtyp := Underlying_Type (Base_Type (Rtyp));
1458 end if;
1459 end if;
1461 -- First attempt is to decompose the expressions to extract a
1462 -- constant offset resulting from the use of any of the forms:
1464 -- expr + literal
1465 -- expr - literal
1466 -- typ'Succ (expr)
1467 -- typ'Pred (expr)
1469 -- Then we see if the two expressions are the same value, and if so
1470 -- the result is obtained by comparing the offsets.
1472 -- Note: the reason we do this test first is that it returns only
1473 -- decisive results (with diff set), where other tests, like the
1474 -- range test, may not be as so decisive. Consider for example
1475 -- J .. J + 1. This code can conclude LT with a difference of 1,
1476 -- even if the range of J is not known.
1478 declare
1479 Lnode : Node_Id;
1480 Loffs : Uint;
1481 Rnode : Node_Id;
1482 Roffs : Uint;
1484 begin
1485 Compare_Decompose (L, Lnode, Loffs);
1486 Compare_Decompose (R, Rnode, Roffs);
1488 if Is_Same_Value (Lnode, Rnode) then
1489 if Loffs = Roffs then
1490 return EQ;
1491 end if;
1493 -- When the offsets are not equal, we can go farther only if
1494 -- the types are not modular (e.g. X < X + 1 is False if X is
1495 -- the largest number).
1497 if not Is_Modular_Integer_Type (Ltyp)
1498 and then not Is_Modular_Integer_Type (Rtyp)
1499 then
1500 if Loffs < Roffs then
1501 Diff.all := Roffs - Loffs;
1502 return LT;
1503 else
1504 Diff.all := Loffs - Roffs;
1505 return GT;
1506 end if;
1507 end if;
1508 end if;
1509 end;
1511 -- Next, try range analysis and see if operand ranges are disjoint
1513 declare
1514 LOK, ROK : Boolean;
1515 LLo, LHi : Uint;
1516 RLo, RHi : Uint;
1518 Single : Boolean;
1519 -- True if each range is a single point
1521 begin
1522 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1523 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1525 if LOK and ROK then
1526 Single := (LLo = LHi) and then (RLo = RHi);
1528 if LHi < RLo then
1529 if Single and Assume_Valid then
1530 Diff.all := RLo - LLo;
1531 end if;
1533 return LT;
1535 elsif RHi < LLo then
1536 if Single and Assume_Valid then
1537 Diff.all := LLo - RLo;
1538 end if;
1540 return GT;
1542 elsif Single and then LLo = RLo then
1544 -- If the range includes a single literal and we can assume
1545 -- validity then the result is known even if an operand is
1546 -- not static.
1548 if Assume_Valid then
1549 return EQ;
1550 else
1551 return Unknown;
1552 end if;
1554 elsif LHi = RLo then
1555 return LE;
1557 elsif RHi = LLo then
1558 return GE;
1560 elsif not Is_Known_Valid_Operand (L)
1561 and then not Assume_Valid
1562 then
1563 if Is_Same_Value (L, R) then
1564 return EQ;
1565 else
1566 return Unknown;
1567 end if;
1568 end if;
1570 -- If the range of either operand cannot be determined, nothing
1571 -- further can be inferred.
1573 else
1574 return Unknown;
1575 end if;
1576 end;
1578 -- Here is where we check for comparisons against maximum bounds of
1579 -- types, where we know that no value can be outside the bounds of
1580 -- the subtype. Note that this routine is allowed to assume that all
1581 -- expressions are within their subtype bounds. Callers wishing to
1582 -- deal with possibly invalid values must in any case take special
1583 -- steps (e.g. conversions to larger types) to avoid this kind of
1584 -- optimization, which is always considered to be valid. We do not
1585 -- attempt this optimization with generic types, since the type
1586 -- bounds may not be meaningful in this case.
1588 -- We are in danger of an infinite recursion here. It does not seem
1589 -- useful to go more than one level deep, so the parameter Rec is
1590 -- used to protect ourselves against this infinite recursion.
1592 if not Rec then
1594 -- See if we can get a decisive check against one operand and a
1595 -- bound of the other operand (four possible tests here). Note
1596 -- that we avoid testing junk bounds of a generic type.
1598 if not Is_Generic_Type (Rtyp) then
1599 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1600 Discard'Access,
1601 Assume_Valid, Rec => True)
1603 when LT => return LT;
1604 when LE => return LE;
1605 when EQ => return LE;
1606 when others => null;
1607 end case;
1609 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1610 Discard'Access,
1611 Assume_Valid, Rec => True)
1613 when GT => return GT;
1614 when GE => return GE;
1615 when EQ => return GE;
1616 when others => null;
1617 end case;
1618 end if;
1620 if not Is_Generic_Type (Ltyp) then
1621 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1622 Discard'Access,
1623 Assume_Valid, Rec => True)
1625 when GT => return GT;
1626 when GE => return GE;
1627 when EQ => return GE;
1628 when others => null;
1629 end case;
1631 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1632 Discard'Access,
1633 Assume_Valid, Rec => True)
1635 when LT => return LT;
1636 when LE => return LE;
1637 when EQ => return LE;
1638 when others => null;
1639 end case;
1640 end if;
1641 end if;
1643 -- Next attempt is to see if we have an entity compared with a
1644 -- compile-time-known value, where there is a current value
1645 -- conditional for the entity which can tell us the result.
1647 declare
1648 Var : Node_Id;
1649 -- Entity variable (left operand)
1651 Val : Uint;
1652 -- Value (right operand)
1654 Inv : Boolean;
1655 -- If False, we have reversed the operands
1657 Op : Node_Kind;
1658 -- Comparison operator kind from Get_Current_Value_Condition call
1660 Opn : Node_Id;
1661 -- Value from Get_Current_Value_Condition call
1663 Opv : Uint;
1664 -- Value of Opn
1666 Result : Compare_Result;
1667 -- Known result before inversion
1669 begin
1670 if Is_Entity_Name (L)
1671 and then Compile_Time_Known_Value (R)
1672 then
1673 Var := L;
1674 Val := Expr_Value (R);
1675 Inv := False;
1677 elsif Is_Entity_Name (R)
1678 and then Compile_Time_Known_Value (L)
1679 then
1680 Var := R;
1681 Val := Expr_Value (L);
1682 Inv := True;
1684 -- That was the last chance at finding a compile time result
1686 else
1687 return Unknown;
1688 end if;
1690 Get_Current_Value_Condition (Var, Op, Opn);
1692 -- That was the last chance, so if we got nothing return
1694 if No (Opn) then
1695 return Unknown;
1696 end if;
1698 Opv := Expr_Value (Opn);
1700 -- We got a comparison, so we might have something interesting
1702 -- Convert LE to LT and GE to GT, just so we have fewer cases
1704 if Op = N_Op_Le then
1705 Op := N_Op_Lt;
1706 Opv := Opv + 1;
1708 elsif Op = N_Op_Ge then
1709 Op := N_Op_Gt;
1710 Opv := Opv - 1;
1711 end if;
1713 -- Deal with equality case
1715 if Op = N_Op_Eq then
1716 if Val = Opv then
1717 Result := EQ;
1718 elsif Opv < Val then
1719 Result := LT;
1720 else
1721 Result := GT;
1722 end if;
1724 -- Deal with inequality case
1726 elsif Op = N_Op_Ne then
1727 if Val = Opv then
1728 Result := NE;
1729 else
1730 return Unknown;
1731 end if;
1733 -- Deal with greater than case
1735 elsif Op = N_Op_Gt then
1736 if Opv >= Val then
1737 Result := GT;
1738 elsif Opv = Val - 1 then
1739 Result := GE;
1740 else
1741 return Unknown;
1742 end if;
1744 -- Deal with less than case
1746 else pragma Assert (Op = N_Op_Lt);
1747 if Opv <= Val then
1748 Result := LT;
1749 elsif Opv = Val + 1 then
1750 Result := LE;
1751 else
1752 return Unknown;
1753 end if;
1754 end if;
1756 -- Deal with inverting result
1758 if Inv then
1759 case Result is
1760 when GT => return LT;
1761 when GE => return LE;
1762 when LT => return GT;
1763 when LE => return GE;
1764 when others => return Result;
1765 end case;
1766 end if;
1768 return Result;
1769 end;
1770 end if;
1771 end Compile_Time_Compare;
1773 -------------------------------
1774 -- Compile_Time_Known_Bounds --
1775 -------------------------------
1777 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1778 Indx : Node_Id;
1779 Typ : Entity_Id;
1781 begin
1782 if T = Any_Composite or else not Is_Array_Type (T) then
1783 return False;
1784 end if;
1786 Indx := First_Index (T);
1787 while Present (Indx) loop
1788 Typ := Underlying_Type (Etype (Indx));
1790 -- Never look at junk bounds of a generic type
1792 if Is_Generic_Type (Typ) then
1793 return False;
1794 end if;
1796 -- Otherwise check bounds for compile-time-known
1798 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1799 return False;
1800 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1801 return False;
1802 else
1803 Next_Index (Indx);
1804 end if;
1805 end loop;
1807 return True;
1808 end Compile_Time_Known_Bounds;
1810 ------------------------------
1811 -- Compile_Time_Known_Value --
1812 ------------------------------
1814 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1815 K : constant Node_Kind := Nkind (Op);
1816 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1818 begin
1819 -- Never known at compile time if bad type or raises Constraint_Error
1820 -- or empty (which can occur as a result of a previous error or in the
1821 -- case of e.g. an imported constant).
1823 if No (Op) then
1824 return False;
1826 elsif Op = Error
1827 or else Nkind (Op) not in N_Has_Etype
1828 or else Etype (Op) = Any_Type
1829 or else Raises_Constraint_Error (Op)
1830 then
1831 return False;
1832 end if;
1834 -- If we have an entity name, then see if it is the name of a constant
1835 -- and if so, test the corresponding constant value, or the name of an
1836 -- enumeration literal, which is always a constant.
1838 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1839 declare
1840 Ent : constant Entity_Id := Entity (Op);
1841 Val : Node_Id;
1843 begin
1844 -- Never known at compile time if it is a packed array value. We
1845 -- might want to try to evaluate these at compile time one day,
1846 -- but we do not make that attempt now.
1848 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1849 return False;
1851 elsif Ekind (Ent) = E_Enumeration_Literal then
1852 return True;
1854 elsif Ekind (Ent) = E_Constant then
1855 Val := Constant_Value (Ent);
1857 if Present (Val) then
1859 -- Guard against an illegal deferred constant whose full
1860 -- view is initialized with a reference to itself. Treat
1861 -- this case as a value not known at compile time.
1863 if Is_Entity_Name (Val) and then Entity (Val) = Ent then
1864 return False;
1865 else
1866 return Compile_Time_Known_Value (Val);
1867 end if;
1869 -- Otherwise, the constant does not have a compile-time-known
1870 -- value.
1872 else
1873 return False;
1874 end if;
1875 end if;
1876 end;
1878 -- We have a value, see if it is compile-time-known
1880 else
1881 -- Integer literals are worth storing in the cache
1883 if K = N_Integer_Literal then
1884 CV_Ent.N := Op;
1885 CV_Ent.V := Intval (Op);
1886 return True;
1888 -- Other literals and NULL are known at compile time
1890 elsif K in
1891 N_Character_Literal | N_Real_Literal | N_String_Literal | N_Null
1892 then
1893 return True;
1895 -- Evaluate static discriminants, to eliminate dead paths and
1896 -- redundant discriminant checks.
1898 elsif Is_Static_Discriminant_Component (Op) then
1899 return True;
1900 end if;
1901 end if;
1903 -- If we fall through, not known at compile time
1905 return False;
1907 -- If we get an exception while trying to do this test, then some error
1908 -- has occurred, and we simply say that the value is not known after all
1910 exception
1911 when others =>
1912 -- With debug flag K we will get an exception unless an error has
1913 -- already occurred (useful for debugging).
1915 if Debug_Flag_K then
1916 Check_Error_Detected;
1917 end if;
1919 return False;
1920 end Compile_Time_Known_Value;
1922 ---------------------------------------
1923 -- CRT_Safe_Compile_Time_Known_Value --
1924 ---------------------------------------
1926 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1927 begin
1928 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1929 and then not Is_OK_Static_Expression (Op)
1930 then
1931 return False;
1932 else
1933 return Compile_Time_Known_Value (Op);
1934 end if;
1935 end CRT_Safe_Compile_Time_Known_Value;
1937 -----------------
1938 -- Eval_Actual --
1939 -----------------
1941 -- This is only called for actuals of functions that are not predefined
1942 -- operators (which have already been rewritten as operators at this
1943 -- stage), so the call can never be folded, and all that needs doing for
1944 -- the actual is to do the check for a non-static context.
1946 procedure Eval_Actual (N : Node_Id) is
1947 begin
1948 Check_Non_Static_Context (N);
1949 end Eval_Actual;
1951 --------------------
1952 -- Eval_Allocator --
1953 --------------------
1955 -- Allocators are never static, so all we have to do is to do the
1956 -- check for a non-static context if an expression is present.
1958 procedure Eval_Allocator (N : Node_Id) is
1959 Expr : constant Node_Id := Expression (N);
1960 begin
1961 if Nkind (Expr) = N_Qualified_Expression then
1962 Check_Non_Static_Context (Expression (Expr));
1963 end if;
1964 end Eval_Allocator;
1966 ------------------------
1967 -- Eval_Arithmetic_Op --
1968 ------------------------
1970 -- Arithmetic operations are static functions, so the result is static
1971 -- if both operands are static (RM 4.9(7), 4.9(20)).
1973 procedure Eval_Arithmetic_Op (N : Node_Id) is
1974 Left : constant Node_Id := Left_Opnd (N);
1975 Right : constant Node_Id := Right_Opnd (N);
1976 Ltype : constant Entity_Id := Etype (Left);
1977 Rtype : constant Entity_Id := Etype (Right);
1978 Otype : Entity_Id := Empty;
1979 Stat : Boolean;
1980 Fold : Boolean;
1982 begin
1983 -- If not foldable we are done
1985 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1987 if not Fold then
1988 return;
1989 end if;
1991 -- Otherwise attempt to fold
1993 if Is_Universal_Numeric_Type (Etype (Left))
1994 and then
1995 Is_Universal_Numeric_Type (Etype (Right))
1996 then
1997 Otype := Find_Universal_Operator_Type (N);
1998 end if;
2000 -- Fold for cases where both operands are of integer type
2002 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
2003 declare
2004 Left_Int : constant Uint := Expr_Value (Left);
2005 Right_Int : constant Uint := Expr_Value (Right);
2006 Result : Uint;
2008 begin
2009 case Nkind (N) is
2010 when N_Op_Add =>
2011 Result := Left_Int + Right_Int;
2013 when N_Op_Subtract =>
2014 Result := Left_Int - Right_Int;
2016 when N_Op_Multiply =>
2017 if OK_Bits
2018 (N, UI_From_Int
2019 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
2020 then
2021 Result := Left_Int * Right_Int;
2022 else
2023 Result := Left_Int;
2024 end if;
2026 when N_Op_Divide =>
2028 -- The exception Constraint_Error is raised by integer
2029 -- division, rem and mod if the right operand is zero.
2031 if Right_Int = 0 then
2033 -- When SPARK_Mode is On, force a warning instead of
2034 -- an error in that case, as this likely corresponds
2035 -- to deactivated code.
2037 Apply_Compile_Time_Constraint_Error
2038 (N, "division by zero", CE_Divide_By_Zero,
2039 Loc => Sloc (Right),
2040 Warn => not Stat or SPARK_Mode = On);
2041 return;
2043 -- Otherwise we can do the division
2045 else
2046 Result := Left_Int / Right_Int;
2047 end if;
2049 when N_Op_Mod =>
2051 -- The exception Constraint_Error is raised by integer
2052 -- division, rem and mod if the right operand is zero.
2054 if Right_Int = 0 then
2056 -- When SPARK_Mode is On, force a warning instead of
2057 -- an error in that case, as this likely corresponds
2058 -- to deactivated code.
2060 Apply_Compile_Time_Constraint_Error
2061 (N, "mod with zero divisor", CE_Divide_By_Zero,
2062 Loc => Sloc (Right),
2063 Warn => not Stat or SPARK_Mode = On);
2064 return;
2066 else
2067 Result := Left_Int mod Right_Int;
2068 end if;
2070 when N_Op_Rem =>
2072 -- The exception Constraint_Error is raised by integer
2073 -- division, rem and mod if the right operand is zero.
2075 if Right_Int = 0 then
2077 -- When SPARK_Mode is On, force a warning instead of
2078 -- an error in that case, as this likely corresponds
2079 -- to deactivated code.
2081 Apply_Compile_Time_Constraint_Error
2082 (N, "rem with zero divisor", CE_Divide_By_Zero,
2083 Loc => Sloc (Right),
2084 Warn => not Stat or SPARK_Mode = On);
2085 return;
2087 else
2088 Result := Left_Int rem Right_Int;
2089 end if;
2091 when others =>
2092 raise Program_Error;
2093 end case;
2095 -- Adjust the result by the modulus if the type is a modular type
2097 if Is_Modular_Integer_Type (Ltype) then
2098 Result := Result mod Modulus (Ltype);
2099 end if;
2101 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
2103 -- If we get here we can fold the result
2105 Fold_Uint (N, Result, Stat);
2106 end;
2108 -- Cases where at least one operand is a real. We handle the cases of
2109 -- both reals, or mixed/real integer cases (the latter happen only for
2110 -- divide and multiply, and the result is always real).
2112 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2113 declare
2114 Left_Real : Ureal;
2115 Right_Real : Ureal;
2116 Result : Ureal;
2118 begin
2119 if Is_Real_Type (Ltype) then
2120 Left_Real := Expr_Value_R (Left);
2121 else
2122 Left_Real := UR_From_Uint (Expr_Value (Left));
2123 end if;
2125 if Is_Real_Type (Rtype) then
2126 Right_Real := Expr_Value_R (Right);
2127 else
2128 Right_Real := UR_From_Uint (Expr_Value (Right));
2129 end if;
2131 if Nkind (N) = N_Op_Add then
2132 Result := Left_Real + Right_Real;
2134 elsif Nkind (N) = N_Op_Subtract then
2135 Result := Left_Real - Right_Real;
2137 elsif Nkind (N) = N_Op_Multiply then
2138 Result := Left_Real * Right_Real;
2140 else pragma Assert (Nkind (N) = N_Op_Divide);
2141 if UR_Is_Zero (Right_Real) then
2142 Apply_Compile_Time_Constraint_Error
2143 (N, "division by zero", CE_Divide_By_Zero,
2144 Loc => Sloc (Right));
2145 return;
2146 end if;
2148 Result := Left_Real / Right_Real;
2149 end if;
2151 Fold_Ureal (N, Result, Stat);
2152 end;
2153 end if;
2155 -- If the operator was resolved to a specific type, make sure that type
2156 -- is frozen even if the expression is folded into a literal (which has
2157 -- a universal type).
2159 if Present (Otype) then
2160 Freeze_Before (N, Otype);
2161 end if;
2162 end Eval_Arithmetic_Op;
2164 ----------------------------
2165 -- Eval_Character_Literal --
2166 ----------------------------
2168 -- Nothing to be done
2170 procedure Eval_Character_Literal (N : Node_Id) is
2171 pragma Warnings (Off, N);
2172 begin
2173 null;
2174 end Eval_Character_Literal;
2176 ---------------
2177 -- Eval_Call --
2178 ---------------
2180 -- Static function calls are either calls to predefined operators
2181 -- with static arguments, or calls to functions that rename a literal.
2182 -- Only the latter case is handled here, predefined operators are
2183 -- constant-folded elsewhere.
2185 -- If the function is itself inherited the literal of the parent type must
2186 -- be explicitly converted to the return type of the function.
2188 procedure Eval_Call (N : Node_Id) is
2189 Loc : constant Source_Ptr := Sloc (N);
2190 Typ : constant Entity_Id := Etype (N);
2191 Lit : Entity_Id;
2193 begin
2194 if Nkind (N) = N_Function_Call
2195 and then No (Parameter_Associations (N))
2196 and then Is_Entity_Name (Name (N))
2197 and then Present (Alias (Entity (Name (N))))
2198 and then Is_Enumeration_Type (Base_Type (Typ))
2199 then
2200 Lit := Ultimate_Alias (Entity (Name (N)));
2202 if Ekind (Lit) = E_Enumeration_Literal then
2203 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2204 Rewrite
2205 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2206 else
2207 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2208 end if;
2210 Resolve (N, Typ);
2211 end if;
2213 elsif Nkind (N) = N_Function_Call
2214 and then Is_Entity_Name (Name (N))
2215 and then Is_Intrinsic_Subprogram (Entity (Name (N)))
2216 then
2217 Eval_Intrinsic_Call (N, Entity (Name (N)));
2219 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2220 -- is enabled and we have a call to a static function, substitute a
2221 -- static value for the call, to allow folding the expression. This
2222 -- supports checking the requirement of RM 6.8(5.3/5) in
2223 -- Analyze_Expression_Function.
2225 elsif Checking_Potentially_Static_Expression
2226 and then Is_Static_Function_Call (N)
2227 then
2228 Fold_Dummy (N, Typ);
2229 end if;
2230 end Eval_Call;
2232 --------------------------
2233 -- Eval_Case_Expression --
2234 --------------------------
2236 -- A conditional expression is static if all its conditions and dependent
2237 -- expressions are static. Note that we do not care if the dependent
2238 -- expressions raise CE, except for the one that will be selected.
2240 procedure Eval_Case_Expression (N : Node_Id) is
2241 Alt : Node_Id;
2242 Choice : Node_Id;
2244 begin
2245 Set_Is_Static_Expression (N, False);
2247 if Error_Posted (Expression (N))
2248 or else not Is_Static_Expression (Expression (N))
2249 then
2250 Check_Non_Static_Context (Expression (N));
2251 return;
2252 end if;
2254 -- First loop, make sure all the alternatives are static expressions
2255 -- none of which raise Constraint_Error. We make the Constraint_Error
2256 -- check because part of the legality condition for a correct static
2257 -- case expression is that the cases are covered, like any other case
2258 -- expression. And we can't do that if any of the conditions raise an
2259 -- exception, so we don't even try to evaluate if that is the case.
2261 Alt := First (Alternatives (N));
2262 while Present (Alt) loop
2264 -- The expression must be static, but we don't care at this stage
2265 -- if it raises Constraint_Error (the alternative might not match,
2266 -- in which case the expression is statically unevaluated anyway).
2268 if not Is_Static_Expression (Expression (Alt)) then
2269 Check_Non_Static_Context (Expression (Alt));
2270 return;
2271 end if;
2273 -- The choices of a case always have to be static, and cannot raise
2274 -- an exception. If this condition is not met, then the expression
2275 -- is plain illegal, so just abandon evaluation attempts. No need
2276 -- to check non-static context when we have something illegal anyway.
2278 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2279 return;
2280 end if;
2282 Next (Alt);
2283 end loop;
2285 -- OK, if the above loop gets through it means that all choices are OK
2286 -- static (don't raise exceptions), so the whole case is static, and we
2287 -- can find the matching alternative.
2289 Set_Is_Static_Expression (N);
2291 -- Now to deal with propagating a possible Constraint_Error
2293 -- If the selecting expression raises CE, propagate and we are done
2295 if Raises_Constraint_Error (Expression (N)) then
2296 Set_Raises_Constraint_Error (N);
2298 -- Otherwise we need to check the alternatives to find the matching
2299 -- one. CE's in other than the matching one are not relevant. But we
2300 -- do need to check the matching one. Unlike the first loop, we do not
2301 -- have to go all the way through, when we find the matching one, quit.
2303 else
2304 Alt := First (Alternatives (N));
2305 Search : loop
2307 -- We must find a match among the alternatives. If not, this must
2308 -- be due to other errors, so just ignore, leaving as non-static.
2310 if No (Alt) then
2311 Set_Is_Static_Expression (N, False);
2312 return;
2313 end if;
2315 -- Otherwise loop through choices of this alternative
2317 Choice := First (Discrete_Choices (Alt));
2318 while Present (Choice) loop
2320 -- If we find a matching choice, then the Expression of this
2321 -- alternative replaces N (Raises_Constraint_Error flag is
2322 -- included, so we don't have to special case that).
2324 if Choice_Matches (Expression (N), Choice) = Match then
2325 Rewrite (N, Relocate_Node (Expression (Alt)));
2326 return;
2327 end if;
2329 Next (Choice);
2330 end loop;
2332 Next (Alt);
2333 end loop Search;
2334 end if;
2335 end Eval_Case_Expression;
2337 ------------------------
2338 -- Eval_Concatenation --
2339 ------------------------
2341 -- Concatenation is a static function, so the result is static if both
2342 -- operands are static (RM 4.9(7), 4.9(21)).
2344 procedure Eval_Concatenation (N : Node_Id) is
2345 Left : constant Node_Id := Left_Opnd (N);
2346 Right : constant Node_Id := Right_Opnd (N);
2347 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2348 Stat : Boolean;
2349 Fold : Boolean;
2351 begin
2352 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2353 -- non-static context.
2355 if Ada_Version = Ada_83
2356 and then Comes_From_Source (N)
2357 then
2358 Check_Non_Static_Context (Left);
2359 Check_Non_Static_Context (Right);
2360 return;
2361 end if;
2363 -- If not foldable we are done. In principle concatenation that yields
2364 -- any string type is static (i.e. an array type of character types).
2365 -- However, character types can include enumeration literals, and
2366 -- concatenation in that case cannot be described by a literal, so we
2367 -- only consider the operation static if the result is an array of
2368 -- (a descendant of) a predefined character type.
2370 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2372 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2373 Set_Is_Static_Expression (N, False);
2374 return;
2375 end if;
2377 -- Compile time string concatenation
2379 -- ??? Note that operands that are aggregates can be marked as static,
2380 -- so we should attempt at a later stage to fold concatenations with
2381 -- such aggregates.
2383 declare
2384 Left_Str : constant Node_Id := Get_String_Val (Left);
2385 Left_Len : Nat;
2386 Right_Str : constant Node_Id := Get_String_Val (Right);
2387 Folded_Val : String_Id := No_String;
2389 begin
2390 -- Establish new string literal, and store left operand. We make
2391 -- sure to use the special Start_String that takes an operand if
2392 -- the left operand is a string literal. Since this is optimized
2393 -- in the case where that is the most recently created string
2394 -- literal, we ensure efficient time/space behavior for the
2395 -- case of a concatenation of a series of string literals.
2397 if Nkind (Left_Str) = N_String_Literal then
2398 Left_Len := String_Length (Strval (Left_Str));
2400 -- If the left operand is the empty string, and the right operand
2401 -- is a string literal (the case of "" & "..."), the result is the
2402 -- value of the right operand. This optimization is important when
2403 -- Is_Folded_In_Parser, to avoid copying an enormous right
2404 -- operand.
2406 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2407 Folded_Val := Strval (Right_Str);
2408 else
2409 Start_String (Strval (Left_Str));
2410 end if;
2412 else
2413 Start_String;
2414 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2415 Left_Len := 1;
2416 end if;
2418 -- Now append the characters of the right operand, unless we
2419 -- optimized the "" & "..." case above.
2421 if Nkind (Right_Str) = N_String_Literal then
2422 if Left_Len /= 0 then
2423 Store_String_Chars (Strval (Right_Str));
2424 Folded_Val := End_String;
2425 end if;
2426 else
2427 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2428 Folded_Val := End_String;
2429 end if;
2431 Set_Is_Static_Expression (N, Stat);
2433 -- If left operand is the empty string, the result is the
2434 -- right operand, including its bounds if anomalous.
2436 if Left_Len = 0
2437 and then Is_Array_Type (Etype (Right))
2438 and then Etype (Right) /= Any_String
2439 then
2440 Set_Etype (N, Etype (Right));
2441 end if;
2443 Fold_Str (N, Folded_Val, Static => Stat);
2444 end;
2445 end Eval_Concatenation;
2447 ----------------------
2448 -- Eval_Entity_Name --
2449 ----------------------
2451 -- This procedure is used for identifiers and expanded names other than
2452 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2453 -- static if they denote a static constant (RM 4.9(6)) or if the name
2454 -- denotes an enumeration literal (RM 4.9(22)).
2456 procedure Eval_Entity_Name (N : Node_Id) is
2457 Def_Id : constant Entity_Id := Entity (N);
2458 Val : Node_Id;
2460 begin
2461 -- Enumeration literals are always considered to be constants
2462 -- and cannot raise Constraint_Error (RM 4.9(22)).
2464 if Ekind (Def_Id) = E_Enumeration_Literal then
2465 Set_Is_Static_Expression (N);
2466 return;
2468 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2469 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2470 -- it does not violate 10.2.1(8) here, since this is not a variable.
2472 elsif Ekind (Def_Id) = E_Constant then
2474 -- Deferred constants must always be treated as nonstatic outside the
2475 -- scope of their full view.
2477 if Present (Full_View (Def_Id))
2478 and then not In_Open_Scopes (Scope (Def_Id))
2479 then
2480 Val := Empty;
2481 else
2482 Val := Constant_Value (Def_Id);
2483 end if;
2485 if Present (Val) then
2486 Set_Is_Static_Expression
2487 (N, Is_Static_Expression (Val)
2488 and then Is_Static_Subtype (Etype (Def_Id)));
2489 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2491 if not Is_Static_Expression (N)
2492 and then not Is_Generic_Type (Etype (N))
2493 then
2494 Validate_Static_Object_Name (N);
2495 end if;
2497 -- Mark constant condition in SCOs
2499 if Generate_SCO
2500 and then Comes_From_Source (N)
2501 and then Is_Boolean_Type (Etype (Def_Id))
2502 and then Compile_Time_Known_Value (N)
2503 then
2504 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2505 end if;
2507 return;
2508 end if;
2510 -- Ada 2022 (AI12-0075): If checking for potentially static expressions
2511 -- is enabled and we have a reference to a formal parameter of mode in,
2512 -- substitute a static value for the reference, to allow folding the
2513 -- expression. This supports checking the requirement of RM 6.8(5.3/5)
2514 -- in Analyze_Expression_Function.
2516 elsif Ekind (Def_Id) = E_In_Parameter
2517 and then Checking_Potentially_Static_Expression
2518 and then Is_Static_Function (Scope (Def_Id))
2519 then
2520 Fold_Dummy (N, Etype (Def_Id));
2521 end if;
2523 -- Fall through if the name is not static
2525 Validate_Static_Object_Name (N);
2526 end Eval_Entity_Name;
2528 ------------------------
2529 -- Eval_If_Expression --
2530 ------------------------
2532 -- We can fold to a static expression if the condition and both dependent
2533 -- expressions are static. Otherwise, the only required processing is to do
2534 -- the check for non-static context for the then and else expressions.
2536 procedure Eval_If_Expression (N : Node_Id) is
2537 Condition : constant Node_Id := First (Expressions (N));
2538 Then_Expr : constant Node_Id := Next (Condition);
2539 Else_Expr : constant Node_Id := Next (Then_Expr);
2540 Result : Node_Id;
2541 Non_Result : Node_Id;
2543 Rstat : constant Boolean :=
2544 Is_Static_Expression (Condition)
2545 and then
2546 Is_Static_Expression (Then_Expr)
2547 and then
2548 Is_Static_Expression (Else_Expr);
2549 -- True if result is static
2551 begin
2552 -- If result not static, nothing to do, otherwise set static result
2554 if not Rstat then
2555 return;
2556 else
2557 Set_Is_Static_Expression (N);
2558 end if;
2560 -- If any operand is Any_Type, just propagate to result and do not try
2561 -- to fold, this prevents cascaded errors.
2563 if Etype (Condition) = Any_Type or else
2564 Etype (Then_Expr) = Any_Type or else
2565 Etype (Else_Expr) = Any_Type
2566 then
2567 Set_Etype (N, Any_Type);
2568 Set_Is_Static_Expression (N, False);
2569 return;
2570 end if;
2572 -- If condition raises Constraint_Error then we have already signaled
2573 -- an error, and we just propagate to the result and do not fold.
2575 if Raises_Constraint_Error (Condition) then
2576 Set_Raises_Constraint_Error (N);
2577 return;
2578 end if;
2580 -- Static case where we can fold. Note that we don't try to fold cases
2581 -- where the condition is known at compile time, but the result is
2582 -- non-static. This avoids possible cases of infinite recursion where
2583 -- the expander puts in a redundant test and we remove it. Instead we
2584 -- deal with these cases in the expander.
2586 -- Select result operand
2588 if Is_True (Expr_Value (Condition)) then
2589 Result := Then_Expr;
2590 Non_Result := Else_Expr;
2591 else
2592 Result := Else_Expr;
2593 Non_Result := Then_Expr;
2594 end if;
2596 -- Note that it does not matter if the non-result operand raises a
2597 -- Constraint_Error, but if the result raises Constraint_Error then we
2598 -- replace the node with a raise Constraint_Error. This will properly
2599 -- propagate Raises_Constraint_Error since this flag is set in Result.
2601 if Raises_Constraint_Error (Result) then
2602 Rewrite_In_Raise_CE (N, Result);
2603 Check_Non_Static_Context (Non_Result);
2605 -- Otherwise the result operand replaces the original node
2607 else
2608 Rewrite (N, Relocate_Node (Result));
2609 Set_Is_Static_Expression (N);
2610 end if;
2611 end Eval_If_Expression;
2613 ----------------------------
2614 -- Eval_Indexed_Component --
2615 ----------------------------
2617 -- Indexed components are never static, so we need to perform the check
2618 -- for non-static context on the index values. Then, we check if the
2619 -- value can be obtained at compile time, even though it is non-static.
2621 procedure Eval_Indexed_Component (N : Node_Id) is
2622 Expr : Node_Id;
2624 begin
2625 -- Check for non-static context on index values
2627 Expr := First (Expressions (N));
2628 while Present (Expr) loop
2629 Check_Non_Static_Context (Expr);
2630 Next (Expr);
2631 end loop;
2633 -- If the indexed component appears in an object renaming declaration
2634 -- then we do not want to try to evaluate it, since in this case we
2635 -- need the identity of the array element.
2637 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2638 return;
2640 -- Similarly if the indexed component appears as the prefix of an
2641 -- attribute we don't want to evaluate it, because at least for
2642 -- some cases of attributes we need the identify (e.g. Access, Size).
2644 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2645 return;
2646 end if;
2648 -- Note: there are other cases, such as the left side of an assignment,
2649 -- or an OUT parameter for a call, where the replacement results in the
2650 -- illegal use of a constant, But these cases are illegal in the first
2651 -- place, so the replacement, though silly, is harmless.
2653 -- Now see if this is a constant array reference
2655 if List_Length (Expressions (N)) = 1
2656 and then Is_Entity_Name (Prefix (N))
2657 and then Ekind (Entity (Prefix (N))) = E_Constant
2658 and then Present (Constant_Value (Entity (Prefix (N))))
2659 then
2660 declare
2661 Loc : constant Source_Ptr := Sloc (N);
2662 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2663 Sub : constant Node_Id := First (Expressions (N));
2665 Atyp : Entity_Id;
2666 -- Type of array
2668 Lin : Nat;
2669 -- Linear one's origin subscript value for array reference
2671 Lbd : Node_Id;
2672 -- Lower bound of the first array index
2674 Elm : Node_Id;
2675 -- Value from constant array
2677 begin
2678 Atyp := Etype (Arr);
2680 if Is_Access_Type (Atyp) then
2681 Atyp := Designated_Type (Atyp);
2682 end if;
2684 -- If we have an array type (we should have but perhaps there are
2685 -- error cases where this is not the case), then see if we can do
2686 -- a constant evaluation of the array reference.
2688 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2689 if Ekind (Atyp) = E_String_Literal_Subtype then
2690 Lbd := String_Literal_Low_Bound (Atyp);
2691 else
2692 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2693 end if;
2695 if Compile_Time_Known_Value (Sub)
2696 and then Nkind (Arr) = N_Aggregate
2697 and then Compile_Time_Known_Value (Lbd)
2698 and then Is_Discrete_Type (Component_Type (Atyp))
2699 then
2700 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2702 if List_Length (Expressions (Arr)) >= Lin then
2703 Elm := Pick (Expressions (Arr), Lin);
2705 -- If the resulting expression is compile-time-known,
2706 -- then we can rewrite the indexed component with this
2707 -- value, being sure to mark the result as non-static.
2708 -- We also reset the Sloc, in case this generates an
2709 -- error later on (e.g. 136'Access).
2711 if Compile_Time_Known_Value (Elm) then
2712 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2713 Set_Is_Static_Expression (N, False);
2714 Set_Sloc (N, Loc);
2715 end if;
2716 end if;
2718 -- We can also constant-fold if the prefix is a string literal.
2719 -- This will be useful in an instantiation or an inlining.
2721 elsif Compile_Time_Known_Value (Sub)
2722 and then Nkind (Arr) = N_String_Literal
2723 and then Compile_Time_Known_Value (Lbd)
2724 and then Expr_Value (Lbd) = 1
2725 and then Expr_Value (Sub) <=
2726 String_Literal_Length (Etype (Arr))
2727 then
2728 declare
2729 C : constant Char_Code :=
2730 Get_String_Char (Strval (Arr),
2731 UI_To_Int (Expr_Value (Sub)));
2732 begin
2733 Set_Character_Literal_Name (C);
2735 Elm :=
2736 Make_Character_Literal (Loc,
2737 Chars => Name_Find,
2738 Char_Literal_Value => UI_From_CC (C));
2739 Set_Etype (Elm, Component_Type (Atyp));
2740 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2741 Set_Is_Static_Expression (N, False);
2742 end;
2743 end if;
2744 end if;
2745 end;
2746 end if;
2747 end Eval_Indexed_Component;
2749 --------------------------
2750 -- Eval_Integer_Literal --
2751 --------------------------
2753 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2754 -- as static by the analyzer. The reason we did it that early is to allow
2755 -- the possibility of turning off the Is_Static_Expression flag after
2756 -- analysis, but before resolution, when integer literals are generated in
2757 -- the expander that do not correspond to static expressions.
2759 procedure Eval_Integer_Literal (N : Node_Id) is
2760 function In_Any_Integer_Context (K : Node_Kind) return Boolean;
2761 -- If the literal is resolved with a specific type in a context where
2762 -- the expected type is Any_Integer, there are no range checks on the
2763 -- literal. By the time the literal is evaluated, it carries the type
2764 -- imposed by the enclosing expression, and we must recover the context
2765 -- to determine that Any_Integer is meant.
2767 ----------------------------
2768 -- In_Any_Integer_Context --
2769 ----------------------------
2771 function In_Any_Integer_Context (K : Node_Kind) return Boolean is
2772 begin
2773 -- Any_Integer also appears in digits specifications for real types,
2774 -- but those have bounds smaller that those of any integer base type,
2775 -- so we can safely ignore these cases.
2777 return K in N_Attribute_Definition_Clause
2778 | N_Modular_Type_Definition
2779 | N_Number_Declaration
2780 | N_Signed_Integer_Type_Definition;
2781 end In_Any_Integer_Context;
2783 -- Local variables
2785 PK : constant Node_Kind := Nkind (Parent (N));
2786 Typ : constant Entity_Id := Etype (N);
2788 -- Start of processing for Eval_Integer_Literal
2790 begin
2791 -- If the literal appears in a non-expression context, then it is
2792 -- certainly appearing in a non-static context, so check it. This is
2793 -- actually a redundant check, since Check_Non_Static_Context would
2794 -- check it, but it seems worthwhile to optimize out the call.
2796 -- Additionally, when the literal appears within an if or case
2797 -- expression it must be checked as well. However, due to the literal
2798 -- appearing within a conditional statement, expansion greatly changes
2799 -- the nature of its context and performing some of the checks within
2800 -- Check_Non_Static_Context on an expanded literal may lead to spurious
2801 -- and misleading warnings.
2803 if (PK not in N_Case_Expression_Alternative | N_Subexpr
2804 or else (PK in N_Case_Expression_Alternative | N_If_Expression
2805 and then
2806 Comes_From_Source (N)))
2807 and then not In_Any_Integer_Context (PK)
2808 then
2809 Check_Non_Static_Context (N);
2810 end if;
2812 -- Modular integer literals must be in their base range
2814 if Is_Modular_Integer_Type (Typ)
2815 and then Is_Out_Of_Range (N, Base_Type (Typ), Assume_Valid => True)
2816 then
2817 Out_Of_Range (N);
2818 end if;
2819 end Eval_Integer_Literal;
2821 -------------------------
2822 -- Eval_Intrinsic_Call --
2823 -------------------------
2825 procedure Eval_Intrinsic_Call (N : Node_Id; E : Entity_Id) is
2827 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind);
2828 -- Evaluate an intrinsic shift call N on the given subprogram E.
2829 -- Op is the kind for the shift node.
2831 ----------------
2832 -- Eval_Shift --
2833 ----------------
2835 procedure Eval_Shift (N : Node_Id; E : Entity_Id; Op : Node_Kind) is
2836 Left : constant Node_Id := First_Actual (N);
2837 Right : constant Node_Id := Next_Actual (Left);
2838 Static : constant Boolean := Is_Static_Function (E);
2840 begin
2841 if Static then
2842 if Checking_Potentially_Static_Expression then
2843 Fold_Dummy (N, Etype (N));
2844 return;
2845 end if;
2846 end if;
2848 Fold_Shift
2849 (N, Left, Right, Op, Static => Static, Check_Elab => not Static);
2850 end Eval_Shift;
2852 Nam : Name_Id;
2854 begin
2855 -- Nothing to do if the intrinsic is handled by the back end.
2857 if Present (Interface_Name (E)) then
2858 return;
2859 end if;
2861 -- Intrinsic calls as part of a static function is a (core)
2862 -- language extension.
2864 if Checking_Potentially_Static_Expression
2865 and then not Core_Extensions_Allowed
2866 then
2867 return;
2868 end if;
2870 -- If we have a renaming, expand the call to the original operation,
2871 -- which must itself be intrinsic, since renaming requires matching
2872 -- conventions and this has already been checked.
2874 if Present (Alias (E)) then
2875 Eval_Intrinsic_Call (N, Alias (E));
2876 return;
2877 end if;
2879 -- If the intrinsic subprogram is generic, gets its original name
2881 if Present (Parent (E))
2882 and then Present (Generic_Parent (Parent (E)))
2883 then
2884 Nam := Chars (Generic_Parent (Parent (E)));
2885 else
2886 Nam := Chars (E);
2887 end if;
2889 case Nam is
2890 when Name_Shift_Left =>
2891 Eval_Shift (N, E, N_Op_Shift_Left);
2892 when Name_Shift_Right =>
2893 Eval_Shift (N, E, N_Op_Shift_Right);
2894 when Name_Shift_Right_Arithmetic =>
2895 Eval_Shift (N, E, N_Op_Shift_Right_Arithmetic);
2896 when others =>
2897 null;
2898 end case;
2899 end Eval_Intrinsic_Call;
2901 ---------------------
2902 -- Eval_Logical_Op --
2903 ---------------------
2905 -- Logical operations are static functions, so the result is potentially
2906 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2908 procedure Eval_Logical_Op (N : Node_Id) is
2909 Left : constant Node_Id := Left_Opnd (N);
2910 Right : constant Node_Id := Right_Opnd (N);
2911 Left_Int : Uint := No_Uint;
2912 Right_Int : Uint := No_Uint;
2913 Stat : Boolean;
2914 Fold : Boolean;
2916 begin
2917 -- If not foldable we are done
2919 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2921 if not Fold then
2922 return;
2923 end if;
2925 -- Compile time evaluation of logical operation
2927 if Is_Modular_Integer_Type (Etype (N)) then
2928 Left_Int := Expr_Value (Left);
2929 Right_Int := Expr_Value (Right);
2931 declare
2932 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2933 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2935 begin
2936 To_Bits (Left_Int, Left_Bits);
2937 To_Bits (Right_Int, Right_Bits);
2939 -- Note: should really be able to use array ops instead of
2940 -- these loops, but they break the build with a cryptic error
2941 -- during the bind of gnat1 likely due to a wrong computation
2942 -- of a date or checksum.
2944 if Nkind (N) = N_Op_And then
2945 for J in Left_Bits'Range loop
2946 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2947 end loop;
2949 elsif Nkind (N) = N_Op_Or then
2950 for J in Left_Bits'Range loop
2951 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2952 end loop;
2954 else
2955 pragma Assert (Nkind (N) = N_Op_Xor);
2957 for J in Left_Bits'Range loop
2958 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2959 end loop;
2960 end if;
2962 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2963 end;
2965 else
2966 pragma Assert (Is_Boolean_Type (Etype (N)));
2968 if Compile_Time_Known_Value (Left)
2969 and then Compile_Time_Known_Value (Right)
2970 then
2971 Right_Int := Expr_Value (Right);
2972 Left_Int := Expr_Value (Left);
2973 end if;
2975 if Nkind (N) = N_Op_And then
2977 -- If Left or Right are not compile time known values it means
2978 -- that the result is always False as per
2979 -- Test_Expression_Is_Foldable.
2980 -- Note that in this case, both Right_Int and Left_Int are set
2981 -- to No_Uint, so need to test for both.
2983 if No (Right_Int) then
2984 Fold_Uint (N, Uint_0, Stat);
2985 else
2986 Fold_Uint (N,
2987 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2988 end if;
2989 elsif Nkind (N) = N_Op_Or then
2991 -- If Left or Right are not compile time known values it means
2992 -- that the result is always True. as per
2993 -- Test_Expression_Is_Foldable.
2994 -- Note that in this case, both Right_Int and Left_Int are set
2995 -- to No_Uint, so need to test for both.
2997 if No (Right_Int) then
2998 Fold_Uint (N, Uint_1, Stat);
2999 else
3000 Fold_Uint (N,
3001 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
3002 end if;
3003 else
3004 pragma Assert (Nkind (N) = N_Op_Xor);
3005 Fold_Uint (N,
3006 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
3007 end if;
3008 end if;
3009 end Eval_Logical_Op;
3011 ------------------------
3012 -- Eval_Membership_Op --
3013 ------------------------
3015 -- A membership test is potentially static if the expression is static, and
3016 -- the range is a potentially static range, or is a subtype mark denoting a
3017 -- static subtype (RM 4.9(12)).
3019 procedure Eval_Membership_Op (N : Node_Id) is
3020 Alts : constant List_Id := Alternatives (N);
3021 Choice : constant Node_Id := Right_Opnd (N);
3022 Expr : constant Node_Id := Left_Opnd (N);
3023 Result : Match_Result;
3025 begin
3026 -- Ignore if error in either operand, except to make sure that Any_Type
3027 -- is properly propagated to avoid junk cascaded errors.
3029 if Etype (Expr) = Any_Type
3030 or else (Present (Choice) and then Etype (Choice) = Any_Type)
3031 then
3032 Set_Etype (N, Any_Type);
3033 return;
3034 end if;
3036 -- If left operand non-static, then nothing to do
3038 if not Is_Static_Expression (Expr) then
3039 return;
3040 end if;
3042 -- If choice is non-static, left operand is in non-static context
3044 if (Present (Choice) and then not Is_Static_Choice (Choice))
3045 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
3046 then
3047 Check_Non_Static_Context (Expr);
3048 return;
3049 end if;
3051 -- Otherwise we definitely have a static expression
3053 Set_Is_Static_Expression (N);
3055 -- If left operand raises Constraint_Error, propagate and we are done
3057 if Raises_Constraint_Error (Expr) then
3058 Set_Raises_Constraint_Error (N, True);
3060 -- See if we match
3062 else
3063 if Present (Choice) then
3064 Result := Choice_Matches (Expr, Choice);
3065 else
3066 Result := Choices_Match (Expr, Alts);
3067 end if;
3069 -- If result is Non_Static, it means that we raise Constraint_Error,
3070 -- since we already tested that the operands were themselves static.
3072 if Result = Non_Static then
3073 Set_Raises_Constraint_Error (N);
3075 -- Otherwise we have our result (flipped if NOT IN case)
3077 else
3078 Fold_Uint
3079 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
3080 Warn_On_Known_Condition (N);
3081 end if;
3082 end if;
3083 end Eval_Membership_Op;
3085 ------------------------
3086 -- Eval_Named_Integer --
3087 ------------------------
3089 procedure Eval_Named_Integer (N : Node_Id) is
3090 begin
3091 Fold_Uint (N,
3092 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
3093 end Eval_Named_Integer;
3095 ---------------------
3096 -- Eval_Named_Real --
3097 ---------------------
3099 procedure Eval_Named_Real (N : Node_Id) is
3100 begin
3101 Fold_Ureal (N,
3102 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
3103 end Eval_Named_Real;
3105 -------------------
3106 -- Eval_Op_Expon --
3107 -------------------
3109 -- Exponentiation is a static functions, so the result is potentially
3110 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
3112 procedure Eval_Op_Expon (N : Node_Id) is
3113 Left : constant Node_Id := Left_Opnd (N);
3114 Right : constant Node_Id := Right_Opnd (N);
3115 Stat : Boolean;
3116 Fold : Boolean;
3118 begin
3119 -- If not foldable we are done
3121 Test_Expression_Is_Foldable
3122 (N, Left, Right, Stat, Fold, CRT_Safe => True);
3124 -- Return if not foldable
3126 if not Fold then
3127 return;
3128 end if;
3130 if Configurable_Run_Time_Mode and not Stat then
3131 return;
3132 end if;
3134 -- Fold exponentiation operation
3136 declare
3137 Right_Int : constant Uint := Expr_Value (Right);
3139 begin
3140 -- Integer case
3142 if Is_Integer_Type (Etype (Left)) then
3143 declare
3144 Left_Int : constant Uint := Expr_Value (Left);
3145 Result : Uint;
3147 begin
3148 -- Exponentiation of an integer raises Constraint_Error for a
3149 -- negative exponent (RM 4.5.6).
3151 if Right_Int < 0 then
3152 Apply_Compile_Time_Constraint_Error
3153 (N, "integer exponent negative", CE_Range_Check_Failed,
3154 Warn => not Stat);
3155 return;
3157 else
3158 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
3159 Result := Left_Int ** Right_Int;
3160 else
3161 Result := Left_Int;
3162 end if;
3164 if Is_Modular_Integer_Type (Etype (N)) then
3165 Result := Result mod Modulus (Etype (N));
3166 end if;
3168 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
3170 Fold_Uint (N, Result, Stat);
3171 end if;
3172 end;
3174 -- Real case
3176 else
3177 declare
3178 Left_Real : constant Ureal := Expr_Value_R (Left);
3180 begin
3181 -- Cannot have a zero base with a negative exponent
3183 if UR_Is_Zero (Left_Real) then
3185 if Right_Int < 0 then
3186 Apply_Compile_Time_Constraint_Error
3187 (N, "zero ** negative integer", CE_Range_Check_Failed,
3188 Warn => not Stat);
3189 return;
3190 else
3191 Fold_Ureal (N, Ureal_0, Stat);
3192 end if;
3194 else
3195 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
3196 end if;
3197 end;
3198 end if;
3199 end;
3200 end Eval_Op_Expon;
3202 -----------------
3203 -- Eval_Op_Not --
3204 -----------------
3206 -- The not operation is a static function, so the result is potentially
3207 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3209 procedure Eval_Op_Not (N : Node_Id) is
3210 Right : constant Node_Id := Right_Opnd (N);
3211 Stat : Boolean;
3212 Fold : Boolean;
3214 begin
3215 -- If not foldable we are done
3217 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3219 if not Fold then
3220 return;
3221 end if;
3223 -- Fold not operation
3225 declare
3226 Rint : constant Uint := Expr_Value (Right);
3227 Typ : constant Entity_Id := Etype (N);
3229 begin
3230 -- Negation is equivalent to subtracting from the modulus minus one.
3231 -- For a binary modulus this is equivalent to the ones-complement of
3232 -- the original value. For a nonbinary modulus this is an arbitrary
3233 -- but consistent definition.
3235 if Is_Modular_Integer_Type (Typ) then
3236 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3237 else pragma Assert (Is_Boolean_Type (Typ));
3238 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3239 end if;
3241 Set_Is_Static_Expression (N, Stat);
3242 end;
3243 end Eval_Op_Not;
3245 -------------------------------
3246 -- Eval_Qualified_Expression --
3247 -------------------------------
3249 -- A qualified expression is potentially static if its subtype mark denotes
3250 -- a static subtype and its expression is potentially static (RM 4.9 (10)).
3252 procedure Eval_Qualified_Expression (N : Node_Id) is
3253 Operand : constant Node_Id := Expression (N);
3254 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3256 Stat : Boolean;
3257 Fold : Boolean;
3258 Hex : Boolean;
3260 begin
3261 -- Can only fold if target is string or scalar and subtype is static.
3262 -- Also, do not fold if our parent is an allocator (this is because the
3263 -- qualified expression is really part of the syntactic structure of an
3264 -- allocator, and we do not want to end up with something that
3265 -- corresponds to "new 1" where the 1 is the result of folding a
3266 -- qualified expression).
3268 if not Is_Static_Subtype (Target_Type)
3269 or else Nkind (Parent (N)) = N_Allocator
3270 then
3271 Check_Non_Static_Context (Operand);
3273 -- If operand is known to raise Constraint_Error, set the flag on the
3274 -- expression so it does not get optimized away.
3276 if Nkind (Operand) = N_Raise_Constraint_Error then
3277 Set_Raises_Constraint_Error (N);
3278 end if;
3280 return;
3282 -- Also return if a semantic error has been posted on the node, as we
3283 -- don't want to fold in that case (for GNATprove, the node might lead
3284 -- to Constraint_Error but won't have been replaced with a raise node
3285 -- or marked as raising CE).
3287 elsif Error_Posted (N) then
3288 return;
3289 end if;
3291 -- If not foldable we are done
3293 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3295 if not Fold then
3296 return;
3298 -- Don't try fold if target type has Constraint_Error bounds
3300 elsif not Is_OK_Static_Subtype (Target_Type) then
3301 Set_Raises_Constraint_Error (N);
3302 return;
3303 end if;
3305 -- Fold the result of qualification
3307 if Is_Discrete_Type (Target_Type) then
3309 -- Save Print_In_Hex indication
3311 Hex := Nkind (Operand) = N_Integer_Literal
3312 and then Print_In_Hex (Operand);
3314 Fold_Uint (N, Expr_Value (Operand), Stat);
3316 -- Preserve Print_In_Hex indication
3318 if Hex and then Nkind (N) = N_Integer_Literal then
3319 Set_Print_In_Hex (N);
3320 end if;
3322 elsif Is_Real_Type (Target_Type) then
3323 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3325 else
3326 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3328 if not Stat then
3329 Set_Is_Static_Expression (N, False);
3330 else
3331 Check_String_Literal_Length (N, Target_Type);
3332 end if;
3334 return;
3335 end if;
3337 -- The expression may be foldable but not static
3339 Set_Is_Static_Expression (N, Stat);
3341 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3342 Out_Of_Range (N);
3343 end if;
3344 end Eval_Qualified_Expression;
3346 -----------------------
3347 -- Eval_Real_Literal --
3348 -----------------------
3350 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3351 -- as static by the analyzer. The reason we did it that early is to allow
3352 -- the possibility of turning off the Is_Static_Expression flag after
3353 -- analysis, but before resolution, when integer literals are generated
3354 -- in the expander that do not correspond to static expressions.
3356 procedure Eval_Real_Literal (N : Node_Id) is
3357 PK : constant Node_Kind := Nkind (Parent (N));
3359 begin
3360 -- If the literal appears in a non-expression context and not as part of
3361 -- a number declaration, then it is appearing in a non-static context,
3362 -- so check it.
3364 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3365 Check_Non_Static_Context (N);
3366 end if;
3367 end Eval_Real_Literal;
3369 ------------------------
3370 -- Eval_Relational_Op --
3371 ------------------------
3373 -- Relational operations are static functions, so the result is static if
3374 -- both operands are static (RM 4.9(7), 4.9(20)), except that up to Ada
3375 -- 2012, for strings the result is never static, even if the operands are.
3376 -- The string case was relaxed in Ada 2022, see AI12-0201.
3378 -- However, for internally generated nodes, we allow string equality and
3379 -- inequality to be static. This is because we rewrite A in "ABC" as an
3380 -- equality test A = "ABC", and the former is definitely static.
3382 procedure Eval_Relational_Op (N : Node_Id) is
3383 Left : constant Node_Id := Left_Opnd (N);
3384 Right : constant Node_Id := Right_Opnd (N);
3386 procedure Decompose_Expr
3387 (Expr : Node_Id;
3388 Ent : out Entity_Id;
3389 Kind : out Character;
3390 Cons : out Uint;
3391 Orig : Boolean := True);
3392 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3393 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3394 -- simple entity, and Cons is the value of K. If the expression is not
3395 -- of the required form, Ent is set to Empty.
3397 -- Orig indicates whether Expr is the original expression to consider,
3398 -- or if we are handling a subexpression (e.g. recursive call to
3399 -- Decompose_Expr).
3401 procedure Fold_General_Op (Is_Static : Boolean);
3402 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3403 -- be set when the operator denotes a static expression.
3405 procedure Fold_Static_Real_Op;
3406 -- Attempt to fold static real type relational operator N
3408 function Static_Length (Expr : Node_Id) return Uint;
3409 -- If Expr is an expression for a constrained array whose length is
3410 -- known at compile time, return the non-negative length, otherwise
3411 -- return -1.
3413 --------------------
3414 -- Decompose_Expr --
3415 --------------------
3417 procedure Decompose_Expr
3418 (Expr : Node_Id;
3419 Ent : out Entity_Id;
3420 Kind : out Character;
3421 Cons : out Uint;
3422 Orig : Boolean := True)
3424 Exp : Node_Id;
3426 begin
3427 -- Assume that the expression does not meet the expected form
3429 Cons := No_Uint;
3430 Ent := Empty;
3431 Kind := '?';
3433 if Nkind (Expr) = N_Op_Add
3434 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3435 then
3436 Exp := Left_Opnd (Expr);
3437 Cons := Expr_Value (Right_Opnd (Expr));
3439 elsif Nkind (Expr) = N_Op_Subtract
3440 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3441 then
3442 Exp := Left_Opnd (Expr);
3443 Cons := -Expr_Value (Right_Opnd (Expr));
3445 -- If the bound is a constant created to remove side effects, recover
3446 -- the original expression to see if it has one of the recognizable
3447 -- forms.
3449 elsif Nkind (Expr) = N_Identifier
3450 and then not Comes_From_Source (Entity (Expr))
3451 and then Ekind (Entity (Expr)) = E_Constant
3452 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3453 then
3454 Exp := Expression (Parent (Entity (Expr)));
3455 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3457 -- If original expression includes an entity, create a reference
3458 -- to it for use below.
3460 if Present (Ent) then
3461 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3462 else
3463 return;
3464 end if;
3466 else
3467 -- Only consider the case of X + 0 for a full expression, and
3468 -- not when recursing, otherwise we may end up with evaluating
3469 -- expressions not known at compile time to 0.
3471 if Orig then
3472 Exp := Expr;
3473 Cons := Uint_0;
3474 else
3475 return;
3476 end if;
3477 end if;
3479 -- At this stage Exp is set to the potential X
3481 if Nkind (Exp) = N_Attribute_Reference then
3482 if Attribute_Name (Exp) = Name_First then
3483 Kind := 'F';
3484 elsif Attribute_Name (Exp) = Name_Last then
3485 Kind := 'L';
3486 else
3487 return;
3488 end if;
3490 Exp := Prefix (Exp);
3492 else
3493 Kind := 'E';
3494 end if;
3496 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3497 Ent := Entity (Exp);
3498 end if;
3499 end Decompose_Expr;
3501 ---------------------
3502 -- Fold_General_Op --
3503 ---------------------
3505 procedure Fold_General_Op (Is_Static : Boolean) is
3506 CR : constant Compare_Result :=
3507 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3509 Result : Boolean;
3511 begin
3512 if CR = Unknown then
3513 return;
3514 end if;
3516 case Nkind (N) is
3517 when N_Op_Eq =>
3518 if CR = EQ then
3519 Result := True;
3520 elsif CR = NE or else CR = GT or else CR = LT then
3521 Result := False;
3522 else
3523 return;
3524 end if;
3526 when N_Op_Ge =>
3527 if CR = GT or else CR = EQ or else CR = GE then
3528 Result := True;
3529 elsif CR = LT then
3530 Result := False;
3531 else
3532 return;
3533 end if;
3535 when N_Op_Gt =>
3536 if CR = GT then
3537 Result := True;
3538 elsif CR = EQ or else CR = LT or else CR = LE then
3539 Result := False;
3540 else
3541 return;
3542 end if;
3544 when N_Op_Le =>
3545 if CR = LT or else CR = EQ or else CR = LE then
3546 Result := True;
3547 elsif CR = GT then
3548 Result := False;
3549 else
3550 return;
3551 end if;
3553 when N_Op_Lt =>
3554 if CR = LT then
3555 Result := True;
3556 elsif CR = EQ or else CR = GT or else CR = GE then
3557 Result := False;
3558 else
3559 return;
3560 end if;
3562 when N_Op_Ne =>
3563 if CR = NE or else CR = GT or else CR = LT then
3564 Result := True;
3565 elsif CR = EQ then
3566 Result := False;
3567 else
3568 return;
3569 end if;
3571 when others =>
3572 raise Program_Error;
3573 end case;
3575 -- Determine the potential outcome of the relation assuming the
3576 -- operands are valid and emit a warning when the relation yields
3577 -- True or False only in the presence of invalid values.
3579 Warn_On_Constant_Valid_Condition (N);
3581 Fold_Uint (N, Test (Result), Is_Static);
3582 end Fold_General_Op;
3584 -------------------------
3585 -- Fold_Static_Real_Op --
3586 -------------------------
3588 procedure Fold_Static_Real_Op is
3589 Left_Real : constant Ureal := Expr_Value_R (Left);
3590 Right_Real : constant Ureal := Expr_Value_R (Right);
3591 Result : Boolean;
3593 begin
3594 case Nkind (N) is
3595 when N_Op_Eq => Result := (Left_Real = Right_Real);
3596 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3597 when N_Op_Gt => Result := (Left_Real > Right_Real);
3598 when N_Op_Le => Result := (Left_Real <= Right_Real);
3599 when N_Op_Lt => Result := (Left_Real < Right_Real);
3600 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3601 when others => raise Program_Error;
3602 end case;
3604 Fold_Uint (N, Test (Result), True);
3605 end Fold_Static_Real_Op;
3607 -------------------
3608 -- Static_Length --
3609 -------------------
3611 function Static_Length (Expr : Node_Id) return Uint is
3612 Cons1 : Uint;
3613 Cons2 : Uint;
3614 Ent1 : Entity_Id;
3615 Ent2 : Entity_Id;
3616 Kind1 : Character;
3617 Kind2 : Character;
3618 Typ : Entity_Id;
3620 begin
3621 -- First easy case string literal
3623 if Nkind (Expr) = N_String_Literal then
3624 return UI_From_Int (String_Length (Strval (Expr)));
3626 -- With frontend inlining as performed in GNATprove mode, a variable
3627 -- may be inserted that has a string literal subtype. Deal with this
3628 -- specially as for the previous case.
3630 elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then
3631 return String_Literal_Length (Etype (Expr));
3633 -- Second easy case, not constrained subtype, so no length
3635 elsif not Is_Constrained (Etype (Expr)) then
3636 return Uint_Minus_1;
3637 end if;
3639 -- General case
3641 Typ := Etype (First_Index (Etype (Expr)));
3643 -- The simple case, both bounds are known at compile time
3645 if Is_Discrete_Type (Typ)
3646 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3647 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3648 then
3649 return
3650 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3651 Expr_Value (Type_Low_Bound (Typ)) + 1);
3652 end if;
3654 -- A more complex case, where the bounds are of the form X [+/- K1]
3655 -- .. X [+/- K2]), where X is an expression that is either A'First or
3656 -- A'Last (with A an entity name), or X is an entity name, and the
3657 -- two X's are the same and K1 and K2 are known at compile time, in
3658 -- this case, the length can also be computed at compile time, even
3659 -- though the bounds are not known. A common case of this is e.g.
3660 -- (X'First .. X'First+5).
3662 Decompose_Expr
3663 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3664 Decompose_Expr
3665 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3667 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3668 return Cons2 - Cons1 + 1;
3669 else
3670 return Uint_Minus_1;
3671 end if;
3672 end Static_Length;
3674 -- Local variables
3676 Left_Typ : constant Entity_Id := Etype (Left);
3677 Right_Typ : constant Entity_Id := Etype (Right);
3678 Fold : Boolean;
3679 Left_Len : Uint;
3680 Op_Typ : Entity_Id := Empty;
3681 Right_Len : Uint;
3683 Is_Static_Expression : Boolean;
3685 -- Start of processing for Eval_Relational_Op
3687 begin
3688 -- One special case to deal with first. If we can tell that the result
3689 -- will be false because the lengths of one or more index subtypes are
3690 -- compile-time known and different, then we can replace the entire
3691 -- result by False. We only do this for one-dimensional arrays, because
3692 -- the case of multidimensional arrays is rare and too much trouble. If
3693 -- one of the operands is an illegal aggregate, its type might still be
3694 -- an arbitrary composite type, so nothing to do.
3696 if Is_Array_Type (Left_Typ)
3697 and then Left_Typ /= Any_Composite
3698 and then Number_Dimensions (Left_Typ) = 1
3699 and then Nkind (N) in N_Op_Eq | N_Op_Ne
3700 then
3701 if Raises_Constraint_Error (Left)
3702 or else
3703 Raises_Constraint_Error (Right)
3704 then
3705 return;
3706 end if;
3708 -- OK, we have the case where we may be able to do this fold
3710 Left_Len := Static_Length (Left);
3711 Right_Len := Static_Length (Right);
3713 if Left_Len /= Uint_Minus_1
3714 and then Right_Len /= Uint_Minus_1
3715 and then Left_Len /= Right_Len
3716 then
3717 -- AI12-0201: comparison of string is static in Ada 2022
3719 Fold_Uint
3721 Test (Nkind (N) = N_Op_Ne),
3722 Static => Ada_Version >= Ada_2022
3723 and then Is_String_Type (Left_Typ));
3724 Warn_On_Known_Condition (N);
3725 return;
3726 end if;
3727 end if;
3729 -- General case
3731 -- Initialize the value of Is_Static_Expression. The value of Fold
3732 -- returned by Test_Expression_Is_Foldable is not needed since, even
3733 -- when some operand is a variable, we can still perform the static
3734 -- evaluation of the expression in some cases (for example, for a
3735 -- variable of a subtype of Integer we statically know that any value
3736 -- stored in such variable is smaller than Integer'Last).
3738 Test_Expression_Is_Foldable
3739 (N, Left, Right, Is_Static_Expression, Fold);
3741 -- Comparisons of scalars can give static results.
3742 -- In addition starting with Ada 2022 (AI12-0201), comparison of strings
3743 -- can also give static results, and as noted above, we also allow for
3744 -- earlier Ada versions internally generated equality and inequality for
3745 -- strings.
3746 -- The Comes_From_Source test below isn't correct and will accept
3747 -- some cases that are illegal in Ada 2012 and before. Now that Ada
3748 -- 2022 has relaxed the rules, this doesn't really matter.
3750 if Is_String_Type (Left_Typ) then
3751 if Ada_Version < Ada_2022
3752 and then (Comes_From_Source (N)
3753 or else Nkind (N) not in N_Op_Eq | N_Op_Ne)
3754 then
3755 Is_Static_Expression := False;
3756 Set_Is_Static_Expression (N, False);
3757 end if;
3759 elsif not Is_Scalar_Type (Left_Typ) then
3760 Is_Static_Expression := False;
3761 Set_Is_Static_Expression (N, False);
3762 end if;
3764 -- For operators on universal numeric types called as functions with an
3765 -- explicit scope, determine appropriate specific numeric type, and
3766 -- diagnose possible ambiguity.
3768 if Is_Universal_Numeric_Type (Left_Typ)
3769 and then
3770 Is_Universal_Numeric_Type (Right_Typ)
3771 then
3772 Op_Typ := Find_Universal_Operator_Type (N);
3773 end if;
3775 -- Attempt to fold the relational operator
3777 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3778 Fold_Static_Real_Op;
3779 else
3780 Fold_General_Op (Is_Static_Expression);
3781 end if;
3783 -- For the case of a folded relational operator on a specific numeric
3784 -- type, freeze the operand type now.
3786 if Present (Op_Typ) then
3787 Freeze_Before (N, Op_Typ);
3788 end if;
3790 Warn_On_Known_Condition (N);
3791 end Eval_Relational_Op;
3793 -----------------------------
3794 -- Eval_Selected_Component --
3795 -----------------------------
3797 procedure Eval_Selected_Component (N : Node_Id) is
3798 Node : Node_Id;
3799 Comp : Node_Id;
3800 C : Node_Id;
3801 Nam : Name_Id;
3803 begin
3804 -- If an attribute reference or a LHS, nothing to do.
3805 -- Also do not fold if N is an [in] out subprogram parameter.
3806 -- Fold will perform the other relevant tests.
3808 if Nkind (Parent (N)) /= N_Attribute_Reference
3809 and then not Known_To_Be_Assigned (N)
3810 and then not Is_Actual_Out_Or_In_Out_Parameter (N)
3811 then
3812 -- Simplify a selected_component on an aggregate by extracting
3813 -- the field directly.
3815 Node := Unqualify (Prefix (N));
3817 if Nkind (Node) = N_Aggregate
3818 and then Compile_Time_Known_Aggregate (Node)
3819 then
3820 Comp := First (Component_Associations (Node));
3821 Nam := Chars (Selector_Name (N));
3823 while Present (Comp) loop
3824 C := First (Choices (Comp));
3826 while Present (C) loop
3827 if Chars (C) = Nam then
3828 Rewrite (N, Relocate_Node (Expression (Comp)));
3829 return;
3830 end if;
3832 Next (C);
3833 end loop;
3835 Next (Comp);
3836 end loop;
3837 else
3838 Fold (N);
3839 end if;
3840 end if;
3841 end Eval_Selected_Component;
3843 ----------------
3844 -- Eval_Shift --
3845 ----------------
3847 procedure Eval_Shift (N : Node_Id) is
3848 begin
3849 -- This procedure is only called for compiler generated code (e.g.
3850 -- packed arrays), so there is nothing to do except attempting to fold
3851 -- the expression.
3853 Fold_Shift (N, Left_Opnd (N), Right_Opnd (N), Nkind (N));
3854 end Eval_Shift;
3856 ------------------------
3857 -- Eval_Short_Circuit --
3858 ------------------------
3860 -- A short circuit operation is potentially static if both operands are
3861 -- potentially static (RM 4.9 (13)).
3863 procedure Eval_Short_Circuit (N : Node_Id) is
3864 Kind : constant Node_Kind := Nkind (N);
3865 Left : constant Node_Id := Left_Opnd (N);
3866 Right : constant Node_Id := Right_Opnd (N);
3867 Left_Int : Uint;
3869 Rstat : constant Boolean :=
3870 Is_Static_Expression (Left)
3871 and then
3872 Is_Static_Expression (Right);
3874 begin
3875 -- Short circuit operations are never static in Ada 83
3877 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3878 Check_Non_Static_Context (Left);
3879 Check_Non_Static_Context (Right);
3880 return;
3881 end if;
3883 -- Now look at the operands, we can't quite use the normal call to
3884 -- Test_Expression_Is_Foldable here because short circuit operations
3885 -- are a special case, they can still be foldable, even if the right
3886 -- operand raises Constraint_Error.
3888 -- If either operand is Any_Type, just propagate to result and do not
3889 -- try to fold, this prevents cascaded errors.
3891 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3892 Set_Etype (N, Any_Type);
3893 return;
3895 -- If left operand raises Constraint_Error, then replace node N with
3896 -- the raise Constraint_Error node, and we are obviously not foldable.
3897 -- Is_Static_Expression is set from the two operands in the normal way,
3898 -- and we check the right operand if it is in a non-static context.
3900 elsif Raises_Constraint_Error (Left) then
3901 if not Rstat then
3902 Check_Non_Static_Context (Right);
3903 end if;
3905 Rewrite_In_Raise_CE (N, Left);
3906 Set_Is_Static_Expression (N, Rstat);
3907 return;
3909 -- If the result is not static, then we won't in any case fold
3911 elsif not Rstat then
3912 Check_Non_Static_Context (Left);
3913 Check_Non_Static_Context (Right);
3914 return;
3915 end if;
3917 -- Here the result is static, note that, unlike the normal processing
3918 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3919 -- the right operand raises Constraint_Error, that's because it is not
3920 -- significant if the left operand is decisive.
3922 Set_Is_Static_Expression (N);
3924 -- It does not matter if the right operand raises Constraint_Error if
3925 -- it will not be evaluated. So deal specially with the cases where
3926 -- the right operand is not evaluated. Note that we will fold these
3927 -- cases even if the right operand is non-static, which is fine, but
3928 -- of course in these cases the result is not potentially static.
3930 Left_Int := Expr_Value (Left);
3932 if (Kind = N_And_Then and then Is_False (Left_Int))
3933 or else
3934 (Kind = N_Or_Else and then Is_True (Left_Int))
3935 then
3936 Fold_Uint (N, Left_Int, Rstat);
3937 return;
3938 end if;
3940 -- If first operand not decisive, then it does matter if the right
3941 -- operand raises Constraint_Error, since it will be evaluated, so
3942 -- we simply replace the node with the right operand. Note that this
3943 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3944 -- (both are set to True in Right).
3946 if Raises_Constraint_Error (Right) then
3947 Rewrite_In_Raise_CE (N, Right);
3948 Check_Non_Static_Context (Left);
3949 return;
3950 end if;
3952 -- Otherwise the result depends on the right operand
3954 Fold_Uint (N, Expr_Value (Right), Rstat);
3955 return;
3956 end Eval_Short_Circuit;
3958 ----------------
3959 -- Eval_Slice --
3960 ----------------
3962 -- Slices can never be static, so the only processing required is to check
3963 -- for non-static context if an explicit range is given.
3965 procedure Eval_Slice (N : Node_Id) is
3966 Drange : constant Node_Id := Discrete_Range (N);
3967 Name : constant Node_Id := Prefix (N);
3969 begin
3970 if Nkind (Drange) = N_Range then
3971 Check_Non_Static_Context (Low_Bound (Drange));
3972 Check_Non_Static_Context (High_Bound (Drange));
3973 end if;
3975 -- A slice of the form A (subtype), when the subtype is the index of
3976 -- the type of A, is redundant, the slice can be replaced with A, and
3977 -- this is worth a warning.
3979 if Is_Entity_Name (Name) then
3980 declare
3981 E : constant Entity_Id := Entity (Name);
3982 T : constant Entity_Id := Etype (E);
3984 begin
3985 if Is_Object (E)
3986 and then Is_Array_Type (T)
3987 and then Is_Entity_Name (Drange)
3988 then
3989 if Is_Entity_Name (Original_Node (First_Index (T)))
3990 and then Entity (Original_Node (First_Index (T)))
3991 = Entity (Drange)
3992 then
3993 if Warn_On_Redundant_Constructs then
3994 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3995 end if;
3997 -- The following might be a useful optimization???
3999 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
4000 end if;
4001 end if;
4002 end;
4003 end if;
4004 end Eval_Slice;
4006 -------------------------
4007 -- Eval_String_Literal --
4008 -------------------------
4010 procedure Eval_String_Literal (N : Node_Id) is
4011 Typ : constant Entity_Id := Etype (N);
4012 Bas : constant Entity_Id := Base_Type (Typ);
4013 Xtp : Entity_Id;
4014 Len : Nat;
4015 Lo : Node_Id;
4017 begin
4018 -- Nothing to do if error type (handles cases like default expressions
4019 -- or generics where we have not yet fully resolved the type).
4021 if Bas = Any_Type or else Bas = Any_String then
4022 return;
4023 end if;
4025 -- String literals are static if the subtype is static (RM 4.9(2)), so
4026 -- reset the static expression flag (it was set unconditionally in
4027 -- Analyze_String_Literal) if the subtype is non-static. We tell if
4028 -- the subtype is static by looking at the lower bound.
4030 if Ekind (Typ) = E_String_Literal_Subtype then
4031 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
4032 Set_Is_Static_Expression (N, False);
4033 return;
4034 end if;
4036 -- Here if Etype of string literal is normal Etype (not yet possible,
4037 -- but may be possible in future).
4039 elsif not Is_OK_Static_Expression
4040 (Type_Low_Bound (Etype (First_Index (Typ))))
4041 then
4042 Set_Is_Static_Expression (N, False);
4043 return;
4044 end if;
4046 -- If original node was a type conversion, then result if non-static
4047 -- up to Ada 2012. AI12-0201 changes that with Ada 2022.
4049 if Nkind (Original_Node (N)) = N_Type_Conversion
4050 and then Ada_Version <= Ada_2012
4051 then
4052 Set_Is_Static_Expression (N, False);
4053 return;
4054 end if;
4056 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
4057 -- if its bounds are outside the index base type and this index type is
4058 -- static. This can happen in only two ways. Either the string literal
4059 -- is too long, or it is null, and the lower bound is type'First. Either
4060 -- way it is the upper bound that is out of range of the index type.
4062 if Ada_Version >= Ada_95 then
4063 if Is_Standard_String_Type (Bas) then
4064 Xtp := Standard_Positive;
4065 else
4066 Xtp := Etype (First_Index (Bas));
4067 end if;
4069 if Ekind (Typ) = E_String_Literal_Subtype then
4070 Lo := String_Literal_Low_Bound (Typ);
4071 else
4072 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
4073 end if;
4075 -- Check for string too long
4077 Len := String_Length (Strval (N));
4079 if Len > String_Type_Len (Bas) then
4081 -- Issue message. Note that this message is a warning if the
4082 -- string literal is not marked as static (happens in some cases
4083 -- of folding strings known at compile time, but not static).
4084 -- Furthermore in such cases, we reword the message, since there
4085 -- is no string literal in the source program.
4087 if Is_Static_Expression (N) then
4088 Apply_Compile_Time_Constraint_Error
4089 (N, "string literal too long for}", CE_Length_Check_Failed,
4090 Ent => Bas,
4091 Typ => First_Subtype (Bas));
4092 else
4093 Apply_Compile_Time_Constraint_Error
4094 (N, "string value too long for}", CE_Length_Check_Failed,
4095 Ent => Bas,
4096 Typ => First_Subtype (Bas),
4097 Warn => True);
4098 end if;
4100 -- Test for null string not allowed
4102 elsif Len = 0
4103 and then not Is_Generic_Type (Xtp)
4104 and then
4105 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
4106 then
4107 -- Same specialization of message
4109 if Is_Static_Expression (N) then
4110 Apply_Compile_Time_Constraint_Error
4111 (N, "null string literal not allowed for}",
4112 CE_Length_Check_Failed,
4113 Ent => Bas,
4114 Typ => First_Subtype (Bas));
4115 else
4116 Apply_Compile_Time_Constraint_Error
4117 (N, "null string value not allowed for}",
4118 CE_Length_Check_Failed,
4119 Ent => Bas,
4120 Typ => First_Subtype (Bas),
4121 Warn => True);
4122 end if;
4123 end if;
4124 end if;
4125 end Eval_String_Literal;
4127 --------------------------
4128 -- Eval_Type_Conversion --
4129 --------------------------
4131 -- A type conversion is potentially static if its subtype mark is for a
4132 -- static scalar subtype, and its operand expression is potentially static
4133 -- (RM 4.9(10)).
4134 -- Also add support for static string types.
4136 procedure Eval_Type_Conversion (N : Node_Id) is
4137 Operand : constant Node_Id := Expression (N);
4138 Source_Type : constant Entity_Id := Etype (Operand);
4139 Target_Type : constant Entity_Id := Etype (N);
4141 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
4142 -- Returns true if type T is an integer type, or if it is a fixed-point
4143 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
4144 -- on the conversion node).
4146 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
4147 -- Returns true if type T is a floating-point type, or if it is a
4148 -- fixed-point type that is not to be treated as an integer (i.e. the
4149 -- flag Conversion_OK is not set on the conversion node).
4151 ------------------------------
4152 -- To_Be_Treated_As_Integer --
4153 ------------------------------
4155 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
4156 begin
4157 return
4158 Is_Integer_Type (T)
4159 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
4160 end To_Be_Treated_As_Integer;
4162 ---------------------------
4163 -- To_Be_Treated_As_Real --
4164 ---------------------------
4166 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
4167 begin
4168 return
4169 Is_Floating_Point_Type (T)
4170 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
4171 end To_Be_Treated_As_Real;
4173 -- Local variables
4175 Fold : Boolean;
4176 Stat : Boolean;
4178 -- Start of processing for Eval_Type_Conversion
4180 begin
4181 -- Cannot fold if target type is non-static or if semantic error
4183 if not Is_Static_Subtype (Target_Type) then
4184 Check_Non_Static_Context (Operand);
4185 return;
4186 elsif Error_Posted (N) then
4187 return;
4188 end if;
4190 -- If not foldable we are done
4192 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
4194 if not Fold then
4195 return;
4197 -- Don't try fold if target type has Constraint_Error bounds
4199 elsif not Is_OK_Static_Subtype (Target_Type) then
4200 Set_Raises_Constraint_Error (N);
4201 return;
4202 end if;
4204 -- Remaining processing depends on operand types. Note that in the
4205 -- following type test, fixed-point counts as real unless the flag
4206 -- Conversion_OK is set, in which case it counts as integer.
4208 -- Fold conversion, case of string type. The result is static starting
4209 -- with Ada 2022 (AI12-0201).
4211 if Is_String_Type (Target_Type) then
4212 Fold_Str
4214 Strval (Get_String_Val (Operand)),
4215 Static => Ada_Version >= Ada_2022);
4216 return;
4218 -- Fold conversion, case of integer target type
4220 elsif To_Be_Treated_As_Integer (Target_Type) then
4221 declare
4222 Result : Uint;
4224 begin
4225 -- Integer to integer conversion
4227 if To_Be_Treated_As_Integer (Source_Type) then
4228 Result := Expr_Value (Operand);
4230 -- Real to integer conversion
4232 elsif To_Be_Treated_As_Real (Source_Type) then
4233 Result := UR_To_Uint (Expr_Value_R (Operand));
4235 -- Enumeration to integer conversion, aka 'Enum_Rep
4237 else
4238 Result := Expr_Rep_Value (Operand);
4239 end if;
4241 -- If fixed-point type (Conversion_OK must be set), then the
4242 -- result is logically an integer, but we must replace the
4243 -- conversion with the corresponding real literal, since the
4244 -- type from a semantic point of view is still fixed-point.
4246 if Is_Fixed_Point_Type (Target_Type) then
4247 Fold_Ureal
4248 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
4250 -- Otherwise result is integer literal
4252 else
4253 Fold_Uint (N, Result, Stat);
4254 end if;
4255 end;
4257 -- Fold conversion, case of real target type
4259 elsif To_Be_Treated_As_Real (Target_Type) then
4260 declare
4261 Result : Ureal;
4263 begin
4264 if To_Be_Treated_As_Real (Source_Type) then
4265 Result := Expr_Value_R (Operand);
4266 else
4267 Result := UR_From_Uint (Expr_Value (Operand));
4268 end if;
4270 Fold_Ureal (N, Result, Stat);
4271 end;
4273 -- Enumeration types
4275 else
4276 Fold_Uint (N, Expr_Value (Operand), Stat);
4277 end if;
4279 -- If the target is a static floating-point subtype, then its bounds
4280 -- are machine numbers so we must consider the machine-rounded value.
4282 if Is_Floating_Point_Type (Target_Type)
4283 and then Nkind (N) = N_Real_Literal
4284 and then not Is_Machine_Number (N)
4285 then
4286 declare
4287 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
4288 Hi : constant Node_Id := Type_High_Bound (Target_Type);
4289 Valr : constant Ureal :=
4290 Machine_Number (Target_Type, Expr_Value_R (N), N);
4291 begin
4292 if Valr < Expr_Value_R (Lo) or else Valr > Expr_Value_R (Hi) then
4293 Out_Of_Range (N);
4294 end if;
4295 end;
4297 elsif Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
4298 Out_Of_Range (N);
4299 end if;
4300 end Eval_Type_Conversion;
4302 -------------------
4303 -- Eval_Unary_Op --
4304 -------------------
4306 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4307 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4309 procedure Eval_Unary_Op (N : Node_Id) is
4310 Right : constant Node_Id := Right_Opnd (N);
4311 Otype : Entity_Id := Empty;
4312 Stat : Boolean;
4313 Fold : Boolean;
4315 begin
4316 -- If not foldable we are done
4318 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
4320 if not Fold then
4321 return;
4322 end if;
4324 if Is_Universal_Numeric_Type (Etype (Right)) then
4325 Otype := Find_Universal_Operator_Type (N);
4326 end if;
4328 -- Fold for integer case
4330 if Is_Integer_Type (Etype (N)) then
4331 declare
4332 Rint : constant Uint := Expr_Value (Right);
4333 Result : Uint;
4335 begin
4336 -- In the case of modular unary plus and abs there is no need
4337 -- to adjust the result of the operation since if the original
4338 -- operand was in bounds the result will be in the bounds of the
4339 -- modular type. However, in the case of modular unary minus the
4340 -- result may go out of the bounds of the modular type and needs
4341 -- adjustment.
4343 if Nkind (N) = N_Op_Plus then
4344 Result := Rint;
4346 elsif Nkind (N) = N_Op_Minus then
4347 if Is_Modular_Integer_Type (Etype (N)) then
4348 Result := (-Rint) mod Modulus (Etype (N));
4349 else
4350 Result := (-Rint);
4351 end if;
4353 else
4354 pragma Assert (Nkind (N) = N_Op_Abs);
4355 Result := abs Rint;
4356 end if;
4358 Check_Non_Static_Context_For_Overflow (N, Stat, Result);
4360 Fold_Uint (N, Result, Stat);
4361 end;
4363 -- Fold for real case
4365 elsif Is_Real_Type (Etype (N)) then
4366 declare
4367 Rreal : constant Ureal := Expr_Value_R (Right);
4368 Result : Ureal;
4370 begin
4371 if Nkind (N) = N_Op_Plus then
4372 Result := Rreal;
4373 elsif Nkind (N) = N_Op_Minus then
4374 Result := UR_Negate (Rreal);
4375 else
4376 pragma Assert (Nkind (N) = N_Op_Abs);
4377 Result := abs Rreal;
4378 end if;
4380 Fold_Ureal (N, Result, Stat);
4381 end;
4382 end if;
4384 -- If the operator was resolved to a specific type, make sure that type
4385 -- is frozen even if the expression is folded into a literal (which has
4386 -- a universal type).
4388 if Present (Otype) then
4389 Freeze_Before (N, Otype);
4390 end if;
4391 end Eval_Unary_Op;
4393 -------------------------------
4394 -- Eval_Unchecked_Conversion --
4395 -------------------------------
4397 -- Unchecked conversions can never be static, so the only required
4398 -- processing is to check for a non-static context for the operand.
4400 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4401 Target_Type : constant Entity_Id := Etype (N);
4402 Operand : constant Node_Id := Expression (N);
4403 Operand_Type : constant Entity_Id := Etype (Operand);
4405 begin
4406 Check_Non_Static_Context (Operand);
4408 -- If we have a conversion of a compile time known value to a target
4409 -- type and the value is in range of the target type, then we can simply
4410 -- replace the construct by an integer literal of the correct type. We
4411 -- only apply this to discrete types being converted. Possibly it may
4412 -- apply in other cases, but it is too much trouble to worry about.
4414 -- Note that we do not do this transformation if the Kill_Range_Check
4415 -- flag is set, since then the value may be outside the expected range.
4416 -- This happens in the Normalize_Scalars case.
4418 -- We also skip this if either the target or operand type is biased
4419 -- because in this case, the unchecked conversion is supposed to
4420 -- preserve the bit pattern, not the integer value.
4422 if Is_Integer_Type (Target_Type)
4423 and then not Has_Biased_Representation (Target_Type)
4424 and then Is_Discrete_Type (Operand_Type)
4425 and then not Has_Biased_Representation (Operand_Type)
4426 and then Compile_Time_Known_Value (Operand)
4427 and then not Kill_Range_Check (N)
4428 then
4429 declare
4430 Val : constant Uint := Expr_Rep_Value (Operand);
4432 begin
4433 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
4434 and then
4435 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
4436 and then
4437 Val >= Expr_Value (Type_Low_Bound (Target_Type))
4438 and then
4439 Val <= Expr_Value (Type_High_Bound (Target_Type))
4440 then
4441 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
4443 -- If Address is the target type, just set the type to avoid a
4444 -- spurious type error on the literal when Address is a visible
4445 -- integer type.
4447 if Is_Descendant_Of_Address (Target_Type) then
4448 Set_Etype (N, Target_Type);
4449 else
4450 Analyze_And_Resolve (N, Target_Type);
4451 end if;
4453 return;
4454 end if;
4455 end;
4456 end if;
4457 end Eval_Unchecked_Conversion;
4459 --------------------
4460 -- Expr_Rep_Value --
4461 --------------------
4463 function Expr_Rep_Value (N : Node_Id) return Uint is
4464 Kind : constant Node_Kind := Nkind (N);
4465 Ent : Entity_Id;
4467 begin
4468 if Is_Entity_Name (N) then
4469 Ent := Entity (N);
4471 -- An enumeration literal that was either in the source or created
4472 -- as a result of static evaluation.
4474 if Ekind (Ent) = E_Enumeration_Literal then
4475 return Enumeration_Rep (Ent);
4477 -- A user defined static constant
4479 else
4480 pragma Assert (Ekind (Ent) = E_Constant);
4481 return Expr_Rep_Value (Constant_Value (Ent));
4482 end if;
4484 -- An integer literal that was either in the source or created as a
4485 -- result of static evaluation.
4487 elsif Kind = N_Integer_Literal then
4488 return Intval (N);
4490 -- A real literal for a fixed-point type. This must be the fixed-point
4491 -- case, either the literal is of a fixed-point type, or it is a bound
4492 -- of a fixed-point type, with type universal real. In either case we
4493 -- obtain the desired value from Corresponding_Integer_Value.
4495 elsif Kind = N_Real_Literal then
4496 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4497 return Corresponding_Integer_Value (N);
4499 -- The NULL access value
4501 elsif Kind = N_Null then
4502 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4503 or else Error_Posted (N));
4504 return Uint_0;
4506 -- Character literal
4508 elsif Kind = N_Character_Literal then
4509 Ent := Entity (N);
4511 -- Since Character literals of type Standard.Character don't have any
4512 -- defining character literals built for them, they do not have their
4513 -- Entity set, so just use their Char code. Otherwise for user-
4514 -- defined character literals use their Pos value as usual which is
4515 -- the same as the Rep value.
4517 if No (Ent) then
4518 return Char_Literal_Value (N);
4519 else
4520 return Enumeration_Rep (Ent);
4521 end if;
4523 -- Unchecked conversion, which can come from System'To_Address (X)
4524 -- where X is a static integer expression. Recursively evaluate X.
4526 elsif Kind = N_Unchecked_Type_Conversion then
4527 return Expr_Rep_Value (Expression (N));
4529 -- Static discriminant value
4531 elsif Is_Static_Discriminant_Component (N) then
4532 return Expr_Rep_Value
4533 (Get_Discriminant_Value
4534 (Entity (Selector_Name (N)),
4535 Etype (Prefix (N)),
4536 Discriminant_Constraint (Etype (Prefix (N)))));
4538 else
4539 raise Program_Error;
4540 end if;
4541 end Expr_Rep_Value;
4543 ----------------
4544 -- Expr_Value --
4545 ----------------
4547 function Expr_Value (N : Node_Id) return Uint is
4548 Kind : constant Node_Kind := Nkind (N);
4549 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4550 Ent : Entity_Id;
4551 Val : Uint;
4553 begin
4554 -- If already in cache, then we know it's compile-time-known and we can
4555 -- return the value that was previously stored in the cache since
4556 -- compile-time-known values cannot change.
4558 if CV_Ent.N = N then
4559 return CV_Ent.V;
4560 end if;
4562 -- Otherwise proceed to test value
4564 if Is_Entity_Name (N) then
4565 Ent := Entity (N);
4567 -- An enumeration literal that was either in the source or created as
4568 -- a result of static evaluation.
4570 if Ekind (Ent) = E_Enumeration_Literal then
4571 Val := Enumeration_Pos (Ent);
4573 -- A user defined static constant
4575 else
4576 pragma Assert (Ekind (Ent) = E_Constant);
4577 Val := Expr_Value (Constant_Value (Ent));
4578 end if;
4580 -- An integer literal that was either in the source or created as a
4581 -- result of static evaluation.
4583 elsif Kind = N_Integer_Literal then
4584 Val := Intval (N);
4586 -- A real literal for a fixed-point type. This must be the fixed-point
4587 -- case, either the literal is of a fixed-point type, or it is a bound
4588 -- of a fixed-point type, with type universal real. In either case we
4589 -- obtain the desired value from Corresponding_Integer_Value.
4591 elsif Kind = N_Real_Literal then
4592 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4593 Val := Corresponding_Integer_Value (N);
4595 -- The NULL access value
4597 elsif Kind = N_Null then
4598 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N)))
4599 or else Error_Posted (N));
4600 Val := Uint_0;
4602 -- Character literal
4604 elsif Kind = N_Character_Literal then
4605 Ent := Entity (N);
4607 -- Since Character literals of type Standard.Character don't
4608 -- have any defining character literals built for them, they
4609 -- do not have their Entity set, so just use their Char
4610 -- code. Otherwise for user-defined character literals use
4611 -- their Pos value as usual.
4613 if No (Ent) then
4614 Val := Char_Literal_Value (N);
4615 else
4616 Val := Enumeration_Pos (Ent);
4617 end if;
4619 -- Unchecked conversion, which can come from System'To_Address (X)
4620 -- where X is a static integer expression. Recursively evaluate X.
4622 elsif Kind = N_Unchecked_Type_Conversion then
4623 Val := Expr_Value (Expression (N));
4625 -- Static discriminant value
4627 elsif Is_Static_Discriminant_Component (N) then
4628 Val := Expr_Value
4629 (Get_Discriminant_Value
4630 (Entity (Selector_Name (N)),
4631 Etype (Prefix (N)),
4632 Discriminant_Constraint (Etype (Prefix (N)))));
4634 else
4635 raise Program_Error;
4636 end if;
4638 -- Come here with Val set to value to be returned, set cache
4640 CV_Ent.N := N;
4641 CV_Ent.V := Val;
4642 return Val;
4643 end Expr_Value;
4645 ------------------
4646 -- Expr_Value_E --
4647 ------------------
4649 function Expr_Value_E (N : Node_Id) return Entity_Id is
4650 Ent : constant Entity_Id := Entity (N);
4651 begin
4652 if Ekind (Ent) = E_Enumeration_Literal then
4653 return Ent;
4654 else
4655 pragma Assert (Ekind (Ent) = E_Constant);
4657 -- We may be dealing with a enumerated character type constant, so
4658 -- handle that case here.
4660 if Nkind (Constant_Value (Ent)) = N_Character_Literal then
4661 return Ent;
4662 else
4663 return Expr_Value_E (Constant_Value (Ent));
4664 end if;
4665 end if;
4666 end Expr_Value_E;
4668 ------------------
4669 -- Expr_Value_R --
4670 ------------------
4672 function Expr_Value_R (N : Node_Id) return Ureal is
4673 Kind : constant Node_Kind := Nkind (N);
4674 Ent : Entity_Id;
4676 begin
4677 if Kind = N_Real_Literal then
4678 return Realval (N);
4680 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4681 Ent := Entity (N);
4682 pragma Assert (Ekind (Ent) = E_Constant);
4683 return Expr_Value_R (Constant_Value (Ent));
4685 elsif Kind = N_Integer_Literal then
4686 return UR_From_Uint (Expr_Value (N));
4688 -- Here, we have a node that cannot be interpreted as a compile time
4689 -- constant. That is definitely an error.
4691 else
4692 raise Program_Error;
4693 end if;
4694 end Expr_Value_R;
4696 ------------------
4697 -- Expr_Value_S --
4698 ------------------
4700 function Expr_Value_S (N : Node_Id) return Node_Id is
4701 begin
4702 if Nkind (N) = N_String_Literal then
4703 return N;
4704 else
4705 pragma Assert (Ekind (Entity (N)) = E_Constant);
4706 return Expr_Value_S (Constant_Value (Entity (N)));
4707 end if;
4708 end Expr_Value_S;
4710 ----------------------------------
4711 -- Find_Universal_Operator_Type --
4712 ----------------------------------
4714 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4715 PN : constant Node_Id := Parent (N);
4716 Call : constant Node_Id := Original_Node (N);
4717 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4719 Is_Fix : constant Boolean :=
4720 Nkind (N) in N_Binary_Op
4721 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4722 -- A mixed-mode operation in this context indicates the presence of
4723 -- fixed-point type in the designated package.
4725 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4726 -- Case where N is a relational (or membership) operator (else it is an
4727 -- arithmetic one).
4729 In_Membership : constant Boolean :=
4730 Nkind (PN) in N_Membership_Test
4731 and then
4732 Nkind (Right_Opnd (PN)) = N_Range
4733 and then
4734 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4735 and then
4736 Is_Universal_Numeric_Type
4737 (Etype (Low_Bound (Right_Opnd (PN))))
4738 and then
4739 Is_Universal_Numeric_Type
4740 (Etype (High_Bound (Right_Opnd (PN))));
4741 -- Case where N is part of a membership test with a universal range
4743 E : Entity_Id;
4744 Pack : Entity_Id;
4745 Typ1 : Entity_Id := Empty;
4746 Priv_E : Entity_Id;
4748 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4749 -- Check whether one operand is a mixed-mode operation that requires the
4750 -- presence of a fixed-point type. Given that all operands are universal
4751 -- and have been constant-folded, retrieve the original function call.
4753 ---------------------------
4754 -- Is_Mixed_Mode_Operand --
4755 ---------------------------
4757 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4758 Onod : constant Node_Id := Original_Node (Op);
4759 begin
4760 return Nkind (Onod) = N_Function_Call
4761 and then Present (Next_Actual (First_Actual (Onod)))
4762 and then Etype (First_Actual (Onod)) /=
4763 Etype (Next_Actual (First_Actual (Onod)));
4764 end Is_Mixed_Mode_Operand;
4766 -- Start of processing for Find_Universal_Operator_Type
4768 begin
4769 if Nkind (Call) /= N_Function_Call
4770 or else Nkind (Name (Call)) /= N_Expanded_Name
4771 then
4772 return Empty;
4774 -- There are several cases where the context does not imply the type of
4775 -- the operands:
4776 -- - the universal expression appears in a type conversion;
4777 -- - the expression is a relational operator applied to universal
4778 -- operands;
4779 -- - the expression is a membership test with a universal operand
4780 -- and a range with universal bounds.
4782 elsif Nkind (Parent (N)) = N_Type_Conversion
4783 or else Is_Relational
4784 or else In_Membership
4785 then
4786 Pack := Entity (Prefix (Name (Call)));
4788 -- If the prefix is a package declared elsewhere, iterate over its
4789 -- visible entities, otherwise iterate over all declarations in the
4790 -- designated scope.
4792 if Ekind (Pack) = E_Package
4793 and then not In_Open_Scopes (Pack)
4794 then
4795 Priv_E := First_Private_Entity (Pack);
4796 else
4797 Priv_E := Empty;
4798 end if;
4800 Typ1 := Empty;
4801 E := First_Entity (Pack);
4802 while Present (E) and then E /= Priv_E loop
4803 if Is_Numeric_Type (E)
4804 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4805 and then Comes_From_Source (E)
4806 and then Is_Integer_Type (E) = Is_Int
4807 and then (Nkind (N) in N_Unary_Op
4808 or else Is_Relational
4809 or else Is_Fixed_Point_Type (E) = Is_Fix)
4810 then
4811 if No (Typ1) then
4812 Typ1 := E;
4814 -- Before emitting an error, check for the presence of a
4815 -- mixed-mode operation that specifies a fixed point type.
4817 elsif Is_Relational
4818 and then
4819 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4820 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4821 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4823 then
4824 if Is_Fixed_Point_Type (E) then
4825 Typ1 := E;
4826 end if;
4828 else
4829 -- More than one type of the proper class declared in P
4831 Error_Msg_N ("ambiguous operation", N);
4832 Error_Msg_Sloc := Sloc (Typ1);
4833 Error_Msg_N ("\possible interpretation (inherited)#", N);
4834 Error_Msg_Sloc := Sloc (E);
4835 Error_Msg_N ("\possible interpretation (inherited)#", N);
4836 return Empty;
4837 end if;
4838 end if;
4840 Next_Entity (E);
4841 end loop;
4842 end if;
4844 return Typ1;
4845 end Find_Universal_Operator_Type;
4847 --------------------------
4848 -- Flag_Non_Static_Expr --
4849 --------------------------
4851 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4852 begin
4853 if Error_Posted (Expr) and then not All_Errors_Mode then
4854 return;
4855 else
4856 Error_Msg_F (Msg, Expr);
4857 Why_Not_Static (Expr);
4858 end if;
4859 end Flag_Non_Static_Expr;
4861 ----------
4862 -- Fold --
4863 ----------
4865 procedure Fold (N : Node_Id) is
4866 Typ : constant Entity_Id := Etype (N);
4867 begin
4868 -- If not known at compile time or if already a literal, nothing to do
4870 if Nkind (N) in N_Numeric_Or_String_Literal
4871 or else not Compile_Time_Known_Value (N)
4872 then
4873 null;
4875 elsif Is_Discrete_Type (Typ) then
4876 Fold_Uint (N, Expr_Value (N), Static => Is_Static_Expression (N));
4878 elsif Is_Real_Type (Typ) then
4879 Fold_Ureal (N, Expr_Value_R (N), Static => Is_Static_Expression (N));
4881 elsif Is_String_Type (Typ) then
4882 Fold_Str
4883 (N, Strval (Expr_Value_S (N)), Static => Is_Static_Expression (N));
4884 end if;
4885 end Fold;
4887 ----------------
4888 -- Fold_Dummy --
4889 ----------------
4891 procedure Fold_Dummy (N : Node_Id; Typ : Entity_Id) is
4892 begin
4893 if Is_Integer_Type (Typ) then
4894 Fold_Uint (N, Uint_1, Static => True);
4896 elsif Is_Real_Type (Typ) then
4897 Fold_Ureal (N, Ureal_1, Static => True);
4899 elsif Is_Enumeration_Type (Typ) then
4900 Fold_Uint
4902 Expr_Value (Type_Low_Bound (Base_Type (Typ))),
4903 Static => True);
4905 elsif Is_String_Type (Typ) then
4906 Fold_Str
4908 Strval (Make_String_Literal (Sloc (N), "")),
4909 Static => True);
4910 end if;
4911 end Fold_Dummy;
4913 ----------------
4914 -- Fold_Shift --
4915 ----------------
4917 procedure Fold_Shift
4918 (N : Node_Id;
4919 Left : Node_Id;
4920 Right : Node_Id;
4921 Op : Node_Kind;
4922 Static : Boolean := False;
4923 Check_Elab : Boolean := False)
4925 Typ : constant Entity_Id := Base_Type (Etype (Left));
4927 procedure Check_Elab_Call;
4928 -- Add checks related to calls in elaboration code
4930 ---------------------
4931 -- Check_Elab_Call --
4932 ---------------------
4934 procedure Check_Elab_Call is
4935 begin
4936 if Check_Elab then
4937 if Legacy_Elaboration_Checks then
4938 Check_Elab_Call (N);
4939 end if;
4941 Build_Call_Marker (N);
4942 end if;
4943 end Check_Elab_Call;
4945 Modulus, Val : Uint;
4947 begin
4948 if Compile_Time_Known_Value (Left)
4949 and then Compile_Time_Known_Value (Right)
4950 then
4951 pragma Assert (not Non_Binary_Modulus (Typ));
4953 if Op = N_Op_Shift_Left then
4954 Check_Elab_Call;
4956 if Is_Modular_Integer_Type (Typ) then
4957 Modulus := Einfo.Entities.Modulus (Typ);
4958 else
4959 Modulus := Uint_2 ** RM_Size (Typ);
4960 end if;
4962 -- Fold Shift_Left (X, Y) by computing
4963 -- (X * 2**Y) rem modulus [- Modulus]
4965 Val := (Expr_Value (Left) * (Uint_2 ** Expr_Value (Right)))
4966 rem Modulus;
4968 if Is_Modular_Integer_Type (Typ)
4969 or else Val < Modulus / Uint_2
4970 then
4971 Fold_Uint (N, Val, Static => Static);
4972 else
4973 Fold_Uint (N, Val - Modulus, Static => Static);
4974 end if;
4976 elsif Op = N_Op_Shift_Right then
4977 Check_Elab_Call;
4979 -- X >> 0 is a no-op
4981 if Expr_Value (Right) = Uint_0 then
4982 Fold_Uint (N, Expr_Value (Left), Static => Static);
4983 else
4984 if Is_Modular_Integer_Type (Typ) then
4985 Modulus := Einfo.Entities.Modulus (Typ);
4986 else
4987 Modulus := Uint_2 ** RM_Size (Typ);
4988 end if;
4990 -- Fold X >> Y by computing (X [+ Modulus]) / 2**Y
4991 -- Note that after a Shift_Right operation (with Y > 0), the
4992 -- result is always positive, even if the original operand was
4993 -- negative.
4995 declare
4996 M : Unat;
4997 begin
4998 if Expr_Value (Left) >= Uint_0 then
4999 M := Uint_0;
5000 else
5001 M := Modulus;
5002 end if;
5004 Fold_Uint
5006 (Expr_Value (Left) + M) / (Uint_2 ** Expr_Value (Right)),
5007 Static => Static);
5008 end;
5009 end if;
5010 elsif Op = N_Op_Shift_Right_Arithmetic then
5011 Check_Elab_Call;
5013 declare
5014 Two_Y : constant Uint := Uint_2 ** Expr_Value (Right);
5015 begin
5016 if Is_Modular_Integer_Type (Typ) then
5017 Modulus := Einfo.Entities.Modulus (Typ);
5018 else
5019 Modulus := Uint_2 ** RM_Size (Typ);
5020 end if;
5022 -- X / 2**Y if X if positive or a small enough modular integer
5024 if (Is_Modular_Integer_Type (Typ)
5025 and then Expr_Value (Left) < Modulus / Uint_2)
5026 or else
5027 (not Is_Modular_Integer_Type (Typ)
5028 and then Expr_Value (Left) >= 0)
5029 then
5030 Fold_Uint (N, Expr_Value (Left) / Two_Y, Static => Static);
5032 -- -1 (aka all 1's) if Y is larger than the number of bits
5033 -- available or if X = -1.
5035 elsif Two_Y > Modulus
5036 or else Expr_Value (Left) = Uint_Minus_1
5037 then
5038 if Is_Modular_Integer_Type (Typ) then
5039 Fold_Uint (N, Modulus - Uint_1, Static => Static);
5040 else
5041 Fold_Uint (N, Uint_Minus_1, Static => Static);
5042 end if;
5044 -- Large modular integer, compute via multiply/divide the
5045 -- following: X >> Y + (1 << Y - 1) << (RM_Size - Y)
5047 elsif Is_Modular_Integer_Type (Typ) then
5048 Fold_Uint
5050 (Expr_Value (Left)) / Two_Y
5051 + (Two_Y - Uint_1)
5052 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right)),
5053 Static => Static);
5055 -- Negative signed integer, compute via multiple/divide the
5056 -- following:
5057 -- (Modulus + X) >> Y + (1 << Y - 1) << (RM_Size - Y) - Modulus
5059 else
5060 Fold_Uint
5062 (Modulus + Expr_Value (Left)) / Two_Y
5063 + (Two_Y - Uint_1)
5064 * Uint_2 ** (RM_Size (Typ) - Expr_Value (Right))
5065 - Modulus,
5066 Static => Static);
5067 end if;
5068 end;
5069 end if;
5070 end if;
5071 end Fold_Shift;
5073 --------------
5074 -- Fold_Str --
5075 --------------
5077 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
5078 Loc : constant Source_Ptr := Sloc (N);
5079 Typ : constant Entity_Id := Etype (N);
5081 begin
5082 if Raises_Constraint_Error (N) then
5083 Set_Is_Static_Expression (N, Static);
5084 return;
5085 end if;
5087 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
5089 -- We now have the literal with the right value, both the actual type
5090 -- and the expected type of this literal are taken from the expression
5091 -- that was evaluated. So now we do the Analyze and Resolve.
5093 -- Note that we have to reset Is_Static_Expression both after the
5094 -- analyze step (because Resolve will evaluate the literal, which
5095 -- will cause semantic errors if it is marked as static), and after
5096 -- the Resolve step (since Resolve in some cases resets this flag).
5098 Analyze (N);
5099 Set_Is_Static_Expression (N, Static);
5100 Set_Etype (N, Typ);
5101 Resolve (N);
5102 Set_Is_Static_Expression (N, Static);
5103 end Fold_Str;
5105 ---------------
5106 -- Fold_Uint --
5107 ---------------
5109 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
5110 Loc : constant Source_Ptr := Sloc (N);
5111 Typ : Entity_Id := Etype (N);
5112 Ent : Entity_Id;
5114 begin
5115 if Raises_Constraint_Error (N) then
5116 Set_Is_Static_Expression (N, Static);
5117 return;
5118 end if;
5120 -- If we are folding a named number, retain the entity in the literal
5121 -- in the original tree.
5123 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
5124 Ent := Entity (N);
5125 else
5126 Ent := Empty;
5127 end if;
5129 if Is_Private_Type (Typ) then
5130 Typ := Full_View (Typ);
5131 end if;
5133 -- For a result of type integer, substitute an N_Integer_Literal node
5134 -- for the result of the compile time evaluation of the expression.
5135 -- Set a link to the original named number when not in a generic context
5136 -- for reference in the original tree.
5138 if Is_Integer_Type (Typ) then
5139 Rewrite (N, Make_Integer_Literal (Loc, Val));
5140 Set_Original_Entity (N, Ent);
5142 -- Otherwise we have an enumeration type, and we substitute either
5143 -- an N_Identifier or N_Character_Literal to represent the enumeration
5144 -- literal corresponding to the given value, which must always be in
5145 -- range, because appropriate tests have already been made for this.
5147 else pragma Assert (Is_Enumeration_Type (Typ));
5148 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
5149 end if;
5151 -- We now have the literal with the right value, both the actual type
5152 -- and the expected type of this literal are taken from the expression
5153 -- that was evaluated. So now we do the Analyze and Resolve.
5155 -- Note that we have to reset Is_Static_Expression both after the
5156 -- analyze step (because Resolve will evaluate the literal, which
5157 -- will cause semantic errors if it is marked as static), and after
5158 -- the Resolve step (since Resolve in some cases sets this flag).
5160 Analyze (N);
5161 Set_Is_Static_Expression (N, Static);
5162 Set_Etype (N, Typ);
5163 Resolve (N);
5164 Set_Is_Static_Expression (N, Static);
5165 end Fold_Uint;
5167 ----------------
5168 -- Fold_Ureal --
5169 ----------------
5171 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
5172 Loc : constant Source_Ptr := Sloc (N);
5173 Typ : constant Entity_Id := Etype (N);
5174 Ent : Entity_Id;
5176 begin
5177 if Raises_Constraint_Error (N) then
5178 Set_Is_Static_Expression (N, Static);
5179 return;
5180 end if;
5182 -- If we are folding a named number, retain the entity in the literal
5183 -- in the original tree.
5185 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
5186 Ent := Entity (N);
5187 else
5188 Ent := Empty;
5189 end if;
5191 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
5193 -- Set link to original named number
5195 Set_Original_Entity (N, Ent);
5197 -- We now have the literal with the right value, both the actual type
5198 -- and the expected type of this literal are taken from the expression
5199 -- that was evaluated. So now we do the Analyze and Resolve.
5201 -- Note that we have to reset Is_Static_Expression both after the
5202 -- analyze step (because Resolve will evaluate the literal, which
5203 -- will cause semantic errors if it is marked as static), and after
5204 -- the Resolve step (since Resolve in some cases sets this flag).
5206 -- We mark the node as analyzed so that its type is not erased by
5207 -- calling Analyze_Real_Literal.
5209 Analyze (N);
5210 Set_Is_Static_Expression (N, Static);
5211 Set_Etype (N, Typ);
5212 Resolve (N);
5213 Set_Analyzed (N);
5214 Set_Is_Static_Expression (N, Static);
5215 end Fold_Ureal;
5217 ---------------
5218 -- From_Bits --
5219 ---------------
5221 function From_Bits (B : Bits; T : Entity_Id) return Uint is
5222 V : Uint := Uint_0;
5224 begin
5225 for J in 0 .. B'Last loop
5226 if B (J) then
5227 V := V + 2 ** J;
5228 end if;
5229 end loop;
5231 if Non_Binary_Modulus (T) then
5232 V := V mod Modulus (T);
5233 end if;
5235 return V;
5236 end From_Bits;
5238 --------------------
5239 -- Get_String_Val --
5240 --------------------
5242 function Get_String_Val (N : Node_Id) return Node_Id is
5243 begin
5244 if Nkind (N) in N_String_Literal | N_Character_Literal then
5245 return N;
5246 else
5247 pragma Assert (Is_Entity_Name (N));
5248 return Get_String_Val (Constant_Value (Entity (N)));
5249 end if;
5250 end Get_String_Val;
5252 ----------------
5253 -- Initialize --
5254 ----------------
5256 procedure Initialize is
5257 begin
5258 CV_Cache := (others => (Node_High_Bound, Uint_0));
5259 end Initialize;
5261 --------------------
5262 -- In_Subrange_Of --
5263 --------------------
5265 function In_Subrange_Of
5266 (T1 : Entity_Id;
5267 T2 : Entity_Id;
5268 Fixed_Int : Boolean := False) return Boolean
5270 L1 : Node_Id;
5271 H1 : Node_Id;
5273 L2 : Node_Id;
5274 H2 : Node_Id;
5276 begin
5277 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
5278 return True;
5280 -- Never in range if both types are not scalar. Don't know if this can
5281 -- actually happen, but just in case.
5283 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
5284 return False;
5286 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
5287 -- definitely not compatible with T2.
5289 elsif Is_Floating_Point_Type (T1)
5290 and then Has_Infinities (T1)
5291 and then Is_Floating_Point_Type (T2)
5292 and then not Has_Infinities (T2)
5293 then
5294 return False;
5296 else
5297 L1 := Type_Low_Bound (T1);
5298 H1 := Type_High_Bound (T1);
5300 L2 := Type_Low_Bound (T2);
5301 H2 := Type_High_Bound (T2);
5303 -- Check bounds to see if comparison possible at compile time
5305 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
5306 and then
5307 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
5308 then
5309 return True;
5310 end if;
5312 -- If bounds not comparable at compile time, then the bounds of T2
5313 -- must be compile-time-known or we cannot answer the query.
5315 if not Compile_Time_Known_Value (L2)
5316 or else not Compile_Time_Known_Value (H2)
5317 then
5318 return False;
5319 end if;
5321 -- If the bounds of T1 are know at compile time then use these
5322 -- ones, otherwise use the bounds of the base type (which are of
5323 -- course always static).
5325 if not Compile_Time_Known_Value (L1) then
5326 L1 := Type_Low_Bound (Base_Type (T1));
5327 end if;
5329 if not Compile_Time_Known_Value (H1) then
5330 H1 := Type_High_Bound (Base_Type (T1));
5331 end if;
5333 -- Fixed point types should be considered as such only if
5334 -- flag Fixed_Int is set to False.
5336 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
5337 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
5338 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
5339 then
5340 return
5341 Expr_Value_R (L2) <= Expr_Value_R (L1)
5342 and then
5343 Expr_Value_R (H2) >= Expr_Value_R (H1);
5345 else
5346 return
5347 Expr_Value (L2) <= Expr_Value (L1)
5348 and then
5349 Expr_Value (H2) >= Expr_Value (H1);
5351 end if;
5352 end if;
5354 -- If any exception occurs, it means that we have some bug in the compiler
5355 -- possibly triggered by a previous error, or by some unforeseen peculiar
5356 -- occurrence. However, this is only an optimization attempt, so there is
5357 -- really no point in crashing the compiler. Instead we just decide, too
5358 -- bad, we can't figure out the answer in this case after all.
5360 exception
5361 when others =>
5362 -- With debug flag K we will get an exception unless an error has
5363 -- already occurred (useful for debugging).
5365 if Debug_Flag_K then
5366 Check_Error_Detected;
5367 end if;
5369 return False;
5370 end In_Subrange_Of;
5372 -----------------
5373 -- Is_In_Range --
5374 -----------------
5376 function Is_In_Range
5377 (N : Node_Id;
5378 Typ : Entity_Id;
5379 Assume_Valid : Boolean := False;
5380 Fixed_Int : Boolean := False;
5381 Int_Real : Boolean := False) return Boolean
5383 begin
5384 return
5385 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
5386 end Is_In_Range;
5388 -------------------
5389 -- Is_Null_Range --
5390 -------------------
5392 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5393 begin
5394 if Compile_Time_Known_Value (Lo)
5395 and then Compile_Time_Known_Value (Hi)
5396 then
5397 declare
5398 Typ : Entity_Id := Etype (Lo);
5399 begin
5400 -- When called from the frontend, as part of the analysis of
5401 -- potentially static expressions, Typ will be the full view of a
5402 -- type with all the info needed to answer this query. When called
5403 -- from the backend, for example to know whether a range of a loop
5404 -- is null, Typ might be a private type and we need to explicitly
5405 -- switch to its corresponding full view to access the same info.
5407 if Is_Incomplete_Or_Private_Type (Typ)
5408 and then Present (Full_View (Typ))
5409 then
5410 Typ := Full_View (Typ);
5411 end if;
5413 if Is_Discrete_Type (Typ) then
5414 return Expr_Value (Lo) > Expr_Value (Hi);
5415 else pragma Assert (Is_Real_Type (Typ));
5416 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
5417 end if;
5418 end;
5419 else
5420 return False;
5421 end if;
5422 end Is_Null_Range;
5424 -------------------------
5425 -- Is_OK_Static_Choice --
5426 -------------------------
5428 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
5429 begin
5430 -- Check various possibilities for choice
5432 -- Note: for membership tests, we test more cases than are possible
5433 -- (in particular subtype indication), but it doesn't matter because
5434 -- it just won't occur (we have already done a syntax check).
5436 if Nkind (Choice) = N_Others_Choice then
5437 return True;
5439 elsif Nkind (Choice) = N_Range then
5440 return Is_OK_Static_Range (Choice);
5442 elsif Nkind (Choice) = N_Subtype_Indication
5443 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5444 then
5445 return Is_OK_Static_Subtype (Etype (Choice));
5447 else
5448 return Is_OK_Static_Expression (Choice);
5449 end if;
5450 end Is_OK_Static_Choice;
5452 ------------------------------
5453 -- Is_OK_Static_Choice_List --
5454 ------------------------------
5456 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
5457 Choice : Node_Id;
5459 begin
5460 if not Is_Static_Choice_List (Choices) then
5461 return False;
5462 end if;
5464 Choice := First (Choices);
5465 while Present (Choice) loop
5466 if not Is_OK_Static_Choice (Choice) then
5467 Set_Raises_Constraint_Error (Choice);
5468 return False;
5469 end if;
5471 Next (Choice);
5472 end loop;
5474 return True;
5475 end Is_OK_Static_Choice_List;
5477 -----------------------------
5478 -- Is_OK_Static_Expression --
5479 -----------------------------
5481 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
5482 begin
5483 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
5484 end Is_OK_Static_Expression;
5486 ------------------------
5487 -- Is_OK_Static_Range --
5488 ------------------------
5490 -- A static range is a range whose bounds are static expressions, or a
5491 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5492 -- We have already converted range attribute references, so we get the
5493 -- "or" part of this rule without needing a special test.
5495 function Is_OK_Static_Range (N : Node_Id) return Boolean is
5496 begin
5497 return Is_OK_Static_Expression (Low_Bound (N))
5498 and then Is_OK_Static_Expression (High_Bound (N));
5499 end Is_OK_Static_Range;
5501 --------------------------
5502 -- Is_OK_Static_Subtype --
5503 --------------------------
5505 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
5506 -- neither bound raises Constraint_Error when evaluated.
5508 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
5509 Base_T : constant Entity_Id := Base_Type (Typ);
5510 Anc_Subt : Entity_Id;
5512 begin
5513 -- First a quick check on the non static subtype flag. As described
5514 -- in further detail in Einfo, this flag is not decisive in all cases,
5515 -- but if it is set, then the subtype is definitely non-static.
5517 if Is_Non_Static_Subtype (Typ) then
5518 return False;
5519 end if;
5521 -- Then, check if the subtype is strictly static. This takes care of
5522 -- checking for generics and predicates.
5524 if not Is_Static_Subtype (Typ) then
5525 return False;
5526 end if;
5528 -- String types
5530 if Is_String_Type (Typ) then
5531 return
5532 Ekind (Typ) = E_String_Literal_Subtype
5533 or else
5534 (Is_OK_Static_Subtype (Component_Type (Typ))
5535 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
5537 -- Scalar types
5539 elsif Is_Scalar_Type (Typ) then
5540 if Base_T = Typ then
5541 return True;
5543 else
5544 Anc_Subt := Ancestor_Subtype (Typ);
5546 if No (Anc_Subt) then
5547 Anc_Subt := Base_T;
5548 end if;
5550 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
5551 -- Get_Type_{Low,High}_Bound.
5553 return Is_OK_Static_Subtype (Anc_Subt)
5554 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
5555 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
5556 end if;
5558 -- Types other than string and scalar types are never static
5560 else
5561 return False;
5562 end if;
5563 end Is_OK_Static_Subtype;
5565 ---------------------
5566 -- Is_Out_Of_Range --
5567 ---------------------
5569 function Is_Out_Of_Range
5570 (N : Node_Id;
5571 Typ : Entity_Id;
5572 Assume_Valid : Boolean := False;
5573 Fixed_Int : Boolean := False;
5574 Int_Real : Boolean := False) return Boolean
5576 begin
5577 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
5578 Out_Of_Range;
5579 end Is_Out_Of_Range;
5581 ----------------------
5582 -- Is_Static_Choice --
5583 ----------------------
5585 function Is_Static_Choice (Choice : Node_Id) return Boolean is
5586 begin
5587 -- Check various possibilities for choice
5589 -- Note: for membership tests, we test more cases than are possible
5590 -- (in particular subtype indication), but it doesn't matter because
5591 -- it just won't occur (we have already done a syntax check).
5593 if Nkind (Choice) = N_Others_Choice then
5594 return True;
5596 elsif Nkind (Choice) = N_Range then
5597 return Is_Static_Range (Choice);
5599 elsif Nkind (Choice) = N_Subtype_Indication
5600 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5601 then
5602 return Is_Static_Subtype (Etype (Choice));
5604 else
5605 return Is_Static_Expression (Choice);
5606 end if;
5607 end Is_Static_Choice;
5609 ---------------------------
5610 -- Is_Static_Choice_List --
5611 ---------------------------
5613 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
5614 Choice : Node_Id;
5616 begin
5617 Choice := First (Choices);
5618 while Present (Choice) loop
5619 if not Is_Static_Choice (Choice) then
5620 return False;
5621 end if;
5623 Next (Choice);
5624 end loop;
5626 return True;
5627 end Is_Static_Choice_List;
5629 ---------------------
5630 -- Is_Static_Range --
5631 ---------------------
5633 -- A static range is a range whose bounds are static expressions, or a
5634 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5635 -- We have already converted range attribute references, so we get the
5636 -- "or" part of this rule without needing a special test.
5638 function Is_Static_Range (N : Node_Id) return Boolean is
5639 begin
5640 return Is_Static_Expression (Low_Bound (N))
5641 and then
5642 Is_Static_Expression (High_Bound (N));
5643 end Is_Static_Range;
5645 -----------------------
5646 -- Is_Static_Subtype --
5647 -----------------------
5649 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5651 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
5652 Base_T : constant Entity_Id := Base_Type (Typ);
5653 Anc_Subt : Entity_Id;
5655 begin
5656 -- First a quick check on the non static subtype flag. As described
5657 -- in further detail in Einfo, this flag is not decisive in all cases,
5658 -- but if it is set, then the subtype is definitely non-static.
5660 if Is_Non_Static_Subtype (Typ) then
5661 return False;
5662 end if;
5664 Anc_Subt := Ancestor_Subtype (Typ);
5666 if Anc_Subt = Empty then
5667 Anc_Subt := Base_T;
5668 end if;
5670 if Is_Generic_Type (Root_Type (Base_T))
5671 or else Is_Generic_Actual_Type (Base_T)
5672 then
5673 return False;
5675 -- If there is a dynamic predicate for the type (declared or inherited)
5676 -- the expression is not static.
5678 elsif Has_Dynamic_Predicate_Aspect (Typ)
5679 or else (Is_Derived_Type (Typ)
5680 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5681 or else (Has_Aspect (Typ, Aspect_Predicate)
5682 and then not Has_Static_Predicate (Typ))
5683 then
5684 return False;
5686 -- String types
5688 elsif Is_String_Type (Typ) then
5689 return
5690 Ekind (Typ) = E_String_Literal_Subtype
5691 or else (Is_Static_Subtype (Component_Type (Typ))
5692 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5694 -- Scalar types
5696 elsif Is_Scalar_Type (Typ) then
5697 if Base_T = Typ then
5698 return True;
5700 else
5701 return Is_Static_Subtype (Anc_Subt)
5702 and then Is_Static_Expression (Type_Low_Bound (Typ))
5703 and then Is_Static_Expression (Type_High_Bound (Typ));
5704 end if;
5706 -- Types other than string and scalar types are never static
5708 else
5709 return False;
5710 end if;
5711 end Is_Static_Subtype;
5713 -------------------------------
5714 -- Is_Statically_Unevaluated --
5715 -------------------------------
5717 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5718 function Check_Case_Expr_Alternative
5719 (CEA : Node_Id) return Match_Result;
5720 -- We have a message emanating from the Expression of a case expression
5721 -- alternative. We examine this alternative, as follows:
5723 -- If the selecting expression of the parent case is non-static, or
5724 -- if any of the discrete choices of the given case alternative are
5725 -- non-static or raise Constraint_Error, return Non_Static.
5727 -- Otherwise check if the selecting expression matches any of the given
5728 -- discrete choices. If so, the alternative is executed and we return
5729 -- Match, otherwise, the alternative can never be executed, and so we
5730 -- return No_Match.
5732 ---------------------------------
5733 -- Check_Case_Expr_Alternative --
5734 ---------------------------------
5736 function Check_Case_Expr_Alternative
5737 (CEA : Node_Id) return Match_Result
5739 Case_Exp : constant Node_Id := Parent (CEA);
5740 Choice : Node_Id;
5741 Prev_CEA : Node_Id;
5743 begin
5744 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5746 -- Check that selecting expression is static
5748 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5749 return Non_Static;
5750 end if;
5752 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5753 return Non_Static;
5754 end if;
5756 -- All choices are now known to be static. Now see if alternative
5757 -- matches one of the choices.
5759 Choice := First (Discrete_Choices (CEA));
5760 while Present (Choice) loop
5762 -- Check various possibilities for choice, returning Match if we
5763 -- find the selecting value matches any of the choices. Note that
5764 -- we know we are the last choice, so we don't have to keep going.
5766 if Nkind (Choice) = N_Others_Choice then
5768 -- Others choice is a bit annoying, it matches if none of the
5769 -- previous alternatives matches (note that we know we are the
5770 -- last alternative in this case, so we can just go backwards
5771 -- from us to see if any previous one matches).
5773 Prev_CEA := Prev (CEA);
5774 while Present (Prev_CEA) loop
5775 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5776 return No_Match;
5777 end if;
5779 Prev (Prev_CEA);
5780 end loop;
5782 return Match;
5784 -- Else we have a normal static choice
5786 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5787 return Match;
5788 end if;
5790 -- If we fall through, it means that the discrete choice did not
5791 -- match the selecting expression, so continue.
5793 Next (Choice);
5794 end loop;
5796 -- If we get through that loop then all choices were static, and none
5797 -- of them matched the selecting expression. So return No_Match.
5799 return No_Match;
5800 end Check_Case_Expr_Alternative;
5802 -- Local variables
5804 P : Node_Id;
5805 OldP : Node_Id;
5806 Choice : Node_Id;
5808 -- Start of processing for Is_Statically_Unevaluated
5810 begin
5811 -- The (32.x) references here are from RM section 4.9
5813 -- (32.1) An expression is statically unevaluated if it is part of ...
5815 -- This means we have to climb the tree looking for one of the cases
5817 P := Expr;
5818 loop
5819 OldP := P;
5820 P := Parent (P);
5822 -- (32.2) The right operand of a static short-circuit control form
5823 -- whose value is determined by its left operand.
5825 -- AND THEN with False as left operand
5827 if Nkind (P) = N_And_Then
5828 and then Compile_Time_Known_Value (Left_Opnd (P))
5829 and then Is_False (Expr_Value (Left_Opnd (P)))
5830 then
5831 return True;
5833 -- OR ELSE with True as left operand
5835 elsif Nkind (P) = N_Or_Else
5836 and then Compile_Time_Known_Value (Left_Opnd (P))
5837 and then Is_True (Expr_Value (Left_Opnd (P)))
5838 then
5839 return True;
5841 -- (32.3) A dependent_expression of an if_expression whose associated
5842 -- condition is static and equals False.
5844 elsif Nkind (P) = N_If_Expression then
5845 declare
5846 Cond : constant Node_Id := First (Expressions (P));
5847 Texp : constant Node_Id := Next (Cond);
5848 Fexp : constant Node_Id := Next (Texp);
5850 begin
5851 if Compile_Time_Known_Value (Cond) then
5853 -- Condition is True and we are in the right operand
5855 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5856 return True;
5858 -- Condition is False and we are in the left operand
5860 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5861 return True;
5862 end if;
5863 end if;
5864 end;
5866 -- (32.4) A condition or dependent_expression of an if_expression
5867 -- where the condition corresponding to at least one preceding
5868 -- dependent_expression of the if_expression is static and equals
5869 -- True.
5871 -- This refers to cases like
5873 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5875 -- But we expand elsif's out anyway, so the above looks like:
5877 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5879 -- So for us this is caught by the above check for the 32.3 case.
5881 -- (32.5) A dependent_expression of a case_expression whose
5882 -- selecting_expression is static and whose value is not covered
5883 -- by the corresponding discrete_choice_list.
5885 elsif Nkind (P) = N_Case_Expression_Alternative then
5887 -- First, we have to be in the expression to suppress messages.
5888 -- If we are within one of the choices, we want the message.
5890 if OldP = Expression (P) then
5892 -- Statically unevaluated if alternative does not match
5894 if Check_Case_Expr_Alternative (P) = No_Match then
5895 return True;
5896 end if;
5897 end if;
5899 -- (32.6) A choice_expression (or a simple_expression of a range
5900 -- that occurs as a membership_choice of a membership_choice_list)
5901 -- of a static membership test that is preceded in the enclosing
5902 -- membership_choice_list by another item whose individual
5903 -- membership test (see (RM 4.5.2)) statically yields True.
5905 elsif Nkind (P) in N_Membership_Test then
5907 -- Only possibly unevaluated if simple expression is static
5909 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5910 null;
5912 -- All members of the choice list must be static
5914 elsif (Present (Right_Opnd (P))
5915 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5916 or else (Present (Alternatives (P))
5917 and then
5918 not Is_OK_Static_Choice_List (Alternatives (P)))
5919 then
5920 null;
5922 -- If expression is the one and only alternative, then it is
5923 -- definitely not statically unevaluated, so we only have to
5924 -- test the case where there are alternatives present.
5926 elsif Present (Alternatives (P)) then
5928 -- Look for previous matching Choice
5930 Choice := First (Alternatives (P));
5931 while Present (Choice) loop
5933 -- If we reached us and no previous choices matched, this
5934 -- is not the case where we are statically unevaluated.
5936 exit when OldP = Choice;
5938 -- If a previous choice matches, then that is the case where
5939 -- we know our choice is statically unevaluated.
5941 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5942 return True;
5943 end if;
5945 Next (Choice);
5946 end loop;
5948 -- If we fall through the loop, we were not one of the choices,
5949 -- we must have been the expression, so that is not covered by
5950 -- this rule, and we keep going.
5952 null;
5953 end if;
5954 end if;
5956 -- OK, not statically unevaluated at this level, see if we should
5957 -- keep climbing to look for a higher level reason.
5959 -- Special case for component association in aggregates, where
5960 -- we want to keep climbing up to the parent aggregate.
5962 if Nkind (P) = N_Component_Association
5963 and then Nkind (Parent (P)) = N_Aggregate
5964 then
5965 null;
5967 -- All done if not still within subexpression
5969 else
5970 exit when Nkind (P) not in N_Subexpr;
5971 end if;
5972 end loop;
5974 -- If we fall through the loop, not one of the cases covered!
5976 return False;
5977 end Is_Statically_Unevaluated;
5979 --------------------
5980 -- Machine_Number --
5981 --------------------
5983 -- Historical note: RM 4.9(38) originally specified biased rounding but
5984 -- this has been modified by AI-268 to prevent confusing differences in
5985 -- rounding between static and nonstatic expressions. This AI specifies
5986 -- that the effect of such rounding is implementation-dependent instead,
5987 -- and in GNAT we round to nearest even to match the run-time behavior.
5988 -- Note that this applies to floating-point literals, not fixed-point
5989 -- ones, even though their representation is also a universal real.
5991 function Machine_Number
5992 (Typ : Entity_Id;
5993 Val : Ureal;
5994 N : Node_Id) return Ureal
5996 begin
5997 return Machine (Typ, Val, Round_Even, N);
5998 end Machine_Number;
6000 --------------------
6001 -- Not_Null_Range --
6002 --------------------
6004 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
6005 begin
6006 if Compile_Time_Known_Value (Lo)
6007 and then Compile_Time_Known_Value (Hi)
6008 then
6009 declare
6010 Typ : Entity_Id := Etype (Lo);
6011 begin
6012 -- When called from the frontend, as part of the analysis of
6013 -- potentially static expressions, Typ will be the full view of a
6014 -- type with all the info needed to answer this query. When called
6015 -- from the backend, for example to know whether a range of a loop
6016 -- is null, Typ might be a private type and we need to explicitly
6017 -- switch to its corresponding full view to access the same info.
6019 if Is_Incomplete_Or_Private_Type (Typ)
6020 and then Present (Full_View (Typ))
6021 then
6022 Typ := Full_View (Typ);
6023 end if;
6025 if Is_Discrete_Type (Typ) then
6026 return Expr_Value (Lo) <= Expr_Value (Hi);
6027 else pragma Assert (Is_Real_Type (Typ));
6028 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
6029 end if;
6030 end;
6031 else
6032 return False;
6033 end if;
6035 end Not_Null_Range;
6037 -------------
6038 -- OK_Bits --
6039 -------------
6041 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
6042 begin
6043 -- We allow a maximum of 500,000 bits which seems a reasonable limit
6045 if Bits < 500_000 then
6046 return True;
6048 -- Error if this maximum is exceeded
6050 else
6051 Error_Msg_N ("static value too large, capacity exceeded", N);
6052 return False;
6053 end if;
6054 end OK_Bits;
6056 ------------------
6057 -- Out_Of_Range --
6058 ------------------
6060 procedure Out_Of_Range (N : Node_Id) is
6062 -- If the FE conjures up an expression that would normally be
6063 -- an illegal static expression (e.g., an integer literal with
6064 -- a value outside of its base subtype), we don't want to
6065 -- flag it as illegal; we only want a warning in such cases.
6067 function Force_Warning return Boolean is
6068 (if Comes_From_Source (Original_Node (N)) then False
6069 elsif Nkind (Original_Node (N)) = N_Type_Conversion then True
6070 else Is_Null_Array_Aggregate_High_Bound (N));
6071 begin
6072 -- If we have the static expression case, then this is an illegality
6073 -- in Ada 95 mode, except that in an instance, we never generate an
6074 -- error (if the error is legitimate, it was already diagnosed in the
6075 -- template).
6077 if Is_Static_Expression (N)
6078 and then not In_Instance
6079 and then not In_Inlined_Body
6080 and then Ada_Version >= Ada_95
6081 then
6082 -- No message if we are statically unevaluated
6084 if Is_Statically_Unevaluated (N) then
6085 null;
6087 -- The expression to compute the length of a packed array is attached
6088 -- to the array type itself, and deserves a separate message.
6090 elsif Nkind (Parent (N)) = N_Defining_Identifier
6091 and then Is_Array_Type (Parent (N))
6092 and then Present (Packed_Array_Impl_Type (Parent (N)))
6093 and then Present (First_Rep_Item (Parent (N)))
6094 then
6095 Error_Msg_N
6096 ("length of packed array must not exceed Integer''Last",
6097 First_Rep_Item (Parent (N)));
6098 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
6100 -- All cases except the special array case.
6101 -- No message if we are dealing with System.Priority values in
6102 -- CodePeer mode where the target runtime may have more priorities.
6104 elsif not CodePeer_Mode
6105 or else not Is_RTE (Etype (N), RE_Priority)
6106 then
6107 -- Determine if the out-of-range violation constitutes a warning
6108 -- or an error based on context, according to RM 4.9 (34/3).
6110 if Force_Warning then
6111 Apply_Compile_Time_Constraint_Error
6112 (N, "value not in range of}??", CE_Range_Check_Failed);
6113 else
6114 Apply_Compile_Time_Constraint_Error
6115 (N, "value not in range of}", CE_Range_Check_Failed);
6116 end if;
6117 end if;
6119 -- Here we generate a warning for the Ada 83 case, or when we are in an
6120 -- instance, or when we have a non-static expression case.
6122 else
6123 Apply_Compile_Time_Constraint_Error
6124 (N, "value not in range of}??", CE_Range_Check_Failed);
6125 end if;
6126 end Out_Of_Range;
6128 ---------------------------
6129 -- Predicates_Compatible --
6130 ---------------------------
6132 function Predicates_Compatible (T1, T2 : Entity_Id) return Boolean is
6134 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean;
6135 -- Return True if the rep item for Nam is either absent on T2 or also
6136 -- applies to T1.
6138 -------------------------------
6139 -- T2_Rep_Item_Applies_To_T1 --
6140 -------------------------------
6142 function T2_Rep_Item_Applies_To_T1 (Nam : Name_Id) return Boolean is
6143 Rep_Item : constant Node_Id := Get_Rep_Item (T2, Nam);
6145 begin
6146 return No (Rep_Item) or else Get_Rep_Item (T1, Nam) = Rep_Item;
6147 end T2_Rep_Item_Applies_To_T1;
6149 -- Start of processing for Predicates_Compatible
6151 begin
6152 if Ada_Version < Ada_2012 then
6153 return True;
6155 -- If T2 has no predicates, there is no compatibility issue
6157 elsif not Has_Predicates (T2) then
6158 return True;
6160 -- T2 has predicates, if T1 has none then we defer to the static check
6162 elsif not Has_Predicates (T1) then
6163 null;
6165 -- Both T2 and T1 have predicates, check that all predicates that apply
6166 -- to T2 apply also to T1 (RM 4.9.1(9/3)).
6168 elsif T2_Rep_Item_Applies_To_T1 (Name_Static_Predicate)
6169 and then T2_Rep_Item_Applies_To_T1 (Name_Dynamic_Predicate)
6170 and then T2_Rep_Item_Applies_To_T1 (Name_Predicate)
6171 then
6172 return True;
6173 end if;
6175 -- Implement the static check prescribed by RM 4.9.1(10/3)
6177 if Is_Static_Subtype (T1) and then Is_Static_Subtype (T2) then
6178 -- We just need to query Interval_Lists for discrete types
6180 if Is_Discrete_Type (T1) and then Is_Discrete_Type (T2) then
6181 declare
6182 Interval_List1 : constant Interval_Lists.Discrete_Interval_List
6183 := Interval_Lists.Type_Intervals (T1);
6184 Interval_List2 : constant Interval_Lists.Discrete_Interval_List
6185 := Interval_Lists.Type_Intervals (T2);
6186 begin
6187 return Interval_Lists.Is_Subset (Interval_List1, Interval_List2)
6188 and then not (Has_Predicates (T1)
6189 and then not Predicate_Checks_Suppressed (T2)
6190 and then Predicate_Checks_Suppressed (T1));
6191 end;
6193 else
6194 -- ??? Need to implement Interval_Lists for real types
6196 return False;
6197 end if;
6199 -- If either subtype is not static, the predicates are not compatible
6201 else
6202 return False;
6203 end if;
6204 end Predicates_Compatible;
6206 ----------------------
6207 -- Predicates_Match --
6208 ----------------------
6210 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
6212 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean;
6213 -- Return True if T1 and T2 have the same rep item for Nam
6215 ------------------------
6216 -- Have_Same_Rep_Item --
6217 ------------------------
6219 function Have_Same_Rep_Item (Nam : Name_Id) return Boolean is
6220 begin
6221 return Get_Rep_Item (T1, Nam) = Get_Rep_Item (T2, Nam);
6222 end Have_Same_Rep_Item;
6224 -- Start of processing for Predicates_Match
6226 begin
6227 if Ada_Version < Ada_2012 then
6228 return True;
6230 -- If T2 has no predicates, match if and only if T1 has none
6232 elsif not Has_Predicates (T2) then
6233 return not Has_Predicates (T1);
6235 -- T2 has predicates, no match if T1 has none
6237 elsif not Has_Predicates (T1) then
6238 return False;
6240 -- Both T2 and T1 have predicates, check that they all come
6241 -- from the same declarations.
6243 else
6244 return Have_Same_Rep_Item (Name_Static_Predicate)
6245 and then Have_Same_Rep_Item (Name_Dynamic_Predicate)
6246 and then Have_Same_Rep_Item (Name_Predicate);
6247 end if;
6248 end Predicates_Match;
6250 ---------------------------------------------
6251 -- Real_Or_String_Static_Predicate_Matches --
6252 ---------------------------------------------
6254 function Real_Or_String_Static_Predicate_Matches
6255 (Val : Node_Id;
6256 Typ : Entity_Id) return Boolean
6258 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
6259 -- The predicate expression from the type
6261 Pfun : constant Entity_Id := Predicate_Function (Typ);
6262 -- The entity for the predicate function
6264 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
6265 -- The name of the formal of the predicate function. Occurrences of the
6266 -- type name in Expr have been rewritten as references to this formal,
6267 -- and it has a unique name, so we can identify references by this name.
6269 Copy : Node_Id;
6270 -- Copy of the predicate function tree
6272 function Process (N : Node_Id) return Traverse_Result;
6273 -- Function used to process nodes during the traversal in which we will
6274 -- find occurrences of the entity name, and replace such occurrences
6275 -- by a real literal with the value to be tested.
6277 procedure Traverse is new Traverse_Proc (Process);
6278 -- The actual traversal procedure
6280 -------------
6281 -- Process --
6282 -------------
6284 function Process (N : Node_Id) return Traverse_Result is
6285 begin
6286 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
6287 declare
6288 Nod : constant Node_Id := New_Copy (Val);
6289 begin
6290 Set_Sloc (Nod, Sloc (N));
6291 Rewrite (N, Nod);
6292 return Skip;
6293 end;
6295 -- The predicate function may contain string-comparison operations
6296 -- that have been converted into calls to run-time array-comparison
6297 -- routines. To evaluate the predicate statically, we recover the
6298 -- original comparison operation and replace the occurrence of the
6299 -- formal by the static string value. The actuals of the generated
6300 -- call are of the form X'Address.
6302 elsif Nkind (N) in N_Op_Compare
6303 and then Nkind (Left_Opnd (N)) = N_Function_Call
6304 then
6305 declare
6306 C : constant Node_Id := Left_Opnd (N);
6307 F : constant Node_Id := First (Parameter_Associations (C));
6308 L : constant Node_Id := Prefix (F);
6309 R : constant Node_Id := Prefix (Next (F));
6311 begin
6312 -- If an operand is an entity name, it is the formal of the
6313 -- predicate function, so replace it with the string value.
6314 -- It may be either operand in the call. The other operand
6315 -- is a static string from the original predicate.
6317 if Is_Entity_Name (L) then
6318 Rewrite (Left_Opnd (N), New_Copy (Val));
6319 Rewrite (Right_Opnd (N), New_Copy (R));
6321 else
6322 Rewrite (Left_Opnd (N), New_Copy (L));
6323 Rewrite (Right_Opnd (N), New_Copy (Val));
6324 end if;
6326 return Skip;
6327 end;
6329 else
6330 return OK;
6331 end if;
6332 end Process;
6334 -- Start of processing for Real_Or_String_Static_Predicate_Matches
6336 begin
6337 -- First deal with special case of inherited predicate, where the
6338 -- predicate expression looks like:
6340 -- xxPredicate (typ (Ent)) and then Expr
6342 -- where Expr is the predicate expression for this level, and the
6343 -- left operand is the call to evaluate the inherited predicate.
6345 if Nkind (Expr) = N_And_Then
6346 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
6347 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
6348 then
6349 -- OK we have the inherited case, so make a call to evaluate the
6350 -- inherited predicate. If that fails, so do we!
6352 if not
6353 Real_Or_String_Static_Predicate_Matches
6354 (Val => Val,
6355 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
6356 then
6357 return False;
6358 end if;
6360 -- Use the right operand for the continued processing
6362 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
6364 -- Case where call to predicate function appears on its own (this means
6365 -- that the predicate at this level is just inherited from the parent).
6367 elsif Nkind (Expr) = N_Function_Call then
6368 declare
6369 Typ : constant Entity_Id :=
6370 Etype (First_Formal (Entity (Name (Expr))));
6372 begin
6373 -- If the inherited predicate is dynamic, just ignore it. We can't
6374 -- go trying to evaluate a dynamic predicate as a static one!
6376 if Has_Dynamic_Predicate_Aspect (Typ) then
6377 return True;
6379 -- Otherwise inherited predicate is static, check for match
6381 else
6382 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
6383 end if;
6384 end;
6386 -- If not just an inherited predicate, copy whole expression
6388 else
6389 Copy := Copy_Separate_Tree (Expr);
6390 end if;
6392 -- Now we replace occurrences of the entity by the value
6394 Traverse (Copy);
6396 -- And analyze the resulting static expression to see if it is True
6398 Analyze_And_Resolve (Copy, Standard_Boolean);
6399 return Is_True (Expr_Value (Copy));
6400 end Real_Or_String_Static_Predicate_Matches;
6402 -------------------------
6403 -- Rewrite_In_Raise_CE --
6404 -------------------------
6406 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
6407 Stat : constant Boolean := Is_Static_Expression (N);
6408 Typ : constant Entity_Id := Etype (N);
6410 begin
6411 -- If we want to raise CE in the condition of a N_Raise_CE node, we
6412 -- can just clear the condition if the reason is appropriate. We do
6413 -- not do this operation if the parent has a reason other than range
6414 -- check failed, because otherwise we would change the reason.
6416 if Present (Parent (N))
6417 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
6418 and then Reason (Parent (N)) =
6419 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
6420 then
6421 Set_Condition (Parent (N), Empty);
6423 -- Else build an explicit N_Raise_CE
6425 else
6426 if Nkind (Exp) = N_Raise_Constraint_Error then
6427 Rewrite (N,
6428 Make_Raise_Constraint_Error (Sloc (Exp),
6429 Reason => Reason (Exp)));
6430 else
6431 Rewrite (N,
6432 Make_Raise_Constraint_Error (Sloc (Exp),
6433 Reason => CE_Range_Check_Failed));
6434 end if;
6436 Set_Raises_Constraint_Error (N);
6437 Set_Etype (N, Typ);
6438 end if;
6440 -- Set proper flags in result
6442 Set_Raises_Constraint_Error (N, True);
6443 Set_Is_Static_Expression (N, Stat);
6444 end Rewrite_In_Raise_CE;
6446 ------------------------------------------------
6447 -- Set_Checking_Potentially_Static_Expression --
6448 ------------------------------------------------
6450 procedure Set_Checking_Potentially_Static_Expression (Value : Boolean) is
6451 begin
6452 -- Verify that we only start/stop checking for a potentially static
6453 -- expression and do not start or stop it twice in a row.
6455 pragma Assert (Checking_For_Potentially_Static_Expression /= Value);
6457 Checking_For_Potentially_Static_Expression := Value;
6458 end Set_Checking_Potentially_Static_Expression;
6460 ---------------------
6461 -- String_Type_Len --
6462 ---------------------
6464 function String_Type_Len (Stype : Entity_Id) return Uint is
6465 NT : constant Entity_Id := Etype (First_Index (Stype));
6466 T : Entity_Id;
6468 begin
6469 if Is_OK_Static_Subtype (NT) then
6470 T := NT;
6471 else
6472 T := Base_Type (NT);
6473 end if;
6475 return Expr_Value (Type_High_Bound (T)) -
6476 Expr_Value (Type_Low_Bound (T)) + 1;
6477 end String_Type_Len;
6479 ------------------------------------
6480 -- Subtypes_Statically_Compatible --
6481 ------------------------------------
6483 function Subtypes_Statically_Compatible
6484 (T1 : Entity_Id;
6485 T2 : Entity_Id;
6486 Formal_Derived_Matching : Boolean := False) return Boolean
6488 begin
6489 -- A type is always statically compatible with itself
6491 if T1 = T2 then
6492 return True;
6494 -- Not compatible if predicates are not compatible
6496 elsif not Predicates_Compatible (T1, T2) then
6497 return False;
6499 -- Scalar types
6501 elsif Is_Scalar_Type (T1) then
6503 -- Definitely compatible if we match
6505 if Subtypes_Statically_Match (T1, T2) then
6506 return True;
6508 -- A scalar subtype S1 is compatible with S2 if their bounds
6509 -- are static and compatible, even if S1 has dynamic predicates
6510 -- and is thus non-static. Predicate compatibility has been
6511 -- checked above.
6513 elsif not Is_Static_Range (Scalar_Range (T1))
6514 or else not Is_Static_Range (Scalar_Range (T2))
6515 then
6516 return False;
6518 -- Base types must match, but we don't check that (should we???) but
6519 -- we do at least check that both types are real, or both types are
6520 -- not real.
6522 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
6523 return False;
6525 -- Here we check the bounds
6527 else
6528 declare
6529 LB1 : constant Node_Id := Type_Low_Bound (T1);
6530 HB1 : constant Node_Id := Type_High_Bound (T1);
6531 LB2 : constant Node_Id := Type_Low_Bound (T2);
6532 HB2 : constant Node_Id := Type_High_Bound (T2);
6534 begin
6535 if Is_Real_Type (T1) then
6536 return
6537 Expr_Value_R (LB1) > Expr_Value_R (HB1)
6538 or else
6539 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
6540 and then Expr_Value_R (HB1) <= Expr_Value_R (HB2));
6542 else
6543 return
6544 Expr_Value (LB1) > Expr_Value (HB1)
6545 or else
6546 (Expr_Value (LB2) <= Expr_Value (LB1)
6547 and then Expr_Value (HB1) <= Expr_Value (HB2));
6548 end if;
6549 end;
6550 end if;
6552 -- Access types
6554 elsif Is_Access_Type (T1) then
6555 return
6556 (not Is_Constrained (T2)
6557 or else Subtypes_Statically_Match
6558 (Designated_Type (T1), Designated_Type (T2)))
6559 and then not (Can_Never_Be_Null (T2)
6560 and then not Can_Never_Be_Null (T1));
6562 -- Private types without discriminants can be handled specially.
6563 -- Predicate matching has been checked above.
6565 elsif Is_Private_Type (T1)
6566 and then not Has_Discriminants (T1)
6567 then
6568 return not Has_Discriminants (T2);
6570 -- All other cases
6572 else
6573 return
6574 (Is_Composite_Type (T1) and then not Is_Constrained (T2))
6575 or else Subtypes_Statically_Match
6576 (T1, T2, Formal_Derived_Matching);
6577 end if;
6578 end Subtypes_Statically_Compatible;
6580 -------------------------------
6581 -- Subtypes_Statically_Match --
6582 -------------------------------
6584 -- Subtypes statically match if they have statically matching constraints
6585 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
6586 -- they are the same identical constraint, or if they are static and the
6587 -- values match (RM 4.9.1(1)).
6589 -- In addition, in GNAT, the object size (Esize) values of the types must
6590 -- match if they are set (unless checking an actual for a formal derived
6591 -- type). The use of 'Object_Size can cause this to be false even if the
6592 -- types would otherwise match in the Ada 95 RM sense, but this deviation
6593 -- is adopted by AI12-059 which introduces Object_Size in Ada 2022.
6595 function Subtypes_Statically_Match
6596 (T1 : Entity_Id;
6597 T2 : Entity_Id;
6598 Formal_Derived_Matching : Boolean := False) return Boolean
6600 begin
6601 -- A type always statically matches itself
6603 if T1 = T2 then
6604 return True;
6606 -- No match if sizes different (from use of 'Object_Size). This test
6607 -- is excluded if Formal_Derived_Matching is True, as the base types
6608 -- can be different in that case and typically have different sizes.
6610 elsif not Formal_Derived_Matching
6611 and then Known_Static_Esize (T1)
6612 and then Known_Static_Esize (T2)
6613 and then Esize (T1) /= Esize (T2)
6614 then
6615 return False;
6617 -- No match if predicates do not match
6619 elsif not Predicates_Match (T1, T2) then
6620 return False;
6622 -- Scalar types
6624 elsif Is_Scalar_Type (T1) then
6626 -- Base types must be the same
6628 if Base_Type (T1) /= Base_Type (T2) then
6629 return False;
6630 end if;
6632 -- A constrained numeric subtype never matches an unconstrained
6633 -- subtype, i.e. both types must be constrained or unconstrained.
6635 -- To understand the requirement for this test, see RM 4.9.1(1).
6636 -- As is made clear in RM 3.5.4(11), type Integer, for example is
6637 -- a constrained subtype with constraint bounds matching the bounds
6638 -- of its corresponding unconstrained base type. In this situation,
6639 -- Integer and Integer'Base do not statically match, even though
6640 -- they have the same bounds.
6642 -- We only apply this test to types in Standard and types that appear
6643 -- in user programs. That way, we do not have to be too careful about
6644 -- setting Is_Constrained right for Itypes.
6646 if Is_Numeric_Type (T1)
6647 and then (Is_Constrained (T1) /= Is_Constrained (T2))
6648 and then (Scope (T1) = Standard_Standard
6649 or else Comes_From_Source (T1))
6650 and then (Scope (T2) = Standard_Standard
6651 or else Comes_From_Source (T2))
6652 then
6653 return False;
6655 -- A generic scalar type does not statically match its base type
6656 -- (AI-311). In this case we make sure that the formals, which are
6657 -- first subtypes of their bases, are constrained.
6659 elsif Is_Generic_Type (T1)
6660 and then Is_Generic_Type (T2)
6661 and then (Is_Constrained (T1) /= Is_Constrained (T2))
6662 then
6663 return False;
6664 end if;
6666 -- If there was an error in either range, then just assume the types
6667 -- statically match to avoid further junk errors.
6669 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
6670 or else Error_Posted (Scalar_Range (T1))
6671 or else Error_Posted (Scalar_Range (T2))
6672 then
6673 return True;
6674 end if;
6676 -- Otherwise both types have bounds that can be compared
6678 declare
6679 LB1 : constant Node_Id := Type_Low_Bound (T1);
6680 HB1 : constant Node_Id := Type_High_Bound (T1);
6681 LB2 : constant Node_Id := Type_Low_Bound (T2);
6682 HB2 : constant Node_Id := Type_High_Bound (T2);
6684 begin
6685 -- If the bounds are the same tree node, then match (common case)
6687 if LB1 = LB2 and then HB1 = HB2 then
6688 return True;
6690 -- Otherwise bounds must be static and identical value
6692 else
6693 if not Is_OK_Static_Subtype (T1)
6694 or else
6695 not Is_OK_Static_Subtype (T2)
6696 then
6697 return False;
6699 elsif Is_Real_Type (T1) then
6700 return
6701 Expr_Value_R (LB1) = Expr_Value_R (LB2)
6702 and then
6703 Expr_Value_R (HB1) = Expr_Value_R (HB2);
6705 else
6706 return
6707 Expr_Value (LB1) = Expr_Value (LB2)
6708 and then
6709 Expr_Value (HB1) = Expr_Value (HB2);
6710 end if;
6711 end if;
6712 end;
6714 -- Type with discriminants
6716 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
6718 -- Handle derivations of private subtypes. For example S1 statically
6719 -- matches the full view of T1 in the following example:
6721 -- type T1(<>) is new Root with private;
6722 -- subtype S1 is new T1;
6723 -- overriding proc P1 (P : S1);
6724 -- private
6725 -- type T1 (D : Disc) is new Root with ...
6727 if Ekind (T2) = E_Record_Subtype_With_Private
6728 and then not Has_Discriminants (T2)
6729 and then Partial_View_Has_Unknown_Discr (T1)
6730 and then Etype (T2) = T1
6731 then
6732 return True;
6734 elsif Ekind (T1) = E_Record_Subtype_With_Private
6735 and then not Has_Discriminants (T1)
6736 and then Partial_View_Has_Unknown_Discr (T2)
6737 and then Etype (T1) = T2
6738 then
6739 return True;
6741 -- Because of view exchanges in multiple instantiations, conformance
6742 -- checking might try to match a partial view of a type with no
6743 -- discriminants with a full view that has defaulted discriminants.
6744 -- In such a case, use the discriminant constraint of the full view,
6745 -- which must exist because we know that the two subtypes have the
6746 -- same base type.
6748 elsif Has_Discriminants (T1) /= Has_Discriminants (T2) then
6749 if In_Instance then
6750 if Is_Private_Type (T2)
6751 and then Present (Full_View (T2))
6752 and then Has_Discriminants (Full_View (T2))
6753 then
6754 return Subtypes_Statically_Match (T1, Full_View (T2));
6756 elsif Is_Private_Type (T1)
6757 and then Present (Full_View (T1))
6758 and then Has_Discriminants (Full_View (T1))
6759 then
6760 return Subtypes_Statically_Match (Full_View (T1), T2);
6762 else
6763 return False;
6764 end if;
6765 else
6766 return False;
6767 end if;
6768 end if;
6770 declare
6772 function Original_Discriminant_Constraint
6773 (Typ : Entity_Id) return Elist_Id;
6774 -- Returns Typ's discriminant constraint, or if the constraint
6775 -- is inherited from an ancestor type, then climbs the parent
6776 -- types to locate and return the constraint farthest up the
6777 -- parent chain that Typ's constraint is ultimately inherited
6778 -- from (stopping before a parent that doesn't impose a constraint
6779 -- or a parent that has new discriminants). This ensures a proper
6780 -- result from the equality comparison of Elist_Ids below (as
6781 -- otherwise, derived types that inherit constraints may appear
6782 -- to be unequal, because each level of derivation can have its
6783 -- own copy of the constraint).
6785 function Original_Discriminant_Constraint
6786 (Typ : Entity_Id) return Elist_Id
6788 begin
6789 if not Has_Discriminants (Typ) then
6790 return No_Elist;
6792 -- If Typ is not a derived type, then directly return the
6793 -- its constraint.
6795 elsif not Is_Derived_Type (Typ) then
6796 return Discriminant_Constraint (Typ);
6798 -- If the parent type doesn't have discriminants, doesn't
6799 -- have a constraint, or has new discriminants, then stop
6800 -- and return Typ's constraint.
6802 elsif not Has_Discriminants (Etype (Typ))
6804 -- No constraint on the parent type
6806 or else not Present (Discriminant_Constraint (Etype (Typ)))
6807 or else Is_Empty_Elmt_List
6808 (Discriminant_Constraint (Etype (Typ)))
6810 -- The parent type defines new discriminants
6812 or else
6813 (Is_Base_Type (Etype (Typ))
6814 and then Present (Discriminant_Specifications
6815 (Parent (Etype (Typ)))))
6816 then
6817 return Discriminant_Constraint (Typ);
6819 -- Otherwise, make a recursive call on the parent type
6821 else
6822 return Original_Discriminant_Constraint (Etype (Typ));
6823 end if;
6824 end Original_Discriminant_Constraint;
6826 -- Local variables
6828 DL1 : constant Elist_Id := Original_Discriminant_Constraint (T1);
6829 DL2 : constant Elist_Id := Original_Discriminant_Constraint (T2);
6831 DA1 : Elmt_Id;
6832 DA2 : Elmt_Id;
6834 begin
6835 if DL1 = DL2 then
6836 return True;
6837 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
6838 return False;
6839 end if;
6841 -- Now loop through the discriminant constraints
6843 -- Note: the guard here seems necessary, since it is possible at
6844 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
6846 if Present (DL1) and then Present (DL2) then
6847 DA1 := First_Elmt (DL1);
6848 DA2 := First_Elmt (DL2);
6849 while Present (DA1) loop
6850 declare
6851 Expr1 : constant Node_Id := Node (DA1);
6852 Expr2 : constant Node_Id := Node (DA2);
6854 begin
6855 if not Is_OK_Static_Expression (Expr1)
6856 or else not Is_OK_Static_Expression (Expr2)
6857 then
6858 return False;
6860 -- If either expression raised a Constraint_Error,
6861 -- consider the expressions as matching, since this
6862 -- helps to prevent cascading errors.
6864 elsif Raises_Constraint_Error (Expr1)
6865 or else Raises_Constraint_Error (Expr2)
6866 then
6867 null;
6869 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
6870 return False;
6871 end if;
6872 end;
6874 Next_Elmt (DA1);
6875 Next_Elmt (DA2);
6876 end loop;
6877 end if;
6878 end;
6880 return True;
6882 -- A definite type does not match an indefinite or classwide type.
6883 -- However, a generic type with unknown discriminants may be
6884 -- instantiated with a type with no discriminants, and conformance
6885 -- checking on an inherited operation may compare the actual with the
6886 -- subtype that renames it in the instance.
6888 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
6889 then
6890 return
6891 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
6893 -- Array type
6895 elsif Is_Array_Type (T1) then
6897 -- If either subtype is unconstrained then both must be, and if both
6898 -- are unconstrained then no further checking is needed.
6900 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
6901 return not (Is_Constrained (T1) or else Is_Constrained (T2));
6902 end if;
6904 -- Both subtypes are constrained, so check that the index subtypes
6905 -- statically match.
6907 declare
6908 Index1 : Node_Id := First_Index (T1);
6909 Index2 : Node_Id := First_Index (T2);
6911 begin
6912 while Present (Index1) loop
6913 if not
6914 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6915 then
6916 return False;
6917 end if;
6919 Next_Index (Index1);
6920 Next_Index (Index2);
6921 end loop;
6923 return True;
6924 end;
6926 elsif Is_Access_Type (T1) then
6927 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6928 return False;
6930 elsif Ekind (T1) in E_Access_Subprogram_Type
6931 | E_Anonymous_Access_Subprogram_Type
6932 then
6933 return
6934 Subtype_Conformant
6935 (Designated_Type (T1),
6936 Designated_Type (T2));
6937 else
6938 return
6939 Subtypes_Statically_Match
6940 (Designated_Type (T1),
6941 Designated_Type (T2))
6942 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
6943 end if;
6945 -- All other types definitely match
6947 else
6948 return True;
6949 end if;
6950 end Subtypes_Statically_Match;
6952 ----------
6953 -- Test --
6954 ----------
6956 function Test (Cond : Boolean) return Uint is
6957 begin
6958 if Cond then
6959 return Uint_1;
6960 else
6961 return Uint_0;
6962 end if;
6963 end Test;
6965 ---------------------
6966 -- Test_Comparison --
6967 ---------------------
6969 procedure Test_Comparison
6970 (Op : Node_Id;
6971 Assume_Valid : Boolean;
6972 True_Result : out Boolean;
6973 False_Result : out Boolean)
6975 Left : constant Node_Id := Left_Opnd (Op);
6976 Left_Typ : constant Entity_Id := Etype (Left);
6977 Orig_Op : constant Node_Id := Original_Node (Op);
6979 procedure Replacement_Warning (Msg : String);
6980 -- Emit a warning on a comparison that can be replaced by '='
6982 -------------------------
6983 -- Replacement_Warning --
6984 -------------------------
6986 procedure Replacement_Warning (Msg : String) is
6987 begin
6988 if Constant_Condition_Warnings
6989 and then Comes_From_Source (Orig_Op)
6990 and then Is_Integer_Type (Left_Typ)
6991 and then not Error_Posted (Op)
6992 and then not Has_Warnings_Off (Left_Typ)
6993 and then not In_Instance
6994 then
6995 Error_Msg_N (Msg, Op);
6996 end if;
6997 end Replacement_Warning;
6999 -- Local variables
7001 Res : constant Compare_Result :=
7002 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
7004 -- Start of processing for Test_Comparison
7006 begin
7007 case N_Op_Compare (Nkind (Op)) is
7008 when N_Op_Eq =>
7009 True_Result := Res = EQ;
7010 False_Result := Res = LT or else Res = GT or else Res = NE;
7012 when N_Op_Ge =>
7013 True_Result := Res in Compare_GE;
7014 False_Result := Res = LT;
7016 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
7017 Replacement_Warning
7018 ("can never be greater than, could replace by ""'=""?c?");
7019 end if;
7021 when N_Op_Gt =>
7022 True_Result := Res = GT;
7023 False_Result := Res in Compare_LE;
7025 when N_Op_Le =>
7026 True_Result := Res in Compare_LE;
7027 False_Result := Res = GT;
7029 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
7030 Replacement_Warning
7031 ("can never be less than, could replace by ""'=""?c?");
7032 end if;
7034 when N_Op_Lt =>
7035 True_Result := Res = LT;
7036 False_Result := Res in Compare_GE;
7038 when N_Op_Ne =>
7039 True_Result := Res = NE or else Res = GT or else Res = LT;
7040 False_Result := Res = EQ;
7041 end case;
7042 end Test_Comparison;
7044 ---------------------------------
7045 -- Test_Expression_Is_Foldable --
7046 ---------------------------------
7048 -- One operand case
7050 procedure Test_Expression_Is_Foldable
7051 (N : Node_Id;
7052 Op1 : Node_Id;
7053 Stat : out Boolean;
7054 Fold : out Boolean)
7056 begin
7057 Stat := False;
7058 Fold := False;
7060 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7061 return;
7062 end if;
7064 -- If operand is Any_Type, just propagate to result and do not
7065 -- try to fold, this prevents cascaded errors.
7067 if Etype (Op1) = Any_Type then
7068 Set_Etype (N, Any_Type);
7069 return;
7071 -- If operand raises Constraint_Error, then replace node N with the
7072 -- raise Constraint_Error node, and we are obviously not foldable.
7073 -- Note that this replacement inherits the Is_Static_Expression flag
7074 -- from the operand.
7076 elsif Raises_Constraint_Error (Op1) then
7077 Rewrite_In_Raise_CE (N, Op1);
7078 return;
7080 -- If the operand is not static, then the result is not static, and
7081 -- all we have to do is to check the operand since it is now known
7082 -- to appear in a non-static context.
7084 elsif not Is_Static_Expression (Op1) then
7085 Check_Non_Static_Context (Op1);
7086 Fold := Compile_Time_Known_Value (Op1);
7087 return;
7089 -- An expression of a formal modular type is not foldable because
7090 -- the modulus is unknown.
7092 elsif Is_Modular_Integer_Type (Etype (Op1))
7093 and then Is_Generic_Type (Etype (Op1))
7094 then
7095 Check_Non_Static_Context (Op1);
7096 return;
7098 -- Here we have the case of an operand whose type is OK, which is
7099 -- static, and which does not raise Constraint_Error, we can fold.
7101 else
7102 Set_Is_Static_Expression (N);
7103 Fold := True;
7104 Stat := True;
7105 end if;
7106 end Test_Expression_Is_Foldable;
7108 -- Two operand case
7110 procedure Test_Expression_Is_Foldable
7111 (N : Node_Id;
7112 Op1 : Node_Id;
7113 Op2 : Node_Id;
7114 Stat : out Boolean;
7115 Fold : out Boolean;
7116 CRT_Safe : Boolean := False)
7118 Rstat : constant Boolean := Is_Static_Expression (Op1)
7119 and then
7120 Is_Static_Expression (Op2);
7122 begin
7123 Stat := False;
7124 Fold := False;
7126 -- Inhibit folding if -gnatd.f flag set
7128 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
7129 return;
7130 end if;
7132 -- If either operand is Any_Type, just propagate to result and
7133 -- do not try to fold, this prevents cascaded errors.
7135 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
7136 Set_Etype (N, Any_Type);
7137 return;
7139 -- If left operand raises Constraint_Error, then replace node N with the
7140 -- Raise_Constraint_Error node, and we are obviously not foldable.
7141 -- Is_Static_Expression is set from the two operands in the normal way,
7142 -- and we check the right operand if it is in a non-static context.
7144 elsif Raises_Constraint_Error (Op1) then
7145 if not Rstat then
7146 Check_Non_Static_Context (Op2);
7147 end if;
7149 Rewrite_In_Raise_CE (N, Op1);
7150 Set_Is_Static_Expression (N, Rstat);
7151 return;
7153 -- Similar processing for the case of the right operand. Note that we
7154 -- don't use this routine for the short-circuit case, so we do not have
7155 -- to worry about that special case here.
7157 elsif Raises_Constraint_Error (Op2) then
7158 if not Rstat then
7159 Check_Non_Static_Context (Op1);
7160 end if;
7162 Rewrite_In_Raise_CE (N, Op2);
7163 Set_Is_Static_Expression (N, Rstat);
7164 return;
7166 -- Exclude expressions of a generic modular type, as above
7168 elsif Is_Modular_Integer_Type (Etype (Op1))
7169 and then Is_Generic_Type (Etype (Op1))
7170 then
7171 Check_Non_Static_Context (Op1);
7172 return;
7174 -- If result is not static, then check non-static contexts on operands
7175 -- since one of them may be static and the other one may not be static.
7177 elsif not Rstat then
7178 Check_Non_Static_Context (Op1);
7179 Check_Non_Static_Context (Op2);
7181 if CRT_Safe then
7182 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
7183 and then CRT_Safe_Compile_Time_Known_Value (Op2);
7184 else
7185 Fold := Compile_Time_Known_Value (Op1)
7186 and then Compile_Time_Known_Value (Op2);
7187 end if;
7189 if not Fold
7190 and then not Is_Modular_Integer_Type (Etype (N))
7191 then
7192 case Nkind (N) is
7193 when N_Op_And =>
7195 -- (False and XXX) = (XXX and False) = False
7197 Fold :=
7198 (Compile_Time_Known_Value (Op1)
7199 and then Is_False (Expr_Value (Op1))
7200 and then Side_Effect_Free (Op2))
7201 or else (Compile_Time_Known_Value (Op2)
7202 and then Is_False (Expr_Value (Op2))
7203 and then Side_Effect_Free (Op1));
7205 when N_Op_Or =>
7207 -- (True and XXX) = (XXX and True) = True
7209 Fold :=
7210 (Compile_Time_Known_Value (Op1)
7211 and then Is_True (Expr_Value (Op1))
7212 and then Side_Effect_Free (Op2))
7213 or else (Compile_Time_Known_Value (Op2)
7214 and then Is_True (Expr_Value (Op2))
7215 and then Side_Effect_Free (Op1));
7217 when others => null;
7218 end case;
7219 end if;
7221 return;
7223 -- Else result is static and foldable. Both operands are static, and
7224 -- neither raises Constraint_Error, so we can definitely fold.
7226 else
7227 Set_Is_Static_Expression (N);
7228 Fold := True;
7229 Stat := True;
7230 return;
7231 end if;
7232 end Test_Expression_Is_Foldable;
7234 -------------------
7235 -- Test_In_Range --
7236 -------------------
7238 function Test_In_Range
7239 (N : Node_Id;
7240 Typ : Entity_Id;
7241 Assume_Valid : Boolean;
7242 Fixed_Int : Boolean;
7243 Int_Real : Boolean) return Range_Membership
7245 Val : Uint;
7246 Valr : Ureal;
7248 pragma Warnings (Off, Assume_Valid);
7249 -- For now Assume_Valid is unreferenced since the current implementation
7250 -- always returns Unknown if N is not a compile-time-known value, but we
7251 -- keep the parameter to allow for future enhancements in which we try
7252 -- to get the information in the variable case as well.
7254 begin
7255 -- If an error was posted on expression, then return Unknown, we do not
7256 -- want cascaded errors based on some false analysis of a junk node.
7258 if Error_Posted (N) then
7259 return Unknown;
7261 -- Expression that raises Constraint_Error is an odd case. We certainly
7262 -- do not want to consider it to be in range. It might make sense to
7263 -- consider it always out of range, but this causes incorrect error
7264 -- messages about static expressions out of range. So we just return
7265 -- Unknown, which is always safe.
7267 elsif Raises_Constraint_Error (N) then
7268 return Unknown;
7270 -- Universal types have no range limits, so always in range
7272 elsif Is_Universal_Numeric_Type (Typ) then
7273 return In_Range;
7275 -- Never known if not scalar type. Don't know if this can actually
7276 -- happen, but our spec allows it, so we must check.
7278 elsif not Is_Scalar_Type (Typ) then
7279 return Unknown;
7281 -- Never known if this is a generic type, since the bounds of generic
7282 -- types are junk. Note that if we only checked for static expressions
7283 -- (instead of compile-time-known values) below, we would not need this
7284 -- check, because values of a generic type can never be static, but they
7285 -- can be known at compile time.
7287 elsif Is_Generic_Type (Typ) then
7288 return Unknown;
7290 -- Case of a known compile time value, where we can check if it is in
7291 -- the bounds of the given type.
7293 elsif Compile_Time_Known_Value (N) then
7294 declare
7295 Lo : constant Node_Id := Type_Low_Bound (Typ);
7296 Hi : constant Node_Id := Type_High_Bound (Typ);
7297 LB_Known : constant Boolean := Compile_Time_Known_Value (Lo);
7298 HB_Known : constant Boolean := Compile_Time_Known_Value (Hi);
7300 begin
7301 -- Fixed point types should be considered as such only if flag
7302 -- Fixed_Int is set to False.
7304 if Is_Floating_Point_Type (Typ)
7305 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
7306 or else Int_Real
7307 then
7308 Valr := Expr_Value_R (N);
7310 if LB_Known and HB_Known then
7311 if Valr >= Expr_Value_R (Lo)
7312 and then
7313 Valr <= Expr_Value_R (Hi)
7314 then
7315 return In_Range;
7316 else
7317 return Out_Of_Range;
7318 end if;
7320 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
7321 or else
7322 (HB_Known and then Valr > Expr_Value_R (Hi))
7323 then
7324 return Out_Of_Range;
7326 else
7327 return Unknown;
7328 end if;
7330 else
7331 Val := Expr_Value (N);
7333 if LB_Known and HB_Known then
7334 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
7335 then
7336 return In_Range;
7337 else
7338 return Out_Of_Range;
7339 end if;
7341 elsif (LB_Known and then Val < Expr_Value (Lo))
7342 or else
7343 (HB_Known and then Val > Expr_Value (Hi))
7344 then
7345 return Out_Of_Range;
7347 else
7348 return Unknown;
7349 end if;
7350 end if;
7351 end;
7353 -- Here for value not known at compile time. Case of expression subtype
7354 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
7355 -- In this case we know it is in range without knowing its value.
7357 elsif Assume_Valid
7358 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
7359 then
7360 return In_Range;
7362 -- Another special case. For signed integer types, if the target type
7363 -- has Is_Known_Valid set, and the source type does not have a larger
7364 -- size, then the source value must be in range. We exclude biased
7365 -- types, because they bizarrely can generate out of range values.
7367 elsif Is_Signed_Integer_Type (Etype (N))
7368 and then Is_Known_Valid (Typ)
7369 and then Esize (Etype (N)) <= Esize (Typ)
7370 and then not Has_Biased_Representation (Etype (N))
7371 then
7372 return In_Range;
7374 -- For all other cases, result is unknown
7376 else
7377 return Unknown;
7378 end if;
7379 end Test_In_Range;
7381 --------------
7382 -- To_Bits --
7383 --------------
7385 procedure To_Bits (U : Uint; B : out Bits) is
7386 begin
7387 for J in 0 .. B'Last loop
7388 B (J) := (U / (2 ** J)) mod 2 /= 0;
7389 end loop;
7390 end To_Bits;
7392 --------------------
7393 -- Why_Not_Static --
7394 --------------------
7396 procedure Why_Not_Static (Expr : Node_Id) is
7397 N : constant Node_Id := Original_Node (Expr);
7398 Typ : Entity_Id := Empty;
7399 E : Entity_Id;
7400 Alt : Node_Id;
7401 Exp : Node_Id;
7403 procedure Why_Not_Static_List (L : List_Id);
7404 -- A version that can be called on a list of expressions. Finds all
7405 -- non-static violations in any element of the list.
7407 -------------------------
7408 -- Why_Not_Static_List --
7409 -------------------------
7411 procedure Why_Not_Static_List (L : List_Id) is
7412 N : Node_Id;
7413 begin
7414 N := First (L);
7415 while Present (N) loop
7416 Why_Not_Static (N);
7417 Next (N);
7418 end loop;
7419 end Why_Not_Static_List;
7421 -- Start of processing for Why_Not_Static
7423 begin
7424 -- Ignore call on error or empty node
7426 if No (Expr) or else Nkind (Expr) = N_Error then
7427 return;
7428 end if;
7430 -- Preprocessing for sub expressions
7432 if Nkind (Expr) in N_Subexpr then
7434 -- Nothing to do if expression is static
7436 if Is_OK_Static_Expression (Expr) then
7437 return;
7438 end if;
7440 -- Test for Constraint_Error raised
7442 if Raises_Constraint_Error (Expr) then
7444 -- Special case membership to find out which piece to flag
7446 if Nkind (N) in N_Membership_Test then
7447 if Raises_Constraint_Error (Left_Opnd (N)) then
7448 Why_Not_Static (Left_Opnd (N));
7449 return;
7451 elsif Present (Right_Opnd (N))
7452 and then Raises_Constraint_Error (Right_Opnd (N))
7453 then
7454 Why_Not_Static (Right_Opnd (N));
7455 return;
7457 else
7458 pragma Assert (Present (Alternatives (N)));
7460 Alt := First (Alternatives (N));
7461 while Present (Alt) loop
7462 if Raises_Constraint_Error (Alt) then
7463 Why_Not_Static (Alt);
7464 return;
7465 else
7466 Next (Alt);
7467 end if;
7468 end loop;
7469 end if;
7471 -- Special case a range to find out which bound to flag
7473 elsif Nkind (N) = N_Range then
7474 if Raises_Constraint_Error (Low_Bound (N)) then
7475 Why_Not_Static (Low_Bound (N));
7476 return;
7478 elsif Raises_Constraint_Error (High_Bound (N)) then
7479 Why_Not_Static (High_Bound (N));
7480 return;
7481 end if;
7483 -- Special case attribute to see which part to flag
7485 elsif Nkind (N) = N_Attribute_Reference then
7486 if Raises_Constraint_Error (Prefix (N)) then
7487 Why_Not_Static (Prefix (N));
7488 return;
7489 end if;
7491 Exp := First (Expressions (N));
7492 while Present (Exp) loop
7493 if Raises_Constraint_Error (Exp) then
7494 Why_Not_Static (Exp);
7495 return;
7496 end if;
7498 Next (Exp);
7499 end loop;
7501 -- Special case a subtype name
7503 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
7504 Error_Msg_NE
7505 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
7506 return;
7507 end if;
7509 -- End of special cases
7511 Error_Msg_N
7512 ("!expression raises exception, cannot be static (RM 4.9(34))",
7514 return;
7515 end if;
7517 -- If no type, then something is pretty wrong, so ignore
7519 Typ := Etype (Expr);
7521 if No (Typ) then
7522 return;
7523 end if;
7525 -- Type must be scalar or string type (but allow Bignum, since this
7526 -- is really a scalar type from our point of view in this diagnosis).
7528 if not Is_Scalar_Type (Typ)
7529 and then not Is_String_Type (Typ)
7530 and then not Is_RTE (Typ, RE_Bignum)
7531 then
7532 Error_Msg_N
7533 ("!static expression must have scalar or string type " &
7534 "(RM 4.9(2))", N);
7535 return;
7536 end if;
7537 end if;
7539 -- If we got through those checks, test particular node kind
7541 case Nkind (N) is
7543 -- Entity name
7545 when N_Expanded_Name
7546 | N_Identifier
7547 | N_Operator_Symbol
7549 E := Entity (N);
7551 if Is_Named_Number (E) then
7552 null;
7554 elsif Ekind (E) = E_Constant then
7556 -- One case we can give a better message is when we have a
7557 -- string literal created by concatenating an aggregate with
7558 -- an others expression.
7560 Entity_Case : declare
7561 CV : constant Node_Id := Constant_Value (E);
7562 CO : constant Node_Id := Original_Node (CV);
7564 function Is_Aggregate (N : Node_Id) return Boolean;
7565 -- See if node N came from an others aggregate, if so
7566 -- return True and set Error_Msg_Sloc to aggregate.
7568 ------------------
7569 -- Is_Aggregate --
7570 ------------------
7572 function Is_Aggregate (N : Node_Id) return Boolean is
7573 begin
7574 if Nkind (Original_Node (N)) = N_Aggregate then
7575 Error_Msg_Sloc := Sloc (Original_Node (N));
7576 return True;
7578 elsif Is_Entity_Name (N)
7579 and then Ekind (Entity (N)) = E_Constant
7580 and then
7581 Nkind (Original_Node (Constant_Value (Entity (N)))) =
7582 N_Aggregate
7583 then
7584 Error_Msg_Sloc :=
7585 Sloc (Original_Node (Constant_Value (Entity (N))));
7586 return True;
7588 else
7589 return False;
7590 end if;
7591 end Is_Aggregate;
7593 -- Start of processing for Entity_Case
7595 begin
7596 if Is_Aggregate (CV)
7597 or else (Nkind (CO) = N_Op_Concat
7598 and then (Is_Aggregate (Left_Opnd (CO))
7599 or else
7600 Is_Aggregate (Right_Opnd (CO))))
7601 then
7602 Error_Msg_N ("!aggregate (#) is never static", N);
7604 elsif No (CV) or else not Is_Static_Expression (CV) then
7605 Error_Msg_NE
7606 ("!& is not a static constant (RM 4.9(5))", N, E);
7607 end if;
7608 end Entity_Case;
7610 elsif Is_Type (E) then
7611 Error_Msg_NE
7612 ("!& is not a static subtype (RM 4.9(26))", N, E);
7614 else
7615 Error_Msg_NE
7616 ("!& is not static constant or named number "
7617 & "(RM 4.9(5))", N, E);
7618 end if;
7620 -- Binary operator
7622 when N_Binary_Op
7623 | N_Membership_Test
7624 | N_Short_Circuit
7626 if Nkind (N) in N_Op_Shift then
7627 Error_Msg_N
7628 ("!shift functions are never static (RM 4.9(6,18))", N);
7629 else
7630 Why_Not_Static (Left_Opnd (N));
7631 Why_Not_Static (Right_Opnd (N));
7632 end if;
7634 -- Unary operator
7636 when N_Unary_Op =>
7637 Why_Not_Static (Right_Opnd (N));
7639 -- Attribute reference
7641 when N_Attribute_Reference =>
7642 Why_Not_Static_List (Expressions (N));
7644 E := Etype (Prefix (N));
7646 if E = Standard_Void_Type then
7647 return;
7648 end if;
7650 -- Special case non-scalar'Size since this is a common error
7652 if Attribute_Name (N) = Name_Size then
7653 Error_Msg_N
7654 ("!size attribute is only static for static scalar type "
7655 & "(RM 4.9(7,8))", N);
7657 -- Flag array cases
7659 elsif Is_Array_Type (E) then
7660 if Attribute_Name (N)
7661 not in Name_First | Name_Last | Name_Length
7662 then
7663 Error_Msg_N
7664 ("!static array attribute must be Length, First, or Last "
7665 & "(RM 4.9(8))", N);
7667 -- Since we know the expression is not-static (we already
7668 -- tested for this, must mean array is not static).
7670 else
7671 Error_Msg_N
7672 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
7673 end if;
7675 return;
7677 -- Special case generic types, since again this is a common source
7678 -- of confusion.
7680 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
7681 Error_Msg_N
7682 ("!attribute of generic type is never static "
7683 & "(RM 4.9(7,8))", N);
7685 elsif Is_OK_Static_Subtype (E) then
7686 null;
7688 elsif Is_Scalar_Type (E) then
7689 Error_Msg_N
7690 ("!prefix type for attribute is not static scalar subtype "
7691 & "(RM 4.9(7))", N);
7693 else
7694 Error_Msg_N
7695 ("!static attribute must apply to array/scalar type "
7696 & "(RM 4.9(7,8))", N);
7697 end if;
7699 -- String literal
7701 when N_String_Literal =>
7702 Error_Msg_N
7703 ("!subtype of string literal is non-static (RM 4.9(4))", N);
7705 -- Explicit dereference
7707 when N_Explicit_Dereference =>
7708 Error_Msg_N
7709 ("!explicit dereference is never static (RM 4.9)", N);
7711 -- Function call
7713 when N_Function_Call =>
7714 Why_Not_Static_List (Parameter_Associations (N));
7716 -- Complain about non-static function call unless we have Bignum
7717 -- which means that the underlying expression is really some
7718 -- scalar arithmetic operation.
7720 if not Is_RTE (Typ, RE_Bignum) then
7721 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
7722 end if;
7724 -- Parameter assocation (test actual parameter)
7726 when N_Parameter_Association =>
7727 Why_Not_Static (Explicit_Actual_Parameter (N));
7729 -- Indexed component
7731 when N_Indexed_Component =>
7732 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
7734 -- Procedure call
7736 when N_Procedure_Call_Statement =>
7737 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
7739 -- Qualified expression (test expression)
7741 when N_Qualified_Expression =>
7742 Why_Not_Static (Expression (N));
7744 -- Aggregate
7746 when N_Aggregate
7747 | N_Extension_Aggregate
7749 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
7751 -- Range
7753 when N_Range =>
7754 Why_Not_Static (Low_Bound (N));
7755 Why_Not_Static (High_Bound (N));
7757 -- Range constraint, test range expression
7759 when N_Range_Constraint =>
7760 Why_Not_Static (Range_Expression (N));
7762 -- Subtype indication, test constraint
7764 when N_Subtype_Indication =>
7765 Why_Not_Static (Constraint (N));
7767 -- Selected component
7769 when N_Selected_Component =>
7770 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
7772 -- Slice
7774 when N_Slice =>
7775 Error_Msg_N ("!slice is never static (RM 4.9)", N);
7777 when N_Type_Conversion =>
7778 Why_Not_Static (Expression (N));
7780 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
7781 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
7782 then
7783 Error_Msg_N
7784 ("!static conversion requires static scalar subtype result "
7785 & "(RM 4.9(9))", N);
7786 end if;
7788 -- Unchecked type conversion
7790 when N_Unchecked_Type_Conversion =>
7791 Error_Msg_N
7792 ("!unchecked type conversion is never static (RM 4.9)", N);
7794 -- All other cases, no reason to give
7796 when others =>
7797 null;
7798 end case;
7799 end Why_Not_Static;
7801 end Sem_Eval;