1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, 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 -- We inhibit the warning when expansion is disabled, because the
551 -- preanalysis of a range of a 64-bit modular type may appear to
552 -- violate the constraint on non-static Universal_Integer. If there
553 -- is a true overflow it will be diagnosed during full analysis.
555 if Etype
(N
) = Universal_Integer
556 and then Nkind
(N
) = N_Integer_Literal
557 and then Nkind
(Parent
(N
)) in N_Subexpr
558 and then Expander_Active
560 (Intval
(N
) < Expr_Value
(Type_Low_Bound
(Universal_Integer
))
562 Intval
(N
) > Expr_Value
(Type_High_Bound
(Universal_Integer
)))
564 Apply_Compile_Time_Constraint_Error
565 (N
, "non-static universal integer value out of range<<",
566 CE_Range_Check_Failed
);
568 -- Check out of range of base type
570 elsif Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
573 -- Give warning if outside subtype (where one or both of the bounds of
574 -- the subtype is static). This warning is omitted if the expression
575 -- appears in a range that could be null (warnings are handled elsewhere
578 elsif T
/= Base_Type
(T
) and then Nkind
(Parent
(N
)) /= N_Range
then
579 if Is_In_Range
(N
, T
, Assume_Valid
=> True) then
582 elsif Is_Out_Of_Range
(N
, T
, Assume_Valid
=> True) then
584 -- Ignore out of range values for System.Priority in CodePeer
585 -- mode since the actual target compiler may provide a wider
588 if CodePeer_Mode
and then T
= RTE
(RE_Priority
) then
589 Set_Do_Range_Check
(N
, False);
591 Apply_Compile_Time_Constraint_Error
592 (N
, "value not in range of}<<", CE_Range_Check_Failed
);
596 Enable_Range_Check
(N
);
599 Set_Do_Range_Check
(N
, False);
602 end Check_Non_Static_Context
;
604 ---------------------------------
605 -- Check_String_Literal_Length --
606 ---------------------------------
608 procedure Check_String_Literal_Length
(N
: Node_Id
; Ttype
: Entity_Id
) is
610 if not Raises_Constraint_Error
(N
) and then Is_Constrained
(Ttype
) then
611 if UI_From_Int
(String_Length
(Strval
(N
))) /= String_Type_Len
(Ttype
)
613 Apply_Compile_Time_Constraint_Error
614 (N
, "string length wrong for}??",
615 CE_Length_Check_Failed
,
620 end Check_String_Literal_Length
;
626 function Choice_Matches
628 Choice
: Node_Id
) return Match_Result
630 Etyp
: constant Entity_Id
:= Etype
(Expr
);
636 pragma Assert
(Compile_Time_Known_Value
(Expr
));
637 pragma Assert
(Is_Scalar_Type
(Etyp
) or else Is_String_Type
(Etyp
));
639 if not Is_OK_Static_Choice
(Choice
) then
640 Set_Raises_Constraint_Error
(Choice
);
643 -- When the choice denotes a subtype with a static predictate, check the
644 -- expression against the predicate values. Different procedures apply
645 -- to discrete and non-discrete types.
647 elsif (Nkind
(Choice
) = N_Subtype_Indication
648 or else (Is_Entity_Name
(Choice
)
649 and then Is_Type
(Entity
(Choice
))))
650 and then Has_Predicates
(Etype
(Choice
))
651 and then Has_Static_Predicate
(Etype
(Choice
))
653 if Is_Discrete_Type
(Etype
(Choice
)) then
656 (Expr
, Static_Discrete_Predicate
(Etype
(Choice
)));
658 elsif Real_Or_String_Static_Predicate_Matches
(Expr
, Etype
(Choice
))
666 -- Discrete type case only
668 elsif Is_Discrete_Type
(Etyp
) then
669 Val
:= Expr_Value
(Expr
);
671 if Nkind
(Choice
) = N_Range
then
672 if Val
>= Expr_Value
(Low_Bound
(Choice
))
674 Val
<= Expr_Value
(High_Bound
(Choice
))
681 elsif Nkind
(Choice
) = N_Subtype_Indication
682 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
684 if Val
>= Expr_Value
(Type_Low_Bound
(Etype
(Choice
)))
686 Val
<= Expr_Value
(Type_High_Bound
(Etype
(Choice
)))
693 elsif Nkind
(Choice
) = N_Others_Choice
then
697 if Val
= Expr_Value
(Choice
) then
706 elsif Is_Real_Type
(Etyp
) then
707 ValR
:= Expr_Value_R
(Expr
);
709 if Nkind
(Choice
) = N_Range
then
710 if ValR
>= Expr_Value_R
(Low_Bound
(Choice
))
712 ValR
<= Expr_Value_R
(High_Bound
(Choice
))
719 elsif Nkind
(Choice
) = N_Subtype_Indication
720 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
722 if ValR
>= Expr_Value_R
(Type_Low_Bound
(Etype
(Choice
)))
724 ValR
<= Expr_Value_R
(Type_High_Bound
(Etype
(Choice
)))
732 if ValR
= Expr_Value_R
(Choice
) then
742 pragma Assert
(Is_String_Type
(Etyp
));
743 ValS
:= Expr_Value_S
(Expr
);
745 if Nkind
(Choice
) = N_Subtype_Indication
746 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
748 if not Is_Constrained
(Etype
(Choice
)) then
753 Typlen
: constant Uint
:=
754 String_Type_Len
(Etype
(Choice
));
755 Strlen
: constant Uint
:=
756 UI_From_Int
(String_Length
(Strval
(ValS
)));
758 if Typlen
= Strlen
then
767 if String_Equal
(Strval
(ValS
), Strval
(Expr_Value_S
(Choice
)))
781 function Choices_Match
783 Choices
: List_Id
) return Match_Result
786 Result
: Match_Result
;
789 Choice
:= First
(Choices
);
790 while Present
(Choice
) loop
791 Result
:= Choice_Matches
(Expr
, Choice
);
793 if Result
/= No_Match
then
803 --------------------------
804 -- Compile_Time_Compare --
805 --------------------------
807 function Compile_Time_Compare
809 Assume_Valid
: Boolean) return Compare_Result
811 Discard
: aliased Uint
;
813 return Compile_Time_Compare
(L
, R
, Discard
'Access, Assume_Valid
);
814 end Compile_Time_Compare
;
816 function Compile_Time_Compare
819 Assume_Valid
: Boolean;
820 Rec
: Boolean := False) return Compare_Result
822 Ltyp
: Entity_Id
:= Etype
(L
);
823 Rtyp
: Entity_Id
:= Etype
(R
);
825 Discard
: aliased Uint
;
827 procedure Compare_Decompose
831 -- This procedure decomposes the node N into an expression node and a
832 -- signed offset, so that the value of N is equal to the value of R plus
833 -- the value V (which may be negative). If no such decomposition is
834 -- possible, then on return R is a copy of N, and V is set to zero.
836 function Compare_Fixup
(N
: Node_Id
) return Node_Id
;
837 -- This function deals with replacing 'Last and 'First references with
838 -- their corresponding type bounds, which we then can compare. The
839 -- argument is the original node, the result is the identity, unless we
840 -- have a 'Last/'First reference in which case the value returned is the
841 -- appropriate type bound.
843 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean;
844 -- Even if the context does not assume that values are valid, some
845 -- simple cases can be recognized.
847 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean;
848 -- Returns True iff L and R represent expressions that definitely have
849 -- identical (but not necessarily compile-time-known) values Indeed the
850 -- caller is expected to have already dealt with the cases of compile
851 -- time known values, so these are not tested here.
853 -----------------------
854 -- Compare_Decompose --
855 -----------------------
857 procedure Compare_Decompose
863 if Nkind
(N
) = N_Op_Add
864 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
867 V
:= Intval
(Right_Opnd
(N
));
870 elsif Nkind
(N
) = N_Op_Subtract
871 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
874 V
:= UI_Negate
(Intval
(Right_Opnd
(N
)));
877 elsif Nkind
(N
) = N_Attribute_Reference
then
878 if Attribute_Name
(N
) = Name_Succ
then
879 R
:= First
(Expressions
(N
));
883 elsif Attribute_Name
(N
) = Name_Pred
then
884 R
:= First
(Expressions
(N
));
892 end Compare_Decompose
;
898 function Compare_Fixup
(N
: Node_Id
) return Node_Id
is
904 -- Fixup only required for First/Last attribute reference
906 if Nkind
(N
) = N_Attribute_Reference
907 and then Nam_In
(Attribute_Name
(N
), Name_First
, Name_Last
)
909 Xtyp
:= Etype
(Prefix
(N
));
911 -- If we have no type, then just abandon the attempt to do
912 -- a fixup, this is probably the result of some other error.
918 -- Dereference an access type
920 if Is_Access_Type
(Xtyp
) then
921 Xtyp
:= Designated_Type
(Xtyp
);
924 -- If we don't have an array type at this stage, something is
925 -- peculiar, e.g. another error, and we abandon the attempt at
928 if not Is_Array_Type
(Xtyp
) then
932 -- Ignore unconstrained array, since bounds are not meaningful
934 if not Is_Constrained
(Xtyp
) then
938 if Ekind
(Xtyp
) = E_String_Literal_Subtype
then
939 if Attribute_Name
(N
) = Name_First
then
940 return String_Literal_Low_Bound
(Xtyp
);
943 Make_Integer_Literal
(Sloc
(N
),
944 Intval
=> Intval
(String_Literal_Low_Bound
(Xtyp
)) +
945 String_Literal_Length
(Xtyp
));
949 -- Find correct index type
951 Indx
:= First_Index
(Xtyp
);
953 if Present
(Expressions
(N
)) then
954 Subs
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
956 for J
in 2 .. Subs
loop
957 Indx
:= Next_Index
(Indx
);
961 Xtyp
:= Etype
(Indx
);
963 if Attribute_Name
(N
) = Name_First
then
964 return Type_Low_Bound
(Xtyp
);
966 return Type_High_Bound
(Xtyp
);
973 ----------------------------
974 -- Is_Known_Valid_Operand --
975 ----------------------------
977 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean is
979 return (Is_Entity_Name
(Opnd
)
981 (Is_Known_Valid
(Entity
(Opnd
))
982 or else Ekind
(Entity
(Opnd
)) = E_In_Parameter
984 (Ekind
(Entity
(Opnd
)) in Object_Kind
985 and then Present
(Current_Value
(Entity
(Opnd
))))))
986 or else Is_OK_Static_Expression
(Opnd
);
987 end Is_Known_Valid_Operand
;
993 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean is
994 Lf
: constant Node_Id
:= Compare_Fixup
(L
);
995 Rf
: constant Node_Id
:= Compare_Fixup
(R
);
997 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean;
998 -- L, R are the Expressions values from two attribute nodes for First
999 -- or Last attributes. Either may be set to No_List if no expressions
1000 -- are present (indicating subscript 1). The result is True if both
1001 -- expressions represent the same subscript (note one case is where
1002 -- one subscript is missing and the other is explicitly set to 1).
1004 -----------------------
1005 -- Is_Same_Subscript --
1006 -----------------------
1008 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean is
1014 return Expr_Value
(First
(R
)) = Uint_1
;
1019 return Expr_Value
(First
(L
)) = Uint_1
;
1021 return Expr_Value
(First
(L
)) = Expr_Value
(First
(R
));
1024 end Is_Same_Subscript
;
1026 -- Start of processing for Is_Same_Value
1029 -- Values are the same if they refer to the same entity and the
1030 -- entity is non-volatile. This does not however apply to Float
1031 -- types, since we may have two NaN values and they should never
1034 -- If the entity is a discriminant, the two expressions may be bounds
1035 -- of components of objects of the same discriminated type. The
1036 -- values of the discriminants are not static, and therefore the
1037 -- result is unknown.
1039 -- It would be better to comment individual branches of this test ???
1041 if Nkind_In
(Lf
, N_Identifier
, N_Expanded_Name
)
1042 and then Nkind_In
(Rf
, N_Identifier
, N_Expanded_Name
)
1043 and then Entity
(Lf
) = Entity
(Rf
)
1044 and then Ekind
(Entity
(Lf
)) /= E_Discriminant
1045 and then Present
(Entity
(Lf
))
1046 and then not Is_Floating_Point_Type
(Etype
(L
))
1047 and then not Is_Volatile_Reference
(L
)
1048 and then not Is_Volatile_Reference
(R
)
1052 -- Or if they are compile-time-known and identical
1054 elsif Compile_Time_Known_Value
(Lf
)
1056 Compile_Time_Known_Value
(Rf
)
1057 and then Expr_Value
(Lf
) = Expr_Value
(Rf
)
1061 -- False if Nkind of the two nodes is different for remaining cases
1063 elsif Nkind
(Lf
) /= Nkind
(Rf
) then
1066 -- True if both 'First or 'Last values applying to the same entity
1067 -- (first and last don't change even if value does). Note that we
1068 -- need this even with the calls to Compare_Fixup, to handle the
1069 -- case of unconstrained array attributes where Compare_Fixup
1070 -- cannot find useful bounds.
1072 elsif Nkind
(Lf
) = N_Attribute_Reference
1073 and then Attribute_Name
(Lf
) = Attribute_Name
(Rf
)
1074 and then Nam_In
(Attribute_Name
(Lf
), Name_First
, Name_Last
)
1075 and then Nkind_In
(Prefix
(Lf
), N_Identifier
, N_Expanded_Name
)
1076 and then Nkind_In
(Prefix
(Rf
), N_Identifier
, N_Expanded_Name
)
1077 and then Entity
(Prefix
(Lf
)) = Entity
(Prefix
(Rf
))
1078 and then Is_Same_Subscript
(Expressions
(Lf
), Expressions
(Rf
))
1082 -- True if the same selected component from the same record
1084 elsif Nkind
(Lf
) = N_Selected_Component
1085 and then Selector_Name
(Lf
) = Selector_Name
(Rf
)
1086 and then Is_Same_Value
(Prefix
(Lf
), Prefix
(Rf
))
1090 -- True if the same unary operator applied to the same operand
1092 elsif Nkind
(Lf
) in N_Unary_Op
1093 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1097 -- True if the same binary operator applied to the same operands
1099 elsif Nkind
(Lf
) in N_Binary_Op
1100 and then Is_Same_Value
(Left_Opnd
(Lf
), Left_Opnd
(Rf
))
1101 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1105 -- All other cases, we can't tell, so return False
1112 -- Start of processing for Compile_Time_Compare
1115 Diff
.all := No_Uint
;
1117 -- In preanalysis mode, always return Unknown unless the expression
1118 -- is static. It is too early to be thinking we know the result of a
1119 -- comparison, save that judgment for the full analysis. This is
1120 -- particularly important in the case of pre and postconditions, which
1121 -- otherwise can be prematurely collapsed into having True or False
1122 -- conditions when this is inappropriate.
1124 if not (Full_Analysis
1125 or else (Is_OK_Static_Expression
(L
)
1127 Is_OK_Static_Expression
(R
)))
1132 -- If either operand could raise constraint error, then we cannot
1133 -- know the result at compile time (since CE may be raised).
1135 if not (Cannot_Raise_Constraint_Error
(L
)
1137 Cannot_Raise_Constraint_Error
(R
))
1142 -- Identical operands are most certainly equal
1148 -- If expressions have no types, then do not attempt to determine if
1149 -- they are the same, since something funny is going on. One case in
1150 -- which this happens is during generic template analysis, when bounds
1151 -- are not fully analyzed.
1153 if No
(Ltyp
) or else No
(Rtyp
) then
1157 -- These get reset to the base type for the case of entities where
1158 -- Is_Known_Valid is not set. This takes care of handling possible
1159 -- invalid representations using the value of the base type, in
1160 -- accordance with RM 13.9.1(10).
1162 Ltyp
:= Underlying_Type
(Ltyp
);
1163 Rtyp
:= Underlying_Type
(Rtyp
);
1165 -- Same rationale as above, but for Underlying_Type instead of Etype
1167 if No
(Ltyp
) or else No
(Rtyp
) then
1171 -- We do not attempt comparisons for packed arrays represented as
1172 -- modular types, where the semantics of comparison is quite different.
1174 if Is_Packed_Array_Impl_Type
(Ltyp
)
1175 and then Is_Modular_Integer_Type
(Ltyp
)
1179 -- For access types, the only time we know the result at compile time
1180 -- (apart from identical operands, which we handled already) is if we
1181 -- know one operand is null and the other is not, or both operands are
1184 elsif Is_Access_Type
(Ltyp
) then
1185 if Known_Null
(L
) then
1186 if Known_Null
(R
) then
1188 elsif Known_Non_Null
(R
) then
1194 elsif Known_Non_Null
(L
) and then Known_Null
(R
) then
1201 -- Case where comparison involves two compile-time-known values
1203 elsif Compile_Time_Known_Value
(L
)
1205 Compile_Time_Known_Value
(R
)
1207 -- For the floating-point case, we have to be a little careful, since
1208 -- at compile time we are dealing with universal exact values, but at
1209 -- runtime, these will be in non-exact target form. That's why the
1210 -- returned results are LE and GE below instead of LT and GT.
1212 if Is_Floating_Point_Type
(Ltyp
)
1214 Is_Floating_Point_Type
(Rtyp
)
1217 Lo
: constant Ureal
:= Expr_Value_R
(L
);
1218 Hi
: constant Ureal
:= Expr_Value_R
(R
);
1229 -- For string types, we have two string literals and we proceed to
1230 -- compare them using the Ada style dictionary string comparison.
1232 elsif not Is_Scalar_Type
(Ltyp
) then
1234 Lstring
: constant String_Id
:= Strval
(Expr_Value_S
(L
));
1235 Rstring
: constant String_Id
:= Strval
(Expr_Value_S
(R
));
1236 Llen
: constant Nat
:= String_Length
(Lstring
);
1237 Rlen
: constant Nat
:= String_Length
(Rstring
);
1240 for J
in 1 .. Nat
'Min (Llen
, Rlen
) loop
1242 LC
: constant Char_Code
:= Get_String_Char
(Lstring
, J
);
1243 RC
: constant Char_Code
:= Get_String_Char
(Rstring
, J
);
1255 elsif Llen
> Rlen
then
1262 -- For remaining scalar cases we know exactly (note that this does
1263 -- include the fixed-point case, where we know the run time integer
1268 Lo
: constant Uint
:= Expr_Value
(L
);
1269 Hi
: constant Uint
:= Expr_Value
(R
);
1272 Diff
.all := Hi
- Lo
;
1277 Diff
.all := Lo
- Hi
;
1283 -- Cases where at least one operand is not known at compile time
1286 -- Remaining checks apply only for discrete types
1288 if not Is_Discrete_Type
(Ltyp
)
1290 not Is_Discrete_Type
(Rtyp
)
1295 -- Defend against generic types, or actually any expressions that
1296 -- contain a reference to a generic type from within a generic
1297 -- template. We don't want to do any range analysis of such
1298 -- expressions for two reasons. First, the bounds of a generic type
1299 -- itself are junk and cannot be used for any kind of analysis.
1300 -- Second, we may have a case where the range at run time is indeed
1301 -- known, but we don't want to do compile time analysis in the
1302 -- template based on that range since in an instance the value may be
1303 -- static, and able to be elaborated without reference to the bounds
1304 -- of types involved. As an example, consider:
1306 -- (F'Pos (F'Last) + 1) > Integer'Last
1308 -- The expression on the left side of > is Universal_Integer and thus
1309 -- acquires the type Integer for evaluation at run time, and at run
1310 -- time it is true that this condition is always False, but within
1311 -- an instance F may be a type with a static range greater than the
1312 -- range of Integer, and the expression statically evaluates to True.
1314 if References_Generic_Formal_Type
(L
)
1316 References_Generic_Formal_Type
(R
)
1321 -- Replace types by base types for the case of values which are not
1322 -- known to have valid representations. This takes care of properly
1323 -- dealing with invalid representations.
1325 if not Assume_Valid
then
1326 if not (Is_Entity_Name
(L
)
1327 and then (Is_Known_Valid
(Entity
(L
))
1328 or else Assume_No_Invalid_Values
))
1330 Ltyp
:= Underlying_Type
(Base_Type
(Ltyp
));
1333 if not (Is_Entity_Name
(R
)
1334 and then (Is_Known_Valid
(Entity
(R
))
1335 or else Assume_No_Invalid_Values
))
1337 Rtyp
:= Underlying_Type
(Base_Type
(Rtyp
));
1341 -- First attempt is to decompose the expressions to extract a
1342 -- constant offset resulting from the use of any of the forms:
1349 -- Then we see if the two expressions are the same value, and if so
1350 -- the result is obtained by comparing the offsets.
1352 -- Note: the reason we do this test first is that it returns only
1353 -- decisive results (with diff set), where other tests, like the
1354 -- range test, may not be as so decisive. Consider for example
1355 -- J .. J + 1. This code can conclude LT with a difference of 1,
1356 -- even if the range of J is not known.
1365 Compare_Decompose
(L
, Lnode
, Loffs
);
1366 Compare_Decompose
(R
, Rnode
, Roffs
);
1368 if Is_Same_Value
(Lnode
, Rnode
) then
1369 if Loffs
= Roffs
then
1373 -- When the offsets are not equal, we can go farther only if
1374 -- the types are not modular (e.g. X < X + 1 is False if X is
1375 -- the largest number).
1377 if not Is_Modular_Integer_Type
(Ltyp
)
1378 and then not Is_Modular_Integer_Type
(Rtyp
)
1380 if Loffs
< Roffs
then
1381 Diff
.all := Roffs
- Loffs
;
1384 Diff
.all := Loffs
- Roffs
;
1391 -- Next, try range analysis and see if operand ranges are disjoint
1399 -- True if each range is a single point
1402 Determine_Range
(L
, LOK
, LLo
, LHi
, Assume_Valid
);
1403 Determine_Range
(R
, ROK
, RLo
, RHi
, Assume_Valid
);
1406 Single
:= (LLo
= LHi
) and then (RLo
= RHi
);
1409 if Single
and Assume_Valid
then
1410 Diff
.all := RLo
- LLo
;
1415 elsif RHi
< LLo
then
1416 if Single
and Assume_Valid
then
1417 Diff
.all := LLo
- RLo
;
1422 elsif Single
and then LLo
= RLo
then
1424 -- If the range includes a single literal and we can assume
1425 -- validity then the result is known even if an operand is
1428 if Assume_Valid
then
1434 elsif LHi
= RLo
then
1437 elsif RHi
= LLo
then
1440 elsif not Is_Known_Valid_Operand
(L
)
1441 and then not Assume_Valid
1443 if Is_Same_Value
(L
, R
) then
1450 -- If the range of either operand cannot be determined, nothing
1451 -- further can be inferred.
1458 -- Here is where we check for comparisons against maximum bounds of
1459 -- types, where we know that no value can be outside the bounds of
1460 -- the subtype. Note that this routine is allowed to assume that all
1461 -- expressions are within their subtype bounds. Callers wishing to
1462 -- deal with possibly invalid values must in any case take special
1463 -- steps (e.g. conversions to larger types) to avoid this kind of
1464 -- optimization, which is always considered to be valid. We do not
1465 -- attempt this optimization with generic types, since the type
1466 -- bounds may not be meaningful in this case.
1468 -- We are in danger of an infinite recursion here. It does not seem
1469 -- useful to go more than one level deep, so the parameter Rec is
1470 -- used to protect ourselves against this infinite recursion.
1474 -- See if we can get a decisive check against one operand and a
1475 -- bound of the other operand (four possible tests here). Note
1476 -- that we avoid testing junk bounds of a generic type.
1478 if not Is_Generic_Type
(Rtyp
) then
1479 case Compile_Time_Compare
(L
, Type_Low_Bound
(Rtyp
),
1481 Assume_Valid
, Rec
=> True)
1483 when LT
=> return LT
;
1484 when LE
=> return LE
;
1485 when EQ
=> return LE
;
1486 when others => null;
1489 case Compile_Time_Compare
(L
, Type_High_Bound
(Rtyp
),
1491 Assume_Valid
, Rec
=> True)
1493 when GT
=> return GT
;
1494 when GE
=> return GE
;
1495 when EQ
=> return GE
;
1496 when others => null;
1500 if not Is_Generic_Type
(Ltyp
) then
1501 case Compile_Time_Compare
(Type_Low_Bound
(Ltyp
), R
,
1503 Assume_Valid
, Rec
=> True)
1505 when GT
=> return GT
;
1506 when GE
=> return GE
;
1507 when EQ
=> return GE
;
1508 when others => null;
1511 case Compile_Time_Compare
(Type_High_Bound
(Ltyp
), R
,
1513 Assume_Valid
, Rec
=> True)
1515 when LT
=> return LT
;
1516 when LE
=> return LE
;
1517 when EQ
=> return LE
;
1518 when others => null;
1523 -- Next attempt is to see if we have an entity compared with a
1524 -- compile-time-known value, where there is a current value
1525 -- conditional for the entity which can tell us the result.
1529 -- Entity variable (left operand)
1532 -- Value (right operand)
1535 -- If False, we have reversed the operands
1538 -- Comparison operator kind from Get_Current_Value_Condition call
1541 -- Value from Get_Current_Value_Condition call
1546 Result
: Compare_Result
;
1547 -- Known result before inversion
1550 if Is_Entity_Name
(L
)
1551 and then Compile_Time_Known_Value
(R
)
1554 Val
:= Expr_Value
(R
);
1557 elsif Is_Entity_Name
(R
)
1558 and then Compile_Time_Known_Value
(L
)
1561 Val
:= Expr_Value
(L
);
1564 -- That was the last chance at finding a compile time result
1570 Get_Current_Value_Condition
(Var
, Op
, Opn
);
1572 -- That was the last chance, so if we got nothing return
1578 Opv
:= Expr_Value
(Opn
);
1580 -- We got a comparison, so we might have something interesting
1582 -- Convert LE to LT and GE to GT, just so we have fewer cases
1584 if Op
= N_Op_Le
then
1588 elsif Op
= N_Op_Ge
then
1593 -- Deal with equality case
1595 if Op
= N_Op_Eq
then
1598 elsif Opv
< Val
then
1604 -- Deal with inequality case
1606 elsif Op
= N_Op_Ne
then
1613 -- Deal with greater than case
1615 elsif Op
= N_Op_Gt
then
1618 elsif Opv
= Val
- 1 then
1624 -- Deal with less than case
1626 else pragma Assert
(Op
= N_Op_Lt
);
1629 elsif Opv
= Val
+ 1 then
1636 -- Deal with inverting result
1640 when GT
=> return LT
;
1641 when GE
=> return LE
;
1642 when LT
=> return GT
;
1643 when LE
=> return GE
;
1644 when others => return Result
;
1651 end Compile_Time_Compare
;
1653 -------------------------------
1654 -- Compile_Time_Known_Bounds --
1655 -------------------------------
1657 function Compile_Time_Known_Bounds
(T
: Entity_Id
) return Boolean is
1662 if T
= Any_Composite
or else not Is_Array_Type
(T
) then
1666 Indx
:= First_Index
(T
);
1667 while Present
(Indx
) loop
1668 Typ
:= Underlying_Type
(Etype
(Indx
));
1670 -- Never look at junk bounds of a generic type
1672 if Is_Generic_Type
(Typ
) then
1676 -- Otherwise check bounds for compile-time-known
1678 if not Compile_Time_Known_Value
(Type_Low_Bound
(Typ
)) then
1680 elsif not Compile_Time_Known_Value
(Type_High_Bound
(Typ
)) then
1688 end Compile_Time_Known_Bounds
;
1690 ------------------------------
1691 -- Compile_Time_Known_Value --
1692 ------------------------------
1694 function Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1695 K
: constant Node_Kind
:= Nkind
(Op
);
1696 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(Op
) mod CV_Cache_Size
);
1699 -- Never known at compile time if bad type or raises constraint error
1700 -- or empty (latter case occurs only as a result of a previous error).
1703 Check_Error_Detected
;
1707 or else Etype
(Op
) = Any_Type
1708 or else Raises_Constraint_Error
(Op
)
1713 -- If we have an entity name, then see if it is the name of a constant
1714 -- and if so, test the corresponding constant value, or the name of an
1715 -- enumeration literal, which is always a constant.
1717 if Present
(Etype
(Op
)) and then Is_Entity_Name
(Op
) then
1719 Ent
: constant Entity_Id
:= Entity
(Op
);
1723 -- Never known at compile time if it is a packed array value. We
1724 -- might want to try to evaluate these at compile time one day,
1725 -- but we do not make that attempt now.
1727 if Is_Packed_Array_Impl_Type
(Etype
(Op
)) then
1730 elsif Ekind
(Ent
) = E_Enumeration_Literal
then
1733 elsif Ekind
(Ent
) = E_Constant
then
1734 Val
:= Constant_Value
(Ent
);
1736 if Present
(Val
) then
1738 -- Guard against an illegal deferred constant whose full
1739 -- view is initialized with a reference to itself. Treat
1740 -- this case as a value not known at compile time.
1742 if Is_Entity_Name
(Val
) and then Entity
(Val
) = Ent
then
1745 return Compile_Time_Known_Value
(Val
);
1748 -- Otherwise, the constant does not have a compile-time-known
1757 -- We have a value, see if it is compile-time-known
1760 -- Integer literals are worth storing in the cache
1762 if K
= N_Integer_Literal
then
1764 CV_Ent
.V
:= Intval
(Op
);
1767 -- Other literals and NULL are known at compile time
1770 Nkind_In
(K
, N_Character_Literal
,
1779 -- If we fall through, not known at compile time
1783 -- If we get an exception while trying to do this test, then some error
1784 -- has occurred, and we simply say that the value is not known after all
1789 end Compile_Time_Known_Value
;
1791 --------------------------------------
1792 -- Compile_Time_Known_Value_Or_Aggr --
1793 --------------------------------------
1795 function Compile_Time_Known_Value_Or_Aggr
(Op
: Node_Id
) return Boolean is
1797 -- If we have an entity name, then see if it is the name of a constant
1798 -- and if so, test the corresponding constant value, or the name of
1799 -- an enumeration literal, which is always a constant.
1801 if Is_Entity_Name
(Op
) then
1803 E
: constant Entity_Id
:= Entity
(Op
);
1807 if Ekind
(E
) = E_Enumeration_Literal
then
1810 elsif Ekind
(E
) /= E_Constant
then
1814 V
:= Constant_Value
(E
);
1816 and then Compile_Time_Known_Value_Or_Aggr
(V
);
1820 -- We have a value, see if it is compile-time-known
1823 if Compile_Time_Known_Value
(Op
) then
1826 elsif Nkind
(Op
) = N_Aggregate
then
1828 if Present
(Expressions
(Op
)) then
1832 Expr
:= First
(Expressions
(Op
));
1833 while Present
(Expr
) loop
1834 if not Compile_Time_Known_Value_Or_Aggr
(Expr
) then
1843 if Present
(Component_Associations
(Op
)) then
1848 Cass
:= First
(Component_Associations
(Op
));
1849 while Present
(Cass
) loop
1851 Compile_Time_Known_Value_Or_Aggr
(Expression
(Cass
))
1863 elsif Nkind
(Op
) = N_Qualified_Expression
then
1864 return Compile_Time_Known_Value_Or_Aggr
(Expression
(Op
));
1866 -- All other types of values are not known at compile time
1873 end Compile_Time_Known_Value_Or_Aggr
;
1875 ---------------------------------------
1876 -- CRT_Safe_Compile_Time_Known_Value --
1877 ---------------------------------------
1879 function CRT_Safe_Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1881 if (Configurable_Run_Time_Mode
or No_Run_Time_Mode
)
1882 and then not Is_OK_Static_Expression
(Op
)
1886 return Compile_Time_Known_Value
(Op
);
1888 end CRT_Safe_Compile_Time_Known_Value
;
1894 -- This is only called for actuals of functions that are not predefined
1895 -- operators (which have already been rewritten as operators at this
1896 -- stage), so the call can never be folded, and all that needs doing for
1897 -- the actual is to do the check for a non-static context.
1899 procedure Eval_Actual
(N
: Node_Id
) is
1901 Check_Non_Static_Context
(N
);
1904 --------------------
1905 -- Eval_Allocator --
1906 --------------------
1908 -- Allocators are never static, so all we have to do is to do the
1909 -- check for a non-static context if an expression is present.
1911 procedure Eval_Allocator
(N
: Node_Id
) is
1912 Expr
: constant Node_Id
:= Expression
(N
);
1914 if Nkind
(Expr
) = N_Qualified_Expression
then
1915 Check_Non_Static_Context
(Expression
(Expr
));
1919 ------------------------
1920 -- Eval_Arithmetic_Op --
1921 ------------------------
1923 -- Arithmetic operations are static functions, so the result is static
1924 -- if both operands are static (RM 4.9(7), 4.9(20)).
1926 procedure Eval_Arithmetic_Op
(N
: Node_Id
) is
1927 Left
: constant Node_Id
:= Left_Opnd
(N
);
1928 Right
: constant Node_Id
:= Right_Opnd
(N
);
1929 Ltype
: constant Entity_Id
:= Etype
(Left
);
1930 Rtype
: constant Entity_Id
:= Etype
(Right
);
1931 Otype
: Entity_Id
:= Empty
;
1936 -- If not foldable we are done
1938 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
1944 -- Otherwise attempt to fold
1946 if Is_Universal_Numeric_Type
(Etype
(Left
))
1948 Is_Universal_Numeric_Type
(Etype
(Right
))
1950 Otype
:= Find_Universal_Operator_Type
(N
);
1953 -- Fold for cases where both operands are of integer type
1955 if Is_Integer_Type
(Ltype
) and then Is_Integer_Type
(Rtype
) then
1957 Left_Int
: constant Uint
:= Expr_Value
(Left
);
1958 Right_Int
: constant Uint
:= Expr_Value
(Right
);
1964 Result
:= Left_Int
+ Right_Int
;
1966 when N_Op_Subtract
=>
1967 Result
:= Left_Int
- Right_Int
;
1969 when N_Op_Multiply
=>
1972 (Num_Bits
(Left_Int
) + Num_Bits
(Right_Int
)))
1974 Result
:= Left_Int
* Right_Int
;
1981 -- The exception Constraint_Error is raised by integer
1982 -- division, rem and mod if the right operand is zero.
1984 if Right_Int
= 0 then
1986 -- When SPARK_Mode is On, force a warning instead of
1987 -- an error in that case, as this likely corresponds
1988 -- to deactivated code.
1990 Apply_Compile_Time_Constraint_Error
1991 (N
, "division by zero", CE_Divide_By_Zero
,
1992 Warn
=> not Stat
or SPARK_Mode
= On
);
1993 Set_Raises_Constraint_Error
(N
);
1996 -- Otherwise we can do the division
1999 Result
:= Left_Int
/ Right_Int
;
2004 -- The exception Constraint_Error is raised by integer
2005 -- division, rem and mod if the right operand is zero.
2007 if Right_Int
= 0 then
2009 -- When SPARK_Mode is On, force a warning instead of
2010 -- an error in that case, as this likely corresponds
2011 -- to deactivated code.
2013 Apply_Compile_Time_Constraint_Error
2014 (N
, "mod with zero divisor", CE_Divide_By_Zero
,
2015 Warn
=> not Stat
or SPARK_Mode
= On
);
2019 Result
:= Left_Int
mod Right_Int
;
2024 -- The exception Constraint_Error is raised by integer
2025 -- division, rem and mod if the right operand is zero.
2027 if Right_Int
= 0 then
2029 -- When SPARK_Mode is On, force a warning instead of
2030 -- an error in that case, as this likely corresponds
2031 -- to deactivated code.
2033 Apply_Compile_Time_Constraint_Error
2034 (N
, "rem with zero divisor", CE_Divide_By_Zero
,
2035 Warn
=> not Stat
or SPARK_Mode
= On
);
2039 Result
:= Left_Int
rem Right_Int
;
2043 raise Program_Error
;
2046 -- Adjust the result by the modulus if the type is a modular type
2048 if Is_Modular_Integer_Type
(Ltype
) then
2049 Result
:= Result
mod Modulus
(Ltype
);
2051 -- For a signed integer type, check non-static overflow
2053 elsif (not Stat
) and then Is_Signed_Integer_Type
(Ltype
) then
2055 BT
: constant Entity_Id
:= Base_Type
(Ltype
);
2056 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(BT
));
2057 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(BT
));
2059 if Result
< Lo
or else Result
> Hi
then
2060 Apply_Compile_Time_Constraint_Error
2061 (N
, "value not in range of }??",
2062 CE_Overflow_Check_Failed
,
2069 -- If we get here we can fold the result
2071 Fold_Uint
(N
, Result
, Stat
);
2074 -- Cases where at least one operand is a real. We handle the cases of
2075 -- both reals, or mixed/real integer cases (the latter happen only for
2076 -- divide and multiply, and the result is always real).
2078 elsif Is_Real_Type
(Ltype
) or else Is_Real_Type
(Rtype
) then
2085 if Is_Real_Type
(Ltype
) then
2086 Left_Real
:= Expr_Value_R
(Left
);
2088 Left_Real
:= UR_From_Uint
(Expr_Value
(Left
));
2091 if Is_Real_Type
(Rtype
) then
2092 Right_Real
:= Expr_Value_R
(Right
);
2094 Right_Real
:= UR_From_Uint
(Expr_Value
(Right
));
2097 if Nkind
(N
) = N_Op_Add
then
2098 Result
:= Left_Real
+ Right_Real
;
2100 elsif Nkind
(N
) = N_Op_Subtract
then
2101 Result
:= Left_Real
- Right_Real
;
2103 elsif Nkind
(N
) = N_Op_Multiply
then
2104 Result
:= Left_Real
* Right_Real
;
2106 else pragma Assert
(Nkind
(N
) = N_Op_Divide
);
2107 if UR_Is_Zero
(Right_Real
) then
2108 Apply_Compile_Time_Constraint_Error
2109 (N
, "division by zero", CE_Divide_By_Zero
);
2113 Result
:= Left_Real
/ Right_Real
;
2116 Fold_Ureal
(N
, Result
, Stat
);
2120 -- If the operator was resolved to a specific type, make sure that type
2121 -- is frozen even if the expression is folded into a literal (which has
2122 -- a universal type).
2124 if Present
(Otype
) then
2125 Freeze_Before
(N
, Otype
);
2127 end Eval_Arithmetic_Op
;
2129 ----------------------------
2130 -- Eval_Character_Literal --
2131 ----------------------------
2133 -- Nothing to be done
2135 procedure Eval_Character_Literal
(N
: Node_Id
) is
2136 pragma Warnings
(Off
, N
);
2139 end Eval_Character_Literal
;
2145 -- Static function calls are either calls to predefined operators
2146 -- with static arguments, or calls to functions that rename a literal.
2147 -- Only the latter case is handled here, predefined operators are
2148 -- constant-folded elsewhere.
2150 -- If the function is itself inherited (see 7423-001) the literal of
2151 -- the parent type must be explicitly converted to the return type
2154 procedure Eval_Call
(N
: Node_Id
) is
2155 Loc
: constant Source_Ptr
:= Sloc
(N
);
2156 Typ
: constant Entity_Id
:= Etype
(N
);
2160 if Nkind
(N
) = N_Function_Call
2161 and then No
(Parameter_Associations
(N
))
2162 and then Is_Entity_Name
(Name
(N
))
2163 and then Present
(Alias
(Entity
(Name
(N
))))
2164 and then Is_Enumeration_Type
(Base_Type
(Typ
))
2166 Lit
:= Ultimate_Alias
(Entity
(Name
(N
)));
2168 if Ekind
(Lit
) = E_Enumeration_Literal
then
2169 if Base_Type
(Etype
(Lit
)) /= Base_Type
(Typ
) then
2171 (N
, Convert_To
(Typ
, New_Occurrence_Of
(Lit
, Loc
)));
2173 Rewrite
(N
, New_Occurrence_Of
(Lit
, Loc
));
2181 --------------------------
2182 -- Eval_Case_Expression --
2183 --------------------------
2185 -- A conditional expression is static if all its conditions and dependent
2186 -- expressions are static. Note that we do not care if the dependent
2187 -- expressions raise CE, except for the one that will be selected.
2189 procedure Eval_Case_Expression
(N
: Node_Id
) is
2194 Set_Is_Static_Expression
(N
, False);
2196 if Error_Posted
(Expression
(N
))
2197 or else not Is_Static_Expression
(Expression
(N
))
2199 Check_Non_Static_Context
(Expression
(N
));
2203 -- First loop, make sure all the alternatives are static expressions
2204 -- none of which raise Constraint_Error. We make the constraint error
2205 -- check because part of the legality condition for a correct static
2206 -- case expression is that the cases are covered, like any other case
2207 -- expression. And we can't do that if any of the conditions raise an
2208 -- exception, so we don't even try to evaluate if that is the case.
2210 Alt
:= First
(Alternatives
(N
));
2211 while Present
(Alt
) loop
2213 -- The expression must be static, but we don't care at this stage
2214 -- if it raises Constraint_Error (the alternative might not match,
2215 -- in which case the expression is statically unevaluated anyway).
2217 if not Is_Static_Expression
(Expression
(Alt
)) then
2218 Check_Non_Static_Context
(Expression
(Alt
));
2222 -- The choices of a case always have to be static, and cannot raise
2223 -- an exception. If this condition is not met, then the expression
2224 -- is plain illegal, so just abandon evaluation attempts. No need
2225 -- to check non-static context when we have something illegal anyway.
2227 if not Is_OK_Static_Choice_List
(Discrete_Choices
(Alt
)) then
2234 -- OK, if the above loop gets through it means that all choices are OK
2235 -- static (don't raise exceptions), so the whole case is static, and we
2236 -- can find the matching alternative.
2238 Set_Is_Static_Expression
(N
);
2240 -- Now to deal with propagating a possible constraint error
2242 -- If the selecting expression raises CE, propagate and we are done
2244 if Raises_Constraint_Error
(Expression
(N
)) then
2245 Set_Raises_Constraint_Error
(N
);
2247 -- Otherwise we need to check the alternatives to find the matching
2248 -- one. CE's in other than the matching one are not relevant. But we
2249 -- do need to check the matching one. Unlike the first loop, we do not
2250 -- have to go all the way through, when we find the matching one, quit.
2253 Alt
:= First
(Alternatives
(N
));
2256 -- We must find a match among the alternatives. If not, this must
2257 -- be due to other errors, so just ignore, leaving as non-static.
2260 Set_Is_Static_Expression
(N
, False);
2264 -- Otherwise loop through choices of this alternative
2266 Choice
:= First
(Discrete_Choices
(Alt
));
2267 while Present
(Choice
) loop
2269 -- If we find a matching choice, then the Expression of this
2270 -- alternative replaces N (Raises_Constraint_Error flag is
2271 -- included, so we don't have to special case that).
2273 if Choice_Matches
(Expression
(N
), Choice
) = Match
then
2274 Rewrite
(N
, Relocate_Node
(Expression
(Alt
)));
2284 end Eval_Case_Expression
;
2286 ------------------------
2287 -- Eval_Concatenation --
2288 ------------------------
2290 -- Concatenation is a static function, so the result is static if both
2291 -- operands are static (RM 4.9(7), 4.9(21)).
2293 procedure Eval_Concatenation
(N
: Node_Id
) is
2294 Left
: constant Node_Id
:= Left_Opnd
(N
);
2295 Right
: constant Node_Id
:= Right_Opnd
(N
);
2296 C_Typ
: constant Entity_Id
:= Root_Type
(Component_Type
(Etype
(N
)));
2301 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2302 -- non-static context.
2304 if Ada_Version
= Ada_83
2305 and then Comes_From_Source
(N
)
2307 Check_Non_Static_Context
(Left
);
2308 Check_Non_Static_Context
(Right
);
2312 -- If not foldable we are done. In principle concatenation that yields
2313 -- any string type is static (i.e. an array type of character types).
2314 -- However, character types can include enumeration literals, and
2315 -- concatenation in that case cannot be described by a literal, so we
2316 -- only consider the operation static if the result is an array of
2317 -- (a descendant of) a predefined character type.
2319 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2321 if not (Is_Standard_Character_Type
(C_Typ
) and then Fold
) then
2322 Set_Is_Static_Expression
(N
, False);
2326 -- Compile time string concatenation
2328 -- ??? Note that operands that are aggregates can be marked as static,
2329 -- so we should attempt at a later stage to fold concatenations with
2333 Left_Str
: constant Node_Id
:= Get_String_Val
(Left
);
2335 Right_Str
: constant Node_Id
:= Get_String_Val
(Right
);
2336 Folded_Val
: String_Id
:= No_String
;
2339 -- Establish new string literal, and store left operand. We make
2340 -- sure to use the special Start_String that takes an operand if
2341 -- the left operand is a string literal. Since this is optimized
2342 -- in the case where that is the most recently created string
2343 -- literal, we ensure efficient time/space behavior for the
2344 -- case of a concatenation of a series of string literals.
2346 if Nkind
(Left_Str
) = N_String_Literal
then
2347 Left_Len
:= String_Length
(Strval
(Left_Str
));
2349 -- If the left operand is the empty string, and the right operand
2350 -- is a string literal (the case of "" & "..."), the result is the
2351 -- value of the right operand. This optimization is important when
2352 -- Is_Folded_In_Parser, to avoid copying an enormous right
2355 if Left_Len
= 0 and then Nkind
(Right_Str
) = N_String_Literal
then
2356 Folded_Val
:= Strval
(Right_Str
);
2358 Start_String
(Strval
(Left_Str
));
2363 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Left_Str
)));
2367 -- Now append the characters of the right operand, unless we
2368 -- optimized the "" & "..." case above.
2370 if Nkind
(Right_Str
) = N_String_Literal
then
2371 if Left_Len
/= 0 then
2372 Store_String_Chars
(Strval
(Right_Str
));
2373 Folded_Val
:= End_String
;
2376 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Right_Str
)));
2377 Folded_Val
:= End_String
;
2380 Set_Is_Static_Expression
(N
, Stat
);
2382 -- If left operand is the empty string, the result is the
2383 -- right operand, including its bounds if anomalous.
2386 and then Is_Array_Type
(Etype
(Right
))
2387 and then Etype
(Right
) /= Any_String
2389 Set_Etype
(N
, Etype
(Right
));
2392 Fold_Str
(N
, Folded_Val
, Static
=> Stat
);
2394 end Eval_Concatenation
;
2396 ----------------------
2397 -- Eval_Entity_Name --
2398 ----------------------
2400 -- This procedure is used for identifiers and expanded names other than
2401 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2402 -- static if they denote a static constant (RM 4.9(6)) or if the name
2403 -- denotes an enumeration literal (RM 4.9(22)).
2405 procedure Eval_Entity_Name
(N
: Node_Id
) is
2406 Def_Id
: constant Entity_Id
:= Entity
(N
);
2410 -- Enumeration literals are always considered to be constants
2411 -- and cannot raise constraint error (RM 4.9(22)).
2413 if Ekind
(Def_Id
) = E_Enumeration_Literal
then
2414 Set_Is_Static_Expression
(N
);
2417 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2418 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2419 -- it does not violate 10.2.1(8) here, since this is not a variable.
2421 elsif Ekind
(Def_Id
) = E_Constant
then
2423 -- Deferred constants must always be treated as nonstatic outside the
2424 -- scope of their full view.
2426 if Present
(Full_View
(Def_Id
))
2427 and then not In_Open_Scopes
(Scope
(Def_Id
))
2431 Val
:= Constant_Value
(Def_Id
);
2434 if Present
(Val
) then
2435 Set_Is_Static_Expression
2436 (N
, Is_Static_Expression
(Val
)
2437 and then Is_Static_Subtype
(Etype
(Def_Id
)));
2438 Set_Raises_Constraint_Error
(N
, Raises_Constraint_Error
(Val
));
2440 if not Is_Static_Expression
(N
)
2441 and then not Is_Generic_Type
(Etype
(N
))
2443 Validate_Static_Object_Name
(N
);
2446 -- Mark constant condition in SCOs
2449 and then Comes_From_Source
(N
)
2450 and then Is_Boolean_Type
(Etype
(Def_Id
))
2451 and then Compile_Time_Known_Value
(N
)
2453 Set_SCO_Condition
(N
, Expr_Value_E
(N
) = Standard_True
);
2460 -- Fall through if the name is not static
2462 Validate_Static_Object_Name
(N
);
2463 end Eval_Entity_Name
;
2465 ------------------------
2466 -- Eval_If_Expression --
2467 ------------------------
2469 -- We can fold to a static expression if the condition and both dependent
2470 -- expressions are static. Otherwise, the only required processing is to do
2471 -- the check for non-static context for the then and else expressions.
2473 procedure Eval_If_Expression
(N
: Node_Id
) is
2474 Condition
: constant Node_Id
:= First
(Expressions
(N
));
2475 Then_Expr
: constant Node_Id
:= Next
(Condition
);
2476 Else_Expr
: constant Node_Id
:= Next
(Then_Expr
);
2478 Non_Result
: Node_Id
;
2480 Rstat
: constant Boolean :=
2481 Is_Static_Expression
(Condition
)
2483 Is_Static_Expression
(Then_Expr
)
2485 Is_Static_Expression
(Else_Expr
);
2486 -- True if result is static
2489 -- If result not static, nothing to do, otherwise set static result
2494 Set_Is_Static_Expression
(N
);
2497 -- If any operand is Any_Type, just propagate to result and do not try
2498 -- to fold, this prevents cascaded errors.
2500 if Etype
(Condition
) = Any_Type
or else
2501 Etype
(Then_Expr
) = Any_Type
or else
2502 Etype
(Else_Expr
) = Any_Type
2504 Set_Etype
(N
, Any_Type
);
2505 Set_Is_Static_Expression
(N
, False);
2509 -- If condition raises constraint error then we have already signaled
2510 -- an error, and we just propagate to the result and do not fold.
2512 if Raises_Constraint_Error
(Condition
) then
2513 Set_Raises_Constraint_Error
(N
);
2517 -- Static case where we can fold. Note that we don't try to fold cases
2518 -- where the condition is known at compile time, but the result is
2519 -- non-static. This avoids possible cases of infinite recursion where
2520 -- the expander puts in a redundant test and we remove it. Instead we
2521 -- deal with these cases in the expander.
2523 -- Select result operand
2525 if Is_True
(Expr_Value
(Condition
)) then
2526 Result
:= Then_Expr
;
2527 Non_Result
:= Else_Expr
;
2529 Result
:= Else_Expr
;
2530 Non_Result
:= Then_Expr
;
2533 -- Note that it does not matter if the non-result operand raises a
2534 -- Constraint_Error, but if the result raises constraint error then we
2535 -- replace the node with a raise constraint error. This will properly
2536 -- propagate Raises_Constraint_Error since this flag is set in Result.
2538 if Raises_Constraint_Error
(Result
) then
2539 Rewrite_In_Raise_CE
(N
, Result
);
2540 Check_Non_Static_Context
(Non_Result
);
2542 -- Otherwise the result operand replaces the original node
2545 Rewrite
(N
, Relocate_Node
(Result
));
2546 Set_Is_Static_Expression
(N
);
2548 end Eval_If_Expression
;
2550 ----------------------------
2551 -- Eval_Indexed_Component --
2552 ----------------------------
2554 -- Indexed components are never static, so we need to perform the check
2555 -- for non-static context on the index values. Then, we check if the
2556 -- value can be obtained at compile time, even though it is non-static.
2558 procedure Eval_Indexed_Component
(N
: Node_Id
) is
2562 -- Check for non-static context on index values
2564 Expr
:= First
(Expressions
(N
));
2565 while Present
(Expr
) loop
2566 Check_Non_Static_Context
(Expr
);
2570 -- If the indexed component appears in an object renaming declaration
2571 -- then we do not want to try to evaluate it, since in this case we
2572 -- need the identity of the array element.
2574 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
2577 -- Similarly if the indexed component appears as the prefix of an
2578 -- attribute we don't want to evaluate it, because at least for
2579 -- some cases of attributes we need the identify (e.g. Access, Size)
2581 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
then
2585 -- Note: there are other cases, such as the left side of an assignment,
2586 -- or an OUT parameter for a call, where the replacement results in the
2587 -- illegal use of a constant, But these cases are illegal in the first
2588 -- place, so the replacement, though silly, is harmless.
2590 -- Now see if this is a constant array reference
2592 if List_Length
(Expressions
(N
)) = 1
2593 and then Is_Entity_Name
(Prefix
(N
))
2594 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
2595 and then Present
(Constant_Value
(Entity
(Prefix
(N
))))
2598 Loc
: constant Source_Ptr
:= Sloc
(N
);
2599 Arr
: constant Node_Id
:= Constant_Value
(Entity
(Prefix
(N
)));
2600 Sub
: constant Node_Id
:= First
(Expressions
(N
));
2606 -- Linear one's origin subscript value for array reference
2609 -- Lower bound of the first array index
2612 -- Value from constant array
2615 Atyp
:= Etype
(Arr
);
2617 if Is_Access_Type
(Atyp
) then
2618 Atyp
:= Designated_Type
(Atyp
);
2621 -- If we have an array type (we should have but perhaps there are
2622 -- error cases where this is not the case), then see if we can do
2623 -- a constant evaluation of the array reference.
2625 if Is_Array_Type
(Atyp
) and then Atyp
/= Any_Composite
then
2626 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
2627 Lbd
:= String_Literal_Low_Bound
(Atyp
);
2629 Lbd
:= Type_Low_Bound
(Etype
(First_Index
(Atyp
)));
2632 if Compile_Time_Known_Value
(Sub
)
2633 and then Nkind
(Arr
) = N_Aggregate
2634 and then Compile_Time_Known_Value
(Lbd
)
2635 and then Is_Discrete_Type
(Component_Type
(Atyp
))
2637 Lin
:= UI_To_Int
(Expr_Value
(Sub
) - Expr_Value
(Lbd
)) + 1;
2639 if List_Length
(Expressions
(Arr
)) >= Lin
then
2640 Elm
:= Pick
(Expressions
(Arr
), Lin
);
2642 -- If the resulting expression is compile-time-known,
2643 -- then we can rewrite the indexed component with this
2644 -- value, being sure to mark the result as non-static.
2645 -- We also reset the Sloc, in case this generates an
2646 -- error later on (e.g. 136'Access).
2648 if Compile_Time_Known_Value
(Elm
) then
2649 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2650 Set_Is_Static_Expression
(N
, False);
2655 -- We can also constant-fold if the prefix is a string literal.
2656 -- This will be useful in an instantiation or an inlining.
2658 elsif Compile_Time_Known_Value
(Sub
)
2659 and then Nkind
(Arr
) = N_String_Literal
2660 and then Compile_Time_Known_Value
(Lbd
)
2661 and then Expr_Value
(Lbd
) = 1
2662 and then Expr_Value
(Sub
) <=
2663 String_Literal_Length
(Etype
(Arr
))
2666 C
: constant Char_Code
:=
2667 Get_String_Char
(Strval
(Arr
),
2668 UI_To_Int
(Expr_Value
(Sub
)));
2670 Set_Character_Literal_Name
(C
);
2673 Make_Character_Literal
(Loc
,
2675 Char_Literal_Value
=> UI_From_CC
(C
));
2676 Set_Etype
(Elm
, Component_Type
(Atyp
));
2677 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2678 Set_Is_Static_Expression
(N
, False);
2684 end Eval_Indexed_Component
;
2686 --------------------------
2687 -- Eval_Integer_Literal --
2688 --------------------------
2690 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2691 -- as static by the analyzer. The reason we did it that early is to allow
2692 -- the possibility of turning off the Is_Static_Expression flag after
2693 -- analysis, but before resolution, when integer literals are generated in
2694 -- the expander that do not correspond to static expressions.
2696 procedure Eval_Integer_Literal
(N
: Node_Id
) is
2697 function In_Any_Integer_Context
(Context
: Node_Id
) return Boolean;
2698 -- If the literal is resolved with a specific type in a context where
2699 -- the expected type is Any_Integer, there are no range checks on the
2700 -- literal. By the time the literal is evaluated, it carries the type
2701 -- imposed by the enclosing expression, and we must recover the context
2702 -- to determine that Any_Integer is meant.
2704 ----------------------------
2705 -- In_Any_Integer_Context --
2706 ----------------------------
2708 function In_Any_Integer_Context
(Context
: Node_Id
) return Boolean is
2710 -- Any_Integer also appears in digits specifications for real types,
2711 -- but those have bounds smaller that those of any integer base type,
2712 -- so we can safely ignore these cases.
2715 Nkind_In
(Context
, N_Attribute_Definition_Clause
,
2716 N_Attribute_Reference
,
2717 N_Modular_Type_Definition
,
2718 N_Number_Declaration
,
2719 N_Signed_Integer_Type_Definition
);
2720 end In_Any_Integer_Context
;
2724 Par
: constant Node_Id
:= Parent
(N
);
2725 Typ
: constant Entity_Id
:= Etype
(N
);
2727 -- Start of processing for Eval_Integer_Literal
2730 -- If the literal appears in a non-expression context, then it is
2731 -- certainly appearing in a non-static context, so check it. This is
2732 -- actually a redundant check, since Check_Non_Static_Context would
2733 -- check it, but it seems worthwhile to optimize out the call.
2735 -- Additionally, when the literal appears within an if or case
2736 -- expression it must be checked as well. However, due to the literal
2737 -- appearing within a conditional statement, expansion greatly changes
2738 -- the nature of its context and performing some of the checks within
2739 -- Check_Non_Static_Context on an expanded literal may lead to spurious
2740 -- and misleading warnings.
2742 if (Nkind_In
(Par
, N_Case_Expression_Alternative
, N_If_Expression
)
2743 or else Nkind
(Parent
(N
)) not in N_Subexpr
)
2744 and then (not Nkind_In
(Par
, N_Case_Expression_Alternative
,
2746 or else Comes_From_Source
(N
))
2747 and then not In_Any_Integer_Context
(Par
)
2749 Check_Non_Static_Context
(N
);
2752 -- Modular integer literals must be in their base range
2754 if Is_Modular_Integer_Type
(Typ
)
2755 and then Is_Out_Of_Range
(N
, Base_Type
(Typ
), Assume_Valid
=> True)
2759 end Eval_Integer_Literal
;
2761 ---------------------
2762 -- Eval_Logical_Op --
2763 ---------------------
2765 -- Logical operations are static functions, so the result is potentially
2766 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2768 procedure Eval_Logical_Op
(N
: Node_Id
) is
2769 Left
: constant Node_Id
:= Left_Opnd
(N
);
2770 Right
: constant Node_Id
:= Right_Opnd
(N
);
2775 -- If not foldable we are done
2777 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2783 -- Compile time evaluation of logical operation
2786 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2787 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2790 if Is_Modular_Integer_Type
(Etype
(N
)) then
2792 Left_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2793 Right_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2796 To_Bits
(Left_Int
, Left_Bits
);
2797 To_Bits
(Right_Int
, Right_Bits
);
2799 -- Note: should really be able to use array ops instead of
2800 -- these loops, but they weren't working at the time ???
2802 if Nkind
(N
) = N_Op_And
then
2803 for J
in Left_Bits
'Range loop
2804 Left_Bits
(J
) := Left_Bits
(J
) and Right_Bits
(J
);
2807 elsif Nkind
(N
) = N_Op_Or
then
2808 for J
in Left_Bits
'Range loop
2809 Left_Bits
(J
) := Left_Bits
(J
) or Right_Bits
(J
);
2813 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2815 for J
in Left_Bits
'Range loop
2816 Left_Bits
(J
) := Left_Bits
(J
) xor Right_Bits
(J
);
2820 Fold_Uint
(N
, From_Bits
(Left_Bits
, Etype
(N
)), Stat
);
2824 pragma Assert
(Is_Boolean_Type
(Etype
(N
)));
2826 if Nkind
(N
) = N_Op_And
then
2828 Test
(Is_True
(Left_Int
) and then Is_True
(Right_Int
)), Stat
);
2830 elsif Nkind
(N
) = N_Op_Or
then
2832 Test
(Is_True
(Left_Int
) or else Is_True
(Right_Int
)), Stat
);
2835 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2837 Test
(Is_True
(Left_Int
) xor Is_True
(Right_Int
)), Stat
);
2841 end Eval_Logical_Op
;
2843 ------------------------
2844 -- Eval_Membership_Op --
2845 ------------------------
2847 -- A membership test is potentially static if the expression is static, and
2848 -- the range is a potentially static range, or is a subtype mark denoting a
2849 -- static subtype (RM 4.9(12)).
2851 procedure Eval_Membership_Op
(N
: Node_Id
) is
2852 Alts
: constant List_Id
:= Alternatives
(N
);
2853 Choice
: constant Node_Id
:= Right_Opnd
(N
);
2854 Expr
: constant Node_Id
:= Left_Opnd
(N
);
2855 Result
: Match_Result
;
2858 -- Ignore if error in either operand, except to make sure that Any_Type
2859 -- is properly propagated to avoid junk cascaded errors.
2861 if Etype
(Expr
) = Any_Type
2862 or else (Present
(Choice
) and then Etype
(Choice
) = Any_Type
)
2864 Set_Etype
(N
, Any_Type
);
2868 -- If left operand non-static, then nothing to do
2870 if not Is_Static_Expression
(Expr
) then
2874 -- If choice is non-static, left operand is in non-static context
2876 if (Present
(Choice
) and then not Is_Static_Choice
(Choice
))
2877 or else (Present
(Alts
) and then not Is_Static_Choice_List
(Alts
))
2879 Check_Non_Static_Context
(Expr
);
2883 -- Otherwise we definitely have a static expression
2885 Set_Is_Static_Expression
(N
);
2887 -- If left operand raises constraint error, propagate and we are done
2889 if Raises_Constraint_Error
(Expr
) then
2890 Set_Raises_Constraint_Error
(N
, True);
2895 if Present
(Choice
) then
2896 Result
:= Choice_Matches
(Expr
, Choice
);
2898 Result
:= Choices_Match
(Expr
, Alts
);
2901 -- If result is Non_Static, it means that we raise Constraint_Error,
2902 -- since we already tested that the operands were themselves static.
2904 if Result
= Non_Static
then
2905 Set_Raises_Constraint_Error
(N
);
2907 -- Otherwise we have our result (flipped if NOT IN case)
2911 (N
, Test
((Result
= Match
) xor (Nkind
(N
) = N_Not_In
)), True);
2912 Warn_On_Known_Condition
(N
);
2915 end Eval_Membership_Op
;
2917 ------------------------
2918 -- Eval_Named_Integer --
2919 ------------------------
2921 procedure Eval_Named_Integer
(N
: Node_Id
) is
2924 Expr_Value
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2925 end Eval_Named_Integer
;
2927 ---------------------
2928 -- Eval_Named_Real --
2929 ---------------------
2931 procedure Eval_Named_Real
(N
: Node_Id
) is
2934 Expr_Value_R
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2935 end Eval_Named_Real
;
2941 -- Exponentiation is a static functions, so the result is potentially
2942 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2944 procedure Eval_Op_Expon
(N
: Node_Id
) is
2945 Left
: constant Node_Id
:= Left_Opnd
(N
);
2946 Right
: constant Node_Id
:= Right_Opnd
(N
);
2951 -- If not foldable we are done
2953 Test_Expression_Is_Foldable
2954 (N
, Left
, Right
, Stat
, Fold
, CRT_Safe
=> True);
2956 -- Return if not foldable
2962 if Configurable_Run_Time_Mode
and not Stat
then
2966 -- Fold exponentiation operation
2969 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2974 if Is_Integer_Type
(Etype
(Left
)) then
2976 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2980 -- Exponentiation of an integer raises Constraint_Error for a
2981 -- negative exponent (RM 4.5.6).
2983 if Right_Int
< 0 then
2984 Apply_Compile_Time_Constraint_Error
2985 (N
, "integer exponent negative", CE_Range_Check_Failed
,
2990 if OK_Bits
(N
, Num_Bits
(Left_Int
) * Right_Int
) then
2991 Result
:= Left_Int
** Right_Int
;
2996 if Is_Modular_Integer_Type
(Etype
(N
)) then
2997 Result
:= Result
mod Modulus
(Etype
(N
));
3000 Fold_Uint
(N
, Result
, Stat
);
3008 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
3011 -- Cannot have a zero base with a negative exponent
3013 if UR_Is_Zero
(Left_Real
) then
3015 if Right_Int
< 0 then
3016 Apply_Compile_Time_Constraint_Error
3017 (N
, "zero ** negative integer", CE_Range_Check_Failed
,
3021 Fold_Ureal
(N
, Ureal_0
, Stat
);
3025 Fold_Ureal
(N
, Left_Real
** Right_Int
, Stat
);
3036 -- The not operation is a static functions, so the result is potentially
3037 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
3039 procedure Eval_Op_Not
(N
: Node_Id
) is
3040 Right
: constant Node_Id
:= Right_Opnd
(N
);
3045 -- If not foldable we are done
3047 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
3053 -- Fold not operation
3056 Rint
: constant Uint
:= Expr_Value
(Right
);
3057 Typ
: constant Entity_Id
:= Etype
(N
);
3060 -- Negation is equivalent to subtracting from the modulus minus one.
3061 -- For a binary modulus this is equivalent to the ones-complement of
3062 -- the original value. For a nonbinary modulus this is an arbitrary
3063 -- but consistent definition.
3065 if Is_Modular_Integer_Type
(Typ
) then
3066 Fold_Uint
(N
, Modulus
(Typ
) - 1 - Rint
, Stat
);
3067 else pragma Assert
(Is_Boolean_Type
(Typ
));
3068 Fold_Uint
(N
, Test
(not Is_True
(Rint
)), Stat
);
3071 Set_Is_Static_Expression
(N
, Stat
);
3075 -------------------------------
3076 -- Eval_Qualified_Expression --
3077 -------------------------------
3079 -- A qualified expression is potentially static if its subtype mark denotes
3080 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
3082 procedure Eval_Qualified_Expression
(N
: Node_Id
) is
3083 Operand
: constant Node_Id
:= Expression
(N
);
3084 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
3091 -- Can only fold if target is string or scalar and subtype is static.
3092 -- Also, do not fold if our parent is an allocator (this is because the
3093 -- qualified expression is really part of the syntactic structure of an
3094 -- allocator, and we do not want to end up with something that
3095 -- corresponds to "new 1" where the 1 is the result of folding a
3096 -- qualified expression).
3098 if not Is_Static_Subtype
(Target_Type
)
3099 or else Nkind
(Parent
(N
)) = N_Allocator
3101 Check_Non_Static_Context
(Operand
);
3103 -- If operand is known to raise constraint_error, set the flag on the
3104 -- expression so it does not get optimized away.
3106 if Nkind
(Operand
) = N_Raise_Constraint_Error
then
3107 Set_Raises_Constraint_Error
(N
);
3113 -- If not foldable we are done
3115 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3120 -- Don't try fold if target type has constraint error bounds
3122 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3123 Set_Raises_Constraint_Error
(N
);
3127 -- Here we will fold, save Print_In_Hex indication
3129 Hex
:= Nkind
(Operand
) = N_Integer_Literal
3130 and then Print_In_Hex
(Operand
);
3132 -- Fold the result of qualification
3134 if Is_Discrete_Type
(Target_Type
) then
3135 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3137 -- Preserve Print_In_Hex indication
3139 if Hex
and then Nkind
(N
) = N_Integer_Literal
then
3140 Set_Print_In_Hex
(N
);
3143 elsif Is_Real_Type
(Target_Type
) then
3144 Fold_Ureal
(N
, Expr_Value_R
(Operand
), Stat
);
3147 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Stat
);
3150 Set_Is_Static_Expression
(N
, False);
3152 Check_String_Literal_Length
(N
, Target_Type
);
3158 -- The expression may be foldable but not static
3160 Set_Is_Static_Expression
(N
, Stat
);
3162 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3165 end Eval_Qualified_Expression
;
3167 -----------------------
3168 -- Eval_Real_Literal --
3169 -----------------------
3171 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3172 -- as static by the analyzer. The reason we did it that early is to allow
3173 -- the possibility of turning off the Is_Static_Expression flag after
3174 -- analysis, but before resolution, when integer literals are generated
3175 -- in the expander that do not correspond to static expressions.
3177 procedure Eval_Real_Literal
(N
: Node_Id
) is
3178 PK
: constant Node_Kind
:= Nkind
(Parent
(N
));
3181 -- If the literal appears in a non-expression context and not as part of
3182 -- a number declaration, then it is appearing in a non-static context,
3185 if PK
not in N_Subexpr
and then PK
/= N_Number_Declaration
then
3186 Check_Non_Static_Context
(N
);
3188 end Eval_Real_Literal
;
3190 ------------------------
3191 -- Eval_Relational_Op --
3192 ------------------------
3194 -- Relational operations are static functions, so the result is static if
3195 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3196 -- the result is never static, even if the operands are.
3198 -- However, for internally generated nodes, we allow string equality and
3199 -- inequality to be static. This is because we rewrite A in "ABC" as an
3200 -- equality test A = "ABC", and the former is definitely static.
3202 procedure Eval_Relational_Op
(N
: Node_Id
) is
3203 Left
: constant Node_Id
:= Left_Opnd
(N
);
3204 Right
: constant Node_Id
:= Right_Opnd
(N
);
3206 procedure Decompose_Expr
3208 Ent
: out Entity_Id
;
3209 Kind
: out Character;
3211 Orig
: Boolean := True);
3212 -- Given expression Expr, see if it is of the form X [+/- K]. If so, Ent
3213 -- is set to the entity in X, Kind is 'F','L','E' for 'First or 'Last or
3214 -- simple entity, and Cons is the value of K. If the expression is not
3215 -- of the required form, Ent is set to Empty.
3217 -- Orig indicates whether Expr is the original expression to consider,
3218 -- or if we are handling a subexpression (e.g. recursive call to
3221 procedure Fold_General_Op
(Is_Static
: Boolean);
3222 -- Attempt to fold arbitrary relational operator N. Flag Is_Static must
3223 -- be set when the operator denotes a static expression.
3225 procedure Fold_Static_Real_Op
;
3226 -- Attempt to fold static real type relational operator N
3228 function Static_Length
(Expr
: Node_Id
) return Uint
;
3229 -- If Expr is an expression for a constrained array whose length is
3230 -- known at compile time, return the non-negative length, otherwise
3233 --------------------
3234 -- Decompose_Expr --
3235 --------------------
3237 procedure Decompose_Expr
3239 Ent
: out Entity_Id
;
3240 Kind
: out Character;
3242 Orig
: Boolean := True)
3247 -- Assume that the expression does not meet the expected form
3253 if Nkind
(Expr
) = N_Op_Add
3254 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3256 Exp
:= Left_Opnd
(Expr
);
3257 Cons
:= Expr_Value
(Right_Opnd
(Expr
));
3259 elsif Nkind
(Expr
) = N_Op_Subtract
3260 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3262 Exp
:= Left_Opnd
(Expr
);
3263 Cons
:= -Expr_Value
(Right_Opnd
(Expr
));
3265 -- If the bound is a constant created to remove side effects, recover
3266 -- the original expression to see if it has one of the recognizable
3269 elsif Nkind
(Expr
) = N_Identifier
3270 and then not Comes_From_Source
(Entity
(Expr
))
3271 and then Ekind
(Entity
(Expr
)) = E_Constant
3272 and then Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
3274 Exp
:= Expression
(Parent
(Entity
(Expr
)));
3275 Decompose_Expr
(Exp
, Ent
, Kind
, Cons
, Orig
=> False);
3277 -- If original expression includes an entity, create a reference
3278 -- to it for use below.
3280 if Present
(Ent
) then
3281 Exp
:= New_Occurrence_Of
(Ent
, Sloc
(Ent
));
3287 -- Only consider the case of X + 0 for a full expression, and
3288 -- not when recursing, otherwise we may end up with evaluating
3289 -- expressions not known at compile time to 0.
3299 -- At this stage Exp is set to the potential X
3301 if Nkind
(Exp
) = N_Attribute_Reference
then
3302 if Attribute_Name
(Exp
) = Name_First
then
3304 elsif Attribute_Name
(Exp
) = Name_Last
then
3310 Exp
:= Prefix
(Exp
);
3316 if Is_Entity_Name
(Exp
) and then Present
(Entity
(Exp
)) then
3317 Ent
:= Entity
(Exp
);
3321 ---------------------
3322 -- Fold_General_Op --
3323 ---------------------
3325 procedure Fold_General_Op
(Is_Static
: Boolean) is
3326 CR
: constant Compare_Result
:=
3327 Compile_Time_Compare
(Left
, Right
, Assume_Valid
=> False);
3332 if CR
= Unknown
then
3340 elsif CR
= NE
or else CR
= GT
or else CR
= LT
then
3347 if CR
= GT
or else CR
= EQ
or else CR
= GE
then
3358 elsif CR
= EQ
or else CR
= LT
or else CR
= LE
then
3365 if CR
= LT
or else CR
= EQ
or else CR
= LE
then
3376 elsif CR
= EQ
or else CR
= GT
or else CR
= GE
then
3383 if CR
= NE
or else CR
= GT
or else CR
= LT
then
3392 raise Program_Error
;
3395 -- Determine the potential outcome of the relation assuming the
3396 -- operands are valid and emit a warning when the relation yields
3397 -- True or False only in the presence of invalid values.
3399 Warn_On_Constant_Valid_Condition
(N
);
3401 Fold_Uint
(N
, Test
(Result
), Is_Static
);
3402 end Fold_General_Op
;
3404 -------------------------
3405 -- Fold_Static_Real_Op --
3406 -------------------------
3408 procedure Fold_Static_Real_Op
is
3409 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
3410 Right_Real
: constant Ureal
:= Expr_Value_R
(Right
);
3415 when N_Op_Eq
=> Result
:= (Left_Real
= Right_Real
);
3416 when N_Op_Ge
=> Result
:= (Left_Real
>= Right_Real
);
3417 when N_Op_Gt
=> Result
:= (Left_Real
> Right_Real
);
3418 when N_Op_Le
=> Result
:= (Left_Real
<= Right_Real
);
3419 when N_Op_Lt
=> Result
:= (Left_Real
< Right_Real
);
3420 when N_Op_Ne
=> Result
:= (Left_Real
/= Right_Real
);
3421 when others => raise Program_Error
;
3424 Fold_Uint
(N
, Test
(Result
), True);
3425 end Fold_Static_Real_Op
;
3431 function Static_Length
(Expr
: Node_Id
) return Uint
is
3441 -- First easy case string literal
3443 if Nkind
(Expr
) = N_String_Literal
then
3444 return UI_From_Int
(String_Length
(Strval
(Expr
)));
3446 -- With frontend inlining as performed in GNATprove mode, a variable
3447 -- may be inserted that has a string literal subtype. Deal with this
3448 -- specially as for the previous case.
3450 elsif Ekind
(Etype
(Expr
)) = E_String_Literal_Subtype
then
3451 return String_Literal_Length
(Etype
(Expr
));
3453 -- Second easy case, not constrained subtype, so no length
3455 elsif not Is_Constrained
(Etype
(Expr
)) then
3456 return Uint_Minus_1
;
3461 Typ
:= Etype
(First_Index
(Etype
(Expr
)));
3463 -- The simple case, both bounds are known at compile time
3465 if Is_Discrete_Type
(Typ
)
3466 and then Compile_Time_Known_Value
(Type_Low_Bound
(Typ
))
3467 and then Compile_Time_Known_Value
(Type_High_Bound
(Typ
))
3470 UI_Max
(Uint_0
, Expr_Value
(Type_High_Bound
(Typ
)) -
3471 Expr_Value
(Type_Low_Bound
(Typ
)) + 1);
3474 -- A more complex case, where the bounds are of the form X [+/- K1]
3475 -- .. X [+/- K2]), where X is an expression that is either A'First or
3476 -- A'Last (with A an entity name), or X is an entity name, and the
3477 -- two X's are the same and K1 and K2 are known at compile time, in
3478 -- this case, the length can also be computed at compile time, even
3479 -- though the bounds are not known. A common case of this is e.g.
3480 -- (X'First .. X'First+5).
3483 (Original_Node
(Type_Low_Bound
(Typ
)), Ent1
, Kind1
, Cons1
);
3485 (Original_Node
(Type_High_Bound
(Typ
)), Ent2
, Kind2
, Cons2
);
3487 if Present
(Ent1
) and then Ent1
= Ent2
and then Kind1
= Kind2
then
3488 return Cons2
- Cons1
+ 1;
3490 return Uint_Minus_1
;
3496 Left_Typ
: constant Entity_Id
:= Etype
(Left
);
3497 Right_Typ
: constant Entity_Id
:= Etype
(Right
);
3500 Op_Typ
: Entity_Id
:= Empty
;
3503 Is_Static_Expression
: Boolean;
3505 -- Start of processing for Eval_Relational_Op
3508 -- One special case to deal with first. If we can tell that the result
3509 -- will be false because the lengths of one or more index subtypes are
3510 -- compile-time known and different, then we can replace the entire
3511 -- result by False. We only do this for one-dimensional arrays, because
3512 -- the case of multidimensional arrays is rare and too much trouble. If
3513 -- one of the operands is an illegal aggregate, its type might still be
3514 -- an arbitrary composite type, so nothing to do.
3516 if Is_Array_Type
(Left_Typ
)
3517 and then Left_Typ
/= Any_Composite
3518 and then Number_Dimensions
(Left_Typ
) = 1
3519 and then Nkind_In
(N
, N_Op_Eq
, N_Op_Ne
)
3521 if Raises_Constraint_Error
(Left
)
3523 Raises_Constraint_Error
(Right
)
3527 -- OK, we have the case where we may be able to do this fold
3530 Left_Len
:= Static_Length
(Left
);
3531 Right_Len
:= Static_Length
(Right
);
3533 if Left_Len
/= Uint_Minus_1
3534 and then Right_Len
/= Uint_Minus_1
3535 and then Left_Len
/= Right_Len
3537 Fold_Uint
(N
, Test
(Nkind
(N
) = N_Op_Ne
), False);
3538 Warn_On_Known_Condition
(N
);
3546 -- Initialize the value of Is_Static_Expression. The value of Fold
3547 -- returned by Test_Expression_Is_Foldable is not needed since, even
3548 -- when some operand is a variable, we can still perform the static
3549 -- evaluation of the expression in some cases (for example, for a
3550 -- variable of a subtype of Integer we statically know that any value
3551 -- stored in such variable is smaller than Integer'Last).
3553 Test_Expression_Is_Foldable
3554 (N
, Left
, Right
, Is_Static_Expression
, Fold
);
3556 -- Only comparisons of scalars can give static results. A comparison
3557 -- of strings never yields a static result, even if both operands are
3558 -- static strings, except that as noted above, we allow equality and
3559 -- inequality for strings.
3561 if Is_String_Type
(Left_Typ
)
3562 and then not Comes_From_Source
(N
)
3563 and then Nkind_In
(N
, N_Op_Eq
, N_Op_Ne
)
3567 elsif not Is_Scalar_Type
(Left_Typ
) then
3568 Is_Static_Expression
:= False;
3569 Set_Is_Static_Expression
(N
, False);
3572 -- For operators on universal numeric types called as functions with
3573 -- an explicit scope, determine appropriate specific numeric type,
3574 -- and diagnose possible ambiguity.
3576 if Is_Universal_Numeric_Type
(Left_Typ
)
3578 Is_Universal_Numeric_Type
(Right_Typ
)
3580 Op_Typ
:= Find_Universal_Operator_Type
(N
);
3583 -- Attempt to fold the relational operator
3585 if Is_Static_Expression
and then Is_Real_Type
(Left_Typ
) then
3586 Fold_Static_Real_Op
;
3588 Fold_General_Op
(Is_Static_Expression
);
3592 -- For the case of a folded relational operator on a specific numeric
3593 -- type, freeze the operand type now.
3595 if Present
(Op_Typ
) then
3596 Freeze_Before
(N
, Op_Typ
);
3599 Warn_On_Known_Condition
(N
);
3600 end Eval_Relational_Op
;
3606 -- Shift operations are intrinsic operations that can never be static, so
3607 -- the only processing required is to perform the required check for a non
3608 -- static context for the two operands.
3610 -- Actually we could do some compile time evaluation here some time ???
3612 procedure Eval_Shift
(N
: Node_Id
) is
3614 Check_Non_Static_Context
(Left_Opnd
(N
));
3615 Check_Non_Static_Context
(Right_Opnd
(N
));
3618 ------------------------
3619 -- Eval_Short_Circuit --
3620 ------------------------
3622 -- A short circuit operation is potentially static if both operands are
3623 -- potentially static (RM 4.9 (13)).
3625 procedure Eval_Short_Circuit
(N
: Node_Id
) is
3626 Kind
: constant Node_Kind
:= Nkind
(N
);
3627 Left
: constant Node_Id
:= Left_Opnd
(N
);
3628 Right
: constant Node_Id
:= Right_Opnd
(N
);
3631 Rstat
: constant Boolean :=
3632 Is_Static_Expression
(Left
)
3634 Is_Static_Expression
(Right
);
3637 -- Short circuit operations are never static in Ada 83
3639 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
3640 Check_Non_Static_Context
(Left
);
3641 Check_Non_Static_Context
(Right
);
3645 -- Now look at the operands, we can't quite use the normal call to
3646 -- Test_Expression_Is_Foldable here because short circuit operations
3647 -- are a special case, they can still be foldable, even if the right
3648 -- operand raises constraint error.
3650 -- If either operand is Any_Type, just propagate to result and do not
3651 -- try to fold, this prevents cascaded errors.
3653 if Etype
(Left
) = Any_Type
or else Etype
(Right
) = Any_Type
then
3654 Set_Etype
(N
, Any_Type
);
3657 -- If left operand raises constraint error, then replace node N with
3658 -- the raise constraint error node, and we are obviously not foldable.
3659 -- Is_Static_Expression is set from the two operands in the normal way,
3660 -- and we check the right operand if it is in a non-static context.
3662 elsif Raises_Constraint_Error
(Left
) then
3664 Check_Non_Static_Context
(Right
);
3667 Rewrite_In_Raise_CE
(N
, Left
);
3668 Set_Is_Static_Expression
(N
, Rstat
);
3671 -- If the result is not static, then we won't in any case fold
3673 elsif not Rstat
then
3674 Check_Non_Static_Context
(Left
);
3675 Check_Non_Static_Context
(Right
);
3679 -- Here the result is static, note that, unlike the normal processing
3680 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3681 -- the right operand raises constraint error, that's because it is not
3682 -- significant if the left operand is decisive.
3684 Set_Is_Static_Expression
(N
);
3686 -- It does not matter if the right operand raises constraint error if
3687 -- it will not be evaluated. So deal specially with the cases where
3688 -- the right operand is not evaluated. Note that we will fold these
3689 -- cases even if the right operand is non-static, which is fine, but
3690 -- of course in these cases the result is not potentially static.
3692 Left_Int
:= Expr_Value
(Left
);
3694 if (Kind
= N_And_Then
and then Is_False
(Left_Int
))
3696 (Kind
= N_Or_Else
and then Is_True
(Left_Int
))
3698 Fold_Uint
(N
, Left_Int
, Rstat
);
3702 -- If first operand not decisive, then it does matter if the right
3703 -- operand raises constraint error, since it will be evaluated, so
3704 -- we simply replace the node with the right operand. Note that this
3705 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3706 -- (both are set to True in Right).
3708 if Raises_Constraint_Error
(Right
) then
3709 Rewrite_In_Raise_CE
(N
, Right
);
3710 Check_Non_Static_Context
(Left
);
3714 -- Otherwise the result depends on the right operand
3716 Fold_Uint
(N
, Expr_Value
(Right
), Rstat
);
3718 end Eval_Short_Circuit
;
3724 -- Slices can never be static, so the only processing required is to check
3725 -- for non-static context if an explicit range is given.
3727 procedure Eval_Slice
(N
: Node_Id
) is
3728 Drange
: constant Node_Id
:= Discrete_Range
(N
);
3731 if Nkind
(Drange
) = N_Range
then
3732 Check_Non_Static_Context
(Low_Bound
(Drange
));
3733 Check_Non_Static_Context
(High_Bound
(Drange
));
3736 -- A slice of the form A (subtype), when the subtype is the index of
3737 -- the type of A, is redundant, the slice can be replaced with A, and
3738 -- this is worth a warning.
3740 if Is_Entity_Name
(Prefix
(N
)) then
3742 E
: constant Entity_Id
:= Entity
(Prefix
(N
));
3743 T
: constant Entity_Id
:= Etype
(E
);
3746 if Ekind
(E
) = E_Constant
3747 and then Is_Array_Type
(T
)
3748 and then Is_Entity_Name
(Drange
)
3750 if Is_Entity_Name
(Original_Node
(First_Index
(T
)))
3751 and then Entity
(Original_Node
(First_Index
(T
)))
3754 if Warn_On_Redundant_Constructs
then
3755 Error_Msg_N
("redundant slice denotes whole array?r?", N
);
3758 -- The following might be a useful optimization???
3760 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3767 -------------------------
3768 -- Eval_String_Literal --
3769 -------------------------
3771 procedure Eval_String_Literal
(N
: Node_Id
) is
3772 Typ
: constant Entity_Id
:= Etype
(N
);
3773 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
3779 -- Nothing to do if error type (handles cases like default expressions
3780 -- or generics where we have not yet fully resolved the type).
3782 if Bas
= Any_Type
or else Bas
= Any_String
then
3786 -- String literals are static if the subtype is static (RM 4.9(2)), so
3787 -- reset the static expression flag (it was set unconditionally in
3788 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3789 -- the subtype is static by looking at the lower bound.
3791 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3792 if not Is_OK_Static_Expression
(String_Literal_Low_Bound
(Typ
)) then
3793 Set_Is_Static_Expression
(N
, False);
3797 -- Here if Etype of string literal is normal Etype (not yet possible,
3798 -- but may be possible in future).
3800 elsif not Is_OK_Static_Expression
3801 (Type_Low_Bound
(Etype
(First_Index
(Typ
))))
3803 Set_Is_Static_Expression
(N
, False);
3807 -- If original node was a type conversion, then result if non-static
3809 if Nkind
(Original_Node
(N
)) = N_Type_Conversion
then
3810 Set_Is_Static_Expression
(N
, False);
3814 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3815 -- if its bounds are outside the index base type and this index type is
3816 -- static. This can happen in only two ways. Either the string literal
3817 -- is too long, or it is null, and the lower bound is type'First. Either
3818 -- way it is the upper bound that is out of range of the index type.
3820 if Ada_Version
>= Ada_95
then
3821 if Is_Standard_String_Type
(Bas
) then
3822 Xtp
:= Standard_Positive
;
3824 Xtp
:= Etype
(First_Index
(Bas
));
3827 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3828 Lo
:= String_Literal_Low_Bound
(Typ
);
3830 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Typ
)));
3833 -- Check for string too long
3835 Len
:= String_Length
(Strval
(N
));
3837 if UI_From_Int
(Len
) > String_Type_Len
(Bas
) then
3839 -- Issue message. Note that this message is a warning if the
3840 -- string literal is not marked as static (happens in some cases
3841 -- of folding strings known at compile time, but not static).
3842 -- Furthermore in such cases, we reword the message, since there
3843 -- is no string literal in the source program.
3845 if Is_Static_Expression
(N
) then
3846 Apply_Compile_Time_Constraint_Error
3847 (N
, "string literal too long for}", CE_Length_Check_Failed
,
3849 Typ
=> First_Subtype
(Bas
));
3851 Apply_Compile_Time_Constraint_Error
3852 (N
, "string value too long for}", CE_Length_Check_Failed
,
3854 Typ
=> First_Subtype
(Bas
),
3858 -- Test for null string not allowed
3861 and then not Is_Generic_Type
(Xtp
)
3863 Expr_Value
(Lo
) = Expr_Value
(Type_Low_Bound
(Base_Type
(Xtp
)))
3865 -- Same specialization of message
3867 if Is_Static_Expression
(N
) then
3868 Apply_Compile_Time_Constraint_Error
3869 (N
, "null string literal not allowed for}",
3870 CE_Length_Check_Failed
,
3872 Typ
=> First_Subtype
(Bas
));
3874 Apply_Compile_Time_Constraint_Error
3875 (N
, "null string value not allowed for}",
3876 CE_Length_Check_Failed
,
3878 Typ
=> First_Subtype
(Bas
),
3883 end Eval_String_Literal
;
3885 --------------------------
3886 -- Eval_Type_Conversion --
3887 --------------------------
3889 -- A type conversion is potentially static if its subtype mark is for a
3890 -- static scalar subtype, and its operand expression is potentially static
3893 procedure Eval_Type_Conversion
(N
: Node_Id
) is
3894 Operand
: constant Node_Id
:= Expression
(N
);
3895 Source_Type
: constant Entity_Id
:= Etype
(Operand
);
3896 Target_Type
: constant Entity_Id
:= Etype
(N
);
3898 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean;
3899 -- Returns true if type T is an integer type, or if it is a fixed-point
3900 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3901 -- on the conversion node).
3903 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean;
3904 -- Returns true if type T is a floating-point type, or if it is a
3905 -- fixed-point type that is not to be treated as an integer (i.e. the
3906 -- flag Conversion_OK is not set on the conversion node).
3908 ------------------------------
3909 -- To_Be_Treated_As_Integer --
3910 ------------------------------
3912 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean is
3916 or else (Is_Fixed_Point_Type
(T
) and then Conversion_OK
(N
));
3917 end To_Be_Treated_As_Integer
;
3919 ---------------------------
3920 -- To_Be_Treated_As_Real --
3921 ---------------------------
3923 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean is
3926 Is_Floating_Point_Type
(T
)
3927 or else (Is_Fixed_Point_Type
(T
) and then not Conversion_OK
(N
));
3928 end To_Be_Treated_As_Real
;
3935 -- Start of processing for Eval_Type_Conversion
3938 -- Cannot fold if target type is non-static or if semantic error
3940 if not Is_Static_Subtype
(Target_Type
) then
3941 Check_Non_Static_Context
(Operand
);
3943 elsif Error_Posted
(N
) then
3947 -- If not foldable we are done
3949 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3954 -- Don't try fold if target type has constraint error bounds
3956 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3957 Set_Raises_Constraint_Error
(N
);
3961 -- Remaining processing depends on operand types. Note that in the
3962 -- following type test, fixed-point counts as real unless the flag
3963 -- Conversion_OK is set, in which case it counts as integer.
3965 -- Fold conversion, case of string type. The result is not static
3967 if Is_String_Type
(Target_Type
) then
3968 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Static
=> False);
3971 -- Fold conversion, case of integer target type
3973 elsif To_Be_Treated_As_Integer
(Target_Type
) then
3978 -- Integer to integer conversion
3980 if To_Be_Treated_As_Integer
(Source_Type
) then
3981 Result
:= Expr_Value
(Operand
);
3983 -- Real to integer conversion
3986 Result
:= UR_To_Uint
(Expr_Value_R
(Operand
));
3989 -- If fixed-point type (Conversion_OK must be set), then the
3990 -- result is logically an integer, but we must replace the
3991 -- conversion with the corresponding real literal, since the
3992 -- type from a semantic point of view is still fixed-point.
3994 if Is_Fixed_Point_Type
(Target_Type
) then
3996 (N
, UR_From_Uint
(Result
) * Small_Value
(Target_Type
), Stat
);
3998 -- Otherwise result is integer literal
4001 Fold_Uint
(N
, Result
, Stat
);
4005 -- Fold conversion, case of real target type
4007 elsif To_Be_Treated_As_Real
(Target_Type
) then
4012 if To_Be_Treated_As_Real
(Source_Type
) then
4013 Result
:= Expr_Value_R
(Operand
);
4015 Result
:= UR_From_Uint
(Expr_Value
(Operand
));
4018 Fold_Ureal
(N
, Result
, Stat
);
4021 -- Enumeration types
4024 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
4027 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
4031 end Eval_Type_Conversion
;
4037 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
4038 -- are potentially static if the operand is potentially static (RM 4.9(7)).
4040 procedure Eval_Unary_Op
(N
: Node_Id
) is
4041 Right
: constant Node_Id
:= Right_Opnd
(N
);
4042 Otype
: Entity_Id
:= Empty
;
4047 -- If not foldable we are done
4049 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
4055 if Etype
(Right
) = Universal_Integer
4057 Etype
(Right
) = Universal_Real
4059 Otype
:= Find_Universal_Operator_Type
(N
);
4062 -- Fold for integer case
4064 if Is_Integer_Type
(Etype
(N
)) then
4066 Rint
: constant Uint
:= Expr_Value
(Right
);
4070 -- In the case of modular unary plus and abs there is no need
4071 -- to adjust the result of the operation since if the original
4072 -- operand was in bounds the result will be in the bounds of the
4073 -- modular type. However, in the case of modular unary minus the
4074 -- result may go out of the bounds of the modular type and needs
4077 if Nkind
(N
) = N_Op_Plus
then
4080 elsif Nkind
(N
) = N_Op_Minus
then
4081 if Is_Modular_Integer_Type
(Etype
(N
)) then
4082 Result
:= (-Rint
) mod Modulus
(Etype
(N
));
4088 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
4092 Fold_Uint
(N
, Result
, Stat
);
4095 -- Fold for real case
4097 elsif Is_Real_Type
(Etype
(N
)) then
4099 Rreal
: constant Ureal
:= Expr_Value_R
(Right
);
4103 if Nkind
(N
) = N_Op_Plus
then
4105 elsif Nkind
(N
) = N_Op_Minus
then
4106 Result
:= UR_Negate
(Rreal
);
4108 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
4109 Result
:= abs Rreal
;
4112 Fold_Ureal
(N
, Result
, Stat
);
4116 -- If the operator was resolved to a specific type, make sure that type
4117 -- is frozen even if the expression is folded into a literal (which has
4118 -- a universal type).
4120 if Present
(Otype
) then
4121 Freeze_Before
(N
, Otype
);
4125 -------------------------------
4126 -- Eval_Unchecked_Conversion --
4127 -------------------------------
4129 -- Unchecked conversions can never be static, so the only required
4130 -- processing is to check for a non-static context for the operand.
4132 procedure Eval_Unchecked_Conversion
(N
: Node_Id
) is
4134 Check_Non_Static_Context
(Expression
(N
));
4135 end Eval_Unchecked_Conversion
;
4137 --------------------
4138 -- Expr_Rep_Value --
4139 --------------------
4141 function Expr_Rep_Value
(N
: Node_Id
) return Uint
is
4142 Kind
: constant Node_Kind
:= Nkind
(N
);
4146 if Is_Entity_Name
(N
) then
4149 -- An enumeration literal that was either in the source or created
4150 -- as a result of static evaluation.
4152 if Ekind
(Ent
) = E_Enumeration_Literal
then
4153 return Enumeration_Rep
(Ent
);
4155 -- A user defined static constant
4158 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4159 return Expr_Rep_Value
(Constant_Value
(Ent
));
4162 -- An integer literal that was either in the source or created as a
4163 -- result of static evaluation.
4165 elsif Kind
= N_Integer_Literal
then
4168 -- A real literal for a fixed-point type. This must be the fixed-point
4169 -- case, either the literal is of a fixed-point type, or it is a bound
4170 -- of a fixed-point type, with type universal real. In either case we
4171 -- obtain the desired value from Corresponding_Integer_Value.
4173 elsif Kind
= N_Real_Literal
then
4174 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4175 return Corresponding_Integer_Value
(N
);
4177 -- Otherwise must be character literal
4180 pragma Assert
(Kind
= N_Character_Literal
);
4183 -- Since Character literals of type Standard.Character don't have any
4184 -- defining character literals built for them, they do not have their
4185 -- Entity set, so just use their Char code. Otherwise for user-
4186 -- defined character literals use their Pos value as usual which is
4187 -- the same as the Rep value.
4190 return Char_Literal_Value
(N
);
4192 return Enumeration_Rep
(Ent
);
4201 function Expr_Value
(N
: Node_Id
) return Uint
is
4202 Kind
: constant Node_Kind
:= Nkind
(N
);
4203 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(N
) mod CV_Cache_Size
);
4208 -- If already in cache, then we know it's compile-time-known and we can
4209 -- return the value that was previously stored in the cache since
4210 -- compile-time-known values cannot change.
4212 if CV_Ent
.N
= N
then
4216 -- Otherwise proceed to test value
4218 if Is_Entity_Name
(N
) then
4221 -- An enumeration literal that was either in the source or created as
4222 -- a result of static evaluation.
4224 if Ekind
(Ent
) = E_Enumeration_Literal
then
4225 Val
:= Enumeration_Pos
(Ent
);
4227 -- A user defined static constant
4230 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4231 Val
:= Expr_Value
(Constant_Value
(Ent
));
4234 -- An integer literal that was either in the source or created as a
4235 -- result of static evaluation.
4237 elsif Kind
= N_Integer_Literal
then
4240 -- A real literal for a fixed-point type. This must be the fixed-point
4241 -- case, either the literal is of a fixed-point type, or it is a bound
4242 -- of a fixed-point type, with type universal real. In either case we
4243 -- obtain the desired value from Corresponding_Integer_Value.
4245 elsif Kind
= N_Real_Literal
then
4246 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4247 Val
:= Corresponding_Integer_Value
(N
);
4249 -- The NULL access value
4251 elsif Kind
= N_Null
then
4252 pragma Assert
(Is_Access_Type
(Underlying_Type
(Etype
(N
))));
4255 -- Otherwise must be character literal
4258 pragma Assert
(Kind
= N_Character_Literal
);
4261 -- Since Character literals of type Standard.Character don't
4262 -- have any defining character literals built for them, they
4263 -- do not have their Entity set, so just use their Char
4264 -- code. Otherwise for user-defined character literals use
4265 -- their Pos value as usual.
4268 Val
:= Char_Literal_Value
(N
);
4270 Val
:= Enumeration_Pos
(Ent
);
4274 -- Come here with Val set to value to be returned, set cache
4285 function Expr_Value_E
(N
: Node_Id
) return Entity_Id
is
4286 Ent
: constant Entity_Id
:= Entity
(N
);
4288 if Ekind
(Ent
) = E_Enumeration_Literal
then
4291 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4292 return Expr_Value_E
(Constant_Value
(Ent
));
4300 function Expr_Value_R
(N
: Node_Id
) return Ureal
is
4301 Kind
: constant Node_Kind
:= Nkind
(N
);
4305 if Kind
= N_Real_Literal
then
4308 elsif Kind
= N_Identifier
or else Kind
= N_Expanded_Name
then
4310 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4311 return Expr_Value_R
(Constant_Value
(Ent
));
4313 elsif Kind
= N_Integer_Literal
then
4314 return UR_From_Uint
(Expr_Value
(N
));
4316 -- Here, we have a node that cannot be interpreted as a compile time
4317 -- constant. That is definitely an error.
4320 raise Program_Error
;
4328 function Expr_Value_S
(N
: Node_Id
) return Node_Id
is
4330 if Nkind
(N
) = N_String_Literal
then
4333 pragma Assert
(Ekind
(Entity
(N
)) = E_Constant
);
4334 return Expr_Value_S
(Constant_Value
(Entity
(N
)));
4338 ----------------------------------
4339 -- Find_Universal_Operator_Type --
4340 ----------------------------------
4342 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
is
4343 PN
: constant Node_Id
:= Parent
(N
);
4344 Call
: constant Node_Id
:= Original_Node
(N
);
4345 Is_Int
: constant Boolean := Is_Integer_Type
(Etype
(N
));
4347 Is_Fix
: constant Boolean :=
4348 Nkind
(N
) in N_Binary_Op
4349 and then Nkind
(Right_Opnd
(N
)) /= Nkind
(Left_Opnd
(N
));
4350 -- A mixed-mode operation in this context indicates the presence of
4351 -- fixed-point type in the designated package.
4353 Is_Relational
: constant Boolean := Etype
(N
) = Standard_Boolean
;
4354 -- Case where N is a relational (or membership) operator (else it is an
4357 In_Membership
: constant Boolean :=
4358 Nkind
(PN
) in N_Membership_Test
4360 Nkind
(Right_Opnd
(PN
)) = N_Range
4362 Is_Universal_Numeric_Type
(Etype
(Left_Opnd
(PN
)))
4364 Is_Universal_Numeric_Type
4365 (Etype
(Low_Bound
(Right_Opnd
(PN
))))
4367 Is_Universal_Numeric_Type
4368 (Etype
(High_Bound
(Right_Opnd
(PN
))));
4369 -- Case where N is part of a membership test with a universal range
4373 Typ1
: Entity_Id
:= Empty
;
4376 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean;
4377 -- Check whether one operand is a mixed-mode operation that requires the
4378 -- presence of a fixed-point type. Given that all operands are universal
4379 -- and have been constant-folded, retrieve the original function call.
4381 ---------------------------
4382 -- Is_Mixed_Mode_Operand --
4383 ---------------------------
4385 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean is
4386 Onod
: constant Node_Id
:= Original_Node
(Op
);
4388 return Nkind
(Onod
) = N_Function_Call
4389 and then Present
(Next_Actual
(First_Actual
(Onod
)))
4390 and then Etype
(First_Actual
(Onod
)) /=
4391 Etype
(Next_Actual
(First_Actual
(Onod
)));
4392 end Is_Mixed_Mode_Operand
;
4394 -- Start of processing for Find_Universal_Operator_Type
4397 if Nkind
(Call
) /= N_Function_Call
4398 or else Nkind
(Name
(Call
)) /= N_Expanded_Name
4402 -- There are several cases where the context does not imply the type of
4404 -- - the universal expression appears in a type conversion;
4405 -- - the expression is a relational operator applied to universal
4407 -- - the expression is a membership test with a universal operand
4408 -- and a range with universal bounds.
4410 elsif Nkind
(Parent
(N
)) = N_Type_Conversion
4411 or else Is_Relational
4412 or else In_Membership
4414 Pack
:= Entity
(Prefix
(Name
(Call
)));
4416 -- If the prefix is a package declared elsewhere, iterate over its
4417 -- visible entities, otherwise iterate over all declarations in the
4418 -- designated scope.
4420 if Ekind
(Pack
) = E_Package
4421 and then not In_Open_Scopes
(Pack
)
4423 Priv_E
:= First_Private_Entity
(Pack
);
4429 E
:= First_Entity
(Pack
);
4430 while Present
(E
) and then E
/= Priv_E
loop
4431 if Is_Numeric_Type
(E
)
4432 and then Nkind
(Parent
(E
)) /= N_Subtype_Declaration
4433 and then Comes_From_Source
(E
)
4434 and then Is_Integer_Type
(E
) = Is_Int
4435 and then (Nkind
(N
) in N_Unary_Op
4436 or else Is_Relational
4437 or else Is_Fixed_Point_Type
(E
) = Is_Fix
)
4442 -- Before emitting an error, check for the presence of a
4443 -- mixed-mode operation that specifies a fixed point type.
4447 (Is_Mixed_Mode_Operand
(Left_Opnd
(N
))
4448 or else Is_Mixed_Mode_Operand
(Right_Opnd
(N
)))
4449 and then Is_Fixed_Point_Type
(E
) /= Is_Fixed_Point_Type
(Typ1
)
4452 if Is_Fixed_Point_Type
(E
) then
4457 -- More than one type of the proper class declared in P
4459 Error_Msg_N
("ambiguous operation", N
);
4460 Error_Msg_Sloc
:= Sloc
(Typ1
);
4461 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4462 Error_Msg_Sloc
:= Sloc
(E
);
4463 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4473 end Find_Universal_Operator_Type
;
4475 --------------------------
4476 -- Flag_Non_Static_Expr --
4477 --------------------------
4479 procedure Flag_Non_Static_Expr
(Msg
: String; Expr
: Node_Id
) is
4481 if Error_Posted
(Expr
) and then not All_Errors_Mode
then
4484 Error_Msg_F
(Msg
, Expr
);
4485 Why_Not_Static
(Expr
);
4487 end Flag_Non_Static_Expr
;
4493 procedure Fold_Str
(N
: Node_Id
; Val
: String_Id
; Static
: Boolean) is
4494 Loc
: constant Source_Ptr
:= Sloc
(N
);
4495 Typ
: constant Entity_Id
:= Etype
(N
);
4498 if Raises_Constraint_Error
(N
) then
4499 Set_Is_Static_Expression
(N
, Static
);
4503 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Val
));
4505 -- We now have the literal with the right value, both the actual type
4506 -- and the expected type of this literal are taken from the expression
4507 -- that was evaluated. So now we do the Analyze and Resolve.
4509 -- Note that we have to reset Is_Static_Expression both after the
4510 -- analyze step (because Resolve will evaluate the literal, which
4511 -- will cause semantic errors if it is marked as static), and after
4512 -- the Resolve step (since Resolve in some cases resets this flag).
4515 Set_Is_Static_Expression
(N
, Static
);
4518 Set_Is_Static_Expression
(N
, Static
);
4525 procedure Fold_Uint
(N
: Node_Id
; Val
: Uint
; Static
: Boolean) is
4526 Loc
: constant Source_Ptr
:= Sloc
(N
);
4527 Typ
: Entity_Id
:= Etype
(N
);
4531 if Raises_Constraint_Error
(N
) then
4532 Set_Is_Static_Expression
(N
, Static
);
4536 -- If we are folding a named number, retain the entity in the literal,
4539 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Integer
then
4545 if Is_Private_Type
(Typ
) then
4546 Typ
:= Full_View
(Typ
);
4549 -- For a result of type integer, substitute an N_Integer_Literal node
4550 -- for the result of the compile time evaluation of the expression.
4551 -- For ASIS use, set a link to the original named number when not in
4552 -- a generic context.
4554 if Is_Integer_Type
(Typ
) then
4555 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
4556 Set_Original_Entity
(N
, Ent
);
4558 -- Otherwise we have an enumeration type, and we substitute either
4559 -- an N_Identifier or N_Character_Literal to represent the enumeration
4560 -- literal corresponding to the given value, which must always be in
4561 -- range, because appropriate tests have already been made for this.
4563 else pragma Assert
(Is_Enumeration_Type
(Typ
));
4564 Rewrite
(N
, Get_Enum_Lit_From_Pos
(Etype
(N
), Val
, Loc
));
4567 -- We now have the literal with the right value, both the actual type
4568 -- and the expected type of this literal are taken from the expression
4569 -- that was evaluated. So now we do the Analyze and Resolve.
4571 -- Note that we have to reset Is_Static_Expression both after the
4572 -- analyze step (because Resolve will evaluate the literal, which
4573 -- will cause semantic errors if it is marked as static), and after
4574 -- the Resolve step (since Resolve in some cases sets this flag).
4577 Set_Is_Static_Expression
(N
, Static
);
4580 Set_Is_Static_Expression
(N
, Static
);
4587 procedure Fold_Ureal
(N
: Node_Id
; Val
: Ureal
; Static
: Boolean) is
4588 Loc
: constant Source_Ptr
:= Sloc
(N
);
4589 Typ
: constant Entity_Id
:= Etype
(N
);
4593 if Raises_Constraint_Error
(N
) then
4594 Set_Is_Static_Expression
(N
, Static
);
4598 -- If we are folding a named number, retain the entity in the literal,
4601 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Real
then
4607 Rewrite
(N
, Make_Real_Literal
(Loc
, Realval
=> Val
));
4609 -- Set link to original named number, for ASIS use
4611 Set_Original_Entity
(N
, Ent
);
4613 -- We now have the literal with the right value, both the actual type
4614 -- and the expected type of this literal are taken from the expression
4615 -- that was evaluated. So now we do the Analyze and Resolve.
4617 -- Note that we have to reset Is_Static_Expression both after the
4618 -- analyze step (because Resolve will evaluate the literal, which
4619 -- will cause semantic errors if it is marked as static), and after
4620 -- the Resolve step (since Resolve in some cases sets this flag).
4623 Set_Is_Static_Expression
(N
, Static
);
4626 Set_Is_Static_Expression
(N
, Static
);
4633 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
is
4637 for J
in 0 .. B
'Last loop
4643 if Non_Binary_Modulus
(T
) then
4644 V
:= V
mod Modulus
(T
);
4650 --------------------
4651 -- Get_String_Val --
4652 --------------------
4654 function Get_String_Val
(N
: Node_Id
) return Node_Id
is
4656 if Nkind_In
(N
, N_String_Literal
, N_Character_Literal
) then
4659 pragma Assert
(Is_Entity_Name
(N
));
4660 return Get_String_Val
(Constant_Value
(Entity
(N
)));
4668 procedure Initialize
is
4670 CV_Cache
:= (others => (Node_High_Bound
, Uint_0
));
4673 --------------------
4674 -- In_Subrange_Of --
4675 --------------------
4677 function In_Subrange_Of
4680 Fixed_Int
: Boolean := False) return Boolean
4689 if T1
= T2
or else Is_Subtype_Of
(T1
, T2
) then
4692 -- Never in range if both types are not scalar. Don't know if this can
4693 -- actually happen, but just in case.
4695 elsif not Is_Scalar_Type
(T1
) or else not Is_Scalar_Type
(T2
) then
4698 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4699 -- definitely not compatible with T2.
4701 elsif Is_Floating_Point_Type
(T1
)
4702 and then Has_Infinities
(T1
)
4703 and then Is_Floating_Point_Type
(T2
)
4704 and then not Has_Infinities
(T2
)
4709 L1
:= Type_Low_Bound
(T1
);
4710 H1
:= Type_High_Bound
(T1
);
4712 L2
:= Type_Low_Bound
(T2
);
4713 H2
:= Type_High_Bound
(T2
);
4715 -- Check bounds to see if comparison possible at compile time
4717 if Compile_Time_Compare
(L1
, L2
, Assume_Valid
=> True) in Compare_GE
4719 Compile_Time_Compare
(H1
, H2
, Assume_Valid
=> True) in Compare_LE
4724 -- If bounds not comparable at compile time, then the bounds of T2
4725 -- must be compile-time-known or we cannot answer the query.
4727 if not Compile_Time_Known_Value
(L2
)
4728 or else not Compile_Time_Known_Value
(H2
)
4733 -- If the bounds of T1 are know at compile time then use these
4734 -- ones, otherwise use the bounds of the base type (which are of
4735 -- course always static).
4737 if not Compile_Time_Known_Value
(L1
) then
4738 L1
:= Type_Low_Bound
(Base_Type
(T1
));
4741 if not Compile_Time_Known_Value
(H1
) then
4742 H1
:= Type_High_Bound
(Base_Type
(T1
));
4745 -- Fixed point types should be considered as such only if
4746 -- flag Fixed_Int is set to False.
4748 if Is_Floating_Point_Type
(T1
) or else Is_Floating_Point_Type
(T2
)
4749 or else (Is_Fixed_Point_Type
(T1
) and then not Fixed_Int
)
4750 or else (Is_Fixed_Point_Type
(T2
) and then not Fixed_Int
)
4753 Expr_Value_R
(L2
) <= Expr_Value_R
(L1
)
4755 Expr_Value_R
(H2
) >= Expr_Value_R
(H1
);
4759 Expr_Value
(L2
) <= Expr_Value
(L1
)
4761 Expr_Value
(H2
) >= Expr_Value
(H1
);
4766 -- If any exception occurs, it means that we have some bug in the compiler
4767 -- possibly triggered by a previous error, or by some unforeseen peculiar
4768 -- occurrence. However, this is only an optimization attempt, so there is
4769 -- really no point in crashing the compiler. Instead we just decide, too
4770 -- bad, we can't figure out the answer in this case after all.
4775 -- Debug flag K disables this behavior (useful for debugging)
4777 if Debug_Flag_K
then
4788 function Is_In_Range
4791 Assume_Valid
: Boolean := False;
4792 Fixed_Int
: Boolean := False;
4793 Int_Real
: Boolean := False) return Boolean
4797 Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) = In_Range
;
4804 function Is_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
4806 if Compile_Time_Known_Value
(Lo
)
4807 and then Compile_Time_Known_Value
(Hi
)
4810 Typ
: Entity_Id
:= Etype
(Lo
);
4812 -- When called from the frontend, as part of the analysis of
4813 -- potentially static expressions, Typ will be the full view of a
4814 -- type with all the info needed to answer this query. When called
4815 -- from the backend, for example to know whether a range of a loop
4816 -- is null, Typ might be a private type and we need to explicitly
4817 -- switch to its corresponding full view to access the same info.
4819 if Is_Incomplete_Or_Private_Type
(Typ
)
4820 and then Present
(Full_View
(Typ
))
4822 Typ
:= Full_View
(Typ
);
4825 if Is_Discrete_Type
(Typ
) then
4826 return Expr_Value
(Lo
) > Expr_Value
(Hi
);
4827 else pragma Assert
(Is_Real_Type
(Typ
));
4828 return Expr_Value_R
(Lo
) > Expr_Value_R
(Hi
);
4836 -------------------------
4837 -- Is_OK_Static_Choice --
4838 -------------------------
4840 function Is_OK_Static_Choice
(Choice
: Node_Id
) return Boolean is
4842 -- Check various possibilities for choice
4844 -- Note: for membership tests, we test more cases than are possible
4845 -- (in particular subtype indication), but it doesn't matter because
4846 -- it just won't occur (we have already done a syntax check).
4848 if Nkind
(Choice
) = N_Others_Choice
then
4851 elsif Nkind
(Choice
) = N_Range
then
4852 return Is_OK_Static_Range
(Choice
);
4854 elsif Nkind
(Choice
) = N_Subtype_Indication
4855 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
4857 return Is_OK_Static_Subtype
(Etype
(Choice
));
4860 return Is_OK_Static_Expression
(Choice
);
4862 end Is_OK_Static_Choice
;
4864 ------------------------------
4865 -- Is_OK_Static_Choice_List --
4866 ------------------------------
4868 function Is_OK_Static_Choice_List
(Choices
: List_Id
) return Boolean is
4872 if not Is_Static_Choice_List
(Choices
) then
4876 Choice
:= First
(Choices
);
4877 while Present
(Choice
) loop
4878 if not Is_OK_Static_Choice
(Choice
) then
4879 Set_Raises_Constraint_Error
(Choice
);
4887 end Is_OK_Static_Choice_List
;
4889 -----------------------------
4890 -- Is_OK_Static_Expression --
4891 -----------------------------
4893 function Is_OK_Static_Expression
(N
: Node_Id
) return Boolean is
4895 return Is_Static_Expression
(N
) and then not Raises_Constraint_Error
(N
);
4896 end Is_OK_Static_Expression
;
4898 ------------------------
4899 -- Is_OK_Static_Range --
4900 ------------------------
4902 -- A static range is a range whose bounds are static expressions, or a
4903 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4904 -- We have already converted range attribute references, so we get the
4905 -- "or" part of this rule without needing a special test.
4907 function Is_OK_Static_Range
(N
: Node_Id
) return Boolean is
4909 return Is_OK_Static_Expression
(Low_Bound
(N
))
4910 and then Is_OK_Static_Expression
(High_Bound
(N
));
4911 end Is_OK_Static_Range
;
4913 --------------------------
4914 -- Is_OK_Static_Subtype --
4915 --------------------------
4917 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4918 -- neither bound raises constraint error when evaluated.
4920 function Is_OK_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4921 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4922 Anc_Subt
: Entity_Id
;
4925 -- First a quick check on the non static subtype flag. As described
4926 -- in further detail in Einfo, this flag is not decisive in all cases,
4927 -- but if it is set, then the subtype is definitely non-static.
4929 if Is_Non_Static_Subtype
(Typ
) then
4933 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4935 if Anc_Subt
= Empty
then
4939 if Is_Generic_Type
(Root_Type
(Base_T
))
4940 or else Is_Generic_Actual_Type
(Base_T
)
4944 elsif Has_Dynamic_Predicate_Aspect
(Typ
) then
4949 elsif Is_String_Type
(Typ
) then
4951 Ekind
(Typ
) = E_String_Literal_Subtype
4953 (Is_OK_Static_Subtype
(Component_Type
(Typ
))
4954 and then Is_OK_Static_Subtype
(Etype
(First_Index
(Typ
))));
4958 elsif Is_Scalar_Type
(Typ
) then
4959 if Base_T
= Typ
then
4963 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4964 -- Get_Type_{Low,High}_Bound.
4966 return Is_OK_Static_Subtype
(Anc_Subt
)
4967 and then Is_OK_Static_Expression
(Type_Low_Bound
(Typ
))
4968 and then Is_OK_Static_Expression
(Type_High_Bound
(Typ
));
4971 -- Types other than string and scalar types are never static
4976 end Is_OK_Static_Subtype
;
4978 ---------------------
4979 -- Is_Out_Of_Range --
4980 ---------------------
4982 function Is_Out_Of_Range
4985 Assume_Valid
: Boolean := False;
4986 Fixed_Int
: Boolean := False;
4987 Int_Real
: Boolean := False) return Boolean
4990 return Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) =
4992 end Is_Out_Of_Range
;
4994 ----------------------
4995 -- Is_Static_Choice --
4996 ----------------------
4998 function Is_Static_Choice
(Choice
: Node_Id
) return Boolean is
5000 -- Check various possibilities for choice
5002 -- Note: for membership tests, we test more cases than are possible
5003 -- (in particular subtype indication), but it doesn't matter because
5004 -- it just won't occur (we have already done a syntax check).
5006 if Nkind
(Choice
) = N_Others_Choice
then
5009 elsif Nkind
(Choice
) = N_Range
then
5010 return Is_Static_Range
(Choice
);
5012 elsif Nkind
(Choice
) = N_Subtype_Indication
5013 or else (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
5015 return Is_Static_Subtype
(Etype
(Choice
));
5018 return Is_Static_Expression
(Choice
);
5020 end Is_Static_Choice
;
5022 ---------------------------
5023 -- Is_Static_Choice_List --
5024 ---------------------------
5026 function Is_Static_Choice_List
(Choices
: List_Id
) return Boolean is
5030 Choice
:= First
(Choices
);
5031 while Present
(Choice
) loop
5032 if not Is_Static_Choice
(Choice
) then
5040 end Is_Static_Choice_List
;
5042 ---------------------
5043 -- Is_Static_Range --
5044 ---------------------
5046 -- A static range is a range whose bounds are static expressions, or a
5047 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
5048 -- We have already converted range attribute references, so we get the
5049 -- "or" part of this rule without needing a special test.
5051 function Is_Static_Range
(N
: Node_Id
) return Boolean is
5053 return Is_Static_Expression
(Low_Bound
(N
))
5055 Is_Static_Expression
(High_Bound
(N
));
5056 end Is_Static_Range
;
5058 -----------------------
5059 -- Is_Static_Subtype --
5060 -----------------------
5062 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
5064 function Is_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
5065 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
5066 Anc_Subt
: Entity_Id
;
5069 -- First a quick check on the non static subtype flag. As described
5070 -- in further detail in Einfo, this flag is not decisive in all cases,
5071 -- but if it is set, then the subtype is definitely non-static.
5073 if Is_Non_Static_Subtype
(Typ
) then
5077 Anc_Subt
:= Ancestor_Subtype
(Typ
);
5079 if Anc_Subt
= Empty
then
5083 if Is_Generic_Type
(Root_Type
(Base_T
))
5084 or else Is_Generic_Actual_Type
(Base_T
)
5088 -- If there is a dynamic predicate for the type (declared or inherited)
5089 -- the expression is not static.
5091 elsif Has_Dynamic_Predicate_Aspect
(Typ
)
5092 or else (Is_Derived_Type
(Typ
)
5093 and then Has_Aspect
(Typ
, Aspect_Dynamic_Predicate
))
5099 elsif Is_String_Type
(Typ
) then
5101 Ekind
(Typ
) = E_String_Literal_Subtype
5102 or else (Is_Static_Subtype
(Component_Type
(Typ
))
5103 and then Is_Static_Subtype
(Etype
(First_Index
(Typ
))));
5107 elsif Is_Scalar_Type
(Typ
) then
5108 if Base_T
= Typ
then
5112 return Is_Static_Subtype
(Anc_Subt
)
5113 and then Is_Static_Expression
(Type_Low_Bound
(Typ
))
5114 and then Is_Static_Expression
(Type_High_Bound
(Typ
));
5117 -- Types other than string and scalar types are never static
5122 end Is_Static_Subtype
;
5124 -------------------------------
5125 -- Is_Statically_Unevaluated --
5126 -------------------------------
5128 function Is_Statically_Unevaluated
(Expr
: Node_Id
) return Boolean is
5129 function Check_Case_Expr_Alternative
5130 (CEA
: Node_Id
) return Match_Result
;
5131 -- We have a message emanating from the Expression of a case expression
5132 -- alternative. We examine this alternative, as follows:
5134 -- If the selecting expression of the parent case is non-static, or
5135 -- if any of the discrete choices of the given case alternative are
5136 -- non-static or raise Constraint_Error, return Non_Static.
5138 -- Otherwise check if the selecting expression matches any of the given
5139 -- discrete choices. If so, the alternative is executed and we return
5140 -- Match, otherwise, the alternative can never be executed, and so we
5143 ---------------------------------
5144 -- Check_Case_Expr_Alternative --
5145 ---------------------------------
5147 function Check_Case_Expr_Alternative
5148 (CEA
: Node_Id
) return Match_Result
5150 Case_Exp
: constant Node_Id
:= Parent
(CEA
);
5155 pragma Assert
(Nkind
(Case_Exp
) = N_Case_Expression
);
5157 -- Check that selecting expression is static
5159 if not Is_OK_Static_Expression
(Expression
(Case_Exp
)) then
5163 if not Is_OK_Static_Choice_List
(Discrete_Choices
(CEA
)) then
5167 -- All choices are now known to be static. Now see if alternative
5168 -- matches one of the choices.
5170 Choice
:= First
(Discrete_Choices
(CEA
));
5171 while Present
(Choice
) loop
5173 -- Check various possibilities for choice, returning Match if we
5174 -- find the selecting value matches any of the choices. Note that
5175 -- we know we are the last choice, so we don't have to keep going.
5177 if Nkind
(Choice
) = N_Others_Choice
then
5179 -- Others choice is a bit annoying, it matches if none of the
5180 -- previous alternatives matches (note that we know we are the
5181 -- last alternative in this case, so we can just go backwards
5182 -- from us to see if any previous one matches).
5184 Prev_CEA
:= Prev
(CEA
);
5185 while Present
(Prev_CEA
) loop
5186 if Check_Case_Expr_Alternative
(Prev_CEA
) = Match
then
5195 -- Else we have a normal static choice
5197 elsif Choice_Matches
(Expression
(Case_Exp
), Choice
) = Match
then
5201 -- If we fall through, it means that the discrete choice did not
5202 -- match the selecting expression, so continue.
5207 -- If we get through that loop then all choices were static, and none
5208 -- of them matched the selecting expression. So return No_Match.
5211 end Check_Case_Expr_Alternative
;
5219 -- Start of processing for Is_Statically_Unevaluated
5222 -- The (32.x) references here are from RM section 4.9
5224 -- (32.1) An expression is statically unevaluated if it is part of ...
5226 -- This means we have to climb the tree looking for one of the cases
5233 -- (32.2) The right operand of a static short-circuit control form
5234 -- whose value is determined by its left operand.
5236 -- AND THEN with False as left operand
5238 if Nkind
(P
) = N_And_Then
5239 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5240 and then Is_False
(Expr_Value
(Left_Opnd
(P
)))
5244 -- OR ELSE with True as left operand
5246 elsif Nkind
(P
) = N_Or_Else
5247 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5248 and then Is_True
(Expr_Value
(Left_Opnd
(P
)))
5252 -- (32.3) A dependent_expression of an if_expression whose associated
5253 -- condition is static and equals False.
5255 elsif Nkind
(P
) = N_If_Expression
then
5257 Cond
: constant Node_Id
:= First
(Expressions
(P
));
5258 Texp
: constant Node_Id
:= Next
(Cond
);
5259 Fexp
: constant Node_Id
:= Next
(Texp
);
5262 if Compile_Time_Known_Value
(Cond
) then
5264 -- Condition is True and we are in the right operand
5266 if Is_True
(Expr_Value
(Cond
)) and then OldP
= Fexp
then
5269 -- Condition is False and we are in the left operand
5271 elsif Is_False
(Expr_Value
(Cond
)) and then OldP
= Texp
then
5277 -- (32.4) A condition or dependent_expression of an if_expression
5278 -- where the condition corresponding to at least one preceding
5279 -- dependent_expression of the if_expression is static and equals
5282 -- This refers to cases like
5284 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5286 -- But we expand elsif's out anyway, so the above looks like:
5288 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5290 -- So for us this is caught by the above check for the 32.3 case.
5292 -- (32.5) A dependent_expression of a case_expression whose
5293 -- selecting_expression is static and whose value is not covered
5294 -- by the corresponding discrete_choice_list.
5296 elsif Nkind
(P
) = N_Case_Expression_Alternative
then
5298 -- First, we have to be in the expression to suppress messages.
5299 -- If we are within one of the choices, we want the message.
5301 if OldP
= Expression
(P
) then
5303 -- Statically unevaluated if alternative does not match
5305 if Check_Case_Expr_Alternative
(P
) = No_Match
then
5310 -- (32.6) A choice_expression (or a simple_expression of a range
5311 -- that occurs as a membership_choice of a membership_choice_list)
5312 -- of a static membership test that is preceded in the enclosing
5313 -- membership_choice_list by another item whose individual
5314 -- membership test (see (RM 4.5.2)) statically yields True.
5316 elsif Nkind
(P
) in N_Membership_Test
then
5318 -- Only possibly unevaluated if simple expression is static
5320 if not Is_OK_Static_Expression
(Left_Opnd
(P
)) then
5323 -- All members of the choice list must be static
5325 elsif (Present
(Right_Opnd
(P
))
5326 and then not Is_OK_Static_Choice
(Right_Opnd
(P
)))
5327 or else (Present
(Alternatives
(P
))
5329 not Is_OK_Static_Choice_List
(Alternatives
(P
)))
5333 -- If expression is the one and only alternative, then it is
5334 -- definitely not statically unevaluated, so we only have to
5335 -- test the case where there are alternatives present.
5337 elsif Present
(Alternatives
(P
)) then
5339 -- Look for previous matching Choice
5341 Choice
:= First
(Alternatives
(P
));
5342 while Present
(Choice
) loop
5344 -- If we reached us and no previous choices matched, this
5345 -- is not the case where we are statically unevaluated.
5347 exit when OldP
= Choice
;
5349 -- If a previous choice matches, then that is the case where
5350 -- we know our choice is statically unevaluated.
5352 if Choice_Matches
(Left_Opnd
(P
), Choice
) = Match
then
5359 -- If we fall through the loop, we were not one of the choices,
5360 -- we must have been the expression, so that is not covered by
5361 -- this rule, and we keep going.
5367 -- OK, not statically unevaluated at this level, see if we should
5368 -- keep climbing to look for a higher level reason.
5370 -- Special case for component association in aggregates, where
5371 -- we want to keep climbing up to the parent aggregate.
5373 if Nkind
(P
) = N_Component_Association
5374 and then Nkind
(Parent
(P
)) = N_Aggregate
5378 -- All done if not still within subexpression
5381 exit when Nkind
(P
) not in N_Subexpr
;
5385 -- If we fall through the loop, not one of the cases covered!
5388 end Is_Statically_Unevaluated
;
5390 --------------------
5391 -- Not_Null_Range --
5392 --------------------
5394 function Not_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
5396 if Compile_Time_Known_Value
(Lo
)
5397 and then Compile_Time_Known_Value
(Hi
)
5400 Typ
: Entity_Id
:= Etype
(Lo
);
5402 -- When called from the frontend, as part of the analysis of
5403 -- potentially static expressions, Typ will be the full view of a
5404 -- type with all the info needed to answer this query. When called
5405 -- from the backend, for example to know whether a range of a loop
5406 -- is null, Typ might be a private type and we need to explicitly
5407 -- switch to its corresponding full view to access the same info.
5409 if Is_Incomplete_Or_Private_Type
(Typ
)
5410 and then Present
(Full_View
(Typ
))
5412 Typ
:= Full_View
(Typ
);
5415 if Is_Discrete_Type
(Typ
) then
5416 return Expr_Value
(Lo
) <= Expr_Value
(Hi
);
5417 else pragma Assert
(Is_Real_Type
(Typ
));
5418 return Expr_Value_R
(Lo
) <= Expr_Value_R
(Hi
);
5431 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean is
5433 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5435 if Bits
< 500_000
then
5438 -- Error if this maximum is exceeded
5441 Error_Msg_N
("static value too large, capacity exceeded", N
);
5450 procedure Out_Of_Range
(N
: Node_Id
) is
5452 -- If we have the static expression case, then this is an illegality
5453 -- in Ada 95 mode, except that in an instance, we never generate an
5454 -- error (if the error is legitimate, it was already diagnosed in the
5457 if Is_Static_Expression
(N
)
5458 and then not In_Instance
5459 and then not In_Inlined_Body
5460 and then Ada_Version
>= Ada_95
5462 -- No message if we are statically unevaluated
5464 if Is_Statically_Unevaluated
(N
) then
5467 -- The expression to compute the length of a packed array is attached
5468 -- to the array type itself, and deserves a separate message.
5470 elsif Nkind
(Parent
(N
)) = N_Defining_Identifier
5471 and then Is_Array_Type
(Parent
(N
))
5472 and then Present
(Packed_Array_Impl_Type
(Parent
(N
)))
5473 and then Present
(First_Rep_Item
(Parent
(N
)))
5476 ("length of packed array must not exceed Integer''Last",
5477 First_Rep_Item
(Parent
(N
)));
5478 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Uint_1
));
5480 -- All cases except the special array case.
5481 -- No message if we are dealing with System.Priority values in
5482 -- CodePeer mode where the target runtime may have more priorities.
5484 elsif not CodePeer_Mode
or else Etype
(N
) /= RTE
(RE_Priority
) then
5485 Apply_Compile_Time_Constraint_Error
5486 (N
, "value not in range of}", CE_Range_Check_Failed
);
5489 -- Here we generate a warning for the Ada 83 case, or when we are in an
5490 -- instance, or when we have a non-static expression case.
5493 Apply_Compile_Time_Constraint_Error
5494 (N
, "value not in range of}??", CE_Range_Check_Failed
);
5498 ----------------------
5499 -- Predicates_Match --
5500 ----------------------
5502 function Predicates_Match
(T1
, T2
: Entity_Id
) return Boolean is
5507 if Ada_Version
< Ada_2012
then
5510 -- Both types must have predicates or lack them
5512 elsif Has_Predicates
(T1
) /= Has_Predicates
(T2
) then
5515 -- Check matching predicates
5520 (T1
, Name_Static_Predicate
, Check_Parents
=> False);
5523 (T2
, Name_Static_Predicate
, Check_Parents
=> False);
5525 -- Subtypes statically match if the predicate comes from the
5526 -- same declaration, which can only happen if one is a subtype
5527 -- of the other and has no explicit predicate.
5529 -- Suppress warnings on order of actuals, which is otherwise
5530 -- triggered by one of the two calls below.
5532 pragma Warnings
(Off
);
5533 return Pred1
= Pred2
5534 or else (No
(Pred1
) and then Is_Subtype_Of
(T1
, T2
))
5535 or else (No
(Pred2
) and then Is_Subtype_Of
(T2
, T1
));
5536 pragma Warnings
(On
);
5538 end Predicates_Match
;
5540 ---------------------------------------------
5541 -- Real_Or_String_Static_Predicate_Matches --
5542 ---------------------------------------------
5544 function Real_Or_String_Static_Predicate_Matches
5546 Typ
: Entity_Id
) return Boolean
5548 Expr
: constant Node_Id
:= Static_Real_Or_String_Predicate
(Typ
);
5549 -- The predicate expression from the type
5551 Pfun
: constant Entity_Id
:= Predicate_Function
(Typ
);
5552 -- The entity for the predicate function
5554 Ent_Name
: constant Name_Id
:= Chars
(First_Formal
(Pfun
));
5555 -- The name of the formal of the predicate function. Occurrences of the
5556 -- type name in Expr have been rewritten as references to this formal,
5557 -- and it has a unique name, so we can identify references by this name.
5560 -- Copy of the predicate function tree
5562 function Process
(N
: Node_Id
) return Traverse_Result
;
5563 -- Function used to process nodes during the traversal in which we will
5564 -- find occurrences of the entity name, and replace such occurrences
5565 -- by a real literal with the value to be tested.
5567 procedure Traverse
is new Traverse_Proc
(Process
);
5568 -- The actual traversal procedure
5574 function Process
(N
: Node_Id
) return Traverse_Result
is
5576 if Nkind
(N
) = N_Identifier
and then Chars
(N
) = Ent_Name
then
5578 Nod
: constant Node_Id
:= New_Copy
(Val
);
5580 Set_Sloc
(Nod
, Sloc
(N
));
5585 -- The predicate function may contain string-comparison operations
5586 -- that have been converted into calls to run-time array-comparison
5587 -- routines. To evaluate the predicate statically, we recover the
5588 -- original comparison operation and replace the occurrence of the
5589 -- formal by the static string value. The actuals of the generated
5590 -- call are of the form X'Address.
5592 elsif Nkind
(N
) in N_Op_Compare
5593 and then Nkind
(Left_Opnd
(N
)) = N_Function_Call
5596 C
: constant Node_Id
:= Left_Opnd
(N
);
5597 F
: constant Node_Id
:= First
(Parameter_Associations
(C
));
5598 L
: constant Node_Id
:= Prefix
(F
);
5599 R
: constant Node_Id
:= Prefix
(Next
(F
));
5602 -- If an operand is an entity name, it is the formal of the
5603 -- predicate function, so replace it with the string value.
5604 -- It may be either operand in the call. The other operand
5605 -- is a static string from the original predicate.
5607 if Is_Entity_Name
(L
) then
5608 Rewrite
(Left_Opnd
(N
), New_Copy
(Val
));
5609 Rewrite
(Right_Opnd
(N
), New_Copy
(R
));
5612 Rewrite
(Left_Opnd
(N
), New_Copy
(L
));
5613 Rewrite
(Right_Opnd
(N
), New_Copy
(Val
));
5624 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5627 -- First deal with special case of inherited predicate, where the
5628 -- predicate expression looks like:
5630 -- xxPredicate (typ (Ent)) and then Expr
5632 -- where Expr is the predicate expression for this level, and the
5633 -- left operand is the call to evaluate the inherited predicate.
5635 if Nkind
(Expr
) = N_And_Then
5636 and then Nkind
(Left_Opnd
(Expr
)) = N_Function_Call
5637 and then Is_Predicate_Function
(Entity
(Name
(Left_Opnd
(Expr
))))
5639 -- OK we have the inherited case, so make a call to evaluate the
5640 -- inherited predicate. If that fails, so do we!
5643 Real_Or_String_Static_Predicate_Matches
5645 Typ
=> Etype
(First_Formal
(Entity
(Name
(Left_Opnd
(Expr
))))))
5650 -- Use the right operand for the continued processing
5652 Copy
:= Copy_Separate_Tree
(Right_Opnd
(Expr
));
5654 -- Case where call to predicate function appears on its own (this means
5655 -- that the predicate at this level is just inherited from the parent).
5657 elsif Nkind
(Expr
) = N_Function_Call
then
5659 Typ
: constant Entity_Id
:=
5660 Etype
(First_Formal
(Entity
(Name
(Expr
))));
5663 -- If the inherited predicate is dynamic, just ignore it. We can't
5664 -- go trying to evaluate a dynamic predicate as a static one!
5666 if Has_Dynamic_Predicate_Aspect
(Typ
) then
5669 -- Otherwise inherited predicate is static, check for match
5672 return Real_Or_String_Static_Predicate_Matches
(Val
, Typ
);
5676 -- If not just an inherited predicate, copy whole expression
5679 Copy
:= Copy_Separate_Tree
(Expr
);
5682 -- Now we replace occurrences of the entity by the value
5686 -- And analyze the resulting static expression to see if it is True
5688 Analyze_And_Resolve
(Copy
, Standard_Boolean
);
5689 return Is_True
(Expr_Value
(Copy
));
5690 end Real_Or_String_Static_Predicate_Matches
;
5692 -------------------------
5693 -- Rewrite_In_Raise_CE --
5694 -------------------------
5696 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
) is
5697 Stat
: constant Boolean := Is_Static_Expression
(N
);
5698 Typ
: constant Entity_Id
:= Etype
(N
);
5701 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5702 -- can just clear the condition if the reason is appropriate. We do
5703 -- not do this operation if the parent has a reason other than range
5704 -- check failed, because otherwise we would change the reason.
5706 if Present
(Parent
(N
))
5707 and then Nkind
(Parent
(N
)) = N_Raise_Constraint_Error
5708 and then Reason
(Parent
(N
)) =
5709 UI_From_Int
(RT_Exception_Code
'Pos (CE_Range_Check_Failed
))
5711 Set_Condition
(Parent
(N
), Empty
);
5713 -- Else build an explicit N_Raise_CE
5716 if Nkind
(Exp
) = N_Raise_Constraint_Error
then
5718 Make_Raise_Constraint_Error
(Sloc
(Exp
),
5719 Reason
=> Reason
(Exp
)));
5722 Make_Raise_Constraint_Error
(Sloc
(Exp
),
5723 Reason
=> CE_Range_Check_Failed
));
5726 Set_Raises_Constraint_Error
(N
);
5730 -- Set proper flags in result
5732 Set_Raises_Constraint_Error
(N
, True);
5733 Set_Is_Static_Expression
(N
, Stat
);
5734 end Rewrite_In_Raise_CE
;
5736 ---------------------
5737 -- String_Type_Len --
5738 ---------------------
5740 function String_Type_Len
(Stype
: Entity_Id
) return Uint
is
5741 NT
: constant Entity_Id
:= Etype
(First_Index
(Stype
));
5745 if Is_OK_Static_Subtype
(NT
) then
5748 T
:= Base_Type
(NT
);
5751 return Expr_Value
(Type_High_Bound
(T
)) -
5752 Expr_Value
(Type_Low_Bound
(T
)) + 1;
5753 end String_Type_Len
;
5755 ------------------------------------
5756 -- Subtypes_Statically_Compatible --
5757 ------------------------------------
5759 function Subtypes_Statically_Compatible
5762 Formal_Derived_Matching
: Boolean := False) return Boolean
5767 if Is_Scalar_Type
(T1
) then
5769 -- Definitely compatible if we match
5771 if Subtypes_Statically_Match
(T1
, T2
) then
5774 -- If either subtype is nonstatic then they're not compatible
5776 elsif not Is_OK_Static_Subtype
(T1
)
5778 not Is_OK_Static_Subtype
(T2
)
5782 -- Base types must match, but we don't check that (should we???) but
5783 -- we do at least check that both types are real, or both types are
5786 elsif Is_Real_Type
(T1
) /= Is_Real_Type
(T2
) then
5789 -- Here we check the bounds
5793 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5794 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5795 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5796 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5799 if Is_Real_Type
(T1
) then
5801 Expr_Value_R
(LB1
) > Expr_Value_R
(HB1
)
5803 (Expr_Value_R
(LB2
) <= Expr_Value_R
(LB1
)
5804 and then Expr_Value_R
(HB1
) <= Expr_Value_R
(HB2
));
5808 Expr_Value
(LB1
) > Expr_Value
(HB1
)
5810 (Expr_Value
(LB2
) <= Expr_Value
(LB1
)
5811 and then Expr_Value
(HB1
) <= Expr_Value
(HB2
));
5818 elsif Is_Access_Type
(T1
) then
5820 (not Is_Constrained
(T2
)
5821 or else Subtypes_Statically_Match
5822 (Designated_Type
(T1
), Designated_Type
(T2
)))
5823 and then not (Can_Never_Be_Null
(T2
)
5824 and then not Can_Never_Be_Null
(T1
));
5830 (Is_Composite_Type
(T1
) and then not Is_Constrained
(T2
))
5831 or else Subtypes_Statically_Match
5832 (T1
, T2
, Formal_Derived_Matching
);
5834 end Subtypes_Statically_Compatible
;
5836 -------------------------------
5837 -- Subtypes_Statically_Match --
5838 -------------------------------
5840 -- Subtypes statically match if they have statically matching constraints
5841 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5842 -- they are the same identical constraint, or if they are static and the
5843 -- values match (RM 4.9.1(1)).
5845 -- In addition, in GNAT, the object size (Esize) values of the types must
5846 -- match if they are set (unless checking an actual for a formal derived
5847 -- type). The use of 'Object_Size can cause this to be false even if the
5848 -- types would otherwise match in the RM sense.
5850 function Subtypes_Statically_Match
5853 Formal_Derived_Matching
: Boolean := False) return Boolean
5856 -- A type always statically matches itself
5861 -- No match if sizes different (from use of 'Object_Size). This test
5862 -- is excluded if Formal_Derived_Matching is True, as the base types
5863 -- can be different in that case and typically have different sizes.
5864 -- ??? Frontend_Layout_On_Target used to set Esizes but this is no
5865 -- longer the case, consider removing the last test below.
5867 elsif not Formal_Derived_Matching
5868 and then Known_Static_Esize
(T1
)
5869 and then Known_Static_Esize
(T2
)
5870 and then Esize
(T1
) /= Esize
(T2
)
5874 -- No match if predicates do not match
5876 elsif not Predicates_Match
(T1
, T2
) then
5881 elsif Is_Scalar_Type
(T1
) then
5883 -- Base types must be the same
5885 if Base_Type
(T1
) /= Base_Type
(T2
) then
5889 -- A constrained numeric subtype never matches an unconstrained
5890 -- subtype, i.e. both types must be constrained or unconstrained.
5892 -- To understand the requirement for this test, see RM 4.9.1(1).
5893 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5894 -- a constrained subtype with constraint bounds matching the bounds
5895 -- of its corresponding unconstrained base type. In this situation,
5896 -- Integer and Integer'Base do not statically match, even though
5897 -- they have the same bounds.
5899 -- We only apply this test to types in Standard and types that appear
5900 -- in user programs. That way, we do not have to be too careful about
5901 -- setting Is_Constrained right for Itypes.
5903 if Is_Numeric_Type
(T1
)
5904 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5905 and then (Scope
(T1
) = Standard_Standard
5906 or else Comes_From_Source
(T1
))
5907 and then (Scope
(T2
) = Standard_Standard
5908 or else Comes_From_Source
(T2
))
5912 -- A generic scalar type does not statically match its base type
5913 -- (AI-311). In this case we make sure that the formals, which are
5914 -- first subtypes of their bases, are constrained.
5916 elsif Is_Generic_Type
(T1
)
5917 and then Is_Generic_Type
(T2
)
5918 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5923 -- If there was an error in either range, then just assume the types
5924 -- statically match to avoid further junk errors.
5926 if No
(Scalar_Range
(T1
)) or else No
(Scalar_Range
(T2
))
5927 or else Error_Posted
(Scalar_Range
(T1
))
5928 or else Error_Posted
(Scalar_Range
(T2
))
5933 -- Otherwise both types have bounds that can be compared
5936 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5937 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5938 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5939 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5942 -- If the bounds are the same tree node, then match (common case)
5944 if LB1
= LB2
and then HB1
= HB2
then
5947 -- Otherwise bounds must be static and identical value
5950 if not Is_OK_Static_Subtype
(T1
)
5952 not Is_OK_Static_Subtype
(T2
)
5956 elsif Is_Real_Type
(T1
) then
5958 Expr_Value_R
(LB1
) = Expr_Value_R
(LB2
)
5960 Expr_Value_R
(HB1
) = Expr_Value_R
(HB2
);
5964 Expr_Value
(LB1
) = Expr_Value
(LB2
)
5966 Expr_Value
(HB1
) = Expr_Value
(HB2
);
5971 -- Type with discriminants
5973 elsif Has_Discriminants
(T1
) or else Has_Discriminants
(T2
) then
5975 -- Because of view exchanges in multiple instantiations, conformance
5976 -- checking might try to match a partial view of a type with no
5977 -- discriminants with a full view that has defaulted discriminants.
5978 -- In such a case, use the discriminant constraint of the full view,
5979 -- which must exist because we know that the two subtypes have the
5982 if Has_Discriminants
(T1
) /= Has_Discriminants
(T2
) then
5983 -- A generic actual type is declared through a subtype declaration
5984 -- and may have an inconsistent indication of the presence of
5985 -- discriminants, so check the type it renames.
5987 if Is_Generic_Actual_Type
(T1
)
5988 and then not Has_Discriminants
(Etype
(T1
))
5989 and then not Has_Discriminants
(T2
)
5993 elsif In_Instance
then
5994 if Is_Private_Type
(T2
)
5995 and then Present
(Full_View
(T2
))
5996 and then Has_Discriminants
(Full_View
(T2
))
5998 return Subtypes_Statically_Match
(T1
, Full_View
(T2
));
6000 elsif Is_Private_Type
(T1
)
6001 and then Present
(Full_View
(T1
))
6002 and then Has_Discriminants
(Full_View
(T1
))
6004 return Subtypes_Statically_Match
(Full_View
(T1
), T2
);
6015 DL1
: constant Elist_Id
:= Discriminant_Constraint
(T1
);
6016 DL2
: constant Elist_Id
:= Discriminant_Constraint
(T2
);
6024 elsif Is_Constrained
(T1
) /= Is_Constrained
(T2
) then
6028 -- Now loop through the discriminant constraints
6030 -- Note: the guard here seems necessary, since it is possible at
6031 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
6033 if Present
(DL1
) and then Present
(DL2
) then
6034 DA1
:= First_Elmt
(DL1
);
6035 DA2
:= First_Elmt
(DL2
);
6036 while Present
(DA1
) loop
6038 Expr1
: constant Node_Id
:= Node
(DA1
);
6039 Expr2
: constant Node_Id
:= Node
(DA2
);
6042 if not Is_OK_Static_Expression
(Expr1
)
6043 or else not Is_OK_Static_Expression
(Expr2
)
6047 -- If either expression raised a constraint error,
6048 -- consider the expressions as matching, since this
6049 -- helps to prevent cascading errors.
6051 elsif Raises_Constraint_Error
(Expr1
)
6052 or else Raises_Constraint_Error
(Expr2
)
6056 elsif Expr_Value
(Expr1
) /= Expr_Value
(Expr2
) then
6069 -- A definite type does not match an indefinite or classwide type.
6070 -- However, a generic type with unknown discriminants may be
6071 -- instantiated with a type with no discriminants, and conformance
6072 -- checking on an inherited operation may compare the actual with the
6073 -- subtype that renames it in the instance.
6075 elsif Has_Unknown_Discriminants
(T1
) /= Has_Unknown_Discriminants
(T2
)
6078 Is_Generic_Actual_Type
(T1
) or else Is_Generic_Actual_Type
(T2
);
6082 elsif Is_Array_Type
(T1
) then
6084 -- If either subtype is unconstrained then both must be, and if both
6085 -- are unconstrained then no further checking is needed.
6087 if not Is_Constrained
(T1
) or else not Is_Constrained
(T2
) then
6088 return not (Is_Constrained
(T1
) or else Is_Constrained
(T2
));
6091 -- Both subtypes are constrained, so check that the index subtypes
6092 -- statically match.
6095 Index1
: Node_Id
:= First_Index
(T1
);
6096 Index2
: Node_Id
:= First_Index
(T2
);
6099 while Present
(Index1
) loop
6101 Subtypes_Statically_Match
(Etype
(Index1
), Etype
(Index2
))
6106 Next_Index
(Index1
);
6107 Next_Index
(Index2
);
6113 elsif Is_Access_Type
(T1
) then
6114 if Can_Never_Be_Null
(T1
) /= Can_Never_Be_Null
(T2
) then
6117 elsif Ekind_In
(T1
, E_Access_Subprogram_Type
,
6118 E_Anonymous_Access_Subprogram_Type
)
6122 (Designated_Type
(T1
),
6123 Designated_Type
(T2
));
6126 Subtypes_Statically_Match
6127 (Designated_Type
(T1
),
6128 Designated_Type
(T2
))
6129 and then Is_Access_Constant
(T1
) = Is_Access_Constant
(T2
);
6132 -- All other types definitely match
6137 end Subtypes_Statically_Match
;
6143 function Test
(Cond
: Boolean) return Uint
is
6152 ---------------------
6153 -- Test_Comparison --
6154 ---------------------
6156 procedure Test_Comparison
6158 Assume_Valid
: Boolean;
6159 True_Result
: out Boolean;
6160 False_Result
: out Boolean)
6162 Left
: constant Node_Id
:= Left_Opnd
(Op
);
6163 Left_Typ
: constant Entity_Id
:= Etype
(Left
);
6164 Orig_Op
: constant Node_Id
:= Original_Node
(Op
);
6166 procedure Replacement_Warning
(Msg
: String);
6167 -- Emit a warning on a comparison that can be replaced by '='
6169 -------------------------
6170 -- Replacement_Warning --
6171 -------------------------
6173 procedure Replacement_Warning
(Msg
: String) is
6175 if Constant_Condition_Warnings
6176 and then Comes_From_Source
(Orig_Op
)
6177 and then Is_Integer_Type
(Left_Typ
)
6178 and then not Error_Posted
(Op
)
6179 and then not Has_Warnings_Off
(Left_Typ
)
6180 and then not In_Instance
6182 Error_Msg_N
(Msg
, Op
);
6184 end Replacement_Warning
;
6188 Res
: constant Compare_Result
:=
6189 Compile_Time_Compare
(Left
, Right_Opnd
(Op
), Assume_Valid
);
6191 -- Start of processing for Test_Comparison
6194 case N_Op_Compare
(Nkind
(Op
)) is
6196 True_Result
:= Res
= EQ
;
6197 False_Result
:= Res
= LT
or else Res
= GT
or else Res
= NE
;
6200 True_Result
:= Res
in Compare_GE
;
6201 False_Result
:= Res
= LT
;
6203 if Res
= LE
and then Nkind
(Orig_Op
) = N_Op_Ge
then
6205 ("can never be greater than, could replace by ""'=""?c?");
6209 True_Result
:= Res
= GT
;
6210 False_Result
:= Res
in Compare_LE
;
6213 True_Result
:= Res
in Compare_LE
;
6214 False_Result
:= Res
= GT
;
6216 if Res
= GE
and then Nkind
(Orig_Op
) = N_Op_Le
then
6218 ("can never be less than, could replace by ""'=""?c?");
6222 True_Result
:= Res
= LT
;
6223 False_Result
:= Res
in Compare_GE
;
6226 True_Result
:= Res
= NE
or else Res
= GT
or else Res
= LT
;
6227 False_Result
:= Res
= EQ
;
6229 end Test_Comparison
;
6231 ---------------------------------
6232 -- Test_Expression_Is_Foldable --
6233 ---------------------------------
6237 procedure Test_Expression_Is_Foldable
6247 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
6251 -- If operand is Any_Type, just propagate to result and do not
6252 -- try to fold, this prevents cascaded errors.
6254 if Etype
(Op1
) = Any_Type
then
6255 Set_Etype
(N
, Any_Type
);
6258 -- If operand raises constraint error, then replace node N with the
6259 -- raise constraint error node, and we are obviously not foldable.
6260 -- Note that this replacement inherits the Is_Static_Expression flag
6261 -- from the operand.
6263 elsif Raises_Constraint_Error
(Op1
) then
6264 Rewrite_In_Raise_CE
(N
, Op1
);
6267 -- If the operand is not static, then the result is not static, and
6268 -- all we have to do is to check the operand since it is now known
6269 -- to appear in a non-static context.
6271 elsif not Is_Static_Expression
(Op1
) then
6272 Check_Non_Static_Context
(Op1
);
6273 Fold
:= Compile_Time_Known_Value
(Op1
);
6276 -- An expression of a formal modular type is not foldable because
6277 -- the modulus is unknown.
6279 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
6280 and then Is_Generic_Type
(Etype
(Op1
))
6282 Check_Non_Static_Context
(Op1
);
6285 -- Here we have the case of an operand whose type is OK, which is
6286 -- static, and which does not raise constraint error, we can fold.
6289 Set_Is_Static_Expression
(N
);
6293 end Test_Expression_Is_Foldable
;
6297 procedure Test_Expression_Is_Foldable
6303 CRT_Safe
: Boolean := False)
6305 Rstat
: constant Boolean := Is_Static_Expression
(Op1
)
6307 Is_Static_Expression
(Op2
);
6313 -- Inhibit folding if -gnatd.f flag set
6315 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
6319 -- If either operand is Any_Type, just propagate to result and
6320 -- do not try to fold, this prevents cascaded errors.
6322 if Etype
(Op1
) = Any_Type
or else Etype
(Op2
) = Any_Type
then
6323 Set_Etype
(N
, Any_Type
);
6326 -- If left operand raises constraint error, then replace node N with the
6327 -- Raise_Constraint_Error node, and we are obviously not foldable.
6328 -- Is_Static_Expression is set from the two operands in the normal way,
6329 -- and we check the right operand if it is in a non-static context.
6331 elsif Raises_Constraint_Error
(Op1
) then
6333 Check_Non_Static_Context
(Op2
);
6336 Rewrite_In_Raise_CE
(N
, Op1
);
6337 Set_Is_Static_Expression
(N
, Rstat
);
6340 -- Similar processing for the case of the right operand. Note that we
6341 -- don't use this routine for the short-circuit case, so we do not have
6342 -- to worry about that special case here.
6344 elsif Raises_Constraint_Error
(Op2
) then
6346 Check_Non_Static_Context
(Op1
);
6349 Rewrite_In_Raise_CE
(N
, Op2
);
6350 Set_Is_Static_Expression
(N
, Rstat
);
6353 -- Exclude expressions of a generic modular type, as above
6355 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
6356 and then Is_Generic_Type
(Etype
(Op1
))
6358 Check_Non_Static_Context
(Op1
);
6361 -- If result is not static, then check non-static contexts on operands
6362 -- since one of them may be static and the other one may not be static.
6364 elsif not Rstat
then
6365 Check_Non_Static_Context
(Op1
);
6366 Check_Non_Static_Context
(Op2
);
6369 Fold
:= CRT_Safe_Compile_Time_Known_Value
(Op1
)
6370 and then CRT_Safe_Compile_Time_Known_Value
(Op2
);
6372 Fold
:= Compile_Time_Known_Value
(Op1
)
6373 and then Compile_Time_Known_Value
(Op2
);
6378 -- Else result is static and foldable. Both operands are static, and
6379 -- neither raises constraint error, so we can definitely fold.
6382 Set_Is_Static_Expression
(N
);
6387 end Test_Expression_Is_Foldable
;
6393 function Test_In_Range
6396 Assume_Valid
: Boolean;
6397 Fixed_Int
: Boolean;
6398 Int_Real
: Boolean) return Range_Membership
6403 pragma Warnings
(Off
, Assume_Valid
);
6404 -- For now Assume_Valid is unreferenced since the current implementation
6405 -- always returns Unknown if N is not a compile-time-known value, but we
6406 -- keep the parameter to allow for future enhancements in which we try
6407 -- to get the information in the variable case as well.
6410 -- If an error was posted on expression, then return Unknown, we do not
6411 -- want cascaded errors based on some false analysis of a junk node.
6413 if Error_Posted
(N
) then
6416 -- Expression that raises constraint error is an odd case. We certainly
6417 -- do not want to consider it to be in range. It might make sense to
6418 -- consider it always out of range, but this causes incorrect error
6419 -- messages about static expressions out of range. So we just return
6420 -- Unknown, which is always safe.
6422 elsif Raises_Constraint_Error
(N
) then
6425 -- Universal types have no range limits, so always in range
6427 elsif Typ
= Universal_Integer
or else Typ
= Universal_Real
then
6430 -- Never known if not scalar type. Don't know if this can actually
6431 -- happen, but our spec allows it, so we must check.
6433 elsif not Is_Scalar_Type
(Typ
) then
6436 -- Never known if this is a generic type, since the bounds of generic
6437 -- types are junk. Note that if we only checked for static expressions
6438 -- (instead of compile-time-known values) below, we would not need this
6439 -- check, because values of a generic type can never be static, but they
6440 -- can be known at compile time.
6442 elsif Is_Generic_Type
(Typ
) then
6445 -- Case of a known compile time value, where we can check if it is in
6446 -- the bounds of the given type.
6448 elsif Compile_Time_Known_Value
(N
) then
6457 Lo
:= Type_Low_Bound
(Typ
);
6458 Hi
:= Type_High_Bound
(Typ
);
6460 LB_Known
:= Compile_Time_Known_Value
(Lo
);
6461 HB_Known
:= Compile_Time_Known_Value
(Hi
);
6463 -- Fixed point types should be considered as such only if flag
6464 -- Fixed_Int is set to False.
6466 if Is_Floating_Point_Type
(Typ
)
6467 or else (Is_Fixed_Point_Type
(Typ
) and then not Fixed_Int
)
6470 Valr
:= Expr_Value_R
(N
);
6472 if LB_Known
and HB_Known
then
6473 if Valr
>= Expr_Value_R
(Lo
)
6475 Valr
<= Expr_Value_R
(Hi
)
6479 return Out_Of_Range
;
6482 elsif (LB_Known
and then Valr
< Expr_Value_R
(Lo
))
6484 (HB_Known
and then Valr
> Expr_Value_R
(Hi
))
6486 return Out_Of_Range
;
6493 Val
:= Expr_Value
(N
);
6495 if LB_Known
and HB_Known
then
6496 if Val
>= Expr_Value
(Lo
) and then Val
<= Expr_Value
(Hi
)
6500 return Out_Of_Range
;
6503 elsif (LB_Known
and then Val
< Expr_Value
(Lo
))
6505 (HB_Known
and then Val
> Expr_Value
(Hi
))
6507 return Out_Of_Range
;
6515 -- Here for value not known at compile time. Case of expression subtype
6516 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6517 -- In this case we know it is in range without knowing its value.
6520 and then (Etype
(N
) = Typ
or else Is_Subtype_Of
(Etype
(N
), Typ
))
6524 -- Another special case. For signed integer types, if the target type
6525 -- has Is_Known_Valid set, and the source type does not have a larger
6526 -- size, then the source value must be in range. We exclude biased
6527 -- types, because they bizarrely can generate out of range values.
6529 elsif Is_Signed_Integer_Type
(Etype
(N
))
6530 and then Is_Known_Valid
(Typ
)
6531 and then Esize
(Etype
(N
)) <= Esize
(Typ
)
6532 and then not Has_Biased_Representation
(Etype
(N
))
6536 -- For all other cases, result is unknown
6547 procedure To_Bits
(U
: Uint
; B
: out Bits
) is
6549 for J
in 0 .. B
'Last loop
6550 B
(J
) := (U
/ (2 ** J
)) mod 2 /= 0;
6554 --------------------
6555 -- Why_Not_Static --
6556 --------------------
6558 procedure Why_Not_Static
(Expr
: Node_Id
) is
6559 N
: constant Node_Id
:= Original_Node
(Expr
);
6560 Typ
: Entity_Id
:= Empty
;
6565 procedure Why_Not_Static_List
(L
: List_Id
);
6566 -- A version that can be called on a list of expressions. Finds all
6567 -- non-static violations in any element of the list.
6569 -------------------------
6570 -- Why_Not_Static_List --
6571 -------------------------
6573 procedure Why_Not_Static_List
(L
: List_Id
) is
6576 if Is_Non_Empty_List
(L
) then
6578 while Present
(N
) loop
6583 end Why_Not_Static_List
;
6585 -- Start of processing for Why_Not_Static
6588 -- Ignore call on error or empty node
6590 if No
(Expr
) or else Nkind
(Expr
) = N_Error
then
6594 -- Preprocessing for sub expressions
6596 if Nkind
(Expr
) in N_Subexpr
then
6598 -- Nothing to do if expression is static
6600 if Is_OK_Static_Expression
(Expr
) then
6604 -- Test for constraint error raised
6606 if Raises_Constraint_Error
(Expr
) then
6608 -- Special case membership to find out which piece to flag
6610 if Nkind
(N
) in N_Membership_Test
then
6611 if Raises_Constraint_Error
(Left_Opnd
(N
)) then
6612 Why_Not_Static
(Left_Opnd
(N
));
6615 elsif Present
(Right_Opnd
(N
))
6616 and then Raises_Constraint_Error
(Right_Opnd
(N
))
6618 Why_Not_Static
(Right_Opnd
(N
));
6622 pragma Assert
(Present
(Alternatives
(N
)));
6624 Alt
:= First
(Alternatives
(N
));
6625 while Present
(Alt
) loop
6626 if Raises_Constraint_Error
(Alt
) then
6627 Why_Not_Static
(Alt
);
6635 -- Special case a range to find out which bound to flag
6637 elsif Nkind
(N
) = N_Range
then
6638 if Raises_Constraint_Error
(Low_Bound
(N
)) then
6639 Why_Not_Static
(Low_Bound
(N
));
6642 elsif Raises_Constraint_Error
(High_Bound
(N
)) then
6643 Why_Not_Static
(High_Bound
(N
));
6647 -- Special case attribute to see which part to flag
6649 elsif Nkind
(N
) = N_Attribute_Reference
then
6650 if Raises_Constraint_Error
(Prefix
(N
)) then
6651 Why_Not_Static
(Prefix
(N
));
6655 if Present
(Expressions
(N
)) then
6656 Exp
:= First
(Expressions
(N
));
6657 while Present
(Exp
) loop
6658 if Raises_Constraint_Error
(Exp
) then
6659 Why_Not_Static
(Exp
);
6667 -- Special case a subtype name
6669 elsif Is_Entity_Name
(Expr
) and then Is_Type
(Entity
(Expr
)) then
6671 ("!& is not a static subtype (RM 4.9(26))", N
, Entity
(Expr
));
6675 -- End of special cases
6678 ("!expression raises exception, cannot be static (RM 4.9(34))",
6683 -- If no type, then something is pretty wrong, so ignore
6685 Typ
:= Etype
(Expr
);
6691 -- Type must be scalar or string type (but allow Bignum, since this
6692 -- is really a scalar type from our point of view in this diagnosis).
6694 if not Is_Scalar_Type
(Typ
)
6695 and then not Is_String_Type
(Typ
)
6696 and then not Is_RTE
(Typ
, RE_Bignum
)
6699 ("!static expression must have scalar or string type " &
6705 -- If we got through those checks, test particular node kind
6711 when N_Expanded_Name
6717 if Is_Named_Number
(E
) then
6720 elsif Ekind
(E
) = E_Constant
then
6722 -- One case we can give a metter message is when we have a
6723 -- string literal created by concatenating an aggregate with
6724 -- an others expression.
6726 Entity_Case
: declare
6727 CV
: constant Node_Id
:= Constant_Value
(E
);
6728 CO
: constant Node_Id
:= Original_Node
(CV
);
6730 function Is_Aggregate
(N
: Node_Id
) return Boolean;
6731 -- See if node N came from an others aggregate, if so
6732 -- return True and set Error_Msg_Sloc to aggregate.
6738 function Is_Aggregate
(N
: Node_Id
) return Boolean is
6740 if Nkind
(Original_Node
(N
)) = N_Aggregate
then
6741 Error_Msg_Sloc
:= Sloc
(Original_Node
(N
));
6744 elsif Is_Entity_Name
(N
)
6745 and then Ekind
(Entity
(N
)) = E_Constant
6747 Nkind
(Original_Node
(Constant_Value
(Entity
(N
)))) =
6751 Sloc
(Original_Node
(Constant_Value
(Entity
(N
))));
6759 -- Start of processing for Entity_Case
6762 if Is_Aggregate
(CV
)
6763 or else (Nkind
(CO
) = N_Op_Concat
6764 and then (Is_Aggregate
(Left_Opnd
(CO
))
6766 Is_Aggregate
(Right_Opnd
(CO
))))
6768 Error_Msg_N
("!aggregate (#) is never static", N
);
6770 elsif No
(CV
) or else not Is_Static_Expression
(CV
) then
6772 ("!& is not a static constant (RM 4.9(5))", N
, E
);
6776 elsif Is_Type
(E
) then
6778 ("!& is not a static subtype (RM 4.9(26))", N
, E
);
6782 ("!& is not static constant or named number "
6783 & "(RM 4.9(5))", N
, E
);
6792 if Nkind
(N
) in N_Op_Shift
then
6794 ("!shift functions are never static (RM 4.9(6,18))", N
);
6796 Why_Not_Static
(Left_Opnd
(N
));
6797 Why_Not_Static
(Right_Opnd
(N
));
6803 Why_Not_Static
(Right_Opnd
(N
));
6805 -- Attribute reference
6807 when N_Attribute_Reference
=>
6808 Why_Not_Static_List
(Expressions
(N
));
6810 E
:= Etype
(Prefix
(N
));
6812 if E
= Standard_Void_Type
then
6816 -- Special case non-scalar'Size since this is a common error
6818 if Attribute_Name
(N
) = Name_Size
then
6820 ("!size attribute is only static for static scalar type "
6821 & "(RM 4.9(7,8))", N
);
6825 elsif Is_Array_Type
(E
) then
6826 if not Nam_In
(Attribute_Name
(N
), Name_First
,
6831 ("!static array attribute must be Length, First, or Last "
6832 & "(RM 4.9(8))", N
);
6834 -- Since we know the expression is not-static (we already
6835 -- tested for this, must mean array is not static).
6839 ("!prefix is non-static array (RM 4.9(8))", Prefix
(N
));
6844 -- Special case generic types, since again this is a common source
6847 elsif Is_Generic_Actual_Type
(E
) or else Is_Generic_Type
(E
) then
6849 ("!attribute of generic type is never static "
6850 & "(RM 4.9(7,8))", N
);
6852 elsif Is_OK_Static_Subtype
(E
) then
6855 elsif Is_Scalar_Type
(E
) then
6857 ("!prefix type for attribute is not static scalar subtype "
6858 & "(RM 4.9(7))", N
);
6862 ("!static attribute must apply to array/scalar type "
6863 & "(RM 4.9(7,8))", N
);
6868 when N_String_Literal
=>
6870 ("!subtype of string literal is non-static (RM 4.9(4))", N
);
6872 -- Explicit dereference
6874 when N_Explicit_Dereference
=>
6876 ("!explicit dereference is never static (RM 4.9)", N
);
6880 when N_Function_Call
=>
6881 Why_Not_Static_List
(Parameter_Associations
(N
));
6883 -- Complain about non-static function call unless we have Bignum
6884 -- which means that the underlying expression is really some
6885 -- scalar arithmetic operation.
6887 if not Is_RTE
(Typ
, RE_Bignum
) then
6888 Error_Msg_N
("!non-static function call (RM 4.9(6,18))", N
);
6891 -- Parameter assocation (test actual parameter)
6893 when N_Parameter_Association
=>
6894 Why_Not_Static
(Explicit_Actual_Parameter
(N
));
6896 -- Indexed component
6898 when N_Indexed_Component
=>
6899 Error_Msg_N
("!indexed component is never static (RM 4.9)", N
);
6903 when N_Procedure_Call_Statement
=>
6904 Error_Msg_N
("!procedure call is never static (RM 4.9)", N
);
6906 -- Qualified expression (test expression)
6908 when N_Qualified_Expression
=>
6909 Why_Not_Static
(Expression
(N
));
6914 | N_Extension_Aggregate
6916 Error_Msg_N
("!an aggregate is never static (RM 4.9)", N
);
6921 Why_Not_Static
(Low_Bound
(N
));
6922 Why_Not_Static
(High_Bound
(N
));
6924 -- Range constraint, test range expression
6926 when N_Range_Constraint
=>
6927 Why_Not_Static
(Range_Expression
(N
));
6929 -- Subtype indication, test constraint
6931 when N_Subtype_Indication
=>
6932 Why_Not_Static
(Constraint
(N
));
6934 -- Selected component
6936 when N_Selected_Component
=>
6937 Error_Msg_N
("!selected component is never static (RM 4.9)", N
);
6942 Error_Msg_N
("!slice is never static (RM 4.9)", N
);
6944 when N_Type_Conversion
=>
6945 Why_Not_Static
(Expression
(N
));
6947 if not Is_Scalar_Type
(Entity
(Subtype_Mark
(N
)))
6948 or else not Is_OK_Static_Subtype
(Entity
(Subtype_Mark
(N
)))
6951 ("!static conversion requires static scalar subtype result "
6952 & "(RM 4.9(9))", N
);
6955 -- Unchecked type conversion
6957 when N_Unchecked_Type_Conversion
=>
6959 ("!unchecked type conversion is never static (RM 4.9)", N
);
6961 -- All other cases, no reason to give