1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
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;
35 with Namet; use Namet;
36 with Nmake; use Nmake;
37 with Nlists; use Nlists;
40 with Sem_Cat; use Sem_Cat;
41 with Sem_Ch6; use Sem_Ch6;
42 with Sem_Ch8; use Sem_Ch8;
43 with Sem_Res; use Sem_Res;
44 with Sem_Util; use Sem_Util;
45 with Sem_Type; use Sem_Type;
46 with Sem_Warn; use Sem_Warn;
47 with Sinfo; use Sinfo;
48 with Snames; use Snames;
49 with Stand; use Stand;
50 with Stringt; use Stringt;
51 with Tbuild; use Tbuild;
53 package body Sem_Eval is
55 -----------------------------------------
56 -- Handling of Compile Time Evaluation --
57 -----------------------------------------
59 -- The compile time evaluation of expressions is distributed over several
60 -- Eval_xxx procedures. These procedures are called immediately after
61 -- a subexpression is resolved and is therefore accomplished in a bottom
62 -- up fashion. The flags are synthesized using the following approach.
64 -- Is_Static_Expression is determined by following the detailed rules
65 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
66 -- flag of the operands in many cases.
68 -- Raises_Constraint_Error is set if any of the operands have the flag
69 -- set or if an attempt to compute the value of the current expression
70 -- results in detection of a runtime constraint error.
72 -- As described in the spec, the requirement is that Is_Static_Expression
73 -- be accurately set, and in addition for nodes for which this flag is set,
74 -- Raises_Constraint_Error must also be set. Furthermore a node which has
75 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
76 -- requirement is that the expression value must be precomputed, and the
77 -- node is either a literal, or the name of a constant entity whose value
78 -- is a static expression.
80 -- The general approach is as follows. First compute Is_Static_Expression.
81 -- If the node is not static, then the flag is left off in the node and
82 -- we are all done. Otherwise for a static node, we test if any of the
83 -- operands will raise constraint error, and if so, propagate the flag
84 -- Raises_Constraint_Error to the result node and we are done (since the
85 -- error was already posted at a lower level).
87 -- For the case of a static node whose operands do not raise constraint
88 -- error, we attempt to evaluate the node. If this evaluation succeeds,
89 -- then the node is replaced by the result of this computation. If the
90 -- evaluation raises constraint error, then we rewrite the node with
91 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
92 -- to post appropriate error messages.
98 type Bits is array (Nat range <>) of Boolean;
99 -- Used to convert unsigned (modular) values for folding logical ops
101 -- The following definitions are used to maintain a cache of nodes that
102 -- have compile time known values. The cache is maintained only for
103 -- discrete types (the most common case), and is populated by calls to
104 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
105 -- since it is possible for the status to change (in particular it is
106 -- possible for a node to get replaced by a constraint error node).
108 CV_Bits : constant := 5;
109 -- Number of low order bits of Node_Id value used to reference entries
110 -- in the cache table.
112 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
113 -- Size of cache for compile time values
115 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
117 type CV_Entry is record
122 type CV_Cache_Array is array (CV_Range) of CV_Entry;
124 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
125 -- This is the actual cache, with entries consisting of node/value pairs,
126 -- and the impossible value Node_High_Bound used for unset entries.
128 -----------------------
129 -- Local Subprograms --
130 -----------------------
132 function From_Bits (B : Bits; T : Entity_Id) return Uint;
133 -- Converts a bit string of length B'Length to a Uint value to be used
134 -- for a target of type T, which is a modular type. This procedure
135 -- includes the necessary reduction by the modulus in the case of a
136 -- non-binary modulus (for a binary modulus, the bit string is the
137 -- right length any way so all is well).
139 function Get_String_Val (N : Node_Id) return Node_Id;
140 -- Given a tree node for a folded string or character value, returns
141 -- the corresponding string literal or character literal (one of the
142 -- two must be available, or the operand would not have been marked
143 -- as foldable in the earlier analysis of the operation).
145 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
146 -- Bits represents the number of bits in an integer value to be computed
147 -- (but the value has not been computed yet). If this value in Bits is
148 -- reasonable, a result of True is returned, with the implication that
149 -- the caller should go ahead and complete the calculation. If the value
150 -- in Bits is unreasonably large, then an error is posted on node N, and
151 -- False is returned (and the caller skips the proposed calculation).
153 procedure Out_Of_Range (N : Node_Id);
154 -- This procedure is called if it is determined that node N, which
155 -- appears in a non-static context, is a compile time known value
156 -- which is outside its range, i.e. the range of Etype. This is used
157 -- in contexts where this is an illegality if N is static, and should
158 -- generate a warning otherwise.
160 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
161 -- N and Exp are nodes representing an expression, Exp is known
162 -- to raise CE. N is rewritten in term of Exp in the optimal way.
164 function String_Type_Len (Stype : Entity_Id) return Uint;
165 -- Given a string type, determines the length of the index type, or,
166 -- if this index type is non-static, the length of the base type of
167 -- this index type. Note that if the string type is itself static,
168 -- then the index type is static, so the second case applies only
169 -- if the string type passed is non-static.
171 function Test (Cond : Boolean) return Uint;
172 pragma Inline (Test);
173 -- This function simply returns the appropriate Boolean'Pos value
174 -- corresponding to the value of Cond as a universal integer. It is
175 -- used for producing the result of the static evaluation of the
178 procedure Test_Expression_Is_Foldable
183 -- Tests to see if expression N whose single operand is Op1 is foldable,
184 -- i.e. the operand value is known at compile time. If the operation is
185 -- foldable, then Fold is True on return, and Stat indicates whether
186 -- the result is static (i.e. both operands were static). Note that it
187 -- is quite possible for Fold to be True, and Stat to be False, since
188 -- there are cases in which we know the value of an operand even though
189 -- it is not technically static (e.g. the static lower bound of a range
190 -- whose upper bound is non-static).
192 -- If Stat is set False on return, then Expression_Is_Foldable makes a
193 -- call to Check_Non_Static_Context on the operand. If Fold is False on
194 -- return, then all processing is complete, and the caller should
195 -- return, since there is nothing else to do.
197 procedure Test_Expression_Is_Foldable
203 -- Same processing, except applies to an expression N with two operands
206 procedure To_Bits (U : Uint; B : out Bits);
207 -- Converts a Uint value to a bit string of length B'Length
209 ------------------------------
210 -- Check_Non_Static_Context --
211 ------------------------------
213 procedure Check_Non_Static_Context (N : Node_Id) is
214 T : constant Entity_Id := Etype (N);
215 Checks_On : constant Boolean :=
216 not Index_Checks_Suppressed (T)
217 and not Range_Checks_Suppressed (T);
220 -- Ignore cases of non-scalar types or error types
222 if T = Any_Type or else not Is_Scalar_Type (T) then
226 -- At this stage we have a scalar type. If we have an expression
227 -- that raises CE, then we already issued a warning or error msg
228 -- so there is nothing more to be done in this routine.
230 if Raises_Constraint_Error (N) then
234 -- Now we have a scalar type which is not marked as raising a
235 -- constraint error exception. The main purpose of this routine
236 -- is to deal with static expressions appearing in a non-static
237 -- context. That means that if we do not have a static expression
238 -- then there is not much to do. The one case that we deal with
239 -- here is that if we have a floating-point value that is out of
240 -- range, then we post a warning that an infinity will result.
242 if not Is_Static_Expression (N) then
243 if Is_Floating_Point_Type (T)
244 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
247 ("?float value out of range, infinity will be generated", N);
253 -- Here we have the case of outer level static expression of
254 -- scalar type, where the processing of this procedure is needed.
256 -- For real types, this is where we convert the value to a machine
257 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
258 -- only need to do this if the parent is a constant declaration,
259 -- since in other cases, gigi should do the necessary conversion
260 -- correctly, but experimentation shows that this is not the case
261 -- on all machines, in particular if we do not convert all literals
262 -- to machine values in non-static contexts, then ACVC test C490001
263 -- fails on Sparc/Solaris and SGI/Irix.
265 if Nkind (N) = N_Real_Literal
266 and then not Is_Machine_Number (N)
267 and then not Is_Generic_Type (Etype (N))
268 and then Etype (N) /= Universal_Real
270 -- Check that value is in bounds before converting to machine
271 -- number, so as not to lose case where value overflows in the
272 -- least significant bit or less. See B490001.
274 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
279 -- Note: we have to copy the node, to avoid problems with conformance
280 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
282 Rewrite (N, New_Copy (N));
284 if not Is_Floating_Point_Type (T) then
286 (N, Corresponding_Integer_Value (N) * Small_Value (T));
288 elsif not UR_Is_Zero (Realval (N)) then
290 -- Note: even though RM 4.9(38) specifies biased rounding,
291 -- this has been modified by AI-100 in order to prevent
292 -- confusing differences in rounding between static and
293 -- non-static expressions. AI-100 specifies that the effect
294 -- of such rounding is implementation dependent, and in GNAT
295 -- we round to nearest even to match the run-time behavior.
298 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
301 Set_Is_Machine_Number (N);
304 -- Check for out of range universal integer. This is a non-static
305 -- context, so the integer value must be in range of the runtime
306 -- representation of universal integers.
308 -- We do this only within an expression, because that is the only
309 -- case in which non-static universal integer values can occur, and
310 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
311 -- called in contexts like the expression of a number declaration where
312 -- we certainly want to allow out of range values.
314 if Etype (N) = Universal_Integer
315 and then Nkind (N) = N_Integer_Literal
316 and then Nkind (Parent (N)) in N_Subexpr
318 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
320 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
322 Apply_Compile_Time_Constraint_Error
323 (N, "non-static universal integer value out of range?",
324 CE_Range_Check_Failed);
326 -- Check out of range of base type
328 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
331 -- Give warning if outside subtype (where one or both of the bounds of
332 -- the subtype is static). This warning is omitted if the expression
333 -- appears in a range that could be null (warnings are handled elsewhere
336 elsif T /= Base_Type (T)
337 and then Nkind (Parent (N)) /= N_Range
339 if Is_In_Range (N, T, Assume_Valid => True) then
342 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
343 Apply_Compile_Time_Constraint_Error
344 (N, "value not in range of}?", CE_Range_Check_Failed);
347 Enable_Range_Check (N);
350 Set_Do_Range_Check (N, False);
353 end Check_Non_Static_Context;
355 ---------------------------------
356 -- Check_String_Literal_Length --
357 ---------------------------------
359 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
361 if not Raises_Constraint_Error (N)
362 and then Is_Constrained (Ttype)
365 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
367 Apply_Compile_Time_Constraint_Error
368 (N, "string length wrong for}?",
369 CE_Length_Check_Failed,
374 end Check_String_Literal_Length;
376 --------------------------
377 -- Compile_Time_Compare --
378 --------------------------
380 function Compile_Time_Compare
382 Assume_Valid : Boolean;
383 Rec : Boolean := False) return Compare_Result
385 Ltyp : Entity_Id := Etype (L);
386 Rtyp : Entity_Id := Etype (R);
387 -- These get reset to the base type for the case of entities where
388 -- Is_Known_Valid is not set. This takes care of handling possible
389 -- invalid representations using the value of the base type, in
390 -- accordance with RM 13.9.1(10).
392 procedure Compare_Decompose
396 -- This procedure decomposes the node N into an expression node and a
397 -- signed offset, so that the value of N is equal to the value of R plus
398 -- the value V (which may be negative). If no such decomposition is
399 -- possible, then on return R is a copy of N, and V is set to zero.
401 function Compare_Fixup (N : Node_Id) return Node_Id;
402 -- This function deals with replacing 'Last and 'First references with
403 -- their corresponding type bounds, which we then can compare. The
404 -- argument is the original node, the result is the identity, unless we
405 -- have a 'Last/'First reference in which case the value returned is the
406 -- appropriate type bound.
408 function Is_Same_Value (L, R : Node_Id) return Boolean;
409 -- Returns True iff L and R represent expressions that definitely
410 -- have identical (but not necessarily compile time known) values
411 -- Indeed the caller is expected to have already dealt with the
412 -- cases of compile time known values, so these are not tested here.
414 -----------------------
415 -- Compare_Decompose --
416 -----------------------
418 procedure Compare_Decompose
424 if Nkind (N) = N_Op_Add
425 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
428 V := Intval (Right_Opnd (N));
431 elsif Nkind (N) = N_Op_Subtract
432 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
435 V := UI_Negate (Intval (Right_Opnd (N)));
438 elsif Nkind (N) = N_Attribute_Reference then
439 if Attribute_Name (N) = Name_Succ then
440 R := First (Expressions (N));
444 elsif Attribute_Name (N) = Name_Pred then
445 R := First (Expressions (N));
453 end Compare_Decompose;
459 function Compare_Fixup (N : Node_Id) return Node_Id is
465 if Nkind (N) = N_Attribute_Reference
466 and then (Attribute_Name (N) = Name_First
468 Attribute_Name (N) = Name_Last)
470 Xtyp := Etype (Prefix (N));
472 -- If we have no type, then just abandon the attempt to do
473 -- a fixup, this is probably the result of some other error.
479 -- Dereference an access type
481 if Is_Access_Type (Xtyp) then
482 Xtyp := Designated_Type (Xtyp);
485 -- If we don't have an array type at this stage, something
486 -- is peculiar, e.g. another error, and we abandon the attempt
489 if not Is_Array_Type (Xtyp) then
493 -- Ignore unconstrained array, since bounds are not meaningful
495 if not Is_Constrained (Xtyp) then
499 if Ekind (Xtyp) = E_String_Literal_Subtype then
500 if Attribute_Name (N) = Name_First then
501 return String_Literal_Low_Bound (Xtyp);
503 else -- Attribute_Name (N) = Name_Last
504 return Make_Integer_Literal (Sloc (N),
505 Intval => Intval (String_Literal_Low_Bound (Xtyp))
506 + String_Literal_Length (Xtyp));
510 -- Find correct index type
512 Indx := First_Index (Xtyp);
514 if Present (Expressions (N)) then
515 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
517 for J in 2 .. Subs loop
518 Indx := Next_Index (Indx);
522 Xtyp := Etype (Indx);
524 if Attribute_Name (N) = Name_First then
525 return Type_Low_Bound (Xtyp);
527 else -- Attribute_Name (N) = Name_Last
528 return Type_High_Bound (Xtyp);
539 function Is_Same_Value (L, R : Node_Id) return Boolean is
540 Lf : constant Node_Id := Compare_Fixup (L);
541 Rf : constant Node_Id := Compare_Fixup (R);
543 function Is_Same_Subscript (L, R : List_Id) return Boolean;
544 -- L, R are the Expressions values from two attribute nodes
545 -- for First or Last attributes. Either may be set to No_List
546 -- if no expressions are present (indicating subscript 1).
547 -- The result is True if both expressions represent the same
548 -- subscript (note that one case is where one subscript is
549 -- missing and the other is explicitly set to 1).
551 -----------------------
552 -- Is_Same_Subscript --
553 -----------------------
555 function Is_Same_Subscript (L, R : List_Id) return Boolean is
561 return Expr_Value (First (R)) = Uint_1;
566 return Expr_Value (First (L)) = Uint_1;
568 return Expr_Value (First (L)) = Expr_Value (First (R));
571 end Is_Same_Subscript;
573 -- Start of processing for Is_Same_Value
576 -- Values are the same if they refer to the same entity and the
577 -- entity is non-volatile. This does not however apply to Float
578 -- types, since we may have two NaN values and they should never
581 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
582 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
583 and then Entity (Lf) = Entity (Rf)
584 and then Present (Entity (Lf))
585 and then not Is_Floating_Point_Type (Etype (L))
586 and then not Is_Volatile_Reference (L)
587 and then not Is_Volatile_Reference (R)
591 -- Or if they are compile time known and identical
593 elsif Compile_Time_Known_Value (Lf)
595 Compile_Time_Known_Value (Rf)
596 and then Expr_Value (Lf) = Expr_Value (Rf)
600 -- False if Nkind of the two nodes is different for remaining cases
602 elsif Nkind (Lf) /= Nkind (Rf) then
605 -- True if both 'First or 'Last values applying to the same entity
606 -- (first and last don't change even if value does). Note that we
607 -- need this even with the calls to Compare_Fixup, to handle the
608 -- case of unconstrained array attributes where Compare_Fixup
609 -- cannot find useful bounds.
611 elsif Nkind (Lf) = N_Attribute_Reference
612 and then Attribute_Name (Lf) = Attribute_Name (Rf)
613 and then (Attribute_Name (Lf) = Name_First
615 Attribute_Name (Lf) = Name_Last)
616 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
617 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
618 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
619 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
623 -- True if the same selected component from the same record
625 elsif Nkind (Lf) = N_Selected_Component
626 and then Selector_Name (Lf) = Selector_Name (Rf)
627 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
631 -- True if the same unary operator applied to the same operand
633 elsif Nkind (Lf) in N_Unary_Op
634 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
638 -- True if the same binary operator applied to the same operands
640 elsif Nkind (Lf) in N_Binary_Op
641 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
642 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
646 -- All other cases, we can't tell, so return False
653 -- Start of processing for Compile_Time_Compare
656 -- If either operand could raise constraint error, then we cannot
657 -- know the result at compile time (since CE may be raised!)
659 if not (Cannot_Raise_Constraint_Error (L)
661 Cannot_Raise_Constraint_Error (R))
666 -- Identical operands are most certainly equal
671 -- If expressions have no types, then do not attempt to determine
672 -- if they are the same, since something funny is going on. One
673 -- case in which this happens is during generic template analysis,
674 -- when bounds are not fully analyzed.
676 elsif No (Ltyp) or else No (Rtyp) then
679 -- We only attempt compile time analysis for scalar values, and
680 -- not for packed arrays represented as modular types, where the
681 -- semantics of comparison is quite different.
683 elsif not Is_Scalar_Type (Ltyp)
684 or else Is_Packed_Array_Type (Ltyp)
688 -- Case where comparison involves two compile time known values
690 elsif Compile_Time_Known_Value (L)
691 and then Compile_Time_Known_Value (R)
693 -- For the floating-point case, we have to be a little careful, since
694 -- at compile time we are dealing with universal exact values, but at
695 -- runtime, these will be in non-exact target form. That's why the
696 -- returned results are LE and GE below instead of LT and GT.
698 if Is_Floating_Point_Type (Ltyp)
700 Is_Floating_Point_Type (Rtyp)
703 Lo : constant Ureal := Expr_Value_R (L);
704 Hi : constant Ureal := Expr_Value_R (R);
716 -- For the integer case we know exactly (note that this includes the
717 -- fixed-point case, where we know the run time integer values now)
721 Lo : constant Uint := Expr_Value (L);
722 Hi : constant Uint := Expr_Value (R);
735 -- Cases where at least one operand is not known at compile time
738 -- Remaining checks apply only for non-generic discrete types
740 if not Is_Discrete_Type (Ltyp)
741 or else not Is_Discrete_Type (Rtyp)
742 or else Is_Generic_Type (Ltyp)
743 or else Is_Generic_Type (Rtyp)
748 -- Replace types by base types for the case of entities which are
749 -- not known to have valid representations. This takes care of
750 -- properly dealing with invalid representations.
752 if not Assume_Valid and then not Assume_No_Invalid_Values then
753 if Is_Entity_Name (L) and then not Is_Known_Valid (Entity (L)) then
754 Ltyp := Base_Type (Ltyp);
757 if Is_Entity_Name (R) and then not Is_Known_Valid (Entity (R)) then
758 Rtyp := Base_Type (Rtyp);
762 -- Try range analysis on variables and see if ranges are disjoint
770 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
771 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
795 -- Here is where we check for comparisons against maximum bounds of
796 -- types, where we know that no value can be outside the bounds of
797 -- the subtype. Note that this routine is allowed to assume that all
798 -- expressions are within their subtype bounds. Callers wishing to
799 -- deal with possibly invalid values must in any case take special
800 -- steps (e.g. conversions to larger types) to avoid this kind of
801 -- optimization, which is always considered to be valid. We do not
802 -- attempt this optimization with generic types, since the type
803 -- bounds may not be meaningful in this case.
805 -- We are in danger of an infinite recursion here. It does not seem
806 -- useful to go more than one level deep, so the parameter Rec is
807 -- used to protect ourselves against this infinite recursion.
811 -- See if we can get a decisive check against one operand and
812 -- a bound of the other operand (four possible tests here).
814 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
815 Assume_Valid, Rec => True) is
816 when LT => return LT;
817 when LE => return LE;
818 when EQ => return LE;
822 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
823 Assume_Valid, Rec => True) is
824 when GT => return GT;
825 when GE => return GE;
826 when EQ => return GE;
830 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
831 Assume_Valid, Rec => True) is
832 when GT => return GT;
833 when GE => return GE;
834 when EQ => return GE;
838 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
839 Assume_Valid, Rec => True) is
840 when LT => return LT;
841 when LE => return LE;
842 when EQ => return LE;
847 -- Next attempt is to decompose the expressions to extract
848 -- a constant offset resulting from the use of any of the forms:
855 -- Then we see if the two expressions are the same value, and if so
856 -- the result is obtained by comparing the offsets.
865 Compare_Decompose (L, Lnode, Loffs);
866 Compare_Decompose (R, Rnode, Roffs);
868 if Is_Same_Value (Lnode, Rnode) then
869 if Loffs = Roffs then
872 elsif Loffs < Roffs then
881 -- Next attempt is to see if we have an entity compared with a
882 -- compile time known value, where there is a current value
883 -- conditional for the entity which can tell us the result.
887 -- Entity variable (left operand)
890 -- Value (right operand)
893 -- If False, we have reversed the operands
896 -- Comparison operator kind from Get_Current_Value_Condition call
899 -- Value from Get_Current_Value_Condition call
904 Result : Compare_Result;
905 -- Known result before inversion
908 if Is_Entity_Name (L)
909 and then Compile_Time_Known_Value (R)
912 Val := Expr_Value (R);
915 elsif Is_Entity_Name (R)
916 and then Compile_Time_Known_Value (L)
919 Val := Expr_Value (L);
922 -- That was the last chance at finding a compile time result
928 Get_Current_Value_Condition (Var, Op, Opn);
930 -- That was the last chance, so if we got nothing return
936 Opv := Expr_Value (Opn);
938 -- We got a comparison, so we might have something interesting
940 -- Convert LE to LT and GE to GT, just so we have fewer cases
945 elsif Op = N_Op_Ge then
950 -- Deal with equality case
961 -- Deal with inequality case
963 elsif Op = N_Op_Ne then
970 -- Deal with greater than case
972 elsif Op = N_Op_Gt then
975 elsif Opv = Val - 1 then
981 -- Deal with less than case
983 else pragma Assert (Op = N_Op_Lt);
986 elsif Opv = Val + 1 then
993 -- Deal with inverting result
997 when GT => return LT;
998 when GE => return LE;
999 when LT => return GT;
1000 when LE => return GE;
1001 when others => return Result;
1008 end Compile_Time_Compare;
1010 -------------------------------
1011 -- Compile_Time_Known_Bounds --
1012 -------------------------------
1014 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1019 if not Is_Array_Type (T) then
1023 Indx := First_Index (T);
1024 while Present (Indx) loop
1025 Typ := Underlying_Type (Etype (Indx));
1026 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1028 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1036 end Compile_Time_Known_Bounds;
1038 ------------------------------
1039 -- Compile_Time_Known_Value --
1040 ------------------------------
1042 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
1043 K : constant Node_Kind := Nkind (Op);
1044 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
1047 -- Never known at compile time if bad type or raises constraint error
1048 -- or empty (latter case occurs only as a result of a previous error)
1052 or else Etype (Op) = Any_Type
1053 or else Raises_Constraint_Error (Op)
1058 -- If this is not a static expression or a null literal, and we are in
1059 -- configurable run-time mode, then we consider it not known at compile
1060 -- time. This avoids anomalies where whether something is allowed with a
1061 -- given configurable run-time library depends on how good the compiler
1062 -- is at optimizing and knowing that things are constant when they are
1065 if Configurable_Run_Time_Mode
1066 and then K /= N_Null
1067 and then not Is_Static_Expression (Op)
1072 -- If we have an entity name, then see if it is the name of a constant
1073 -- and if so, test the corresponding constant value, or the name of
1074 -- an enumeration literal, which is always a constant.
1076 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1078 E : constant Entity_Id := Entity (Op);
1082 -- Never known at compile time if it is a packed array value.
1083 -- We might want to try to evaluate these at compile time one
1084 -- day, but we do not make that attempt now.
1086 if Is_Packed_Array_Type (Etype (Op)) then
1090 if Ekind (E) = E_Enumeration_Literal then
1093 elsif Ekind (E) = E_Constant then
1094 V := Constant_Value (E);
1095 return Present (V) and then Compile_Time_Known_Value (V);
1099 -- We have a value, see if it is compile time known
1102 -- Integer literals are worth storing in the cache
1104 if K = N_Integer_Literal then
1106 CV_Ent.V := Intval (Op);
1109 -- Other literals and NULL are known at compile time
1112 K = N_Character_Literal
1116 K = N_String_Literal
1122 -- Any reference to Null_Parameter is known at compile time. No
1123 -- other attribute references (that have not already been folded)
1124 -- are known at compile time.
1126 elsif K = N_Attribute_Reference then
1127 return Attribute_Name (Op) = Name_Null_Parameter;
1131 -- If we fall through, not known at compile time
1135 -- If we get an exception while trying to do this test, then some error
1136 -- has occurred, and we simply say that the value is not known after all
1141 end Compile_Time_Known_Value;
1143 --------------------------------------
1144 -- Compile_Time_Known_Value_Or_Aggr --
1145 --------------------------------------
1147 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1149 -- If we have an entity name, then see if it is the name of a constant
1150 -- and if so, test the corresponding constant value, or the name of
1151 -- an enumeration literal, which is always a constant.
1153 if Is_Entity_Name (Op) then
1155 E : constant Entity_Id := Entity (Op);
1159 if Ekind (E) = E_Enumeration_Literal then
1162 elsif Ekind (E) /= E_Constant then
1166 V := Constant_Value (E);
1168 and then Compile_Time_Known_Value_Or_Aggr (V);
1172 -- We have a value, see if it is compile time known
1175 if Compile_Time_Known_Value (Op) then
1178 elsif Nkind (Op) = N_Aggregate then
1180 if Present (Expressions (Op)) then
1185 Expr := First (Expressions (Op));
1186 while Present (Expr) loop
1187 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1196 if Present (Component_Associations (Op)) then
1201 Cass := First (Component_Associations (Op));
1202 while Present (Cass) loop
1204 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1216 -- All other types of values are not known at compile time
1223 end Compile_Time_Known_Value_Or_Aggr;
1229 -- This is only called for actuals of functions that are not predefined
1230 -- operators (which have already been rewritten as operators at this
1231 -- stage), so the call can never be folded, and all that needs doing for
1232 -- the actual is to do the check for a non-static context.
1234 procedure Eval_Actual (N : Node_Id) is
1236 Check_Non_Static_Context (N);
1239 --------------------
1240 -- Eval_Allocator --
1241 --------------------
1243 -- Allocators are never static, so all we have to do is to do the
1244 -- check for a non-static context if an expression is present.
1246 procedure Eval_Allocator (N : Node_Id) is
1247 Expr : constant Node_Id := Expression (N);
1250 if Nkind (Expr) = N_Qualified_Expression then
1251 Check_Non_Static_Context (Expression (Expr));
1255 ------------------------
1256 -- Eval_Arithmetic_Op --
1257 ------------------------
1259 -- Arithmetic operations are static functions, so the result is static
1260 -- if both operands are static (RM 4.9(7), 4.9(20)).
1262 procedure Eval_Arithmetic_Op (N : Node_Id) is
1263 Left : constant Node_Id := Left_Opnd (N);
1264 Right : constant Node_Id := Right_Opnd (N);
1265 Ltype : constant Entity_Id := Etype (Left);
1266 Rtype : constant Entity_Id := Etype (Right);
1271 -- If not foldable we are done
1273 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1279 -- Fold for cases where both operands are of integer type
1281 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1283 Left_Int : constant Uint := Expr_Value (Left);
1284 Right_Int : constant Uint := Expr_Value (Right);
1291 Result := Left_Int + Right_Int;
1293 when N_Op_Subtract =>
1294 Result := Left_Int - Right_Int;
1296 when N_Op_Multiply =>
1299 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1301 Result := Left_Int * Right_Int;
1308 -- The exception Constraint_Error is raised by integer
1309 -- division, rem and mod if the right operand is zero.
1311 if Right_Int = 0 then
1312 Apply_Compile_Time_Constraint_Error
1313 (N, "division by zero",
1319 Result := Left_Int / Right_Int;
1324 -- The exception Constraint_Error is raised by integer
1325 -- division, rem and mod if the right operand is zero.
1327 if Right_Int = 0 then
1328 Apply_Compile_Time_Constraint_Error
1329 (N, "mod with zero divisor",
1334 Result := Left_Int mod Right_Int;
1339 -- The exception Constraint_Error is raised by integer
1340 -- division, rem and mod if the right operand is zero.
1342 if Right_Int = 0 then
1343 Apply_Compile_Time_Constraint_Error
1344 (N, "rem with zero divisor",
1350 Result := Left_Int rem Right_Int;
1354 raise Program_Error;
1357 -- Adjust the result by the modulus if the type is a modular type
1359 if Is_Modular_Integer_Type (Ltype) then
1360 Result := Result mod Modulus (Ltype);
1362 -- For a signed integer type, check non-static overflow
1364 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1366 BT : constant Entity_Id := Base_Type (Ltype);
1367 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1368 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1370 if Result < Lo or else Result > Hi then
1371 Apply_Compile_Time_Constraint_Error
1372 (N, "value not in range of }?",
1373 CE_Overflow_Check_Failed,
1380 -- If we get here we can fold the result
1382 Fold_Uint (N, Result, Stat);
1385 -- Cases where at least one operand is a real. We handle the cases
1386 -- of both reals, or mixed/real integer cases (the latter happen
1387 -- only for divide and multiply, and the result is always real).
1389 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1396 if Is_Real_Type (Ltype) then
1397 Left_Real := Expr_Value_R (Left);
1399 Left_Real := UR_From_Uint (Expr_Value (Left));
1402 if Is_Real_Type (Rtype) then
1403 Right_Real := Expr_Value_R (Right);
1405 Right_Real := UR_From_Uint (Expr_Value (Right));
1408 if Nkind (N) = N_Op_Add then
1409 Result := Left_Real + Right_Real;
1411 elsif Nkind (N) = N_Op_Subtract then
1412 Result := Left_Real - Right_Real;
1414 elsif Nkind (N) = N_Op_Multiply then
1415 Result := Left_Real * Right_Real;
1417 else pragma Assert (Nkind (N) = N_Op_Divide);
1418 if UR_Is_Zero (Right_Real) then
1419 Apply_Compile_Time_Constraint_Error
1420 (N, "division by zero", CE_Divide_By_Zero);
1424 Result := Left_Real / Right_Real;
1427 Fold_Ureal (N, Result, Stat);
1430 end Eval_Arithmetic_Op;
1432 ----------------------------
1433 -- Eval_Character_Literal --
1434 ----------------------------
1436 -- Nothing to be done!
1438 procedure Eval_Character_Literal (N : Node_Id) is
1439 pragma Warnings (Off, N);
1442 end Eval_Character_Literal;
1448 -- Static function calls are either calls to predefined operators
1449 -- with static arguments, or calls to functions that rename a literal.
1450 -- Only the latter case is handled here, predefined operators are
1451 -- constant-folded elsewhere.
1453 -- If the function is itself inherited (see 7423-001) the literal of
1454 -- the parent type must be explicitly converted to the return type
1457 procedure Eval_Call (N : Node_Id) is
1458 Loc : constant Source_Ptr := Sloc (N);
1459 Typ : constant Entity_Id := Etype (N);
1463 if Nkind (N) = N_Function_Call
1464 and then No (Parameter_Associations (N))
1465 and then Is_Entity_Name (Name (N))
1466 and then Present (Alias (Entity (Name (N))))
1467 and then Is_Enumeration_Type (Base_Type (Typ))
1469 Lit := Alias (Entity (Name (N)));
1470 while Present (Alias (Lit)) loop
1474 if Ekind (Lit) = E_Enumeration_Literal then
1475 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1477 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1479 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1487 ------------------------
1488 -- Eval_Concatenation --
1489 ------------------------
1491 -- Concatenation is a static function, so the result is static if
1492 -- both operands are static (RM 4.9(7), 4.9(21)).
1494 procedure Eval_Concatenation (N : Node_Id) is
1495 Left : constant Node_Id := Left_Opnd (N);
1496 Right : constant Node_Id := Right_Opnd (N);
1497 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
1502 -- Concatenation is never static in Ada 83, so if Ada 83
1503 -- check operand non-static context
1505 if Ada_Version = Ada_83
1506 and then Comes_From_Source (N)
1508 Check_Non_Static_Context (Left);
1509 Check_Non_Static_Context (Right);
1513 -- If not foldable we are done. In principle concatenation that yields
1514 -- any string type is static (i.e. an array type of character types).
1515 -- However, character types can include enumeration literals, and
1516 -- concatenation in that case cannot be described by a literal, so we
1517 -- only consider the operation static if the result is an array of
1518 -- (a descendant of) a predefined character type.
1520 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1522 if Is_Standard_Character_Type (C_Typ)
1527 Set_Is_Static_Expression (N, False);
1531 -- Compile time string concatenation
1533 -- ??? Note that operands that are aggregates can be marked as
1534 -- static, so we should attempt at a later stage to fold
1535 -- concatenations with such aggregates.
1538 Left_Str : constant Node_Id := Get_String_Val (Left);
1540 Right_Str : constant Node_Id := Get_String_Val (Right);
1541 Folded_Val : String_Id;
1544 -- Establish new string literal, and store left operand. We make
1545 -- sure to use the special Start_String that takes an operand if
1546 -- the left operand is a string literal. Since this is optimized
1547 -- in the case where that is the most recently created string
1548 -- literal, we ensure efficient time/space behavior for the
1549 -- case of a concatenation of a series of string literals.
1551 if Nkind (Left_Str) = N_String_Literal then
1552 Left_Len := String_Length (Strval (Left_Str));
1554 -- If the left operand is the empty string, and the right operand
1555 -- is a string literal (the case of "" & "..."), the result is the
1556 -- value of the right operand. This optimization is important when
1557 -- Is_Folded_In_Parser, to avoid copying an enormous right
1560 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1561 Folded_Val := Strval (Right_Str);
1563 Start_String (Strval (Left_Str));
1568 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
1572 -- Now append the characters of the right operand, unless we
1573 -- optimized the "" & "..." case above.
1575 if Nkind (Right_Str) = N_String_Literal then
1576 if Left_Len /= 0 then
1577 Store_String_Chars (Strval (Right_Str));
1578 Folded_Val := End_String;
1581 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
1582 Folded_Val := End_String;
1585 Set_Is_Static_Expression (N, Stat);
1589 -- If left operand is the empty string, the result is the
1590 -- right operand, including its bounds if anomalous.
1593 and then Is_Array_Type (Etype (Right))
1594 and then Etype (Right) /= Any_String
1596 Set_Etype (N, Etype (Right));
1599 Fold_Str (N, Folded_Val, Static => True);
1602 end Eval_Concatenation;
1604 ---------------------------------
1605 -- Eval_Conditional_Expression --
1606 ---------------------------------
1608 -- This GNAT internal construct can never be statically folded, so the
1609 -- only required processing is to do the check for non-static context
1610 -- for the two expression operands.
1612 procedure Eval_Conditional_Expression (N : Node_Id) is
1613 Condition : constant Node_Id := First (Expressions (N));
1614 Then_Expr : constant Node_Id := Next (Condition);
1615 Else_Expr : constant Node_Id := Next (Then_Expr);
1618 Check_Non_Static_Context (Then_Expr);
1619 Check_Non_Static_Context (Else_Expr);
1620 end Eval_Conditional_Expression;
1622 ----------------------
1623 -- Eval_Entity_Name --
1624 ----------------------
1626 -- This procedure is used for identifiers and expanded names other than
1627 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1628 -- static if they denote a static constant (RM 4.9(6)) or if the name
1629 -- denotes an enumeration literal (RM 4.9(22)).
1631 procedure Eval_Entity_Name (N : Node_Id) is
1632 Def_Id : constant Entity_Id := Entity (N);
1636 -- Enumeration literals are always considered to be constants
1637 -- and cannot raise constraint error (RM 4.9(22)).
1639 if Ekind (Def_Id) = E_Enumeration_Literal then
1640 Set_Is_Static_Expression (N);
1643 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1644 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1645 -- it does not violate 10.2.1(8) here, since this is not a variable.
1647 elsif Ekind (Def_Id) = E_Constant then
1649 -- Deferred constants must always be treated as nonstatic
1650 -- outside the scope of their full view.
1652 if Present (Full_View (Def_Id))
1653 and then not In_Open_Scopes (Scope (Def_Id))
1657 Val := Constant_Value (Def_Id);
1660 if Present (Val) then
1661 Set_Is_Static_Expression
1662 (N, Is_Static_Expression (Val)
1663 and then Is_Static_Subtype (Etype (Def_Id)));
1664 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1666 if not Is_Static_Expression (N)
1667 and then not Is_Generic_Type (Etype (N))
1669 Validate_Static_Object_Name (N);
1676 -- Fall through if the name is not static
1678 Validate_Static_Object_Name (N);
1679 end Eval_Entity_Name;
1681 ----------------------------
1682 -- Eval_Indexed_Component --
1683 ----------------------------
1685 -- Indexed components are never static, so we need to perform the check
1686 -- for non-static context on the index values. Then, we check if the
1687 -- value can be obtained at compile time, even though it is non-static.
1689 procedure Eval_Indexed_Component (N : Node_Id) is
1693 -- Check for non-static context on index values
1695 Expr := First (Expressions (N));
1696 while Present (Expr) loop
1697 Check_Non_Static_Context (Expr);
1701 -- If the indexed component appears in an object renaming declaration
1702 -- then we do not want to try to evaluate it, since in this case we
1703 -- need the identity of the array element.
1705 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
1708 -- Similarly if the indexed component appears as the prefix of an
1709 -- attribute we don't want to evaluate it, because at least for
1710 -- some cases of attributes we need the identify (e.g. Access, Size)
1712 elsif Nkind (Parent (N)) = N_Attribute_Reference then
1716 -- Note: there are other cases, such as the left side of an assignment,
1717 -- or an OUT parameter for a call, where the replacement results in the
1718 -- illegal use of a constant, But these cases are illegal in the first
1719 -- place, so the replacement, though silly, is harmless.
1721 -- Now see if this is a constant array reference
1723 if List_Length (Expressions (N)) = 1
1724 and then Is_Entity_Name (Prefix (N))
1725 and then Ekind (Entity (Prefix (N))) = E_Constant
1726 and then Present (Constant_Value (Entity (Prefix (N))))
1729 Loc : constant Source_Ptr := Sloc (N);
1730 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
1731 Sub : constant Node_Id := First (Expressions (N));
1737 -- Linear one's origin subscript value for array reference
1740 -- Lower bound of the first array index
1743 -- Value from constant array
1746 Atyp := Etype (Arr);
1748 if Is_Access_Type (Atyp) then
1749 Atyp := Designated_Type (Atyp);
1752 -- If we have an array type (we should have but perhaps there
1753 -- are error cases where this is not the case), then see if we
1754 -- can do a constant evaluation of the array reference.
1756 if Is_Array_Type (Atyp) then
1757 if Ekind (Atyp) = E_String_Literal_Subtype then
1758 Lbd := String_Literal_Low_Bound (Atyp);
1760 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
1763 if Compile_Time_Known_Value (Sub)
1764 and then Nkind (Arr) = N_Aggregate
1765 and then Compile_Time_Known_Value (Lbd)
1766 and then Is_Discrete_Type (Component_Type (Atyp))
1768 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
1770 if List_Length (Expressions (Arr)) >= Lin then
1771 Elm := Pick (Expressions (Arr), Lin);
1773 -- If the resulting expression is compile time known,
1774 -- then we can rewrite the indexed component with this
1775 -- value, being sure to mark the result as non-static.
1776 -- We also reset the Sloc, in case this generates an
1777 -- error later on (e.g. 136'Access).
1779 if Compile_Time_Known_Value (Elm) then
1780 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
1781 Set_Is_Static_Expression (N, False);
1789 end Eval_Indexed_Component;
1791 --------------------------
1792 -- Eval_Integer_Literal --
1793 --------------------------
1795 -- Numeric literals are static (RM 4.9(1)), and have already been marked
1796 -- as static by the analyzer. The reason we did it that early is to allow
1797 -- the possibility of turning off the Is_Static_Expression flag after
1798 -- analysis, but before resolution, when integer literals are generated
1799 -- in the expander that do not correspond to static expressions.
1801 procedure Eval_Integer_Literal (N : Node_Id) is
1802 T : constant Entity_Id := Etype (N);
1804 function In_Any_Integer_Context return Boolean;
1805 -- If the literal is resolved with a specific type in a context
1806 -- where the expected type is Any_Integer, there are no range checks
1807 -- on the literal. By the time the literal is evaluated, it carries
1808 -- the type imposed by the enclosing expression, and we must recover
1809 -- the context to determine that Any_Integer is meant.
1811 ----------------------------
1812 -- To_Any_Integer_Context --
1813 ----------------------------
1815 function In_Any_Integer_Context return Boolean is
1816 Par : constant Node_Id := Parent (N);
1817 K : constant Node_Kind := Nkind (Par);
1820 -- Any_Integer also appears in digits specifications for real types,
1821 -- but those have bounds smaller that those of any integer base
1822 -- type, so we can safely ignore these cases.
1824 return K = N_Number_Declaration
1825 or else K = N_Attribute_Reference
1826 or else K = N_Attribute_Definition_Clause
1827 or else K = N_Modular_Type_Definition
1828 or else K = N_Signed_Integer_Type_Definition;
1829 end In_Any_Integer_Context;
1831 -- Start of processing for Eval_Integer_Literal
1835 -- If the literal appears in a non-expression context, then it is
1836 -- certainly appearing in a non-static context, so check it. This
1837 -- is actually a redundant check, since Check_Non_Static_Context
1838 -- would check it, but it seems worth while avoiding the call.
1840 if Nkind (Parent (N)) not in N_Subexpr
1841 and then not In_Any_Integer_Context
1843 Check_Non_Static_Context (N);
1846 -- Modular integer literals must be in their base range
1848 if Is_Modular_Integer_Type (T)
1849 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
1853 end Eval_Integer_Literal;
1855 ---------------------
1856 -- Eval_Logical_Op --
1857 ---------------------
1859 -- Logical operations are static functions, so the result is potentially
1860 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
1862 procedure Eval_Logical_Op (N : Node_Id) is
1863 Left : constant Node_Id := Left_Opnd (N);
1864 Right : constant Node_Id := Right_Opnd (N);
1869 -- If not foldable we are done
1871 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1877 -- Compile time evaluation of logical operation
1880 Left_Int : constant Uint := Expr_Value (Left);
1881 Right_Int : constant Uint := Expr_Value (Right);
1884 if Is_Modular_Integer_Type (Etype (N)) then
1886 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1887 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
1890 To_Bits (Left_Int, Left_Bits);
1891 To_Bits (Right_Int, Right_Bits);
1893 -- Note: should really be able to use array ops instead of
1894 -- these loops, but they weren't working at the time ???
1896 if Nkind (N) = N_Op_And then
1897 for J in Left_Bits'Range loop
1898 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
1901 elsif Nkind (N) = N_Op_Or then
1902 for J in Left_Bits'Range loop
1903 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
1907 pragma Assert (Nkind (N) = N_Op_Xor);
1909 for J in Left_Bits'Range loop
1910 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
1914 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
1918 pragma Assert (Is_Boolean_Type (Etype (N)));
1920 if Nkind (N) = N_Op_And then
1922 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
1924 elsif Nkind (N) = N_Op_Or then
1926 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
1929 pragma Assert (Nkind (N) = N_Op_Xor);
1931 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
1935 end Eval_Logical_Op;
1937 ------------------------
1938 -- Eval_Membership_Op --
1939 ------------------------
1941 -- A membership test is potentially static if the expression is static,
1942 -- and the range is a potentially static range, or is a subtype mark
1943 -- denoting a static subtype (RM 4.9(12)).
1945 procedure Eval_Membership_Op (N : Node_Id) is
1946 Left : constant Node_Id := Left_Opnd (N);
1947 Right : constant Node_Id := Right_Opnd (N);
1956 -- Ignore if error in either operand, except to make sure that
1957 -- Any_Type is properly propagated to avoid junk cascaded errors.
1959 if Etype (Left) = Any_Type
1960 or else Etype (Right) = Any_Type
1962 Set_Etype (N, Any_Type);
1966 -- Case of right operand is a subtype name
1968 if Is_Entity_Name (Right) then
1969 Def_Id := Entity (Right);
1971 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
1972 and then Is_OK_Static_Subtype (Def_Id)
1974 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
1976 if not Fold or else not Stat then
1980 Check_Non_Static_Context (Left);
1984 -- For string membership tests we will check the length
1987 if not Is_String_Type (Def_Id) then
1988 Lo := Type_Low_Bound (Def_Id);
1989 Hi := Type_High_Bound (Def_Id);
1996 -- Case of right operand is a range
1999 if Is_Static_Range (Right) then
2000 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2002 if not Fold or else not Stat then
2005 -- If one bound of range raises CE, then don't try to fold
2007 elsif not Is_OK_Static_Range (Right) then
2008 Check_Non_Static_Context (Left);
2013 Check_Non_Static_Context (Left);
2017 -- Here we know range is an OK static range
2019 Lo := Low_Bound (Right);
2020 Hi := High_Bound (Right);
2023 -- For strings we check that the length of the string expression is
2024 -- compatible with the string subtype if the subtype is constrained,
2025 -- or if unconstrained then the test is always true.
2027 if Is_String_Type (Etype (Right)) then
2028 if not Is_Constrained (Etype (Right)) then
2033 Typlen : constant Uint := String_Type_Len (Etype (Right));
2034 Strlen : constant Uint :=
2035 UI_From_Int (String_Length (Strval (Get_String_Val (Left))));
2037 Result := (Typlen = Strlen);
2041 -- Fold the membership test. We know we have a static range and Lo
2042 -- and Hi are set to the expressions for the end points of this range.
2044 elsif Is_Real_Type (Etype (Right)) then
2046 Leftval : constant Ureal := Expr_Value_R (Left);
2049 Result := Expr_Value_R (Lo) <= Leftval
2050 and then Leftval <= Expr_Value_R (Hi);
2055 Leftval : constant Uint := Expr_Value (Left);
2058 Result := Expr_Value (Lo) <= Leftval
2059 and then Leftval <= Expr_Value (Hi);
2063 if Nkind (N) = N_Not_In then
2064 Result := not Result;
2067 Fold_Uint (N, Test (Result), True);
2068 Warn_On_Known_Condition (N);
2069 end Eval_Membership_Op;
2071 ------------------------
2072 -- Eval_Named_Integer --
2073 ------------------------
2075 procedure Eval_Named_Integer (N : Node_Id) is
2078 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
2079 end Eval_Named_Integer;
2081 ---------------------
2082 -- Eval_Named_Real --
2083 ---------------------
2085 procedure Eval_Named_Real (N : Node_Id) is
2088 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
2089 end Eval_Named_Real;
2095 -- Exponentiation is a static functions, so the result is potentially
2096 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2098 procedure Eval_Op_Expon (N : Node_Id) is
2099 Left : constant Node_Id := Left_Opnd (N);
2100 Right : constant Node_Id := Right_Opnd (N);
2105 -- If not foldable we are done
2107 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2113 -- Fold exponentiation operation
2116 Right_Int : constant Uint := Expr_Value (Right);
2121 if Is_Integer_Type (Etype (Left)) then
2123 Left_Int : constant Uint := Expr_Value (Left);
2127 -- Exponentiation of an integer raises the exception
2128 -- Constraint_Error for a negative exponent (RM 4.5.6)
2130 if Right_Int < 0 then
2131 Apply_Compile_Time_Constraint_Error
2132 (N, "integer exponent negative",
2133 CE_Range_Check_Failed,
2138 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2139 Result := Left_Int ** Right_Int;
2144 if Is_Modular_Integer_Type (Etype (N)) then
2145 Result := Result mod Modulus (Etype (N));
2148 Fold_Uint (N, Result, Stat);
2156 Left_Real : constant Ureal := Expr_Value_R (Left);
2159 -- Cannot have a zero base with a negative exponent
2161 if UR_Is_Zero (Left_Real) then
2163 if Right_Int < 0 then
2164 Apply_Compile_Time_Constraint_Error
2165 (N, "zero ** negative integer",
2166 CE_Range_Check_Failed,
2170 Fold_Ureal (N, Ureal_0, Stat);
2174 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
2185 -- The not operation is a static functions, so the result is potentially
2186 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2188 procedure Eval_Op_Not (N : Node_Id) is
2189 Right : constant Node_Id := Right_Opnd (N);
2194 -- If not foldable we are done
2196 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2202 -- Fold not operation
2205 Rint : constant Uint := Expr_Value (Right);
2206 Typ : constant Entity_Id := Etype (N);
2209 -- Negation is equivalent to subtracting from the modulus minus
2210 -- one. For a binary modulus this is equivalent to the ones-
2211 -- component of the original value. For non-binary modulus this
2212 -- is an arbitrary but consistent definition.
2214 if Is_Modular_Integer_Type (Typ) then
2215 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
2218 pragma Assert (Is_Boolean_Type (Typ));
2219 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
2222 Set_Is_Static_Expression (N, Stat);
2226 -------------------------------
2227 -- Eval_Qualified_Expression --
2228 -------------------------------
2230 -- A qualified expression is potentially static if its subtype mark denotes
2231 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2233 procedure Eval_Qualified_Expression (N : Node_Id) is
2234 Operand : constant Node_Id := Expression (N);
2235 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2242 -- Can only fold if target is string or scalar and subtype is static
2243 -- Also, do not fold if our parent is an allocator (this is because
2244 -- the qualified expression is really part of the syntactic structure
2245 -- of an allocator, and we do not want to end up with something that
2246 -- corresponds to "new 1" where the 1 is the result of folding a
2247 -- qualified expression).
2249 if not Is_Static_Subtype (Target_Type)
2250 or else Nkind (Parent (N)) = N_Allocator
2252 Check_Non_Static_Context (Operand);
2254 -- If operand is known to raise constraint_error, set the
2255 -- flag on the expression so it does not get optimized away.
2257 if Nkind (Operand) = N_Raise_Constraint_Error then
2258 Set_Raises_Constraint_Error (N);
2264 -- If not foldable we are done
2266 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2271 -- Don't try fold if target type has constraint error bounds
2273 elsif not Is_OK_Static_Subtype (Target_Type) then
2274 Set_Raises_Constraint_Error (N);
2278 -- Here we will fold, save Print_In_Hex indication
2280 Hex := Nkind (Operand) = N_Integer_Literal
2281 and then Print_In_Hex (Operand);
2283 -- Fold the result of qualification
2285 if Is_Discrete_Type (Target_Type) then
2286 Fold_Uint (N, Expr_Value (Operand), Stat);
2288 -- Preserve Print_In_Hex indication
2290 if Hex and then Nkind (N) = N_Integer_Literal then
2291 Set_Print_In_Hex (N);
2294 elsif Is_Real_Type (Target_Type) then
2295 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
2298 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
2301 Set_Is_Static_Expression (N, False);
2303 Check_String_Literal_Length (N, Target_Type);
2309 -- The expression may be foldable but not static
2311 Set_Is_Static_Expression (N, Stat);
2313 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
2316 end Eval_Qualified_Expression;
2318 -----------------------
2319 -- Eval_Real_Literal --
2320 -----------------------
2322 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2323 -- as static by the analyzer. The reason we did it that early is to allow
2324 -- the possibility of turning off the Is_Static_Expression flag after
2325 -- analysis, but before resolution, when integer literals are generated
2326 -- in the expander that do not correspond to static expressions.
2328 procedure Eval_Real_Literal (N : Node_Id) is
2329 PK : constant Node_Kind := Nkind (Parent (N));
2332 -- If the literal appears in a non-expression context
2333 -- and not as part of a number declaration, then it is
2334 -- appearing in a non-static context, so check it.
2336 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
2337 Check_Non_Static_Context (N);
2339 end Eval_Real_Literal;
2341 ------------------------
2342 -- Eval_Relational_Op --
2343 ------------------------
2345 -- Relational operations are static functions, so the result is static
2346 -- if both operands are static (RM 4.9(7), 4.9(20)).
2348 procedure Eval_Relational_Op (N : Node_Id) is
2349 Left : constant Node_Id := Left_Opnd (N);
2350 Right : constant Node_Id := Right_Opnd (N);
2351 Typ : constant Entity_Id := Etype (Left);
2357 -- One special case to deal with first. If we can tell that the result
2358 -- will be false because the lengths of one or more index subtypes are
2359 -- compile time known and different, then we can replace the entire
2360 -- result by False. We only do this for one dimensional arrays, because
2361 -- the case of multi-dimensional arrays is rare and too much trouble! If
2362 -- one of the operands is an illegal aggregate, its type might still be
2363 -- an arbitrary composite type, so nothing to do.
2365 if Is_Array_Type (Typ)
2366 and then Typ /= Any_Composite
2367 and then Number_Dimensions (Typ) = 1
2368 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
2370 if Raises_Constraint_Error (Left)
2371 or else Raises_Constraint_Error (Right)
2376 -- OK, we have the case where we may be able to do this fold
2378 Length_Mismatch : declare
2379 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
2380 -- If Op is an expression for a constrained array with a known
2381 -- at compile time length, then Len is set to this (non-negative
2382 -- length). Otherwise Len is set to minus 1.
2384 -----------------------
2385 -- Get_Static_Length --
2386 -----------------------
2388 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2392 -- First easy case string literal
2394 if Nkind (Op) = N_String_Literal then
2395 Len := UI_From_Int (String_Length (Strval (Op)));
2399 -- Second easy case, not constrained subtype, so no length
2401 if not Is_Constrained (Etype (Op)) then
2402 Len := Uint_Minus_1;
2408 T := Etype (First_Index (Etype (Op)));
2410 -- The simple case, both bounds are known at compile time
2412 if Is_Discrete_Type (T)
2414 Compile_Time_Known_Value (Type_Low_Bound (T))
2416 Compile_Time_Known_Value (Type_High_Bound (T))
2418 Len := UI_Max (Uint_0,
2419 Expr_Value (Type_High_Bound (T)) -
2420 Expr_Value (Type_Low_Bound (T)) + 1);
2424 -- A more complex case, where the bounds are of the form
2425 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2426 -- either A'First or A'Last (with A an entity name), or X is an
2427 -- entity name, and the two X's are the same and K1 and K2 are
2428 -- known at compile time, in this case, the length can also be
2429 -- computed at compile time, even though the bounds are not
2430 -- known. A common case of this is e.g. (X'First..X'First+5).
2432 Extract_Length : declare
2433 procedure Decompose_Expr
2435 Ent : out Entity_Id;
2436 Kind : out Character;
2438 -- Given an expression, see if is of the form above,
2439 -- X [+/- K]. If so Ent is set to the entity in X,
2440 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2441 -- and Cons is the value of K. If the expression is
2442 -- not of the required form, Ent is set to Empty.
2444 --------------------
2445 -- Decompose_Expr --
2446 --------------------
2448 procedure Decompose_Expr
2450 Ent : out Entity_Id;
2451 Kind : out Character;
2457 if Nkind (Expr) = N_Op_Add
2458 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2460 Exp := Left_Opnd (Expr);
2461 Cons := Expr_Value (Right_Opnd (Expr));
2463 elsif Nkind (Expr) = N_Op_Subtract
2464 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2466 Exp := Left_Opnd (Expr);
2467 Cons := -Expr_Value (Right_Opnd (Expr));
2474 -- At this stage Exp is set to the potential X
2476 if Nkind (Exp) = N_Attribute_Reference then
2477 if Attribute_Name (Exp) = Name_First then
2479 elsif Attribute_Name (Exp) = Name_Last then
2486 Exp := Prefix (Exp);
2492 if Is_Entity_Name (Exp)
2493 and then Present (Entity (Exp))
2495 Ent := Entity (Exp);
2503 Ent1, Ent2 : Entity_Id;
2504 Kind1, Kind2 : Character;
2505 Cons1, Cons2 : Uint;
2507 -- Start of processing for Extract_Length
2510 Decompose_Expr (Type_Low_Bound (T), Ent1, Kind1, Cons1);
2511 Decompose_Expr (Type_High_Bound (T), Ent2, Kind2, Cons2);
2514 and then Kind1 = Kind2
2515 and then Ent1 = Ent2
2517 Len := Cons2 - Cons1 + 1;
2519 Len := Uint_Minus_1;
2522 end Get_Static_Length;
2529 -- Start of processing for Length_Mismatch
2532 Get_Static_Length (Left, Len_L);
2533 Get_Static_Length (Right, Len_R);
2535 if Len_L /= Uint_Minus_1
2536 and then Len_R /= Uint_Minus_1
2537 and then Len_L /= Len_R
2539 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2540 Warn_On_Known_Condition (N);
2543 end Length_Mismatch;
2546 -- Another special case: comparisons of access types, where one or both
2547 -- operands are known to be null, so the result can be determined.
2549 if Is_Access_Type (Typ) then
2550 if Known_Null (Left) then
2551 if Known_Null (Right) then
2552 Fold_Uint (N, Test (Nkind (N) = N_Op_Eq), False);
2553 Warn_On_Known_Condition (N);
2556 elsif Known_Non_Null (Right) then
2557 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2558 Warn_On_Known_Condition (N);
2562 elsif Known_Non_Null (Left) then
2563 if Known_Null (Right) then
2564 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
2565 Warn_On_Known_Condition (N);
2571 -- Can only fold if type is scalar (don't fold string ops)
2573 if not Is_Scalar_Type (Typ) then
2574 Check_Non_Static_Context (Left);
2575 Check_Non_Static_Context (Right);
2579 -- If not foldable we are done
2581 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2587 -- Integer and Enumeration (discrete) type cases
2589 if Is_Discrete_Type (Typ) then
2591 Left_Int : constant Uint := Expr_Value (Left);
2592 Right_Int : constant Uint := Expr_Value (Right);
2596 when N_Op_Eq => Result := Left_Int = Right_Int;
2597 when N_Op_Ne => Result := Left_Int /= Right_Int;
2598 when N_Op_Lt => Result := Left_Int < Right_Int;
2599 when N_Op_Le => Result := Left_Int <= Right_Int;
2600 when N_Op_Gt => Result := Left_Int > Right_Int;
2601 when N_Op_Ge => Result := Left_Int >= Right_Int;
2604 raise Program_Error;
2607 Fold_Uint (N, Test (Result), Stat);
2613 pragma Assert (Is_Real_Type (Typ));
2616 Left_Real : constant Ureal := Expr_Value_R (Left);
2617 Right_Real : constant Ureal := Expr_Value_R (Right);
2621 when N_Op_Eq => Result := (Left_Real = Right_Real);
2622 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2623 when N_Op_Lt => Result := (Left_Real < Right_Real);
2624 when N_Op_Le => Result := (Left_Real <= Right_Real);
2625 when N_Op_Gt => Result := (Left_Real > Right_Real);
2626 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2629 raise Program_Error;
2632 Fold_Uint (N, Test (Result), Stat);
2636 Warn_On_Known_Condition (N);
2637 end Eval_Relational_Op;
2643 -- Shift operations are intrinsic operations that can never be static,
2644 -- so the only processing required is to perform the required check for
2645 -- a non static context for the two operands.
2647 -- Actually we could do some compile time evaluation here some time ???
2649 procedure Eval_Shift (N : Node_Id) is
2651 Check_Non_Static_Context (Left_Opnd (N));
2652 Check_Non_Static_Context (Right_Opnd (N));
2655 ------------------------
2656 -- Eval_Short_Circuit --
2657 ------------------------
2659 -- A short circuit operation is potentially static if both operands
2660 -- are potentially static (RM 4.9 (13))
2662 procedure Eval_Short_Circuit (N : Node_Id) is
2663 Kind : constant Node_Kind := Nkind (N);
2664 Left : constant Node_Id := Left_Opnd (N);
2665 Right : constant Node_Id := Right_Opnd (N);
2667 Rstat : constant Boolean :=
2668 Is_Static_Expression (Left)
2669 and then Is_Static_Expression (Right);
2672 -- Short circuit operations are never static in Ada 83
2674 if Ada_Version = Ada_83
2675 and then Comes_From_Source (N)
2677 Check_Non_Static_Context (Left);
2678 Check_Non_Static_Context (Right);
2682 -- Now look at the operands, we can't quite use the normal call to
2683 -- Test_Expression_Is_Foldable here because short circuit operations
2684 -- are a special case, they can still be foldable, even if the right
2685 -- operand raises constraint error.
2687 -- If either operand is Any_Type, just propagate to result and
2688 -- do not try to fold, this prevents cascaded errors.
2690 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
2691 Set_Etype (N, Any_Type);
2694 -- If left operand raises constraint error, then replace node N with
2695 -- the raise constraint error node, and we are obviously not foldable.
2696 -- Is_Static_Expression is set from the two operands in the normal way,
2697 -- and we check the right operand if it is in a non-static context.
2699 elsif Raises_Constraint_Error (Left) then
2701 Check_Non_Static_Context (Right);
2704 Rewrite_In_Raise_CE (N, Left);
2705 Set_Is_Static_Expression (N, Rstat);
2708 -- If the result is not static, then we won't in any case fold
2710 elsif not Rstat then
2711 Check_Non_Static_Context (Left);
2712 Check_Non_Static_Context (Right);
2716 -- Here the result is static, note that, unlike the normal processing
2717 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
2718 -- the right operand raises constraint error, that's because it is not
2719 -- significant if the left operand is decisive.
2721 Set_Is_Static_Expression (N);
2723 -- It does not matter if the right operand raises constraint error if
2724 -- it will not be evaluated. So deal specially with the cases where
2725 -- the right operand is not evaluated. Note that we will fold these
2726 -- cases even if the right operand is non-static, which is fine, but
2727 -- of course in these cases the result is not potentially static.
2729 Left_Int := Expr_Value (Left);
2731 if (Kind = N_And_Then and then Is_False (Left_Int))
2732 or else (Kind = N_Or_Else and Is_True (Left_Int))
2734 Fold_Uint (N, Left_Int, Rstat);
2738 -- If first operand not decisive, then it does matter if the right
2739 -- operand raises constraint error, since it will be evaluated, so
2740 -- we simply replace the node with the right operand. Note that this
2741 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
2742 -- (both are set to True in Right).
2744 if Raises_Constraint_Error (Right) then
2745 Rewrite_In_Raise_CE (N, Right);
2746 Check_Non_Static_Context (Left);
2750 -- Otherwise the result depends on the right operand
2752 Fold_Uint (N, Expr_Value (Right), Rstat);
2754 end Eval_Short_Circuit;
2760 -- Slices can never be static, so the only processing required is to
2761 -- check for non-static context if an explicit range is given.
2763 procedure Eval_Slice (N : Node_Id) is
2764 Drange : constant Node_Id := Discrete_Range (N);
2766 if Nkind (Drange) = N_Range then
2767 Check_Non_Static_Context (Low_Bound (Drange));
2768 Check_Non_Static_Context (High_Bound (Drange));
2771 -- A slice of the form A (subtype), when the subtype is the index of
2772 -- the type of A, is redundant, the slice can be replaced with A, and
2773 -- this is worth a warning.
2775 if Is_Entity_Name (Prefix (N)) then
2777 E : constant Entity_Id := Entity (Prefix (N));
2778 T : constant Entity_Id := Etype (E);
2780 if Ekind (E) = E_Constant
2781 and then Is_Array_Type (T)
2782 and then Is_Entity_Name (Drange)
2784 if Is_Entity_Name (Original_Node (First_Index (T)))
2785 and then Entity (Original_Node (First_Index (T)))
2788 if Warn_On_Redundant_Constructs then
2789 Error_Msg_N ("redundant slice denotes whole array?", N);
2792 -- The following might be a useful optimization ????
2794 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
2801 -------------------------
2802 -- Eval_String_Literal --
2803 -------------------------
2805 procedure Eval_String_Literal (N : Node_Id) is
2806 Typ : constant Entity_Id := Etype (N);
2807 Bas : constant Entity_Id := Base_Type (Typ);
2813 -- Nothing to do if error type (handles cases like default expressions
2814 -- or generics where we have not yet fully resolved the type)
2816 if Bas = Any_Type or else Bas = Any_String then
2820 -- String literals are static if the subtype is static (RM 4.9(2)), so
2821 -- reset the static expression flag (it was set unconditionally in
2822 -- Analyze_String_Literal) if the subtype is non-static. We tell if
2823 -- the subtype is static by looking at the lower bound.
2825 if Ekind (Typ) = E_String_Literal_Subtype then
2826 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
2827 Set_Is_Static_Expression (N, False);
2831 -- Here if Etype of string literal is normal Etype (not yet possible,
2832 -- but may be possible in future!)
2834 elsif not Is_OK_Static_Expression
2835 (Type_Low_Bound (Etype (First_Index (Typ))))
2837 Set_Is_Static_Expression (N, False);
2841 -- If original node was a type conversion, then result if non-static
2843 if Nkind (Original_Node (N)) = N_Type_Conversion then
2844 Set_Is_Static_Expression (N, False);
2848 -- Test for illegal Ada 95 cases. A string literal is illegal in
2849 -- Ada 95 if its bounds are outside the index base type and this
2850 -- index type is static. This can happen in only two ways. Either
2851 -- the string literal is too long, or it is null, and the lower
2852 -- bound is type'First. In either case it is the upper bound that
2853 -- is out of range of the index type.
2855 if Ada_Version >= Ada_95 then
2856 if Root_Type (Bas) = Standard_String
2858 Root_Type (Bas) = Standard_Wide_String
2860 Xtp := Standard_Positive;
2862 Xtp := Etype (First_Index (Bas));
2865 if Ekind (Typ) = E_String_Literal_Subtype then
2866 Lo := String_Literal_Low_Bound (Typ);
2868 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
2871 Len := String_Length (Strval (N));
2873 if UI_From_Int (Len) > String_Type_Len (Bas) then
2874 Apply_Compile_Time_Constraint_Error
2875 (N, "string literal too long for}", CE_Length_Check_Failed,
2877 Typ => First_Subtype (Bas));
2880 and then not Is_Generic_Type (Xtp)
2882 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
2884 Apply_Compile_Time_Constraint_Error
2885 (N, "null string literal not allowed for}",
2886 CE_Length_Check_Failed,
2888 Typ => First_Subtype (Bas));
2891 end Eval_String_Literal;
2893 --------------------------
2894 -- Eval_Type_Conversion --
2895 --------------------------
2897 -- A type conversion is potentially static if its subtype mark is for a
2898 -- static scalar subtype, and its operand expression is potentially static
2901 procedure Eval_Type_Conversion (N : Node_Id) is
2902 Operand : constant Node_Id := Expression (N);
2903 Source_Type : constant Entity_Id := Etype (Operand);
2904 Target_Type : constant Entity_Id := Etype (N);
2909 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
2910 -- Returns true if type T is an integer type, or if it is a
2911 -- fixed-point type to be treated as an integer (i.e. the flag
2912 -- Conversion_OK is set on the conversion node).
2914 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
2915 -- Returns true if type T is a floating-point type, or if it is a
2916 -- fixed-point type that is not to be treated as an integer (i.e. the
2917 -- flag Conversion_OK is not set on the conversion node).
2919 ------------------------------
2920 -- To_Be_Treated_As_Integer --
2921 ------------------------------
2923 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
2927 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
2928 end To_Be_Treated_As_Integer;
2930 ---------------------------
2931 -- To_Be_Treated_As_Real --
2932 ---------------------------
2934 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
2937 Is_Floating_Point_Type (T)
2938 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
2939 end To_Be_Treated_As_Real;
2941 -- Start of processing for Eval_Type_Conversion
2944 -- Cannot fold if target type is non-static or if semantic error
2946 if not Is_Static_Subtype (Target_Type) then
2947 Check_Non_Static_Context (Operand);
2950 elsif Error_Posted (N) then
2954 -- If not foldable we are done
2956 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2961 -- Don't try fold if target type has constraint error bounds
2963 elsif not Is_OK_Static_Subtype (Target_Type) then
2964 Set_Raises_Constraint_Error (N);
2968 -- Remaining processing depends on operand types. Note that in the
2969 -- following type test, fixed-point counts as real unless the flag
2970 -- Conversion_OK is set, in which case it counts as integer.
2972 -- Fold conversion, case of string type. The result is not static
2974 if Is_String_Type (Target_Type) then
2975 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
2979 -- Fold conversion, case of integer target type
2981 elsif To_Be_Treated_As_Integer (Target_Type) then
2986 -- Integer to integer conversion
2988 if To_Be_Treated_As_Integer (Source_Type) then
2989 Result := Expr_Value (Operand);
2991 -- Real to integer conversion
2994 Result := UR_To_Uint (Expr_Value_R (Operand));
2997 -- If fixed-point type (Conversion_OK must be set), then the
2998 -- result is logically an integer, but we must replace the
2999 -- conversion with the corresponding real literal, since the
3000 -- type from a semantic point of view is still fixed-point.
3002 if Is_Fixed_Point_Type (Target_Type) then
3004 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
3006 -- Otherwise result is integer literal
3009 Fold_Uint (N, Result, Stat);
3013 -- Fold conversion, case of real target type
3015 elsif To_Be_Treated_As_Real (Target_Type) then
3020 if To_Be_Treated_As_Real (Source_Type) then
3021 Result := Expr_Value_R (Operand);
3023 Result := UR_From_Uint (Expr_Value (Operand));
3026 Fold_Ureal (N, Result, Stat);
3029 -- Enumeration types
3032 Fold_Uint (N, Expr_Value (Operand), Stat);
3035 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
3039 end Eval_Type_Conversion;
3045 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3046 -- are potentially static if the operand is potentially static (RM 4.9(7))
3048 procedure Eval_Unary_Op (N : Node_Id) is
3049 Right : constant Node_Id := Right_Opnd (N);
3054 -- If not foldable we are done
3056 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3062 -- Fold for integer case
3064 if Is_Integer_Type (Etype (N)) then
3066 Rint : constant Uint := Expr_Value (Right);
3070 -- In the case of modular unary plus and abs there is no need
3071 -- to adjust the result of the operation since if the original
3072 -- operand was in bounds the result will be in the bounds of the
3073 -- modular type. However, in the case of modular unary minus the
3074 -- result may go out of the bounds of the modular type and needs
3077 if Nkind (N) = N_Op_Plus then
3080 elsif Nkind (N) = N_Op_Minus then
3081 if Is_Modular_Integer_Type (Etype (N)) then
3082 Result := (-Rint) mod Modulus (Etype (N));
3088 pragma Assert (Nkind (N) = N_Op_Abs);
3092 Fold_Uint (N, Result, Stat);
3095 -- Fold for real case
3097 elsif Is_Real_Type (Etype (N)) then
3099 Rreal : constant Ureal := Expr_Value_R (Right);
3103 if Nkind (N) = N_Op_Plus then
3106 elsif Nkind (N) = N_Op_Minus then
3107 Result := UR_Negate (Rreal);
3110 pragma Assert (Nkind (N) = N_Op_Abs);
3111 Result := abs Rreal;
3114 Fold_Ureal (N, Result, Stat);
3119 -------------------------------
3120 -- Eval_Unchecked_Conversion --
3121 -------------------------------
3123 -- Unchecked conversions can never be static, so the only required
3124 -- processing is to check for a non-static context for the operand.
3126 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3128 Check_Non_Static_Context (Expression (N));
3129 end Eval_Unchecked_Conversion;
3131 --------------------
3132 -- Expr_Rep_Value --
3133 --------------------
3135 function Expr_Rep_Value (N : Node_Id) return Uint is
3136 Kind : constant Node_Kind := Nkind (N);
3140 if Is_Entity_Name (N) then
3143 -- An enumeration literal that was either in the source or
3144 -- created as a result of static evaluation.
3146 if Ekind (Ent) = E_Enumeration_Literal then
3147 return Enumeration_Rep (Ent);
3149 -- A user defined static constant
3152 pragma Assert (Ekind (Ent) = E_Constant);
3153 return Expr_Rep_Value (Constant_Value (Ent));
3156 -- An integer literal that was either in the source or created
3157 -- as a result of static evaluation.
3159 elsif Kind = N_Integer_Literal then
3162 -- A real literal for a fixed-point type. This must be the fixed-point
3163 -- case, either the literal is of a fixed-point type, or it is a bound
3164 -- of a fixed-point type, with type universal real. In either case we
3165 -- obtain the desired value from Corresponding_Integer_Value.
3167 elsif Kind = N_Real_Literal then
3168 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3169 return Corresponding_Integer_Value (N);
3171 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3173 elsif Kind = N_Attribute_Reference
3174 and then Attribute_Name (N) = Name_Null_Parameter
3178 -- Otherwise must be character literal
3181 pragma Assert (Kind = N_Character_Literal);
3184 -- Since Character literals of type Standard.Character don't
3185 -- have any defining character literals built for them, they
3186 -- do not have their Entity set, so just use their Char
3187 -- code. Otherwise for user-defined character literals use
3188 -- their Pos value as usual which is the same as the Rep value.
3191 return Char_Literal_Value (N);
3193 return Enumeration_Rep (Ent);
3202 function Expr_Value (N : Node_Id) return Uint is
3203 Kind : constant Node_Kind := Nkind (N);
3204 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
3209 -- If already in cache, then we know it's compile time known and we can
3210 -- return the value that was previously stored in the cache since
3211 -- compile time known values cannot change.
3213 if CV_Ent.N = N then
3217 -- Otherwise proceed to test value
3219 if Is_Entity_Name (N) then
3222 -- An enumeration literal that was either in the source or
3223 -- created as a result of static evaluation.
3225 if Ekind (Ent) = E_Enumeration_Literal then
3226 Val := Enumeration_Pos (Ent);
3228 -- A user defined static constant
3231 pragma Assert (Ekind (Ent) = E_Constant);
3232 Val := Expr_Value (Constant_Value (Ent));
3235 -- An integer literal that was either in the source or created
3236 -- as a result of static evaluation.
3238 elsif Kind = N_Integer_Literal then
3241 -- A real literal for a fixed-point type. This must be the fixed-point
3242 -- case, either the literal is of a fixed-point type, or it is a bound
3243 -- of a fixed-point type, with type universal real. In either case we
3244 -- obtain the desired value from Corresponding_Integer_Value.
3246 elsif Kind = N_Real_Literal then
3248 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3249 Val := Corresponding_Integer_Value (N);
3251 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3253 elsif Kind = N_Attribute_Reference
3254 and then Attribute_Name (N) = Name_Null_Parameter
3258 -- Otherwise must be character literal
3261 pragma Assert (Kind = N_Character_Literal);
3264 -- Since Character literals of type Standard.Character don't
3265 -- have any defining character literals built for them, they
3266 -- do not have their Entity set, so just use their Char
3267 -- code. Otherwise for user-defined character literals use
3268 -- their Pos value as usual.
3271 Val := Char_Literal_Value (N);
3273 Val := Enumeration_Pos (Ent);
3277 -- Come here with Val set to value to be returned, set cache
3288 function Expr_Value_E (N : Node_Id) return Entity_Id is
3289 Ent : constant Entity_Id := Entity (N);
3292 if Ekind (Ent) = E_Enumeration_Literal then
3295 pragma Assert (Ekind (Ent) = E_Constant);
3296 return Expr_Value_E (Constant_Value (Ent));
3304 function Expr_Value_R (N : Node_Id) return Ureal is
3305 Kind : constant Node_Kind := Nkind (N);
3310 if Kind = N_Real_Literal then
3313 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3315 pragma Assert (Ekind (Ent) = E_Constant);
3316 return Expr_Value_R (Constant_Value (Ent));
3318 elsif Kind = N_Integer_Literal then
3319 return UR_From_Uint (Expr_Value (N));
3321 -- Strange case of VAX literals, which are at this stage transformed
3322 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
3323 -- Exp_Vfpt for further details.
3325 elsif Vax_Float (Etype (N))
3326 and then Nkind (N) = N_Unchecked_Type_Conversion
3328 Expr := Expression (N);
3330 if Nkind (Expr) = N_Function_Call
3331 and then Present (Parameter_Associations (Expr))
3333 Expr := First (Parameter_Associations (Expr));
3335 if Nkind (Expr) = N_Real_Literal then
3336 return Realval (Expr);
3340 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3342 elsif Kind = N_Attribute_Reference
3343 and then Attribute_Name (N) = Name_Null_Parameter
3348 -- If we fall through, we have a node that cannot be interpreted
3349 -- as a compile time constant. That is definitely an error.
3351 raise Program_Error;
3358 function Expr_Value_S (N : Node_Id) return Node_Id is
3360 if Nkind (N) = N_String_Literal then
3363 pragma Assert (Ekind (Entity (N)) = E_Constant);
3364 return Expr_Value_S (Constant_Value (Entity (N)));
3368 --------------------------
3369 -- Flag_Non_Static_Expr --
3370 --------------------------
3372 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
3374 if Error_Posted (Expr) and then not All_Errors_Mode then
3377 Error_Msg_F (Msg, Expr);
3378 Why_Not_Static (Expr);
3380 end Flag_Non_Static_Expr;
3386 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
3387 Loc : constant Source_Ptr := Sloc (N);
3388 Typ : constant Entity_Id := Etype (N);
3391 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
3393 -- We now have the literal with the right value, both the actual type
3394 -- and the expected type of this literal are taken from the expression
3395 -- that was evaluated.
3398 Set_Is_Static_Expression (N, Static);
3407 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
3408 Loc : constant Source_Ptr := Sloc (N);
3409 Typ : Entity_Id := Etype (N);
3413 -- If we are folding a named number, retain the entity in the
3414 -- literal, for ASIS use.
3416 if Is_Entity_Name (N)
3417 and then Ekind (Entity (N)) = E_Named_Integer
3424 if Is_Private_Type (Typ) then
3425 Typ := Full_View (Typ);
3428 -- For a result of type integer, substitute an N_Integer_Literal node
3429 -- for the result of the compile time evaluation of the expression.
3430 -- For ASIS use, set a link to the original named number when not in
3431 -- a generic context.
3433 if Is_Integer_Type (Typ) then
3434 Rewrite (N, Make_Integer_Literal (Loc, Val));
3436 Set_Original_Entity (N, Ent);
3438 -- Otherwise we have an enumeration type, and we substitute either
3439 -- an N_Identifier or N_Character_Literal to represent the enumeration
3440 -- literal corresponding to the given value, which must always be in
3441 -- range, because appropriate tests have already been made for this.
3443 else pragma Assert (Is_Enumeration_Type (Typ));
3444 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
3447 -- We now have the literal with the right value, both the actual type
3448 -- and the expected type of this literal are taken from the expression
3449 -- that was evaluated.
3452 Set_Is_Static_Expression (N, Static);
3461 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
3462 Loc : constant Source_Ptr := Sloc (N);
3463 Typ : constant Entity_Id := Etype (N);
3467 -- If we are folding a named number, retain the entity in the
3468 -- literal, for ASIS use.
3470 if Is_Entity_Name (N)
3471 and then Ekind (Entity (N)) = E_Named_Real
3478 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
3480 -- Set link to original named number, for ASIS use
3482 Set_Original_Entity (N, Ent);
3484 -- Both the actual and expected type comes from the original expression
3487 Set_Is_Static_Expression (N, Static);
3496 function From_Bits (B : Bits; T : Entity_Id) return Uint is
3500 for J in 0 .. B'Last loop
3506 if Non_Binary_Modulus (T) then
3507 V := V mod Modulus (T);
3513 --------------------
3514 -- Get_String_Val --
3515 --------------------
3517 function Get_String_Val (N : Node_Id) return Node_Id is
3519 if Nkind (N) = N_String_Literal then
3522 elsif Nkind (N) = N_Character_Literal then
3526 pragma Assert (Is_Entity_Name (N));
3527 return Get_String_Val (Constant_Value (Entity (N)));
3535 procedure Initialize is
3537 CV_Cache := (others => (Node_High_Bound, Uint_0));
3540 --------------------
3541 -- In_Subrange_Of --
3542 --------------------
3544 function In_Subrange_Of
3547 Assume_Valid : Boolean;
3548 Fixed_Int : Boolean := False) return Boolean
3557 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
3560 -- Never in range if both types are not scalar. Don't know if this can
3561 -- actually happen, but just in case.
3563 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
3567 L1 := Type_Low_Bound (T1);
3568 H1 := Type_High_Bound (T1);
3570 L2 := Type_Low_Bound (T2);
3571 H2 := Type_High_Bound (T2);
3573 -- Check bounds to see if comparison possible at compile time
3575 if Compile_Time_Compare (L1, L2, Assume_Valid) in Compare_GE
3577 Compile_Time_Compare (H1, H2, Assume_Valid) in Compare_LE
3582 -- If bounds not comparable at compile time, then the bounds of T2
3583 -- must be compile time known or we cannot answer the query.
3585 if not Compile_Time_Known_Value (L2)
3586 or else not Compile_Time_Known_Value (H2)
3591 -- If the bounds of T1 are know at compile time then use these
3592 -- ones, otherwise use the bounds of the base type (which are of
3593 -- course always static).
3595 if not Compile_Time_Known_Value (L1) then
3596 L1 := Type_Low_Bound (Base_Type (T1));
3599 if not Compile_Time_Known_Value (H1) then
3600 H1 := Type_High_Bound (Base_Type (T1));
3603 -- Fixed point types should be considered as such only if
3604 -- flag Fixed_Int is set to False.
3606 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
3607 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
3608 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
3611 Expr_Value_R (L2) <= Expr_Value_R (L1)
3613 Expr_Value_R (H2) >= Expr_Value_R (H1);
3617 Expr_Value (L2) <= Expr_Value (L1)
3619 Expr_Value (H2) >= Expr_Value (H1);
3624 -- If any exception occurs, it means that we have some bug in the compiler
3625 -- possibly triggered by a previous error, or by some unforeseen peculiar
3626 -- occurrence. However, this is only an optimization attempt, so there is
3627 -- really no point in crashing the compiler. Instead we just decide, too
3628 -- bad, we can't figure out the answer in this case after all.
3633 -- Debug flag K disables this behavior (useful for debugging)
3635 if Debug_Flag_K then
3646 function Is_In_Range
3649 Assume_Valid : Boolean := False;
3650 Fixed_Int : Boolean := False;
3651 Int_Real : Boolean := False) return Boolean
3657 -- Universal types have no range limits, so always in range
3659 if Typ = Universal_Integer or else Typ = Universal_Real then
3662 -- Never in range if not scalar type. Don't know if this can
3663 -- actually happen, but our spec allows it, so we must check!
3665 elsif not Is_Scalar_Type (Typ) then
3668 -- Never in range unless we have a compile time known value
3670 elsif not Compile_Time_Known_Value (N) then
3673 -- General processing with a known compile time value
3685 or else Assume_No_Invalid_Values
3686 or else (Is_Entity_Name (N)
3687 and then Is_Known_Valid (Entity (N)))
3691 Typt := Underlying_Type (Base_Type (Typ));
3694 Lo := Type_Low_Bound (Typt);
3695 Hi := Type_High_Bound (Typt);
3697 LB_Known := Compile_Time_Known_Value (Lo);
3698 UB_Known := Compile_Time_Known_Value (Hi);
3700 -- Fixed point types should be considered as such only in
3701 -- flag Fixed_Int is set to False.
3703 if Is_Floating_Point_Type (Typt)
3704 or else (Is_Fixed_Point_Type (Typt) and then not Fixed_Int)
3707 Valr := Expr_Value_R (N);
3709 if LB_Known and then Valr >= Expr_Value_R (Lo)
3710 and then UB_Known and then Valr <= Expr_Value_R (Hi)
3718 Val := Expr_Value (N);
3720 if LB_Known and then Val >= Expr_Value (Lo)
3721 and then UB_Known and then Val <= Expr_Value (Hi)
3736 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
3737 Typ : constant Entity_Id := Etype (Lo);
3740 if not Compile_Time_Known_Value (Lo)
3741 or else not Compile_Time_Known_Value (Hi)
3746 if Is_Discrete_Type (Typ) then
3747 return Expr_Value (Lo) > Expr_Value (Hi);
3750 pragma Assert (Is_Real_Type (Typ));
3751 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
3755 -----------------------------
3756 -- Is_OK_Static_Expression --
3757 -----------------------------
3759 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
3761 return Is_Static_Expression (N)
3762 and then not Raises_Constraint_Error (N);
3763 end Is_OK_Static_Expression;
3765 ------------------------
3766 -- Is_OK_Static_Range --
3767 ------------------------
3769 -- A static range is a range whose bounds are static expressions, or a
3770 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3771 -- We have already converted range attribute references, so we get the
3772 -- "or" part of this rule without needing a special test.
3774 function Is_OK_Static_Range (N : Node_Id) return Boolean is
3776 return Is_OK_Static_Expression (Low_Bound (N))
3777 and then Is_OK_Static_Expression (High_Bound (N));
3778 end Is_OK_Static_Range;
3780 --------------------------
3781 -- Is_OK_Static_Subtype --
3782 --------------------------
3784 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3785 -- where neither bound raises constraint error when evaluated.
3787 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
3788 Base_T : constant Entity_Id := Base_Type (Typ);
3789 Anc_Subt : Entity_Id;
3792 -- First a quick check on the non static subtype flag. As described
3793 -- in further detail in Einfo, this flag is not decisive in all cases,
3794 -- but if it is set, then the subtype is definitely non-static.
3796 if Is_Non_Static_Subtype (Typ) then
3800 Anc_Subt := Ancestor_Subtype (Typ);
3802 if Anc_Subt = Empty then
3806 if Is_Generic_Type (Root_Type (Base_T))
3807 or else Is_Generic_Actual_Type (Base_T)
3813 elsif Is_String_Type (Typ) then
3815 Ekind (Typ) = E_String_Literal_Subtype
3817 (Is_OK_Static_Subtype (Component_Type (Typ))
3818 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
3822 elsif Is_Scalar_Type (Typ) then
3823 if Base_T = Typ then
3827 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so
3828 -- use Get_Type_Low,High_Bound.
3830 return Is_OK_Static_Subtype (Anc_Subt)
3831 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
3832 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
3835 -- Types other than string and scalar types are never static
3840 end Is_OK_Static_Subtype;
3842 ---------------------
3843 -- Is_Out_Of_Range --
3844 ---------------------
3846 function Is_Out_Of_Range
3849 Assume_Valid : Boolean := False;
3850 Fixed_Int : Boolean := False;
3851 Int_Real : Boolean := False) return Boolean
3857 -- Universal types have no range limits, so always in range
3859 if Typ = Universal_Integer or else Typ = Universal_Real then
3862 -- Never out of range if not scalar type. Don't know if this can
3863 -- actually happen, but our spec allows it, so we must check!
3865 elsif not Is_Scalar_Type (Typ) then
3868 -- Never out of range if this is a generic type, since the bounds
3869 -- of generic types are junk. Note that if we only checked for
3870 -- static expressions (instead of compile time known values) below,
3871 -- we would not need this check, because values of a generic type
3872 -- can never be static, but they can be known at compile time.
3874 elsif Is_Generic_Type (Typ) then
3877 -- Never out of range unless we have a compile time known value
3879 elsif not Compile_Time_Known_Value (N) then
3891 -- Go to base type if we could have invalid values
3894 or else Assume_No_Invalid_Values
3895 or else (Is_Entity_Name (N)
3896 and then Is_Known_Valid (Entity (N)))
3900 Typt := Underlying_Type (Base_Type (Typ));
3903 Lo := Type_Low_Bound (Typt);
3904 Hi := Type_High_Bound (Typt);
3906 LB_Known := Compile_Time_Known_Value (Lo);
3907 UB_Known := Compile_Time_Known_Value (Hi);
3909 -- Real types (note that fixed-point types are not treated
3910 -- as being of a real type if the flag Fixed_Int is set,
3911 -- since in that case they are regarded as integer types).
3913 if Is_Floating_Point_Type (Typt)
3914 or else (Is_Fixed_Point_Type (Typt) and then not Fixed_Int)
3917 Valr := Expr_Value_R (N);
3919 if LB_Known and then Valr < Expr_Value_R (Lo) then
3922 elsif UB_Known and then Expr_Value_R (Hi) < Valr then
3930 Val := Expr_Value (N);
3932 if LB_Known and then Val < Expr_Value (Lo) then
3935 elsif UB_Known and then Expr_Value (Hi) < Val then
3944 end Is_Out_Of_Range;
3946 ---------------------
3947 -- Is_Static_Range --
3948 ---------------------
3950 -- A static range is a range whose bounds are static expressions, or a
3951 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
3952 -- We have already converted range attribute references, so we get the
3953 -- "or" part of this rule without needing a special test.
3955 function Is_Static_Range (N : Node_Id) return Boolean is
3957 return Is_Static_Expression (Low_Bound (N))
3958 and then Is_Static_Expression (High_Bound (N));
3959 end Is_Static_Range;
3961 -----------------------
3962 -- Is_Static_Subtype --
3963 -----------------------
3965 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
3967 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
3968 Base_T : constant Entity_Id := Base_Type (Typ);
3969 Anc_Subt : Entity_Id;
3972 -- First a quick check on the non static subtype flag. As described
3973 -- in further detail in Einfo, this flag is not decisive in all cases,
3974 -- but if it is set, then the subtype is definitely non-static.
3976 if Is_Non_Static_Subtype (Typ) then
3980 Anc_Subt := Ancestor_Subtype (Typ);
3982 if Anc_Subt = Empty then
3986 if Is_Generic_Type (Root_Type (Base_T))
3987 or else Is_Generic_Actual_Type (Base_T)
3993 elsif Is_String_Type (Typ) then
3995 Ekind (Typ) = E_String_Literal_Subtype
3997 (Is_Static_Subtype (Component_Type (Typ))
3998 and then Is_Static_Subtype (Etype (First_Index (Typ))));
4002 elsif Is_Scalar_Type (Typ) then
4003 if Base_T = Typ then
4007 return Is_Static_Subtype (Anc_Subt)
4008 and then Is_Static_Expression (Type_Low_Bound (Typ))
4009 and then Is_Static_Expression (Type_High_Bound (Typ));
4012 -- Types other than string and scalar types are never static
4017 end Is_Static_Subtype;
4019 --------------------
4020 -- Not_Null_Range --
4021 --------------------
4023 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4024 Typ : constant Entity_Id := Etype (Lo);
4027 if not Compile_Time_Known_Value (Lo)
4028 or else not Compile_Time_Known_Value (Hi)
4033 if Is_Discrete_Type (Typ) then
4034 return Expr_Value (Lo) <= Expr_Value (Hi);
4037 pragma Assert (Is_Real_Type (Typ));
4039 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
4047 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
4049 -- We allow a maximum of 500,000 bits which seems a reasonable limit
4051 if Bits < 500_000 then
4055 Error_Msg_N ("static value too large, capacity exceeded", N);
4064 procedure Out_Of_Range (N : Node_Id) is
4066 -- If we have the static expression case, then this is an illegality
4067 -- in Ada 95 mode, except that in an instance, we never generate an
4068 -- error (if the error is legitimate, it was already diagnosed in
4069 -- the template). The expression to compute the length of a packed
4070 -- array is attached to the array type itself, and deserves a separate
4073 if Is_Static_Expression (N)
4074 and then not In_Instance
4075 and then not In_Inlined_Body
4076 and then Ada_Version >= Ada_95
4078 if Nkind (Parent (N)) = N_Defining_Identifier
4079 and then Is_Array_Type (Parent (N))
4080 and then Present (Packed_Array_Type (Parent (N)))
4081 and then Present (First_Rep_Item (Parent (N)))
4084 ("length of packed array must not exceed Integer''Last",
4085 First_Rep_Item (Parent (N)));
4086 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
4089 Apply_Compile_Time_Constraint_Error
4090 (N, "value not in range of}", CE_Range_Check_Failed);
4093 -- Here we generate a warning for the Ada 83 case, or when we are
4094 -- in an instance, or when we have a non-static expression case.
4097 Apply_Compile_Time_Constraint_Error
4098 (N, "value not in range of}?", CE_Range_Check_Failed);
4102 -------------------------
4103 -- Rewrite_In_Raise_CE --
4104 -------------------------
4106 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
4107 Typ : constant Entity_Id := Etype (N);
4110 -- If we want to raise CE in the condition of a raise_CE node
4111 -- we may as well get rid of the condition
4113 if Present (Parent (N))
4114 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
4116 Set_Condition (Parent (N), Empty);
4118 -- If the expression raising CE is a N_Raise_CE node, we can use
4119 -- that one. We just preserve the type of the context
4121 elsif Nkind (Exp) = N_Raise_Constraint_Error then
4125 -- We have to build an explicit raise_ce node
4129 Make_Raise_Constraint_Error (Sloc (Exp),
4130 Reason => CE_Range_Check_Failed));
4131 Set_Raises_Constraint_Error (N);
4134 end Rewrite_In_Raise_CE;
4136 ---------------------
4137 -- String_Type_Len --
4138 ---------------------
4140 function String_Type_Len (Stype : Entity_Id) return Uint is
4141 NT : constant Entity_Id := Etype (First_Index (Stype));
4145 if Is_OK_Static_Subtype (NT) then
4148 T := Base_Type (NT);
4151 return Expr_Value (Type_High_Bound (T)) -
4152 Expr_Value (Type_Low_Bound (T)) + 1;
4153 end String_Type_Len;
4155 ------------------------------------
4156 -- Subtypes_Statically_Compatible --
4157 ------------------------------------
4159 function Subtypes_Statically_Compatible
4161 T2 : Entity_Id) return Boolean
4164 if Is_Scalar_Type (T1) then
4166 -- Definitely compatible if we match
4168 if Subtypes_Statically_Match (T1, T2) then
4171 -- If either subtype is nonstatic then they're not compatible
4173 elsif not Is_Static_Subtype (T1)
4174 or else not Is_Static_Subtype (T2)
4178 -- If either type has constraint error bounds, then consider that
4179 -- they match to avoid junk cascaded errors here.
4181 elsif not Is_OK_Static_Subtype (T1)
4182 or else not Is_OK_Static_Subtype (T2)
4186 -- Base types must match, but we don't check that (should
4187 -- we???) but we do at least check that both types are
4188 -- real, or both types are not real.
4190 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
4193 -- Here we check the bounds
4197 LB1 : constant Node_Id := Type_Low_Bound (T1);
4198 HB1 : constant Node_Id := Type_High_Bound (T1);
4199 LB2 : constant Node_Id := Type_Low_Bound (T2);
4200 HB2 : constant Node_Id := Type_High_Bound (T2);
4203 if Is_Real_Type (T1) then
4205 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
4207 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
4209 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
4213 (Expr_Value (LB1) > Expr_Value (HB1))
4215 (Expr_Value (LB2) <= Expr_Value (LB1)
4217 Expr_Value (HB1) <= Expr_Value (HB2));
4222 elsif Is_Access_Type (T1) then
4223 return not Is_Constrained (T2)
4224 or else Subtypes_Statically_Match
4225 (Designated_Type (T1), Designated_Type (T2));
4228 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
4229 or else Subtypes_Statically_Match (T1, T2);
4231 end Subtypes_Statically_Compatible;
4233 -------------------------------
4234 -- Subtypes_Statically_Match --
4235 -------------------------------
4237 -- Subtypes statically match if they have statically matching constraints
4238 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4239 -- they are the same identical constraint, or if they are static and the
4240 -- values match (RM 4.9.1(1)).
4242 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
4244 -- A type always statically matches itself
4251 elsif Is_Scalar_Type (T1) then
4253 -- Base types must be the same
4255 if Base_Type (T1) /= Base_Type (T2) then
4259 -- A constrained numeric subtype never matches an unconstrained
4260 -- subtype, i.e. both types must be constrained or unconstrained.
4262 -- To understand the requirement for this test, see RM 4.9.1(1).
4263 -- As is made clear in RM 3.5.4(11), type Integer, for example
4264 -- is a constrained subtype with constraint bounds matching the
4265 -- bounds of its corresponding unconstrained base type. In this
4266 -- situation, Integer and Integer'Base do not statically match,
4267 -- even though they have the same bounds.
4269 -- We only apply this test to types in Standard and types that
4270 -- appear in user programs. That way, we do not have to be
4271 -- too careful about setting Is_Constrained right for itypes.
4273 if Is_Numeric_Type (T1)
4274 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4275 and then (Scope (T1) = Standard_Standard
4276 or else Comes_From_Source (T1))
4277 and then (Scope (T2) = Standard_Standard
4278 or else Comes_From_Source (T2))
4282 -- A generic scalar type does not statically match its base
4283 -- type (AI-311). In this case we make sure that the formals,
4284 -- which are first subtypes of their bases, are constrained.
4286 elsif Is_Generic_Type (T1)
4287 and then Is_Generic_Type (T2)
4288 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4293 -- If there was an error in either range, then just assume
4294 -- the types statically match to avoid further junk errors
4296 if Error_Posted (Scalar_Range (T1))
4298 Error_Posted (Scalar_Range (T2))
4303 -- Otherwise both types have bound that can be compared
4306 LB1 : constant Node_Id := Type_Low_Bound (T1);
4307 HB1 : constant Node_Id := Type_High_Bound (T1);
4308 LB2 : constant Node_Id := Type_Low_Bound (T2);
4309 HB2 : constant Node_Id := Type_High_Bound (T2);
4312 -- If the bounds are the same tree node, then match
4314 if LB1 = LB2 and then HB1 = HB2 then
4317 -- Otherwise bounds must be static and identical value
4320 if not Is_Static_Subtype (T1)
4321 or else not Is_Static_Subtype (T2)
4325 -- If either type has constraint error bounds, then say
4326 -- that they match to avoid junk cascaded errors here.
4328 elsif not Is_OK_Static_Subtype (T1)
4329 or else not Is_OK_Static_Subtype (T2)
4333 elsif Is_Real_Type (T1) then
4335 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4337 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4341 Expr_Value (LB1) = Expr_Value (LB2)
4343 Expr_Value (HB1) = Expr_Value (HB2);
4348 -- Type with discriminants
4350 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
4352 -- Because of view exchanges in multiple instantiations, conformance
4353 -- checking might try to match a partial view of a type with no
4354 -- discriminants with a full view that has defaulted discriminants.
4355 -- In such a case, use the discriminant constraint of the full view,
4356 -- which must exist because we know that the two subtypes have the
4359 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
4361 if Is_Private_Type (T2)
4362 and then Present (Full_View (T2))
4363 and then Has_Discriminants (Full_View (T2))
4365 return Subtypes_Statically_Match (T1, Full_View (T2));
4367 elsif Is_Private_Type (T1)
4368 and then Present (Full_View (T1))
4369 and then Has_Discriminants (Full_View (T1))
4371 return Subtypes_Statically_Match (Full_View (T1), T2);
4382 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
4383 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
4391 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
4395 -- Now loop through the discriminant constraints
4397 -- Note: the guard here seems necessary, since it is possible at
4398 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
4400 if Present (DL1) and then Present (DL2) then
4401 DA1 := First_Elmt (DL1);
4402 DA2 := First_Elmt (DL2);
4403 while Present (DA1) loop
4405 Expr1 : constant Node_Id := Node (DA1);
4406 Expr2 : constant Node_Id := Node (DA2);
4409 if not Is_Static_Expression (Expr1)
4410 or else not Is_Static_Expression (Expr2)
4414 -- If either expression raised a constraint error,
4415 -- consider the expressions as matching, since this
4416 -- helps to prevent cascading errors.
4418 elsif Raises_Constraint_Error (Expr1)
4419 or else Raises_Constraint_Error (Expr2)
4423 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
4436 -- A definite type does not match an indefinite or classwide type
4437 -- However, a generic type with unknown discriminants may be
4438 -- instantiated with a type with no discriminants, and conformance
4439 -- checking on an inherited operation may compare the actual with
4440 -- the subtype that renames it in the instance.
4443 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
4446 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
4450 elsif Is_Array_Type (T1) then
4452 -- If either subtype is unconstrained then both must be,
4453 -- and if both are unconstrained then no further checking
4456 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
4457 return not (Is_Constrained (T1) or else Is_Constrained (T2));
4460 -- Both subtypes are constrained, so check that the index
4461 -- subtypes statically match.
4464 Index1 : Node_Id := First_Index (T1);
4465 Index2 : Node_Id := First_Index (T2);
4468 while Present (Index1) loop
4470 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
4475 Next_Index (Index1);
4476 Next_Index (Index2);
4482 elsif Is_Access_Type (T1) then
4483 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
4486 elsif Ekind (T1) = E_Access_Subprogram_Type
4487 or else Ekind (T1) = E_Anonymous_Access_Subprogram_Type
4491 (Designated_Type (T1),
4492 Designated_Type (T2));
4495 Subtypes_Statically_Match
4496 (Designated_Type (T1),
4497 Designated_Type (T2))
4498 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
4501 -- All other types definitely match
4506 end Subtypes_Statically_Match;
4512 function Test (Cond : Boolean) return Uint is
4521 ---------------------------------
4522 -- Test_Expression_Is_Foldable --
4523 ---------------------------------
4527 procedure Test_Expression_Is_Foldable
4537 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4541 -- If operand is Any_Type, just propagate to result and do not
4542 -- try to fold, this prevents cascaded errors.
4544 if Etype (Op1) = Any_Type then
4545 Set_Etype (N, Any_Type);
4548 -- If operand raises constraint error, then replace node N with the
4549 -- raise constraint error node, and we are obviously not foldable.
4550 -- Note that this replacement inherits the Is_Static_Expression flag
4551 -- from the operand.
4553 elsif Raises_Constraint_Error (Op1) then
4554 Rewrite_In_Raise_CE (N, Op1);
4557 -- If the operand is not static, then the result is not static, and
4558 -- all we have to do is to check the operand since it is now known
4559 -- to appear in a non-static context.
4561 elsif not Is_Static_Expression (Op1) then
4562 Check_Non_Static_Context (Op1);
4563 Fold := Compile_Time_Known_Value (Op1);
4566 -- An expression of a formal modular type is not foldable because
4567 -- the modulus is unknown.
4569 elsif Is_Modular_Integer_Type (Etype (Op1))
4570 and then Is_Generic_Type (Etype (Op1))
4572 Check_Non_Static_Context (Op1);
4575 -- Here we have the case of an operand whose type is OK, which is
4576 -- static, and which does not raise constraint error, we can fold.
4579 Set_Is_Static_Expression (N);
4583 end Test_Expression_Is_Foldable;
4587 procedure Test_Expression_Is_Foldable
4594 Rstat : constant Boolean := Is_Static_Expression (Op1)
4595 and then Is_Static_Expression (Op2);
4601 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
4605 -- If either operand is Any_Type, just propagate to result and
4606 -- do not try to fold, this prevents cascaded errors.
4608 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
4609 Set_Etype (N, Any_Type);
4612 -- If left operand raises constraint error, then replace node N with
4613 -- the raise constraint error node, and we are obviously not foldable.
4614 -- Is_Static_Expression is set from the two operands in the normal way,
4615 -- and we check the right operand if it is in a non-static context.
4617 elsif Raises_Constraint_Error (Op1) then
4619 Check_Non_Static_Context (Op2);
4622 Rewrite_In_Raise_CE (N, Op1);
4623 Set_Is_Static_Expression (N, Rstat);
4626 -- Similar processing for the case of the right operand. Note that
4627 -- we don't use this routine for the short-circuit case, so we do
4628 -- not have to worry about that special case here.
4630 elsif Raises_Constraint_Error (Op2) then
4632 Check_Non_Static_Context (Op1);
4635 Rewrite_In_Raise_CE (N, Op2);
4636 Set_Is_Static_Expression (N, Rstat);
4639 -- Exclude expressions of a generic modular type, as above
4641 elsif Is_Modular_Integer_Type (Etype (Op1))
4642 and then Is_Generic_Type (Etype (Op1))
4644 Check_Non_Static_Context (Op1);
4647 -- If result is not static, then check non-static contexts on operands
4648 -- since one of them may be static and the other one may not be static
4650 elsif not Rstat then
4651 Check_Non_Static_Context (Op1);
4652 Check_Non_Static_Context (Op2);
4653 Fold := Compile_Time_Known_Value (Op1)
4654 and then Compile_Time_Known_Value (Op2);
4657 -- Else result is static and foldable. Both operands are static,
4658 -- and neither raises constraint error, so we can definitely fold.
4661 Set_Is_Static_Expression (N);
4666 end Test_Expression_Is_Foldable;
4672 procedure To_Bits (U : Uint; B : out Bits) is
4674 for J in 0 .. B'Last loop
4675 B (J) := (U / (2 ** J)) mod 2 /= 0;
4679 --------------------
4680 -- Why_Not_Static --
4681 --------------------
4683 procedure Why_Not_Static (Expr : Node_Id) is
4684 N : constant Node_Id := Original_Node (Expr);
4688 procedure Why_Not_Static_List (L : List_Id);
4689 -- A version that can be called on a list of expressions. Finds
4690 -- all non-static violations in any element of the list.
4692 -------------------------
4693 -- Why_Not_Static_List --
4694 -------------------------
4696 procedure Why_Not_Static_List (L : List_Id) is
4700 if Is_Non_Empty_List (L) then
4702 while Present (N) loop
4707 end Why_Not_Static_List;
4709 -- Start of processing for Why_Not_Static
4712 -- If in ACATS mode (debug flag 2), then suppress all these
4713 -- messages, this avoids massive updates to the ACATS base line.
4715 if Debug_Flag_2 then
4719 -- Ignore call on error or empty node
4721 if No (Expr) or else Nkind (Expr) = N_Error then
4725 -- Preprocessing for sub expressions
4727 if Nkind (Expr) in N_Subexpr then
4729 -- Nothing to do if expression is static
4731 if Is_OK_Static_Expression (Expr) then
4735 -- Test for constraint error raised
4737 if Raises_Constraint_Error (Expr) then
4739 ("expression raises exception, cannot be static " &
4740 "(RM 4.9(34))!", N);
4744 -- If no type, then something is pretty wrong, so ignore
4746 Typ := Etype (Expr);
4752 -- Type must be scalar or string type
4754 if not Is_Scalar_Type (Typ)
4755 and then not Is_String_Type (Typ)
4758 ("static expression must have scalar or string type " &
4764 -- If we got through those checks, test particular node kind
4767 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
4770 if Is_Named_Number (E) then
4773 elsif Ekind (E) = E_Constant then
4774 if not Is_Static_Expression (Constant_Value (E)) then
4776 ("& is not a static constant (RM 4.9(5))!", N, E);
4781 ("& is not static constant or named number " &
4782 "(RM 4.9(5))!", N, E);
4785 when N_Binary_Op | N_And_Then | N_Or_Else | N_Membership_Test =>
4786 if Nkind (N) in N_Op_Shift then
4788 ("shift functions are never static (RM 4.9(6,18))!", N);
4791 Why_Not_Static (Left_Opnd (N));
4792 Why_Not_Static (Right_Opnd (N));
4796 Why_Not_Static (Right_Opnd (N));
4798 when N_Attribute_Reference =>
4799 Why_Not_Static_List (Expressions (N));
4801 E := Etype (Prefix (N));
4803 if E = Standard_Void_Type then
4807 -- Special case non-scalar'Size since this is a common error
4809 if Attribute_Name (N) = Name_Size then
4811 ("size attribute is only static for scalar type " &
4812 "(RM 4.9(7,8))", N);
4816 elsif Is_Array_Type (E) then
4817 if Attribute_Name (N) /= Name_First
4819 Attribute_Name (N) /= Name_Last
4821 Attribute_Name (N) /= Name_Length
4824 ("static array attribute must be Length, First, or Last " &
4827 -- Since we know the expression is not-static (we already
4828 -- tested for this, must mean array is not static).
4832 ("prefix is non-static array (RM 4.9(8))!", Prefix (N));
4837 -- Special case generic types, since again this is a common
4838 -- source of confusion.
4840 elsif Is_Generic_Actual_Type (E)
4845 ("attribute of generic type is never static " &
4846 "(RM 4.9(7,8))!", N);
4848 elsif Is_Static_Subtype (E) then
4851 elsif Is_Scalar_Type (E) then
4853 ("prefix type for attribute is not static scalar subtype " &
4858 ("static attribute must apply to array/scalar type " &
4859 "(RM 4.9(7,8))!", N);
4862 when N_String_Literal =>
4864 ("subtype of string literal is non-static (RM 4.9(4))!", N);
4866 when N_Explicit_Dereference =>
4868 ("explicit dereference is never static (RM 4.9)!", N);
4870 when N_Function_Call =>
4871 Why_Not_Static_List (Parameter_Associations (N));
4872 Error_Msg_N ("non-static function call (RM 4.9(6,18))!", N);
4874 when N_Parameter_Association =>
4875 Why_Not_Static (Explicit_Actual_Parameter (N));
4877 when N_Indexed_Component =>
4879 ("indexed component is never static (RM 4.9)!", N);
4881 when N_Procedure_Call_Statement =>
4883 ("procedure call is never static (RM 4.9)!", N);
4885 when N_Qualified_Expression =>
4886 Why_Not_Static (Expression (N));
4888 when N_Aggregate | N_Extension_Aggregate =>
4890 ("an aggregate is never static (RM 4.9)!", N);
4893 Why_Not_Static (Low_Bound (N));
4894 Why_Not_Static (High_Bound (N));
4896 when N_Range_Constraint =>
4897 Why_Not_Static (Range_Expression (N));
4899 when N_Subtype_Indication =>
4900 Why_Not_Static (Constraint (N));
4902 when N_Selected_Component =>
4904 ("selected component is never static (RM 4.9)!", N);
4908 ("slice is never static (RM 4.9)!", N);
4910 when N_Type_Conversion =>
4911 Why_Not_Static (Expression (N));
4913 if not Is_Scalar_Type (Etype (Prefix (N)))
4914 or else not Is_Static_Subtype (Etype (Prefix (N)))
4917 ("static conversion requires static scalar subtype result " &
4921 when N_Unchecked_Type_Conversion =>
4923 ("unchecked type conversion is never static (RM 4.9)!", N);