Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / sem_eval.adb
blob94ce100a7c5a2aa4bfbb6a3dbb4cc2474a79f902
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 -- Same processing, except applies to an expression N with two operands
236 -- Op1 and Op2. The result is static only if both operands are static.
238 function Test_In_Range
239 (N : Node_Id;
240 Typ : Entity_Id;
241 Assume_Valid : Boolean;
242 Fixed_Int : Boolean;
243 Int_Real : Boolean) return Range_Membership;
244 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
245 -- or Out_Of_Range if it can be guaranteed at compile time that expression
246 -- N is known to be in or out of range of the subtype Typ. If not compile
247 -- time known, Unknown is returned. See documentation of Is_In_Range for
248 -- complete description of parameters.
250 procedure To_Bits (U : Uint; B : out Bits);
251 -- Converts a Uint value to a bit string of length B'Length
253 ------------------------------
254 -- Check_Non_Static_Context --
255 ------------------------------
257 procedure Check_Non_Static_Context (N : Node_Id) is
258 T : constant Entity_Id := Etype (N);
259 Checks_On : constant Boolean :=
260 not Index_Checks_Suppressed (T)
261 and not Range_Checks_Suppressed (T);
263 begin
264 -- Ignore cases of non-scalar types, error types, or universal real
265 -- types that have no usable bounds.
267 if T = Any_Type
268 or else not Is_Scalar_Type (T)
269 or else T = Universal_Fixed
270 or else T = Universal_Real
271 then
272 return;
273 end if;
275 -- At this stage we have a scalar type. If we have an expression that
276 -- raises CE, then we already issued a warning or error msg so there
277 -- is nothing more to be done in this routine.
279 if Raises_Constraint_Error (N) then
280 return;
281 end if;
283 -- Now we have a scalar type which is not marked as raising a constraint
284 -- error exception. The main purpose of this routine is to deal with
285 -- static expressions appearing in a non-static context. That means
286 -- that if we do not have a static expression then there is not much
287 -- to do. The one case that we deal with here is that if we have a
288 -- floating-point value that is out of range, then we post a warning
289 -- that an infinity will result.
291 if not Is_Static_Expression (N) then
292 if Is_Floating_Point_Type (T)
293 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
294 then
295 Error_Msg_N
296 ("??float value out of range, infinity will be generated", N);
297 end if;
299 return;
300 end if;
302 -- Here we have the case of outer level static expression of scalar
303 -- type, where the processing of this procedure is needed.
305 -- For real types, this is where we convert the value to a machine
306 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
307 -- need to do this if the parent is a constant declaration, since in
308 -- other cases, gigi should do the necessary conversion correctly, but
309 -- experimentation shows that this is not the case on all machines, in
310 -- particular if we do not convert all literals to machine values in
311 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
312 -- and SGI/Irix.
314 if Nkind (N) = N_Real_Literal
315 and then not Is_Machine_Number (N)
316 and then not Is_Generic_Type (Etype (N))
317 and then Etype (N) /= Universal_Real
318 then
319 -- Check that value is in bounds before converting to machine
320 -- number, so as not to lose case where value overflows in the
321 -- least significant bit or less. See B490001.
323 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
324 Out_Of_Range (N);
325 return;
326 end if;
328 -- Note: we have to copy the node, to avoid problems with conformance
329 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
331 Rewrite (N, New_Copy (N));
333 if not Is_Floating_Point_Type (T) then
334 Set_Realval
335 (N, Corresponding_Integer_Value (N) * Small_Value (T));
337 elsif not UR_Is_Zero (Realval (N)) then
339 -- Note: even though RM 4.9(38) specifies biased rounding, this
340 -- has been modified by AI-100 in order to prevent confusing
341 -- differences in rounding between static and non-static
342 -- expressions. AI-100 specifies that the effect of such rounding
343 -- is implementation dependent, and in GNAT we round to nearest
344 -- even to match the run-time behavior.
346 Set_Realval
347 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
348 end if;
350 Set_Is_Machine_Number (N);
351 end if;
353 -- Check for out of range universal integer. This is a non-static
354 -- context, so the integer value must be in range of the runtime
355 -- representation of universal integers.
357 -- We do this only within an expression, because that is the only
358 -- case in which non-static universal integer values can occur, and
359 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
360 -- called in contexts like the expression of a number declaration where
361 -- we certainly want to allow out of range values.
363 if Etype (N) = Universal_Integer
364 and then Nkind (N) = N_Integer_Literal
365 and then Nkind (Parent (N)) in N_Subexpr
366 and then
367 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
368 or else
369 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
370 then
371 Apply_Compile_Time_Constraint_Error
372 (N, "non-static universal integer value out of range??",
373 CE_Range_Check_Failed);
375 -- Check out of range of base type
377 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
378 Out_Of_Range (N);
380 -- Give warning if outside subtype (where one or both of the bounds of
381 -- the subtype is static). This warning is omitted if the expression
382 -- appears in a range that could be null (warnings are handled elsewhere
383 -- for this case).
385 elsif T /= Base_Type (T)
386 and then Nkind (Parent (N)) /= N_Range
387 then
388 if Is_In_Range (N, T, Assume_Valid => True) then
389 null;
391 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
392 Apply_Compile_Time_Constraint_Error
393 (N, "value not in range of}??", CE_Range_Check_Failed);
395 elsif Checks_On then
396 Enable_Range_Check (N);
398 else
399 Set_Do_Range_Check (N, False);
400 end if;
401 end if;
402 end Check_Non_Static_Context;
404 ---------------------------------
405 -- Check_String_Literal_Length --
406 ---------------------------------
408 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
409 begin
410 if not Raises_Constraint_Error (N) and then Is_Constrained (Ttype) then
412 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
413 then
414 Apply_Compile_Time_Constraint_Error
415 (N, "string length wrong for}??",
416 CE_Length_Check_Failed,
417 Ent => Ttype,
418 Typ => Ttype);
419 end if;
420 end if;
421 end Check_String_Literal_Length;
423 --------------------------
424 -- Compile_Time_Compare --
425 --------------------------
427 function Compile_Time_Compare
428 (L, R : Node_Id;
429 Assume_Valid : Boolean) return Compare_Result
431 Discard : aliased Uint;
432 begin
433 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
434 end Compile_Time_Compare;
436 function Compile_Time_Compare
437 (L, R : Node_Id;
438 Diff : access Uint;
439 Assume_Valid : Boolean;
440 Rec : Boolean := False) return Compare_Result
442 Ltyp : Entity_Id := Underlying_Type (Etype (L));
443 Rtyp : Entity_Id := Underlying_Type (Etype (R));
444 -- These get reset to the base type for the case of entities where
445 -- Is_Known_Valid is not set. This takes care of handling possible
446 -- invalid representations using the value of the base type, in
447 -- accordance with RM 13.9.1(10).
449 Discard : aliased Uint;
451 procedure Compare_Decompose
452 (N : Node_Id;
453 R : out Node_Id;
454 V : out Uint);
455 -- This procedure decomposes the node N into an expression node and a
456 -- signed offset, so that the value of N is equal to the value of R plus
457 -- the value V (which may be negative). If no such decomposition is
458 -- possible, then on return R is a copy of N, and V is set to zero.
460 function Compare_Fixup (N : Node_Id) return Node_Id;
461 -- This function deals with replacing 'Last and 'First references with
462 -- their corresponding type bounds, which we then can compare. The
463 -- argument is the original node, the result is the identity, unless we
464 -- have a 'Last/'First reference in which case the value returned is the
465 -- appropriate type bound.
467 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
468 -- Even if the context does not assume that values are valid, some
469 -- simple cases can be recognized.
471 function Is_Same_Value (L, R : Node_Id) return Boolean;
472 -- Returns True iff L and R represent expressions that definitely have
473 -- identical (but not necessarily compile time known) values Indeed the
474 -- caller is expected to have already dealt with the cases of compile
475 -- time known values, so these are not tested here.
477 -----------------------
478 -- Compare_Decompose --
479 -----------------------
481 procedure Compare_Decompose
482 (N : Node_Id;
483 R : out Node_Id;
484 V : out Uint)
486 begin
487 if Nkind (N) = N_Op_Add
488 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
489 then
490 R := Left_Opnd (N);
491 V := Intval (Right_Opnd (N));
492 return;
494 elsif Nkind (N) = N_Op_Subtract
495 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
496 then
497 R := Left_Opnd (N);
498 V := UI_Negate (Intval (Right_Opnd (N)));
499 return;
501 elsif Nkind (N) = N_Attribute_Reference then
502 if Attribute_Name (N) = Name_Succ then
503 R := First (Expressions (N));
504 V := Uint_1;
505 return;
507 elsif Attribute_Name (N) = Name_Pred then
508 R := First (Expressions (N));
509 V := Uint_Minus_1;
510 return;
511 end if;
512 end if;
514 R := N;
515 V := Uint_0;
516 end Compare_Decompose;
518 -------------------
519 -- Compare_Fixup --
520 -------------------
522 function Compare_Fixup (N : Node_Id) return Node_Id is
523 Indx : Node_Id;
524 Xtyp : Entity_Id;
525 Subs : Nat;
527 begin
528 -- Fixup only required for First/Last attribute reference
530 if Nkind (N) = N_Attribute_Reference
531 and then Nam_In (Attribute_Name (N), Name_First, Name_Last)
532 then
533 Xtyp := Etype (Prefix (N));
535 -- If we have no type, then just abandon the attempt to do
536 -- a fixup, this is probably the result of some other error.
538 if No (Xtyp) then
539 return N;
540 end if;
542 -- Dereference an access type
544 if Is_Access_Type (Xtyp) then
545 Xtyp := Designated_Type (Xtyp);
546 end if;
548 -- If we don't have an array type at this stage, something
549 -- is peculiar, e.g. another error, and we abandon the attempt
550 -- at a fixup.
552 if not Is_Array_Type (Xtyp) then
553 return N;
554 end if;
556 -- Ignore unconstrained array, since bounds are not meaningful
558 if not Is_Constrained (Xtyp) then
559 return N;
560 end if;
562 if Ekind (Xtyp) = E_String_Literal_Subtype then
563 if Attribute_Name (N) = Name_First then
564 return String_Literal_Low_Bound (Xtyp);
566 else
567 return Make_Integer_Literal (Sloc (N),
568 Intval => Intval (String_Literal_Low_Bound (Xtyp))
569 + String_Literal_Length (Xtyp));
570 end if;
571 end if;
573 -- Find correct index type
575 Indx := First_Index (Xtyp);
577 if Present (Expressions (N)) then
578 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
580 for J in 2 .. Subs loop
581 Indx := Next_Index (Indx);
582 end loop;
583 end if;
585 Xtyp := Etype (Indx);
587 if Attribute_Name (N) = Name_First then
588 return Type_Low_Bound (Xtyp);
589 else
590 return Type_High_Bound (Xtyp);
591 end if;
592 end if;
594 return N;
595 end Compare_Fixup;
597 ----------------------------
598 -- Is_Known_Valid_Operand --
599 ----------------------------
601 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
602 begin
603 return (Is_Entity_Name (Opnd)
604 and then
605 (Is_Known_Valid (Entity (Opnd))
606 or else Ekind (Entity (Opnd)) = E_In_Parameter
607 or else
608 (Ekind (Entity (Opnd)) in Object_Kind
609 and then Present (Current_Value (Entity (Opnd))))))
610 or else Is_OK_Static_Expression (Opnd);
611 end Is_Known_Valid_Operand;
613 -------------------
614 -- Is_Same_Value --
615 -------------------
617 function Is_Same_Value (L, R : Node_Id) return Boolean is
618 Lf : constant Node_Id := Compare_Fixup (L);
619 Rf : constant Node_Id := Compare_Fixup (R);
621 function Is_Same_Subscript (L, R : List_Id) return Boolean;
622 -- L, R are the Expressions values from two attribute nodes for First
623 -- or Last attributes. Either may be set to No_List if no expressions
624 -- are present (indicating subscript 1). The result is True if both
625 -- expressions represent the same subscript (note one case is where
626 -- one subscript is missing and the other is explicitly set to 1).
628 -----------------------
629 -- Is_Same_Subscript --
630 -----------------------
632 function Is_Same_Subscript (L, R : List_Id) return Boolean is
633 begin
634 if L = No_List then
635 if R = No_List then
636 return True;
637 else
638 return Expr_Value (First (R)) = Uint_1;
639 end if;
641 else
642 if R = No_List then
643 return Expr_Value (First (L)) = Uint_1;
644 else
645 return Expr_Value (First (L)) = Expr_Value (First (R));
646 end if;
647 end if;
648 end Is_Same_Subscript;
650 -- Start of processing for Is_Same_Value
652 begin
653 -- Values are the same if they refer to the same entity and the
654 -- entity is non-volatile. This does not however apply to Float
655 -- types, since we may have two NaN values and they should never
656 -- compare equal.
658 -- If the entity is a discriminant, the two expressions may be bounds
659 -- of components of objects of the same discriminated type. The
660 -- values of the discriminants are not static, and therefore the
661 -- result is unknown.
663 -- It would be better to comment individual branches of this test ???
665 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
666 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
667 and then Entity (Lf) = Entity (Rf)
668 and then Ekind (Entity (Lf)) /= E_Discriminant
669 and then Present (Entity (Lf))
670 and then not Is_Floating_Point_Type (Etype (L))
671 and then not Is_Volatile_Reference (L)
672 and then not Is_Volatile_Reference (R)
673 then
674 return True;
676 -- Or if they are compile time known and identical
678 elsif Compile_Time_Known_Value (Lf)
679 and then
680 Compile_Time_Known_Value (Rf)
681 and then Expr_Value (Lf) = Expr_Value (Rf)
682 then
683 return True;
685 -- False if Nkind of the two nodes is different for remaining cases
687 elsif Nkind (Lf) /= Nkind (Rf) then
688 return False;
690 -- True if both 'First or 'Last values applying to the same entity
691 -- (first and last don't change even if value does). Note that we
692 -- need this even with the calls to Compare_Fixup, to handle the
693 -- case of unconstrained array attributes where Compare_Fixup
694 -- cannot find useful bounds.
696 elsif Nkind (Lf) = N_Attribute_Reference
697 and then Attribute_Name (Lf) = Attribute_Name (Rf)
698 and then Nam_In (Attribute_Name (Lf), Name_First, Name_Last)
699 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
700 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
701 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
702 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
703 then
704 return True;
706 -- True if the same selected component from the same record
708 elsif Nkind (Lf) = N_Selected_Component
709 and then Selector_Name (Lf) = Selector_Name (Rf)
710 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
711 then
712 return True;
714 -- True if the same unary operator applied to the same operand
716 elsif Nkind (Lf) in N_Unary_Op
717 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
718 then
719 return True;
721 -- True if the same binary operator applied to the same operands
723 elsif Nkind (Lf) in N_Binary_Op
724 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
725 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
726 then
727 return True;
729 -- All other cases, we can't tell, so return False
731 else
732 return False;
733 end if;
734 end Is_Same_Value;
736 -- Start of processing for Compile_Time_Compare
738 begin
739 Diff.all := No_Uint;
741 -- In preanalysis mode, always return Unknown unless the expression
742 -- is static. It is too early to be thinking we know the result of a
743 -- comparison, save that judgment for the full analysis. This is
744 -- particularly important in the case of pre and postconditions, which
745 -- otherwise can be prematurely collapsed into having True or False
746 -- conditions when this is inappropriate.
748 if not (Full_Analysis
749 or else (Is_Static_Expression (L)
750 and then
751 Is_Static_Expression (R)))
752 then
753 return Unknown;
754 end if;
756 -- If either operand could raise constraint error, then we cannot
757 -- know the result at compile time (since CE may be raised!)
759 if not (Cannot_Raise_Constraint_Error (L)
760 and then
761 Cannot_Raise_Constraint_Error (R))
762 then
763 return Unknown;
764 end if;
766 -- Identical operands are most certainly equal
768 if L = R then
769 return EQ;
771 -- If expressions have no types, then do not attempt to determine if
772 -- they are the same, since something funny is going on. One case in
773 -- which this happens is during generic template analysis, when bounds
774 -- are not fully analyzed.
776 elsif No (Ltyp) or else No (Rtyp) then
777 return Unknown;
779 -- We do not attempt comparisons for packed arrays arrays represented as
780 -- modular types, where the semantics of comparison is quite different.
782 elsif Is_Packed_Array_Type (Ltyp)
783 and then Is_Modular_Integer_Type (Ltyp)
784 then
785 return Unknown;
787 -- For access types, the only time we know the result at compile time
788 -- (apart from identical operands, which we handled already) is if we
789 -- know one operand is null and the other is not, or both operands are
790 -- known null.
792 elsif Is_Access_Type (Ltyp) then
793 if Known_Null (L) then
794 if Known_Null (R) then
795 return EQ;
796 elsif Known_Non_Null (R) then
797 return NE;
798 else
799 return Unknown;
800 end if;
802 elsif Known_Non_Null (L) and then Known_Null (R) then
803 return NE;
805 else
806 return Unknown;
807 end if;
809 -- Case where comparison involves two compile time known values
811 elsif Compile_Time_Known_Value (L)
812 and then Compile_Time_Known_Value (R)
813 then
814 -- For the floating-point case, we have to be a little careful, since
815 -- at compile time we are dealing with universal exact values, but at
816 -- runtime, these will be in non-exact target form. That's why the
817 -- returned results are LE and GE below instead of LT and GT.
819 if Is_Floating_Point_Type (Ltyp)
820 or else
821 Is_Floating_Point_Type (Rtyp)
822 then
823 declare
824 Lo : constant Ureal := Expr_Value_R (L);
825 Hi : constant Ureal := Expr_Value_R (R);
827 begin
828 if Lo < Hi then
829 return LE;
830 elsif Lo = Hi then
831 return EQ;
832 else
833 return GE;
834 end if;
835 end;
837 -- For string types, we have two string literals and we proceed to
838 -- compare them using the Ada style dictionary string comparison.
840 elsif not Is_Scalar_Type (Ltyp) then
841 declare
842 Lstring : constant String_Id := Strval (Expr_Value_S (L));
843 Rstring : constant String_Id := Strval (Expr_Value_S (R));
844 Llen : constant Nat := String_Length (Lstring);
845 Rlen : constant Nat := String_Length (Rstring);
847 begin
848 for J in 1 .. Nat'Min (Llen, Rlen) loop
849 declare
850 LC : constant Char_Code := Get_String_Char (Lstring, J);
851 RC : constant Char_Code := Get_String_Char (Rstring, J);
852 begin
853 if LC < RC then
854 return LT;
855 elsif LC > RC then
856 return GT;
857 end if;
858 end;
859 end loop;
861 if Llen < Rlen then
862 return LT;
863 elsif Llen > Rlen then
864 return GT;
865 else
866 return EQ;
867 end if;
868 end;
870 -- For remaining scalar cases we know exactly (note that this does
871 -- include the fixed-point case, where we know the run time integer
872 -- values now).
874 else
875 declare
876 Lo : constant Uint := Expr_Value (L);
877 Hi : constant Uint := Expr_Value (R);
879 begin
880 if Lo < Hi then
881 Diff.all := Hi - Lo;
882 return LT;
884 elsif Lo = Hi then
885 return EQ;
887 else
888 Diff.all := Lo - Hi;
889 return GT;
890 end if;
891 end;
892 end if;
894 -- Cases where at least one operand is not known at compile time
896 else
897 -- Remaining checks apply only for discrete types
899 if not Is_Discrete_Type (Ltyp)
900 or else not Is_Discrete_Type (Rtyp)
901 then
902 return Unknown;
903 end if;
905 -- Defend against generic types, or actually any expressions that
906 -- contain a reference to a generic type from within a generic
907 -- template. We don't want to do any range analysis of such
908 -- expressions for two reasons. First, the bounds of a generic type
909 -- itself are junk and cannot be used for any kind of analysis.
910 -- Second, we may have a case where the range at run time is indeed
911 -- known, but we don't want to do compile time analysis in the
912 -- template based on that range since in an instance the value may be
913 -- static, and able to be elaborated without reference to the bounds
914 -- of types involved. As an example, consider:
916 -- (F'Pos (F'Last) + 1) > Integer'Last
918 -- The expression on the left side of > is Universal_Integer and thus
919 -- acquires the type Integer for evaluation at run time, and at run
920 -- time it is true that this condition is always False, but within
921 -- an instance F may be a type with a static range greater than the
922 -- range of Integer, and the expression statically evaluates to True.
924 if References_Generic_Formal_Type (L)
925 or else
926 References_Generic_Formal_Type (R)
927 then
928 return Unknown;
929 end if;
931 -- Replace types by base types for the case of entities which are
932 -- not known to have valid representations. This takes care of
933 -- properly dealing with invalid representations.
935 if not Assume_Valid and then not Assume_No_Invalid_Values then
936 if Is_Entity_Name (L) and then not Is_Known_Valid (Entity (L)) then
937 Ltyp := Underlying_Type (Base_Type (Ltyp));
938 end if;
940 if Is_Entity_Name (R) and then not Is_Known_Valid (Entity (R)) then
941 Rtyp := Underlying_Type (Base_Type (Rtyp));
942 end if;
943 end if;
945 -- First attempt is to decompose the expressions to extract a
946 -- constant offset resulting from the use of any of the forms:
948 -- expr + literal
949 -- expr - literal
950 -- typ'Succ (expr)
951 -- typ'Pred (expr)
953 -- Then we see if the two expressions are the same value, and if so
954 -- the result is obtained by comparing the offsets.
956 -- Note: the reason we do this test first is that it returns only
957 -- decisive results (with diff set), where other tests, like the
958 -- range test, may not be as so decisive. Consider for example
959 -- J .. J + 1. This code can conclude LT with a difference of 1,
960 -- even if the range of J is not known.
962 declare
963 Lnode : Node_Id;
964 Loffs : Uint;
965 Rnode : Node_Id;
966 Roffs : Uint;
968 begin
969 Compare_Decompose (L, Lnode, Loffs);
970 Compare_Decompose (R, Rnode, Roffs);
972 if Is_Same_Value (Lnode, Rnode) then
973 if Loffs = Roffs then
974 return EQ;
976 elsif Loffs < Roffs then
977 Diff.all := Roffs - Loffs;
978 return LT;
980 else
981 Diff.all := Loffs - Roffs;
982 return GT;
983 end if;
984 end if;
985 end;
987 -- Next, try range analysis and see if operand ranges are disjoint
989 declare
990 LOK, ROK : Boolean;
991 LLo, LHi : Uint;
992 RLo, RHi : Uint;
994 Single : Boolean;
995 -- True if each range is a single point
997 begin
998 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
999 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
1001 if LOK and ROK then
1002 Single := (LLo = LHi) and then (RLo = RHi);
1004 if LHi < RLo then
1005 if Single and Assume_Valid then
1006 Diff.all := RLo - LLo;
1007 end if;
1009 return LT;
1011 elsif RHi < LLo then
1012 if Single and Assume_Valid then
1013 Diff.all := LLo - RLo;
1014 end if;
1016 return GT;
1018 elsif Single and then LLo = RLo then
1020 -- If the range includes a single literal and we can assume
1021 -- validity then the result is known even if an operand is
1022 -- not static.
1024 if Assume_Valid then
1025 return EQ;
1026 else
1027 return Unknown;
1028 end if;
1030 elsif LHi = RLo then
1031 return LE;
1033 elsif RHi = LLo then
1034 return GE;
1036 elsif not Is_Known_Valid_Operand (L)
1037 and then not Assume_Valid
1038 then
1039 if Is_Same_Value (L, R) then
1040 return EQ;
1041 else
1042 return Unknown;
1043 end if;
1044 end if;
1046 -- If the range of either operand cannot be determined, nothing
1047 -- further can be inferred.
1049 else
1050 return Unknown;
1051 end if;
1052 end;
1054 -- Here is where we check for comparisons against maximum bounds of
1055 -- types, where we know that no value can be outside the bounds of
1056 -- the subtype. Note that this routine is allowed to assume that all
1057 -- expressions are within their subtype bounds. Callers wishing to
1058 -- deal with possibly invalid values must in any case take special
1059 -- steps (e.g. conversions to larger types) to avoid this kind of
1060 -- optimization, which is always considered to be valid. We do not
1061 -- attempt this optimization with generic types, since the type
1062 -- bounds may not be meaningful in this case.
1064 -- We are in danger of an infinite recursion here. It does not seem
1065 -- useful to go more than one level deep, so the parameter Rec is
1066 -- used to protect ourselves against this infinite recursion.
1068 if not Rec then
1070 -- See if we can get a decisive check against one operand and
1071 -- a bound of the other operand (four possible tests here).
1072 -- Note that we avoid testing junk bounds of a generic type.
1074 if not Is_Generic_Type (Rtyp) then
1075 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1076 Discard'Access,
1077 Assume_Valid, Rec => True)
1079 when LT => return LT;
1080 when LE => return LE;
1081 when EQ => return LE;
1082 when others => null;
1083 end case;
1085 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1086 Discard'Access,
1087 Assume_Valid, Rec => True)
1089 when GT => return GT;
1090 when GE => return GE;
1091 when EQ => return GE;
1092 when others => null;
1093 end case;
1094 end if;
1096 if not Is_Generic_Type (Ltyp) then
1097 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1098 Discard'Access,
1099 Assume_Valid, Rec => True)
1101 when GT => return GT;
1102 when GE => return GE;
1103 when EQ => return GE;
1104 when others => null;
1105 end case;
1107 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1108 Discard'Access,
1109 Assume_Valid, Rec => True)
1111 when LT => return LT;
1112 when LE => return LE;
1113 when EQ => return LE;
1114 when others => null;
1115 end case;
1116 end if;
1117 end if;
1119 -- Next attempt is to see if we have an entity compared with a
1120 -- compile time known value, where there is a current value
1121 -- conditional for the entity which can tell us the result.
1123 declare
1124 Var : Node_Id;
1125 -- Entity variable (left operand)
1127 Val : Uint;
1128 -- Value (right operand)
1130 Inv : Boolean;
1131 -- If False, we have reversed the operands
1133 Op : Node_Kind;
1134 -- Comparison operator kind from Get_Current_Value_Condition call
1136 Opn : Node_Id;
1137 -- Value from Get_Current_Value_Condition call
1139 Opv : Uint;
1140 -- Value of Opn
1142 Result : Compare_Result;
1143 -- Known result before inversion
1145 begin
1146 if Is_Entity_Name (L)
1147 and then Compile_Time_Known_Value (R)
1148 then
1149 Var := L;
1150 Val := Expr_Value (R);
1151 Inv := False;
1153 elsif Is_Entity_Name (R)
1154 and then Compile_Time_Known_Value (L)
1155 then
1156 Var := R;
1157 Val := Expr_Value (L);
1158 Inv := True;
1160 -- That was the last chance at finding a compile time result
1162 else
1163 return Unknown;
1164 end if;
1166 Get_Current_Value_Condition (Var, Op, Opn);
1168 -- That was the last chance, so if we got nothing return
1170 if No (Opn) then
1171 return Unknown;
1172 end if;
1174 Opv := Expr_Value (Opn);
1176 -- We got a comparison, so we might have something interesting
1178 -- Convert LE to LT and GE to GT, just so we have fewer cases
1180 if Op = N_Op_Le then
1181 Op := N_Op_Lt;
1182 Opv := Opv + 1;
1184 elsif Op = N_Op_Ge then
1185 Op := N_Op_Gt;
1186 Opv := Opv - 1;
1187 end if;
1189 -- Deal with equality case
1191 if Op = N_Op_Eq then
1192 if Val = Opv then
1193 Result := EQ;
1194 elsif Opv < Val then
1195 Result := LT;
1196 else
1197 Result := GT;
1198 end if;
1200 -- Deal with inequality case
1202 elsif Op = N_Op_Ne then
1203 if Val = Opv then
1204 Result := NE;
1205 else
1206 return Unknown;
1207 end if;
1209 -- Deal with greater than case
1211 elsif Op = N_Op_Gt then
1212 if Opv >= Val then
1213 Result := GT;
1214 elsif Opv = Val - 1 then
1215 Result := GE;
1216 else
1217 return Unknown;
1218 end if;
1220 -- Deal with less than case
1222 else pragma Assert (Op = N_Op_Lt);
1223 if Opv <= Val then
1224 Result := LT;
1225 elsif Opv = Val + 1 then
1226 Result := LE;
1227 else
1228 return Unknown;
1229 end if;
1230 end if;
1232 -- Deal with inverting result
1234 if Inv then
1235 case Result is
1236 when GT => return LT;
1237 when GE => return LE;
1238 when LT => return GT;
1239 when LE => return GE;
1240 when others => return Result;
1241 end case;
1242 end if;
1244 return Result;
1245 end;
1246 end if;
1247 end Compile_Time_Compare;
1249 -------------------------------
1250 -- Compile_Time_Known_Bounds --
1251 -------------------------------
1253 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1254 Indx : Node_Id;
1255 Typ : Entity_Id;
1257 begin
1258 if not Is_Array_Type (T) then
1259 return False;
1260 end if;
1262 Indx := First_Index (T);
1263 while Present (Indx) loop
1264 Typ := Underlying_Type (Etype (Indx));
1266 -- Never look at junk bounds of a generic type
1268 if Is_Generic_Type (Typ) then
1269 return False;
1270 end if;
1272 -- Otherwise check bounds for compile time known
1274 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1275 return False;
1276 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1277 return False;
1278 else
1279 Next_Index (Indx);
1280 end if;
1281 end loop;
1283 return True;
1284 end Compile_Time_Known_Bounds;
1286 ------------------------------
1287 -- Compile_Time_Known_Value --
1288 ------------------------------
1290 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1291 K : constant Node_Kind := Nkind (Op);
1292 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1294 begin
1295 -- Never known at compile time if bad type or raises constraint error
1296 -- or empty (latter case occurs only as a result of a previous error).
1298 if No (Op) then
1299 Check_Error_Detected;
1300 return False;
1302 elsif Op = Error
1303 or else Etype (Op) = Any_Type
1304 or else Raises_Constraint_Error (Op)
1305 then
1306 return False;
1307 end if;
1309 -- If this is not a static expression or a null literal, and we are in
1310 -- configurable run-time mode, then we consider it not known at compile
1311 -- time. This avoids anomalies where whether something is allowed with a
1312 -- given configurable run-time library depends on how good the compiler
1313 -- is at optimizing and knowing that things are constant when they are
1314 -- nonstatic.
1316 if Configurable_Run_Time_Mode
1317 and then K /= N_Null
1318 and then not Is_Static_Expression (Op)
1319 then
1320 -- We make an exception for expressions that evaluate to True/False,
1321 -- to suppress spurious checks in ZFP mode. So far we have not seen
1322 -- any negative consequences of this exception.
1324 if Is_Entity_Name (Op)
1325 and then Ekind (Entity (Op)) = E_Enumeration_Literal
1326 and then Etype (Entity (Op)) = Standard_Boolean
1327 then
1328 null;
1330 else
1331 return False;
1332 end if;
1333 end if;
1335 -- If we have an entity name, then see if it is the name of a constant
1336 -- and if so, test the corresponding constant value, or the name of
1337 -- an enumeration literal, which is always a constant.
1339 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1340 declare
1341 E : constant Entity_Id := Entity (Op);
1342 V : Node_Id;
1344 begin
1345 -- Never known at compile time if it is a packed array value.
1346 -- We might want to try to evaluate these at compile time one
1347 -- day, but we do not make that attempt now.
1349 if Is_Packed_Array_Type (Etype (Op)) then
1350 return False;
1351 end if;
1353 if Ekind (E) = E_Enumeration_Literal then
1354 return True;
1356 -- In SPARK mode, the value of deferred constants should be
1357 -- ignored outside the scope of their full view. This allows
1358 -- parameterized formal verification, in which a deferred constant
1359 -- value if not known from client units.
1361 elsif Ekind (E) = E_Constant
1362 and then not (SPARK_Mode
1363 and then Present (Full_View (E))
1364 and then not In_Open_Scopes (Scope (E)))
1365 then
1366 V := Constant_Value (E);
1367 return Present (V) and then Compile_Time_Known_Value (V);
1368 end if;
1369 end;
1371 -- We have a value, see if it is compile time known
1373 else
1374 -- Integer literals are worth storing in the cache
1376 if K = N_Integer_Literal then
1377 CV_Ent.N := Op;
1378 CV_Ent.V := Intval (Op);
1379 return True;
1381 -- Other literals and NULL are known at compile time
1383 elsif
1384 K = N_Character_Literal
1385 or else
1386 K = N_Real_Literal
1387 or else
1388 K = N_String_Literal
1389 or else
1390 K = N_Null
1391 then
1392 return True;
1394 -- Any reference to Null_Parameter is known at compile time. No
1395 -- other attribute references (that have not already been folded)
1396 -- are known at compile time.
1398 elsif K = N_Attribute_Reference then
1399 return Attribute_Name (Op) = Name_Null_Parameter;
1400 end if;
1401 end if;
1403 -- If we fall through, not known at compile time
1405 return False;
1407 -- If we get an exception while trying to do this test, then some error
1408 -- has occurred, and we simply say that the value is not known after all
1410 exception
1411 when others =>
1412 return False;
1413 end Compile_Time_Known_Value;
1415 --------------------------------------
1416 -- Compile_Time_Known_Value_Or_Aggr --
1417 --------------------------------------
1419 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1420 begin
1421 -- If we have an entity name, then see if it is the name of a constant
1422 -- and if so, test the corresponding constant value, or the name of
1423 -- an enumeration literal, which is always a constant.
1425 if Is_Entity_Name (Op) then
1426 declare
1427 E : constant Entity_Id := Entity (Op);
1428 V : Node_Id;
1430 begin
1431 if Ekind (E) = E_Enumeration_Literal then
1432 return True;
1434 elsif Ekind (E) /= E_Constant then
1435 return False;
1437 else
1438 V := Constant_Value (E);
1439 return Present (V)
1440 and then Compile_Time_Known_Value_Or_Aggr (V);
1441 end if;
1442 end;
1444 -- We have a value, see if it is compile time known
1446 else
1447 if Compile_Time_Known_Value (Op) then
1448 return True;
1450 elsif Nkind (Op) = N_Aggregate then
1452 if Present (Expressions (Op)) then
1453 declare
1454 Expr : Node_Id;
1456 begin
1457 Expr := First (Expressions (Op));
1458 while Present (Expr) loop
1459 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1460 return False;
1461 end if;
1463 Next (Expr);
1464 end loop;
1465 end;
1466 end if;
1468 if Present (Component_Associations (Op)) then
1469 declare
1470 Cass : Node_Id;
1472 begin
1473 Cass := First (Component_Associations (Op));
1474 while Present (Cass) loop
1475 if not
1476 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1477 then
1478 return False;
1479 end if;
1481 Next (Cass);
1482 end loop;
1483 end;
1484 end if;
1486 return True;
1488 -- All other types of values are not known at compile time
1490 else
1491 return False;
1492 end if;
1494 end if;
1495 end Compile_Time_Known_Value_Or_Aggr;
1497 -----------------
1498 -- Eval_Actual --
1499 -----------------
1501 -- This is only called for actuals of functions that are not predefined
1502 -- operators (which have already been rewritten as operators at this
1503 -- stage), so the call can never be folded, and all that needs doing for
1504 -- the actual is to do the check for a non-static context.
1506 procedure Eval_Actual (N : Node_Id) is
1507 begin
1508 Check_Non_Static_Context (N);
1509 end Eval_Actual;
1511 --------------------
1512 -- Eval_Allocator --
1513 --------------------
1515 -- Allocators are never static, so all we have to do is to do the
1516 -- check for a non-static context if an expression is present.
1518 procedure Eval_Allocator (N : Node_Id) is
1519 Expr : constant Node_Id := Expression (N);
1521 begin
1522 if Nkind (Expr) = N_Qualified_Expression then
1523 Check_Non_Static_Context (Expression (Expr));
1524 end if;
1525 end Eval_Allocator;
1527 ------------------------
1528 -- Eval_Arithmetic_Op --
1529 ------------------------
1531 -- Arithmetic operations are static functions, so the result is static
1532 -- if both operands are static (RM 4.9(7), 4.9(20)).
1534 procedure Eval_Arithmetic_Op (N : Node_Id) is
1535 Left : constant Node_Id := Left_Opnd (N);
1536 Right : constant Node_Id := Right_Opnd (N);
1537 Ltype : constant Entity_Id := Etype (Left);
1538 Rtype : constant Entity_Id := Etype (Right);
1539 Otype : Entity_Id := Empty;
1540 Stat : Boolean;
1541 Fold : Boolean;
1543 begin
1544 -- If not foldable we are done
1546 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1548 if not Fold then
1549 return;
1550 end if;
1552 if Is_Universal_Numeric_Type (Etype (Left))
1553 and then
1554 Is_Universal_Numeric_Type (Etype (Right))
1555 then
1556 Otype := Find_Universal_Operator_Type (N);
1557 end if;
1559 -- Fold for cases where both operands are of integer type
1561 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1562 declare
1563 Left_Int : constant Uint := Expr_Value (Left);
1564 Right_Int : constant Uint := Expr_Value (Right);
1565 Result : Uint;
1567 begin
1568 case Nkind (N) is
1570 when N_Op_Add =>
1571 Result := Left_Int + Right_Int;
1573 when N_Op_Subtract =>
1574 Result := Left_Int - Right_Int;
1576 when N_Op_Multiply =>
1577 if OK_Bits
1578 (N, UI_From_Int
1579 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1580 then
1581 Result := Left_Int * Right_Int;
1582 else
1583 Result := Left_Int;
1584 end if;
1586 when N_Op_Divide =>
1588 -- The exception Constraint_Error is raised by integer
1589 -- division, rem and mod if the right operand is zero.
1591 if Right_Int = 0 then
1592 Apply_Compile_Time_Constraint_Error
1593 (N, "division by zero",
1594 CE_Divide_By_Zero,
1595 Warn => not Stat);
1596 return;
1598 else
1599 Result := Left_Int / Right_Int;
1600 end if;
1602 when N_Op_Mod =>
1604 -- The exception Constraint_Error is raised by integer
1605 -- division, rem and mod if the right operand is zero.
1607 if Right_Int = 0 then
1608 Apply_Compile_Time_Constraint_Error
1609 (N, "mod with zero divisor",
1610 CE_Divide_By_Zero,
1611 Warn => not Stat);
1612 return;
1613 else
1614 Result := Left_Int mod Right_Int;
1615 end if;
1617 when N_Op_Rem =>
1619 -- The exception Constraint_Error is raised by integer
1620 -- division, rem and mod if the right operand is zero.
1622 if Right_Int = 0 then
1623 Apply_Compile_Time_Constraint_Error
1624 (N, "rem with zero divisor",
1625 CE_Divide_By_Zero,
1626 Warn => not Stat);
1627 return;
1629 else
1630 Result := Left_Int rem Right_Int;
1631 end if;
1633 when others =>
1634 raise Program_Error;
1635 end case;
1637 -- Adjust the result by the modulus if the type is a modular type
1639 if Is_Modular_Integer_Type (Ltype) then
1640 Result := Result mod Modulus (Ltype);
1642 -- For a signed integer type, check non-static overflow
1644 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1645 declare
1646 BT : constant Entity_Id := Base_Type (Ltype);
1647 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1648 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1649 begin
1650 if Result < Lo or else Result > Hi then
1651 Apply_Compile_Time_Constraint_Error
1652 (N, "value not in range of }??",
1653 CE_Overflow_Check_Failed,
1654 Ent => BT);
1655 return;
1656 end if;
1657 end;
1658 end if;
1660 -- If we get here we can fold the result
1662 Fold_Uint (N, Result, Stat);
1663 end;
1665 -- Cases where at least one operand is a real. We handle the cases of
1666 -- both reals, or mixed/real integer cases (the latter happen only for
1667 -- divide and multiply, and the result is always real).
1669 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1670 declare
1671 Left_Real : Ureal;
1672 Right_Real : Ureal;
1673 Result : Ureal;
1675 begin
1676 if Is_Real_Type (Ltype) then
1677 Left_Real := Expr_Value_R (Left);
1678 else
1679 Left_Real := UR_From_Uint (Expr_Value (Left));
1680 end if;
1682 if Is_Real_Type (Rtype) then
1683 Right_Real := Expr_Value_R (Right);
1684 else
1685 Right_Real := UR_From_Uint (Expr_Value (Right));
1686 end if;
1688 if Nkind (N) = N_Op_Add then
1689 Result := Left_Real + Right_Real;
1691 elsif Nkind (N) = N_Op_Subtract then
1692 Result := Left_Real - Right_Real;
1694 elsif Nkind (N) = N_Op_Multiply then
1695 Result := Left_Real * Right_Real;
1697 else pragma Assert (Nkind (N) = N_Op_Divide);
1698 if UR_Is_Zero (Right_Real) then
1699 Apply_Compile_Time_Constraint_Error
1700 (N, "division by zero", CE_Divide_By_Zero);
1701 return;
1702 end if;
1704 Result := Left_Real / Right_Real;
1705 end if;
1707 Fold_Ureal (N, Result, Stat);
1708 end;
1709 end if;
1711 -- If the operator was resolved to a specific type, make sure that type
1712 -- is frozen even if the expression is folded into a literal (which has
1713 -- a universal type).
1715 if Present (Otype) then
1716 Freeze_Before (N, Otype);
1717 end if;
1718 end Eval_Arithmetic_Op;
1720 ----------------------------
1721 -- Eval_Character_Literal --
1722 ----------------------------
1724 -- Nothing to be done!
1726 procedure Eval_Character_Literal (N : Node_Id) is
1727 pragma Warnings (Off, N);
1728 begin
1729 null;
1730 end Eval_Character_Literal;
1732 ---------------
1733 -- Eval_Call --
1734 ---------------
1736 -- Static function calls are either calls to predefined operators
1737 -- with static arguments, or calls to functions that rename a literal.
1738 -- Only the latter case is handled here, predefined operators are
1739 -- constant-folded elsewhere.
1741 -- If the function is itself inherited (see 7423-001) the literal of
1742 -- the parent type must be explicitly converted to the return type
1743 -- of the function.
1745 procedure Eval_Call (N : Node_Id) is
1746 Loc : constant Source_Ptr := Sloc (N);
1747 Typ : constant Entity_Id := Etype (N);
1748 Lit : Entity_Id;
1750 begin
1751 if Nkind (N) = N_Function_Call
1752 and then No (Parameter_Associations (N))
1753 and then Is_Entity_Name (Name (N))
1754 and then Present (Alias (Entity (Name (N))))
1755 and then Is_Enumeration_Type (Base_Type (Typ))
1756 then
1757 Lit := Ultimate_Alias (Entity (Name (N)));
1759 if Ekind (Lit) = E_Enumeration_Literal then
1760 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1761 Rewrite
1762 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1763 else
1764 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1765 end if;
1767 Resolve (N, Typ);
1768 end if;
1769 end if;
1770 end Eval_Call;
1772 --------------------------
1773 -- Eval_Case_Expression --
1774 --------------------------
1776 -- A conditional expression is static if all its conditions and dependent
1777 -- expressions are static.
1779 procedure Eval_Case_Expression (N : Node_Id) is
1780 Alt : Node_Id;
1781 Choice : Node_Id;
1782 Is_Static : Boolean;
1783 Result : Node_Id;
1784 Val : Uint;
1786 begin
1787 Result := Empty;
1788 Is_Static := True;
1790 if Is_Static_Expression (Expression (N)) then
1791 Val := Expr_Value (Expression (N));
1793 else
1794 Check_Non_Static_Context (Expression (N));
1795 Is_Static := False;
1796 end if;
1798 Alt := First (Alternatives (N));
1800 Search : while Present (Alt) loop
1801 if not Is_Static
1802 or else not Is_Static_Expression (Expression (Alt))
1803 then
1804 Check_Non_Static_Context (Expression (Alt));
1805 Is_Static := False;
1807 else
1808 Choice := First (Discrete_Choices (Alt));
1809 while Present (Choice) loop
1810 if Nkind (Choice) = N_Others_Choice then
1811 Result := Expression (Alt);
1812 exit Search;
1814 elsif Expr_Value (Choice) = Val then
1815 Result := Expression (Alt);
1816 exit Search;
1818 else
1819 Next (Choice);
1820 end if;
1821 end loop;
1822 end if;
1824 Next (Alt);
1825 end loop Search;
1827 if Is_Static then
1828 Rewrite (N, Relocate_Node (Result));
1830 else
1831 Set_Is_Static_Expression (N, False);
1832 end if;
1833 end Eval_Case_Expression;
1835 ------------------------
1836 -- Eval_Concatenation --
1837 ------------------------
1839 -- Concatenation is a static function, so the result is static if both
1840 -- operands are static (RM 4.9(7), 4.9(21)).
1842 procedure Eval_Concatenation (N : Node_Id) is
1843 Left : constant Node_Id := Left_Opnd (N);
1844 Right : constant Node_Id := Right_Opnd (N);
1845 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1846 Stat : Boolean;
1847 Fold : Boolean;
1849 begin
1850 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
1851 -- non-static context.
1853 if Ada_Version = Ada_83
1854 and then Comes_From_Source (N)
1855 then
1856 Check_Non_Static_Context (Left);
1857 Check_Non_Static_Context (Right);
1858 return;
1859 end if;
1861 -- If not foldable we are done. In principle concatenation that yields
1862 -- any string type is static (i.e. an array type of character types).
1863 -- However, character types can include enumeration literals, and
1864 -- concatenation in that case cannot be described by a literal, so we
1865 -- only consider the operation static if the result is an array of
1866 -- (a descendant of) a predefined character type.
1868 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1870 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
1871 Set_Is_Static_Expression (N, False);
1872 return;
1873 end if;
1875 -- Compile time string concatenation
1877 -- ??? Note that operands that are aggregates can be marked as static,
1878 -- so we should attempt at a later stage to fold concatenations with
1879 -- such aggregates.
1881 declare
1882 Left_Str : constant Node_Id := Get_String_Val (Left);
1883 Left_Len : Nat;
1884 Right_Str : constant Node_Id := Get_String_Val (Right);
1885 Folded_Val : String_Id;
1887 begin
1888 -- Establish new string literal, and store left operand. We make
1889 -- sure to use the special Start_String that takes an operand if
1890 -- the left operand is a string literal. Since this is optimized
1891 -- in the case where that is the most recently created string
1892 -- literal, we ensure efficient time/space behavior for the
1893 -- case of a concatenation of a series of string literals.
1895 if Nkind (Left_Str) = N_String_Literal then
1896 Left_Len := String_Length (Strval (Left_Str));
1898 -- If the left operand is the empty string, and the right operand
1899 -- is a string literal (the case of "" & "..."), the result is the
1900 -- value of the right operand. This optimization is important when
1901 -- Is_Folded_In_Parser, to avoid copying an enormous right
1902 -- operand.
1904 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1905 Folded_Val := Strval (Right_Str);
1906 else
1907 Start_String (Strval (Left_Str));
1908 end if;
1910 else
1911 Start_String;
1912 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1913 Left_Len := 1;
1914 end if;
1916 -- Now append the characters of the right operand, unless we
1917 -- optimized the "" & "..." case above.
1919 if Nkind (Right_Str) = N_String_Literal then
1920 if Left_Len /= 0 then
1921 Store_String_Chars (Strval (Right_Str));
1922 Folded_Val := End_String;
1923 end if;
1924 else
1925 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1926 Folded_Val := End_String;
1927 end if;
1929 Set_Is_Static_Expression (N, Stat);
1931 -- If left operand is the empty string, the result is the
1932 -- right operand, including its bounds if anomalous.
1934 if Left_Len = 0
1935 and then Is_Array_Type (Etype (Right))
1936 and then Etype (Right) /= Any_String
1937 then
1938 Set_Etype (N, Etype (Right));
1939 end if;
1941 Fold_Str (N, Folded_Val, Static => Stat);
1942 end;
1943 end Eval_Concatenation;
1945 ----------------------
1946 -- Eval_Entity_Name --
1947 ----------------------
1949 -- This procedure is used for identifiers and expanded names other than
1950 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1951 -- static if they denote a static constant (RM 4.9(6)) or if the name
1952 -- denotes an enumeration literal (RM 4.9(22)).
1954 procedure Eval_Entity_Name (N : Node_Id) is
1955 Def_Id : constant Entity_Id := Entity (N);
1956 Val : Node_Id;
1958 begin
1959 -- Enumeration literals are always considered to be constants
1960 -- and cannot raise constraint error (RM 4.9(22)).
1962 if Ekind (Def_Id) = E_Enumeration_Literal then
1963 Set_Is_Static_Expression (N);
1964 return;
1966 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1967 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1968 -- it does not violate 10.2.1(8) here, since this is not a variable.
1970 elsif Ekind (Def_Id) = E_Constant then
1972 -- Deferred constants must always be treated as nonstatic
1973 -- outside the scope of their full view.
1975 if Present (Full_View (Def_Id))
1976 and then not In_Open_Scopes (Scope (Def_Id))
1977 then
1978 Val := Empty;
1979 else
1980 Val := Constant_Value (Def_Id);
1981 end if;
1983 if Present (Val) then
1984 Set_Is_Static_Expression
1985 (N, Is_Static_Expression (Val)
1986 and then Is_Static_Subtype (Etype (Def_Id)));
1987 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1989 if not Is_Static_Expression (N)
1990 and then not Is_Generic_Type (Etype (N))
1991 then
1992 Validate_Static_Object_Name (N);
1993 end if;
1995 return;
1996 end if;
1997 end if;
1999 -- Fall through if the name is not static
2001 Validate_Static_Object_Name (N);
2002 end Eval_Entity_Name;
2004 ------------------------
2005 -- Eval_If_Expression --
2006 ------------------------
2008 -- We can fold to a static expression if the condition and both dependent
2009 -- expressions are static. Otherwise, the only required processing is to do
2010 -- the check for non-static context for the then and else expressions.
2012 procedure Eval_If_Expression (N : Node_Id) is
2013 Condition : constant Node_Id := First (Expressions (N));
2014 Then_Expr : constant Node_Id := Next (Condition);
2015 Else_Expr : constant Node_Id := Next (Then_Expr);
2016 Result : Node_Id;
2017 Non_Result : Node_Id;
2019 Rstat : constant Boolean :=
2020 Is_Static_Expression (Condition)
2021 and then
2022 Is_Static_Expression (Then_Expr)
2023 and then
2024 Is_Static_Expression (Else_Expr);
2026 begin
2027 -- If any operand is Any_Type, just propagate to result and do not try
2028 -- to fold, this prevents cascaded errors.
2030 if Etype (Condition) = Any_Type or else
2031 Etype (Then_Expr) = Any_Type or else
2032 Etype (Else_Expr) = Any_Type
2033 then
2034 Set_Etype (N, Any_Type);
2035 Set_Is_Static_Expression (N, False);
2036 return;
2038 -- Static case where we can fold. Note that we don't try to fold cases
2039 -- where the condition is known at compile time, but the result is
2040 -- non-static. This avoids possible cases of infinite recursion where
2041 -- the expander puts in a redundant test and we remove it. Instead we
2042 -- deal with these cases in the expander.
2044 elsif Rstat then
2046 -- Select result operand
2048 if Is_True (Expr_Value (Condition)) then
2049 Result := Then_Expr;
2050 Non_Result := Else_Expr;
2051 else
2052 Result := Else_Expr;
2053 Non_Result := Then_Expr;
2054 end if;
2056 -- Note that it does not matter if the non-result operand raises a
2057 -- Constraint_Error, but if the result raises constraint error then
2058 -- we replace the node with a raise constraint error. This will
2059 -- properly propagate Raises_Constraint_Error since this flag is
2060 -- set in Result.
2062 if Raises_Constraint_Error (Result) then
2063 Rewrite_In_Raise_CE (N, Result);
2064 Check_Non_Static_Context (Non_Result);
2066 -- Otherwise the result operand replaces the original node
2068 else
2069 Rewrite (N, Relocate_Node (Result));
2070 end if;
2072 -- Case of condition not known at compile time
2074 else
2075 Check_Non_Static_Context (Condition);
2076 Check_Non_Static_Context (Then_Expr);
2077 Check_Non_Static_Context (Else_Expr);
2078 end if;
2080 Set_Is_Static_Expression (N, Rstat);
2081 end Eval_If_Expression;
2083 ----------------------------
2084 -- Eval_Indexed_Component --
2085 ----------------------------
2087 -- Indexed components are never static, so we need to perform the check
2088 -- for non-static context on the index values. Then, we check if the
2089 -- value can be obtained at compile time, even though it is non-static.
2091 procedure Eval_Indexed_Component (N : Node_Id) is
2092 Expr : Node_Id;
2094 begin
2095 -- Check for non-static context on index values
2097 Expr := First (Expressions (N));
2098 while Present (Expr) loop
2099 Check_Non_Static_Context (Expr);
2100 Next (Expr);
2101 end loop;
2103 -- If the indexed component appears in an object renaming declaration
2104 -- then we do not want to try to evaluate it, since in this case we
2105 -- need the identity of the array element.
2107 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2108 return;
2110 -- Similarly if the indexed component appears as the prefix of an
2111 -- attribute we don't want to evaluate it, because at least for
2112 -- some cases of attributes we need the identify (e.g. Access, Size)
2114 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2115 return;
2116 end if;
2118 -- Note: there are other cases, such as the left side of an assignment,
2119 -- or an OUT parameter for a call, where the replacement results in the
2120 -- illegal use of a constant, But these cases are illegal in the first
2121 -- place, so the replacement, though silly, is harmless.
2123 -- Now see if this is a constant array reference
2125 if List_Length (Expressions (N)) = 1
2126 and then Is_Entity_Name (Prefix (N))
2127 and then Ekind (Entity (Prefix (N))) = E_Constant
2128 and then Present (Constant_Value (Entity (Prefix (N))))
2129 then
2130 declare
2131 Loc : constant Source_Ptr := Sloc (N);
2132 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2133 Sub : constant Node_Id := First (Expressions (N));
2135 Atyp : Entity_Id;
2136 -- Type of array
2138 Lin : Nat;
2139 -- Linear one's origin subscript value for array reference
2141 Lbd : Node_Id;
2142 -- Lower bound of the first array index
2144 Elm : Node_Id;
2145 -- Value from constant array
2147 begin
2148 Atyp := Etype (Arr);
2150 if Is_Access_Type (Atyp) then
2151 Atyp := Designated_Type (Atyp);
2152 end if;
2154 -- If we have an array type (we should have but perhaps there are
2155 -- error cases where this is not the case), then see if we can do
2156 -- a constant evaluation of the array reference.
2158 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
2159 if Ekind (Atyp) = E_String_Literal_Subtype then
2160 Lbd := String_Literal_Low_Bound (Atyp);
2161 else
2162 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2163 end if;
2165 if Compile_Time_Known_Value (Sub)
2166 and then Nkind (Arr) = N_Aggregate
2167 and then Compile_Time_Known_Value (Lbd)
2168 and then Is_Discrete_Type (Component_Type (Atyp))
2169 then
2170 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2172 if List_Length (Expressions (Arr)) >= Lin then
2173 Elm := Pick (Expressions (Arr), Lin);
2175 -- If the resulting expression is compile time known,
2176 -- then we can rewrite the indexed component with this
2177 -- value, being sure to mark the result as non-static.
2178 -- We also reset the Sloc, in case this generates an
2179 -- error later on (e.g. 136'Access).
2181 if Compile_Time_Known_Value (Elm) then
2182 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2183 Set_Is_Static_Expression (N, False);
2184 Set_Sloc (N, Loc);
2185 end if;
2186 end if;
2188 -- We can also constant-fold if the prefix is a string literal.
2189 -- This will be useful in an instantiation or an inlining.
2191 elsif Compile_Time_Known_Value (Sub)
2192 and then Nkind (Arr) = N_String_Literal
2193 and then Compile_Time_Known_Value (Lbd)
2194 and then Expr_Value (Lbd) = 1
2195 and then Expr_Value (Sub) <=
2196 String_Literal_Length (Etype (Arr))
2197 then
2198 declare
2199 C : constant Char_Code :=
2200 Get_String_Char (Strval (Arr),
2201 UI_To_Int (Expr_Value (Sub)));
2202 begin
2203 Set_Character_Literal_Name (C);
2205 Elm :=
2206 Make_Character_Literal (Loc,
2207 Chars => Name_Find,
2208 Char_Literal_Value => UI_From_CC (C));
2209 Set_Etype (Elm, Component_Type (Atyp));
2210 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2211 Set_Is_Static_Expression (N, False);
2212 end;
2213 end if;
2214 end if;
2215 end;
2216 end if;
2217 end Eval_Indexed_Component;
2219 --------------------------
2220 -- Eval_Integer_Literal --
2221 --------------------------
2223 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2224 -- as static by the analyzer. The reason we did it that early is to allow
2225 -- the possibility of turning off the Is_Static_Expression flag after
2226 -- analysis, but before resolution, when integer literals are generated in
2227 -- the expander that do not correspond to static expressions.
2229 procedure Eval_Integer_Literal (N : Node_Id) is
2230 T : constant Entity_Id := Etype (N);
2232 function In_Any_Integer_Context return Boolean;
2233 -- If the literal is resolved with a specific type in a context where
2234 -- the expected type is Any_Integer, there are no range checks on the
2235 -- literal. By the time the literal is evaluated, it carries the type
2236 -- imposed by the enclosing expression, and we must recover the context
2237 -- to determine that Any_Integer is meant.
2239 ----------------------------
2240 -- In_Any_Integer_Context --
2241 ----------------------------
2243 function In_Any_Integer_Context return Boolean is
2244 Par : constant Node_Id := Parent (N);
2245 K : constant Node_Kind := Nkind (Par);
2247 begin
2248 -- Any_Integer also appears in digits specifications for real types,
2249 -- but those have bounds smaller that those of any integer base type,
2250 -- so we can safely ignore these cases.
2252 return K = N_Number_Declaration
2253 or else K = N_Attribute_Reference
2254 or else K = N_Attribute_Definition_Clause
2255 or else K = N_Modular_Type_Definition
2256 or else K = N_Signed_Integer_Type_Definition;
2257 end In_Any_Integer_Context;
2259 -- Start of processing for Eval_Integer_Literal
2261 begin
2263 -- If the literal appears in a non-expression context, then it is
2264 -- certainly appearing in a non-static context, so check it. This is
2265 -- actually a redundant check, since Check_Non_Static_Context would
2266 -- check it, but it seems worth while avoiding the call.
2268 if Nkind (Parent (N)) not in N_Subexpr
2269 and then not In_Any_Integer_Context
2270 then
2271 Check_Non_Static_Context (N);
2272 end if;
2274 -- Modular integer literals must be in their base range
2276 if Is_Modular_Integer_Type (T)
2277 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
2278 then
2279 Out_Of_Range (N);
2280 end if;
2281 end Eval_Integer_Literal;
2283 ---------------------
2284 -- Eval_Logical_Op --
2285 ---------------------
2287 -- Logical operations are static functions, so the result is potentially
2288 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2290 procedure Eval_Logical_Op (N : Node_Id) is
2291 Left : constant Node_Id := Left_Opnd (N);
2292 Right : constant Node_Id := Right_Opnd (N);
2293 Stat : Boolean;
2294 Fold : Boolean;
2296 begin
2297 -- If not foldable we are done
2299 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2301 if not Fold then
2302 return;
2303 end if;
2305 -- Compile time evaluation of logical operation
2307 declare
2308 Left_Int : constant Uint := Expr_Value (Left);
2309 Right_Int : constant Uint := Expr_Value (Right);
2311 begin
2312 -- VMS includes bitwise operations on signed types
2314 if Is_Modular_Integer_Type (Etype (N))
2315 or else Is_VMS_Operator (Entity (N))
2316 then
2317 declare
2318 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2319 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2321 begin
2322 To_Bits (Left_Int, Left_Bits);
2323 To_Bits (Right_Int, Right_Bits);
2325 -- Note: should really be able to use array ops instead of
2326 -- these loops, but they weren't working at the time ???
2328 if Nkind (N) = N_Op_And then
2329 for J in Left_Bits'Range loop
2330 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2331 end loop;
2333 elsif Nkind (N) = N_Op_Or then
2334 for J in Left_Bits'Range loop
2335 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2336 end loop;
2338 else
2339 pragma Assert (Nkind (N) = N_Op_Xor);
2341 for J in Left_Bits'Range loop
2342 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2343 end loop;
2344 end if;
2346 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
2347 end;
2349 else
2350 pragma Assert (Is_Boolean_Type (Etype (N)));
2352 if Nkind (N) = N_Op_And then
2353 Fold_Uint (N,
2354 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
2356 elsif Nkind (N) = N_Op_Or then
2357 Fold_Uint (N,
2358 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
2360 else
2361 pragma Assert (Nkind (N) = N_Op_Xor);
2362 Fold_Uint (N,
2363 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
2364 end if;
2365 end if;
2366 end;
2367 end Eval_Logical_Op;
2369 ------------------------
2370 -- Eval_Membership_Op --
2371 ------------------------
2373 -- A membership test is potentially static if the expression is static, and
2374 -- the range is a potentially static range, or is a subtype mark denoting a
2375 -- static subtype (RM 4.9(12)).
2377 procedure Eval_Membership_Op (N : Node_Id) is
2378 Left : constant Node_Id := Left_Opnd (N);
2379 Right : constant Node_Id := Right_Opnd (N);
2380 Def_Id : Entity_Id;
2381 Lo : Node_Id;
2382 Hi : Node_Id;
2383 Result : Boolean;
2384 Stat : Boolean;
2385 Fold : Boolean;
2387 begin
2388 -- Ignore if error in either operand, except to make sure that Any_Type
2389 -- is properly propagated to avoid junk cascaded errors.
2391 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2392 Set_Etype (N, Any_Type);
2393 return;
2394 end if;
2396 -- Ignore if types involved have predicates
2398 if Present (Predicate_Function (Etype (Left)))
2399 or else
2400 Present (Predicate_Function (Etype (Right)))
2401 then
2402 return;
2403 end if;
2405 -- Case of right operand is a subtype name
2407 if Is_Entity_Name (Right) then
2408 Def_Id := Entity (Right);
2410 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
2411 and then Is_OK_Static_Subtype (Def_Id)
2412 then
2413 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2415 if not Fold or else not Stat then
2416 return;
2417 end if;
2418 else
2419 Check_Non_Static_Context (Left);
2420 return;
2421 end if;
2423 -- For string membership tests we will check the length further on
2425 if not Is_String_Type (Def_Id) then
2426 Lo := Type_Low_Bound (Def_Id);
2427 Hi := Type_High_Bound (Def_Id);
2429 else
2430 Lo := Empty;
2431 Hi := Empty;
2432 end if;
2434 -- Case of right operand is a range
2436 else
2437 if Is_Static_Range (Right) then
2438 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2440 if not Fold or else not Stat then
2441 return;
2443 -- If one bound of range raises CE, then don't try to fold
2445 elsif not Is_OK_Static_Range (Right) then
2446 Check_Non_Static_Context (Left);
2447 return;
2448 end if;
2450 else
2451 Check_Non_Static_Context (Left);
2452 return;
2453 end if;
2455 -- Here we know range is an OK static range
2457 Lo := Low_Bound (Right);
2458 Hi := High_Bound (Right);
2459 end if;
2461 -- For strings we check that the length of the string expression is
2462 -- compatible with the string subtype if the subtype is constrained,
2463 -- or if unconstrained then the test is always true.
2465 if Is_String_Type (Etype (Right)) then
2466 if not Is_Constrained (Etype (Right)) then
2467 Result := True;
2469 else
2470 declare
2471 Typlen : constant Uint := String_Type_Len (Etype (Right));
2472 Strlen : constant Uint :=
2473 UI_From_Int
2474 (String_Length (Strval (Get_String_Val (Left))));
2475 begin
2476 Result := (Typlen = Strlen);
2477 end;
2478 end if;
2480 -- Fold the membership test. We know we have a static range and Lo and
2481 -- Hi are set to the expressions for the end points of this range.
2483 elsif Is_Real_Type (Etype (Right)) then
2484 declare
2485 Leftval : constant Ureal := Expr_Value_R (Left);
2487 begin
2488 Result := Expr_Value_R (Lo) <= Leftval
2489 and then Leftval <= Expr_Value_R (Hi);
2490 end;
2492 else
2493 declare
2494 Leftval : constant Uint := Expr_Value (Left);
2496 begin
2497 Result := Expr_Value (Lo) <= Leftval
2498 and then Leftval <= Expr_Value (Hi);
2499 end;
2500 end if;
2502 if Nkind (N) = N_Not_In then
2503 Result := not Result;
2504 end if;
2506 Fold_Uint (N, Test (Result), True);
2508 Warn_On_Known_Condition (N);
2509 end Eval_Membership_Op;
2511 ------------------------
2512 -- Eval_Named_Integer --
2513 ------------------------
2515 procedure Eval_Named_Integer (N : Node_Id) is
2516 begin
2517 Fold_Uint (N,
2518 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2519 end Eval_Named_Integer;
2521 ---------------------
2522 -- Eval_Named_Real --
2523 ---------------------
2525 procedure Eval_Named_Real (N : Node_Id) is
2526 begin
2527 Fold_Ureal (N,
2528 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2529 end Eval_Named_Real;
2531 -------------------
2532 -- Eval_Op_Expon --
2533 -------------------
2535 -- Exponentiation is a static functions, so the result is potentially
2536 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2538 procedure Eval_Op_Expon (N : Node_Id) is
2539 Left : constant Node_Id := Left_Opnd (N);
2540 Right : constant Node_Id := Right_Opnd (N);
2541 Stat : Boolean;
2542 Fold : Boolean;
2544 begin
2545 -- If not foldable we are done
2547 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2549 if not Fold then
2550 return;
2551 end if;
2553 -- Fold exponentiation operation
2555 declare
2556 Right_Int : constant Uint := Expr_Value (Right);
2558 begin
2559 -- Integer case
2561 if Is_Integer_Type (Etype (Left)) then
2562 declare
2563 Left_Int : constant Uint := Expr_Value (Left);
2564 Result : Uint;
2566 begin
2567 -- Exponentiation of an integer raises Constraint_Error for a
2568 -- negative exponent (RM 4.5.6).
2570 if Right_Int < 0 then
2571 Apply_Compile_Time_Constraint_Error
2572 (N, "integer exponent negative",
2573 CE_Range_Check_Failed,
2574 Warn => not Stat);
2575 return;
2577 else
2578 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2579 Result := Left_Int ** Right_Int;
2580 else
2581 Result := Left_Int;
2582 end if;
2584 if Is_Modular_Integer_Type (Etype (N)) then
2585 Result := Result mod Modulus (Etype (N));
2586 end if;
2588 Fold_Uint (N, Result, Stat);
2589 end if;
2590 end;
2592 -- Real case
2594 else
2595 declare
2596 Left_Real : constant Ureal := Expr_Value_R (Left);
2598 begin
2599 -- Cannot have a zero base with a negative exponent
2601 if UR_Is_Zero (Left_Real) then
2603 if Right_Int < 0 then
2604 Apply_Compile_Time_Constraint_Error
2605 (N, "zero ** negative integer",
2606 CE_Range_Check_Failed,
2607 Warn => not Stat);
2608 return;
2609 else
2610 Fold_Ureal (N, Ureal_0, Stat);
2611 end if;
2613 else
2614 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2615 end if;
2616 end;
2617 end if;
2618 end;
2619 end Eval_Op_Expon;
2621 -----------------
2622 -- Eval_Op_Not --
2623 -----------------
2625 -- The not operation is a static functions, so the result is potentially
2626 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2628 procedure Eval_Op_Not (N : Node_Id) is
2629 Right : constant Node_Id := Right_Opnd (N);
2630 Stat : Boolean;
2631 Fold : Boolean;
2633 begin
2634 -- If not foldable we are done
2636 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2638 if not Fold then
2639 return;
2640 end if;
2642 -- Fold not operation
2644 declare
2645 Rint : constant Uint := Expr_Value (Right);
2646 Typ : constant Entity_Id := Etype (N);
2648 begin
2649 -- Negation is equivalent to subtracting from the modulus minus one.
2650 -- For a binary modulus this is equivalent to the ones-complement of
2651 -- the original value. For non-binary modulus this is an arbitrary
2652 -- but consistent definition.
2654 if Is_Modular_Integer_Type (Typ) then
2655 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2657 else
2658 pragma Assert (Is_Boolean_Type (Typ));
2659 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2660 end if;
2662 Set_Is_Static_Expression (N, Stat);
2663 end;
2664 end Eval_Op_Not;
2666 -------------------------------
2667 -- Eval_Qualified_Expression --
2668 -------------------------------
2670 -- A qualified expression is potentially static if its subtype mark denotes
2671 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2673 procedure Eval_Qualified_Expression (N : Node_Id) is
2674 Operand : constant Node_Id := Expression (N);
2675 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2677 Stat : Boolean;
2678 Fold : Boolean;
2679 Hex : Boolean;
2681 begin
2682 -- Can only fold if target is string or scalar and subtype is static.
2683 -- Also, do not fold if our parent is an allocator (this is because the
2684 -- qualified expression is really part of the syntactic structure of an
2685 -- allocator, and we do not want to end up with something that
2686 -- corresponds to "new 1" where the 1 is the result of folding a
2687 -- qualified expression).
2689 if not Is_Static_Subtype (Target_Type)
2690 or else Nkind (Parent (N)) = N_Allocator
2691 then
2692 Check_Non_Static_Context (Operand);
2694 -- If operand is known to raise constraint_error, set the flag on the
2695 -- expression so it does not get optimized away.
2697 if Nkind (Operand) = N_Raise_Constraint_Error then
2698 Set_Raises_Constraint_Error (N);
2699 end if;
2701 return;
2702 end if;
2704 -- If not foldable we are done
2706 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2708 if not Fold then
2709 return;
2711 -- Don't try fold if target type has constraint error bounds
2713 elsif not Is_OK_Static_Subtype (Target_Type) then
2714 Set_Raises_Constraint_Error (N);
2715 return;
2716 end if;
2718 -- Here we will fold, save Print_In_Hex indication
2720 Hex := Nkind (Operand) = N_Integer_Literal
2721 and then Print_In_Hex (Operand);
2723 -- Fold the result of qualification
2725 if Is_Discrete_Type (Target_Type) then
2726 Fold_Uint (N, Expr_Value (Operand), Stat);
2728 -- Preserve Print_In_Hex indication
2730 if Hex and then Nkind (N) = N_Integer_Literal then
2731 Set_Print_In_Hex (N);
2732 end if;
2734 elsif Is_Real_Type (Target_Type) then
2735 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2737 else
2738 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2740 if not Stat then
2741 Set_Is_Static_Expression (N, False);
2742 else
2743 Check_String_Literal_Length (N, Target_Type);
2744 end if;
2746 return;
2747 end if;
2749 -- The expression may be foldable but not static
2751 Set_Is_Static_Expression (N, Stat);
2753 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
2754 Out_Of_Range (N);
2755 end if;
2756 end Eval_Qualified_Expression;
2758 -----------------------
2759 -- Eval_Real_Literal --
2760 -----------------------
2762 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2763 -- as static by the analyzer. The reason we did it that early is to allow
2764 -- the possibility of turning off the Is_Static_Expression flag after
2765 -- analysis, but before resolution, when integer literals are generated
2766 -- in the expander that do not correspond to static expressions.
2768 procedure Eval_Real_Literal (N : Node_Id) is
2769 PK : constant Node_Kind := Nkind (Parent (N));
2771 begin
2772 -- If the literal appears in a non-expression context and not as part of
2773 -- a number declaration, then it is appearing in a non-static context,
2774 -- so check it.
2776 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
2777 Check_Non_Static_Context (N);
2778 end if;
2779 end Eval_Real_Literal;
2781 ------------------------
2782 -- Eval_Relational_Op --
2783 ------------------------
2785 -- Relational operations are static functions, so the result is static if
2786 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
2787 -- the result is never static, even if the operands are.
2789 procedure Eval_Relational_Op (N : Node_Id) is
2790 Left : constant Node_Id := Left_Opnd (N);
2791 Right : constant Node_Id := Right_Opnd (N);
2792 Typ : constant Entity_Id := Etype (Left);
2793 Otype : Entity_Id := Empty;
2794 Result : Boolean;
2796 begin
2797 -- One special case to deal with first. If we can tell that the result
2798 -- will be false because the lengths of one or more index subtypes are
2799 -- compile time known and different, then we can replace the entire
2800 -- result by False. We only do this for one dimensional arrays, because
2801 -- the case of multi-dimensional arrays is rare and too much trouble! If
2802 -- one of the operands is an illegal aggregate, its type might still be
2803 -- an arbitrary composite type, so nothing to do.
2805 if Is_Array_Type (Typ)
2806 and then Typ /= Any_Composite
2807 and then Number_Dimensions (Typ) = 1
2808 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
2809 then
2810 if Raises_Constraint_Error (Left)
2811 or else Raises_Constraint_Error (Right)
2812 then
2813 return;
2814 end if;
2816 -- OK, we have the case where we may be able to do this fold
2818 Length_Mismatch : declare
2819 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2820 -- If Op is an expression for a constrained array with a known at
2821 -- compile time length, then Len is set to this (non-negative
2822 -- length). Otherwise Len is set to minus 1.
2824 -----------------------
2825 -- Get_Static_Length --
2826 -----------------------
2828 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2829 T : Entity_Id;
2831 begin
2832 -- First easy case string literal
2834 if Nkind (Op) = N_String_Literal then
2835 Len := UI_From_Int (String_Length (Strval (Op)));
2836 return;
2837 end if;
2839 -- Second easy case, not constrained subtype, so no length
2841 if not Is_Constrained (Etype (Op)) then
2842 Len := Uint_Minus_1;
2843 return;
2844 end if;
2846 -- General case
2848 T := Etype (First_Index (Etype (Op)));
2850 -- The simple case, both bounds are known at compile time
2852 if Is_Discrete_Type (T)
2853 and then
2854 Compile_Time_Known_Value (Type_Low_Bound (T))
2855 and then
2856 Compile_Time_Known_Value (Type_High_Bound (T))
2857 then
2858 Len := UI_Max (Uint_0,
2859 Expr_Value (Type_High_Bound (T)) -
2860 Expr_Value (Type_Low_Bound (T)) + 1);
2861 return;
2862 end if;
2864 -- A more complex case, where the bounds are of the form
2865 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2866 -- either A'First or A'Last (with A an entity name), or X is an
2867 -- entity name, and the two X's are the same and K1 and K2 are
2868 -- known at compile time, in this case, the length can also be
2869 -- computed at compile time, even though the bounds are not
2870 -- known. A common case of this is e.g. (X'First .. X'First+5).
2872 Extract_Length : declare
2873 procedure Decompose_Expr
2874 (Expr : Node_Id;
2875 Ent : out Entity_Id;
2876 Kind : out Character;
2877 Cons : out Uint);
2878 -- Given an expression, see if is of the form above,
2879 -- X [+/- K]. If so Ent is set to the entity in X,
2880 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2881 -- and Cons is the value of K. If the expression is
2882 -- not of the required form, Ent is set to Empty.
2884 --------------------
2885 -- Decompose_Expr --
2886 --------------------
2888 procedure Decompose_Expr
2889 (Expr : Node_Id;
2890 Ent : out Entity_Id;
2891 Kind : out Character;
2892 Cons : out Uint)
2894 Exp : Node_Id;
2896 begin
2897 if Nkind (Expr) = N_Op_Add
2898 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2899 then
2900 Exp := Left_Opnd (Expr);
2901 Cons := Expr_Value (Right_Opnd (Expr));
2903 elsif Nkind (Expr) = N_Op_Subtract
2904 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2905 then
2906 Exp := Left_Opnd (Expr);
2907 Cons := -Expr_Value (Right_Opnd (Expr));
2909 -- If the bound is a constant created to remove side
2910 -- effects, recover original expression to see if it has
2911 -- one of the recognizable forms.
2913 elsif Nkind (Expr) = N_Identifier
2914 and then not Comes_From_Source (Entity (Expr))
2915 and then Ekind (Entity (Expr)) = E_Constant
2916 and then
2917 Nkind (Parent (Entity (Expr))) = N_Object_Declaration
2918 then
2919 Exp := Expression (Parent (Entity (Expr)));
2920 Decompose_Expr (Exp, Ent, Kind, Cons);
2922 -- If original expression includes an entity, create a
2923 -- reference to it for use below.
2925 if Present (Ent) then
2926 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
2927 end if;
2929 else
2930 Exp := Expr;
2931 Cons := Uint_0;
2932 end if;
2934 -- At this stage Exp is set to the potential X
2936 if Nkind (Exp) = N_Attribute_Reference then
2937 if Attribute_Name (Exp) = Name_First then
2938 Kind := 'F';
2940 elsif Attribute_Name (Exp) = Name_Last then
2941 Kind := 'L';
2943 else
2944 Ent := Empty;
2945 return;
2946 end if;
2948 Exp := Prefix (Exp);
2950 else
2951 Kind := 'E';
2952 end if;
2954 if Is_Entity_Name (Exp)
2955 and then Present (Entity (Exp))
2956 then
2957 Ent := Entity (Exp);
2958 else
2959 Ent := Empty;
2960 end if;
2961 end Decompose_Expr;
2963 -- Local Variables
2965 Ent1, Ent2 : Entity_Id;
2966 Kind1, Kind2 : Character;
2967 Cons1, Cons2 : Uint;
2969 -- Start of processing for Extract_Length
2971 begin
2972 Decompose_Expr
2973 (Original_Node (Type_Low_Bound (T)), Ent1, Kind1, Cons1);
2974 Decompose_Expr
2975 (Original_Node (Type_High_Bound (T)), Ent2, Kind2, Cons2);
2977 if Present (Ent1)
2978 and then Kind1 = Kind2
2979 and then Ent1 = Ent2
2980 then
2981 Len := Cons2 - Cons1 + 1;
2982 else
2983 Len := Uint_Minus_1;
2984 end if;
2985 end Extract_Length;
2986 end Get_Static_Length;
2988 -- Local Variables
2990 Len_L : Uint;
2991 Len_R : Uint;
2993 -- Start of processing for Length_Mismatch
2995 begin
2996 Get_Static_Length (Left, Len_L);
2997 Get_Static_Length (Right, Len_R);
2999 if Len_L /= Uint_Minus_1
3000 and then Len_R /= Uint_Minus_1
3001 and then Len_L /= Len_R
3002 then
3003 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
3004 Warn_On_Known_Condition (N);
3005 return;
3006 end if;
3007 end Length_Mismatch;
3008 end if;
3010 declare
3011 Is_Static_Expression : Boolean;
3012 Is_Foldable : Boolean;
3013 pragma Unreferenced (Is_Foldable);
3015 begin
3016 -- Initialize the value of Is_Static_Expression. The value of
3017 -- Is_Foldable returned by Test_Expression_Is_Foldable is not needed
3018 -- since, even when some operand is a variable, we can still perform
3019 -- the static evaluation of the expression in some cases (for
3020 -- example, for a variable of a subtype of Integer we statically
3021 -- know that any value stored in such variable is smaller than
3022 -- Integer'Last).
3024 Test_Expression_Is_Foldable
3025 (N, Left, Right, Is_Static_Expression, Is_Foldable);
3027 -- Only comparisons of scalars can give static results. In
3028 -- particular, comparisons of strings never yield a static
3029 -- result, even if both operands are static strings.
3031 if not Is_Scalar_Type (Typ) then
3032 Is_Static_Expression := False;
3033 Set_Is_Static_Expression (N, False);
3034 end if;
3036 -- For operators on universal numeric types called as functions with
3037 -- an explicit scope, determine appropriate specific numeric type,
3038 -- and diagnose possible ambiguity.
3040 if Is_Universal_Numeric_Type (Etype (Left))
3041 and then
3042 Is_Universal_Numeric_Type (Etype (Right))
3043 then
3044 Otype := Find_Universal_Operator_Type (N);
3045 end if;
3047 -- For static real type expressions, we cannot use
3048 -- Compile_Time_Compare since it worries about run-time
3049 -- results which are not exact.
3051 if Is_Static_Expression and then Is_Real_Type (Typ) then
3052 declare
3053 Left_Real : constant Ureal := Expr_Value_R (Left);
3054 Right_Real : constant Ureal := Expr_Value_R (Right);
3056 begin
3057 case Nkind (N) is
3058 when N_Op_Eq => Result := (Left_Real = Right_Real);
3059 when N_Op_Ne => Result := (Left_Real /= Right_Real);
3060 when N_Op_Lt => Result := (Left_Real < Right_Real);
3061 when N_Op_Le => Result := (Left_Real <= Right_Real);
3062 when N_Op_Gt => Result := (Left_Real > Right_Real);
3063 when N_Op_Ge => Result := (Left_Real >= Right_Real);
3065 when others =>
3066 raise Program_Error;
3067 end case;
3069 Fold_Uint (N, Test (Result), True);
3070 end;
3072 -- For all other cases, we use Compile_Time_Compare to do the compare
3074 else
3075 declare
3076 CR : constant Compare_Result :=
3077 Compile_Time_Compare
3078 (Left, Right, Assume_Valid => False);
3080 begin
3081 if CR = Unknown then
3082 return;
3083 end if;
3085 case Nkind (N) is
3086 when N_Op_Eq =>
3087 if CR = EQ then
3088 Result := True;
3089 elsif CR = NE or else CR = GT or else CR = LT then
3090 Result := False;
3091 else
3092 return;
3093 end if;
3095 when N_Op_Ne =>
3096 if CR = NE or else CR = GT or else CR = LT then
3097 Result := True;
3098 elsif CR = EQ then
3099 Result := False;
3100 else
3101 return;
3102 end if;
3104 when N_Op_Lt =>
3105 if CR = LT then
3106 Result := True;
3107 elsif CR = EQ or else CR = GT or else CR = GE then
3108 Result := False;
3109 else
3110 return;
3111 end if;
3113 when N_Op_Le =>
3114 if CR = LT or else CR = EQ or else CR = LE then
3115 Result := True;
3116 elsif CR = GT then
3117 Result := False;
3118 else
3119 return;
3120 end if;
3122 when N_Op_Gt =>
3123 if CR = GT then
3124 Result := True;
3125 elsif CR = EQ or else CR = LT or else CR = LE then
3126 Result := False;
3127 else
3128 return;
3129 end if;
3131 when N_Op_Ge =>
3132 if CR = GT or else CR = EQ or else CR = GE then
3133 Result := True;
3134 elsif CR = LT then
3135 Result := False;
3136 else
3137 return;
3138 end if;
3140 when others =>
3141 raise Program_Error;
3142 end case;
3143 end;
3145 Fold_Uint (N, Test (Result), Is_Static_Expression);
3146 end if;
3147 end;
3149 -- For the case of a folded relational operator on a specific numeric
3150 -- type, freeze operand type now.
3152 if Present (Otype) then
3153 Freeze_Before (N, Otype);
3154 end if;
3156 Warn_On_Known_Condition (N);
3157 end Eval_Relational_Op;
3159 ----------------
3160 -- Eval_Shift --
3161 ----------------
3163 -- Shift operations are intrinsic operations that can never be static, so
3164 -- the only processing required is to perform the required check for a non
3165 -- static context for the two operands.
3167 -- Actually we could do some compile time evaluation here some time ???
3169 procedure Eval_Shift (N : Node_Id) is
3170 begin
3171 Check_Non_Static_Context (Left_Opnd (N));
3172 Check_Non_Static_Context (Right_Opnd (N));
3173 end Eval_Shift;
3175 ------------------------
3176 -- Eval_Short_Circuit --
3177 ------------------------
3179 -- A short circuit operation is potentially static if both operands are
3180 -- potentially static (RM 4.9 (13)).
3182 procedure Eval_Short_Circuit (N : Node_Id) is
3183 Kind : constant Node_Kind := Nkind (N);
3184 Left : constant Node_Id := Left_Opnd (N);
3185 Right : constant Node_Id := Right_Opnd (N);
3186 Left_Int : Uint;
3188 Rstat : constant Boolean :=
3189 Is_Static_Expression (Left)
3190 and then
3191 Is_Static_Expression (Right);
3193 begin
3194 -- Short circuit operations are never static in Ada 83
3196 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3197 Check_Non_Static_Context (Left);
3198 Check_Non_Static_Context (Right);
3199 return;
3200 end if;
3202 -- Now look at the operands, we can't quite use the normal call to
3203 -- Test_Expression_Is_Foldable here because short circuit operations
3204 -- are a special case, they can still be foldable, even if the right
3205 -- operand raises constraint error.
3207 -- If either operand is Any_Type, just propagate to result and do not
3208 -- try to fold, this prevents cascaded errors.
3210 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3211 Set_Etype (N, Any_Type);
3212 return;
3214 -- If left operand raises constraint error, then replace node N with
3215 -- the raise constraint error node, and we are obviously not foldable.
3216 -- Is_Static_Expression is set from the two operands in the normal way,
3217 -- and we check the right operand if it is in a non-static context.
3219 elsif Raises_Constraint_Error (Left) then
3220 if not Rstat then
3221 Check_Non_Static_Context (Right);
3222 end if;
3224 Rewrite_In_Raise_CE (N, Left);
3225 Set_Is_Static_Expression (N, Rstat);
3226 return;
3228 -- If the result is not static, then we won't in any case fold
3230 elsif not Rstat then
3231 Check_Non_Static_Context (Left);
3232 Check_Non_Static_Context (Right);
3233 return;
3234 end if;
3236 -- Here the result is static, note that, unlike the normal processing
3237 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3238 -- the right operand raises constraint error, that's because it is not
3239 -- significant if the left operand is decisive.
3241 Set_Is_Static_Expression (N);
3243 -- It does not matter if the right operand raises constraint error if
3244 -- it will not be evaluated. So deal specially with the cases where
3245 -- the right operand is not evaluated. Note that we will fold these
3246 -- cases even if the right operand is non-static, which is fine, but
3247 -- of course in these cases the result is not potentially static.
3249 Left_Int := Expr_Value (Left);
3251 if (Kind = N_And_Then and then Is_False (Left_Int))
3252 or else
3253 (Kind = N_Or_Else and then Is_True (Left_Int))
3254 then
3255 Fold_Uint (N, Left_Int, Rstat);
3256 return;
3257 end if;
3259 -- If first operand not decisive, then it does matter if the right
3260 -- operand raises constraint error, since it will be evaluated, so
3261 -- we simply replace the node with the right operand. Note that this
3262 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3263 -- (both are set to True in Right).
3265 if Raises_Constraint_Error (Right) then
3266 Rewrite_In_Raise_CE (N, Right);
3267 Check_Non_Static_Context (Left);
3268 return;
3269 end if;
3271 -- Otherwise the result depends on the right operand
3273 Fold_Uint (N, Expr_Value (Right), Rstat);
3274 return;
3275 end Eval_Short_Circuit;
3277 ----------------
3278 -- Eval_Slice --
3279 ----------------
3281 -- Slices can never be static, so the only processing required is to check
3282 -- for non-static context if an explicit range is given.
3284 procedure Eval_Slice (N : Node_Id) is
3285 Drange : constant Node_Id := Discrete_Range (N);
3286 begin
3287 if Nkind (Drange) = N_Range then
3288 Check_Non_Static_Context (Low_Bound (Drange));
3289 Check_Non_Static_Context (High_Bound (Drange));
3290 end if;
3292 -- A slice of the form A (subtype), when the subtype is the index of
3293 -- the type of A, is redundant, the slice can be replaced with A, and
3294 -- this is worth a warning.
3296 if Is_Entity_Name (Prefix (N)) then
3297 declare
3298 E : constant Entity_Id := Entity (Prefix (N));
3299 T : constant Entity_Id := Etype (E);
3300 begin
3301 if Ekind (E) = E_Constant
3302 and then Is_Array_Type (T)
3303 and then Is_Entity_Name (Drange)
3304 then
3305 if Is_Entity_Name (Original_Node (First_Index (T)))
3306 and then Entity (Original_Node (First_Index (T)))
3307 = Entity (Drange)
3308 then
3309 if Warn_On_Redundant_Constructs then
3310 Error_Msg_N ("redundant slice denotes whole array?r?", N);
3311 end if;
3313 -- The following might be a useful optimization???
3315 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3316 end if;
3317 end if;
3318 end;
3319 end if;
3320 end Eval_Slice;
3322 ---------------------------------
3323 -- Eval_Static_Predicate_Check --
3324 ---------------------------------
3326 function Eval_Static_Predicate_Check
3327 (N : Node_Id;
3328 Typ : Entity_Id) return Boolean
3330 Loc : constant Source_Ptr := Sloc (N);
3331 Pred : constant List_Id := Static_Predicate (Typ);
3332 Test : Node_Id;
3334 begin
3335 if No (Pred) then
3336 return True;
3337 end if;
3339 -- The static predicate is a list of alternatives in the proper format
3340 -- for an Ada 2012 membership test. If the argument is a literal, the
3341 -- membership test can be evaluated statically. The caller transforms
3342 -- a result of False into a static contraint error.
3344 Test := Make_In (Loc,
3345 Left_Opnd => New_Copy_Tree (N),
3346 Right_Opnd => Empty,
3347 Alternatives => Pred);
3348 Analyze_And_Resolve (Test, Standard_Boolean);
3350 return Nkind (Test) = N_Identifier
3351 and then Entity (Test) = Standard_True;
3352 end Eval_Static_Predicate_Check;
3354 -------------------------
3355 -- Eval_String_Literal --
3356 -------------------------
3358 procedure Eval_String_Literal (N : Node_Id) is
3359 Typ : constant Entity_Id := Etype (N);
3360 Bas : constant Entity_Id := Base_Type (Typ);
3361 Xtp : Entity_Id;
3362 Len : Nat;
3363 Lo : Node_Id;
3365 begin
3366 -- Nothing to do if error type (handles cases like default expressions
3367 -- or generics where we have not yet fully resolved the type).
3369 if Bas = Any_Type or else Bas = Any_String then
3370 return;
3371 end if;
3373 -- String literals are static if the subtype is static (RM 4.9(2)), so
3374 -- reset the static expression flag (it was set unconditionally in
3375 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3376 -- the subtype is static by looking at the lower bound.
3378 if Ekind (Typ) = E_String_Literal_Subtype then
3379 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3380 Set_Is_Static_Expression (N, False);
3381 return;
3382 end if;
3384 -- Here if Etype of string literal is normal Etype (not yet possible,
3385 -- but may be possible in future).
3387 elsif not Is_OK_Static_Expression
3388 (Type_Low_Bound (Etype (First_Index (Typ))))
3389 then
3390 Set_Is_Static_Expression (N, False);
3391 return;
3392 end if;
3394 -- If original node was a type conversion, then result if non-static
3396 if Nkind (Original_Node (N)) = N_Type_Conversion then
3397 Set_Is_Static_Expression (N, False);
3398 return;
3399 end if;
3401 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3402 -- if its bounds are outside the index base type and this index type is
3403 -- static. This can happen in only two ways. Either the string literal
3404 -- is too long, or it is null, and the lower bound is type'First. In
3405 -- either case it is the upper bound that is out of range of the index
3406 -- type.
3407 if Ada_Version >= Ada_95 then
3408 if Root_Type (Bas) = Standard_String
3409 or else
3410 Root_Type (Bas) = Standard_Wide_String
3411 or else
3412 Root_Type (Bas) = Standard_Wide_Wide_String
3413 then
3414 Xtp := Standard_Positive;
3415 else
3416 Xtp := Etype (First_Index (Bas));
3417 end if;
3419 if Ekind (Typ) = E_String_Literal_Subtype then
3420 Lo := String_Literal_Low_Bound (Typ);
3421 else
3422 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3423 end if;
3425 -- Check for string too long
3427 Len := String_Length (Strval (N));
3429 if UI_From_Int (Len) > String_Type_Len (Bas) then
3431 -- Issue message. Note that this message is a warning if the
3432 -- string literal is not marked as static (happens in some cases
3433 -- of folding strings known at compile time, but not static).
3434 -- Furthermore in such cases, we reword the message, since there
3435 -- is no string literal in the source program!
3437 if Is_Static_Expression (N) then
3438 Apply_Compile_Time_Constraint_Error
3439 (N, "string literal too long for}", CE_Length_Check_Failed,
3440 Ent => Bas,
3441 Typ => First_Subtype (Bas));
3442 else
3443 Apply_Compile_Time_Constraint_Error
3444 (N, "string value too long for}", CE_Length_Check_Failed,
3445 Ent => Bas,
3446 Typ => First_Subtype (Bas),
3447 Warn => True);
3448 end if;
3450 -- Test for null string not allowed
3452 elsif Len = 0
3453 and then not Is_Generic_Type (Xtp)
3454 and then
3455 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
3456 then
3457 -- Same specialization of message
3459 if Is_Static_Expression (N) then
3460 Apply_Compile_Time_Constraint_Error
3461 (N, "null string literal not allowed for}",
3462 CE_Length_Check_Failed,
3463 Ent => Bas,
3464 Typ => First_Subtype (Bas));
3465 else
3466 Apply_Compile_Time_Constraint_Error
3467 (N, "null string value not allowed for}",
3468 CE_Length_Check_Failed,
3469 Ent => Bas,
3470 Typ => First_Subtype (Bas),
3471 Warn => True);
3472 end if;
3473 end if;
3474 end if;
3475 end Eval_String_Literal;
3477 --------------------------
3478 -- Eval_Type_Conversion --
3479 --------------------------
3481 -- A type conversion is potentially static if its subtype mark is for a
3482 -- static scalar subtype, and its operand expression is potentially static
3483 -- (RM 4.9(10)).
3485 procedure Eval_Type_Conversion (N : Node_Id) is
3486 Operand : constant Node_Id := Expression (N);
3487 Source_Type : constant Entity_Id := Etype (Operand);
3488 Target_Type : constant Entity_Id := Etype (N);
3490 Stat : Boolean;
3491 Fold : Boolean;
3493 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
3494 -- Returns true if type T is an integer type, or if it is a fixed-point
3495 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3496 -- on the conversion node).
3498 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3499 -- Returns true if type T is a floating-point type, or if it is a
3500 -- fixed-point type that is not to be treated as an integer (i.e. the
3501 -- flag Conversion_OK is not set on the conversion node).
3503 ------------------------------
3504 -- To_Be_Treated_As_Integer --
3505 ------------------------------
3507 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3508 begin
3509 return
3510 Is_Integer_Type (T)
3511 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3512 end To_Be_Treated_As_Integer;
3514 ---------------------------
3515 -- To_Be_Treated_As_Real --
3516 ---------------------------
3518 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3519 begin
3520 return
3521 Is_Floating_Point_Type (T)
3522 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3523 end To_Be_Treated_As_Real;
3525 -- Start of processing for Eval_Type_Conversion
3527 begin
3528 -- Cannot fold if target type is non-static or if semantic error
3530 if not Is_Static_Subtype (Target_Type) then
3531 Check_Non_Static_Context (Operand);
3532 return;
3534 elsif Error_Posted (N) then
3535 return;
3536 end if;
3538 -- If not foldable we are done
3540 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3542 if not Fold then
3543 return;
3545 -- Don't try fold if target type has constraint error bounds
3547 elsif not Is_OK_Static_Subtype (Target_Type) then
3548 Set_Raises_Constraint_Error (N);
3549 return;
3550 end if;
3552 -- Remaining processing depends on operand types. Note that in the
3553 -- following type test, fixed-point counts as real unless the flag
3554 -- Conversion_OK is set, in which case it counts as integer.
3556 -- Fold conversion, case of string type. The result is not static
3558 if Is_String_Type (Target_Type) then
3559 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
3561 return;
3563 -- Fold conversion, case of integer target type
3565 elsif To_Be_Treated_As_Integer (Target_Type) then
3566 declare
3567 Result : Uint;
3569 begin
3570 -- Integer to integer conversion
3572 if To_Be_Treated_As_Integer (Source_Type) then
3573 Result := Expr_Value (Operand);
3575 -- Real to integer conversion
3577 else
3578 Result := UR_To_Uint (Expr_Value_R (Operand));
3579 end if;
3581 -- If fixed-point type (Conversion_OK must be set), then the
3582 -- result is logically an integer, but we must replace the
3583 -- conversion with the corresponding real literal, since the
3584 -- type from a semantic point of view is still fixed-point.
3586 if Is_Fixed_Point_Type (Target_Type) then
3587 Fold_Ureal
3588 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3590 -- Otherwise result is integer literal
3592 else
3593 Fold_Uint (N, Result, Stat);
3594 end if;
3595 end;
3597 -- Fold conversion, case of real target type
3599 elsif To_Be_Treated_As_Real (Target_Type) then
3600 declare
3601 Result : Ureal;
3603 begin
3604 if To_Be_Treated_As_Real (Source_Type) then
3605 Result := Expr_Value_R (Operand);
3606 else
3607 Result := UR_From_Uint (Expr_Value (Operand));
3608 end if;
3610 Fold_Ureal (N, Result, Stat);
3611 end;
3613 -- Enumeration types
3615 else
3616 Fold_Uint (N, Expr_Value (Operand), Stat);
3617 end if;
3619 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3620 Out_Of_Range (N);
3621 end if;
3623 end Eval_Type_Conversion;
3625 -------------------
3626 -- Eval_Unary_Op --
3627 -------------------
3629 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3630 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3632 procedure Eval_Unary_Op (N : Node_Id) is
3633 Right : constant Node_Id := Right_Opnd (N);
3634 Otype : Entity_Id := Empty;
3635 Stat : Boolean;
3636 Fold : Boolean;
3638 begin
3639 -- If not foldable we are done
3641 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3643 if not Fold then
3644 return;
3645 end if;
3647 if Etype (Right) = Universal_Integer
3648 or else
3649 Etype (Right) = Universal_Real
3650 then
3651 Otype := Find_Universal_Operator_Type (N);
3652 end if;
3654 -- Fold for integer case
3656 if Is_Integer_Type (Etype (N)) then
3657 declare
3658 Rint : constant Uint := Expr_Value (Right);
3659 Result : Uint;
3661 begin
3662 -- In the case of modular unary plus and abs there is no need
3663 -- to adjust the result of the operation since if the original
3664 -- operand was in bounds the result will be in the bounds of the
3665 -- modular type. However, in the case of modular unary minus the
3666 -- result may go out of the bounds of the modular type and needs
3667 -- adjustment.
3669 if Nkind (N) = N_Op_Plus then
3670 Result := Rint;
3672 elsif Nkind (N) = N_Op_Minus then
3673 if Is_Modular_Integer_Type (Etype (N)) then
3674 Result := (-Rint) mod Modulus (Etype (N));
3675 else
3676 Result := (-Rint);
3677 end if;
3679 else
3680 pragma Assert (Nkind (N) = N_Op_Abs);
3681 Result := abs Rint;
3682 end if;
3684 Fold_Uint (N, Result, Stat);
3685 end;
3687 -- Fold for real case
3689 elsif Is_Real_Type (Etype (N)) then
3690 declare
3691 Rreal : constant Ureal := Expr_Value_R (Right);
3692 Result : Ureal;
3694 begin
3695 if Nkind (N) = N_Op_Plus then
3696 Result := Rreal;
3698 elsif Nkind (N) = N_Op_Minus then
3699 Result := UR_Negate (Rreal);
3701 else
3702 pragma Assert (Nkind (N) = N_Op_Abs);
3703 Result := abs Rreal;
3704 end if;
3706 Fold_Ureal (N, Result, Stat);
3707 end;
3708 end if;
3710 -- If the operator was resolved to a specific type, make sure that type
3711 -- is frozen even if the expression is folded into a literal (which has
3712 -- a universal type).
3714 if Present (Otype) then
3715 Freeze_Before (N, Otype);
3716 end if;
3717 end Eval_Unary_Op;
3719 -------------------------------
3720 -- Eval_Unchecked_Conversion --
3721 -------------------------------
3723 -- Unchecked conversions can never be static, so the only required
3724 -- processing is to check for a non-static context for the operand.
3726 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3727 begin
3728 Check_Non_Static_Context (Expression (N));
3729 end Eval_Unchecked_Conversion;
3731 --------------------
3732 -- Expr_Rep_Value --
3733 --------------------
3735 function Expr_Rep_Value (N : Node_Id) return Uint is
3736 Kind : constant Node_Kind := Nkind (N);
3737 Ent : Entity_Id;
3739 begin
3740 if Is_Entity_Name (N) then
3741 Ent := Entity (N);
3743 -- An enumeration literal that was either in the source or created
3744 -- as a result of static evaluation.
3746 if Ekind (Ent) = E_Enumeration_Literal then
3747 return Enumeration_Rep (Ent);
3749 -- A user defined static constant
3751 else
3752 pragma Assert (Ekind (Ent) = E_Constant);
3753 return Expr_Rep_Value (Constant_Value (Ent));
3754 end if;
3756 -- An integer literal that was either in the source or created as a
3757 -- result of static evaluation.
3759 elsif Kind = N_Integer_Literal then
3760 return Intval (N);
3762 -- A real literal for a fixed-point type. This must be the fixed-point
3763 -- case, either the literal is of a fixed-point type, or it is a bound
3764 -- of a fixed-point type, with type universal real. In either case we
3765 -- obtain the desired value from Corresponding_Integer_Value.
3767 elsif Kind = N_Real_Literal then
3768 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3769 return Corresponding_Integer_Value (N);
3771 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3773 elsif Kind = N_Attribute_Reference
3774 and then Attribute_Name (N) = Name_Null_Parameter
3775 then
3776 return Uint_0;
3778 -- Otherwise must be character literal
3780 else
3781 pragma Assert (Kind = N_Character_Literal);
3782 Ent := Entity (N);
3784 -- Since Character literals of type Standard.Character don't have any
3785 -- defining character literals built for them, they do not have their
3786 -- Entity set, so just use their Char code. Otherwise for user-
3787 -- defined character literals use their Pos value as usual which is
3788 -- the same as the Rep value.
3790 if No (Ent) then
3791 return Char_Literal_Value (N);
3792 else
3793 return Enumeration_Rep (Ent);
3794 end if;
3795 end if;
3796 end Expr_Rep_Value;
3798 ----------------
3799 -- Expr_Value --
3800 ----------------
3802 function Expr_Value (N : Node_Id) return Uint is
3803 Kind : constant Node_Kind := Nkind (N);
3804 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
3805 Ent : Entity_Id;
3806 Val : Uint;
3808 begin
3809 -- If already in cache, then we know it's compile time known and we can
3810 -- return the value that was previously stored in the cache since
3811 -- compile time known values cannot change.
3813 if CV_Ent.N = N then
3814 return CV_Ent.V;
3815 end if;
3817 -- Otherwise proceed to test value
3819 if Is_Entity_Name (N) then
3820 Ent := Entity (N);
3822 -- An enumeration literal that was either in the source or created as
3823 -- a result of static evaluation.
3825 if Ekind (Ent) = E_Enumeration_Literal then
3826 Val := Enumeration_Pos (Ent);
3828 -- A user defined static constant
3830 else
3831 pragma Assert (Ekind (Ent) = E_Constant);
3832 Val := Expr_Value (Constant_Value (Ent));
3833 end if;
3835 -- An integer literal that was either in the source or created as a
3836 -- result of static evaluation.
3838 elsif Kind = N_Integer_Literal then
3839 Val := Intval (N);
3841 -- A real literal for a fixed-point type. This must be the fixed-point
3842 -- case, either the literal is of a fixed-point type, or it is a bound
3843 -- of a fixed-point type, with type universal real. In either case we
3844 -- obtain the desired value from Corresponding_Integer_Value.
3846 elsif Kind = N_Real_Literal then
3848 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3849 Val := Corresponding_Integer_Value (N);
3851 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3853 elsif Kind = N_Attribute_Reference
3854 and then Attribute_Name (N) = Name_Null_Parameter
3855 then
3856 Val := Uint_0;
3858 -- Otherwise must be character literal
3860 else
3861 pragma Assert (Kind = N_Character_Literal);
3862 Ent := Entity (N);
3864 -- Since Character literals of type Standard.Character don't
3865 -- have any defining character literals built for them, they
3866 -- do not have their Entity set, so just use their Char
3867 -- code. Otherwise for user-defined character literals use
3868 -- their Pos value as usual.
3870 if No (Ent) then
3871 Val := Char_Literal_Value (N);
3872 else
3873 Val := Enumeration_Pos (Ent);
3874 end if;
3875 end if;
3877 -- Come here with Val set to value to be returned, set cache
3879 CV_Ent.N := N;
3880 CV_Ent.V := Val;
3881 return Val;
3882 end Expr_Value;
3884 ------------------
3885 -- Expr_Value_E --
3886 ------------------
3888 function Expr_Value_E (N : Node_Id) return Entity_Id is
3889 Ent : constant Entity_Id := Entity (N);
3891 begin
3892 if Ekind (Ent) = E_Enumeration_Literal then
3893 return Ent;
3894 else
3895 pragma Assert (Ekind (Ent) = E_Constant);
3896 return Expr_Value_E (Constant_Value (Ent));
3897 end if;
3898 end Expr_Value_E;
3900 ------------------
3901 -- Expr_Value_R --
3902 ------------------
3904 function Expr_Value_R (N : Node_Id) return Ureal is
3905 Kind : constant Node_Kind := Nkind (N);
3906 Ent : Entity_Id;
3908 begin
3909 if Kind = N_Real_Literal then
3910 return Realval (N);
3912 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3913 Ent := Entity (N);
3914 pragma Assert (Ekind (Ent) = E_Constant);
3915 return Expr_Value_R (Constant_Value (Ent));
3917 elsif Kind = N_Integer_Literal then
3918 return UR_From_Uint (Expr_Value (N));
3920 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3922 elsif Kind = N_Attribute_Reference
3923 and then Attribute_Name (N) = Name_Null_Parameter
3924 then
3925 return Ureal_0;
3926 end if;
3928 -- If we fall through, we have a node that cannot be interpreted as a
3929 -- compile time constant. That is definitely an error.
3931 raise Program_Error;
3932 end Expr_Value_R;
3934 ------------------
3935 -- Expr_Value_S --
3936 ------------------
3938 function Expr_Value_S (N : Node_Id) return Node_Id is
3939 begin
3940 if Nkind (N) = N_String_Literal then
3941 return N;
3942 else
3943 pragma Assert (Ekind (Entity (N)) = E_Constant);
3944 return Expr_Value_S (Constant_Value (Entity (N)));
3945 end if;
3946 end Expr_Value_S;
3948 ----------------------------------
3949 -- Find_Universal_Operator_Type --
3950 ----------------------------------
3952 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
3953 PN : constant Node_Id := Parent (N);
3954 Call : constant Node_Id := Original_Node (N);
3955 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
3957 Is_Fix : constant Boolean :=
3958 Nkind (N) in N_Binary_Op
3959 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
3960 -- A mixed-mode operation in this context indicates the presence of
3961 -- fixed-point type in the designated package.
3963 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
3964 -- Case where N is a relational (or membership) operator (else it is an
3965 -- arithmetic one).
3967 In_Membership : constant Boolean :=
3968 Nkind (PN) in N_Membership_Test
3969 and then
3970 Nkind (Right_Opnd (PN)) = N_Range
3971 and then
3972 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
3973 and then
3974 Is_Universal_Numeric_Type
3975 (Etype (Low_Bound (Right_Opnd (PN))))
3976 and then
3977 Is_Universal_Numeric_Type
3978 (Etype (High_Bound (Right_Opnd (PN))));
3979 -- Case where N is part of a membership test with a universal range
3981 E : Entity_Id;
3982 Pack : Entity_Id;
3983 Typ1 : Entity_Id := Empty;
3984 Priv_E : Entity_Id;
3986 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
3987 -- Check whether one operand is a mixed-mode operation that requires the
3988 -- presence of a fixed-point type. Given that all operands are universal
3989 -- and have been constant-folded, retrieve the original function call.
3991 ---------------------------
3992 -- Is_Mixed_Mode_Operand --
3993 ---------------------------
3995 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
3996 Onod : constant Node_Id := Original_Node (Op);
3997 begin
3998 return Nkind (Onod) = N_Function_Call
3999 and then Present (Next_Actual (First_Actual (Onod)))
4000 and then Etype (First_Actual (Onod)) /=
4001 Etype (Next_Actual (First_Actual (Onod)));
4002 end Is_Mixed_Mode_Operand;
4004 -- Start of processing for Find_Universal_Operator_Type
4006 begin
4007 if Nkind (Call) /= N_Function_Call
4008 or else Nkind (Name (Call)) /= N_Expanded_Name
4009 then
4010 return Empty;
4012 -- There are several cases where the context does not imply the type of
4013 -- the operands:
4014 -- - the universal expression appears in a type conversion;
4015 -- - the expression is a relational operator applied to universal
4016 -- operands;
4017 -- - the expression is a membership test with a universal operand
4018 -- and a range with universal bounds.
4020 elsif Nkind (Parent (N)) = N_Type_Conversion
4021 or else Is_Relational
4022 or else In_Membership
4023 then
4024 Pack := Entity (Prefix (Name (Call)));
4026 -- If the prefix is a package declared elsewhere, iterate over its
4027 -- visible entities, otherwise iterate over all declarations in the
4028 -- designated scope.
4030 if Ekind (Pack) = E_Package
4031 and then not In_Open_Scopes (Pack)
4032 then
4033 Priv_E := First_Private_Entity (Pack);
4034 else
4035 Priv_E := Empty;
4036 end if;
4038 Typ1 := Empty;
4039 E := First_Entity (Pack);
4040 while Present (E) and then E /= Priv_E loop
4041 if Is_Numeric_Type (E)
4042 and then Nkind (Parent (E)) /= N_Subtype_Declaration
4043 and then Comes_From_Source (E)
4044 and then Is_Integer_Type (E) = Is_Int
4045 and then
4046 (Nkind (N) in N_Unary_Op
4047 or else Is_Relational
4048 or else Is_Fixed_Point_Type (E) = Is_Fix)
4049 then
4050 if No (Typ1) then
4051 Typ1 := E;
4053 -- Before emitting an error, check for the presence of a
4054 -- mixed-mode operation that specifies a fixed point type.
4056 elsif Is_Relational
4057 and then
4058 (Is_Mixed_Mode_Operand (Left_Opnd (N))
4059 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
4060 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
4062 then
4063 if Is_Fixed_Point_Type (E) then
4064 Typ1 := E;
4065 end if;
4067 else
4068 -- More than one type of the proper class declared in P
4070 Error_Msg_N ("ambiguous operation", N);
4071 Error_Msg_Sloc := Sloc (Typ1);
4072 Error_Msg_N ("\possible interpretation (inherited)#", N);
4073 Error_Msg_Sloc := Sloc (E);
4074 Error_Msg_N ("\possible interpretation (inherited)#", N);
4075 return Empty;
4076 end if;
4077 end if;
4079 Next_Entity (E);
4080 end loop;
4081 end if;
4083 return Typ1;
4084 end Find_Universal_Operator_Type;
4086 --------------------------
4087 -- Flag_Non_Static_Expr --
4088 --------------------------
4090 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
4091 begin
4092 if Error_Posted (Expr) and then not All_Errors_Mode then
4093 return;
4094 else
4095 Error_Msg_F (Msg, Expr);
4096 Why_Not_Static (Expr);
4097 end if;
4098 end Flag_Non_Static_Expr;
4100 --------------
4101 -- Fold_Str --
4102 --------------
4104 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
4105 Loc : constant Source_Ptr := Sloc (N);
4106 Typ : constant Entity_Id := Etype (N);
4108 begin
4109 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
4111 -- We now have the literal with the right value, both the actual type
4112 -- and the expected type of this literal are taken from the expression
4113 -- that was evaluated. So now we do the Analyze and Resolve.
4115 -- Note that we have to reset Is_Static_Expression both after the
4116 -- analyze step (because Resolve will evaluate the literal, which
4117 -- will cause semantic errors if it is marked as static), and after
4118 -- the Resolve step (since Resolve in some cases resets this flag).
4120 Analyze (N);
4121 Set_Is_Static_Expression (N, Static);
4122 Set_Etype (N, Typ);
4123 Resolve (N);
4124 Set_Is_Static_Expression (N, Static);
4125 end Fold_Str;
4127 ---------------
4128 -- Fold_Uint --
4129 ---------------
4131 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
4132 Loc : constant Source_Ptr := Sloc (N);
4133 Typ : Entity_Id := Etype (N);
4134 Ent : Entity_Id;
4136 begin
4137 -- If we are folding a named number, retain the entity in the literal,
4138 -- for ASIS use.
4140 if Is_Entity_Name (N)
4141 and then Ekind (Entity (N)) = E_Named_Integer
4142 then
4143 Ent := Entity (N);
4144 else
4145 Ent := Empty;
4146 end if;
4148 if Is_Private_Type (Typ) then
4149 Typ := Full_View (Typ);
4150 end if;
4152 -- For a result of type integer, substitute an N_Integer_Literal node
4153 -- for the result of the compile time evaluation of the expression.
4154 -- For ASIS use, set a link to the original named number when not in
4155 -- a generic context.
4157 if Is_Integer_Type (Typ) then
4158 Rewrite (N, Make_Integer_Literal (Loc, Val));
4160 Set_Original_Entity (N, Ent);
4162 -- Otherwise we have an enumeration type, and we substitute either
4163 -- an N_Identifier or N_Character_Literal to represent the enumeration
4164 -- literal corresponding to the given value, which must always be in
4165 -- range, because appropriate tests have already been made for this.
4167 else pragma Assert (Is_Enumeration_Type (Typ));
4168 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4169 end if;
4171 -- We now have the literal with the right value, both the actual type
4172 -- and the expected type of this literal are taken from the expression
4173 -- that was evaluated. So now we do the Analyze and Resolve.
4175 -- Note that we have to reset Is_Static_Expression both after the
4176 -- analyze step (because Resolve will evaluate the literal, which
4177 -- will cause semantic errors if it is marked as static), and after
4178 -- the Resolve step (since Resolve in some cases sets this flag).
4180 Analyze (N);
4181 Set_Is_Static_Expression (N, Static);
4182 Set_Etype (N, Typ);
4183 Resolve (N);
4184 Set_Is_Static_Expression (N, Static);
4185 end Fold_Uint;
4187 ----------------
4188 -- Fold_Ureal --
4189 ----------------
4191 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
4192 Loc : constant Source_Ptr := Sloc (N);
4193 Typ : constant Entity_Id := Etype (N);
4194 Ent : Entity_Id;
4196 begin
4197 -- If we are folding a named number, retain the entity in the literal,
4198 -- for ASIS use.
4200 if Is_Entity_Name (N)
4201 and then Ekind (Entity (N)) = E_Named_Real
4202 then
4203 Ent := Entity (N);
4204 else
4205 Ent := Empty;
4206 end if;
4208 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
4210 -- Set link to original named number, for ASIS use
4212 Set_Original_Entity (N, Ent);
4214 -- We now have the literal with the right value, both the actual type
4215 -- and the expected type of this literal are taken from the expression
4216 -- that was evaluated. So now we do the Analyze and Resolve.
4218 -- Note that we have to reset Is_Static_Expression both after the
4219 -- analyze step (because Resolve will evaluate the literal, which
4220 -- will cause semantic errors if it is marked as static), and after
4221 -- the Resolve step (since Resolve in some cases sets this flag).
4223 Analyze (N);
4224 Set_Is_Static_Expression (N, Static);
4225 Set_Etype (N, Typ);
4226 Resolve (N);
4227 Set_Is_Static_Expression (N, Static);
4228 end Fold_Ureal;
4230 ---------------
4231 -- From_Bits --
4232 ---------------
4234 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4235 V : Uint := Uint_0;
4237 begin
4238 for J in 0 .. B'Last loop
4239 if B (J) then
4240 V := V + 2 ** J;
4241 end if;
4242 end loop;
4244 if Non_Binary_Modulus (T) then
4245 V := V mod Modulus (T);
4246 end if;
4248 return V;
4249 end From_Bits;
4251 --------------------
4252 -- Get_String_Val --
4253 --------------------
4255 function Get_String_Val (N : Node_Id) return Node_Id is
4256 begin
4257 if Nkind (N) = N_String_Literal then
4258 return N;
4260 elsif Nkind (N) = N_Character_Literal then
4261 return N;
4263 else
4264 pragma Assert (Is_Entity_Name (N));
4265 return Get_String_Val (Constant_Value (Entity (N)));
4266 end if;
4267 end Get_String_Val;
4269 ----------------
4270 -- Initialize --
4271 ----------------
4273 procedure Initialize is
4274 begin
4275 CV_Cache := (others => (Node_High_Bound, Uint_0));
4276 end Initialize;
4278 --------------------
4279 -- In_Subrange_Of --
4280 --------------------
4282 function In_Subrange_Of
4283 (T1 : Entity_Id;
4284 T2 : Entity_Id;
4285 Fixed_Int : Boolean := False) return Boolean
4287 L1 : Node_Id;
4288 H1 : Node_Id;
4290 L2 : Node_Id;
4291 H2 : Node_Id;
4293 begin
4294 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4295 return True;
4297 -- Never in range if both types are not scalar. Don't know if this can
4298 -- actually happen, but just in case.
4300 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T2) then
4301 return False;
4303 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4304 -- definitely not compatible with T2.
4306 elsif Is_Floating_Point_Type (T1)
4307 and then Has_Infinities (T1)
4308 and then Is_Floating_Point_Type (T2)
4309 and then not Has_Infinities (T2)
4310 then
4311 return False;
4313 else
4314 L1 := Type_Low_Bound (T1);
4315 H1 := Type_High_Bound (T1);
4317 L2 := Type_Low_Bound (T2);
4318 H2 := Type_High_Bound (T2);
4320 -- Check bounds to see if comparison possible at compile time
4322 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
4323 and then
4324 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
4325 then
4326 return True;
4327 end if;
4329 -- If bounds not comparable at compile time, then the bounds of T2
4330 -- must be compile time known or we cannot answer the query.
4332 if not Compile_Time_Known_Value (L2)
4333 or else not Compile_Time_Known_Value (H2)
4334 then
4335 return False;
4336 end if;
4338 -- If the bounds of T1 are know at compile time then use these
4339 -- ones, otherwise use the bounds of the base type (which are of
4340 -- course always static).
4342 if not Compile_Time_Known_Value (L1) then
4343 L1 := Type_Low_Bound (Base_Type (T1));
4344 end if;
4346 if not Compile_Time_Known_Value (H1) then
4347 H1 := Type_High_Bound (Base_Type (T1));
4348 end if;
4350 -- Fixed point types should be considered as such only if
4351 -- flag Fixed_Int is set to False.
4353 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4354 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4355 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4356 then
4357 return
4358 Expr_Value_R (L2) <= Expr_Value_R (L1)
4359 and then
4360 Expr_Value_R (H2) >= Expr_Value_R (H1);
4362 else
4363 return
4364 Expr_Value (L2) <= Expr_Value (L1)
4365 and then
4366 Expr_Value (H2) >= Expr_Value (H1);
4368 end if;
4369 end if;
4371 -- If any exception occurs, it means that we have some bug in the compiler
4372 -- possibly triggered by a previous error, or by some unforeseen peculiar
4373 -- occurrence. However, this is only an optimization attempt, so there is
4374 -- really no point in crashing the compiler. Instead we just decide, too
4375 -- bad, we can't figure out the answer in this case after all.
4377 exception
4378 when others =>
4380 -- Debug flag K disables this behavior (useful for debugging)
4382 if Debug_Flag_K then
4383 raise;
4384 else
4385 return False;
4386 end if;
4387 end In_Subrange_Of;
4389 -----------------
4390 -- Is_In_Range --
4391 -----------------
4393 function Is_In_Range
4394 (N : Node_Id;
4395 Typ : Entity_Id;
4396 Assume_Valid : Boolean := False;
4397 Fixed_Int : Boolean := False;
4398 Int_Real : Boolean := False) return Boolean
4400 begin
4401 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4402 = In_Range;
4403 end Is_In_Range;
4405 -------------------
4406 -- Is_Null_Range --
4407 -------------------
4409 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4410 Typ : constant Entity_Id := Etype (Lo);
4412 begin
4413 if not Compile_Time_Known_Value (Lo)
4414 or else not Compile_Time_Known_Value (Hi)
4415 then
4416 return False;
4417 end if;
4419 if Is_Discrete_Type (Typ) then
4420 return Expr_Value (Lo) > Expr_Value (Hi);
4422 else
4423 pragma Assert (Is_Real_Type (Typ));
4424 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4425 end if;
4426 end Is_Null_Range;
4428 -----------------------------
4429 -- Is_OK_Static_Expression --
4430 -----------------------------
4432 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4433 begin
4434 return Is_Static_Expression (N)
4435 and then not Raises_Constraint_Error (N);
4436 end Is_OK_Static_Expression;
4438 ------------------------
4439 -- Is_OK_Static_Range --
4440 ------------------------
4442 -- A static range is a range whose bounds are static expressions, or a
4443 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4444 -- We have already converted range attribute references, so we get the
4445 -- "or" part of this rule without needing a special test.
4447 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4448 begin
4449 return Is_OK_Static_Expression (Low_Bound (N))
4450 and then Is_OK_Static_Expression (High_Bound (N));
4451 end Is_OK_Static_Range;
4453 --------------------------
4454 -- Is_OK_Static_Subtype --
4455 --------------------------
4457 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4458 -- neither bound raises constraint error when evaluated.
4460 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4461 Base_T : constant Entity_Id := Base_Type (Typ);
4462 Anc_Subt : Entity_Id;
4464 begin
4465 -- First a quick check on the non static subtype flag. As described
4466 -- in further detail in Einfo, this flag is not decisive in all cases,
4467 -- but if it is set, then the subtype is definitely non-static.
4469 if Is_Non_Static_Subtype (Typ) then
4470 return False;
4471 end if;
4473 Anc_Subt := Ancestor_Subtype (Typ);
4475 if Anc_Subt = Empty then
4476 Anc_Subt := Base_T;
4477 end if;
4479 if Is_Generic_Type (Root_Type (Base_T))
4480 or else Is_Generic_Actual_Type (Base_T)
4481 then
4482 return False;
4484 -- String types
4486 elsif Is_String_Type (Typ) then
4487 return
4488 Ekind (Typ) = E_String_Literal_Subtype
4489 or else
4490 (Is_OK_Static_Subtype (Component_Type (Typ))
4491 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
4493 -- Scalar types
4495 elsif Is_Scalar_Type (Typ) then
4496 if Base_T = Typ then
4497 return True;
4499 else
4500 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4501 -- Get_Type_{Low,High}_Bound.
4503 return Is_OK_Static_Subtype (Anc_Subt)
4504 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4505 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4506 end if;
4508 -- Types other than string and scalar types are never static
4510 else
4511 return False;
4512 end if;
4513 end Is_OK_Static_Subtype;
4515 ---------------------
4516 -- Is_Out_Of_Range --
4517 ---------------------
4519 function Is_Out_Of_Range
4520 (N : Node_Id;
4521 Typ : Entity_Id;
4522 Assume_Valid : Boolean := False;
4523 Fixed_Int : Boolean := False;
4524 Int_Real : Boolean := False) return Boolean
4526 begin
4527 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4528 = Out_Of_Range;
4529 end Is_Out_Of_Range;
4531 ---------------------
4532 -- Is_Static_Range --
4533 ---------------------
4535 -- A static range is a range whose bounds are static expressions, or a
4536 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4537 -- We have already converted range attribute references, so we get the
4538 -- "or" part of this rule without needing a special test.
4540 function Is_Static_Range (N : Node_Id) return Boolean is
4541 begin
4542 return Is_Static_Expression (Low_Bound (N))
4543 and then Is_Static_Expression (High_Bound (N));
4544 end Is_Static_Range;
4546 -----------------------
4547 -- Is_Static_Subtype --
4548 -----------------------
4550 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4552 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
4553 Base_T : constant Entity_Id := Base_Type (Typ);
4554 Anc_Subt : Entity_Id;
4556 begin
4557 -- First a quick check on the non static subtype flag. As described
4558 -- in further detail in Einfo, this flag is not decisive in all cases,
4559 -- but if it is set, then the subtype is definitely non-static.
4561 if Is_Non_Static_Subtype (Typ) then
4562 return False;
4563 end if;
4565 Anc_Subt := Ancestor_Subtype (Typ);
4567 if Anc_Subt = Empty then
4568 Anc_Subt := Base_T;
4569 end if;
4571 if Is_Generic_Type (Root_Type (Base_T))
4572 or else Is_Generic_Actual_Type (Base_T)
4573 then
4574 return False;
4576 -- String types
4578 elsif Is_String_Type (Typ) then
4579 return
4580 Ekind (Typ) = E_String_Literal_Subtype
4581 or else (Is_Static_Subtype (Component_Type (Typ))
4582 and then Is_Static_Subtype (Etype (First_Index (Typ))));
4584 -- Scalar types
4586 elsif Is_Scalar_Type (Typ) then
4587 if Base_T = Typ then
4588 return True;
4590 else
4591 return Is_Static_Subtype (Anc_Subt)
4592 and then Is_Static_Expression (Type_Low_Bound (Typ))
4593 and then Is_Static_Expression (Type_High_Bound (Typ));
4594 end if;
4596 -- Types other than string and scalar types are never static
4598 else
4599 return False;
4600 end if;
4601 end Is_Static_Subtype;
4603 --------------------
4604 -- Not_Null_Range --
4605 --------------------
4607 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4608 Typ : constant Entity_Id := Etype (Lo);
4610 begin
4611 if not Compile_Time_Known_Value (Lo)
4612 or else not Compile_Time_Known_Value (Hi)
4613 then
4614 return False;
4615 end if;
4617 if Is_Discrete_Type (Typ) then
4618 return Expr_Value (Lo) <= Expr_Value (Hi);
4620 else
4621 pragma Assert (Is_Real_Type (Typ));
4623 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
4624 end if;
4625 end Not_Null_Range;
4627 -------------
4628 -- OK_Bits --
4629 -------------
4631 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
4632 begin
4633 -- We allow a maximum of 500,000 bits which seems a reasonable limit
4635 if Bits < 500_000 then
4636 return True;
4638 else
4639 Error_Msg_N ("static value too large, capacity exceeded", N);
4640 return False;
4641 end if;
4642 end OK_Bits;
4644 ------------------
4645 -- Out_Of_Range --
4646 ------------------
4648 procedure Out_Of_Range (N : Node_Id) is
4649 begin
4650 -- If we have the static expression case, then this is an illegality
4651 -- in Ada 95 mode, except that in an instance, we never generate an
4652 -- error (if the error is legitimate, it was already diagnosed in the
4653 -- template). The expression to compute the length of a packed array is
4654 -- attached to the array type itself, and deserves a separate message.
4656 if Is_Static_Expression (N)
4657 and then not In_Instance
4658 and then not In_Inlined_Body
4659 and then Ada_Version >= Ada_95
4660 then
4661 if Nkind (Parent (N)) = N_Defining_Identifier
4662 and then Is_Array_Type (Parent (N))
4663 and then Present (Packed_Array_Type (Parent (N)))
4664 and then Present (First_Rep_Item (Parent (N)))
4665 then
4666 Error_Msg_N
4667 ("length of packed array must not exceed Integer''Last",
4668 First_Rep_Item (Parent (N)));
4669 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
4671 else
4672 Apply_Compile_Time_Constraint_Error
4673 (N, "value not in range of}", CE_Range_Check_Failed);
4674 end if;
4676 -- Here we generate a warning for the Ada 83 case, or when we are in an
4677 -- instance, or when we have a non-static expression case.
4679 else
4680 Apply_Compile_Time_Constraint_Error
4681 (N, "value not in range of}??", CE_Range_Check_Failed);
4682 end if;
4683 end Out_Of_Range;
4685 -------------------------
4686 -- Rewrite_In_Raise_CE --
4687 -------------------------
4689 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
4690 Typ : constant Entity_Id := Etype (N);
4692 begin
4693 -- If we want to raise CE in the condition of a N_Raise_CE node
4694 -- we may as well get rid of the condition.
4696 if Present (Parent (N))
4697 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
4698 then
4699 Set_Condition (Parent (N), Empty);
4701 -- If the expression raising CE is a N_Raise_CE node, we can use that
4702 -- one. We just preserve the type of the context.
4704 elsif Nkind (Exp) = N_Raise_Constraint_Error then
4705 Rewrite (N, Exp);
4706 Set_Etype (N, Typ);
4708 -- Else build an explcit N_Raise_CE
4710 else
4711 Rewrite (N,
4712 Make_Raise_Constraint_Error (Sloc (Exp),
4713 Reason => CE_Range_Check_Failed));
4714 Set_Raises_Constraint_Error (N);
4715 Set_Etype (N, Typ);
4716 end if;
4717 end Rewrite_In_Raise_CE;
4719 ---------------------
4720 -- String_Type_Len --
4721 ---------------------
4723 function String_Type_Len (Stype : Entity_Id) return Uint is
4724 NT : constant Entity_Id := Etype (First_Index (Stype));
4725 T : Entity_Id;
4727 begin
4728 if Is_OK_Static_Subtype (NT) then
4729 T := NT;
4730 else
4731 T := Base_Type (NT);
4732 end if;
4734 return Expr_Value (Type_High_Bound (T)) -
4735 Expr_Value (Type_Low_Bound (T)) + 1;
4736 end String_Type_Len;
4738 ------------------------------------
4739 -- Subtypes_Statically_Compatible --
4740 ------------------------------------
4742 function Subtypes_Statically_Compatible
4743 (T1 : Entity_Id;
4744 T2 : Entity_Id) return Boolean
4746 begin
4747 -- Scalar types
4749 if Is_Scalar_Type (T1) then
4751 -- Definitely compatible if we match
4753 if Subtypes_Statically_Match (T1, T2) then
4754 return True;
4756 -- If either subtype is nonstatic then they're not compatible
4758 elsif not Is_Static_Subtype (T1)
4759 or else not Is_Static_Subtype (T2)
4760 then
4761 return False;
4763 -- If either type has constraint error bounds, then consider that
4764 -- they match to avoid junk cascaded errors here.
4766 elsif not Is_OK_Static_Subtype (T1)
4767 or else not Is_OK_Static_Subtype (T2)
4768 then
4769 return True;
4771 -- Base types must match, but we don't check that (should we???) but
4772 -- we do at least check that both types are real, or both types are
4773 -- not real.
4775 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
4776 return False;
4778 -- Here we check the bounds
4780 else
4781 declare
4782 LB1 : constant Node_Id := Type_Low_Bound (T1);
4783 HB1 : constant Node_Id := Type_High_Bound (T1);
4784 LB2 : constant Node_Id := Type_Low_Bound (T2);
4785 HB2 : constant Node_Id := Type_High_Bound (T2);
4787 begin
4788 if Is_Real_Type (T1) then
4789 return
4790 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
4791 or else
4792 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
4793 and then
4794 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
4796 else
4797 return
4798 (Expr_Value (LB1) > Expr_Value (HB1))
4799 or else
4800 (Expr_Value (LB2) <= Expr_Value (LB1)
4801 and then
4802 Expr_Value (HB1) <= Expr_Value (HB2));
4803 end if;
4804 end;
4805 end if;
4807 -- Access types
4809 elsif Is_Access_Type (T1) then
4810 return (not Is_Constrained (T2)
4811 or else (Subtypes_Statically_Match
4812 (Designated_Type (T1), Designated_Type (T2))))
4813 and then not (Can_Never_Be_Null (T2)
4814 and then not Can_Never_Be_Null (T1));
4816 -- All other cases
4818 else
4819 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
4820 or else Subtypes_Statically_Match (T1, T2);
4821 end if;
4822 end Subtypes_Statically_Compatible;
4824 -------------------------------
4825 -- Subtypes_Statically_Match --
4826 -------------------------------
4828 -- Subtypes statically match if they have statically matching constraints
4829 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4830 -- they are the same identical constraint, or if they are static and the
4831 -- values match (RM 4.9.1(1)).
4833 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
4835 function Predicates_Match return Boolean;
4836 -- In Ada 2012, subtypes statically match if their static predicates
4837 -- match as well.
4839 ----------------------
4840 -- Predicates_Match --
4841 ----------------------
4843 function Predicates_Match return Boolean is
4844 Pred1 : Node_Id;
4845 Pred2 : Node_Id;
4847 begin
4848 if Ada_Version < Ada_2012 then
4849 return True;
4851 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
4852 return False;
4854 else
4855 Pred1 :=
4856 Get_Rep_Item
4857 (T1, Name_Static_Predicate, Check_Parents => False);
4858 Pred2 :=
4859 Get_Rep_Item
4860 (T2, Name_Static_Predicate, Check_Parents => False);
4862 -- Subtypes statically match if the predicate comes from the
4863 -- same declaration, which can only happen if one is a subtype
4864 -- of the other and has no explicit predicate.
4866 -- Suppress warnings on order of actuals, which is otherwise
4867 -- triggered by one of the two calls below.
4869 pragma Warnings (Off);
4870 return Pred1 = Pred2
4871 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
4872 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
4873 pragma Warnings (On);
4874 end if;
4875 end Predicates_Match;
4877 -- Start of processing for Subtypes_Statically_Match
4879 begin
4880 -- A type always statically matches itself
4882 if T1 = T2 then
4883 return True;
4885 -- Scalar types
4887 elsif Is_Scalar_Type (T1) then
4889 -- Base types must be the same
4891 if Base_Type (T1) /= Base_Type (T2) then
4892 return False;
4893 end if;
4895 -- A constrained numeric subtype never matches an unconstrained
4896 -- subtype, i.e. both types must be constrained or unconstrained.
4898 -- To understand the requirement for this test, see RM 4.9.1(1).
4899 -- As is made clear in RM 3.5.4(11), type Integer, for example is
4900 -- a constrained subtype with constraint bounds matching the bounds
4901 -- of its corresponding unconstrained base type. In this situation,
4902 -- Integer and Integer'Base do not statically match, even though
4903 -- they have the same bounds.
4905 -- We only apply this test to types in Standard and types that appear
4906 -- in user programs. That way, we do not have to be too careful about
4907 -- setting Is_Constrained right for Itypes.
4909 if Is_Numeric_Type (T1)
4910 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4911 and then (Scope (T1) = Standard_Standard
4912 or else Comes_From_Source (T1))
4913 and then (Scope (T2) = Standard_Standard
4914 or else Comes_From_Source (T2))
4915 then
4916 return False;
4918 -- A generic scalar type does not statically match its base type
4919 -- (AI-311). In this case we make sure that the formals, which are
4920 -- first subtypes of their bases, are constrained.
4922 elsif Is_Generic_Type (T1)
4923 and then Is_Generic_Type (T2)
4924 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4925 then
4926 return False;
4927 end if;
4929 -- If there was an error in either range, then just assume the types
4930 -- statically match to avoid further junk errors.
4932 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
4933 or else Error_Posted (Scalar_Range (T1))
4934 or else Error_Posted (Scalar_Range (T2))
4935 then
4936 return True;
4937 end if;
4939 -- Otherwise both types have bound that can be compared
4941 declare
4942 LB1 : constant Node_Id := Type_Low_Bound (T1);
4943 HB1 : constant Node_Id := Type_High_Bound (T1);
4944 LB2 : constant Node_Id := Type_Low_Bound (T2);
4945 HB2 : constant Node_Id := Type_High_Bound (T2);
4947 begin
4948 -- If the bounds are the same tree node, then match if and only
4949 -- if any predicates present also match.
4951 if LB1 = LB2 and then HB1 = HB2 then
4952 return Predicates_Match;
4954 -- Otherwise bounds must be static and identical value
4956 else
4957 if not Is_Static_Subtype (T1)
4958 or else not Is_Static_Subtype (T2)
4959 then
4960 return False;
4962 -- If either type has constraint error bounds, then say that
4963 -- they match to avoid junk cascaded errors here.
4965 elsif not Is_OK_Static_Subtype (T1)
4966 or else not Is_OK_Static_Subtype (T2)
4967 then
4968 return True;
4970 elsif Is_Real_Type (T1) then
4971 return
4972 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4973 and then
4974 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4976 else
4977 return
4978 Expr_Value (LB1) = Expr_Value (LB2)
4979 and then
4980 Expr_Value (HB1) = Expr_Value (HB2);
4981 end if;
4982 end if;
4983 end;
4985 -- Type with discriminants
4987 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
4989 -- Because of view exchanges in multiple instantiations, conformance
4990 -- checking might try to match a partial view of a type with no
4991 -- discriminants with a full view that has defaulted discriminants.
4992 -- In such a case, use the discriminant constraint of the full view,
4993 -- which must exist because we know that the two subtypes have the
4994 -- same base type.
4996 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
4997 if In_Instance then
4998 if Is_Private_Type (T2)
4999 and then Present (Full_View (T2))
5000 and then Has_Discriminants (Full_View (T2))
5001 then
5002 return Subtypes_Statically_Match (T1, Full_View (T2));
5004 elsif Is_Private_Type (T1)
5005 and then Present (Full_View (T1))
5006 and then Has_Discriminants (Full_View (T1))
5007 then
5008 return Subtypes_Statically_Match (Full_View (T1), T2);
5010 else
5011 return False;
5012 end if;
5013 else
5014 return False;
5015 end if;
5016 end if;
5018 declare
5019 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
5020 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
5022 DA1 : Elmt_Id;
5023 DA2 : Elmt_Id;
5025 begin
5026 if DL1 = DL2 then
5027 return True;
5028 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
5029 return False;
5030 end if;
5032 -- Now loop through the discriminant constraints
5034 -- Note: the guard here seems necessary, since it is possible at
5035 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5037 if Present (DL1) and then Present (DL2) then
5038 DA1 := First_Elmt (DL1);
5039 DA2 := First_Elmt (DL2);
5040 while Present (DA1) loop
5041 declare
5042 Expr1 : constant Node_Id := Node (DA1);
5043 Expr2 : constant Node_Id := Node (DA2);
5045 begin
5046 if not Is_Static_Expression (Expr1)
5047 or else not Is_Static_Expression (Expr2)
5048 then
5049 return False;
5051 -- If either expression raised a constraint error,
5052 -- consider the expressions as matching, since this
5053 -- helps to prevent cascading errors.
5055 elsif Raises_Constraint_Error (Expr1)
5056 or else Raises_Constraint_Error (Expr2)
5057 then
5058 null;
5060 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
5061 return False;
5062 end if;
5063 end;
5065 Next_Elmt (DA1);
5066 Next_Elmt (DA2);
5067 end loop;
5068 end if;
5069 end;
5071 return True;
5073 -- A definite type does not match an indefinite or classwide type.
5074 -- However, a generic type with unknown discriminants may be
5075 -- instantiated with a type with no discriminants, and conformance
5076 -- checking on an inherited operation may compare the actual with the
5077 -- subtype that renames it in the instance.
5079 elsif
5080 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
5081 then
5082 return
5083 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
5085 -- Array type
5087 elsif Is_Array_Type (T1) then
5089 -- If either subtype is unconstrained then both must be, and if both
5090 -- are unconstrained then no further checking is needed.
5092 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
5093 return not (Is_Constrained (T1) or else Is_Constrained (T2));
5094 end if;
5096 -- Both subtypes are constrained, so check that the index subtypes
5097 -- statically match.
5099 declare
5100 Index1 : Node_Id := First_Index (T1);
5101 Index2 : Node_Id := First_Index (T2);
5103 begin
5104 while Present (Index1) loop
5105 if not
5106 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
5107 then
5108 return False;
5109 end if;
5111 Next_Index (Index1);
5112 Next_Index (Index2);
5113 end loop;
5115 return True;
5116 end;
5118 elsif Is_Access_Type (T1) then
5119 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
5120 return False;
5122 elsif Ekind_In (T1, E_Access_Subprogram_Type,
5123 E_Anonymous_Access_Subprogram_Type)
5124 then
5125 return
5126 Subtype_Conformant
5127 (Designated_Type (T1),
5128 Designated_Type (T2));
5129 else
5130 return
5131 Subtypes_Statically_Match
5132 (Designated_Type (T1),
5133 Designated_Type (T2))
5134 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
5135 end if;
5137 -- All other types definitely match
5139 else
5140 return True;
5141 end if;
5142 end Subtypes_Statically_Match;
5144 ----------
5145 -- Test --
5146 ----------
5148 function Test (Cond : Boolean) return Uint is
5149 begin
5150 if Cond then
5151 return Uint_1;
5152 else
5153 return Uint_0;
5154 end if;
5155 end Test;
5157 ---------------------------------
5158 -- Test_Expression_Is_Foldable --
5159 ---------------------------------
5161 -- One operand case
5163 procedure Test_Expression_Is_Foldable
5164 (N : Node_Id;
5165 Op1 : Node_Id;
5166 Stat : out Boolean;
5167 Fold : out Boolean)
5169 begin
5170 Stat := False;
5171 Fold := False;
5173 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5174 return;
5175 end if;
5177 -- If operand is Any_Type, just propagate to result and do not
5178 -- try to fold, this prevents cascaded errors.
5180 if Etype (Op1) = Any_Type then
5181 Set_Etype (N, Any_Type);
5182 return;
5184 -- If operand raises constraint error, then replace node N with the
5185 -- raise constraint error node, and we are obviously not foldable.
5186 -- Note that this replacement inherits the Is_Static_Expression flag
5187 -- from the operand.
5189 elsif Raises_Constraint_Error (Op1) then
5190 Rewrite_In_Raise_CE (N, Op1);
5191 return;
5193 -- If the operand is not static, then the result is not static, and
5194 -- all we have to do is to check the operand since it is now known
5195 -- to appear in a non-static context.
5197 elsif not Is_Static_Expression (Op1) then
5198 Check_Non_Static_Context (Op1);
5199 Fold := Compile_Time_Known_Value (Op1);
5200 return;
5202 -- An expression of a formal modular type is not foldable because
5203 -- the modulus is unknown.
5205 elsif Is_Modular_Integer_Type (Etype (Op1))
5206 and then Is_Generic_Type (Etype (Op1))
5207 then
5208 Check_Non_Static_Context (Op1);
5209 return;
5211 -- Here we have the case of an operand whose type is OK, which is
5212 -- static, and which does not raise constraint error, we can fold.
5214 else
5215 Set_Is_Static_Expression (N);
5216 Fold := True;
5217 Stat := True;
5218 end if;
5219 end Test_Expression_Is_Foldable;
5221 -- Two operand case
5223 procedure Test_Expression_Is_Foldable
5224 (N : Node_Id;
5225 Op1 : Node_Id;
5226 Op2 : Node_Id;
5227 Stat : out Boolean;
5228 Fold : out Boolean)
5230 Rstat : constant Boolean := Is_Static_Expression (Op1)
5231 and then Is_Static_Expression (Op2);
5233 begin
5234 Stat := False;
5235 Fold := False;
5237 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5238 return;
5239 end if;
5241 -- If either operand is Any_Type, just propagate to result and
5242 -- do not try to fold, this prevents cascaded errors.
5244 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
5245 Set_Etype (N, Any_Type);
5246 return;
5248 -- If left operand raises constraint error, then replace node N with the
5249 -- Raise_Constraint_Error node, and we are obviously not foldable.
5250 -- Is_Static_Expression is set from the two operands in the normal way,
5251 -- and we check the right operand if it is in a non-static context.
5253 elsif Raises_Constraint_Error (Op1) then
5254 if not Rstat then
5255 Check_Non_Static_Context (Op2);
5256 end if;
5258 Rewrite_In_Raise_CE (N, Op1);
5259 Set_Is_Static_Expression (N, Rstat);
5260 return;
5262 -- Similar processing for the case of the right operand. Note that we
5263 -- don't use this routine for the short-circuit case, so we do not have
5264 -- to worry about that special case here.
5266 elsif Raises_Constraint_Error (Op2) then
5267 if not Rstat then
5268 Check_Non_Static_Context (Op1);
5269 end if;
5271 Rewrite_In_Raise_CE (N, Op2);
5272 Set_Is_Static_Expression (N, Rstat);
5273 return;
5275 -- Exclude expressions of a generic modular type, as above
5277 elsif Is_Modular_Integer_Type (Etype (Op1))
5278 and then Is_Generic_Type (Etype (Op1))
5279 then
5280 Check_Non_Static_Context (Op1);
5281 return;
5283 -- If result is not static, then check non-static contexts on operands
5284 -- since one of them may be static and the other one may not be static.
5286 elsif not Rstat then
5287 Check_Non_Static_Context (Op1);
5288 Check_Non_Static_Context (Op2);
5289 Fold := Compile_Time_Known_Value (Op1)
5290 and then Compile_Time_Known_Value (Op2);
5291 return;
5293 -- Else result is static and foldable. Both operands are static, and
5294 -- neither raises constraint error, so we can definitely fold.
5296 else
5297 Set_Is_Static_Expression (N);
5298 Fold := True;
5299 Stat := True;
5300 return;
5301 end if;
5302 end Test_Expression_Is_Foldable;
5304 -------------------
5305 -- Test_In_Range --
5306 -------------------
5308 function Test_In_Range
5309 (N : Node_Id;
5310 Typ : Entity_Id;
5311 Assume_Valid : Boolean;
5312 Fixed_Int : Boolean;
5313 Int_Real : Boolean) return Range_Membership
5315 Val : Uint;
5316 Valr : Ureal;
5318 pragma Warnings (Off, Assume_Valid);
5319 -- For now Assume_Valid is unreferenced since the current implementation
5320 -- always returns Unknown if N is not a compile time known value, but we
5321 -- keep the parameter to allow for future enhancements in which we try
5322 -- to get the information in the variable case as well.
5324 begin
5325 -- Universal types have no range limits, so always in range
5327 if Typ = Universal_Integer or else Typ = Universal_Real then
5328 return In_Range;
5330 -- Never known if not scalar type. Don't know if this can actually
5331 -- happen, but our spec allows it, so we must check!
5333 elsif not Is_Scalar_Type (Typ) then
5334 return Unknown;
5336 -- Never known if this is a generic type, since the bounds of generic
5337 -- types are junk. Note that if we only checked for static expressions
5338 -- (instead of compile time known values) below, we would not need this
5339 -- check, because values of a generic type can never be static, but they
5340 -- can be known at compile time.
5342 elsif Is_Generic_Type (Typ) then
5343 return Unknown;
5345 -- Never known unless we have a compile time known value
5347 elsif not Compile_Time_Known_Value (N) then
5348 return Unknown;
5350 -- General processing with a known compile time value
5352 else
5353 declare
5354 Lo : Node_Id;
5355 Hi : Node_Id;
5357 LB_Known : Boolean;
5358 HB_Known : Boolean;
5360 begin
5361 Lo := Type_Low_Bound (Typ);
5362 Hi := Type_High_Bound (Typ);
5364 LB_Known := Compile_Time_Known_Value (Lo);
5365 HB_Known := Compile_Time_Known_Value (Hi);
5367 -- Fixed point types should be considered as such only if flag
5368 -- Fixed_Int is set to False.
5370 if Is_Floating_Point_Type (Typ)
5371 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
5372 or else Int_Real
5373 then
5374 Valr := Expr_Value_R (N);
5376 if LB_Known and HB_Known then
5377 if Valr >= Expr_Value_R (Lo)
5378 and then
5379 Valr <= Expr_Value_R (Hi)
5380 then
5381 return In_Range;
5382 else
5383 return Out_Of_Range;
5384 end if;
5386 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
5387 or else
5388 (HB_Known and then Valr > Expr_Value_R (Hi))
5389 then
5390 return Out_Of_Range;
5392 else
5393 return Unknown;
5394 end if;
5396 else
5397 Val := Expr_Value (N);
5399 if LB_Known and HB_Known then
5400 if Val >= Expr_Value (Lo)
5401 and then
5402 Val <= Expr_Value (Hi)
5403 then
5404 return In_Range;
5405 else
5406 return Out_Of_Range;
5407 end if;
5409 elsif (LB_Known and then Val < Expr_Value (Lo))
5410 or else
5411 (HB_Known and then Val > Expr_Value (Hi))
5412 then
5413 return Out_Of_Range;
5415 else
5416 return Unknown;
5417 end if;
5418 end if;
5419 end;
5420 end if;
5421 end Test_In_Range;
5423 --------------
5424 -- To_Bits --
5425 --------------
5427 procedure To_Bits (U : Uint; B : out Bits) is
5428 begin
5429 for J in 0 .. B'Last loop
5430 B (J) := (U / (2 ** J)) mod 2 /= 0;
5431 end loop;
5432 end To_Bits;
5434 --------------------
5435 -- Why_Not_Static --
5436 --------------------
5438 procedure Why_Not_Static (Expr : Node_Id) is
5439 N : constant Node_Id := Original_Node (Expr);
5440 Typ : Entity_Id;
5441 E : Entity_Id;
5443 procedure Why_Not_Static_List (L : List_Id);
5444 -- A version that can be called on a list of expressions. Finds all
5445 -- non-static violations in any element of the list.
5447 -------------------------
5448 -- Why_Not_Static_List --
5449 -------------------------
5451 procedure Why_Not_Static_List (L : List_Id) is
5452 N : Node_Id;
5454 begin
5455 if Is_Non_Empty_List (L) then
5456 N := First (L);
5457 while Present (N) loop
5458 Why_Not_Static (N);
5459 Next (N);
5460 end loop;
5461 end if;
5462 end Why_Not_Static_List;
5464 -- Start of processing for Why_Not_Static
5466 begin
5467 -- If in ACATS mode (debug flag 2), then suppress all these messages,
5468 -- this avoids massive updates to the ACATS base line.
5470 if Debug_Flag_2 then
5471 return;
5472 end if;
5474 -- Ignore call on error or empty node
5476 if No (Expr) or else Nkind (Expr) = N_Error then
5477 return;
5478 end if;
5480 -- Preprocessing for sub expressions
5482 if Nkind (Expr) in N_Subexpr then
5484 -- Nothing to do if expression is static
5486 if Is_OK_Static_Expression (Expr) then
5487 return;
5488 end if;
5490 -- Test for constraint error raised
5492 if Raises_Constraint_Error (Expr) then
5493 Error_Msg_N
5494 ("\expression raises exception, cannot be static " &
5495 "(RM 4.9(34))", N);
5496 return;
5497 end if;
5499 -- If no type, then something is pretty wrong, so ignore
5501 Typ := Etype (Expr);
5503 if No (Typ) then
5504 return;
5505 end if;
5507 -- Type must be scalar or string type (but allow Bignum, since this
5508 -- is really a scalar type from our point of view in this diagnosis).
5510 if not Is_Scalar_Type (Typ)
5511 and then not Is_String_Type (Typ)
5512 and then not Is_RTE (Typ, RE_Bignum)
5513 then
5514 Error_Msg_N
5515 ("\static expression must have scalar or string type " &
5516 "(RM 4.9(2))", N);
5517 return;
5518 end if;
5519 end if;
5521 -- If we got through those checks, test particular node kind
5523 case Nkind (N) is
5525 -- Entity name
5527 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
5528 E := Entity (N);
5530 if Is_Named_Number (E) then
5531 null;
5533 elsif Ekind (E) = E_Constant then
5535 -- One case we can give a metter message is when we have a
5536 -- string literal created by concatenating an aggregate with
5537 -- an others expression.
5539 Entity_Case : declare
5540 CV : constant Node_Id := Constant_Value (E);
5541 CO : constant Node_Id := Original_Node (CV);
5543 function Is_Aggregate (N : Node_Id) return Boolean;
5544 -- See if node N came from an others aggregate, if so
5545 -- return True and set Error_Msg_Sloc to aggregate.
5547 ------------------
5548 -- Is_Aggregate --
5549 ------------------
5551 function Is_Aggregate (N : Node_Id) return Boolean is
5552 begin
5553 if Nkind (Original_Node (N)) = N_Aggregate then
5554 Error_Msg_Sloc := Sloc (Original_Node (N));
5555 return True;
5556 elsif Is_Entity_Name (N)
5557 and then Ekind (Entity (N)) = E_Constant
5558 and then
5559 Nkind (Original_Node (Constant_Value (Entity (N)))) =
5560 N_Aggregate
5561 then
5562 Error_Msg_Sloc :=
5563 Sloc (Original_Node (Constant_Value (Entity (N))));
5564 return True;
5565 else
5566 return False;
5567 end if;
5568 end Is_Aggregate;
5570 -- Start of processing for Entity_Case
5572 begin
5573 if Is_Aggregate (CV)
5574 or else (Nkind (CO) = N_Op_Concat
5575 and then (Is_Aggregate (Left_Opnd (CO))
5576 or else
5577 Is_Aggregate (Right_Opnd (CO))))
5578 then
5579 Error_Msg_N ("\aggregate (#) is never static", N);
5581 elsif not Is_Static_Expression (CV) then
5582 Error_Msg_NE
5583 ("\& is not a static constant (RM 4.9(5))", N, E);
5584 end if;
5585 end Entity_Case;
5587 else
5588 Error_Msg_NE
5589 ("\& is not static constant or named number "
5590 & "(RM 4.9(5))", N, E);
5591 end if;
5593 -- Binary operator
5595 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
5596 if Nkind (N) in N_Op_Shift then
5597 Error_Msg_N
5598 ("\shift functions are never static (RM 4.9(6,18))", N);
5600 else
5601 Why_Not_Static (Left_Opnd (N));
5602 Why_Not_Static (Right_Opnd (N));
5603 end if;
5605 -- Unary operator
5607 when N_Unary_Op =>
5608 Why_Not_Static (Right_Opnd (N));
5610 -- Attribute reference
5612 when N_Attribute_Reference =>
5613 Why_Not_Static_List (Expressions (N));
5615 E := Etype (Prefix (N));
5617 if E = Standard_Void_Type then
5618 return;
5619 end if;
5621 -- Special case non-scalar'Size since this is a common error
5623 if Attribute_Name (N) = Name_Size then
5624 Error_Msg_N
5625 ("\size attribute is only static for static scalar type "
5626 & "(RM 4.9(7,8))", N);
5628 -- Flag array cases
5630 elsif Is_Array_Type (E) then
5631 if Attribute_Name (N) /= Name_First
5632 and then
5633 Attribute_Name (N) /= Name_Last
5634 and then
5635 Attribute_Name (N) /= Name_Length
5636 then
5637 Error_Msg_N
5638 ("\static array attribute must be Length, First, or Last "
5639 & "(RM 4.9(8))", N);
5641 -- Since we know the expression is not-static (we already
5642 -- tested for this, must mean array is not static).
5644 else
5645 Error_Msg_N
5646 ("\prefix is non-static array (RM 4.9(8))", Prefix (N));
5647 end if;
5649 return;
5651 -- Special case generic types, since again this is a common source
5652 -- of confusion.
5654 elsif Is_Generic_Actual_Type (E)
5655 or else
5656 Is_Generic_Type (E)
5657 then
5658 Error_Msg_N
5659 ("\attribute of generic type is never static "
5660 & "(RM 4.9(7,8))", N);
5662 elsif Is_Static_Subtype (E) then
5663 null;
5665 elsif Is_Scalar_Type (E) then
5666 Error_Msg_N
5667 ("\prefix type for attribute is not static scalar subtype "
5668 & "(RM 4.9(7))", N);
5670 else
5671 Error_Msg_N
5672 ("\static attribute must apply to array/scalar type "
5673 & "(RM 4.9(7,8))", N);
5674 end if;
5676 -- String literal
5678 when N_String_Literal =>
5679 Error_Msg_N
5680 ("\subtype of string literal is non-static (RM 4.9(4))", N);
5682 -- Explicit dereference
5684 when N_Explicit_Dereference =>
5685 Error_Msg_N
5686 ("\explicit dereference is never static (RM 4.9)", N);
5688 -- Function call
5690 when N_Function_Call =>
5691 Why_Not_Static_List (Parameter_Associations (N));
5693 -- Complain about non-static function call unless we have Bignum
5694 -- which means that the underlying expression is really some
5695 -- scalar arithmetic operation.
5697 if not Is_RTE (Typ, RE_Bignum) then
5698 Error_Msg_N ("\non-static function call (RM 4.9(6,18))", N);
5699 end if;
5701 -- Parameter assocation (test actual parameter)
5703 when N_Parameter_Association =>
5704 Why_Not_Static (Explicit_Actual_Parameter (N));
5706 -- Indexed component
5708 when N_Indexed_Component =>
5709 Error_Msg_N ("\indexed component is never static (RM 4.9)", N);
5711 -- Procedure call
5713 when N_Procedure_Call_Statement =>
5714 Error_Msg_N ("\procedure call is never static (RM 4.9)", N);
5716 -- Qualified expression (test expression)
5718 when N_Qualified_Expression =>
5719 Why_Not_Static (Expression (N));
5721 -- Aggregate
5723 when N_Aggregate | N_Extension_Aggregate =>
5724 Error_Msg_N ("\an aggregate is never static (RM 4.9)", N);
5726 -- Range
5728 when N_Range =>
5729 Why_Not_Static (Low_Bound (N));
5730 Why_Not_Static (High_Bound (N));
5732 -- Range constraint, test range expression
5734 when N_Range_Constraint =>
5735 Why_Not_Static (Range_Expression (N));
5737 -- Subtype indication, test constraint
5739 when N_Subtype_Indication =>
5740 Why_Not_Static (Constraint (N));
5742 -- Selected component
5744 when N_Selected_Component =>
5745 Error_Msg_N ("\selected component is never static (RM 4.9)", N);
5747 -- Slice
5749 when N_Slice =>
5750 Error_Msg_N ("\slice is never static (RM 4.9)", N);
5752 when N_Type_Conversion =>
5753 Why_Not_Static (Expression (N));
5755 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
5756 or else not Is_Static_Subtype (Entity (Subtype_Mark (N)))
5757 then
5758 Error_Msg_N
5759 ("\static conversion requires static scalar subtype result "
5760 & "(RM 4.9(9))", N);
5761 end if;
5763 -- Unchecked type conversion
5765 when N_Unchecked_Type_Conversion =>
5766 Error_Msg_N
5767 ("\unchecked type conversion is never static (RM 4.9)", N);
5769 -- All other cases, no reason to give
5771 when others =>
5772 null;
5774 end case;
5775 end Why_Not_Static;
5777 end Sem_Eval;