Remove not needed __builtin_expect due to malloc predictor.
[official-gcc.git] / gcc / ada / sem_eval.adb
blob4560a512fb8563f263fa71d381cb1713fd6d2921
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-2018, 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 Elists; use Elists;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Util; use Exp_Util;
35 with Freeze; use Freeze;
36 with Lib; use Lib;
37 with Namet; use Namet;
38 with Nmake; use Nmake;
39 with Nlists; use Nlists;
40 with Opt; use Opt;
41 with Par_SCO; use Par_SCO;
42 with Rtsfind; use Rtsfind;
43 with Sem; use Sem;
44 with Sem_Aux; use Sem_Aux;
45 with Sem_Cat; use Sem_Cat;
46 with Sem_Ch6; use Sem_Ch6;
47 with Sem_Ch8; use Sem_Ch8;
48 with Sem_Res; use Sem_Res;
49 with Sem_Util; use Sem_Util;
50 with Sem_Type; use Sem_Type;
51 with Sem_Warn; use Sem_Warn;
52 with Sinfo; use Sinfo;
53 with Snames; use Snames;
54 with Stand; use Stand;
55 with Stringt; use Stringt;
56 with Tbuild; use Tbuild;
58 package body Sem_Eval is
60 -----------------------------------------
61 -- Handling of Compile Time Evaluation --
62 -----------------------------------------
64 -- The compile time evaluation of expressions is distributed over several
65 -- Eval_xxx procedures. These procedures are called immediately after
66 -- a subexpression is resolved and is therefore accomplished in a bottom
67 -- up fashion. The flags are synthesized using the following approach.
69 -- Is_Static_Expression is determined by following the detailed rules
70 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
71 -- flag of the operands in many cases.
73 -- Raises_Constraint_Error is set if any of the operands have the flag
74 -- set or if an attempt to compute the value of the current expression
75 -- results in detection of a runtime constraint error.
77 -- As described in the spec, the requirement is that Is_Static_Expression
78 -- be accurately set, and in addition for nodes for which this flag is set,
79 -- Raises_Constraint_Error must also be set. Furthermore a node which has
80 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
81 -- requirement is that the expression value must be precomputed, and the
82 -- node is either a literal, or the name of a constant entity whose value
83 -- is a static expression.
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;
124 V : Uint;
125 end record;
127 type Match_Result is (Match, No_Match, Non_Static);
128 -- Result returned from functions that test for a matching result. If the
129 -- operands are not OK_Static then Non_Static will be returned. Otherwise
130 -- Match/No_Match is returned depending on whether the match succeeds.
132 type CV_Cache_Array is array (CV_Range) of CV_Entry;
134 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
135 -- This is the actual cache, with entries consisting of node/value pairs,
136 -- and the impossible value Node_High_Bound used for unset entries.
138 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
139 -- Range membership may either be statically known to be in range or out
140 -- of range, or not statically known. Used for Test_In_Range below.
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 function Choice_Matches
147 (Expr : Node_Id;
148 Choice : Node_Id) return Match_Result;
149 -- Determines whether given value Expr matches the given Choice. The Expr
150 -- can be of discrete, real, or string type and must be a compile time
151 -- known value (it is an error to make the call if these conditions are
152 -- not met). The choice can be a range, subtype name, subtype indication,
153 -- or expression. The returned result is Non_Static if Choice is not
154 -- OK_Static, otherwise either Match or No_Match is returned depending
155 -- on whether Choice matches Expr. This is used for case expression
156 -- alternatives, and also for membership tests. In each case, more
157 -- possibilities are tested than the syntax allows (e.g. membership allows
158 -- subtype indications and non-discrete types, and case allows an OTHERS
159 -- choice), but it does not matter, since we have already done a full
160 -- semantic and syntax check of the construct, so the extra possibilities
161 -- just will not arise for correct expressions.
163 -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g
164 -- a reference to a type, one of whose bounds raises Constraint_Error, then
165 -- it also sets the Raises_Constraint_Error flag on the Choice itself.
167 function Choices_Match
168 (Expr : Node_Id;
169 Choices : List_Id) return Match_Result;
170 -- This function applies Choice_Matches to each element of Choices. If the
171 -- result is No_Match, then it continues and checks the next element. If
172 -- the result is Match or Non_Static, this result is immediately given
173 -- as the result without checking the rest of the list. Expr can be of
174 -- discrete, real, or string type and must be a compile-time-known value
175 -- (it is an error to make the call if these conditions are not met).
177 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
178 -- Check whether an arithmetic operation with universal operands which is a
179 -- rewritten function call with an explicit scope indication is ambiguous:
180 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
181 -- type declared in P and the context does not impose a type on the result
182 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
183 -- error and return Empty, else return the result type of the operator.
185 function From_Bits (B : Bits; T : Entity_Id) return Uint;
186 -- Converts a bit string of length B'Length to a Uint value to be used for
187 -- a target of type T, which is a modular type. This procedure includes the
188 -- necessary reduction by the modulus in the case of a nonbinary modulus
189 -- (for a binary modulus, the bit string is the right length any way so all
190 -- is well).
192 function Get_String_Val (N : Node_Id) return Node_Id;
193 -- Given a tree node for a folded string or character value, returns the
194 -- corresponding string literal or character literal (one of the two must
195 -- be available, or the operand would not have been marked as foldable in
196 -- the earlier analysis of the operation).
198 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean;
199 -- Given a choice (from a case expression or membership test), returns
200 -- True if the choice is static and does not raise a Constraint_Error.
202 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean;
203 -- Given a choice list (from a case expression or membership test), return
204 -- True if all choices are static in the sense of Is_OK_Static_Choice.
206 function Is_Static_Choice (Choice : Node_Id) return Boolean;
207 -- Given a choice (from a case expression or membership test), returns
208 -- True if the choice is static. No test is made for raising of constraint
209 -- error, so this function is used only for legality tests.
211 function Is_Static_Choice_List (Choices : List_Id) return Boolean;
212 -- Given a choice list (from a case expression or membership test), return
213 -- True if all choices are static in the sense of Is_Static_Choice.
215 function Is_Static_Range (N : Node_Id) return Boolean;
216 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
217 -- argument is an N_Range node (but note that the semantic analysis of
218 -- equivalent range attribute references already turned them into the
219 -- equivalent range). This differs from Is_OK_Static_Range (which is what
220 -- must be used by clients) in that it does not care whether the bounds
221 -- raise Constraint_Error or not. Used for checking whether expressions are
222 -- static in the 4.9 sense (without worrying about exceptions).
224 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
225 -- Bits represents the number of bits in an integer value to be computed
226 -- (but the value has not been computed yet). If this value in Bits is
227 -- reasonable, a result of True is returned, with the implication that the
228 -- caller should go ahead and complete the calculation. If the value in
229 -- Bits is unreasonably large, then an error is posted on node N, and
230 -- False is returned (and the caller skips the proposed calculation).
232 procedure Out_Of_Range (N : Node_Id);
233 -- This procedure is called if it is determined that node N, which appears
234 -- in a non-static context, is a compile-time-known value which is outside
235 -- its range, i.e. the range of Etype. This is used in contexts where
236 -- this is an illegality if N is static, and should generate a warning
237 -- otherwise.
239 function Real_Or_String_Static_Predicate_Matches
240 (Val : Node_Id;
241 Typ : Entity_Id) return Boolean;
242 -- This is the function used to evaluate real or string static predicates.
243 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
244 -- represents the value to be tested against the predicate. Typ is the
245 -- type with the predicate, from which the predicate expression can be
246 -- extracted. The result returned is True if the given value satisfies
247 -- the predicate.
249 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
250 -- N and Exp are nodes representing an expression, Exp is known to raise
251 -- CE. N is rewritten in term of Exp in the optimal way.
253 function String_Type_Len (Stype : Entity_Id) return Uint;
254 -- Given a string type, determines the length of the index type, or, if
255 -- this index type is non-static, the length of the base type of this index
256 -- type. Note that if the string type is itself static, then the index type
257 -- is static, so the second case applies only if the string type passed is
258 -- non-static.
260 function Test (Cond : Boolean) return Uint;
261 pragma Inline (Test);
262 -- This function simply returns the appropriate Boolean'Pos value
263 -- corresponding to the value of Cond as a universal integer. It is
264 -- used for producing the result of the static evaluation of the
265 -- logical operators
267 procedure Test_Expression_Is_Foldable
268 (N : Node_Id;
269 Op1 : Node_Id;
270 Stat : out Boolean;
271 Fold : out Boolean);
272 -- Tests to see if expression N whose single operand is Op1 is foldable,
273 -- i.e. the operand value is known at compile time. If the operation is
274 -- foldable, then Fold is True on return, and Stat indicates whether the
275 -- result is static (i.e. the operand was static). Note that it is quite
276 -- possible for Fold to be True, and Stat to be False, since there are
277 -- cases in which we know the value of an operand even though it is not
278 -- technically static (e.g. the static lower bound of a range whose upper
279 -- bound is non-static).
281 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes
282 -- a call to Check_Non_Static_Context on the operand. If Fold is False on
283 -- return, then all processing is complete, and the caller should return,
284 -- since there is nothing else to do.
286 -- If Stat is set True on return, then Is_Static_Expression is also set
287 -- true in node N. There are some cases where this is over-enthusiastic,
288 -- e.g. in the two operand case below, for string comparison, the result is
289 -- not static even though the two operands are static. In such cases, the
290 -- caller must reset the Is_Static_Expression flag in N.
292 -- If Fold and Stat are both set to False then this routine performs also
293 -- the following extra actions:
295 -- If either operand is Any_Type then propagate it to result to prevent
296 -- cascaded errors.
298 -- If some operand raises constraint error, then replace the node N
299 -- with the raise constraint error node. This replacement inherits the
300 -- Is_Static_Expression flag from the operands.
302 procedure Test_Expression_Is_Foldable
303 (N : Node_Id;
304 Op1 : Node_Id;
305 Op2 : Node_Id;
306 Stat : out Boolean;
307 Fold : out Boolean;
308 CRT_Safe : Boolean := False);
309 -- Same processing, except applies to an expression N with two operands
310 -- Op1 and Op2. The result is static only if both operands are static. If
311 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
312 -- for the tests that the two operands are known at compile time. See
313 -- spec of this routine for further details.
315 function Test_In_Range
316 (N : Node_Id;
317 Typ : Entity_Id;
318 Assume_Valid : Boolean;
319 Fixed_Int : Boolean;
320 Int_Real : Boolean) return Range_Membership;
321 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
322 -- or Out_Of_Range if it can be guaranteed at compile time that expression
323 -- N is known to be in or out of range of the subtype Typ. If not compile
324 -- time known, Unknown is returned. See documentation of Is_In_Range for
325 -- complete description of parameters.
327 procedure To_Bits (U : Uint; B : out Bits);
328 -- Converts a Uint value to a bit string of length B'Length
330 -----------------------------------------------
331 -- Check_Expression_Against_Static_Predicate --
332 -----------------------------------------------
334 procedure Check_Expression_Against_Static_Predicate
335 (Expr : Node_Id;
336 Typ : Entity_Id)
338 begin
339 -- Nothing to do if expression is not known at compile time, or the
340 -- type has no static predicate set (will be the case for all non-scalar
341 -- types, so no need to make a special test for that).
343 if not (Has_Static_Predicate (Typ)
344 and then Compile_Time_Known_Value (Expr))
345 then
346 return;
347 end if;
349 -- Here we have a static predicate (note that it could have arisen from
350 -- an explicitly specified Dynamic_Predicate whose expression met the
351 -- rules for being predicate-static). If the expression is known at
352 -- compile time and obeys the predicate, then it is static and must be
353 -- labeled as such, which matters e.g. for case statements. The original
354 -- expression may be a type conversion of a variable with a known value,
355 -- which might otherwise not be marked static.
357 -- Case of real static predicate
359 if Is_Real_Type (Typ) then
360 if Real_Or_String_Static_Predicate_Matches
361 (Val => Make_Real_Literal (Sloc (Expr), Expr_Value_R (Expr)),
362 Typ => Typ)
363 then
364 Set_Is_Static_Expression (Expr);
365 return;
366 end if;
368 -- Case of string static predicate
370 elsif Is_String_Type (Typ) then
371 if Real_Or_String_Static_Predicate_Matches
372 (Val => Expr_Value_S (Expr), Typ => Typ)
373 then
374 Set_Is_Static_Expression (Expr);
375 return;
376 end if;
378 -- Case of discrete static predicate
380 else
381 pragma Assert (Is_Discrete_Type (Typ));
383 -- If static predicate matches, nothing to do
385 if Choices_Match (Expr, Static_Discrete_Predicate (Typ)) = Match then
386 Set_Is_Static_Expression (Expr);
387 return;
388 end if;
389 end if;
391 -- Here we know that the predicate will fail
393 -- Special case of static expression failing a predicate (other than one
394 -- that was explicitly specified with a Dynamic_Predicate aspect). This
395 -- is the case where the expression is no longer considered static.
397 if Is_Static_Expression (Expr)
398 and then not Has_Dynamic_Predicate_Aspect (Typ)
399 then
400 Error_Msg_NE
401 ("??static expression fails static predicate check on &",
402 Expr, Typ);
403 Error_Msg_N
404 ("\??expression is no longer considered static", Expr);
405 Set_Is_Static_Expression (Expr, False);
407 -- In all other cases, this is just a warning that a test will fail.
408 -- It does not matter if the expression is static or not, or if the
409 -- predicate comes from a dynamic predicate aspect or not.
411 else
412 Error_Msg_NE
413 ("??expression fails predicate check on &", Expr, Typ);
414 end if;
415 end Check_Expression_Against_Static_Predicate;
417 ------------------------------
418 -- Check_Non_Static_Context --
419 ------------------------------
421 procedure Check_Non_Static_Context (N : Node_Id) is
422 T : constant Entity_Id := Etype (N);
423 Checks_On : constant Boolean :=
424 not Index_Checks_Suppressed (T)
425 and not Range_Checks_Suppressed (T);
427 begin
428 -- Ignore cases of non-scalar types, error types, or universal real
429 -- types that have no usable bounds.
431 if T = Any_Type
432 or else not Is_Scalar_Type (T)
433 or else T = Universal_Fixed
434 or else T = Universal_Real
435 then
436 return;
437 end if;
439 -- At this stage we have a scalar type. If we have an expression that
440 -- raises CE, then we already issued a warning or error msg so there is
441 -- nothing more to be done in this routine.
443 if Raises_Constraint_Error (N) then
444 return;
445 end if;
447 -- Now we have a scalar type which is not marked as raising a constraint
448 -- error exception. The main purpose of this routine is to deal with
449 -- static expressions appearing in a non-static context. That means
450 -- that if we do not have a static expression then there is not much
451 -- to do. The one case that we deal with here is that if we have a
452 -- floating-point value that is out of range, then we post a warning
453 -- that an infinity will result.
455 if not Is_Static_Expression (N) then
456 if Is_Floating_Point_Type (T) then
457 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
458 Error_Msg_N
459 ("??float value out of range, infinity will be generated", N);
461 -- The literal may be the result of constant-folding of a non-
462 -- static subexpression of a larger expression (e.g. a conversion
463 -- of a non-static variable whose value happens to be known). At
464 -- this point we must reduce the value of the subexpression to a
465 -- machine number (RM 4.9 (38/2)).
467 elsif Nkind (N) = N_Real_Literal
468 and then Nkind (Parent (N)) in N_Subexpr
469 then
470 Rewrite (N, New_Copy (N));
471 Set_Realval
472 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
473 end if;
474 end if;
476 return;
477 end if;
479 -- Here we have the case of outer level static expression of scalar
480 -- type, where the processing of this procedure is needed.
482 -- For real types, this is where we convert the value to a machine
483 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
484 -- need to do this if the parent is a constant declaration, since in
485 -- other cases, gigi should do the necessary conversion correctly, but
486 -- experimentation shows that this is not the case on all machines, in
487 -- particular if we do not convert all literals to machine values in
488 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
489 -- and SGI/Irix.
491 -- This conversion is always done by GNATprove on real literals in
492 -- non-static expressions, by calling Check_Non_Static_Context from
493 -- gnat2why, as GNATprove cannot do the conversion later contrary
494 -- to gigi. The frontend computes the information about which
495 -- expressions are static, which is used by gnat2why to call
496 -- Check_Non_Static_Context on exactly those real literals that are
497 -- not subexpressions of static expressions.
499 if Nkind (N) = N_Real_Literal
500 and then not Is_Machine_Number (N)
501 and then not Is_Generic_Type (Etype (N))
502 and then Etype (N) /= Universal_Real
503 then
504 -- Check that value is in bounds before converting to machine
505 -- number, so as not to lose case where value overflows in the
506 -- least significant bit or less. See B490001.
508 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
509 Out_Of_Range (N);
510 return;
511 end if;
513 -- Note: we have to copy the node, to avoid problems with conformance
514 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
516 Rewrite (N, New_Copy (N));
518 if not Is_Floating_Point_Type (T) then
519 Set_Realval
520 (N, Corresponding_Integer_Value (N) * Small_Value (T));
522 elsif not UR_Is_Zero (Realval (N)) then
524 -- Note: even though RM 4.9(38) specifies biased rounding, this
525 -- has been modified by AI-100 in order to prevent confusing
526 -- differences in rounding between static and non-static
527 -- expressions. AI-100 specifies that the effect of such rounding
528 -- is implementation dependent, and in GNAT we round to nearest
529 -- even to match the run-time behavior. Note that this applies
530 -- to floating point literals, not fixed points ones, even though
531 -- their compiler representation is also as a universal real.
533 Set_Realval
534 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
535 Set_Is_Machine_Number (N);
536 end if;
538 end if;
540 -- Check for out of range universal integer. This is a non-static
541 -- context, so the integer value must be in range of the runtime
542 -- representation of universal integers.
544 -- We do this only within an expression, because that is the only
545 -- case in which non-static universal integer values can occur, and
546 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
547 -- called in contexts like the expression of a number declaration where
548 -- we certainly want to allow out of range values.
550 -- We inhibit the warning when expansion is disabled, because the
551 -- preanalysis of a range of a 64-bit modular type may appear to
552 -- violate the constraint on non-static Universal_Integer. If there
553 -- is a true overflow it will be diagnosed during full analysis.
555 if Etype (N) = Universal_Integer
556 and then Nkind (N) = N_Integer_Literal
557 and then Nkind (Parent (N)) in N_Subexpr
558 and then Expander_Active
559 and then
560 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
561 or else
562 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
563 then
564 Apply_Compile_Time_Constraint_Error
565 (N, "non-static universal integer value out of range<<",
566 CE_Range_Check_Failed);
568 -- Check out of range of base type
570 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
571 Out_Of_Range (N);
573 -- Give warning if outside subtype (where one or both of the bounds of
574 -- the subtype is static). This warning is omitted if the expression
575 -- appears in a range that could be null (warnings are handled elsewhere
576 -- for this case).
578 elsif T /= Base_Type (T) and then Nkind (Parent (N)) /= N_Range then
579 if Is_In_Range (N, T, Assume_Valid => True) then
580 null;
582 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
584 -- Ignore out of range values for System.Priority in CodePeer
585 -- mode since the actual target compiler may provide a wider
586 -- range.
588 if CodePeer_Mode and then T = RTE (RE_Priority) then
589 Set_Do_Range_Check (N, False);
590 else
591 Apply_Compile_Time_Constraint_Error
592 (N, "value not in range of}<<", CE_Range_Check_Failed);
593 end if;
595 elsif Checks_On then
596 Enable_Range_Check (N);
598 else
599 Set_Do_Range_Check (N, False);
600 end if;
601 end if;
602 end Check_Non_Static_Context;
604 ---------------------------------
605 -- Check_String_Literal_Length --
606 ---------------------------------
608 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
609 begin
610 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
611 if UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
612 then
613 Apply_Compile_Time_Constraint_Error
614 (N, "string length wrong for}??",
615 CE_Length_Check_Failed,
616 Ent => Ttype,
617 Typ => Ttype);
618 end if;
619 end if;
620 end Check_String_Literal_Length;
622 --------------------
623 -- Choice_Matches --
624 --------------------
626 function Choice_Matches
627 (Expr : Node_Id;
628 Choice : Node_Id) return Match_Result
630 Etyp : constant Entity_Id := Etype (Expr);
631 Val : Uint;
632 ValR : Ureal;
633 ValS : Node_Id;
635 begin
636 pragma Assert (Compile_Time_Known_Value (Expr));
637 pragma Assert (Is_Scalar_Type (Etyp) or else Is_String_Type (Etyp));
639 if not Is_OK_Static_Choice (Choice) then
640 Set_Raises_Constraint_Error (Choice);
641 return Non_Static;
643 -- When the choice denotes a subtype with a static predictate, check the
644 -- expression against the predicate values. Different procedures apply
645 -- to discrete and non-discrete types.
647 elsif (Nkind (Choice) = N_Subtype_Indication
648 or else (Is_Entity_Name (Choice)
649 and then Is_Type (Entity (Choice))))
650 and then Has_Predicates (Etype (Choice))
651 and then Has_Static_Predicate (Etype (Choice))
652 then
653 if Is_Discrete_Type (Etype (Choice)) then
654 return
655 Choices_Match
656 (Expr, Static_Discrete_Predicate (Etype (Choice)));
658 elsif Real_Or_String_Static_Predicate_Matches (Expr, Etype (Choice))
659 then
660 return Match;
662 else
663 return No_Match;
664 end if;
666 -- Discrete type case only
668 elsif Is_Discrete_Type (Etyp) then
669 Val := Expr_Value (Expr);
671 if Nkind (Choice) = N_Range then
672 if Val >= Expr_Value (Low_Bound (Choice))
673 and then
674 Val <= Expr_Value (High_Bound (Choice))
675 then
676 return Match;
677 else
678 return No_Match;
679 end if;
681 elsif Nkind (Choice) = N_Subtype_Indication
682 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
683 then
684 if Val >= Expr_Value (Type_Low_Bound (Etype (Choice)))
685 and then
686 Val <= Expr_Value (Type_High_Bound (Etype (Choice)))
687 then
688 return Match;
689 else
690 return No_Match;
691 end if;
693 elsif Nkind (Choice) = N_Others_Choice then
694 return Match;
696 else
697 if Val = Expr_Value (Choice) then
698 return Match;
699 else
700 return No_Match;
701 end if;
702 end if;
704 -- Real type case
706 elsif Is_Real_Type (Etyp) then
707 ValR := Expr_Value_R (Expr);
709 if Nkind (Choice) = N_Range then
710 if ValR >= Expr_Value_R (Low_Bound (Choice))
711 and then
712 ValR <= Expr_Value_R (High_Bound (Choice))
713 then
714 return Match;
715 else
716 return No_Match;
717 end if;
719 elsif Nkind (Choice) = N_Subtype_Indication
720 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
721 then
722 if ValR >= Expr_Value_R (Type_Low_Bound (Etype (Choice)))
723 and then
724 ValR <= Expr_Value_R (Type_High_Bound (Etype (Choice)))
725 then
726 return Match;
727 else
728 return No_Match;
729 end if;
731 else
732 if ValR = Expr_Value_R (Choice) then
733 return Match;
734 else
735 return No_Match;
736 end if;
737 end if;
739 -- String type cases
741 else
742 pragma Assert (Is_String_Type (Etyp));
743 ValS := Expr_Value_S (Expr);
745 if Nkind (Choice) = N_Subtype_Indication
746 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
747 then
748 if not Is_Constrained (Etype (Choice)) then
749 return Match;
751 else
752 declare
753 Typlen : constant Uint :=
754 String_Type_Len (Etype (Choice));
755 Strlen : constant Uint :=
756 UI_From_Int (String_Length (Strval (ValS)));
757 begin
758 if Typlen = Strlen then
759 return Match;
760 else
761 return No_Match;
762 end if;
763 end;
764 end if;
766 else
767 if String_Equal (Strval (ValS), Strval (Expr_Value_S (Choice)))
768 then
769 return Match;
770 else
771 return No_Match;
772 end if;
773 end if;
774 end if;
775 end Choice_Matches;
777 -------------------
778 -- Choices_Match --
779 -------------------
781 function Choices_Match
782 (Expr : Node_Id;
783 Choices : List_Id) return Match_Result
785 Choice : Node_Id;
786 Result : Match_Result;
788 begin
789 Choice := First (Choices);
790 while Present (Choice) loop
791 Result := Choice_Matches (Expr, Choice);
793 if Result /= No_Match then
794 return Result;
795 end if;
797 Next (Choice);
798 end loop;
800 return No_Match;
801 end Choices_Match;
803 --------------------------
804 -- Compile_Time_Compare --
805 --------------------------
807 function Compile_Time_Compare
808 (L, R : Node_Id;
809 Assume_Valid : Boolean) return Compare_Result
811 Discard : aliased Uint;
812 begin
813 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
814 end Compile_Time_Compare;
816 function Compile_Time_Compare
817 (L, R : Node_Id;
818 Diff : access Uint;
819 Assume_Valid : Boolean;
820 Rec : Boolean := False) return Compare_Result
822 Ltyp : Entity_Id := Etype (L);
823 Rtyp : Entity_Id := Etype (R);
825 Discard : aliased Uint;
827 procedure Compare_Decompose
828 (N : Node_Id;
829 R : out Node_Id;
830 V : out Uint);
831 -- This procedure decomposes the node N into an expression node and a
832 -- signed offset, so that the value of N is equal to the value of R plus
833 -- the value V (which may be negative). If no such decomposition is
834 -- possible, then on return R is a copy of N, and V is set to zero.
836 function Compare_Fixup (N : Node_Id) return Node_Id;
837 -- This function deals with replacing 'Last and 'First references with
838 -- their corresponding type bounds, which we then can compare. The
839 -- argument is the original node, the result is the identity, unless we
840 -- have a 'Last/'First reference in which case the value returned is the
841 -- appropriate type bound.
843 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
844 -- Even if the context does not assume that values are valid, some
845 -- simple cases can be recognized.
847 function Is_Same_Value (L, R : Node_Id) return Boolean;
848 -- Returns True iff L and R represent expressions that definitely have
849 -- identical (but not necessarily compile-time-known) values Indeed the
850 -- caller is expected to have already dealt with the cases of compile
851 -- time known values, so these are not tested here.
853 -----------------------
854 -- Compare_Decompose --
855 -----------------------
857 procedure Compare_Decompose
858 (N : Node_Id;
859 R : out Node_Id;
860 V : out Uint)
862 begin
863 if Nkind (N) = N_Op_Add
864 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
865 then
866 R := Left_Opnd (N);
867 V := Intval (Right_Opnd (N));
868 return;
870 elsif Nkind (N) = N_Op_Subtract
871 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
872 then
873 R := Left_Opnd (N);
874 V := UI_Negate (Intval (Right_Opnd (N)));
875 return;
877 elsif Nkind (N) = N_Attribute_Reference then
878 if Attribute_Name (N) = Name_Succ then
879 R := First (Expressions (N));
880 V := Uint_1;
881 return;
883 elsif Attribute_Name (N) = Name_Pred then
884 R := First (Expressions (N));
885 V := Uint_Minus_1;
886 return;
887 end if;
888 end if;
890 R := N;
891 V := Uint_0;
892 end Compare_Decompose;
894 -------------------
895 -- Compare_Fixup --
896 -------------------
898 function Compare_Fixup (N : Node_Id) return Node_Id is
899 Indx : Node_Id;
900 Xtyp : Entity_Id;
901 Subs : Nat;
903 begin
904 -- Fixup only required for First/Last attribute reference
906 if Nkind (N) = N_Attribute_Reference
907 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
908 then
909 Xtyp := Etype (Prefix (N));
911 -- If we have no type, then just abandon the attempt to do
912 -- a fixup, this is probably the result of some other error.
914 if No (Xtyp) then
915 return N;
916 end if;
918 -- Dereference an access type
920 if Is_Access_Type (Xtyp) then
921 Xtyp := Designated_Type (Xtyp);
922 end if;
924 -- If we don't have an array type at this stage, something is
925 -- peculiar, e.g. another error, and we abandon the attempt at
926 -- a fixup.
928 if not Is_Array_Type (Xtyp) then
929 return N;
930 end if;
932 -- Ignore unconstrained array, since bounds are not meaningful
934 if not Is_Constrained (Xtyp) then
935 return N;
936 end if;
938 if Ekind (Xtyp) = E_String_Literal_Subtype then
939 if Attribute_Name (N) = Name_First then
940 return String_Literal_Low_Bound (Xtyp);
941 else
942 return
943 Make_Integer_Literal (Sloc (N),
944 Intval => Intval (String_Literal_Low_Bound (Xtyp)) +
945 String_Literal_Length (Xtyp));
946 end if;
947 end if;
949 -- Find correct index type
951 Indx := First_Index (Xtyp);
953 if Present (Expressions (N)) then
954 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
956 for J in 2 .. Subs loop
957 Indx := Next_Index (Indx);
958 end loop;
959 end if;
961 Xtyp := Etype (Indx);
963 if Attribute_Name (N) = Name_First then
964 return Type_Low_Bound (Xtyp);
965 else
966 return Type_High_Bound (Xtyp);
967 end if;
968 end if;
970 return N;
971 end Compare_Fixup;
973 ----------------------------
974 -- Is_Known_Valid_Operand --
975 ----------------------------
977 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
978 begin
979 return (Is_Entity_Name (Opnd)
980 and then
981 (Is_Known_Valid (Entity (Opnd))
982 or else Ekind (Entity (Opnd)) = E_In_Parameter
983 or else
984 (Ekind (Entity (Opnd)) in Object_Kind
985 and then Present (Current_Value (Entity (Opnd))))))
986 or else Is_OK_Static_Expression (Opnd);
987 end Is_Known_Valid_Operand;
989 -------------------
990 -- Is_Same_Value --
991 -------------------
993 function Is_Same_Value (L, R : Node_Id) return Boolean is
994 Lf : constant Node_Id := Compare_Fixup (L);
995 Rf : constant Node_Id := Compare_Fixup (R);
997 function Is_Same_Subscript (L, R : List_Id) return Boolean;
998 -- L, R are the Expressions values from two attribute nodes for First
999 -- or Last attributes. Either may be set to No_List if no expressions
1000 -- are present (indicating subscript 1). The result is True if both
1001 -- expressions represent the same subscript (note one case is where
1002 -- one subscript is missing and the other is explicitly set to 1).
1004 -----------------------
1005 -- Is_Same_Subscript --
1006 -----------------------
1008 function Is_Same_Subscript (L, R : List_Id) return Boolean is
1009 begin
1010 if L = No_List then
1011 if R = No_List then
1012 return True;
1013 else
1014 return Expr_Value (First (R)) = Uint_1;
1015 end if;
1017 else
1018 if R = No_List then
1019 return Expr_Value (First (L)) = Uint_1;
1020 else
1021 return Expr_Value (First (L)) = Expr_Value (First (R));
1022 end if;
1023 end if;
1024 end Is_Same_Subscript;
1026 -- Start of processing for Is_Same_Value
1028 begin
1029 -- Values are the same if they refer to the same entity and the
1030 -- entity is non-volatile. This does not however apply to Float
1031 -- types, since we may have two NaN values and they should never
1032 -- compare equal.
1034 -- If the entity is a discriminant, the two expressions may be bounds
1035 -- of components of objects of the same discriminated type. The
1036 -- values of the discriminants are not static, and therefore the
1037 -- result is unknown.
1039 -- It would be better to comment individual branches of this test ???
1041 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
1042 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
1043 and then Entity (Lf) = Entity (Rf)
1044 and then Ekind (Entity (Lf)) /= E_Discriminant
1045 and then Present (Entity (Lf))
1046 and then not Is_Floating_Point_Type (Etype (L))
1047 and then not Is_Volatile_Reference (L)
1048 and then not Is_Volatile_Reference (R)
1049 then
1050 return True;
1052 -- Or if they are compile-time-known and identical
1054 elsif Compile_Time_Known_Value (Lf)
1055 and then
1056 Compile_Time_Known_Value (Rf)
1057 and then Expr_Value (Lf) = Expr_Value (Rf)
1058 then
1059 return True;
1061 -- False if Nkind of the two nodes is different for remaining cases
1063 elsif Nkind (Lf) /= Nkind (Rf) then
1064 return False;
1066 -- True if both 'First or 'Last values applying to the same entity
1067 -- (first and last don't change even if value does). Note that we
1068 -- need this even with the calls to Compare_Fixup, to handle the
1069 -- case of unconstrained array attributes where Compare_Fixup
1070 -- cannot find useful bounds.
1072 elsif Nkind (Lf) = N_Attribute_Reference
1073 and then Attribute_Name (Lf) = Attribute_Name (Rf)
1074 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
1075 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
1076 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
1077 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
1078 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
1079 then
1080 return True;
1082 -- True if the same selected component from the same record
1084 elsif Nkind (Lf) = N_Selected_Component
1085 and then Selector_Name (Lf) = Selector_Name (Rf)
1086 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
1087 then
1088 return True;
1090 -- True if the same unary operator applied to the same operand
1092 elsif Nkind (Lf) in N_Unary_Op
1093 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1094 then
1095 return True;
1097 -- True if the same binary operator applied to the same operands
1099 elsif Nkind (Lf) in N_Binary_Op
1100 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
1101 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
1102 then
1103 return True;
1105 -- All other cases, we can't tell, so return False
1107 else
1108 return False;
1109 end if;
1110 end Is_Same_Value;
1112 -- Start of processing for Compile_Time_Compare
1114 begin
1115 Diff.all := No_Uint;
1117 -- In preanalysis mode, always return Unknown unless the expression
1118 -- is static. It is too early to be thinking we know the result of a
1119 -- comparison, save that judgment for the full analysis. This is
1120 -- particularly important in the case of pre and postconditions, which
1121 -- otherwise can be prematurely collapsed into having True or False
1122 -- conditions when this is inappropriate.
1124 if not (Full_Analysis
1125 or else (Is_OK_Static_Expression (L)
1126 and then
1127 Is_OK_Static_Expression (R)))
1128 then
1129 return Unknown;
1130 end if;
1132 -- If either operand could raise constraint error, then we cannot
1133 -- know the result at compile time (since CE may be raised).
1135 if not (Cannot_Raise_Constraint_Error (L)
1136 and then
1137 Cannot_Raise_Constraint_Error (R))
1138 then
1139 return Unknown;
1140 end if;
1142 -- Identical operands are most certainly equal
1144 if L = R then
1145 return EQ;
1146 end if;
1148 -- If expressions have no types, then do not attempt to determine if
1149 -- they are the same, since something funny is going on. One case in
1150 -- which this happens is during generic template analysis, when bounds
1151 -- are not fully analyzed.
1153 if No (Ltyp) or else No (Rtyp) then
1154 return Unknown;
1155 end if;
1157 -- These get reset to the base type for the case of entities where
1158 -- Is_Known_Valid is not set. This takes care of handling possible
1159 -- invalid representations using the value of the base type, in
1160 -- accordance with RM 13.9.1(10).
1162 Ltyp := Underlying_Type (Ltyp);
1163 Rtyp := Underlying_Type (Rtyp);
1165 -- Same rationale as above, but for Underlying_Type instead of Etype
1167 if No (Ltyp) or else No (Rtyp) then
1168 return Unknown;
1169 end if;
1171 -- We do not attempt comparisons for packed arrays represented as
1172 -- modular types, where the semantics of comparison is quite different.
1174 if Is_Packed_Array_Impl_Type (Ltyp)
1175 and then Is_Modular_Integer_Type (Ltyp)
1176 then
1177 return Unknown;
1179 -- For access types, the only time we know the result at compile time
1180 -- (apart from identical operands, which we handled already) is if we
1181 -- know one operand is null and the other is not, or both operands are
1182 -- known null.
1184 elsif Is_Access_Type (Ltyp) then
1185 if Known_Null (L) then
1186 if Known_Null (R) then
1187 return EQ;
1188 elsif Known_Non_Null (R) then
1189 return NE;
1190 else
1191 return Unknown;
1192 end if;
1194 elsif Known_Non_Null (L) and then Known_Null (R) then
1195 return NE;
1197 else
1198 return Unknown;
1199 end if;
1201 -- Case where comparison involves two compile-time-known values
1203 elsif Compile_Time_Known_Value (L)
1204 and then
1205 Compile_Time_Known_Value (R)
1206 then
1207 -- For the floating-point case, we have to be a little careful, since
1208 -- at compile time we are dealing with universal exact values, but at
1209 -- runtime, these will be in non-exact target form. That's why the
1210 -- returned results are LE and GE below instead of LT and GT.
1212 if Is_Floating_Point_Type (Ltyp)
1213 or else
1214 Is_Floating_Point_Type (Rtyp)
1215 then
1216 declare
1217 Lo : constant Ureal := Expr_Value_R (L);
1218 Hi : constant Ureal := Expr_Value_R (R);
1219 begin
1220 if Lo < Hi then
1221 return LE;
1222 elsif Lo = Hi then
1223 return EQ;
1224 else
1225 return GE;
1226 end if;
1227 end;
1229 -- For string types, we have two string literals and we proceed to
1230 -- compare them using the Ada style dictionary string comparison.
1232 elsif not Is_Scalar_Type (Ltyp) then
1233 declare
1234 Lstring : constant String_Id := Strval (Expr_Value_S (L));
1235 Rstring : constant String_Id := Strval (Expr_Value_S (R));
1236 Llen : constant Nat := String_Length (Lstring);
1237 Rlen : constant Nat := String_Length (Rstring);
1239 begin
1240 for J in 1 .. Nat'Min (Llen, Rlen) loop
1241 declare
1242 LC : constant Char_Code := Get_String_Char (Lstring, J);
1243 RC : constant Char_Code := Get_String_Char (Rstring, J);
1244 begin
1245 if LC < RC then
1246 return LT;
1247 elsif LC > RC then
1248 return GT;
1249 end if;
1250 end;
1251 end loop;
1253 if Llen < Rlen then
1254 return LT;
1255 elsif Llen > Rlen then
1256 return GT;
1257 else
1258 return EQ;
1259 end if;
1260 end;
1262 -- For remaining scalar cases we know exactly (note that this does
1263 -- include the fixed-point case, where we know the run time integer
1264 -- values now).
1266 else
1267 declare
1268 Lo : constant Uint := Expr_Value (L);
1269 Hi : constant Uint := Expr_Value (R);
1270 begin
1271 if Lo < Hi then
1272 Diff.all := Hi - Lo;
1273 return LT;
1274 elsif Lo = Hi then
1275 return EQ;
1276 else
1277 Diff.all := Lo - Hi;
1278 return GT;
1279 end if;
1280 end;
1281 end if;
1283 -- Cases where at least one operand is not known at compile time
1285 else
1286 -- Remaining checks apply only for discrete types
1288 if not Is_Discrete_Type (Ltyp)
1289 or else
1290 not Is_Discrete_Type (Rtyp)
1291 then
1292 return Unknown;
1293 end if;
1295 -- Defend against generic types, or actually any expressions that
1296 -- contain a reference to a generic type from within a generic
1297 -- template. We don't want to do any range analysis of such
1298 -- expressions for two reasons. First, the bounds of a generic type
1299 -- itself are junk and cannot be used for any kind of analysis.
1300 -- Second, we may have a case where the range at run time is indeed
1301 -- known, but we don't want to do compile time analysis in the
1302 -- template based on that range since in an instance the value may be
1303 -- static, and able to be elaborated without reference to the bounds
1304 -- of types involved. As an example, consider:
1306 -- (F'Pos (F'Last) + 1) > Integer'Last
1308 -- The expression on the left side of > is Universal_Integer and thus
1309 -- acquires the type Integer for evaluation at run time, and at run
1310 -- time it is true that this condition is always False, but within
1311 -- an instance F may be a type with a static range greater than the
1312 -- range of Integer, and the expression statically evaluates to True.
1314 if References_Generic_Formal_Type (L)
1315 or else
1316 References_Generic_Formal_Type (R)
1317 then
1318 return Unknown;
1319 end if;
1321 -- Replace types by base types for the case of values which are not
1322 -- known to have valid representations. This takes care of properly
1323 -- dealing with invalid representations.
1325 if not Assume_Valid then
1326 if not (Is_Entity_Name (L)
1327 and then (Is_Known_Valid (Entity (L))
1328 or else Assume_No_Invalid_Values))
1329 then
1330 Ltyp := Underlying_Type (Base_Type (Ltyp));
1331 end if;
1333 if not (Is_Entity_Name (R)
1334 and then (Is_Known_Valid (Entity (R))
1335 or else Assume_No_Invalid_Values))
1336 then
1337 Rtyp := Underlying_Type (Base_Type (Rtyp));
1338 end if;
1339 end if;
1341 -- First attempt is to decompose the expressions to extract a
1342 -- constant offset resulting from the use of any of the forms:
1344 -- expr + literal
1345 -- expr - literal
1346 -- typ'Succ (expr)
1347 -- typ'Pred (expr)
1349 -- Then we see if the two expressions are the same value, and if so
1350 -- the result is obtained by comparing the offsets.
1352 -- Note: the reason we do this test first is that it returns only
1353 -- decisive results (with diff set), where other tests, like the
1354 -- range test, may not be as so decisive. Consider for example
1355 -- J .. J + 1. This code can conclude LT with a difference of 1,
1356 -- even if the range of J is not known.
1358 declare
1359 Lnode : Node_Id;
1360 Loffs : Uint;
1361 Rnode : Node_Id;
1362 Roffs : Uint;
1364 begin
1365 Compare_Decompose (L, Lnode, Loffs);
1366 Compare_Decompose (R, Rnode, Roffs);
1368 if Is_Same_Value (Lnode, Rnode) then
1369 if Loffs = Roffs then
1370 return EQ;
1371 end if;
1373 -- When the offsets are not equal, we can go farther only if
1374 -- the types are not modular (e.g. X < X + 1 is False if X is
1375 -- the largest number).
1377 if not Is_Modular_Integer_Type (Ltyp)
1378 and then not Is_Modular_Integer_Type (Rtyp)
1379 then
1380 if Loffs < Roffs then
1381 Diff.all := Roffs - Loffs;
1382 return LT;
1383 else
1384 Diff.all := Loffs - Roffs;
1385 return GT;
1386 end if;
1387 end if;
1388 end if;
1389 end;
1391 -- Next, try range analysis and see if operand ranges are disjoint
1393 declare
1394 LOK, ROK : Boolean;
1395 LLo, LHi : Uint;
1396 RLo, RHi : Uint;
1398 Single : Boolean;
1399 -- True if each range is a single point
1401 begin
1402 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1403 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1405 if LOK and ROK then
1406 Single := (LLo = LHi) and then (RLo = RHi);
1408 if LHi < RLo then
1409 if Single and Assume_Valid then
1410 Diff.all := RLo - LLo;
1411 end if;
1413 return LT;
1415 elsif RHi < LLo then
1416 if Single and Assume_Valid then
1417 Diff.all := LLo - RLo;
1418 end if;
1420 return GT;
1422 elsif Single and then LLo = RLo then
1424 -- If the range includes a single literal and we can assume
1425 -- validity then the result is known even if an operand is
1426 -- not static.
1428 if Assume_Valid then
1429 return EQ;
1430 else
1431 return Unknown;
1432 end if;
1434 elsif LHi = RLo then
1435 return LE;
1437 elsif RHi = LLo then
1438 return GE;
1440 elsif not Is_Known_Valid_Operand (L)
1441 and then not Assume_Valid
1442 then
1443 if Is_Same_Value (L, R) then
1444 return EQ;
1445 else
1446 return Unknown;
1447 end if;
1448 end if;
1450 -- If the range of either operand cannot be determined, nothing
1451 -- further can be inferred.
1453 else
1454 return Unknown;
1455 end if;
1456 end;
1458 -- Here is where we check for comparisons against maximum bounds of
1459 -- types, where we know that no value can be outside the bounds of
1460 -- the subtype. Note that this routine is allowed to assume that all
1461 -- expressions are within their subtype bounds. Callers wishing to
1462 -- deal with possibly invalid values must in any case take special
1463 -- steps (e.g. conversions to larger types) to avoid this kind of
1464 -- optimization, which is always considered to be valid. We do not
1465 -- attempt this optimization with generic types, since the type
1466 -- bounds may not be meaningful in this case.
1468 -- We are in danger of an infinite recursion here. It does not seem
1469 -- useful to go more than one level deep, so the parameter Rec is
1470 -- used to protect ourselves against this infinite recursion.
1472 if not Rec then
1474 -- See if we can get a decisive check against one operand and a
1475 -- bound of the other operand (four possible tests here). Note
1476 -- that we avoid testing junk bounds of a generic type.
1478 if not Is_Generic_Type (Rtyp) then
1479 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1480 Discard'Access,
1481 Assume_Valid, Rec => True)
1483 when LT => return LT;
1484 when LE => return LE;
1485 when EQ => return LE;
1486 when others => null;
1487 end case;
1489 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1490 Discard'Access,
1491 Assume_Valid, Rec => True)
1493 when GT => return GT;
1494 when GE => return GE;
1495 when EQ => return GE;
1496 when others => null;
1497 end case;
1498 end if;
1500 if not Is_Generic_Type (Ltyp) then
1501 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1502 Discard'Access,
1503 Assume_Valid, Rec => True)
1505 when GT => return GT;
1506 when GE => return GE;
1507 when EQ => return GE;
1508 when others => null;
1509 end case;
1511 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1512 Discard'Access,
1513 Assume_Valid, Rec => True)
1515 when LT => return LT;
1516 when LE => return LE;
1517 when EQ => return LE;
1518 when others => null;
1519 end case;
1520 end if;
1521 end if;
1523 -- Next attempt is to see if we have an entity compared with a
1524 -- compile-time-known value, where there is a current value
1525 -- conditional for the entity which can tell us the result.
1527 declare
1528 Var : Node_Id;
1529 -- Entity variable (left operand)
1531 Val : Uint;
1532 -- Value (right operand)
1534 Inv : Boolean;
1535 -- If False, we have reversed the operands
1537 Op : Node_Kind;
1538 -- Comparison operator kind from Get_Current_Value_Condition call
1540 Opn : Node_Id;
1541 -- Value from Get_Current_Value_Condition call
1543 Opv : Uint;
1544 -- Value of Opn
1546 Result : Compare_Result;
1547 -- Known result before inversion
1549 begin
1550 if Is_Entity_Name (L)
1551 and then Compile_Time_Known_Value (R)
1552 then
1553 Var := L;
1554 Val := Expr_Value (R);
1555 Inv := False;
1557 elsif Is_Entity_Name (R)
1558 and then Compile_Time_Known_Value (L)
1559 then
1560 Var := R;
1561 Val := Expr_Value (L);
1562 Inv := True;
1564 -- That was the last chance at finding a compile time result
1566 else
1567 return Unknown;
1568 end if;
1570 Get_Current_Value_Condition (Var, Op, Opn);
1572 -- That was the last chance, so if we got nothing return
1574 if No (Opn) then
1575 return Unknown;
1576 end if;
1578 Opv := Expr_Value (Opn);
1580 -- We got a comparison, so we might have something interesting
1582 -- Convert LE to LT and GE to GT, just so we have fewer cases
1584 if Op = N_Op_Le then
1585 Op := N_Op_Lt;
1586 Opv := Opv + 1;
1588 elsif Op = N_Op_Ge then
1589 Op := N_Op_Gt;
1590 Opv := Opv - 1;
1591 end if;
1593 -- Deal with equality case
1595 if Op = N_Op_Eq then
1596 if Val = Opv then
1597 Result := EQ;
1598 elsif Opv < Val then
1599 Result := LT;
1600 else
1601 Result := GT;
1602 end if;
1604 -- Deal with inequality case
1606 elsif Op = N_Op_Ne then
1607 if Val = Opv then
1608 Result := NE;
1609 else
1610 return Unknown;
1611 end if;
1613 -- Deal with greater than case
1615 elsif Op = N_Op_Gt then
1616 if Opv >= Val then
1617 Result := GT;
1618 elsif Opv = Val - 1 then
1619 Result := GE;
1620 else
1621 return Unknown;
1622 end if;
1624 -- Deal with less than case
1626 else pragma Assert (Op = N_Op_Lt);
1627 if Opv <= Val then
1628 Result := LT;
1629 elsif Opv = Val + 1 then
1630 Result := LE;
1631 else
1632 return Unknown;
1633 end if;
1634 end if;
1636 -- Deal with inverting result
1638 if Inv then
1639 case Result is
1640 when GT => return LT;
1641 when GE => return LE;
1642 when LT => return GT;
1643 when LE => return GE;
1644 when others => return Result;
1645 end case;
1646 end if;
1648 return Result;
1649 end;
1650 end if;
1651 end Compile_Time_Compare;
1653 -------------------------------
1654 -- Compile_Time_Known_Bounds --
1655 -------------------------------
1657 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1658 Indx : Node_Id;
1659 Typ : Entity_Id;
1661 begin
1662 if T = Any_Composite or else not Is_Array_Type (T) then
1663 return False;
1664 end if;
1666 Indx := First_Index (T);
1667 while Present (Indx) loop
1668 Typ := Underlying_Type (Etype (Indx));
1670 -- Never look at junk bounds of a generic type
1672 if Is_Generic_Type (Typ) then
1673 return False;
1674 end if;
1676 -- Otherwise check bounds for compile-time-known
1678 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1679 return False;
1680 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1681 return False;
1682 else
1683 Next_Index (Indx);
1684 end if;
1685 end loop;
1687 return True;
1688 end Compile_Time_Known_Bounds;
1690 ------------------------------
1691 -- Compile_Time_Known_Value --
1692 ------------------------------
1694 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1695 K : constant Node_Kind := Nkind (Op);
1696 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1698 begin
1699 -- Never known at compile time if bad type or raises constraint error
1700 -- or empty (latter case occurs only as a result of a previous error).
1702 if No (Op) then
1703 Check_Error_Detected;
1704 return False;
1706 elsif Op = Error
1707 or else Etype (Op) = Any_Type
1708 or else Raises_Constraint_Error (Op)
1709 then
1710 return False;
1711 end if;
1713 -- If we have an entity name, then see if it is the name of a constant
1714 -- and if so, test the corresponding constant value, or the name of an
1715 -- enumeration literal, which is always a constant.
1717 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1718 declare
1719 Ent : constant Entity_Id := Entity (Op);
1720 Val : Node_Id;
1722 begin
1723 -- Never known at compile time if it is a packed array value. We
1724 -- might want to try to evaluate these at compile time one day,
1725 -- but we do not make that attempt now.
1727 if Is_Packed_Array_Impl_Type (Etype (Op)) then
1728 return False;
1730 elsif Ekind (Ent) = E_Enumeration_Literal then
1731 return True;
1733 elsif Ekind (Ent) = E_Constant then
1734 Val := Constant_Value (Ent);
1736 if Present (Val) then
1738 -- Guard against an illegal deferred constant whose full
1739 -- view is initialized with a reference to itself. Treat
1740 -- this case as a value not known at compile time.
1742 if Is_Entity_Name (Val) and then Entity (Val) = Ent then
1743 return False;
1744 else
1745 return Compile_Time_Known_Value (Val);
1746 end if;
1748 -- Otherwise, the constant does not have a compile-time-known
1749 -- value.
1751 else
1752 return False;
1753 end if;
1754 end if;
1755 end;
1757 -- We have a value, see if it is compile-time-known
1759 else
1760 -- Integer literals are worth storing in the cache
1762 if K = N_Integer_Literal then
1763 CV_Ent.N := Op;
1764 CV_Ent.V := Intval (Op);
1765 return True;
1767 -- Other literals and NULL are known at compile time
1769 elsif
1770 Nkind_In (K, N_Character_Literal,
1771 N_Real_Literal,
1772 N_String_Literal,
1773 N_Null)
1774 then
1775 return True;
1776 end if;
1777 end if;
1779 -- If we fall through, not known at compile time
1781 return False;
1783 -- If we get an exception while trying to do this test, then some error
1784 -- has occurred, and we simply say that the value is not known after all
1786 exception
1787 when others =>
1788 return False;
1789 end Compile_Time_Known_Value;
1791 --------------------------------------
1792 -- Compile_Time_Known_Value_Or_Aggr --
1793 --------------------------------------
1795 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1796 begin
1797 -- If we have an entity name, then see if it is the name of a constant
1798 -- and if so, test the corresponding constant value, or the name of
1799 -- an enumeration literal, which is always a constant.
1801 if Is_Entity_Name (Op) then
1802 declare
1803 E : constant Entity_Id := Entity (Op);
1804 V : Node_Id;
1806 begin
1807 if Ekind (E) = E_Enumeration_Literal then
1808 return True;
1810 elsif Ekind (E) /= E_Constant then
1811 return False;
1813 else
1814 V := Constant_Value (E);
1815 return Present (V)
1816 and then Compile_Time_Known_Value_Or_Aggr (V);
1817 end if;
1818 end;
1820 -- We have a value, see if it is compile-time-known
1822 else
1823 if Compile_Time_Known_Value (Op) then
1824 return True;
1826 elsif Nkind (Op) = N_Aggregate then
1828 if Present (Expressions (Op)) then
1829 declare
1830 Expr : Node_Id;
1831 begin
1832 Expr := First (Expressions (Op));
1833 while Present (Expr) loop
1834 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1835 return False;
1836 else
1837 Next (Expr);
1838 end if;
1839 end loop;
1840 end;
1841 end if;
1843 if Present (Component_Associations (Op)) then
1844 declare
1845 Cass : Node_Id;
1847 begin
1848 Cass := First (Component_Associations (Op));
1849 while Present (Cass) loop
1850 if not
1851 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1852 then
1853 return False;
1854 end if;
1856 Next (Cass);
1857 end loop;
1858 end;
1859 end if;
1861 return True;
1863 elsif Nkind (Op) = N_Qualified_Expression then
1864 return Compile_Time_Known_Value_Or_Aggr (Expression (Op));
1866 -- All other types of values are not known at compile time
1868 else
1869 return False;
1870 end if;
1872 end if;
1873 end Compile_Time_Known_Value_Or_Aggr;
1875 ---------------------------------------
1876 -- CRT_Safe_Compile_Time_Known_Value --
1877 ---------------------------------------
1879 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1880 begin
1881 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1882 and then not Is_OK_Static_Expression (Op)
1883 then
1884 return False;
1885 else
1886 return Compile_Time_Known_Value (Op);
1887 end if;
1888 end CRT_Safe_Compile_Time_Known_Value;
1890 -----------------
1891 -- Eval_Actual --
1892 -----------------
1894 -- This is only called for actuals of functions that are not predefined
1895 -- operators (which have already been rewritten as operators at this
1896 -- stage), so the call can never be folded, and all that needs doing for
1897 -- the actual is to do the check for a non-static context.
1899 procedure Eval_Actual (N : Node_Id) is
1900 begin
1901 Check_Non_Static_Context (N);
1902 end Eval_Actual;
1904 --------------------
1905 -- Eval_Allocator --
1906 --------------------
1908 -- Allocators are never static, so all we have to do is to do the
1909 -- check for a non-static context if an expression is present.
1911 procedure Eval_Allocator (N : Node_Id) is
1912 Expr : constant Node_Id := Expression (N);
1913 begin
1914 if Nkind (Expr) = N_Qualified_Expression then
1915 Check_Non_Static_Context (Expression (Expr));
1916 end if;
1917 end Eval_Allocator;
1919 ------------------------
1920 -- Eval_Arithmetic_Op --
1921 ------------------------
1923 -- Arithmetic operations are static functions, so the result is static
1924 -- if both operands are static (RM 4.9(7), 4.9(20)).
1926 procedure Eval_Arithmetic_Op (N : Node_Id) is
1927 Left : constant Node_Id := Left_Opnd (N);
1928 Right : constant Node_Id := Right_Opnd (N);
1929 Ltype : constant Entity_Id := Etype (Left);
1930 Rtype : constant Entity_Id := Etype (Right);
1931 Otype : Entity_Id := Empty;
1932 Stat : Boolean;
1933 Fold : Boolean;
1935 begin
1936 -- If not foldable we are done
1938 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1940 if not Fold then
1941 return;
1942 end if;
1944 -- Otherwise attempt to fold
1946 if Is_Universal_Numeric_Type (Etype (Left))
1947 and then
1948 Is_Universal_Numeric_Type (Etype (Right))
1949 then
1950 Otype := Find_Universal_Operator_Type (N);
1951 end if;
1953 -- Fold for cases where both operands are of integer type
1955 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1956 declare
1957 Left_Int : constant Uint := Expr_Value (Left);
1958 Right_Int : constant Uint := Expr_Value (Right);
1959 Result : Uint;
1961 begin
1962 case Nkind (N) is
1963 when N_Op_Add =>
1964 Result := Left_Int + Right_Int;
1966 when N_Op_Subtract =>
1967 Result := Left_Int - Right_Int;
1969 when N_Op_Multiply =>
1970 if OK_Bits
1971 (N, UI_From_Int
1972 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1973 then
1974 Result := Left_Int * Right_Int;
1975 else
1976 Result := Left_Int;
1977 end if;
1979 when N_Op_Divide =>
1981 -- The exception Constraint_Error is raised by integer
1982 -- division, rem and mod if the right operand is zero.
1984 if Right_Int = 0 then
1986 -- When SPARK_Mode is On, force a warning instead of
1987 -- an error in that case, as this likely corresponds
1988 -- to deactivated code.
1990 Apply_Compile_Time_Constraint_Error
1991 (N, "division by zero", CE_Divide_By_Zero,
1992 Warn => not Stat or SPARK_Mode = On);
1993 Set_Raises_Constraint_Error (N);
1994 return;
1996 -- Otherwise we can do the division
1998 else
1999 Result := Left_Int / Right_Int;
2000 end if;
2002 when N_Op_Mod =>
2004 -- The exception Constraint_Error is raised by integer
2005 -- division, rem and mod if the right operand is zero.
2007 if Right_Int = 0 then
2009 -- When SPARK_Mode is On, force a warning instead of
2010 -- an error in that case, as this likely corresponds
2011 -- to deactivated code.
2013 Apply_Compile_Time_Constraint_Error
2014 (N, "mod with zero divisor", CE_Divide_By_Zero,
2015 Warn => not Stat or SPARK_Mode = On);
2016 return;
2018 else
2019 Result := Left_Int mod Right_Int;
2020 end if;
2022 when N_Op_Rem =>
2024 -- The exception Constraint_Error is raised by integer
2025 -- division, rem and mod if the right operand is zero.
2027 if Right_Int = 0 then
2029 -- When SPARK_Mode is On, force a warning instead of
2030 -- an error in that case, as this likely corresponds
2031 -- to deactivated code.
2033 Apply_Compile_Time_Constraint_Error
2034 (N, "rem with zero divisor", CE_Divide_By_Zero,
2035 Warn => not Stat or SPARK_Mode = On);
2036 return;
2038 else
2039 Result := Left_Int rem Right_Int;
2040 end if;
2042 when others =>
2043 raise Program_Error;
2044 end case;
2046 -- Adjust the result by the modulus if the type is a modular type
2048 if Is_Modular_Integer_Type (Ltype) then
2049 Result := Result mod Modulus (Ltype);
2051 -- For a signed integer type, check non-static overflow
2053 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
2054 declare
2055 BT : constant Entity_Id := Base_Type (Ltype);
2056 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
2057 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
2058 begin
2059 if Result < Lo or else Result > Hi then
2060 Apply_Compile_Time_Constraint_Error
2061 (N, "value not in range of }??",
2062 CE_Overflow_Check_Failed,
2063 Ent => BT);
2064 return;
2065 end if;
2066 end;
2067 end if;
2069 -- If we get here we can fold the result
2071 Fold_Uint (N, Result, Stat);
2072 end;
2074 -- Cases where at least one operand is a real. We handle the cases of
2075 -- both reals, or mixed/real integer cases (the latter happen only for
2076 -- divide and multiply, and the result is always real).
2078 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
2079 declare
2080 Left_Real : Ureal;
2081 Right_Real : Ureal;
2082 Result : Ureal;
2084 begin
2085 if Is_Real_Type (Ltype) then
2086 Left_Real := Expr_Value_R (Left);
2087 else
2088 Left_Real := UR_From_Uint (Expr_Value (Left));
2089 end if;
2091 if Is_Real_Type (Rtype) then
2092 Right_Real := Expr_Value_R (Right);
2093 else
2094 Right_Real := UR_From_Uint (Expr_Value (Right));
2095 end if;
2097 if Nkind (N) = N_Op_Add then
2098 Result := Left_Real + Right_Real;
2100 elsif Nkind (N) = N_Op_Subtract then
2101 Result := Left_Real - Right_Real;
2103 elsif Nkind (N) = N_Op_Multiply then
2104 Result := Left_Real * Right_Real;
2106 else pragma Assert (Nkind (N) = N_Op_Divide);
2107 if UR_Is_Zero (Right_Real) then
2108 Apply_Compile_Time_Constraint_Error
2109 (N, "division by zero", CE_Divide_By_Zero);
2110 return;
2111 end if;
2113 Result := Left_Real / Right_Real;
2114 end if;
2116 Fold_Ureal (N, Result, Stat);
2117 end;
2118 end if;
2120 -- If the operator was resolved to a specific type, make sure that type
2121 -- is frozen even if the expression is folded into a literal (which has
2122 -- a universal type).
2124 if Present (Otype) then
2125 Freeze_Before (N, Otype);
2126 end if;
2127 end Eval_Arithmetic_Op;
2129 ----------------------------
2130 -- Eval_Character_Literal --
2131 ----------------------------
2133 -- Nothing to be done
2135 procedure Eval_Character_Literal (N : Node_Id) is
2136 pragma Warnings (Off, N);
2137 begin
2138 null;
2139 end Eval_Character_Literal;
2141 ---------------
2142 -- Eval_Call --
2143 ---------------
2145 -- Static function calls are either calls to predefined operators
2146 -- with static arguments, or calls to functions that rename a literal.
2147 -- Only the latter case is handled here, predefined operators are
2148 -- constant-folded elsewhere.
2150 -- If the function is itself inherited (see 7423-001) the literal of
2151 -- the parent type must be explicitly converted to the return type
2152 -- of the function.
2154 procedure Eval_Call (N : Node_Id) is
2155 Loc : constant Source_Ptr := Sloc (N);
2156 Typ : constant Entity_Id := Etype (N);
2157 Lit : Entity_Id;
2159 begin
2160 if Nkind (N) = N_Function_Call
2161 and then No (Parameter_Associations (N))
2162 and then Is_Entity_Name (Name (N))
2163 and then Present (Alias (Entity (Name (N))))
2164 and then Is_Enumeration_Type (Base_Type (Typ))
2165 then
2166 Lit := Ultimate_Alias (Entity (Name (N)));
2168 if Ekind (Lit) = E_Enumeration_Literal then
2169 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
2170 Rewrite
2171 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
2172 else
2173 Rewrite (N, New_Occurrence_Of (Lit, Loc));
2174 end if;
2176 Resolve (N, Typ);
2177 end if;
2178 end if;
2179 end Eval_Call;
2181 --------------------------
2182 -- Eval_Case_Expression --
2183 --------------------------
2185 -- A conditional expression is static if all its conditions and dependent
2186 -- expressions are static. Note that we do not care if the dependent
2187 -- expressions raise CE, except for the one that will be selected.
2189 procedure Eval_Case_Expression (N : Node_Id) is
2190 Alt : Node_Id;
2191 Choice : Node_Id;
2193 begin
2194 Set_Is_Static_Expression (N, False);
2196 if Error_Posted (Expression (N))
2197 or else not Is_Static_Expression (Expression (N))
2198 then
2199 Check_Non_Static_Context (Expression (N));
2200 return;
2201 end if;
2203 -- First loop, make sure all the alternatives are static expressions
2204 -- none of which raise Constraint_Error. We make the constraint error
2205 -- check because part of the legality condition for a correct static
2206 -- case expression is that the cases are covered, like any other case
2207 -- expression. And we can't do that if any of the conditions raise an
2208 -- exception, so we don't even try to evaluate if that is the case.
2210 Alt := First (Alternatives (N));
2211 while Present (Alt) loop
2213 -- The expression must be static, but we don't care at this stage
2214 -- if it raises Constraint_Error (the alternative might not match,
2215 -- in which case the expression is statically unevaluated anyway).
2217 if not Is_Static_Expression (Expression (Alt)) then
2218 Check_Non_Static_Context (Expression (Alt));
2219 return;
2220 end if;
2222 -- The choices of a case always have to be static, and cannot raise
2223 -- an exception. If this condition is not met, then the expression
2224 -- is plain illegal, so just abandon evaluation attempts. No need
2225 -- to check non-static context when we have something illegal anyway.
2227 if not Is_OK_Static_Choice_List (Discrete_Choices (Alt)) then
2228 return;
2229 end if;
2231 Next (Alt);
2232 end loop;
2234 -- OK, if the above loop gets through it means that all choices are OK
2235 -- static (don't raise exceptions), so the whole case is static, and we
2236 -- can find the matching alternative.
2238 Set_Is_Static_Expression (N);
2240 -- Now to deal with propagating a possible constraint error
2242 -- If the selecting expression raises CE, propagate and we are done
2244 if Raises_Constraint_Error (Expression (N)) then
2245 Set_Raises_Constraint_Error (N);
2247 -- Otherwise we need to check the alternatives to find the matching
2248 -- one. CE's in other than the matching one are not relevant. But we
2249 -- do need to check the matching one. Unlike the first loop, we do not
2250 -- have to go all the way through, when we find the matching one, quit.
2252 else
2253 Alt := First (Alternatives (N));
2254 Search : loop
2256 -- We must find a match among the alternatives. If not, this must
2257 -- be due to other errors, so just ignore, leaving as non-static.
2259 if No (Alt) then
2260 Set_Is_Static_Expression (N, False);
2261 return;
2262 end if;
2264 -- Otherwise loop through choices of this alternative
2266 Choice := First (Discrete_Choices (Alt));
2267 while Present (Choice) loop
2269 -- If we find a matching choice, then the Expression of this
2270 -- alternative replaces N (Raises_Constraint_Error flag is
2271 -- included, so we don't have to special case that).
2273 if Choice_Matches (Expression (N), Choice) = Match then
2274 Rewrite (N, Relocate_Node (Expression (Alt)));
2275 return;
2276 end if;
2278 Next (Choice);
2279 end loop;
2281 Next (Alt);
2282 end loop Search;
2283 end if;
2284 end Eval_Case_Expression;
2286 ------------------------
2287 -- Eval_Concatenation --
2288 ------------------------
2290 -- Concatenation is a static function, so the result is static if both
2291 -- operands are static (RM 4.9(7), 4.9(21)).
2293 procedure Eval_Concatenation (N : Node_Id) is
2294 Left : constant Node_Id := Left_Opnd (N);
2295 Right : constant Node_Id := Right_Opnd (N);
2296 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
2297 Stat : Boolean;
2298 Fold : Boolean;
2300 begin
2301 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2302 -- non-static context.
2304 if Ada_Version = Ada_83
2305 and then Comes_From_Source (N)
2306 then
2307 Check_Non_Static_Context (Left);
2308 Check_Non_Static_Context (Right);
2309 return;
2310 end if;
2312 -- If not foldable we are done. In principle concatenation that yields
2313 -- any string type is static (i.e. an array type of character types).
2314 -- However, character types can include enumeration literals, and
2315 -- concatenation in that case cannot be described by a literal, so we
2316 -- only consider the operation static if the result is an array of
2317 -- (a descendant of) a predefined character type.
2319 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2321 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
2322 Set_Is_Static_Expression (N, False);
2323 return;
2324 end if;
2326 -- Compile time string concatenation
2328 -- ??? Note that operands that are aggregates can be marked as static,
2329 -- so we should attempt at a later stage to fold concatenations with
2330 -- such aggregates.
2332 declare
2333 Left_Str : constant Node_Id := Get_String_Val (Left);
2334 Left_Len : Nat;
2335 Right_Str : constant Node_Id := Get_String_Val (Right);
2336 Folded_Val : String_Id := No_String;
2338 begin
2339 -- Establish new string literal, and store left operand. We make
2340 -- sure to use the special Start_String that takes an operand if
2341 -- the left operand is a string literal. Since this is optimized
2342 -- in the case where that is the most recently created string
2343 -- literal, we ensure efficient time/space behavior for the
2344 -- case of a concatenation of a series of string literals.
2346 if Nkind (Left_Str) = N_String_Literal then
2347 Left_Len := String_Length (Strval (Left_Str));
2349 -- If the left operand is the empty string, and the right operand
2350 -- is a string literal (the case of "" & "..."), the result is the
2351 -- value of the right operand. This optimization is important when
2352 -- Is_Folded_In_Parser, to avoid copying an enormous right
2353 -- operand.
2355 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
2356 Folded_Val := Strval (Right_Str);
2357 else
2358 Start_String (Strval (Left_Str));
2359 end if;
2361 else
2362 Start_String;
2363 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
2364 Left_Len := 1;
2365 end if;
2367 -- Now append the characters of the right operand, unless we
2368 -- optimized the "" & "..." case above.
2370 if Nkind (Right_Str) = N_String_Literal then
2371 if Left_Len /= 0 then
2372 Store_String_Chars (Strval (Right_Str));
2373 Folded_Val := End_String;
2374 end if;
2375 else
2376 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
2377 Folded_Val := End_String;
2378 end if;
2380 Set_Is_Static_Expression (N, Stat);
2382 -- If left operand is the empty string, the result is the
2383 -- right operand, including its bounds if anomalous.
2385 if Left_Len = 0
2386 and then Is_Array_Type (Etype (Right))
2387 and then Etype (Right) /= Any_String
2388 then
2389 Set_Etype (N, Etype (Right));
2390 end if;
2392 Fold_Str (N, Folded_Val, Static => Stat);
2393 end;
2394 end Eval_Concatenation;
2396 ----------------------
2397 -- Eval_Entity_Name --
2398 ----------------------
2400 -- This procedure is used for identifiers and expanded names other than
2401 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2402 -- static if they denote a static constant (RM 4.9(6)) or if the name
2403 -- denotes an enumeration literal (RM 4.9(22)).
2405 procedure Eval_Entity_Name (N : Node_Id) is
2406 Def_Id : constant Entity_Id := Entity (N);
2407 Val : Node_Id;
2409 begin
2410 -- Enumeration literals are always considered to be constants
2411 -- and cannot raise constraint error (RM 4.9(22)).
2413 if Ekind (Def_Id) = E_Enumeration_Literal then
2414 Set_Is_Static_Expression (N);
2415 return;
2417 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2418 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2419 -- it does not violate 10.2.1(8) here, since this is not a variable.
2421 elsif Ekind (Def_Id) = E_Constant then
2423 -- Deferred constants must always be treated as nonstatic outside the
2424 -- scope of their full view.
2426 if Present (Full_View (Def_Id))
2427 and then not In_Open_Scopes (Scope (Def_Id))
2428 then
2429 Val := Empty;
2430 else
2431 Val := Constant_Value (Def_Id);
2432 end if;
2434 if Present (Val) then
2435 Set_Is_Static_Expression
2436 (N, Is_Static_Expression (Val)
2437 and then Is_Static_Subtype (Etype (Def_Id)));
2438 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
2440 if not Is_Static_Expression (N)
2441 and then not Is_Generic_Type (Etype (N))
2442 then
2443 Validate_Static_Object_Name (N);
2444 end if;
2446 -- Mark constant condition in SCOs
2448 if Generate_SCO
2449 and then Comes_From_Source (N)
2450 and then Is_Boolean_Type (Etype (Def_Id))
2451 and then Compile_Time_Known_Value (N)
2452 then
2453 Set_SCO_Condition (N, Expr_Value_E (N) = Standard_True);
2454 end if;
2456 return;
2457 end if;
2458 end if;
2460 -- Fall through if the name is not static
2462 Validate_Static_Object_Name (N);
2463 end Eval_Entity_Name;
2465 ------------------------
2466 -- Eval_If_Expression --
2467 ------------------------
2469 -- We can fold to a static expression if the condition and both dependent
2470 -- expressions are static. Otherwise, the only required processing is to do
2471 -- the check for non-static context for the then and else expressions.
2473 procedure Eval_If_Expression (N : Node_Id) is
2474 Condition : constant Node_Id := First (Expressions (N));
2475 Then_Expr : constant Node_Id := Next (Condition);
2476 Else_Expr : constant Node_Id := Next (Then_Expr);
2477 Result : Node_Id;
2478 Non_Result : Node_Id;
2480 Rstat : constant Boolean :=
2481 Is_Static_Expression (Condition)
2482 and then
2483 Is_Static_Expression (Then_Expr)
2484 and then
2485 Is_Static_Expression (Else_Expr);
2486 -- True if result is static
2488 begin
2489 -- If result not static, nothing to do, otherwise set static result
2491 if not Rstat then
2492 return;
2493 else
2494 Set_Is_Static_Expression (N);
2495 end if;
2497 -- If any operand is Any_Type, just propagate to result and do not try
2498 -- to fold, this prevents cascaded errors.
2500 if Etype (Condition) = Any_Type or else
2501 Etype (Then_Expr) = Any_Type or else
2502 Etype (Else_Expr) = Any_Type
2503 then
2504 Set_Etype (N, Any_Type);
2505 Set_Is_Static_Expression (N, False);
2506 return;
2507 end if;
2509 -- If condition raises constraint error then we have already signaled
2510 -- an error, and we just propagate to the result and do not fold.
2512 if Raises_Constraint_Error (Condition) then
2513 Set_Raises_Constraint_Error (N);
2514 return;
2515 end if;
2517 -- Static case where we can fold. Note that we don't try to fold cases
2518 -- where the condition is known at compile time, but the result is
2519 -- non-static. This avoids possible cases of infinite recursion where
2520 -- the expander puts in a redundant test and we remove it. Instead we
2521 -- deal with these cases in the expander.
2523 -- Select result operand
2525 if Is_True (Expr_Value (Condition)) then
2526 Result := Then_Expr;
2527 Non_Result := Else_Expr;
2528 else
2529 Result := Else_Expr;
2530 Non_Result := Then_Expr;
2531 end if;
2533 -- Note that it does not matter if the non-result operand raises a
2534 -- Constraint_Error, but if the result raises constraint error then we
2535 -- replace the node with a raise constraint error. This will properly
2536 -- propagate Raises_Constraint_Error since this flag is set in Result.
2538 if Raises_Constraint_Error (Result) then
2539 Rewrite_In_Raise_CE (N, Result);
2540 Check_Non_Static_Context (Non_Result);
2542 -- Otherwise the result operand replaces the original node
2544 else
2545 Rewrite (N, Relocate_Node (Result));
2546 Set_Is_Static_Expression (N);
2547 end if;
2548 end Eval_If_Expression;
2550 ----------------------------
2551 -- Eval_Indexed_Component --
2552 ----------------------------
2554 -- Indexed components are never static, so we need to perform the check
2555 -- for non-static context on the index values. Then, we check if the
2556 -- value can be obtained at compile time, even though it is non-static.
2558 procedure Eval_Indexed_Component (N : Node_Id) is
2559 Expr : Node_Id;
2561 begin
2562 -- Check for non-static context on index values
2564 Expr := First (Expressions (N));
2565 while Present (Expr) loop
2566 Check_Non_Static_Context (Expr);
2567 Next (Expr);
2568 end loop;
2570 -- If the indexed component appears in an object renaming declaration
2571 -- then we do not want to try to evaluate it, since in this case we
2572 -- need the identity of the array element.
2574 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2575 return;
2577 -- Similarly if the indexed component appears as the prefix of an
2578 -- attribute we don't want to evaluate it, because at least for
2579 -- some cases of attributes we need the identify (e.g. Access, Size)
2581 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2582 return;
2583 end if;
2585 -- Note: there are other cases, such as the left side of an assignment,
2586 -- or an OUT parameter for a call, where the replacement results in the
2587 -- illegal use of a constant, But these cases are illegal in the first
2588 -- place, so the replacement, though silly, is harmless.
2590 -- Now see if this is a constant array reference
2592 if List_Length (Expressions (N)) = 1
2593 and then Is_Entity_Name (Prefix (N))
2594 and then Ekind (Entity (Prefix (N))) = E_Constant
2595 and then Present (Constant_Value (Entity (Prefix (N))))
2596 then
2597 declare
2598 Loc : constant Source_Ptr := Sloc (N);
2599 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2600 Sub : constant Node_Id := First (Expressions (N));
2602 Atyp : Entity_Id;
2603 -- Type of array
2605 Lin : Nat;
2606 -- Linear one's origin subscript value for array reference
2608 Lbd : Node_Id;
2609 -- Lower bound of the first array index
2611 Elm : Node_Id;
2612 -- Value from constant array
2614 begin
2615 Atyp := Etype (Arr);
2617 if Is_Access_Type (Atyp) then
2618 Atyp := Designated_Type (Atyp);
2619 end if;
2621 -- If we have an array type (we should have but perhaps there are
2622 -- error cases where this is not the case), then see if we can do
2623 -- a constant evaluation of the array reference.
2625 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2626 if Ekind (Atyp) = E_String_Literal_Subtype then
2627 Lbd := String_Literal_Low_Bound (Atyp);
2628 else
2629 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2630 end if;
2632 if Compile_Time_Known_Value (Sub)
2633 and then Nkind (Arr) = N_Aggregate
2634 and then Compile_Time_Known_Value (Lbd)
2635 and then Is_Discrete_Type (Component_Type (Atyp))
2636 then
2637 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2639 if List_Length (Expressions (Arr)) >= Lin then
2640 Elm := Pick (Expressions (Arr), Lin);
2642 -- If the resulting expression is compile-time-known,
2643 -- then we can rewrite the indexed component with this
2644 -- value, being sure to mark the result as non-static.
2645 -- We also reset the Sloc, in case this generates an
2646 -- error later on (e.g. 136'Access).
2648 if Compile_Time_Known_Value (Elm) then
2649 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2650 Set_Is_Static_Expression (N, False);
2651 Set_Sloc (N, Loc);
2652 end if;
2653 end if;
2655 -- We can also constant-fold if the prefix is a string literal.
2656 -- This will be useful in an instantiation or an inlining.
2658 elsif Compile_Time_Known_Value (Sub)
2659 and then Nkind (Arr) = N_String_Literal
2660 and then Compile_Time_Known_Value (Lbd)
2661 and then Expr_Value (Lbd) = 1
2662 and then Expr_Value (Sub) <=
2663 String_Literal_Length (Etype (Arr))
2664 then
2665 declare
2666 C : constant Char_Code :=
2667 Get_String_Char (Strval (Arr),
2668 UI_To_Int (Expr_Value (Sub)));
2669 begin
2670 Set_Character_Literal_Name (C);
2672 Elm :=
2673 Make_Character_Literal (Loc,
2674 Chars => Name_Find,
2675 Char_Literal_Value => UI_From_CC (C));
2676 Set_Etype (Elm, Component_Type (Atyp));
2677 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2678 Set_Is_Static_Expression (N, False);
2679 end;
2680 end if;
2681 end if;
2682 end;
2683 end if;
2684 end Eval_Indexed_Component;
2686 --------------------------
2687 -- Eval_Integer_Literal --
2688 --------------------------
2690 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2691 -- as static by the analyzer. The reason we did it that early is to allow
2692 -- the possibility of turning off the Is_Static_Expression flag after
2693 -- analysis, but before resolution, when integer literals are generated in
2694 -- the expander that do not correspond to static expressions.
2696 procedure Eval_Integer_Literal (N : Node_Id) is
2697 function In_Any_Integer_Context (Context : Node_Id) return Boolean;
2698 -- If the literal is resolved with a specific type in a context where
2699 -- the expected type is Any_Integer, there are no range checks on the
2700 -- literal. By the time the literal is evaluated, it carries the type
2701 -- imposed by the enclosing expression, and we must recover the context
2702 -- to determine that Any_Integer is meant.
2704 ----------------------------
2705 -- In_Any_Integer_Context --
2706 ----------------------------
2708 function In_Any_Integer_Context (Context : Node_Id) return Boolean is
2709 begin
2710 -- Any_Integer also appears in digits specifications for real types,
2711 -- but those have bounds smaller that those of any integer base type,
2712 -- so we can safely ignore these cases.
2714 return
2715 Nkind_In (Context, N_Attribute_Definition_Clause,
2716 N_Attribute_Reference,
2717 N_Modular_Type_Definition,
2718 N_Number_Declaration,
2719 N_Signed_Integer_Type_Definition);
2720 end In_Any_Integer_Context;
2722 -- Local variables
2724 Par : constant Node_Id := Parent (N);
2725 Typ : constant Entity_Id := Etype (N);
2727 -- Start of processing for Eval_Integer_Literal
2729 begin
2730 -- If the literal appears in a non-expression context, then it is
2731 -- certainly appearing in a non-static context, so check it. This is
2732 -- actually a redundant check, since Check_Non_Static_Context would
2733 -- check it, but it seems worthwhile to optimize out the call.
2735 -- Additionally, when the literal appears within an if or case
2736 -- expression it must be checked as well. However, due to the literal
2737 -- appearing within a conditional statement, expansion greatly changes
2738 -- the nature of its context and performing some of the checks within
2739 -- Check_Non_Static_Context on an expanded literal may lead to spurious
2740 -- and misleading warnings.
2742 if (Nkind_In (Par, N_Case_Expression_Alternative, N_If_Expression)
2743 or else Nkind (Parent (N)) not in N_Subexpr)
2744 and then (not Nkind_In (Par, N_Case_Expression_Alternative,
2745 N_If_Expression)
2746 or else Comes_From_Source (N))
2747 and then not In_Any_Integer_Context (Par)
2748 then
2749 Check_Non_Static_Context (N);
2750 end if;
2752 -- Modular integer literals must be in their base range
2754 if Is_Modular_Integer_Type (Typ)
2755 and then Is_Out_Of_Range (N, Base_Type (Typ), Assume_Valid => True)
2756 then
2757 Out_Of_Range (N);
2758 end if;
2759 end Eval_Integer_Literal;
2761 ---------------------
2762 -- Eval_Logical_Op --
2763 ---------------------
2765 -- Logical operations are static functions, so the result is potentially
2766 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2768 procedure Eval_Logical_Op (N : Node_Id) is
2769 Left : constant Node_Id := Left_Opnd (N);
2770 Right : constant Node_Id := Right_Opnd (N);
2771 Stat : Boolean;
2772 Fold : Boolean;
2774 begin
2775 -- If not foldable we are done
2777 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2779 if not Fold then
2780 return;
2781 end if;
2783 -- Compile time evaluation of logical operation
2785 declare
2786 Left_Int : constant Uint := Expr_Value (Left);
2787 Right_Int : constant Uint := Expr_Value (Right);
2789 begin
2790 if Is_Modular_Integer_Type (Etype (N)) then
2791 declare
2792 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2793 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2795 begin
2796 To_Bits (Left_Int, Left_Bits);
2797 To_Bits (Right_Int, Right_Bits);
2799 -- Note: should really be able to use array ops instead of
2800 -- these loops, but they weren't working at the time ???
2802 if Nkind (N) = N_Op_And then
2803 for J in Left_Bits'Range loop
2804 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2805 end loop;
2807 elsif Nkind (N) = N_Op_Or then
2808 for J in Left_Bits'Range loop
2809 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2810 end loop;
2812 else
2813 pragma Assert (Nkind (N) = N_Op_Xor);
2815 for J in Left_Bits'Range loop
2816 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2817 end loop;
2818 end if;
2820 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2821 end;
2823 else
2824 pragma Assert (Is_Boolean_Type (Etype (N)));
2826 if Nkind (N) = N_Op_And then
2827 Fold_Uint (N,
2828 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2830 elsif Nkind (N) = N_Op_Or then
2831 Fold_Uint (N,
2832 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2834 else
2835 pragma Assert (Nkind (N) = N_Op_Xor);
2836 Fold_Uint (N,
2837 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2838 end if;
2839 end if;
2840 end;
2841 end Eval_Logical_Op;
2843 ------------------------
2844 -- Eval_Membership_Op --
2845 ------------------------
2847 -- A membership test is potentially static if the expression is static, and
2848 -- the range is a potentially static range, or is a subtype mark denoting a
2849 -- static subtype (RM 4.9(12)).
2851 procedure Eval_Membership_Op (N : Node_Id) is
2852 Alts : constant List_Id := Alternatives (N);
2853 Choice : constant Node_Id := Right_Opnd (N);
2854 Expr : constant Node_Id := Left_Opnd (N);
2855 Result : Match_Result;
2857 begin
2858 -- Ignore if error in either operand, except to make sure that Any_Type
2859 -- is properly propagated to avoid junk cascaded errors.
2861 if Etype (Expr) = Any_Type
2862 or else (Present (Choice) and then Etype (Choice) = Any_Type)
2863 then
2864 Set_Etype (N, Any_Type);
2865 return;
2866 end if;
2868 -- If left operand non-static, then nothing to do
2870 if not Is_Static_Expression (Expr) then
2871 return;
2872 end if;
2874 -- If choice is non-static, left operand is in non-static context
2876 if (Present (Choice) and then not Is_Static_Choice (Choice))
2877 or else (Present (Alts) and then not Is_Static_Choice_List (Alts))
2878 then
2879 Check_Non_Static_Context (Expr);
2880 return;
2881 end if;
2883 -- Otherwise we definitely have a static expression
2885 Set_Is_Static_Expression (N);
2887 -- If left operand raises constraint error, propagate and we are done
2889 if Raises_Constraint_Error (Expr) then
2890 Set_Raises_Constraint_Error (N, True);
2892 -- See if we match
2894 else
2895 if Present (Choice) then
2896 Result := Choice_Matches (Expr, Choice);
2897 else
2898 Result := Choices_Match (Expr, Alts);
2899 end if;
2901 -- If result is Non_Static, it means that we raise Constraint_Error,
2902 -- since we already tested that the operands were themselves static.
2904 if Result = Non_Static then
2905 Set_Raises_Constraint_Error (N);
2907 -- Otherwise we have our result (flipped if NOT IN case)
2909 else
2910 Fold_Uint
2911 (N, Test ((Result = Match) xor (Nkind (N) = N_Not_In)), True);
2912 Warn_On_Known_Condition (N);
2913 end if;
2914 end if;
2915 end Eval_Membership_Op;
2917 ------------------------
2918 -- Eval_Named_Integer --
2919 ------------------------
2921 procedure Eval_Named_Integer (N : Node_Id) is
2922 begin
2923 Fold_Uint (N,
2924 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2925 end Eval_Named_Integer;
2927 ---------------------
2928 -- Eval_Named_Real --
2929 ---------------------
2931 procedure Eval_Named_Real (N : Node_Id) is
2932 begin
2933 Fold_Ureal (N,
2934 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2935 end Eval_Named_Real;
2937 -------------------
2938 -- Eval_Op_Expon --
2939 -------------------
2941 -- Exponentiation is a static functions, so the result is potentially
2942 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2944 procedure Eval_Op_Expon (N : Node_Id) is
2945 Left : constant Node_Id := Left_Opnd (N);
2946 Right : constant Node_Id := Right_Opnd (N);
2947 Stat : Boolean;
2948 Fold : Boolean;
2950 begin
2951 -- If not foldable we are done
2953 Test_Expression_Is_Foldable
2954 (N, Left, Right, Stat, Fold, CRT_Safe => True);
2956 -- Return if not foldable
2958 if not Fold then
2959 return;
2960 end if;
2962 if Configurable_Run_Time_Mode and not Stat then
2963 return;
2964 end if;
2966 -- Fold exponentiation operation
2968 declare
2969 Right_Int : constant Uint := Expr_Value (Right);
2971 begin
2972 -- Integer case
2974 if Is_Integer_Type (Etype (Left)) then
2975 declare
2976 Left_Int : constant Uint := Expr_Value (Left);
2977 Result : Uint;
2979 begin
2980 -- Exponentiation of an integer raises Constraint_Error for a
2981 -- negative exponent (RM 4.5.6).
2983 if Right_Int < 0 then
2984 Apply_Compile_Time_Constraint_Error
2985 (N, "integer exponent negative", CE_Range_Check_Failed,
2986 Warn => not Stat);
2987 return;
2989 else
2990 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2991 Result := Left_Int ** Right_Int;
2992 else
2993 Result := Left_Int;
2994 end if;
2996 if Is_Modular_Integer_Type (Etype (N)) then
2997 Result := Result mod Modulus (Etype (N));
2998 end if;
3000 Fold_Uint (N, Result, Stat);
3001 end if;
3002 end;
3004 -- Real case
3006 else
3007 declare
3008 Left_Real : constant Ureal := Expr_Value_R (Left);
3010 begin
3011 -- Cannot have a zero base with a negative exponent
3013 if UR_Is_Zero (Left_Real) then
3015 if Right_Int < 0 then
3016 Apply_Compile_Time_Constraint_Error
3017 (N, "zero ** negative integer", CE_Range_Check_Failed,
3018 Warn => not Stat);
3019 return;
3020 else
3021 Fold_Ureal (N, Ureal_0, Stat);
3022 end if;
3024 else
3025 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
3026 end if;
3027 end;
3028 end if;
3029 end;
3030 end Eval_Op_Expon;
3032 -----------------
3033 -- Eval_Op_Not --
3034 -----------------
3036 -- The not operation is a static functions, so the result is potentially
3037 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3039 procedure Eval_Op_Not (N : Node_Id) is
3040 Right : constant Node_Id := Right_Opnd (N);
3041 Stat : Boolean;
3042 Fold : Boolean;
3044 begin
3045 -- If not foldable we are done
3047 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3049 if not Fold then
3050 return;
3051 end if;
3053 -- Fold not operation
3055 declare
3056 Rint : constant Uint := Expr_Value (Right);
3057 Typ : constant Entity_Id := Etype (N);
3059 begin
3060 -- Negation is equivalent to subtracting from the modulus minus one.
3061 -- For a binary modulus this is equivalent to the ones-complement of
3062 -- the original value. For a nonbinary modulus this is an arbitrary
3063 -- but consistent definition.
3065 if Is_Modular_Integer_Type (Typ) then
3066 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
3067 else pragma Assert (Is_Boolean_Type (Typ));
3068 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
3069 end if;
3071 Set_Is_Static_Expression (N, Stat);
3072 end;
3073 end Eval_Op_Not;
3075 -------------------------------
3076 -- Eval_Qualified_Expression --
3077 -------------------------------
3079 -- A qualified expression is potentially static if its subtype mark denotes
3080 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
3082 procedure Eval_Qualified_Expression (N : Node_Id) is
3083 Operand : constant Node_Id := Expression (N);
3084 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
3086 Stat : Boolean;
3087 Fold : Boolean;
3088 Hex : Boolean;
3090 begin
3091 -- Can only fold if target is string or scalar and subtype is static.
3092 -- Also, do not fold if our parent is an allocator (this is because the
3093 -- qualified expression is really part of the syntactic structure of an
3094 -- allocator, and we do not want to end up with something that
3095 -- corresponds to "new 1" where the 1 is the result of folding a
3096 -- qualified expression).
3098 if not Is_Static_Subtype (Target_Type)
3099 or else Nkind (Parent (N)) = N_Allocator
3100 then
3101 Check_Non_Static_Context (Operand);
3103 -- If operand is known to raise constraint_error, set the flag on the
3104 -- expression so it does not get optimized away.
3106 if Nkind (Operand) = N_Raise_Constraint_Error then
3107 Set_Raises_Constraint_Error (N);
3108 end if;
3110 return;
3111 end if;
3113 -- If not foldable we are done
3115 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3117 if not Fold then
3118 return;
3120 -- Don't try fold if target type has constraint error bounds
3122 elsif not Is_OK_Static_Subtype (Target_Type) then
3123 Set_Raises_Constraint_Error (N);
3124 return;
3125 end if;
3127 -- Here we will fold, save Print_In_Hex indication
3129 Hex := Nkind (Operand) = N_Integer_Literal
3130 and then Print_In_Hex (Operand);
3132 -- Fold the result of qualification
3134 if Is_Discrete_Type (Target_Type) then
3135 Fold_Uint (N, Expr_Value (Operand), Stat);
3137 -- Preserve Print_In_Hex indication
3139 if Hex and then Nkind (N) = N_Integer_Literal then
3140 Set_Print_In_Hex (N);
3141 end if;
3143 elsif Is_Real_Type (Target_Type) then
3144 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
3146 else
3147 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
3149 if not Stat then
3150 Set_Is_Static_Expression (N, False);
3151 else
3152 Check_String_Literal_Length (N, Target_Type);
3153 end if;
3155 return;
3156 end if;
3158 -- The expression may be foldable but not static
3160 Set_Is_Static_Expression (N, Stat);
3162 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3163 Out_Of_Range (N);
3164 end if;
3165 end Eval_Qualified_Expression;
3167 -----------------------
3168 -- Eval_Real_Literal --
3169 -----------------------
3171 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3172 -- as static by the analyzer. The reason we did it that early is to allow
3173 -- the possibility of turning off the Is_Static_Expression flag after
3174 -- analysis, but before resolution, when integer literals are generated
3175 -- in the expander that do not correspond to static expressions.
3177 procedure Eval_Real_Literal (N : Node_Id) is
3178 PK : constant Node_Kind := Nkind (Parent (N));
3180 begin
3181 -- If the literal appears in a non-expression context and not as part of
3182 -- a number declaration, then it is appearing in a non-static context,
3183 -- so check it.
3185 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
3186 Check_Non_Static_Context (N);
3187 end if;
3188 end Eval_Real_Literal;
3190 ------------------------
3191 -- Eval_Relational_Op --
3192 ------------------------
3194 -- Relational operations are static functions, so the result is static if
3195 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3196 -- the result is never static, even if the operands are.
3198 -- However, for internally generated nodes, we allow string equality and
3199 -- inequality to be static. This is because we rewrite A in "ABC" as an
3200 -- equality test A = "ABC", and the former is definitely static.
3202 procedure Eval_Relational_Op (N : Node_Id) is
3203 Left : constant Node_Id := Left_Opnd (N);
3204 Right : constant Node_Id := Right_Opnd (N);
3206 procedure Decompose_Expr
3207 (Expr : Node_Id;
3208 Ent : out Entity_Id;
3209 Kind : out Character;
3210 Cons : out Uint;
3211 Orig : Boolean := True);
3212 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3213 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3214 -- simple entity, and Cons is the value of K. If the expression is not
3215 -- of the required form, Ent is set to Empty.
3217 -- Orig indicates whether Expr is the original expression to consider,
3218 -- or if we are handling a subexpression (e.g. recursive call to
3219 -- Decompose_Expr).
3221 procedure Fold_General_Op (Is_Static : Boolean);
3222 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3223 -- be set when the operator denotes a static expression.
3225 procedure Fold_Static_Real_Op;
3226 -- Attempt to fold static real type relational operator N
3228 function Static_Length (Expr : Node_Id) return Uint;
3229 -- If Expr is an expression for a constrained array whose length is
3230 -- known at compile time, return the non-negative length, otherwise
3231 -- return -1.
3233 --------------------
3234 -- Decompose_Expr --
3235 --------------------
3237 procedure Decompose_Expr
3238 (Expr : Node_Id;
3239 Ent : out Entity_Id;
3240 Kind : out Character;
3241 Cons : out Uint;
3242 Orig : Boolean := True)
3244 Exp : Node_Id;
3246 begin
3247 -- Assume that the expression does not meet the expected form
3249 Cons := No_Uint;
3250 Ent := Empty;
3251 Kind := '?';
3253 if Nkind (Expr) = N_Op_Add
3254 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3255 then
3256 Exp := Left_Opnd (Expr);
3257 Cons := Expr_Value (Right_Opnd (Expr));
3259 elsif Nkind (Expr) = N_Op_Subtract
3260 and then Compile_Time_Known_Value (Right_Opnd (Expr))
3261 then
3262 Exp := Left_Opnd (Expr);
3263 Cons := -Expr_Value (Right_Opnd (Expr));
3265 -- If the bound is a constant created to remove side effects, recover
3266 -- the original expression to see if it has one of the recognizable
3267 -- forms.
3269 elsif Nkind (Expr) = N_Identifier
3270 and then not Comes_From_Source (Entity (Expr))
3271 and then Ekind (Entity (Expr)) = E_Constant
3272 and then Nkind (Parent (Entity (Expr))) = N_Object_Declaration
3273 then
3274 Exp := Expression (Parent (Entity (Expr)));
3275 Decompose_Expr (Exp, Ent, Kind, Cons, Orig => False);
3277 -- If original expression includes an entity, create a reference
3278 -- to it for use below.
3280 if Present (Ent) then
3281 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
3282 else
3283 return;
3284 end if;
3286 else
3287 -- Only consider the case of X + 0 for a full expression, and
3288 -- not when recursing, otherwise we may end up with evaluating
3289 -- expressions not known at compile time to 0.
3291 if Orig then
3292 Exp := Expr;
3293 Cons := Uint_0;
3294 else
3295 return;
3296 end if;
3297 end if;
3299 -- At this stage Exp is set to the potential X
3301 if Nkind (Exp) = N_Attribute_Reference then
3302 if Attribute_Name (Exp) = Name_First then
3303 Kind := 'F';
3304 elsif Attribute_Name (Exp) = Name_Last then
3305 Kind := 'L';
3306 else
3307 return;
3308 end if;
3310 Exp := Prefix (Exp);
3312 else
3313 Kind := 'E';
3314 end if;
3316 if Is_Entity_Name (Exp) and then Present (Entity (Exp)) then
3317 Ent := Entity (Exp);
3318 end if;
3319 end Decompose_Expr;
3321 ---------------------
3322 -- Fold_General_Op --
3323 ---------------------
3325 procedure Fold_General_Op (Is_Static : Boolean) is
3326 CR : constant Compare_Result :=
3327 Compile_Time_Compare (Left, Right, Assume_Valid => False);
3329 Result : Boolean;
3331 begin
3332 if CR = Unknown then
3333 return;
3334 end if;
3336 case Nkind (N) is
3337 when N_Op_Eq =>
3338 if CR = EQ then
3339 Result := True;
3340 elsif CR = NE or else CR = GT or else CR = LT then
3341 Result := False;
3342 else
3343 return;
3344 end if;
3346 when N_Op_Ge =>
3347 if CR = GT or else CR = EQ or else CR = GE then
3348 Result := True;
3349 elsif CR = LT then
3350 Result := False;
3351 else
3352 return;
3353 end if;
3355 when N_Op_Gt =>
3356 if CR = GT then
3357 Result := True;
3358 elsif CR = EQ or else CR = LT or else CR = LE then
3359 Result := False;
3360 else
3361 return;
3362 end if;
3364 when N_Op_Le =>
3365 if CR = LT or else CR = EQ or else CR = LE then
3366 Result := True;
3367 elsif CR = GT then
3368 Result := False;
3369 else
3370 return;
3371 end if;
3373 when N_Op_Lt =>
3374 if CR = LT then
3375 Result := True;
3376 elsif CR = EQ or else CR = GT or else CR = GE then
3377 Result := False;
3378 else
3379 return;
3380 end if;
3382 when N_Op_Ne =>
3383 if CR = NE or else CR = GT or else CR = LT then
3384 Result := True;
3385 elsif CR = EQ then
3386 Result := False;
3387 else
3388 return;
3389 end if;
3391 when others =>
3392 raise Program_Error;
3393 end case;
3395 -- Determine the potential outcome of the relation assuming the
3396 -- operands are valid and emit a warning when the relation yields
3397 -- True or False only in the presence of invalid values.
3399 Warn_On_Constant_Valid_Condition (N);
3401 Fold_Uint (N, Test (Result), Is_Static);
3402 end Fold_General_Op;
3404 -------------------------
3405 -- Fold_Static_Real_Op --
3406 -------------------------
3408 procedure Fold_Static_Real_Op is
3409 Left_Real : constant Ureal := Expr_Value_R (Left);
3410 Right_Real : constant Ureal := Expr_Value_R (Right);
3411 Result : Boolean;
3413 begin
3414 case Nkind (N) is
3415 when N_Op_Eq => Result := (Left_Real = Right_Real);
3416 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3417 when N_Op_Gt => Result := (Left_Real > Right_Real);
3418 when N_Op_Le => Result := (Left_Real <= Right_Real);
3419 when N_Op_Lt => Result := (Left_Real < Right_Real);
3420 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3421 when others => raise Program_Error;
3422 end case;
3424 Fold_Uint (N, Test (Result), True);
3425 end Fold_Static_Real_Op;
3427 -------------------
3428 -- Static_Length --
3429 -------------------
3431 function Static_Length (Expr : Node_Id) return Uint is
3432 Cons1 : Uint;
3433 Cons2 : Uint;
3434 Ent1 : Entity_Id;
3435 Ent2 : Entity_Id;
3436 Kind1 : Character;
3437 Kind2 : Character;
3438 Typ : Entity_Id;
3440 begin
3441 -- First easy case string literal
3443 if Nkind (Expr) = N_String_Literal then
3444 return UI_From_Int (String_Length (Strval (Expr)));
3446 -- With frontend inlining as performed in GNATprove mode, a variable
3447 -- may be inserted that has a string literal subtype. Deal with this
3448 -- specially as for the previous case.
3450 elsif Ekind (Etype (Expr)) = E_String_Literal_Subtype then
3451 return String_Literal_Length (Etype (Expr));
3453 -- Second easy case, not constrained subtype, so no length
3455 elsif not Is_Constrained (Etype (Expr)) then
3456 return Uint_Minus_1;
3457 end if;
3459 -- General case
3461 Typ := Etype (First_Index (Etype (Expr)));
3463 -- The simple case, both bounds are known at compile time
3465 if Is_Discrete_Type (Typ)
3466 and then Compile_Time_Known_Value (Type_Low_Bound (Typ))
3467 and then Compile_Time_Known_Value (Type_High_Bound (Typ))
3468 then
3469 return
3470 UI_Max (Uint_0, Expr_Value (Type_High_Bound (Typ)) -
3471 Expr_Value (Type_Low_Bound (Typ)) + 1);
3472 end if;
3474 -- A more complex case, where the bounds are of the form X [+/- K1]
3475 -- .. X [+/- K2]), where X is an expression that is either A'First or
3476 -- A'Last (with A an entity name), or X is an entity name, and the
3477 -- two X's are the same and K1 and K2 are known at compile time, in
3478 -- this case, the length can also be computed at compile time, even
3479 -- though the bounds are not known. A common case of this is e.g.
3480 -- (X'First .. X'First+5).
3482 Decompose_Expr
3483 (Original_Node (Type_Low_Bound (Typ)), Ent1, Kind1, Cons1);
3484 Decompose_Expr
3485 (Original_Node (Type_High_Bound (Typ)), Ent2, Kind2, Cons2);
3487 if Present (Ent1) and then Ent1 = Ent2 and then Kind1 = Kind2 then
3488 return Cons2 - Cons1 + 1;
3489 else
3490 return Uint_Minus_1;
3491 end if;
3492 end Static_Length;
3494 -- Local variables
3496 Left_Typ : constant Entity_Id := Etype (Left);
3497 Right_Typ : constant Entity_Id := Etype (Right);
3498 Fold : Boolean;
3499 Left_Len : Uint;
3500 Op_Typ : Entity_Id := Empty;
3501 Right_Len : Uint;
3503 Is_Static_Expression : Boolean;
3505 -- Start of processing for Eval_Relational_Op
3507 begin
3508 -- One special case to deal with first. If we can tell that the result
3509 -- will be false because the lengths of one or more index subtypes are
3510 -- compile-time known and different, then we can replace the entire
3511 -- result by False. We only do this for one-dimensional arrays, because
3512 -- the case of multidimensional arrays is rare and too much trouble. If
3513 -- one of the operands is an illegal aggregate, its type might still be
3514 -- an arbitrary composite type, so nothing to do.
3516 if Is_Array_Type (Left_Typ)
3517 and then Left_Typ /= Any_Composite
3518 and then Number_Dimensions (Left_Typ) = 1
3519 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3520 then
3521 if Raises_Constraint_Error (Left)
3522 or else
3523 Raises_Constraint_Error (Right)
3524 then
3525 return;
3527 -- OK, we have the case where we may be able to do this fold
3529 else
3530 Left_Len := Static_Length (Left);
3531 Right_Len := Static_Length (Right);
3533 if Left_Len /= Uint_Minus_1
3534 and then Right_Len /= Uint_Minus_1
3535 and then Left_Len /= Right_Len
3536 then
3537 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
3538 Warn_On_Known_Condition (N);
3539 return;
3540 end if;
3541 end if;
3543 -- General case
3545 else
3546 -- Initialize the value of Is_Static_Expression. The value of Fold
3547 -- returned by Test_Expression_Is_Foldable is not needed since, even
3548 -- when some operand is a variable, we can still perform the static
3549 -- evaluation of the expression in some cases (for example, for a
3550 -- variable of a subtype of Integer we statically know that any value
3551 -- stored in such variable is smaller than Integer'Last).
3553 Test_Expression_Is_Foldable
3554 (N, Left, Right, Is_Static_Expression, Fold);
3556 -- Only comparisons of scalars can give static results. A comparison
3557 -- of strings never yields a static result, even if both operands are
3558 -- static strings, except that as noted above, we allow equality and
3559 -- inequality for strings.
3561 if Is_String_Type (Left_Typ)
3562 and then not Comes_From_Source (N)
3563 and then Nkind_In (N, N_Op_Eq, N_Op_Ne)
3564 then
3565 null;
3567 elsif not Is_Scalar_Type (Left_Typ) then
3568 Is_Static_Expression := False;
3569 Set_Is_Static_Expression (N, False);
3570 end if;
3572 -- For operators on universal numeric types called as functions with
3573 -- an explicit scope, determine appropriate specific numeric type,
3574 -- and diagnose possible ambiguity.
3576 if Is_Universal_Numeric_Type (Left_Typ)
3577 and then
3578 Is_Universal_Numeric_Type (Right_Typ)
3579 then
3580 Op_Typ := Find_Universal_Operator_Type (N);
3581 end if;
3583 -- Attempt to fold the relational operator
3585 if Is_Static_Expression and then Is_Real_Type (Left_Typ) then
3586 Fold_Static_Real_Op;
3587 else
3588 Fold_General_Op (Is_Static_Expression);
3589 end if;
3590 end if;
3592 -- For the case of a folded relational operator on a specific numeric
3593 -- type, freeze the operand type now.
3595 if Present (Op_Typ) then
3596 Freeze_Before (N, Op_Typ);
3597 end if;
3599 Warn_On_Known_Condition (N);
3600 end Eval_Relational_Op;
3602 ----------------
3603 -- Eval_Shift --
3604 ----------------
3606 -- Shift operations are intrinsic operations that can never be static, so
3607 -- the only processing required is to perform the required check for a non
3608 -- static context for the two operands.
3610 -- Actually we could do some compile time evaluation here some time ???
3612 procedure Eval_Shift (N : Node_Id) is
3613 begin
3614 Check_Non_Static_Context (Left_Opnd (N));
3615 Check_Non_Static_Context (Right_Opnd (N));
3616 end Eval_Shift;
3618 ------------------------
3619 -- Eval_Short_Circuit --
3620 ------------------------
3622 -- A short circuit operation is potentially static if both operands are
3623 -- potentially static (RM 4.9 (13)).
3625 procedure Eval_Short_Circuit (N : Node_Id) is
3626 Kind : constant Node_Kind := Nkind (N);
3627 Left : constant Node_Id := Left_Opnd (N);
3628 Right : constant Node_Id := Right_Opnd (N);
3629 Left_Int : Uint;
3631 Rstat : constant Boolean :=
3632 Is_Static_Expression (Left)
3633 and then
3634 Is_Static_Expression (Right);
3636 begin
3637 -- Short circuit operations are never static in Ada 83
3639 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3640 Check_Non_Static_Context (Left);
3641 Check_Non_Static_Context (Right);
3642 return;
3643 end if;
3645 -- Now look at the operands, we can't quite use the normal call to
3646 -- Test_Expression_Is_Foldable here because short circuit operations
3647 -- are a special case, they can still be foldable, even if the right
3648 -- operand raises constraint error.
3650 -- If either operand is Any_Type, just propagate to result and do not
3651 -- try to fold, this prevents cascaded errors.
3653 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3654 Set_Etype (N, Any_Type);
3655 return;
3657 -- If left operand raises constraint error, then replace node N with
3658 -- the raise constraint error node, and we are obviously not foldable.
3659 -- Is_Static_Expression is set from the two operands in the normal way,
3660 -- and we check the right operand if it is in a non-static context.
3662 elsif Raises_Constraint_Error (Left) then
3663 if not Rstat then
3664 Check_Non_Static_Context (Right);
3665 end if;
3667 Rewrite_In_Raise_CE (N, Left);
3668 Set_Is_Static_Expression (N, Rstat);
3669 return;
3671 -- If the result is not static, then we won't in any case fold
3673 elsif not Rstat then
3674 Check_Non_Static_Context (Left);
3675 Check_Non_Static_Context (Right);
3676 return;
3677 end if;
3679 -- Here the result is static, note that, unlike the normal processing
3680 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3681 -- the right operand raises constraint error, that's because it is not
3682 -- significant if the left operand is decisive.
3684 Set_Is_Static_Expression (N);
3686 -- It does not matter if the right operand raises constraint error if
3687 -- it will not be evaluated. So deal specially with the cases where
3688 -- the right operand is not evaluated. Note that we will fold these
3689 -- cases even if the right operand is non-static, which is fine, but
3690 -- of course in these cases the result is not potentially static.
3692 Left_Int := Expr_Value (Left);
3694 if (Kind = N_And_Then and then Is_False (Left_Int))
3695 or else
3696 (Kind = N_Or_Else and then Is_True (Left_Int))
3697 then
3698 Fold_Uint (N, Left_Int, Rstat);
3699 return;
3700 end if;
3702 -- If first operand not decisive, then it does matter if the right
3703 -- operand raises constraint error, since it will be evaluated, so
3704 -- we simply replace the node with the right operand. Note that this
3705 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3706 -- (both are set to True in Right).
3708 if Raises_Constraint_Error (Right) then
3709 Rewrite_In_Raise_CE (N, Right);
3710 Check_Non_Static_Context (Left);
3711 return;
3712 end if;
3714 -- Otherwise the result depends on the right operand
3716 Fold_Uint (N, Expr_Value (Right), Rstat);
3717 return;
3718 end Eval_Short_Circuit;
3720 ----------------
3721 -- Eval_Slice --
3722 ----------------
3724 -- Slices can never be static, so the only processing required is to check
3725 -- for non-static context if an explicit range is given.
3727 procedure Eval_Slice (N : Node_Id) is
3728 Drange : constant Node_Id := Discrete_Range (N);
3730 begin
3731 if Nkind (Drange) = N_Range then
3732 Check_Non_Static_Context (Low_Bound (Drange));
3733 Check_Non_Static_Context (High_Bound (Drange));
3734 end if;
3736 -- A slice of the form A (subtype), when the subtype is the index of
3737 -- the type of A, is redundant, the slice can be replaced with A, and
3738 -- this is worth a warning.
3740 if Is_Entity_Name (Prefix (N)) then
3741 declare
3742 E : constant Entity_Id := Entity (Prefix (N));
3743 T : constant Entity_Id := Etype (E);
3745 begin
3746 if Ekind (E) = E_Constant
3747 and then Is_Array_Type (T)
3748 and then Is_Entity_Name (Drange)
3749 then
3750 if Is_Entity_Name (Original_Node (First_Index (T)))
3751 and then Entity (Original_Node (First_Index (T)))
3752 = Entity (Drange)
3753 then
3754 if Warn_On_Redundant_Constructs then
3755 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3756 end if;
3758 -- The following might be a useful optimization???
3760 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3761 end if;
3762 end if;
3763 end;
3764 end if;
3765 end Eval_Slice;
3767 -------------------------
3768 -- Eval_String_Literal --
3769 -------------------------
3771 procedure Eval_String_Literal (N : Node_Id) is
3772 Typ : constant Entity_Id := Etype (N);
3773 Bas : constant Entity_Id := Base_Type (Typ);
3774 Xtp : Entity_Id;
3775 Len : Nat;
3776 Lo : Node_Id;
3778 begin
3779 -- Nothing to do if error type (handles cases like default expressions
3780 -- or generics where we have not yet fully resolved the type).
3782 if Bas = Any_Type or else Bas = Any_String then
3783 return;
3784 end if;
3786 -- String literals are static if the subtype is static (RM 4.9(2)), so
3787 -- reset the static expression flag (it was set unconditionally in
3788 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3789 -- the subtype is static by looking at the lower bound.
3791 if Ekind (Typ) = E_String_Literal_Subtype then
3792 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3793 Set_Is_Static_Expression (N, False);
3794 return;
3795 end if;
3797 -- Here if Etype of string literal is normal Etype (not yet possible,
3798 -- but may be possible in future).
3800 elsif not Is_OK_Static_Expression
3801 (Type_Low_Bound (Etype (First_Index (Typ))))
3802 then
3803 Set_Is_Static_Expression (N, False);
3804 return;
3805 end if;
3807 -- If original node was a type conversion, then result if non-static
3809 if Nkind (Original_Node (N)) = N_Type_Conversion then
3810 Set_Is_Static_Expression (N, False);
3811 return;
3812 end if;
3814 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3815 -- if its bounds are outside the index base type and this index type is
3816 -- static. This can happen in only two ways. Either the string literal
3817 -- is too long, or it is null, and the lower bound is type'First. Either
3818 -- way it is the upper bound that is out of range of the index type.
3820 if Ada_Version >= Ada_95 then
3821 if Is_Standard_String_Type (Bas) then
3822 Xtp := Standard_Positive;
3823 else
3824 Xtp := Etype (First_Index (Bas));
3825 end if;
3827 if Ekind (Typ) = E_String_Literal_Subtype then
3828 Lo := String_Literal_Low_Bound (Typ);
3829 else
3830 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3831 end if;
3833 -- Check for string too long
3835 Len := String_Length (Strval (N));
3837 if UI_From_Int (Len) > String_Type_Len (Bas) then
3839 -- Issue message. Note that this message is a warning if the
3840 -- string literal is not marked as static (happens in some cases
3841 -- of folding strings known at compile time, but not static).
3842 -- Furthermore in such cases, we reword the message, since there
3843 -- is no string literal in the source program.
3845 if Is_Static_Expression (N) then
3846 Apply_Compile_Time_Constraint_Error
3847 (N, "string literal too long for}", CE_Length_Check_Failed,
3848 Ent => Bas,
3849 Typ => First_Subtype (Bas));
3850 else
3851 Apply_Compile_Time_Constraint_Error
3852 (N, "string value too long for}", CE_Length_Check_Failed,
3853 Ent => Bas,
3854 Typ => First_Subtype (Bas),
3855 Warn => True);
3856 end if;
3858 -- Test for null string not allowed
3860 elsif Len = 0
3861 and then not Is_Generic_Type (Xtp)
3862 and then
3863 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3864 then
3865 -- Same specialization of message
3867 if Is_Static_Expression (N) then
3868 Apply_Compile_Time_Constraint_Error
3869 (N, "null string literal not allowed for}",
3870 CE_Length_Check_Failed,
3871 Ent => Bas,
3872 Typ => First_Subtype (Bas));
3873 else
3874 Apply_Compile_Time_Constraint_Error
3875 (N, "null string value not allowed for}",
3876 CE_Length_Check_Failed,
3877 Ent => Bas,
3878 Typ => First_Subtype (Bas),
3879 Warn => True);
3880 end if;
3881 end if;
3882 end if;
3883 end Eval_String_Literal;
3885 --------------------------
3886 -- Eval_Type_Conversion --
3887 --------------------------
3889 -- A type conversion is potentially static if its subtype mark is for a
3890 -- static scalar subtype, and its operand expression is potentially static
3891 -- (RM 4.9(10)).
3893 procedure Eval_Type_Conversion (N : Node_Id) is
3894 Operand : constant Node_Id := Expression (N);
3895 Source_Type : constant Entity_Id := Etype (Operand);
3896 Target_Type : constant Entity_Id := Etype (N);
3898 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3899 -- Returns true if type T is an integer type, or if it is a fixed-point
3900 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3901 -- on the conversion node).
3903 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3904 -- Returns true if type T is a floating-point type, or if it is a
3905 -- fixed-point type that is not to be treated as an integer (i.e. the
3906 -- flag Conversion_OK is not set on the conversion node).
3908 ------------------------------
3909 -- To_Be_Treated_As_Integer --
3910 ------------------------------
3912 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3913 begin
3914 return
3915 Is_Integer_Type (T)
3916 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3917 end To_Be_Treated_As_Integer;
3919 ---------------------------
3920 -- To_Be_Treated_As_Real --
3921 ---------------------------
3923 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3924 begin
3925 return
3926 Is_Floating_Point_Type (T)
3927 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3928 end To_Be_Treated_As_Real;
3930 -- Local variables
3932 Fold : Boolean;
3933 Stat : Boolean;
3935 -- Start of processing for Eval_Type_Conversion
3937 begin
3938 -- Cannot fold if target type is non-static or if semantic error
3940 if not Is_Static_Subtype (Target_Type) then
3941 Check_Non_Static_Context (Operand);
3942 return;
3943 elsif Error_Posted (N) then
3944 return;
3945 end if;
3947 -- If not foldable we are done
3949 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3951 if not Fold then
3952 return;
3954 -- Don't try fold if target type has constraint error bounds
3956 elsif not Is_OK_Static_Subtype (Target_Type) then
3957 Set_Raises_Constraint_Error (N);
3958 return;
3959 end if;
3961 -- Remaining processing depends on operand types. Note that in the
3962 -- following type test, fixed-point counts as real unless the flag
3963 -- Conversion_OK is set, in which case it counts as integer.
3965 -- Fold conversion, case of string type. The result is not static
3967 if Is_String_Type (Target_Type) then
3968 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3969 return;
3971 -- Fold conversion, case of integer target type
3973 elsif To_Be_Treated_As_Integer (Target_Type) then
3974 declare
3975 Result : Uint;
3977 begin
3978 -- Integer to integer conversion
3980 if To_Be_Treated_As_Integer (Source_Type) then
3981 Result := Expr_Value (Operand);
3983 -- Real to integer conversion
3985 else
3986 Result := UR_To_Uint (Expr_Value_R (Operand));
3987 end if;
3989 -- If fixed-point type (Conversion_OK must be set), then the
3990 -- result is logically an integer, but we must replace the
3991 -- conversion with the corresponding real literal, since the
3992 -- type from a semantic point of view is still fixed-point.
3994 if Is_Fixed_Point_Type (Target_Type) then
3995 Fold_Ureal
3996 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3998 -- Otherwise result is integer literal
4000 else
4001 Fold_Uint (N, Result, Stat);
4002 end if;
4003 end;
4005 -- Fold conversion, case of real target type
4007 elsif To_Be_Treated_As_Real (Target_Type) then
4008 declare
4009 Result : Ureal;
4011 begin
4012 if To_Be_Treated_As_Real (Source_Type) then
4013 Result := Expr_Value_R (Operand);
4014 else
4015 Result := UR_From_Uint (Expr_Value (Operand));
4016 end if;
4018 Fold_Ureal (N, Result, Stat);
4019 end;
4021 -- Enumeration types
4023 else
4024 Fold_Uint (N, Expr_Value (Operand), Stat);
4025 end if;
4027 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
4028 Out_Of_Range (N);
4029 end if;
4031 end Eval_Type_Conversion;
4033 -------------------
4034 -- Eval_Unary_Op --
4035 -------------------
4037 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4038 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4040 procedure Eval_Unary_Op (N : Node_Id) is
4041 Right : constant Node_Id := Right_Opnd (N);
4042 Otype : Entity_Id := Empty;
4043 Stat : Boolean;
4044 Fold : Boolean;
4046 begin
4047 -- If not foldable we are done
4049 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
4051 if not Fold then
4052 return;
4053 end if;
4055 if Etype (Right) = Universal_Integer
4056 or else
4057 Etype (Right) = Universal_Real
4058 then
4059 Otype := Find_Universal_Operator_Type (N);
4060 end if;
4062 -- Fold for integer case
4064 if Is_Integer_Type (Etype (N)) then
4065 declare
4066 Rint : constant Uint := Expr_Value (Right);
4067 Result : Uint;
4069 begin
4070 -- In the case of modular unary plus and abs there is no need
4071 -- to adjust the result of the operation since if the original
4072 -- operand was in bounds the result will be in the bounds of the
4073 -- modular type. However, in the case of modular unary minus the
4074 -- result may go out of the bounds of the modular type and needs
4075 -- adjustment.
4077 if Nkind (N) = N_Op_Plus then
4078 Result := Rint;
4080 elsif Nkind (N) = N_Op_Minus then
4081 if Is_Modular_Integer_Type (Etype (N)) then
4082 Result := (-Rint) mod Modulus (Etype (N));
4083 else
4084 Result := (-Rint);
4085 end if;
4087 else
4088 pragma Assert (Nkind (N) = N_Op_Abs);
4089 Result := abs Rint;
4090 end if;
4092 Fold_Uint (N, Result, Stat);
4093 end;
4095 -- Fold for real case
4097 elsif Is_Real_Type (Etype (N)) then
4098 declare
4099 Rreal : constant Ureal := Expr_Value_R (Right);
4100 Result : Ureal;
4102 begin
4103 if Nkind (N) = N_Op_Plus then
4104 Result := Rreal;
4105 elsif Nkind (N) = N_Op_Minus then
4106 Result := UR_Negate (Rreal);
4107 else
4108 pragma Assert (Nkind (N) = N_Op_Abs);
4109 Result := abs Rreal;
4110 end if;
4112 Fold_Ureal (N, Result, Stat);
4113 end;
4114 end if;
4116 -- If the operator was resolved to a specific type, make sure that type
4117 -- is frozen even if the expression is folded into a literal (which has
4118 -- a universal type).
4120 if Present (Otype) then
4121 Freeze_Before (N, Otype);
4122 end if;
4123 end Eval_Unary_Op;
4125 -------------------------------
4126 -- Eval_Unchecked_Conversion --
4127 -------------------------------
4129 -- Unchecked conversions can never be static, so the only required
4130 -- processing is to check for a non-static context for the operand.
4132 procedure Eval_Unchecked_Conversion (N : Node_Id) is
4133 begin
4134 Check_Non_Static_Context (Expression (N));
4135 end Eval_Unchecked_Conversion;
4137 --------------------
4138 -- Expr_Rep_Value --
4139 --------------------
4141 function Expr_Rep_Value (N : Node_Id) return Uint is
4142 Kind : constant Node_Kind := Nkind (N);
4143 Ent : Entity_Id;
4145 begin
4146 if Is_Entity_Name (N) then
4147 Ent := Entity (N);
4149 -- An enumeration literal that was either in the source or created
4150 -- as a result of static evaluation.
4152 if Ekind (Ent) = E_Enumeration_Literal then
4153 return Enumeration_Rep (Ent);
4155 -- A user defined static constant
4157 else
4158 pragma Assert (Ekind (Ent) = E_Constant);
4159 return Expr_Rep_Value (Constant_Value (Ent));
4160 end if;
4162 -- An integer literal that was either in the source or created as a
4163 -- result of static evaluation.
4165 elsif Kind = N_Integer_Literal then
4166 return Intval (N);
4168 -- A real literal for a fixed-point type. This must be the fixed-point
4169 -- case, either the literal is of a fixed-point type, or it is a bound
4170 -- of a fixed-point type, with type universal real. In either case we
4171 -- obtain the desired value from Corresponding_Integer_Value.
4173 elsif Kind = N_Real_Literal then
4174 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4175 return Corresponding_Integer_Value (N);
4177 -- Otherwise must be character literal
4179 else
4180 pragma Assert (Kind = N_Character_Literal);
4181 Ent := Entity (N);
4183 -- Since Character literals of type Standard.Character don't have any
4184 -- defining character literals built for them, they do not have their
4185 -- Entity set, so just use their Char code. Otherwise for user-
4186 -- defined character literals use their Pos value as usual which is
4187 -- the same as the Rep value.
4189 if No (Ent) then
4190 return Char_Literal_Value (N);
4191 else
4192 return Enumeration_Rep (Ent);
4193 end if;
4194 end if;
4195 end Expr_Rep_Value;
4197 ----------------
4198 -- Expr_Value --
4199 ----------------
4201 function Expr_Value (N : Node_Id) return Uint is
4202 Kind : constant Node_Kind := Nkind (N);
4203 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
4204 Ent : Entity_Id;
4205 Val : Uint;
4207 begin
4208 -- If already in cache, then we know it's compile-time-known and we can
4209 -- return the value that was previously stored in the cache since
4210 -- compile-time-known values cannot change.
4212 if CV_Ent.N = N then
4213 return CV_Ent.V;
4214 end if;
4216 -- Otherwise proceed to test value
4218 if Is_Entity_Name (N) then
4219 Ent := Entity (N);
4221 -- An enumeration literal that was either in the source or created as
4222 -- a result of static evaluation.
4224 if Ekind (Ent) = E_Enumeration_Literal then
4225 Val := Enumeration_Pos (Ent);
4227 -- A user defined static constant
4229 else
4230 pragma Assert (Ekind (Ent) = E_Constant);
4231 Val := Expr_Value (Constant_Value (Ent));
4232 end if;
4234 -- An integer literal that was either in the source or created as a
4235 -- result of static evaluation.
4237 elsif Kind = N_Integer_Literal then
4238 Val := Intval (N);
4240 -- A real literal for a fixed-point type. This must be the fixed-point
4241 -- case, either the literal is of a fixed-point type, or it is a bound
4242 -- of a fixed-point type, with type universal real. In either case we
4243 -- obtain the desired value from Corresponding_Integer_Value.
4245 elsif Kind = N_Real_Literal then
4246 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
4247 Val := Corresponding_Integer_Value (N);
4249 -- The NULL access value
4251 elsif Kind = N_Null then
4252 pragma Assert (Is_Access_Type (Underlying_Type (Etype (N))));
4253 Val := Uint_0;
4255 -- Otherwise must be character literal
4257 else
4258 pragma Assert (Kind = N_Character_Literal);
4259 Ent := Entity (N);
4261 -- Since Character literals of type Standard.Character don't
4262 -- have any defining character literals built for them, they
4263 -- do not have their Entity set, so just use their Char
4264 -- code. Otherwise for user-defined character literals use
4265 -- their Pos value as usual.
4267 if No (Ent) then
4268 Val := Char_Literal_Value (N);
4269 else
4270 Val := Enumeration_Pos (Ent);
4271 end if;
4272 end if;
4274 -- Come here with Val set to value to be returned, set cache
4276 CV_Ent.N := N;
4277 CV_Ent.V := Val;
4278 return Val;
4279 end Expr_Value;
4281 ------------------
4282 -- Expr_Value_E --
4283 ------------------
4285 function Expr_Value_E (N : Node_Id) return Entity_Id is
4286 Ent : constant Entity_Id := Entity (N);
4287 begin
4288 if Ekind (Ent) = E_Enumeration_Literal then
4289 return Ent;
4290 else
4291 pragma Assert (Ekind (Ent) = E_Constant);
4292 return Expr_Value_E (Constant_Value (Ent));
4293 end if;
4294 end Expr_Value_E;
4296 ------------------
4297 -- Expr_Value_R --
4298 ------------------
4300 function Expr_Value_R (N : Node_Id) return Ureal is
4301 Kind : constant Node_Kind := Nkind (N);
4302 Ent : Entity_Id;
4304 begin
4305 if Kind = N_Real_Literal then
4306 return Realval (N);
4308 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
4309 Ent := Entity (N);
4310 pragma Assert (Ekind (Ent) = E_Constant);
4311 return Expr_Value_R (Constant_Value (Ent));
4313 elsif Kind = N_Integer_Literal then
4314 return UR_From_Uint (Expr_Value (N));
4316 -- Here, we have a node that cannot be interpreted as a compile time
4317 -- constant. That is definitely an error.
4319 else
4320 raise Program_Error;
4321 end if;
4322 end Expr_Value_R;
4324 ------------------
4325 -- Expr_Value_S --
4326 ------------------
4328 function Expr_Value_S (N : Node_Id) return Node_Id is
4329 begin
4330 if Nkind (N) = N_String_Literal then
4331 return N;
4332 else
4333 pragma Assert (Ekind (Entity (N)) = E_Constant);
4334 return Expr_Value_S (Constant_Value (Entity (N)));
4335 end if;
4336 end Expr_Value_S;
4338 ----------------------------------
4339 -- Find_Universal_Operator_Type --
4340 ----------------------------------
4342 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
4343 PN : constant Node_Id := Parent (N);
4344 Call : constant Node_Id := Original_Node (N);
4345 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
4347 Is_Fix : constant Boolean :=
4348 Nkind (N) in N_Binary_Op
4349 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
4350 -- A mixed-mode operation in this context indicates the presence of
4351 -- fixed-point type in the designated package.
4353 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
4354 -- Case where N is a relational (or membership) operator (else it is an
4355 -- arithmetic one).
4357 In_Membership : constant Boolean :=
4358 Nkind (PN) in N_Membership_Test
4359 and then
4360 Nkind (Right_Opnd (PN)) = N_Range
4361 and then
4362 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
4363 and then
4364 Is_Universal_Numeric_Type
4365 (Etype (Low_Bound (Right_Opnd (PN))))
4366 and then
4367 Is_Universal_Numeric_Type
4368 (Etype (High_Bound (Right_Opnd (PN))));
4369 -- Case where N is part of a membership test with a universal range
4371 E : Entity_Id;
4372 Pack : Entity_Id;
4373 Typ1 : Entity_Id := Empty;
4374 Priv_E : Entity_Id;
4376 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
4377 -- Check whether one operand is a mixed-mode operation that requires the
4378 -- presence of a fixed-point type. Given that all operands are universal
4379 -- and have been constant-folded, retrieve the original function call.
4381 ---------------------------
4382 -- Is_Mixed_Mode_Operand --
4383 ---------------------------
4385 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
4386 Onod : constant Node_Id := Original_Node (Op);
4387 begin
4388 return Nkind (Onod) = N_Function_Call
4389 and then Present (Next_Actual (First_Actual (Onod)))
4390 and then Etype (First_Actual (Onod)) /=
4391 Etype (Next_Actual (First_Actual (Onod)));
4392 end Is_Mixed_Mode_Operand;
4394 -- Start of processing for Find_Universal_Operator_Type
4396 begin
4397 if Nkind (Call) /= N_Function_Call
4398 or else Nkind (Name (Call)) /= N_Expanded_Name
4399 then
4400 return Empty;
4402 -- There are several cases where the context does not imply the type of
4403 -- the operands:
4404 -- - the universal expression appears in a type conversion;
4405 -- - the expression is a relational operator applied to universal
4406 -- operands;
4407 -- - the expression is a membership test with a universal operand
4408 -- and a range with universal bounds.
4410 elsif Nkind (Parent (N)) = N_Type_Conversion
4411 or else Is_Relational
4412 or else In_Membership
4413 then
4414 Pack := Entity (Prefix (Name (Call)));
4416 -- If the prefix is a package declared elsewhere, iterate over its
4417 -- visible entities, otherwise iterate over all declarations in the
4418 -- designated scope.
4420 if Ekind (Pack) = E_Package
4421 and then not In_Open_Scopes (Pack)
4422 then
4423 Priv_E := First_Private_Entity (Pack);
4424 else
4425 Priv_E := Empty;
4426 end if;
4428 Typ1 := Empty;
4429 E := First_Entity (Pack);
4430 while Present (E) and then E /= Priv_E loop
4431 if Is_Numeric_Type (E)
4432 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4433 and then Comes_From_Source (E)
4434 and then Is_Integer_Type (E) = Is_Int
4435 and then (Nkind (N) in N_Unary_Op
4436 or else Is_Relational
4437 or else Is_Fixed_Point_Type (E) = Is_Fix)
4438 then
4439 if No (Typ1) then
4440 Typ1 := E;
4442 -- Before emitting an error, check for the presence of a
4443 -- mixed-mode operation that specifies a fixed point type.
4445 elsif Is_Relational
4446 and then
4447 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4448 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4449 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4451 then
4452 if Is_Fixed_Point_Type (E) then
4453 Typ1 := E;
4454 end if;
4456 else
4457 -- More than one type of the proper class declared in P
4459 Error_Msg_N ("ambiguous operation", N);
4460 Error_Msg_Sloc := Sloc (Typ1);
4461 Error_Msg_N ("\possible interpretation (inherited)#", N);
4462 Error_Msg_Sloc := Sloc (E);
4463 Error_Msg_N ("\possible interpretation (inherited)#", N);
4464 return Empty;
4465 end if;
4466 end if;
4468 Next_Entity (E);
4469 end loop;
4470 end if;
4472 return Typ1;
4473 end Find_Universal_Operator_Type;
4475 --------------------------
4476 -- Flag_Non_Static_Expr --
4477 --------------------------
4479 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4480 begin
4481 if Error_Posted (Expr) and then not All_Errors_Mode then
4482 return;
4483 else
4484 Error_Msg_F (Msg, Expr);
4485 Why_Not_Static (Expr);
4486 end if;
4487 end Flag_Non_Static_Expr;
4489 --------------
4490 -- Fold_Str --
4491 --------------
4493 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4494 Loc : constant Source_Ptr := Sloc (N);
4495 Typ : constant Entity_Id := Etype (N);
4497 begin
4498 if Raises_Constraint_Error (N) then
4499 Set_Is_Static_Expression (N, Static);
4500 return;
4501 end if;
4503 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4505 -- We now have the literal with the right value, both the actual type
4506 -- and the expected type of this literal are taken from the expression
4507 -- that was evaluated. So now we do the Analyze and Resolve.
4509 -- Note that we have to reset Is_Static_Expression both after the
4510 -- analyze step (because Resolve will evaluate the literal, which
4511 -- will cause semantic errors if it is marked as static), and after
4512 -- the Resolve step (since Resolve in some cases resets this flag).
4514 Analyze (N);
4515 Set_Is_Static_Expression (N, Static);
4516 Set_Etype (N, Typ);
4517 Resolve (N);
4518 Set_Is_Static_Expression (N, Static);
4519 end Fold_Str;
4521 ---------------
4522 -- Fold_Uint --
4523 ---------------
4525 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4526 Loc : constant Source_Ptr := Sloc (N);
4527 Typ : Entity_Id := Etype (N);
4528 Ent : Entity_Id;
4530 begin
4531 if Raises_Constraint_Error (N) then
4532 Set_Is_Static_Expression (N, Static);
4533 return;
4534 end if;
4536 -- If we are folding a named number, retain the entity in the literal,
4537 -- for ASIS use.
4539 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Integer then
4540 Ent := Entity (N);
4541 else
4542 Ent := Empty;
4543 end if;
4545 if Is_Private_Type (Typ) then
4546 Typ := Full_View (Typ);
4547 end if;
4549 -- For a result of type integer, substitute an N_Integer_Literal node
4550 -- for the result of the compile time evaluation of the expression.
4551 -- For ASIS use, set a link to the original named number when not in
4552 -- a generic context.
4554 if Is_Integer_Type (Typ) then
4555 Rewrite (N, Make_Integer_Literal (Loc, Val));
4556 Set_Original_Entity (N, Ent);
4558 -- Otherwise we have an enumeration type, and we substitute either
4559 -- an N_Identifier or N_Character_Literal to represent the enumeration
4560 -- literal corresponding to the given value, which must always be in
4561 -- range, because appropriate tests have already been made for this.
4563 else pragma Assert (Is_Enumeration_Type (Typ));
4564 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4565 end if;
4567 -- We now have the literal with the right value, both the actual type
4568 -- and the expected type of this literal are taken from the expression
4569 -- that was evaluated. So now we do the Analyze and Resolve.
4571 -- Note that we have to reset Is_Static_Expression both after the
4572 -- analyze step (because Resolve will evaluate the literal, which
4573 -- will cause semantic errors if it is marked as static), and after
4574 -- the Resolve step (since Resolve in some cases sets this flag).
4576 Analyze (N);
4577 Set_Is_Static_Expression (N, Static);
4578 Set_Etype (N, Typ);
4579 Resolve (N);
4580 Set_Is_Static_Expression (N, Static);
4581 end Fold_Uint;
4583 ----------------
4584 -- Fold_Ureal --
4585 ----------------
4587 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4588 Loc : constant Source_Ptr := Sloc (N);
4589 Typ : constant Entity_Id := Etype (N);
4590 Ent : Entity_Id;
4592 begin
4593 if Raises_Constraint_Error (N) then
4594 Set_Is_Static_Expression (N, Static);
4595 return;
4596 end if;
4598 -- If we are folding a named number, retain the entity in the literal,
4599 -- for ASIS use.
4601 if Is_Entity_Name (N) and then Ekind (Entity (N)) = E_Named_Real then
4602 Ent := Entity (N);
4603 else
4604 Ent := Empty;
4605 end if;
4607 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4609 -- Set link to original named number, for ASIS use
4611 Set_Original_Entity (N, Ent);
4613 -- We now have the literal with the right value, both the actual type
4614 -- and the expected type of this literal are taken from the expression
4615 -- that was evaluated. So now we do the Analyze and Resolve.
4617 -- Note that we have to reset Is_Static_Expression both after the
4618 -- analyze step (because Resolve will evaluate the literal, which
4619 -- will cause semantic errors if it is marked as static), and after
4620 -- the Resolve step (since Resolve in some cases sets this flag).
4622 Analyze (N);
4623 Set_Is_Static_Expression (N, Static);
4624 Set_Etype (N, Typ);
4625 Resolve (N);
4626 Set_Is_Static_Expression (N, Static);
4627 end Fold_Ureal;
4629 ---------------
4630 -- From_Bits --
4631 ---------------
4633 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4634 V : Uint := Uint_0;
4636 begin
4637 for J in 0 .. B'Last loop
4638 if B (J) then
4639 V := V + 2 ** J;
4640 end if;
4641 end loop;
4643 if Non_Binary_Modulus (T) then
4644 V := V mod Modulus (T);
4645 end if;
4647 return V;
4648 end From_Bits;
4650 --------------------
4651 -- Get_String_Val --
4652 --------------------
4654 function Get_String_Val (N : Node_Id) return Node_Id is
4655 begin
4656 if Nkind_In (N, N_String_Literal, N_Character_Literal) then
4657 return N;
4658 else
4659 pragma Assert (Is_Entity_Name (N));
4660 return Get_String_Val (Constant_Value (Entity (N)));
4661 end if;
4662 end Get_String_Val;
4664 ----------------
4665 -- Initialize --
4666 ----------------
4668 procedure Initialize is
4669 begin
4670 CV_Cache := (others => (Node_High_Bound, Uint_0));
4671 end Initialize;
4673 --------------------
4674 -- In_Subrange_Of --
4675 --------------------
4677 function In_Subrange_Of
4678 (T1 : Entity_Id;
4679 T2 : Entity_Id;
4680 Fixed_Int : Boolean := False) return Boolean
4682 L1 : Node_Id;
4683 H1 : Node_Id;
4685 L2 : Node_Id;
4686 H2 : Node_Id;
4688 begin
4689 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4690 return True;
4692 -- Never in range if both types are not scalar. Don't know if this can
4693 -- actually happen, but just in case.
4695 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4696 return False;
4698 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4699 -- definitely not compatible with T2.
4701 elsif Is_Floating_Point_Type (T1)
4702 and then Has_Infinities (T1)
4703 and then Is_Floating_Point_Type (T2)
4704 and then not Has_Infinities (T2)
4705 then
4706 return False;
4708 else
4709 L1 := Type_Low_Bound (T1);
4710 H1 := Type_High_Bound (T1);
4712 L2 := Type_Low_Bound (T2);
4713 H2 := Type_High_Bound (T2);
4715 -- Check bounds to see if comparison possible at compile time
4717 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4718 and then
4719 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4720 then
4721 return True;
4722 end if;
4724 -- If bounds not comparable at compile time, then the bounds of T2
4725 -- must be compile-time-known or we cannot answer the query.
4727 if not Compile_Time_Known_Value (L2)
4728 or else not Compile_Time_Known_Value (H2)
4729 then
4730 return False;
4731 end if;
4733 -- If the bounds of T1 are know at compile time then use these
4734 -- ones, otherwise use the bounds of the base type (which are of
4735 -- course always static).
4737 if not Compile_Time_Known_Value (L1) then
4738 L1 := Type_Low_Bound (Base_Type (T1));
4739 end if;
4741 if not Compile_Time_Known_Value (H1) then
4742 H1 := Type_High_Bound (Base_Type (T1));
4743 end if;
4745 -- Fixed point types should be considered as such only if
4746 -- flag Fixed_Int is set to False.
4748 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4749 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4750 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4751 then
4752 return
4753 Expr_Value_R (L2) <= Expr_Value_R (L1)
4754 and then
4755 Expr_Value_R (H2) >= Expr_Value_R (H1);
4757 else
4758 return
4759 Expr_Value (L2) <= Expr_Value (L1)
4760 and then
4761 Expr_Value (H2) >= Expr_Value (H1);
4763 end if;
4764 end if;
4766 -- If any exception occurs, it means that we have some bug in the compiler
4767 -- possibly triggered by a previous error, or by some unforeseen peculiar
4768 -- occurrence. However, this is only an optimization attempt, so there is
4769 -- really no point in crashing the compiler. Instead we just decide, too
4770 -- bad, we can't figure out the answer in this case after all.
4772 exception
4773 when others =>
4775 -- Debug flag K disables this behavior (useful for debugging)
4777 if Debug_Flag_K then
4778 raise;
4779 else
4780 return False;
4781 end if;
4782 end In_Subrange_Of;
4784 -----------------
4785 -- Is_In_Range --
4786 -----------------
4788 function Is_In_Range
4789 (N : Node_Id;
4790 Typ : Entity_Id;
4791 Assume_Valid : Boolean := False;
4792 Fixed_Int : Boolean := False;
4793 Int_Real : Boolean := False) return Boolean
4795 begin
4796 return
4797 Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) = In_Range;
4798 end Is_In_Range;
4800 -------------------
4801 -- Is_Null_Range --
4802 -------------------
4804 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4805 begin
4806 if Compile_Time_Known_Value (Lo)
4807 and then Compile_Time_Known_Value (Hi)
4808 then
4809 declare
4810 Typ : Entity_Id := Etype (Lo);
4811 begin
4812 -- When called from the frontend, as part of the analysis of
4813 -- potentially static expressions, Typ will be the full view of a
4814 -- type with all the info needed to answer this query. When called
4815 -- from the backend, for example to know whether a range of a loop
4816 -- is null, Typ might be a private type and we need to explicitly
4817 -- switch to its corresponding full view to access the same info.
4819 if Is_Incomplete_Or_Private_Type (Typ)
4820 and then Present (Full_View (Typ))
4821 then
4822 Typ := Full_View (Typ);
4823 end if;
4825 if Is_Discrete_Type (Typ) then
4826 return Expr_Value (Lo) > Expr_Value (Hi);
4827 else pragma Assert (Is_Real_Type (Typ));
4828 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4829 end if;
4830 end;
4831 else
4832 return False;
4833 end if;
4834 end Is_Null_Range;
4836 -------------------------
4837 -- Is_OK_Static_Choice --
4838 -------------------------
4840 function Is_OK_Static_Choice (Choice : Node_Id) return Boolean is
4841 begin
4842 -- Check various possibilities for choice
4844 -- Note: for membership tests, we test more cases than are possible
4845 -- (in particular subtype indication), but it doesn't matter because
4846 -- it just won't occur (we have already done a syntax check).
4848 if Nkind (Choice) = N_Others_Choice then
4849 return True;
4851 elsif Nkind (Choice) = N_Range then
4852 return Is_OK_Static_Range (Choice);
4854 elsif Nkind (Choice) = N_Subtype_Indication
4855 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
4856 then
4857 return Is_OK_Static_Subtype (Etype (Choice));
4859 else
4860 return Is_OK_Static_Expression (Choice);
4861 end if;
4862 end Is_OK_Static_Choice;
4864 ------------------------------
4865 -- Is_OK_Static_Choice_List --
4866 ------------------------------
4868 function Is_OK_Static_Choice_List (Choices : List_Id) return Boolean is
4869 Choice : Node_Id;
4871 begin
4872 if not Is_Static_Choice_List (Choices) then
4873 return False;
4874 end if;
4876 Choice := First (Choices);
4877 while Present (Choice) loop
4878 if not Is_OK_Static_Choice (Choice) then
4879 Set_Raises_Constraint_Error (Choice);
4880 return False;
4881 end if;
4883 Next (Choice);
4884 end loop;
4886 return True;
4887 end Is_OK_Static_Choice_List;
4889 -----------------------------
4890 -- Is_OK_Static_Expression --
4891 -----------------------------
4893 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4894 begin
4895 return Is_Static_Expression (N) and then not Raises_Constraint_Error (N);
4896 end Is_OK_Static_Expression;
4898 ------------------------
4899 -- Is_OK_Static_Range --
4900 ------------------------
4902 -- A static range is a range whose bounds are static expressions, or a
4903 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4904 -- We have already converted range attribute references, so we get the
4905 -- "or" part of this rule without needing a special test.
4907 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4908 begin
4909 return Is_OK_Static_Expression (Low_Bound (N))
4910 and then Is_OK_Static_Expression (High_Bound (N));
4911 end Is_OK_Static_Range;
4913 --------------------------
4914 -- Is_OK_Static_Subtype --
4915 --------------------------
4917 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4918 -- neither bound raises constraint error when evaluated.
4920 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4921 Base_T : constant Entity_Id := Base_Type (Typ);
4922 Anc_Subt : Entity_Id;
4924 begin
4925 -- First a quick check on the non static subtype flag. As described
4926 -- in further detail in Einfo, this flag is not decisive in all cases,
4927 -- but if it is set, then the subtype is definitely non-static.
4929 if Is_Non_Static_Subtype (Typ) then
4930 return False;
4931 end if;
4933 Anc_Subt := Ancestor_Subtype (Typ);
4935 if Anc_Subt = Empty then
4936 Anc_Subt := Base_T;
4937 end if;
4939 if Is_Generic_Type (Root_Type (Base_T))
4940 or else Is_Generic_Actual_Type (Base_T)
4941 then
4942 return False;
4944 elsif Has_Dynamic_Predicate_Aspect (Typ) then
4945 return False;
4947 -- String types
4949 elsif Is_String_Type (Typ) then
4950 return
4951 Ekind (Typ) = E_String_Literal_Subtype
4952 or else
4953 (Is_OK_Static_Subtype (Component_Type (Typ))
4954 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4956 -- Scalar types
4958 elsif Is_Scalar_Type (Typ) then
4959 if Base_T = Typ then
4960 return True;
4962 else
4963 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4964 -- Get_Type_{Low,High}_Bound.
4966 return Is_OK_Static_Subtype (Anc_Subt)
4967 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4968 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4969 end if;
4971 -- Types other than string and scalar types are never static
4973 else
4974 return False;
4975 end if;
4976 end Is_OK_Static_Subtype;
4978 ---------------------
4979 -- Is_Out_Of_Range --
4980 ---------------------
4982 function Is_Out_Of_Range
4983 (N : Node_Id;
4984 Typ : Entity_Id;
4985 Assume_Valid : Boolean := False;
4986 Fixed_Int : Boolean := False;
4987 Int_Real : Boolean := False) return Boolean
4989 begin
4990 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real) =
4991 Out_Of_Range;
4992 end Is_Out_Of_Range;
4994 ----------------------
4995 -- Is_Static_Choice --
4996 ----------------------
4998 function Is_Static_Choice (Choice : Node_Id) return Boolean is
4999 begin
5000 -- Check various possibilities for choice
5002 -- Note: for membership tests, we test more cases than are possible
5003 -- (in particular subtype indication), but it doesn't matter because
5004 -- it just won't occur (we have already done a syntax check).
5006 if Nkind (Choice) = N_Others_Choice then
5007 return True;
5009 elsif Nkind (Choice) = N_Range then
5010 return Is_Static_Range (Choice);
5012 elsif Nkind (Choice) = N_Subtype_Indication
5013 or else (Is_Entity_Name (Choice) and then Is_Type (Entity (Choice)))
5014 then
5015 return Is_Static_Subtype (Etype (Choice));
5017 else
5018 return Is_Static_Expression (Choice);
5019 end if;
5020 end Is_Static_Choice;
5022 ---------------------------
5023 -- Is_Static_Choice_List --
5024 ---------------------------
5026 function Is_Static_Choice_List (Choices : List_Id) return Boolean is
5027 Choice : Node_Id;
5029 begin
5030 Choice := First (Choices);
5031 while Present (Choice) loop
5032 if not Is_Static_Choice (Choice) then
5033 return False;
5034 end if;
5036 Next (Choice);
5037 end loop;
5039 return True;
5040 end Is_Static_Choice_List;
5042 ---------------------
5043 -- Is_Static_Range --
5044 ---------------------
5046 -- A static range is a range whose bounds are static expressions, or a
5047 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5048 -- We have already converted range attribute references, so we get the
5049 -- "or" part of this rule without needing a special test.
5051 function Is_Static_Range (N : Node_Id) return Boolean is
5052 begin
5053 return Is_Static_Expression (Low_Bound (N))
5054 and then
5055 Is_Static_Expression (High_Bound (N));
5056 end Is_Static_Range;
5058 -----------------------
5059 -- Is_Static_Subtype --
5060 -----------------------
5062 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5064 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
5065 Base_T : constant Entity_Id := Base_Type (Typ);
5066 Anc_Subt : Entity_Id;
5068 begin
5069 -- First a quick check on the non static subtype flag. As described
5070 -- in further detail in Einfo, this flag is not decisive in all cases,
5071 -- but if it is set, then the subtype is definitely non-static.
5073 if Is_Non_Static_Subtype (Typ) then
5074 return False;
5075 end if;
5077 Anc_Subt := Ancestor_Subtype (Typ);
5079 if Anc_Subt = Empty then
5080 Anc_Subt := Base_T;
5081 end if;
5083 if Is_Generic_Type (Root_Type (Base_T))
5084 or else Is_Generic_Actual_Type (Base_T)
5085 then
5086 return False;
5088 -- If there is a dynamic predicate for the type (declared or inherited)
5089 -- the expression is not static.
5091 elsif Has_Dynamic_Predicate_Aspect (Typ)
5092 or else (Is_Derived_Type (Typ)
5093 and then Has_Aspect (Typ, Aspect_Dynamic_Predicate))
5094 then
5095 return False;
5097 -- String types
5099 elsif Is_String_Type (Typ) then
5100 return
5101 Ekind (Typ) = E_String_Literal_Subtype
5102 or else (Is_Static_Subtype (Component_Type (Typ))
5103 and then Is_Static_Subtype (Etype (First_Index (Typ))));
5105 -- Scalar types
5107 elsif Is_Scalar_Type (Typ) then
5108 if Base_T = Typ then
5109 return True;
5111 else
5112 return Is_Static_Subtype (Anc_Subt)
5113 and then Is_Static_Expression (Type_Low_Bound (Typ))
5114 and then Is_Static_Expression (Type_High_Bound (Typ));
5115 end if;
5117 -- Types other than string and scalar types are never static
5119 else
5120 return False;
5121 end if;
5122 end Is_Static_Subtype;
5124 -------------------------------
5125 -- Is_Statically_Unevaluated --
5126 -------------------------------
5128 function Is_Statically_Unevaluated (Expr : Node_Id) return Boolean is
5129 function Check_Case_Expr_Alternative
5130 (CEA : Node_Id) return Match_Result;
5131 -- We have a message emanating from the Expression of a case expression
5132 -- alternative. We examine this alternative, as follows:
5134 -- If the selecting expression of the parent case is non-static, or
5135 -- if any of the discrete choices of the given case alternative are
5136 -- non-static or raise Constraint_Error, return Non_Static.
5138 -- Otherwise check if the selecting expression matches any of the given
5139 -- discrete choices. If so, the alternative is executed and we return
5140 -- Match, otherwise, the alternative can never be executed, and so we
5141 -- return No_Match.
5143 ---------------------------------
5144 -- Check_Case_Expr_Alternative --
5145 ---------------------------------
5147 function Check_Case_Expr_Alternative
5148 (CEA : Node_Id) return Match_Result
5150 Case_Exp : constant Node_Id := Parent (CEA);
5151 Choice : Node_Id;
5152 Prev_CEA : Node_Id;
5154 begin
5155 pragma Assert (Nkind (Case_Exp) = N_Case_Expression);
5157 -- Check that selecting expression is static
5159 if not Is_OK_Static_Expression (Expression (Case_Exp)) then
5160 return Non_Static;
5161 end if;
5163 if not Is_OK_Static_Choice_List (Discrete_Choices (CEA)) then
5164 return Non_Static;
5165 end if;
5167 -- All choices are now known to be static. Now see if alternative
5168 -- matches one of the choices.
5170 Choice := First (Discrete_Choices (CEA));
5171 while Present (Choice) loop
5173 -- Check various possibilities for choice, returning Match if we
5174 -- find the selecting value matches any of the choices. Note that
5175 -- we know we are the last choice, so we don't have to keep going.
5177 if Nkind (Choice) = N_Others_Choice then
5179 -- Others choice is a bit annoying, it matches if none of the
5180 -- previous alternatives matches (note that we know we are the
5181 -- last alternative in this case, so we can just go backwards
5182 -- from us to see if any previous one matches).
5184 Prev_CEA := Prev (CEA);
5185 while Present (Prev_CEA) loop
5186 if Check_Case_Expr_Alternative (Prev_CEA) = Match then
5187 return No_Match;
5188 end if;
5190 Prev (Prev_CEA);
5191 end loop;
5193 return Match;
5195 -- Else we have a normal static choice
5197 elsif Choice_Matches (Expression (Case_Exp), Choice) = Match then
5198 return Match;
5199 end if;
5201 -- If we fall through, it means that the discrete choice did not
5202 -- match the selecting expression, so continue.
5204 Next (Choice);
5205 end loop;
5207 -- If we get through that loop then all choices were static, and none
5208 -- of them matched the selecting expression. So return No_Match.
5210 return No_Match;
5211 end Check_Case_Expr_Alternative;
5213 -- Local variables
5215 P : Node_Id;
5216 OldP : Node_Id;
5217 Choice : Node_Id;
5219 -- Start of processing for Is_Statically_Unevaluated
5221 begin
5222 -- The (32.x) references here are from RM section 4.9
5224 -- (32.1) An expression is statically unevaluated if it is part of ...
5226 -- This means we have to climb the tree looking for one of the cases
5228 P := Expr;
5229 loop
5230 OldP := P;
5231 P := Parent (P);
5233 -- (32.2) The right operand of a static short-circuit control form
5234 -- whose value is determined by its left operand.
5236 -- AND THEN with False as left operand
5238 if Nkind (P) = N_And_Then
5239 and then Compile_Time_Known_Value (Left_Opnd (P))
5240 and then Is_False (Expr_Value (Left_Opnd (P)))
5241 then
5242 return True;
5244 -- OR ELSE with True as left operand
5246 elsif Nkind (P) = N_Or_Else
5247 and then Compile_Time_Known_Value (Left_Opnd (P))
5248 and then Is_True (Expr_Value (Left_Opnd (P)))
5249 then
5250 return True;
5252 -- (32.3) A dependent_expression of an if_expression whose associated
5253 -- condition is static and equals False.
5255 elsif Nkind (P) = N_If_Expression then
5256 declare
5257 Cond : constant Node_Id := First (Expressions (P));
5258 Texp : constant Node_Id := Next (Cond);
5259 Fexp : constant Node_Id := Next (Texp);
5261 begin
5262 if Compile_Time_Known_Value (Cond) then
5264 -- Condition is True and we are in the right operand
5266 if Is_True (Expr_Value (Cond)) and then OldP = Fexp then
5267 return True;
5269 -- Condition is False and we are in the left operand
5271 elsif Is_False (Expr_Value (Cond)) and then OldP = Texp then
5272 return True;
5273 end if;
5274 end if;
5275 end;
5277 -- (32.4) A condition or dependent_expression of an if_expression
5278 -- where the condition corresponding to at least one preceding
5279 -- dependent_expression of the if_expression is static and equals
5280 -- True.
5282 -- This refers to cases like
5284 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5286 -- But we expand elsif's out anyway, so the above looks like:
5288 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5290 -- So for us this is caught by the above check for the 32.3 case.
5292 -- (32.5) A dependent_expression of a case_expression whose
5293 -- selecting_expression is static and whose value is not covered
5294 -- by the corresponding discrete_choice_list.
5296 elsif Nkind (P) = N_Case_Expression_Alternative then
5298 -- First, we have to be in the expression to suppress messages.
5299 -- If we are within one of the choices, we want the message.
5301 if OldP = Expression (P) then
5303 -- Statically unevaluated if alternative does not match
5305 if Check_Case_Expr_Alternative (P) = No_Match then
5306 return True;
5307 end if;
5308 end if;
5310 -- (32.6) A choice_expression (or a simple_expression of a range
5311 -- that occurs as a membership_choice of a membership_choice_list)
5312 -- of a static membership test that is preceded in the enclosing
5313 -- membership_choice_list by another item whose individual
5314 -- membership test (see (RM 4.5.2)) statically yields True.
5316 elsif Nkind (P) in N_Membership_Test then
5318 -- Only possibly unevaluated if simple expression is static
5320 if not Is_OK_Static_Expression (Left_Opnd (P)) then
5321 null;
5323 -- All members of the choice list must be static
5325 elsif (Present (Right_Opnd (P))
5326 and then not Is_OK_Static_Choice (Right_Opnd (P)))
5327 or else (Present (Alternatives (P))
5328 and then
5329 not Is_OK_Static_Choice_List (Alternatives (P)))
5330 then
5331 null;
5333 -- If expression is the one and only alternative, then it is
5334 -- definitely not statically unevaluated, so we only have to
5335 -- test the case where there are alternatives present.
5337 elsif Present (Alternatives (P)) then
5339 -- Look for previous matching Choice
5341 Choice := First (Alternatives (P));
5342 while Present (Choice) loop
5344 -- If we reached us and no previous choices matched, this
5345 -- is not the case where we are statically unevaluated.
5347 exit when OldP = Choice;
5349 -- If a previous choice matches, then that is the case where
5350 -- we know our choice is statically unevaluated.
5352 if Choice_Matches (Left_Opnd (P), Choice) = Match then
5353 return True;
5354 end if;
5356 Next (Choice);
5357 end loop;
5359 -- If we fall through the loop, we were not one of the choices,
5360 -- we must have been the expression, so that is not covered by
5361 -- this rule, and we keep going.
5363 null;
5364 end if;
5365 end if;
5367 -- OK, not statically unevaluated at this level, see if we should
5368 -- keep climbing to look for a higher level reason.
5370 -- Special case for component association in aggregates, where
5371 -- we want to keep climbing up to the parent aggregate.
5373 if Nkind (P) = N_Component_Association
5374 and then Nkind (Parent (P)) = N_Aggregate
5375 then
5376 null;
5378 -- All done if not still within subexpression
5380 else
5381 exit when Nkind (P) not in N_Subexpr;
5382 end if;
5383 end loop;
5385 -- If we fall through the loop, not one of the cases covered!
5387 return False;
5388 end Is_Statically_Unevaluated;
5390 --------------------
5391 -- Not_Null_Range --
5392 --------------------
5394 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
5395 begin
5396 if Compile_Time_Known_Value (Lo)
5397 and then Compile_Time_Known_Value (Hi)
5398 then
5399 declare
5400 Typ : Entity_Id := Etype (Lo);
5401 begin
5402 -- When called from the frontend, as part of the analysis of
5403 -- potentially static expressions, Typ will be the full view of a
5404 -- type with all the info needed to answer this query. When called
5405 -- from the backend, for example to know whether a range of a loop
5406 -- is null, Typ might be a private type and we need to explicitly
5407 -- switch to its corresponding full view to access the same info.
5409 if Is_Incomplete_Or_Private_Type (Typ)
5410 and then Present (Full_View (Typ))
5411 then
5412 Typ := Full_View (Typ);
5413 end if;
5415 if Is_Discrete_Type (Typ) then
5416 return Expr_Value (Lo) <= Expr_Value (Hi);
5417 else pragma Assert (Is_Real_Type (Typ));
5418 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
5419 end if;
5420 end;
5421 else
5422 return False;
5423 end if;
5425 end Not_Null_Range;
5427 -------------
5428 -- OK_Bits --
5429 -------------
5431 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
5432 begin
5433 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5435 if Bits < 500_000 then
5436 return True;
5438 -- Error if this maximum is exceeded
5440 else
5441 Error_Msg_N ("static value too large, capacity exceeded", N);
5442 return False;
5443 end if;
5444 end OK_Bits;
5446 ------------------
5447 -- Out_Of_Range --
5448 ------------------
5450 procedure Out_Of_Range (N : Node_Id) is
5451 begin
5452 -- If we have the static expression case, then this is an illegality
5453 -- in Ada 95 mode, except that in an instance, we never generate an
5454 -- error (if the error is legitimate, it was already diagnosed in the
5455 -- template).
5457 if Is_Static_Expression (N)
5458 and then not In_Instance
5459 and then not In_Inlined_Body
5460 and then Ada_Version >= Ada_95
5461 then
5462 -- No message if we are statically unevaluated
5464 if Is_Statically_Unevaluated (N) then
5465 null;
5467 -- The expression to compute the length of a packed array is attached
5468 -- to the array type itself, and deserves a separate message.
5470 elsif Nkind (Parent (N)) = N_Defining_Identifier
5471 and then Is_Array_Type (Parent (N))
5472 and then Present (Packed_Array_Impl_Type (Parent (N)))
5473 and then Present (First_Rep_Item (Parent (N)))
5474 then
5475 Error_Msg_N
5476 ("length of packed array must not exceed Integer''Last",
5477 First_Rep_Item (Parent (N)));
5478 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
5480 -- All cases except the special array case.
5481 -- No message if we are dealing with System.Priority values in
5482 -- CodePeer mode where the target runtime may have more priorities.
5484 elsif not CodePeer_Mode or else Etype (N) /= RTE (RE_Priority) then
5485 Apply_Compile_Time_Constraint_Error
5486 (N, "value not in range of}", CE_Range_Check_Failed);
5487 end if;
5489 -- Here we generate a warning for the Ada 83 case, or when we are in an
5490 -- instance, or when we have a non-static expression case.
5492 else
5493 Apply_Compile_Time_Constraint_Error
5494 (N, "value not in range of}??", CE_Range_Check_Failed);
5495 end if;
5496 end Out_Of_Range;
5498 ----------------------
5499 -- Predicates_Match --
5500 ----------------------
5502 function Predicates_Match (T1, T2 : Entity_Id) return Boolean is
5503 Pred1 : Node_Id;
5504 Pred2 : Node_Id;
5506 begin
5507 if Ada_Version < Ada_2012 then
5508 return True;
5510 -- Both types must have predicates or lack them
5512 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
5513 return False;
5515 -- Check matching predicates
5517 else
5518 Pred1 :=
5519 Get_Rep_Item
5520 (T1, Name_Static_Predicate, Check_Parents => False);
5521 Pred2 :=
5522 Get_Rep_Item
5523 (T2, Name_Static_Predicate, Check_Parents => False);
5525 -- Subtypes statically match if the predicate comes from the
5526 -- same declaration, which can only happen if one is a subtype
5527 -- of the other and has no explicit predicate.
5529 -- Suppress warnings on order of actuals, which is otherwise
5530 -- triggered by one of the two calls below.
5532 pragma Warnings (Off);
5533 return Pred1 = Pred2
5534 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
5535 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
5536 pragma Warnings (On);
5537 end if;
5538 end Predicates_Match;
5540 ---------------------------------------------
5541 -- Real_Or_String_Static_Predicate_Matches --
5542 ---------------------------------------------
5544 function Real_Or_String_Static_Predicate_Matches
5545 (Val : Node_Id;
5546 Typ : Entity_Id) return Boolean
5548 Expr : constant Node_Id := Static_Real_Or_String_Predicate (Typ);
5549 -- The predicate expression from the type
5551 Pfun : constant Entity_Id := Predicate_Function (Typ);
5552 -- The entity for the predicate function
5554 Ent_Name : constant Name_Id := Chars (First_Formal (Pfun));
5555 -- The name of the formal of the predicate function. Occurrences of the
5556 -- type name in Expr have been rewritten as references to this formal,
5557 -- and it has a unique name, so we can identify references by this name.
5559 Copy : Node_Id;
5560 -- Copy of the predicate function tree
5562 function Process (N : Node_Id) return Traverse_Result;
5563 -- Function used to process nodes during the traversal in which we will
5564 -- find occurrences of the entity name, and replace such occurrences
5565 -- by a real literal with the value to be tested.
5567 procedure Traverse is new Traverse_Proc (Process);
5568 -- The actual traversal procedure
5570 -------------
5571 -- Process --
5572 -------------
5574 function Process (N : Node_Id) return Traverse_Result is
5575 begin
5576 if Nkind (N) = N_Identifier and then Chars (N) = Ent_Name then
5577 declare
5578 Nod : constant Node_Id := New_Copy (Val);
5579 begin
5580 Set_Sloc (Nod, Sloc (N));
5581 Rewrite (N, Nod);
5582 return Skip;
5583 end;
5585 -- The predicate function may contain string-comparison operations
5586 -- that have been converted into calls to run-time array-comparison
5587 -- routines. To evaluate the predicate statically, we recover the
5588 -- original comparison operation and replace the occurrence of the
5589 -- formal by the static string value. The actuals of the generated
5590 -- call are of the form X'Address.
5592 elsif Nkind (N) in N_Op_Compare
5593 and then Nkind (Left_Opnd (N)) = N_Function_Call
5594 then
5595 declare
5596 C : constant Node_Id := Left_Opnd (N);
5597 F : constant Node_Id := First (Parameter_Associations (C));
5598 L : constant Node_Id := Prefix (F);
5599 R : constant Node_Id := Prefix (Next (F));
5601 begin
5602 -- If an operand is an entity name, it is the formal of the
5603 -- predicate function, so replace it with the string value.
5604 -- It may be either operand in the call. The other operand
5605 -- is a static string from the original predicate.
5607 if Is_Entity_Name (L) then
5608 Rewrite (Left_Opnd (N), New_Copy (Val));
5609 Rewrite (Right_Opnd (N), New_Copy (R));
5611 else
5612 Rewrite (Left_Opnd (N), New_Copy (L));
5613 Rewrite (Right_Opnd (N), New_Copy (Val));
5614 end if;
5616 return Skip;
5617 end;
5619 else
5620 return OK;
5621 end if;
5622 end Process;
5624 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5626 begin
5627 -- First deal with special case of inherited predicate, where the
5628 -- predicate expression looks like:
5630 -- xxPredicate (typ (Ent)) and then Expr
5632 -- where Expr is the predicate expression for this level, and the
5633 -- left operand is the call to evaluate the inherited predicate.
5635 if Nkind (Expr) = N_And_Then
5636 and then Nkind (Left_Opnd (Expr)) = N_Function_Call
5637 and then Is_Predicate_Function (Entity (Name (Left_Opnd (Expr))))
5638 then
5639 -- OK we have the inherited case, so make a call to evaluate the
5640 -- inherited predicate. If that fails, so do we!
5642 if not
5643 Real_Or_String_Static_Predicate_Matches
5644 (Val => Val,
5645 Typ => Etype (First_Formal (Entity (Name (Left_Opnd (Expr))))))
5646 then
5647 return False;
5648 end if;
5650 -- Use the right operand for the continued processing
5652 Copy := Copy_Separate_Tree (Right_Opnd (Expr));
5654 -- Case where call to predicate function appears on its own (this means
5655 -- that the predicate at this level is just inherited from the parent).
5657 elsif Nkind (Expr) = N_Function_Call then
5658 declare
5659 Typ : constant Entity_Id :=
5660 Etype (First_Formal (Entity (Name (Expr))));
5662 begin
5663 -- If the inherited predicate is dynamic, just ignore it. We can't
5664 -- go trying to evaluate a dynamic predicate as a static one!
5666 if Has_Dynamic_Predicate_Aspect (Typ) then
5667 return True;
5669 -- Otherwise inherited predicate is static, check for match
5671 else
5672 return Real_Or_String_Static_Predicate_Matches (Val, Typ);
5673 end if;
5674 end;
5676 -- If not just an inherited predicate, copy whole expression
5678 else
5679 Copy := Copy_Separate_Tree (Expr);
5680 end if;
5682 -- Now we replace occurrences of the entity by the value
5684 Traverse (Copy);
5686 -- And analyze the resulting static expression to see if it is True
5688 Analyze_And_Resolve (Copy, Standard_Boolean);
5689 return Is_True (Expr_Value (Copy));
5690 end Real_Or_String_Static_Predicate_Matches;
5692 -------------------------
5693 -- Rewrite_In_Raise_CE --
5694 -------------------------
5696 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
5697 Stat : constant Boolean := Is_Static_Expression (N);
5698 Typ : constant Entity_Id := Etype (N);
5700 begin
5701 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5702 -- can just clear the condition if the reason is appropriate. We do
5703 -- not do this operation if the parent has a reason other than range
5704 -- check failed, because otherwise we would change the reason.
5706 if Present (Parent (N))
5707 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
5708 and then Reason (Parent (N)) =
5709 UI_From_Int (RT_Exception_Code'Pos (CE_Range_Check_Failed))
5710 then
5711 Set_Condition (Parent (N), Empty);
5713 -- Else build an explicit N_Raise_CE
5715 else
5716 if Nkind (Exp) = N_Raise_Constraint_Error then
5717 Rewrite (N,
5718 Make_Raise_Constraint_Error (Sloc (Exp),
5719 Reason => Reason (Exp)));
5720 else
5721 Rewrite (N,
5722 Make_Raise_Constraint_Error (Sloc (Exp),
5723 Reason => CE_Range_Check_Failed));
5724 end if;
5726 Set_Raises_Constraint_Error (N);
5727 Set_Etype (N, Typ);
5728 end if;
5730 -- Set proper flags in result
5732 Set_Raises_Constraint_Error (N, True);
5733 Set_Is_Static_Expression (N, Stat);
5734 end Rewrite_In_Raise_CE;
5736 ---------------------
5737 -- String_Type_Len --
5738 ---------------------
5740 function String_Type_Len (Stype : Entity_Id) return Uint is
5741 NT : constant Entity_Id := Etype (First_Index (Stype));
5742 T : Entity_Id;
5744 begin
5745 if Is_OK_Static_Subtype (NT) then
5746 T := NT;
5747 else
5748 T := Base_Type (NT);
5749 end if;
5751 return Expr_Value (Type_High_Bound (T)) -
5752 Expr_Value (Type_Low_Bound (T)) + 1;
5753 end String_Type_Len;
5755 ------------------------------------
5756 -- Subtypes_Statically_Compatible --
5757 ------------------------------------
5759 function Subtypes_Statically_Compatible
5760 (T1 : Entity_Id;
5761 T2 : Entity_Id;
5762 Formal_Derived_Matching : Boolean := False) return Boolean
5764 begin
5765 -- Scalar types
5767 if Is_Scalar_Type (T1) then
5769 -- Definitely compatible if we match
5771 if Subtypes_Statically_Match (T1, T2) then
5772 return True;
5774 -- If either subtype is nonstatic then they're not compatible
5776 elsif not Is_OK_Static_Subtype (T1)
5777 or else
5778 not Is_OK_Static_Subtype (T2)
5779 then
5780 return False;
5782 -- Base types must match, but we don't check that (should we???) but
5783 -- we do at least check that both types are real, or both types are
5784 -- not real.
5786 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
5787 return False;
5789 -- Here we check the bounds
5791 else
5792 declare
5793 LB1 : constant Node_Id := Type_Low_Bound (T1);
5794 HB1 : constant Node_Id := Type_High_Bound (T1);
5795 LB2 : constant Node_Id := Type_Low_Bound (T2);
5796 HB2 : constant Node_Id := Type_High_Bound (T2);
5798 begin
5799 if Is_Real_Type (T1) then
5800 return
5801 Expr_Value_R (LB1) > Expr_Value_R (HB1)
5802 or else
5803 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
5804 and then Expr_Value_R (HB1) <= Expr_Value_R (HB2));
5806 else
5807 return
5808 Expr_Value (LB1) > Expr_Value (HB1)
5809 or else
5810 (Expr_Value (LB2) <= Expr_Value (LB1)
5811 and then Expr_Value (HB1) <= Expr_Value (HB2));
5812 end if;
5813 end;
5814 end if;
5816 -- Access types
5818 elsif Is_Access_Type (T1) then
5819 return
5820 (not Is_Constrained (T2)
5821 or else Subtypes_Statically_Match
5822 (Designated_Type (T1), Designated_Type (T2)))
5823 and then not (Can_Never_Be_Null (T2)
5824 and then not Can_Never_Be_Null (T1));
5826 -- All other cases
5828 else
5829 return
5830 (Is_Composite_Type (T1) and then not Is_Constrained (T2))
5831 or else Subtypes_Statically_Match
5832 (T1, T2, Formal_Derived_Matching);
5833 end if;
5834 end Subtypes_Statically_Compatible;
5836 -------------------------------
5837 -- Subtypes_Statically_Match --
5838 -------------------------------
5840 -- Subtypes statically match if they have statically matching constraints
5841 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5842 -- they are the same identical constraint, or if they are static and the
5843 -- values match (RM 4.9.1(1)).
5845 -- In addition, in GNAT, the object size (Esize) values of the types must
5846 -- match if they are set (unless checking an actual for a formal derived
5847 -- type). The use of 'Object_Size can cause this to be false even if the
5848 -- types would otherwise match in the RM sense.
5850 function Subtypes_Statically_Match
5851 (T1 : Entity_Id;
5852 T2 : Entity_Id;
5853 Formal_Derived_Matching : Boolean := False) return Boolean
5855 begin
5856 -- A type always statically matches itself
5858 if T1 = T2 then
5859 return True;
5861 -- No match if sizes different (from use of 'Object_Size). This test
5862 -- is excluded if Formal_Derived_Matching is True, as the base types
5863 -- can be different in that case and typically have different sizes.
5864 -- ??? Frontend_Layout_On_Target used to set Esizes but this is no
5865 -- longer the case, consider removing the last test below.
5867 elsif not Formal_Derived_Matching
5868 and then Known_Static_Esize (T1)
5869 and then Known_Static_Esize (T2)
5870 and then Esize (T1) /= Esize (T2)
5871 then
5872 return False;
5874 -- No match if predicates do not match
5876 elsif not Predicates_Match (T1, T2) then
5877 return False;
5879 -- Scalar types
5881 elsif Is_Scalar_Type (T1) then
5883 -- Base types must be the same
5885 if Base_Type (T1) /= Base_Type (T2) then
5886 return False;
5887 end if;
5889 -- A constrained numeric subtype never matches an unconstrained
5890 -- subtype, i.e. both types must be constrained or unconstrained.
5892 -- To understand the requirement for this test, see RM 4.9.1(1).
5893 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5894 -- a constrained subtype with constraint bounds matching the bounds
5895 -- of its corresponding unconstrained base type. In this situation,
5896 -- Integer and Integer'Base do not statically match, even though
5897 -- they have the same bounds.
5899 -- We only apply this test to types in Standard and types that appear
5900 -- in user programs. That way, we do not have to be too careful about
5901 -- setting Is_Constrained right for Itypes.
5903 if Is_Numeric_Type (T1)
5904 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5905 and then (Scope (T1) = Standard_Standard
5906 or else Comes_From_Source (T1))
5907 and then (Scope (T2) = Standard_Standard
5908 or else Comes_From_Source (T2))
5909 then
5910 return False;
5912 -- A generic scalar type does not statically match its base type
5913 -- (AI-311). In this case we make sure that the formals, which are
5914 -- first subtypes of their bases, are constrained.
5916 elsif Is_Generic_Type (T1)
5917 and then Is_Generic_Type (T2)
5918 and then (Is_Constrained (T1) /= Is_Constrained (T2))
5919 then
5920 return False;
5921 end if;
5923 -- If there was an error in either range, then just assume the types
5924 -- statically match to avoid further junk errors.
5926 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
5927 or else Error_Posted (Scalar_Range (T1))
5928 or else Error_Posted (Scalar_Range (T2))
5929 then
5930 return True;
5931 end if;
5933 -- Otherwise both types have bounds that can be compared
5935 declare
5936 LB1 : constant Node_Id := Type_Low_Bound (T1);
5937 HB1 : constant Node_Id := Type_High_Bound (T1);
5938 LB2 : constant Node_Id := Type_Low_Bound (T2);
5939 HB2 : constant Node_Id := Type_High_Bound (T2);
5941 begin
5942 -- If the bounds are the same tree node, then match (common case)
5944 if LB1 = LB2 and then HB1 = HB2 then
5945 return True;
5947 -- Otherwise bounds must be static and identical value
5949 else
5950 if not Is_OK_Static_Subtype (T1)
5951 or else
5952 not Is_OK_Static_Subtype (T2)
5953 then
5954 return False;
5956 elsif Is_Real_Type (T1) then
5957 return
5958 Expr_Value_R (LB1) = Expr_Value_R (LB2)
5959 and then
5960 Expr_Value_R (HB1) = Expr_Value_R (HB2);
5962 else
5963 return
5964 Expr_Value (LB1) = Expr_Value (LB2)
5965 and then
5966 Expr_Value (HB1) = Expr_Value (HB2);
5967 end if;
5968 end if;
5969 end;
5971 -- Type with discriminants
5973 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
5975 -- Because of view exchanges in multiple instantiations, conformance
5976 -- checking might try to match a partial view of a type with no
5977 -- discriminants with a full view that has defaulted discriminants.
5978 -- In such a case, use the discriminant constraint of the full view,
5979 -- which must exist because we know that the two subtypes have the
5980 -- same base type.
5982 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
5983 -- A generic actual type is declared through a subtype declaration
5984 -- and may have an inconsistent indication of the presence of
5985 -- discriminants, so check the type it renames.
5987 if Is_Generic_Actual_Type (T1)
5988 and then not Has_Discriminants (Etype (T1))
5989 and then not Has_Discriminants (T2)
5990 then
5991 return True;
5993 elsif In_Instance then
5994 if Is_Private_Type (T2)
5995 and then Present (Full_View (T2))
5996 and then Has_Discriminants (Full_View (T2))
5997 then
5998 return Subtypes_Statically_Match (T1, Full_View (T2));
6000 elsif Is_Private_Type (T1)
6001 and then Present (Full_View (T1))
6002 and then Has_Discriminants (Full_View (T1))
6003 then
6004 return Subtypes_Statically_Match (Full_View (T1), T2);
6006 else
6007 return False;
6008 end if;
6009 else
6010 return False;
6011 end if;
6012 end if;
6014 declare
6015 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
6016 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
6018 DA1 : Elmt_Id;
6019 DA2 : Elmt_Id;
6021 begin
6022 if DL1 = DL2 then
6023 return True;
6024 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
6025 return False;
6026 end if;
6028 -- Now loop through the discriminant constraints
6030 -- Note: the guard here seems necessary, since it is possible at
6031 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
6033 if Present (DL1) and then Present (DL2) then
6034 DA1 := First_Elmt (DL1);
6035 DA2 := First_Elmt (DL2);
6036 while Present (DA1) loop
6037 declare
6038 Expr1 : constant Node_Id := Node (DA1);
6039 Expr2 : constant Node_Id := Node (DA2);
6041 begin
6042 if not Is_OK_Static_Expression (Expr1)
6043 or else not Is_OK_Static_Expression (Expr2)
6044 then
6045 return False;
6047 -- If either expression raised a constraint error,
6048 -- consider the expressions as matching, since this
6049 -- helps to prevent cascading errors.
6051 elsif Raises_Constraint_Error (Expr1)
6052 or else Raises_Constraint_Error (Expr2)
6053 then
6054 null;
6056 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
6057 return False;
6058 end if;
6059 end;
6061 Next_Elmt (DA1);
6062 Next_Elmt (DA2);
6063 end loop;
6064 end if;
6065 end;
6067 return True;
6069 -- A definite type does not match an indefinite or classwide type.
6070 -- However, a generic type with unknown discriminants may be
6071 -- instantiated with a type with no discriminants, and conformance
6072 -- checking on an inherited operation may compare the actual with the
6073 -- subtype that renames it in the instance.
6075 elsif Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
6076 then
6077 return
6078 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
6080 -- Array type
6082 elsif Is_Array_Type (T1) then
6084 -- If either subtype is unconstrained then both must be, and if both
6085 -- are unconstrained then no further checking is needed.
6087 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
6088 return not (Is_Constrained (T1) or else Is_Constrained (T2));
6089 end if;
6091 -- Both subtypes are constrained, so check that the index subtypes
6092 -- statically match.
6094 declare
6095 Index1 : Node_Id := First_Index (T1);
6096 Index2 : Node_Id := First_Index (T2);
6098 begin
6099 while Present (Index1) loop
6100 if not
6101 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
6102 then
6103 return False;
6104 end if;
6106 Next_Index (Index1);
6107 Next_Index (Index2);
6108 end loop;
6110 return True;
6111 end;
6113 elsif Is_Access_Type (T1) then
6114 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
6115 return False;
6117 elsif Ekind_In (T1, E_Access_Subprogram_Type,
6118 E_Anonymous_Access_Subprogram_Type)
6119 then
6120 return
6121 Subtype_Conformant
6122 (Designated_Type (T1),
6123 Designated_Type (T2));
6124 else
6125 return
6126 Subtypes_Statically_Match
6127 (Designated_Type (T1),
6128 Designated_Type (T2))
6129 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
6130 end if;
6132 -- All other types definitely match
6134 else
6135 return True;
6136 end if;
6137 end Subtypes_Statically_Match;
6139 ----------
6140 -- Test --
6141 ----------
6143 function Test (Cond : Boolean) return Uint is
6144 begin
6145 if Cond then
6146 return Uint_1;
6147 else
6148 return Uint_0;
6149 end if;
6150 end Test;
6152 ---------------------
6153 -- Test_Comparison --
6154 ---------------------
6156 procedure Test_Comparison
6157 (Op : Node_Id;
6158 Assume_Valid : Boolean;
6159 True_Result : out Boolean;
6160 False_Result : out Boolean)
6162 Left : constant Node_Id := Left_Opnd (Op);
6163 Left_Typ : constant Entity_Id := Etype (Left);
6164 Orig_Op : constant Node_Id := Original_Node (Op);
6166 procedure Replacement_Warning (Msg : String);
6167 -- Emit a warning on a comparison that can be replaced by '='
6169 -------------------------
6170 -- Replacement_Warning --
6171 -------------------------
6173 procedure Replacement_Warning (Msg : String) is
6174 begin
6175 if Constant_Condition_Warnings
6176 and then Comes_From_Source (Orig_Op)
6177 and then Is_Integer_Type (Left_Typ)
6178 and then not Error_Posted (Op)
6179 and then not Has_Warnings_Off (Left_Typ)
6180 and then not In_Instance
6181 then
6182 Error_Msg_N (Msg, Op);
6183 end if;
6184 end Replacement_Warning;
6186 -- Local variables
6188 Res : constant Compare_Result :=
6189 Compile_Time_Compare (Left, Right_Opnd (Op), Assume_Valid);
6191 -- Start of processing for Test_Comparison
6193 begin
6194 case N_Op_Compare (Nkind (Op)) is
6195 when N_Op_Eq =>
6196 True_Result := Res = EQ;
6197 False_Result := Res = LT or else Res = GT or else Res = NE;
6199 when N_Op_Ge =>
6200 True_Result := Res in Compare_GE;
6201 False_Result := Res = LT;
6203 if Res = LE and then Nkind (Orig_Op) = N_Op_Ge then
6204 Replacement_Warning
6205 ("can never be greater than, could replace by ""'=""?c?");
6206 end if;
6208 when N_Op_Gt =>
6209 True_Result := Res = GT;
6210 False_Result := Res in Compare_LE;
6212 when N_Op_Le =>
6213 True_Result := Res in Compare_LE;
6214 False_Result := Res = GT;
6216 if Res = GE and then Nkind (Orig_Op) = N_Op_Le then
6217 Replacement_Warning
6218 ("can never be less than, could replace by ""'=""?c?");
6219 end if;
6221 when N_Op_Lt =>
6222 True_Result := Res = LT;
6223 False_Result := Res in Compare_GE;
6225 when N_Op_Ne =>
6226 True_Result := Res = NE or else Res = GT or else Res = LT;
6227 False_Result := Res = EQ;
6228 end case;
6229 end Test_Comparison;
6231 ---------------------------------
6232 -- Test_Expression_Is_Foldable --
6233 ---------------------------------
6235 -- One operand case
6237 procedure Test_Expression_Is_Foldable
6238 (N : Node_Id;
6239 Op1 : Node_Id;
6240 Stat : out Boolean;
6241 Fold : out Boolean)
6243 begin
6244 Stat := False;
6245 Fold := False;
6247 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6248 return;
6249 end if;
6251 -- If operand is Any_Type, just propagate to result and do not
6252 -- try to fold, this prevents cascaded errors.
6254 if Etype (Op1) = Any_Type then
6255 Set_Etype (N, Any_Type);
6256 return;
6258 -- If operand raises constraint error, then replace node N with the
6259 -- raise constraint error node, and we are obviously not foldable.
6260 -- Note that this replacement inherits the Is_Static_Expression flag
6261 -- from the operand.
6263 elsif Raises_Constraint_Error (Op1) then
6264 Rewrite_In_Raise_CE (N, Op1);
6265 return;
6267 -- If the operand is not static, then the result is not static, and
6268 -- all we have to do is to check the operand since it is now known
6269 -- to appear in a non-static context.
6271 elsif not Is_Static_Expression (Op1) then
6272 Check_Non_Static_Context (Op1);
6273 Fold := Compile_Time_Known_Value (Op1);
6274 return;
6276 -- An expression of a formal modular type is not foldable because
6277 -- the modulus is unknown.
6279 elsif Is_Modular_Integer_Type (Etype (Op1))
6280 and then Is_Generic_Type (Etype (Op1))
6281 then
6282 Check_Non_Static_Context (Op1);
6283 return;
6285 -- Here we have the case of an operand whose type is OK, which is
6286 -- static, and which does not raise constraint error, we can fold.
6288 else
6289 Set_Is_Static_Expression (N);
6290 Fold := True;
6291 Stat := True;
6292 end if;
6293 end Test_Expression_Is_Foldable;
6295 -- Two operand case
6297 procedure Test_Expression_Is_Foldable
6298 (N : Node_Id;
6299 Op1 : Node_Id;
6300 Op2 : Node_Id;
6301 Stat : out Boolean;
6302 Fold : out Boolean;
6303 CRT_Safe : Boolean := False)
6305 Rstat : constant Boolean := Is_Static_Expression (Op1)
6306 and then
6307 Is_Static_Expression (Op2);
6309 begin
6310 Stat := False;
6311 Fold := False;
6313 -- Inhibit folding if -gnatd.f flag set
6315 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
6316 return;
6317 end if;
6319 -- If either operand is Any_Type, just propagate to result and
6320 -- do not try to fold, this prevents cascaded errors.
6322 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
6323 Set_Etype (N, Any_Type);
6324 return;
6326 -- If left operand raises constraint error, then replace node N with the
6327 -- Raise_Constraint_Error node, and we are obviously not foldable.
6328 -- Is_Static_Expression is set from the two operands in the normal way,
6329 -- and we check the right operand if it is in a non-static context.
6331 elsif Raises_Constraint_Error (Op1) then
6332 if not Rstat then
6333 Check_Non_Static_Context (Op2);
6334 end if;
6336 Rewrite_In_Raise_CE (N, Op1);
6337 Set_Is_Static_Expression (N, Rstat);
6338 return;
6340 -- Similar processing for the case of the right operand. Note that we
6341 -- don't use this routine for the short-circuit case, so we do not have
6342 -- to worry about that special case here.
6344 elsif Raises_Constraint_Error (Op2) then
6345 if not Rstat then
6346 Check_Non_Static_Context (Op1);
6347 end if;
6349 Rewrite_In_Raise_CE (N, Op2);
6350 Set_Is_Static_Expression (N, Rstat);
6351 return;
6353 -- Exclude expressions of a generic modular type, as above
6355 elsif Is_Modular_Integer_Type (Etype (Op1))
6356 and then Is_Generic_Type (Etype (Op1))
6357 then
6358 Check_Non_Static_Context (Op1);
6359 return;
6361 -- If result is not static, then check non-static contexts on operands
6362 -- since one of them may be static and the other one may not be static.
6364 elsif not Rstat then
6365 Check_Non_Static_Context (Op1);
6366 Check_Non_Static_Context (Op2);
6368 if CRT_Safe then
6369 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
6370 and then CRT_Safe_Compile_Time_Known_Value (Op2);
6371 else
6372 Fold := Compile_Time_Known_Value (Op1)
6373 and then Compile_Time_Known_Value (Op2);
6374 end if;
6376 return;
6378 -- Else result is static and foldable. Both operands are static, and
6379 -- neither raises constraint error, so we can definitely fold.
6381 else
6382 Set_Is_Static_Expression (N);
6383 Fold := True;
6384 Stat := True;
6385 return;
6386 end if;
6387 end Test_Expression_Is_Foldable;
6389 -------------------
6390 -- Test_In_Range --
6391 -------------------
6393 function Test_In_Range
6394 (N : Node_Id;
6395 Typ : Entity_Id;
6396 Assume_Valid : Boolean;
6397 Fixed_Int : Boolean;
6398 Int_Real : Boolean) return Range_Membership
6400 Val : Uint;
6401 Valr : Ureal;
6403 pragma Warnings (Off, Assume_Valid);
6404 -- For now Assume_Valid is unreferenced since the current implementation
6405 -- always returns Unknown if N is not a compile-time-known value, but we
6406 -- keep the parameter to allow for future enhancements in which we try
6407 -- to get the information in the variable case as well.
6409 begin
6410 -- If an error was posted on expression, then return Unknown, we do not
6411 -- want cascaded errors based on some false analysis of a junk node.
6413 if Error_Posted (N) then
6414 return Unknown;
6416 -- Expression that raises constraint error is an odd case. We certainly
6417 -- do not want to consider it to be in range. It might make sense to
6418 -- consider it always out of range, but this causes incorrect error
6419 -- messages about static expressions out of range. So we just return
6420 -- Unknown, which is always safe.
6422 elsif Raises_Constraint_Error (N) then
6423 return Unknown;
6425 -- Universal types have no range limits, so always in range
6427 elsif Typ = Universal_Integer or else Typ = Universal_Real then
6428 return In_Range;
6430 -- Never known if not scalar type. Don't know if this can actually
6431 -- happen, but our spec allows it, so we must check.
6433 elsif not Is_Scalar_Type (Typ) then
6434 return Unknown;
6436 -- Never known if this is a generic type, since the bounds of generic
6437 -- types are junk. Note that if we only checked for static expressions
6438 -- (instead of compile-time-known values) below, we would not need this
6439 -- check, because values of a generic type can never be static, but they
6440 -- can be known at compile time.
6442 elsif Is_Generic_Type (Typ) then
6443 return Unknown;
6445 -- Case of a known compile time value, where we can check if it is in
6446 -- the bounds of the given type.
6448 elsif Compile_Time_Known_Value (N) then
6449 declare
6450 Lo : Node_Id;
6451 Hi : Node_Id;
6453 LB_Known : Boolean;
6454 HB_Known : Boolean;
6456 begin
6457 Lo := Type_Low_Bound (Typ);
6458 Hi := Type_High_Bound (Typ);
6460 LB_Known := Compile_Time_Known_Value (Lo);
6461 HB_Known := Compile_Time_Known_Value (Hi);
6463 -- Fixed point types should be considered as such only if flag
6464 -- Fixed_Int is set to False.
6466 if Is_Floating_Point_Type (Typ)
6467 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
6468 or else Int_Real
6469 then
6470 Valr := Expr_Value_R (N);
6472 if LB_Known and HB_Known then
6473 if Valr >= Expr_Value_R (Lo)
6474 and then
6475 Valr <= Expr_Value_R (Hi)
6476 then
6477 return In_Range;
6478 else
6479 return Out_Of_Range;
6480 end if;
6482 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
6483 or else
6484 (HB_Known and then Valr > Expr_Value_R (Hi))
6485 then
6486 return Out_Of_Range;
6488 else
6489 return Unknown;
6490 end if;
6492 else
6493 Val := Expr_Value (N);
6495 if LB_Known and HB_Known then
6496 if Val >= Expr_Value (Lo) and then Val <= Expr_Value (Hi)
6497 then
6498 return In_Range;
6499 else
6500 return Out_Of_Range;
6501 end if;
6503 elsif (LB_Known and then Val < Expr_Value (Lo))
6504 or else
6505 (HB_Known and then Val > Expr_Value (Hi))
6506 then
6507 return Out_Of_Range;
6509 else
6510 return Unknown;
6511 end if;
6512 end if;
6513 end;
6515 -- Here for value not known at compile time. Case of expression subtype
6516 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6517 -- In this case we know it is in range without knowing its value.
6519 elsif Assume_Valid
6520 and then (Etype (N) = Typ or else Is_Subtype_Of (Etype (N), Typ))
6521 then
6522 return In_Range;
6524 -- Another special case. For signed integer types, if the target type
6525 -- has Is_Known_Valid set, and the source type does not have a larger
6526 -- size, then the source value must be in range. We exclude biased
6527 -- types, because they bizarrely can generate out of range values.
6529 elsif Is_Signed_Integer_Type (Etype (N))
6530 and then Is_Known_Valid (Typ)
6531 and then Esize (Etype (N)) <= Esize (Typ)
6532 and then not Has_Biased_Representation (Etype (N))
6533 then
6534 return In_Range;
6536 -- For all other cases, result is unknown
6538 else
6539 return Unknown;
6540 end if;
6541 end Test_In_Range;
6543 --------------
6544 -- To_Bits --
6545 --------------
6547 procedure To_Bits (U : Uint; B : out Bits) is
6548 begin
6549 for J in 0 .. B'Last loop
6550 B (J) := (U / (2 ** J)) mod 2 /= 0;
6551 end loop;
6552 end To_Bits;
6554 --------------------
6555 -- Why_Not_Static --
6556 --------------------
6558 procedure Why_Not_Static (Expr : Node_Id) is
6559 N : constant Node_Id := Original_Node (Expr);
6560 Typ : Entity_Id := Empty;
6561 E : Entity_Id;
6562 Alt : Node_Id;
6563 Exp : Node_Id;
6565 procedure Why_Not_Static_List (L : List_Id);
6566 -- A version that can be called on a list of expressions. Finds all
6567 -- non-static violations in any element of the list.
6569 -------------------------
6570 -- Why_Not_Static_List --
6571 -------------------------
6573 procedure Why_Not_Static_List (L : List_Id) is
6574 N : Node_Id;
6575 begin
6576 if Is_Non_Empty_List (L) then
6577 N := First (L);
6578 while Present (N) loop
6579 Why_Not_Static (N);
6580 Next (N);
6581 end loop;
6582 end if;
6583 end Why_Not_Static_List;
6585 -- Start of processing for Why_Not_Static
6587 begin
6588 -- Ignore call on error or empty node
6590 if No (Expr) or else Nkind (Expr) = N_Error then
6591 return;
6592 end if;
6594 -- Preprocessing for sub expressions
6596 if Nkind (Expr) in N_Subexpr then
6598 -- Nothing to do if expression is static
6600 if Is_OK_Static_Expression (Expr) then
6601 return;
6602 end if;
6604 -- Test for constraint error raised
6606 if Raises_Constraint_Error (Expr) then
6608 -- Special case membership to find out which piece to flag
6610 if Nkind (N) in N_Membership_Test then
6611 if Raises_Constraint_Error (Left_Opnd (N)) then
6612 Why_Not_Static (Left_Opnd (N));
6613 return;
6615 elsif Present (Right_Opnd (N))
6616 and then Raises_Constraint_Error (Right_Opnd (N))
6617 then
6618 Why_Not_Static (Right_Opnd (N));
6619 return;
6621 else
6622 pragma Assert (Present (Alternatives (N)));
6624 Alt := First (Alternatives (N));
6625 while Present (Alt) loop
6626 if Raises_Constraint_Error (Alt) then
6627 Why_Not_Static (Alt);
6628 return;
6629 else
6630 Next (Alt);
6631 end if;
6632 end loop;
6633 end if;
6635 -- Special case a range to find out which bound to flag
6637 elsif Nkind (N) = N_Range then
6638 if Raises_Constraint_Error (Low_Bound (N)) then
6639 Why_Not_Static (Low_Bound (N));
6640 return;
6642 elsif Raises_Constraint_Error (High_Bound (N)) then
6643 Why_Not_Static (High_Bound (N));
6644 return;
6645 end if;
6647 -- Special case attribute to see which part to flag
6649 elsif Nkind (N) = N_Attribute_Reference then
6650 if Raises_Constraint_Error (Prefix (N)) then
6651 Why_Not_Static (Prefix (N));
6652 return;
6653 end if;
6655 if Present (Expressions (N)) then
6656 Exp := First (Expressions (N));
6657 while Present (Exp) loop
6658 if Raises_Constraint_Error (Exp) then
6659 Why_Not_Static (Exp);
6660 return;
6661 end if;
6663 Next (Exp);
6664 end loop;
6665 end if;
6667 -- Special case a subtype name
6669 elsif Is_Entity_Name (Expr) and then Is_Type (Entity (Expr)) then
6670 Error_Msg_NE
6671 ("!& is not a static subtype (RM 4.9(26))", N, Entity (Expr));
6672 return;
6673 end if;
6675 -- End of special cases
6677 Error_Msg_N
6678 ("!expression raises exception, cannot be static (RM 4.9(34))",
6680 return;
6681 end if;
6683 -- If no type, then something is pretty wrong, so ignore
6685 Typ := Etype (Expr);
6687 if No (Typ) then
6688 return;
6689 end if;
6691 -- Type must be scalar or string type (but allow Bignum, since this
6692 -- is really a scalar type from our point of view in this diagnosis).
6694 if not Is_Scalar_Type (Typ)
6695 and then not Is_String_Type (Typ)
6696 and then not Is_RTE (Typ, RE_Bignum)
6697 then
6698 Error_Msg_N
6699 ("!static expression must have scalar or string type " &
6700 "(RM 4.9(2))", N);
6701 return;
6702 end if;
6703 end if;
6705 -- If we got through those checks, test particular node kind
6707 case Nkind (N) is
6709 -- Entity name
6711 when N_Expanded_Name
6712 | N_Identifier
6713 | N_Operator_Symbol
6715 E := Entity (N);
6717 if Is_Named_Number (E) then
6718 null;
6720 elsif Ekind (E) = E_Constant then
6722 -- One case we can give a metter message is when we have a
6723 -- string literal created by concatenating an aggregate with
6724 -- an others expression.
6726 Entity_Case : declare
6727 CV : constant Node_Id := Constant_Value (E);
6728 CO : constant Node_Id := Original_Node (CV);
6730 function Is_Aggregate (N : Node_Id) return Boolean;
6731 -- See if node N came from an others aggregate, if so
6732 -- return True and set Error_Msg_Sloc to aggregate.
6734 ------------------
6735 -- Is_Aggregate --
6736 ------------------
6738 function Is_Aggregate (N : Node_Id) return Boolean is
6739 begin
6740 if Nkind (Original_Node (N)) = N_Aggregate then
6741 Error_Msg_Sloc := Sloc (Original_Node (N));
6742 return True;
6744 elsif Is_Entity_Name (N)
6745 and then Ekind (Entity (N)) = E_Constant
6746 and then
6747 Nkind (Original_Node (Constant_Value (Entity (N)))) =
6748 N_Aggregate
6749 then
6750 Error_Msg_Sloc :=
6751 Sloc (Original_Node (Constant_Value (Entity (N))));
6752 return True;
6754 else
6755 return False;
6756 end if;
6757 end Is_Aggregate;
6759 -- Start of processing for Entity_Case
6761 begin
6762 if Is_Aggregate (CV)
6763 or else (Nkind (CO) = N_Op_Concat
6764 and then (Is_Aggregate (Left_Opnd (CO))
6765 or else
6766 Is_Aggregate (Right_Opnd (CO))))
6767 then
6768 Error_Msg_N ("!aggregate (#) is never static", N);
6770 elsif No (CV) or else not Is_Static_Expression (CV) then
6771 Error_Msg_NE
6772 ("!& is not a static constant (RM 4.9(5))", N, E);
6773 end if;
6774 end Entity_Case;
6776 elsif Is_Type (E) then
6777 Error_Msg_NE
6778 ("!& is not a static subtype (RM 4.9(26))", N, E);
6780 else
6781 Error_Msg_NE
6782 ("!& is not static constant or named number "
6783 & "(RM 4.9(5))", N, E);
6784 end if;
6786 -- Binary operator
6788 when N_Binary_Op
6789 | N_Membership_Test
6790 | N_Short_Circuit
6792 if Nkind (N) in N_Op_Shift then
6793 Error_Msg_N
6794 ("!shift functions are never static (RM 4.9(6,18))", N);
6795 else
6796 Why_Not_Static (Left_Opnd (N));
6797 Why_Not_Static (Right_Opnd (N));
6798 end if;
6800 -- Unary operator
6802 when N_Unary_Op =>
6803 Why_Not_Static (Right_Opnd (N));
6805 -- Attribute reference
6807 when N_Attribute_Reference =>
6808 Why_Not_Static_List (Expressions (N));
6810 E := Etype (Prefix (N));
6812 if E = Standard_Void_Type then
6813 return;
6814 end if;
6816 -- Special case non-scalar'Size since this is a common error
6818 if Attribute_Name (N) = Name_Size then
6819 Error_Msg_N
6820 ("!size attribute is only static for static scalar type "
6821 & "(RM 4.9(7,8))", N);
6823 -- Flag array cases
6825 elsif Is_Array_Type (E) then
6826 if not Nam_In (Attribute_Name (N), Name_First,
6827 Name_Last,
6828 Name_Length)
6829 then
6830 Error_Msg_N
6831 ("!static array attribute must be Length, First, or Last "
6832 & "(RM 4.9(8))", N);
6834 -- Since we know the expression is not-static (we already
6835 -- tested for this, must mean array is not static).
6837 else
6838 Error_Msg_N
6839 ("!prefix is non-static array (RM 4.9(8))", Prefix (N));
6840 end if;
6842 return;
6844 -- Special case generic types, since again this is a common source
6845 -- of confusion.
6847 elsif Is_Generic_Actual_Type (E) or else Is_Generic_Type (E) then
6848 Error_Msg_N
6849 ("!attribute of generic type is never static "
6850 & "(RM 4.9(7,8))", N);
6852 elsif Is_OK_Static_Subtype (E) then
6853 null;
6855 elsif Is_Scalar_Type (E) then
6856 Error_Msg_N
6857 ("!prefix type for attribute is not static scalar subtype "
6858 & "(RM 4.9(7))", N);
6860 else
6861 Error_Msg_N
6862 ("!static attribute must apply to array/scalar type "
6863 & "(RM 4.9(7,8))", N);
6864 end if;
6866 -- String literal
6868 when N_String_Literal =>
6869 Error_Msg_N
6870 ("!subtype of string literal is non-static (RM 4.9(4))", N);
6872 -- Explicit dereference
6874 when N_Explicit_Dereference =>
6875 Error_Msg_N
6876 ("!explicit dereference is never static (RM 4.9)", N);
6878 -- Function call
6880 when N_Function_Call =>
6881 Why_Not_Static_List (Parameter_Associations (N));
6883 -- Complain about non-static function call unless we have Bignum
6884 -- which means that the underlying expression is really some
6885 -- scalar arithmetic operation.
6887 if not Is_RTE (Typ, RE_Bignum) then
6888 Error_Msg_N ("!non-static function call (RM 4.9(6,18))", N);
6889 end if;
6891 -- Parameter assocation (test actual parameter)
6893 when N_Parameter_Association =>
6894 Why_Not_Static (Explicit_Actual_Parameter (N));
6896 -- Indexed component
6898 when N_Indexed_Component =>
6899 Error_Msg_N ("!indexed component is never static (RM 4.9)", N);
6901 -- Procedure call
6903 when N_Procedure_Call_Statement =>
6904 Error_Msg_N ("!procedure call is never static (RM 4.9)", N);
6906 -- Qualified expression (test expression)
6908 when N_Qualified_Expression =>
6909 Why_Not_Static (Expression (N));
6911 -- Aggregate
6913 when N_Aggregate
6914 | N_Extension_Aggregate
6916 Error_Msg_N ("!an aggregate is never static (RM 4.9)", N);
6918 -- Range
6920 when N_Range =>
6921 Why_Not_Static (Low_Bound (N));
6922 Why_Not_Static (High_Bound (N));
6924 -- Range constraint, test range expression
6926 when N_Range_Constraint =>
6927 Why_Not_Static (Range_Expression (N));
6929 -- Subtype indication, test constraint
6931 when N_Subtype_Indication =>
6932 Why_Not_Static (Constraint (N));
6934 -- Selected component
6936 when N_Selected_Component =>
6937 Error_Msg_N ("!selected component is never static (RM 4.9)", N);
6939 -- Slice
6941 when N_Slice =>
6942 Error_Msg_N ("!slice is never static (RM 4.9)", N);
6944 when N_Type_Conversion =>
6945 Why_Not_Static (Expression (N));
6947 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
6948 or else not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
6949 then
6950 Error_Msg_N
6951 ("!static conversion requires static scalar subtype result "
6952 & "(RM 4.9(9))", N);
6953 end if;
6955 -- Unchecked type conversion
6957 when N_Unchecked_Type_Conversion =>
6958 Error_Msg_N
6959 ("!unchecked type conversion is never static (RM 4.9)", N);
6961 -- All other cases, no reason to give
6963 when others =>
6964 null;
6965 end case;
6966 end Why_Not_Static;
6968 end Sem_Eval;