1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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
;
34 with Freeze
; use Freeze
;
36 with Namet
; use Namet
;
37 with Nmake
; use Nmake
;
38 with Nlists
; use Nlists
;
41 with Sem_Aux
; use Sem_Aux
;
42 with Sem_Cat
; use Sem_Cat
;
43 with Sem_Ch6
; use Sem_Ch6
;
44 with Sem_Ch8
; use Sem_Ch8
;
45 with Sem_Res
; use Sem_Res
;
46 with Sem_Util
; use Sem_Util
;
47 with Sem_Type
; use Sem_Type
;
48 with Sem_Warn
; use Sem_Warn
;
49 with Sinfo
; use Sinfo
;
50 with Snames
; use Snames
;
51 with Stand
; use Stand
;
52 with Stringt
; use Stringt
;
53 with Tbuild
; use Tbuild
;
55 package body Sem_Eval
is
57 -----------------------------------------
58 -- Handling of Compile Time Evaluation --
59 -----------------------------------------
61 -- The compile time evaluation of expressions is distributed over several
62 -- Eval_xxx procedures. These procedures are called immediately after
63 -- a subexpression is resolved and is therefore accomplished in a bottom
64 -- up fashion. The flags are synthesized using the following approach.
66 -- Is_Static_Expression is determined by following the detailed rules
67 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
68 -- flag of the operands in many cases.
70 -- Raises_Constraint_Error is set if any of the operands have the flag
71 -- set or if an attempt to compute the value of the current expression
72 -- results in detection of a runtime constraint error.
74 -- As described in the spec, the requirement is that Is_Static_Expression
75 -- be accurately set, and in addition for nodes for which this flag is set,
76 -- Raises_Constraint_Error must also be set. Furthermore a node which has
77 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
78 -- requirement is that the expression value must be precomputed, and the
79 -- node is either a literal, or the name of a constant entity whose value
80 -- is a static expression.
82 -- The general approach is as follows. First compute Is_Static_Expression.
83 -- If the node is not static, then the flag is left off in the node and
84 -- we are all done. Otherwise for a static node, we test if any of the
85 -- operands will raise constraint error, and if so, propagate the flag
86 -- Raises_Constraint_Error to the result node and we are done (since the
87 -- error was already posted at a lower level).
89 -- For the case of a static node whose operands do not raise constraint
90 -- error, we attempt to evaluate the node. If this evaluation succeeds,
91 -- then the node is replaced by the result of this computation. If the
92 -- evaluation raises constraint error, then we rewrite the node with
93 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
94 -- to post appropriate error messages.
100 type Bits
is array (Nat
range <>) of Boolean;
101 -- Used to convert unsigned (modular) values for folding logical ops
103 -- The following definitions are used to maintain a cache of nodes that
104 -- have compile time known values. The cache is maintained only for
105 -- discrete types (the most common case), and is populated by calls to
106 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
107 -- since it is possible for the status to change (in particular it is
108 -- possible for a node to get replaced by a constraint error node).
110 CV_Bits
: constant := 5;
111 -- Number of low order bits of Node_Id value used to reference entries
112 -- in the cache table.
114 CV_Cache_Size
: constant Nat
:= 2 ** CV_Bits
;
115 -- Size of cache for compile time values
117 subtype CV_Range
is Nat
range 0 .. CV_Cache_Size
;
119 type CV_Entry
is record
124 type CV_Cache_Array
is array (CV_Range
) of CV_Entry
;
126 CV_Cache
: CV_Cache_Array
:= (others => (Node_High_Bound
, Uint_0
));
127 -- This is the actual cache, with entries consisting of node/value pairs,
128 -- and the impossible value Node_High_Bound used for unset entries.
130 type Range_Membership
is (In_Range
, Out_Of_Range
, Unknown
);
131 -- Range membership may either be statically known to be in range or out
132 -- of range, or not statically known. Used for Test_In_Range below.
134 -----------------------
135 -- Local Subprograms --
136 -----------------------
138 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
;
139 -- Converts a bit string of length B'Length to a Uint value to be used
140 -- for a target of type T, which is a modular type. This procedure
141 -- includes the necessary reduction by the modulus in the case of a
142 -- non-binary modulus (for a binary modulus, the bit string is the
143 -- right length any way so all is well).
145 function Get_String_Val
(N
: Node_Id
) return Node_Id
;
146 -- Given a tree node for a folded string or character value, returns
147 -- the corresponding string literal or character literal (one of the
148 -- two must be available, or the operand would not have been marked
149 -- as foldable in the earlier analysis of the operation).
151 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean;
152 -- Bits represents the number of bits in an integer value to be computed
153 -- (but the value has not been computed yet). If this value in Bits is
154 -- reasonable, a result of True is returned, with the implication that
155 -- the caller should go ahead and complete the calculation. If the value
156 -- in Bits is unreasonably large, then an error is posted on node N, and
157 -- False is returned (and the caller skips the proposed calculation).
159 procedure Out_Of_Range
(N
: Node_Id
);
160 -- This procedure is called if it is determined that node N, which
161 -- appears in a non-static context, is a compile time known value
162 -- which is outside its range, i.e. the range of Etype. This is used
163 -- in contexts where this is an illegality if N is static, and should
164 -- generate a warning otherwise.
166 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
);
167 -- N and Exp are nodes representing an expression, Exp is known
168 -- to raise CE. N is rewritten in term of Exp in the optimal way.
170 function String_Type_Len
(Stype
: Entity_Id
) return Uint
;
171 -- Given a string type, determines the length of the index type, or,
172 -- if this index type is non-static, the length of the base type of
173 -- this index type. Note that if the string type is itself static,
174 -- then the index type is static, so the second case applies only
175 -- if the string type passed is non-static.
177 function Test
(Cond
: Boolean) return Uint
;
178 pragma Inline
(Test
);
179 -- This function simply returns the appropriate Boolean'Pos value
180 -- corresponding to the value of Cond as a universal integer. It is
181 -- used for producing the result of the static evaluation of the
184 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
;
185 -- Check whether an arithmetic operation with universal operands which
186 -- is a rewritten function call with an explicit scope indication is
187 -- ambiguous: P."+" (1, 2) will be ambiguous if there is more than one
188 -- visible numeric type declared in P and the context does not impose a
189 -- type on the result (e.g. in the expression of a type conversion).
190 -- If ambiguous, emit an error and return Empty, else return the result
191 -- type of the operator.
193 procedure Test_Expression_Is_Foldable
198 -- Tests to see if expression N whose single operand is Op1 is foldable,
199 -- i.e. the operand value is known at compile time. If the operation is
200 -- foldable, then Fold is True on return, and Stat indicates whether
201 -- the result is static (i.e. both operands were static). Note that it
202 -- is quite possible for Fold to be True, and Stat to be False, since
203 -- there are cases in which we know the value of an operand even though
204 -- it is not technically static (e.g. the static lower bound of a range
205 -- whose upper bound is non-static).
207 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes a
208 -- call to Check_Non_Static_Context on the operand. If Fold is False on
209 -- return, then all processing is complete, and the caller should
210 -- return, since there is nothing else to do.
212 -- If Stat is set True on return, then Is_Static_Expression is also set
213 -- true in node N. There are some cases where this is over-enthusiastic,
214 -- e.g. in the two operand case below, for string comaprison, the result
215 -- is not static even though the two operands are static. In such cases,
216 -- the caller must reset the Is_Static_Expression flag in N.
218 procedure Test_Expression_Is_Foldable
224 -- Same processing, except applies to an expression N with two operands
227 function Test_In_Range
230 Assume_Valid
: Boolean;
232 Int_Real
: Boolean) return Range_Membership
;
233 -- Common processing for Is_In_Range and Is_Out_Of_Range:
234 -- Returns In_Range or Out_Of_Range if it can be guaranteed at compile time
235 -- that expression N is known to be in or out of range of the subtype Typ.
236 -- If not compile time known, Unknown is returned.
237 -- See documentation of Is_In_Range for complete description of parameters.
239 procedure To_Bits
(U
: Uint
; B
: out Bits
);
240 -- Converts a Uint value to a bit string of length B'Length
242 ------------------------------
243 -- Check_Non_Static_Context --
244 ------------------------------
246 procedure Check_Non_Static_Context
(N
: Node_Id
) is
247 T
: constant Entity_Id
:= Etype
(N
);
248 Checks_On
: constant Boolean :=
249 not Index_Checks_Suppressed
(T
)
250 and not Range_Checks_Suppressed
(T
);
253 -- Ignore cases of non-scalar types or error types
255 if T
= Any_Type
or else not Is_Scalar_Type
(T
) then
259 -- At this stage we have a scalar type. If we have an expression
260 -- that raises CE, then we already issued a warning or error msg
261 -- so there is nothing more to be done in this routine.
263 if Raises_Constraint_Error
(N
) then
267 -- Now we have a scalar type which is not marked as raising a
268 -- constraint error exception. The main purpose of this routine
269 -- is to deal with static expressions appearing in a non-static
270 -- context. That means that if we do not have a static expression
271 -- then there is not much to do. The one case that we deal with
272 -- here is that if we have a floating-point value that is out of
273 -- range, then we post a warning that an infinity will result.
275 if not Is_Static_Expression
(N
) then
276 if Is_Floating_Point_Type
(T
)
277 and then Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True)
280 ("?float value out of range, infinity will be generated", N
);
286 -- Here we have the case of outer level static expression of
287 -- scalar type, where the processing of this procedure is needed.
289 -- For real types, this is where we convert the value to a machine
290 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should
291 -- only need to do this if the parent is a constant declaration,
292 -- since in other cases, gigi should do the necessary conversion
293 -- correctly, but experimentation shows that this is not the case
294 -- on all machines, in particular if we do not convert all literals
295 -- to machine values in non-static contexts, then ACVC test C490001
296 -- fails on Sparc/Solaris and SGI/Irix.
298 if Nkind
(N
) = N_Real_Literal
299 and then not Is_Machine_Number
(N
)
300 and then not Is_Generic_Type
(Etype
(N
))
301 and then Etype
(N
) /= Universal_Real
303 -- Check that value is in bounds before converting to machine
304 -- number, so as not to lose case where value overflows in the
305 -- least significant bit or less. See B490001.
307 if Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
312 -- Note: we have to copy the node, to avoid problems with conformance
313 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
315 Rewrite
(N
, New_Copy
(N
));
317 if not Is_Floating_Point_Type
(T
) then
319 (N
, Corresponding_Integer_Value
(N
) * Small_Value
(T
));
321 elsif not UR_Is_Zero
(Realval
(N
)) then
323 -- Note: even though RM 4.9(38) specifies biased rounding,
324 -- this has been modified by AI-100 in order to prevent
325 -- confusing differences in rounding between static and
326 -- non-static expressions. AI-100 specifies that the effect
327 -- of such rounding is implementation dependent, and in GNAT
328 -- we round to nearest even to match the run-time behavior.
331 (N
, Machine
(Base_Type
(T
), Realval
(N
), Round_Even
, N
));
334 Set_Is_Machine_Number
(N
);
337 -- Check for out of range universal integer. This is a non-static
338 -- context, so the integer value must be in range of the runtime
339 -- representation of universal integers.
341 -- We do this only within an expression, because that is the only
342 -- case in which non-static universal integer values can occur, and
343 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
344 -- called in contexts like the expression of a number declaration where
345 -- we certainly want to allow out of range values.
347 if Etype
(N
) = Universal_Integer
348 and then Nkind
(N
) = N_Integer_Literal
349 and then Nkind
(Parent
(N
)) in N_Subexpr
351 (Intval
(N
) < Expr_Value
(Type_Low_Bound
(Universal_Integer
))
353 Intval
(N
) > Expr_Value
(Type_High_Bound
(Universal_Integer
)))
355 Apply_Compile_Time_Constraint_Error
356 (N
, "non-static universal integer value out of range?",
357 CE_Range_Check_Failed
);
359 -- Check out of range of base type
361 elsif Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
364 -- Give warning if outside subtype (where one or both of the bounds of
365 -- the subtype is static). This warning is omitted if the expression
366 -- appears in a range that could be null (warnings are handled elsewhere
369 elsif T
/= Base_Type
(T
)
370 and then Nkind
(Parent
(N
)) /= N_Range
372 if Is_In_Range
(N
, T
, Assume_Valid
=> True) then
375 elsif Is_Out_Of_Range
(N
, T
, Assume_Valid
=> True) then
376 Apply_Compile_Time_Constraint_Error
377 (N
, "value not in range of}?", CE_Range_Check_Failed
);
380 Enable_Range_Check
(N
);
383 Set_Do_Range_Check
(N
, False);
386 end Check_Non_Static_Context
;
388 ---------------------------------
389 -- Check_String_Literal_Length --
390 ---------------------------------
392 procedure Check_String_Literal_Length
(N
: Node_Id
; Ttype
: Entity_Id
) is
394 if not Raises_Constraint_Error
(N
)
395 and then Is_Constrained
(Ttype
)
398 UI_From_Int
(String_Length
(Strval
(N
))) /= String_Type_Len
(Ttype
)
400 Apply_Compile_Time_Constraint_Error
401 (N
, "string length wrong for}?",
402 CE_Length_Check_Failed
,
407 end Check_String_Literal_Length
;
409 --------------------------
410 -- Compile_Time_Compare --
411 --------------------------
413 function Compile_Time_Compare
415 Assume_Valid
: Boolean) return Compare_Result
417 Discard
: aliased Uint
;
419 return Compile_Time_Compare
(L
, R
, Discard
'Access, Assume_Valid
);
420 end Compile_Time_Compare
;
422 function Compile_Time_Compare
425 Assume_Valid
: Boolean;
426 Rec
: Boolean := False) return Compare_Result
428 Ltyp
: Entity_Id
:= Underlying_Type
(Etype
(L
));
429 Rtyp
: Entity_Id
:= Underlying_Type
(Etype
(R
));
430 -- These get reset to the base type for the case of entities where
431 -- Is_Known_Valid is not set. This takes care of handling possible
432 -- invalid representations using the value of the base type, in
433 -- accordance with RM 13.9.1(10).
435 Discard
: aliased Uint
;
437 procedure Compare_Decompose
441 -- This procedure decomposes the node N into an expression node and a
442 -- signed offset, so that the value of N is equal to the value of R plus
443 -- the value V (which may be negative). If no such decomposition is
444 -- possible, then on return R is a copy of N, and V is set to zero.
446 function Compare_Fixup
(N
: Node_Id
) return Node_Id
;
447 -- This function deals with replacing 'Last and 'First references with
448 -- their corresponding type bounds, which we then can compare. The
449 -- argument is the original node, the result is the identity, unless we
450 -- have a 'Last/'First reference in which case the value returned is the
451 -- appropriate type bound.
453 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean;
454 -- Even if the context does not assume that values are valid, some
455 -- simple cases can be recognized.
457 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean;
458 -- Returns True iff L and R represent expressions that definitely
459 -- have identical (but not necessarily compile time known) values
460 -- Indeed the caller is expected to have already dealt with the
461 -- cases of compile time known values, so these are not tested here.
463 -----------------------
464 -- Compare_Decompose --
465 -----------------------
467 procedure Compare_Decompose
473 if Nkind
(N
) = N_Op_Add
474 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
477 V
:= Intval
(Right_Opnd
(N
));
480 elsif Nkind
(N
) = N_Op_Subtract
481 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
484 V
:= UI_Negate
(Intval
(Right_Opnd
(N
)));
487 elsif Nkind
(N
) = N_Attribute_Reference
then
488 if Attribute_Name
(N
) = Name_Succ
then
489 R
:= First
(Expressions
(N
));
493 elsif Attribute_Name
(N
) = Name_Pred
then
494 R
:= First
(Expressions
(N
));
502 end Compare_Decompose
;
508 function Compare_Fixup
(N
: Node_Id
) return Node_Id
is
514 if Nkind
(N
) = N_Attribute_Reference
515 and then (Attribute_Name
(N
) = Name_First
517 Attribute_Name
(N
) = Name_Last
)
519 Xtyp
:= Etype
(Prefix
(N
));
521 -- If we have no type, then just abandon the attempt to do
522 -- a fixup, this is probably the result of some other error.
528 -- Dereference an access type
530 if Is_Access_Type
(Xtyp
) then
531 Xtyp
:= Designated_Type
(Xtyp
);
534 -- If we don't have an array type at this stage, something
535 -- is peculiar, e.g. another error, and we abandon the attempt
538 if not Is_Array_Type
(Xtyp
) then
542 -- Ignore unconstrained array, since bounds are not meaningful
544 if not Is_Constrained
(Xtyp
) then
548 if Ekind
(Xtyp
) = E_String_Literal_Subtype
then
549 if Attribute_Name
(N
) = Name_First
then
550 return String_Literal_Low_Bound
(Xtyp
);
552 else -- Attribute_Name (N) = Name_Last
553 return Make_Integer_Literal
(Sloc
(N
),
554 Intval
=> Intval
(String_Literal_Low_Bound
(Xtyp
))
555 + String_Literal_Length
(Xtyp
));
559 -- Find correct index type
561 Indx
:= First_Index
(Xtyp
);
563 if Present
(Expressions
(N
)) then
564 Subs
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
566 for J
in 2 .. Subs
loop
567 Indx
:= Next_Index
(Indx
);
571 Xtyp
:= Etype
(Indx
);
573 if Attribute_Name
(N
) = Name_First
then
574 return Type_Low_Bound
(Xtyp
);
576 else -- Attribute_Name (N) = Name_Last
577 return Type_High_Bound
(Xtyp
);
584 ----------------------------
585 -- Is_Known_Valid_Operand --
586 ----------------------------
588 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean is
590 return (Is_Entity_Name
(Opnd
)
592 (Is_Known_Valid
(Entity
(Opnd
))
593 or else Ekind
(Entity
(Opnd
)) = E_In_Parameter
595 (Ekind
(Entity
(Opnd
)) in Object_Kind
596 and then Present
(Current_Value
(Entity
(Opnd
))))))
597 or else Is_OK_Static_Expression
(Opnd
);
598 end Is_Known_Valid_Operand
;
604 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean is
605 Lf
: constant Node_Id
:= Compare_Fixup
(L
);
606 Rf
: constant Node_Id
:= Compare_Fixup
(R
);
608 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean;
609 -- L, R are the Expressions values from two attribute nodes for First
610 -- or Last attributes. Either may be set to No_List if no expressions
611 -- are present (indicating subscript 1). The result is True if both
612 -- expressions represent the same subscript (note one case is where
613 -- one subscript is missing and the other is explicitly set to 1).
615 -----------------------
616 -- Is_Same_Subscript --
617 -----------------------
619 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean is
625 return Expr_Value
(First
(R
)) = Uint_1
;
630 return Expr_Value
(First
(L
)) = Uint_1
;
632 return Expr_Value
(First
(L
)) = Expr_Value
(First
(R
));
635 end Is_Same_Subscript
;
637 -- Start of processing for Is_Same_Value
640 -- Values are the same if they refer to the same entity and the
641 -- entity is non-volatile. This does not however apply to Float
642 -- types, since we may have two NaN values and they should never
645 -- If the entity is a discriminant, the two expressions may be bounds
646 -- of components of objects of the same discriminated type. The
647 -- values of the discriminants are not static, and therefore the
648 -- result is unknown.
650 -- It would be better to comment individual branches of this test ???
652 if Nkind_In
(Lf
, N_Identifier
, N_Expanded_Name
)
653 and then Nkind_In
(Rf
, N_Identifier
, N_Expanded_Name
)
654 and then Entity
(Lf
) = Entity
(Rf
)
655 and then Ekind
(Entity
(Lf
)) /= E_Discriminant
656 and then Present
(Entity
(Lf
))
657 and then not Is_Floating_Point_Type
(Etype
(L
))
658 and then not Is_Volatile_Reference
(L
)
659 and then not Is_Volatile_Reference
(R
)
663 -- Or if they are compile time known and identical
665 elsif Compile_Time_Known_Value
(Lf
)
667 Compile_Time_Known_Value
(Rf
)
668 and then Expr_Value
(Lf
) = Expr_Value
(Rf
)
672 -- False if Nkind of the two nodes is different for remaining cases
674 elsif Nkind
(Lf
) /= Nkind
(Rf
) then
677 -- True if both 'First or 'Last values applying to the same entity
678 -- (first and last don't change even if value does). Note that we
679 -- need this even with the calls to Compare_Fixup, to handle the
680 -- case of unconstrained array attributes where Compare_Fixup
681 -- cannot find useful bounds.
683 elsif Nkind
(Lf
) = N_Attribute_Reference
684 and then Attribute_Name
(Lf
) = Attribute_Name
(Rf
)
685 and then (Attribute_Name
(Lf
) = Name_First
687 Attribute_Name
(Lf
) = Name_Last
)
688 and then Nkind_In
(Prefix
(Lf
), N_Identifier
, N_Expanded_Name
)
689 and then Nkind_In
(Prefix
(Rf
), N_Identifier
, N_Expanded_Name
)
690 and then Entity
(Prefix
(Lf
)) = Entity
(Prefix
(Rf
))
691 and then Is_Same_Subscript
(Expressions
(Lf
), Expressions
(Rf
))
695 -- True if the same selected component from the same record
697 elsif Nkind
(Lf
) = N_Selected_Component
698 and then Selector_Name
(Lf
) = Selector_Name
(Rf
)
699 and then Is_Same_Value
(Prefix
(Lf
), Prefix
(Rf
))
703 -- True if the same unary operator applied to the same operand
705 elsif Nkind
(Lf
) in N_Unary_Op
706 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
710 -- True if the same binary operator applied to the same operands
712 elsif Nkind
(Lf
) in N_Binary_Op
713 and then Is_Same_Value
(Left_Opnd
(Lf
), Left_Opnd
(Rf
))
714 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
718 -- All other cases, we can't tell, so return False
725 -- Start of processing for Compile_Time_Compare
730 -- If either operand could raise constraint error, then we cannot
731 -- know the result at compile time (since CE may be raised!)
733 if not (Cannot_Raise_Constraint_Error
(L
)
735 Cannot_Raise_Constraint_Error
(R
))
740 -- Identical operands are most certainly equal
745 -- If expressions have no types, then do not attempt to determine if
746 -- they are the same, since something funny is going on. One case in
747 -- which this happens is during generic template analysis, when bounds
748 -- are not fully analyzed.
750 elsif No
(Ltyp
) or else No
(Rtyp
) then
753 -- We do not attempt comparisons for packed arrays arrays represented as
754 -- modular types, where the semantics of comparison is quite different.
756 elsif Is_Packed_Array_Type
(Ltyp
)
757 and then Is_Modular_Integer_Type
(Ltyp
)
761 -- For access types, the only time we know the result at compile time
762 -- (apart from identical operands, which we handled already) is if we
763 -- know one operand is null and the other is not, or both operands are
766 elsif Is_Access_Type
(Ltyp
) then
767 if Known_Null
(L
) then
768 if Known_Null
(R
) then
770 elsif Known_Non_Null
(R
) then
776 elsif Known_Non_Null
(L
) and then Known_Null
(R
) then
783 -- Case where comparison involves two compile time known values
785 elsif Compile_Time_Known_Value
(L
)
786 and then Compile_Time_Known_Value
(R
)
788 -- For the floating-point case, we have to be a little careful, since
789 -- at compile time we are dealing with universal exact values, but at
790 -- runtime, these will be in non-exact target form. That's why the
791 -- returned results are LE and GE below instead of LT and GT.
793 if Is_Floating_Point_Type
(Ltyp
)
795 Is_Floating_Point_Type
(Rtyp
)
798 Lo
: constant Ureal
:= Expr_Value_R
(L
);
799 Hi
: constant Ureal
:= Expr_Value_R
(R
);
811 -- For string types, we have two string literals and we proceed to
812 -- compare them using the Ada style dictionary string comparison.
814 elsif not Is_Scalar_Type
(Ltyp
) then
816 Lstring
: constant String_Id
:= Strval
(Expr_Value_S
(L
));
817 Rstring
: constant String_Id
:= Strval
(Expr_Value_S
(R
));
818 Llen
: constant Nat
:= String_Length
(Lstring
);
819 Rlen
: constant Nat
:= String_Length
(Rstring
);
822 for J
in 1 .. Nat
'Min (Llen
, Rlen
) loop
824 LC
: constant Char_Code
:= Get_String_Char
(Lstring
, J
);
825 RC
: constant Char_Code
:= Get_String_Char
(Rstring
, J
);
837 elsif Llen
> Rlen
then
844 -- For remaining scalar cases we know exactly (note that this does
845 -- include the fixed-point case, where we know the run time integer
850 Lo
: constant Uint
:= Expr_Value
(L
);
851 Hi
: constant Uint
:= Expr_Value
(R
);
868 -- Cases where at least one operand is not known at compile time
871 -- Remaining checks apply only for discrete types
873 if not Is_Discrete_Type
(Ltyp
)
874 or else not Is_Discrete_Type
(Rtyp
)
879 -- Defend against generic types, or actually any expressions that
880 -- contain a reference to a generic type from within a generic
881 -- template. We don't want to do any range analysis of such
882 -- expressions for two reasons. First, the bounds of a generic type
883 -- itself are junk and cannot be used for any kind of analysis.
884 -- Second, we may have a case where the range at run time is indeed
885 -- known, but we don't want to do compile time analysis in the
886 -- template based on that range since in an instance the value may be
887 -- static, and able to be elaborated without reference to the bounds
888 -- of types involved. As an example, consider:
890 -- (F'Pos (F'Last) + 1) > Integer'Last
892 -- The expression on the left side of > is Universal_Integer and thus
893 -- acquires the type Integer for evaluation at run time, and at run
894 -- time it is true that this condition is always False, but within
895 -- an instance F may be a type with a static range greater than the
896 -- range of Integer, and the expression statically evaluates to True.
898 if References_Generic_Formal_Type
(L
)
900 References_Generic_Formal_Type
(R
)
905 -- Replace types by base types for the case of entities which are
906 -- not known to have valid representations. This takes care of
907 -- properly dealing with invalid representations.
909 if not Assume_Valid
and then not Assume_No_Invalid_Values
then
910 if Is_Entity_Name
(L
) and then not Is_Known_Valid
(Entity
(L
)) then
911 Ltyp
:= Underlying_Type
(Base_Type
(Ltyp
));
914 if Is_Entity_Name
(R
) and then not Is_Known_Valid
(Entity
(R
)) then
915 Rtyp
:= Underlying_Type
(Base_Type
(Rtyp
));
919 -- Try range analysis on variables and see if ranges are disjoint
927 Determine_Range
(L
, LOK
, LLo
, LHi
, Assume_Valid
);
928 Determine_Range
(R
, ROK
, RLo
, RHi
, Assume_Valid
);
942 -- If the range includes a single literal and we can assume
943 -- validity then the result is known even if an operand is
958 elsif not Is_Known_Valid_Operand
(L
)
959 and then not Assume_Valid
961 if Is_Same_Value
(L
, R
) then
970 -- Here is where we check for comparisons against maximum bounds of
971 -- types, where we know that no value can be outside the bounds of
972 -- the subtype. Note that this routine is allowed to assume that all
973 -- expressions are within their subtype bounds. Callers wishing to
974 -- deal with possibly invalid values must in any case take special
975 -- steps (e.g. conversions to larger types) to avoid this kind of
976 -- optimization, which is always considered to be valid. We do not
977 -- attempt this optimization with generic types, since the type
978 -- bounds may not be meaningful in this case.
980 -- We are in danger of an infinite recursion here. It does not seem
981 -- useful to go more than one level deep, so the parameter Rec is
982 -- used to protect ourselves against this infinite recursion.
986 -- See if we can get a decisive check against one operand and
987 -- a bound of the other operand (four possible tests here).
988 -- Note that we avoid testing junk bounds of a generic type.
990 if not Is_Generic_Type
(Rtyp
) then
991 case Compile_Time_Compare
(L
, Type_Low_Bound
(Rtyp
),
993 Assume_Valid
, Rec
=> True)
995 when LT
=> return LT
;
996 when LE
=> return LE
;
997 when EQ
=> return LE
;
1001 case Compile_Time_Compare
(L
, Type_High_Bound
(Rtyp
),
1003 Assume_Valid
, Rec
=> True)
1005 when GT
=> return GT
;
1006 when GE
=> return GE
;
1007 when EQ
=> return GE
;
1008 when others => null;
1012 if not Is_Generic_Type
(Ltyp
) then
1013 case Compile_Time_Compare
(Type_Low_Bound
(Ltyp
), R
,
1015 Assume_Valid
, Rec
=> True)
1017 when GT
=> return GT
;
1018 when GE
=> return GE
;
1019 when EQ
=> return GE
;
1020 when others => null;
1023 case Compile_Time_Compare
(Type_High_Bound
(Ltyp
), R
,
1025 Assume_Valid
, Rec
=> True)
1027 when LT
=> return LT
;
1028 when LE
=> return LE
;
1029 when EQ
=> return LE
;
1030 when others => null;
1035 -- Next attempt is to decompose the expressions to extract
1036 -- a constant offset resulting from the use of any of the forms:
1043 -- Then we see if the two expressions are the same value, and if so
1044 -- the result is obtained by comparing the offsets.
1053 Compare_Decompose
(L
, Lnode
, Loffs
);
1054 Compare_Decompose
(R
, Rnode
, Roffs
);
1056 if Is_Same_Value
(Lnode
, Rnode
) then
1057 if Loffs
= Roffs
then
1060 elsif Loffs
< Roffs
then
1061 Diff
.all := Roffs
- Loffs
;
1065 Diff
.all := Loffs
- Roffs
;
1071 -- Next attempt is to see if we have an entity compared with a
1072 -- compile time known value, where there is a current value
1073 -- conditional for the entity which can tell us the result.
1077 -- Entity variable (left operand)
1080 -- Value (right operand)
1083 -- If False, we have reversed the operands
1086 -- Comparison operator kind from Get_Current_Value_Condition call
1089 -- Value from Get_Current_Value_Condition call
1094 Result
: Compare_Result
;
1095 -- Known result before inversion
1098 if Is_Entity_Name
(L
)
1099 and then Compile_Time_Known_Value
(R
)
1102 Val
:= Expr_Value
(R
);
1105 elsif Is_Entity_Name
(R
)
1106 and then Compile_Time_Known_Value
(L
)
1109 Val
:= Expr_Value
(L
);
1112 -- That was the last chance at finding a compile time result
1118 Get_Current_Value_Condition
(Var
, Op
, Opn
);
1120 -- That was the last chance, so if we got nothing return
1126 Opv
:= Expr_Value
(Opn
);
1128 -- We got a comparison, so we might have something interesting
1130 -- Convert LE to LT and GE to GT, just so we have fewer cases
1132 if Op
= N_Op_Le
then
1136 elsif Op
= N_Op_Ge
then
1141 -- Deal with equality case
1143 if Op
= N_Op_Eq
then
1146 elsif Opv
< Val
then
1152 -- Deal with inequality case
1154 elsif Op
= N_Op_Ne
then
1161 -- Deal with greater than case
1163 elsif Op
= N_Op_Gt
then
1166 elsif Opv
= Val
- 1 then
1172 -- Deal with less than case
1174 else pragma Assert
(Op
= N_Op_Lt
);
1177 elsif Opv
= Val
+ 1 then
1184 -- Deal with inverting result
1188 when GT
=> return LT
;
1189 when GE
=> return LE
;
1190 when LT
=> return GT
;
1191 when LE
=> return GE
;
1192 when others => return Result
;
1199 end Compile_Time_Compare
;
1201 -------------------------------
1202 -- Compile_Time_Known_Bounds --
1203 -------------------------------
1205 function Compile_Time_Known_Bounds
(T
: Entity_Id
) return Boolean is
1210 if not Is_Array_Type
(T
) then
1214 Indx
:= First_Index
(T
);
1215 while Present
(Indx
) loop
1216 Typ
:= Underlying_Type
(Etype
(Indx
));
1218 -- Never look at junk bounds of a generic type
1220 if Is_Generic_Type
(Typ
) then
1224 -- Otherwise check bounds for compile time known
1226 if not Compile_Time_Known_Value
(Type_Low_Bound
(Typ
)) then
1228 elsif not Compile_Time_Known_Value
(Type_High_Bound
(Typ
)) then
1236 end Compile_Time_Known_Bounds
;
1238 ------------------------------
1239 -- Compile_Time_Known_Value --
1240 ------------------------------
1242 function Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1243 K
: constant Node_Kind
:= Nkind
(Op
);
1244 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(Op
) mod CV_Cache_Size
);
1247 -- Never known at compile time if bad type or raises constraint error
1248 -- or empty (latter case occurs only as a result of a previous error)
1252 or else Etype
(Op
) = Any_Type
1253 or else Raises_Constraint_Error
(Op
)
1258 -- If this is not a static expression or a null literal, and we are in
1259 -- configurable run-time mode, then we consider it not known at compile
1260 -- time. This avoids anomalies where whether something is allowed with a
1261 -- given configurable run-time library depends on how good the compiler
1262 -- is at optimizing and knowing that things are constant when they are
1265 if Configurable_Run_Time_Mode
1266 and then K
/= N_Null
1267 and then not Is_Static_Expression
(Op
)
1272 -- If we have an entity name, then see if it is the name of a constant
1273 -- and if so, test the corresponding constant value, or the name of
1274 -- an enumeration literal, which is always a constant.
1276 if Present
(Etype
(Op
)) and then Is_Entity_Name
(Op
) then
1278 E
: constant Entity_Id
:= Entity
(Op
);
1282 -- Never known at compile time if it is a packed array value.
1283 -- We might want to try to evaluate these at compile time one
1284 -- day, but we do not make that attempt now.
1286 if Is_Packed_Array_Type
(Etype
(Op
)) then
1290 if Ekind
(E
) = E_Enumeration_Literal
then
1293 elsif Ekind
(E
) = E_Constant
then
1294 V
:= Constant_Value
(E
);
1295 return Present
(V
) and then Compile_Time_Known_Value
(V
);
1299 -- We have a value, see if it is compile time known
1302 -- Integer literals are worth storing in the cache
1304 if K
= N_Integer_Literal
then
1306 CV_Ent
.V
:= Intval
(Op
);
1309 -- Other literals and NULL are known at compile time
1312 K
= N_Character_Literal
1316 K
= N_String_Literal
1322 -- Any reference to Null_Parameter is known at compile time. No
1323 -- other attribute references (that have not already been folded)
1324 -- are known at compile time.
1326 elsif K
= N_Attribute_Reference
then
1327 return Attribute_Name
(Op
) = Name_Null_Parameter
;
1331 -- If we fall through, not known at compile time
1335 -- If we get an exception while trying to do this test, then some error
1336 -- has occurred, and we simply say that the value is not known after all
1341 end Compile_Time_Known_Value
;
1343 --------------------------------------
1344 -- Compile_Time_Known_Value_Or_Aggr --
1345 --------------------------------------
1347 function Compile_Time_Known_Value_Or_Aggr
(Op
: Node_Id
) return Boolean is
1349 -- If we have an entity name, then see if it is the name of a constant
1350 -- and if so, test the corresponding constant value, or the name of
1351 -- an enumeration literal, which is always a constant.
1353 if Is_Entity_Name
(Op
) then
1355 E
: constant Entity_Id
:= Entity
(Op
);
1359 if Ekind
(E
) = E_Enumeration_Literal
then
1362 elsif Ekind
(E
) /= E_Constant
then
1366 V
:= Constant_Value
(E
);
1368 and then Compile_Time_Known_Value_Or_Aggr
(V
);
1372 -- We have a value, see if it is compile time known
1375 if Compile_Time_Known_Value
(Op
) then
1378 elsif Nkind
(Op
) = N_Aggregate
then
1380 if Present
(Expressions
(Op
)) then
1385 Expr
:= First
(Expressions
(Op
));
1386 while Present
(Expr
) loop
1387 if not Compile_Time_Known_Value_Or_Aggr
(Expr
) then
1396 if Present
(Component_Associations
(Op
)) then
1401 Cass
:= First
(Component_Associations
(Op
));
1402 while Present
(Cass
) loop
1404 Compile_Time_Known_Value_Or_Aggr
(Expression
(Cass
))
1416 -- All other types of values are not known at compile time
1423 end Compile_Time_Known_Value_Or_Aggr
;
1429 -- This is only called for actuals of functions that are not predefined
1430 -- operators (which have already been rewritten as operators at this
1431 -- stage), so the call can never be folded, and all that needs doing for
1432 -- the actual is to do the check for a non-static context.
1434 procedure Eval_Actual
(N
: Node_Id
) is
1436 Check_Non_Static_Context
(N
);
1439 --------------------
1440 -- Eval_Allocator --
1441 --------------------
1443 -- Allocators are never static, so all we have to do is to do the
1444 -- check for a non-static context if an expression is present.
1446 procedure Eval_Allocator
(N
: Node_Id
) is
1447 Expr
: constant Node_Id
:= Expression
(N
);
1450 if Nkind
(Expr
) = N_Qualified_Expression
then
1451 Check_Non_Static_Context
(Expression
(Expr
));
1455 ------------------------
1456 -- Eval_Arithmetic_Op --
1457 ------------------------
1459 -- Arithmetic operations are static functions, so the result is static
1460 -- if both operands are static (RM 4.9(7), 4.9(20)).
1462 procedure Eval_Arithmetic_Op
(N
: Node_Id
) is
1463 Left
: constant Node_Id
:= Left_Opnd
(N
);
1464 Right
: constant Node_Id
:= Right_Opnd
(N
);
1465 Ltype
: constant Entity_Id
:= Etype
(Left
);
1466 Rtype
: constant Entity_Id
:= Etype
(Right
);
1467 Otype
: Entity_Id
:= Empty
;
1472 -- If not foldable we are done
1474 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
1480 if Is_Universal_Numeric_Type
(Etype
(Left
))
1482 Is_Universal_Numeric_Type
(Etype
(Right
))
1484 Otype
:= Find_Universal_Operator_Type
(N
);
1487 -- Fold for cases where both operands are of integer type
1489 if Is_Integer_Type
(Ltype
) and then Is_Integer_Type
(Rtype
) then
1491 Left_Int
: constant Uint
:= Expr_Value
(Left
);
1492 Right_Int
: constant Uint
:= Expr_Value
(Right
);
1499 Result
:= Left_Int
+ Right_Int
;
1501 when N_Op_Subtract
=>
1502 Result
:= Left_Int
- Right_Int
;
1504 when N_Op_Multiply
=>
1507 (Num_Bits
(Left_Int
) + Num_Bits
(Right_Int
)))
1509 Result
:= Left_Int
* Right_Int
;
1516 -- The exception Constraint_Error is raised by integer
1517 -- division, rem and mod if the right operand is zero.
1519 if Right_Int
= 0 then
1520 Apply_Compile_Time_Constraint_Error
1521 (N
, "division by zero",
1527 Result
:= Left_Int
/ Right_Int
;
1532 -- The exception Constraint_Error is raised by integer
1533 -- division, rem and mod if the right operand is zero.
1535 if Right_Int
= 0 then
1536 Apply_Compile_Time_Constraint_Error
1537 (N
, "mod with zero divisor",
1542 Result
:= Left_Int
mod Right_Int
;
1547 -- The exception Constraint_Error is raised by integer
1548 -- division, rem and mod if the right operand is zero.
1550 if Right_Int
= 0 then
1551 Apply_Compile_Time_Constraint_Error
1552 (N
, "rem with zero divisor",
1558 Result
:= Left_Int
rem Right_Int
;
1562 raise Program_Error
;
1565 -- Adjust the result by the modulus if the type is a modular type
1567 if Is_Modular_Integer_Type
(Ltype
) then
1568 Result
:= Result
mod Modulus
(Ltype
);
1570 -- For a signed integer type, check non-static overflow
1572 elsif (not Stat
) and then Is_Signed_Integer_Type
(Ltype
) then
1574 BT
: constant Entity_Id
:= Base_Type
(Ltype
);
1575 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(BT
));
1576 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(BT
));
1578 if Result
< Lo
or else Result
> Hi
then
1579 Apply_Compile_Time_Constraint_Error
1580 (N
, "value not in range of }?",
1581 CE_Overflow_Check_Failed
,
1588 -- If we get here we can fold the result
1590 Fold_Uint
(N
, Result
, Stat
);
1593 -- Cases where at least one operand is a real. We handle the cases of
1594 -- both reals, or mixed/real integer cases (the latter happen only for
1595 -- divide and multiply, and the result is always real).
1597 elsif Is_Real_Type
(Ltype
) or else Is_Real_Type
(Rtype
) then
1604 if Is_Real_Type
(Ltype
) then
1605 Left_Real
:= Expr_Value_R
(Left
);
1607 Left_Real
:= UR_From_Uint
(Expr_Value
(Left
));
1610 if Is_Real_Type
(Rtype
) then
1611 Right_Real
:= Expr_Value_R
(Right
);
1613 Right_Real
:= UR_From_Uint
(Expr_Value
(Right
));
1616 if Nkind
(N
) = N_Op_Add
then
1617 Result
:= Left_Real
+ Right_Real
;
1619 elsif Nkind
(N
) = N_Op_Subtract
then
1620 Result
:= Left_Real
- Right_Real
;
1622 elsif Nkind
(N
) = N_Op_Multiply
then
1623 Result
:= Left_Real
* Right_Real
;
1625 else pragma Assert
(Nkind
(N
) = N_Op_Divide
);
1626 if UR_Is_Zero
(Right_Real
) then
1627 Apply_Compile_Time_Constraint_Error
1628 (N
, "division by zero", CE_Divide_By_Zero
);
1632 Result
:= Left_Real
/ Right_Real
;
1635 Fold_Ureal
(N
, Result
, Stat
);
1639 -- If the operator was resolved to a specific type, make sure that type
1640 -- is frozen even if the expression is folded into a literal (which has
1641 -- a universal type).
1643 if Present
(Otype
) then
1644 Freeze_Before
(N
, Otype
);
1646 end Eval_Arithmetic_Op
;
1648 ----------------------------
1649 -- Eval_Character_Literal --
1650 ----------------------------
1652 -- Nothing to be done!
1654 procedure Eval_Character_Literal
(N
: Node_Id
) is
1655 pragma Warnings
(Off
, N
);
1658 end Eval_Character_Literal
;
1664 -- Static function calls are either calls to predefined operators
1665 -- with static arguments, or calls to functions that rename a literal.
1666 -- Only the latter case is handled here, predefined operators are
1667 -- constant-folded elsewhere.
1669 -- If the function is itself inherited (see 7423-001) the literal of
1670 -- the parent type must be explicitly converted to the return type
1673 procedure Eval_Call
(N
: Node_Id
) is
1674 Loc
: constant Source_Ptr
:= Sloc
(N
);
1675 Typ
: constant Entity_Id
:= Etype
(N
);
1679 if Nkind
(N
) = N_Function_Call
1680 and then No
(Parameter_Associations
(N
))
1681 and then Is_Entity_Name
(Name
(N
))
1682 and then Present
(Alias
(Entity
(Name
(N
))))
1683 and then Is_Enumeration_Type
(Base_Type
(Typ
))
1685 Lit
:= Ultimate_Alias
(Entity
(Name
(N
)));
1687 if Ekind
(Lit
) = E_Enumeration_Literal
then
1688 if Base_Type
(Etype
(Lit
)) /= Base_Type
(Typ
) then
1690 (N
, Convert_To
(Typ
, New_Occurrence_Of
(Lit
, Loc
)));
1692 Rewrite
(N
, New_Occurrence_Of
(Lit
, Loc
));
1700 --------------------------
1701 -- Eval_Case_Expression --
1702 --------------------------
1704 -- Right now we do not attempt folding of any case expressions, and the
1705 -- language does not require it, so the only required processing is to
1706 -- do the check for all expressions appearing in the case expression.
1708 procedure Eval_Case_Expression
(N
: Node_Id
) is
1712 Check_Non_Static_Context
(Expression
(N
));
1714 Alt
:= First
(Alternatives
(N
));
1715 while Present
(Alt
) loop
1716 Check_Non_Static_Context
(Expression
(Alt
));
1719 end Eval_Case_Expression
;
1721 ------------------------
1722 -- Eval_Concatenation --
1723 ------------------------
1725 -- Concatenation is a static function, so the result is static if both
1726 -- operands are static (RM 4.9(7), 4.9(21)).
1728 procedure Eval_Concatenation
(N
: Node_Id
) is
1729 Left
: constant Node_Id
:= Left_Opnd
(N
);
1730 Right
: constant Node_Id
:= Right_Opnd
(N
);
1731 C_Typ
: constant Entity_Id
:= Root_Type
(Component_Type
(Etype
(N
)));
1736 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
1737 -- non-static context.
1739 if Ada_Version
= Ada_83
1740 and then Comes_From_Source
(N
)
1742 Check_Non_Static_Context
(Left
);
1743 Check_Non_Static_Context
(Right
);
1747 -- If not foldable we are done. In principle concatenation that yields
1748 -- any string type is static (i.e. an array type of character types).
1749 -- However, character types can include enumeration literals, and
1750 -- concatenation in that case cannot be described by a literal, so we
1751 -- only consider the operation static if the result is an array of
1752 -- (a descendant of) a predefined character type.
1754 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
1756 if not (Is_Standard_Character_Type
(C_Typ
) and then Fold
) then
1757 Set_Is_Static_Expression
(N
, False);
1761 -- Compile time string concatenation
1763 -- ??? Note that operands that are aggregates can be marked as static,
1764 -- so we should attempt at a later stage to fold concatenations with
1768 Left_Str
: constant Node_Id
:= Get_String_Val
(Left
);
1770 Right_Str
: constant Node_Id
:= Get_String_Val
(Right
);
1771 Folded_Val
: String_Id
;
1774 -- Establish new string literal, and store left operand. We make
1775 -- sure to use the special Start_String that takes an operand if
1776 -- the left operand is a string literal. Since this is optimized
1777 -- in the case where that is the most recently created string
1778 -- literal, we ensure efficient time/space behavior for the
1779 -- case of a concatenation of a series of string literals.
1781 if Nkind
(Left_Str
) = N_String_Literal
then
1782 Left_Len
:= String_Length
(Strval
(Left_Str
));
1784 -- If the left operand is the empty string, and the right operand
1785 -- is a string literal (the case of "" & "..."), the result is the
1786 -- value of the right operand. This optimization is important when
1787 -- Is_Folded_In_Parser, to avoid copying an enormous right
1790 if Left_Len
= 0 and then Nkind
(Right_Str
) = N_String_Literal
then
1791 Folded_Val
:= Strval
(Right_Str
);
1793 Start_String
(Strval
(Left_Str
));
1798 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Left_Str
)));
1802 -- Now append the characters of the right operand, unless we
1803 -- optimized the "" & "..." case above.
1805 if Nkind
(Right_Str
) = N_String_Literal
then
1806 if Left_Len
/= 0 then
1807 Store_String_Chars
(Strval
(Right_Str
));
1808 Folded_Val
:= End_String
;
1811 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Right_Str
)));
1812 Folded_Val
:= End_String
;
1815 Set_Is_Static_Expression
(N
, Stat
);
1819 -- If left operand is the empty string, the result is the
1820 -- right operand, including its bounds if anomalous.
1823 and then Is_Array_Type
(Etype
(Right
))
1824 and then Etype
(Right
) /= Any_String
1826 Set_Etype
(N
, Etype
(Right
));
1829 Fold_Str
(N
, Folded_Val
, Static
=> True);
1832 end Eval_Concatenation
;
1834 ---------------------------------
1835 -- Eval_Conditional_Expression --
1836 ---------------------------------
1838 -- We can fold to a static expression if the condition and both constituent
1839 -- expressions are static. Otherwise, the only required processing is to do
1840 -- the check for non-static context for the then and else expressions.
1842 procedure Eval_Conditional_Expression
(N
: Node_Id
) is
1843 Condition
: constant Node_Id
:= First
(Expressions
(N
));
1844 Then_Expr
: constant Node_Id
:= Next
(Condition
);
1845 Else_Expr
: constant Node_Id
:= Next
(Then_Expr
);
1847 Non_Result
: Node_Id
;
1849 Rstat
: constant Boolean :=
1850 Is_Static_Expression
(Condition
)
1852 Is_Static_Expression
(Then_Expr
)
1854 Is_Static_Expression
(Else_Expr
);
1857 -- If any operand is Any_Type, just propagate to result and do not try
1858 -- to fold, this prevents cascaded errors.
1860 if Etype
(Condition
) = Any_Type
or else
1861 Etype
(Then_Expr
) = Any_Type
or else
1862 Etype
(Else_Expr
) = Any_Type
1864 Set_Etype
(N
, Any_Type
);
1865 Set_Is_Static_Expression
(N
, False);
1868 -- Static case where we can fold. Note that we don't try to fold cases
1869 -- where the condition is known at compile time, but the result is
1870 -- non-static. This avoids possible cases of infinite recursion where
1871 -- the expander puts in a redundant test and we remove it. Instead we
1872 -- deal with these cases in the expander.
1876 -- Select result operand
1878 if Is_True
(Expr_Value
(Condition
)) then
1879 Result
:= Then_Expr
;
1880 Non_Result
:= Else_Expr
;
1882 Result
:= Else_Expr
;
1883 Non_Result
:= Then_Expr
;
1886 -- Note that it does not matter if the non-result operand raises a
1887 -- Constraint_Error, but if the result raises constraint error then
1888 -- we replace the node with a raise constraint error. This will
1889 -- properly propagate Raises_Constraint_Error since this flag is
1892 if Raises_Constraint_Error
(Result
) then
1893 Rewrite_In_Raise_CE
(N
, Result
);
1894 Check_Non_Static_Context
(Non_Result
);
1896 -- Otherwise the result operand replaces the original node
1899 Rewrite
(N
, Relocate_Node
(Result
));
1902 -- Case of condition not known at compile time
1905 Check_Non_Static_Context
(Condition
);
1906 Check_Non_Static_Context
(Then_Expr
);
1907 Check_Non_Static_Context
(Else_Expr
);
1910 Set_Is_Static_Expression
(N
, Rstat
);
1911 end Eval_Conditional_Expression
;
1913 ----------------------
1914 -- Eval_Entity_Name --
1915 ----------------------
1917 -- This procedure is used for identifiers and expanded names other than
1918 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1919 -- static if they denote a static constant (RM 4.9(6)) or if the name
1920 -- denotes an enumeration literal (RM 4.9(22)).
1922 procedure Eval_Entity_Name
(N
: Node_Id
) is
1923 Def_Id
: constant Entity_Id
:= Entity
(N
);
1927 -- Enumeration literals are always considered to be constants
1928 -- and cannot raise constraint error (RM 4.9(22)).
1930 if Ekind
(Def_Id
) = E_Enumeration_Literal
then
1931 Set_Is_Static_Expression
(N
);
1934 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1935 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1936 -- it does not violate 10.2.1(8) here, since this is not a variable.
1938 elsif Ekind
(Def_Id
) = E_Constant
then
1940 -- Deferred constants must always be treated as nonstatic
1941 -- outside the scope of their full view.
1943 if Present
(Full_View
(Def_Id
))
1944 and then not In_Open_Scopes
(Scope
(Def_Id
))
1948 Val
:= Constant_Value
(Def_Id
);
1951 if Present
(Val
) then
1952 Set_Is_Static_Expression
1953 (N
, Is_Static_Expression
(Val
)
1954 and then Is_Static_Subtype
(Etype
(Def_Id
)));
1955 Set_Raises_Constraint_Error
(N
, Raises_Constraint_Error
(Val
));
1957 if not Is_Static_Expression
(N
)
1958 and then not Is_Generic_Type
(Etype
(N
))
1960 Validate_Static_Object_Name
(N
);
1967 -- Fall through if the name is not static
1969 Validate_Static_Object_Name
(N
);
1970 end Eval_Entity_Name
;
1972 ----------------------------
1973 -- Eval_Indexed_Component --
1974 ----------------------------
1976 -- Indexed components are never static, so we need to perform the check
1977 -- for non-static context on the index values. Then, we check if the
1978 -- value can be obtained at compile time, even though it is non-static.
1980 procedure Eval_Indexed_Component
(N
: Node_Id
) is
1984 -- Check for non-static context on index values
1986 Expr
:= First
(Expressions
(N
));
1987 while Present
(Expr
) loop
1988 Check_Non_Static_Context
(Expr
);
1992 -- If the indexed component appears in an object renaming declaration
1993 -- then we do not want to try to evaluate it, since in this case we
1994 -- need the identity of the array element.
1996 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
1999 -- Similarly if the indexed component appears as the prefix of an
2000 -- attribute we don't want to evaluate it, because at least for
2001 -- some cases of attributes we need the identify (e.g. Access, Size)
2003 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
then
2007 -- Note: there are other cases, such as the left side of an assignment,
2008 -- or an OUT parameter for a call, where the replacement results in the
2009 -- illegal use of a constant, But these cases are illegal in the first
2010 -- place, so the replacement, though silly, is harmless.
2012 -- Now see if this is a constant array reference
2014 if List_Length
(Expressions
(N
)) = 1
2015 and then Is_Entity_Name
(Prefix
(N
))
2016 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
2017 and then Present
(Constant_Value
(Entity
(Prefix
(N
))))
2020 Loc
: constant Source_Ptr
:= Sloc
(N
);
2021 Arr
: constant Node_Id
:= Constant_Value
(Entity
(Prefix
(N
)));
2022 Sub
: constant Node_Id
:= First
(Expressions
(N
));
2028 -- Linear one's origin subscript value for array reference
2031 -- Lower bound of the first array index
2034 -- Value from constant array
2037 Atyp
:= Etype
(Arr
);
2039 if Is_Access_Type
(Atyp
) then
2040 Atyp
:= Designated_Type
(Atyp
);
2043 -- If we have an array type (we should have but perhaps there are
2044 -- error cases where this is not the case), then see if we can do
2045 -- a constant evaluation of the array reference.
2047 if Is_Array_Type
(Atyp
) and then Atyp
/= Any_Composite
then
2048 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
2049 Lbd
:= String_Literal_Low_Bound
(Atyp
);
2051 Lbd
:= Type_Low_Bound
(Etype
(First_Index
(Atyp
)));
2054 if Compile_Time_Known_Value
(Sub
)
2055 and then Nkind
(Arr
) = N_Aggregate
2056 and then Compile_Time_Known_Value
(Lbd
)
2057 and then Is_Discrete_Type
(Component_Type
(Atyp
))
2059 Lin
:= UI_To_Int
(Expr_Value
(Sub
) - Expr_Value
(Lbd
)) + 1;
2061 if List_Length
(Expressions
(Arr
)) >= Lin
then
2062 Elm
:= Pick
(Expressions
(Arr
), Lin
);
2064 -- If the resulting expression is compile time known,
2065 -- then we can rewrite the indexed component with this
2066 -- value, being sure to mark the result as non-static.
2067 -- We also reset the Sloc, in case this generates an
2068 -- error later on (e.g. 136'Access).
2070 if Compile_Time_Known_Value
(Elm
) then
2071 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2072 Set_Is_Static_Expression
(N
, False);
2077 -- We can also constant-fold if the prefix is a string literal.
2078 -- This will be useful in an instantiation or an inlining.
2080 elsif Compile_Time_Known_Value
(Sub
)
2081 and then Nkind
(Arr
) = N_String_Literal
2082 and then Compile_Time_Known_Value
(Lbd
)
2083 and then Expr_Value
(Lbd
) = 1
2084 and then Expr_Value
(Sub
) <=
2085 String_Literal_Length
(Etype
(Arr
))
2088 C
: constant Char_Code
:=
2089 Get_String_Char
(Strval
(Arr
),
2090 UI_To_Int
(Expr_Value
(Sub
)));
2092 Set_Character_Literal_Name
(C
);
2095 Make_Character_Literal
(Loc
,
2097 Char_Literal_Value
=> UI_From_CC
(C
));
2098 Set_Etype
(Elm
, Component_Type
(Atyp
));
2099 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2100 Set_Is_Static_Expression
(N
, False);
2106 end Eval_Indexed_Component
;
2108 --------------------------
2109 -- Eval_Integer_Literal --
2110 --------------------------
2112 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2113 -- as static by the analyzer. The reason we did it that early is to allow
2114 -- the possibility of turning off the Is_Static_Expression flag after
2115 -- analysis, but before resolution, when integer literals are generated in
2116 -- the expander that do not correspond to static expressions.
2118 procedure Eval_Integer_Literal
(N
: Node_Id
) is
2119 T
: constant Entity_Id
:= Etype
(N
);
2121 function In_Any_Integer_Context
return Boolean;
2122 -- If the literal is resolved with a specific type in a context where
2123 -- the expected type is Any_Integer, there are no range checks on the
2124 -- literal. By the time the literal is evaluated, it carries the type
2125 -- imposed by the enclosing expression, and we must recover the context
2126 -- to determine that Any_Integer is meant.
2128 ----------------------------
2129 -- In_Any_Integer_Context --
2130 ----------------------------
2132 function In_Any_Integer_Context
return Boolean is
2133 Par
: constant Node_Id
:= Parent
(N
);
2134 K
: constant Node_Kind
:= Nkind
(Par
);
2137 -- Any_Integer also appears in digits specifications for real types,
2138 -- but those have bounds smaller that those of any integer base type,
2139 -- so we can safely ignore these cases.
2141 return K
= N_Number_Declaration
2142 or else K
= N_Attribute_Reference
2143 or else K
= N_Attribute_Definition_Clause
2144 or else K
= N_Modular_Type_Definition
2145 or else K
= N_Signed_Integer_Type_Definition
;
2146 end In_Any_Integer_Context
;
2148 -- Start of processing for Eval_Integer_Literal
2152 -- If the literal appears in a non-expression context, then it is
2153 -- certainly appearing in a non-static context, so check it. This is
2154 -- actually a redundant check, since Check_Non_Static_Context would
2155 -- check it, but it seems worth while avoiding the call.
2157 if Nkind
(Parent
(N
)) not in N_Subexpr
2158 and then not In_Any_Integer_Context
2160 Check_Non_Static_Context
(N
);
2163 -- Modular integer literals must be in their base range
2165 if Is_Modular_Integer_Type
(T
)
2166 and then Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True)
2170 end Eval_Integer_Literal
;
2172 ---------------------
2173 -- Eval_Logical_Op --
2174 ---------------------
2176 -- Logical operations are static functions, so the result is potentially
2177 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2179 procedure Eval_Logical_Op
(N
: Node_Id
) is
2180 Left
: constant Node_Id
:= Left_Opnd
(N
);
2181 Right
: constant Node_Id
:= Right_Opnd
(N
);
2186 -- If not foldable we are done
2188 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2194 -- Compile time evaluation of logical operation
2197 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2198 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2201 -- VMS includes bitwise operations on signed types
2203 if Is_Modular_Integer_Type
(Etype
(N
))
2204 or else Is_VMS_Operator
(Entity
(N
))
2207 Left_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2208 Right_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2211 To_Bits
(Left_Int
, Left_Bits
);
2212 To_Bits
(Right_Int
, Right_Bits
);
2214 -- Note: should really be able to use array ops instead of
2215 -- these loops, but they weren't working at the time ???
2217 if Nkind
(N
) = N_Op_And
then
2218 for J
in Left_Bits
'Range loop
2219 Left_Bits
(J
) := Left_Bits
(J
) and Right_Bits
(J
);
2222 elsif Nkind
(N
) = N_Op_Or
then
2223 for J
in Left_Bits
'Range loop
2224 Left_Bits
(J
) := Left_Bits
(J
) or Right_Bits
(J
);
2228 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2230 for J
in Left_Bits
'Range loop
2231 Left_Bits
(J
) := Left_Bits
(J
) xor Right_Bits
(J
);
2235 Fold_Uint
(N
, From_Bits
(Left_Bits
, Etype
(N
)), Stat
);
2239 pragma Assert
(Is_Boolean_Type
(Etype
(N
)));
2241 if Nkind
(N
) = N_Op_And
then
2243 Test
(Is_True
(Left_Int
) and then Is_True
(Right_Int
)), Stat
);
2245 elsif Nkind
(N
) = N_Op_Or
then
2247 Test
(Is_True
(Left_Int
) or else Is_True
(Right_Int
)), Stat
);
2250 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2252 Test
(Is_True
(Left_Int
) xor Is_True
(Right_Int
)), Stat
);
2256 end Eval_Logical_Op
;
2258 ------------------------
2259 -- Eval_Membership_Op --
2260 ------------------------
2262 -- A membership test is potentially static if the expression is static, and
2263 -- the range is a potentially static range, or is a subtype mark denoting a
2264 -- static subtype (RM 4.9(12)).
2266 procedure Eval_Membership_Op
(N
: Node_Id
) is
2267 Left
: constant Node_Id
:= Left_Opnd
(N
);
2268 Right
: constant Node_Id
:= Right_Opnd
(N
);
2277 -- Ignore if error in either operand, except to make sure that Any_Type
2278 -- is properly propagated to avoid junk cascaded errors.
2280 if Etype
(Left
) = Any_Type
or else Etype
(Right
) = Any_Type
then
2281 Set_Etype
(N
, Any_Type
);
2285 -- Case of right operand is a subtype name
2287 if Is_Entity_Name
(Right
) then
2288 Def_Id
:= Entity
(Right
);
2290 if (Is_Scalar_Type
(Def_Id
) or else Is_String_Type
(Def_Id
))
2291 and then Is_OK_Static_Subtype
(Def_Id
)
2293 Test_Expression_Is_Foldable
(N
, Left
, Stat
, Fold
);
2295 if not Fold
or else not Stat
then
2299 Check_Non_Static_Context
(Left
);
2303 -- For string membership tests we will check the length further on
2305 if not Is_String_Type
(Def_Id
) then
2306 Lo
:= Type_Low_Bound
(Def_Id
);
2307 Hi
:= Type_High_Bound
(Def_Id
);
2314 -- Case of right operand is a range
2317 if Is_Static_Range
(Right
) then
2318 Test_Expression_Is_Foldable
(N
, Left
, Stat
, Fold
);
2320 if not Fold
or else not Stat
then
2323 -- If one bound of range raises CE, then don't try to fold
2325 elsif not Is_OK_Static_Range
(Right
) then
2326 Check_Non_Static_Context
(Left
);
2331 Check_Non_Static_Context
(Left
);
2335 -- Here we know range is an OK static range
2337 Lo
:= Low_Bound
(Right
);
2338 Hi
:= High_Bound
(Right
);
2341 -- For strings we check that the length of the string expression is
2342 -- compatible with the string subtype if the subtype is constrained,
2343 -- or if unconstrained then the test is always true.
2345 if Is_String_Type
(Etype
(Right
)) then
2346 if not Is_Constrained
(Etype
(Right
)) then
2351 Typlen
: constant Uint
:= String_Type_Len
(Etype
(Right
));
2352 Strlen
: constant Uint
:=
2354 (String_Length
(Strval
(Get_String_Val
(Left
))));
2356 Result
:= (Typlen
= Strlen
);
2360 -- Fold the membership test. We know we have a static range and Lo and
2361 -- Hi are set to the expressions for the end points of this range.
2363 elsif Is_Real_Type
(Etype
(Right
)) then
2365 Leftval
: constant Ureal
:= Expr_Value_R
(Left
);
2368 Result
:= Expr_Value_R
(Lo
) <= Leftval
2369 and then Leftval
<= Expr_Value_R
(Hi
);
2374 Leftval
: constant Uint
:= Expr_Value
(Left
);
2377 Result
:= Expr_Value
(Lo
) <= Leftval
2378 and then Leftval
<= Expr_Value
(Hi
);
2382 if Nkind
(N
) = N_Not_In
then
2383 Result
:= not Result
;
2386 Fold_Uint
(N
, Test
(Result
), True);
2388 Warn_On_Known_Condition
(N
);
2389 end Eval_Membership_Op
;
2391 ------------------------
2392 -- Eval_Named_Integer --
2393 ------------------------
2395 procedure Eval_Named_Integer
(N
: Node_Id
) is
2398 Expr_Value
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2399 end Eval_Named_Integer
;
2401 ---------------------
2402 -- Eval_Named_Real --
2403 ---------------------
2405 procedure Eval_Named_Real
(N
: Node_Id
) is
2408 Expr_Value_R
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2409 end Eval_Named_Real
;
2415 -- Exponentiation is a static functions, so the result is potentially
2416 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2418 procedure Eval_Op_Expon
(N
: Node_Id
) is
2419 Left
: constant Node_Id
:= Left_Opnd
(N
);
2420 Right
: constant Node_Id
:= Right_Opnd
(N
);
2425 -- If not foldable we are done
2427 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2433 -- Fold exponentiation operation
2436 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2441 if Is_Integer_Type
(Etype
(Left
)) then
2443 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2447 -- Exponentiation of an integer raises Constraint_Error for a
2448 -- negative exponent (RM 4.5.6).
2450 if Right_Int
< 0 then
2451 Apply_Compile_Time_Constraint_Error
2452 (N
, "integer exponent negative",
2453 CE_Range_Check_Failed
,
2458 if OK_Bits
(N
, Num_Bits
(Left_Int
) * Right_Int
) then
2459 Result
:= Left_Int
** Right_Int
;
2464 if Is_Modular_Integer_Type
(Etype
(N
)) then
2465 Result
:= Result
mod Modulus
(Etype
(N
));
2468 Fold_Uint
(N
, Result
, Stat
);
2476 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
2479 -- Cannot have a zero base with a negative exponent
2481 if UR_Is_Zero
(Left_Real
) then
2483 if Right_Int
< 0 then
2484 Apply_Compile_Time_Constraint_Error
2485 (N
, "zero ** negative integer",
2486 CE_Range_Check_Failed
,
2490 Fold_Ureal
(N
, Ureal_0
, Stat
);
2494 Fold_Ureal
(N
, Left_Real
** Right_Int
, Stat
);
2505 -- The not operation is a static functions, so the result is potentially
2506 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2508 procedure Eval_Op_Not
(N
: Node_Id
) is
2509 Right
: constant Node_Id
:= Right_Opnd
(N
);
2514 -- If not foldable we are done
2516 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
2522 -- Fold not operation
2525 Rint
: constant Uint
:= Expr_Value
(Right
);
2526 Typ
: constant Entity_Id
:= Etype
(N
);
2529 -- Negation is equivalent to subtracting from the modulus minus one.
2530 -- For a binary modulus this is equivalent to the ones-complement of
2531 -- the original value. For non-binary modulus this is an arbitrary
2532 -- but consistent definition.
2534 if Is_Modular_Integer_Type
(Typ
) then
2535 Fold_Uint
(N
, Modulus
(Typ
) - 1 - Rint
, Stat
);
2538 pragma Assert
(Is_Boolean_Type
(Typ
));
2539 Fold_Uint
(N
, Test
(not Is_True
(Rint
)), Stat
);
2542 Set_Is_Static_Expression
(N
, Stat
);
2546 -------------------------------
2547 -- Eval_Qualified_Expression --
2548 -------------------------------
2550 -- A qualified expression is potentially static if its subtype mark denotes
2551 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2553 procedure Eval_Qualified_Expression
(N
: Node_Id
) is
2554 Operand
: constant Node_Id
:= Expression
(N
);
2555 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
2562 -- Can only fold if target is string or scalar and subtype is static.
2563 -- Also, do not fold if our parent is an allocator (this is because the
2564 -- qualified expression is really part of the syntactic structure of an
2565 -- allocator, and we do not want to end up with something that
2566 -- corresponds to "new 1" where the 1 is the result of folding a
2567 -- qualified expression).
2569 if not Is_Static_Subtype
(Target_Type
)
2570 or else Nkind
(Parent
(N
)) = N_Allocator
2572 Check_Non_Static_Context
(Operand
);
2574 -- If operand is known to raise constraint_error, set the flag on the
2575 -- expression so it does not get optimized away.
2577 if Nkind
(Operand
) = N_Raise_Constraint_Error
then
2578 Set_Raises_Constraint_Error
(N
);
2584 -- If not foldable we are done
2586 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
2591 -- Don't try fold if target type has constraint error bounds
2593 elsif not Is_OK_Static_Subtype
(Target_Type
) then
2594 Set_Raises_Constraint_Error
(N
);
2598 -- Here we will fold, save Print_In_Hex indication
2600 Hex
:= Nkind
(Operand
) = N_Integer_Literal
2601 and then Print_In_Hex
(Operand
);
2603 -- Fold the result of qualification
2605 if Is_Discrete_Type
(Target_Type
) then
2606 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
2608 -- Preserve Print_In_Hex indication
2610 if Hex
and then Nkind
(N
) = N_Integer_Literal
then
2611 Set_Print_In_Hex
(N
);
2614 elsif Is_Real_Type
(Target_Type
) then
2615 Fold_Ureal
(N
, Expr_Value_R
(Operand
), Stat
);
2618 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Stat
);
2621 Set_Is_Static_Expression
(N
, False);
2623 Check_String_Literal_Length
(N
, Target_Type
);
2629 -- The expression may be foldable but not static
2631 Set_Is_Static_Expression
(N
, Stat
);
2633 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
2636 end Eval_Qualified_Expression
;
2638 -----------------------
2639 -- Eval_Real_Literal --
2640 -----------------------
2642 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2643 -- as static by the analyzer. The reason we did it that early is to allow
2644 -- the possibility of turning off the Is_Static_Expression flag after
2645 -- analysis, but before resolution, when integer literals are generated
2646 -- in the expander that do not correspond to static expressions.
2648 procedure Eval_Real_Literal
(N
: Node_Id
) is
2649 PK
: constant Node_Kind
:= Nkind
(Parent
(N
));
2652 -- If the literal appears in a non-expression context and not as part of
2653 -- a number declaration, then it is appearing in a non-static context,
2656 if PK
not in N_Subexpr
and then PK
/= N_Number_Declaration
then
2657 Check_Non_Static_Context
(N
);
2659 end Eval_Real_Literal
;
2661 ------------------------
2662 -- Eval_Relational_Op --
2663 ------------------------
2665 -- Relational operations are static functions, so the result is static if
2666 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
2667 -- the result is never static, even if the operands are.
2669 procedure Eval_Relational_Op
(N
: Node_Id
) is
2670 Left
: constant Node_Id
:= Left_Opnd
(N
);
2671 Right
: constant Node_Id
:= Right_Opnd
(N
);
2672 Typ
: constant Entity_Id
:= Etype
(Left
);
2673 Otype
: Entity_Id
:= Empty
;
2679 -- One special case to deal with first. If we can tell that the result
2680 -- will be false because the lengths of one or more index subtypes are
2681 -- compile time known and different, then we can replace the entire
2682 -- result by False. We only do this for one dimensional arrays, because
2683 -- the case of multi-dimensional arrays is rare and too much trouble! If
2684 -- one of the operands is an illegal aggregate, its type might still be
2685 -- an arbitrary composite type, so nothing to do.
2687 if Is_Array_Type
(Typ
)
2688 and then Typ
/= Any_Composite
2689 and then Number_Dimensions
(Typ
) = 1
2690 and then (Nkind
(N
) = N_Op_Eq
or else Nkind
(N
) = N_Op_Ne
)
2692 if Raises_Constraint_Error
(Left
)
2693 or else Raises_Constraint_Error
(Right
)
2698 -- OK, we have the case where we may be able to do this fold
2700 Length_Mismatch
: declare
2701 procedure Get_Static_Length
(Op
: Node_Id
; Len
: out Uint
);
2702 -- If Op is an expression for a constrained array with a known at
2703 -- compile time length, then Len is set to this (non-negative
2704 -- length). Otherwise Len is set to minus 1.
2706 -----------------------
2707 -- Get_Static_Length --
2708 -----------------------
2710 procedure Get_Static_Length
(Op
: Node_Id
; Len
: out Uint
) is
2714 -- First easy case string literal
2716 if Nkind
(Op
) = N_String_Literal
then
2717 Len
:= UI_From_Int
(String_Length
(Strval
(Op
)));
2721 -- Second easy case, not constrained subtype, so no length
2723 if not Is_Constrained
(Etype
(Op
)) then
2724 Len
:= Uint_Minus_1
;
2730 T
:= Etype
(First_Index
(Etype
(Op
)));
2732 -- The simple case, both bounds are known at compile time
2734 if Is_Discrete_Type
(T
)
2736 Compile_Time_Known_Value
(Type_Low_Bound
(T
))
2738 Compile_Time_Known_Value
(Type_High_Bound
(T
))
2740 Len
:= UI_Max
(Uint_0
,
2741 Expr_Value
(Type_High_Bound
(T
)) -
2742 Expr_Value
(Type_Low_Bound
(T
)) + 1);
2746 -- A more complex case, where the bounds are of the form
2747 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2748 -- either A'First or A'Last (with A an entity name), or X is an
2749 -- entity name, and the two X's are the same and K1 and K2 are
2750 -- known at compile time, in this case, the length can also be
2751 -- computed at compile time, even though the bounds are not
2752 -- known. A common case of this is e.g. (X'First .. X'First+5).
2754 Extract_Length
: declare
2755 procedure Decompose_Expr
2757 Ent
: out Entity_Id
;
2758 Kind
: out Character;
2760 -- Given an expression, see if is of the form above,
2761 -- X [+/- K]. If so Ent is set to the entity in X,
2762 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2763 -- and Cons is the value of K. If the expression is
2764 -- not of the required form, Ent is set to Empty.
2766 --------------------
2767 -- Decompose_Expr --
2768 --------------------
2770 procedure Decompose_Expr
2772 Ent
: out Entity_Id
;
2773 Kind
: out Character;
2779 if Nkind
(Expr
) = N_Op_Add
2780 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
2782 Exp
:= Left_Opnd
(Expr
);
2783 Cons
:= Expr_Value
(Right_Opnd
(Expr
));
2785 elsif Nkind
(Expr
) = N_Op_Subtract
2786 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
2788 Exp
:= Left_Opnd
(Expr
);
2789 Cons
:= -Expr_Value
(Right_Opnd
(Expr
));
2791 -- If the bound is a constant created to remove side
2792 -- effects, recover original expression to see if it has
2793 -- one of the recognizable forms.
2795 elsif Nkind
(Expr
) = N_Identifier
2796 and then not Comes_From_Source
(Entity
(Expr
))
2797 and then Ekind
(Entity
(Expr
)) = E_Constant
2799 Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
2801 Exp
:= Expression
(Parent
(Entity
(Expr
)));
2802 Decompose_Expr
(Exp
, Ent
, Kind
, Cons
);
2804 -- If original expression includes an entity, create a
2805 -- reference to it for use below.
2807 if Present
(Ent
) then
2808 Exp
:= New_Occurrence_Of
(Ent
, Sloc
(Ent
));
2816 -- At this stage Exp is set to the potential X
2818 if Nkind
(Exp
) = N_Attribute_Reference
then
2819 if Attribute_Name
(Exp
) = Name_First
then
2822 elsif Attribute_Name
(Exp
) = Name_Last
then
2830 Exp
:= Prefix
(Exp
);
2836 if Is_Entity_Name
(Exp
)
2837 and then Present
(Entity
(Exp
))
2839 Ent
:= Entity
(Exp
);
2847 Ent1
, Ent2
: Entity_Id
;
2848 Kind1
, Kind2
: Character;
2849 Cons1
, Cons2
: Uint
;
2851 -- Start of processing for Extract_Length
2855 (Original_Node
(Type_Low_Bound
(T
)), Ent1
, Kind1
, Cons1
);
2857 (Original_Node
(Type_High_Bound
(T
)), Ent2
, Kind2
, Cons2
);
2860 and then Kind1
= Kind2
2861 and then Ent1
= Ent2
2863 Len
:= Cons2
- Cons1
+ 1;
2865 Len
:= Uint_Minus_1
;
2868 end Get_Static_Length
;
2875 -- Start of processing for Length_Mismatch
2878 Get_Static_Length
(Left
, Len_L
);
2879 Get_Static_Length
(Right
, Len_R
);
2881 if Len_L
/= Uint_Minus_1
2882 and then Len_R
/= Uint_Minus_1
2883 and then Len_L
/= Len_R
2885 Fold_Uint
(N
, Test
(Nkind
(N
) = N_Op_Ne
), False);
2886 Warn_On_Known_Condition
(N
);
2889 end Length_Mismatch
;
2892 -- Test for expression being foldable
2894 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2896 -- Only comparisons of scalars can give static results. In particular,
2897 -- comparisons of strings never yield a static result, even if both
2898 -- operands are static strings.
2900 if not Is_Scalar_Type
(Typ
) then
2902 Set_Is_Static_Expression
(N
, False);
2905 -- For operators on universal numeric types called as functions with
2906 -- an explicit scope, determine appropriate specific numeric type, and
2907 -- diagnose possible ambiguity.
2909 if Is_Universal_Numeric_Type
(Etype
(Left
))
2911 Is_Universal_Numeric_Type
(Etype
(Right
))
2913 Otype
:= Find_Universal_Operator_Type
(N
);
2916 -- For static real type expressions, we cannot use Compile_Time_Compare
2917 -- since it worries about run-time results which are not exact.
2919 if Stat
and then Is_Real_Type
(Typ
) then
2921 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
2922 Right_Real
: constant Ureal
:= Expr_Value_R
(Right
);
2926 when N_Op_Eq
=> Result
:= (Left_Real
= Right_Real
);
2927 when N_Op_Ne
=> Result
:= (Left_Real
/= Right_Real
);
2928 when N_Op_Lt
=> Result
:= (Left_Real
< Right_Real
);
2929 when N_Op_Le
=> Result
:= (Left_Real
<= Right_Real
);
2930 when N_Op_Gt
=> Result
:= (Left_Real
> Right_Real
);
2931 when N_Op_Ge
=> Result
:= (Left_Real
>= Right_Real
);
2934 raise Program_Error
;
2937 Fold_Uint
(N
, Test
(Result
), True);
2940 -- For all other cases, we use Compile_Time_Compare to do the compare
2944 CR
: constant Compare_Result
:=
2945 Compile_Time_Compare
(Left
, Right
, Assume_Valid
=> False);
2948 if CR
= Unknown
then
2956 elsif CR
= NE
or else CR
= GT
or else CR
= LT
then
2963 if CR
= NE
or else CR
= GT
or else CR
= LT
then
2974 elsif CR
= EQ
or else CR
= GT
or else CR
= GE
then
2981 if CR
= LT
or else CR
= EQ
or else CR
= LE
then
2992 elsif CR
= EQ
or else CR
= LT
or else CR
= LE
then
2999 if CR
= GT
or else CR
= EQ
or else CR
= GE
then
3008 raise Program_Error
;
3012 Fold_Uint
(N
, Test
(Result
), Stat
);
3015 -- For the case of a folded relational operator on a specific numeric
3016 -- type, freeze operand type now.
3018 if Present
(Otype
) then
3019 Freeze_Before
(N
, Otype
);
3022 Warn_On_Known_Condition
(N
);
3023 end Eval_Relational_Op
;
3029 -- Shift operations are intrinsic operations that can never be static, so
3030 -- the only processing required is to perform the required check for a non
3031 -- static context for the two operands.
3033 -- Actually we could do some compile time evaluation here some time ???
3035 procedure Eval_Shift
(N
: Node_Id
) is
3037 Check_Non_Static_Context
(Left_Opnd
(N
));
3038 Check_Non_Static_Context
(Right_Opnd
(N
));
3041 ------------------------
3042 -- Eval_Short_Circuit --
3043 ------------------------
3045 -- A short circuit operation is potentially static if both operands are
3046 -- potentially static (RM 4.9 (13)).
3048 procedure Eval_Short_Circuit
(N
: Node_Id
) is
3049 Kind
: constant Node_Kind
:= Nkind
(N
);
3050 Left
: constant Node_Id
:= Left_Opnd
(N
);
3051 Right
: constant Node_Id
:= Right_Opnd
(N
);
3054 Rstat
: constant Boolean :=
3055 Is_Static_Expression
(Left
)
3057 Is_Static_Expression
(Right
);
3060 -- Short circuit operations are never static in Ada 83
3062 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
3063 Check_Non_Static_Context
(Left
);
3064 Check_Non_Static_Context
(Right
);
3068 -- Now look at the operands, we can't quite use the normal call to
3069 -- Test_Expression_Is_Foldable here because short circuit operations
3070 -- are a special case, they can still be foldable, even if the right
3071 -- operand raises constraint error.
3073 -- If either operand is Any_Type, just propagate to result and do not
3074 -- try to fold, this prevents cascaded errors.
3076 if Etype
(Left
) = Any_Type
or else Etype
(Right
) = Any_Type
then
3077 Set_Etype
(N
, Any_Type
);
3080 -- If left operand raises constraint error, then replace node N with
3081 -- the raise constraint error node, and we are obviously not foldable.
3082 -- Is_Static_Expression is set from the two operands in the normal way,
3083 -- and we check the right operand if it is in a non-static context.
3085 elsif Raises_Constraint_Error
(Left
) then
3087 Check_Non_Static_Context
(Right
);
3090 Rewrite_In_Raise_CE
(N
, Left
);
3091 Set_Is_Static_Expression
(N
, Rstat
);
3094 -- If the result is not static, then we won't in any case fold
3096 elsif not Rstat
then
3097 Check_Non_Static_Context
(Left
);
3098 Check_Non_Static_Context
(Right
);
3102 -- Here the result is static, note that, unlike the normal processing
3103 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3104 -- the right operand raises constraint error, that's because it is not
3105 -- significant if the left operand is decisive.
3107 Set_Is_Static_Expression
(N
);
3109 -- It does not matter if the right operand raises constraint error if
3110 -- it will not be evaluated. So deal specially with the cases where
3111 -- the right operand is not evaluated. Note that we will fold these
3112 -- cases even if the right operand is non-static, which is fine, but
3113 -- of course in these cases the result is not potentially static.
3115 Left_Int
:= Expr_Value
(Left
);
3117 if (Kind
= N_And_Then
and then Is_False
(Left_Int
))
3119 (Kind
= N_Or_Else
and then Is_True
(Left_Int
))
3121 Fold_Uint
(N
, Left_Int
, Rstat
);
3125 -- If first operand not decisive, then it does matter if the right
3126 -- operand raises constraint error, since it will be evaluated, so
3127 -- we simply replace the node with the right operand. Note that this
3128 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3129 -- (both are set to True in Right).
3131 if Raises_Constraint_Error
(Right
) then
3132 Rewrite_In_Raise_CE
(N
, Right
);
3133 Check_Non_Static_Context
(Left
);
3137 -- Otherwise the result depends on the right operand
3139 Fold_Uint
(N
, Expr_Value
(Right
), Rstat
);
3141 end Eval_Short_Circuit
;
3147 -- Slices can never be static, so the only processing required is to check
3148 -- for non-static context if an explicit range is given.
3150 procedure Eval_Slice
(N
: Node_Id
) is
3151 Drange
: constant Node_Id
:= Discrete_Range
(N
);
3153 if Nkind
(Drange
) = N_Range
then
3154 Check_Non_Static_Context
(Low_Bound
(Drange
));
3155 Check_Non_Static_Context
(High_Bound
(Drange
));
3158 -- A slice of the form A (subtype), when the subtype is the index of
3159 -- the type of A, is redundant, the slice can be replaced with A, and
3160 -- this is worth a warning.
3162 if Is_Entity_Name
(Prefix
(N
)) then
3164 E
: constant Entity_Id
:= Entity
(Prefix
(N
));
3165 T
: constant Entity_Id
:= Etype
(E
);
3167 if Ekind
(E
) = E_Constant
3168 and then Is_Array_Type
(T
)
3169 and then Is_Entity_Name
(Drange
)
3171 if Is_Entity_Name
(Original_Node
(First_Index
(T
)))
3172 and then Entity
(Original_Node
(First_Index
(T
)))
3175 if Warn_On_Redundant_Constructs
then
3176 Error_Msg_N
("redundant slice denotes whole array?", N
);
3179 -- The following might be a useful optimization????
3181 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3188 -------------------------
3189 -- Eval_String_Literal --
3190 -------------------------
3192 procedure Eval_String_Literal
(N
: Node_Id
) is
3193 Typ
: constant Entity_Id
:= Etype
(N
);
3194 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
3200 -- Nothing to do if error type (handles cases like default expressions
3201 -- or generics where we have not yet fully resolved the type).
3203 if Bas
= Any_Type
or else Bas
= Any_String
then
3207 -- String literals are static if the subtype is static (RM 4.9(2)), so
3208 -- reset the static expression flag (it was set unconditionally in
3209 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3210 -- the subtype is static by looking at the lower bound.
3212 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3213 if not Is_OK_Static_Expression
(String_Literal_Low_Bound
(Typ
)) then
3214 Set_Is_Static_Expression
(N
, False);
3218 -- Here if Etype of string literal is normal Etype (not yet possible,
3219 -- but may be possible in future).
3221 elsif not Is_OK_Static_Expression
3222 (Type_Low_Bound
(Etype
(First_Index
(Typ
))))
3224 Set_Is_Static_Expression
(N
, False);
3228 -- If original node was a type conversion, then result if non-static
3230 if Nkind
(Original_Node
(N
)) = N_Type_Conversion
then
3231 Set_Is_Static_Expression
(N
, False);
3235 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3236 -- if its bounds are outside the index base type and this index type is
3237 -- static. This can happen in only two ways. Either the string literal
3238 -- is too long, or it is null, and the lower bound is type'First. In
3239 -- either case it is the upper bound that is out of range of the index
3242 if Ada_Version
>= Ada_95
then
3243 if Root_Type
(Bas
) = Standard_String
3245 Root_Type
(Bas
) = Standard_Wide_String
3247 Xtp
:= Standard_Positive
;
3249 Xtp
:= Etype
(First_Index
(Bas
));
3252 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3253 Lo
:= String_Literal_Low_Bound
(Typ
);
3255 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Typ
)));
3258 Len
:= String_Length
(Strval
(N
));
3260 if UI_From_Int
(Len
) > String_Type_Len
(Bas
) then
3261 Apply_Compile_Time_Constraint_Error
3262 (N
, "string literal too long for}", CE_Length_Check_Failed
,
3264 Typ
=> First_Subtype
(Bas
));
3267 and then not Is_Generic_Type
(Xtp
)
3269 Expr_Value
(Lo
) = Expr_Value
(Type_Low_Bound
(Base_Type
(Xtp
)))
3271 Apply_Compile_Time_Constraint_Error
3272 (N
, "null string literal not allowed for}",
3273 CE_Length_Check_Failed
,
3275 Typ
=> First_Subtype
(Bas
));
3278 end Eval_String_Literal
;
3280 --------------------------
3281 -- Eval_Type_Conversion --
3282 --------------------------
3284 -- A type conversion is potentially static if its subtype mark is for a
3285 -- static scalar subtype, and its operand expression is potentially static
3288 procedure Eval_Type_Conversion
(N
: Node_Id
) is
3289 Operand
: constant Node_Id
:= Expression
(N
);
3290 Source_Type
: constant Entity_Id
:= Etype
(Operand
);
3291 Target_Type
: constant Entity_Id
:= Etype
(N
);
3296 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean;
3297 -- Returns true if type T is an integer type, or if it is a fixed-point
3298 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3299 -- on the conversion node).
3301 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean;
3302 -- Returns true if type T is a floating-point type, or if it is a
3303 -- fixed-point type that is not to be treated as an integer (i.e. the
3304 -- flag Conversion_OK is not set on the conversion node).
3306 ------------------------------
3307 -- To_Be_Treated_As_Integer --
3308 ------------------------------
3310 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean is
3314 or else (Is_Fixed_Point_Type
(T
) and then Conversion_OK
(N
));
3315 end To_Be_Treated_As_Integer
;
3317 ---------------------------
3318 -- To_Be_Treated_As_Real --
3319 ---------------------------
3321 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean is
3324 Is_Floating_Point_Type
(T
)
3325 or else (Is_Fixed_Point_Type
(T
) and then not Conversion_OK
(N
));
3326 end To_Be_Treated_As_Real
;
3328 -- Start of processing for Eval_Type_Conversion
3331 -- Cannot fold if target type is non-static or if semantic error
3333 if not Is_Static_Subtype
(Target_Type
) then
3334 Check_Non_Static_Context
(Operand
);
3337 elsif Error_Posted
(N
) then
3341 -- If not foldable we are done
3343 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3348 -- Don't try fold if target type has constraint error bounds
3350 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3351 Set_Raises_Constraint_Error
(N
);
3355 -- Remaining processing depends on operand types. Note that in the
3356 -- following type test, fixed-point counts as real unless the flag
3357 -- Conversion_OK is set, in which case it counts as integer.
3359 -- Fold conversion, case of string type. The result is not static
3361 if Is_String_Type
(Target_Type
) then
3362 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Static
=> False);
3366 -- Fold conversion, case of integer target type
3368 elsif To_Be_Treated_As_Integer
(Target_Type
) then
3373 -- Integer to integer conversion
3375 if To_Be_Treated_As_Integer
(Source_Type
) then
3376 Result
:= Expr_Value
(Operand
);
3378 -- Real to integer conversion
3381 Result
:= UR_To_Uint
(Expr_Value_R
(Operand
));
3384 -- If fixed-point type (Conversion_OK must be set), then the
3385 -- result is logically an integer, but we must replace the
3386 -- conversion with the corresponding real literal, since the
3387 -- type from a semantic point of view is still fixed-point.
3389 if Is_Fixed_Point_Type
(Target_Type
) then
3391 (N
, UR_From_Uint
(Result
) * Small_Value
(Target_Type
), Stat
);
3393 -- Otherwise result is integer literal
3396 Fold_Uint
(N
, Result
, Stat
);
3400 -- Fold conversion, case of real target type
3402 elsif To_Be_Treated_As_Real
(Target_Type
) then
3407 if To_Be_Treated_As_Real
(Source_Type
) then
3408 Result
:= Expr_Value_R
(Operand
);
3410 Result
:= UR_From_Uint
(Expr_Value
(Operand
));
3413 Fold_Ureal
(N
, Result
, Stat
);
3416 -- Enumeration types
3419 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3422 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3426 end Eval_Type_Conversion
;
3432 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3433 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3435 procedure Eval_Unary_Op
(N
: Node_Id
) is
3436 Right
: constant Node_Id
:= Right_Opnd
(N
);
3437 Otype
: Entity_Id
:= Empty
;
3442 -- If not foldable we are done
3444 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
3450 if Etype
(Right
) = Universal_Integer
3452 Etype
(Right
) = Universal_Real
3454 Otype
:= Find_Universal_Operator_Type
(N
);
3457 -- Fold for integer case
3459 if Is_Integer_Type
(Etype
(N
)) then
3461 Rint
: constant Uint
:= Expr_Value
(Right
);
3465 -- In the case of modular unary plus and abs there is no need
3466 -- to adjust the result of the operation since if the original
3467 -- operand was in bounds the result will be in the bounds of the
3468 -- modular type. However, in the case of modular unary minus the
3469 -- result may go out of the bounds of the modular type and needs
3472 if Nkind
(N
) = N_Op_Plus
then
3475 elsif Nkind
(N
) = N_Op_Minus
then
3476 if Is_Modular_Integer_Type
(Etype
(N
)) then
3477 Result
:= (-Rint
) mod Modulus
(Etype
(N
));
3483 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
3487 Fold_Uint
(N
, Result
, Stat
);
3490 -- Fold for real case
3492 elsif Is_Real_Type
(Etype
(N
)) then
3494 Rreal
: constant Ureal
:= Expr_Value_R
(Right
);
3498 if Nkind
(N
) = N_Op_Plus
then
3501 elsif Nkind
(N
) = N_Op_Minus
then
3502 Result
:= UR_Negate
(Rreal
);
3505 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
3506 Result
:= abs Rreal
;
3509 Fold_Ureal
(N
, Result
, Stat
);
3513 -- If the operator was resolved to a specific type, make sure that type
3514 -- is frozen even if the expression is folded into a literal (which has
3515 -- a universal type).
3517 if Present
(Otype
) then
3518 Freeze_Before
(N
, Otype
);
3522 -------------------------------
3523 -- Eval_Unchecked_Conversion --
3524 -------------------------------
3526 -- Unchecked conversions can never be static, so the only required
3527 -- processing is to check for a non-static context for the operand.
3529 procedure Eval_Unchecked_Conversion
(N
: Node_Id
) is
3531 Check_Non_Static_Context
(Expression
(N
));
3532 end Eval_Unchecked_Conversion
;
3534 --------------------
3535 -- Expr_Rep_Value --
3536 --------------------
3538 function Expr_Rep_Value
(N
: Node_Id
) return Uint
is
3539 Kind
: constant Node_Kind
:= Nkind
(N
);
3543 if Is_Entity_Name
(N
) then
3546 -- An enumeration literal that was either in the source or created
3547 -- as a result of static evaluation.
3549 if Ekind
(Ent
) = E_Enumeration_Literal
then
3550 return Enumeration_Rep
(Ent
);
3552 -- A user defined static constant
3555 pragma Assert
(Ekind
(Ent
) = E_Constant
);
3556 return Expr_Rep_Value
(Constant_Value
(Ent
));
3559 -- An integer literal that was either in the source or created as a
3560 -- result of static evaluation.
3562 elsif Kind
= N_Integer_Literal
then
3565 -- A real literal for a fixed-point type. This must be the fixed-point
3566 -- case, either the literal is of a fixed-point type, or it is a bound
3567 -- of a fixed-point type, with type universal real. In either case we
3568 -- obtain the desired value from Corresponding_Integer_Value.
3570 elsif Kind
= N_Real_Literal
then
3571 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
3572 return Corresponding_Integer_Value
(N
);
3574 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3576 elsif Kind
= N_Attribute_Reference
3577 and then Attribute_Name
(N
) = Name_Null_Parameter
3581 -- Otherwise must be character literal
3584 pragma Assert
(Kind
= N_Character_Literal
);
3587 -- Since Character literals of type Standard.Character don't have any
3588 -- defining character literals built for them, they do not have their
3589 -- Entity set, so just use their Char code. Otherwise for user-
3590 -- defined character literals use their Pos value as usual which is
3591 -- the same as the Rep value.
3594 return Char_Literal_Value
(N
);
3596 return Enumeration_Rep
(Ent
);
3605 function Expr_Value
(N
: Node_Id
) return Uint
is
3606 Kind
: constant Node_Kind
:= Nkind
(N
);
3607 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(N
) mod CV_Cache_Size
);
3612 -- If already in cache, then we know it's compile time known and we can
3613 -- return the value that was previously stored in the cache since
3614 -- compile time known values cannot change.
3616 if CV_Ent
.N
= N
then
3620 -- Otherwise proceed to test value
3622 if Is_Entity_Name
(N
) then
3625 -- An enumeration literal that was either in the source or created as
3626 -- a result of static evaluation.
3628 if Ekind
(Ent
) = E_Enumeration_Literal
then
3629 Val
:= Enumeration_Pos
(Ent
);
3631 -- A user defined static constant
3634 pragma Assert
(Ekind
(Ent
) = E_Constant
);
3635 Val
:= Expr_Value
(Constant_Value
(Ent
));
3638 -- An integer literal that was either in the source or created as a
3639 -- result of static evaluation.
3641 elsif Kind
= N_Integer_Literal
then
3644 -- A real literal for a fixed-point type. This must be the fixed-point
3645 -- case, either the literal is of a fixed-point type, or it is a bound
3646 -- of a fixed-point type, with type universal real. In either case we
3647 -- obtain the desired value from Corresponding_Integer_Value.
3649 elsif Kind
= N_Real_Literal
then
3651 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
3652 Val
:= Corresponding_Integer_Value
(N
);
3654 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3656 elsif Kind
= N_Attribute_Reference
3657 and then Attribute_Name
(N
) = Name_Null_Parameter
3661 -- Otherwise must be character literal
3664 pragma Assert
(Kind
= N_Character_Literal
);
3667 -- Since Character literals of type Standard.Character don't
3668 -- have any defining character literals built for them, they
3669 -- do not have their Entity set, so just use their Char
3670 -- code. Otherwise for user-defined character literals use
3671 -- their Pos value as usual.
3674 Val
:= Char_Literal_Value
(N
);
3676 Val
:= Enumeration_Pos
(Ent
);
3680 -- Come here with Val set to value to be returned, set cache
3691 function Expr_Value_E
(N
: Node_Id
) return Entity_Id
is
3692 Ent
: constant Entity_Id
:= Entity
(N
);
3695 if Ekind
(Ent
) = E_Enumeration_Literal
then
3698 pragma Assert
(Ekind
(Ent
) = E_Constant
);
3699 return Expr_Value_E
(Constant_Value
(Ent
));
3707 function Expr_Value_R
(N
: Node_Id
) return Ureal
is
3708 Kind
: constant Node_Kind
:= Nkind
(N
);
3713 if Kind
= N_Real_Literal
then
3716 elsif Kind
= N_Identifier
or else Kind
= N_Expanded_Name
then
3718 pragma Assert
(Ekind
(Ent
) = E_Constant
);
3719 return Expr_Value_R
(Constant_Value
(Ent
));
3721 elsif Kind
= N_Integer_Literal
then
3722 return UR_From_Uint
(Expr_Value
(N
));
3724 -- Strange case of VAX literals, which are at this stage transformed
3725 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
3726 -- Exp_Vfpt for further details.
3728 elsif Vax_Float
(Etype
(N
))
3729 and then Nkind
(N
) = N_Unchecked_Type_Conversion
3731 Expr
:= Expression
(N
);
3733 if Nkind
(Expr
) = N_Function_Call
3734 and then Present
(Parameter_Associations
(Expr
))
3736 Expr
:= First
(Parameter_Associations
(Expr
));
3738 if Nkind
(Expr
) = N_Real_Literal
then
3739 return Realval
(Expr
);
3743 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3745 elsif Kind
= N_Attribute_Reference
3746 and then Attribute_Name
(N
) = Name_Null_Parameter
3751 -- If we fall through, we have a node that cannot be interpreted as a
3752 -- compile time constant. That is definitely an error.
3754 raise Program_Error
;
3761 function Expr_Value_S
(N
: Node_Id
) return Node_Id
is
3763 if Nkind
(N
) = N_String_Literal
then
3766 pragma Assert
(Ekind
(Entity
(N
)) = E_Constant
);
3767 return Expr_Value_S
(Constant_Value
(Entity
(N
)));
3771 ----------------------------------
3772 -- Find_Universal_Operator_Type --
3773 ----------------------------------
3775 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
is
3776 PN
: constant Node_Id
:= Parent
(N
);
3777 Call
: constant Node_Id
:= Original_Node
(N
);
3778 Is_Int
: constant Boolean := Is_Integer_Type
(Etype
(N
));
3780 Is_Fix
: constant Boolean :=
3781 Nkind
(N
) in N_Binary_Op
3782 and then Nkind
(Right_Opnd
(N
)) /= Nkind
(Left_Opnd
(N
));
3783 -- A mixed-mode operation in this context indicates the presence of
3784 -- fixed-point type in the designated package.
3786 Is_Relational
: constant Boolean := Etype
(N
) = Standard_Boolean
;
3787 -- Case where N is a relational (or membership) operator (else it is an
3790 In_Membership
: constant Boolean :=
3791 Nkind
(PN
) in N_Membership_Test
3793 Nkind
(Right_Opnd
(PN
)) = N_Range
3795 Is_Universal_Numeric_Type
(Etype
(Left_Opnd
(PN
)))
3797 Is_Universal_Numeric_Type
3798 (Etype
(Low_Bound
(Right_Opnd
(PN
))))
3800 Is_Universal_Numeric_Type
3801 (Etype
(High_Bound
(Right_Opnd
(PN
))));
3802 -- Case where N is part of a membership test with a universal range
3806 Typ1
: Entity_Id
:= Empty
;
3809 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean;
3810 -- Check whether one operand is a mixed-mode operation that requires the
3811 -- presence of a fixed-point type. Given that all operands are universal
3812 -- and have been constant-folded, retrieve the original function call.
3814 ---------------------------
3815 -- Is_Mixed_Mode_Operand --
3816 ---------------------------
3818 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean is
3819 Onod
: constant Node_Id
:= Original_Node
(Op
);
3821 return Nkind
(Onod
) = N_Function_Call
3822 and then Present
(Next_Actual
(First_Actual
(Onod
)))
3823 and then Etype
(First_Actual
(Onod
)) /=
3824 Etype
(Next_Actual
(First_Actual
(Onod
)));
3825 end Is_Mixed_Mode_Operand
;
3827 -- Start of processing for Find_Universal_Operator_Type
3830 if Nkind
(Call
) /= N_Function_Call
3831 or else Nkind
(Name
(Call
)) /= N_Expanded_Name
3835 -- There are several cases where the context does not imply the type of
3837 -- - the universal expression appears in a type conversion;
3838 -- - the expression is a relational operator applied to universal
3840 -- - the expression is a membership test with a universal operand
3841 -- and a range with universal bounds.
3843 elsif Nkind
(Parent
(N
)) = N_Type_Conversion
3844 or else Is_Relational
3845 or else In_Membership
3847 Pack
:= Entity
(Prefix
(Name
(Call
)));
3849 -- If the prefix is a package declared elsewhere, iterate over its
3850 -- visible entities, otherwise iterate over all declarations in the
3851 -- designated scope.
3853 if Ekind
(Pack
) = E_Package
3854 and then not In_Open_Scopes
(Pack
)
3856 Priv_E
:= First_Private_Entity
(Pack
);
3862 E
:= First_Entity
(Pack
);
3863 while Present
(E
) and then E
/= Priv_E
loop
3864 if Is_Numeric_Type
(E
)
3865 and then Nkind
(Parent
(E
)) /= N_Subtype_Declaration
3866 and then Comes_From_Source
(E
)
3867 and then Is_Integer_Type
(E
) = Is_Int
3869 (Nkind
(N
) in N_Unary_Op
3870 or else Is_Relational
3871 or else Is_Fixed_Point_Type
(E
) = Is_Fix
)
3876 -- Before emitting an error, check for the presence of a
3877 -- mixed-mode operation that specifies a fixed point type.
3881 (Is_Mixed_Mode_Operand
(Left_Opnd
(N
))
3882 or else Is_Mixed_Mode_Operand
(Right_Opnd
(N
)))
3883 and then Is_Fixed_Point_Type
(E
) /= Is_Fixed_Point_Type
(Typ1
)
3886 if Is_Fixed_Point_Type
(E
) then
3891 -- More than one type of the proper class declared in P
3893 Error_Msg_N
("ambiguous operation", N
);
3894 Error_Msg_Sloc
:= Sloc
(Typ1
);
3895 Error_Msg_N
("\possible interpretation (inherited)#", N
);
3896 Error_Msg_Sloc
:= Sloc
(E
);
3897 Error_Msg_N
("\possible interpretation (inherited)#", N
);
3907 end Find_Universal_Operator_Type
;
3909 --------------------------
3910 -- Flag_Non_Static_Expr --
3911 --------------------------
3913 procedure Flag_Non_Static_Expr
(Msg
: String; Expr
: Node_Id
) is
3915 if Error_Posted
(Expr
) and then not All_Errors_Mode
then
3918 Error_Msg_F
(Msg
, Expr
);
3919 Why_Not_Static
(Expr
);
3921 end Flag_Non_Static_Expr
;
3927 procedure Fold_Str
(N
: Node_Id
; Val
: String_Id
; Static
: Boolean) is
3928 Loc
: constant Source_Ptr
:= Sloc
(N
);
3929 Typ
: constant Entity_Id
:= Etype
(N
);
3932 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Val
));
3934 -- We now have the literal with the right value, both the actual type
3935 -- and the expected type of this literal are taken from the expression
3936 -- that was evaluated.
3939 Set_Is_Static_Expression
(N
, Static
);
3948 procedure Fold_Uint
(N
: Node_Id
; Val
: Uint
; Static
: Boolean) is
3949 Loc
: constant Source_Ptr
:= Sloc
(N
);
3950 Typ
: Entity_Id
:= Etype
(N
);
3954 -- If we are folding a named number, retain the entity in the literal,
3957 if Is_Entity_Name
(N
)
3958 and then Ekind
(Entity
(N
)) = E_Named_Integer
3965 if Is_Private_Type
(Typ
) then
3966 Typ
:= Full_View
(Typ
);
3969 -- For a result of type integer, substitute an N_Integer_Literal node
3970 -- for the result of the compile time evaluation of the expression.
3971 -- For ASIS use, set a link to the original named number when not in
3972 -- a generic context.
3974 if Is_Integer_Type
(Typ
) then
3975 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
3977 Set_Original_Entity
(N
, Ent
);
3979 -- Otherwise we have an enumeration type, and we substitute either
3980 -- an N_Identifier or N_Character_Literal to represent the enumeration
3981 -- literal corresponding to the given value, which must always be in
3982 -- range, because appropriate tests have already been made for this.
3984 else pragma Assert
(Is_Enumeration_Type
(Typ
));
3985 Rewrite
(N
, Get_Enum_Lit_From_Pos
(Etype
(N
), Val
, Loc
));
3988 -- We now have the literal with the right value, both the actual type
3989 -- and the expected type of this literal are taken from the expression
3990 -- that was evaluated.
3993 Set_Is_Static_Expression
(N
, Static
);
4002 procedure Fold_Ureal
(N
: Node_Id
; Val
: Ureal
; Static
: Boolean) is
4003 Loc
: constant Source_Ptr
:= Sloc
(N
);
4004 Typ
: constant Entity_Id
:= Etype
(N
);
4008 -- If we are folding a named number, retain the entity in the literal,
4011 if Is_Entity_Name
(N
)
4012 and then Ekind
(Entity
(N
)) = E_Named_Real
4019 Rewrite
(N
, Make_Real_Literal
(Loc
, Realval
=> Val
));
4021 -- Set link to original named number, for ASIS use
4023 Set_Original_Entity
(N
, Ent
);
4025 -- Both the actual and expected type comes from the original expression
4028 Set_Is_Static_Expression
(N
, Static
);
4037 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
is
4041 for J
in 0 .. B
'Last loop
4047 if Non_Binary_Modulus
(T
) then
4048 V
:= V
mod Modulus
(T
);
4054 --------------------
4055 -- Get_String_Val --
4056 --------------------
4058 function Get_String_Val
(N
: Node_Id
) return Node_Id
is
4060 if Nkind
(N
) = N_String_Literal
then
4063 elsif Nkind
(N
) = N_Character_Literal
then
4067 pragma Assert
(Is_Entity_Name
(N
));
4068 return Get_String_Val
(Constant_Value
(Entity
(N
)));
4076 procedure Initialize
is
4078 CV_Cache
:= (others => (Node_High_Bound
, Uint_0
));
4081 --------------------
4082 -- In_Subrange_Of --
4083 --------------------
4085 function In_Subrange_Of
4088 Fixed_Int
: Boolean := False) return Boolean
4097 if T1
= T2
or else Is_Subtype_Of
(T1
, T2
) then
4100 -- Never in range if both types are not scalar. Don't know if this can
4101 -- actually happen, but just in case.
4103 elsif not Is_Scalar_Type
(T1
) or else not Is_Scalar_Type
(T1
) then
4106 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4107 -- definitely not compatible with T2.
4109 elsif Is_Floating_Point_Type
(T1
)
4110 and then Has_Infinities
(T1
)
4111 and then Is_Floating_Point_Type
(T2
)
4112 and then not Has_Infinities
(T2
)
4117 L1
:= Type_Low_Bound
(T1
);
4118 H1
:= Type_High_Bound
(T1
);
4120 L2
:= Type_Low_Bound
(T2
);
4121 H2
:= Type_High_Bound
(T2
);
4123 -- Check bounds to see if comparison possible at compile time
4125 if Compile_Time_Compare
(L1
, L2
, Assume_Valid
=> True) in Compare_GE
4127 Compile_Time_Compare
(H1
, H2
, Assume_Valid
=> True) in Compare_LE
4132 -- If bounds not comparable at compile time, then the bounds of T2
4133 -- must be compile time known or we cannot answer the query.
4135 if not Compile_Time_Known_Value
(L2
)
4136 or else not Compile_Time_Known_Value
(H2
)
4141 -- If the bounds of T1 are know at compile time then use these
4142 -- ones, otherwise use the bounds of the base type (which are of
4143 -- course always static).
4145 if not Compile_Time_Known_Value
(L1
) then
4146 L1
:= Type_Low_Bound
(Base_Type
(T1
));
4149 if not Compile_Time_Known_Value
(H1
) then
4150 H1
:= Type_High_Bound
(Base_Type
(T1
));
4153 -- Fixed point types should be considered as such only if
4154 -- flag Fixed_Int is set to False.
4156 if Is_Floating_Point_Type
(T1
) or else Is_Floating_Point_Type
(T2
)
4157 or else (Is_Fixed_Point_Type
(T1
) and then not Fixed_Int
)
4158 or else (Is_Fixed_Point_Type
(T2
) and then not Fixed_Int
)
4161 Expr_Value_R
(L2
) <= Expr_Value_R
(L1
)
4163 Expr_Value_R
(H2
) >= Expr_Value_R
(H1
);
4167 Expr_Value
(L2
) <= Expr_Value
(L1
)
4169 Expr_Value
(H2
) >= Expr_Value
(H1
);
4174 -- If any exception occurs, it means that we have some bug in the compiler
4175 -- possibly triggered by a previous error, or by some unforeseen peculiar
4176 -- occurrence. However, this is only an optimization attempt, so there is
4177 -- really no point in crashing the compiler. Instead we just decide, too
4178 -- bad, we can't figure out the answer in this case after all.
4183 -- Debug flag K disables this behavior (useful for debugging)
4185 if Debug_Flag_K
then
4196 function Is_In_Range
4199 Assume_Valid
: Boolean := False;
4200 Fixed_Int
: Boolean := False;
4201 Int_Real
: Boolean := False) return Boolean
4204 return Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
)
4212 function Is_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
4213 Typ
: constant Entity_Id
:= Etype
(Lo
);
4216 if not Compile_Time_Known_Value
(Lo
)
4217 or else not Compile_Time_Known_Value
(Hi
)
4222 if Is_Discrete_Type
(Typ
) then
4223 return Expr_Value
(Lo
) > Expr_Value
(Hi
);
4226 pragma Assert
(Is_Real_Type
(Typ
));
4227 return Expr_Value_R
(Lo
) > Expr_Value_R
(Hi
);
4231 -----------------------------
4232 -- Is_OK_Static_Expression --
4233 -----------------------------
4235 function Is_OK_Static_Expression
(N
: Node_Id
) return Boolean is
4237 return Is_Static_Expression
(N
)
4238 and then not Raises_Constraint_Error
(N
);
4239 end Is_OK_Static_Expression
;
4241 ------------------------
4242 -- Is_OK_Static_Range --
4243 ------------------------
4245 -- A static range is a range whose bounds are static expressions, or a
4246 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4247 -- We have already converted range attribute references, so we get the
4248 -- "or" part of this rule without needing a special test.
4250 function Is_OK_Static_Range
(N
: Node_Id
) return Boolean is
4252 return Is_OK_Static_Expression
(Low_Bound
(N
))
4253 and then Is_OK_Static_Expression
(High_Bound
(N
));
4254 end Is_OK_Static_Range
;
4256 --------------------------
4257 -- Is_OK_Static_Subtype --
4258 --------------------------
4260 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4261 -- neither bound raises constraint error when evaluated.
4263 function Is_OK_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4264 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4265 Anc_Subt
: Entity_Id
;
4268 -- First a quick check on the non static subtype flag. As described
4269 -- in further detail in Einfo, this flag is not decisive in all cases,
4270 -- but if it is set, then the subtype is definitely non-static.
4272 if Is_Non_Static_Subtype
(Typ
) then
4276 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4278 if Anc_Subt
= Empty
then
4282 if Is_Generic_Type
(Root_Type
(Base_T
))
4283 or else Is_Generic_Actual_Type
(Base_T
)
4289 elsif Is_String_Type
(Typ
) then
4291 Ekind
(Typ
) = E_String_Literal_Subtype
4293 (Is_OK_Static_Subtype
(Component_Type
(Typ
))
4294 and then Is_OK_Static_Subtype
(Etype
(First_Index
(Typ
))));
4298 elsif Is_Scalar_Type
(Typ
) then
4299 if Base_T
= Typ
then
4303 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4304 -- Get_Type_{Low,High}_Bound.
4306 return Is_OK_Static_Subtype
(Anc_Subt
)
4307 and then Is_OK_Static_Expression
(Type_Low_Bound
(Typ
))
4308 and then Is_OK_Static_Expression
(Type_High_Bound
(Typ
));
4311 -- Types other than string and scalar types are never static
4316 end Is_OK_Static_Subtype
;
4318 ---------------------
4319 -- Is_Out_Of_Range --
4320 ---------------------
4322 function Is_Out_Of_Range
4325 Assume_Valid
: Boolean := False;
4326 Fixed_Int
: Boolean := False;
4327 Int_Real
: Boolean := False) return Boolean
4330 return Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
)
4332 end Is_Out_Of_Range
;
4334 ---------------------
4335 -- Is_Static_Range --
4336 ---------------------
4338 -- A static range is a range whose bounds are static expressions, or a
4339 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4340 -- We have already converted range attribute references, so we get the
4341 -- "or" part of this rule without needing a special test.
4343 function Is_Static_Range
(N
: Node_Id
) return Boolean is
4345 return Is_Static_Expression
(Low_Bound
(N
))
4346 and then Is_Static_Expression
(High_Bound
(N
));
4347 end Is_Static_Range
;
4349 -----------------------
4350 -- Is_Static_Subtype --
4351 -----------------------
4353 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4355 function Is_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4356 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4357 Anc_Subt
: Entity_Id
;
4360 -- First a quick check on the non static subtype flag. As described
4361 -- in further detail in Einfo, this flag is not decisive in all cases,
4362 -- but if it is set, then the subtype is definitely non-static.
4364 if Is_Non_Static_Subtype
(Typ
) then
4368 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4370 if Anc_Subt
= Empty
then
4374 if Is_Generic_Type
(Root_Type
(Base_T
))
4375 or else Is_Generic_Actual_Type
(Base_T
)
4381 elsif Is_String_Type
(Typ
) then
4383 Ekind
(Typ
) = E_String_Literal_Subtype
4385 (Is_Static_Subtype
(Component_Type
(Typ
))
4386 and then Is_Static_Subtype
(Etype
(First_Index
(Typ
))));
4390 elsif Is_Scalar_Type
(Typ
) then
4391 if Base_T
= Typ
then
4395 return Is_Static_Subtype
(Anc_Subt
)
4396 and then Is_Static_Expression
(Type_Low_Bound
(Typ
))
4397 and then Is_Static_Expression
(Type_High_Bound
(Typ
));
4400 -- Types other than string and scalar types are never static
4405 end Is_Static_Subtype
;
4407 --------------------
4408 -- Not_Null_Range --
4409 --------------------
4411 function Not_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
4412 Typ
: constant Entity_Id
:= Etype
(Lo
);
4415 if not Compile_Time_Known_Value
(Lo
)
4416 or else not Compile_Time_Known_Value
(Hi
)
4421 if Is_Discrete_Type
(Typ
) then
4422 return Expr_Value
(Lo
) <= Expr_Value
(Hi
);
4425 pragma Assert
(Is_Real_Type
(Typ
));
4427 return Expr_Value_R
(Lo
) <= Expr_Value_R
(Hi
);
4435 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean is
4437 -- We allow a maximum of 500,000 bits which seems a reasonable limit
4439 if Bits
< 500_000
then
4443 Error_Msg_N
("static value too large, capacity exceeded", N
);
4452 procedure Out_Of_Range
(N
: Node_Id
) is
4454 -- If we have the static expression case, then this is an illegality
4455 -- in Ada 95 mode, except that in an instance, we never generate an
4456 -- error (if the error is legitimate, it was already diagnosed in the
4457 -- template). The expression to compute the length of a packed array is
4458 -- attached to the array type itself, and deserves a separate message.
4460 if Is_Static_Expression
(N
)
4461 and then not In_Instance
4462 and then not In_Inlined_Body
4463 and then Ada_Version
>= Ada_95
4465 if Nkind
(Parent
(N
)) = N_Defining_Identifier
4466 and then Is_Array_Type
(Parent
(N
))
4467 and then Present
(Packed_Array_Type
(Parent
(N
)))
4468 and then Present
(First_Rep_Item
(Parent
(N
)))
4471 ("length of packed array must not exceed Integer''Last",
4472 First_Rep_Item
(Parent
(N
)));
4473 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Uint_1
));
4476 Apply_Compile_Time_Constraint_Error
4477 (N
, "value not in range of}", CE_Range_Check_Failed
);
4480 -- Here we generate a warning for the Ada 83 case, or when we are in an
4481 -- instance, or when we have a non-static expression case.
4484 Apply_Compile_Time_Constraint_Error
4485 (N
, "value not in range of}?", CE_Range_Check_Failed
);
4489 -------------------------
4490 -- Rewrite_In_Raise_CE --
4491 -------------------------
4493 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
) is
4494 Typ
: constant Entity_Id
:= Etype
(N
);
4497 -- If we want to raise CE in the condition of a N_Raise_CE node
4498 -- we may as well get rid of the condition.
4500 if Present
(Parent
(N
))
4501 and then Nkind
(Parent
(N
)) = N_Raise_Constraint_Error
4503 Set_Condition
(Parent
(N
), Empty
);
4505 -- If the expression raising CE is a N_Raise_CE node, we can use that
4506 -- one. We just preserve the type of the context.
4508 elsif Nkind
(Exp
) = N_Raise_Constraint_Error
then
4512 -- Else build an explcit N_Raise_CE
4516 Make_Raise_Constraint_Error
(Sloc
(Exp
),
4517 Reason
=> CE_Range_Check_Failed
));
4518 Set_Raises_Constraint_Error
(N
);
4521 end Rewrite_In_Raise_CE
;
4523 ---------------------
4524 -- String_Type_Len --
4525 ---------------------
4527 function String_Type_Len
(Stype
: Entity_Id
) return Uint
is
4528 NT
: constant Entity_Id
:= Etype
(First_Index
(Stype
));
4532 if Is_OK_Static_Subtype
(NT
) then
4535 T
:= Base_Type
(NT
);
4538 return Expr_Value
(Type_High_Bound
(T
)) -
4539 Expr_Value
(Type_Low_Bound
(T
)) + 1;
4540 end String_Type_Len
;
4542 ------------------------------------
4543 -- Subtypes_Statically_Compatible --
4544 ------------------------------------
4546 function Subtypes_Statically_Compatible
4548 T2
: Entity_Id
) return Boolean
4551 if Is_Scalar_Type
(T1
) then
4553 -- Definitely compatible if we match
4555 if Subtypes_Statically_Match
(T1
, T2
) then
4558 -- If either subtype is nonstatic then they're not compatible
4560 elsif not Is_Static_Subtype
(T1
)
4561 or else not Is_Static_Subtype
(T2
)
4565 -- If either type has constraint error bounds, then consider that
4566 -- they match to avoid junk cascaded errors here.
4568 elsif not Is_OK_Static_Subtype
(T1
)
4569 or else not Is_OK_Static_Subtype
(T2
)
4573 -- Base types must match, but we don't check that (should
4574 -- we???) but we do at least check that both types are
4575 -- real, or both types are not real.
4577 elsif Is_Real_Type
(T1
) /= Is_Real_Type
(T2
) then
4580 -- Here we check the bounds
4584 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
4585 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
4586 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
4587 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
4590 if Is_Real_Type
(T1
) then
4592 (Expr_Value_R
(LB1
) > Expr_Value_R
(HB1
))
4594 (Expr_Value_R
(LB2
) <= Expr_Value_R
(LB1
)
4596 Expr_Value_R
(HB1
) <= Expr_Value_R
(HB2
));
4600 (Expr_Value
(LB1
) > Expr_Value
(HB1
))
4602 (Expr_Value
(LB2
) <= Expr_Value
(LB1
)
4604 Expr_Value
(HB1
) <= Expr_Value
(HB2
));
4609 elsif Is_Access_Type
(T1
) then
4610 return not Is_Constrained
(T2
)
4611 or else Subtypes_Statically_Match
4612 (Designated_Type
(T1
), Designated_Type
(T2
));
4615 return (Is_Composite_Type
(T1
) and then not Is_Constrained
(T2
))
4616 or else Subtypes_Statically_Match
(T1
, T2
);
4618 end Subtypes_Statically_Compatible
;
4620 -------------------------------
4621 -- Subtypes_Statically_Match --
4622 -------------------------------
4624 -- Subtypes statically match if they have statically matching constraints
4625 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4626 -- they are the same identical constraint, or if they are static and the
4627 -- values match (RM 4.9.1(1)).
4629 function Subtypes_Statically_Match
(T1
, T2
: Entity_Id
) return Boolean is
4631 -- A type always statically matches itself
4638 elsif Is_Scalar_Type
(T1
) then
4640 -- Base types must be the same
4642 if Base_Type
(T1
) /= Base_Type
(T2
) then
4646 -- A constrained numeric subtype never matches an unconstrained
4647 -- subtype, i.e. both types must be constrained or unconstrained.
4649 -- To understand the requirement for this test, see RM 4.9.1(1).
4650 -- As is made clear in RM 3.5.4(11), type Integer, for example is
4651 -- a constrained subtype with constraint bounds matching the bounds
4652 -- of its corresponding unconstrained base type. In this situation,
4653 -- Integer and Integer'Base do not statically match, even though
4654 -- they have the same bounds.
4656 -- We only apply this test to types in Standard and types that appear
4657 -- in user programs. That way, we do not have to be too careful about
4658 -- setting Is_Constrained right for Itypes.
4660 if Is_Numeric_Type
(T1
)
4661 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
4662 and then (Scope
(T1
) = Standard_Standard
4663 or else Comes_From_Source
(T1
))
4664 and then (Scope
(T2
) = Standard_Standard
4665 or else Comes_From_Source
(T2
))
4669 -- A generic scalar type does not statically match its base type
4670 -- (AI-311). In this case we make sure that the formals, which are
4671 -- first subtypes of their bases, are constrained.
4673 elsif Is_Generic_Type
(T1
)
4674 and then Is_Generic_Type
(T2
)
4675 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
4680 -- If there was an error in either range, then just assume the types
4681 -- statically match to avoid further junk errors.
4683 if No
(Scalar_Range
(T1
)) or else No
(Scalar_Range
(T2
))
4684 or else Error_Posted
(Scalar_Range
(T1
))
4685 or else Error_Posted
(Scalar_Range
(T2
))
4690 -- Otherwise both types have bound that can be compared
4693 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
4694 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
4695 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
4696 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
4699 -- If the bounds are the same tree node, then match
4701 if LB1
= LB2
and then HB1
= HB2
then
4704 -- Otherwise bounds must be static and identical value
4707 if not Is_Static_Subtype
(T1
)
4708 or else not Is_Static_Subtype
(T2
)
4712 -- If either type has constraint error bounds, then say that
4713 -- they match to avoid junk cascaded errors here.
4715 elsif not Is_OK_Static_Subtype
(T1
)
4716 or else not Is_OK_Static_Subtype
(T2
)
4720 elsif Is_Real_Type
(T1
) then
4722 (Expr_Value_R
(LB1
) = Expr_Value_R
(LB2
))
4724 (Expr_Value_R
(HB1
) = Expr_Value_R
(HB2
));
4728 Expr_Value
(LB1
) = Expr_Value
(LB2
)
4730 Expr_Value
(HB1
) = Expr_Value
(HB2
);
4735 -- Type with discriminants
4737 elsif Has_Discriminants
(T1
) or else Has_Discriminants
(T2
) then
4739 -- Because of view exchanges in multiple instantiations, conformance
4740 -- checking might try to match a partial view of a type with no
4741 -- discriminants with a full view that has defaulted discriminants.
4742 -- In such a case, use the discriminant constraint of the full view,
4743 -- which must exist because we know that the two subtypes have the
4746 if Has_Discriminants
(T1
) /= Has_Discriminants
(T2
) then
4748 if Is_Private_Type
(T2
)
4749 and then Present
(Full_View
(T2
))
4750 and then Has_Discriminants
(Full_View
(T2
))
4752 return Subtypes_Statically_Match
(T1
, Full_View
(T2
));
4754 elsif Is_Private_Type
(T1
)
4755 and then Present
(Full_View
(T1
))
4756 and then Has_Discriminants
(Full_View
(T1
))
4758 return Subtypes_Statically_Match
(Full_View
(T1
), T2
);
4769 DL1
: constant Elist_Id
:= Discriminant_Constraint
(T1
);
4770 DL2
: constant Elist_Id
:= Discriminant_Constraint
(T2
);
4778 elsif Is_Constrained
(T1
) /= Is_Constrained
(T2
) then
4782 -- Now loop through the discriminant constraints
4784 -- Note: the guard here seems necessary, since it is possible at
4785 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
4787 if Present
(DL1
) and then Present
(DL2
) then
4788 DA1
:= First_Elmt
(DL1
);
4789 DA2
:= First_Elmt
(DL2
);
4790 while Present
(DA1
) loop
4792 Expr1
: constant Node_Id
:= Node
(DA1
);
4793 Expr2
: constant Node_Id
:= Node
(DA2
);
4796 if not Is_Static_Expression
(Expr1
)
4797 or else not Is_Static_Expression
(Expr2
)
4801 -- If either expression raised a constraint error,
4802 -- consider the expressions as matching, since this
4803 -- helps to prevent cascading errors.
4805 elsif Raises_Constraint_Error
(Expr1
)
4806 or else Raises_Constraint_Error
(Expr2
)
4810 elsif Expr_Value
(Expr1
) /= Expr_Value
(Expr2
) then
4823 -- A definite type does not match an indefinite or classwide type.
4824 -- However, a generic type with unknown discriminants may be
4825 -- instantiated with a type with no discriminants, and conformance
4826 -- checking on an inherited operation may compare the actual with the
4827 -- subtype that renames it in the instance.
4830 Has_Unknown_Discriminants
(T1
) /= Has_Unknown_Discriminants
(T2
)
4833 Is_Generic_Actual_Type
(T1
) or else Is_Generic_Actual_Type
(T2
);
4837 elsif Is_Array_Type
(T1
) then
4839 -- If either subtype is unconstrained then both must be, and if both
4840 -- are unconstrained then no further checking is neede.
4842 if not Is_Constrained
(T1
) or else not Is_Constrained
(T2
) then
4843 return not (Is_Constrained
(T1
) or else Is_Constrained
(T2
));
4846 -- Both subtypes are constrained, so check that the index subtypes
4847 -- statically match.
4850 Index1
: Node_Id
:= First_Index
(T1
);
4851 Index2
: Node_Id
:= First_Index
(T2
);
4854 while Present
(Index1
) loop
4856 Subtypes_Statically_Match
(Etype
(Index1
), Etype
(Index2
))
4861 Next_Index
(Index1
);
4862 Next_Index
(Index2
);
4868 elsif Is_Access_Type
(T1
) then
4869 if Can_Never_Be_Null
(T1
) /= Can_Never_Be_Null
(T2
) then
4872 elsif Ekind_In
(T1
, E_Access_Subprogram_Type
,
4873 E_Anonymous_Access_Subprogram_Type
)
4877 (Designated_Type
(T1
),
4878 Designated_Type
(T2
));
4881 Subtypes_Statically_Match
4882 (Designated_Type
(T1
),
4883 Designated_Type
(T2
))
4884 and then Is_Access_Constant
(T1
) = Is_Access_Constant
(T2
);
4887 -- All other types definitely match
4892 end Subtypes_Statically_Match
;
4898 function Test
(Cond
: Boolean) return Uint
is
4907 ---------------------------------
4908 -- Test_Expression_Is_Foldable --
4909 ---------------------------------
4913 procedure Test_Expression_Is_Foldable
4923 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
4927 -- If operand is Any_Type, just propagate to result and do not
4928 -- try to fold, this prevents cascaded errors.
4930 if Etype
(Op1
) = Any_Type
then
4931 Set_Etype
(N
, Any_Type
);
4934 -- If operand raises constraint error, then replace node N with the
4935 -- raise constraint error node, and we are obviously not foldable.
4936 -- Note that this replacement inherits the Is_Static_Expression flag
4937 -- from the operand.
4939 elsif Raises_Constraint_Error
(Op1
) then
4940 Rewrite_In_Raise_CE
(N
, Op1
);
4943 -- If the operand is not static, then the result is not static, and
4944 -- all we have to do is to check the operand since it is now known
4945 -- to appear in a non-static context.
4947 elsif not Is_Static_Expression
(Op1
) then
4948 Check_Non_Static_Context
(Op1
);
4949 Fold
:= Compile_Time_Known_Value
(Op1
);
4952 -- An expression of a formal modular type is not foldable because
4953 -- the modulus is unknown.
4955 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
4956 and then Is_Generic_Type
(Etype
(Op1
))
4958 Check_Non_Static_Context
(Op1
);
4961 -- Here we have the case of an operand whose type is OK, which is
4962 -- static, and which does not raise constraint error, we can fold.
4965 Set_Is_Static_Expression
(N
);
4969 end Test_Expression_Is_Foldable
;
4973 procedure Test_Expression_Is_Foldable
4980 Rstat
: constant Boolean := Is_Static_Expression
(Op1
)
4981 and then Is_Static_Expression
(Op2
);
4987 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
4991 -- If either operand is Any_Type, just propagate to result and
4992 -- do not try to fold, this prevents cascaded errors.
4994 if Etype
(Op1
) = Any_Type
or else Etype
(Op2
) = Any_Type
then
4995 Set_Etype
(N
, Any_Type
);
4998 -- If left operand raises constraint error, then replace node N with the
4999 -- Raise_Constraint_Error node, and we are obviously not foldable.
5000 -- Is_Static_Expression is set from the two operands in the normal way,
5001 -- and we check the right operand if it is in a non-static context.
5003 elsif Raises_Constraint_Error
(Op1
) then
5005 Check_Non_Static_Context
(Op2
);
5008 Rewrite_In_Raise_CE
(N
, Op1
);
5009 Set_Is_Static_Expression
(N
, Rstat
);
5012 -- Similar processing for the case of the right operand. Note that we
5013 -- don't use this routine for the short-circuit case, so we do not have
5014 -- to worry about that special case here.
5016 elsif Raises_Constraint_Error
(Op2
) then
5018 Check_Non_Static_Context
(Op1
);
5021 Rewrite_In_Raise_CE
(N
, Op2
);
5022 Set_Is_Static_Expression
(N
, Rstat
);
5025 -- Exclude expressions of a generic modular type, as above
5027 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
5028 and then Is_Generic_Type
(Etype
(Op1
))
5030 Check_Non_Static_Context
(Op1
);
5033 -- If result is not static, then check non-static contexts on operands
5034 -- since one of them may be static and the other one may not be static.
5036 elsif not Rstat
then
5037 Check_Non_Static_Context
(Op1
);
5038 Check_Non_Static_Context
(Op2
);
5039 Fold
:= Compile_Time_Known_Value
(Op1
)
5040 and then Compile_Time_Known_Value
(Op2
);
5043 -- Else result is static and foldable. Both operands are static, and
5044 -- neither raises constraint error, so we can definitely fold.
5047 Set_Is_Static_Expression
(N
);
5052 end Test_Expression_Is_Foldable
;
5058 function Test_In_Range
5061 Assume_Valid
: Boolean;
5062 Fixed_Int
: Boolean;
5063 Int_Real
: Boolean) return Range_Membership
5068 pragma Warnings
(Off
, Assume_Valid
);
5069 -- For now Assume_Valid is unreferenced since the current implementation
5070 -- always returns Unknown if N is not a compile time known value, but we
5071 -- keep the parameter to allow for future enhancements in which we try
5072 -- to get the information in the variable case as well.
5075 -- Universal types have no range limits, so always in range
5077 if Typ
= Universal_Integer
or else Typ
= Universal_Real
then
5080 -- Never known if not scalar type. Don't know if this can actually
5081 -- happen, but our spec allows it, so we must check!
5083 elsif not Is_Scalar_Type
(Typ
) then
5086 -- Never known if this is a generic type, since the bounds of generic
5087 -- types are junk. Note that if we only checked for static expressions
5088 -- (instead of compile time known values) below, we would not need this
5089 -- check, because values of a generic type can never be static, but they
5090 -- can be known at compile time.
5092 elsif Is_Generic_Type
(Typ
) then
5095 -- Never known unless we have a compile time known value
5097 elsif not Compile_Time_Known_Value
(N
) then
5100 -- General processing with a known compile time value
5111 Lo
:= Type_Low_Bound
(Typ
);
5112 Hi
:= Type_High_Bound
(Typ
);
5114 LB_Known
:= Compile_Time_Known_Value
(Lo
);
5115 HB_Known
:= Compile_Time_Known_Value
(Hi
);
5117 -- Fixed point types should be considered as such only if flag
5118 -- Fixed_Int is set to False.
5120 if Is_Floating_Point_Type
(Typ
)
5121 or else (Is_Fixed_Point_Type
(Typ
) and then not Fixed_Int
)
5124 Valr
:= Expr_Value_R
(N
);
5126 if LB_Known
and HB_Known
then
5127 if Valr
>= Expr_Value_R
(Lo
)
5129 Valr
<= Expr_Value_R
(Hi
)
5133 return Out_Of_Range
;
5136 elsif (LB_Known
and then Valr
< Expr_Value_R
(Lo
))
5138 (HB_Known
and then Valr
> Expr_Value_R
(Hi
))
5140 return Out_Of_Range
;
5147 Val
:= Expr_Value
(N
);
5149 if LB_Known
and HB_Known
then
5150 if Val
>= Expr_Value
(Lo
)
5152 Val
<= Expr_Value
(Hi
)
5156 return Out_Of_Range
;
5159 elsif (LB_Known
and then Val
< Expr_Value
(Lo
))
5161 (HB_Known
and then Val
> Expr_Value
(Hi
))
5163 return Out_Of_Range
;
5177 procedure To_Bits
(U
: Uint
; B
: out Bits
) is
5179 for J
in 0 .. B
'Last loop
5180 B
(J
) := (U
/ (2 ** J
)) mod 2 /= 0;
5184 --------------------
5185 -- Why_Not_Static --
5186 --------------------
5188 procedure Why_Not_Static
(Expr
: Node_Id
) is
5189 N
: constant Node_Id
:= Original_Node
(Expr
);
5193 procedure Why_Not_Static_List
(L
: List_Id
);
5194 -- A version that can be called on a list of expressions. Finds all
5195 -- non-static violations in any element of the list.
5197 -------------------------
5198 -- Why_Not_Static_List --
5199 -------------------------
5201 procedure Why_Not_Static_List
(L
: List_Id
) is
5205 if Is_Non_Empty_List
(L
) then
5207 while Present
(N
) loop
5212 end Why_Not_Static_List
;
5214 -- Start of processing for Why_Not_Static
5217 -- If in ACATS mode (debug flag 2), then suppress all these messages,
5218 -- this avoids massive updates to the ACATS base line.
5220 if Debug_Flag_2
then
5224 -- Ignore call on error or empty node
5226 if No
(Expr
) or else Nkind
(Expr
) = N_Error
then
5230 -- Preprocessing for sub expressions
5232 if Nkind
(Expr
) in N_Subexpr
then
5234 -- Nothing to do if expression is static
5236 if Is_OK_Static_Expression
(Expr
) then
5240 -- Test for constraint error raised
5242 if Raises_Constraint_Error
(Expr
) then
5244 ("expression raises exception, cannot be static " &
5245 "(RM 4.9(34))!", N
);
5249 -- If no type, then something is pretty wrong, so ignore
5251 Typ
:= Etype
(Expr
);
5257 -- Type must be scalar or string type
5259 if not Is_Scalar_Type
(Typ
)
5260 and then not Is_String_Type
(Typ
)
5263 ("static expression must have scalar or string type " &
5269 -- If we got through those checks, test particular node kind
5272 when N_Expanded_Name | N_Identifier | N_Operator_Symbol
=>
5275 if Is_Named_Number
(E
) then
5278 elsif Ekind
(E
) = E_Constant
then
5279 if not Is_Static_Expression
(Constant_Value
(E
)) then
5281 ("& is not a static constant (RM 4.9(5))!", N
, E
);
5286 ("& is not static constant or named number " &
5287 "(RM 4.9(5))!", N
, E
);
5290 when N_Binary_Op | N_Short_Circuit | N_Membership_Test
=>
5291 if Nkind
(N
) in N_Op_Shift
then
5293 ("shift functions are never static (RM 4.9(6,18))!", N
);
5296 Why_Not_Static
(Left_Opnd
(N
));
5297 Why_Not_Static
(Right_Opnd
(N
));
5301 Why_Not_Static
(Right_Opnd
(N
));
5303 when N_Attribute_Reference
=>
5304 Why_Not_Static_List
(Expressions
(N
));
5306 E
:= Etype
(Prefix
(N
));
5308 if E
= Standard_Void_Type
then
5312 -- Special case non-scalar'Size since this is a common error
5314 if Attribute_Name
(N
) = Name_Size
then
5316 ("size attribute is only static for static scalar type " &
5317 "(RM 4.9(7,8))", N
);
5321 elsif Is_Array_Type
(E
) then
5322 if Attribute_Name
(N
) /= Name_First
5324 Attribute_Name
(N
) /= Name_Last
5326 Attribute_Name
(N
) /= Name_Length
5329 ("static array attribute must be Length, First, or Last " &
5332 -- Since we know the expression is not-static (we already
5333 -- tested for this, must mean array is not static).
5337 ("prefix is non-static array (RM 4.9(8))!", Prefix
(N
));
5342 -- Special case generic types, since again this is a common source
5345 elsif Is_Generic_Actual_Type
(E
)
5350 ("attribute of generic type is never static " &
5351 "(RM 4.9(7,8))!", N
);
5353 elsif Is_Static_Subtype
(E
) then
5356 elsif Is_Scalar_Type
(E
) then
5358 ("prefix type for attribute is not static scalar subtype " &
5363 ("static attribute must apply to array/scalar type " &
5364 "(RM 4.9(7,8))!", N
);
5367 when N_String_Literal
=>
5369 ("subtype of string literal is non-static (RM 4.9(4))!", N
);
5371 when N_Explicit_Dereference
=>
5373 ("explicit dereference is never static (RM 4.9)!", N
);
5375 when N_Function_Call
=>
5376 Why_Not_Static_List
(Parameter_Associations
(N
));
5377 Error_Msg_N
("non-static function call (RM 4.9(6,18))!", N
);
5379 when N_Parameter_Association
=>
5380 Why_Not_Static
(Explicit_Actual_Parameter
(N
));
5382 when N_Indexed_Component
=>
5384 ("indexed component is never static (RM 4.9)!", N
);
5386 when N_Procedure_Call_Statement
=>
5388 ("procedure call is never static (RM 4.9)!", N
);
5390 when N_Qualified_Expression
=>
5391 Why_Not_Static
(Expression
(N
));
5393 when N_Aggregate | N_Extension_Aggregate
=>
5395 ("an aggregate is never static (RM 4.9)!", N
);
5398 Why_Not_Static
(Low_Bound
(N
));
5399 Why_Not_Static
(High_Bound
(N
));
5401 when N_Range_Constraint
=>
5402 Why_Not_Static
(Range_Expression
(N
));
5404 when N_Subtype_Indication
=>
5405 Why_Not_Static
(Constraint
(N
));
5407 when N_Selected_Component
=>
5409 ("selected component is never static (RM 4.9)!", N
);
5413 ("slice is never static (RM 4.9)!", N
);
5415 when N_Type_Conversion
=>
5416 Why_Not_Static
(Expression
(N
));
5418 if not Is_Scalar_Type
(Etype
(Prefix
(N
)))
5419 or else not Is_Static_Subtype
(Etype
(Prefix
(N
)))
5422 ("static conversion requires static scalar subtype result " &
5426 when N_Unchecked_Type_Conversion
=>
5428 ("unchecked type conversion is never static (RM 4.9)!", N
);