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