1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, 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 Aspects
; use Aspects
;
27 with Atree
; use Atree
;
28 with Checks
; use Checks
;
29 with Debug
; use Debug
;
30 with Einfo
; use Einfo
;
31 with Elists
; use Elists
;
32 with Errout
; use Errout
;
33 with Eval_Fat
; use Eval_Fat
;
34 with Exp_Util
; use Exp_Util
;
35 with Freeze
; use Freeze
;
37 with Namet
; use Namet
;
38 with Nmake
; use Nmake
;
39 with Nlists
; use Nlists
;
41 with Par_SCO
; use Par_SCO
;
42 with Rtsfind
; use Rtsfind
;
44 with Sem_Aux
; use Sem_Aux
;
45 with Sem_Cat
; use Sem_Cat
;
46 with Sem_Ch6
; use Sem_Ch6
;
47 with Sem_Ch8
; use Sem_Ch8
;
48 with Sem_Res
; use Sem_Res
;
49 with Sem_Util
; use Sem_Util
;
50 with Sem_Type
; use Sem_Type
;
51 with Sem_Warn
; use Sem_Warn
;
52 with Sinfo
; use Sinfo
;
53 with Snames
; use Snames
;
54 with Stand
; use Stand
;
55 with Stringt
; use Stringt
;
56 with Tbuild
; use Tbuild
;
58 package body Sem_Eval
is
60 -----------------------------------------
61 -- Handling of Compile Time Evaluation --
62 -----------------------------------------
64 -- The compile time evaluation of expressions is distributed over several
65 -- Eval_xxx procedures. These procedures are called immediately after
66 -- a subexpression is resolved and is therefore accomplished in a bottom
67 -- up fashion. The flags are synthesized using the following approach.
69 -- Is_Static_Expression is determined by following the detailed rules
70 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
71 -- flag of the operands in many cases.
73 -- Raises_Constraint_Error is set if any of the operands have the flag
74 -- set or if an attempt to compute the value of the current expression
75 -- results in detection of a runtime constraint error.
77 -- As described in the spec, the requirement is that Is_Static_Expression
78 -- be accurately set, and in addition for nodes for which this flag is set,
79 -- Raises_Constraint_Error must also be set. Furthermore a node which has
80 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
81 -- requirement is that the expression value must be precomputed, and the
82 -- node is either a literal, or the name of a constant entity whose value
83 -- is a static expression.
85 -- The general approach is as follows. First compute Is_Static_Expression.
86 -- If the node is not static, then the flag is left off in the node and
87 -- we are all done. Otherwise for a static node, we test if any of the
88 -- operands will raise constraint error, and if so, propagate the flag
89 -- Raises_Constraint_Error to the result node and we are done (since the
90 -- error was already posted at a lower level).
92 -- For the case of a static node whose operands do not raise constraint
93 -- error, we attempt to evaluate the node. If this evaluation succeeds,
94 -- then the node is replaced by the result of this computation. If the
95 -- evaluation raises constraint error, then we rewrite the node with
96 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
97 -- to post appropriate error messages.
103 type Bits
is array (Nat
range <>) of Boolean;
104 -- Used to convert unsigned (modular) values for folding logical ops
106 -- The following declarations are used to maintain a cache of nodes that
107 -- have compile time known values. The cache is maintained only for
108 -- discrete types (the most common case), and is populated by calls to
109 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
110 -- since it is possible for the status to change (in particular it is
111 -- possible for a node to get replaced by a constraint error node).
113 CV_Bits
: constant := 5;
114 -- Number of low order bits of Node_Id value used to reference entries
115 -- in the cache table.
117 CV_Cache_Size
: constant Nat
:= 2 ** CV_Bits
;
118 -- Size of cache for compile time values
120 subtype CV_Range
is Nat
range 0 .. CV_Cache_Size
;
122 type CV_Entry
is record
127 type Match_Result
is (Match
, No_Match
, Non_Static
);
128 -- Result returned from functions that test for a matching result. If the
129 -- operands are not OK_Static then Non_Static will be returned. Otherwise
130 -- Match/No_Match is returned depending on whether the match succeeds.
132 type CV_Cache_Array
is array (CV_Range
) of CV_Entry
;
134 CV_Cache
: CV_Cache_Array
:= (others => (Node_High_Bound
, Uint_0
));
135 -- This is the actual cache, with entries consisting of node/value pairs,
136 -- and the impossible value Node_High_Bound used for unset entries.
138 type Range_Membership
is (In_Range
, Out_Of_Range
, Unknown
);
139 -- Range membership may either be statically known to be in range or out
140 -- of range, or not statically known. Used for Test_In_Range below.
142 -----------------------
143 -- Local Subprograms --
144 -----------------------
146 function Choice_Matches
148 Choice
: Node_Id
) return Match_Result
;
149 -- Determines whether given value Expr matches the given Choice. The Expr
150 -- can be of discrete, real, or string type and must be a compile time
151 -- known value (it is an error to make the call if these conditions are
152 -- not met). The choice can be a range, subtype name, subtype indication,
153 -- or expression. The returned result is Non_Static if Choice is not
154 -- OK_Static, otherwise either Match or No_Match is returned depending
155 -- on whether Choice matches Expr. This is used for case expression
156 -- alternatives, and also for membership tests. In each case, more
157 -- possibilities are tested than the syntax allows (e.g. membership allows
158 -- subtype indications and non-discrete types, and case allows an OTHERS
159 -- choice), but it does not matter, since we have already done a full
160 -- semantic and syntax check of the construct, so the extra possibilities
161 -- just will not arise for correct expressions.
163 -- Note: if Choice_Matches finds that a choice raises Constraint_Error, e.g
164 -- a reference to a type, one of whose bounds raises Constraint_Error, then
165 -- it also sets the Raises_Constraint_Error flag on the Choice itself.
167 function Choices_Match
169 Choices
: List_Id
) return Match_Result
;
170 -- This function applies Choice_Matches to each element of Choices. If the
171 -- result is No_Match, then it continues and checks the next element. If
172 -- the result is Match or Non_Static, this result is immediately given
173 -- as the result without checking the rest of the list. Expr can be of
174 -- discrete, real, or string type and must be a compile time known value
175 -- (it is an error to make the call if these conditions are not met).
177 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
;
178 -- Check whether an arithmetic operation with universal operands which is a
179 -- rewritten function call with an explicit scope indication is ambiguous:
180 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
181 -- type declared in P and the context does not impose a type on the result
182 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
183 -- error and return Empty, else return the result type of the operator.
185 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
;
186 -- Converts a bit string of length B'Length to a Uint value to be used for
187 -- a target of type T, which is a modular type. This procedure includes the
188 -- necessary reduction by the modulus in the case of a nonbinary modulus
189 -- (for a binary modulus, the bit string is the right length any way so all
192 function Get_String_Val
(N
: Node_Id
) return Node_Id
;
193 -- Given a tree node for a folded string or character value, returns the
194 -- corresponding string literal or character literal (one of the two must
195 -- be available, or the operand would not have been marked as foldable in
196 -- the earlier analysis of the operation).
198 function Is_OK_Static_Choice
(Choice
: Node_Id
) return Boolean;
199 -- Given a choice (from a case expression or membership test), returns
200 -- True if the choice is static and does not raise a Constraint_Error.
202 function Is_OK_Static_Choice_List
(Choices
: List_Id
) return Boolean;
203 -- Given a choice list (from a case expression or membership test), return
204 -- True if all choices are static in the sense of Is_OK_Static_Choice.
206 function Is_Static_Choice
(Choice
: Node_Id
) return Boolean;
207 -- Given a choice (from a case expression or membership test), returns
208 -- True if the choice is static. No test is made for raising of constraint
209 -- error, so this function is used only for legality tests.
211 function Is_Static_Choice_List
(Choices
: List_Id
) return Boolean;
212 -- Given a choice list (from a case expression or membership test), return
213 -- True if all choices are static in the sense of Is_Static_Choice.
215 function Is_Static_Range
(N
: Node_Id
) return Boolean;
216 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
217 -- argument is an N_Range node (but note that the semantic analysis of
218 -- equivalent range attribute references already turned them into the
219 -- equivalent range). This differs from Is_OK_Static_Range (which is what
220 -- must be used by clients) in that it does not care whether the bounds
221 -- raise Constraint_Error or not. Used for checking whether expressions are
222 -- static in the 4.9 sense (without worrying about exceptions).
224 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean;
225 -- Bits represents the number of bits in an integer value to be computed
226 -- (but the value has not been computed yet). If this value in Bits is
227 -- reasonable, a result of True is returned, with the implication that the
228 -- caller should go ahead and complete the calculation. If the value in
229 -- Bits is unreasonably large, then an error is posted on node N, and
230 -- False is returned (and the caller skips the proposed calculation).
232 procedure Out_Of_Range
(N
: Node_Id
);
233 -- This procedure is called if it is determined that node N, which appears
234 -- in a non-static context, is a compile time known value which is outside
235 -- its range, i.e. the range of Etype. This is used in contexts where
236 -- this is an illegality if N is static, and should generate a warning
239 function Real_Or_String_Static_Predicate_Matches
241 Typ
: Entity_Id
) return Boolean;
242 -- This is the function used to evaluate real or string static predicates.
243 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
244 -- represents the value to be tested against the predicate. Typ is the
245 -- type with the predicate, from which the predicate expression can be
246 -- extracted. The result returned is True if the given value satisfies
249 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
);
250 -- N and Exp are nodes representing an expression, Exp is known to raise
251 -- CE. N is rewritten in term of Exp in the optimal way.
253 function String_Type_Len
(Stype
: Entity_Id
) return Uint
;
254 -- Given a string type, determines the length of the index type, or, if
255 -- this index type is non-static, the length of the base type of this index
256 -- type. Note that if the string type is itself static, then the index type
257 -- is static, so the second case applies only if the string type passed is
260 function Test
(Cond
: Boolean) return Uint
;
261 pragma Inline
(Test
);
262 -- This function simply returns the appropriate Boolean'Pos value
263 -- corresponding to the value of Cond as a universal integer. It is
264 -- used for producing the result of the static evaluation of the
267 procedure Test_Expression_Is_Foldable
272 -- Tests to see if expression N whose single operand is Op1 is foldable,
273 -- i.e. the operand value is known at compile time. If the operation is
274 -- foldable, then Fold is True on return, and Stat indicates whether the
275 -- result is static (i.e. the operand was static). Note that it is quite
276 -- possible for Fold to be True, and Stat to be False, since there are
277 -- cases in which we know the value of an operand even though it is not
278 -- technically static (e.g. the static lower bound of a range whose upper
279 -- bound is non-static).
281 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes
282 -- a call to Check_Non_Static_Context on the operand. If Fold is False on
283 -- return, then all processing is complete, and the caller should return,
284 -- since there is nothing else to do.
286 -- If Stat is set True on return, then Is_Static_Expression is also set
287 -- true in node N. There are some cases where this is over-enthusiastic,
288 -- e.g. in the two operand case below, for string comparison, the result is
289 -- not static even though the two operands are static. In such cases, the
290 -- caller must reset the Is_Static_Expression flag in N.
292 -- If Fold and Stat are both set to False then this routine performs also
293 -- the following extra actions:
295 -- If either operand is Any_Type then propagate it to result to prevent
298 -- If some operand raises constraint error, then replace the node N
299 -- with the raise constraint error node. This replacement inherits the
300 -- Is_Static_Expression flag from the operands.
302 procedure Test_Expression_Is_Foldable
308 CRT_Safe
: Boolean := False);
309 -- Same processing, except applies to an expression N with two operands
310 -- Op1 and Op2. The result is static only if both operands are static. If
311 -- CRT_Safe is set True, then CRT_Safe_Compile_Time_Known_Value is used
312 -- for the tests that the two operands are known at compile time. See
313 -- spec of this routine for further details.
315 function Test_In_Range
318 Assume_Valid
: Boolean;
320 Int_Real
: Boolean) return Range_Membership
;
321 -- Common processing for Is_In_Range and Is_Out_Of_Range: Returns In_Range
322 -- or Out_Of_Range if it can be guaranteed at compile time that expression
323 -- N is known to be in or out of range of the subtype Typ. If not compile
324 -- time known, Unknown is returned. See documentation of Is_In_Range for
325 -- complete description of parameters.
327 procedure To_Bits
(U
: Uint
; B
: out Bits
);
328 -- Converts a Uint value to a bit string of length B'Length
330 -----------------------------------------------
331 -- Check_Expression_Against_Static_Predicate --
332 -----------------------------------------------
334 procedure Check_Expression_Against_Static_Predicate
339 -- Nothing to do if expression is not known at compile time, or the
340 -- type has no static predicate set (will be the case for all non-scalar
341 -- types, so no need to make a special test for that).
343 if not (Has_Static_Predicate
(Typ
)
344 and then Compile_Time_Known_Value
(Expr
))
349 -- Here we have a static predicate (note that it could have arisen from
350 -- an explicitly specified Dynamic_Predicate whose expression met the
351 -- rules for being predicate-static). If the expression is known at
352 -- compile time and obeys the predicate, then it is static and must be
353 -- labeled as such, which matters e.g. for case statements. The original
354 -- expression may be a type conversion of a variable with a known value,
355 -- which might otherwise not be marked static.
357 -- Case of real static predicate
359 if Is_Real_Type
(Typ
) then
360 if Real_Or_String_Static_Predicate_Matches
361 (Val
=> Make_Real_Literal
(Sloc
(Expr
), Expr_Value_R
(Expr
)),
364 Set_Is_Static_Expression
(Expr
);
368 -- Case of string static predicate
370 elsif Is_String_Type
(Typ
) then
371 if Real_Or_String_Static_Predicate_Matches
372 (Val
=> Expr_Value_S
(Expr
), Typ
=> Typ
)
374 Set_Is_Static_Expression
(Expr
);
378 -- Case of discrete static predicate
381 pragma Assert
(Is_Discrete_Type
(Typ
));
383 -- If static predicate matches, nothing to do
385 if Choices_Match
(Expr
, Static_Discrete_Predicate
(Typ
)) = Match
then
386 Set_Is_Static_Expression
(Expr
);
391 -- Here we know that the predicate will fail
393 -- Special case of static expression failing a predicate (other than one
394 -- that was explicitly specified with a Dynamic_Predicate aspect). This
395 -- is the case where the expression is no longer considered static.
397 if Is_Static_Expression
(Expr
)
398 and then not Has_Dynamic_Predicate_Aspect
(Typ
)
401 ("??static expression fails static predicate check on &",
404 ("\??expression is no longer considered static", Expr
);
405 Set_Is_Static_Expression
(Expr
, False);
407 -- In all other cases, this is just a warning that a test will fail.
408 -- It does not matter if the expression is static or not, or if the
409 -- predicate comes from a dynamic predicate aspect or not.
413 ("??expression fails predicate check on &", Expr
, Typ
);
415 end Check_Expression_Against_Static_Predicate
;
417 ------------------------------
418 -- Check_Non_Static_Context --
419 ------------------------------
421 procedure Check_Non_Static_Context
(N
: Node_Id
) is
422 T
: constant Entity_Id
:= Etype
(N
);
423 Checks_On
: constant Boolean :=
424 not Index_Checks_Suppressed
(T
)
425 and not Range_Checks_Suppressed
(T
);
428 -- Ignore cases of non-scalar types, error types, or universal real
429 -- types that have no usable bounds.
432 or else not Is_Scalar_Type
(T
)
433 or else T
= Universal_Fixed
434 or else T
= Universal_Real
439 -- At this stage we have a scalar type. If we have an expression that
440 -- raises CE, then we already issued a warning or error msg so there is
441 -- nothing more to be done in this routine.
443 if Raises_Constraint_Error
(N
) then
447 -- Now we have a scalar type which is not marked as raising a constraint
448 -- error exception. The main purpose of this routine is to deal with
449 -- static expressions appearing in a non-static context. That means
450 -- that if we do not have a static expression then there is not much
451 -- to do. The one case that we deal with here is that if we have a
452 -- floating-point value that is out of range, then we post a warning
453 -- that an infinity will result.
455 if not Is_Static_Expression
(N
) then
456 if Is_Floating_Point_Type
(T
) then
457 if Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
459 ("??float value out of range, infinity will be generated", N
);
461 -- The literal may be the result of constant-folding of a non-
462 -- static subexpression of a larger expression (e.g. a conversion
463 -- of a non-static variable whose value happens to be known). At
464 -- this point we must reduce the value of the subexpression to a
465 -- machine number (RM 4.9 (38/2)).
467 elsif Nkind
(N
) = N_Real_Literal
468 and then Nkind
(Parent
(N
)) in N_Subexpr
470 Rewrite
(N
, New_Copy
(N
));
472 (N
, Machine
(Base_Type
(T
), Realval
(N
), Round_Even
, N
));
479 -- Here we have the case of outer level static expression of scalar
480 -- type, where the processing of this procedure is needed.
482 -- For real types, this is where we convert the value to a machine
483 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
484 -- need to do this if the parent is a constant declaration, since in
485 -- other cases, gigi should do the necessary conversion correctly, but
486 -- experimentation shows that this is not the case on all machines, in
487 -- particular if we do not convert all literals to machine values in
488 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
491 -- This conversion is always done by GNATprove on real literals in
492 -- non-static expressions, by calling Check_Non_Static_Context from
493 -- gnat2why, as GNATprove cannot do the conversion later contrary
494 -- to gigi. The frontend computes the information about which
495 -- expressions are static, which is used by gnat2why to call
496 -- Check_Non_Static_Context on exactly those real literals that are
497 -- not subexpressions of static expressions.
499 if Nkind
(N
) = N_Real_Literal
500 and then not Is_Machine_Number
(N
)
501 and then not Is_Generic_Type
(Etype
(N
))
502 and then Etype
(N
) /= Universal_Real
504 -- Check that value is in bounds before converting to machine
505 -- number, so as not to lose case where value overflows in the
506 -- least significant bit or less. See B490001.
508 if Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
513 -- Note: we have to copy the node, to avoid problems with conformance
514 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
516 Rewrite
(N
, New_Copy
(N
));
518 if not Is_Floating_Point_Type
(T
) then
520 (N
, Corresponding_Integer_Value
(N
) * Small_Value
(T
));
522 elsif not UR_Is_Zero
(Realval
(N
)) then
524 -- Note: even though RM 4.9(38) specifies biased rounding, this
525 -- has been modified by AI-100 in order to prevent confusing
526 -- differences in rounding between static and non-static
527 -- expressions. AI-100 specifies that the effect of such rounding
528 -- is implementation dependent, and in GNAT we round to nearest
529 -- even to match the run-time behavior. Note that this applies
530 -- to floating point literals, not fixed points ones, even though
531 -- their compiler representation is also as a universal real.
534 (N
, Machine
(Base_Type
(T
), Realval
(N
), Round_Even
, N
));
535 Set_Is_Machine_Number
(N
);
540 -- Check for out of range universal integer. This is a non-static
541 -- context, so the integer value must be in range of the runtime
542 -- representation of universal integers.
544 -- We do this only within an expression, because that is the only
545 -- case in which non-static universal integer values can occur, and
546 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
547 -- called in contexts like the expression of a number declaration where
548 -- we certainly want to allow out of range values.
550 if Etype
(N
) = Universal_Integer
551 and then Nkind
(N
) = N_Integer_Literal
552 and then Nkind
(Parent
(N
)) in N_Subexpr
554 (Intval
(N
) < Expr_Value
(Type_Low_Bound
(Universal_Integer
))
556 Intval
(N
) > Expr_Value
(Type_High_Bound
(Universal_Integer
)))
558 Apply_Compile_Time_Constraint_Error
559 (N
, "non-static universal integer value out of range<<",
560 CE_Range_Check_Failed
);
562 -- Check out of range of base type
564 elsif Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
567 -- Give warning if outside subtype (where one or both of the bounds of
568 -- the subtype is static). This warning is omitted if the expression
569 -- appears in a range that could be null (warnings are handled elsewhere
572 elsif T
/= Base_Type
(T
) and then Nkind
(Parent
(N
)) /= N_Range
then
573 if Is_In_Range
(N
, T
, Assume_Valid
=> True) then
576 elsif Is_Out_Of_Range
(N
, T
, Assume_Valid
=> True) then
577 Apply_Compile_Time_Constraint_Error
578 (N
, "value not in range of}<<", CE_Range_Check_Failed
);
581 Enable_Range_Check
(N
);
584 Set_Do_Range_Check
(N
, False);
587 end Check_Non_Static_Context
;
589 ---------------------------------
590 -- Check_String_Literal_Length --
591 ---------------------------------
593 procedure Check_String_Literal_Length
(N
: Node_Id
; Ttype
: Entity_Id
) is
595 if not Raises_Constraint_Error
(N
) and then Is_Constrained
(Ttype
) then
596 if UI_From_Int
(String_Length
(Strval
(N
))) /= String_Type_Len
(Ttype
)
598 Apply_Compile_Time_Constraint_Error
599 (N
, "string length wrong for}??",
600 CE_Length_Check_Failed
,
605 end Check_String_Literal_Length
;
611 function Choice_Matches
613 Choice
: Node_Id
) return Match_Result
615 Etyp
: constant Entity_Id
:= Etype
(Expr
);
621 pragma Assert
(Compile_Time_Known_Value
(Expr
));
622 pragma Assert
(Is_Scalar_Type
(Etyp
) or else Is_String_Type
(Etyp
));
624 if not Is_OK_Static_Choice
(Choice
) then
625 Set_Raises_Constraint_Error
(Choice
);
628 -- When the choice denotes a subtype with a static predictate, check the
629 -- expression against the predicate values. Different procedures apply
630 -- to discrete and non-discrete types.
632 elsif (Nkind
(Choice
) = N_Subtype_Indication
633 or else (Is_Entity_Name
(Choice
)
634 and then Is_Type
(Entity
(Choice
))))
635 and then Has_Predicates
(Etype
(Choice
))
636 and then Has_Static_Predicate
(Etype
(Choice
))
638 if Is_Discrete_Type
(Etype
(Choice
)) then
641 (Expr
, Static_Discrete_Predicate
(Etype
(Choice
)));
643 elsif Real_Or_String_Static_Predicate_Matches
(Expr
, Etype
(Choice
))
651 -- Discrete type case only
653 elsif Is_Discrete_Type
(Etyp
) then
654 Val
:= Expr_Value
(Expr
);
656 if Nkind
(Choice
) = N_Range
then
657 if Val
>= Expr_Value
(Low_Bound
(Choice
))
659 Val
<= Expr_Value
(High_Bound
(Choice
))
666 elsif Nkind
(Choice
) = N_Subtype_Indication
667 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
669 if Val
>= Expr_Value
(Type_Low_Bound
(Etype
(Choice
)))
671 Val
<= Expr_Value
(Type_High_Bound
(Etype
(Choice
)))
678 elsif Nkind
(Choice
) = N_Others_Choice
then
682 if Val
= Expr_Value
(Choice
) then
691 elsif Is_Real_Type
(Etyp
) then
692 ValR
:= Expr_Value_R
(Expr
);
694 if Nkind
(Choice
) = N_Range
then
695 if ValR
>= Expr_Value_R
(Low_Bound
(Choice
))
697 ValR
<= Expr_Value_R
(High_Bound
(Choice
))
704 elsif Nkind
(Choice
) = N_Subtype_Indication
705 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
707 if ValR
>= Expr_Value_R
(Type_Low_Bound
(Etype
(Choice
)))
709 ValR
<= Expr_Value_R
(Type_High_Bound
(Etype
(Choice
)))
717 if ValR
= Expr_Value_R
(Choice
) then
727 pragma Assert
(Is_String_Type
(Etyp
));
728 ValS
:= Expr_Value_S
(Expr
);
730 if Nkind
(Choice
) = N_Subtype_Indication
731 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
733 if not Is_Constrained
(Etype
(Choice
)) then
738 Typlen
: constant Uint
:=
739 String_Type_Len
(Etype
(Choice
));
740 Strlen
: constant Uint
:=
741 UI_From_Int
(String_Length
(Strval
(ValS
)));
743 if Typlen
= Strlen
then
752 if String_Equal
(Strval
(ValS
), Strval
(Expr_Value_S
(Choice
)))
766 function Choices_Match
768 Choices
: List_Id
) return Match_Result
771 Result
: Match_Result
;
774 Choice
:= First
(Choices
);
775 while Present
(Choice
) loop
776 Result
:= Choice_Matches
(Expr
, Choice
);
778 if Result
/= No_Match
then
788 --------------------------
789 -- Compile_Time_Compare --
790 --------------------------
792 function Compile_Time_Compare
794 Assume_Valid
: Boolean) return Compare_Result
796 Discard
: aliased Uint
;
798 return Compile_Time_Compare
(L
, R
, Discard
'Access, Assume_Valid
);
799 end Compile_Time_Compare
;
801 function Compile_Time_Compare
804 Assume_Valid
: Boolean;
805 Rec
: Boolean := False) return Compare_Result
807 Ltyp
: Entity_Id
:= Etype
(L
);
808 Rtyp
: Entity_Id
:= Etype
(R
);
810 Discard
: aliased Uint
;
812 procedure Compare_Decompose
816 -- This procedure decomposes the node N into an expression node and a
817 -- signed offset, so that the value of N is equal to the value of R plus
818 -- the value V (which may be negative). If no such decomposition is
819 -- possible, then on return R is a copy of N, and V is set to zero.
821 function Compare_Fixup
(N
: Node_Id
) return Node_Id
;
822 -- This function deals with replacing 'Last and 'First references with
823 -- their corresponding type bounds, which we then can compare. The
824 -- argument is the original node, the result is the identity, unless we
825 -- have a 'Last/'First reference in which case the value returned is the
826 -- appropriate type bound.
828 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean;
829 -- Even if the context does not assume that values are valid, some
830 -- simple cases can be recognized.
832 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean;
833 -- Returns True iff L and R represent expressions that definitely have
834 -- identical (but not necessarily compile time known) values Indeed the
835 -- caller is expected to have already dealt with the cases of compile
836 -- time known values, so these are not tested here.
838 -----------------------
839 -- Compare_Decompose --
840 -----------------------
842 procedure Compare_Decompose
848 if Nkind
(N
) = N_Op_Add
849 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
852 V
:= Intval
(Right_Opnd
(N
));
855 elsif Nkind
(N
) = N_Op_Subtract
856 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
859 V
:= UI_Negate
(Intval
(Right_Opnd
(N
)));
862 elsif Nkind
(N
) = N_Attribute_Reference
then
863 if Attribute_Name
(N
) = Name_Succ
then
864 R
:= First
(Expressions
(N
));
868 elsif Attribute_Name
(N
) = Name_Pred
then
869 R
:= First
(Expressions
(N
));
877 end Compare_Decompose
;
883 function Compare_Fixup
(N
: Node_Id
) return Node_Id
is
889 -- Fixup only required for First/Last attribute reference
891 if Nkind
(N
) = N_Attribute_Reference
892 and then Nam_In
(Attribute_Name
(N
), Name_First
, Name_Last
)
894 Xtyp
:= Etype
(Prefix
(N
));
896 -- If we have no type, then just abandon the attempt to do
897 -- a fixup, this is probably the result of some other error.
903 -- Dereference an access type
905 if Is_Access_Type
(Xtyp
) then
906 Xtyp
:= Designated_Type
(Xtyp
);
909 -- If we don't have an array type at this stage, something is
910 -- peculiar, e.g. another error, and we abandon the attempt at
913 if not Is_Array_Type
(Xtyp
) then
917 -- Ignore unconstrained array, since bounds are not meaningful
919 if not Is_Constrained
(Xtyp
) then
923 if Ekind
(Xtyp
) = E_String_Literal_Subtype
then
924 if Attribute_Name
(N
) = Name_First
then
925 return String_Literal_Low_Bound
(Xtyp
);
928 Make_Integer_Literal
(Sloc
(N
),
929 Intval
=> Intval
(String_Literal_Low_Bound
(Xtyp
)) +
930 String_Literal_Length
(Xtyp
));
934 -- Find correct index type
936 Indx
:= First_Index
(Xtyp
);
938 if Present
(Expressions
(N
)) then
939 Subs
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
941 for J
in 2 .. Subs
loop
942 Indx
:= Next_Index
(Indx
);
946 Xtyp
:= Etype
(Indx
);
948 if Attribute_Name
(N
) = Name_First
then
949 return Type_Low_Bound
(Xtyp
);
951 return Type_High_Bound
(Xtyp
);
958 ----------------------------
959 -- Is_Known_Valid_Operand --
960 ----------------------------
962 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean is
964 return (Is_Entity_Name
(Opnd
)
966 (Is_Known_Valid
(Entity
(Opnd
))
967 or else Ekind
(Entity
(Opnd
)) = E_In_Parameter
969 (Ekind
(Entity
(Opnd
)) in Object_Kind
970 and then Present
(Current_Value
(Entity
(Opnd
))))))
971 or else Is_OK_Static_Expression
(Opnd
);
972 end Is_Known_Valid_Operand
;
978 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean is
979 Lf
: constant Node_Id
:= Compare_Fixup
(L
);
980 Rf
: constant Node_Id
:= Compare_Fixup
(R
);
982 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean;
983 -- L, R are the Expressions values from two attribute nodes for First
984 -- or Last attributes. Either may be set to No_List if no expressions
985 -- are present (indicating subscript 1). The result is True if both
986 -- expressions represent the same subscript (note one case is where
987 -- one subscript is missing and the other is explicitly set to 1).
989 -----------------------
990 -- Is_Same_Subscript --
991 -----------------------
993 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean is
999 return Expr_Value
(First
(R
)) = Uint_1
;
1004 return Expr_Value
(First
(L
)) = Uint_1
;
1006 return Expr_Value
(First
(L
)) = Expr_Value
(First
(R
));
1009 end Is_Same_Subscript
;
1011 -- Start of processing for Is_Same_Value
1014 -- Values are the same if they refer to the same entity and the
1015 -- entity is non-volatile. This does not however apply to Float
1016 -- types, since we may have two NaN values and they should never
1019 -- If the entity is a discriminant, the two expressions may be bounds
1020 -- of components of objects of the same discriminated type. The
1021 -- values of the discriminants are not static, and therefore the
1022 -- result is unknown.
1024 -- It would be better to comment individual branches of this test ???
1026 if Nkind_In
(Lf
, N_Identifier
, N_Expanded_Name
)
1027 and then Nkind_In
(Rf
, N_Identifier
, N_Expanded_Name
)
1028 and then Entity
(Lf
) = Entity
(Rf
)
1029 and then Ekind
(Entity
(Lf
)) /= E_Discriminant
1030 and then Present
(Entity
(Lf
))
1031 and then not Is_Floating_Point_Type
(Etype
(L
))
1032 and then not Is_Volatile_Reference
(L
)
1033 and then not Is_Volatile_Reference
(R
)
1037 -- Or if they are compile time known and identical
1039 elsif Compile_Time_Known_Value
(Lf
)
1041 Compile_Time_Known_Value
(Rf
)
1042 and then Expr_Value
(Lf
) = Expr_Value
(Rf
)
1046 -- False if Nkind of the two nodes is different for remaining cases
1048 elsif Nkind
(Lf
) /= Nkind
(Rf
) then
1051 -- True if both 'First or 'Last values applying to the same entity
1052 -- (first and last don't change even if value does). Note that we
1053 -- need this even with the calls to Compare_Fixup, to handle the
1054 -- case of unconstrained array attributes where Compare_Fixup
1055 -- cannot find useful bounds.
1057 elsif Nkind
(Lf
) = N_Attribute_Reference
1058 and then Attribute_Name
(Lf
) = Attribute_Name
(Rf
)
1059 and then Nam_In
(Attribute_Name
(Lf
), Name_First
, Name_Last
)
1060 and then Nkind_In
(Prefix
(Lf
), N_Identifier
, N_Expanded_Name
)
1061 and then Nkind_In
(Prefix
(Rf
), N_Identifier
, N_Expanded_Name
)
1062 and then Entity
(Prefix
(Lf
)) = Entity
(Prefix
(Rf
))
1063 and then Is_Same_Subscript
(Expressions
(Lf
), Expressions
(Rf
))
1067 -- True if the same selected component from the same record
1069 elsif Nkind
(Lf
) = N_Selected_Component
1070 and then Selector_Name
(Lf
) = Selector_Name
(Rf
)
1071 and then Is_Same_Value
(Prefix
(Lf
), Prefix
(Rf
))
1075 -- True if the same unary operator applied to the same operand
1077 elsif Nkind
(Lf
) in N_Unary_Op
1078 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1082 -- True if the same binary operator applied to the same operands
1084 elsif Nkind
(Lf
) in N_Binary_Op
1085 and then Is_Same_Value
(Left_Opnd
(Lf
), Left_Opnd
(Rf
))
1086 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1090 -- All other cases, we can't tell, so return False
1097 -- Start of processing for Compile_Time_Compare
1100 Diff
.all := No_Uint
;
1102 -- In preanalysis mode, always return Unknown unless the expression
1103 -- is static. It is too early to be thinking we know the result of a
1104 -- comparison, save that judgment for the full analysis. This is
1105 -- particularly important in the case of pre and postconditions, which
1106 -- otherwise can be prematurely collapsed into having True or False
1107 -- conditions when this is inappropriate.
1109 if not (Full_Analysis
1110 or else (Is_OK_Static_Expression
(L
)
1112 Is_OK_Static_Expression
(R
)))
1117 -- If either operand could raise constraint error, then we cannot
1118 -- know the result at compile time (since CE may be raised).
1120 if not (Cannot_Raise_Constraint_Error
(L
)
1122 Cannot_Raise_Constraint_Error
(R
))
1127 -- Identical operands are most certainly equal
1133 -- If expressions have no types, then do not attempt to determine if
1134 -- they are the same, since something funny is going on. One case in
1135 -- which this happens is during generic template analysis, when bounds
1136 -- are not fully analyzed.
1138 if No
(Ltyp
) or else No
(Rtyp
) then
1142 -- These get reset to the base type for the case of entities where
1143 -- Is_Known_Valid is not set. This takes care of handling possible
1144 -- invalid representations using the value of the base type, in
1145 -- accordance with RM 13.9.1(10).
1147 Ltyp
:= Underlying_Type
(Ltyp
);
1148 Rtyp
:= Underlying_Type
(Rtyp
);
1150 -- Same rationale as above, but for Underlying_Type instead of Etype
1152 if No
(Ltyp
) or else No
(Rtyp
) then
1156 -- We do not attempt comparisons for packed arrays represented as
1157 -- modular types, where the semantics of comparison is quite different.
1159 if Is_Packed_Array_Impl_Type
(Ltyp
)
1160 and then Is_Modular_Integer_Type
(Ltyp
)
1164 -- For access types, the only time we know the result at compile time
1165 -- (apart from identical operands, which we handled already) is if we
1166 -- know one operand is null and the other is not, or both operands are
1169 elsif Is_Access_Type
(Ltyp
) then
1170 if Known_Null
(L
) then
1171 if Known_Null
(R
) then
1173 elsif Known_Non_Null
(R
) then
1179 elsif Known_Non_Null
(L
) and then Known_Null
(R
) then
1186 -- Case where comparison involves two compile time known values
1188 elsif Compile_Time_Known_Value
(L
)
1190 Compile_Time_Known_Value
(R
)
1192 -- For the floating-point case, we have to be a little careful, since
1193 -- at compile time we are dealing with universal exact values, but at
1194 -- runtime, these will be in non-exact target form. That's why the
1195 -- returned results are LE and GE below instead of LT and GT.
1197 if Is_Floating_Point_Type
(Ltyp
)
1199 Is_Floating_Point_Type
(Rtyp
)
1202 Lo
: constant Ureal
:= Expr_Value_R
(L
);
1203 Hi
: constant Ureal
:= Expr_Value_R
(R
);
1214 -- For string types, we have two string literals and we proceed to
1215 -- compare them using the Ada style dictionary string comparison.
1217 elsif not Is_Scalar_Type
(Ltyp
) then
1219 Lstring
: constant String_Id
:= Strval
(Expr_Value_S
(L
));
1220 Rstring
: constant String_Id
:= Strval
(Expr_Value_S
(R
));
1221 Llen
: constant Nat
:= String_Length
(Lstring
);
1222 Rlen
: constant Nat
:= String_Length
(Rstring
);
1225 for J
in 1 .. Nat
'Min (Llen
, Rlen
) loop
1227 LC
: constant Char_Code
:= Get_String_Char
(Lstring
, J
);
1228 RC
: constant Char_Code
:= Get_String_Char
(Rstring
, J
);
1240 elsif Llen
> Rlen
then
1247 -- For remaining scalar cases we know exactly (note that this does
1248 -- include the fixed-point case, where we know the run time integer
1253 Lo
: constant Uint
:= Expr_Value
(L
);
1254 Hi
: constant Uint
:= Expr_Value
(R
);
1257 Diff
.all := Hi
- Lo
;
1262 Diff
.all := Lo
- Hi
;
1268 -- Cases where at least one operand is not known at compile time
1271 -- Remaining checks apply only for discrete types
1273 if not Is_Discrete_Type
(Ltyp
)
1275 not Is_Discrete_Type
(Rtyp
)
1280 -- Defend against generic types, or actually any expressions that
1281 -- contain a reference to a generic type from within a generic
1282 -- template. We don't want to do any range analysis of such
1283 -- expressions for two reasons. First, the bounds of a generic type
1284 -- itself are junk and cannot be used for any kind of analysis.
1285 -- Second, we may have a case where the range at run time is indeed
1286 -- known, but we don't want to do compile time analysis in the
1287 -- template based on that range since in an instance the value may be
1288 -- static, and able to be elaborated without reference to the bounds
1289 -- of types involved. As an example, consider:
1291 -- (F'Pos (F'Last) + 1) > Integer'Last
1293 -- The expression on the left side of > is Universal_Integer and thus
1294 -- acquires the type Integer for evaluation at run time, and at run
1295 -- time it is true that this condition is always False, but within
1296 -- an instance F may be a type with a static range greater than the
1297 -- range of Integer, and the expression statically evaluates to True.
1299 if References_Generic_Formal_Type
(L
)
1301 References_Generic_Formal_Type
(R
)
1306 -- Replace types by base types for the case of values which are not
1307 -- known to have valid representations. This takes care of properly
1308 -- dealing with invalid representations.
1310 if not Assume_Valid
then
1311 if not (Is_Entity_Name
(L
)
1312 and then (Is_Known_Valid
(Entity
(L
))
1313 or else Assume_No_Invalid_Values
))
1315 Ltyp
:= Underlying_Type
(Base_Type
(Ltyp
));
1318 if not (Is_Entity_Name
(R
)
1319 and then (Is_Known_Valid
(Entity
(R
))
1320 or else Assume_No_Invalid_Values
))
1322 Rtyp
:= Underlying_Type
(Base_Type
(Rtyp
));
1326 -- First attempt is to decompose the expressions to extract a
1327 -- constant offset resulting from the use of any of the forms:
1334 -- Then we see if the two expressions are the same value, and if so
1335 -- the result is obtained by comparing the offsets.
1337 -- Note: the reason we do this test first is that it returns only
1338 -- decisive results (with diff set), where other tests, like the
1339 -- range test, may not be as so decisive. Consider for example
1340 -- J .. J + 1. This code can conclude LT with a difference of 1,
1341 -- even if the range of J is not known.
1350 Compare_Decompose
(L
, Lnode
, Loffs
);
1351 Compare_Decompose
(R
, Rnode
, Roffs
);
1353 if Is_Same_Value
(Lnode
, Rnode
) then
1354 if Loffs
= Roffs
then
1358 -- When the offsets are not equal, we can go farther only if
1359 -- the types are not modular (e.g. X < X + 1 is False if X is
1360 -- the largest number).
1362 if not Is_Modular_Integer_Type
(Ltyp
)
1363 and then not Is_Modular_Integer_Type
(Rtyp
)
1365 if Loffs
< Roffs
then
1366 Diff
.all := Roffs
- Loffs
;
1369 Diff
.all := Loffs
- Roffs
;
1376 -- Next, try range analysis and see if operand ranges are disjoint
1384 -- True if each range is a single point
1387 Determine_Range
(L
, LOK
, LLo
, LHi
, Assume_Valid
);
1388 Determine_Range
(R
, ROK
, RLo
, RHi
, Assume_Valid
);
1391 Single
:= (LLo
= LHi
) and then (RLo
= RHi
);
1394 if Single
and Assume_Valid
then
1395 Diff
.all := RLo
- LLo
;
1400 elsif RHi
< LLo
then
1401 if Single
and Assume_Valid
then
1402 Diff
.all := LLo
- RLo
;
1407 elsif Single
and then LLo
= RLo
then
1409 -- If the range includes a single literal and we can assume
1410 -- validity then the result is known even if an operand is
1413 if Assume_Valid
then
1419 elsif LHi
= RLo
then
1422 elsif RHi
= LLo
then
1425 elsif not Is_Known_Valid_Operand
(L
)
1426 and then not Assume_Valid
1428 if Is_Same_Value
(L
, R
) then
1435 -- If the range of either operand cannot be determined, nothing
1436 -- further can be inferred.
1443 -- Here is where we check for comparisons against maximum bounds of
1444 -- types, where we know that no value can be outside the bounds of
1445 -- the subtype. Note that this routine is allowed to assume that all
1446 -- expressions are within their subtype bounds. Callers wishing to
1447 -- deal with possibly invalid values must in any case take special
1448 -- steps (e.g. conversions to larger types) to avoid this kind of
1449 -- optimization, which is always considered to be valid. We do not
1450 -- attempt this optimization with generic types, since the type
1451 -- bounds may not be meaningful in this case.
1453 -- We are in danger of an infinite recursion here. It does not seem
1454 -- useful to go more than one level deep, so the parameter Rec is
1455 -- used to protect ourselves against this infinite recursion.
1459 -- See if we can get a decisive check against one operand and a
1460 -- bound of the other operand (four possible tests here). Note
1461 -- that we avoid testing junk bounds of a generic type.
1463 if not Is_Generic_Type
(Rtyp
) then
1464 case Compile_Time_Compare
(L
, Type_Low_Bound
(Rtyp
),
1466 Assume_Valid
, Rec
=> True)
1468 when LT
=> return LT
;
1469 when LE
=> return LE
;
1470 when EQ
=> return LE
;
1471 when others => null;
1474 case Compile_Time_Compare
(L
, Type_High_Bound
(Rtyp
),
1476 Assume_Valid
, Rec
=> True)
1478 when GT
=> return GT
;
1479 when GE
=> return GE
;
1480 when EQ
=> return GE
;
1481 when others => null;
1485 if not Is_Generic_Type
(Ltyp
) then
1486 case Compile_Time_Compare
(Type_Low_Bound
(Ltyp
), R
,
1488 Assume_Valid
, Rec
=> True)
1490 when GT
=> return GT
;
1491 when GE
=> return GE
;
1492 when EQ
=> return GE
;
1493 when others => null;
1496 case Compile_Time_Compare
(Type_High_Bound
(Ltyp
), R
,
1498 Assume_Valid
, Rec
=> True)
1500 when LT
=> return LT
;
1501 when LE
=> return LE
;
1502 when EQ
=> return LE
;
1503 when others => null;
1508 -- Next attempt is to see if we have an entity compared with a
1509 -- compile time known value, where there is a current value
1510 -- conditional for the entity which can tell us the result.
1514 -- Entity variable (left operand)
1517 -- Value (right operand)
1520 -- If False, we have reversed the operands
1523 -- Comparison operator kind from Get_Current_Value_Condition call
1526 -- Value from Get_Current_Value_Condition call
1531 Result
: Compare_Result
;
1532 -- Known result before inversion
1535 if Is_Entity_Name
(L
)
1536 and then Compile_Time_Known_Value
(R
)
1539 Val
:= Expr_Value
(R
);
1542 elsif Is_Entity_Name
(R
)
1543 and then Compile_Time_Known_Value
(L
)
1546 Val
:= Expr_Value
(L
);
1549 -- That was the last chance at finding a compile time result
1555 Get_Current_Value_Condition
(Var
, Op
, Opn
);
1557 -- That was the last chance, so if we got nothing return
1563 Opv
:= Expr_Value
(Opn
);
1565 -- We got a comparison, so we might have something interesting
1567 -- Convert LE to LT and GE to GT, just so we have fewer cases
1569 if Op
= N_Op_Le
then
1573 elsif Op
= N_Op_Ge
then
1578 -- Deal with equality case
1580 if Op
= N_Op_Eq
then
1583 elsif Opv
< Val
then
1589 -- Deal with inequality case
1591 elsif Op
= N_Op_Ne
then
1598 -- Deal with greater than case
1600 elsif Op
= N_Op_Gt
then
1603 elsif Opv
= Val
- 1 then
1609 -- Deal with less than case
1611 else pragma Assert
(Op
= N_Op_Lt
);
1614 elsif Opv
= Val
+ 1 then
1621 -- Deal with inverting result
1625 when GT
=> return LT
;
1626 when GE
=> return LE
;
1627 when LT
=> return GT
;
1628 when LE
=> return GE
;
1629 when others => return Result
;
1636 end Compile_Time_Compare
;
1638 -------------------------------
1639 -- Compile_Time_Known_Bounds --
1640 -------------------------------
1642 function Compile_Time_Known_Bounds
(T
: Entity_Id
) return Boolean is
1647 if T
= Any_Composite
or else not Is_Array_Type
(T
) then
1651 Indx
:= First_Index
(T
);
1652 while Present
(Indx
) loop
1653 Typ
:= Underlying_Type
(Etype
(Indx
));
1655 -- Never look at junk bounds of a generic type
1657 if Is_Generic_Type
(Typ
) then
1661 -- Otherwise check bounds for compile time known
1663 if not Compile_Time_Known_Value
(Type_Low_Bound
(Typ
)) then
1665 elsif not Compile_Time_Known_Value
(Type_High_Bound
(Typ
)) then
1673 end Compile_Time_Known_Bounds
;
1675 ------------------------------
1676 -- Compile_Time_Known_Value --
1677 ------------------------------
1679 function Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1680 K
: constant Node_Kind
:= Nkind
(Op
);
1681 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(Op
) mod CV_Cache_Size
);
1684 -- Never known at compile time if bad type or raises constraint error
1685 -- or empty (latter case occurs only as a result of a previous error).
1688 Check_Error_Detected
;
1692 or else Etype
(Op
) = Any_Type
1693 or else Raises_Constraint_Error
(Op
)
1698 -- If we have an entity name, then see if it is the name of a constant
1699 -- and if so, test the corresponding constant value, or the name of
1700 -- an enumeration literal, which is always a constant.
1702 if Present
(Etype
(Op
)) and then Is_Entity_Name
(Op
) then
1704 E
: constant Entity_Id
:= Entity
(Op
);
1708 -- Never known at compile time if it is a packed array value.
1709 -- We might want to try to evaluate these at compile time one
1710 -- day, but we do not make that attempt now.
1712 if Is_Packed_Array_Impl_Type
(Etype
(Op
)) then
1716 if Ekind
(E
) = E_Enumeration_Literal
then
1719 elsif Ekind
(E
) = E_Constant
then
1720 V
:= Constant_Value
(E
);
1721 return Present
(V
) and then Compile_Time_Known_Value
(V
);
1725 -- We have a value, see if it is compile time known
1728 -- Integer literals are worth storing in the cache
1730 if K
= N_Integer_Literal
then
1732 CV_Ent
.V
:= Intval
(Op
);
1735 -- Other literals and NULL are known at compile time
1738 Nkind_In
(K
, N_Character_Literal
,
1747 -- If we fall through, not known at compile time
1751 -- If we get an exception while trying to do this test, then some error
1752 -- has occurred, and we simply say that the value is not known after all
1757 end Compile_Time_Known_Value
;
1759 --------------------------------------
1760 -- Compile_Time_Known_Value_Or_Aggr --
1761 --------------------------------------
1763 function Compile_Time_Known_Value_Or_Aggr
(Op
: Node_Id
) return Boolean is
1765 -- If we have an entity name, then see if it is the name of a constant
1766 -- and if so, test the corresponding constant value, or the name of
1767 -- an enumeration literal, which is always a constant.
1769 if Is_Entity_Name
(Op
) then
1771 E
: constant Entity_Id
:= Entity
(Op
);
1775 if Ekind
(E
) = E_Enumeration_Literal
then
1778 elsif Ekind
(E
) /= E_Constant
then
1782 V
:= Constant_Value
(E
);
1784 and then Compile_Time_Known_Value_Or_Aggr
(V
);
1788 -- We have a value, see if it is compile time known
1791 if Compile_Time_Known_Value
(Op
) then
1794 elsif Nkind
(Op
) = N_Aggregate
then
1796 if Present
(Expressions
(Op
)) then
1800 Expr
:= First
(Expressions
(Op
));
1801 while Present
(Expr
) loop
1802 if not Compile_Time_Known_Value_Or_Aggr
(Expr
) then
1811 if Present
(Component_Associations
(Op
)) then
1816 Cass
:= First
(Component_Associations
(Op
));
1817 while Present
(Cass
) loop
1819 Compile_Time_Known_Value_Or_Aggr
(Expression
(Cass
))
1831 -- All other types of values are not known at compile time
1838 end Compile_Time_Known_Value_Or_Aggr
;
1840 ---------------------------------------
1841 -- CRT_Safe_Compile_Time_Known_Value --
1842 ---------------------------------------
1844 function CRT_Safe_Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1846 if (Configurable_Run_Time_Mode
or No_Run_Time_Mode
)
1847 and then not Is_OK_Static_Expression
(Op
)
1851 return Compile_Time_Known_Value
(Op
);
1853 end CRT_Safe_Compile_Time_Known_Value
;
1859 -- This is only called for actuals of functions that are not predefined
1860 -- operators (which have already been rewritten as operators at this
1861 -- stage), so the call can never be folded, and all that needs doing for
1862 -- the actual is to do the check for a non-static context.
1864 procedure Eval_Actual
(N
: Node_Id
) is
1866 Check_Non_Static_Context
(N
);
1869 --------------------
1870 -- Eval_Allocator --
1871 --------------------
1873 -- Allocators are never static, so all we have to do is to do the
1874 -- check for a non-static context if an expression is present.
1876 procedure Eval_Allocator
(N
: Node_Id
) is
1877 Expr
: constant Node_Id
:= Expression
(N
);
1879 if Nkind
(Expr
) = N_Qualified_Expression
then
1880 Check_Non_Static_Context
(Expression
(Expr
));
1884 ------------------------
1885 -- Eval_Arithmetic_Op --
1886 ------------------------
1888 -- Arithmetic operations are static functions, so the result is static
1889 -- if both operands are static (RM 4.9(7), 4.9(20)).
1891 procedure Eval_Arithmetic_Op
(N
: Node_Id
) is
1892 Left
: constant Node_Id
:= Left_Opnd
(N
);
1893 Right
: constant Node_Id
:= Right_Opnd
(N
);
1894 Ltype
: constant Entity_Id
:= Etype
(Left
);
1895 Rtype
: constant Entity_Id
:= Etype
(Right
);
1896 Otype
: Entity_Id
:= Empty
;
1901 -- If not foldable we are done
1903 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
1909 -- Otherwise attempt to fold
1911 if Is_Universal_Numeric_Type
(Etype
(Left
))
1913 Is_Universal_Numeric_Type
(Etype
(Right
))
1915 Otype
:= Find_Universal_Operator_Type
(N
);
1918 -- Fold for cases where both operands are of integer type
1920 if Is_Integer_Type
(Ltype
) and then Is_Integer_Type
(Rtype
) then
1922 Left_Int
: constant Uint
:= Expr_Value
(Left
);
1923 Right_Int
: constant Uint
:= Expr_Value
(Right
);
1929 Result
:= Left_Int
+ Right_Int
;
1931 when N_Op_Subtract
=>
1932 Result
:= Left_Int
- Right_Int
;
1934 when N_Op_Multiply
=>
1937 (Num_Bits
(Left_Int
) + Num_Bits
(Right_Int
)))
1939 Result
:= Left_Int
* Right_Int
;
1946 -- The exception Constraint_Error is raised by integer
1947 -- division, rem and mod if the right operand is zero.
1949 if Right_Int
= 0 then
1951 -- When SPARK_Mode is On, force a warning instead of
1952 -- an error in that case, as this likely corresponds
1953 -- to deactivated code.
1955 Apply_Compile_Time_Constraint_Error
1956 (N
, "division by zero", CE_Divide_By_Zero
,
1957 Warn
=> not Stat
or SPARK_Mode
= On
);
1958 Set_Raises_Constraint_Error
(N
);
1961 -- Otherwise we can do the division
1964 Result
:= Left_Int
/ Right_Int
;
1969 -- The exception Constraint_Error is raised by integer
1970 -- division, rem and mod if the right operand is zero.
1972 if Right_Int
= 0 then
1974 -- When SPARK_Mode is On, force a warning instead of
1975 -- an error in that case, as this likely corresponds
1976 -- to deactivated code.
1978 Apply_Compile_Time_Constraint_Error
1979 (N
, "mod with zero divisor", CE_Divide_By_Zero
,
1980 Warn
=> not Stat
or SPARK_Mode
= On
);
1984 Result
:= Left_Int
mod Right_Int
;
1989 -- The exception Constraint_Error is raised by integer
1990 -- division, rem and mod if the right operand is zero.
1992 if Right_Int
= 0 then
1994 -- When SPARK_Mode is On, force a warning instead of
1995 -- an error in that case, as this likely corresponds
1996 -- to deactivated code.
1998 Apply_Compile_Time_Constraint_Error
1999 (N
, "rem with zero divisor", CE_Divide_By_Zero
,
2000 Warn
=> not Stat
or SPARK_Mode
= On
);
2004 Result
:= Left_Int
rem Right_Int
;
2008 raise Program_Error
;
2011 -- Adjust the result by the modulus if the type is a modular type
2013 if Is_Modular_Integer_Type
(Ltype
) then
2014 Result
:= Result
mod Modulus
(Ltype
);
2016 -- For a signed integer type, check non-static overflow
2018 elsif (not Stat
) and then Is_Signed_Integer_Type
(Ltype
) then
2020 BT
: constant Entity_Id
:= Base_Type
(Ltype
);
2021 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(BT
));
2022 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(BT
));
2024 if Result
< Lo
or else Result
> Hi
then
2025 Apply_Compile_Time_Constraint_Error
2026 (N
, "value not in range of }??",
2027 CE_Overflow_Check_Failed
,
2034 -- If we get here we can fold the result
2036 Fold_Uint
(N
, Result
, Stat
);
2039 -- Cases where at least one operand is a real. We handle the cases of
2040 -- both reals, or mixed/real integer cases (the latter happen only for
2041 -- divide and multiply, and the result is always real).
2043 elsif Is_Real_Type
(Ltype
) or else Is_Real_Type
(Rtype
) then
2050 if Is_Real_Type
(Ltype
) then
2051 Left_Real
:= Expr_Value_R
(Left
);
2053 Left_Real
:= UR_From_Uint
(Expr_Value
(Left
));
2056 if Is_Real_Type
(Rtype
) then
2057 Right_Real
:= Expr_Value_R
(Right
);
2059 Right_Real
:= UR_From_Uint
(Expr_Value
(Right
));
2062 if Nkind
(N
) = N_Op_Add
then
2063 Result
:= Left_Real
+ Right_Real
;
2065 elsif Nkind
(N
) = N_Op_Subtract
then
2066 Result
:= Left_Real
- Right_Real
;
2068 elsif Nkind
(N
) = N_Op_Multiply
then
2069 Result
:= Left_Real
* Right_Real
;
2071 else pragma Assert
(Nkind
(N
) = N_Op_Divide
);
2072 if UR_Is_Zero
(Right_Real
) then
2073 Apply_Compile_Time_Constraint_Error
2074 (N
, "division by zero", CE_Divide_By_Zero
);
2078 Result
:= Left_Real
/ Right_Real
;
2081 Fold_Ureal
(N
, Result
, Stat
);
2085 -- If the operator was resolved to a specific type, make sure that type
2086 -- is frozen even if the expression is folded into a literal (which has
2087 -- a universal type).
2089 if Present
(Otype
) then
2090 Freeze_Before
(N
, Otype
);
2092 end Eval_Arithmetic_Op
;
2094 ----------------------------
2095 -- Eval_Character_Literal --
2096 ----------------------------
2098 -- Nothing to be done
2100 procedure Eval_Character_Literal
(N
: Node_Id
) is
2101 pragma Warnings
(Off
, N
);
2104 end Eval_Character_Literal
;
2110 -- Static function calls are either calls to predefined operators
2111 -- with static arguments, or calls to functions that rename a literal.
2112 -- Only the latter case is handled here, predefined operators are
2113 -- constant-folded elsewhere.
2115 -- If the function is itself inherited (see 7423-001) the literal of
2116 -- the parent type must be explicitly converted to the return type
2119 procedure Eval_Call
(N
: Node_Id
) is
2120 Loc
: constant Source_Ptr
:= Sloc
(N
);
2121 Typ
: constant Entity_Id
:= Etype
(N
);
2125 if Nkind
(N
) = N_Function_Call
2126 and then No
(Parameter_Associations
(N
))
2127 and then Is_Entity_Name
(Name
(N
))
2128 and then Present
(Alias
(Entity
(Name
(N
))))
2129 and then Is_Enumeration_Type
(Base_Type
(Typ
))
2131 Lit
:= Ultimate_Alias
(Entity
(Name
(N
)));
2133 if Ekind
(Lit
) = E_Enumeration_Literal
then
2134 if Base_Type
(Etype
(Lit
)) /= Base_Type
(Typ
) then
2136 (N
, Convert_To
(Typ
, New_Occurrence_Of
(Lit
, Loc
)));
2138 Rewrite
(N
, New_Occurrence_Of
(Lit
, Loc
));
2146 --------------------------
2147 -- Eval_Case_Expression --
2148 --------------------------
2150 -- A conditional expression is static if all its conditions and dependent
2151 -- expressions are static. Note that we do not care if the dependent
2152 -- expressions raise CE, except for the one that will be selected.
2154 procedure Eval_Case_Expression
(N
: Node_Id
) is
2159 Set_Is_Static_Expression
(N
, False);
2161 if Error_Posted
(Expression
(N
))
2162 or else not Is_Static_Expression
(Expression
(N
))
2164 Check_Non_Static_Context
(Expression
(N
));
2168 -- First loop, make sure all the alternatives are static expressions
2169 -- none of which raise Constraint_Error. We make the constraint error
2170 -- check because part of the legality condition for a correct static
2171 -- case expression is that the cases are covered, like any other case
2172 -- expression. And we can't do that if any of the conditions raise an
2173 -- exception, so we don't even try to evaluate if that is the case.
2175 Alt
:= First
(Alternatives
(N
));
2176 while Present
(Alt
) loop
2178 -- The expression must be static, but we don't care at this stage
2179 -- if it raises Constraint_Error (the alternative might not match,
2180 -- in which case the expression is statically unevaluated anyway).
2182 if not Is_Static_Expression
(Expression
(Alt
)) then
2183 Check_Non_Static_Context
(Expression
(Alt
));
2187 -- The choices of a case always have to be static, and cannot raise
2188 -- an exception. If this condition is not met, then the expression
2189 -- is plain illegal, so just abandon evaluation attempts. No need
2190 -- to check non-static context when we have something illegal anyway.
2192 if not Is_OK_Static_Choice_List
(Discrete_Choices
(Alt
)) then
2199 -- OK, if the above loop gets through it means that all choices are OK
2200 -- static (don't raise exceptions), so the whole case is static, and we
2201 -- can find the matching alternative.
2203 Set_Is_Static_Expression
(N
);
2205 -- Now to deal with propagating a possible constraint error
2207 -- If the selecting expression raises CE, propagate and we are done
2209 if Raises_Constraint_Error
(Expression
(N
)) then
2210 Set_Raises_Constraint_Error
(N
);
2212 -- Otherwise we need to check the alternatives to find the matching
2213 -- one. CE's in other than the matching one are not relevant. But we
2214 -- do need to check the matching one. Unlike the first loop, we do not
2215 -- have to go all the way through, when we find the matching one, quit.
2218 Alt
:= First
(Alternatives
(N
));
2221 -- We must find a match among the alternatives. If not, this must
2222 -- be due to other errors, so just ignore, leaving as non-static.
2225 Set_Is_Static_Expression
(N
, False);
2229 -- Otherwise loop through choices of this alternative
2231 Choice
:= First
(Discrete_Choices
(Alt
));
2232 while Present
(Choice
) loop
2234 -- If we find a matching choice, then the Expression of this
2235 -- alternative replaces N (Raises_Constraint_Error flag is
2236 -- included, so we don't have to special case that).
2238 if Choice_Matches
(Expression
(N
), Choice
) = Match
then
2239 Rewrite
(N
, Relocate_Node
(Expression
(Alt
)));
2249 end Eval_Case_Expression
;
2251 ------------------------
2252 -- Eval_Concatenation --
2253 ------------------------
2255 -- Concatenation is a static function, so the result is static if both
2256 -- operands are static (RM 4.9(7), 4.9(21)).
2258 procedure Eval_Concatenation
(N
: Node_Id
) is
2259 Left
: constant Node_Id
:= Left_Opnd
(N
);
2260 Right
: constant Node_Id
:= Right_Opnd
(N
);
2261 C_Typ
: constant Entity_Id
:= Root_Type
(Component_Type
(Etype
(N
)));
2266 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2267 -- non-static context.
2269 if Ada_Version
= Ada_83
2270 and then Comes_From_Source
(N
)
2272 Check_Non_Static_Context
(Left
);
2273 Check_Non_Static_Context
(Right
);
2277 -- If not foldable we are done. In principle concatenation that yields
2278 -- any string type is static (i.e. an array type of character types).
2279 -- However, character types can include enumeration literals, and
2280 -- concatenation in that case cannot be described by a literal, so we
2281 -- only consider the operation static if the result is an array of
2282 -- (a descendant of) a predefined character type.
2284 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2286 if not (Is_Standard_Character_Type
(C_Typ
) and then Fold
) then
2287 Set_Is_Static_Expression
(N
, False);
2291 -- Compile time string concatenation
2293 -- ??? Note that operands that are aggregates can be marked as static,
2294 -- so we should attempt at a later stage to fold concatenations with
2298 Left_Str
: constant Node_Id
:= Get_String_Val
(Left
);
2300 Right_Str
: constant Node_Id
:= Get_String_Val
(Right
);
2301 Folded_Val
: String_Id
;
2304 -- Establish new string literal, and store left operand. We make
2305 -- sure to use the special Start_String that takes an operand if
2306 -- the left operand is a string literal. Since this is optimized
2307 -- in the case where that is the most recently created string
2308 -- literal, we ensure efficient time/space behavior for the
2309 -- case of a concatenation of a series of string literals.
2311 if Nkind
(Left_Str
) = N_String_Literal
then
2312 Left_Len
:= String_Length
(Strval
(Left_Str
));
2314 -- If the left operand is the empty string, and the right operand
2315 -- is a string literal (the case of "" & "..."), the result is the
2316 -- value of the right operand. This optimization is important when
2317 -- Is_Folded_In_Parser, to avoid copying an enormous right
2320 if Left_Len
= 0 and then Nkind
(Right_Str
) = N_String_Literal
then
2321 Folded_Val
:= Strval
(Right_Str
);
2323 Start_String
(Strval
(Left_Str
));
2328 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Left_Str
)));
2332 -- Now append the characters of the right operand, unless we
2333 -- optimized the "" & "..." case above.
2335 if Nkind
(Right_Str
) = N_String_Literal
then
2336 if Left_Len
/= 0 then
2337 Store_String_Chars
(Strval
(Right_Str
));
2338 Folded_Val
:= End_String
;
2341 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Right_Str
)));
2342 Folded_Val
:= End_String
;
2345 Set_Is_Static_Expression
(N
, Stat
);
2347 -- If left operand is the empty string, the result is the
2348 -- right operand, including its bounds if anomalous.
2351 and then Is_Array_Type
(Etype
(Right
))
2352 and then Etype
(Right
) /= Any_String
2354 Set_Etype
(N
, Etype
(Right
));
2357 Fold_Str
(N
, Folded_Val
, Static
=> Stat
);
2359 end Eval_Concatenation
;
2361 ----------------------
2362 -- Eval_Entity_Name --
2363 ----------------------
2365 -- This procedure is used for identifiers and expanded names other than
2366 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2367 -- static if they denote a static constant (RM 4.9(6)) or if the name
2368 -- denotes an enumeration literal (RM 4.9(22)).
2370 procedure Eval_Entity_Name
(N
: Node_Id
) is
2371 Def_Id
: constant Entity_Id
:= Entity
(N
);
2375 -- Enumeration literals are always considered to be constants
2376 -- and cannot raise constraint error (RM 4.9(22)).
2378 if Ekind
(Def_Id
) = E_Enumeration_Literal
then
2379 Set_Is_Static_Expression
(N
);
2382 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2383 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2384 -- it does not violate 10.2.1(8) here, since this is not a variable.
2386 elsif Ekind
(Def_Id
) = E_Constant
then
2388 -- Deferred constants must always be treated as nonstatic outside the
2389 -- scope of their full view.
2391 if Present
(Full_View
(Def_Id
))
2392 and then not In_Open_Scopes
(Scope
(Def_Id
))
2396 Val
:= Constant_Value
(Def_Id
);
2399 if Present
(Val
) then
2400 Set_Is_Static_Expression
2401 (N
, Is_Static_Expression
(Val
)
2402 and then Is_Static_Subtype
(Etype
(Def_Id
)));
2403 Set_Raises_Constraint_Error
(N
, Raises_Constraint_Error
(Val
));
2405 if not Is_Static_Expression
(N
)
2406 and then not Is_Generic_Type
(Etype
(N
))
2408 Validate_Static_Object_Name
(N
);
2411 -- Mark constant condition in SCOs
2414 and then Comes_From_Source
(N
)
2415 and then Is_Boolean_Type
(Etype
(Def_Id
))
2416 and then Compile_Time_Known_Value
(N
)
2418 Set_SCO_Condition
(N
, Expr_Value_E
(N
) = Standard_True
);
2425 -- Fall through if the name is not static
2427 Validate_Static_Object_Name
(N
);
2428 end Eval_Entity_Name
;
2430 ------------------------
2431 -- Eval_If_Expression --
2432 ------------------------
2434 -- We can fold to a static expression if the condition and both dependent
2435 -- expressions are static. Otherwise, the only required processing is to do
2436 -- the check for non-static context for the then and else expressions.
2438 procedure Eval_If_Expression
(N
: Node_Id
) is
2439 Condition
: constant Node_Id
:= First
(Expressions
(N
));
2440 Then_Expr
: constant Node_Id
:= Next
(Condition
);
2441 Else_Expr
: constant Node_Id
:= Next
(Then_Expr
);
2443 Non_Result
: Node_Id
;
2445 Rstat
: constant Boolean :=
2446 Is_Static_Expression
(Condition
)
2448 Is_Static_Expression
(Then_Expr
)
2450 Is_Static_Expression
(Else_Expr
);
2451 -- True if result is static
2454 -- If result not static, nothing to do, otherwise set static result
2459 Set_Is_Static_Expression
(N
);
2462 -- If any operand is Any_Type, just propagate to result and do not try
2463 -- to fold, this prevents cascaded errors.
2465 if Etype
(Condition
) = Any_Type
or else
2466 Etype
(Then_Expr
) = Any_Type
or else
2467 Etype
(Else_Expr
) = Any_Type
2469 Set_Etype
(N
, Any_Type
);
2470 Set_Is_Static_Expression
(N
, False);
2474 -- If condition raises constraint error then we have already signaled
2475 -- an error, and we just propagate to the result and do not fold.
2477 if Raises_Constraint_Error
(Condition
) then
2478 Set_Raises_Constraint_Error
(N
);
2482 -- Static case where we can fold. Note that we don't try to fold cases
2483 -- where the condition is known at compile time, but the result is
2484 -- non-static. This avoids possible cases of infinite recursion where
2485 -- the expander puts in a redundant test and we remove it. Instead we
2486 -- deal with these cases in the expander.
2488 -- Select result operand
2490 if Is_True
(Expr_Value
(Condition
)) then
2491 Result
:= Then_Expr
;
2492 Non_Result
:= Else_Expr
;
2494 Result
:= Else_Expr
;
2495 Non_Result
:= Then_Expr
;
2498 -- Note that it does not matter if the non-result operand raises a
2499 -- Constraint_Error, but if the result raises constraint error then we
2500 -- replace the node with a raise constraint error. This will properly
2501 -- propagate Raises_Constraint_Error since this flag is set in Result.
2503 if Raises_Constraint_Error
(Result
) then
2504 Rewrite_In_Raise_CE
(N
, Result
);
2505 Check_Non_Static_Context
(Non_Result
);
2507 -- Otherwise the result operand replaces the original node
2510 Rewrite
(N
, Relocate_Node
(Result
));
2511 Set_Is_Static_Expression
(N
);
2513 end Eval_If_Expression
;
2515 ----------------------------
2516 -- Eval_Indexed_Component --
2517 ----------------------------
2519 -- Indexed components are never static, so we need to perform the check
2520 -- for non-static context on the index values. Then, we check if the
2521 -- value can be obtained at compile time, even though it is non-static.
2523 procedure Eval_Indexed_Component
(N
: Node_Id
) is
2527 -- Check for non-static context on index values
2529 Expr
:= First
(Expressions
(N
));
2530 while Present
(Expr
) loop
2531 Check_Non_Static_Context
(Expr
);
2535 -- If the indexed component appears in an object renaming declaration
2536 -- then we do not want to try to evaluate it, since in this case we
2537 -- need the identity of the array element.
2539 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
2542 -- Similarly if the indexed component appears as the prefix of an
2543 -- attribute we don't want to evaluate it, because at least for
2544 -- some cases of attributes we need the identify (e.g. Access, Size)
2546 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
then
2550 -- Note: there are other cases, such as the left side of an assignment,
2551 -- or an OUT parameter for a call, where the replacement results in the
2552 -- illegal use of a constant, But these cases are illegal in the first
2553 -- place, so the replacement, though silly, is harmless.
2555 -- Now see if this is a constant array reference
2557 if List_Length
(Expressions
(N
)) = 1
2558 and then Is_Entity_Name
(Prefix
(N
))
2559 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
2560 and then Present
(Constant_Value
(Entity
(Prefix
(N
))))
2563 Loc
: constant Source_Ptr
:= Sloc
(N
);
2564 Arr
: constant Node_Id
:= Constant_Value
(Entity
(Prefix
(N
)));
2565 Sub
: constant Node_Id
:= First
(Expressions
(N
));
2571 -- Linear one's origin subscript value for array reference
2574 -- Lower bound of the first array index
2577 -- Value from constant array
2580 Atyp
:= Etype
(Arr
);
2582 if Is_Access_Type
(Atyp
) then
2583 Atyp
:= Designated_Type
(Atyp
);
2586 -- If we have an array type (we should have but perhaps there are
2587 -- error cases where this is not the case), then see if we can do
2588 -- a constant evaluation of the array reference.
2590 if Is_Array_Type
(Atyp
) and then Atyp
/= Any_Composite
then
2591 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
2592 Lbd
:= String_Literal_Low_Bound
(Atyp
);
2594 Lbd
:= Type_Low_Bound
(Etype
(First_Index
(Atyp
)));
2597 if Compile_Time_Known_Value
(Sub
)
2598 and then Nkind
(Arr
) = N_Aggregate
2599 and then Compile_Time_Known_Value
(Lbd
)
2600 and then Is_Discrete_Type
(Component_Type
(Atyp
))
2602 Lin
:= UI_To_Int
(Expr_Value
(Sub
) - Expr_Value
(Lbd
)) + 1;
2604 if List_Length
(Expressions
(Arr
)) >= Lin
then
2605 Elm
:= Pick
(Expressions
(Arr
), Lin
);
2607 -- If the resulting expression is compile time known,
2608 -- then we can rewrite the indexed component with this
2609 -- value, being sure to mark the result as non-static.
2610 -- We also reset the Sloc, in case this generates an
2611 -- error later on (e.g. 136'Access).
2613 if Compile_Time_Known_Value
(Elm
) then
2614 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2615 Set_Is_Static_Expression
(N
, False);
2620 -- We can also constant-fold if the prefix is a string literal.
2621 -- This will be useful in an instantiation or an inlining.
2623 elsif Compile_Time_Known_Value
(Sub
)
2624 and then Nkind
(Arr
) = N_String_Literal
2625 and then Compile_Time_Known_Value
(Lbd
)
2626 and then Expr_Value
(Lbd
) = 1
2627 and then Expr_Value
(Sub
) <=
2628 String_Literal_Length
(Etype
(Arr
))
2631 C
: constant Char_Code
:=
2632 Get_String_Char
(Strval
(Arr
),
2633 UI_To_Int
(Expr_Value
(Sub
)));
2635 Set_Character_Literal_Name
(C
);
2638 Make_Character_Literal
(Loc
,
2640 Char_Literal_Value
=> UI_From_CC
(C
));
2641 Set_Etype
(Elm
, Component_Type
(Atyp
));
2642 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2643 Set_Is_Static_Expression
(N
, False);
2649 end Eval_Indexed_Component
;
2651 --------------------------
2652 -- Eval_Integer_Literal --
2653 --------------------------
2655 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2656 -- as static by the analyzer. The reason we did it that early is to allow
2657 -- the possibility of turning off the Is_Static_Expression flag after
2658 -- analysis, but before resolution, when integer literals are generated in
2659 -- the expander that do not correspond to static expressions.
2661 procedure Eval_Integer_Literal
(N
: Node_Id
) is
2662 T
: constant Entity_Id
:= Etype
(N
);
2664 function In_Any_Integer_Context
return Boolean;
2665 -- If the literal is resolved with a specific type in a context where
2666 -- the expected type is Any_Integer, there are no range checks on the
2667 -- literal. By the time the literal is evaluated, it carries the type
2668 -- imposed by the enclosing expression, and we must recover the context
2669 -- to determine that Any_Integer is meant.
2671 ----------------------------
2672 -- In_Any_Integer_Context --
2673 ----------------------------
2675 function In_Any_Integer_Context
return Boolean is
2676 Par
: constant Node_Id
:= Parent
(N
);
2677 K
: constant Node_Kind
:= Nkind
(Par
);
2680 -- Any_Integer also appears in digits specifications for real types,
2681 -- but those have bounds smaller that those of any integer base type,
2682 -- so we can safely ignore these cases.
2684 return Nkind_In
(K
, N_Number_Declaration
,
2685 N_Attribute_Reference
,
2686 N_Attribute_Definition_Clause
,
2687 N_Modular_Type_Definition
,
2688 N_Signed_Integer_Type_Definition
);
2689 end In_Any_Integer_Context
;
2691 -- Start of processing for Eval_Integer_Literal
2695 -- If the literal appears in a non-expression context, then it is
2696 -- certainly appearing in a non-static context, so check it. This is
2697 -- actually a redundant check, since Check_Non_Static_Context would
2698 -- check it, but it seems worthwhile to optimize out the call.
2700 -- An exception is made for a literal in an if or case expression
2702 if (Nkind_In
(Parent
(N
), N_If_Expression
, N_Case_Expression_Alternative
)
2703 or else Nkind
(Parent
(N
)) not in N_Subexpr
)
2704 and then not In_Any_Integer_Context
2706 Check_Non_Static_Context
(N
);
2709 -- Modular integer literals must be in their base range
2711 if Is_Modular_Integer_Type
(T
)
2712 and then Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True)
2716 end Eval_Integer_Literal
;
2718 ---------------------
2719 -- Eval_Logical_Op --
2720 ---------------------
2722 -- Logical operations are static functions, so the result is potentially
2723 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2725 procedure Eval_Logical_Op
(N
: Node_Id
) is
2726 Left
: constant Node_Id
:= Left_Opnd
(N
);
2727 Right
: constant Node_Id
:= Right_Opnd
(N
);
2732 -- If not foldable we are done
2734 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2740 -- Compile time evaluation of logical operation
2743 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2744 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2747 if Is_Modular_Integer_Type
(Etype
(N
)) then
2749 Left_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2750 Right_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2753 To_Bits
(Left_Int
, Left_Bits
);
2754 To_Bits
(Right_Int
, Right_Bits
);
2756 -- Note: should really be able to use array ops instead of
2757 -- these loops, but they weren't working at the time ???
2759 if Nkind
(N
) = N_Op_And
then
2760 for J
in Left_Bits
'Range loop
2761 Left_Bits
(J
) := Left_Bits
(J
) and Right_Bits
(J
);
2764 elsif Nkind
(N
) = N_Op_Or
then
2765 for J
in Left_Bits
'Range loop
2766 Left_Bits
(J
) := Left_Bits
(J
) or Right_Bits
(J
);
2770 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2772 for J
in Left_Bits
'Range loop
2773 Left_Bits
(J
) := Left_Bits
(J
) xor Right_Bits
(J
);
2777 Fold_Uint
(N
, From_Bits
(Left_Bits
, Etype
(N
)), Stat
);
2781 pragma Assert
(Is_Boolean_Type
(Etype
(N
)));
2783 if Nkind
(N
) = N_Op_And
then
2785 Test
(Is_True
(Left_Int
) and then Is_True
(Right_Int
)), Stat
);
2787 elsif Nkind
(N
) = N_Op_Or
then
2789 Test
(Is_True
(Left_Int
) or else Is_True
(Right_Int
)), Stat
);
2792 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2794 Test
(Is_True
(Left_Int
) xor Is_True
(Right_Int
)), Stat
);
2798 end Eval_Logical_Op
;
2800 ------------------------
2801 -- Eval_Membership_Op --
2802 ------------------------
2804 -- A membership test is potentially static if the expression is static, and
2805 -- the range is a potentially static range, or is a subtype mark denoting a
2806 -- static subtype (RM 4.9(12)).
2808 procedure Eval_Membership_Op
(N
: Node_Id
) is
2809 Alts
: constant List_Id
:= Alternatives
(N
);
2810 Choice
: constant Node_Id
:= Right_Opnd
(N
);
2811 Expr
: constant Node_Id
:= Left_Opnd
(N
);
2812 Result
: Match_Result
;
2815 -- Ignore if error in either operand, except to make sure that Any_Type
2816 -- is properly propagated to avoid junk cascaded errors.
2818 if Etype
(Expr
) = Any_Type
2819 or else (Present
(Choice
) and then Etype
(Choice
) = Any_Type
)
2821 Set_Etype
(N
, Any_Type
);
2825 -- If left operand non-static, then nothing to do
2827 if not Is_Static_Expression
(Expr
) then
2831 -- If choice is non-static, left operand is in non-static context
2833 if (Present
(Choice
) and then not Is_Static_Choice
(Choice
))
2834 or else (Present
(Alts
) and then not Is_Static_Choice_List
(Alts
))
2836 Check_Non_Static_Context
(Expr
);
2840 -- Otherwise we definitely have a static expression
2842 Set_Is_Static_Expression
(N
);
2844 -- If left operand raises constraint error, propagate and we are done
2846 if Raises_Constraint_Error
(Expr
) then
2847 Set_Raises_Constraint_Error
(N
, True);
2852 if Present
(Choice
) then
2853 Result
:= Choice_Matches
(Expr
, Choice
);
2855 Result
:= Choices_Match
(Expr
, Alts
);
2858 -- If result is Non_Static, it means that we raise Constraint_Error,
2859 -- since we already tested that the operands were themselves static.
2861 if Result
= Non_Static
then
2862 Set_Raises_Constraint_Error
(N
);
2864 -- Otherwise we have our result (flipped if NOT IN case)
2868 (N
, Test
((Result
= Match
) xor (Nkind
(N
) = N_Not_In
)), True);
2869 Warn_On_Known_Condition
(N
);
2872 end Eval_Membership_Op
;
2874 ------------------------
2875 -- Eval_Named_Integer --
2876 ------------------------
2878 procedure Eval_Named_Integer
(N
: Node_Id
) is
2881 Expr_Value
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2882 end Eval_Named_Integer
;
2884 ---------------------
2885 -- Eval_Named_Real --
2886 ---------------------
2888 procedure Eval_Named_Real
(N
: Node_Id
) is
2891 Expr_Value_R
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2892 end Eval_Named_Real
;
2898 -- Exponentiation is a static functions, so the result is potentially
2899 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2901 procedure Eval_Op_Expon
(N
: Node_Id
) is
2902 Left
: constant Node_Id
:= Left_Opnd
(N
);
2903 Right
: constant Node_Id
:= Right_Opnd
(N
);
2908 -- If not foldable we are done
2910 Test_Expression_Is_Foldable
2911 (N
, Left
, Right
, Stat
, Fold
, CRT_Safe
=> True);
2913 -- Return if not foldable
2919 if Configurable_Run_Time_Mode
and not Stat
then
2923 -- Fold exponentiation operation
2926 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2931 if Is_Integer_Type
(Etype
(Left
)) then
2933 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2937 -- Exponentiation of an integer raises Constraint_Error for a
2938 -- negative exponent (RM 4.5.6).
2940 if Right_Int
< 0 then
2941 Apply_Compile_Time_Constraint_Error
2942 (N
, "integer exponent negative", CE_Range_Check_Failed
,
2947 if OK_Bits
(N
, Num_Bits
(Left_Int
) * Right_Int
) then
2948 Result
:= Left_Int
** Right_Int
;
2953 if Is_Modular_Integer_Type
(Etype
(N
)) then
2954 Result
:= Result
mod Modulus
(Etype
(N
));
2957 Fold_Uint
(N
, Result
, Stat
);
2965 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
2968 -- Cannot have a zero base with a negative exponent
2970 if UR_Is_Zero
(Left_Real
) then
2972 if Right_Int
< 0 then
2973 Apply_Compile_Time_Constraint_Error
2974 (N
, "zero ** negative integer", CE_Range_Check_Failed
,
2978 Fold_Ureal
(N
, Ureal_0
, Stat
);
2982 Fold_Ureal
(N
, Left_Real
** Right_Int
, Stat
);
2993 -- The not operation is a static functions, so the result is potentially
2994 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2996 procedure Eval_Op_Not
(N
: Node_Id
) is
2997 Right
: constant Node_Id
:= Right_Opnd
(N
);
3002 -- If not foldable we are done
3004 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
3010 -- Fold not operation
3013 Rint
: constant Uint
:= Expr_Value
(Right
);
3014 Typ
: constant Entity_Id
:= Etype
(N
);
3017 -- Negation is equivalent to subtracting from the modulus minus one.
3018 -- For a binary modulus this is equivalent to the ones-complement of
3019 -- the original value. For a nonbinary modulus this is an arbitrary
3020 -- but consistent definition.
3022 if Is_Modular_Integer_Type
(Typ
) then
3023 Fold_Uint
(N
, Modulus
(Typ
) - 1 - Rint
, Stat
);
3024 else pragma Assert
(Is_Boolean_Type
(Typ
));
3025 Fold_Uint
(N
, Test
(not Is_True
(Rint
)), Stat
);
3028 Set_Is_Static_Expression
(N
, Stat
);
3032 -------------------------------
3033 -- Eval_Qualified_Expression --
3034 -------------------------------
3036 -- A qualified expression is potentially static if its subtype mark denotes
3037 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
3039 procedure Eval_Qualified_Expression
(N
: Node_Id
) is
3040 Operand
: constant Node_Id
:= Expression
(N
);
3041 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
3048 -- Can only fold if target is string or scalar and subtype is static.
3049 -- Also, do not fold if our parent is an allocator (this is because the
3050 -- qualified expression is really part of the syntactic structure of an
3051 -- allocator, and we do not want to end up with something that
3052 -- corresponds to "new 1" where the 1 is the result of folding a
3053 -- qualified expression).
3055 if not Is_Static_Subtype
(Target_Type
)
3056 or else Nkind
(Parent
(N
)) = N_Allocator
3058 Check_Non_Static_Context
(Operand
);
3060 -- If operand is known to raise constraint_error, set the flag on the
3061 -- expression so it does not get optimized away.
3063 if Nkind
(Operand
) = N_Raise_Constraint_Error
then
3064 Set_Raises_Constraint_Error
(N
);
3070 -- If not foldable we are done
3072 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3077 -- Don't try fold if target type has constraint error bounds
3079 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3080 Set_Raises_Constraint_Error
(N
);
3084 -- Here we will fold, save Print_In_Hex indication
3086 Hex
:= Nkind
(Operand
) = N_Integer_Literal
3087 and then Print_In_Hex
(Operand
);
3089 -- Fold the result of qualification
3091 if Is_Discrete_Type
(Target_Type
) then
3092 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3094 -- Preserve Print_In_Hex indication
3096 if Hex
and then Nkind
(N
) = N_Integer_Literal
then
3097 Set_Print_In_Hex
(N
);
3100 elsif Is_Real_Type
(Target_Type
) then
3101 Fold_Ureal
(N
, Expr_Value_R
(Operand
), Stat
);
3104 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Stat
);
3107 Set_Is_Static_Expression
(N
, False);
3109 Check_String_Literal_Length
(N
, Target_Type
);
3115 -- The expression may be foldable but not static
3117 Set_Is_Static_Expression
(N
, Stat
);
3119 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3122 end Eval_Qualified_Expression
;
3124 -----------------------
3125 -- Eval_Real_Literal --
3126 -----------------------
3128 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3129 -- as static by the analyzer. The reason we did it that early is to allow
3130 -- the possibility of turning off the Is_Static_Expression flag after
3131 -- analysis, but before resolution, when integer literals are generated
3132 -- in the expander that do not correspond to static expressions.
3134 procedure Eval_Real_Literal
(N
: Node_Id
) is
3135 PK
: constant Node_Kind
:= Nkind
(Parent
(N
));
3138 -- If the literal appears in a non-expression context and not as part of
3139 -- a number declaration, then it is appearing in a non-static context,
3142 if PK
not in N_Subexpr
and then PK
/= N_Number_Declaration
then
3143 Check_Non_Static_Context
(N
);
3145 end Eval_Real_Literal
;
3147 ------------------------
3148 -- Eval_Relational_Op --
3149 ------------------------
3151 -- Relational operations are static functions, so the result is static if
3152 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3153 -- the result is never static, even if the operands are.
3155 -- However, for internally generated nodes, we allow string equality and
3156 -- inequality to be static. This is because we rewrite A in "ABC" as an
3157 -- equality test A = "ABC", and the former is definitely static.
3159 procedure Eval_Relational_Op
(N
: Node_Id
) is
3160 Left
: constant Node_Id
:= Left_Opnd
(N
);
3161 Right
: constant Node_Id
:= Right_Opnd
(N
);
3163 procedure Decompose_Expr
3165 Ent
: out Entity_Id
;
3166 Kind
: out Character;
3168 Orig
: Boolean := True);
3169 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3170 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3171 -- simple entity, and Cons is the value of K. If the expression is not
3172 -- of the required form, Ent is set to Empty.
3174 -- Orig indicates whether Expr is the original expression to consider,
3175 -- or if we are handling a subexpression (e.g. recursive call to
3178 procedure Fold_General_Op
(Is_Static
: Boolean);
3179 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3180 -- be set when the operator denotes a static expression.
3182 procedure Fold_Static_Real_Op
;
3183 -- Attempt to fold static real type relational operator N
3185 function Static_Length
(Expr
: Node_Id
) return Uint
;
3186 -- If Expr is an expression for a constrained array whose length is
3187 -- known at compile time, return the non-negative length, otherwise
3190 --------------------
3191 -- Decompose_Expr --
3192 --------------------
3194 procedure Decompose_Expr
3196 Ent
: out Entity_Id
;
3197 Kind
: out Character;
3199 Orig
: Boolean := True)
3204 -- Assume that the expression does not meet the expected form
3210 if Nkind
(Expr
) = N_Op_Add
3211 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3213 Exp
:= Left_Opnd
(Expr
);
3214 Cons
:= Expr_Value
(Right_Opnd
(Expr
));
3216 elsif Nkind
(Expr
) = N_Op_Subtract
3217 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3219 Exp
:= Left_Opnd
(Expr
);
3220 Cons
:= -Expr_Value
(Right_Opnd
(Expr
));
3222 -- If the bound is a constant created to remove side effects, recover
3223 -- the original expression to see if it has one of the recognizable
3226 elsif Nkind
(Expr
) = N_Identifier
3227 and then not Comes_From_Source
(Entity
(Expr
))
3228 and then Ekind
(Entity
(Expr
)) = E_Constant
3229 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
3231 Exp
:= Expression
(Parent
(Entity
(Expr
)));
3232 Decompose_Expr
(Exp
, Ent
, Kind
, Cons
, Orig
=> False);
3234 -- If original expression includes an entity, create a reference
3235 -- to it for use below.
3237 if Present
(Ent
) then
3238 Exp
:= New_Occurrence_Of
(Ent
, Sloc
(Ent
));
3244 -- Only consider the case of X + 0 for a full expression, and
3245 -- not when recursing, otherwise we may end up with evaluating
3246 -- expressions not known at compile time to 0.
3256 -- At this stage Exp is set to the potential X
3258 if Nkind
(Exp
) = N_Attribute_Reference
then
3259 if Attribute_Name
(Exp
) = Name_First
then
3261 elsif Attribute_Name
(Exp
) = Name_Last
then
3267 Exp
:= Prefix
(Exp
);
3273 if Is_Entity_Name
(Exp
) and then Present
(Entity
(Exp
)) then
3274 Ent
:= Entity
(Exp
);
3278 ---------------------
3279 -- Fold_General_Op --
3280 ---------------------
3282 procedure Fold_General_Op
(Is_Static
: Boolean) is
3283 CR
: constant Compare_Result
:=
3284 Compile_Time_Compare
(Left
, Right
, Assume_Valid
=> False);
3289 if CR
= Unknown
then
3297 elsif CR
= NE
or else CR
= GT
or else CR
= LT
then
3304 if CR
= GT
or else CR
= EQ
or else CR
= GE
then
3315 elsif CR
= EQ
or else CR
= LT
or else CR
= LE
then
3322 if CR
= LT
or else CR
= EQ
or else CR
= LE
then
3333 elsif CR
= EQ
or else CR
= GT
or else CR
= GE
then
3340 if CR
= NE
or else CR
= GT
or else CR
= LT
then
3349 raise Program_Error
;
3352 -- Determine the potential outcome of the relation assuming the
3353 -- operands are valid and emit a warning when the relation yields
3354 -- True or False only in the presence of invalid values.
3356 Warn_On_Constant_Valid_Condition
(N
);
3358 Fold_Uint
(N
, Test
(Result
), Is_Static
);
3359 end Fold_General_Op
;
3361 -------------------------
3362 -- Fold_Static_Real_Op --
3363 -------------------------
3365 procedure Fold_Static_Real_Op
is
3366 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
3367 Right_Real
: constant Ureal
:= Expr_Value_R
(Right
);
3372 when N_Op_Eq
=> Result
:= (Left_Real
= Right_Real
);
3373 when N_Op_Ge
=> Result
:= (Left_Real
>= Right_Real
);
3374 when N_Op_Gt
=> Result
:= (Left_Real
> Right_Real
);
3375 when N_Op_Le
=> Result
:= (Left_Real
<= Right_Real
);
3376 when N_Op_Lt
=> Result
:= (Left_Real
< Right_Real
);
3377 when N_Op_Ne
=> Result
:= (Left_Real
/= Right_Real
);
3378 when others => raise Program_Error
;
3381 Fold_Uint
(N
, Test
(Result
), True);
3382 end Fold_Static_Real_Op
;
3388 function Static_Length
(Expr
: Node_Id
) return Uint
is
3398 -- First easy case string literal
3400 if Nkind
(Expr
) = N_String_Literal
then
3401 return UI_From_Int
(String_Length
(Strval
(Expr
)));
3403 -- Second easy case, not constrained subtype, so no length
3405 elsif not Is_Constrained
(Etype
(Expr
)) then
3406 return Uint_Minus_1
;
3411 Typ
:= Etype
(First_Index
(Etype
(Expr
)));
3413 -- The simple case, both bounds are known at compile time
3415 if Is_Discrete_Type
(Typ
)
3416 and then Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
3417 and then Compile_Time_Known_Value
(Type_High_Bound
(Typ
))
3420 UI_Max
(Uint_0
, Expr_Value
(Type_High_Bound
(Typ
)) -
3421 Expr_Value
(Type_Low_Bound
(Typ
)) + 1);
3424 -- A more complex case, where the bounds are of the form X [+/- K1]
3425 -- .. X [+/- K2]), where X is an expression that is either A'First or
3426 -- A'Last (with A an entity name), or X is an entity name, and the
3427 -- two X's are the same and K1 and K2 are known at compile time, in
3428 -- this case, the length can also be computed at compile time, even
3429 -- though the bounds are not known. A common case of this is e.g.
3430 -- (X'First .. X'First+5).
3433 (Original_Node
(Type_Low_Bound
(Typ
)), Ent1
, Kind1
, Cons1
);
3435 (Original_Node
(Type_High_Bound
(Typ
)), Ent2
, Kind2
, Cons2
);
3437 if Present
(Ent1
) and then Ent1
= Ent2
and then Kind1
= Kind2
then
3438 return Cons2
- Cons1
+ 1;
3440 return Uint_Minus_1
;
3446 Left_Typ
: constant Entity_Id
:= Etype
(Left
);
3447 Right_Typ
: constant Entity_Id
:= Etype
(Right
);
3450 Op_Typ
: Entity_Id
:= Empty
;
3453 Is_Static_Expression
: Boolean;
3455 -- Start of processing for Eval_Relational_Op
3458 -- One special case to deal with first. If we can tell that the result
3459 -- will be false because the lengths of one or more index subtypes are
3460 -- compile-time known and different, then we can replace the entire
3461 -- result by False. We only do this for one-dimensional arrays, because
3462 -- the case of multidimensional arrays is rare and too much trouble. If
3463 -- one of the operands is an illegal aggregate, its type might still be
3464 -- an arbitrary composite type, so nothing to do.
3466 if Is_Array_Type
(Left_Typ
)
3467 and then Left_Typ
/= Any_Composite
3468 and then Number_Dimensions
(Left_Typ
) = 1
3469 and then Nkind_In
(N
, N_Op_Eq
, N_Op_Ne
)
3471 if Raises_Constraint_Error
(Left
)
3473 Raises_Constraint_Error
(Right
)
3477 -- OK, we have the case where we may be able to do this fold
3480 Left_Len
:= Static_Length
(Left
);
3481 Right_Len
:= Static_Length
(Right
);
3483 if Left_Len
/= Uint_Minus_1
3484 and then Right_Len
/= Uint_Minus_1
3485 and then Left_Len
/= Right_Len
3487 Fold_Uint
(N
, Test
(Nkind
(N
) = N_Op_Ne
), False);
3488 Warn_On_Known_Condition
(N
);
3496 -- Initialize the value of Is_Static_Expression. The value of Fold
3497 -- returned by Test_Expression_Is_Foldable is not needed since, even
3498 -- when some operand is a variable, we can still perform the static
3499 -- evaluation of the expression in some cases (for example, for a
3500 -- variable of a subtype of Integer we statically know that any value
3501 -- stored in such variable is smaller than Integer'Last).
3503 Test_Expression_Is_Foldable
3504 (N
, Left
, Right
, Is_Static_Expression
, Fold
);
3506 -- Only comparisons of scalars can give static results. A comparison
3507 -- of strings never yields a static result, even if both operands are
3508 -- static strings, except that as noted above, we allow equality and
3509 -- inequality for strings.
3511 if Is_String_Type
(Left_Typ
)
3512 and then not Comes_From_Source
(N
)
3513 and then Nkind_In
(N
, N_Op_Eq
, N_Op_Ne
)
3517 elsif not Is_Scalar_Type
(Left_Typ
) then
3518 Is_Static_Expression
:= False;
3519 Set_Is_Static_Expression
(N
, False);
3522 -- For operators on universal numeric types called as functions with
3523 -- an explicit scope, determine appropriate specific numeric type,
3524 -- and diagnose possible ambiguity.
3526 if Is_Universal_Numeric_Type
(Left_Typ
)
3528 Is_Universal_Numeric_Type
(Right_Typ
)
3530 Op_Typ
:= Find_Universal_Operator_Type
(N
);
3533 -- Attempt to fold the relational operator
3535 if Is_Static_Expression
and then Is_Real_Type
(Left_Typ
) then
3536 Fold_Static_Real_Op
;
3538 Fold_General_Op
(Is_Static_Expression
);
3542 -- For the case of a folded relational operator on a specific numeric
3543 -- type, freeze the operand type now.
3545 if Present
(Op_Typ
) then
3546 Freeze_Before
(N
, Op_Typ
);
3549 Warn_On_Known_Condition
(N
);
3550 end Eval_Relational_Op
;
3556 -- Shift operations are intrinsic operations that can never be static, so
3557 -- the only processing required is to perform the required check for a non
3558 -- static context for the two operands.
3560 -- Actually we could do some compile time evaluation here some time ???
3562 procedure Eval_Shift
(N
: Node_Id
) is
3564 Check_Non_Static_Context
(Left_Opnd
(N
));
3565 Check_Non_Static_Context
(Right_Opnd
(N
));
3568 ------------------------
3569 -- Eval_Short_Circuit --
3570 ------------------------
3572 -- A short circuit operation is potentially static if both operands are
3573 -- potentially static (RM 4.9 (13)).
3575 procedure Eval_Short_Circuit
(N
: Node_Id
) is
3576 Kind
: constant Node_Kind
:= Nkind
(N
);
3577 Left
: constant Node_Id
:= Left_Opnd
(N
);
3578 Right
: constant Node_Id
:= Right_Opnd
(N
);
3581 Rstat
: constant Boolean :=
3582 Is_Static_Expression
(Left
)
3584 Is_Static_Expression
(Right
);
3587 -- Short circuit operations are never static in Ada 83
3589 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
3590 Check_Non_Static_Context
(Left
);
3591 Check_Non_Static_Context
(Right
);
3595 -- Now look at the operands, we can't quite use the normal call to
3596 -- Test_Expression_Is_Foldable here because short circuit operations
3597 -- are a special case, they can still be foldable, even if the right
3598 -- operand raises constraint error.
3600 -- If either operand is Any_Type, just propagate to result and do not
3601 -- try to fold, this prevents cascaded errors.
3603 if Etype
(Left
) = Any_Type
or else Etype
(Right
) = Any_Type
then
3604 Set_Etype
(N
, Any_Type
);
3607 -- If left operand raises constraint error, then replace node N with
3608 -- the raise constraint error node, and we are obviously not foldable.
3609 -- Is_Static_Expression is set from the two operands in the normal way,
3610 -- and we check the right operand if it is in a non-static context.
3612 elsif Raises_Constraint_Error
(Left
) then
3614 Check_Non_Static_Context
(Right
);
3617 Rewrite_In_Raise_CE
(N
, Left
);
3618 Set_Is_Static_Expression
(N
, Rstat
);
3621 -- If the result is not static, then we won't in any case fold
3623 elsif not Rstat
then
3624 Check_Non_Static_Context
(Left
);
3625 Check_Non_Static_Context
(Right
);
3629 -- Here the result is static, note that, unlike the normal processing
3630 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3631 -- the right operand raises constraint error, that's because it is not
3632 -- significant if the left operand is decisive.
3634 Set_Is_Static_Expression
(N
);
3636 -- It does not matter if the right operand raises constraint error if
3637 -- it will not be evaluated. So deal specially with the cases where
3638 -- the right operand is not evaluated. Note that we will fold these
3639 -- cases even if the right operand is non-static, which is fine, but
3640 -- of course in these cases the result is not potentially static.
3642 Left_Int
:= Expr_Value
(Left
);
3644 if (Kind
= N_And_Then
and then Is_False
(Left_Int
))
3646 (Kind
= N_Or_Else
and then Is_True
(Left_Int
))
3648 Fold_Uint
(N
, Left_Int
, Rstat
);
3652 -- If first operand not decisive, then it does matter if the right
3653 -- operand raises constraint error, since it will be evaluated, so
3654 -- we simply replace the node with the right operand. Note that this
3655 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3656 -- (both are set to True in Right).
3658 if Raises_Constraint_Error
(Right
) then
3659 Rewrite_In_Raise_CE
(N
, Right
);
3660 Check_Non_Static_Context
(Left
);
3664 -- Otherwise the result depends on the right operand
3666 Fold_Uint
(N
, Expr_Value
(Right
), Rstat
);
3668 end Eval_Short_Circuit
;
3674 -- Slices can never be static, so the only processing required is to check
3675 -- for non-static context if an explicit range is given.
3677 procedure Eval_Slice
(N
: Node_Id
) is
3678 Drange
: constant Node_Id
:= Discrete_Range
(N
);
3681 if Nkind
(Drange
) = N_Range
then
3682 Check_Non_Static_Context
(Low_Bound
(Drange
));
3683 Check_Non_Static_Context
(High_Bound
(Drange
));
3686 -- A slice of the form A (subtype), when the subtype is the index of
3687 -- the type of A, is redundant, the slice can be replaced with A, and
3688 -- this is worth a warning.
3690 if Is_Entity_Name
(Prefix
(N
)) then
3692 E
: constant Entity_Id
:= Entity
(Prefix
(N
));
3693 T
: constant Entity_Id
:= Etype
(E
);
3696 if Ekind
(E
) = E_Constant
3697 and then Is_Array_Type
(T
)
3698 and then Is_Entity_Name
(Drange
)
3700 if Is_Entity_Name
(Original_Node
(First_Index
(T
)))
3701 and then Entity
(Original_Node
(First_Index
(T
)))
3704 if Warn_On_Redundant_Constructs
then
3705 Error_Msg_N
("redundant slice denotes whole array?r?", N
);
3708 -- The following might be a useful optimization???
3710 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3717 -------------------------
3718 -- Eval_String_Literal --
3719 -------------------------
3721 procedure Eval_String_Literal
(N
: Node_Id
) is
3722 Typ
: constant Entity_Id
:= Etype
(N
);
3723 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
3729 -- Nothing to do if error type (handles cases like default expressions
3730 -- or generics where we have not yet fully resolved the type).
3732 if Bas
= Any_Type
or else Bas
= Any_String
then
3736 -- String literals are static if the subtype is static (RM 4.9(2)), so
3737 -- reset the static expression flag (it was set unconditionally in
3738 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3739 -- the subtype is static by looking at the lower bound.
3741 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3742 if not Is_OK_Static_Expression
(String_Literal_Low_Bound
(Typ
)) then
3743 Set_Is_Static_Expression
(N
, False);
3747 -- Here if Etype of string literal is normal Etype (not yet possible,
3748 -- but may be possible in future).
3750 elsif not Is_OK_Static_Expression
3751 (Type_Low_Bound
(Etype
(First_Index
(Typ
))))
3753 Set_Is_Static_Expression
(N
, False);
3757 -- If original node was a type conversion, then result if non-static
3759 if Nkind
(Original_Node
(N
)) = N_Type_Conversion
then
3760 Set_Is_Static_Expression
(N
, False);
3764 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3765 -- if its bounds are outside the index base type and this index type is
3766 -- static. This can happen in only two ways. Either the string literal
3767 -- is too long, or it is null, and the lower bound is type'First. Either
3768 -- way it is the upper bound that is out of range of the index type.
3770 if Ada_Version
>= Ada_95
then
3771 if Is_Standard_String_Type
(Bas
) then
3772 Xtp
:= Standard_Positive
;
3774 Xtp
:= Etype
(First_Index
(Bas
));
3777 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3778 Lo
:= String_Literal_Low_Bound
(Typ
);
3780 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Typ
)));
3783 -- Check for string too long
3785 Len
:= String_Length
(Strval
(N
));
3787 if UI_From_Int
(Len
) > String_Type_Len
(Bas
) then
3789 -- Issue message. Note that this message is a warning if the
3790 -- string literal is not marked as static (happens in some cases
3791 -- of folding strings known at compile time, but not static).
3792 -- Furthermore in such cases, we reword the message, since there
3793 -- is no string literal in the source program.
3795 if Is_Static_Expression
(N
) then
3796 Apply_Compile_Time_Constraint_Error
3797 (N
, "string literal too long for}", CE_Length_Check_Failed
,
3799 Typ
=> First_Subtype
(Bas
));
3801 Apply_Compile_Time_Constraint_Error
3802 (N
, "string value too long for}", CE_Length_Check_Failed
,
3804 Typ
=> First_Subtype
(Bas
),
3808 -- Test for null string not allowed
3811 and then not Is_Generic_Type
(Xtp
)
3813 Expr_Value
(Lo
) = Expr_Value
(Type_Low_Bound
(Base_Type
(Xtp
)))
3815 -- Same specialization of message
3817 if Is_Static_Expression
(N
) then
3818 Apply_Compile_Time_Constraint_Error
3819 (N
, "null string literal not allowed for}",
3820 CE_Length_Check_Failed
,
3822 Typ
=> First_Subtype
(Bas
));
3824 Apply_Compile_Time_Constraint_Error
3825 (N
, "null string value not allowed for}",
3826 CE_Length_Check_Failed
,
3828 Typ
=> First_Subtype
(Bas
),
3833 end Eval_String_Literal
;
3835 --------------------------
3836 -- Eval_Type_Conversion --
3837 --------------------------
3839 -- A type conversion is potentially static if its subtype mark is for a
3840 -- static scalar subtype, and its operand expression is potentially static
3843 procedure Eval_Type_Conversion
(N
: Node_Id
) is
3844 Operand
: constant Node_Id
:= Expression
(N
);
3845 Source_Type
: constant Entity_Id
:= Etype
(Operand
);
3846 Target_Type
: constant Entity_Id
:= Etype
(N
);
3848 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean;
3849 -- Returns true if type T is an integer type, or if it is a fixed-point
3850 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3851 -- on the conversion node).
3853 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean;
3854 -- Returns true if type T is a floating-point type, or if it is a
3855 -- fixed-point type that is not to be treated as an integer (i.e. the
3856 -- flag Conversion_OK is not set on the conversion node).
3858 ------------------------------
3859 -- To_Be_Treated_As_Integer --
3860 ------------------------------
3862 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean is
3866 or else (Is_Fixed_Point_Type
(T
) and then Conversion_OK
(N
));
3867 end To_Be_Treated_As_Integer
;
3869 ---------------------------
3870 -- To_Be_Treated_As_Real --
3871 ---------------------------
3873 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean is
3876 Is_Floating_Point_Type
(T
)
3877 or else (Is_Fixed_Point_Type
(T
) and then not Conversion_OK
(N
));
3878 end To_Be_Treated_As_Real
;
3885 -- Start of processing for Eval_Type_Conversion
3888 -- Cannot fold if target type is non-static or if semantic error
3890 if not Is_Static_Subtype
(Target_Type
) then
3891 Check_Non_Static_Context
(Operand
);
3893 elsif Error_Posted
(N
) then
3897 -- If not foldable we are done
3899 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3904 -- Don't try fold if target type has constraint error bounds
3906 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3907 Set_Raises_Constraint_Error
(N
);
3911 -- Remaining processing depends on operand types. Note that in the
3912 -- following type test, fixed-point counts as real unless the flag
3913 -- Conversion_OK is set, in which case it counts as integer.
3915 -- Fold conversion, case of string type. The result is not static
3917 if Is_String_Type
(Target_Type
) then
3918 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Static
=> False);
3921 -- Fold conversion, case of integer target type
3923 elsif To_Be_Treated_As_Integer
(Target_Type
) then
3928 -- Integer to integer conversion
3930 if To_Be_Treated_As_Integer
(Source_Type
) then
3931 Result
:= Expr_Value
(Operand
);
3933 -- Real to integer conversion
3936 Result
:= UR_To_Uint
(Expr_Value_R
(Operand
));
3939 -- If fixed-point type (Conversion_OK must be set), then the
3940 -- result is logically an integer, but we must replace the
3941 -- conversion with the corresponding real literal, since the
3942 -- type from a semantic point of view is still fixed-point.
3944 if Is_Fixed_Point_Type
(Target_Type
) then
3946 (N
, UR_From_Uint
(Result
) * Small_Value
(Target_Type
), Stat
);
3948 -- Otherwise result is integer literal
3951 Fold_Uint
(N
, Result
, Stat
);
3955 -- Fold conversion, case of real target type
3957 elsif To_Be_Treated_As_Real
(Target_Type
) then
3962 if To_Be_Treated_As_Real
(Source_Type
) then
3963 Result
:= Expr_Value_R
(Operand
);
3965 Result
:= UR_From_Uint
(Expr_Value
(Operand
));
3968 Fold_Ureal
(N
, Result
, Stat
);
3971 -- Enumeration types
3974 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3977 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3981 end Eval_Type_Conversion
;
3987 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3988 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3990 procedure Eval_Unary_Op
(N
: Node_Id
) is
3991 Right
: constant Node_Id
:= Right_Opnd
(N
);
3992 Otype
: Entity_Id
:= Empty
;
3997 -- If not foldable we are done
3999 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
4005 if Etype
(Right
) = Universal_Integer
4007 Etype
(Right
) = Universal_Real
4009 Otype
:= Find_Universal_Operator_Type
(N
);
4012 -- Fold for integer case
4014 if Is_Integer_Type
(Etype
(N
)) then
4016 Rint
: constant Uint
:= Expr_Value
(Right
);
4020 -- In the case of modular unary plus and abs there is no need
4021 -- to adjust the result of the operation since if the original
4022 -- operand was in bounds the result will be in the bounds of the
4023 -- modular type. However, in the case of modular unary minus the
4024 -- result may go out of the bounds of the modular type and needs
4027 if Nkind
(N
) = N_Op_Plus
then
4030 elsif Nkind
(N
) = N_Op_Minus
then
4031 if Is_Modular_Integer_Type
(Etype
(N
)) then
4032 Result
:= (-Rint
) mod Modulus
(Etype
(N
));
4038 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
4042 Fold_Uint
(N
, Result
, Stat
);
4045 -- Fold for real case
4047 elsif Is_Real_Type
(Etype
(N
)) then
4049 Rreal
: constant Ureal
:= Expr_Value_R
(Right
);
4053 if Nkind
(N
) = N_Op_Plus
then
4055 elsif Nkind
(N
) = N_Op_Minus
then
4056 Result
:= UR_Negate
(Rreal
);
4058 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
4059 Result
:= abs Rreal
;
4062 Fold_Ureal
(N
, Result
, Stat
);
4066 -- If the operator was resolved to a specific type, make sure that type
4067 -- is frozen even if the expression is folded into a literal (which has
4068 -- a universal type).
4070 if Present
(Otype
) then
4071 Freeze_Before
(N
, Otype
);
4075 -------------------------------
4076 -- Eval_Unchecked_Conversion --
4077 -------------------------------
4079 -- Unchecked conversions can never be static, so the only required
4080 -- processing is to check for a non-static context for the operand.
4082 procedure Eval_Unchecked_Conversion
(N
: Node_Id
) is
4084 Check_Non_Static_Context
(Expression
(N
));
4085 end Eval_Unchecked_Conversion
;
4087 --------------------
4088 -- Expr_Rep_Value --
4089 --------------------
4091 function Expr_Rep_Value
(N
: Node_Id
) return Uint
is
4092 Kind
: constant Node_Kind
:= Nkind
(N
);
4096 if Is_Entity_Name
(N
) then
4099 -- An enumeration literal that was either in the source or created
4100 -- as a result of static evaluation.
4102 if Ekind
(Ent
) = E_Enumeration_Literal
then
4103 return Enumeration_Rep
(Ent
);
4105 -- A user defined static constant
4108 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4109 return Expr_Rep_Value
(Constant_Value
(Ent
));
4112 -- An integer literal that was either in the source or created as a
4113 -- result of static evaluation.
4115 elsif Kind
= N_Integer_Literal
then
4118 -- A real literal for a fixed-point type. This must be the fixed-point
4119 -- case, either the literal is of a fixed-point type, or it is a bound
4120 -- of a fixed-point type, with type universal real. In either case we
4121 -- obtain the desired value from Corresponding_Integer_Value.
4123 elsif Kind
= N_Real_Literal
then
4124 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4125 return Corresponding_Integer_Value
(N
);
4127 -- Otherwise must be character literal
4130 pragma Assert
(Kind
= N_Character_Literal
);
4133 -- Since Character literals of type Standard.Character don't have any
4134 -- defining character literals built for them, they do not have their
4135 -- Entity set, so just use their Char code. Otherwise for user-
4136 -- defined character literals use their Pos value as usual which is
4137 -- the same as the Rep value.
4140 return Char_Literal_Value
(N
);
4142 return Enumeration_Rep
(Ent
);
4151 function Expr_Value
(N
: Node_Id
) return Uint
is
4152 Kind
: constant Node_Kind
:= Nkind
(N
);
4153 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(N
) mod CV_Cache_Size
);
4158 -- If already in cache, then we know it's compile time known and we can
4159 -- return the value that was previously stored in the cache since
4160 -- compile time known values cannot change.
4162 if CV_Ent
.N
= N
then
4166 -- Otherwise proceed to test value
4168 if Is_Entity_Name
(N
) then
4171 -- An enumeration literal that was either in the source or created as
4172 -- a result of static evaluation.
4174 if Ekind
(Ent
) = E_Enumeration_Literal
then
4175 Val
:= Enumeration_Pos
(Ent
);
4177 -- A user defined static constant
4180 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4181 Val
:= Expr_Value
(Constant_Value
(Ent
));
4184 -- An integer literal that was either in the source or created as a
4185 -- result of static evaluation.
4187 elsif Kind
= N_Integer_Literal
then
4190 -- A real literal for a fixed-point type. This must be the fixed-point
4191 -- case, either the literal is of a fixed-point type, or it is a bound
4192 -- of a fixed-point type, with type universal real. In either case we
4193 -- obtain the desired value from Corresponding_Integer_Value.
4195 elsif Kind
= N_Real_Literal
then
4196 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4197 Val
:= Corresponding_Integer_Value
(N
);
4199 -- Otherwise must be character literal
4202 pragma Assert
(Kind
= N_Character_Literal
);
4205 -- Since Character literals of type Standard.Character don't
4206 -- have any defining character literals built for them, they
4207 -- do not have their Entity set, so just use their Char
4208 -- code. Otherwise for user-defined character literals use
4209 -- their Pos value as usual.
4212 Val
:= Char_Literal_Value
(N
);
4214 Val
:= Enumeration_Pos
(Ent
);
4218 -- Come here with Val set to value to be returned, set cache
4229 function Expr_Value_E
(N
: Node_Id
) return Entity_Id
is
4230 Ent
: constant Entity_Id
:= Entity
(N
);
4232 if Ekind
(Ent
) = E_Enumeration_Literal
then
4235 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4236 return Expr_Value_E
(Constant_Value
(Ent
));
4244 function Expr_Value_R
(N
: Node_Id
) return Ureal
is
4245 Kind
: constant Node_Kind
:= Nkind
(N
);
4249 if Kind
= N_Real_Literal
then
4252 elsif Kind
= N_Identifier
or else Kind
= N_Expanded_Name
then
4254 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4255 return Expr_Value_R
(Constant_Value
(Ent
));
4257 elsif Kind
= N_Integer_Literal
then
4258 return UR_From_Uint
(Expr_Value
(N
));
4260 -- Here, we have a node that cannot be interpreted as a compile time
4261 -- constant. That is definitely an error.
4264 raise Program_Error
;
4272 function Expr_Value_S
(N
: Node_Id
) return Node_Id
is
4274 if Nkind
(N
) = N_String_Literal
then
4277 pragma Assert
(Ekind
(Entity
(N
)) = E_Constant
);
4278 return Expr_Value_S
(Constant_Value
(Entity
(N
)));
4282 ----------------------------------
4283 -- Find_Universal_Operator_Type --
4284 ----------------------------------
4286 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
is
4287 PN
: constant Node_Id
:= Parent
(N
);
4288 Call
: constant Node_Id
:= Original_Node
(N
);
4289 Is_Int
: constant Boolean := Is_Integer_Type
(Etype
(N
));
4291 Is_Fix
: constant Boolean :=
4292 Nkind
(N
) in N_Binary_Op
4293 and then Nkind
(Right_Opnd
(N
)) /= Nkind
(Left_Opnd
(N
));
4294 -- A mixed-mode operation in this context indicates the presence of
4295 -- fixed-point type in the designated package.
4297 Is_Relational
: constant Boolean := Etype
(N
) = Standard_Boolean
;
4298 -- Case where N is a relational (or membership) operator (else it is an
4301 In_Membership
: constant Boolean :=
4302 Nkind
(PN
) in N_Membership_Test
4304 Nkind
(Right_Opnd
(PN
)) = N_Range
4306 Is_Universal_Numeric_Type
(Etype
(Left_Opnd
(PN
)))
4308 Is_Universal_Numeric_Type
4309 (Etype
(Low_Bound
(Right_Opnd
(PN
))))
4311 Is_Universal_Numeric_Type
4312 (Etype
(High_Bound
(Right_Opnd
(PN
))));
4313 -- Case where N is part of a membership test with a universal range
4317 Typ1
: Entity_Id
:= Empty
;
4320 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean;
4321 -- Check whether one operand is a mixed-mode operation that requires the
4322 -- presence of a fixed-point type. Given that all operands are universal
4323 -- and have been constant-folded, retrieve the original function call.
4325 ---------------------------
4326 -- Is_Mixed_Mode_Operand --
4327 ---------------------------
4329 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean is
4330 Onod
: constant Node_Id
:= Original_Node
(Op
);
4332 return Nkind
(Onod
) = N_Function_Call
4333 and then Present
(Next_Actual
(First_Actual
(Onod
)))
4334 and then Etype
(First_Actual
(Onod
)) /=
4335 Etype
(Next_Actual
(First_Actual
(Onod
)));
4336 end Is_Mixed_Mode_Operand
;
4338 -- Start of processing for Find_Universal_Operator_Type
4341 if Nkind
(Call
) /= N_Function_Call
4342 or else Nkind
(Name
(Call
)) /= N_Expanded_Name
4346 -- There are several cases where the context does not imply the type of
4348 -- - the universal expression appears in a type conversion;
4349 -- - the expression is a relational operator applied to universal
4351 -- - the expression is a membership test with a universal operand
4352 -- and a range with universal bounds.
4354 elsif Nkind
(Parent
(N
)) = N_Type_Conversion
4355 or else Is_Relational
4356 or else In_Membership
4358 Pack
:= Entity
(Prefix
(Name
(Call
)));
4360 -- If the prefix is a package declared elsewhere, iterate over its
4361 -- visible entities, otherwise iterate over all declarations in the
4362 -- designated scope.
4364 if Ekind
(Pack
) = E_Package
4365 and then not In_Open_Scopes
(Pack
)
4367 Priv_E
:= First_Private_Entity
(Pack
);
4373 E
:= First_Entity
(Pack
);
4374 while Present
(E
) and then E
/= Priv_E
loop
4375 if Is_Numeric_Type
(E
)
4376 and then Nkind
(Parent
(E
)) /= N_Subtype_Declaration
4377 and then Comes_From_Source
(E
)
4378 and then Is_Integer_Type
(E
) = Is_Int
4379 and then (Nkind
(N
) in N_Unary_Op
4380 or else Is_Relational
4381 or else Is_Fixed_Point_Type
(E
) = Is_Fix
)
4386 -- Before emitting an error, check for the presence of a
4387 -- mixed-mode operation that specifies a fixed point type.
4391 (Is_Mixed_Mode_Operand
(Left_Opnd
(N
))
4392 or else Is_Mixed_Mode_Operand
(Right_Opnd
(N
)))
4393 and then Is_Fixed_Point_Type
(E
) /= Is_Fixed_Point_Type
(Typ1
)
4396 if Is_Fixed_Point_Type
(E
) then
4401 -- More than one type of the proper class declared in P
4403 Error_Msg_N
("ambiguous operation", N
);
4404 Error_Msg_Sloc
:= Sloc
(Typ1
);
4405 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4406 Error_Msg_Sloc
:= Sloc
(E
);
4407 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4417 end Find_Universal_Operator_Type
;
4419 --------------------------
4420 -- Flag_Non_Static_Expr --
4421 --------------------------
4423 procedure Flag_Non_Static_Expr
(Msg
: String; Expr
: Node_Id
) is
4425 if Error_Posted
(Expr
) and then not All_Errors_Mode
then
4428 Error_Msg_F
(Msg
, Expr
);
4429 Why_Not_Static
(Expr
);
4431 end Flag_Non_Static_Expr
;
4437 procedure Fold_Str
(N
: Node_Id
; Val
: String_Id
; Static
: Boolean) is
4438 Loc
: constant Source_Ptr
:= Sloc
(N
);
4439 Typ
: constant Entity_Id
:= Etype
(N
);
4442 if Raises_Constraint_Error
(N
) then
4443 Set_Is_Static_Expression
(N
, Static
);
4447 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Val
));
4449 -- We now have the literal with the right value, both the actual type
4450 -- and the expected type of this literal are taken from the expression
4451 -- that was evaluated. So now we do the Analyze and Resolve.
4453 -- Note that we have to reset Is_Static_Expression both after the
4454 -- analyze step (because Resolve will evaluate the literal, which
4455 -- will cause semantic errors if it is marked as static), and after
4456 -- the Resolve step (since Resolve in some cases resets this flag).
4459 Set_Is_Static_Expression
(N
, Static
);
4462 Set_Is_Static_Expression
(N
, Static
);
4469 procedure Fold_Uint
(N
: Node_Id
; Val
: Uint
; Static
: Boolean) is
4470 Loc
: constant Source_Ptr
:= Sloc
(N
);
4471 Typ
: Entity_Id
:= Etype
(N
);
4475 if Raises_Constraint_Error
(N
) then
4476 Set_Is_Static_Expression
(N
, Static
);
4480 -- If we are folding a named number, retain the entity in the literal,
4483 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Integer
then
4489 if Is_Private_Type
(Typ
) then
4490 Typ
:= Full_View
(Typ
);
4493 -- For a result of type integer, substitute an N_Integer_Literal node
4494 -- for the result of the compile time evaluation of the expression.
4495 -- For ASIS use, set a link to the original named number when not in
4496 -- a generic context.
4498 if Is_Integer_Type
(Typ
) then
4499 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
4500 Set_Original_Entity
(N
, Ent
);
4502 -- Otherwise we have an enumeration type, and we substitute either
4503 -- an N_Identifier or N_Character_Literal to represent the enumeration
4504 -- literal corresponding to the given value, which must always be in
4505 -- range, because appropriate tests have already been made for this.
4507 else pragma Assert
(Is_Enumeration_Type
(Typ
));
4508 Rewrite
(N
, Get_Enum_Lit_From_Pos
(Etype
(N
), Val
, Loc
));
4511 -- We now have the literal with the right value, both the actual type
4512 -- and the expected type of this literal are taken from the expression
4513 -- that was evaluated. So now we do the Analyze and Resolve.
4515 -- Note that we have to reset Is_Static_Expression both after the
4516 -- analyze step (because Resolve will evaluate the literal, which
4517 -- will cause semantic errors if it is marked as static), and after
4518 -- the Resolve step (since Resolve in some cases sets this flag).
4521 Set_Is_Static_Expression
(N
, Static
);
4524 Set_Is_Static_Expression
(N
, Static
);
4531 procedure Fold_Ureal
(N
: Node_Id
; Val
: Ureal
; Static
: Boolean) is
4532 Loc
: constant Source_Ptr
:= Sloc
(N
);
4533 Typ
: constant Entity_Id
:= Etype
(N
);
4537 if Raises_Constraint_Error
(N
) then
4538 Set_Is_Static_Expression
(N
, Static
);
4542 -- If we are folding a named number, retain the entity in the literal,
4545 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Real
then
4551 Rewrite
(N
, Make_Real_Literal
(Loc
, Realval
=> Val
));
4553 -- Set link to original named number, for ASIS use
4555 Set_Original_Entity
(N
, Ent
);
4557 -- We now have the literal with the right value, both the actual type
4558 -- and the expected type of this literal are taken from the expression
4559 -- that was evaluated. So now we do the Analyze and Resolve.
4561 -- Note that we have to reset Is_Static_Expression both after the
4562 -- analyze step (because Resolve will evaluate the literal, which
4563 -- will cause semantic errors if it is marked as static), and after
4564 -- the Resolve step (since Resolve in some cases sets this flag).
4567 Set_Is_Static_Expression
(N
, Static
);
4570 Set_Is_Static_Expression
(N
, Static
);
4577 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
is
4581 for J
in 0 .. B
'Last loop
4587 if Non_Binary_Modulus
(T
) then
4588 V
:= V
mod Modulus
(T
);
4594 --------------------
4595 -- Get_String_Val --
4596 --------------------
4598 function Get_String_Val
(N
: Node_Id
) return Node_Id
is
4600 if Nkind_In
(N
, N_String_Literal
, N_Character_Literal
) then
4603 pragma Assert
(Is_Entity_Name
(N
));
4604 return Get_String_Val
(Constant_Value
(Entity
(N
)));
4612 procedure Initialize
is
4614 CV_Cache
:= (others => (Node_High_Bound
, Uint_0
));
4617 --------------------
4618 -- In_Subrange_Of --
4619 --------------------
4621 function In_Subrange_Of
4624 Fixed_Int
: Boolean := False) return Boolean
4633 if T1
= T2
or else Is_Subtype_Of
(T1
, T2
) then
4636 -- Never in range if both types are not scalar. Don't know if this can
4637 -- actually happen, but just in case.
4639 elsif not Is_Scalar_Type
(T1
) or else not Is_Scalar_Type
(T2
) then
4642 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4643 -- definitely not compatible with T2.
4645 elsif Is_Floating_Point_Type
(T1
)
4646 and then Has_Infinities
(T1
)
4647 and then Is_Floating_Point_Type
(T2
)
4648 and then not Has_Infinities
(T2
)
4653 L1
:= Type_Low_Bound
(T1
);
4654 H1
:= Type_High_Bound
(T1
);
4656 L2
:= Type_Low_Bound
(T2
);
4657 H2
:= Type_High_Bound
(T2
);
4659 -- Check bounds to see if comparison possible at compile time
4661 if Compile_Time_Compare
(L1
, L2
, Assume_Valid
=> True) in Compare_GE
4663 Compile_Time_Compare
(H1
, H2
, Assume_Valid
=> True) in Compare_LE
4668 -- If bounds not comparable at compile time, then the bounds of T2
4669 -- must be compile time known or we cannot answer the query.
4671 if not Compile_Time_Known_Value
(L2
)
4672 or else not Compile_Time_Known_Value
(H2
)
4677 -- If the bounds of T1 are know at compile time then use these
4678 -- ones, otherwise use the bounds of the base type (which are of
4679 -- course always static).
4681 if not Compile_Time_Known_Value
(L1
) then
4682 L1
:= Type_Low_Bound
(Base_Type
(T1
));
4685 if not Compile_Time_Known_Value
(H1
) then
4686 H1
:= Type_High_Bound
(Base_Type
(T1
));
4689 -- Fixed point types should be considered as such only if
4690 -- flag Fixed_Int is set to False.
4692 if Is_Floating_Point_Type
(T1
) or else Is_Floating_Point_Type
(T2
)
4693 or else (Is_Fixed_Point_Type
(T1
) and then not Fixed_Int
)
4694 or else (Is_Fixed_Point_Type
(T2
) and then not Fixed_Int
)
4697 Expr_Value_R
(L2
) <= Expr_Value_R
(L1
)
4699 Expr_Value_R
(H2
) >= Expr_Value_R
(H1
);
4703 Expr_Value
(L2
) <= Expr_Value
(L1
)
4705 Expr_Value
(H2
) >= Expr_Value
(H1
);
4710 -- If any exception occurs, it means that we have some bug in the compiler
4711 -- possibly triggered by a previous error, or by some unforeseen peculiar
4712 -- occurrence. However, this is only an optimization attempt, so there is
4713 -- really no point in crashing the compiler. Instead we just decide, too
4714 -- bad, we can't figure out the answer in this case after all.
4719 -- Debug flag K disables this behavior (useful for debugging)
4721 if Debug_Flag_K
then
4732 function Is_In_Range
4735 Assume_Valid
: Boolean := False;
4736 Fixed_Int
: Boolean := False;
4737 Int_Real
: Boolean := False) return Boolean
4741 Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) = In_Range
;
4748 function Is_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
4749 Typ
: constant Entity_Id
:= Etype
(Lo
);
4752 if not Compile_Time_Known_Value
(Lo
)
4753 or else not Compile_Time_Known_Value
(Hi
)
4758 if Is_Discrete_Type
(Typ
) then
4759 return Expr_Value
(Lo
) > Expr_Value
(Hi
);
4760 else pragma Assert
(Is_Real_Type
(Typ
));
4761 return Expr_Value_R
(Lo
) > Expr_Value_R
(Hi
);
4765 -------------------------
4766 -- Is_OK_Static_Choice --
4767 -------------------------
4769 function Is_OK_Static_Choice
(Choice
: Node_Id
) return Boolean is
4771 -- Check various possibilities for choice
4773 -- Note: for membership tests, we test more cases than are possible
4774 -- (in particular subtype indication), but it doesn't matter because
4775 -- it just won't occur (we have already done a syntax check).
4777 if Nkind
(Choice
) = N_Others_Choice
then
4780 elsif Nkind
(Choice
) = N_Range
then
4781 return Is_OK_Static_Range
(Choice
);
4783 elsif Nkind
(Choice
) = N_Subtype_Indication
4784 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
4786 return Is_OK_Static_Subtype
(Etype
(Choice
));
4789 return Is_OK_Static_Expression
(Choice
);
4791 end Is_OK_Static_Choice
;
4793 ------------------------------
4794 -- Is_OK_Static_Choice_List --
4795 ------------------------------
4797 function Is_OK_Static_Choice_List
(Choices
: List_Id
) return Boolean is
4801 if not Is_Static_Choice_List
(Choices
) then
4805 Choice
:= First
(Choices
);
4806 while Present
(Choice
) loop
4807 if not Is_OK_Static_Choice
(Choice
) then
4808 Set_Raises_Constraint_Error
(Choice
);
4816 end Is_OK_Static_Choice_List
;
4818 -----------------------------
4819 -- Is_OK_Static_Expression --
4820 -----------------------------
4822 function Is_OK_Static_Expression
(N
: Node_Id
) return Boolean is
4824 return Is_Static_Expression
(N
) and then not Raises_Constraint_Error
(N
);
4825 end Is_OK_Static_Expression
;
4827 ------------------------
4828 -- Is_OK_Static_Range --
4829 ------------------------
4831 -- A static range is a range whose bounds are static expressions, or a
4832 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4833 -- We have already converted range attribute references, so we get the
4834 -- "or" part of this rule without needing a special test.
4836 function Is_OK_Static_Range
(N
: Node_Id
) return Boolean is
4838 return Is_OK_Static_Expression
(Low_Bound
(N
))
4839 and then Is_OK_Static_Expression
(High_Bound
(N
));
4840 end Is_OK_Static_Range
;
4842 --------------------------
4843 -- Is_OK_Static_Subtype --
4844 --------------------------
4846 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4847 -- neither bound raises constraint error when evaluated.
4849 function Is_OK_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4850 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4851 Anc_Subt
: Entity_Id
;
4854 -- First a quick check on the non static subtype flag. As described
4855 -- in further detail in Einfo, this flag is not decisive in all cases,
4856 -- but if it is set, then the subtype is definitely non-static.
4858 if Is_Non_Static_Subtype
(Typ
) then
4862 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4864 if Anc_Subt
= Empty
then
4868 if Is_Generic_Type
(Root_Type
(Base_T
))
4869 or else Is_Generic_Actual_Type
(Base_T
)
4873 elsif Has_Dynamic_Predicate_Aspect
(Typ
) then
4878 elsif Is_String_Type
(Typ
) then
4880 Ekind
(Typ
) = E_String_Literal_Subtype
4882 (Is_OK_Static_Subtype
(Component_Type
(Typ
))
4883 and then Is_OK_Static_Subtype
(Etype
(First_Index
(Typ
))));
4887 elsif Is_Scalar_Type
(Typ
) then
4888 if Base_T
= Typ
then
4892 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4893 -- Get_Type_{Low,High}_Bound.
4895 return Is_OK_Static_Subtype
(Anc_Subt
)
4896 and then Is_OK_Static_Expression
(Type_Low_Bound
(Typ
))
4897 and then Is_OK_Static_Expression
(Type_High_Bound
(Typ
));
4900 -- Types other than string and scalar types are never static
4905 end Is_OK_Static_Subtype
;
4907 ---------------------
4908 -- Is_Out_Of_Range --
4909 ---------------------
4911 function Is_Out_Of_Range
4914 Assume_Valid
: Boolean := False;
4915 Fixed_Int
: Boolean := False;
4916 Int_Real
: Boolean := False) return Boolean
4919 return Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) =
4921 end Is_Out_Of_Range
;
4923 ----------------------
4924 -- Is_Static_Choice --
4925 ----------------------
4927 function Is_Static_Choice
(Choice
: Node_Id
) return Boolean is
4929 -- Check various possibilities for choice
4931 -- Note: for membership tests, we test more cases than are possible
4932 -- (in particular subtype indication), but it doesn't matter because
4933 -- it just won't occur (we have already done a syntax check).
4935 if Nkind
(Choice
) = N_Others_Choice
then
4938 elsif Nkind
(Choice
) = N_Range
then
4939 return Is_Static_Range
(Choice
);
4941 elsif Nkind
(Choice
) = N_Subtype_Indication
4942 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
4944 return Is_Static_Subtype
(Etype
(Choice
));
4947 return Is_Static_Expression
(Choice
);
4949 end Is_Static_Choice
;
4951 ---------------------------
4952 -- Is_Static_Choice_List --
4953 ---------------------------
4955 function Is_Static_Choice_List
(Choices
: List_Id
) return Boolean is
4959 Choice
:= First
(Choices
);
4960 while Present
(Choice
) loop
4961 if not Is_Static_Choice
(Choice
) then
4969 end Is_Static_Choice_List
;
4971 ---------------------
4972 -- Is_Static_Range --
4973 ---------------------
4975 -- A static range is a range whose bounds are static expressions, or a
4976 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4977 -- We have already converted range attribute references, so we get the
4978 -- "or" part of this rule without needing a special test.
4980 function Is_Static_Range
(N
: Node_Id
) return Boolean is
4982 return Is_Static_Expression
(Low_Bound
(N
))
4984 Is_Static_Expression
(High_Bound
(N
));
4985 end Is_Static_Range
;
4987 -----------------------
4988 -- Is_Static_Subtype --
4989 -----------------------
4991 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4993 function Is_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4994 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4995 Anc_Subt
: Entity_Id
;
4998 -- First a quick check on the non static subtype flag. As described
4999 -- in further detail in Einfo, this flag is not decisive in all cases,
5000 -- but if it is set, then the subtype is definitely non-static.
5002 if Is_Non_Static_Subtype
(Typ
) then
5006 Anc_Subt
:= Ancestor_Subtype
(Typ
);
5008 if Anc_Subt
= Empty
then
5012 if Is_Generic_Type
(Root_Type
(Base_T
))
5013 or else Is_Generic_Actual_Type
(Base_T
)
5017 -- If there is a dynamic predicate for the type (declared or inherited)
5018 -- the expression is not static.
5020 elsif Has_Dynamic_Predicate_Aspect
(Typ
)
5021 or else (Is_Derived_Type
(Typ
)
5022 and then Has_Aspect
(Typ
, Aspect_Dynamic_Predicate
))
5028 elsif Is_String_Type
(Typ
) then
5030 Ekind
(Typ
) = E_String_Literal_Subtype
5031 or else (Is_Static_Subtype
(Component_Type
(Typ
))
5032 and then Is_Static_Subtype
(Etype
(First_Index
(Typ
))));
5036 elsif Is_Scalar_Type
(Typ
) then
5037 if Base_T
= Typ
then
5041 return Is_Static_Subtype
(Anc_Subt
)
5042 and then Is_Static_Expression
(Type_Low_Bound
(Typ
))
5043 and then Is_Static_Expression
(Type_High_Bound
(Typ
));
5046 -- Types other than string and scalar types are never static
5051 end Is_Static_Subtype
;
5053 -------------------------------
5054 -- Is_Statically_Unevaluated --
5055 -------------------------------
5057 function Is_Statically_Unevaluated
(Expr
: Node_Id
) return Boolean is
5058 function Check_Case_Expr_Alternative
5059 (CEA
: Node_Id
) return Match_Result
;
5060 -- We have a message emanating from the Expression of a case expression
5061 -- alternative. We examine this alternative, as follows:
5063 -- If the selecting expression of the parent case is non-static, or
5064 -- if any of the discrete choices of the given case alternative are
5065 -- non-static or raise Constraint_Error, return Non_Static.
5067 -- Otherwise check if the selecting expression matches any of the given
5068 -- discrete choices. If so, the alternative is executed and we return
5069 -- Match, otherwise, the alternative can never be executed, and so we
5072 ---------------------------------
5073 -- Check_Case_Expr_Alternative --
5074 ---------------------------------
5076 function Check_Case_Expr_Alternative
5077 (CEA
: Node_Id
) return Match_Result
5079 Case_Exp
: constant Node_Id
:= Parent
(CEA
);
5084 pragma Assert
(Nkind
(Case_Exp
) = N_Case_Expression
);
5086 -- Check that selecting expression is static
5088 if not Is_OK_Static_Expression
(Expression
(Case_Exp
)) then
5092 if not Is_OK_Static_Choice_List
(Discrete_Choices
(CEA
)) then
5096 -- All choices are now known to be static. Now see if alternative
5097 -- matches one of the choices.
5099 Choice
:= First
(Discrete_Choices
(CEA
));
5100 while Present
(Choice
) loop
5102 -- Check various possibilities for choice, returning Match if we
5103 -- find the selecting value matches any of the choices. Note that
5104 -- we know we are the last choice, so we don't have to keep going.
5106 if Nkind
(Choice
) = N_Others_Choice
then
5108 -- Others choice is a bit annoying, it matches if none of the
5109 -- previous alternatives matches (note that we know we are the
5110 -- last alternative in this case, so we can just go backwards
5111 -- from us to see if any previous one matches).
5113 Prev_CEA
:= Prev
(CEA
);
5114 while Present
(Prev_CEA
) loop
5115 if Check_Case_Expr_Alternative
(Prev_CEA
) = Match
then
5124 -- Else we have a normal static choice
5126 elsif Choice_Matches
(Expression
(Case_Exp
), Choice
) = Match
then
5130 -- If we fall through, it means that the discrete choice did not
5131 -- match the selecting expression, so continue.
5136 -- If we get through that loop then all choices were static, and none
5137 -- of them matched the selecting expression. So return No_Match.
5140 end Check_Case_Expr_Alternative
;
5148 -- Start of processing for Is_Statically_Unevaluated
5151 -- The (32.x) references here are from RM section 4.9
5153 -- (32.1) An expression is statically unevaluated if it is part of ...
5155 -- This means we have to climb the tree looking for one of the cases
5162 -- (32.2) The right operand of a static short-circuit control form
5163 -- whose value is determined by its left operand.
5165 -- AND THEN with False as left operand
5167 if Nkind
(P
) = N_And_Then
5168 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5169 and then Is_False
(Expr_Value
(Left_Opnd
(P
)))
5173 -- OR ELSE with True as left operand
5175 elsif Nkind
(P
) = N_Or_Else
5176 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5177 and then Is_True
(Expr_Value
(Left_Opnd
(P
)))
5181 -- (32.3) A dependent_expression of an if_expression whose associated
5182 -- condition is static and equals False.
5184 elsif Nkind
(P
) = N_If_Expression
then
5186 Cond
: constant Node_Id
:= First
(Expressions
(P
));
5187 Texp
: constant Node_Id
:= Next
(Cond
);
5188 Fexp
: constant Node_Id
:= Next
(Texp
);
5191 if Compile_Time_Known_Value
(Cond
) then
5193 -- Condition is True and we are in the right operand
5195 if Is_True
(Expr_Value
(Cond
)) and then OldP
= Fexp
then
5198 -- Condition is False and we are in the left operand
5200 elsif Is_False
(Expr_Value
(Cond
)) and then OldP
= Texp
then
5206 -- (32.4) A condition or dependent_expression of an if_expression
5207 -- where the condition corresponding to at least one preceding
5208 -- dependent_expression of the if_expression is static and equals
5211 -- This refers to cases like
5213 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5215 -- But we expand elsif's out anyway, so the above looks like:
5217 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5219 -- So for us this is caught by the above check for the 32.3 case.
5221 -- (32.5) A dependent_expression of a case_expression whose
5222 -- selecting_expression is static and whose value is not covered
5223 -- by the corresponding discrete_choice_list.
5225 elsif Nkind
(P
) = N_Case_Expression_Alternative
then
5227 -- First, we have to be in the expression to suppress messages.
5228 -- If we are within one of the choices, we want the message.
5230 if OldP
= Expression
(P
) then
5232 -- Statically unevaluated if alternative does not match
5234 if Check_Case_Expr_Alternative
(P
) = No_Match
then
5239 -- (32.6) A choice_expression (or a simple_expression of a range
5240 -- that occurs as a membership_choice of a membership_choice_list)
5241 -- of a static membership test that is preceded in the enclosing
5242 -- membership_choice_list by another item whose individual
5243 -- membership test (see (RM 4.5.2)) statically yields True.
5245 elsif Nkind
(P
) in N_Membership_Test
then
5247 -- Only possibly unevaluated if simple expression is static
5249 if not Is_OK_Static_Expression
(Left_Opnd
(P
)) then
5252 -- All members of the choice list must be static
5254 elsif (Present
(Right_Opnd
(P
))
5255 and then not Is_OK_Static_Choice
(Right_Opnd
(P
)))
5256 or else (Present
(Alternatives
(P
))
5258 not Is_OK_Static_Choice_List
(Alternatives
(P
)))
5262 -- If expression is the one and only alternative, then it is
5263 -- definitely not statically unevaluated, so we only have to
5264 -- test the case where there are alternatives present.
5266 elsif Present
(Alternatives
(P
)) then
5268 -- Look for previous matching Choice
5270 Choice
:= First
(Alternatives
(P
));
5271 while Present
(Choice
) loop
5273 -- If we reached us and no previous choices matched, this
5274 -- is not the case where we are statically unevaluated.
5276 exit when OldP
= Choice
;
5278 -- If a previous choice matches, then that is the case where
5279 -- we know our choice is statically unevaluated.
5281 if Choice_Matches
(Left_Opnd
(P
), Choice
) = Match
then
5288 -- If we fall through the loop, we were not one of the choices,
5289 -- we must have been the expression, so that is not covered by
5290 -- this rule, and we keep going.
5296 -- OK, not statically unevaluated at this level, see if we should
5297 -- keep climbing to look for a higher level reason.
5299 -- Special case for component association in aggregates, where
5300 -- we want to keep climbing up to the parent aggregate.
5302 if Nkind
(P
) = N_Component_Association
5303 and then Nkind
(Parent
(P
)) = N_Aggregate
5307 -- All done if not still within subexpression
5310 exit when Nkind
(P
) not in N_Subexpr
;
5314 -- If we fall through the loop, not one of the cases covered!
5317 end Is_Statically_Unevaluated
;
5319 --------------------
5320 -- Not_Null_Range --
5321 --------------------
5323 function Not_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
5324 Typ
: constant Entity_Id
:= Etype
(Lo
);
5327 if not Compile_Time_Known_Value
(Lo
)
5328 or else not Compile_Time_Known_Value
(Hi
)
5333 if Is_Discrete_Type
(Typ
) then
5334 return Expr_Value
(Lo
) <= Expr_Value
(Hi
);
5335 else pragma Assert
(Is_Real_Type
(Typ
));
5336 return Expr_Value_R
(Lo
) <= Expr_Value_R
(Hi
);
5344 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean is
5346 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5348 if Bits
< 500_000
then
5351 -- Error if this maximum is exceeded
5354 Error_Msg_N
("static value too large, capacity exceeded", N
);
5363 procedure Out_Of_Range
(N
: Node_Id
) is
5365 -- If we have the static expression case, then this is an illegality
5366 -- in Ada 95 mode, except that in an instance, we never generate an
5367 -- error (if the error is legitimate, it was already diagnosed in the
5370 if Is_Static_Expression
(N
)
5371 and then not In_Instance
5372 and then not In_Inlined_Body
5373 and then Ada_Version
>= Ada_95
5375 -- No message if we are statically unevaluated
5377 if Is_Statically_Unevaluated
(N
) then
5380 -- The expression to compute the length of a packed array is attached
5381 -- to the array type itself, and deserves a separate message.
5383 elsif Nkind
(Parent
(N
)) = N_Defining_Identifier
5384 and then Is_Array_Type
(Parent
(N
))
5385 and then Present
(Packed_Array_Impl_Type
(Parent
(N
)))
5386 and then Present
(First_Rep_Item
(Parent
(N
)))
5389 ("length of packed array must not exceed Integer''Last",
5390 First_Rep_Item
(Parent
(N
)));
5391 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Uint_1
));
5393 -- All cases except the special array case
5396 Apply_Compile_Time_Constraint_Error
5397 (N
, "value not in range of}", CE_Range_Check_Failed
);
5400 -- Here we generate a warning for the Ada 83 case, or when we are in an
5401 -- instance, or when we have a non-static expression case.
5404 Apply_Compile_Time_Constraint_Error
5405 (N
, "value not in range of}??", CE_Range_Check_Failed
);
5409 ----------------------
5410 -- Predicates_Match --
5411 ----------------------
5413 function Predicates_Match
(T1
, T2
: Entity_Id
) return Boolean is
5418 if Ada_Version
< Ada_2012
then
5421 -- Both types must have predicates or lack them
5423 elsif Has_Predicates
(T1
) /= Has_Predicates
(T2
) then
5426 -- Check matching predicates
5431 (T1
, Name_Static_Predicate
, Check_Parents
=> False);
5434 (T2
, Name_Static_Predicate
, Check_Parents
=> False);
5436 -- Subtypes statically match if the predicate comes from the
5437 -- same declaration, which can only happen if one is a subtype
5438 -- of the other and has no explicit predicate.
5440 -- Suppress warnings on order of actuals, which is otherwise
5441 -- triggered by one of the two calls below.
5443 pragma Warnings
(Off
);
5444 return Pred1
= Pred2
5445 or else (No
(Pred1
) and then Is_Subtype_Of
(T1
, T2
))
5446 or else (No
(Pred2
) and then Is_Subtype_Of
(T2
, T1
));
5447 pragma Warnings
(On
);
5449 end Predicates_Match
;
5451 ---------------------------------------------
5452 -- Real_Or_String_Static_Predicate_Matches --
5453 ---------------------------------------------
5455 function Real_Or_String_Static_Predicate_Matches
5457 Typ
: Entity_Id
) return Boolean
5459 Expr
: constant Node_Id
:= Static_Real_Or_String_Predicate
(Typ
);
5460 -- The predicate expression from the type
5462 Pfun
: constant Entity_Id
:= Predicate_Function
(Typ
);
5463 -- The entity for the predicate function
5465 Ent_Name
: constant Name_Id
:= Chars
(First_Formal
(Pfun
));
5466 -- The name of the formal of the predicate function. Occurrences of the
5467 -- type name in Expr have been rewritten as references to this formal,
5468 -- and it has a unique name, so we can identify references by this name.
5471 -- Copy of the predicate function tree
5473 function Process
(N
: Node_Id
) return Traverse_Result
;
5474 -- Function used to process nodes during the traversal in which we will
5475 -- find occurrences of the entity name, and replace such occurrences
5476 -- by a real literal with the value to be tested.
5478 procedure Traverse
is new Traverse_Proc
(Process
);
5479 -- The actual traversal procedure
5485 function Process
(N
: Node_Id
) return Traverse_Result
is
5487 if Nkind
(N
) = N_Identifier
and then Chars
(N
) = Ent_Name
then
5489 Nod
: constant Node_Id
:= New_Copy
(Val
);
5491 Set_Sloc
(Nod
, Sloc
(N
));
5496 -- The predicate function may contain string-comparison operations
5497 -- that have been converted into calls to run-time array-comparison
5498 -- routines. To evaluate the predicate statically, we recover the
5499 -- original comparison operation and replace the occurrence of the
5500 -- formal by the static string value. The actuals of the generated
5501 -- call are of the form X'Address.
5503 elsif Nkind
(N
) in N_Op_Compare
5504 and then Nkind
(Left_Opnd
(N
)) = N_Function_Call
5507 C
: constant Node_Id
:= Left_Opnd
(N
);
5508 F
: constant Node_Id
:= First
(Parameter_Associations
(C
));
5509 L
: constant Node_Id
:= Prefix
(F
);
5510 R
: constant Node_Id
:= Prefix
(Next
(F
));
5513 -- If an operand is an entity name, it is the formal of the
5514 -- predicate function, so replace it with the string value.
5515 -- It may be either operand in the call. The other operand
5516 -- is a static string from the original predicate.
5518 if Is_Entity_Name
(L
) then
5519 Rewrite
(Left_Opnd
(N
), New_Copy
(Val
));
5520 Rewrite
(Right_Opnd
(N
), New_Copy
(R
));
5523 Rewrite
(Left_Opnd
(N
), New_Copy
(L
));
5524 Rewrite
(Right_Opnd
(N
), New_Copy
(Val
));
5535 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5538 -- First deal with special case of inherited predicate, where the
5539 -- predicate expression looks like:
5541 -- xxPredicate (typ (Ent)) and then Expr
5543 -- where Expr is the predicate expression for this level, and the
5544 -- left operand is the call to evaluate the inherited predicate.
5546 if Nkind
(Expr
) = N_And_Then
5547 and then Nkind
(Left_Opnd
(Expr
)) = N_Function_Call
5548 and then Is_Predicate_Function
(Entity
(Name
(Left_Opnd
(Expr
))))
5550 -- OK we have the inherited case, so make a call to evaluate the
5551 -- inherited predicate. If that fails, so do we!
5554 Real_Or_String_Static_Predicate_Matches
5556 Typ
=> Etype
(First_Formal
(Entity
(Name
(Left_Opnd
(Expr
))))))
5561 -- Use the right operand for the continued processing
5563 Copy
:= Copy_Separate_Tree
(Right_Opnd
(Expr
));
5565 -- Case where call to predicate function appears on its own (this means
5566 -- that the predicate at this level is just inherited from the parent).
5568 elsif Nkind
(Expr
) = N_Function_Call
then
5570 Typ
: constant Entity_Id
:=
5571 Etype
(First_Formal
(Entity
(Name
(Expr
))));
5574 -- If the inherited predicate is dynamic, just ignore it. We can't
5575 -- go trying to evaluate a dynamic predicate as a static one!
5577 if Has_Dynamic_Predicate_Aspect
(Typ
) then
5580 -- Otherwise inherited predicate is static, check for match
5583 return Real_Or_String_Static_Predicate_Matches
(Val
, Typ
);
5587 -- If not just an inherited predicate, copy whole expression
5590 Copy
:= Copy_Separate_Tree
(Expr
);
5593 -- Now we replace occurrences of the entity by the value
5597 -- And analyze the resulting static expression to see if it is True
5599 Analyze_And_Resolve
(Copy
, Standard_Boolean
);
5600 return Is_True
(Expr_Value
(Copy
));
5601 end Real_Or_String_Static_Predicate_Matches
;
5603 -------------------------
5604 -- Rewrite_In_Raise_CE --
5605 -------------------------
5607 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
) is
5608 Typ
: constant Entity_Id
:= Etype
(N
);
5609 Stat
: constant Boolean := Is_Static_Expression
(N
);
5612 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5613 -- can just clear the condition if the reason is appropriate. We do
5614 -- not do this operation if the parent has a reason other than range
5615 -- check failed, because otherwise we would change the reason.
5617 if Present
(Parent
(N
))
5618 and then Nkind
(Parent
(N
)) = N_Raise_Constraint_Error
5619 and then Reason
(Parent
(N
)) =
5620 UI_From_Int
(RT_Exception_Code
'Pos (CE_Range_Check_Failed
))
5622 Set_Condition
(Parent
(N
), Empty
);
5624 -- Else build an explicit N_Raise_CE
5628 Make_Raise_Constraint_Error
(Sloc
(Exp
),
5629 Reason
=> CE_Range_Check_Failed
));
5630 Set_Raises_Constraint_Error
(N
);
5634 -- Set proper flags in result
5636 Set_Raises_Constraint_Error
(N
, True);
5637 Set_Is_Static_Expression
(N
, Stat
);
5638 end Rewrite_In_Raise_CE
;
5640 ---------------------
5641 -- String_Type_Len --
5642 ---------------------
5644 function String_Type_Len
(Stype
: Entity_Id
) return Uint
is
5645 NT
: constant Entity_Id
:= Etype
(First_Index
(Stype
));
5649 if Is_OK_Static_Subtype
(NT
) then
5652 T
:= Base_Type
(NT
);
5655 return Expr_Value
(Type_High_Bound
(T
)) -
5656 Expr_Value
(Type_Low_Bound
(T
)) + 1;
5657 end String_Type_Len
;
5659 ------------------------------------
5660 -- Subtypes_Statically_Compatible --
5661 ------------------------------------
5663 function Subtypes_Statically_Compatible
5666 Formal_Derived_Matching
: Boolean := False) return Boolean
5671 if Is_Scalar_Type
(T1
) then
5673 -- Definitely compatible if we match
5675 if Subtypes_Statically_Match
(T1
, T2
) then
5678 -- If either subtype is nonstatic then they're not compatible
5680 elsif not Is_OK_Static_Subtype
(T1
)
5682 not Is_OK_Static_Subtype
(T2
)
5686 -- Base types must match, but we don't check that (should we???) but
5687 -- we do at least check that both types are real, or both types are
5690 elsif Is_Real_Type
(T1
) /= Is_Real_Type
(T2
) then
5693 -- Here we check the bounds
5697 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5698 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5699 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5700 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5703 if Is_Real_Type
(T1
) then
5705 Expr_Value_R
(LB1
) > Expr_Value_R
(HB1
)
5707 (Expr_Value_R
(LB2
) <= Expr_Value_R
(LB1
)
5708 and then Expr_Value_R
(HB1
) <= Expr_Value_R
(HB2
));
5712 Expr_Value
(LB1
) > Expr_Value
(HB1
)
5714 (Expr_Value
(LB2
) <= Expr_Value
(LB1
)
5715 and then Expr_Value
(HB1
) <= Expr_Value
(HB2
));
5722 elsif Is_Access_Type
(T1
) then
5724 (not Is_Constrained
(T2
)
5725 or else Subtypes_Statically_Match
5726 (Designated_Type
(T1
), Designated_Type
(T2
)))
5727 and then not (Can_Never_Be_Null
(T2
)
5728 and then not Can_Never_Be_Null
(T1
));
5734 (Is_Composite_Type
(T1
) and then not Is_Constrained
(T2
))
5735 or else Subtypes_Statically_Match
5736 (T1
, T2
, Formal_Derived_Matching
);
5738 end Subtypes_Statically_Compatible
;
5740 -------------------------------
5741 -- Subtypes_Statically_Match --
5742 -------------------------------
5744 -- Subtypes statically match if they have statically matching constraints
5745 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5746 -- they are the same identical constraint, or if they are static and the
5747 -- values match (RM 4.9.1(1)).
5749 -- In addition, in GNAT, the object size (Esize) values of the types must
5750 -- match if they are set (unless checking an actual for a formal derived
5751 -- type). The use of 'Object_Size can cause this to be false even if the
5752 -- types would otherwise match in the RM sense.
5754 function Subtypes_Statically_Match
5757 Formal_Derived_Matching
: Boolean := False) return Boolean
5760 -- A type always statically matches itself
5765 -- No match if sizes different (from use of 'Object_Size). This test
5766 -- is excluded if Formal_Derived_Matching is True, as the base types
5767 -- can be different in that case and typically have different sizes
5768 -- (and Esizes can be set when Frontend_Layout_On_Target is True).
5770 elsif not Formal_Derived_Matching
5771 and then Known_Static_Esize
(T1
)
5772 and then Known_Static_Esize
(T2
)
5773 and then Esize
(T1
) /= Esize
(T2
)
5777 -- No match if predicates do not match
5779 elsif not Predicates_Match
(T1
, T2
) then
5784 elsif Is_Scalar_Type
(T1
) then
5786 -- Base types must be the same
5788 if Base_Type
(T1
) /= Base_Type
(T2
) then
5792 -- A constrained numeric subtype never matches an unconstrained
5793 -- subtype, i.e. both types must be constrained or unconstrained.
5795 -- To understand the requirement for this test, see RM 4.9.1(1).
5796 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5797 -- a constrained subtype with constraint bounds matching the bounds
5798 -- of its corresponding unconstrained base type. In this situation,
5799 -- Integer and Integer'Base do not statically match, even though
5800 -- they have the same bounds.
5802 -- We only apply this test to types in Standard and types that appear
5803 -- in user programs. That way, we do not have to be too careful about
5804 -- setting Is_Constrained right for Itypes.
5806 if Is_Numeric_Type
(T1
)
5807 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5808 and then (Scope
(T1
) = Standard_Standard
5809 or else Comes_From_Source
(T1
))
5810 and then (Scope
(T2
) = Standard_Standard
5811 or else Comes_From_Source
(T2
))
5815 -- A generic scalar type does not statically match its base type
5816 -- (AI-311). In this case we make sure that the formals, which are
5817 -- first subtypes of their bases, are constrained.
5819 elsif Is_Generic_Type
(T1
)
5820 and then Is_Generic_Type
(T2
)
5821 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5826 -- If there was an error in either range, then just assume the types
5827 -- statically match to avoid further junk errors.
5829 if No
(Scalar_Range
(T1
)) or else No
(Scalar_Range
(T2
))
5830 or else Error_Posted
(Scalar_Range
(T1
))
5831 or else Error_Posted
(Scalar_Range
(T2
))
5836 -- Otherwise both types have bounds that can be compared
5839 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5840 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5841 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5842 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5845 -- If the bounds are the same tree node, then match (common case)
5847 if LB1
= LB2
and then HB1
= HB2
then
5850 -- Otherwise bounds must be static and identical value
5853 if not Is_OK_Static_Subtype
(T1
)
5855 not Is_OK_Static_Subtype
(T2
)
5859 elsif Is_Real_Type
(T1
) then
5861 Expr_Value_R
(LB1
) = Expr_Value_R
(LB2
)
5863 Expr_Value_R
(HB1
) = Expr_Value_R
(HB2
);
5867 Expr_Value
(LB1
) = Expr_Value
(LB2
)
5869 Expr_Value
(HB1
) = Expr_Value
(HB2
);
5874 -- Type with discriminants
5876 elsif Has_Discriminants
(T1
) or else Has_Discriminants
(T2
) then
5878 -- Because of view exchanges in multiple instantiations, conformance
5879 -- checking might try to match a partial view of a type with no
5880 -- discriminants with a full view that has defaulted discriminants.
5881 -- In such a case, use the discriminant constraint of the full view,
5882 -- which must exist because we know that the two subtypes have the
5885 if Has_Discriminants
(T1
) /= Has_Discriminants
(T2
) then
5886 -- A generic actual type is declared through a subtype declaration
5887 -- and may have an inconsistent indication of the presence of
5888 -- discriminants, so check the type it renames.
5890 if Is_Generic_Actual_Type
(T1
)
5891 and then not Has_Discriminants
(Etype
(T1
))
5892 and then not Has_Discriminants
(T2
)
5896 elsif In_Instance
then
5897 if Is_Private_Type
(T2
)
5898 and then Present
(Full_View
(T2
))
5899 and then Has_Discriminants
(Full_View
(T2
))
5901 return Subtypes_Statically_Match
(T1
, Full_View
(T2
));
5903 elsif Is_Private_Type
(T1
)
5904 and then Present
(Full_View
(T1
))
5905 and then Has_Discriminants
(Full_View
(T1
))
5907 return Subtypes_Statically_Match
(Full_View
(T1
), T2
);
5918 DL1
: constant Elist_Id
:= Discriminant_Constraint
(T1
);
5919 DL2
: constant Elist_Id
:= Discriminant_Constraint
(T2
);
5927 elsif Is_Constrained
(T1
) /= Is_Constrained
(T2
) then
5931 -- Now loop through the discriminant constraints
5933 -- Note: the guard here seems necessary, since it is possible at
5934 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5936 if Present
(DL1
) and then Present
(DL2
) then
5937 DA1
:= First_Elmt
(DL1
);
5938 DA2
:= First_Elmt
(DL2
);
5939 while Present
(DA1
) loop
5941 Expr1
: constant Node_Id
:= Node
(DA1
);
5942 Expr2
: constant Node_Id
:= Node
(DA2
);
5945 if not Is_OK_Static_Expression
(Expr1
)
5946 or else not Is_OK_Static_Expression
(Expr2
)
5950 -- If either expression raised a constraint error,
5951 -- consider the expressions as matching, since this
5952 -- helps to prevent cascading errors.
5954 elsif Raises_Constraint_Error
(Expr1
)
5955 or else Raises_Constraint_Error
(Expr2
)
5959 elsif Expr_Value
(Expr1
) /= Expr_Value
(Expr2
) then
5972 -- A definite type does not match an indefinite or classwide type.
5973 -- However, a generic type with unknown discriminants may be
5974 -- instantiated with a type with no discriminants, and conformance
5975 -- checking on an inherited operation may compare the actual with the
5976 -- subtype that renames it in the instance.
5978 elsif Has_Unknown_Discriminants
(T1
) /= Has_Unknown_Discriminants
(T2
)
5981 Is_Generic_Actual_Type
(T1
) or else Is_Generic_Actual_Type
(T2
);
5985 elsif Is_Array_Type
(T1
) then
5987 -- If either subtype is unconstrained then both must be, and if both
5988 -- are unconstrained then no further checking is needed.
5990 if not Is_Constrained
(T1
) or else not Is_Constrained
(T2
) then
5991 return not (Is_Constrained
(T1
) or else Is_Constrained
(T2
));
5994 -- Both subtypes are constrained, so check that the index subtypes
5995 -- statically match.
5998 Index1
: Node_Id
:= First_Index
(T1
);
5999 Index2
: Node_Id
:= First_Index
(T2
);
6002 while Present
(Index1
) loop
6004 Subtypes_Statically_Match
(Etype
(Index1
), Etype
(Index2
))
6009 Next_Index
(Index1
);
6010 Next_Index
(Index2
);
6016 elsif Is_Access_Type
(T1
) then
6017 if Can_Never_Be_Null
(T1
) /= Can_Never_Be_Null
(T2
) then
6020 elsif Ekind_In
(T1
, E_Access_Subprogram_Type
,
6021 E_Anonymous_Access_Subprogram_Type
)
6025 (Designated_Type
(T1
),
6026 Designated_Type
(T2
));
6029 Subtypes_Statically_Match
6030 (Designated_Type
(T1
),
6031 Designated_Type
(T2
))
6032 and then Is_Access_Constant
(T1
) = Is_Access_Constant
(T2
);
6035 -- All other types definitely match
6040 end Subtypes_Statically_Match
;
6046 function Test
(Cond
: Boolean) return Uint
is
6055 ---------------------
6056 -- Test_Comparison --
6057 ---------------------
6059 procedure Test_Comparison
6061 Assume_Valid
: Boolean;
6062 True_Result
: out Boolean;
6063 False_Result
: out Boolean)
6065 Left
: constant Node_Id
:= Left_Opnd
(Op
);
6066 Left_Typ
: constant Entity_Id
:= Etype
(Left
);
6067 Orig_Op
: constant Node_Id
:= Original_Node
(Op
);
6069 procedure Replacement_Warning
(Msg
: String);
6070 -- Emit a warning on a comparison that can be replaced by '='
6072 -------------------------
6073 -- Replacement_Warning --
6074 -------------------------
6076 procedure Replacement_Warning
(Msg
: String) is
6078 if Constant_Condition_Warnings
6079 and then Comes_From_Source
(Orig_Op
)
6080 and then Is_Integer_Type
(Left_Typ
)
6081 and then not Error_Posted
(Op
)
6082 and then not Has_Warnings_Off
(Left_Typ
)
6083 and then not In_Instance
6085 Error_Msg_N
(Msg
, Op
);
6087 end Replacement_Warning
;
6091 Res
: constant Compare_Result
:=
6092 Compile_Time_Compare
(Left
, Right_Opnd
(Op
), Assume_Valid
);
6094 -- Start of processing for Test_Comparison
6097 case N_Op_Compare
(Nkind
(Op
)) is
6099 True_Result
:= Res
= EQ
;
6100 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
6103 True_Result
:= Res
in Compare_GE
;
6104 False_Result
:= Res
= LT
;
6106 if Res
= LE
and then Nkind
(Orig_Op
) = N_Op_Ge
then
6108 ("can never be greater than, could replace by ""'=""?c?");
6112 True_Result
:= Res
= GT
;
6113 False_Result
:= Res
in Compare_LE
;
6116 True_Result
:= Res
in Compare_LE
;
6117 False_Result
:= Res
= GT
;
6119 if Res
= GE
and then Nkind
(Orig_Op
) = N_Op_Le
then
6121 ("can never be less than, could replace by ""'=""?c?");
6125 True_Result
:= Res
= LT
;
6126 False_Result
:= Res
in Compare_GE
;
6129 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
6130 False_Result
:= Res
= EQ
;
6132 end Test_Comparison
;
6134 ---------------------------------
6135 -- Test_Expression_Is_Foldable --
6136 ---------------------------------
6140 procedure Test_Expression_Is_Foldable
6150 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
6154 -- If operand is Any_Type, just propagate to result and do not
6155 -- try to fold, this prevents cascaded errors.
6157 if Etype
(Op1
) = Any_Type
then
6158 Set_Etype
(N
, Any_Type
);
6161 -- If operand raises constraint error, then replace node N with the
6162 -- raise constraint error node, and we are obviously not foldable.
6163 -- Note that this replacement inherits the Is_Static_Expression flag
6164 -- from the operand.
6166 elsif Raises_Constraint_Error
(Op1
) then
6167 Rewrite_In_Raise_CE
(N
, Op1
);
6170 -- If the operand is not static, then the result is not static, and
6171 -- all we have to do is to check the operand since it is now known
6172 -- to appear in a non-static context.
6174 elsif not Is_Static_Expression
(Op1
) then
6175 Check_Non_Static_Context
(Op1
);
6176 Fold
:= Compile_Time_Known_Value
(Op1
);
6179 -- An expression of a formal modular type is not foldable because
6180 -- the modulus is unknown.
6182 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
6183 and then Is_Generic_Type
(Etype
(Op1
))
6185 Check_Non_Static_Context
(Op1
);
6188 -- Here we have the case of an operand whose type is OK, which is
6189 -- static, and which does not raise constraint error, we can fold.
6192 Set_Is_Static_Expression
(N
);
6196 end Test_Expression_Is_Foldable
;
6200 procedure Test_Expression_Is_Foldable
6206 CRT_Safe
: Boolean := False)
6208 Rstat
: constant Boolean := Is_Static_Expression
(Op1
)
6210 Is_Static_Expression
(Op2
);
6216 -- Inhibit folding if -gnatd.f flag set
6218 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
6222 -- If either operand is Any_Type, just propagate to result and
6223 -- do not try to fold, this prevents cascaded errors.
6225 if Etype
(Op1
) = Any_Type
or else Etype
(Op2
) = Any_Type
then
6226 Set_Etype
(N
, Any_Type
);
6229 -- If left operand raises constraint error, then replace node N with the
6230 -- Raise_Constraint_Error node, and we are obviously not foldable.
6231 -- Is_Static_Expression is set from the two operands in the normal way,
6232 -- and we check the right operand if it is in a non-static context.
6234 elsif Raises_Constraint_Error
(Op1
) then
6236 Check_Non_Static_Context
(Op2
);
6239 Rewrite_In_Raise_CE
(N
, Op1
);
6240 Set_Is_Static_Expression
(N
, Rstat
);
6243 -- Similar processing for the case of the right operand. Note that we
6244 -- don't use this routine for the short-circuit case, so we do not have
6245 -- to worry about that special case here.
6247 elsif Raises_Constraint_Error
(Op2
) then
6249 Check_Non_Static_Context
(Op1
);
6252 Rewrite_In_Raise_CE
(N
, Op2
);
6253 Set_Is_Static_Expression
(N
, Rstat
);
6256 -- Exclude expressions of a generic modular type, as above
6258 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
6259 and then Is_Generic_Type
(Etype
(Op1
))
6261 Check_Non_Static_Context
(Op1
);
6264 -- If result is not static, then check non-static contexts on operands
6265 -- since one of them may be static and the other one may not be static.
6267 elsif not Rstat
then
6268 Check_Non_Static_Context
(Op1
);
6269 Check_Non_Static_Context
(Op2
);
6272 Fold
:= CRT_Safe_Compile_Time_Known_Value
(Op1
)
6273 and then CRT_Safe_Compile_Time_Known_Value
(Op2
);
6275 Fold
:= Compile_Time_Known_Value
(Op1
)
6276 and then Compile_Time_Known_Value
(Op2
);
6281 -- Else result is static and foldable. Both operands are static, and
6282 -- neither raises constraint error, so we can definitely fold.
6285 Set_Is_Static_Expression
(N
);
6290 end Test_Expression_Is_Foldable
;
6296 function Test_In_Range
6299 Assume_Valid
: Boolean;
6300 Fixed_Int
: Boolean;
6301 Int_Real
: Boolean) return Range_Membership
6306 pragma Warnings
(Off
, Assume_Valid
);
6307 -- For now Assume_Valid is unreferenced since the current implementation
6308 -- always returns Unknown if N is not a compile time known value, but we
6309 -- keep the parameter to allow for future enhancements in which we try
6310 -- to get the information in the variable case as well.
6313 -- If an error was posted on expression, then return Unknown, we do not
6314 -- want cascaded errors based on some false analysis of a junk node.
6316 if Error_Posted
(N
) then
6319 -- Expression that raises constraint error is an odd case. We certainly
6320 -- do not want to consider it to be in range. It might make sense to
6321 -- consider it always out of range, but this causes incorrect error
6322 -- messages about static expressions out of range. So we just return
6323 -- Unknown, which is always safe.
6325 elsif Raises_Constraint_Error
(N
) then
6328 -- Universal types have no range limits, so always in range
6330 elsif Typ
= Universal_Integer
or else Typ
= Universal_Real
then
6333 -- Never known if not scalar type. Don't know if this can actually
6334 -- happen, but our spec allows it, so we must check.
6336 elsif not Is_Scalar_Type
(Typ
) then
6339 -- Never known if this is a generic type, since the bounds of generic
6340 -- types are junk. Note that if we only checked for static expressions
6341 -- (instead of compile time known values) below, we would not need this
6342 -- check, because values of a generic type can never be static, but they
6343 -- can be known at compile time.
6345 elsif Is_Generic_Type
(Typ
) then
6348 -- Case of a known compile time value, where we can check if it is in
6349 -- the bounds of the given type.
6351 elsif Compile_Time_Known_Value
(N
) then
6360 Lo
:= Type_Low_Bound
(Typ
);
6361 Hi
:= Type_High_Bound
(Typ
);
6363 LB_Known
:= Compile_Time_Known_Value
(Lo
);
6364 HB_Known
:= Compile_Time_Known_Value
(Hi
);
6366 -- Fixed point types should be considered as such only if flag
6367 -- Fixed_Int is set to False.
6369 if Is_Floating_Point_Type
(Typ
)
6370 or else (Is_Fixed_Point_Type
(Typ
) and then not Fixed_Int
)
6373 Valr
:= Expr_Value_R
(N
);
6375 if LB_Known
and HB_Known
then
6376 if Valr
>= Expr_Value_R
(Lo
)
6378 Valr
<= Expr_Value_R
(Hi
)
6382 return Out_Of_Range
;
6385 elsif (LB_Known
and then Valr
< Expr_Value_R
(Lo
))
6387 (HB_Known
and then Valr
> Expr_Value_R
(Hi
))
6389 return Out_Of_Range
;
6396 Val
:= Expr_Value
(N
);
6398 if LB_Known
and HB_Known
then
6399 if Val
>= Expr_Value
(Lo
) and then Val
<= Expr_Value
(Hi
)
6403 return Out_Of_Range
;
6406 elsif (LB_Known
and then Val
< Expr_Value
(Lo
))
6408 (HB_Known
and then Val
> Expr_Value
(Hi
))
6410 return Out_Of_Range
;
6418 -- Here for value not known at compile time. Case of expression subtype
6419 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6420 -- In this case we know it is in range without knowing its value.
6423 and then (Etype
(N
) = Typ
or else Is_Subtype_Of
(Etype
(N
), Typ
))
6427 -- Another special case. For signed integer types, if the target type
6428 -- has Is_Known_Valid set, and the source type does not have a larger
6429 -- size, then the source value must be in range. We exclude biased
6430 -- types, because they bizarrely can generate out of range values.
6432 elsif Is_Signed_Integer_Type
(Etype
(N
))
6433 and then Is_Known_Valid
(Typ
)
6434 and then Esize
(Etype
(N
)) <= Esize
(Typ
)
6435 and then not Has_Biased_Representation
(Etype
(N
))
6439 -- For all other cases, result is unknown
6450 procedure To_Bits
(U
: Uint
; B
: out Bits
) is
6452 for J
in 0 .. B
'Last loop
6453 B
(J
) := (U
/ (2 ** J
)) mod 2 /= 0;
6457 --------------------
6458 -- Why_Not_Static --
6459 --------------------
6461 procedure Why_Not_Static
(Expr
: Node_Id
) is
6462 N
: constant Node_Id
:= Original_Node
(Expr
);
6463 Typ
: Entity_Id
:= Empty
;
6468 procedure Why_Not_Static_List
(L
: List_Id
);
6469 -- A version that can be called on a list of expressions. Finds all
6470 -- non-static violations in any element of the list.
6472 -------------------------
6473 -- Why_Not_Static_List --
6474 -------------------------
6476 procedure Why_Not_Static_List
(L
: List_Id
) is
6479 if Is_Non_Empty_List
(L
) then
6481 while Present
(N
) loop
6486 end Why_Not_Static_List
;
6488 -- Start of processing for Why_Not_Static
6491 -- Ignore call on error or empty node
6493 if No
(Expr
) or else Nkind
(Expr
) = N_Error
then
6497 -- Preprocessing for sub expressions
6499 if Nkind
(Expr
) in N_Subexpr
then
6501 -- Nothing to do if expression is static
6503 if Is_OK_Static_Expression
(Expr
) then
6507 -- Test for constraint error raised
6509 if Raises_Constraint_Error
(Expr
) then
6511 -- Special case membership to find out which piece to flag
6513 if Nkind
(N
) in N_Membership_Test
then
6514 if Raises_Constraint_Error
(Left_Opnd
(N
)) then
6515 Why_Not_Static
(Left_Opnd
(N
));
6518 elsif Present
(Right_Opnd
(N
))
6519 and then Raises_Constraint_Error
(Right_Opnd
(N
))
6521 Why_Not_Static
(Right_Opnd
(N
));
6525 pragma Assert
(Present
(Alternatives
(N
)));
6527 Alt
:= First
(Alternatives
(N
));
6528 while Present
(Alt
) loop
6529 if Raises_Constraint_Error
(Alt
) then
6530 Why_Not_Static
(Alt
);
6538 -- Special case a range to find out which bound to flag
6540 elsif Nkind
(N
) = N_Range
then
6541 if Raises_Constraint_Error
(Low_Bound
(N
)) then
6542 Why_Not_Static
(Low_Bound
(N
));
6545 elsif Raises_Constraint_Error
(High_Bound
(N
)) then
6546 Why_Not_Static
(High_Bound
(N
));
6550 -- Special case attribute to see which part to flag
6552 elsif Nkind
(N
) = N_Attribute_Reference
then
6553 if Raises_Constraint_Error
(Prefix
(N
)) then
6554 Why_Not_Static
(Prefix
(N
));
6558 if Present
(Expressions
(N
)) then
6559 Exp
:= First
(Expressions
(N
));
6560 while Present
(Exp
) loop
6561 if Raises_Constraint_Error
(Exp
) then
6562 Why_Not_Static
(Exp
);
6570 -- Special case a subtype name
6572 elsif Is_Entity_Name
(Expr
) and then Is_Type
(Entity
(Expr
)) then
6574 ("!& is not a static subtype (RM 4.9(26))", N
, Entity
(Expr
));
6578 -- End of special cases
6581 ("!expression raises exception, cannot be static (RM 4.9(34))",
6586 -- If no type, then something is pretty wrong, so ignore
6588 Typ
:= Etype
(Expr
);
6594 -- Type must be scalar or string type (but allow Bignum, since this
6595 -- is really a scalar type from our point of view in this diagnosis).
6597 if not Is_Scalar_Type
(Typ
)
6598 and then not Is_String_Type
(Typ
)
6599 and then not Is_RTE
(Typ
, RE_Bignum
)
6602 ("!static expression must have scalar or string type " &
6608 -- If we got through those checks, test particular node kind
6614 when N_Expanded_Name
6620 if Is_Named_Number
(E
) then
6623 elsif Ekind
(E
) = E_Constant
then
6625 -- One case we can give a metter message is when we have a
6626 -- string literal created by concatenating an aggregate with
6627 -- an others expression.
6629 Entity_Case
: declare
6630 CV
: constant Node_Id
:= Constant_Value
(E
);
6631 CO
: constant Node_Id
:= Original_Node
(CV
);
6633 function Is_Aggregate
(N
: Node_Id
) return Boolean;
6634 -- See if node N came from an others aggregate, if so
6635 -- return True and set Error_Msg_Sloc to aggregate.
6641 function Is_Aggregate
(N
: Node_Id
) return Boolean is
6643 if Nkind
(Original_Node
(N
)) = N_Aggregate
then
6644 Error_Msg_Sloc
:= Sloc
(Original_Node
(N
));
6647 elsif Is_Entity_Name
(N
)
6648 and then Ekind
(Entity
(N
)) = E_Constant
6650 Nkind
(Original_Node
(Constant_Value
(Entity
(N
)))) =
6654 Sloc
(Original_Node
(Constant_Value
(Entity
(N
))));
6662 -- Start of processing for Entity_Case
6665 if Is_Aggregate
(CV
)
6666 or else (Nkind
(CO
) = N_Op_Concat
6667 and then (Is_Aggregate
(Left_Opnd
(CO
))
6669 Is_Aggregate
(Right_Opnd
(CO
))))
6671 Error_Msg_N
("!aggregate (#) is never static", N
);
6673 elsif No
(CV
) or else not Is_Static_Expression
(CV
) then
6675 ("!& is not a static constant (RM 4.9(5))", N
, E
);
6679 elsif Is_Type
(E
) then
6681 ("!& is not a static subtype (RM 4.9(26))", N
, E
);
6685 ("!& is not static constant or named number "
6686 & "(RM 4.9(5))", N
, E
);
6695 if Nkind
(N
) in N_Op_Shift
then
6697 ("!shift functions are never static (RM 4.9(6,18))", N
);
6699 Why_Not_Static
(Left_Opnd
(N
));
6700 Why_Not_Static
(Right_Opnd
(N
));
6706 Why_Not_Static
(Right_Opnd
(N
));
6708 -- Attribute reference
6710 when N_Attribute_Reference
=>
6711 Why_Not_Static_List
(Expressions
(N
));
6713 E
:= Etype
(Prefix
(N
));
6715 if E
= Standard_Void_Type
then
6719 -- Special case non-scalar'Size since this is a common error
6721 if Attribute_Name
(N
) = Name_Size
then
6723 ("!size attribute is only static for static scalar type "
6724 & "(RM 4.9(7,8))", N
);
6728 elsif Is_Array_Type
(E
) then
6729 if not Nam_In
(Attribute_Name
(N
), Name_First
,
6734 ("!static array attribute must be Length, First, or Last "
6735 & "(RM 4.9(8))", N
);
6737 -- Since we know the expression is not-static (we already
6738 -- tested for this, must mean array is not static).
6742 ("!prefix is non-static array (RM 4.9(8))", Prefix
(N
));
6747 -- Special case generic types, since again this is a common source
6750 elsif Is_Generic_Actual_Type
(E
) or else Is_Generic_Type
(E
) then
6752 ("!attribute of generic type is never static "
6753 & "(RM 4.9(7,8))", N
);
6755 elsif Is_OK_Static_Subtype
(E
) then
6758 elsif Is_Scalar_Type
(E
) then
6760 ("!prefix type for attribute is not static scalar subtype "
6761 & "(RM 4.9(7))", N
);
6765 ("!static attribute must apply to array/scalar type "
6766 & "(RM 4.9(7,8))", N
);
6771 when N_String_Literal
=>
6773 ("!subtype of string literal is non-static (RM 4.9(4))", N
);
6775 -- Explicit dereference
6777 when N_Explicit_Dereference
=>
6779 ("!explicit dereference is never static (RM 4.9)", N
);
6783 when N_Function_Call
=>
6784 Why_Not_Static_List
(Parameter_Associations
(N
));
6786 -- Complain about non-static function call unless we have Bignum
6787 -- which means that the underlying expression is really some
6788 -- scalar arithmetic operation.
6790 if not Is_RTE
(Typ
, RE_Bignum
) then
6791 Error_Msg_N
("!non-static function call (RM 4.9(6,18))", N
);
6794 -- Parameter assocation (test actual parameter)
6796 when N_Parameter_Association
=>
6797 Why_Not_Static
(Explicit_Actual_Parameter
(N
));
6799 -- Indexed component
6801 when N_Indexed_Component
=>
6802 Error_Msg_N
("!indexed component is never static (RM 4.9)", N
);
6806 when N_Procedure_Call_Statement
=>
6807 Error_Msg_N
("!procedure call is never static (RM 4.9)", N
);
6809 -- Qualified expression (test expression)
6811 when N_Qualified_Expression
=>
6812 Why_Not_Static
(Expression
(N
));
6817 | N_Extension_Aggregate
6819 Error_Msg_N
("!an aggregate is never static (RM 4.9)", N
);
6824 Why_Not_Static
(Low_Bound
(N
));
6825 Why_Not_Static
(High_Bound
(N
));
6827 -- Range constraint, test range expression
6829 when N_Range_Constraint
=>
6830 Why_Not_Static
(Range_Expression
(N
));
6832 -- Subtype indication, test constraint
6834 when N_Subtype_Indication
=>
6835 Why_Not_Static
(Constraint
(N
));
6837 -- Selected component
6839 when N_Selected_Component
=>
6840 Error_Msg_N
("!selected component is never static (RM 4.9)", N
);
6845 Error_Msg_N
("!slice is never static (RM 4.9)", N
);
6847 when N_Type_Conversion
=>
6848 Why_Not_Static
(Expression
(N
));
6850 if not Is_Scalar_Type
(Entity
(Subtype_Mark
(N
)))
6851 or else not Is_OK_Static_Subtype
(Entity
(Subtype_Mark
(N
)))
6854 ("!static conversion requires static scalar subtype result "
6855 & "(RM 4.9(9))", N
);
6858 -- Unchecked type conversion
6860 when N_Unchecked_Type_Conversion
=>
6862 ("!unchecked type conversion is never static (RM 4.9)", N
);
6864 -- All other cases, no reason to give