Debian Codegraph @

gcc/ada/sem_eval.adb

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