2014-01-30 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / sem_eval.adb
blob4d6902179879e9332736935544898724e47968af
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-2013, Free Software Foundation, Inc. --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with 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 Rtsfind; use Rtsfind;
41 with Sem; use Sem;
42 with Sem_Aux; use Sem_Aux;
43 with Sem_Cat; use Sem_Cat;
44 with Sem_Ch6; use Sem_Ch6;
45 with Sem_Ch8; use Sem_Ch8;
46 with Sem_Res; use Sem_Res;
47 with Sem_Util; use Sem_Util;
48 with Sem_Type; use Sem_Type;
49 with Sem_Warn; use Sem_Warn;
50 with Sinfo; use Sinfo;
51 with Snames; use Snames;
52 with Stand; use Stand;
53 with Stringt; use Stringt;
54 with Tbuild; use Tbuild;
56 package body Sem_Eval is
58 -----------------------------------------
59 -- Handling of Compile Time Evaluation --
60 -----------------------------------------
62 -- The compile time evaluation of expressions is distributed over several
63 -- Eval_xxx procedures. These procedures are called immediately after
64 -- a subexpression is resolved and is therefore accomplished in a bottom
65 -- up fashion. The flags are synthesized using the following approach.
67 -- Is_Static_Expression is determined by following the detailed rules
68 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
69 -- flag of the operands in many cases.
71 -- Raises_Constraint_Error is set if any of the operands have the flag
72 -- set or if an attempt to compute the value of the current expression
73 -- results in detection of a runtime constraint error.
75 -- As described in the spec, the requirement is that Is_Static_Expression
76 -- be accurately set, and in addition for nodes for which this flag is set,
77 -- Raises_Constraint_Error must also be set. Furthermore a node which has
78 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
79 -- requirement is that the expression value must be precomputed, and the
80 -- node is either a literal, or the name of a constant entity whose value
81 -- is a static expression.
83 -- The general approach is as follows. First compute Is_Static_Expression.
84 -- If the node is not static, then the flag is left off in the node and
85 -- we are all done. Otherwise for a static node, we test if any of the
86 -- operands will raise constraint error, and if so, propagate the flag
87 -- Raises_Constraint_Error to the result node and we are done (since the
88 -- error was already posted at a lower level).
90 -- For the case of a static node whose operands do not raise constraint
91 -- error, we attempt to evaluate the node. If this evaluation succeeds,
92 -- then the node is replaced by the result of this computation. If the
93 -- evaluation raises constraint error, then we rewrite the node with
94 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
95 -- to post appropriate error messages.
97 ----------------
98 -- Local Data --
99 ----------------
101 type Bits is array (Nat range <>) of Boolean;
102 -- Used to convert unsigned (modular) values for folding logical ops
104 -- The following definitions are used to maintain a cache of nodes that
105 -- have compile time known values. The cache is maintained only for
106 -- discrete types (the most common case), and is populated by calls to
107 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
108 -- since it is possible for the status to change (in particular it is
109 -- possible for a node to get replaced by a constraint error node).
111 CV_Bits : constant := 5;
112 -- Number of low order bits of Node_Id value used to reference entries
113 -- in the cache table.
115 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
116 -- Size of cache for compile time values
118 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
120 type CV_Entry is record
121 N : Node_Id;
122 V : Uint;
123 end record;
125 type CV_Cache_Array is array (CV_Range) of CV_Entry;
127 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
128 -- This is the actual cache, with entries consisting of node/value pairs,
129 -- and the impossible value Node_High_Bound used for unset entries.
131 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
132 -- Range membership may either be statically known to be in range or out
133 -- of range, or not statically known. Used for Test_In_Range below.
135 -----------------------
136 -- Local Subprograms --
137 -----------------------
139 function From_Bits (B : Bits; T : Entity_Id) return Uint;
140 -- Converts a bit string of length B'Length to a Uint value to be used
141 -- for a target of type T, which is a modular type. This procedure
142 -- includes the necessary reduction by the modulus in the case of a
143 -- non-binary modulus (for a binary modulus, the bit string is the
144 -- right length any way so all is well).
146 function Get_String_Val (N : Node_Id) return Node_Id;
147 -- Given a tree node for a folded string or character value, returns
148 -- the corresponding string literal or character literal (one of the
149 -- two must be available, or the operand would not have been marked
150 -- as foldable in the earlier analysis of the operation).
152 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
153 -- Bits represents the number of bits in an integer value to be computed
154 -- (but the value has not been computed yet). If this value in Bits is
155 -- reasonable, a result of True is returned, with the implication that
156 -- the caller should go ahead and complete the calculation. If the value
157 -- in Bits is unreasonably large, then an error is posted on node N, and
158 -- False is returned (and the caller skips the proposed calculation).
160 procedure Out_Of_Range (N : Node_Id);
161 -- This procedure is called if it is determined that node N, which
162 -- appears in a non-static context, is a compile time known value
163 -- which is outside its range, i.e. the range of Etype. This is used
164 -- in contexts where this is an illegality if N is static, and should
165 -- generate a warning otherwise.
167 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
168 -- N and Exp are nodes representing an expression, Exp is known
169 -- to raise CE. N is rewritten in term of Exp in the optimal way.
171 function String_Type_Len (Stype : Entity_Id) return Uint;
172 -- Given a string type, determines the length of the index type, or,
173 -- if this index type is non-static, the length of the base type of
174 -- this index type. Note that if the string type is itself static,
175 -- then the index type is static, so the second case applies only
176 -- if the string type passed is non-static.
178 function Test (Cond : Boolean) return Uint;
179 pragma Inline (Test);
180 -- This function simply returns the appropriate Boolean'Pos value
181 -- corresponding to the value of Cond as a universal integer. It is
182 -- used for producing the result of the static evaluation of the
183 -- logical operators
185 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
186 -- Check whether an arithmetic operation with universal operands which
187 -- is a rewritten function call with an explicit scope indication is
188 -- ambiguous: P."+" (1, 2) will be ambiguous if there is more than one
189 -- visible numeric type declared in P and the context does not impose a
190 -- type on the result (e.g. in the expression of a type conversion).
191 -- If ambiguous, emit an error and return Empty, else return the result
192 -- type of the operator.
194 procedure Test_Expression_Is_Foldable
195 (N : Node_Id;
196 Op1 : Node_Id;
197 Stat : out Boolean;
198 Fold : out Boolean);
199 -- Tests to see if expression N whose single operand is Op1 is foldable,
200 -- i.e. the operand value is known at compile time. If the operation is
201 -- foldable, then Fold is True on return, and Stat indicates whether
202 -- the result is static (i.e. the operand was static). Note that it
203 -- is quite possible for Fold to be True, and Stat to be False, since
204 -- there are cases in which we know the value of an operand even though
205 -- it is not technically static (e.g. the static lower bound of a range
206 -- whose upper bound is non-static).
208 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes a
209 -- call to Check_Non_Static_Context on the operand. If Fold is False on
210 -- return, then all processing is complete, and the caller should
211 -- return, since there is nothing else to do.
213 -- If Stat is set True on return, then Is_Static_Expression is also set
214 -- true in node N. There are some cases where this is over-enthusiastic,
215 -- e.g. in the two operand case below, for string comparison, the result
216 -- is not static even though the two operands are static. In such cases,
217 -- the caller must reset the Is_Static_Expression flag in N.
219 -- If Fold and Stat are both set to False then this routine performs also
220 -- the following extra actions:
222 -- If either operand is Any_Type then propagate it to result to
223 -- prevent cascaded errors.
225 -- If some operand raises constraint error, then replace the node N
226 -- with the raise constraint error node. This replacement inherits the
227 -- Is_Static_Expression flag from the operands.
229 procedure Test_Expression_Is_Foldable
230 (N : Node_Id;
231 Op1 : Node_Id;
232 Op2 : Node_Id;
233 Stat : out Boolean;
234 Fold : out Boolean;
235 CRT_Safe : Boolean := False);
236 -- Same processing, except applies to an expression N with two operands
237 -- Op1 and Op2. The result is static only if both operands are static. If
238 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
239 -- for the tests that the two operands are known at compile time. See
240 -- spec of this routine for further details.
242 function Test_In_Range
243 (N : Node_Id;
244 Typ : Entity_Id;
245 Assume_Valid : Boolean;
246 Fixed_Int : Boolean;
247 Int_Real : Boolean) return Range_Membership;
248 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
249 -- or Out_Of_Range if it can be guaranteed at compile time that expression
250 -- N is known to be in or out of range of the subtype Typ. If not compile
251 -- time known, Unknown is returned. See documentation of Is_In_Range for
252 -- complete description of parameters.
254 procedure To_Bits (U : Uint; B : out Bits);
255 -- Converts a Uint value to a bit string of length B'Length
257 ------------------------------
258 -- Check_Non_Static_Context --
259 ------------------------------
261 procedure Check_Non_Static_Context (N : Node_Id) is
262 T : constant Entity_Id := Etype (N);
263 Checks_On : constant Boolean :=
264 not Index_Checks_Suppressed (T)
265 and not Range_Checks_Suppressed (T);
267 begin
268 -- Ignore cases of non-scalar types, error types, or universal real
269 -- types that have no usable bounds.
271 if T = Any_Type
272 or else not Is_Scalar_Type (T)
273 or else T = Universal_Fixed
274 or else T = Universal_Real
275 then
276 return;
277 end if;
279 -- At this stage we have a scalar type. If we have an expression that
280 -- raises CE, then we already issued a warning or error msg so there
281 -- is nothing more to be done in this routine.
283 if Raises_Constraint_Error (N) then
284 return;
285 end if;
287 -- Now we have a scalar type which is not marked as raising a constraint
288 -- error exception. The main purpose of this routine is to deal with
289 -- static expressions appearing in a non-static context. That means
290 -- that if we do not have a static expression then there is not much
291 -- to do. The one case that we deal with here is that if we have a
292 -- floating-point value that is out of range, then we post a warning
293 -- that an infinity will result.
295 if not Is_Static_Expression (N) then
296 if Is_Floating_Point_Type (T)
297 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
298 then
299 Error_Msg_N
300 ("??float value out of range, infinity will be generated", N);
301 end if;
303 return;
304 end if;
306 -- Here we have the case of outer level static expression of scalar
307 -- type, where the processing of this procedure is needed.
309 -- For real types, this is where we convert the value to a machine
310 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
311 -- need to do this if the parent is a constant declaration, since in
312 -- other cases, gigi should do the necessary conversion correctly, but
313 -- experimentation shows that this is not the case on all machines, in
314 -- particular if we do not convert all literals to machine values in
315 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
316 -- and SGI/Irix.
318 if Nkind (N) = N_Real_Literal
319 and then not Is_Machine_Number (N)
320 and then not Is_Generic_Type (Etype (N))
321 and then Etype (N) /= Universal_Real
322 then
323 -- Check that value is in bounds before converting to machine
324 -- number, so as not to lose case where value overflows in the
325 -- least significant bit or less. See B490001.
327 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
328 Out_Of_Range (N);
329 return;
330 end if;
332 -- Note: we have to copy the node, to avoid problems with conformance
333 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
335 Rewrite (N, New_Copy (N));
337 if not Is_Floating_Point_Type (T) then
338 Set_Realval
339 (N, Corresponding_Integer_Value (N) * Small_Value (T));
341 elsif not UR_Is_Zero (Realval (N)) then
343 -- Note: even though RM 4.9(38) specifies biased rounding, this
344 -- has been modified by AI-100 in order to prevent confusing
345 -- differences in rounding between static and non-static
346 -- expressions. AI-100 specifies that the effect of such rounding
347 -- is implementation dependent, and in GNAT we round to nearest
348 -- even to match the run-time behavior.
350 Set_Realval
351 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
352 end if;
354 Set_Is_Machine_Number (N);
355 end if;
357 -- Check for out of range universal integer. This is a non-static
358 -- context, so the integer value must be in range of the runtime
359 -- representation of universal integers.
361 -- We do this only within an expression, because that is the only
362 -- case in which non-static universal integer values can occur, and
363 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
364 -- called in contexts like the expression of a number declaration where
365 -- we certainly want to allow out of range values.
367 if Etype (N) = Universal_Integer
368 and then Nkind (N) = N_Integer_Literal
369 and then Nkind (Parent (N)) in N_Subexpr
370 and then
371 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
372 or else
373 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
374 then
375 Apply_Compile_Time_Constraint_Error
376 (N, "non-static universal integer value out of range<<",
377 CE_Range_Check_Failed);
379 -- Check out of range of base type
381 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
382 Out_Of_Range (N);
384 -- Give warning if outside subtype (where one or both of the bounds of
385 -- the subtype is static). This warning is omitted if the expression
386 -- appears in a range that could be null (warnings are handled elsewhere
387 -- for this case).
389 elsif T /= Base_Type (T)
390 and then Nkind (Parent (N)) /= N_Range
391 then
392 if Is_In_Range (N, T, Assume_Valid => True) then
393 null;
395 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
396 Apply_Compile_Time_Constraint_Error
397 (N, "value not in range of}<<", CE_Range_Check_Failed);
399 elsif Checks_On then
400 Enable_Range_Check (N);
402 else
403 Set_Do_Range_Check (N, False);
404 end if;
405 end if;
406 end Check_Non_Static_Context;
408 ---------------------------------
409 -- Check_String_Literal_Length --
410 ---------------------------------
412 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
413 begin
414 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
416 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
417 then
418 Apply_Compile_Time_Constraint_Error
419 (N, "string length wrong for}??",
420 CE_Length_Check_Failed,
421 Ent => Ttype,
422 Typ => Ttype);
423 end if;
424 end if;
425 end Check_String_Literal_Length;
427 --------------------------
428 -- Compile_Time_Compare --
429 --------------------------
431 function Compile_Time_Compare
432 (L, R : Node_Id;
433 Assume_Valid : Boolean) return Compare_Result
435 Discard : aliased Uint;
436 begin
437 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
438 end Compile_Time_Compare;
440 function Compile_Time_Compare
441 (L, R : Node_Id;
442 Diff : access Uint;
443 Assume_Valid : Boolean;
444 Rec : Boolean := False) return Compare_Result
446 Ltyp : Entity_Id := Underlying_Type (Etype (L));
447 Rtyp : Entity_Id := Underlying_Type (Etype (R));
448 -- These get reset to the base type for the case of entities where
449 -- Is_Known_Valid is not set. This takes care of handling possible
450 -- invalid representations using the value of the base type, in
451 -- accordance with RM 13.9.1(10).
453 Discard : aliased Uint;
455 procedure Compare_Decompose
456 (N : Node_Id;
457 R : out Node_Id;
458 V : out Uint);
459 -- This procedure decomposes the node N into an expression node and a
460 -- signed offset, so that the value of N is equal to the value of R plus
461 -- the value V (which may be negative). If no such decomposition is
462 -- possible, then on return R is a copy of N, and V is set to zero.
464 function Compare_Fixup (N : Node_Id) return Node_Id;
465 -- This function deals with replacing 'Last and 'First references with
466 -- their corresponding type bounds, which we then can compare. The
467 -- argument is the original node, the result is the identity, unless we
468 -- have a 'Last/'First reference in which case the value returned is the
469 -- appropriate type bound.
471 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
472 -- Even if the context does not assume that values are valid, some
473 -- simple cases can be recognized.
475 function Is_Same_Value (L, R : Node_Id) return Boolean;
476 -- Returns True iff L and R represent expressions that definitely have
477 -- identical (but not necessarily compile time known) values Indeed the
478 -- caller is expected to have already dealt with the cases of compile
479 -- time known values, so these are not tested here.
481 -----------------------
482 -- Compare_Decompose --
483 -----------------------
485 procedure Compare_Decompose
486 (N : Node_Id;
487 R : out Node_Id;
488 V : out Uint)
490 begin
491 if Nkind (N) = N_Op_Add
492 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
493 then
494 R := Left_Opnd (N);
495 V := Intval (Right_Opnd (N));
496 return;
498 elsif Nkind (N) = N_Op_Subtract
499 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
500 then
501 R := Left_Opnd (N);
502 V := UI_Negate (Intval (Right_Opnd (N)));
503 return;
505 elsif Nkind (N) = N_Attribute_Reference then
506 if Attribute_Name (N) = Name_Succ then
507 R := First (Expressions (N));
508 V := Uint_1;
509 return;
511 elsif Attribute_Name (N) = Name_Pred then
512 R := First (Expressions (N));
513 V := Uint_Minus_1;
514 return;
515 end if;
516 end if;
518 R := N;
519 V := Uint_0;
520 end Compare_Decompose;
522 -------------------
523 -- Compare_Fixup --
524 -------------------
526 function Compare_Fixup (N : Node_Id) return Node_Id is
527 Indx : Node_Id;
528 Xtyp : Entity_Id;
529 Subs : Nat;
531 begin
532 -- Fixup only required for First/Last attribute reference
534 if Nkind (N) = N_Attribute_Reference
535 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
536 then
537 Xtyp := Etype (Prefix (N));
539 -- If we have no type, then just abandon the attempt to do
540 -- a fixup, this is probably the result of some other error.
542 if No (Xtyp) then
543 return N;
544 end if;
546 -- Dereference an access type
548 if Is_Access_Type (Xtyp) then
549 Xtyp := Designated_Type (Xtyp);
550 end if;
552 -- If we don't have an array type at this stage, something
553 -- is peculiar, e.g. another error, and we abandon the attempt
554 -- at a fixup.
556 if not Is_Array_Type (Xtyp) then
557 return N;
558 end if;
560 -- Ignore unconstrained array, since bounds are not meaningful
562 if not Is_Constrained (Xtyp) then
563 return N;
564 end if;
566 if Ekind (Xtyp) = E_String_Literal_Subtype then
567 if Attribute_Name (N) = Name_First then
568 return String_Literal_Low_Bound (Xtyp);
570 else
571 return Make_Integer_Literal (Sloc (N),
572 Intval => Intval (String_Literal_Low_Bound (Xtyp))
573 + String_Literal_Length (Xtyp));
574 end if;
575 end if;
577 -- Find correct index type
579 Indx := First_Index (Xtyp);
581 if Present (Expressions (N)) then
582 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
584 for J in 2 .. Subs loop
585 Indx := Next_Index (Indx);
586 end loop;
587 end if;
589 Xtyp := Etype (Indx);
591 if Attribute_Name (N) = Name_First then
592 return Type_Low_Bound (Xtyp);
593 else
594 return Type_High_Bound (Xtyp);
595 end if;
596 end if;
598 return N;
599 end Compare_Fixup;
601 ----------------------------
602 -- Is_Known_Valid_Operand --
603 ----------------------------
605 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
606 begin
607 return (Is_Entity_Name (Opnd)
608 and then
609 (Is_Known_Valid (Entity (Opnd))
610 or else Ekind (Entity (Opnd)) = E_In_Parameter
611 or else
612 (Ekind (Entity (Opnd)) in Object_Kind
613 and then Present (Current_Value (Entity (Opnd))))))
614 or else Is_OK_Static_Expression (Opnd);
615 end Is_Known_Valid_Operand;
617 -------------------
618 -- Is_Same_Value --
619 -------------------
621 function Is_Same_Value (L, R : Node_Id) return Boolean is
622 Lf : constant Node_Id := Compare_Fixup (L);
623 Rf : constant Node_Id := Compare_Fixup (R);
625 function Is_Same_Subscript (L, R : List_Id) return Boolean;
626 -- L, R are the Expressions values from two attribute nodes for First
627 -- or Last attributes. Either may be set to No_List if no expressions
628 -- are present (indicating subscript 1). The result is True if both
629 -- expressions represent the same subscript (note one case is where
630 -- one subscript is missing and the other is explicitly set to 1).
632 -----------------------
633 -- Is_Same_Subscript --
634 -----------------------
636 function Is_Same_Subscript (L, R : List_Id) return Boolean is
637 begin
638 if L = No_List then
639 if R = No_List then
640 return True;
641 else
642 return Expr_Value (First (R)) = Uint_1;
643 end if;
645 else
646 if R = No_List then
647 return Expr_Value (First (L)) = Uint_1;
648 else
649 return Expr_Value (First (L)) = Expr_Value (First (R));
650 end if;
651 end if;
652 end Is_Same_Subscript;
654 -- Start of processing for Is_Same_Value
656 begin
657 -- Values are the same if they refer to the same entity and the
658 -- entity is non-volatile. This does not however apply to Float
659 -- types, since we may have two NaN values and they should never
660 -- compare equal.
662 -- If the entity is a discriminant, the two expressions may be bounds
663 -- of components of objects of the same discriminated type. The
664 -- values of the discriminants are not static, and therefore the
665 -- result is unknown.
667 -- It would be better to comment individual branches of this test ???
669 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
670 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
671 and then Entity (Lf) = Entity (Rf)
672 and then Ekind (Entity (Lf)) /= E_Discriminant
673 and then Present (Entity (Lf))
674 and then not Is_Floating_Point_Type (Etype (L))
675 and then not Is_Volatile_Reference (L)
676 and then not Is_Volatile_Reference (R)
677 then
678 return True;
680 -- Or if they are compile time known and identical
682 elsif Compile_Time_Known_Value (Lf)
683 and then
684 Compile_Time_Known_Value (Rf)
685 and then Expr_Value (Lf) = Expr_Value (Rf)
686 then
687 return True;
689 -- False if Nkind of the two nodes is different for remaining cases
691 elsif Nkind (Lf) /= Nkind (Rf) then
692 return False;
694 -- True if both 'First or 'Last values applying to the same entity
695 -- (first and last don't change even if value does). Note that we
696 -- need this even with the calls to Compare_Fixup, to handle the
697 -- case of unconstrained array attributes where Compare_Fixup
698 -- cannot find useful bounds.
700 elsif Nkind (Lf) = N_Attribute_Reference
701 and then Attribute_Name (Lf) = Attribute_Name (Rf)
702 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
703 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
704 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
705 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
706 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
707 then
708 return True;
710 -- True if the same selected component from the same record
712 elsif Nkind (Lf) = N_Selected_Component
713 and then Selector_Name (Lf) = Selector_Name (Rf)
714 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
715 then
716 return True;
718 -- True if the same unary operator applied to the same operand
720 elsif Nkind (Lf) in N_Unary_Op
721 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
722 then
723 return True;
725 -- True if the same binary operator applied to the same operands
727 elsif Nkind (Lf) in N_Binary_Op
728 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
729 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
730 then
731 return True;
733 -- All other cases, we can't tell, so return False
735 else
736 return False;
737 end if;
738 end Is_Same_Value;
740 -- Start of processing for Compile_Time_Compare
742 begin
743 Diff.all := No_Uint;
745 -- In preanalysis mode, always return Unknown unless the expression
746 -- is static. It is too early to be thinking we know the result of a
747 -- comparison, save that judgment for the full analysis. This is
748 -- particularly important in the case of pre and postconditions, which
749 -- otherwise can be prematurely collapsed into having True or False
750 -- conditions when this is inappropriate.
752 if not (Full_Analysis
753 or else (Is_Static_Expression (L)
754 and then
755 Is_Static_Expression (R)))
756 then
757 return Unknown;
758 end if;
760 -- If either operand could raise constraint error, then we cannot
761 -- know the result at compile time (since CE may be raised).
763 if not (Cannot_Raise_Constraint_Error (L)
764 and then
765 Cannot_Raise_Constraint_Error (R))
766 then
767 return Unknown;
768 end if;
770 -- Identical operands are most certainly equal
772 if L = R then
773 return EQ;
775 -- If expressions have no types, then do not attempt to determine if
776 -- they are the same, since something funny is going on. One case in
777 -- which this happens is during generic template analysis, when bounds
778 -- are not fully analyzed.
780 elsif No (Ltyp) or else No (Rtyp) then
781 return Unknown;
783 -- We do not attempt comparisons for packed arrays arrays represented as
784 -- modular types, where the semantics of comparison is quite different.
786 elsif Is_Packed_Array_Type (Ltyp)
787 and then Is_Modular_Integer_Type (Ltyp)
788 then
789 return Unknown;
791 -- For access types, the only time we know the result at compile time
792 -- (apart from identical operands, which we handled already) is if we
793 -- know one operand is null and the other is not, or both operands are
794 -- known null.
796 elsif Is_Access_Type (Ltyp) then
797 if Known_Null (L) then
798 if Known_Null (R) then
799 return EQ;
800 elsif Known_Non_Null (R) then
801 return NE;
802 else
803 return Unknown;
804 end if;
806 elsif Known_Non_Null (L) and then Known_Null (R) then
807 return NE;
809 else
810 return Unknown;
811 end if;
813 -- Case where comparison involves two compile time known values
815 elsif Compile_Time_Known_Value (L)
816 and then Compile_Time_Known_Value (R)
817 then
818 -- For the floating-point case, we have to be a little careful, since
819 -- at compile time we are dealing with universal exact values, but at
820 -- runtime, these will be in non-exact target form. That's why the
821 -- returned results are LE and GE below instead of LT and GT.
823 if Is_Floating_Point_Type (Ltyp)
824 or else
825 Is_Floating_Point_Type (Rtyp)
826 then
827 declare
828 Lo : constant Ureal := Expr_Value_R (L);
829 Hi : constant Ureal := Expr_Value_R (R);
831 begin
832 if Lo < Hi then
833 return LE;
834 elsif Lo = Hi then
835 return EQ;
836 else
837 return GE;
838 end if;
839 end;
841 -- For string types, we have two string literals and we proceed to
842 -- compare them using the Ada style dictionary string comparison.
844 elsif not Is_Scalar_Type (Ltyp) then
845 declare
846 Lstring : constant String_Id := Strval (Expr_Value_S (L));
847 Rstring : constant String_Id := Strval (Expr_Value_S (R));
848 Llen : constant Nat := String_Length (Lstring);
849 Rlen : constant Nat := String_Length (Rstring);
851 begin
852 for J in 1 .. Nat'Min (Llen, Rlen) loop
853 declare
854 LC : constant Char_Code := Get_String_Char (Lstring, J);
855 RC : constant Char_Code := Get_String_Char (Rstring, J);
856 begin
857 if LC < RC then
858 return LT;
859 elsif LC > RC then
860 return GT;
861 end if;
862 end;
863 end loop;
865 if Llen < Rlen then
866 return LT;
867 elsif Llen > Rlen then
868 return GT;
869 else
870 return EQ;
871 end if;
872 end;
874 -- For remaining scalar cases we know exactly (note that this does
875 -- include the fixed-point case, where we know the run time integer
876 -- values now).
878 else
879 declare
880 Lo : constant Uint := Expr_Value (L);
881 Hi : constant Uint := Expr_Value (R);
883 begin
884 if Lo < Hi then
885 Diff.all := Hi - Lo;
886 return LT;
888 elsif Lo = Hi then
889 return EQ;
891 else
892 Diff.all := Lo - Hi;
893 return GT;
894 end if;
895 end;
896 end if;
898 -- Cases where at least one operand is not known at compile time
900 else
901 -- Remaining checks apply only for discrete types
903 if not Is_Discrete_Type (Ltyp)
904 or else not Is_Discrete_Type (Rtyp)
905 then
906 return Unknown;
907 end if;
909 -- Defend against generic types, or actually any expressions that
910 -- contain a reference to a generic type from within a generic
911 -- template. We don't want to do any range analysis of such
912 -- expressions for two reasons. First, the bounds of a generic type
913 -- itself are junk and cannot be used for any kind of analysis.
914 -- Second, we may have a case where the range at run time is indeed
915 -- known, but we don't want to do compile time analysis in the
916 -- template based on that range since in an instance the value may be
917 -- static, and able to be elaborated without reference to the bounds
918 -- of types involved. As an example, consider:
920 -- (F'Pos (F'Last) + 1) > Integer'Last
922 -- The expression on the left side of > is Universal_Integer and thus
923 -- acquires the type Integer for evaluation at run time, and at run
924 -- time it is true that this condition is always False, but within
925 -- an instance F may be a type with a static range greater than the
926 -- range of Integer, and the expression statically evaluates to True.
928 if References_Generic_Formal_Type (L)
929 or else
930 References_Generic_Formal_Type (R)
931 then
932 return Unknown;
933 end if;
935 -- Replace types by base types for the case of entities which are
936 -- not known to have valid representations. This takes care of
937 -- properly dealing with invalid representations.
939 if not Assume_Valid and then not Assume_No_Invalid_Values then
940 if Is_Entity_Name (L) and then not Is_Known_Valid (Entity (L)) then
941 Ltyp := Underlying_Type (Base_Type (Ltyp));
942 end if;
944 if Is_Entity_Name (R) and then not Is_Known_Valid (Entity (R)) then
945 Rtyp := Underlying_Type (Base_Type (Rtyp));
946 end if;
947 end if;
949 -- First attempt is to decompose the expressions to extract a
950 -- constant offset resulting from the use of any of the forms:
952 -- expr + literal
953 -- expr - literal
954 -- typ'Succ (expr)
955 -- typ'Pred (expr)
957 -- Then we see if the two expressions are the same value, and if so
958 -- the result is obtained by comparing the offsets.
960 -- Note: the reason we do this test first is that it returns only
961 -- decisive results (with diff set), where other tests, like the
962 -- range test, may not be as so decisive. Consider for example
963 -- J .. J + 1. This code can conclude LT with a difference of 1,
964 -- even if the range of J is not known.
966 declare
967 Lnode : Node_Id;
968 Loffs : Uint;
969 Rnode : Node_Id;
970 Roffs : Uint;
972 begin
973 Compare_Decompose (L, Lnode, Loffs);
974 Compare_Decompose (R, Rnode, Roffs);
976 if Is_Same_Value (Lnode, Rnode) then
977 if Loffs = Roffs then
978 return EQ;
980 elsif Loffs < Roffs then
981 Diff.all := Roffs - Loffs;
982 return LT;
984 else
985 Diff.all := Loffs - Roffs;
986 return GT;
987 end if;
988 end if;
989 end;
991 -- Next, try range analysis and see if operand ranges are disjoint
993 declare
994 LOK, ROK : Boolean;
995 LLo, LHi : Uint;
996 RLo, RHi : Uint;
998 Single : Boolean;
999 -- True if each range is a single point
1001 begin
1002 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
1003 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1005 if LOK and ROK then
1006 Single := (LLo = LHi) and then (RLo = RHi);
1008 if LHi < RLo then
1009 if Single and Assume_Valid then
1010 Diff.all := RLo - LLo;
1011 end if;
1013 return LT;
1015 elsif RHi < LLo then
1016 if Single and Assume_Valid then
1017 Diff.all := LLo - RLo;
1018 end if;
1020 return GT;
1022 elsif Single and then LLo = RLo then
1024 -- If the range includes a single literal and we can assume
1025 -- validity then the result is known even if an operand is
1026 -- not static.
1028 if Assume_Valid then
1029 return EQ;
1030 else
1031 return Unknown;
1032 end if;
1034 elsif LHi = RLo then
1035 return LE;
1037 elsif RHi = LLo then
1038 return GE;
1040 elsif not Is_Known_Valid_Operand (L)
1041 and then not Assume_Valid
1042 then
1043 if Is_Same_Value (L, R) then
1044 return EQ;
1045 else
1046 return Unknown;
1047 end if;
1048 end if;
1050 -- If the range of either operand cannot be determined, nothing
1051 -- further can be inferred.
1053 else
1054 return Unknown;
1055 end if;
1056 end;
1058 -- Here is where we check for comparisons against maximum bounds of
1059 -- types, where we know that no value can be outside the bounds of
1060 -- the subtype. Note that this routine is allowed to assume that all
1061 -- expressions are within their subtype bounds. Callers wishing to
1062 -- deal with possibly invalid values must in any case take special
1063 -- steps (e.g. conversions to larger types) to avoid this kind of
1064 -- optimization, which is always considered to be valid. We do not
1065 -- attempt this optimization with generic types, since the type
1066 -- bounds may not be meaningful in this case.
1068 -- We are in danger of an infinite recursion here. It does not seem
1069 -- useful to go more than one level deep, so the parameter Rec is
1070 -- used to protect ourselves against this infinite recursion.
1072 if not Rec then
1074 -- See if we can get a decisive check against one operand and
1075 -- a bound of the other operand (four possible tests here).
1076 -- Note that we avoid testing junk bounds of a generic type.
1078 if not Is_Generic_Type (Rtyp) then
1079 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1080 Discard'Access,
1081 Assume_Valid, Rec => True)
1083 when LT => return LT;
1084 when LE => return LE;
1085 when EQ => return LE;
1086 when others => null;
1087 end case;
1089 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1090 Discard'Access,
1091 Assume_Valid, Rec => True)
1093 when GT => return GT;
1094 when GE => return GE;
1095 when EQ => return GE;
1096 when others => null;
1097 end case;
1098 end if;
1100 if not Is_Generic_Type (Ltyp) then
1101 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1102 Discard'Access,
1103 Assume_Valid, Rec => True)
1105 when GT => return GT;
1106 when GE => return GE;
1107 when EQ => return GE;
1108 when others => null;
1109 end case;
1111 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1112 Discard'Access,
1113 Assume_Valid, Rec => True)
1115 when LT => return LT;
1116 when LE => return LE;
1117 when EQ => return LE;
1118 when others => null;
1119 end case;
1120 end if;
1121 end if;
1123 -- Next attempt is to see if we have an entity compared with a
1124 -- compile time known value, where there is a current value
1125 -- conditional for the entity which can tell us the result.
1127 declare
1128 Var : Node_Id;
1129 -- Entity variable (left operand)
1131 Val : Uint;
1132 -- Value (right operand)
1134 Inv : Boolean;
1135 -- If False, we have reversed the operands
1137 Op : Node_Kind;
1138 -- Comparison operator kind from Get_Current_Value_Condition call
1140 Opn : Node_Id;
1141 -- Value from Get_Current_Value_Condition call
1143 Opv : Uint;
1144 -- Value of Opn
1146 Result : Compare_Result;
1147 -- Known result before inversion
1149 begin
1150 if Is_Entity_Name (L)
1151 and then Compile_Time_Known_Value (R)
1152 then
1153 Var := L;
1154 Val := Expr_Value (R);
1155 Inv := False;
1157 elsif Is_Entity_Name (R)
1158 and then Compile_Time_Known_Value (L)
1159 then
1160 Var := R;
1161 Val := Expr_Value (L);
1162 Inv := True;
1164 -- That was the last chance at finding a compile time result
1166 else
1167 return Unknown;
1168 end if;
1170 Get_Current_Value_Condition (Var, Op, Opn);
1172 -- That was the last chance, so if we got nothing return
1174 if No (Opn) then
1175 return Unknown;
1176 end if;
1178 Opv := Expr_Value (Opn);
1180 -- We got a comparison, so we might have something interesting
1182 -- Convert LE to LT and GE to GT, just so we have fewer cases
1184 if Op = N_Op_Le then
1185 Op := N_Op_Lt;
1186 Opv := Opv + 1;
1188 elsif Op = N_Op_Ge then
1189 Op := N_Op_Gt;
1190 Opv := Opv - 1;
1191 end if;
1193 -- Deal with equality case
1195 if Op = N_Op_Eq then
1196 if Val = Opv then
1197 Result := EQ;
1198 elsif Opv < Val then
1199 Result := LT;
1200 else
1201 Result := GT;
1202 end if;
1204 -- Deal with inequality case
1206 elsif Op = N_Op_Ne then
1207 if Val = Opv then
1208 Result := NE;
1209 else
1210 return Unknown;
1211 end if;
1213 -- Deal with greater than case
1215 elsif Op = N_Op_Gt then
1216 if Opv >= Val then
1217 Result := GT;
1218 elsif Opv = Val - 1 then
1219 Result := GE;
1220 else
1221 return Unknown;
1222 end if;
1224 -- Deal with less than case
1226 else pragma Assert (Op = N_Op_Lt);
1227 if Opv <= Val then
1228 Result := LT;
1229 elsif Opv = Val + 1 then
1230 Result := LE;
1231 else
1232 return Unknown;
1233 end if;
1234 end if;
1236 -- Deal with inverting result
1238 if Inv then
1239 case Result is
1240 when GT => return LT;
1241 when GE => return LE;
1242 when LT => return GT;
1243 when LE => return GE;
1244 when others => return Result;
1245 end case;
1246 end if;
1248 return Result;
1249 end;
1250 end if;
1251 end Compile_Time_Compare;
1253 -------------------------------
1254 -- Compile_Time_Known_Bounds --
1255 -------------------------------
1257 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1258 Indx : Node_Id;
1259 Typ : Entity_Id;
1261 begin
1262 if T = Any_Composite or else not Is_Array_Type (T) then
1263 return False;
1264 end if;
1266 Indx := First_Index (T);
1267 while Present (Indx) loop
1268 Typ := Underlying_Type (Etype (Indx));
1270 -- Never look at junk bounds of a generic type
1272 if Is_Generic_Type (Typ) then
1273 return False;
1274 end if;
1276 -- Otherwise check bounds for compile time known
1278 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1279 return False;
1280 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1281 return False;
1282 else
1283 Next_Index (Indx);
1284 end if;
1285 end loop;
1287 return True;
1288 end Compile_Time_Known_Bounds;
1290 ------------------------------
1291 -- Compile_Time_Known_Value --
1292 ------------------------------
1294 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1295 K : constant Node_Kind := Nkind (Op);
1296 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1298 begin
1299 -- Never known at compile time if bad type or raises constraint error
1300 -- or empty (latter case occurs only as a result of a previous error).
1302 if No (Op) then
1303 Check_Error_Detected;
1304 return False;
1306 elsif Op = Error
1307 or else Etype (Op) = Any_Type
1308 or else Raises_Constraint_Error (Op)
1309 then
1310 return False;
1311 end if;
1313 -- If we have an entity name, then see if it is the name of a constant
1314 -- and if so, test the corresponding constant value, or the name of
1315 -- an enumeration literal, which is always a constant.
1317 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1318 declare
1319 E : constant Entity_Id := Entity (Op);
1320 V : Node_Id;
1322 begin
1323 -- Never known at compile time if it is a packed array value.
1324 -- We might want to try to evaluate these at compile time one
1325 -- day, but we do not make that attempt now.
1327 if Is_Packed_Array_Type (Etype (Op)) then
1328 return False;
1329 end if;
1331 if Ekind (E) = E_Enumeration_Literal then
1332 return True;
1334 elsif Ekind (E) = E_Constant then
1335 V := Constant_Value (E);
1336 return Present (V) and then Compile_Time_Known_Value (V);
1337 end if;
1338 end;
1340 -- We have a value, see if it is compile time known
1342 else
1343 -- Integer literals are worth storing in the cache
1345 if K = N_Integer_Literal then
1346 CV_Ent.N := Op;
1347 CV_Ent.V := Intval (Op);
1348 return True;
1350 -- Other literals and NULL are known at compile time
1352 elsif
1353 K = N_Character_Literal
1354 or else
1355 K = N_Real_Literal
1356 or else
1357 K = N_String_Literal
1358 or else
1359 K = N_Null
1360 then
1361 return True;
1363 -- Any reference to Null_Parameter is known at compile time. No
1364 -- other attribute references (that have not already been folded)
1365 -- are known at compile time.
1367 elsif K = N_Attribute_Reference then
1368 return Attribute_Name (Op) = Name_Null_Parameter;
1369 end if;
1370 end if;
1372 -- If we fall through, not known at compile time
1374 return False;
1376 -- If we get an exception while trying to do this test, then some error
1377 -- has occurred, and we simply say that the value is not known after all
1379 exception
1380 when others =>
1381 return False;
1382 end Compile_Time_Known_Value;
1384 --------------------------------------
1385 -- Compile_Time_Known_Value_Or_Aggr --
1386 --------------------------------------
1388 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1389 begin
1390 -- If we have an entity name, then see if it is the name of a constant
1391 -- and if so, test the corresponding constant value, or the name of
1392 -- an enumeration literal, which is always a constant.
1394 if Is_Entity_Name (Op) then
1395 declare
1396 E : constant Entity_Id := Entity (Op);
1397 V : Node_Id;
1399 begin
1400 if Ekind (E) = E_Enumeration_Literal then
1401 return True;
1403 elsif Ekind (E) /= E_Constant then
1404 return False;
1406 else
1407 V := Constant_Value (E);
1408 return Present (V)
1409 and then Compile_Time_Known_Value_Or_Aggr (V);
1410 end if;
1411 end;
1413 -- We have a value, see if it is compile time known
1415 else
1416 if Compile_Time_Known_Value (Op) then
1417 return True;
1419 elsif Nkind (Op) = N_Aggregate then
1421 if Present (Expressions (Op)) then
1422 declare
1423 Expr : Node_Id;
1425 begin
1426 Expr := First (Expressions (Op));
1427 while Present (Expr) loop
1428 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1429 return False;
1430 end if;
1432 Next (Expr);
1433 end loop;
1434 end;
1435 end if;
1437 if Present (Component_Associations (Op)) then
1438 declare
1439 Cass : Node_Id;
1441 begin
1442 Cass := First (Component_Associations (Op));
1443 while Present (Cass) loop
1444 if not
1445 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1446 then
1447 return False;
1448 end if;
1450 Next (Cass);
1451 end loop;
1452 end;
1453 end if;
1455 return True;
1457 -- All other types of values are not known at compile time
1459 else
1460 return False;
1461 end if;
1463 end if;
1464 end Compile_Time_Known_Value_Or_Aggr;
1466 ---------------------------------------
1467 -- CRT_Safe_Compile_Time_Known_Value --
1468 ---------------------------------------
1470 function CRT_Safe_Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1471 begin
1472 if (Configurable_Run_Time_Mode or No_Run_Time_Mode)
1473 and then not Is_OK_Static_Expression (Op)
1474 then
1475 return False;
1476 else
1477 return Compile_Time_Known_Value (Op);
1478 end if;
1479 end CRT_Safe_Compile_Time_Known_Value;
1481 -----------------
1482 -- Eval_Actual --
1483 -----------------
1485 -- This is only called for actuals of functions that are not predefined
1486 -- operators (which have already been rewritten as operators at this
1487 -- stage), so the call can never be folded, and all that needs doing for
1488 -- the actual is to do the check for a non-static context.
1490 procedure Eval_Actual (N : Node_Id) is
1491 begin
1492 Check_Non_Static_Context (N);
1493 end Eval_Actual;
1495 --------------------
1496 -- Eval_Allocator --
1497 --------------------
1499 -- Allocators are never static, so all we have to do is to do the
1500 -- check for a non-static context if an expression is present.
1502 procedure Eval_Allocator (N : Node_Id) is
1503 Expr : constant Node_Id := Expression (N);
1505 begin
1506 if Nkind (Expr) = N_Qualified_Expression then
1507 Check_Non_Static_Context (Expression (Expr));
1508 end if;
1509 end Eval_Allocator;
1511 ------------------------
1512 -- Eval_Arithmetic_Op --
1513 ------------------------
1515 -- Arithmetic operations are static functions, so the result is static
1516 -- if both operands are static (RM 4.9(7), 4.9(20)).
1518 procedure Eval_Arithmetic_Op (N : Node_Id) is
1519 Left : constant Node_Id := Left_Opnd (N);
1520 Right : constant Node_Id := Right_Opnd (N);
1521 Ltype : constant Entity_Id := Etype (Left);
1522 Rtype : constant Entity_Id := Etype (Right);
1523 Otype : Entity_Id := Empty;
1524 Stat : Boolean;
1525 Fold : Boolean;
1527 begin
1528 -- If not foldable we are done
1530 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1532 if not Fold then
1533 return;
1534 end if;
1536 -- Otherwise attempt to fold
1538 if Is_Universal_Numeric_Type (Etype (Left))
1539 and then
1540 Is_Universal_Numeric_Type (Etype (Right))
1541 then
1542 Otype := Find_Universal_Operator_Type (N);
1543 end if;
1545 -- Fold for cases where both operands are of integer type
1547 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1548 declare
1549 Left_Int : constant Uint := Expr_Value (Left);
1550 Right_Int : constant Uint := Expr_Value (Right);
1551 Result : Uint;
1553 begin
1554 case Nkind (N) is
1556 when N_Op_Add =>
1557 Result := Left_Int + Right_Int;
1559 when N_Op_Subtract =>
1560 Result := Left_Int - Right_Int;
1562 when N_Op_Multiply =>
1563 if OK_Bits
1564 (N, UI_From_Int
1565 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1566 then
1567 Result := Left_Int * Right_Int;
1568 else
1569 Result := Left_Int;
1570 end if;
1572 when N_Op_Divide =>
1574 -- The exception Constraint_Error is raised by integer
1575 -- division, rem and mod if the right operand is zero.
1577 if Right_Int = 0 then
1578 Apply_Compile_Time_Constraint_Error
1579 (N, "division by zero",
1580 CE_Divide_By_Zero,
1581 Warn => not Stat);
1582 return;
1584 else
1585 Result := Left_Int / Right_Int;
1586 end if;
1588 when N_Op_Mod =>
1590 -- The exception Constraint_Error is raised by integer
1591 -- division, rem and mod if the right operand is zero.
1593 if Right_Int = 0 then
1594 Apply_Compile_Time_Constraint_Error
1595 (N, "mod with zero divisor",
1596 CE_Divide_By_Zero,
1597 Warn => not Stat);
1598 return;
1599 else
1600 Result := Left_Int mod Right_Int;
1601 end if;
1603 when N_Op_Rem =>
1605 -- The exception Constraint_Error is raised by integer
1606 -- division, rem and mod if the right operand is zero.
1608 if Right_Int = 0 then
1609 Apply_Compile_Time_Constraint_Error
1610 (N, "rem with zero divisor",
1611 CE_Divide_By_Zero,
1612 Warn => not Stat);
1613 return;
1615 else
1616 Result := Left_Int rem Right_Int;
1617 end if;
1619 when others =>
1620 raise Program_Error;
1621 end case;
1623 -- Adjust the result by the modulus if the type is a modular type
1625 if Is_Modular_Integer_Type (Ltype) then
1626 Result := Result mod Modulus (Ltype);
1628 -- For a signed integer type, check non-static overflow
1630 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1631 declare
1632 BT : constant Entity_Id := Base_Type (Ltype);
1633 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1634 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1635 begin
1636 if Result < Lo or else Result > Hi then
1637 Apply_Compile_Time_Constraint_Error
1638 (N, "value not in range of }??",
1639 CE_Overflow_Check_Failed,
1640 Ent => BT);
1641 return;
1642 end if;
1643 end;
1644 end if;
1646 -- If we get here we can fold the result
1648 Fold_Uint (N, Result, Stat);
1649 end;
1651 -- Cases where at least one operand is a real. We handle the cases of
1652 -- both reals, or mixed/real integer cases (the latter happen only for
1653 -- divide and multiply, and the result is always real).
1655 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1656 declare
1657 Left_Real : Ureal;
1658 Right_Real : Ureal;
1659 Result : Ureal;
1661 begin
1662 if Is_Real_Type (Ltype) then
1663 Left_Real := Expr_Value_R (Left);
1664 else
1665 Left_Real := UR_From_Uint (Expr_Value (Left));
1666 end if;
1668 if Is_Real_Type (Rtype) then
1669 Right_Real := Expr_Value_R (Right);
1670 else
1671 Right_Real := UR_From_Uint (Expr_Value (Right));
1672 end if;
1674 if Nkind (N) = N_Op_Add then
1675 Result := Left_Real + Right_Real;
1677 elsif Nkind (N) = N_Op_Subtract then
1678 Result := Left_Real - Right_Real;
1680 elsif Nkind (N) = N_Op_Multiply then
1681 Result := Left_Real * Right_Real;
1683 else pragma Assert (Nkind (N) = N_Op_Divide);
1684 if UR_Is_Zero (Right_Real) then
1685 Apply_Compile_Time_Constraint_Error
1686 (N, "division by zero", CE_Divide_By_Zero);
1687 return;
1688 end if;
1690 Result := Left_Real / Right_Real;
1691 end if;
1693 Fold_Ureal (N, Result, Stat);
1694 end;
1695 end if;
1697 -- If the operator was resolved to a specific type, make sure that type
1698 -- is frozen even if the expression is folded into a literal (which has
1699 -- a universal type).
1701 if Present (Otype) then
1702 Freeze_Before (N, Otype);
1703 end if;
1704 end Eval_Arithmetic_Op;
1706 ----------------------------
1707 -- Eval_Character_Literal --
1708 ----------------------------
1710 -- Nothing to be done
1712 procedure Eval_Character_Literal (N : Node_Id) is
1713 pragma Warnings (Off, N);
1714 begin
1715 null;
1716 end Eval_Character_Literal;
1718 ---------------
1719 -- Eval_Call --
1720 ---------------
1722 -- Static function calls are either calls to predefined operators
1723 -- with static arguments, or calls to functions that rename a literal.
1724 -- Only the latter case is handled here, predefined operators are
1725 -- constant-folded elsewhere.
1727 -- If the function is itself inherited (see 7423-001) the literal of
1728 -- the parent type must be explicitly converted to the return type
1729 -- of the function.
1731 procedure Eval_Call (N : Node_Id) is
1732 Loc : constant Source_Ptr := Sloc (N);
1733 Typ : constant Entity_Id := Etype (N);
1734 Lit : Entity_Id;
1736 begin
1737 if Nkind (N) = N_Function_Call
1738 and then No (Parameter_Associations (N))
1739 and then Is_Entity_Name (Name (N))
1740 and then Present (Alias (Entity (Name (N))))
1741 and then Is_Enumeration_Type (Base_Type (Typ))
1742 then
1743 Lit := Ultimate_Alias (Entity (Name (N)));
1745 if Ekind (Lit) = E_Enumeration_Literal then
1746 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1747 Rewrite
1748 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1749 else
1750 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1751 end if;
1753 Resolve (N, Typ);
1754 end if;
1755 end if;
1756 end Eval_Call;
1758 --------------------------
1759 -- Eval_Case_Expression --
1760 --------------------------
1762 -- A conditional expression is static if all its conditions and dependent
1763 -- expressions are static.
1765 procedure Eval_Case_Expression (N : Node_Id) is
1766 Alt : Node_Id;
1767 Choice : Node_Id;
1768 Is_Static : Boolean;
1769 Result : Node_Id;
1770 Val : Uint;
1772 begin
1773 Result := Empty;
1774 Is_Static := True;
1776 if Is_Static_Expression (Expression (N)) then
1777 Val := Expr_Value (Expression (N));
1779 else
1780 Check_Non_Static_Context (Expression (N));
1781 Is_Static := False;
1782 end if;
1784 Alt := First (Alternatives (N));
1786 Search : while Present (Alt) loop
1787 if not Is_Static
1788 or else not Is_Static_Expression (Expression (Alt))
1789 then
1790 Check_Non_Static_Context (Expression (Alt));
1791 Is_Static := False;
1793 else
1794 Choice := First (Discrete_Choices (Alt));
1795 while Present (Choice) loop
1796 if Nkind (Choice) = N_Others_Choice then
1797 Result := Expression (Alt);
1798 exit Search;
1800 elsif Expr_Value (Choice) = Val then
1801 Result := Expression (Alt);
1802 exit Search;
1804 else
1805 Next (Choice);
1806 end if;
1807 end loop;
1808 end if;
1810 Next (Alt);
1811 end loop Search;
1813 if Is_Static then
1814 Rewrite (N, Relocate_Node (Result));
1816 else
1817 Set_Is_Static_Expression (N, False);
1818 end if;
1819 end Eval_Case_Expression;
1821 ------------------------
1822 -- Eval_Concatenation --
1823 ------------------------
1825 -- Concatenation is a static function, so the result is static if both
1826 -- operands are static (RM 4.9(7), 4.9(21)).
1828 procedure Eval_Concatenation (N : Node_Id) is
1829 Left : constant Node_Id := Left_Opnd (N);
1830 Right : constant Node_Id := Right_Opnd (N);
1831 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1832 Stat : Boolean;
1833 Fold : Boolean;
1835 begin
1836 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
1837 -- non-static context.
1839 if Ada_Version = Ada_83
1840 and then Comes_From_Source (N)
1841 then
1842 Check_Non_Static_Context (Left);
1843 Check_Non_Static_Context (Right);
1844 return;
1845 end if;
1847 -- If not foldable we are done. In principle concatenation that yields
1848 -- any string type is static (i.e. an array type of character types).
1849 -- However, character types can include enumeration literals, and
1850 -- concatenation in that case cannot be described by a literal, so we
1851 -- only consider the operation static if the result is an array of
1852 -- (a descendant of) a predefined character type.
1854 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1856 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
1857 Set_Is_Static_Expression (N, False);
1858 return;
1859 end if;
1861 -- Compile time string concatenation
1863 -- ??? Note that operands that are aggregates can be marked as static,
1864 -- so we should attempt at a later stage to fold concatenations with
1865 -- such aggregates.
1867 declare
1868 Left_Str : constant Node_Id := Get_String_Val (Left);
1869 Left_Len : Nat;
1870 Right_Str : constant Node_Id := Get_String_Val (Right);
1871 Folded_Val : String_Id;
1873 begin
1874 -- Establish new string literal, and store left operand. We make
1875 -- sure to use the special Start_String that takes an operand if
1876 -- the left operand is a string literal. Since this is optimized
1877 -- in the case where that is the most recently created string
1878 -- literal, we ensure efficient time/space behavior for the
1879 -- case of a concatenation of a series of string literals.
1881 if Nkind (Left_Str) = N_String_Literal then
1882 Left_Len := String_Length (Strval (Left_Str));
1884 -- If the left operand is the empty string, and the right operand
1885 -- is a string literal (the case of "" & "..."), the result is the
1886 -- value of the right operand. This optimization is important when
1887 -- Is_Folded_In_Parser, to avoid copying an enormous right
1888 -- operand.
1890 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1891 Folded_Val := Strval (Right_Str);
1892 else
1893 Start_String (Strval (Left_Str));
1894 end if;
1896 else
1897 Start_String;
1898 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1899 Left_Len := 1;
1900 end if;
1902 -- Now append the characters of the right operand, unless we
1903 -- optimized the "" & "..." case above.
1905 if Nkind (Right_Str) = N_String_Literal then
1906 if Left_Len /= 0 then
1907 Store_String_Chars (Strval (Right_Str));
1908 Folded_Val := End_String;
1909 end if;
1910 else
1911 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1912 Folded_Val := End_String;
1913 end if;
1915 Set_Is_Static_Expression (N, Stat);
1917 -- If left operand is the empty string, the result is the
1918 -- right operand, including its bounds if anomalous.
1920 if Left_Len = 0
1921 and then Is_Array_Type (Etype (Right))
1922 and then Etype (Right) /= Any_String
1923 then
1924 Set_Etype (N, Etype (Right));
1925 end if;
1927 Fold_Str (N, Folded_Val, Static => Stat);
1928 end;
1929 end Eval_Concatenation;
1931 ----------------------
1932 -- Eval_Entity_Name --
1933 ----------------------
1935 -- This procedure is used for identifiers and expanded names other than
1936 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1937 -- static if they denote a static constant (RM 4.9(6)) or if the name
1938 -- denotes an enumeration literal (RM 4.9(22)).
1940 procedure Eval_Entity_Name (N : Node_Id) is
1941 Def_Id : constant Entity_Id := Entity (N);
1942 Val : Node_Id;
1944 begin
1945 -- Enumeration literals are always considered to be constants
1946 -- and cannot raise constraint error (RM 4.9(22)).
1948 if Ekind (Def_Id) = E_Enumeration_Literal then
1949 Set_Is_Static_Expression (N);
1950 return;
1952 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1953 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1954 -- it does not violate 10.2.1(8) here, since this is not a variable.
1956 elsif Ekind (Def_Id) = E_Constant then
1958 -- Deferred constants must always be treated as nonstatic
1959 -- outside the scope of their full view.
1961 if Present (Full_View (Def_Id))
1962 and then not In_Open_Scopes (Scope (Def_Id))
1963 then
1964 Val := Empty;
1965 else
1966 Val := Constant_Value (Def_Id);
1967 end if;
1969 if Present (Val) then
1970 Set_Is_Static_Expression
1971 (N, Is_Static_Expression (Val)
1972 and then Is_Static_Subtype (Etype (Def_Id)));
1973 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1975 if not Is_Static_Expression (N)
1976 and then not Is_Generic_Type (Etype (N))
1977 then
1978 Validate_Static_Object_Name (N);
1979 end if;
1981 return;
1982 end if;
1983 end if;
1985 -- Fall through if the name is not static
1987 Validate_Static_Object_Name (N);
1988 end Eval_Entity_Name;
1990 ------------------------
1991 -- Eval_If_Expression --
1992 ------------------------
1994 -- We can fold to a static expression if the condition and both dependent
1995 -- expressions are static. Otherwise, the only required processing is to do
1996 -- the check for non-static context for the then and else expressions.
1998 procedure Eval_If_Expression (N : Node_Id) is
1999 Condition : constant Node_Id := First (Expressions (N));
2000 Then_Expr : constant Node_Id := Next (Condition);
2001 Else_Expr : constant Node_Id := Next (Then_Expr);
2002 Result : Node_Id;
2003 Non_Result : Node_Id;
2005 Rstat : constant Boolean :=
2006 Is_Static_Expression (Condition)
2007 and then
2008 Is_Static_Expression (Then_Expr)
2009 and then
2010 Is_Static_Expression (Else_Expr);
2012 begin
2013 -- If any operand is Any_Type, just propagate to result and do not try
2014 -- to fold, this prevents cascaded errors.
2016 if Etype (Condition) = Any_Type or else
2017 Etype (Then_Expr) = Any_Type or else
2018 Etype (Else_Expr) = Any_Type
2019 then
2020 Set_Etype (N, Any_Type);
2021 Set_Is_Static_Expression (N, False);
2022 return;
2024 -- Static case where we can fold. Note that we don't try to fold cases
2025 -- where the condition is known at compile time, but the result is
2026 -- non-static. This avoids possible cases of infinite recursion where
2027 -- the expander puts in a redundant test and we remove it. Instead we
2028 -- deal with these cases in the expander.
2030 elsif Rstat then
2032 -- Select result operand
2034 if Is_True (Expr_Value (Condition)) then
2035 Result := Then_Expr;
2036 Non_Result := Else_Expr;
2037 else
2038 Result := Else_Expr;
2039 Non_Result := Then_Expr;
2040 end if;
2042 -- Note that it does not matter if the non-result operand raises a
2043 -- Constraint_Error, but if the result raises constraint error then
2044 -- we replace the node with a raise constraint error. This will
2045 -- properly propagate Raises_Constraint_Error since this flag is
2046 -- set in Result.
2048 if Raises_Constraint_Error (Result) then
2049 Rewrite_In_Raise_CE (N, Result);
2050 Check_Non_Static_Context (Non_Result);
2052 -- Otherwise the result operand replaces the original node
2054 else
2055 Rewrite (N, Relocate_Node (Result));
2056 end if;
2058 -- Case of condition not known at compile time
2060 else
2061 Check_Non_Static_Context (Condition);
2062 Check_Non_Static_Context (Then_Expr);
2063 Check_Non_Static_Context (Else_Expr);
2064 end if;
2066 Set_Is_Static_Expression (N, Rstat);
2067 end Eval_If_Expression;
2069 ----------------------------
2070 -- Eval_Indexed_Component --
2071 ----------------------------
2073 -- Indexed components are never static, so we need to perform the check
2074 -- for non-static context on the index values. Then, we check if the
2075 -- value can be obtained at compile time, even though it is non-static.
2077 procedure Eval_Indexed_Component (N : Node_Id) is
2078 Expr : Node_Id;
2080 begin
2081 -- Check for non-static context on index values
2083 Expr := First (Expressions (N));
2084 while Present (Expr) loop
2085 Check_Non_Static_Context (Expr);
2086 Next (Expr);
2087 end loop;
2089 -- If the indexed component appears in an object renaming declaration
2090 -- then we do not want to try to evaluate it, since in this case we
2091 -- need the identity of the array element.
2093 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2094 return;
2096 -- Similarly if the indexed component appears as the prefix of an
2097 -- attribute we don't want to evaluate it, because at least for
2098 -- some cases of attributes we need the identify (e.g. Access, Size)
2100 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2101 return;
2102 end if;
2104 -- Note: there are other cases, such as the left side of an assignment,
2105 -- or an OUT parameter for a call, where the replacement results in the
2106 -- illegal use of a constant, But these cases are illegal in the first
2107 -- place, so the replacement, though silly, is harmless.
2109 -- Now see if this is a constant array reference
2111 if List_Length (Expressions (N)) = 1
2112 and then Is_Entity_Name (Prefix (N))
2113 and then Ekind (Entity (Prefix (N))) = E_Constant
2114 and then Present (Constant_Value (Entity (Prefix (N))))
2115 then
2116 declare
2117 Loc : constant Source_Ptr := Sloc (N);
2118 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2119 Sub : constant Node_Id := First (Expressions (N));
2121 Atyp : Entity_Id;
2122 -- Type of array
2124 Lin : Nat;
2125 -- Linear one's origin subscript value for array reference
2127 Lbd : Node_Id;
2128 -- Lower bound of the first array index
2130 Elm : Node_Id;
2131 -- Value from constant array
2133 begin
2134 Atyp := Etype (Arr);
2136 if Is_Access_Type (Atyp) then
2137 Atyp := Designated_Type (Atyp);
2138 end if;
2140 -- If we have an array type (we should have but perhaps there are
2141 -- error cases where this is not the case), then see if we can do
2142 -- a constant evaluation of the array reference.
2144 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2145 if Ekind (Atyp) = E_String_Literal_Subtype then
2146 Lbd := String_Literal_Low_Bound (Atyp);
2147 else
2148 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2149 end if;
2151 if Compile_Time_Known_Value (Sub)
2152 and then Nkind (Arr) = N_Aggregate
2153 and then Compile_Time_Known_Value (Lbd)
2154 and then Is_Discrete_Type (Component_Type (Atyp))
2155 then
2156 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2158 if List_Length (Expressions (Arr)) >= Lin then
2159 Elm := Pick (Expressions (Arr), Lin);
2161 -- If the resulting expression is compile time known,
2162 -- then we can rewrite the indexed component with this
2163 -- value, being sure to mark the result as non-static.
2164 -- We also reset the Sloc, in case this generates an
2165 -- error later on (e.g. 136'Access).
2167 if Compile_Time_Known_Value (Elm) then
2168 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2169 Set_Is_Static_Expression (N, False);
2170 Set_Sloc (N, Loc);
2171 end if;
2172 end if;
2174 -- We can also constant-fold if the prefix is a string literal.
2175 -- This will be useful in an instantiation or an inlining.
2177 elsif Compile_Time_Known_Value (Sub)
2178 and then Nkind (Arr) = N_String_Literal
2179 and then Compile_Time_Known_Value (Lbd)
2180 and then Expr_Value (Lbd) = 1
2181 and then Expr_Value (Sub) <=
2182 String_Literal_Length (Etype (Arr))
2183 then
2184 declare
2185 C : constant Char_Code :=
2186 Get_String_Char (Strval (Arr),
2187 UI_To_Int (Expr_Value (Sub)));
2188 begin
2189 Set_Character_Literal_Name (C);
2191 Elm :=
2192 Make_Character_Literal (Loc,
2193 Chars => Name_Find,
2194 Char_Literal_Value => UI_From_CC (C));
2195 Set_Etype (Elm, Component_Type (Atyp));
2196 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2197 Set_Is_Static_Expression (N, False);
2198 end;
2199 end if;
2200 end if;
2201 end;
2202 end if;
2203 end Eval_Indexed_Component;
2205 --------------------------
2206 -- Eval_Integer_Literal --
2207 --------------------------
2209 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2210 -- as static by the analyzer. The reason we did it that early is to allow
2211 -- the possibility of turning off the Is_Static_Expression flag after
2212 -- analysis, but before resolution, when integer literals are generated in
2213 -- the expander that do not correspond to static expressions.
2215 procedure Eval_Integer_Literal (N : Node_Id) is
2216 T : constant Entity_Id := Etype (N);
2218 function In_Any_Integer_Context return Boolean;
2219 -- If the literal is resolved with a specific type in a context where
2220 -- the expected type is Any_Integer, there are no range checks on the
2221 -- literal. By the time the literal is evaluated, it carries the type
2222 -- imposed by the enclosing expression, and we must recover the context
2223 -- to determine that Any_Integer is meant.
2225 ----------------------------
2226 -- In_Any_Integer_Context --
2227 ----------------------------
2229 function In_Any_Integer_Context return Boolean is
2230 Par : constant Node_Id := Parent (N);
2231 K : constant Node_Kind := Nkind (Par);
2233 begin
2234 -- Any_Integer also appears in digits specifications for real types,
2235 -- but those have bounds smaller that those of any integer base type,
2236 -- so we can safely ignore these cases.
2238 return K = N_Number_Declaration
2239 or else K = N_Attribute_Reference
2240 or else K = N_Attribute_Definition_Clause
2241 or else K = N_Modular_Type_Definition
2242 or else K = N_Signed_Integer_Type_Definition;
2243 end In_Any_Integer_Context;
2245 -- Start of processing for Eval_Integer_Literal
2247 begin
2249 -- If the literal appears in a non-expression context, then it is
2250 -- certainly appearing in a non-static context, so check it. This is
2251 -- actually a redundant check, since Check_Non_Static_Context would
2252 -- check it, but it seems worth while avoiding the call.
2254 if Nkind (Parent (N)) not in N_Subexpr
2255 and then not In_Any_Integer_Context
2256 then
2257 Check_Non_Static_Context (N);
2258 end if;
2260 -- Modular integer literals must be in their base range
2262 if Is_Modular_Integer_Type (T)
2263 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
2264 then
2265 Out_Of_Range (N);
2266 end if;
2267 end Eval_Integer_Literal;
2269 ---------------------
2270 -- Eval_Logical_Op --
2271 ---------------------
2273 -- Logical operations are static functions, so the result is potentially
2274 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2276 procedure Eval_Logical_Op (N : Node_Id) is
2277 Left : constant Node_Id := Left_Opnd (N);
2278 Right : constant Node_Id := Right_Opnd (N);
2279 Stat : Boolean;
2280 Fold : Boolean;
2282 begin
2283 -- If not foldable we are done
2285 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2287 if not Fold then
2288 return;
2289 end if;
2291 -- Compile time evaluation of logical operation
2293 declare
2294 Left_Int : constant Uint := Expr_Value (Left);
2295 Right_Int : constant Uint := Expr_Value (Right);
2297 begin
2298 -- VMS includes bitwise operations on signed types
2300 if Is_Modular_Integer_Type (Etype (N))
2301 or else Is_VMS_Operator (Entity (N))
2302 then
2303 declare
2304 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2305 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2307 begin
2308 To_Bits (Left_Int, Left_Bits);
2309 To_Bits (Right_Int, Right_Bits);
2311 -- Note: should really be able to use array ops instead of
2312 -- these loops, but they weren't working at the time ???
2314 if Nkind (N) = N_Op_And then
2315 for J in Left_Bits'Range loop
2316 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2317 end loop;
2319 elsif Nkind (N) = N_Op_Or then
2320 for J in Left_Bits'Range loop
2321 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2322 end loop;
2324 else
2325 pragma Assert (Nkind (N) = N_Op_Xor);
2327 for J in Left_Bits'Range loop
2328 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2329 end loop;
2330 end if;
2332 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2333 end;
2335 else
2336 pragma Assert (Is_Boolean_Type (Etype (N)));
2338 if Nkind (N) = N_Op_And then
2339 Fold_Uint (N,
2340 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2342 elsif Nkind (N) = N_Op_Or then
2343 Fold_Uint (N,
2344 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2346 else
2347 pragma Assert (Nkind (N) = N_Op_Xor);
2348 Fold_Uint (N,
2349 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2350 end if;
2351 end if;
2352 end;
2353 end Eval_Logical_Op;
2355 ------------------------
2356 -- Eval_Membership_Op --
2357 ------------------------
2359 -- A membership test is potentially static if the expression is static, and
2360 -- the range is a potentially static range, or is a subtype mark denoting a
2361 -- static subtype (RM 4.9(12)).
2363 procedure Eval_Membership_Op (N : Node_Id) is
2364 Left : constant Node_Id := Left_Opnd (N);
2365 Right : constant Node_Id := Right_Opnd (N);
2366 Def_Id : Entity_Id;
2367 Lo : Node_Id;
2368 Hi : Node_Id;
2369 Result : Boolean;
2370 Stat : Boolean;
2371 Fold : Boolean;
2373 begin
2374 -- Ignore if error in either operand, except to make sure that Any_Type
2375 -- is properly propagated to avoid junk cascaded errors.
2377 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2378 Set_Etype (N, Any_Type);
2379 return;
2380 end if;
2382 -- Ignore if types involved have predicates
2384 if Present (Predicate_Function (Etype (Left)))
2385 or else
2386 Present (Predicate_Function (Etype (Right)))
2387 then
2388 return;
2389 end if;
2391 -- Case of right operand is a subtype name
2393 if Is_Entity_Name (Right) then
2394 Def_Id := Entity (Right);
2396 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
2397 and then Is_OK_Static_Subtype (Def_Id)
2398 then
2399 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2401 if not Fold or else not Stat then
2402 return;
2403 end if;
2404 else
2405 Check_Non_Static_Context (Left);
2406 return;
2407 end if;
2409 -- For string membership tests we will check the length further on
2411 if not Is_String_Type (Def_Id) then
2412 Lo := Type_Low_Bound (Def_Id);
2413 Hi := Type_High_Bound (Def_Id);
2415 else
2416 Lo := Empty;
2417 Hi := Empty;
2418 end if;
2420 -- Case of right operand is a range
2422 else
2423 if Is_Static_Range (Right) then
2424 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2426 if not Fold or else not Stat then
2427 return;
2429 -- If one bound of range raises CE, then don't try to fold
2431 elsif not Is_OK_Static_Range (Right) then
2432 Check_Non_Static_Context (Left);
2433 return;
2434 end if;
2436 else
2437 Check_Non_Static_Context (Left);
2438 return;
2439 end if;
2441 -- Here we know range is an OK static range
2443 Lo := Low_Bound (Right);
2444 Hi := High_Bound (Right);
2445 end if;
2447 -- For strings we check that the length of the string expression is
2448 -- compatible with the string subtype if the subtype is constrained,
2449 -- or if unconstrained then the test is always true.
2451 if Is_String_Type (Etype (Right)) then
2452 if not Is_Constrained (Etype (Right)) then
2453 Result := True;
2455 else
2456 declare
2457 Typlen : constant Uint := String_Type_Len (Etype (Right));
2458 Strlen : constant Uint :=
2459 UI_From_Int
2460 (String_Length (Strval (Get_String_Val (Left))));
2461 begin
2462 Result := (Typlen = Strlen);
2463 end;
2464 end if;
2466 -- Fold the membership test. We know we have a static range and Lo and
2467 -- Hi are set to the expressions for the end points of this range.
2469 elsif Is_Real_Type (Etype (Right)) then
2470 declare
2471 Leftval : constant Ureal := Expr_Value_R (Left);
2473 begin
2474 Result := Expr_Value_R (Lo) <= Leftval
2475 and then Leftval <= Expr_Value_R (Hi);
2476 end;
2478 else
2479 declare
2480 Leftval : constant Uint := Expr_Value (Left);
2482 begin
2483 Result := Expr_Value (Lo) <= Leftval
2484 and then Leftval <= Expr_Value (Hi);
2485 end;
2486 end if;
2488 if Nkind (N) = N_Not_In then
2489 Result := not Result;
2490 end if;
2492 Fold_Uint (N, Test (Result), True);
2494 Warn_On_Known_Condition (N);
2495 end Eval_Membership_Op;
2497 ------------------------
2498 -- Eval_Named_Integer --
2499 ------------------------
2501 procedure Eval_Named_Integer (N : Node_Id) is
2502 begin
2503 Fold_Uint (N,
2504 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2505 end Eval_Named_Integer;
2507 ---------------------
2508 -- Eval_Named_Real --
2509 ---------------------
2511 procedure Eval_Named_Real (N : Node_Id) is
2512 begin
2513 Fold_Ureal (N,
2514 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2515 end Eval_Named_Real;
2517 -------------------
2518 -- Eval_Op_Expon --
2519 -------------------
2521 -- Exponentiation is a static functions, so the result is potentially
2522 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2524 procedure Eval_Op_Expon (N : Node_Id) is
2525 Left : constant Node_Id := Left_Opnd (N);
2526 Right : constant Node_Id := Right_Opnd (N);
2527 Stat : Boolean;
2528 Fold : Boolean;
2530 begin
2531 -- If not foldable we are done
2533 Test_Expression_Is_Foldable
2534 (N, Left, Right, Stat, Fold, CRT_Safe => True);
2536 -- Return if not foldable
2538 if not Fold then
2539 return;
2540 end if;
2542 if Configurable_Run_Time_Mode and not Stat then
2543 return;
2544 end if;
2546 -- Fold exponentiation operation
2548 declare
2549 Right_Int : constant Uint := Expr_Value (Right);
2551 begin
2552 -- Integer case
2554 if Is_Integer_Type (Etype (Left)) then
2555 declare
2556 Left_Int : constant Uint := Expr_Value (Left);
2557 Result : Uint;
2559 begin
2560 -- Exponentiation of an integer raises Constraint_Error for a
2561 -- negative exponent (RM 4.5.6).
2563 if Right_Int < 0 then
2564 Apply_Compile_Time_Constraint_Error
2565 (N, "integer exponent negative",
2566 CE_Range_Check_Failed,
2567 Warn => not Stat);
2568 return;
2570 else
2571 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2572 Result := Left_Int ** Right_Int;
2573 else
2574 Result := Left_Int;
2575 end if;
2577 if Is_Modular_Integer_Type (Etype (N)) then
2578 Result := Result mod Modulus (Etype (N));
2579 end if;
2581 Fold_Uint (N, Result, Stat);
2582 end if;
2583 end;
2585 -- Real case
2587 else
2588 declare
2589 Left_Real : constant Ureal := Expr_Value_R (Left);
2591 begin
2592 -- Cannot have a zero base with a negative exponent
2594 if UR_Is_Zero (Left_Real) then
2596 if Right_Int < 0 then
2597 Apply_Compile_Time_Constraint_Error
2598 (N, "zero ** negative integer",
2599 CE_Range_Check_Failed,
2600 Warn => not Stat);
2601 return;
2602 else
2603 Fold_Ureal (N, Ureal_0, Stat);
2604 end if;
2606 else
2607 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2608 end if;
2609 end;
2610 end if;
2611 end;
2612 end Eval_Op_Expon;
2614 -----------------
2615 -- Eval_Op_Not --
2616 -----------------
2618 -- The not operation is a static functions, so the result is potentially
2619 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2621 procedure Eval_Op_Not (N : Node_Id) is
2622 Right : constant Node_Id := Right_Opnd (N);
2623 Stat : Boolean;
2624 Fold : Boolean;
2626 begin
2627 -- If not foldable we are done
2629 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2631 if not Fold then
2632 return;
2633 end if;
2635 -- Fold not operation
2637 declare
2638 Rint : constant Uint := Expr_Value (Right);
2639 Typ : constant Entity_Id := Etype (N);
2641 begin
2642 -- Negation is equivalent to subtracting from the modulus minus one.
2643 -- For a binary modulus this is equivalent to the ones-complement of
2644 -- the original value. For non-binary modulus this is an arbitrary
2645 -- but consistent definition.
2647 if Is_Modular_Integer_Type (Typ) then
2648 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2650 else
2651 pragma Assert (Is_Boolean_Type (Typ));
2652 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2653 end if;
2655 Set_Is_Static_Expression (N, Stat);
2656 end;
2657 end Eval_Op_Not;
2659 -------------------------------
2660 -- Eval_Qualified_Expression --
2661 -------------------------------
2663 -- A qualified expression is potentially static if its subtype mark denotes
2664 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2666 procedure Eval_Qualified_Expression (N : Node_Id) is
2667 Operand : constant Node_Id := Expression (N);
2668 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2670 Stat : Boolean;
2671 Fold : Boolean;
2672 Hex : Boolean;
2674 begin
2675 -- Can only fold if target is string or scalar and subtype is static.
2676 -- Also, do not fold if our parent is an allocator (this is because the
2677 -- qualified expression is really part of the syntactic structure of an
2678 -- allocator, and we do not want to end up with something that
2679 -- corresponds to "new 1" where the 1 is the result of folding a
2680 -- qualified expression).
2682 if not Is_Static_Subtype (Target_Type)
2683 or else Nkind (Parent (N)) = N_Allocator
2684 then
2685 Check_Non_Static_Context (Operand);
2687 -- If operand is known to raise constraint_error, set the flag on the
2688 -- expression so it does not get optimized away.
2690 if Nkind (Operand) = N_Raise_Constraint_Error then
2691 Set_Raises_Constraint_Error (N);
2692 end if;
2694 return;
2695 end if;
2697 -- If not foldable we are done
2699 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2701 if not Fold then
2702 return;
2704 -- Don't try fold if target type has constraint error bounds
2706 elsif not Is_OK_Static_Subtype (Target_Type) then
2707 Set_Raises_Constraint_Error (N);
2708 return;
2709 end if;
2711 -- Here we will fold, save Print_In_Hex indication
2713 Hex := Nkind (Operand) = N_Integer_Literal
2714 and then Print_In_Hex (Operand);
2716 -- Fold the result of qualification
2718 if Is_Discrete_Type (Target_Type) then
2719 Fold_Uint (N, Expr_Value (Operand), Stat);
2721 -- Preserve Print_In_Hex indication
2723 if Hex and then Nkind (N) = N_Integer_Literal then
2724 Set_Print_In_Hex (N);
2725 end if;
2727 elsif Is_Real_Type (Target_Type) then
2728 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2730 else
2731 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2733 if not Stat then
2734 Set_Is_Static_Expression (N, False);
2735 else
2736 Check_String_Literal_Length (N, Target_Type);
2737 end if;
2739 return;
2740 end if;
2742 -- The expression may be foldable but not static
2744 Set_Is_Static_Expression (N, Stat);
2746 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
2747 Out_Of_Range (N);
2748 end if;
2749 end Eval_Qualified_Expression;
2751 -----------------------
2752 -- Eval_Real_Literal --
2753 -----------------------
2755 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2756 -- as static by the analyzer. The reason we did it that early is to allow
2757 -- the possibility of turning off the Is_Static_Expression flag after
2758 -- analysis, but before resolution, when integer literals are generated
2759 -- in the expander that do not correspond to static expressions.
2761 procedure Eval_Real_Literal (N : Node_Id) is
2762 PK : constant Node_Kind := Nkind (Parent (N));
2764 begin
2765 -- If the literal appears in a non-expression context and not as part of
2766 -- a number declaration, then it is appearing in a non-static context,
2767 -- so check it.
2769 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
2770 Check_Non_Static_Context (N);
2771 end if;
2772 end Eval_Real_Literal;
2774 ------------------------
2775 -- Eval_Relational_Op --
2776 ------------------------
2778 -- Relational operations are static functions, so the result is static if
2779 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
2780 -- the result is never static, even if the operands are.
2782 procedure Eval_Relational_Op (N : Node_Id) is
2783 Left : constant Node_Id := Left_Opnd (N);
2784 Right : constant Node_Id := Right_Opnd (N);
2785 Typ : constant Entity_Id := Etype (Left);
2786 Otype : Entity_Id := Empty;
2787 Result : Boolean;
2789 begin
2790 -- One special case to deal with first. If we can tell that the result
2791 -- will be false because the lengths of one or more index subtypes are
2792 -- compile time known and different, then we can replace the entire
2793 -- result by False. We only do this for one dimensional arrays, because
2794 -- the case of multi-dimensional arrays is rare and too much trouble. If
2795 -- one of the operands is an illegal aggregate, its type might still be
2796 -- an arbitrary composite type, so nothing to do.
2798 if Is_Array_Type (Typ)
2799 and then Typ /= Any_Composite
2800 and then Number_Dimensions (Typ) = 1
2801 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
2802 then
2803 if Raises_Constraint_Error (Left)
2804 or else Raises_Constraint_Error (Right)
2805 then
2806 return;
2807 end if;
2809 -- OK, we have the case where we may be able to do this fold
2811 Length_Mismatch : declare
2812 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2813 -- If Op is an expression for a constrained array with a known at
2814 -- compile time length, then Len is set to this (non-negative
2815 -- length). Otherwise Len is set to minus 1.
2817 -----------------------
2818 -- Get_Static_Length --
2819 -----------------------
2821 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2822 T : Entity_Id;
2824 begin
2825 -- First easy case string literal
2827 if Nkind (Op) = N_String_Literal then
2828 Len := UI_From_Int (String_Length (Strval (Op)));
2829 return;
2830 end if;
2832 -- Second easy case, not constrained subtype, so no length
2834 if not Is_Constrained (Etype (Op)) then
2835 Len := Uint_Minus_1;
2836 return;
2837 end if;
2839 -- General case
2841 T := Etype (First_Index (Etype (Op)));
2843 -- The simple case, both bounds are known at compile time
2845 if Is_Discrete_Type (T)
2846 and then
2847 Compile_Time_Known_Value (Type_Low_Bound (T))
2848 and then
2849 Compile_Time_Known_Value (Type_High_Bound (T))
2850 then
2851 Len := UI_Max (Uint_0,
2852 Expr_Value (Type_High_Bound (T)) -
2853 Expr_Value (Type_Low_Bound (T)) + 1);
2854 return;
2855 end if;
2857 -- A more complex case, where the bounds are of the form
2858 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2859 -- either A'First or A'Last (with A an entity name), or X is an
2860 -- entity name, and the two X's are the same and K1 and K2 are
2861 -- known at compile time, in this case, the length can also be
2862 -- computed at compile time, even though the bounds are not
2863 -- known. A common case of this is e.g. (X'First .. X'First+5).
2865 Extract_Length : declare
2866 procedure Decompose_Expr
2867 (Expr : Node_Id;
2868 Ent : out Entity_Id;
2869 Kind : out Character;
2870 Cons : out Uint);
2871 -- Given an expression, see if is of the form above,
2872 -- X [+/- K]. If so Ent is set to the entity in X,
2873 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2874 -- and Cons is the value of K. If the expression is
2875 -- not of the required form, Ent is set to Empty.
2877 --------------------
2878 -- Decompose_Expr --
2879 --------------------
2881 procedure Decompose_Expr
2882 (Expr : Node_Id;
2883 Ent : out Entity_Id;
2884 Kind : out Character;
2885 Cons : out Uint)
2887 Exp : Node_Id;
2889 begin
2890 if Nkind (Expr) = N_Op_Add
2891 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2892 then
2893 Exp := Left_Opnd (Expr);
2894 Cons := Expr_Value (Right_Opnd (Expr));
2896 elsif Nkind (Expr) = N_Op_Subtract
2897 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2898 then
2899 Exp := Left_Opnd (Expr);
2900 Cons := -Expr_Value (Right_Opnd (Expr));
2902 -- If the bound is a constant created to remove side
2903 -- effects, recover original expression to see if it has
2904 -- one of the recognizable forms.
2906 elsif Nkind (Expr) = N_Identifier
2907 and then not Comes_From_Source (Entity (Expr))
2908 and then Ekind (Entity (Expr)) = E_Constant
2909 and then
2910 Nkind (Parent (Entity (Expr))) = N_Object_Declaration
2911 then
2912 Exp := Expression (Parent (Entity (Expr)));
2913 Decompose_Expr (Exp, Ent, Kind, Cons);
2915 -- If original expression includes an entity, create a
2916 -- reference to it for use below.
2918 if Present (Ent) then
2919 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
2920 end if;
2922 else
2923 Exp := Expr;
2924 Cons := Uint_0;
2925 end if;
2927 -- At this stage Exp is set to the potential X
2929 if Nkind (Exp) = N_Attribute_Reference then
2930 if Attribute_Name (Exp) = Name_First then
2931 Kind := 'F';
2933 elsif Attribute_Name (Exp) = Name_Last then
2934 Kind := 'L';
2936 else
2937 Ent := Empty;
2938 return;
2939 end if;
2941 Exp := Prefix (Exp);
2943 else
2944 Kind := 'E';
2945 end if;
2947 if Is_Entity_Name (Exp)
2948 and then Present (Entity (Exp))
2949 then
2950 Ent := Entity (Exp);
2951 else
2952 Ent := Empty;
2953 end if;
2954 end Decompose_Expr;
2956 -- Local Variables
2958 Ent1, Ent2 : Entity_Id;
2959 Kind1, Kind2 : Character;
2960 Cons1, Cons2 : Uint;
2962 -- Start of processing for Extract_Length
2964 begin
2965 Decompose_Expr
2966 (Original_Node (Type_Low_Bound (T)), Ent1, Kind1, Cons1);
2967 Decompose_Expr
2968 (Original_Node (Type_High_Bound (T)), Ent2, Kind2, Cons2);
2970 if Present (Ent1)
2971 and then Kind1 = Kind2
2972 and then Ent1 = Ent2
2973 then
2974 Len := Cons2 - Cons1 + 1;
2975 else
2976 Len := Uint_Minus_1;
2977 end if;
2978 end Extract_Length;
2979 end Get_Static_Length;
2981 -- Local Variables
2983 Len_L : Uint;
2984 Len_R : Uint;
2986 -- Start of processing for Length_Mismatch
2988 begin
2989 Get_Static_Length (Left, Len_L);
2990 Get_Static_Length (Right, Len_R);
2992 if Len_L /= Uint_Minus_1
2993 and then Len_R /= Uint_Minus_1
2994 and then Len_L /= Len_R
2995 then
2996 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2997 Warn_On_Known_Condition (N);
2998 return;
2999 end if;
3000 end Length_Mismatch;
3001 end if;
3003 declare
3004 Is_Static_Expression : Boolean;
3005 Is_Foldable : Boolean;
3006 pragma Unreferenced (Is_Foldable);
3008 begin
3009 -- Initialize the value of Is_Static_Expression. The value of
3010 -- Is_Foldable returned by Test_Expression_Is_Foldable is not needed
3011 -- since, even when some operand is a variable, we can still perform
3012 -- the static evaluation of the expression in some cases (for
3013 -- example, for a variable of a subtype of Integer we statically
3014 -- know that any value stored in such variable is smaller than
3015 -- Integer'Last).
3017 Test_Expression_Is_Foldable
3018 (N, Left, Right, Is_Static_Expression, Is_Foldable);
3020 -- Only comparisons of scalars can give static results. In
3021 -- particular, comparisons of strings never yield a static
3022 -- result, even if both operands are static strings.
3024 if not Is_Scalar_Type (Typ) then
3025 Is_Static_Expression := False;
3026 Set_Is_Static_Expression (N, False);
3027 end if;
3029 -- For operators on universal numeric types called as functions with
3030 -- an explicit scope, determine appropriate specific numeric type,
3031 -- and diagnose possible ambiguity.
3033 if Is_Universal_Numeric_Type (Etype (Left))
3034 and then
3035 Is_Universal_Numeric_Type (Etype (Right))
3036 then
3037 Otype := Find_Universal_Operator_Type (N);
3038 end if;
3040 -- For static real type expressions, we cannot use
3041 -- Compile_Time_Compare since it worries about run-time
3042 -- results which are not exact.
3044 if Is_Static_Expression and then Is_Real_Type (Typ) then
3045 declare
3046 Left_Real : constant Ureal := Expr_Value_R (Left);
3047 Right_Real : constant Ureal := Expr_Value_R (Right);
3049 begin
3050 case Nkind (N) is
3051 when N_Op_Eq => Result := (Left_Real = Right_Real);
3052 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3053 when N_Op_Lt => Result := (Left_Real < Right_Real);
3054 when N_Op_Le => Result := (Left_Real <= Right_Real);
3055 when N_Op_Gt => Result := (Left_Real > Right_Real);
3056 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3058 when others =>
3059 raise Program_Error;
3060 end case;
3062 Fold_Uint (N, Test (Result), True);
3063 end;
3065 -- For all other cases, we use Compile_Time_Compare to do the compare
3067 else
3068 declare
3069 CR : constant Compare_Result :=
3070 Compile_Time_Compare
3071 (Left, Right, Assume_Valid => False);
3073 begin
3074 if CR = Unknown then
3075 return;
3076 end if;
3078 case Nkind (N) is
3079 when N_Op_Eq =>
3080 if CR = EQ then
3081 Result := True;
3082 elsif CR = NE or else CR = GT or else CR = LT then
3083 Result := False;
3084 else
3085 return;
3086 end if;
3088 when N_Op_Ne =>
3089 if CR = NE or else CR = GT or else CR = LT then
3090 Result := True;
3091 elsif CR = EQ then
3092 Result := False;
3093 else
3094 return;
3095 end if;
3097 when N_Op_Lt =>
3098 if CR = LT then
3099 Result := True;
3100 elsif CR = EQ or else CR = GT or else CR = GE then
3101 Result := False;
3102 else
3103 return;
3104 end if;
3106 when N_Op_Le =>
3107 if CR = LT or else CR = EQ or else CR = LE then
3108 Result := True;
3109 elsif CR = GT then
3110 Result := False;
3111 else
3112 return;
3113 end if;
3115 when N_Op_Gt =>
3116 if CR = GT then
3117 Result := True;
3118 elsif CR = EQ or else CR = LT or else CR = LE then
3119 Result := False;
3120 else
3121 return;
3122 end if;
3124 when N_Op_Ge =>
3125 if CR = GT or else CR = EQ or else CR = GE then
3126 Result := True;
3127 elsif CR = LT then
3128 Result := False;
3129 else
3130 return;
3131 end if;
3133 when others =>
3134 raise Program_Error;
3135 end case;
3136 end;
3138 Fold_Uint (N, Test (Result), Is_Static_Expression);
3139 end if;
3140 end;
3142 -- For the case of a folded relational operator on a specific numeric
3143 -- type, freeze operand type now.
3145 if Present (Otype) then
3146 Freeze_Before (N, Otype);
3147 end if;
3149 Warn_On_Known_Condition (N);
3150 end Eval_Relational_Op;
3152 ----------------
3153 -- Eval_Shift --
3154 ----------------
3156 -- Shift operations are intrinsic operations that can never be static, so
3157 -- the only processing required is to perform the required check for a non
3158 -- static context for the two operands.
3160 -- Actually we could do some compile time evaluation here some time ???
3162 procedure Eval_Shift (N : Node_Id) is
3163 begin
3164 Check_Non_Static_Context (Left_Opnd (N));
3165 Check_Non_Static_Context (Right_Opnd (N));
3166 end Eval_Shift;
3168 ------------------------
3169 -- Eval_Short_Circuit --
3170 ------------------------
3172 -- A short circuit operation is potentially static if both operands are
3173 -- potentially static (RM 4.9 (13)).
3175 procedure Eval_Short_Circuit (N : Node_Id) is
3176 Kind : constant Node_Kind := Nkind (N);
3177 Left : constant Node_Id := Left_Opnd (N);
3178 Right : constant Node_Id := Right_Opnd (N);
3179 Left_Int : Uint;
3181 Rstat : constant Boolean :=
3182 Is_Static_Expression (Left)
3183 and then
3184 Is_Static_Expression (Right);
3186 begin
3187 -- Short circuit operations are never static in Ada 83
3189 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3190 Check_Non_Static_Context (Left);
3191 Check_Non_Static_Context (Right);
3192 return;
3193 end if;
3195 -- Now look at the operands, we can't quite use the normal call to
3196 -- Test_Expression_Is_Foldable here because short circuit operations
3197 -- are a special case, they can still be foldable, even if the right
3198 -- operand raises constraint error.
3200 -- If either operand is Any_Type, just propagate to result and do not
3201 -- try to fold, this prevents cascaded errors.
3203 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3204 Set_Etype (N, Any_Type);
3205 return;
3207 -- If left operand raises constraint error, then replace node N with
3208 -- the raise constraint error node, and we are obviously not foldable.
3209 -- Is_Static_Expression is set from the two operands in the normal way,
3210 -- and we check the right operand if it is in a non-static context.
3212 elsif Raises_Constraint_Error (Left) then
3213 if not Rstat then
3214 Check_Non_Static_Context (Right);
3215 end if;
3217 Rewrite_In_Raise_CE (N, Left);
3218 Set_Is_Static_Expression (N, Rstat);
3219 return;
3221 -- If the result is not static, then we won't in any case fold
3223 elsif not Rstat then
3224 Check_Non_Static_Context (Left);
3225 Check_Non_Static_Context (Right);
3226 return;
3227 end if;
3229 -- Here the result is static, note that, unlike the normal processing
3230 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3231 -- the right operand raises constraint error, that's because it is not
3232 -- significant if the left operand is decisive.
3234 Set_Is_Static_Expression (N);
3236 -- It does not matter if the right operand raises constraint error if
3237 -- it will not be evaluated. So deal specially with the cases where
3238 -- the right operand is not evaluated. Note that we will fold these
3239 -- cases even if the right operand is non-static, which is fine, but
3240 -- of course in these cases the result is not potentially static.
3242 Left_Int := Expr_Value (Left);
3244 if (Kind = N_And_Then and then Is_False (Left_Int))
3245 or else
3246 (Kind = N_Or_Else and then Is_True (Left_Int))
3247 then
3248 Fold_Uint (N, Left_Int, Rstat);
3249 return;
3250 end if;
3252 -- If first operand not decisive, then it does matter if the right
3253 -- operand raises constraint error, since it will be evaluated, so
3254 -- we simply replace the node with the right operand. Note that this
3255 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3256 -- (both are set to True in Right).
3258 if Raises_Constraint_Error (Right) then
3259 Rewrite_In_Raise_CE (N, Right);
3260 Check_Non_Static_Context (Left);
3261 return;
3262 end if;
3264 -- Otherwise the result depends on the right operand
3266 Fold_Uint (N, Expr_Value (Right), Rstat);
3267 return;
3268 end Eval_Short_Circuit;
3270 ----------------
3271 -- Eval_Slice --
3272 ----------------
3274 -- Slices can never be static, so the only processing required is to check
3275 -- for non-static context if an explicit range is given.
3277 procedure Eval_Slice (N : Node_Id) is
3278 Drange : constant Node_Id := Discrete_Range (N);
3279 begin
3280 if Nkind (Drange) = N_Range then
3281 Check_Non_Static_Context (Low_Bound (Drange));
3282 Check_Non_Static_Context (High_Bound (Drange));
3283 end if;
3285 -- A slice of the form A (subtype), when the subtype is the index of
3286 -- the type of A, is redundant, the slice can be replaced with A, and
3287 -- this is worth a warning.
3289 if Is_Entity_Name (Prefix (N)) then
3290 declare
3291 E : constant Entity_Id := Entity (Prefix (N));
3292 T : constant Entity_Id := Etype (E);
3293 begin
3294 if Ekind (E) = E_Constant
3295 and then Is_Array_Type (T)
3296 and then Is_Entity_Name (Drange)
3297 then
3298 if Is_Entity_Name (Original_Node (First_Index (T)))
3299 and then Entity (Original_Node (First_Index (T)))
3300 = Entity (Drange)
3301 then
3302 if Warn_On_Redundant_Constructs then
3303 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3304 end if;
3306 -- The following might be a useful optimization???
3308 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3309 end if;
3310 end if;
3311 end;
3312 end if;
3313 end Eval_Slice;
3315 ---------------------------------
3316 -- Eval_Static_Predicate_Check --
3317 ---------------------------------
3319 function Eval_Static_Predicate_Check
3320 (N : Node_Id;
3321 Typ : Entity_Id) return Boolean
3323 Loc : constant Source_Ptr := Sloc (N);
3324 Pred : constant List_Id := Static_Predicate (Typ);
3325 Test : Node_Id;
3327 begin
3328 if No (Pred) then
3329 return True;
3330 end if;
3332 -- The static predicate is a list of alternatives in the proper format
3333 -- for an Ada 2012 membership test. If the argument is a literal, the
3334 -- membership test can be evaluated statically. The caller transforms
3335 -- a result of False into a static contraint error.
3337 Test := Make_In (Loc,
3338 Left_Opnd => New_Copy_Tree (N),
3339 Right_Opnd => Empty,
3340 Alternatives => Pred);
3341 Analyze_And_Resolve (Test, Standard_Boolean);
3343 return Nkind (Test) = N_Identifier
3344 and then Entity (Test) = Standard_True;
3345 end Eval_Static_Predicate_Check;
3347 -------------------------
3348 -- Eval_String_Literal --
3349 -------------------------
3351 procedure Eval_String_Literal (N : Node_Id) is
3352 Typ : constant Entity_Id := Etype (N);
3353 Bas : constant Entity_Id := Base_Type (Typ);
3354 Xtp : Entity_Id;
3355 Len : Nat;
3356 Lo : Node_Id;
3358 begin
3359 -- Nothing to do if error type (handles cases like default expressions
3360 -- or generics where we have not yet fully resolved the type).
3362 if Bas = Any_Type or else Bas = Any_String then
3363 return;
3364 end if;
3366 -- String literals are static if the subtype is static (RM 4.9(2)), so
3367 -- reset the static expression flag (it was set unconditionally in
3368 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3369 -- the subtype is static by looking at the lower bound.
3371 if Ekind (Typ) = E_String_Literal_Subtype then
3372 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3373 Set_Is_Static_Expression (N, False);
3374 return;
3375 end if;
3377 -- Here if Etype of string literal is normal Etype (not yet possible,
3378 -- but may be possible in future).
3380 elsif not Is_OK_Static_Expression
3381 (Type_Low_Bound (Etype (First_Index (Typ))))
3382 then
3383 Set_Is_Static_Expression (N, False);
3384 return;
3385 end if;
3387 -- If original node was a type conversion, then result if non-static
3389 if Nkind (Original_Node (N)) = N_Type_Conversion then
3390 Set_Is_Static_Expression (N, False);
3391 return;
3392 end if;
3394 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3395 -- if its bounds are outside the index base type and this index type is
3396 -- static. This can happen in only two ways. Either the string literal
3397 -- is too long, or it is null, and the lower bound is type'First. In
3398 -- either case it is the upper bound that is out of range of the index
3399 -- type.
3400 if Ada_Version >= Ada_95 then
3401 if Root_Type (Bas) = Standard_String
3402 or else
3403 Root_Type (Bas) = Standard_Wide_String
3404 or else
3405 Root_Type (Bas) = Standard_Wide_Wide_String
3406 then
3407 Xtp := Standard_Positive;
3408 else
3409 Xtp := Etype (First_Index (Bas));
3410 end if;
3412 if Ekind (Typ) = E_String_Literal_Subtype then
3413 Lo := String_Literal_Low_Bound (Typ);
3414 else
3415 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3416 end if;
3418 -- Check for string too long
3420 Len := String_Length (Strval (N));
3422 if UI_From_Int (Len) > String_Type_Len (Bas) then
3424 -- Issue message. Note that this message is a warning if the
3425 -- string literal is not marked as static (happens in some cases
3426 -- of folding strings known at compile time, but not static).
3427 -- Furthermore in such cases, we reword the message, since there
3428 -- is no string literal in the source program.
3430 if Is_Static_Expression (N) then
3431 Apply_Compile_Time_Constraint_Error
3432 (N, "string literal too long for}", CE_Length_Check_Failed,
3433 Ent => Bas,
3434 Typ => First_Subtype (Bas));
3435 else
3436 Apply_Compile_Time_Constraint_Error
3437 (N, "string value too long for}", CE_Length_Check_Failed,
3438 Ent => Bas,
3439 Typ => First_Subtype (Bas),
3440 Warn => True);
3441 end if;
3443 -- Test for null string not allowed
3445 elsif Len = 0
3446 and then not Is_Generic_Type (Xtp)
3447 and then
3448 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3449 then
3450 -- Same specialization of message
3452 if Is_Static_Expression (N) then
3453 Apply_Compile_Time_Constraint_Error
3454 (N, "null string literal not allowed for}",
3455 CE_Length_Check_Failed,
3456 Ent => Bas,
3457 Typ => First_Subtype (Bas));
3458 else
3459 Apply_Compile_Time_Constraint_Error
3460 (N, "null string value not allowed for}",
3461 CE_Length_Check_Failed,
3462 Ent => Bas,
3463 Typ => First_Subtype (Bas),
3464 Warn => True);
3465 end if;
3466 end if;
3467 end if;
3468 end Eval_String_Literal;
3470 --------------------------
3471 -- Eval_Type_Conversion --
3472 --------------------------
3474 -- A type conversion is potentially static if its subtype mark is for a
3475 -- static scalar subtype, and its operand expression is potentially static
3476 -- (RM 4.9(10)).
3478 procedure Eval_Type_Conversion (N : Node_Id) is
3479 Operand : constant Node_Id := Expression (N);
3480 Source_Type : constant Entity_Id := Etype (Operand);
3481 Target_Type : constant Entity_Id := Etype (N);
3483 Stat : Boolean;
3484 Fold : Boolean;
3486 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3487 -- Returns true if type T is an integer type, or if it is a fixed-point
3488 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3489 -- on the conversion node).
3491 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3492 -- Returns true if type T is a floating-point type, or if it is a
3493 -- fixed-point type that is not to be treated as an integer (i.e. the
3494 -- flag Conversion_OK is not set on the conversion node).
3496 ------------------------------
3497 -- To_Be_Treated_As_Integer --
3498 ------------------------------
3500 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3501 begin
3502 return
3503 Is_Integer_Type (T)
3504 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3505 end To_Be_Treated_As_Integer;
3507 ---------------------------
3508 -- To_Be_Treated_As_Real --
3509 ---------------------------
3511 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3512 begin
3513 return
3514 Is_Floating_Point_Type (T)
3515 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3516 end To_Be_Treated_As_Real;
3518 -- Start of processing for Eval_Type_Conversion
3520 begin
3521 -- Cannot fold if target type is non-static or if semantic error
3523 if not Is_Static_Subtype (Target_Type) then
3524 Check_Non_Static_Context (Operand);
3525 return;
3527 elsif Error_Posted (N) then
3528 return;
3529 end if;
3531 -- If not foldable we are done
3533 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3535 if not Fold then
3536 return;
3538 -- Don't try fold if target type has constraint error bounds
3540 elsif not Is_OK_Static_Subtype (Target_Type) then
3541 Set_Raises_Constraint_Error (N);
3542 return;
3543 end if;
3545 -- Remaining processing depends on operand types. Note that in the
3546 -- following type test, fixed-point counts as real unless the flag
3547 -- Conversion_OK is set, in which case it counts as integer.
3549 -- Fold conversion, case of string type. The result is not static
3551 if Is_String_Type (Target_Type) then
3552 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3554 return;
3556 -- Fold conversion, case of integer target type
3558 elsif To_Be_Treated_As_Integer (Target_Type) then
3559 declare
3560 Result : Uint;
3562 begin
3563 -- Integer to integer conversion
3565 if To_Be_Treated_As_Integer (Source_Type) then
3566 Result := Expr_Value (Operand);
3568 -- Real to integer conversion
3570 else
3571 Result := UR_To_Uint (Expr_Value_R (Operand));
3572 end if;
3574 -- If fixed-point type (Conversion_OK must be set), then the
3575 -- result is logically an integer, but we must replace the
3576 -- conversion with the corresponding real literal, since the
3577 -- type from a semantic point of view is still fixed-point.
3579 if Is_Fixed_Point_Type (Target_Type) then
3580 Fold_Ureal
3581 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3583 -- Otherwise result is integer literal
3585 else
3586 Fold_Uint (N, Result, Stat);
3587 end if;
3588 end;
3590 -- Fold conversion, case of real target type
3592 elsif To_Be_Treated_As_Real (Target_Type) then
3593 declare
3594 Result : Ureal;
3596 begin
3597 if To_Be_Treated_As_Real (Source_Type) then
3598 Result := Expr_Value_R (Operand);
3599 else
3600 Result := UR_From_Uint (Expr_Value (Operand));
3601 end if;
3603 Fold_Ureal (N, Result, Stat);
3604 end;
3606 -- Enumeration types
3608 else
3609 Fold_Uint (N, Expr_Value (Operand), Stat);
3610 end if;
3612 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3613 Out_Of_Range (N);
3614 end if;
3616 end Eval_Type_Conversion;
3618 -------------------
3619 -- Eval_Unary_Op --
3620 -------------------
3622 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3623 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3625 procedure Eval_Unary_Op (N : Node_Id) is
3626 Right : constant Node_Id := Right_Opnd (N);
3627 Otype : Entity_Id := Empty;
3628 Stat : Boolean;
3629 Fold : Boolean;
3631 begin
3632 -- If not foldable we are done
3634 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3636 if not Fold then
3637 return;
3638 end if;
3640 if Etype (Right) = Universal_Integer
3641 or else
3642 Etype (Right) = Universal_Real
3643 then
3644 Otype := Find_Universal_Operator_Type (N);
3645 end if;
3647 -- Fold for integer case
3649 if Is_Integer_Type (Etype (N)) then
3650 declare
3651 Rint : constant Uint := Expr_Value (Right);
3652 Result : Uint;
3654 begin
3655 -- In the case of modular unary plus and abs there is no need
3656 -- to adjust the result of the operation since if the original
3657 -- operand was in bounds the result will be in the bounds of the
3658 -- modular type. However, in the case of modular unary minus the
3659 -- result may go out of the bounds of the modular type and needs
3660 -- adjustment.
3662 if Nkind (N) = N_Op_Plus then
3663 Result := Rint;
3665 elsif Nkind (N) = N_Op_Minus then
3666 if Is_Modular_Integer_Type (Etype (N)) then
3667 Result := (-Rint) mod Modulus (Etype (N));
3668 else
3669 Result := (-Rint);
3670 end if;
3672 else
3673 pragma Assert (Nkind (N) = N_Op_Abs);
3674 Result := abs Rint;
3675 end if;
3677 Fold_Uint (N, Result, Stat);
3678 end;
3680 -- Fold for real case
3682 elsif Is_Real_Type (Etype (N)) then
3683 declare
3684 Rreal : constant Ureal := Expr_Value_R (Right);
3685 Result : Ureal;
3687 begin
3688 if Nkind (N) = N_Op_Plus then
3689 Result := Rreal;
3691 elsif Nkind (N) = N_Op_Minus then
3692 Result := UR_Negate (Rreal);
3694 else
3695 pragma Assert (Nkind (N) = N_Op_Abs);
3696 Result := abs Rreal;
3697 end if;
3699 Fold_Ureal (N, Result, Stat);
3700 end;
3701 end if;
3703 -- If the operator was resolved to a specific type, make sure that type
3704 -- is frozen even if the expression is folded into a literal (which has
3705 -- a universal type).
3707 if Present (Otype) then
3708 Freeze_Before (N, Otype);
3709 end if;
3710 end Eval_Unary_Op;
3712 -------------------------------
3713 -- Eval_Unchecked_Conversion --
3714 -------------------------------
3716 -- Unchecked conversions can never be static, so the only required
3717 -- processing is to check for a non-static context for the operand.
3719 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3720 begin
3721 Check_Non_Static_Context (Expression (N));
3722 end Eval_Unchecked_Conversion;
3724 --------------------
3725 -- Expr_Rep_Value --
3726 --------------------
3728 function Expr_Rep_Value (N : Node_Id) return Uint is
3729 Kind : constant Node_Kind := Nkind (N);
3730 Ent : Entity_Id;
3732 begin
3733 if Is_Entity_Name (N) then
3734 Ent := Entity (N);
3736 -- An enumeration literal that was either in the source or created
3737 -- as a result of static evaluation.
3739 if Ekind (Ent) = E_Enumeration_Literal then
3740 return Enumeration_Rep (Ent);
3742 -- A user defined static constant
3744 else
3745 pragma Assert (Ekind (Ent) = E_Constant);
3746 return Expr_Rep_Value (Constant_Value (Ent));
3747 end if;
3749 -- An integer literal that was either in the source or created as a
3750 -- result of static evaluation.
3752 elsif Kind = N_Integer_Literal then
3753 return Intval (N);
3755 -- A real literal for a fixed-point type. This must be the fixed-point
3756 -- case, either the literal is of a fixed-point type, or it is a bound
3757 -- of a fixed-point type, with type universal real. In either case we
3758 -- obtain the desired value from Corresponding_Integer_Value.
3760 elsif Kind = N_Real_Literal then
3761 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3762 return Corresponding_Integer_Value (N);
3764 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3766 elsif Kind = N_Attribute_Reference
3767 and then Attribute_Name (N) = Name_Null_Parameter
3768 then
3769 return Uint_0;
3771 -- Otherwise must be character literal
3773 else
3774 pragma Assert (Kind = N_Character_Literal);
3775 Ent := Entity (N);
3777 -- Since Character literals of type Standard.Character don't have any
3778 -- defining character literals built for them, they do not have their
3779 -- Entity set, so just use their Char code. Otherwise for user-
3780 -- defined character literals use their Pos value as usual which is
3781 -- the same as the Rep value.
3783 if No (Ent) then
3784 return Char_Literal_Value (N);
3785 else
3786 return Enumeration_Rep (Ent);
3787 end if;
3788 end if;
3789 end Expr_Rep_Value;
3791 ----------------
3792 -- Expr_Value --
3793 ----------------
3795 function Expr_Value (N : Node_Id) return Uint is
3796 Kind : constant Node_Kind := Nkind (N);
3797 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
3798 Ent : Entity_Id;
3799 Val : Uint;
3801 begin
3802 -- If already in cache, then we know it's compile time known and we can
3803 -- return the value that was previously stored in the cache since
3804 -- compile time known values cannot change.
3806 if CV_Ent.N = N then
3807 return CV_Ent.V;
3808 end if;
3810 -- Otherwise proceed to test value
3812 if Is_Entity_Name (N) then
3813 Ent := Entity (N);
3815 -- An enumeration literal that was either in the source or created as
3816 -- a result of static evaluation.
3818 if Ekind (Ent) = E_Enumeration_Literal then
3819 Val := Enumeration_Pos (Ent);
3821 -- A user defined static constant
3823 else
3824 pragma Assert (Ekind (Ent) = E_Constant);
3825 Val := Expr_Value (Constant_Value (Ent));
3826 end if;
3828 -- An integer literal that was either in the source or created as a
3829 -- result of static evaluation.
3831 elsif Kind = N_Integer_Literal then
3832 Val := Intval (N);
3834 -- A real literal for a fixed-point type. This must be the fixed-point
3835 -- case, either the literal is of a fixed-point type, or it is a bound
3836 -- of a fixed-point type, with type universal real. In either case we
3837 -- obtain the desired value from Corresponding_Integer_Value.
3839 elsif Kind = N_Real_Literal then
3841 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3842 Val := Corresponding_Integer_Value (N);
3844 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3846 elsif Kind = N_Attribute_Reference
3847 and then Attribute_Name (N) = Name_Null_Parameter
3848 then
3849 Val := Uint_0;
3851 -- Otherwise must be character literal
3853 else
3854 pragma Assert (Kind = N_Character_Literal);
3855 Ent := Entity (N);
3857 -- Since Character literals of type Standard.Character don't
3858 -- have any defining character literals built for them, they
3859 -- do not have their Entity set, so just use their Char
3860 -- code. Otherwise for user-defined character literals use
3861 -- their Pos value as usual.
3863 if No (Ent) then
3864 Val := Char_Literal_Value (N);
3865 else
3866 Val := Enumeration_Pos (Ent);
3867 end if;
3868 end if;
3870 -- Come here with Val set to value to be returned, set cache
3872 CV_Ent.N := N;
3873 CV_Ent.V := Val;
3874 return Val;
3875 end Expr_Value;
3877 ------------------
3878 -- Expr_Value_E --
3879 ------------------
3881 function Expr_Value_E (N : Node_Id) return Entity_Id is
3882 Ent : constant Entity_Id := Entity (N);
3884 begin
3885 if Ekind (Ent) = E_Enumeration_Literal then
3886 return Ent;
3887 else
3888 pragma Assert (Ekind (Ent) = E_Constant);
3889 return Expr_Value_E (Constant_Value (Ent));
3890 end if;
3891 end Expr_Value_E;
3893 ------------------
3894 -- Expr_Value_R --
3895 ------------------
3897 function Expr_Value_R (N : Node_Id) return Ureal is
3898 Kind : constant Node_Kind := Nkind (N);
3899 Ent : Entity_Id;
3901 begin
3902 if Kind = N_Real_Literal then
3903 return Realval (N);
3905 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3906 Ent := Entity (N);
3907 pragma Assert (Ekind (Ent) = E_Constant);
3908 return Expr_Value_R (Constant_Value (Ent));
3910 elsif Kind = N_Integer_Literal then
3911 return UR_From_Uint (Expr_Value (N));
3913 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3915 elsif Kind = N_Attribute_Reference
3916 and then Attribute_Name (N) = Name_Null_Parameter
3917 then
3918 return Ureal_0;
3919 end if;
3921 -- If we fall through, we have a node that cannot be interpreted as a
3922 -- compile time constant. That is definitely an error.
3924 raise Program_Error;
3925 end Expr_Value_R;
3927 ------------------
3928 -- Expr_Value_S --
3929 ------------------
3931 function Expr_Value_S (N : Node_Id) return Node_Id is
3932 begin
3933 if Nkind (N) = N_String_Literal then
3934 return N;
3935 else
3936 pragma Assert (Ekind (Entity (N)) = E_Constant);
3937 return Expr_Value_S (Constant_Value (Entity (N)));
3938 end if;
3939 end Expr_Value_S;
3941 ----------------------------------
3942 -- Find_Universal_Operator_Type --
3943 ----------------------------------
3945 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
3946 PN : constant Node_Id := Parent (N);
3947 Call : constant Node_Id := Original_Node (N);
3948 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
3950 Is_Fix : constant Boolean :=
3951 Nkind (N) in N_Binary_Op
3952 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
3953 -- A mixed-mode operation in this context indicates the presence of
3954 -- fixed-point type in the designated package.
3956 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
3957 -- Case where N is a relational (or membership) operator (else it is an
3958 -- arithmetic one).
3960 In_Membership : constant Boolean :=
3961 Nkind (PN) in N_Membership_Test
3962 and then
3963 Nkind (Right_Opnd (PN)) = N_Range
3964 and then
3965 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
3966 and then
3967 Is_Universal_Numeric_Type
3968 (Etype (Low_Bound (Right_Opnd (PN))))
3969 and then
3970 Is_Universal_Numeric_Type
3971 (Etype (High_Bound (Right_Opnd (PN))));
3972 -- Case where N is part of a membership test with a universal range
3974 E : Entity_Id;
3975 Pack : Entity_Id;
3976 Typ1 : Entity_Id := Empty;
3977 Priv_E : Entity_Id;
3979 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
3980 -- Check whether one operand is a mixed-mode operation that requires the
3981 -- presence of a fixed-point type. Given that all operands are universal
3982 -- and have been constant-folded, retrieve the original function call.
3984 ---------------------------
3985 -- Is_Mixed_Mode_Operand --
3986 ---------------------------
3988 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
3989 Onod : constant Node_Id := Original_Node (Op);
3990 begin
3991 return Nkind (Onod) = N_Function_Call
3992 and then Present (Next_Actual (First_Actual (Onod)))
3993 and then Etype (First_Actual (Onod)) /=
3994 Etype (Next_Actual (First_Actual (Onod)));
3995 end Is_Mixed_Mode_Operand;
3997 -- Start of processing for Find_Universal_Operator_Type
3999 begin
4000 if Nkind (Call) /= N_Function_Call
4001 or else Nkind (Name (Call)) /= N_Expanded_Name
4002 then
4003 return Empty;
4005 -- There are several cases where the context does not imply the type of
4006 -- the operands:
4007 -- - the universal expression appears in a type conversion;
4008 -- - the expression is a relational operator applied to universal
4009 -- operands;
4010 -- - the expression is a membership test with a universal operand
4011 -- and a range with universal bounds.
4013 elsif Nkind (Parent (N)) = N_Type_Conversion
4014 or else Is_Relational
4015 or else In_Membership
4016 then
4017 Pack := Entity (Prefix (Name (Call)));
4019 -- If the prefix is a package declared elsewhere, iterate over its
4020 -- visible entities, otherwise iterate over all declarations in the
4021 -- designated scope.
4023 if Ekind (Pack) = E_Package
4024 and then not In_Open_Scopes (Pack)
4025 then
4026 Priv_E := First_Private_Entity (Pack);
4027 else
4028 Priv_E := Empty;
4029 end if;
4031 Typ1 := Empty;
4032 E := First_Entity (Pack);
4033 while Present (E) and then E /= Priv_E loop
4034 if Is_Numeric_Type (E)
4035 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4036 and then Comes_From_Source (E)
4037 and then Is_Integer_Type (E) = Is_Int
4038 and then
4039 (Nkind (N) in N_Unary_Op
4040 or else Is_Relational
4041 or else Is_Fixed_Point_Type (E) = Is_Fix)
4042 then
4043 if No (Typ1) then
4044 Typ1 := E;
4046 -- Before emitting an error, check for the presence of a
4047 -- mixed-mode operation that specifies a fixed point type.
4049 elsif Is_Relational
4050 and then
4051 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4052 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4053 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4055 then
4056 if Is_Fixed_Point_Type (E) then
4057 Typ1 := E;
4058 end if;
4060 else
4061 -- More than one type of the proper class declared in P
4063 Error_Msg_N ("ambiguous operation", N);
4064 Error_Msg_Sloc := Sloc (Typ1);
4065 Error_Msg_N ("\possible interpretation (inherited)#", N);
4066 Error_Msg_Sloc := Sloc (E);
4067 Error_Msg_N ("\possible interpretation (inherited)#", N);
4068 return Empty;
4069 end if;
4070 end if;
4072 Next_Entity (E);
4073 end loop;
4074 end if;
4076 return Typ1;
4077 end Find_Universal_Operator_Type;
4079 --------------------------
4080 -- Flag_Non_Static_Expr --
4081 --------------------------
4083 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4084 begin
4085 if Error_Posted (Expr) and then not All_Errors_Mode then
4086 return;
4087 else
4088 Error_Msg_F (Msg, Expr);
4089 Why_Not_Static (Expr);
4090 end if;
4091 end Flag_Non_Static_Expr;
4093 --------------
4094 -- Fold_Str --
4095 --------------
4097 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4098 Loc : constant Source_Ptr := Sloc (N);
4099 Typ : constant Entity_Id := Etype (N);
4101 begin
4102 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4104 -- We now have the literal with the right value, both the actual type
4105 -- and the expected type of this literal are taken from the expression
4106 -- that was evaluated. So now we do the Analyze and Resolve.
4108 -- Note that we have to reset Is_Static_Expression both after the
4109 -- analyze step (because Resolve will evaluate the literal, which
4110 -- will cause semantic errors if it is marked as static), and after
4111 -- the Resolve step (since Resolve in some cases resets this flag).
4113 Analyze (N);
4114 Set_Is_Static_Expression (N, Static);
4115 Set_Etype (N, Typ);
4116 Resolve (N);
4117 Set_Is_Static_Expression (N, Static);
4118 end Fold_Str;
4120 ---------------
4121 -- Fold_Uint --
4122 ---------------
4124 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4125 Loc : constant Source_Ptr := Sloc (N);
4126 Typ : Entity_Id := Etype (N);
4127 Ent : Entity_Id;
4129 begin
4130 -- If we are folding a named number, retain the entity in the literal,
4131 -- for ASIS use.
4133 if Is_Entity_Name (N)
4134 and then Ekind (Entity (N)) = E_Named_Integer
4135 then
4136 Ent := Entity (N);
4137 else
4138 Ent := Empty;
4139 end if;
4141 if Is_Private_Type (Typ) then
4142 Typ := Full_View (Typ);
4143 end if;
4145 -- For a result of type integer, substitute an N_Integer_Literal node
4146 -- for the result of the compile time evaluation of the expression.
4147 -- For ASIS use, set a link to the original named number when not in
4148 -- a generic context.
4150 if Is_Integer_Type (Typ) then
4151 Rewrite (N, Make_Integer_Literal (Loc, Val));
4153 Set_Original_Entity (N, Ent);
4155 -- Otherwise we have an enumeration type, and we substitute either
4156 -- an N_Identifier or N_Character_Literal to represent the enumeration
4157 -- literal corresponding to the given value, which must always be in
4158 -- range, because appropriate tests have already been made for this.
4160 else pragma Assert (Is_Enumeration_Type (Typ));
4161 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4162 end if;
4164 -- We now have the literal with the right value, both the actual type
4165 -- and the expected type of this literal are taken from the expression
4166 -- that was evaluated. So now we do the Analyze and Resolve.
4168 -- Note that we have to reset Is_Static_Expression both after the
4169 -- analyze step (because Resolve will evaluate the literal, which
4170 -- will cause semantic errors if it is marked as static), and after
4171 -- the Resolve step (since Resolve in some cases sets this flag).
4173 Analyze (N);
4174 Set_Is_Static_Expression (N, Static);
4175 Set_Etype (N, Typ);
4176 Resolve (N);
4177 Set_Is_Static_Expression (N, Static);
4178 end Fold_Uint;
4180 ----------------
4181 -- Fold_Ureal --
4182 ----------------
4184 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4185 Loc : constant Source_Ptr := Sloc (N);
4186 Typ : constant Entity_Id := Etype (N);
4187 Ent : Entity_Id;
4189 begin
4190 -- If we are folding a named number, retain the entity in the literal,
4191 -- for ASIS use.
4193 if Is_Entity_Name (N)
4194 and then Ekind (Entity (N)) = E_Named_Real
4195 then
4196 Ent := Entity (N);
4197 else
4198 Ent := Empty;
4199 end if;
4201 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4203 -- Set link to original named number, for ASIS use
4205 Set_Original_Entity (N, Ent);
4207 -- We now have the literal with the right value, both the actual type
4208 -- and the expected type of this literal are taken from the expression
4209 -- that was evaluated. So now we do the Analyze and Resolve.
4211 -- Note that we have to reset Is_Static_Expression both after the
4212 -- analyze step (because Resolve will evaluate the literal, which
4213 -- will cause semantic errors if it is marked as static), and after
4214 -- the Resolve step (since Resolve in some cases sets this flag).
4216 Analyze (N);
4217 Set_Is_Static_Expression (N, Static);
4218 Set_Etype (N, Typ);
4219 Resolve (N);
4220 Set_Is_Static_Expression (N, Static);
4221 end Fold_Ureal;
4223 ---------------
4224 -- From_Bits --
4225 ---------------
4227 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4228 V : Uint := Uint_0;
4230 begin
4231 for J in 0 .. B'Last loop
4232 if B (J) then
4233 V := V + 2 ** J;
4234 end if;
4235 end loop;
4237 if Non_Binary_Modulus (T) then
4238 V := V mod Modulus (T);
4239 end if;
4241 return V;
4242 end From_Bits;
4244 --------------------
4245 -- Get_String_Val --
4246 --------------------
4248 function Get_String_Val (N : Node_Id) return Node_Id is
4249 begin
4250 if Nkind (N) = N_String_Literal then
4251 return N;
4253 elsif Nkind (N) = N_Character_Literal then
4254 return N;
4256 else
4257 pragma Assert (Is_Entity_Name (N));
4258 return Get_String_Val (Constant_Value (Entity (N)));
4259 end if;
4260 end Get_String_Val;
4262 ----------------
4263 -- Initialize --
4264 ----------------
4266 procedure Initialize is
4267 begin
4268 CV_Cache := (others => (Node_High_Bound, Uint_0));
4269 end Initialize;
4271 --------------------
4272 -- In_Subrange_Of --
4273 --------------------
4275 function In_Subrange_Of
4276 (T1 : Entity_Id;
4277 T2 : Entity_Id;
4278 Fixed_Int : Boolean := False) return Boolean
4280 L1 : Node_Id;
4281 H1 : Node_Id;
4283 L2 : Node_Id;
4284 H2 : Node_Id;
4286 begin
4287 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4288 return True;
4290 -- Never in range if both types are not scalar. Don't know if this can
4291 -- actually happen, but just in case.
4293 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4294 return False;
4296 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4297 -- definitely not compatible with T2.
4299 elsif Is_Floating_Point_Type (T1)
4300 and then Has_Infinities (T1)
4301 and then Is_Floating_Point_Type (T2)
4302 and then not Has_Infinities (T2)
4303 then
4304 return False;
4306 else
4307 L1 := Type_Low_Bound (T1);
4308 H1 := Type_High_Bound (T1);
4310 L2 := Type_Low_Bound (T2);
4311 H2 := Type_High_Bound (T2);
4313 -- Check bounds to see if comparison possible at compile time
4315 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4316 and then
4317 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4318 then
4319 return True;
4320 end if;
4322 -- If bounds not comparable at compile time, then the bounds of T2
4323 -- must be compile time known or we cannot answer the query.
4325 if not Compile_Time_Known_Value (L2)
4326 or else not Compile_Time_Known_Value (H2)
4327 then
4328 return False;
4329 end if;
4331 -- If the bounds of T1 are know at compile time then use these
4332 -- ones, otherwise use the bounds of the base type (which are of
4333 -- course always static).
4335 if not Compile_Time_Known_Value (L1) then
4336 L1 := Type_Low_Bound (Base_Type (T1));
4337 end if;
4339 if not Compile_Time_Known_Value (H1) then
4340 H1 := Type_High_Bound (Base_Type (T1));
4341 end if;
4343 -- Fixed point types should be considered as such only if
4344 -- flag Fixed_Int is set to False.
4346 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4347 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4348 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4349 then
4350 return
4351 Expr_Value_R (L2) <= Expr_Value_R (L1)
4352 and then
4353 Expr_Value_R (H2) >= Expr_Value_R (H1);
4355 else
4356 return
4357 Expr_Value (L2) <= Expr_Value (L1)
4358 and then
4359 Expr_Value (H2) >= Expr_Value (H1);
4361 end if;
4362 end if;
4364 -- If any exception occurs, it means that we have some bug in the compiler
4365 -- possibly triggered by a previous error, or by some unforeseen peculiar
4366 -- occurrence. However, this is only an optimization attempt, so there is
4367 -- really no point in crashing the compiler. Instead we just decide, too
4368 -- bad, we can't figure out the answer in this case after all.
4370 exception
4371 when others =>
4373 -- Debug flag K disables this behavior (useful for debugging)
4375 if Debug_Flag_K then
4376 raise;
4377 else
4378 return False;
4379 end if;
4380 end In_Subrange_Of;
4382 -----------------
4383 -- Is_In_Range --
4384 -----------------
4386 function Is_In_Range
4387 (N : Node_Id;
4388 Typ : Entity_Id;
4389 Assume_Valid : Boolean := False;
4390 Fixed_Int : Boolean := False;
4391 Int_Real : Boolean := False) return Boolean
4393 begin
4394 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4395 = In_Range;
4396 end Is_In_Range;
4398 -------------------
4399 -- Is_Null_Range --
4400 -------------------
4402 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4403 Typ : constant Entity_Id := Etype (Lo);
4405 begin
4406 if not Compile_Time_Known_Value (Lo)
4407 or else not Compile_Time_Known_Value (Hi)
4408 then
4409 return False;
4410 end if;
4412 if Is_Discrete_Type (Typ) then
4413 return Expr_Value (Lo) > Expr_Value (Hi);
4415 else
4416 pragma Assert (Is_Real_Type (Typ));
4417 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4418 end if;
4419 end Is_Null_Range;
4421 -----------------------------
4422 -- Is_OK_Static_Expression --
4423 -----------------------------
4425 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4426 begin
4427 return Is_Static_Expression (N)
4428 and then not Raises_Constraint_Error (N);
4429 end Is_OK_Static_Expression;
4431 ------------------------
4432 -- Is_OK_Static_Range --
4433 ------------------------
4435 -- A static range is a range whose bounds are static expressions, or a
4436 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4437 -- We have already converted range attribute references, so we get the
4438 -- "or" part of this rule without needing a special test.
4440 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4441 begin
4442 return Is_OK_Static_Expression (Low_Bound (N))
4443 and then Is_OK_Static_Expression (High_Bound (N));
4444 end Is_OK_Static_Range;
4446 --------------------------
4447 -- Is_OK_Static_Subtype --
4448 --------------------------
4450 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4451 -- neither bound raises constraint error when evaluated.
4453 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4454 Base_T : constant Entity_Id := Base_Type (Typ);
4455 Anc_Subt : Entity_Id;
4457 begin
4458 -- First a quick check on the non static subtype flag. As described
4459 -- in further detail in Einfo, this flag is not decisive in all cases,
4460 -- but if it is set, then the subtype is definitely non-static.
4462 if Is_Non_Static_Subtype (Typ) then
4463 return False;
4464 end if;
4466 Anc_Subt := Ancestor_Subtype (Typ);
4468 if Anc_Subt = Empty then
4469 Anc_Subt := Base_T;
4470 end if;
4472 if Is_Generic_Type (Root_Type (Base_T))
4473 or else Is_Generic_Actual_Type (Base_T)
4474 then
4475 return False;
4477 -- String types
4479 elsif Is_String_Type (Typ) then
4480 return
4481 Ekind (Typ) = E_String_Literal_Subtype
4482 or else
4483 (Is_OK_Static_Subtype (Component_Type (Typ))
4484 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4486 -- Scalar types
4488 elsif Is_Scalar_Type (Typ) then
4489 if Base_T = Typ then
4490 return True;
4492 else
4493 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4494 -- Get_Type_{Low,High}_Bound.
4496 return Is_OK_Static_Subtype (Anc_Subt)
4497 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4498 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4499 end if;
4501 -- Types other than string and scalar types are never static
4503 else
4504 return False;
4505 end if;
4506 end Is_OK_Static_Subtype;
4508 ---------------------
4509 -- Is_Out_Of_Range --
4510 ---------------------
4512 function Is_Out_Of_Range
4513 (N : Node_Id;
4514 Typ : Entity_Id;
4515 Assume_Valid : Boolean := False;
4516 Fixed_Int : Boolean := False;
4517 Int_Real : Boolean := False) return Boolean
4519 begin
4520 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4521 = Out_Of_Range;
4522 end Is_Out_Of_Range;
4524 ---------------------
4525 -- Is_Static_Range --
4526 ---------------------
4528 -- A static range is a range whose bounds are static expressions, or a
4529 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4530 -- We have already converted range attribute references, so we get the
4531 -- "or" part of this rule without needing a special test.
4533 function Is_Static_Range (N : Node_Id) return Boolean is
4534 begin
4535 return Is_Static_Expression (Low_Bound (N))
4536 and then Is_Static_Expression (High_Bound (N));
4537 end Is_Static_Range;
4539 -----------------------
4540 -- Is_Static_Subtype --
4541 -----------------------
4543 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4545 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
4546 Base_T : constant Entity_Id := Base_Type (Typ);
4547 Anc_Subt : Entity_Id;
4549 begin
4550 -- First a quick check on the non static subtype flag. As described
4551 -- in further detail in Einfo, this flag is not decisive in all cases,
4552 -- but if it is set, then the subtype is definitely non-static.
4554 if Is_Non_Static_Subtype (Typ) then
4555 return False;
4556 end if;
4558 Anc_Subt := Ancestor_Subtype (Typ);
4560 if Anc_Subt = Empty then
4561 Anc_Subt := Base_T;
4562 end if;
4564 if Is_Generic_Type (Root_Type (Base_T))
4565 or else Is_Generic_Actual_Type (Base_T)
4566 then
4567 return False;
4569 -- String types
4571 elsif Is_String_Type (Typ) then
4572 return
4573 Ekind (Typ) = E_String_Literal_Subtype
4574 or else (Is_Static_Subtype (Component_Type (Typ))
4575 and then Is_Static_Subtype (Etype (First_Index (Typ))));
4577 -- Scalar types
4579 elsif Is_Scalar_Type (Typ) then
4580 if Base_T = Typ then
4581 return True;
4583 else
4584 return Is_Static_Subtype (Anc_Subt)
4585 and then Is_Static_Expression (Type_Low_Bound (Typ))
4586 and then Is_Static_Expression (Type_High_Bound (Typ));
4587 end if;
4589 -- Types other than string and scalar types are never static
4591 else
4592 return False;
4593 end if;
4594 end Is_Static_Subtype;
4596 --------------------
4597 -- Not_Null_Range --
4598 --------------------
4600 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4601 Typ : constant Entity_Id := Etype (Lo);
4603 begin
4604 if not Compile_Time_Known_Value (Lo)
4605 or else not Compile_Time_Known_Value (Hi)
4606 then
4607 return False;
4608 end if;
4610 if Is_Discrete_Type (Typ) then
4611 return Expr_Value (Lo) <= Expr_Value (Hi);
4613 else
4614 pragma Assert (Is_Real_Type (Typ));
4616 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
4617 end if;
4618 end Not_Null_Range;
4620 -------------
4621 -- OK_Bits --
4622 -------------
4624 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
4625 begin
4626 -- We allow a maximum of 500,000 bits which seems a reasonable limit
4628 if Bits < 500_000 then
4629 return True;
4631 else
4632 Error_Msg_N ("static value too large, capacity exceeded", N);
4633 return False;
4634 end if;
4635 end OK_Bits;
4637 ------------------
4638 -- Out_Of_Range --
4639 ------------------
4641 procedure Out_Of_Range (N : Node_Id) is
4642 begin
4643 -- If we have the static expression case, then this is an illegality
4644 -- in Ada 95 mode, except that in an instance, we never generate an
4645 -- error (if the error is legitimate, it was already diagnosed in the
4646 -- template). The expression to compute the length of a packed array is
4647 -- attached to the array type itself, and deserves a separate message.
4649 if Is_Static_Expression (N)
4650 and then not In_Instance
4651 and then not In_Inlined_Body
4652 and then Ada_Version >= Ada_95
4653 then
4654 if Nkind (Parent (N)) = N_Defining_Identifier
4655 and then Is_Array_Type (Parent (N))
4656 and then Present (Packed_Array_Type (Parent (N)))
4657 and then Present (First_Rep_Item (Parent (N)))
4658 then
4659 Error_Msg_N
4660 ("length of packed array must not exceed Integer''Last",
4661 First_Rep_Item (Parent (N)));
4662 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
4664 else
4665 Apply_Compile_Time_Constraint_Error
4666 (N, "value not in range of}", CE_Range_Check_Failed);
4667 end if;
4669 -- Here we generate a warning for the Ada 83 case, or when we are in an
4670 -- instance, or when we have a non-static expression case.
4672 else
4673 Apply_Compile_Time_Constraint_Error
4674 (N, "value not in range of}??", CE_Range_Check_Failed);
4675 end if;
4676 end Out_Of_Range;
4678 -------------------------
4679 -- Rewrite_In_Raise_CE --
4680 -------------------------
4682 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
4683 Typ : constant Entity_Id := Etype (N);
4685 begin
4686 -- If we want to raise CE in the condition of a N_Raise_CE node
4687 -- we may as well get rid of the condition.
4689 if Present (Parent (N))
4690 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
4691 then
4692 Set_Condition (Parent (N), Empty);
4694 -- If the expression raising CE is a N_Raise_CE node, we can use that
4695 -- one. We just preserve the type of the context.
4697 elsif Nkind (Exp) = N_Raise_Constraint_Error then
4698 Rewrite (N, Exp);
4699 Set_Etype (N, Typ);
4701 -- Else build an explcit N_Raise_CE
4703 else
4704 Rewrite (N,
4705 Make_Raise_Constraint_Error (Sloc (Exp),
4706 Reason => CE_Range_Check_Failed));
4707 Set_Raises_Constraint_Error (N);
4708 Set_Etype (N, Typ);
4709 end if;
4710 end Rewrite_In_Raise_CE;
4712 ---------------------
4713 -- String_Type_Len --
4714 ---------------------
4716 function String_Type_Len (Stype : Entity_Id) return Uint is
4717 NT : constant Entity_Id := Etype (First_Index (Stype));
4718 T : Entity_Id;
4720 begin
4721 if Is_OK_Static_Subtype (NT) then
4722 T := NT;
4723 else
4724 T := Base_Type (NT);
4725 end if;
4727 return Expr_Value (Type_High_Bound (T)) -
4728 Expr_Value (Type_Low_Bound (T)) + 1;
4729 end String_Type_Len;
4731 ------------------------------------
4732 -- Subtypes_Statically_Compatible --
4733 ------------------------------------
4735 function Subtypes_Statically_Compatible
4736 (T1 : Entity_Id;
4737 T2 : Entity_Id) return Boolean
4739 begin
4740 -- Scalar types
4742 if Is_Scalar_Type (T1) then
4744 -- Definitely compatible if we match
4746 if Subtypes_Statically_Match (T1, T2) then
4747 return True;
4749 -- If either subtype is nonstatic then they're not compatible
4751 elsif not Is_Static_Subtype (T1)
4752 or else not Is_Static_Subtype (T2)
4753 then
4754 return False;
4756 -- If either type has constraint error bounds, then consider that
4757 -- they match to avoid junk cascaded errors here.
4759 elsif not Is_OK_Static_Subtype (T1)
4760 or else not Is_OK_Static_Subtype (T2)
4761 then
4762 return True;
4764 -- Base types must match, but we don't check that (should we???) but
4765 -- we do at least check that both types are real, or both types are
4766 -- not real.
4768 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
4769 return False;
4771 -- Here we check the bounds
4773 else
4774 declare
4775 LB1 : constant Node_Id := Type_Low_Bound (T1);
4776 HB1 : constant Node_Id := Type_High_Bound (T1);
4777 LB2 : constant Node_Id := Type_Low_Bound (T2);
4778 HB2 : constant Node_Id := Type_High_Bound (T2);
4780 begin
4781 if Is_Real_Type (T1) then
4782 return
4783 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
4784 or else
4785 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
4786 and then
4787 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
4789 else
4790 return
4791 (Expr_Value (LB1) > Expr_Value (HB1))
4792 or else
4793 (Expr_Value (LB2) <= Expr_Value (LB1)
4794 and then
4795 Expr_Value (HB1) <= Expr_Value (HB2));
4796 end if;
4797 end;
4798 end if;
4800 -- Access types
4802 elsif Is_Access_Type (T1) then
4803 return (not Is_Constrained (T2)
4804 or else (Subtypes_Statically_Match
4805 (Designated_Type (T1), Designated_Type (T2))))
4806 and then not (Can_Never_Be_Null (T2)
4807 and then not Can_Never_Be_Null (T1));
4809 -- All other cases
4811 else
4812 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
4813 or else Subtypes_Statically_Match (T1, T2);
4814 end if;
4815 end Subtypes_Statically_Compatible;
4817 -------------------------------
4818 -- Subtypes_Statically_Match --
4819 -------------------------------
4821 -- Subtypes statically match if they have statically matching constraints
4822 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4823 -- they are the same identical constraint, or if they are static and the
4824 -- values match (RM 4.9.1(1)).
4826 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
4828 function Predicates_Match return Boolean;
4829 -- In Ada 2012, subtypes statically match if their static predicates
4830 -- match as well.
4832 ----------------------
4833 -- Predicates_Match --
4834 ----------------------
4836 function Predicates_Match return Boolean is
4837 Pred1 : Node_Id;
4838 Pred2 : Node_Id;
4840 begin
4841 if Ada_Version < Ada_2012 then
4842 return True;
4844 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
4845 return False;
4847 else
4848 Pred1 :=
4849 Get_Rep_Item
4850 (T1, Name_Static_Predicate, Check_Parents => False);
4851 Pred2 :=
4852 Get_Rep_Item
4853 (T2, Name_Static_Predicate, Check_Parents => False);
4855 -- Subtypes statically match if the predicate comes from the
4856 -- same declaration, which can only happen if one is a subtype
4857 -- of the other and has no explicit predicate.
4859 -- Suppress warnings on order of actuals, which is otherwise
4860 -- triggered by one of the two calls below.
4862 pragma Warnings (Off);
4863 return Pred1 = Pred2
4864 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
4865 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
4866 pragma Warnings (On);
4867 end if;
4868 end Predicates_Match;
4870 -- Start of processing for Subtypes_Statically_Match
4872 begin
4873 -- A type always statically matches itself
4875 if T1 = T2 then
4876 return True;
4878 -- Scalar types
4880 elsif Is_Scalar_Type (T1) then
4882 -- Base types must be the same
4884 if Base_Type (T1) /= Base_Type (T2) then
4885 return False;
4886 end if;
4888 -- A constrained numeric subtype never matches an unconstrained
4889 -- subtype, i.e. both types must be constrained or unconstrained.
4891 -- To understand the requirement for this test, see RM 4.9.1(1).
4892 -- As is made clear in RM 3.5.4(11), type Integer, for example is
4893 -- a constrained subtype with constraint bounds matching the bounds
4894 -- of its corresponding unconstrained base type. In this situation,
4895 -- Integer and Integer'Base do not statically match, even though
4896 -- they have the same bounds.
4898 -- We only apply this test to types in Standard and types that appear
4899 -- in user programs. That way, we do not have to be too careful about
4900 -- setting Is_Constrained right for Itypes.
4902 if Is_Numeric_Type (T1)
4903 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4904 and then (Scope (T1) = Standard_Standard
4905 or else Comes_From_Source (T1))
4906 and then (Scope (T2) = Standard_Standard
4907 or else Comes_From_Source (T2))
4908 then
4909 return False;
4911 -- A generic scalar type does not statically match its base type
4912 -- (AI-311). In this case we make sure that the formals, which are
4913 -- first subtypes of their bases, are constrained.
4915 elsif Is_Generic_Type (T1)
4916 and then Is_Generic_Type (T2)
4917 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4918 then
4919 return False;
4920 end if;
4922 -- If there was an error in either range, then just assume the types
4923 -- statically match to avoid further junk errors.
4925 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
4926 or else Error_Posted (Scalar_Range (T1))
4927 or else Error_Posted (Scalar_Range (T2))
4928 then
4929 return True;
4930 end if;
4932 -- Otherwise both types have bound that can be compared
4934 declare
4935 LB1 : constant Node_Id := Type_Low_Bound (T1);
4936 HB1 : constant Node_Id := Type_High_Bound (T1);
4937 LB2 : constant Node_Id := Type_Low_Bound (T2);
4938 HB2 : constant Node_Id := Type_High_Bound (T2);
4940 begin
4941 -- If the bounds are the same tree node, then match if and only
4942 -- if any predicates present also match.
4944 if LB1 = LB2 and then HB1 = HB2 then
4945 return Predicates_Match;
4947 -- Otherwise bounds must be static and identical value
4949 else
4950 if not Is_Static_Subtype (T1)
4951 or else not Is_Static_Subtype (T2)
4952 then
4953 return False;
4955 -- If either type has constraint error bounds, then say that
4956 -- they match to avoid junk cascaded errors here.
4958 elsif not Is_OK_Static_Subtype (T1)
4959 or else not Is_OK_Static_Subtype (T2)
4960 then
4961 return True;
4963 elsif Is_Real_Type (T1) then
4964 return
4965 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4966 and then
4967 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4969 else
4970 return
4971 Expr_Value (LB1) = Expr_Value (LB2)
4972 and then
4973 Expr_Value (HB1) = Expr_Value (HB2);
4974 end if;
4975 end if;
4976 end;
4978 -- Type with discriminants
4980 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
4982 -- Because of view exchanges in multiple instantiations, conformance
4983 -- checking might try to match a partial view of a type with no
4984 -- discriminants with a full view that has defaulted discriminants.
4985 -- In such a case, use the discriminant constraint of the full view,
4986 -- which must exist because we know that the two subtypes have the
4987 -- same base type.
4989 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
4990 if In_Instance then
4991 if Is_Private_Type (T2)
4992 and then Present (Full_View (T2))
4993 and then Has_Discriminants (Full_View (T2))
4994 then
4995 return Subtypes_Statically_Match (T1, Full_View (T2));
4997 elsif Is_Private_Type (T1)
4998 and then Present (Full_View (T1))
4999 and then Has_Discriminants (Full_View (T1))
5000 then
5001 return Subtypes_Statically_Match (Full_View (T1), T2);
5003 else
5004 return False;
5005 end if;
5006 else
5007 return False;
5008 end if;
5009 end if;
5011 declare
5012 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
5013 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
5015 DA1 : Elmt_Id;
5016 DA2 : Elmt_Id;
5018 begin
5019 if DL1 = DL2 then
5020 return True;
5021 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
5022 return False;
5023 end if;
5025 -- Now loop through the discriminant constraints
5027 -- Note: the guard here seems necessary, since it is possible at
5028 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5030 if Present (DL1) and then Present (DL2) then
5031 DA1 := First_Elmt (DL1);
5032 DA2 := First_Elmt (DL2);
5033 while Present (DA1) loop
5034 declare
5035 Expr1 : constant Node_Id := Node (DA1);
5036 Expr2 : constant Node_Id := Node (DA2);
5038 begin
5039 if not Is_Static_Expression (Expr1)
5040 or else not Is_Static_Expression (Expr2)
5041 then
5042 return False;
5044 -- If either expression raised a constraint error,
5045 -- consider the expressions as matching, since this
5046 -- helps to prevent cascading errors.
5048 elsif Raises_Constraint_Error (Expr1)
5049 or else Raises_Constraint_Error (Expr2)
5050 then
5051 null;
5053 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
5054 return False;
5055 end if;
5056 end;
5058 Next_Elmt (DA1);
5059 Next_Elmt (DA2);
5060 end loop;
5061 end if;
5062 end;
5064 return True;
5066 -- A definite type does not match an indefinite or classwide type.
5067 -- However, a generic type with unknown discriminants may be
5068 -- instantiated with a type with no discriminants, and conformance
5069 -- checking on an inherited operation may compare the actual with the
5070 -- subtype that renames it in the instance.
5072 elsif
5073 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
5074 then
5075 return
5076 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
5078 -- Array type
5080 elsif Is_Array_Type (T1) then
5082 -- If either subtype is unconstrained then both must be, and if both
5083 -- are unconstrained then no further checking is needed.
5085 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
5086 return not (Is_Constrained (T1) or else Is_Constrained (T2));
5087 end if;
5089 -- Both subtypes are constrained, so check that the index subtypes
5090 -- statically match.
5092 declare
5093 Index1 : Node_Id := First_Index (T1);
5094 Index2 : Node_Id := First_Index (T2);
5096 begin
5097 while Present (Index1) loop
5098 if not
5099 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
5100 then
5101 return False;
5102 end if;
5104 Next_Index (Index1);
5105 Next_Index (Index2);
5106 end loop;
5108 return True;
5109 end;
5111 elsif Is_Access_Type (T1) then
5112 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
5113 return False;
5115 elsif Ekind_In (T1, E_Access_Subprogram_Type,
5116 E_Anonymous_Access_Subprogram_Type)
5117 then
5118 return
5119 Subtype_Conformant
5120 (Designated_Type (T1),
5121 Designated_Type (T2));
5122 else
5123 return
5124 Subtypes_Statically_Match
5125 (Designated_Type (T1),
5126 Designated_Type (T2))
5127 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
5128 end if;
5130 -- All other types definitely match
5132 else
5133 return True;
5134 end if;
5135 end Subtypes_Statically_Match;
5137 ----------
5138 -- Test --
5139 ----------
5141 function Test (Cond : Boolean) return Uint is
5142 begin
5143 if Cond then
5144 return Uint_1;
5145 else
5146 return Uint_0;
5147 end if;
5148 end Test;
5150 ---------------------------------
5151 -- Test_Expression_Is_Foldable --
5152 ---------------------------------
5154 -- One operand case
5156 procedure Test_Expression_Is_Foldable
5157 (N : Node_Id;
5158 Op1 : Node_Id;
5159 Stat : out Boolean;
5160 Fold : out Boolean)
5162 begin
5163 Stat := False;
5164 Fold := False;
5166 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5167 return;
5168 end if;
5170 -- If operand is Any_Type, just propagate to result and do not
5171 -- try to fold, this prevents cascaded errors.
5173 if Etype (Op1) = Any_Type then
5174 Set_Etype (N, Any_Type);
5175 return;
5177 -- If operand raises constraint error, then replace node N with the
5178 -- raise constraint error node, and we are obviously not foldable.
5179 -- Note that this replacement inherits the Is_Static_Expression flag
5180 -- from the operand.
5182 elsif Raises_Constraint_Error (Op1) then
5183 Rewrite_In_Raise_CE (N, Op1);
5184 return;
5186 -- If the operand is not static, then the result is not static, and
5187 -- all we have to do is to check the operand since it is now known
5188 -- to appear in a non-static context.
5190 elsif not Is_Static_Expression (Op1) then
5191 Check_Non_Static_Context (Op1);
5192 Fold := Compile_Time_Known_Value (Op1);
5193 return;
5195 -- An expression of a formal modular type is not foldable because
5196 -- the modulus is unknown.
5198 elsif Is_Modular_Integer_Type (Etype (Op1))
5199 and then Is_Generic_Type (Etype (Op1))
5200 then
5201 Check_Non_Static_Context (Op1);
5202 return;
5204 -- Here we have the case of an operand whose type is OK, which is
5205 -- static, and which does not raise constraint error, we can fold.
5207 else
5208 Set_Is_Static_Expression (N);
5209 Fold := True;
5210 Stat := True;
5211 end if;
5212 end Test_Expression_Is_Foldable;
5214 -- Two operand case
5216 procedure Test_Expression_Is_Foldable
5217 (N : Node_Id;
5218 Op1 : Node_Id;
5219 Op2 : Node_Id;
5220 Stat : out Boolean;
5221 Fold : out Boolean;
5222 CRT_Safe : Boolean := False)
5224 Rstat : constant Boolean := Is_Static_Expression (Op1)
5225 and then Is_Static_Expression (Op2);
5227 begin
5228 Stat := False;
5229 Fold := False;
5231 -- Inhibit folding if -gnatd.f flag set
5233 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5234 return;
5235 end if;
5237 -- If either operand is Any_Type, just propagate to result and
5238 -- do not try to fold, this prevents cascaded errors.
5240 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
5241 Set_Etype (N, Any_Type);
5242 return;
5244 -- If left operand raises constraint error, then replace node N with the
5245 -- Raise_Constraint_Error node, and we are obviously not foldable.
5246 -- Is_Static_Expression is set from the two operands in the normal way,
5247 -- and we check the right operand if it is in a non-static context.
5249 elsif Raises_Constraint_Error (Op1) then
5250 if not Rstat then
5251 Check_Non_Static_Context (Op2);
5252 end if;
5254 Rewrite_In_Raise_CE (N, Op1);
5255 Set_Is_Static_Expression (N, Rstat);
5256 return;
5258 -- Similar processing for the case of the right operand. Note that we
5259 -- don't use this routine for the short-circuit case, so we do not have
5260 -- to worry about that special case here.
5262 elsif Raises_Constraint_Error (Op2) then
5263 if not Rstat then
5264 Check_Non_Static_Context (Op1);
5265 end if;
5267 Rewrite_In_Raise_CE (N, Op2);
5268 Set_Is_Static_Expression (N, Rstat);
5269 return;
5271 -- Exclude expressions of a generic modular type, as above
5273 elsif Is_Modular_Integer_Type (Etype (Op1))
5274 and then Is_Generic_Type (Etype (Op1))
5275 then
5276 Check_Non_Static_Context (Op1);
5277 return;
5279 -- If result is not static, then check non-static contexts on operands
5280 -- since one of them may be static and the other one may not be static.
5282 elsif not Rstat then
5283 Check_Non_Static_Context (Op1);
5284 Check_Non_Static_Context (Op2);
5286 if CRT_Safe then
5287 Fold := CRT_Safe_Compile_Time_Known_Value (Op1)
5288 and then CRT_Safe_Compile_Time_Known_Value (Op2);
5289 else
5290 Fold := Compile_Time_Known_Value (Op1)
5291 and then Compile_Time_Known_Value (Op2);
5292 end if;
5294 return;
5296 -- Else result is static and foldable. Both operands are static, and
5297 -- neither raises constraint error, so we can definitely fold.
5299 else
5300 Set_Is_Static_Expression (N);
5301 Fold := True;
5302 Stat := True;
5303 return;
5304 end if;
5305 end Test_Expression_Is_Foldable;
5307 -------------------
5308 -- Test_In_Range --
5309 -------------------
5311 function Test_In_Range
5312 (N : Node_Id;
5313 Typ : Entity_Id;
5314 Assume_Valid : Boolean;
5315 Fixed_Int : Boolean;
5316 Int_Real : Boolean) return Range_Membership
5318 Val : Uint;
5319 Valr : Ureal;
5321 pragma Warnings (Off, Assume_Valid);
5322 -- For now Assume_Valid is unreferenced since the current implementation
5323 -- always returns Unknown if N is not a compile time known value, but we
5324 -- keep the parameter to allow for future enhancements in which we try
5325 -- to get the information in the variable case as well.
5327 begin
5328 -- Universal types have no range limits, so always in range
5330 if Typ = Universal_Integer or else Typ = Universal_Real then
5331 return In_Range;
5333 -- Never known if not scalar type. Don't know if this can actually
5334 -- happen, but our spec allows it, so we must check.
5336 elsif not Is_Scalar_Type (Typ) then
5337 return Unknown;
5339 -- Never known if this is a generic type, since the bounds of generic
5340 -- types are junk. Note that if we only checked for static expressions
5341 -- (instead of compile time known values) below, we would not need this
5342 -- check, because values of a generic type can never be static, but they
5343 -- can be known at compile time.
5345 elsif Is_Generic_Type (Typ) then
5346 return Unknown;
5348 -- Never known unless we have a compile time known value
5350 elsif not Compile_Time_Known_Value (N) then
5351 return Unknown;
5353 -- General processing with a known compile time value
5355 else
5356 declare
5357 Lo : Node_Id;
5358 Hi : Node_Id;
5360 LB_Known : Boolean;
5361 HB_Known : Boolean;
5363 begin
5364 Lo := Type_Low_Bound (Typ);
5365 Hi := Type_High_Bound (Typ);
5367 LB_Known := Compile_Time_Known_Value (Lo);
5368 HB_Known := Compile_Time_Known_Value (Hi);
5370 -- Fixed point types should be considered as such only if flag
5371 -- Fixed_Int is set to False.
5373 if Is_Floating_Point_Type (Typ)
5374 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
5375 or else Int_Real
5376 then
5377 Valr := Expr_Value_R (N);
5379 if LB_Known and HB_Known then
5380 if Valr >= Expr_Value_R (Lo)
5381 and then
5382 Valr <= Expr_Value_R (Hi)
5383 then
5384 return In_Range;
5385 else
5386 return Out_Of_Range;
5387 end if;
5389 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
5390 or else
5391 (HB_Known and then Valr > Expr_Value_R (Hi))
5392 then
5393 return Out_Of_Range;
5395 else
5396 return Unknown;
5397 end if;
5399 else
5400 Val := Expr_Value (N);
5402 if LB_Known and HB_Known then
5403 if Val >= Expr_Value (Lo)
5404 and then
5405 Val <= Expr_Value (Hi)
5406 then
5407 return In_Range;
5408 else
5409 return Out_Of_Range;
5410 end if;
5412 elsif (LB_Known and then Val < Expr_Value (Lo))
5413 or else
5414 (HB_Known and then Val > Expr_Value (Hi))
5415 then
5416 return Out_Of_Range;
5418 else
5419 return Unknown;
5420 end if;
5421 end if;
5422 end;
5423 end if;
5424 end Test_In_Range;
5426 --------------
5427 -- To_Bits --
5428 --------------
5430 procedure To_Bits (U : Uint; B : out Bits) is
5431 begin
5432 for J in 0 .. B'Last loop
5433 B (J) := (U / (2 ** J)) mod 2 /= 0;
5434 end loop;
5435 end To_Bits;
5437 --------------------
5438 -- Why_Not_Static --
5439 --------------------
5441 procedure Why_Not_Static (Expr : Node_Id) is
5442 N : constant Node_Id := Original_Node (Expr);
5443 Typ : Entity_Id;
5444 E : Entity_Id;
5446 procedure Why_Not_Static_List (L : List_Id);
5447 -- A version that can be called on a list of expressions. Finds all
5448 -- non-static violations in any element of the list.
5450 -------------------------
5451 -- Why_Not_Static_List --
5452 -------------------------
5454 procedure Why_Not_Static_List (L : List_Id) is
5455 N : Node_Id;
5457 begin
5458 if Is_Non_Empty_List (L) then
5459 N := First (L);
5460 while Present (N) loop
5461 Why_Not_Static (N);
5462 Next (N);
5463 end loop;
5464 end if;
5465 end Why_Not_Static_List;
5467 -- Start of processing for Why_Not_Static
5469 begin
5470 -- If in ACATS mode (debug flag 2), then suppress all these messages,
5471 -- this avoids massive updates to the ACATS base line.
5473 if Debug_Flag_2 then
5474 return;
5475 end if;
5477 -- Ignore call on error or empty node
5479 if No (Expr) or else Nkind (Expr) = N_Error then
5480 return;
5481 end if;
5483 -- Preprocessing for sub expressions
5485 if Nkind (Expr) in N_Subexpr then
5487 -- Nothing to do if expression is static
5489 if Is_OK_Static_Expression (Expr) then
5490 return;
5491 end if;
5493 -- Test for constraint error raised
5495 if Raises_Constraint_Error (Expr) then
5496 Error_Msg_N
5497 ("\expression raises exception, cannot be static " &
5498 "(RM 4.9(34))", N);
5499 return;
5500 end if;
5502 -- If no type, then something is pretty wrong, so ignore
5504 Typ := Etype (Expr);
5506 if No (Typ) then
5507 return;
5508 end if;
5510 -- Type must be scalar or string type (but allow Bignum, since this
5511 -- is really a scalar type from our point of view in this diagnosis).
5513 if not Is_Scalar_Type (Typ)
5514 and then not Is_String_Type (Typ)
5515 and then not Is_RTE (Typ, RE_Bignum)
5516 then
5517 Error_Msg_N
5518 ("\static expression must have scalar or string type " &
5519 "(RM 4.9(2))", N);
5520 return;
5521 end if;
5522 end if;
5524 -- If we got through those checks, test particular node kind
5526 case Nkind (N) is
5528 -- Entity name
5530 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
5531 E := Entity (N);
5533 if Is_Named_Number (E) then
5534 null;
5536 elsif Ekind (E) = E_Constant then
5538 -- One case we can give a metter message is when we have a
5539 -- string literal created by concatenating an aggregate with
5540 -- an others expression.
5542 Entity_Case : declare
5543 CV : constant Node_Id := Constant_Value (E);
5544 CO : constant Node_Id := Original_Node (CV);
5546 function Is_Aggregate (N : Node_Id) return Boolean;
5547 -- See if node N came from an others aggregate, if so
5548 -- return True and set Error_Msg_Sloc to aggregate.
5550 ------------------
5551 -- Is_Aggregate --
5552 ------------------
5554 function Is_Aggregate (N : Node_Id) return Boolean is
5555 begin
5556 if Nkind (Original_Node (N)) = N_Aggregate then
5557 Error_Msg_Sloc := Sloc (Original_Node (N));
5558 return True;
5559 elsif Is_Entity_Name (N)
5560 and then Ekind (Entity (N)) = E_Constant
5561 and then
5562 Nkind (Original_Node (Constant_Value (Entity (N)))) =
5563 N_Aggregate
5564 then
5565 Error_Msg_Sloc :=
5566 Sloc (Original_Node (Constant_Value (Entity (N))));
5567 return True;
5568 else
5569 return False;
5570 end if;
5571 end Is_Aggregate;
5573 -- Start of processing for Entity_Case
5575 begin
5576 if Is_Aggregate (CV)
5577 or else (Nkind (CO) = N_Op_Concat
5578 and then (Is_Aggregate (Left_Opnd (CO))
5579 or else
5580 Is_Aggregate (Right_Opnd (CO))))
5581 then
5582 Error_Msg_N ("\aggregate (#) is never static", N);
5584 elsif No (CV) or else not Is_Static_Expression (CV) then
5585 Error_Msg_NE
5586 ("\& is not a static constant (RM 4.9(5))", N, E);
5587 end if;
5588 end Entity_Case;
5590 else
5591 Error_Msg_NE
5592 ("\& is not static constant or named number "
5593 & "(RM 4.9(5))", N, E);
5594 end if;
5596 -- Binary operator
5598 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
5599 if Nkind (N) in N_Op_Shift then
5600 Error_Msg_N
5601 ("\shift functions are never static (RM 4.9(6,18))", N);
5603 else
5604 Why_Not_Static (Left_Opnd (N));
5605 Why_Not_Static (Right_Opnd (N));
5606 end if;
5608 -- Unary operator
5610 when N_Unary_Op =>
5611 Why_Not_Static (Right_Opnd (N));
5613 -- Attribute reference
5615 when N_Attribute_Reference =>
5616 Why_Not_Static_List (Expressions (N));
5618 E := Etype (Prefix (N));
5620 if E = Standard_Void_Type then
5621 return;
5622 end if;
5624 -- Special case non-scalar'Size since this is a common error
5626 if Attribute_Name (N) = Name_Size then
5627 Error_Msg_N
5628 ("\size attribute is only static for static scalar type "
5629 & "(RM 4.9(7,8))", N);
5631 -- Flag array cases
5633 elsif Is_Array_Type (E) then
5634 if Attribute_Name (N) /= Name_First
5635 and then
5636 Attribute_Name (N) /= Name_Last
5637 and then
5638 Attribute_Name (N) /= Name_Length
5639 then
5640 Error_Msg_N
5641 ("\static array attribute must be Length, First, or Last "
5642 & "(RM 4.9(8))", N);
5644 -- Since we know the expression is not-static (we already
5645 -- tested for this, must mean array is not static).
5647 else
5648 Error_Msg_N
5649 ("\prefix is non-static array (RM 4.9(8))", Prefix (N));
5650 end if;
5652 return;
5654 -- Special case generic types, since again this is a common source
5655 -- of confusion.
5657 elsif Is_Generic_Actual_Type (E)
5658 or else
5659 Is_Generic_Type (E)
5660 then
5661 Error_Msg_N
5662 ("\attribute of generic type is never static "
5663 & "(RM 4.9(7,8))", N);
5665 elsif Is_Static_Subtype (E) then
5666 null;
5668 elsif Is_Scalar_Type (E) then
5669 Error_Msg_N
5670 ("\prefix type for attribute is not static scalar subtype "
5671 & "(RM 4.9(7))", N);
5673 else
5674 Error_Msg_N
5675 ("\static attribute must apply to array/scalar type "
5676 & "(RM 4.9(7,8))", N);
5677 end if;
5679 -- String literal
5681 when N_String_Literal =>
5682 Error_Msg_N
5683 ("\subtype of string literal is non-static (RM 4.9(4))", N);
5685 -- Explicit dereference
5687 when N_Explicit_Dereference =>
5688 Error_Msg_N
5689 ("\explicit dereference is never static (RM 4.9)", N);
5691 -- Function call
5693 when N_Function_Call =>
5694 Why_Not_Static_List (Parameter_Associations (N));
5696 -- Complain about non-static function call unless we have Bignum
5697 -- which means that the underlying expression is really some
5698 -- scalar arithmetic operation.
5700 if not Is_RTE (Typ, RE_Bignum) then
5701 Error_Msg_N ("\non-static function call (RM 4.9(6,18))", N);
5702 end if;
5704 -- Parameter assocation (test actual parameter)
5706 when N_Parameter_Association =>
5707 Why_Not_Static (Explicit_Actual_Parameter (N));
5709 -- Indexed component
5711 when N_Indexed_Component =>
5712 Error_Msg_N ("\indexed component is never static (RM 4.9)", N);
5714 -- Procedure call
5716 when N_Procedure_Call_Statement =>
5717 Error_Msg_N ("\procedure call is never static (RM 4.9)", N);
5719 -- Qualified expression (test expression)
5721 when N_Qualified_Expression =>
5722 Why_Not_Static (Expression (N));
5724 -- Aggregate
5726 when N_Aggregate | N_Extension_Aggregate =>
5727 Error_Msg_N ("\an aggregate is never static (RM 4.9)", N);
5729 -- Range
5731 when N_Range =>
5732 Why_Not_Static (Low_Bound (N));
5733 Why_Not_Static (High_Bound (N));
5735 -- Range constraint, test range expression
5737 when N_Range_Constraint =>
5738 Why_Not_Static (Range_Expression (N));
5740 -- Subtype indication, test constraint
5742 when N_Subtype_Indication =>
5743 Why_Not_Static (Constraint (N));
5745 -- Selected component
5747 when N_Selected_Component =>
5748 Error_Msg_N ("\selected component is never static (RM 4.9)", N);
5750 -- Slice
5752 when N_Slice =>
5753 Error_Msg_N ("\slice is never static (RM 4.9)", N);
5755 when N_Type_Conversion =>
5756 Why_Not_Static (Expression (N));
5758 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
5759 or else not Is_Static_Subtype (Entity (Subtype_Mark (N)))
5760 then
5761 Error_Msg_N
5762 ("\static conversion requires static scalar subtype result "
5763 & "(RM 4.9(9))", N);
5764 end if;
5766 -- Unchecked type conversion
5768 when N_Unchecked_Type_Conversion =>
5769 Error_Msg_N
5770 ("\unchecked type conversion is never static (RM 4.9)", N);
5772 -- All other cases, no reason to give
5774 when others =>
5775 null;
5777 end case;
5778 end Why_Not_Static;
5780 end Sem_Eval;