1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree
; use Atree
;
27 with Checks
; use Checks
;
28 with Debug
; use Debug
;
29 with Einfo
; use Einfo
;
30 with Elists
; use Elists
;
31 with Errout
; use Errout
;
32 with Eval_Fat
; use Eval_Fat
;
33 with Exp_Util
; use Exp_Util
;
34 with Freeze
; use Freeze
;
36 with Namet
; use Namet
;
37 with Nmake
; use Nmake
;
38 with Nlists
; use Nlists
;
40 with Par_SCO
; use Par_SCO
;
41 with Rtsfind
; use Rtsfind
;
43 with Sem_Aux
; use Sem_Aux
;
44 with Sem_Cat
; use Sem_Cat
;
45 with Sem_Ch6
; use Sem_Ch6
;
46 with Sem_Ch8
; use Sem_Ch8
;
47 with Sem_Res
; use Sem_Res
;
48 with Sem_Util
; use Sem_Util
;
49 with Sem_Type
; use Sem_Type
;
50 with Sem_Warn
; use Sem_Warn
;
51 with Sinfo
; use Sinfo
;
52 with Snames
; use Snames
;
53 with Stand
; use Stand
;
54 with Stringt
; use Stringt
;
55 with Targparm
; use Targparm
;
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 From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
;
178 -- Converts a bit string of length B'Length to a Uint value to be used for
179 -- a target of type T, which is a modular type. This procedure includes the
180 -- necessary reduction by the modulus in the case of a nonbinary modulus
181 -- (for a binary modulus, the bit string is the right length any way so all
184 function Is_Static_Choice
(Choice
: Node_Id
) return Boolean;
185 -- Given a choice (from a case expression or membership test), returns
186 -- True if the choice is static. No test is made for raising of constraint
187 -- error, so this function is used only for legality tests.
189 function Is_Static_Choice_List
(Choices
: List_Id
) return Boolean;
190 -- Given a choice list (from a case expression or membership test), return
191 -- True if all choices are static in the sense of Is_Static_Choice.
193 function Is_OK_Static_Choice
(Choice
: Node_Id
) return Boolean;
194 -- Given a choice (from a case expression or membership test), returns
195 -- True if the choice is static and does not raise a Constraint_Error.
197 function Is_OK_Static_Choice_List
(Choices
: List_Id
) return Boolean;
198 -- Given a choice list (from a case expression or membership test), return
199 -- True if all choices are static in the sense of Is_OK_Static_Choice.
201 function Is_Static_Range
(N
: Node_Id
) return Boolean;
202 -- Determine if range is static, as defined in RM 4.9(26). The only allowed
203 -- argument is an N_Range node (but note that the semantic analysis of
204 -- equivalent range attribute references already turned them into the
205 -- equivalent range). This differs from Is_OK_Static_Range (which is what
206 -- must be used by clients) in that it does not care whether the bounds
207 -- raise Constraint_Error or not. Used for checking whether expressions are
208 -- static in the 4.9 sense (without worrying about exceptions).
210 function Get_String_Val
(N
: Node_Id
) return Node_Id
;
211 -- Given a tree node for a folded string or character value, returns the
212 -- corresponding string literal or character literal (one of the two must
213 -- be available, or the operand would not have been marked as foldable in
214 -- the earlier analysis of the operation).
216 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean;
217 -- Bits represents the number of bits in an integer value to be computed
218 -- (but the value has not been computed yet). If this value in Bits is
219 -- reasonable, a result of True is returned, with the implication that the
220 -- caller should go ahead and complete the calculation. If the value in
221 -- Bits is unreasonably large, then an error is posted on node N, and
222 -- False is returned (and the caller skips the proposed calculation).
224 procedure Out_Of_Range
(N
: Node_Id
);
225 -- This procedure is called if it is determined that node N, which appears
226 -- in a non-static context, is a compile time known value which is outside
227 -- its range, i.e. the range of Etype. This is used in contexts where
228 -- this is an illegality if N is static, and should generate a warning
231 function Real_Or_String_Static_Predicate_Matches
233 Typ
: Entity_Id
) return Boolean;
234 -- This is the function used to evaluate real or string static predicates.
235 -- Val is an unanalyzed N_Real_Literal or N_String_Literal node, which
236 -- represents the value to be tested against the predicate. Typ is the
237 -- type with the predicate, from which the predicate expression can be
238 -- extracted. The result returned is True if the given value satisfies
241 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
);
242 -- N and Exp are nodes representing an expression, Exp is known to raise
243 -- CE. N is rewritten in term of Exp in the optimal way.
245 function String_Type_Len
(Stype
: Entity_Id
) return Uint
;
246 -- Given a string type, determines the length of the index type, or, if
247 -- this index type is non-static, the length of the base type of this index
248 -- type. Note that if the string type is itself static, then the index type
249 -- is static, so the second case applies only if the string type passed is
252 function Test
(Cond
: Boolean) return Uint
;
253 pragma Inline
(Test
);
254 -- This function simply returns the appropriate Boolean'Pos value
255 -- corresponding to the value of Cond as a universal integer. It is
256 -- used for producing the result of the static evaluation of the
259 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
;
260 -- Check whether an arithmetic operation with universal operands which is a
261 -- rewritten function call with an explicit scope indication is ambiguous:
262 -- P."+" (1, 2) will be ambiguous if there is more than one visible numeric
263 -- type declared in P and the context does not impose a type on the result
264 -- (e.g. in the expression of a type conversion). If ambiguous, emit an
265 -- error and return Empty, else return the result type of the operator.
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).
353 -- Case of real static predicate
355 if Is_Real_Type
(Typ
) then
356 if Real_Or_String_Static_Predicate_Matches
357 (Val
=> Make_Real_Literal
(Sloc
(Expr
), Expr_Value_R
(Expr
)),
363 -- Case of string static predicate
365 elsif Is_String_Type
(Typ
) then
366 if Real_Or_String_Static_Predicate_Matches
367 (Val
=> Expr_Value_S
(Expr
), Typ
=> Typ
)
372 -- Case of discrete static predicate
375 pragma Assert
(Is_Discrete_Type
(Typ
));
377 -- If static predicate matches, nothing to do
379 if Choices_Match
(Expr
, Static_Discrete_Predicate
(Typ
)) = Match
then
384 -- Here we know that the predicate will fail
386 -- Special case of static expression failing a predicate (other than one
387 -- that was explicitly specified with a Dynamic_Predicate aspect). This
388 -- is the case where the expression is no longer considered static.
390 if Is_Static_Expression
(Expr
)
391 and then not Has_Dynamic_Predicate_Aspect
(Typ
)
394 ("??static expression fails static predicate check on &",
397 ("\??expression is no longer considered static", Expr
);
398 Set_Is_Static_Expression
(Expr
, False);
400 -- In all other cases, this is just a warning that a test will fail.
401 -- It does not matter if the expression is static or not, or if the
402 -- predicate comes from a dynamic predicate aspect or not.
406 ("??expression fails predicate check on &", Expr
, Typ
);
408 end Check_Expression_Against_Static_Predicate
;
410 ------------------------------
411 -- Check_Non_Static_Context --
412 ------------------------------
414 procedure Check_Non_Static_Context
(N
: Node_Id
) is
415 T
: constant Entity_Id
:= Etype
(N
);
416 Checks_On
: constant Boolean :=
417 not Index_Checks_Suppressed
(T
)
418 and not Range_Checks_Suppressed
(T
);
421 -- Ignore cases of non-scalar types, error types, or universal real
422 -- types that have no usable bounds.
425 or else not Is_Scalar_Type
(T
)
426 or else T
= Universal_Fixed
427 or else T
= Universal_Real
432 -- At this stage we have a scalar type. If we have an expression that
433 -- raises CE, then we already issued a warning or error msg so there is
434 -- nothing more to be done in this routine.
436 if Raises_Constraint_Error
(N
) then
440 -- Now we have a scalar type which is not marked as raising a constraint
441 -- error exception. The main purpose of this routine is to deal with
442 -- static expressions appearing in a non-static context. That means
443 -- that if we do not have a static expression then there is not much
444 -- to do. The one case that we deal with here is that if we have a
445 -- floating-point value that is out of range, then we post a warning
446 -- that an infinity will result.
448 if not Is_Static_Expression
(N
) then
449 if Is_Floating_Point_Type
(T
)
450 and then Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True)
453 ("??float value out of range, infinity will be generated", N
);
459 -- Here we have the case of outer level static expression of scalar
460 -- type, where the processing of this procedure is needed.
462 -- For real types, this is where we convert the value to a machine
463 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
464 -- need to do this if the parent is a constant declaration, since in
465 -- other cases, gigi should do the necessary conversion correctly, but
466 -- experimentation shows that this is not the case on all machines, in
467 -- particular if we do not convert all literals to machine values in
468 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
471 if Nkind
(N
) = N_Real_Literal
472 and then not Is_Machine_Number
(N
)
473 and then not Is_Generic_Type
(Etype
(N
))
474 and then Etype
(N
) /= Universal_Real
476 -- Check that value is in bounds before converting to machine
477 -- number, so as not to lose case where value overflows in the
478 -- least significant bit or less. See B490001.
480 if Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
485 -- Note: we have to copy the node, to avoid problems with conformance
486 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
488 Rewrite
(N
, New_Copy
(N
));
490 if not Is_Floating_Point_Type
(T
) then
492 (N
, Corresponding_Integer_Value
(N
) * Small_Value
(T
));
494 elsif not UR_Is_Zero
(Realval
(N
)) then
496 -- Note: even though RM 4.9(38) specifies biased rounding, this
497 -- has been modified by AI-100 in order to prevent confusing
498 -- differences in rounding between static and non-static
499 -- expressions. AI-100 specifies that the effect of such rounding
500 -- is implementation dependent, and in GNAT we round to nearest
501 -- even to match the run-time behavior. Note that this applies
502 -- to floating point literals, not fixed points ones, even though
503 -- their compiler representation is also as a universal real.
506 (N
, Machine
(Base_Type
(T
), Realval
(N
), Round_Even
, N
));
507 Set_Is_Machine_Number
(N
);
512 -- Check for out of range universal integer. This is a non-static
513 -- context, so the integer value must be in range of the runtime
514 -- representation of universal integers.
516 -- We do this only within an expression, because that is the only
517 -- case in which non-static universal integer values can occur, and
518 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
519 -- called in contexts like the expression of a number declaration where
520 -- we certainly want to allow out of range values.
522 if Etype
(N
) = Universal_Integer
523 and then Nkind
(N
) = N_Integer_Literal
524 and then Nkind
(Parent
(N
)) in N_Subexpr
526 (Intval
(N
) < Expr_Value
(Type_Low_Bound
(Universal_Integer
))
528 Intval
(N
) > Expr_Value
(Type_High_Bound
(Universal_Integer
)))
530 Apply_Compile_Time_Constraint_Error
531 (N
, "non-static universal integer value out of range<<",
532 CE_Range_Check_Failed
);
534 -- Check out of range of base type
536 elsif Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True) then
539 -- Give warning if outside subtype (where one or both of the bounds of
540 -- the subtype is static). This warning is omitted if the expression
541 -- appears in a range that could be null (warnings are handled elsewhere
544 elsif T
/= Base_Type
(T
) and then Nkind
(Parent
(N
)) /= N_Range
then
545 if Is_In_Range
(N
, T
, Assume_Valid
=> True) then
548 elsif Is_Out_Of_Range
(N
, T
, Assume_Valid
=> True) then
549 Apply_Compile_Time_Constraint_Error
550 (N
, "value not in range of}<<", CE_Range_Check_Failed
);
553 Enable_Range_Check
(N
);
556 Set_Do_Range_Check
(N
, False);
559 end Check_Non_Static_Context
;
561 ---------------------------------
562 -- Check_String_Literal_Length --
563 ---------------------------------
565 procedure Check_String_Literal_Length
(N
: Node_Id
; Ttype
: Entity_Id
) is
567 if not Raises_Constraint_Error
(N
) and then Is_Constrained
(Ttype
) then
568 if UI_From_Int
(String_Length
(Strval
(N
))) /= String_Type_Len
(Ttype
)
570 Apply_Compile_Time_Constraint_Error
571 (N
, "string length wrong for}??",
572 CE_Length_Check_Failed
,
577 end Check_String_Literal_Length
;
583 function Choice_Matches
585 Choice
: Node_Id
) return Match_Result
587 Etyp
: constant Entity_Id
:= Etype
(Expr
);
593 pragma Assert
(Compile_Time_Known_Value
(Expr
));
594 pragma Assert
(Is_Scalar_Type
(Etyp
) or else Is_String_Type
(Etyp
));
596 if not Is_OK_Static_Choice
(Choice
) then
597 Set_Raises_Constraint_Error
(Choice
);
600 -- Discrete type case
602 elsif Is_Discrete_Type
(Etype
(Expr
)) then
603 Val
:= Expr_Value
(Expr
);
605 if Nkind
(Choice
) = N_Range
then
606 if Val
>= Expr_Value
(Low_Bound
(Choice
))
608 Val
<= Expr_Value
(High_Bound
(Choice
))
615 elsif Nkind
(Choice
) = N_Subtype_Indication
617 (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
619 if Val
>= Expr_Value
(Type_Low_Bound
(Etype
(Choice
)))
621 Val
<= Expr_Value
(Type_High_Bound
(Etype
(Choice
)))
628 elsif Nkind
(Choice
) = N_Others_Choice
then
632 if Val
= Expr_Value
(Choice
) then
641 elsif Is_Real_Type
(Etype
(Expr
)) then
642 ValR
:= Expr_Value_R
(Expr
);
644 if Nkind
(Choice
) = N_Range
then
645 if ValR
>= Expr_Value_R
(Low_Bound
(Choice
))
647 ValR
<= Expr_Value_R
(High_Bound
(Choice
))
654 elsif Nkind
(Choice
) = N_Subtype_Indication
656 (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
658 if ValR
>= Expr_Value_R
(Type_Low_Bound
(Etype
(Choice
)))
660 ValR
<= Expr_Value_R
(Type_High_Bound
(Etype
(Choice
)))
668 if ValR
= Expr_Value_R
(Choice
) then
678 pragma Assert
(Is_String_Type
(Etype
(Expr
)));
679 ValS
:= Expr_Value_S
(Expr
);
681 if Nkind
(Choice
) = N_Subtype_Indication
683 (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
685 if not Is_Constrained
(Etype
(Choice
)) then
690 Typlen
: constant Uint
:=
691 String_Type_Len
(Etype
(Choice
));
692 Strlen
: constant Uint
:=
693 UI_From_Int
(String_Length
(Strval
(ValS
)));
695 if Typlen
= Strlen
then
704 if String_Equal
(Strval
(ValS
), Strval
(Expr_Value_S
(Choice
)))
718 function Choices_Match
720 Choices
: List_Id
) return Match_Result
723 Result
: Match_Result
;
726 Choice
:= First
(Choices
);
727 while Present
(Choice
) loop
728 Result
:= Choice_Matches
(Expr
, Choice
);
730 if Result
/= No_Match
then
740 --------------------------
741 -- Compile_Time_Compare --
742 --------------------------
744 function Compile_Time_Compare
746 Assume_Valid
: Boolean) return Compare_Result
748 Discard
: aliased Uint
;
750 return Compile_Time_Compare
(L
, R
, Discard
'Access, Assume_Valid
);
751 end Compile_Time_Compare
;
753 function Compile_Time_Compare
756 Assume_Valid
: Boolean;
757 Rec
: Boolean := False) return Compare_Result
759 Ltyp
: Entity_Id
:= Underlying_Type
(Etype
(L
));
760 Rtyp
: Entity_Id
:= Underlying_Type
(Etype
(R
));
761 -- These get reset to the base type for the case of entities where
762 -- Is_Known_Valid is not set. This takes care of handling possible
763 -- invalid representations using the value of the base type, in
764 -- accordance with RM 13.9.1(10).
766 Discard
: aliased Uint
;
768 procedure Compare_Decompose
772 -- This procedure decomposes the node N into an expression node and a
773 -- signed offset, so that the value of N is equal to the value of R plus
774 -- the value V (which may be negative). If no such decomposition is
775 -- possible, then on return R is a copy of N, and V is set to zero.
777 function Compare_Fixup
(N
: Node_Id
) return Node_Id
;
778 -- This function deals with replacing 'Last and 'First references with
779 -- their corresponding type bounds, which we then can compare. The
780 -- argument is the original node, the result is the identity, unless we
781 -- have a 'Last/'First reference in which case the value returned is the
782 -- appropriate type bound.
784 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean;
785 -- Even if the context does not assume that values are valid, some
786 -- simple cases can be recognized.
788 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean;
789 -- Returns True iff L and R represent expressions that definitely have
790 -- identical (but not necessarily compile time known) values Indeed the
791 -- caller is expected to have already dealt with the cases of compile
792 -- time known values, so these are not tested here.
794 -----------------------
795 -- Compare_Decompose --
796 -----------------------
798 procedure Compare_Decompose
804 if Nkind
(N
) = N_Op_Add
805 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
808 V
:= Intval
(Right_Opnd
(N
));
811 elsif Nkind
(N
) = N_Op_Subtract
812 and then Nkind
(Right_Opnd
(N
)) = N_Integer_Literal
815 V
:= UI_Negate
(Intval
(Right_Opnd
(N
)));
818 elsif Nkind
(N
) = N_Attribute_Reference
then
819 if Attribute_Name
(N
) = Name_Succ
then
820 R
:= First
(Expressions
(N
));
824 elsif Attribute_Name
(N
) = Name_Pred
then
825 R
:= First
(Expressions
(N
));
833 end Compare_Decompose
;
839 function Compare_Fixup
(N
: Node_Id
) return Node_Id
is
845 -- Fixup only required for First/Last attribute reference
847 if Nkind
(N
) = N_Attribute_Reference
848 and then Nam_In
(Attribute_Name
(N
), Name_First
, Name_Last
)
850 Xtyp
:= Etype
(Prefix
(N
));
852 -- If we have no type, then just abandon the attempt to do
853 -- a fixup, this is probably the result of some other error.
859 -- Dereference an access type
861 if Is_Access_Type
(Xtyp
) then
862 Xtyp
:= Designated_Type
(Xtyp
);
865 -- If we don't have an array type at this stage, something is
866 -- peculiar, e.g. another error, and we abandon the attempt at
869 if not Is_Array_Type
(Xtyp
) then
873 -- Ignore unconstrained array, since bounds are not meaningful
875 if not Is_Constrained
(Xtyp
) then
879 if Ekind
(Xtyp
) = E_String_Literal_Subtype
then
880 if Attribute_Name
(N
) = Name_First
then
881 return String_Literal_Low_Bound
(Xtyp
);
884 Make_Integer_Literal
(Sloc
(N
),
885 Intval
=> Intval
(String_Literal_Low_Bound
(Xtyp
)) +
886 String_Literal_Length
(Xtyp
));
890 -- Find correct index type
892 Indx
:= First_Index
(Xtyp
);
894 if Present
(Expressions
(N
)) then
895 Subs
:= UI_To_Int
(Expr_Value
(First
(Expressions
(N
))));
897 for J
in 2 .. Subs
loop
898 Indx
:= Next_Index
(Indx
);
902 Xtyp
:= Etype
(Indx
);
904 if Attribute_Name
(N
) = Name_First
then
905 return Type_Low_Bound
(Xtyp
);
907 return Type_High_Bound
(Xtyp
);
914 ----------------------------
915 -- Is_Known_Valid_Operand --
916 ----------------------------
918 function Is_Known_Valid_Operand
(Opnd
: Node_Id
) return Boolean is
920 return (Is_Entity_Name
(Opnd
)
922 (Is_Known_Valid
(Entity
(Opnd
))
923 or else Ekind
(Entity
(Opnd
)) = E_In_Parameter
925 (Ekind
(Entity
(Opnd
)) in Object_Kind
926 and then Present
(Current_Value
(Entity
(Opnd
))))))
927 or else Is_OK_Static_Expression
(Opnd
);
928 end Is_Known_Valid_Operand
;
934 function Is_Same_Value
(L
, R
: Node_Id
) return Boolean is
935 Lf
: constant Node_Id
:= Compare_Fixup
(L
);
936 Rf
: constant Node_Id
:= Compare_Fixup
(R
);
938 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean;
939 -- L, R are the Expressions values from two attribute nodes for First
940 -- or Last attributes. Either may be set to No_List if no expressions
941 -- are present (indicating subscript 1). The result is True if both
942 -- expressions represent the same subscript (note one case is where
943 -- one subscript is missing and the other is explicitly set to 1).
945 -----------------------
946 -- Is_Same_Subscript --
947 -----------------------
949 function Is_Same_Subscript
(L
, R
: List_Id
) return Boolean is
955 return Expr_Value
(First
(R
)) = Uint_1
;
960 return Expr_Value
(First
(L
)) = Uint_1
;
962 return Expr_Value
(First
(L
)) = Expr_Value
(First
(R
));
965 end Is_Same_Subscript
;
967 -- Start of processing for Is_Same_Value
970 -- Values are the same if they refer to the same entity and the
971 -- entity is non-volatile. This does not however apply to Float
972 -- types, since we may have two NaN values and they should never
975 -- If the entity is a discriminant, the two expressions may be bounds
976 -- of components of objects of the same discriminated type. The
977 -- values of the discriminants are not static, and therefore the
978 -- result is unknown.
980 -- It would be better to comment individual branches of this test ???
982 if Nkind_In
(Lf
, N_Identifier
, N_Expanded_Name
)
983 and then Nkind_In
(Rf
, N_Identifier
, N_Expanded_Name
)
984 and then Entity
(Lf
) = Entity
(Rf
)
985 and then Ekind
(Entity
(Lf
)) /= E_Discriminant
986 and then Present
(Entity
(Lf
))
987 and then not Is_Floating_Point_Type
(Etype
(L
))
988 and then not Is_Volatile_Reference
(L
)
989 and then not Is_Volatile_Reference
(R
)
993 -- Or if they are compile time known and identical
995 elsif Compile_Time_Known_Value
(Lf
)
997 Compile_Time_Known_Value
(Rf
)
998 and then Expr_Value
(Lf
) = Expr_Value
(Rf
)
1002 -- False if Nkind of the two nodes is different for remaining cases
1004 elsif Nkind
(Lf
) /= Nkind
(Rf
) then
1007 -- True if both 'First or 'Last values applying to the same entity
1008 -- (first and last don't change even if value does). Note that we
1009 -- need this even with the calls to Compare_Fixup, to handle the
1010 -- case of unconstrained array attributes where Compare_Fixup
1011 -- cannot find useful bounds.
1013 elsif Nkind
(Lf
) = N_Attribute_Reference
1014 and then Attribute_Name
(Lf
) = Attribute_Name
(Rf
)
1015 and then Nam_In
(Attribute_Name
(Lf
), Name_First
, Name_Last
)
1016 and then Nkind_In
(Prefix
(Lf
), N_Identifier
, N_Expanded_Name
)
1017 and then Nkind_In
(Prefix
(Rf
), N_Identifier
, N_Expanded_Name
)
1018 and then Entity
(Prefix
(Lf
)) = Entity
(Prefix
(Rf
))
1019 and then Is_Same_Subscript
(Expressions
(Lf
), Expressions
(Rf
))
1023 -- True if the same selected component from the same record
1025 elsif Nkind
(Lf
) = N_Selected_Component
1026 and then Selector_Name
(Lf
) = Selector_Name
(Rf
)
1027 and then Is_Same_Value
(Prefix
(Lf
), Prefix
(Rf
))
1031 -- True if the same unary operator applied to the same operand
1033 elsif Nkind
(Lf
) in N_Unary_Op
1034 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1038 -- True if the same binary operator applied to the same operands
1040 elsif Nkind
(Lf
) in N_Binary_Op
1041 and then Is_Same_Value
(Left_Opnd
(Lf
), Left_Opnd
(Rf
))
1042 and then Is_Same_Value
(Right_Opnd
(Lf
), Right_Opnd
(Rf
))
1046 -- All other cases, we can't tell, so return False
1053 -- Start of processing for Compile_Time_Compare
1056 Diff
.all := No_Uint
;
1058 -- In preanalysis mode, always return Unknown unless the expression
1059 -- is static. It is too early to be thinking we know the result of a
1060 -- comparison, save that judgment for the full analysis. This is
1061 -- particularly important in the case of pre and postconditions, which
1062 -- otherwise can be prematurely collapsed into having True or False
1063 -- conditions when this is inappropriate.
1065 if not (Full_Analysis
1066 or else (Is_OK_Static_Expression
(L
)
1068 Is_OK_Static_Expression
(R
)))
1073 -- If either operand could raise constraint error, then we cannot
1074 -- know the result at compile time (since CE may be raised).
1076 if not (Cannot_Raise_Constraint_Error
(L
)
1078 Cannot_Raise_Constraint_Error
(R
))
1083 -- Identical operands are most certainly equal
1088 -- If expressions have no types, then do not attempt to determine if
1089 -- they are the same, since something funny is going on. One case in
1090 -- which this happens is during generic template analysis, when bounds
1091 -- are not fully analyzed.
1093 elsif No
(Ltyp
) or else No
(Rtyp
) then
1096 -- We do not attempt comparisons for packed arrays arrays represented as
1097 -- modular types, where the semantics of comparison is quite different.
1099 elsif Is_Packed_Array_Impl_Type
(Ltyp
)
1100 and then Is_Modular_Integer_Type
(Ltyp
)
1104 -- For access types, the only time we know the result at compile time
1105 -- (apart from identical operands, which we handled already) is if we
1106 -- know one operand is null and the other is not, or both operands are
1109 elsif Is_Access_Type
(Ltyp
) then
1110 if Known_Null
(L
) then
1111 if Known_Null
(R
) then
1113 elsif Known_Non_Null
(R
) then
1119 elsif Known_Non_Null
(L
) and then Known_Null
(R
) then
1126 -- Case where comparison involves two compile time known values
1128 elsif Compile_Time_Known_Value
(L
)
1130 Compile_Time_Known_Value
(R
)
1132 -- For the floating-point case, we have to be a little careful, since
1133 -- at compile time we are dealing with universal exact values, but at
1134 -- runtime, these will be in non-exact target form. That's why the
1135 -- returned results are LE and GE below instead of LT and GT.
1137 if Is_Floating_Point_Type
(Ltyp
)
1139 Is_Floating_Point_Type
(Rtyp
)
1142 Lo
: constant Ureal
:= Expr_Value_R
(L
);
1143 Hi
: constant Ureal
:= Expr_Value_R
(R
);
1154 -- For string types, we have two string literals and we proceed to
1155 -- compare them using the Ada style dictionary string comparison.
1157 elsif not Is_Scalar_Type
(Ltyp
) then
1159 Lstring
: constant String_Id
:= Strval
(Expr_Value_S
(L
));
1160 Rstring
: constant String_Id
:= Strval
(Expr_Value_S
(R
));
1161 Llen
: constant Nat
:= String_Length
(Lstring
);
1162 Rlen
: constant Nat
:= String_Length
(Rstring
);
1165 for J
in 1 .. Nat
'Min (Llen
, Rlen
) loop
1167 LC
: constant Char_Code
:= Get_String_Char
(Lstring
, J
);
1168 RC
: constant Char_Code
:= Get_String_Char
(Rstring
, J
);
1180 elsif Llen
> Rlen
then
1187 -- For remaining scalar cases we know exactly (note that this does
1188 -- include the fixed-point case, where we know the run time integer
1193 Lo
: constant Uint
:= Expr_Value
(L
);
1194 Hi
: constant Uint
:= Expr_Value
(R
);
1197 Diff
.all := Hi
- Lo
;
1202 Diff
.all := Lo
- Hi
;
1208 -- Cases where at least one operand is not known at compile time
1211 -- Remaining checks apply only for discrete types
1213 if not Is_Discrete_Type
(Ltyp
)
1215 not Is_Discrete_Type
(Rtyp
)
1220 -- Defend against generic types, or actually any expressions that
1221 -- contain a reference to a generic type from within a generic
1222 -- template. We don't want to do any range analysis of such
1223 -- expressions for two reasons. First, the bounds of a generic type
1224 -- itself are junk and cannot be used for any kind of analysis.
1225 -- Second, we may have a case where the range at run time is indeed
1226 -- known, but we don't want to do compile time analysis in the
1227 -- template based on that range since in an instance the value may be
1228 -- static, and able to be elaborated without reference to the bounds
1229 -- of types involved. As an example, consider:
1231 -- (F'Pos (F'Last) + 1) > Integer'Last
1233 -- The expression on the left side of > is Universal_Integer and thus
1234 -- acquires the type Integer for evaluation at run time, and at run
1235 -- time it is true that this condition is always False, but within
1236 -- an instance F may be a type with a static range greater than the
1237 -- range of Integer, and the expression statically evaluates to True.
1239 if References_Generic_Formal_Type
(L
)
1241 References_Generic_Formal_Type
(R
)
1246 -- Replace types by base types for the case of values which are not
1247 -- known to have valid representations. This takes care of properly
1248 -- dealing with invalid representations.
1250 if not Assume_Valid
then
1251 if not (Is_Entity_Name
(L
)
1252 and then (Is_Known_Valid
(Entity
(L
))
1253 or else Assume_No_Invalid_Values
))
1255 Ltyp
:= Underlying_Type
(Base_Type
(Ltyp
));
1258 if not (Is_Entity_Name
(R
)
1259 and then (Is_Known_Valid
(Entity
(R
))
1260 or else Assume_No_Invalid_Values
))
1262 Rtyp
:= Underlying_Type
(Base_Type
(Rtyp
));
1266 -- First attempt is to decompose the expressions to extract a
1267 -- constant offset resulting from the use of any of the forms:
1274 -- Then we see if the two expressions are the same value, and if so
1275 -- the result is obtained by comparing the offsets.
1277 -- Note: the reason we do this test first is that it returns only
1278 -- decisive results (with diff set), where other tests, like the
1279 -- range test, may not be as so decisive. Consider for example
1280 -- J .. J + 1. This code can conclude LT with a difference of 1,
1281 -- even if the range of J is not known.
1290 Compare_Decompose
(L
, Lnode
, Loffs
);
1291 Compare_Decompose
(R
, Rnode
, Roffs
);
1293 if Is_Same_Value
(Lnode
, Rnode
) then
1294 if Loffs
= Roffs
then
1296 elsif Loffs
< Roffs
then
1297 Diff
.all := Roffs
- Loffs
;
1300 Diff
.all := Loffs
- Roffs
;
1306 -- Next, try range analysis and see if operand ranges are disjoint
1314 -- True if each range is a single point
1317 Determine_Range
(L
, LOK
, LLo
, LHi
, Assume_Valid
);
1318 Determine_Range
(R
, ROK
, RLo
, RHi
, Assume_Valid
);
1321 Single
:= (LLo
= LHi
) and then (RLo
= RHi
);
1324 if Single
and Assume_Valid
then
1325 Diff
.all := RLo
- LLo
;
1330 elsif RHi
< LLo
then
1331 if Single
and Assume_Valid
then
1332 Diff
.all := LLo
- RLo
;
1337 elsif Single
and then LLo
= RLo
then
1339 -- If the range includes a single literal and we can assume
1340 -- validity then the result is known even if an operand is
1343 if Assume_Valid
then
1349 elsif LHi
= RLo
then
1352 elsif RHi
= LLo
then
1355 elsif not Is_Known_Valid_Operand
(L
)
1356 and then not Assume_Valid
1358 if Is_Same_Value
(L
, R
) then
1365 -- If the range of either operand cannot be determined, nothing
1366 -- further can be inferred.
1373 -- Here is where we check for comparisons against maximum bounds of
1374 -- types, where we know that no value can be outside the bounds of
1375 -- the subtype. Note that this routine is allowed to assume that all
1376 -- expressions are within their subtype bounds. Callers wishing to
1377 -- deal with possibly invalid values must in any case take special
1378 -- steps (e.g. conversions to larger types) to avoid this kind of
1379 -- optimization, which is always considered to be valid. We do not
1380 -- attempt this optimization with generic types, since the type
1381 -- bounds may not be meaningful in this case.
1383 -- We are in danger of an infinite recursion here. It does not seem
1384 -- useful to go more than one level deep, so the parameter Rec is
1385 -- used to protect ourselves against this infinite recursion.
1389 -- See if we can get a decisive check against one operand and a
1390 -- bound of the other operand (four possible tests here). Note
1391 -- that we avoid testing junk bounds of a generic type.
1393 if not Is_Generic_Type
(Rtyp
) then
1394 case Compile_Time_Compare
(L
, Type_Low_Bound
(Rtyp
),
1396 Assume_Valid
, Rec
=> True)
1398 when LT
=> return LT
;
1399 when LE
=> return LE
;
1400 when EQ
=> return LE
;
1401 when others => null;
1404 case Compile_Time_Compare
(L
, Type_High_Bound
(Rtyp
),
1406 Assume_Valid
, Rec
=> True)
1408 when GT
=> return GT
;
1409 when GE
=> return GE
;
1410 when EQ
=> return GE
;
1411 when others => null;
1415 if not Is_Generic_Type
(Ltyp
) then
1416 case Compile_Time_Compare
(Type_Low_Bound
(Ltyp
), R
,
1418 Assume_Valid
, Rec
=> True)
1420 when GT
=> return GT
;
1421 when GE
=> return GE
;
1422 when EQ
=> return GE
;
1423 when others => null;
1426 case Compile_Time_Compare
(Type_High_Bound
(Ltyp
), R
,
1428 Assume_Valid
, Rec
=> True)
1430 when LT
=> return LT
;
1431 when LE
=> return LE
;
1432 when EQ
=> return LE
;
1433 when others => null;
1438 -- Next attempt is to see if we have an entity compared with a
1439 -- compile time known value, where there is a current value
1440 -- conditional for the entity which can tell us the result.
1444 -- Entity variable (left operand)
1447 -- Value (right operand)
1450 -- If False, we have reversed the operands
1453 -- Comparison operator kind from Get_Current_Value_Condition call
1456 -- Value from Get_Current_Value_Condition call
1461 Result
: Compare_Result
;
1462 -- Known result before inversion
1465 if Is_Entity_Name
(L
)
1466 and then Compile_Time_Known_Value
(R
)
1469 Val
:= Expr_Value
(R
);
1472 elsif Is_Entity_Name
(R
)
1473 and then Compile_Time_Known_Value
(L
)
1476 Val
:= Expr_Value
(L
);
1479 -- That was the last chance at finding a compile time result
1485 Get_Current_Value_Condition
(Var
, Op
, Opn
);
1487 -- That was the last chance, so if we got nothing return
1493 Opv
:= Expr_Value
(Opn
);
1495 -- We got a comparison, so we might have something interesting
1497 -- Convert LE to LT and GE to GT, just so we have fewer cases
1499 if Op
= N_Op_Le
then
1503 elsif Op
= N_Op_Ge
then
1508 -- Deal with equality case
1510 if Op
= N_Op_Eq
then
1513 elsif Opv
< Val
then
1519 -- Deal with inequality case
1521 elsif Op
= N_Op_Ne
then
1528 -- Deal with greater than case
1530 elsif Op
= N_Op_Gt
then
1533 elsif Opv
= Val
- 1 then
1539 -- Deal with less than case
1541 else pragma Assert
(Op
= N_Op_Lt
);
1544 elsif Opv
= Val
+ 1 then
1551 -- Deal with inverting result
1555 when GT
=> return LT
;
1556 when GE
=> return LE
;
1557 when LT
=> return GT
;
1558 when LE
=> return GE
;
1559 when others => return Result
;
1566 end Compile_Time_Compare
;
1568 -------------------------------
1569 -- Compile_Time_Known_Bounds --
1570 -------------------------------
1572 function Compile_Time_Known_Bounds
(T
: Entity_Id
) return Boolean is
1577 if T
= Any_Composite
or else not Is_Array_Type
(T
) then
1581 Indx
:= First_Index
(T
);
1582 while Present
(Indx
) loop
1583 Typ
:= Underlying_Type
(Etype
(Indx
));
1585 -- Never look at junk bounds of a generic type
1587 if Is_Generic_Type
(Typ
) then
1591 -- Otherwise check bounds for compile time known
1593 if not Compile_Time_Known_Value
(Type_Low_Bound
(Typ
)) then
1595 elsif not Compile_Time_Known_Value
(Type_High_Bound
(Typ
)) then
1603 end Compile_Time_Known_Bounds
;
1605 ------------------------------
1606 -- Compile_Time_Known_Value --
1607 ------------------------------
1609 function Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1610 K
: constant Node_Kind
:= Nkind
(Op
);
1611 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(Op
) mod CV_Cache_Size
);
1614 -- Never known at compile time if bad type or raises constraint error
1615 -- or empty (latter case occurs only as a result of a previous error).
1618 Check_Error_Detected
;
1622 or else Etype
(Op
) = Any_Type
1623 or else Raises_Constraint_Error
(Op
)
1628 -- If we have an entity name, then see if it is the name of a constant
1629 -- and if so, test the corresponding constant value, or the name of
1630 -- an enumeration literal, which is always a constant.
1632 if Present
(Etype
(Op
)) and then Is_Entity_Name
(Op
) then
1634 E
: constant Entity_Id
:= Entity
(Op
);
1638 -- Never known at compile time if it is a packed array value.
1639 -- We might want to try to evaluate these at compile time one
1640 -- day, but we do not make that attempt now.
1642 if Is_Packed_Array_Impl_Type
(Etype
(Op
)) then
1646 if Ekind
(E
) = E_Enumeration_Literal
then
1649 elsif Ekind
(E
) = E_Constant
then
1650 V
:= Constant_Value
(E
);
1651 return Present
(V
) and then Compile_Time_Known_Value
(V
);
1655 -- We have a value, see if it is compile time known
1658 -- Integer literals are worth storing in the cache
1660 if K
= N_Integer_Literal
then
1662 CV_Ent
.V
:= Intval
(Op
);
1665 -- Other literals and NULL are known at compile time
1668 Nkind_In
(K
, N_Character_Literal
,
1677 -- If we fall through, not known at compile time
1681 -- If we get an exception while trying to do this test, then some error
1682 -- has occurred, and we simply say that the value is not known after all
1687 end Compile_Time_Known_Value
;
1689 --------------------------------------
1690 -- Compile_Time_Known_Value_Or_Aggr --
1691 --------------------------------------
1693 function Compile_Time_Known_Value_Or_Aggr
(Op
: Node_Id
) return Boolean is
1695 -- If we have an entity name, then see if it is the name of a constant
1696 -- and if so, test the corresponding constant value, or the name of
1697 -- an enumeration literal, which is always a constant.
1699 if Is_Entity_Name
(Op
) then
1701 E
: constant Entity_Id
:= Entity
(Op
);
1705 if Ekind
(E
) = E_Enumeration_Literal
then
1708 elsif Ekind
(E
) /= E_Constant
then
1712 V
:= Constant_Value
(E
);
1714 and then Compile_Time_Known_Value_Or_Aggr
(V
);
1718 -- We have a value, see if it is compile time known
1721 if Compile_Time_Known_Value
(Op
) then
1724 elsif Nkind
(Op
) = N_Aggregate
then
1726 if Present
(Expressions
(Op
)) then
1730 Expr
:= First
(Expressions
(Op
));
1731 while Present
(Expr
) loop
1732 if not Compile_Time_Known_Value_Or_Aggr
(Expr
) then
1741 if Present
(Component_Associations
(Op
)) then
1746 Cass
:= First
(Component_Associations
(Op
));
1747 while Present
(Cass
) loop
1749 Compile_Time_Known_Value_Or_Aggr
(Expression
(Cass
))
1761 -- All other types of values are not known at compile time
1768 end Compile_Time_Known_Value_Or_Aggr
;
1770 ---------------------------------------
1771 -- CRT_Safe_Compile_Time_Known_Value --
1772 ---------------------------------------
1774 function CRT_Safe_Compile_Time_Known_Value
(Op
: Node_Id
) return Boolean is
1776 if (Configurable_Run_Time_Mode
or No_Run_Time_Mode
)
1777 and then not Is_OK_Static_Expression
(Op
)
1781 return Compile_Time_Known_Value
(Op
);
1783 end CRT_Safe_Compile_Time_Known_Value
;
1789 -- This is only called for actuals of functions that are not predefined
1790 -- operators (which have already been rewritten as operators at this
1791 -- stage), so the call can never be folded, and all that needs doing for
1792 -- the actual is to do the check for a non-static context.
1794 procedure Eval_Actual
(N
: Node_Id
) is
1796 Check_Non_Static_Context
(N
);
1799 --------------------
1800 -- Eval_Allocator --
1801 --------------------
1803 -- Allocators are never static, so all we have to do is to do the
1804 -- check for a non-static context if an expression is present.
1806 procedure Eval_Allocator
(N
: Node_Id
) is
1807 Expr
: constant Node_Id
:= Expression
(N
);
1809 if Nkind
(Expr
) = N_Qualified_Expression
then
1810 Check_Non_Static_Context
(Expression
(Expr
));
1814 ------------------------
1815 -- Eval_Arithmetic_Op --
1816 ------------------------
1818 -- Arithmetic operations are static functions, so the result is static
1819 -- if both operands are static (RM 4.9(7), 4.9(20)).
1821 procedure Eval_Arithmetic_Op
(N
: Node_Id
) is
1822 Left
: constant Node_Id
:= Left_Opnd
(N
);
1823 Right
: constant Node_Id
:= Right_Opnd
(N
);
1824 Ltype
: constant Entity_Id
:= Etype
(Left
);
1825 Rtype
: constant Entity_Id
:= Etype
(Right
);
1826 Otype
: Entity_Id
:= Empty
;
1831 -- If not foldable we are done
1833 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
1839 -- Otherwise attempt to fold
1841 if Is_Universal_Numeric_Type
(Etype
(Left
))
1843 Is_Universal_Numeric_Type
(Etype
(Right
))
1845 Otype
:= Find_Universal_Operator_Type
(N
);
1848 -- Fold for cases where both operands are of integer type
1850 if Is_Integer_Type
(Ltype
) and then Is_Integer_Type
(Rtype
) then
1852 Left_Int
: constant Uint
:= Expr_Value
(Left
);
1853 Right_Int
: constant Uint
:= Expr_Value
(Right
);
1859 Result
:= Left_Int
+ Right_Int
;
1861 when N_Op_Subtract
=>
1862 Result
:= Left_Int
- Right_Int
;
1864 when N_Op_Multiply
=>
1867 (Num_Bits
(Left_Int
) + Num_Bits
(Right_Int
)))
1869 Result
:= Left_Int
* Right_Int
;
1876 -- The exception Constraint_Error is raised by integer
1877 -- division, rem and mod if the right operand is zero.
1879 if Right_Int
= 0 then
1880 Apply_Compile_Time_Constraint_Error
1881 (N
, "division by zero", CE_Divide_By_Zero
,
1883 Set_Raises_Constraint_Error
(N
);
1886 -- Otherwise we can do the division
1889 Result
:= Left_Int
/ Right_Int
;
1894 -- The exception Constraint_Error is raised by integer
1895 -- division, rem and mod if the right operand is zero.
1897 if Right_Int
= 0 then
1898 Apply_Compile_Time_Constraint_Error
1899 (N
, "mod with zero divisor", CE_Divide_By_Zero
,
1903 Result
:= Left_Int
mod Right_Int
;
1908 -- The exception Constraint_Error is raised by integer
1909 -- division, rem and mod if the right operand is zero.
1911 if Right_Int
= 0 then
1912 Apply_Compile_Time_Constraint_Error
1913 (N
, "rem with zero divisor", CE_Divide_By_Zero
,
1918 Result
:= Left_Int
rem Right_Int
;
1922 raise Program_Error
;
1925 -- Adjust the result by the modulus if the type is a modular type
1927 if Is_Modular_Integer_Type
(Ltype
) then
1928 Result
:= Result
mod Modulus
(Ltype
);
1930 -- For a signed integer type, check non-static overflow
1932 elsif (not Stat
) and then Is_Signed_Integer_Type
(Ltype
) then
1934 BT
: constant Entity_Id
:= Base_Type
(Ltype
);
1935 Lo
: constant Uint
:= Expr_Value
(Type_Low_Bound
(BT
));
1936 Hi
: constant Uint
:= Expr_Value
(Type_High_Bound
(BT
));
1938 if Result
< Lo
or else Result
> Hi
then
1939 Apply_Compile_Time_Constraint_Error
1940 (N
, "value not in range of }??",
1941 CE_Overflow_Check_Failed
,
1948 -- If we get here we can fold the result
1950 Fold_Uint
(N
, Result
, Stat
);
1953 -- Cases where at least one operand is a real. We handle the cases of
1954 -- both reals, or mixed/real integer cases (the latter happen only for
1955 -- divide and multiply, and the result is always real).
1957 elsif Is_Real_Type
(Ltype
) or else Is_Real_Type
(Rtype
) then
1964 if Is_Real_Type
(Ltype
) then
1965 Left_Real
:= Expr_Value_R
(Left
);
1967 Left_Real
:= UR_From_Uint
(Expr_Value
(Left
));
1970 if Is_Real_Type
(Rtype
) then
1971 Right_Real
:= Expr_Value_R
(Right
);
1973 Right_Real
:= UR_From_Uint
(Expr_Value
(Right
));
1976 if Nkind
(N
) = N_Op_Add
then
1977 Result
:= Left_Real
+ Right_Real
;
1979 elsif Nkind
(N
) = N_Op_Subtract
then
1980 Result
:= Left_Real
- Right_Real
;
1982 elsif Nkind
(N
) = N_Op_Multiply
then
1983 Result
:= Left_Real
* Right_Real
;
1985 else pragma Assert
(Nkind
(N
) = N_Op_Divide
);
1986 if UR_Is_Zero
(Right_Real
) then
1987 Apply_Compile_Time_Constraint_Error
1988 (N
, "division by zero", CE_Divide_By_Zero
);
1992 Result
:= Left_Real
/ Right_Real
;
1995 Fold_Ureal
(N
, Result
, Stat
);
1999 -- If the operator was resolved to a specific type, make sure that type
2000 -- is frozen even if the expression is folded into a literal (which has
2001 -- a universal type).
2003 if Present
(Otype
) then
2004 Freeze_Before
(N
, Otype
);
2006 end Eval_Arithmetic_Op
;
2008 ----------------------------
2009 -- Eval_Character_Literal --
2010 ----------------------------
2012 -- Nothing to be done
2014 procedure Eval_Character_Literal
(N
: Node_Id
) is
2015 pragma Warnings
(Off
, N
);
2018 end Eval_Character_Literal
;
2024 -- Static function calls are either calls to predefined operators
2025 -- with static arguments, or calls to functions that rename a literal.
2026 -- Only the latter case is handled here, predefined operators are
2027 -- constant-folded elsewhere.
2029 -- If the function is itself inherited (see 7423-001) the literal of
2030 -- the parent type must be explicitly converted to the return type
2033 procedure Eval_Call
(N
: Node_Id
) is
2034 Loc
: constant Source_Ptr
:= Sloc
(N
);
2035 Typ
: constant Entity_Id
:= Etype
(N
);
2039 if Nkind
(N
) = N_Function_Call
2040 and then No
(Parameter_Associations
(N
))
2041 and then Is_Entity_Name
(Name
(N
))
2042 and then Present
(Alias
(Entity
(Name
(N
))))
2043 and then Is_Enumeration_Type
(Base_Type
(Typ
))
2045 Lit
:= Ultimate_Alias
(Entity
(Name
(N
)));
2047 if Ekind
(Lit
) = E_Enumeration_Literal
then
2048 if Base_Type
(Etype
(Lit
)) /= Base_Type
(Typ
) then
2050 (N
, Convert_To
(Typ
, New_Occurrence_Of
(Lit
, Loc
)));
2052 Rewrite
(N
, New_Occurrence_Of
(Lit
, Loc
));
2060 --------------------------
2061 -- Eval_Case_Expression --
2062 --------------------------
2064 -- A conditional expression is static if all its conditions and dependent
2065 -- expressions are static. Note that we do not care if the dependent
2066 -- expressions raise CE, except for the one that will be selected.
2068 procedure Eval_Case_Expression
(N
: Node_Id
) is
2073 Set_Is_Static_Expression
(N
, False);
2075 if not Is_Static_Expression
(Expression
(N
)) then
2076 Check_Non_Static_Context
(Expression
(N
));
2080 -- First loop, make sure all the alternatives are static expressions
2081 -- none of which raise Constraint_Error. We make the constraint error
2082 -- check because part of the legality condition for a correct static
2083 -- case expression is that the cases are covered, like any other case
2084 -- expression. And we can't do that if any of the conditions raise an
2085 -- exception, so we don't even try to evaluate if that is the case.
2087 Alt
:= First
(Alternatives
(N
));
2088 while Present
(Alt
) loop
2090 -- The expression must be static, but we don't care at this stage
2091 -- if it raises Constraint_Error (the alternative might not match,
2092 -- in which case the expression is statically unevaluated anyway).
2094 if not Is_Static_Expression
(Expression
(Alt
)) then
2095 Check_Non_Static_Context
(Expression
(Alt
));
2099 -- The choices of a case always have to be static, and cannot raise
2100 -- an exception. If this condition is not met, then the expression
2101 -- is plain illegal, so just abandon evaluation attempts. No need
2102 -- to check non-static context when we have something illegal anyway.
2104 if not Is_OK_Static_Choice_List
(Discrete_Choices
(Alt
)) then
2111 -- OK, if the above loop gets through it means that all choices are OK
2112 -- static (don't raise exceptions), so the whole case is static, and we
2113 -- can find the matching alternative.
2115 Set_Is_Static_Expression
(N
);
2117 -- Now to deal with propagating a possible constraint error
2119 -- If the selecting expression raises CE, propagate and we are done
2121 if Raises_Constraint_Error
(Expression
(N
)) then
2122 Set_Raises_Constraint_Error
(N
);
2124 -- Otherwise we need to check the alternatives to find the matching
2125 -- one. CE's in other than the matching one are not relevant. But we
2126 -- do need to check the matching one. Unlike the first loop, we do not
2127 -- have to go all the way through, when we find the matching one, quit.
2130 Alt
:= First
(Alternatives
(N
));
2133 -- We must find a match among the alternatives. If not, this must
2134 -- be due to other errors, so just ignore, leaving as non-static.
2137 Set_Is_Static_Expression
(N
, False);
2141 -- Otherwise loop through choices of this alternative
2143 Choice
:= First
(Discrete_Choices
(Alt
));
2144 while Present
(Choice
) loop
2146 -- If we find a matching choice, then the Expression of this
2147 -- alternative replaces N (Raises_Constraint_Error flag is
2148 -- included, so we don't have to special case that).
2150 if Choice_Matches
(Expression
(N
), Choice
) = Match
then
2151 Rewrite
(N
, Relocate_Node
(Expression
(Alt
)));
2161 end Eval_Case_Expression
;
2163 ------------------------
2164 -- Eval_Concatenation --
2165 ------------------------
2167 -- Concatenation is a static function, so the result is static if both
2168 -- operands are static (RM 4.9(7), 4.9(21)).
2170 procedure Eval_Concatenation
(N
: Node_Id
) is
2171 Left
: constant Node_Id
:= Left_Opnd
(N
);
2172 Right
: constant Node_Id
:= Right_Opnd
(N
);
2173 C_Typ
: constant Entity_Id
:= Root_Type
(Component_Type
(Etype
(N
)));
2178 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
2179 -- non-static context.
2181 if Ada_Version
= Ada_83
2182 and then Comes_From_Source
(N
)
2184 Check_Non_Static_Context
(Left
);
2185 Check_Non_Static_Context
(Right
);
2189 -- If not foldable we are done. In principle concatenation that yields
2190 -- any string type is static (i.e. an array type of character types).
2191 -- However, character types can include enumeration literals, and
2192 -- concatenation in that case cannot be described by a literal, so we
2193 -- only consider the operation static if the result is an array of
2194 -- (a descendant of) a predefined character type.
2196 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2198 if not (Is_Standard_Character_Type
(C_Typ
) and then Fold
) then
2199 Set_Is_Static_Expression
(N
, False);
2203 -- Compile time string concatenation
2205 -- ??? Note that operands that are aggregates can be marked as static,
2206 -- so we should attempt at a later stage to fold concatenations with
2210 Left_Str
: constant Node_Id
:= Get_String_Val
(Left
);
2212 Right_Str
: constant Node_Id
:= Get_String_Val
(Right
);
2213 Folded_Val
: String_Id
;
2216 -- Establish new string literal, and store left operand. We make
2217 -- sure to use the special Start_String that takes an operand if
2218 -- the left operand is a string literal. Since this is optimized
2219 -- in the case where that is the most recently created string
2220 -- literal, we ensure efficient time/space behavior for the
2221 -- case of a concatenation of a series of string literals.
2223 if Nkind
(Left_Str
) = N_String_Literal
then
2224 Left_Len
:= String_Length
(Strval
(Left_Str
));
2226 -- If the left operand is the empty string, and the right operand
2227 -- is a string literal (the case of "" & "..."), the result is the
2228 -- value of the right operand. This optimization is important when
2229 -- Is_Folded_In_Parser, to avoid copying an enormous right
2232 if Left_Len
= 0 and then Nkind
(Right_Str
) = N_String_Literal
then
2233 Folded_Val
:= Strval
(Right_Str
);
2235 Start_String
(Strval
(Left_Str
));
2240 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Left_Str
)));
2244 -- Now append the characters of the right operand, unless we
2245 -- optimized the "" & "..." case above.
2247 if Nkind
(Right_Str
) = N_String_Literal
then
2248 if Left_Len
/= 0 then
2249 Store_String_Chars
(Strval
(Right_Str
));
2250 Folded_Val
:= End_String
;
2253 Store_String_Char
(UI_To_CC
(Char_Literal_Value
(Right_Str
)));
2254 Folded_Val
:= End_String
;
2257 Set_Is_Static_Expression
(N
, Stat
);
2259 -- If left operand is the empty string, the result is the
2260 -- right operand, including its bounds if anomalous.
2263 and then Is_Array_Type
(Etype
(Right
))
2264 and then Etype
(Right
) /= Any_String
2266 Set_Etype
(N
, Etype
(Right
));
2269 Fold_Str
(N
, Folded_Val
, Static
=> Stat
);
2271 end Eval_Concatenation
;
2273 ----------------------
2274 -- Eval_Entity_Name --
2275 ----------------------
2277 -- This procedure is used for identifiers and expanded names other than
2278 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
2279 -- static if they denote a static constant (RM 4.9(6)) or if the name
2280 -- denotes an enumeration literal (RM 4.9(22)).
2282 procedure Eval_Entity_Name
(N
: Node_Id
) is
2283 Def_Id
: constant Entity_Id
:= Entity
(N
);
2287 -- Enumeration literals are always considered to be constants
2288 -- and cannot raise constraint error (RM 4.9(22)).
2290 if Ekind
(Def_Id
) = E_Enumeration_Literal
then
2291 Set_Is_Static_Expression
(N
);
2294 -- A name is static if it denotes a static constant (RM 4.9(5)), and
2295 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
2296 -- it does not violate 10.2.1(8) here, since this is not a variable.
2298 elsif Ekind
(Def_Id
) = E_Constant
then
2300 -- Deferred constants must always be treated as nonstatic outside the
2301 -- scope of their full view.
2303 if Present
(Full_View
(Def_Id
))
2304 and then not In_Open_Scopes
(Scope
(Def_Id
))
2308 Val
:= Constant_Value
(Def_Id
);
2311 if Present
(Val
) then
2312 Set_Is_Static_Expression
2313 (N
, Is_Static_Expression
(Val
)
2314 and then Is_Static_Subtype
(Etype
(Def_Id
)));
2315 Set_Raises_Constraint_Error
(N
, Raises_Constraint_Error
(Val
));
2317 if not Is_Static_Expression
(N
)
2318 and then not Is_Generic_Type
(Etype
(N
))
2320 Validate_Static_Object_Name
(N
);
2323 -- Mark constant condition in SCOs
2326 and then Comes_From_Source
(N
)
2327 and then Is_Boolean_Type
(Etype
(Def_Id
))
2328 and then Compile_Time_Known_Value
(N
)
2330 Set_SCO_Condition
(N
, Expr_Value_E
(N
) = Standard_True
);
2337 -- Fall through if the name is not static
2339 Validate_Static_Object_Name
(N
);
2340 end Eval_Entity_Name
;
2342 ------------------------
2343 -- Eval_If_Expression --
2344 ------------------------
2346 -- We can fold to a static expression if the condition and both dependent
2347 -- expressions are static. Otherwise, the only required processing is to do
2348 -- the check for non-static context for the then and else expressions.
2350 procedure Eval_If_Expression
(N
: Node_Id
) is
2351 Condition
: constant Node_Id
:= First
(Expressions
(N
));
2352 Then_Expr
: constant Node_Id
:= Next
(Condition
);
2353 Else_Expr
: constant Node_Id
:= Next
(Then_Expr
);
2355 Non_Result
: Node_Id
;
2357 Rstat
: constant Boolean :=
2358 Is_Static_Expression
(Condition
)
2360 Is_Static_Expression
(Then_Expr
)
2362 Is_Static_Expression
(Else_Expr
);
2363 -- True if result is static
2366 -- If result not static, nothing to do, otherwise set static result
2371 Set_Is_Static_Expression
(N
);
2374 -- If any operand is Any_Type, just propagate to result and do not try
2375 -- to fold, this prevents cascaded errors.
2377 if Etype
(Condition
) = Any_Type
or else
2378 Etype
(Then_Expr
) = Any_Type
or else
2379 Etype
(Else_Expr
) = Any_Type
2381 Set_Etype
(N
, Any_Type
);
2382 Set_Is_Static_Expression
(N
, False);
2386 -- If condition raises constraint error then we have already signaled
2387 -- an error, and we just propagate to the result and do not fold.
2389 if Raises_Constraint_Error
(Condition
) then
2390 Set_Raises_Constraint_Error
(N
);
2394 -- Static case where we can fold. Note that we don't try to fold cases
2395 -- where the condition is known at compile time, but the result is
2396 -- non-static. This avoids possible cases of infinite recursion where
2397 -- the expander puts in a redundant test and we remove it. Instead we
2398 -- deal with these cases in the expander.
2400 -- Select result operand
2402 if Is_True
(Expr_Value
(Condition
)) then
2403 Result
:= Then_Expr
;
2404 Non_Result
:= Else_Expr
;
2406 Result
:= Else_Expr
;
2407 Non_Result
:= Then_Expr
;
2410 -- Note that it does not matter if the non-result operand raises a
2411 -- Constraint_Error, but if the result raises constraint error then we
2412 -- replace the node with a raise constraint error. This will properly
2413 -- propagate Raises_Constraint_Error since this flag is set in Result.
2415 if Raises_Constraint_Error
(Result
) then
2416 Rewrite_In_Raise_CE
(N
, Result
);
2417 Check_Non_Static_Context
(Non_Result
);
2419 -- Otherwise the result operand replaces the original node
2422 Rewrite
(N
, Relocate_Node
(Result
));
2423 Set_Is_Static_Expression
(N
);
2425 end Eval_If_Expression
;
2427 ----------------------------
2428 -- Eval_Indexed_Component --
2429 ----------------------------
2431 -- Indexed components are never static, so we need to perform the check
2432 -- for non-static context on the index values. Then, we check if the
2433 -- value can be obtained at compile time, even though it is non-static.
2435 procedure Eval_Indexed_Component
(N
: Node_Id
) is
2439 -- Check for non-static context on index values
2441 Expr
:= First
(Expressions
(N
));
2442 while Present
(Expr
) loop
2443 Check_Non_Static_Context
(Expr
);
2447 -- If the indexed component appears in an object renaming declaration
2448 -- then we do not want to try to evaluate it, since in this case we
2449 -- need the identity of the array element.
2451 if Nkind
(Parent
(N
)) = N_Object_Renaming_Declaration
then
2454 -- Similarly if the indexed component appears as the prefix of an
2455 -- attribute we don't want to evaluate it, because at least for
2456 -- some cases of attributes we need the identify (e.g. Access, Size)
2458 elsif Nkind
(Parent
(N
)) = N_Attribute_Reference
then
2462 -- Note: there are other cases, such as the left side of an assignment,
2463 -- or an OUT parameter for a call, where the replacement results in the
2464 -- illegal use of a constant, But these cases are illegal in the first
2465 -- place, so the replacement, though silly, is harmless.
2467 -- Now see if this is a constant array reference
2469 if List_Length
(Expressions
(N
)) = 1
2470 and then Is_Entity_Name
(Prefix
(N
))
2471 and then Ekind
(Entity
(Prefix
(N
))) = E_Constant
2472 and then Present
(Constant_Value
(Entity
(Prefix
(N
))))
2475 Loc
: constant Source_Ptr
:= Sloc
(N
);
2476 Arr
: constant Node_Id
:= Constant_Value
(Entity
(Prefix
(N
)));
2477 Sub
: constant Node_Id
:= First
(Expressions
(N
));
2483 -- Linear one's origin subscript value for array reference
2486 -- Lower bound of the first array index
2489 -- Value from constant array
2492 Atyp
:= Etype
(Arr
);
2494 if Is_Access_Type
(Atyp
) then
2495 Atyp
:= Designated_Type
(Atyp
);
2498 -- If we have an array type (we should have but perhaps there are
2499 -- error cases where this is not the case), then see if we can do
2500 -- a constant evaluation of the array reference.
2502 if Is_Array_Type
(Atyp
) and then Atyp
/= Any_Composite
then
2503 if Ekind
(Atyp
) = E_String_Literal_Subtype
then
2504 Lbd
:= String_Literal_Low_Bound
(Atyp
);
2506 Lbd
:= Type_Low_Bound
(Etype
(First_Index
(Atyp
)));
2509 if Compile_Time_Known_Value
(Sub
)
2510 and then Nkind
(Arr
) = N_Aggregate
2511 and then Compile_Time_Known_Value
(Lbd
)
2512 and then Is_Discrete_Type
(Component_Type
(Atyp
))
2514 Lin
:= UI_To_Int
(Expr_Value
(Sub
) - Expr_Value
(Lbd
)) + 1;
2516 if List_Length
(Expressions
(Arr
)) >= Lin
then
2517 Elm
:= Pick
(Expressions
(Arr
), Lin
);
2519 -- If the resulting expression is compile time known,
2520 -- then we can rewrite the indexed component with this
2521 -- value, being sure to mark the result as non-static.
2522 -- We also reset the Sloc, in case this generates an
2523 -- error later on (e.g. 136'Access).
2525 if Compile_Time_Known_Value
(Elm
) then
2526 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2527 Set_Is_Static_Expression
(N
, False);
2532 -- We can also constant-fold if the prefix is a string literal.
2533 -- This will be useful in an instantiation or an inlining.
2535 elsif Compile_Time_Known_Value
(Sub
)
2536 and then Nkind
(Arr
) = N_String_Literal
2537 and then Compile_Time_Known_Value
(Lbd
)
2538 and then Expr_Value
(Lbd
) = 1
2539 and then Expr_Value
(Sub
) <=
2540 String_Literal_Length
(Etype
(Arr
))
2543 C
: constant Char_Code
:=
2544 Get_String_Char
(Strval
(Arr
),
2545 UI_To_Int
(Expr_Value
(Sub
)));
2547 Set_Character_Literal_Name
(C
);
2550 Make_Character_Literal
(Loc
,
2552 Char_Literal_Value
=> UI_From_CC
(C
));
2553 Set_Etype
(Elm
, Component_Type
(Atyp
));
2554 Rewrite
(N
, Duplicate_Subexpr_No_Checks
(Elm
));
2555 Set_Is_Static_Expression
(N
, False);
2561 end Eval_Indexed_Component
;
2563 --------------------------
2564 -- Eval_Integer_Literal --
2565 --------------------------
2567 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2568 -- as static by the analyzer. The reason we did it that early is to allow
2569 -- the possibility of turning off the Is_Static_Expression flag after
2570 -- analysis, but before resolution, when integer literals are generated in
2571 -- the expander that do not correspond to static expressions.
2573 procedure Eval_Integer_Literal
(N
: Node_Id
) is
2574 T
: constant Entity_Id
:= Etype
(N
);
2576 function In_Any_Integer_Context
return Boolean;
2577 -- If the literal is resolved with a specific type in a context where
2578 -- the expected type is Any_Integer, there are no range checks on the
2579 -- literal. By the time the literal is evaluated, it carries the type
2580 -- imposed by the enclosing expression, and we must recover the context
2581 -- to determine that Any_Integer is meant.
2583 ----------------------------
2584 -- In_Any_Integer_Context --
2585 ----------------------------
2587 function In_Any_Integer_Context
return Boolean is
2588 Par
: constant Node_Id
:= Parent
(N
);
2589 K
: constant Node_Kind
:= Nkind
(Par
);
2592 -- Any_Integer also appears in digits specifications for real types,
2593 -- but those have bounds smaller that those of any integer base type,
2594 -- so we can safely ignore these cases.
2596 return Nkind_In
(K
, N_Number_Declaration
,
2597 N_Attribute_Reference
,
2598 N_Attribute_Definition_Clause
,
2599 N_Modular_Type_Definition
,
2600 N_Signed_Integer_Type_Definition
);
2601 end In_Any_Integer_Context
;
2603 -- Start of processing for Eval_Integer_Literal
2607 -- If the literal appears in a non-expression context, then it is
2608 -- certainly appearing in a non-static context, so check it. This is
2609 -- actually a redundant check, since Check_Non_Static_Context would
2610 -- check it, but it seems worth while avoiding the call.
2612 if Nkind
(Parent
(N
)) not in N_Subexpr
2613 and then not In_Any_Integer_Context
2615 Check_Non_Static_Context
(N
);
2618 -- Modular integer literals must be in their base range
2620 if Is_Modular_Integer_Type
(T
)
2621 and then Is_Out_Of_Range
(N
, Base_Type
(T
), Assume_Valid
=> True)
2625 end Eval_Integer_Literal
;
2627 ---------------------
2628 -- Eval_Logical_Op --
2629 ---------------------
2631 -- Logical operations are static functions, so the result is potentially
2632 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2634 procedure Eval_Logical_Op
(N
: Node_Id
) is
2635 Left
: constant Node_Id
:= Left_Opnd
(N
);
2636 Right
: constant Node_Id
:= Right_Opnd
(N
);
2641 -- If not foldable we are done
2643 Test_Expression_Is_Foldable
(N
, Left
, Right
, Stat
, Fold
);
2649 -- Compile time evaluation of logical operation
2652 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2653 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2656 if Is_Modular_Integer_Type
(Etype
(N
)) then
2658 Left_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2659 Right_Bits
: Bits
(0 .. UI_To_Int
(Esize
(Etype
(N
))) - 1);
2662 To_Bits
(Left_Int
, Left_Bits
);
2663 To_Bits
(Right_Int
, Right_Bits
);
2665 -- Note: should really be able to use array ops instead of
2666 -- these loops, but they weren't working at the time ???
2668 if Nkind
(N
) = N_Op_And
then
2669 for J
in Left_Bits
'Range loop
2670 Left_Bits
(J
) := Left_Bits
(J
) and Right_Bits
(J
);
2673 elsif Nkind
(N
) = N_Op_Or
then
2674 for J
in Left_Bits
'Range loop
2675 Left_Bits
(J
) := Left_Bits
(J
) or Right_Bits
(J
);
2679 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2681 for J
in Left_Bits
'Range loop
2682 Left_Bits
(J
) := Left_Bits
(J
) xor Right_Bits
(J
);
2686 Fold_Uint
(N
, From_Bits
(Left_Bits
, Etype
(N
)), Stat
);
2690 pragma Assert
(Is_Boolean_Type
(Etype
(N
)));
2692 if Nkind
(N
) = N_Op_And
then
2694 Test
(Is_True
(Left_Int
) and then Is_True
(Right_Int
)), Stat
);
2696 elsif Nkind
(N
) = N_Op_Or
then
2698 Test
(Is_True
(Left_Int
) or else Is_True
(Right_Int
)), Stat
);
2701 pragma Assert
(Nkind
(N
) = N_Op_Xor
);
2703 Test
(Is_True
(Left_Int
) xor Is_True
(Right_Int
)), Stat
);
2707 end Eval_Logical_Op
;
2709 ------------------------
2710 -- Eval_Membership_Op --
2711 ------------------------
2713 -- A membership test is potentially static if the expression is static, and
2714 -- the range is a potentially static range, or is a subtype mark denoting a
2715 -- static subtype (RM 4.9(12)).
2717 procedure Eval_Membership_Op
(N
: Node_Id
) is
2718 Left
: constant Node_Id
:= Left_Opnd
(N
);
2719 Right
: constant Node_Id
:= Right_Opnd
(N
);
2720 Alts
: constant List_Id
:= Alternatives
(N
);
2721 Result
: Match_Result
;
2724 -- Ignore if error in either operand, except to make sure that Any_Type
2725 -- is properly propagated to avoid junk cascaded errors.
2727 if Etype
(Left
) = Any_Type
2728 or else (Present
(Right
) and then Etype
(Right
) = Any_Type
)
2730 Set_Etype
(N
, Any_Type
);
2734 -- Ignore if types involved have predicates
2735 -- Is this right for static predicates ???
2736 -- And what about the alternatives ???
2738 if Present
(Predicate_Function
(Etype
(Left
)))
2739 or else (Present
(Right
)
2740 and then Present
(Predicate_Function
(Etype
(Right
))))
2745 -- If left operand non-static, then nothing to do
2747 if not Is_Static_Expression
(Left
) then
2751 -- If choice is non-static, left operand is in non-static context
2753 if (Present
(Right
) and then not Is_Static_Choice
(Right
))
2754 or else (Present
(Alts
) and then not Is_Static_Choice_List
(Alts
))
2756 Check_Non_Static_Context
(Left
);
2760 -- Otherwise we definitely have a static expression
2762 Set_Is_Static_Expression
(N
);
2764 -- If left operand raises constraint error, propagate and we are done
2766 if Raises_Constraint_Error
(Left
) then
2767 Set_Raises_Constraint_Error
(N
, True);
2772 if Present
(Right
) then
2773 Result
:= Choice_Matches
(Left
, Right
);
2775 Result
:= Choices_Match
(Left
, Alts
);
2778 -- If result is Non_Static, it means that we raise Constraint_Error,
2779 -- since we already tested that the operands were themselves static.
2781 if Result
= Non_Static
then
2782 Set_Raises_Constraint_Error
(N
);
2784 -- Otherwise we have our result (flipped if NOT IN case)
2788 (N
, Test
((Result
= Match
) xor (Nkind
(N
) = N_Not_In
)), True);
2789 Warn_On_Known_Condition
(N
);
2792 end Eval_Membership_Op
;
2794 ------------------------
2795 -- Eval_Named_Integer --
2796 ------------------------
2798 procedure Eval_Named_Integer
(N
: Node_Id
) is
2801 Expr_Value
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2802 end Eval_Named_Integer
;
2804 ---------------------
2805 -- Eval_Named_Real --
2806 ---------------------
2808 procedure Eval_Named_Real
(N
: Node_Id
) is
2811 Expr_Value_R
(Expression
(Declaration_Node
(Entity
(N
)))), True);
2812 end Eval_Named_Real
;
2818 -- Exponentiation is a static functions, so the result is potentially
2819 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2821 procedure Eval_Op_Expon
(N
: Node_Id
) is
2822 Left
: constant Node_Id
:= Left_Opnd
(N
);
2823 Right
: constant Node_Id
:= Right_Opnd
(N
);
2828 -- If not foldable we are done
2830 Test_Expression_Is_Foldable
2831 (N
, Left
, Right
, Stat
, Fold
, CRT_Safe
=> True);
2833 -- Return if not foldable
2839 if Configurable_Run_Time_Mode
and not Stat
then
2843 -- Fold exponentiation operation
2846 Right_Int
: constant Uint
:= Expr_Value
(Right
);
2851 if Is_Integer_Type
(Etype
(Left
)) then
2853 Left_Int
: constant Uint
:= Expr_Value
(Left
);
2857 -- Exponentiation of an integer raises Constraint_Error for a
2858 -- negative exponent (RM 4.5.6).
2860 if Right_Int
< 0 then
2861 Apply_Compile_Time_Constraint_Error
2862 (N
, "integer exponent negative", CE_Range_Check_Failed
,
2867 if OK_Bits
(N
, Num_Bits
(Left_Int
) * Right_Int
) then
2868 Result
:= Left_Int
** Right_Int
;
2873 if Is_Modular_Integer_Type
(Etype
(N
)) then
2874 Result
:= Result
mod Modulus
(Etype
(N
));
2877 Fold_Uint
(N
, Result
, Stat
);
2885 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
2888 -- Cannot have a zero base with a negative exponent
2890 if UR_Is_Zero
(Left_Real
) then
2892 if Right_Int
< 0 then
2893 Apply_Compile_Time_Constraint_Error
2894 (N
, "zero ** negative integer", CE_Range_Check_Failed
,
2898 Fold_Ureal
(N
, Ureal_0
, Stat
);
2902 Fold_Ureal
(N
, Left_Real
** Right_Int
, Stat
);
2913 -- The not operation is a static functions, so the result is potentially
2914 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2916 procedure Eval_Op_Not
(N
: Node_Id
) is
2917 Right
: constant Node_Id
:= Right_Opnd
(N
);
2922 -- If not foldable we are done
2924 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
2930 -- Fold not operation
2933 Rint
: constant Uint
:= Expr_Value
(Right
);
2934 Typ
: constant Entity_Id
:= Etype
(N
);
2937 -- Negation is equivalent to subtracting from the modulus minus one.
2938 -- For a binary modulus this is equivalent to the ones-complement of
2939 -- the original value. For a nonbinary modulus this is an arbitrary
2940 -- but consistent definition.
2942 if Is_Modular_Integer_Type
(Typ
) then
2943 Fold_Uint
(N
, Modulus
(Typ
) - 1 - Rint
, Stat
);
2944 else pragma Assert
(Is_Boolean_Type
(Typ
));
2945 Fold_Uint
(N
, Test
(not Is_True
(Rint
)), Stat
);
2948 Set_Is_Static_Expression
(N
, Stat
);
2952 -------------------------------
2953 -- Eval_Qualified_Expression --
2954 -------------------------------
2956 -- A qualified expression is potentially static if its subtype mark denotes
2957 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2959 procedure Eval_Qualified_Expression
(N
: Node_Id
) is
2960 Operand
: constant Node_Id
:= Expression
(N
);
2961 Target_Type
: constant Entity_Id
:= Entity
(Subtype_Mark
(N
));
2968 -- Can only fold if target is string or scalar and subtype is static.
2969 -- Also, do not fold if our parent is an allocator (this is because the
2970 -- qualified expression is really part of the syntactic structure of an
2971 -- allocator, and we do not want to end up with something that
2972 -- corresponds to "new 1" where the 1 is the result of folding a
2973 -- qualified expression).
2975 if not Is_Static_Subtype
(Target_Type
)
2976 or else Nkind
(Parent
(N
)) = N_Allocator
2978 Check_Non_Static_Context
(Operand
);
2980 -- If operand is known to raise constraint_error, set the flag on the
2981 -- expression so it does not get optimized away.
2983 if Nkind
(Operand
) = N_Raise_Constraint_Error
then
2984 Set_Raises_Constraint_Error
(N
);
2990 -- If not foldable we are done
2992 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
2997 -- Don't try fold if target type has constraint error bounds
2999 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3000 Set_Raises_Constraint_Error
(N
);
3004 -- Here we will fold, save Print_In_Hex indication
3006 Hex
:= Nkind
(Operand
) = N_Integer_Literal
3007 and then Print_In_Hex
(Operand
);
3009 -- Fold the result of qualification
3011 if Is_Discrete_Type
(Target_Type
) then
3012 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3014 -- Preserve Print_In_Hex indication
3016 if Hex
and then Nkind
(N
) = N_Integer_Literal
then
3017 Set_Print_In_Hex
(N
);
3020 elsif Is_Real_Type
(Target_Type
) then
3021 Fold_Ureal
(N
, Expr_Value_R
(Operand
), Stat
);
3024 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Stat
);
3027 Set_Is_Static_Expression
(N
, False);
3029 Check_String_Literal_Length
(N
, Target_Type
);
3035 -- The expression may be foldable but not static
3037 Set_Is_Static_Expression
(N
, Stat
);
3039 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3042 end Eval_Qualified_Expression
;
3044 -----------------------
3045 -- Eval_Real_Literal --
3046 -----------------------
3048 -- Numeric literals are static (RM 4.9(1)), and have already been marked
3049 -- as static by the analyzer. The reason we did it that early is to allow
3050 -- the possibility of turning off the Is_Static_Expression flag after
3051 -- analysis, but before resolution, when integer literals are generated
3052 -- in the expander that do not correspond to static expressions.
3054 procedure Eval_Real_Literal
(N
: Node_Id
) is
3055 PK
: constant Node_Kind
:= Nkind
(Parent
(N
));
3058 -- If the literal appears in a non-expression context and not as part of
3059 -- a number declaration, then it is appearing in a non-static context,
3062 if PK
not in N_Subexpr
and then PK
/= N_Number_Declaration
then
3063 Check_Non_Static_Context
(N
);
3065 end Eval_Real_Literal
;
3067 ------------------------
3068 -- Eval_Relational_Op --
3069 ------------------------
3071 -- Relational operations are static functions, so the result is static if
3072 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
3073 -- the result is never static, even if the operands are.
3075 -- However, for internally generated nodes, we allow string equality and
3076 -- inequality to be static. This is because we rewrite A in "ABC" as an
3077 -- equality test A = "ABC", and the former is definitely static.
3079 procedure Eval_Relational_Op
(N
: Node_Id
) is
3080 Left
: constant Node_Id
:= Left_Opnd
(N
);
3081 Right
: constant Node_Id
:= Right_Opnd
(N
);
3082 Typ
: constant Entity_Id
:= Etype
(Left
);
3083 Otype
: Entity_Id
:= Empty
;
3087 -- One special case to deal with first. If we can tell that the result
3088 -- will be false because the lengths of one or more index subtypes are
3089 -- compile time known and different, then we can replace the entire
3090 -- result by False. We only do this for one dimensional arrays, because
3091 -- the case of multi-dimensional arrays is rare and too much trouble. If
3092 -- one of the operands is an illegal aggregate, its type might still be
3093 -- an arbitrary composite type, so nothing to do.
3095 if Is_Array_Type
(Typ
)
3096 and then Typ
/= Any_Composite
3097 and then Number_Dimensions
(Typ
) = 1
3098 and then (Nkind
(N
) = N_Op_Eq
or else Nkind
(N
) = N_Op_Ne
)
3100 if Raises_Constraint_Error
(Left
)
3102 Raises_Constraint_Error
(Right
)
3107 -- OK, we have the case where we may be able to do this fold
3109 Length_Mismatch
: declare
3110 procedure Get_Static_Length
(Op
: Node_Id
; Len
: out Uint
);
3111 -- If Op is an expression for a constrained array with a known at
3112 -- compile time length, then Len is set to this (non-negative
3113 -- length). Otherwise Len is set to minus 1.
3115 -----------------------
3116 -- Get_Static_Length --
3117 -----------------------
3119 procedure Get_Static_Length
(Op
: Node_Id
; Len
: out Uint
) is
3123 -- First easy case string literal
3125 if Nkind
(Op
) = N_String_Literal
then
3126 Len
:= UI_From_Int
(String_Length
(Strval
(Op
)));
3130 -- Second easy case, not constrained subtype, so no length
3132 if not Is_Constrained
(Etype
(Op
)) then
3133 Len
:= Uint_Minus_1
;
3139 T
:= Etype
(First_Index
(Etype
(Op
)));
3141 -- The simple case, both bounds are known at compile time
3143 if Is_Discrete_Type
(T
)
3144 and then Compile_Time_Known_Value
(Type_Low_Bound
(T
))
3145 and then Compile_Time_Known_Value
(Type_High_Bound
(T
))
3147 Len
:= UI_Max
(Uint_0
,
3148 Expr_Value
(Type_High_Bound
(T
)) -
3149 Expr_Value
(Type_Low_Bound
(T
)) + 1);
3153 -- A more complex case, where the bounds are of the form
3154 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
3155 -- either A'First or A'Last (with A an entity name), or X is an
3156 -- entity name, and the two X's are the same and K1 and K2 are
3157 -- known at compile time, in this case, the length can also be
3158 -- computed at compile time, even though the bounds are not
3159 -- known. A common case of this is e.g. (X'First .. X'First+5).
3161 Extract_Length
: declare
3162 procedure Decompose_Expr
3164 Ent
: out Entity_Id
;
3165 Kind
: out Character;
3167 Orig
: Boolean := True);
3168 -- Given an expression see if it is of the form given above,
3169 -- X [+/- K]. If so Ent is set to the entity in X, Kind is
3170 -- 'F','L','E' for 'First/'Last/simple entity, and Cons is
3171 -- the value of K. If the expression is not of the required
3172 -- form, Ent is set to Empty.
3174 -- Orig indicates whether Expr is the original expression
3175 -- to consider, or if we are handling a sub-expression
3176 -- (e.g. recursive call to Decompose_Expr).
3178 --------------------
3179 -- Decompose_Expr --
3180 --------------------
3182 procedure Decompose_Expr
3184 Ent
: out Entity_Id
;
3185 Kind
: out Character;
3187 Orig
: Boolean := True)
3194 if Nkind
(Expr
) = N_Op_Add
3195 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3197 Exp
:= Left_Opnd
(Expr
);
3198 Cons
:= Expr_Value
(Right_Opnd
(Expr
));
3200 elsif Nkind
(Expr
) = N_Op_Subtract
3201 and then Compile_Time_Known_Value
(Right_Opnd
(Expr
))
3203 Exp
:= Left_Opnd
(Expr
);
3204 Cons
:= -Expr_Value
(Right_Opnd
(Expr
));
3206 -- If the bound is a constant created to remove side
3207 -- effects, recover original expression to see if it has
3208 -- one of the recognizable forms.
3210 elsif Nkind
(Expr
) = N_Identifier
3211 and then not Comes_From_Source
(Entity
(Expr
))
3212 and then Ekind
(Entity
(Expr
)) = E_Constant
3214 Nkind
(Parent
(Entity
(Expr
))) = N_Object_Declaration
3216 Exp
:= Expression
(Parent
(Entity
(Expr
)));
3217 Decompose_Expr
(Exp
, Ent
, Kind
, Cons
, Orig
=> False);
3219 -- If original expression includes an entity, create a
3220 -- reference to it for use below.
3222 if Present
(Ent
) then
3223 Exp
:= New_Occurrence_Of
(Ent
, Sloc
(Ent
));
3229 -- Only consider the case of X + 0 for a full
3230 -- expression, and not when recursing, otherwise we
3231 -- may end up with evaluating expressions not known
3232 -- at compile time to 0.
3242 -- At this stage Exp is set to the potential X
3244 if Nkind
(Exp
) = N_Attribute_Reference
then
3245 if Attribute_Name
(Exp
) = Name_First
then
3247 elsif Attribute_Name
(Exp
) = Name_Last
then
3253 Exp
:= Prefix
(Exp
);
3259 if Is_Entity_Name
(Exp
)
3260 and then Present
(Entity
(Exp
))
3262 Ent
:= Entity
(Exp
);
3268 Ent1
, Ent2
: Entity_Id
;
3269 Kind1
, Kind2
: Character;
3270 Cons1
, Cons2
: Uint
;
3272 -- Start of processing for Extract_Length
3276 (Original_Node
(Type_Low_Bound
(T
)), Ent1
, Kind1
, Cons1
);
3278 (Original_Node
(Type_High_Bound
(T
)), Ent2
, Kind2
, Cons2
);
3281 and then Kind1
= Kind2
3282 and then Ent1
= Ent2
3284 Len
:= Cons2
- Cons1
+ 1;
3286 Len
:= Uint_Minus_1
;
3289 end Get_Static_Length
;
3296 -- Start of processing for Length_Mismatch
3299 Get_Static_Length
(Left
, Len_L
);
3300 Get_Static_Length
(Right
, Len_R
);
3302 if Len_L
/= Uint_Minus_1
3303 and then Len_R
/= Uint_Minus_1
3304 and then Len_L
/= Len_R
3306 Fold_Uint
(N
, Test
(Nkind
(N
) = N_Op_Ne
), False);
3307 Warn_On_Known_Condition
(N
);
3310 end Length_Mismatch
;
3314 Is_Static_Expression
: Boolean;
3316 Is_Foldable
: Boolean;
3317 pragma Unreferenced
(Is_Foldable
);
3320 -- Initialize the value of Is_Static_Expression. The value of
3321 -- Is_Foldable returned by Test_Expression_Is_Foldable is not needed
3322 -- since, even when some operand is a variable, we can still perform
3323 -- the static evaluation of the expression in some cases (for
3324 -- example, for a variable of a subtype of Integer we statically
3325 -- know that any value stored in such variable is smaller than
3328 Test_Expression_Is_Foldable
3329 (N
, Left
, Right
, Is_Static_Expression
, Is_Foldable
);
3331 -- Only comparisons of scalars can give static results. In
3332 -- particular, comparisons of strings never yield a static
3333 -- result, even if both operands are static strings, except that
3334 -- as noted above, we allow equality/inequality for strings.
3336 if Is_String_Type
(Typ
)
3337 and then not Comes_From_Source
(N
)
3338 and then Nkind_In
(N
, N_Op_Eq
, N_Op_Ne
)
3342 elsif not Is_Scalar_Type
(Typ
) then
3343 Is_Static_Expression
:= False;
3344 Set_Is_Static_Expression
(N
, False);
3347 -- For operators on universal numeric types called as functions with
3348 -- an explicit scope, determine appropriate specific numeric type,
3349 -- and diagnose possible ambiguity.
3351 if Is_Universal_Numeric_Type
(Etype
(Left
))
3353 Is_Universal_Numeric_Type
(Etype
(Right
))
3355 Otype
:= Find_Universal_Operator_Type
(N
);
3358 -- For static real type expressions, do not use Compile_Time_Compare
3359 -- since it worries about run-time results which are not exact.
3361 if Is_Static_Expression
and then Is_Real_Type
(Typ
) then
3363 Left_Real
: constant Ureal
:= Expr_Value_R
(Left
);
3364 Right_Real
: constant Ureal
:= Expr_Value_R
(Right
);
3368 when N_Op_Eq
=> Result
:= (Left_Real
= Right_Real
);
3369 when N_Op_Ne
=> Result
:= (Left_Real
/= Right_Real
);
3370 when N_Op_Lt
=> Result
:= (Left_Real
< Right_Real
);
3371 when N_Op_Le
=> Result
:= (Left_Real
<= Right_Real
);
3372 when N_Op_Gt
=> Result
:= (Left_Real
> Right_Real
);
3373 when N_Op_Ge
=> Result
:= (Left_Real
>= Right_Real
);
3376 raise Program_Error
;
3379 Fold_Uint
(N
, Test
(Result
), True);
3382 -- For all other cases, we use Compile_Time_Compare to do the compare
3386 CR
: constant Compare_Result
:=
3387 Compile_Time_Compare
3388 (Left
, Right
, Assume_Valid
=> False);
3391 if CR
= Unknown
then
3399 elsif CR
= NE
or else CR
= GT
or else CR
= LT
then
3406 if CR
= NE
or else CR
= GT
or else CR
= LT
then
3417 elsif CR
= EQ
or else CR
= GT
or else CR
= GE
then
3424 if CR
= LT
or else CR
= EQ
or else CR
= LE
then
3435 elsif CR
= EQ
or else CR
= LT
or else CR
= LE
then
3442 if CR
= GT
or else CR
= EQ
or else CR
= GE
then
3451 raise Program_Error
;
3455 Fold_Uint
(N
, Test
(Result
), Is_Static_Expression
);
3459 -- For the case of a folded relational operator on a specific numeric
3460 -- type, freeze operand type now.
3462 if Present
(Otype
) then
3463 Freeze_Before
(N
, Otype
);
3466 Warn_On_Known_Condition
(N
);
3467 end Eval_Relational_Op
;
3473 -- Shift operations are intrinsic operations that can never be static, so
3474 -- the only processing required is to perform the required check for a non
3475 -- static context for the two operands.
3477 -- Actually we could do some compile time evaluation here some time ???
3479 procedure Eval_Shift
(N
: Node_Id
) is
3481 Check_Non_Static_Context
(Left_Opnd
(N
));
3482 Check_Non_Static_Context
(Right_Opnd
(N
));
3485 ------------------------
3486 -- Eval_Short_Circuit --
3487 ------------------------
3489 -- A short circuit operation is potentially static if both operands are
3490 -- potentially static (RM 4.9 (13)).
3492 procedure Eval_Short_Circuit
(N
: Node_Id
) is
3493 Kind
: constant Node_Kind
:= Nkind
(N
);
3494 Left
: constant Node_Id
:= Left_Opnd
(N
);
3495 Right
: constant Node_Id
:= Right_Opnd
(N
);
3498 Rstat
: constant Boolean :=
3499 Is_Static_Expression
(Left
)
3501 Is_Static_Expression
(Right
);
3504 -- Short circuit operations are never static in Ada 83
3506 if Ada_Version
= Ada_83
and then Comes_From_Source
(N
) then
3507 Check_Non_Static_Context
(Left
);
3508 Check_Non_Static_Context
(Right
);
3512 -- Now look at the operands, we can't quite use the normal call to
3513 -- Test_Expression_Is_Foldable here because short circuit operations
3514 -- are a special case, they can still be foldable, even if the right
3515 -- operand raises constraint error.
3517 -- If either operand is Any_Type, just propagate to result and do not
3518 -- try to fold, this prevents cascaded errors.
3520 if Etype
(Left
) = Any_Type
or else Etype
(Right
) = Any_Type
then
3521 Set_Etype
(N
, Any_Type
);
3524 -- If left operand raises constraint error, then replace node N with
3525 -- the raise constraint error node, and we are obviously not foldable.
3526 -- Is_Static_Expression is set from the two operands in the normal way,
3527 -- and we check the right operand if it is in a non-static context.
3529 elsif Raises_Constraint_Error
(Left
) then
3531 Check_Non_Static_Context
(Right
);
3534 Rewrite_In_Raise_CE
(N
, Left
);
3535 Set_Is_Static_Expression
(N
, Rstat
);
3538 -- If the result is not static, then we won't in any case fold
3540 elsif not Rstat
then
3541 Check_Non_Static_Context
(Left
);
3542 Check_Non_Static_Context
(Right
);
3546 -- Here the result is static, note that, unlike the normal processing
3547 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3548 -- the right operand raises constraint error, that's because it is not
3549 -- significant if the left operand is decisive.
3551 Set_Is_Static_Expression
(N
);
3553 -- It does not matter if the right operand raises constraint error if
3554 -- it will not be evaluated. So deal specially with the cases where
3555 -- the right operand is not evaluated. Note that we will fold these
3556 -- cases even if the right operand is non-static, which is fine, but
3557 -- of course in these cases the result is not potentially static.
3559 Left_Int
:= Expr_Value
(Left
);
3561 if (Kind
= N_And_Then
and then Is_False
(Left_Int
))
3563 (Kind
= N_Or_Else
and then Is_True
(Left_Int
))
3565 Fold_Uint
(N
, Left_Int
, Rstat
);
3569 -- If first operand not decisive, then it does matter if the right
3570 -- operand raises constraint error, since it will be evaluated, so
3571 -- we simply replace the node with the right operand. Note that this
3572 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3573 -- (both are set to True in Right).
3575 if Raises_Constraint_Error
(Right
) then
3576 Rewrite_In_Raise_CE
(N
, Right
);
3577 Check_Non_Static_Context
(Left
);
3581 -- Otherwise the result depends on the right operand
3583 Fold_Uint
(N
, Expr_Value
(Right
), Rstat
);
3585 end Eval_Short_Circuit
;
3591 -- Slices can never be static, so the only processing required is to check
3592 -- for non-static context if an explicit range is given.
3594 procedure Eval_Slice
(N
: Node_Id
) is
3595 Drange
: constant Node_Id
:= Discrete_Range
(N
);
3598 if Nkind
(Drange
) = N_Range
then
3599 Check_Non_Static_Context
(Low_Bound
(Drange
));
3600 Check_Non_Static_Context
(High_Bound
(Drange
));
3603 -- A slice of the form A (subtype), when the subtype is the index of
3604 -- the type of A, is redundant, the slice can be replaced with A, and
3605 -- this is worth a warning.
3607 if Is_Entity_Name
(Prefix
(N
)) then
3609 E
: constant Entity_Id
:= Entity
(Prefix
(N
));
3610 T
: constant Entity_Id
:= Etype
(E
);
3613 if Ekind
(E
) = E_Constant
3614 and then Is_Array_Type
(T
)
3615 and then Is_Entity_Name
(Drange
)
3617 if Is_Entity_Name
(Original_Node
(First_Index
(T
)))
3618 and then Entity
(Original_Node
(First_Index
(T
)))
3621 if Warn_On_Redundant_Constructs
then
3622 Error_Msg_N
("redundant slice denotes whole array?r?", N
);
3625 -- The following might be a useful optimization???
3627 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3634 -------------------------
3635 -- Eval_String_Literal --
3636 -------------------------
3638 procedure Eval_String_Literal
(N
: Node_Id
) is
3639 Typ
: constant Entity_Id
:= Etype
(N
);
3640 Bas
: constant Entity_Id
:= Base_Type
(Typ
);
3646 -- Nothing to do if error type (handles cases like default expressions
3647 -- or generics where we have not yet fully resolved the type).
3649 if Bas
= Any_Type
or else Bas
= Any_String
then
3653 -- String literals are static if the subtype is static (RM 4.9(2)), so
3654 -- reset the static expression flag (it was set unconditionally in
3655 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3656 -- the subtype is static by looking at the lower bound.
3658 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3659 if not Is_OK_Static_Expression
(String_Literal_Low_Bound
(Typ
)) then
3660 Set_Is_Static_Expression
(N
, False);
3664 -- Here if Etype of string literal is normal Etype (not yet possible,
3665 -- but may be possible in future).
3667 elsif not Is_OK_Static_Expression
3668 (Type_Low_Bound
(Etype
(First_Index
(Typ
))))
3670 Set_Is_Static_Expression
(N
, False);
3674 -- If original node was a type conversion, then result if non-static
3676 if Nkind
(Original_Node
(N
)) = N_Type_Conversion
then
3677 Set_Is_Static_Expression
(N
, False);
3681 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3682 -- if its bounds are outside the index base type and this index type is
3683 -- static. This can happen in only two ways. Either the string literal
3684 -- is too long, or it is null, and the lower bound is type'First. Either
3685 -- way it is the upper bound that is out of range of the index type.
3687 if Ada_Version
>= Ada_95
then
3688 if Is_Standard_String_Type
(Bas
) then
3689 Xtp
:= Standard_Positive
;
3691 Xtp
:= Etype
(First_Index
(Bas
));
3694 if Ekind
(Typ
) = E_String_Literal_Subtype
then
3695 Lo
:= String_Literal_Low_Bound
(Typ
);
3697 Lo
:= Type_Low_Bound
(Etype
(First_Index
(Typ
)));
3700 -- Check for string too long
3702 Len
:= String_Length
(Strval
(N
));
3704 if UI_From_Int
(Len
) > String_Type_Len
(Bas
) then
3706 -- Issue message. Note that this message is a warning if the
3707 -- string literal is not marked as static (happens in some cases
3708 -- of folding strings known at compile time, but not static).
3709 -- Furthermore in such cases, we reword the message, since there
3710 -- is no string literal in the source program.
3712 if Is_Static_Expression
(N
) then
3713 Apply_Compile_Time_Constraint_Error
3714 (N
, "string literal too long for}", CE_Length_Check_Failed
,
3716 Typ
=> First_Subtype
(Bas
));
3718 Apply_Compile_Time_Constraint_Error
3719 (N
, "string value too long for}", CE_Length_Check_Failed
,
3721 Typ
=> First_Subtype
(Bas
),
3725 -- Test for null string not allowed
3728 and then not Is_Generic_Type
(Xtp
)
3730 Expr_Value
(Lo
) = Expr_Value
(Type_Low_Bound
(Base_Type
(Xtp
)))
3732 -- Same specialization of message
3734 if Is_Static_Expression
(N
) then
3735 Apply_Compile_Time_Constraint_Error
3736 (N
, "null string literal not allowed for}",
3737 CE_Length_Check_Failed
,
3739 Typ
=> First_Subtype
(Bas
));
3741 Apply_Compile_Time_Constraint_Error
3742 (N
, "null string value not allowed for}",
3743 CE_Length_Check_Failed
,
3745 Typ
=> First_Subtype
(Bas
),
3750 end Eval_String_Literal
;
3752 --------------------------
3753 -- Eval_Type_Conversion --
3754 --------------------------
3756 -- A type conversion is potentially static if its subtype mark is for a
3757 -- static scalar subtype, and its operand expression is potentially static
3760 procedure Eval_Type_Conversion
(N
: Node_Id
) is
3761 Operand
: constant Node_Id
:= Expression
(N
);
3762 Source_Type
: constant Entity_Id
:= Etype
(Operand
);
3763 Target_Type
: constant Entity_Id
:= Etype
(N
);
3768 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean;
3769 -- Returns true if type T is an integer type, or if it is a fixed-point
3770 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3771 -- on the conversion node).
3773 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean;
3774 -- Returns true if type T is a floating-point type, or if it is a
3775 -- fixed-point type that is not to be treated as an integer (i.e. the
3776 -- flag Conversion_OK is not set on the conversion node).
3778 ------------------------------
3779 -- To_Be_Treated_As_Integer --
3780 ------------------------------
3782 function To_Be_Treated_As_Integer
(T
: Entity_Id
) return Boolean is
3786 or else (Is_Fixed_Point_Type
(T
) and then Conversion_OK
(N
));
3787 end To_Be_Treated_As_Integer
;
3789 ---------------------------
3790 -- To_Be_Treated_As_Real --
3791 ---------------------------
3793 function To_Be_Treated_As_Real
(T
: Entity_Id
) return Boolean is
3796 Is_Floating_Point_Type
(T
)
3797 or else (Is_Fixed_Point_Type
(T
) and then not Conversion_OK
(N
));
3798 end To_Be_Treated_As_Real
;
3800 -- Start of processing for Eval_Type_Conversion
3803 -- Cannot fold if target type is non-static or if semantic error
3805 if not Is_Static_Subtype
(Target_Type
) then
3806 Check_Non_Static_Context
(Operand
);
3808 elsif Error_Posted
(N
) then
3812 -- If not foldable we are done
3814 Test_Expression_Is_Foldable
(N
, Operand
, Stat
, Fold
);
3819 -- Don't try fold if target type has constraint error bounds
3821 elsif not Is_OK_Static_Subtype
(Target_Type
) then
3822 Set_Raises_Constraint_Error
(N
);
3826 -- Remaining processing depends on operand types. Note that in the
3827 -- following type test, fixed-point counts as real unless the flag
3828 -- Conversion_OK is set, in which case it counts as integer.
3830 -- Fold conversion, case of string type. The result is not static
3832 if Is_String_Type
(Target_Type
) then
3833 Fold_Str
(N
, Strval
(Get_String_Val
(Operand
)), Static
=> False);
3836 -- Fold conversion, case of integer target type
3838 elsif To_Be_Treated_As_Integer
(Target_Type
) then
3843 -- Integer to integer conversion
3845 if To_Be_Treated_As_Integer
(Source_Type
) then
3846 Result
:= Expr_Value
(Operand
);
3848 -- Real to integer conversion
3851 Result
:= UR_To_Uint
(Expr_Value_R
(Operand
));
3854 -- If fixed-point type (Conversion_OK must be set), then the
3855 -- result is logically an integer, but we must replace the
3856 -- conversion with the corresponding real literal, since the
3857 -- type from a semantic point of view is still fixed-point.
3859 if Is_Fixed_Point_Type
(Target_Type
) then
3861 (N
, UR_From_Uint
(Result
) * Small_Value
(Target_Type
), Stat
);
3863 -- Otherwise result is integer literal
3866 Fold_Uint
(N
, Result
, Stat
);
3870 -- Fold conversion, case of real target type
3872 elsif To_Be_Treated_As_Real
(Target_Type
) then
3877 if To_Be_Treated_As_Real
(Source_Type
) then
3878 Result
:= Expr_Value_R
(Operand
);
3880 Result
:= UR_From_Uint
(Expr_Value
(Operand
));
3883 Fold_Ureal
(N
, Result
, Stat
);
3886 -- Enumeration types
3889 Fold_Uint
(N
, Expr_Value
(Operand
), Stat
);
3892 if Is_Out_Of_Range
(N
, Etype
(N
), Assume_Valid
=> True) then
3896 end Eval_Type_Conversion
;
3902 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
3903 -- are potentially static if the operand is potentially static (RM 4.9(7)).
3905 procedure Eval_Unary_Op
(N
: Node_Id
) is
3906 Right
: constant Node_Id
:= Right_Opnd
(N
);
3907 Otype
: Entity_Id
:= Empty
;
3912 -- If not foldable we are done
3914 Test_Expression_Is_Foldable
(N
, Right
, Stat
, Fold
);
3920 if Etype
(Right
) = Universal_Integer
3922 Etype
(Right
) = Universal_Real
3924 Otype
:= Find_Universal_Operator_Type
(N
);
3927 -- Fold for integer case
3929 if Is_Integer_Type
(Etype
(N
)) then
3931 Rint
: constant Uint
:= Expr_Value
(Right
);
3935 -- In the case of modular unary plus and abs there is no need
3936 -- to adjust the result of the operation since if the original
3937 -- operand was in bounds the result will be in the bounds of the
3938 -- modular type. However, in the case of modular unary minus the
3939 -- result may go out of the bounds of the modular type and needs
3942 if Nkind
(N
) = N_Op_Plus
then
3945 elsif Nkind
(N
) = N_Op_Minus
then
3946 if Is_Modular_Integer_Type
(Etype
(N
)) then
3947 Result
:= (-Rint
) mod Modulus
(Etype
(N
));
3953 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
3957 Fold_Uint
(N
, Result
, Stat
);
3960 -- Fold for real case
3962 elsif Is_Real_Type
(Etype
(N
)) then
3964 Rreal
: constant Ureal
:= Expr_Value_R
(Right
);
3968 if Nkind
(N
) = N_Op_Plus
then
3970 elsif Nkind
(N
) = N_Op_Minus
then
3971 Result
:= UR_Negate
(Rreal
);
3973 pragma Assert
(Nkind
(N
) = N_Op_Abs
);
3974 Result
:= abs Rreal
;
3977 Fold_Ureal
(N
, Result
, Stat
);
3981 -- If the operator was resolved to a specific type, make sure that type
3982 -- is frozen even if the expression is folded into a literal (which has
3983 -- a universal type).
3985 if Present
(Otype
) then
3986 Freeze_Before
(N
, Otype
);
3990 -------------------------------
3991 -- Eval_Unchecked_Conversion --
3992 -------------------------------
3994 -- Unchecked conversions can never be static, so the only required
3995 -- processing is to check for a non-static context for the operand.
3997 procedure Eval_Unchecked_Conversion
(N
: Node_Id
) is
3999 Check_Non_Static_Context
(Expression
(N
));
4000 end Eval_Unchecked_Conversion
;
4002 --------------------
4003 -- Expr_Rep_Value --
4004 --------------------
4006 function Expr_Rep_Value
(N
: Node_Id
) return Uint
is
4007 Kind
: constant Node_Kind
:= Nkind
(N
);
4011 if Is_Entity_Name
(N
) then
4014 -- An enumeration literal that was either in the source or created
4015 -- as a result of static evaluation.
4017 if Ekind
(Ent
) = E_Enumeration_Literal
then
4018 return Enumeration_Rep
(Ent
);
4020 -- A user defined static constant
4023 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4024 return Expr_Rep_Value
(Constant_Value
(Ent
));
4027 -- An integer literal that was either in the source or created as a
4028 -- result of static evaluation.
4030 elsif Kind
= N_Integer_Literal
then
4033 -- A real literal for a fixed-point type. This must be the fixed-point
4034 -- case, either the literal is of a fixed-point type, or it is a bound
4035 -- of a fixed-point type, with type universal real. In either case we
4036 -- obtain the desired value from Corresponding_Integer_Value.
4038 elsif Kind
= N_Real_Literal
then
4039 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4040 return Corresponding_Integer_Value
(N
);
4042 -- Otherwise must be character literal
4045 pragma Assert
(Kind
= N_Character_Literal
);
4048 -- Since Character literals of type Standard.Character don't have any
4049 -- defining character literals built for them, they do not have their
4050 -- Entity set, so just use their Char code. Otherwise for user-
4051 -- defined character literals use their Pos value as usual which is
4052 -- the same as the Rep value.
4055 return Char_Literal_Value
(N
);
4057 return Enumeration_Rep
(Ent
);
4066 function Expr_Value
(N
: Node_Id
) return Uint
is
4067 Kind
: constant Node_Kind
:= Nkind
(N
);
4068 CV_Ent
: CV_Entry
renames CV_Cache
(Nat
(N
) mod CV_Cache_Size
);
4073 -- If already in cache, then we know it's compile time known and we can
4074 -- return the value that was previously stored in the cache since
4075 -- compile time known values cannot change.
4077 if CV_Ent
.N
= N
then
4081 -- Otherwise proceed to test value
4083 if Is_Entity_Name
(N
) then
4086 -- An enumeration literal that was either in the source or created as
4087 -- a result of static evaluation.
4089 if Ekind
(Ent
) = E_Enumeration_Literal
then
4090 Val
:= Enumeration_Pos
(Ent
);
4092 -- A user defined static constant
4095 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4096 Val
:= Expr_Value
(Constant_Value
(Ent
));
4099 -- An integer literal that was either in the source or created as a
4100 -- result of static evaluation.
4102 elsif Kind
= N_Integer_Literal
then
4105 -- A real literal for a fixed-point type. This must be the fixed-point
4106 -- case, either the literal is of a fixed-point type, or it is a bound
4107 -- of a fixed-point type, with type universal real. In either case we
4108 -- obtain the desired value from Corresponding_Integer_Value.
4110 elsif Kind
= N_Real_Literal
then
4111 pragma Assert
(Is_Fixed_Point_Type
(Underlying_Type
(Etype
(N
))));
4112 Val
:= Corresponding_Integer_Value
(N
);
4114 -- Otherwise must be character literal
4117 pragma Assert
(Kind
= N_Character_Literal
);
4120 -- Since Character literals of type Standard.Character don't
4121 -- have any defining character literals built for them, they
4122 -- do not have their Entity set, so just use their Char
4123 -- code. Otherwise for user-defined character literals use
4124 -- their Pos value as usual.
4127 Val
:= Char_Literal_Value
(N
);
4129 Val
:= Enumeration_Pos
(Ent
);
4133 -- Come here with Val set to value to be returned, set cache
4144 function Expr_Value_E
(N
: Node_Id
) return Entity_Id
is
4145 Ent
: constant Entity_Id
:= Entity
(N
);
4147 if Ekind
(Ent
) = E_Enumeration_Literal
then
4150 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4151 return Expr_Value_E
(Constant_Value
(Ent
));
4159 function Expr_Value_R
(N
: Node_Id
) return Ureal
is
4160 Kind
: constant Node_Kind
:= Nkind
(N
);
4164 if Kind
= N_Real_Literal
then
4167 elsif Kind
= N_Identifier
or else Kind
= N_Expanded_Name
then
4169 pragma Assert
(Ekind
(Ent
) = E_Constant
);
4170 return Expr_Value_R
(Constant_Value
(Ent
));
4172 elsif Kind
= N_Integer_Literal
then
4173 return UR_From_Uint
(Expr_Value
(N
));
4175 -- Here, we have a node that cannot be interpreted as a compile time
4176 -- constant. That is definitely an error.
4179 raise Program_Error
;
4187 function Expr_Value_S
(N
: Node_Id
) return Node_Id
is
4189 if Nkind
(N
) = N_String_Literal
then
4192 pragma Assert
(Ekind
(Entity
(N
)) = E_Constant
);
4193 return Expr_Value_S
(Constant_Value
(Entity
(N
)));
4197 ----------------------------------
4198 -- Find_Universal_Operator_Type --
4199 ----------------------------------
4201 function Find_Universal_Operator_Type
(N
: Node_Id
) return Entity_Id
is
4202 PN
: constant Node_Id
:= Parent
(N
);
4203 Call
: constant Node_Id
:= Original_Node
(N
);
4204 Is_Int
: constant Boolean := Is_Integer_Type
(Etype
(N
));
4206 Is_Fix
: constant Boolean :=
4207 Nkind
(N
) in N_Binary_Op
4208 and then Nkind
(Right_Opnd
(N
)) /= Nkind
(Left_Opnd
(N
));
4209 -- A mixed-mode operation in this context indicates the presence of
4210 -- fixed-point type in the designated package.
4212 Is_Relational
: constant Boolean := Etype
(N
) = Standard_Boolean
;
4213 -- Case where N is a relational (or membership) operator (else it is an
4216 In_Membership
: constant Boolean :=
4217 Nkind
(PN
) in N_Membership_Test
4219 Nkind
(Right_Opnd
(PN
)) = N_Range
4221 Is_Universal_Numeric_Type
(Etype
(Left_Opnd
(PN
)))
4223 Is_Universal_Numeric_Type
4224 (Etype
(Low_Bound
(Right_Opnd
(PN
))))
4226 Is_Universal_Numeric_Type
4227 (Etype
(High_Bound
(Right_Opnd
(PN
))));
4228 -- Case where N is part of a membership test with a universal range
4232 Typ1
: Entity_Id
:= Empty
;
4235 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean;
4236 -- Check whether one operand is a mixed-mode operation that requires the
4237 -- presence of a fixed-point type. Given that all operands are universal
4238 -- and have been constant-folded, retrieve the original function call.
4240 ---------------------------
4241 -- Is_Mixed_Mode_Operand --
4242 ---------------------------
4244 function Is_Mixed_Mode_Operand
(Op
: Node_Id
) return Boolean is
4245 Onod
: constant Node_Id
:= Original_Node
(Op
);
4247 return Nkind
(Onod
) = N_Function_Call
4248 and then Present
(Next_Actual
(First_Actual
(Onod
)))
4249 and then Etype
(First_Actual
(Onod
)) /=
4250 Etype
(Next_Actual
(First_Actual
(Onod
)));
4251 end Is_Mixed_Mode_Operand
;
4253 -- Start of processing for Find_Universal_Operator_Type
4256 if Nkind
(Call
) /= N_Function_Call
4257 or else Nkind
(Name
(Call
)) /= N_Expanded_Name
4261 -- There are several cases where the context does not imply the type of
4263 -- - the universal expression appears in a type conversion;
4264 -- - the expression is a relational operator applied to universal
4266 -- - the expression is a membership test with a universal operand
4267 -- and a range with universal bounds.
4269 elsif Nkind
(Parent
(N
)) = N_Type_Conversion
4270 or else Is_Relational
4271 or else In_Membership
4273 Pack
:= Entity
(Prefix
(Name
(Call
)));
4275 -- If the prefix is a package declared elsewhere, iterate over its
4276 -- visible entities, otherwise iterate over all declarations in the
4277 -- designated scope.
4279 if Ekind
(Pack
) = E_Package
4280 and then not In_Open_Scopes
(Pack
)
4282 Priv_E
:= First_Private_Entity
(Pack
);
4288 E
:= First_Entity
(Pack
);
4289 while Present
(E
) and then E
/= Priv_E
loop
4290 if Is_Numeric_Type
(E
)
4291 and then Nkind
(Parent
(E
)) /= N_Subtype_Declaration
4292 and then Comes_From_Source
(E
)
4293 and then Is_Integer_Type
(E
) = Is_Int
4294 and then (Nkind
(N
) in N_Unary_Op
4295 or else Is_Relational
4296 or else Is_Fixed_Point_Type
(E
) = Is_Fix
)
4301 -- Before emitting an error, check for the presence of a
4302 -- mixed-mode operation that specifies a fixed point type.
4306 (Is_Mixed_Mode_Operand
(Left_Opnd
(N
))
4307 or else Is_Mixed_Mode_Operand
(Right_Opnd
(N
)))
4308 and then Is_Fixed_Point_Type
(E
) /= Is_Fixed_Point_Type
(Typ1
)
4311 if Is_Fixed_Point_Type
(E
) then
4316 -- More than one type of the proper class declared in P
4318 Error_Msg_N
("ambiguous operation", N
);
4319 Error_Msg_Sloc
:= Sloc
(Typ1
);
4320 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4321 Error_Msg_Sloc
:= Sloc
(E
);
4322 Error_Msg_N
("\possible interpretation (inherited)#", N
);
4332 end Find_Universal_Operator_Type
;
4334 --------------------------
4335 -- Flag_Non_Static_Expr --
4336 --------------------------
4338 procedure Flag_Non_Static_Expr
(Msg
: String; Expr
: Node_Id
) is
4340 if Error_Posted
(Expr
) and then not All_Errors_Mode
then
4343 Error_Msg_F
(Msg
, Expr
);
4344 Why_Not_Static
(Expr
);
4346 end Flag_Non_Static_Expr
;
4352 procedure Fold_Str
(N
: Node_Id
; Val
: String_Id
; Static
: Boolean) is
4353 Loc
: constant Source_Ptr
:= Sloc
(N
);
4354 Typ
: constant Entity_Id
:= Etype
(N
);
4357 if Raises_Constraint_Error
(N
) then
4358 Set_Is_Static_Expression
(N
, Static
);
4362 Rewrite
(N
, Make_String_Literal
(Loc
, Strval
=> Val
));
4364 -- We now have the literal with the right value, both the actual type
4365 -- and the expected type of this literal are taken from the expression
4366 -- that was evaluated. So now we do the Analyze and Resolve.
4368 -- Note that we have to reset Is_Static_Expression both after the
4369 -- analyze step (because Resolve will evaluate the literal, which
4370 -- will cause semantic errors if it is marked as static), and after
4371 -- the Resolve step (since Resolve in some cases resets this flag).
4374 Set_Is_Static_Expression
(N
, Static
);
4377 Set_Is_Static_Expression
(N
, Static
);
4384 procedure Fold_Uint
(N
: Node_Id
; Val
: Uint
; Static
: Boolean) is
4385 Loc
: constant Source_Ptr
:= Sloc
(N
);
4386 Typ
: Entity_Id
:= Etype
(N
);
4390 if Raises_Constraint_Error
(N
) then
4391 Set_Is_Static_Expression
(N
, Static
);
4395 -- If we are folding a named number, retain the entity in the literal,
4398 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Integer
then
4404 if Is_Private_Type
(Typ
) then
4405 Typ
:= Full_View
(Typ
);
4408 -- For a result of type integer, substitute an N_Integer_Literal node
4409 -- for the result of the compile time evaluation of the expression.
4410 -- For ASIS use, set a link to the original named number when not in
4411 -- a generic context.
4413 if Is_Integer_Type
(Typ
) then
4414 Rewrite
(N
, Make_Integer_Literal
(Loc
, Val
));
4415 Set_Original_Entity
(N
, Ent
);
4417 -- Otherwise we have an enumeration type, and we substitute either
4418 -- an N_Identifier or N_Character_Literal to represent the enumeration
4419 -- literal corresponding to the given value, which must always be in
4420 -- range, because appropriate tests have already been made for this.
4422 else pragma Assert
(Is_Enumeration_Type
(Typ
));
4423 Rewrite
(N
, Get_Enum_Lit_From_Pos
(Etype
(N
), Val
, Loc
));
4426 -- We now have the literal with the right value, both the actual type
4427 -- and the expected type of this literal are taken from the expression
4428 -- that was evaluated. So now we do the Analyze and Resolve.
4430 -- Note that we have to reset Is_Static_Expression both after the
4431 -- analyze step (because Resolve will evaluate the literal, which
4432 -- will cause semantic errors if it is marked as static), and after
4433 -- the Resolve step (since Resolve in some cases sets this flag).
4436 Set_Is_Static_Expression
(N
, Static
);
4439 Set_Is_Static_Expression
(N
, Static
);
4446 procedure Fold_Ureal
(N
: Node_Id
; Val
: Ureal
; Static
: Boolean) is
4447 Loc
: constant Source_Ptr
:= Sloc
(N
);
4448 Typ
: constant Entity_Id
:= Etype
(N
);
4452 if Raises_Constraint_Error
(N
) then
4453 Set_Is_Static_Expression
(N
, Static
);
4457 -- If we are folding a named number, retain the entity in the literal,
4460 if Is_Entity_Name
(N
) and then Ekind
(Entity
(N
)) = E_Named_Real
then
4466 Rewrite
(N
, Make_Real_Literal
(Loc
, Realval
=> Val
));
4468 -- Set link to original named number, for ASIS use
4470 Set_Original_Entity
(N
, Ent
);
4472 -- We now have the literal with the right value, both the actual type
4473 -- and the expected type of this literal are taken from the expression
4474 -- that was evaluated. So now we do the Analyze and Resolve.
4476 -- Note that we have to reset Is_Static_Expression both after the
4477 -- analyze step (because Resolve will evaluate the literal, which
4478 -- will cause semantic errors if it is marked as static), and after
4479 -- the Resolve step (since Resolve in some cases sets this flag).
4482 Set_Is_Static_Expression
(N
, Static
);
4485 Set_Is_Static_Expression
(N
, Static
);
4492 function From_Bits
(B
: Bits
; T
: Entity_Id
) return Uint
is
4496 for J
in 0 .. B
'Last loop
4502 if Non_Binary_Modulus
(T
) then
4503 V
:= V
mod Modulus
(T
);
4509 --------------------
4510 -- Get_String_Val --
4511 --------------------
4513 function Get_String_Val
(N
: Node_Id
) return Node_Id
is
4515 if Nkind_In
(N
, N_String_Literal
, N_Character_Literal
) then
4518 pragma Assert
(Is_Entity_Name
(N
));
4519 return Get_String_Val
(Constant_Value
(Entity
(N
)));
4527 procedure Initialize
is
4529 CV_Cache
:= (others => (Node_High_Bound
, Uint_0
));
4532 --------------------
4533 -- In_Subrange_Of --
4534 --------------------
4536 function In_Subrange_Of
4539 Fixed_Int
: Boolean := False) return Boolean
4548 if T1
= T2
or else Is_Subtype_Of
(T1
, T2
) then
4551 -- Never in range if both types are not scalar. Don't know if this can
4552 -- actually happen, but just in case.
4554 elsif not Is_Scalar_Type
(T1
) or else not Is_Scalar_Type
(T2
) then
4557 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4558 -- definitely not compatible with T2.
4560 elsif Is_Floating_Point_Type
(T1
)
4561 and then Has_Infinities
(T1
)
4562 and then Is_Floating_Point_Type
(T2
)
4563 and then not Has_Infinities
(T2
)
4568 L1
:= Type_Low_Bound
(T1
);
4569 H1
:= Type_High_Bound
(T1
);
4571 L2
:= Type_Low_Bound
(T2
);
4572 H2
:= Type_High_Bound
(T2
);
4574 -- Check bounds to see if comparison possible at compile time
4576 if Compile_Time_Compare
(L1
, L2
, Assume_Valid
=> True) in Compare_GE
4578 Compile_Time_Compare
(H1
, H2
, Assume_Valid
=> True) in Compare_LE
4583 -- If bounds not comparable at compile time, then the bounds of T2
4584 -- must be compile time known or we cannot answer the query.
4586 if not Compile_Time_Known_Value
(L2
)
4587 or else not Compile_Time_Known_Value
(H2
)
4592 -- If the bounds of T1 are know at compile time then use these
4593 -- ones, otherwise use the bounds of the base type (which are of
4594 -- course always static).
4596 if not Compile_Time_Known_Value
(L1
) then
4597 L1
:= Type_Low_Bound
(Base_Type
(T1
));
4600 if not Compile_Time_Known_Value
(H1
) then
4601 H1
:= Type_High_Bound
(Base_Type
(T1
));
4604 -- Fixed point types should be considered as such only if
4605 -- flag Fixed_Int is set to False.
4607 if Is_Floating_Point_Type
(T1
) or else Is_Floating_Point_Type
(T2
)
4608 or else (Is_Fixed_Point_Type
(T1
) and then not Fixed_Int
)
4609 or else (Is_Fixed_Point_Type
(T2
) and then not Fixed_Int
)
4612 Expr_Value_R
(L2
) <= Expr_Value_R
(L1
)
4614 Expr_Value_R
(H2
) >= Expr_Value_R
(H1
);
4618 Expr_Value
(L2
) <= Expr_Value
(L1
)
4620 Expr_Value
(H2
) >= Expr_Value
(H1
);
4625 -- If any exception occurs, it means that we have some bug in the compiler
4626 -- possibly triggered by a previous error, or by some unforeseen peculiar
4627 -- occurrence. However, this is only an optimization attempt, so there is
4628 -- really no point in crashing the compiler. Instead we just decide, too
4629 -- bad, we can't figure out the answer in this case after all.
4634 -- Debug flag K disables this behavior (useful for debugging)
4636 if Debug_Flag_K
then
4647 function Is_In_Range
4650 Assume_Valid
: Boolean := False;
4651 Fixed_Int
: Boolean := False;
4652 Int_Real
: Boolean := False) return Boolean
4656 Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) = In_Range
;
4663 function Is_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
4664 Typ
: constant Entity_Id
:= Etype
(Lo
);
4667 if not Compile_Time_Known_Value
(Lo
)
4668 or else not Compile_Time_Known_Value
(Hi
)
4673 if Is_Discrete_Type
(Typ
) then
4674 return Expr_Value
(Lo
) > Expr_Value
(Hi
);
4675 else pragma Assert
(Is_Real_Type
(Typ
));
4676 return Expr_Value_R
(Lo
) > Expr_Value_R
(Hi
);
4680 -------------------------
4681 -- Is_OK_Static_Choice --
4682 -------------------------
4684 function Is_OK_Static_Choice
(Choice
: Node_Id
) return Boolean is
4686 -- Check various possibilities for choice
4688 -- Note: for membership tests, we test more cases than are possible
4689 -- (in particular subtype indication), but it doesn't matter because
4690 -- it just won't occur (we have already done a syntax check).
4692 if Nkind
(Choice
) = N_Others_Choice
then
4695 elsif Nkind
(Choice
) = N_Range
then
4696 return Is_OK_Static_Range
(Choice
);
4698 elsif Nkind
(Choice
) = N_Subtype_Indication
4700 (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
4702 return Is_OK_Static_Subtype
(Etype
(Choice
));
4705 return Is_OK_Static_Expression
(Choice
);
4707 end Is_OK_Static_Choice
;
4709 ------------------------------
4710 -- Is_OK_Static_Choice_List --
4711 ------------------------------
4713 function Is_OK_Static_Choice_List
(Choices
: List_Id
) return Boolean is
4717 if not Is_Static_Choice_List
(Choices
) then
4721 Choice
:= First
(Choices
);
4722 while Present
(Choice
) loop
4723 if not Is_OK_Static_Choice
(Choice
) then
4724 Set_Raises_Constraint_Error
(Choice
);
4732 end Is_OK_Static_Choice_List
;
4734 -----------------------------
4735 -- Is_OK_Static_Expression --
4736 -----------------------------
4738 function Is_OK_Static_Expression
(N
: Node_Id
) return Boolean is
4740 return Is_Static_Expression
(N
) and then not Raises_Constraint_Error
(N
);
4741 end Is_OK_Static_Expression
;
4743 ------------------------
4744 -- Is_OK_Static_Range --
4745 ------------------------
4747 -- A static range is a range whose bounds are static expressions, or a
4748 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4749 -- We have already converted range attribute references, so we get the
4750 -- "or" part of this rule without needing a special test.
4752 function Is_OK_Static_Range
(N
: Node_Id
) return Boolean is
4754 return Is_OK_Static_Expression
(Low_Bound
(N
))
4755 and then Is_OK_Static_Expression
(High_Bound
(N
));
4756 end Is_OK_Static_Range
;
4758 --------------------------
4759 -- Is_OK_Static_Subtype --
4760 --------------------------
4762 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4763 -- neither bound raises constraint error when evaluated.
4765 function Is_OK_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4766 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4767 Anc_Subt
: Entity_Id
;
4770 -- First a quick check on the non static subtype flag. As described
4771 -- in further detail in Einfo, this flag is not decisive in all cases,
4772 -- but if it is set, then the subtype is definitely non-static.
4774 if Is_Non_Static_Subtype
(Typ
) then
4778 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4780 if Anc_Subt
= Empty
then
4784 if Is_Generic_Type
(Root_Type
(Base_T
))
4785 or else Is_Generic_Actual_Type
(Base_T
)
4791 elsif Is_String_Type
(Typ
) then
4793 Ekind
(Typ
) = E_String_Literal_Subtype
4795 (Is_OK_Static_Subtype
(Component_Type
(Typ
))
4796 and then Is_OK_Static_Subtype
(Etype
(First_Index
(Typ
))));
4800 elsif Is_Scalar_Type
(Typ
) then
4801 if Base_T
= Typ
then
4805 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4806 -- Get_Type_{Low,High}_Bound.
4808 return Is_OK_Static_Subtype
(Anc_Subt
)
4809 and then Is_OK_Static_Expression
(Type_Low_Bound
(Typ
))
4810 and then Is_OK_Static_Expression
(Type_High_Bound
(Typ
));
4813 -- Types other than string and scalar types are never static
4818 end Is_OK_Static_Subtype
;
4820 ---------------------
4821 -- Is_Out_Of_Range --
4822 ---------------------
4824 function Is_Out_Of_Range
4827 Assume_Valid
: Boolean := False;
4828 Fixed_Int
: Boolean := False;
4829 Int_Real
: Boolean := False) return Boolean
4832 return Test_In_Range
(N
, Typ
, Assume_Valid
, Fixed_Int
, Int_Real
) =
4834 end Is_Out_Of_Range
;
4836 ----------------------
4837 -- Is_Static_Choice --
4838 ----------------------
4840 function Is_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_Static_Range
(Choice
);
4854 elsif Nkind
(Choice
) = N_Subtype_Indication
4856 (Is_Entity_Name
(Choice
) and then Is_Type
(Entity
(Choice
)))
4858 return Is_Static_Subtype
(Etype
(Choice
));
4861 return Is_Static_Expression
(Choice
);
4863 end Is_Static_Choice
;
4865 ---------------------------
4866 -- Is_Static_Choice_List --
4867 ---------------------------
4869 function Is_Static_Choice_List
(Choices
: List_Id
) return Boolean is
4873 Choice
:= First
(Choices
);
4874 while Present
(Choice
) loop
4875 if not Is_Static_Choice
(Choice
) then
4883 end Is_Static_Choice_List
;
4885 ---------------------
4886 -- Is_Static_Range --
4887 ---------------------
4889 -- A static range is a range whose bounds are static expressions, or a
4890 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4891 -- We have already converted range attribute references, so we get the
4892 -- "or" part of this rule without needing a special test.
4894 function Is_Static_Range
(N
: Node_Id
) return Boolean is
4896 return Is_Static_Expression
(Low_Bound
(N
))
4898 Is_Static_Expression
(High_Bound
(N
));
4899 end Is_Static_Range
;
4901 -----------------------
4902 -- Is_Static_Subtype --
4903 -----------------------
4905 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
4907 function Is_Static_Subtype
(Typ
: Entity_Id
) return Boolean is
4908 Base_T
: constant Entity_Id
:= Base_Type
(Typ
);
4909 Anc_Subt
: Entity_Id
;
4912 -- First a quick check on the non static subtype flag. As described
4913 -- in further detail in Einfo, this flag is not decisive in all cases,
4914 -- but if it is set, then the subtype is definitely non-static.
4916 if Is_Non_Static_Subtype
(Typ
) then
4920 Anc_Subt
:= Ancestor_Subtype
(Typ
);
4922 if Anc_Subt
= Empty
then
4926 if Is_Generic_Type
(Root_Type
(Base_T
))
4927 or else Is_Generic_Actual_Type
(Base_T
)
4933 elsif Is_String_Type
(Typ
) then
4935 Ekind
(Typ
) = E_String_Literal_Subtype
4936 or else (Is_Static_Subtype
(Component_Type
(Typ
))
4937 and then Is_Static_Subtype
(Etype
(First_Index
(Typ
))));
4941 elsif Is_Scalar_Type
(Typ
) then
4942 if Base_T
= Typ
then
4946 return Is_Static_Subtype
(Anc_Subt
)
4947 and then Is_Static_Expression
(Type_Low_Bound
(Typ
))
4948 and then Is_Static_Expression
(Type_High_Bound
(Typ
));
4951 -- Types other than string and scalar types are never static
4956 end Is_Static_Subtype
;
4958 -------------------------------
4959 -- Is_Statically_Unevaluated --
4960 -------------------------------
4962 function Is_Statically_Unevaluated
(Expr
: Node_Id
) return Boolean is
4963 function Check_Case_Expr_Alternative
4964 (CEA
: Node_Id
) return Match_Result
;
4965 -- We have a message emanating from the Expression of a case expression
4966 -- alternative. We examine this alternative, as follows:
4968 -- If the selecting expression of the parent case is non-static, or
4969 -- if any of the discrete choices of the given case alternative are
4970 -- non-static or raise Constraint_Error, return Non_Static.
4972 -- Otherwise check if the selecting expression matches any of the given
4973 -- discrete choices. If so, the alternative is executed and we return
4974 -- Match, otherwise, the alternative can never be executed, and so we
4977 ---------------------------------
4978 -- Check_Case_Expr_Alternative --
4979 ---------------------------------
4981 function Check_Case_Expr_Alternative
4982 (CEA
: Node_Id
) return Match_Result
4984 Case_Exp
: constant Node_Id
:= Parent
(CEA
);
4989 pragma Assert
(Nkind
(Case_Exp
) = N_Case_Expression
);
4991 -- Check that selecting expression is static
4993 if not Is_OK_Static_Expression
(Expression
(Case_Exp
)) then
4997 if not Is_OK_Static_Choice_List
(Discrete_Choices
(CEA
)) then
5001 -- All choices are now known to be static. Now see if alternative
5002 -- matches one of the choices.
5004 Choice
:= First
(Discrete_Choices
(CEA
));
5005 while Present
(Choice
) loop
5007 -- Check various possibilities for choice, returning Match if we
5008 -- find the selecting value matches any of the choices. Note that
5009 -- we know we are the last choice, so we don't have to keep going.
5011 if Nkind
(Choice
) = N_Others_Choice
then
5013 -- Others choice is a bit annoying, it matches if none of the
5014 -- previous alternatives matches (note that we know we are the
5015 -- last alternative in this case, so we can just go backwards
5016 -- from us to see if any previous one matches).
5018 Prev_CEA
:= Prev
(CEA
);
5019 while Present
(Prev_CEA
) loop
5020 if Check_Case_Expr_Alternative
(Prev_CEA
) = Match
then
5029 -- Else we have a normal static choice
5031 elsif Choice_Matches
(Expression
(Case_Exp
), Choice
) = Match
then
5035 -- If we fall through, it means that the discrete choice did not
5036 -- match the selecting expression, so continue.
5041 -- If we get through that loop then all choices were static, and none
5042 -- of them matched the selecting expression. So return No_Match.
5045 end Check_Case_Expr_Alternative
;
5053 -- Start of processing for Is_Statically_Unevaluated
5056 -- The (32.x) references here are from RM section 4.9
5058 -- (32.1) An expression is statically unevaluated if it is part of ...
5060 -- This means we have to climb the tree looking for one of the cases
5067 -- (32.2) The right operand of a static short-circuit control form
5068 -- whose value is determined by its left operand.
5070 -- AND THEN with False as left operand
5072 if Nkind
(P
) = N_And_Then
5073 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5074 and then Is_False
(Expr_Value
(Left_Opnd
(P
)))
5078 -- OR ELSE with True as left operand
5080 elsif Nkind
(P
) = N_Or_Else
5081 and then Compile_Time_Known_Value
(Left_Opnd
(P
))
5082 and then Is_True
(Expr_Value
(Left_Opnd
(P
)))
5086 -- (32.3) A dependent_expression of an if_expression whose associated
5087 -- condition is static and equals False.
5089 elsif Nkind
(P
) = N_If_Expression
then
5091 Cond
: constant Node_Id
:= First
(Expressions
(P
));
5092 Texp
: constant Node_Id
:= Next
(Cond
);
5093 Fexp
: constant Node_Id
:= Next
(Texp
);
5096 if Compile_Time_Known_Value
(Cond
) then
5098 -- Condition is True and we are in the right operand
5100 if Is_True
(Expr_Value
(Cond
)) and then OldP
= Fexp
then
5103 -- Condition is False and we are in the left operand
5105 elsif Is_False
(Expr_Value
(Cond
)) and then OldP
= Texp
then
5111 -- (32.4) A condition or dependent_expression of an if_expression
5112 -- where the condition corresponding to at least one preceding
5113 -- dependent_expression of the if_expression is static and equals
5116 -- This refers to cases like
5118 -- (if True then 1 elsif 1/0=2 then 2 else 3)
5120 -- But we expand elsif's out anyway, so the above looks like:
5122 -- (if True then 1 else (if 1/0=2 then 2 else 3))
5124 -- So for us this is caught by the above check for the 32.3 case.
5126 -- (32.5) A dependent_expression of a case_expression whose
5127 -- selecting_expression is static and whose value is not covered
5128 -- by the corresponding discrete_choice_list.
5130 elsif Nkind
(P
) = N_Case_Expression_Alternative
then
5132 -- First, we have to be in the expression to suppress messages.
5133 -- If we are within one of the choices, we want the message.
5135 if OldP
= Expression
(P
) then
5137 -- Statically unevaluated if alternative does not match
5139 if Check_Case_Expr_Alternative
(P
) = No_Match
then
5144 -- (32.6) A choice_expression (or a simple_expression of a range
5145 -- that occurs as a membership_choice of a membership_choice_list)
5146 -- of a static membership test that is preceded in the enclosing
5147 -- membership_choice_list by another item whose individual
5148 -- membership test (see (RM 4.5.2)) statically yields True.
5150 elsif Nkind
(P
) in N_Membership_Test
then
5152 -- Only possibly unevaluated if simple expression is static
5154 if not Is_OK_Static_Expression
(Left_Opnd
(P
)) then
5157 -- All members of the choice list must be static
5159 elsif (Present
(Right_Opnd
(P
))
5160 and then not Is_OK_Static_Choice
(Right_Opnd
(P
)))
5161 or else (Present
(Alternatives
(P
))
5163 not Is_OK_Static_Choice_List
(Alternatives
(P
)))
5167 -- If expression is the one and only alternative, then it is
5168 -- definitely not statically unevaluated, so we only have to
5169 -- test the case where there are alternatives present.
5171 elsif Present
(Alternatives
(P
)) then
5173 -- Look for previous matching Choice
5175 Choice
:= First
(Alternatives
(P
));
5176 while Present
(Choice
) loop
5178 -- If we reached us and no previous choices matched, this
5179 -- is not the case where we are statically unevaluated.
5181 exit when OldP
= Choice
;
5183 -- If a previous choice matches, then that is the case where
5184 -- we know our choice is statically unevaluated.
5186 if Choice_Matches
(Left_Opnd
(P
), Choice
) = Match
then
5193 -- If we fall through the loop, we were not one of the choices,
5194 -- we must have been the expression, so that is not covered by
5195 -- this rule, and we keep going.
5201 -- OK, not statically unevaluated at this level, see if we should
5202 -- keep climbing to look for a higher level reason.
5204 -- Special case for component association in aggregates, where
5205 -- we want to keep climbing up to the parent aggregate.
5207 if Nkind
(P
) = N_Component_Association
5208 and then Nkind
(Parent
(P
)) = N_Aggregate
5212 -- All done if not still within subexpression
5215 exit when Nkind
(P
) not in N_Subexpr
;
5219 -- If we fall through the loop, not one of the cases covered!
5222 end Is_Statically_Unevaluated
;
5224 --------------------
5225 -- Not_Null_Range --
5226 --------------------
5228 function Not_Null_Range
(Lo
: Node_Id
; Hi
: Node_Id
) return Boolean is
5229 Typ
: constant Entity_Id
:= Etype
(Lo
);
5232 if not Compile_Time_Known_Value
(Lo
)
5233 or else not Compile_Time_Known_Value
(Hi
)
5238 if Is_Discrete_Type
(Typ
) then
5239 return Expr_Value
(Lo
) <= Expr_Value
(Hi
);
5240 else pragma Assert
(Is_Real_Type
(Typ
));
5241 return Expr_Value_R
(Lo
) <= Expr_Value_R
(Hi
);
5249 function OK_Bits
(N
: Node_Id
; Bits
: Uint
) return Boolean is
5251 -- We allow a maximum of 500,000 bits which seems a reasonable limit
5253 if Bits
< 500_000
then
5256 -- Error if this maximum is exceeded
5259 Error_Msg_N
("static value too large, capacity exceeded", N
);
5268 procedure Out_Of_Range
(N
: Node_Id
) is
5270 -- If we have the static expression case, then this is an illegality
5271 -- in Ada 95 mode, except that in an instance, we never generate an
5272 -- error (if the error is legitimate, it was already diagnosed in the
5275 if Is_Static_Expression
(N
)
5276 and then not In_Instance
5277 and then not In_Inlined_Body
5278 and then Ada_Version
>= Ada_95
5280 -- No message if we are statically unevaluated
5282 if Is_Statically_Unevaluated
(N
) then
5285 -- The expression to compute the length of a packed array is attached
5286 -- to the array type itself, and deserves a separate message.
5288 elsif Nkind
(Parent
(N
)) = N_Defining_Identifier
5289 and then Is_Array_Type
(Parent
(N
))
5290 and then Present
(Packed_Array_Impl_Type
(Parent
(N
)))
5291 and then Present
(First_Rep_Item
(Parent
(N
)))
5294 ("length of packed array must not exceed Integer''Last",
5295 First_Rep_Item
(Parent
(N
)));
5296 Rewrite
(N
, Make_Integer_Literal
(Sloc
(N
), Uint_1
));
5298 -- All cases except the special array case
5301 Apply_Compile_Time_Constraint_Error
5302 (N
, "value not in range of}", CE_Range_Check_Failed
);
5305 -- Here we generate a warning for the Ada 83 case, or when we are in an
5306 -- instance, or when we have a non-static expression case.
5309 Apply_Compile_Time_Constraint_Error
5310 (N
, "value not in range of}??", CE_Range_Check_Failed
);
5314 ----------------------
5315 -- Predicates_Match --
5316 ----------------------
5318 function Predicates_Match
(T1
, T2
: Entity_Id
) return Boolean is
5323 if Ada_Version
< Ada_2012
then
5326 -- Both types must have predicates or lack them
5328 elsif Has_Predicates
(T1
) /= Has_Predicates
(T2
) then
5331 -- Check matching predicates
5336 (T1
, Name_Static_Predicate
, Check_Parents
=> False);
5339 (T2
, Name_Static_Predicate
, Check_Parents
=> False);
5341 -- Subtypes statically match if the predicate comes from the
5342 -- same declaration, which can only happen if one is a subtype
5343 -- of the other and has no explicit predicate.
5345 -- Suppress warnings on order of actuals, which is otherwise
5346 -- triggered by one of the two calls below.
5348 pragma Warnings
(Off
);
5349 return Pred1
= Pred2
5350 or else (No
(Pred1
) and then Is_Subtype_Of
(T1
, T2
))
5351 or else (No
(Pred2
) and then Is_Subtype_Of
(T2
, T1
));
5352 pragma Warnings
(On
);
5354 end Predicates_Match
;
5356 ---------------------------------------------
5357 -- Real_Or_String_Static_Predicate_Matches --
5358 ---------------------------------------------
5360 function Real_Or_String_Static_Predicate_Matches
5362 Typ
: Entity_Id
) return Boolean
5364 Expr
: constant Node_Id
:= Static_Real_Or_String_Predicate
(Typ
);
5365 -- The predicate expression from the type
5367 Pfun
: constant Entity_Id
:= Predicate_Function
(Typ
);
5368 -- The entity for the predicate function
5370 Ent_Name
: constant Name_Id
:= Chars
(First_Formal
(Pfun
));
5371 -- The name of the formal of the predicate function. Occurrences of the
5372 -- type name in Expr have been rewritten as references to this formal,
5373 -- and it has a unique name, so we can identify references by this name.
5376 -- Copy of the predicate function tree
5378 function Process
(N
: Node_Id
) return Traverse_Result
;
5379 -- Function used to process nodes during the traversal in which we will
5380 -- find occurrences of the entity name, and replace such occurrences
5381 -- by a real literal with the value to be tested.
5383 procedure Traverse
is new Traverse_Proc
(Process
);
5384 -- The actual traversal procedure
5390 function Process
(N
: Node_Id
) return Traverse_Result
is
5392 if Nkind
(N
) = N_Identifier
and then Chars
(N
) = Ent_Name
then
5394 Nod
: constant Node_Id
:= New_Copy
(Val
);
5396 Set_Sloc
(Nod
, Sloc
(N
));
5406 -- Start of processing for Real_Or_String_Static_Predicate_Matches
5409 -- First deal with special case of inherited predicate, where the
5410 -- predicate expression looks like:
5412 -- Expr and then xxPredicate (typ (Ent))
5414 -- where Expr is the predicate expression for this level, and the
5415 -- right operand is the call to evaluate the inherited predicate.
5417 if Nkind
(Expr
) = N_And_Then
5418 and then Nkind
(Right_Opnd
(Expr
)) = N_Function_Call
5420 -- OK we have the inherited case, so make a call to evaluate the
5421 -- inherited predicate. If that fails, so do we!
5424 Real_Or_String_Static_Predicate_Matches
5426 Typ
=> Etype
(First_Formal
(Entity
(Name
(Right_Opnd
(Expr
))))))
5431 -- Use the left operand for the continued processing
5433 Copy
:= Copy_Separate_Tree
(Left_Opnd
(Expr
));
5435 -- Case where call to predicate function appears on its own (this means
5436 -- that the predicate at this level is just inherited from the parent).
5438 elsif Nkind
(Expr
) = N_Function_Call
then
5440 Typ
: constant Entity_Id
:=
5441 Etype
(First_Formal
(Entity
(Name
(Expr
))));
5444 -- If the inherited predicate is dynamic, just ignore it. We can't
5445 -- go trying to evaluate a dynamic predicate as a static one!
5447 if Has_Dynamic_Predicate_Aspect
(Typ
) then
5450 -- Otherwise inherited predicate is static, check for match
5453 return Real_Or_String_Static_Predicate_Matches
(Val
, Typ
);
5457 -- If not just an inherited predicate, copy whole expression
5460 Copy
:= Copy_Separate_Tree
(Expr
);
5463 -- Now we replace occurrences of the entity by the value
5467 -- And analyze the resulting static expression to see if it is True
5469 Analyze_And_Resolve
(Copy
, Standard_Boolean
);
5470 return Is_True
(Expr_Value
(Copy
));
5471 end Real_Or_String_Static_Predicate_Matches
;
5473 -------------------------
5474 -- Rewrite_In_Raise_CE --
5475 -------------------------
5477 procedure Rewrite_In_Raise_CE
(N
: Node_Id
; Exp
: Node_Id
) is
5478 Typ
: constant Entity_Id
:= Etype
(N
);
5479 Stat
: constant Boolean := Is_Static_Expression
(N
);
5482 -- If we want to raise CE in the condition of a N_Raise_CE node, we
5483 -- can just clear the condition if the reason is appropriate. We do
5484 -- not do this operation if the parent has a reason other than range
5485 -- check failed, because otherwise we would change the reason.
5487 if Present
(Parent
(N
))
5488 and then Nkind
(Parent
(N
)) = N_Raise_Constraint_Error
5489 and then Reason
(Parent
(N
)) =
5490 UI_From_Int
(RT_Exception_Code
'Pos (CE_Range_Check_Failed
))
5492 Set_Condition
(Parent
(N
), Empty
);
5494 -- Else build an explicit N_Raise_CE
5498 Make_Raise_Constraint_Error
(Sloc
(Exp
),
5499 Reason
=> CE_Range_Check_Failed
));
5500 Set_Raises_Constraint_Error
(N
);
5504 -- Set proper flags in result
5506 Set_Raises_Constraint_Error
(N
, True);
5507 Set_Is_Static_Expression
(N
, Stat
);
5508 end Rewrite_In_Raise_CE
;
5510 ---------------------
5511 -- String_Type_Len --
5512 ---------------------
5514 function String_Type_Len
(Stype
: Entity_Id
) return Uint
is
5515 NT
: constant Entity_Id
:= Etype
(First_Index
(Stype
));
5519 if Is_OK_Static_Subtype
(NT
) then
5522 T
:= Base_Type
(NT
);
5525 return Expr_Value
(Type_High_Bound
(T
)) -
5526 Expr_Value
(Type_Low_Bound
(T
)) + 1;
5527 end String_Type_Len
;
5529 ------------------------------------
5530 -- Subtypes_Statically_Compatible --
5531 ------------------------------------
5533 function Subtypes_Statically_Compatible
5536 Formal_Derived_Matching
: Boolean := False) return Boolean
5541 if Is_Scalar_Type
(T1
) then
5543 -- Definitely compatible if we match
5545 if Subtypes_Statically_Match
(T1
, T2
) then
5548 -- If either subtype is nonstatic then they're not compatible
5550 elsif not Is_OK_Static_Subtype
(T1
)
5552 not Is_OK_Static_Subtype
(T2
)
5556 -- If either type has constraint error bounds, then consider that
5557 -- they match to avoid junk cascaded errors here.
5559 elsif not Is_OK_Static_Subtype
(T1
)
5560 or else not Is_OK_Static_Subtype
(T2
)
5564 -- Base types must match, but we don't check that (should we???) but
5565 -- we do at least check that both types are real, or both types are
5568 elsif Is_Real_Type
(T1
) /= Is_Real_Type
(T2
) then
5571 -- Here we check the bounds
5575 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5576 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5577 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5578 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5581 if Is_Real_Type
(T1
) then
5583 (Expr_Value_R
(LB1
) > Expr_Value_R
(HB1
))
5585 (Expr_Value_R
(LB2
) <= Expr_Value_R
(LB1
)
5587 Expr_Value_R
(HB1
) <= Expr_Value_R
(HB2
));
5591 (Expr_Value
(LB1
) > Expr_Value
(HB1
))
5593 (Expr_Value
(LB2
) <= Expr_Value
(LB1
)
5595 Expr_Value
(HB1
) <= Expr_Value
(HB2
));
5602 elsif Is_Access_Type
(T1
) then
5603 return (not Is_Constrained
(T2
)
5604 or else (Subtypes_Statically_Match
5605 (Designated_Type
(T1
), Designated_Type
(T2
))))
5606 and then not (Can_Never_Be_Null
(T2
)
5607 and then not Can_Never_Be_Null
(T1
));
5612 return (Is_Composite_Type
(T1
) and then not Is_Constrained
(T2
))
5613 or else Subtypes_Statically_Match
(T1
, T2
, Formal_Derived_Matching
);
5615 end Subtypes_Statically_Compatible
;
5617 -------------------------------
5618 -- Subtypes_Statically_Match --
5619 -------------------------------
5621 -- Subtypes statically match if they have statically matching constraints
5622 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
5623 -- they are the same identical constraint, or if they are static and the
5624 -- values match (RM 4.9.1(1)).
5626 -- In addition, in GNAT, the object size (Esize) values of the types must
5627 -- match if they are set (unless checking an actual for a formal derived
5628 -- type). The use of 'Object_Size can cause this to be false even if the
5629 -- types would otherwise match in the RM sense.
5631 function Subtypes_Statically_Match
5634 Formal_Derived_Matching
: Boolean := False) return Boolean
5637 -- A type always statically matches itself
5642 -- No match if sizes different (from use of 'Object_Size). This test
5643 -- is excluded if Formal_Derived_Matching is True, as the base types
5644 -- can be different in that case and typically have different sizes
5645 -- (and Esizes can be set when Frontend_Layout_On_Target is True).
5647 elsif not Formal_Derived_Matching
5648 and then Known_Static_Esize
(T1
)
5649 and then Known_Static_Esize
(T2
)
5650 and then Esize
(T1
) /= Esize
(T2
)
5654 -- No match if predicates do not match
5656 elsif not Predicates_Match
(T1
, T2
) then
5661 elsif Is_Scalar_Type
(T1
) then
5663 -- Base types must be the same
5665 if Base_Type
(T1
) /= Base_Type
(T2
) then
5669 -- A constrained numeric subtype never matches an unconstrained
5670 -- subtype, i.e. both types must be constrained or unconstrained.
5672 -- To understand the requirement for this test, see RM 4.9.1(1).
5673 -- As is made clear in RM 3.5.4(11), type Integer, for example is
5674 -- a constrained subtype with constraint bounds matching the bounds
5675 -- of its corresponding unconstrained base type. In this situation,
5676 -- Integer and Integer'Base do not statically match, even though
5677 -- they have the same bounds.
5679 -- We only apply this test to types in Standard and types that appear
5680 -- in user programs. That way, we do not have to be too careful about
5681 -- setting Is_Constrained right for Itypes.
5683 if Is_Numeric_Type
(T1
)
5684 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5685 and then (Scope
(T1
) = Standard_Standard
5686 or else Comes_From_Source
(T1
))
5687 and then (Scope
(T2
) = Standard_Standard
5688 or else Comes_From_Source
(T2
))
5692 -- A generic scalar type does not statically match its base type
5693 -- (AI-311). In this case we make sure that the formals, which are
5694 -- first subtypes of their bases, are constrained.
5696 elsif Is_Generic_Type
(T1
)
5697 and then Is_Generic_Type
(T2
)
5698 and then (Is_Constrained
(T1
) /= Is_Constrained
(T2
))
5703 -- If there was an error in either range, then just assume the types
5704 -- statically match to avoid further junk errors.
5706 if No
(Scalar_Range
(T1
)) or else No
(Scalar_Range
(T2
))
5707 or else Error_Posted
(Scalar_Range
(T1
))
5708 or else Error_Posted
(Scalar_Range
(T2
))
5713 -- Otherwise both types have bounds that can be compared
5716 LB1
: constant Node_Id
:= Type_Low_Bound
(T1
);
5717 HB1
: constant Node_Id
:= Type_High_Bound
(T1
);
5718 LB2
: constant Node_Id
:= Type_Low_Bound
(T2
);
5719 HB2
: constant Node_Id
:= Type_High_Bound
(T2
);
5722 -- If the bounds are the same tree node, then match (common case)
5724 if LB1
= LB2
and then HB1
= HB2
then
5727 -- Otherwise bounds must be static and identical value
5730 if not Is_OK_Static_Subtype
(T1
)
5731 or else not Is_OK_Static_Subtype
(T2
)
5735 -- If either type has constraint error bounds, then say that
5736 -- they match to avoid junk cascaded errors here.
5738 elsif not Is_OK_Static_Subtype
(T1
)
5739 or else not Is_OK_Static_Subtype
(T2
)
5743 elsif Is_Real_Type
(T1
) then
5745 (Expr_Value_R
(LB1
) = Expr_Value_R
(LB2
))
5747 (Expr_Value_R
(HB1
) = Expr_Value_R
(HB2
));
5751 Expr_Value
(LB1
) = Expr_Value
(LB2
)
5753 Expr_Value
(HB1
) = Expr_Value
(HB2
);
5758 -- Type with discriminants
5760 elsif Has_Discriminants
(T1
) or else Has_Discriminants
(T2
) then
5762 -- Because of view exchanges in multiple instantiations, conformance
5763 -- checking might try to match a partial view of a type with no
5764 -- discriminants with a full view that has defaulted discriminants.
5765 -- In such a case, use the discriminant constraint of the full view,
5766 -- which must exist because we know that the two subtypes have the
5769 if Has_Discriminants
(T1
) /= Has_Discriminants
(T2
) then
5770 -- A generic actual type is declared through a subtype declaration
5771 -- and may have an inconsistent indication of the presence of
5772 -- discriminants, so check the type it renames.
5774 if Is_Generic_Actual_Type
(T1
)
5775 and then not Has_Discriminants
(Etype
(T1
))
5776 and then not Has_Discriminants
(T2
)
5780 elsif In_Instance
then
5781 if Is_Private_Type
(T2
)
5782 and then Present
(Full_View
(T2
))
5783 and then Has_Discriminants
(Full_View
(T2
))
5785 return Subtypes_Statically_Match
(T1
, Full_View
(T2
));
5787 elsif Is_Private_Type
(T1
)
5788 and then Present
(Full_View
(T1
))
5789 and then Has_Discriminants
(Full_View
(T1
))
5791 return Subtypes_Statically_Match
(Full_View
(T1
), T2
);
5802 DL1
: constant Elist_Id
:= Discriminant_Constraint
(T1
);
5803 DL2
: constant Elist_Id
:= Discriminant_Constraint
(T2
);
5811 elsif Is_Constrained
(T1
) /= Is_Constrained
(T2
) then
5815 -- Now loop through the discriminant constraints
5817 -- Note: the guard here seems necessary, since it is possible at
5818 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
5820 if Present
(DL1
) and then Present
(DL2
) then
5821 DA1
:= First_Elmt
(DL1
);
5822 DA2
:= First_Elmt
(DL2
);
5823 while Present
(DA1
) loop
5825 Expr1
: constant Node_Id
:= Node
(DA1
);
5826 Expr2
: constant Node_Id
:= Node
(DA2
);
5829 if not Is_OK_Static_Expression
(Expr1
)
5830 or else not Is_OK_Static_Expression
(Expr2
)
5834 -- If either expression raised a constraint error,
5835 -- consider the expressions as matching, since this
5836 -- helps to prevent cascading errors.
5838 elsif Raises_Constraint_Error
(Expr1
)
5839 or else Raises_Constraint_Error
(Expr2
)
5843 elsif Expr_Value
(Expr1
) /= Expr_Value
(Expr2
) then
5856 -- A definite type does not match an indefinite or classwide type.
5857 -- However, a generic type with unknown discriminants may be
5858 -- instantiated with a type with no discriminants, and conformance
5859 -- checking on an inherited operation may compare the actual with the
5860 -- subtype that renames it in the instance.
5862 elsif Has_Unknown_Discriminants
(T1
) /= Has_Unknown_Discriminants
(T2
)
5865 Is_Generic_Actual_Type
(T1
) or else Is_Generic_Actual_Type
(T2
);
5869 elsif Is_Array_Type
(T1
) then
5871 -- If either subtype is unconstrained then both must be, and if both
5872 -- are unconstrained then no further checking is needed.
5874 if not Is_Constrained
(T1
) or else not Is_Constrained
(T2
) then
5875 return not (Is_Constrained
(T1
) or else Is_Constrained
(T2
));
5878 -- Both subtypes are constrained, so check that the index subtypes
5879 -- statically match.
5882 Index1
: Node_Id
:= First_Index
(T1
);
5883 Index2
: Node_Id
:= First_Index
(T2
);
5886 while Present
(Index1
) loop
5888 Subtypes_Statically_Match
(Etype
(Index1
), Etype
(Index2
))
5893 Next_Index
(Index1
);
5894 Next_Index
(Index2
);
5900 elsif Is_Access_Type
(T1
) then
5901 if Can_Never_Be_Null
(T1
) /= Can_Never_Be_Null
(T2
) then
5904 elsif Ekind_In
(T1
, E_Access_Subprogram_Type
,
5905 E_Anonymous_Access_Subprogram_Type
)
5909 (Designated_Type
(T1
),
5910 Designated_Type
(T2
));
5913 Subtypes_Statically_Match
5914 (Designated_Type
(T1
),
5915 Designated_Type
(T2
))
5916 and then Is_Access_Constant
(T1
) = Is_Access_Constant
(T2
);
5919 -- All other types definitely match
5924 end Subtypes_Statically_Match
;
5930 function Test
(Cond
: Boolean) return Uint
is
5939 ---------------------------------
5940 -- Test_Expression_Is_Foldable --
5941 ---------------------------------
5945 procedure Test_Expression_Is_Foldable
5955 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
5959 -- If operand is Any_Type, just propagate to result and do not
5960 -- try to fold, this prevents cascaded errors.
5962 if Etype
(Op1
) = Any_Type
then
5963 Set_Etype
(N
, Any_Type
);
5966 -- If operand raises constraint error, then replace node N with the
5967 -- raise constraint error node, and we are obviously not foldable.
5968 -- Note that this replacement inherits the Is_Static_Expression flag
5969 -- from the operand.
5971 elsif Raises_Constraint_Error
(Op1
) then
5972 Rewrite_In_Raise_CE
(N
, Op1
);
5975 -- If the operand is not static, then the result is not static, and
5976 -- all we have to do is to check the operand since it is now known
5977 -- to appear in a non-static context.
5979 elsif not Is_Static_Expression
(Op1
) then
5980 Check_Non_Static_Context
(Op1
);
5981 Fold
:= Compile_Time_Known_Value
(Op1
);
5984 -- An expression of a formal modular type is not foldable because
5985 -- the modulus is unknown.
5987 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
5988 and then Is_Generic_Type
(Etype
(Op1
))
5990 Check_Non_Static_Context
(Op1
);
5993 -- Here we have the case of an operand whose type is OK, which is
5994 -- static, and which does not raise constraint error, we can fold.
5997 Set_Is_Static_Expression
(N
);
6001 end Test_Expression_Is_Foldable
;
6005 procedure Test_Expression_Is_Foldable
6011 CRT_Safe
: Boolean := False)
6013 Rstat
: constant Boolean := Is_Static_Expression
(Op1
)
6015 Is_Static_Expression
(Op2
);
6021 -- Inhibit folding if -gnatd.f flag set
6023 if Debug_Flag_Dot_F
and then In_Extended_Main_Source_Unit
(N
) then
6027 -- If either operand is Any_Type, just propagate to result and
6028 -- do not try to fold, this prevents cascaded errors.
6030 if Etype
(Op1
) = Any_Type
or else Etype
(Op2
) = Any_Type
then
6031 Set_Etype
(N
, Any_Type
);
6034 -- If left operand raises constraint error, then replace node N with the
6035 -- Raise_Constraint_Error node, and we are obviously not foldable.
6036 -- Is_Static_Expression is set from the two operands in the normal way,
6037 -- and we check the right operand if it is in a non-static context.
6039 elsif Raises_Constraint_Error
(Op1
) then
6041 Check_Non_Static_Context
(Op2
);
6044 Rewrite_In_Raise_CE
(N
, Op1
);
6045 Set_Is_Static_Expression
(N
, Rstat
);
6048 -- Similar processing for the case of the right operand. Note that we
6049 -- don't use this routine for the short-circuit case, so we do not have
6050 -- to worry about that special case here.
6052 elsif Raises_Constraint_Error
(Op2
) then
6054 Check_Non_Static_Context
(Op1
);
6057 Rewrite_In_Raise_CE
(N
, Op2
);
6058 Set_Is_Static_Expression
(N
, Rstat
);
6061 -- Exclude expressions of a generic modular type, as above
6063 elsif Is_Modular_Integer_Type
(Etype
(Op1
))
6064 and then Is_Generic_Type
(Etype
(Op1
))
6066 Check_Non_Static_Context
(Op1
);
6069 -- If result is not static, then check non-static contexts on operands
6070 -- since one of them may be static and the other one may not be static.
6072 elsif not Rstat
then
6073 Check_Non_Static_Context
(Op1
);
6074 Check_Non_Static_Context
(Op2
);
6077 Fold
:= CRT_Safe_Compile_Time_Known_Value
(Op1
)
6078 and then CRT_Safe_Compile_Time_Known_Value
(Op2
);
6080 Fold
:= Compile_Time_Known_Value
(Op1
)
6081 and then Compile_Time_Known_Value
(Op2
);
6086 -- Else result is static and foldable. Both operands are static, and
6087 -- neither raises constraint error, so we can definitely fold.
6090 Set_Is_Static_Expression
(N
);
6095 end Test_Expression_Is_Foldable
;
6101 function Test_In_Range
6104 Assume_Valid
: Boolean;
6105 Fixed_Int
: Boolean;
6106 Int_Real
: Boolean) return Range_Membership
6111 pragma Warnings
(Off
, Assume_Valid
);
6112 -- For now Assume_Valid is unreferenced since the current implementation
6113 -- always returns Unknown if N is not a compile time known value, but we
6114 -- keep the parameter to allow for future enhancements in which we try
6115 -- to get the information in the variable case as well.
6118 -- If an error was posted on expression, then return Unknown, we do not
6119 -- want cascaded errors based on some false analysis of a junk node.
6121 if Error_Posted
(N
) then
6124 -- Expression that raises constraint error is an odd case. We certainly
6125 -- do not want to consider it to be in range. It might make sense to
6126 -- consider it always out of range, but this causes incorrect error
6127 -- messages about static expressions out of range. So we just return
6128 -- Unknown, which is always safe.
6130 elsif Raises_Constraint_Error
(N
) then
6133 -- Universal types have no range limits, so always in range
6135 elsif Typ
= Universal_Integer
or else Typ
= Universal_Real
then
6138 -- Never known if not scalar type. Don't know if this can actually
6139 -- happen, but our spec allows it, so we must check.
6141 elsif not Is_Scalar_Type
(Typ
) then
6144 -- Never known if this is a generic type, since the bounds of generic
6145 -- types are junk. Note that if we only checked for static expressions
6146 -- (instead of compile time known values) below, we would not need this
6147 -- check, because values of a generic type can never be static, but they
6148 -- can be known at compile time.
6150 elsif Is_Generic_Type
(Typ
) then
6153 -- Case of a known compile time value, where we can check if it is in
6154 -- the bounds of the given type.
6156 elsif Compile_Time_Known_Value
(N
) then
6165 Lo
:= Type_Low_Bound
(Typ
);
6166 Hi
:= Type_High_Bound
(Typ
);
6168 LB_Known
:= Compile_Time_Known_Value
(Lo
);
6169 HB_Known
:= Compile_Time_Known_Value
(Hi
);
6171 -- Fixed point types should be considered as such only if flag
6172 -- Fixed_Int is set to False.
6174 if Is_Floating_Point_Type
(Typ
)
6175 or else (Is_Fixed_Point_Type
(Typ
) and then not Fixed_Int
)
6178 Valr
:= Expr_Value_R
(N
);
6180 if LB_Known
and HB_Known
then
6181 if Valr
>= Expr_Value_R
(Lo
)
6183 Valr
<= Expr_Value_R
(Hi
)
6187 return Out_Of_Range
;
6190 elsif (LB_Known
and then Valr
< Expr_Value_R
(Lo
))
6192 (HB_Known
and then Valr
> Expr_Value_R
(Hi
))
6194 return Out_Of_Range
;
6201 Val
:= Expr_Value
(N
);
6203 if LB_Known
and HB_Known
then
6204 if Val
>= Expr_Value
(Lo
) and then Val
<= Expr_Value
(Hi
)
6208 return Out_Of_Range
;
6211 elsif (LB_Known
and then Val
< Expr_Value
(Lo
))
6213 (HB_Known
and then Val
> Expr_Value
(Hi
))
6215 return Out_Of_Range
;
6223 -- Here for value not known at compile time. Case of expression subtype
6224 -- is Typ or is a subtype of Typ, and we can assume expression is valid.
6225 -- In this case we know it is in range without knowing its value.
6228 and then (Etype
(N
) = Typ
or else Is_Subtype_Of
(Etype
(N
), Typ
))
6232 -- Another special case. For signed integer types, if the target type
6233 -- has Is_Known_Valid set, and the source type does not have a larger
6234 -- size, then the source value must be in range. We exclude biased
6235 -- types, because they bizarrely can generate out of range values.
6237 elsif Is_Signed_Integer_Type
(Etype
(N
))
6238 and then Is_Known_Valid
(Typ
)
6239 and then Esize
(Etype
(N
)) <= Esize
(Typ
)
6240 and then not Has_Biased_Representation
(Etype
(N
))
6242 -- This check cannot be disabled under VM targets because in some
6243 -- unusual cases the backend of the native compiler raises a run-time
6244 -- exception but the virtual machines do not raise any exception.
6246 and then VM_Target
= No_VM
6250 -- For all other cases, result is unknown
6261 procedure To_Bits
(U
: Uint
; B
: out Bits
) is
6263 for J
in 0 .. B
'Last loop
6264 B
(J
) := (U
/ (2 ** J
)) mod 2 /= 0;
6268 --------------------
6269 -- Why_Not_Static --
6270 --------------------
6272 procedure Why_Not_Static
(Expr
: Node_Id
) is
6273 N
: constant Node_Id
:= Original_Node
(Expr
);
6279 procedure Why_Not_Static_List
(L
: List_Id
);
6280 -- A version that can be called on a list of expressions. Finds all
6281 -- non-static violations in any element of the list.
6283 -------------------------
6284 -- Why_Not_Static_List --
6285 -------------------------
6287 procedure Why_Not_Static_List
(L
: List_Id
) is
6290 if Is_Non_Empty_List
(L
) then
6292 while Present
(N
) loop
6297 end Why_Not_Static_List
;
6299 -- Start of processing for Why_Not_Static
6302 -- Ignore call on error or empty node
6304 if No
(Expr
) or else Nkind
(Expr
) = N_Error
then
6308 -- Preprocessing for sub expressions
6310 if Nkind
(Expr
) in N_Subexpr
then
6312 -- Nothing to do if expression is static
6314 if Is_OK_Static_Expression
(Expr
) then
6318 -- Test for constraint error raised
6320 if Raises_Constraint_Error
(Expr
) then
6322 -- Special case membership to find out which piece to flag
6324 if Nkind
(N
) in N_Membership_Test
then
6325 if Raises_Constraint_Error
(Left_Opnd
(N
)) then
6326 Why_Not_Static
(Left_Opnd
(N
));
6329 elsif Present
(Right_Opnd
(N
))
6330 and then Raises_Constraint_Error
(Right_Opnd
(N
))
6332 Why_Not_Static
(Right_Opnd
(N
));
6336 pragma Assert
(Present
(Alternatives
(N
)));
6338 Alt
:= First
(Alternatives
(N
));
6339 while Present
(Alt
) loop
6340 if Raises_Constraint_Error
(Alt
) then
6341 Why_Not_Static
(Alt
);
6349 -- Special case a range to find out which bound to flag
6351 elsif Nkind
(N
) = N_Range
then
6352 if Raises_Constraint_Error
(Low_Bound
(N
)) then
6353 Why_Not_Static
(Low_Bound
(N
));
6356 elsif Raises_Constraint_Error
(High_Bound
(N
)) then
6357 Why_Not_Static
(High_Bound
(N
));
6361 -- Special case attribute to see which part to flag
6363 elsif Nkind
(N
) = N_Attribute_Reference
then
6364 if Raises_Constraint_Error
(Prefix
(N
)) then
6365 Why_Not_Static
(Prefix
(N
));
6369 if Present
(Expressions
(N
)) then
6370 Exp
:= First
(Expressions
(N
));
6371 while Present
(Exp
) loop
6372 if Raises_Constraint_Error
(Exp
) then
6373 Why_Not_Static
(Exp
);
6381 -- Special case a subtype name
6383 elsif Is_Entity_Name
(Expr
) and then Is_Type
(Entity
(Expr
)) then
6385 ("!& is not a static subtype (RM 4.9(26))", N
, Entity
(Expr
));
6389 -- End of special cases
6392 ("!expression raises exception, cannot be static (RM 4.9(34))",
6397 -- If no type, then something is pretty wrong, so ignore
6399 Typ
:= Etype
(Expr
);
6405 -- Type must be scalar or string type (but allow Bignum, since this
6406 -- is really a scalar type from our point of view in this diagnosis).
6408 if not Is_Scalar_Type
(Typ
)
6409 and then not Is_String_Type
(Typ
)
6410 and then not Is_RTE
(Typ
, RE_Bignum
)
6413 ("!static expression must have scalar or string type " &
6419 -- If we got through those checks, test particular node kind
6425 when N_Expanded_Name | N_Identifier | N_Operator_Symbol
=>
6428 if Is_Named_Number
(E
) then
6431 elsif Ekind
(E
) = E_Constant
then
6433 -- One case we can give a metter message is when we have a
6434 -- string literal created by concatenating an aggregate with
6435 -- an others expression.
6437 Entity_Case
: declare
6438 CV
: constant Node_Id
:= Constant_Value
(E
);
6439 CO
: constant Node_Id
:= Original_Node
(CV
);
6441 function Is_Aggregate
(N
: Node_Id
) return Boolean;
6442 -- See if node N came from an others aggregate, if so
6443 -- return True and set Error_Msg_Sloc to aggregate.
6449 function Is_Aggregate
(N
: Node_Id
) return Boolean is
6451 if Nkind
(Original_Node
(N
)) = N_Aggregate
then
6452 Error_Msg_Sloc
:= Sloc
(Original_Node
(N
));
6455 elsif Is_Entity_Name
(N
)
6456 and then Ekind
(Entity
(N
)) = E_Constant
6458 Nkind
(Original_Node
(Constant_Value
(Entity
(N
)))) =
6462 Sloc
(Original_Node
(Constant_Value
(Entity
(N
))));
6470 -- Start of processing for Entity_Case
6473 if Is_Aggregate
(CV
)
6474 or else (Nkind
(CO
) = N_Op_Concat
6475 and then (Is_Aggregate
(Left_Opnd
(CO
))
6477 Is_Aggregate
(Right_Opnd
(CO
))))
6479 Error_Msg_N
("!aggregate (#) is never static", N
);
6481 elsif No
(CV
) or else not Is_Static_Expression
(CV
) then
6483 ("!& is not a static constant (RM 4.9(5))", N
, E
);
6487 elsif Is_Type
(E
) then
6489 ("!& is not a static subtype (RM 4.9(26))", N
, E
);
6493 ("!& is not static constant or named number "
6494 & "(RM 4.9(5))", N
, E
);
6499 when N_Binary_Op | N_Short_Circuit | N_Membership_Test
=>
6500 if Nkind
(N
) in N_Op_Shift
then
6502 ("!shift functions are never static (RM 4.9(6,18))", N
);
6504 Why_Not_Static
(Left_Opnd
(N
));
6505 Why_Not_Static
(Right_Opnd
(N
));
6511 Why_Not_Static
(Right_Opnd
(N
));
6513 -- Attribute reference
6515 when N_Attribute_Reference
=>
6516 Why_Not_Static_List
(Expressions
(N
));
6518 E
:= Etype
(Prefix
(N
));
6520 if E
= Standard_Void_Type
then
6524 -- Special case non-scalar'Size since this is a common error
6526 if Attribute_Name
(N
) = Name_Size
then
6528 ("!size attribute is only static for static scalar type "
6529 & "(RM 4.9(7,8))", N
);
6533 elsif Is_Array_Type
(E
) then
6534 if not Nam_In
(Attribute_Name
(N
), Name_First
,
6539 ("!static array attribute must be Length, First, or Last "
6540 & "(RM 4.9(8))", N
);
6542 -- Since we know the expression is not-static (we already
6543 -- tested for this, must mean array is not static).
6547 ("!prefix is non-static array (RM 4.9(8))", Prefix
(N
));
6552 -- Special case generic types, since again this is a common source
6555 elsif Is_Generic_Actual_Type
(E
) or else Is_Generic_Type
(E
) then
6557 ("!attribute of generic type is never static "
6558 & "(RM 4.9(7,8))", N
);
6560 elsif Is_OK_Static_Subtype
(E
) then
6563 elsif Is_Scalar_Type
(E
) then
6565 ("!prefix type for attribute is not static scalar subtype "
6566 & "(RM 4.9(7))", N
);
6570 ("!static attribute must apply to array/scalar type "
6571 & "(RM 4.9(7,8))", N
);
6576 when N_String_Literal
=>
6578 ("!subtype of string literal is non-static (RM 4.9(4))", N
);
6580 -- Explicit dereference
6582 when N_Explicit_Dereference
=>
6584 ("!explicit dereference is never static (RM 4.9)", N
);
6588 when N_Function_Call
=>
6589 Why_Not_Static_List
(Parameter_Associations
(N
));
6591 -- Complain about non-static function call unless we have Bignum
6592 -- which means that the underlying expression is really some
6593 -- scalar arithmetic operation.
6595 if not Is_RTE
(Typ
, RE_Bignum
) then
6596 Error_Msg_N
("!non-static function call (RM 4.9(6,18))", N
);
6599 -- Parameter assocation (test actual parameter)
6601 when N_Parameter_Association
=>
6602 Why_Not_Static
(Explicit_Actual_Parameter
(N
));
6604 -- Indexed component
6606 when N_Indexed_Component
=>
6607 Error_Msg_N
("!indexed component is never static (RM 4.9)", N
);
6611 when N_Procedure_Call_Statement
=>
6612 Error_Msg_N
("!procedure call is never static (RM 4.9)", N
);
6614 -- Qualified expression (test expression)
6616 when N_Qualified_Expression
=>
6617 Why_Not_Static
(Expression
(N
));
6621 when N_Aggregate | N_Extension_Aggregate
=>
6622 Error_Msg_N
("!an aggregate is never static (RM 4.9)", N
);
6627 Why_Not_Static
(Low_Bound
(N
));
6628 Why_Not_Static
(High_Bound
(N
));
6630 -- Range constraint, test range expression
6632 when N_Range_Constraint
=>
6633 Why_Not_Static
(Range_Expression
(N
));
6635 -- Subtype indication, test constraint
6637 when N_Subtype_Indication
=>
6638 Why_Not_Static
(Constraint
(N
));
6640 -- Selected component
6642 when N_Selected_Component
=>
6643 Error_Msg_N
("!selected component is never static (RM 4.9)", N
);
6648 Error_Msg_N
("!slice is never static (RM 4.9)", N
);
6650 when N_Type_Conversion
=>
6651 Why_Not_Static
(Expression
(N
));
6653 if not Is_Scalar_Type
(Entity
(Subtype_Mark
(N
)))
6654 or else not Is_OK_Static_Subtype
(Entity
(Subtype_Mark
(N
)))
6657 ("!static conversion requires static scalar subtype result "
6658 & "(RM 4.9(9))", N
);
6661 -- Unchecked type conversion
6663 when N_Unchecked_Type_Conversion
=>
6665 ("!unchecked type conversion is never static (RM 4.9)", N
);
6667 -- All other cases, no reason to give