]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_eval.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_eval.adb
CommitLineData
fbf5a39b 1------------------------------------------------------------------------------
996ae0b0
RK
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ E V A L --
6-- --
7-- B o d y --
8-- --
011f9d5d 9-- Copyright (C) 1992-2012, Free Software Foundation, Inc. --
996ae0b0
RK
10-- --
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- --
b5c84c3c 13-- ware Foundation; either version 3, or (at your option) any later ver- --
996ae0b0
RK
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 --
b5c84c3c
RD
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. --
996ae0b0
RK
20-- --
21-- GNAT was originally developed by the GNAT team at New York University. --
71ff80dc 22-- Extensive contributions were provided by Ada Core Technologies Inc. --
996ae0b0
RK
23-- --
24------------------------------------------------------------------------------
25
26with Atree; use Atree;
27with Checks; use Checks;
28with Debug; use Debug;
29with Einfo; use Einfo;
30with Elists; use Elists;
31with Errout; use Errout;
32with Eval_Fat; use Eval_Fat;
8cbb664e 33with Exp_Util; use Exp_Util;
d7567964 34with Freeze; use Freeze;
0356699b 35with Lib; use Lib;
13f34a3f 36with Namet; use Namet;
996ae0b0
RK
37with Nmake; use Nmake;
38with Nlists; use Nlists;
39with Opt; use Opt;
40with Sem; use Sem;
a4100e55 41with Sem_Aux; use Sem_Aux;
996ae0b0 42with Sem_Cat; use Sem_Cat;
b5bd964f 43with Sem_Ch6; use Sem_Ch6;
996ae0b0
RK
44with Sem_Ch8; use Sem_Ch8;
45with Sem_Res; use Sem_Res;
46with Sem_Util; use Sem_Util;
47with Sem_Type; use Sem_Type;
48with Sem_Warn; use Sem_Warn;
49with Sinfo; use Sinfo;
50with Snames; use Snames;
51with Stand; use Stand;
52with Stringt; use Stringt;
07fc65c4 53with Tbuild; use Tbuild;
996ae0b0
RK
54
55package body Sem_Eval is
56
57 -----------------------------------------
58 -- Handling of Compile Time Evaluation --
59 -----------------------------------------
60
61 -- The compile time evaluation of expressions is distributed over several
f3d57416 62 -- Eval_xxx procedures. These procedures are called immediately after
996ae0b0
RK
63 -- a subexpression is resolved and is therefore accomplished in a bottom
64 -- up fashion. The flags are synthesized using the following approach.
65
66 -- Is_Static_Expression is determined by following the detailed rules
67 -- in RM 4.9(4-14). This involves testing the Is_Static_Expression
68 -- flag of the operands in many cases.
69
70 -- Raises_Constraint_Error is set if any of the operands have the flag
71 -- set or if an attempt to compute the value of the current expression
72 -- results in detection of a runtime constraint error.
73
74 -- As described in the spec, the requirement is that Is_Static_Expression
75 -- be accurately set, and in addition for nodes for which this flag is set,
76 -- Raises_Constraint_Error must also be set. Furthermore a node which has
77 -- Is_Static_Expression set, and Raises_Constraint_Error clear, then the
78 -- requirement is that the expression value must be precomputed, and the
79 -- node is either a literal, or the name of a constant entity whose value
80 -- is a static expression.
81
82 -- The general approach is as follows. First compute Is_Static_Expression.
83 -- If the node is not static, then the flag is left off in the node and
84 -- we are all done. Otherwise for a static node, we test if any of the
85 -- operands will raise constraint error, and if so, propagate the flag
86 -- Raises_Constraint_Error to the result node and we are done (since the
87 -- error was already posted at a lower level).
88
89 -- For the case of a static node whose operands do not raise constraint
90 -- error, we attempt to evaluate the node. If this evaluation succeeds,
91 -- then the node is replaced by the result of this computation. If the
92 -- evaluation raises constraint error, then we rewrite the node with
93 -- Apply_Compile_Time_Constraint_Error to raise the exception and also
94 -- to post appropriate error messages.
95
96 ----------------
97 -- Local Data --
98 ----------------
99
100 type Bits is array (Nat range <>) of Boolean;
101 -- Used to convert unsigned (modular) values for folding logical ops
102
07fc65c4
GB
103 -- The following definitions are used to maintain a cache of nodes that
104 -- have compile time known values. The cache is maintained only for
105 -- discrete types (the most common case), and is populated by calls to
106 -- Compile_Time_Known_Value and Expr_Value, but only used by Expr_Value
107 -- since it is possible for the status to change (in particular it is
108 -- possible for a node to get replaced by a constraint error node).
109
110 CV_Bits : constant := 5;
111 -- Number of low order bits of Node_Id value used to reference entries
112 -- in the cache table.
113
114 CV_Cache_Size : constant Nat := 2 ** CV_Bits;
115 -- Size of cache for compile time values
116
117 subtype CV_Range is Nat range 0 .. CV_Cache_Size;
118
119 type CV_Entry is record
120 N : Node_Id;
121 V : Uint;
122 end record;
123
124 type CV_Cache_Array is array (CV_Range) of CV_Entry;
125
126 CV_Cache : CV_Cache_Array := (others => (Node_High_Bound, Uint_0));
127 -- This is the actual cache, with entries consisting of node/value pairs,
128 -- and the impossible value Node_High_Bound used for unset entries.
129
305caf42
AC
130 type Range_Membership is (In_Range, Out_Of_Range, Unknown);
131 -- Range membership may either be statically known to be in range or out
132 -- of range, or not statically known. Used for Test_In_Range below.
133
996ae0b0
RK
134 -----------------------
135 -- Local Subprograms --
136 -----------------------
137
996ae0b0
RK
138 function From_Bits (B : Bits; T : Entity_Id) return Uint;
139 -- Converts a bit string of length B'Length to a Uint value to be used
140 -- for a target of type T, which is a modular type. This procedure
141 -- includes the necessary reduction by the modulus in the case of a
142 -- non-binary modulus (for a binary modulus, the bit string is the
143 -- right length any way so all is well).
144
145 function Get_String_Val (N : Node_Id) return Node_Id;
146 -- Given a tree node for a folded string or character value, returns
147 -- the corresponding string literal or character literal (one of the
148 -- two must be available, or the operand would not have been marked
149 -- as foldable in the earlier analysis of the operation).
150
07fc65c4
GB
151 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean;
152 -- Bits represents the number of bits in an integer value to be computed
153 -- (but the value has not been computed yet). If this value in Bits is
154 -- reasonable, a result of True is returned, with the implication that
155 -- the caller should go ahead and complete the calculation. If the value
156 -- in Bits is unreasonably large, then an error is posted on node N, and
157 -- False is returned (and the caller skips the proposed calculation).
158
996ae0b0
RK
159 procedure Out_Of_Range (N : Node_Id);
160 -- This procedure is called if it is determined that node N, which
161 -- appears in a non-static context, is a compile time known value
162 -- which is outside its range, i.e. the range of Etype. This is used
163 -- in contexts where this is an illegality if N is static, and should
164 -- generate a warning otherwise.
165
166 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id);
167 -- N and Exp are nodes representing an expression, Exp is known
168 -- to raise CE. N is rewritten in term of Exp in the optimal way.
169
170 function String_Type_Len (Stype : Entity_Id) return Uint;
171 -- Given a string type, determines the length of the index type, or,
172 -- if this index type is non-static, the length of the base type of
173 -- this index type. Note that if the string type is itself static,
174 -- then the index type is static, so the second case applies only
175 -- if the string type passed is non-static.
176
177 function Test (Cond : Boolean) return Uint;
178 pragma Inline (Test);
179 -- This function simply returns the appropriate Boolean'Pos value
180 -- corresponding to the value of Cond as a universal integer. It is
181 -- used for producing the result of the static evaluation of the
182 -- logical operators
183
d7567964 184 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id;
602a7ec0
AC
185 -- Check whether an arithmetic operation with universal operands which
186 -- is a rewritten function call with an explicit scope indication is
ae77c68b 187 -- ambiguous: P."+" (1, 2) will be ambiguous if there is more than one
602a7ec0
AC
188 -- visible numeric type declared in P and the context does not impose a
189 -- type on the result (e.g. in the expression of a type conversion).
d7567964
TQ
190 -- If ambiguous, emit an error and return Empty, else return the result
191 -- type of the operator.
602a7ec0 192
996ae0b0
RK
193 procedure Test_Expression_Is_Foldable
194 (N : Node_Id;
195 Op1 : Node_Id;
196 Stat : out Boolean;
197 Fold : out Boolean);
198 -- Tests to see if expression N whose single operand is Op1 is foldable,
199 -- i.e. the operand value is known at compile time. If the operation is
200 -- foldable, then Fold is True on return, and Stat indicates whether
201 -- the result is static (i.e. both operands were static). Note that it
202 -- is quite possible for Fold to be True, and Stat to be False, since
203 -- there are cases in which we know the value of an operand even though
204 -- it is not technically static (e.g. the static lower bound of a range
205 -- whose upper bound is non-static).
206 --
3d5952be 207 -- If Stat is set False on return, then Test_Expression_Is_Foldable makes a
996ae0b0
RK
208 -- call to Check_Non_Static_Context on the operand. If Fold is False on
209 -- return, then all processing is complete, and the caller should
210 -- return, since there is nothing else to do.
93c3fca7
AC
211 --
212 -- If Stat is set True on return, then Is_Static_Expression is also set
213 -- true in node N. There are some cases where this is over-enthusiastic,
308e6f3a 214 -- e.g. in the two operand case below, for string comparison, the result
93c3fca7
AC
215 -- is not static even though the two operands are static. In such cases,
216 -- the caller must reset the Is_Static_Expression flag in N.
5df1266a
AC
217 --
218 -- If Fold and Stat are both set to False then this routine performs also
219 -- the following extra actions:
220 --
70805b88
AC
221 -- If either operand is Any_Type then propagate it to result to
222 -- prevent cascaded errors.
5df1266a 223 --
70805b88
AC
224 -- If some operand raises constraint error, then replace the node N
225 -- with the raise constraint error node. This replacement inherits the
226 -- Is_Static_Expression flag from the operands.
996ae0b0
RK
227
228 procedure Test_Expression_Is_Foldable
229 (N : Node_Id;
230 Op1 : Node_Id;
231 Op2 : Node_Id;
232 Stat : out Boolean;
233 Fold : out Boolean);
234 -- Same processing, except applies to an expression N with two operands
235 -- Op1 and Op2.
236
305caf42
AC
237 function Test_In_Range
238 (N : Node_Id;
239 Typ : Entity_Id;
240 Assume_Valid : Boolean;
241 Fixed_Int : Boolean;
242 Int_Real : Boolean) return Range_Membership;
243 -- Common processing for Is_In_Range and Is_Out_Of_Range:
244 -- Returns In_Range or Out_Of_Range if it can be guaranteed at compile time
245 -- that expression N is known to be in or out of range of the subtype Typ.
246 -- If not compile time known, Unknown is returned.
247 -- See documentation of Is_In_Range for complete description of parameters.
248
996ae0b0
RK
249 procedure To_Bits (U : Uint; B : out Bits);
250 -- Converts a Uint value to a bit string of length B'Length
251
252 ------------------------------
253 -- Check_Non_Static_Context --
254 ------------------------------
255
256 procedure Check_Non_Static_Context (N : Node_Id) is
fbf5a39b
AC
257 T : constant Entity_Id := Etype (N);
258 Checks_On : constant Boolean :=
996ae0b0
RK
259 not Index_Checks_Suppressed (T)
260 and not Range_Checks_Suppressed (T);
261
262 begin
86f0e17a
AC
263 -- Ignore cases of non-scalar types, error types, or universal real
264 -- types that have no usable bounds.
996ae0b0 265
86f0e17a
AC
266 if T = Any_Type
267 or else not Is_Scalar_Type (T)
268 or else T = Universal_Fixed
269 or else T = Universal_Real
270 then
996ae0b0 271 return;
fbf5a39b 272 end if;
996ae0b0 273
86f0e17a
AC
274 -- At this stage we have a scalar type. If we have an expression that
275 -- raises CE, then we already issued a warning or error msg so there
276 -- is nothing more to be done in this routine.
fbf5a39b
AC
277
278 if Raises_Constraint_Error (N) then
279 return;
280 end if;
281
86f0e17a
AC
282 -- Now we have a scalar type which is not marked as raising a constraint
283 -- error exception. The main purpose of this routine is to deal with
284 -- static expressions appearing in a non-static context. That means
285 -- that if we do not have a static expression then there is not much
286 -- to do. The one case that we deal with here is that if we have a
287 -- floating-point value that is out of range, then we post a warning
288 -- that an infinity will result.
fbf5a39b
AC
289
290 if not Is_Static_Expression (N) then
291 if Is_Floating_Point_Type (T)
c800f862 292 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
fbf5a39b
AC
293 then
294 Error_Msg_N
295 ("?float value out of range, infinity will be generated", N);
296 end if;
996ae0b0 297
996ae0b0
RK
298 return;
299 end if;
300
86f0e17a
AC
301 -- Here we have the case of outer level static expression of scalar
302 -- type, where the processing of this procedure is needed.
996ae0b0
RK
303
304 -- For real types, this is where we convert the value to a machine
86f0e17a
AC
305 -- number (see RM 4.9(38)). Also see ACVC test C490001. We should only
306 -- need to do this if the parent is a constant declaration, since in
307 -- other cases, gigi should do the necessary conversion correctly, but
308 -- experimentation shows that this is not the case on all machines, in
309 -- particular if we do not convert all literals to machine values in
310 -- non-static contexts, then ACVC test C490001 fails on Sparc/Solaris
311 -- and SGI/Irix.
996ae0b0
RK
312
313 if Nkind (N) = N_Real_Literal
314 and then not Is_Machine_Number (N)
315 and then not Is_Generic_Type (Etype (N))
316 and then Etype (N) /= Universal_Real
996ae0b0
RK
317 then
318 -- Check that value is in bounds before converting to machine
319 -- number, so as not to lose case where value overflows in the
320 -- least significant bit or less. See B490001.
321
c800f862 322 if Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
996ae0b0
RK
323 Out_Of_Range (N);
324 return;
325 end if;
326
327 -- Note: we have to copy the node, to avoid problems with conformance
328 -- of very similar numbers (see ACVC tests B4A010C and B63103A).
329
330 Rewrite (N, New_Copy (N));
331
332 if not Is_Floating_Point_Type (T) then
333 Set_Realval
334 (N, Corresponding_Integer_Value (N) * Small_Value (T));
335
336 elsif not UR_Is_Zero (Realval (N)) then
996ae0b0 337
86f0e17a
AC
338 -- Note: even though RM 4.9(38) specifies biased rounding, this
339 -- has been modified by AI-100 in order to prevent confusing
340 -- differences in rounding between static and non-static
341 -- expressions. AI-100 specifies that the effect of such rounding
342 -- is implementation dependent, and in GNAT we round to nearest
343 -- even to match the run-time behavior.
996ae0b0 344
fbf5a39b
AC
345 Set_Realval
346 (N, Machine (Base_Type (T), Realval (N), Round_Even, N));
996ae0b0
RK
347 end if;
348
349 Set_Is_Machine_Number (N);
350 end if;
351
352 -- Check for out of range universal integer. This is a non-static
353 -- context, so the integer value must be in range of the runtime
354 -- representation of universal integers.
355
356 -- We do this only within an expression, because that is the only
357 -- case in which non-static universal integer values can occur, and
358 -- furthermore, Check_Non_Static_Context is currently (incorrectly???)
359 -- called in contexts like the expression of a number declaration where
360 -- we certainly want to allow out of range values.
361
362 if Etype (N) = Universal_Integer
363 and then Nkind (N) = N_Integer_Literal
364 and then Nkind (Parent (N)) in N_Subexpr
365 and then
366 (Intval (N) < Expr_Value (Type_Low_Bound (Universal_Integer))
367 or else
368 Intval (N) > Expr_Value (Type_High_Bound (Universal_Integer)))
369 then
370 Apply_Compile_Time_Constraint_Error
07fc65c4
GB
371 (N, "non-static universal integer value out of range?",
372 CE_Range_Check_Failed);
996ae0b0
RK
373
374 -- Check out of range of base type
375
c800f862 376 elsif Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True) then
996ae0b0
RK
377 Out_Of_Range (N);
378
c800f862
RD
379 -- Give warning if outside subtype (where one or both of the bounds of
380 -- the subtype is static). This warning is omitted if the expression
381 -- appears in a range that could be null (warnings are handled elsewhere
382 -- for this case).
996ae0b0
RK
383
384 elsif T /= Base_Type (T)
385 and then Nkind (Parent (N)) /= N_Range
386 then
c800f862 387 if Is_In_Range (N, T, Assume_Valid => True) then
996ae0b0
RK
388 null;
389
c800f862 390 elsif Is_Out_Of_Range (N, T, Assume_Valid => True) then
996ae0b0 391 Apply_Compile_Time_Constraint_Error
07fc65c4 392 (N, "value not in range of}?", CE_Range_Check_Failed);
996ae0b0
RK
393
394 elsif Checks_On then
395 Enable_Range_Check (N);
396
397 else
398 Set_Do_Range_Check (N, False);
399 end if;
400 end if;
401 end Check_Non_Static_Context;
402
403 ---------------------------------
404 -- Check_String_Literal_Length --
405 ---------------------------------
406
407 procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id) is
408 begin
409 if not Raises_Constraint_Error (N)
410 and then Is_Constrained (Ttype)
411 then
412 if
413 UI_From_Int (String_Length (Strval (N))) /= String_Type_Len (Ttype)
414 then
415 Apply_Compile_Time_Constraint_Error
416 (N, "string length wrong for}?",
07fc65c4 417 CE_Length_Check_Failed,
996ae0b0
RK
418 Ent => Ttype,
419 Typ => Ttype);
420 end if;
421 end if;
422 end Check_String_Literal_Length;
423
424 --------------------------
425 -- Compile_Time_Compare --
426 --------------------------
427
fbf5a39b 428 function Compile_Time_Compare
1c7717c3 429 (L, R : Node_Id;
af02a866
RD
430 Assume_Valid : Boolean) return Compare_Result
431 is
432 Discard : aliased Uint;
433 begin
434 return Compile_Time_Compare (L, R, Discard'Access, Assume_Valid);
435 end Compile_Time_Compare;
436
437 function Compile_Time_Compare
438 (L, R : Node_Id;
439 Diff : access Uint;
1c7717c3
AC
440 Assume_Valid : Boolean;
441 Rec : Boolean := False) return Compare_Result
fbf5a39b 442 is
93c3fca7
AC
443 Ltyp : Entity_Id := Underlying_Type (Etype (L));
444 Rtyp : Entity_Id := Underlying_Type (Etype (R));
1c7717c3
AC
445 -- These get reset to the base type for the case of entities where
446 -- Is_Known_Valid is not set. This takes care of handling possible
447 -- invalid representations using the value of the base type, in
448 -- accordance with RM 13.9.1(10).
996ae0b0 449
af02a866
RD
450 Discard : aliased Uint;
451
996ae0b0
RK
452 procedure Compare_Decompose
453 (N : Node_Id;
454 R : out Node_Id;
455 V : out Uint);
b49365b2
RD
456 -- This procedure decomposes the node N into an expression node and a
457 -- signed offset, so that the value of N is equal to the value of R plus
458 -- the value V (which may be negative). If no such decomposition is
459 -- possible, then on return R is a copy of N, and V is set to zero.
996ae0b0
RK
460
461 function Compare_Fixup (N : Node_Id) return Node_Id;
b49365b2
RD
462 -- This function deals with replacing 'Last and 'First references with
463 -- their corresponding type bounds, which we then can compare. The
464 -- argument is the original node, the result is the identity, unless we
465 -- have a 'Last/'First reference in which case the value returned is the
466 -- appropriate type bound.
996ae0b0 467
57036dcc
ES
468 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean;
469 -- Even if the context does not assume that values are valid, some
470 -- simple cases can be recognized.
471
996ae0b0 472 function Is_Same_Value (L, R : Node_Id) return Boolean;
86f0e17a
AC
473 -- Returns True iff L and R represent expressions that definitely have
474 -- identical (but not necessarily compile time known) values Indeed the
475 -- caller is expected to have already dealt with the cases of compile
476 -- time known values, so these are not tested here.
996ae0b0
RK
477
478 -----------------------
479 -- Compare_Decompose --
480 -----------------------
481
482 procedure Compare_Decompose
483 (N : Node_Id;
484 R : out Node_Id;
485 V : out Uint)
486 is
487 begin
488 if Nkind (N) = N_Op_Add
489 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
490 then
491 R := Left_Opnd (N);
492 V := Intval (Right_Opnd (N));
493 return;
494
495 elsif Nkind (N) = N_Op_Subtract
496 and then Nkind (Right_Opnd (N)) = N_Integer_Literal
497 then
498 R := Left_Opnd (N);
499 V := UI_Negate (Intval (Right_Opnd (N)));
500 return;
501
502 elsif Nkind (N) = N_Attribute_Reference then
996ae0b0
RK
503 if Attribute_Name (N) = Name_Succ then
504 R := First (Expressions (N));
505 V := Uint_1;
506 return;
507
508 elsif Attribute_Name (N) = Name_Pred then
509 R := First (Expressions (N));
510 V := Uint_Minus_1;
511 return;
512 end if;
513 end if;
514
515 R := N;
516 V := Uint_0;
517 end Compare_Decompose;
518
519 -------------------
520 -- Compare_Fixup --
521 -------------------
522
523 function Compare_Fixup (N : Node_Id) return Node_Id is
524 Indx : Node_Id;
525 Xtyp : Entity_Id;
526 Subs : Nat;
527
528 begin
7a6de2e2
AC
529 -- Fixup only required for First/Last attribute reference
530
996ae0b0
RK
531 if Nkind (N) = N_Attribute_Reference
532 and then (Attribute_Name (N) = Name_First
533 or else
534 Attribute_Name (N) = Name_Last)
535 then
536 Xtyp := Etype (Prefix (N));
537
538 -- If we have no type, then just abandon the attempt to do
539 -- a fixup, this is probably the result of some other error.
540
541 if No (Xtyp) then
542 return N;
543 end if;
544
545 -- Dereference an access type
546
547 if Is_Access_Type (Xtyp) then
548 Xtyp := Designated_Type (Xtyp);
549 end if;
550
551 -- If we don't have an array type at this stage, something
552 -- is peculiar, e.g. another error, and we abandon the attempt
553 -- at a fixup.
554
555 if not Is_Array_Type (Xtyp) then
556 return N;
557 end if;
558
559 -- Ignore unconstrained array, since bounds are not meaningful
560
561 if not Is_Constrained (Xtyp) then
562 return N;
563 end if;
564
c3de5c4c
ES
565 if Ekind (Xtyp) = E_String_Literal_Subtype then
566 if Attribute_Name (N) = Name_First then
567 return String_Literal_Low_Bound (Xtyp);
568
5f44f0d4 569 else
c3de5c4c
ES
570 return Make_Integer_Literal (Sloc (N),
571 Intval => Intval (String_Literal_Low_Bound (Xtyp))
57036dcc 572 + String_Literal_Length (Xtyp));
c3de5c4c
ES
573 end if;
574 end if;
575
996ae0b0
RK
576 -- Find correct index type
577
578 Indx := First_Index (Xtyp);
579
580 if Present (Expressions (N)) then
581 Subs := UI_To_Int (Expr_Value (First (Expressions (N))));
582
583 for J in 2 .. Subs loop
584 Indx := Next_Index (Indx);
585 end loop;
586 end if;
587
588 Xtyp := Etype (Indx);
589
590 if Attribute_Name (N) = Name_First then
591 return Type_Low_Bound (Xtyp);
7a6de2e2 592 else
996ae0b0
RK
593 return Type_High_Bound (Xtyp);
594 end if;
595 end if;
596
597 return N;
598 end Compare_Fixup;
599
57036dcc
ES
600 ----------------------------
601 -- Is_Known_Valid_Operand --
602 ----------------------------
603
604 function Is_Known_Valid_Operand (Opnd : Node_Id) return Boolean is
605 begin
606 return (Is_Entity_Name (Opnd)
607 and then
608 (Is_Known_Valid (Entity (Opnd))
609 or else Ekind (Entity (Opnd)) = E_In_Parameter
610 or else
611 (Ekind (Entity (Opnd)) in Object_Kind
612 and then Present (Current_Value (Entity (Opnd))))))
613 or else Is_OK_Static_Expression (Opnd);
614 end Is_Known_Valid_Operand;
615
996ae0b0
RK
616 -------------------
617 -- Is_Same_Value --
618 -------------------
619
620 function Is_Same_Value (L, R : Node_Id) return Boolean is
621 Lf : constant Node_Id := Compare_Fixup (L);
622 Rf : constant Node_Id := Compare_Fixup (R);
623
fbf5a39b 624 function Is_Same_Subscript (L, R : List_Id) return Boolean;
57036dcc
ES
625 -- L, R are the Expressions values from two attribute nodes for First
626 -- or Last attributes. Either may be set to No_List if no expressions
627 -- are present (indicating subscript 1). The result is True if both
628 -- expressions represent the same subscript (note one case is where
629 -- one subscript is missing and the other is explicitly set to 1).
fbf5a39b
AC
630
631 -----------------------
632 -- Is_Same_Subscript --
633 -----------------------
634
635 function Is_Same_Subscript (L, R : List_Id) return Boolean is
636 begin
637 if L = No_List then
638 if R = No_List then
639 return True;
640 else
641 return Expr_Value (First (R)) = Uint_1;
642 end if;
643
644 else
645 if R = No_List then
646 return Expr_Value (First (L)) = Uint_1;
647 else
648 return Expr_Value (First (L)) = Expr_Value (First (R));
649 end if;
650 end if;
651 end Is_Same_Subscript;
652
653 -- Start of processing for Is_Same_Value
654
996ae0b0 655 begin
b49365b2 656 -- Values are the same if they refer to the same entity and the
c800f862
RD
657 -- entity is non-volatile. This does not however apply to Float
658 -- types, since we may have two NaN values and they should never
659 -- compare equal.
996ae0b0 660
f08b2371
RD
661 -- If the entity is a discriminant, the two expressions may be bounds
662 -- of components of objects of the same discriminated type. The
663 -- values of the discriminants are not static, and therefore the
664 -- result is unknown.
665
666 -- It would be better to comment individual branches of this test ???
4fb0b3f0 667
b49365b2
RD
668 if Nkind_In (Lf, N_Identifier, N_Expanded_Name)
669 and then Nkind_In (Rf, N_Identifier, N_Expanded_Name)
996ae0b0 670 and then Entity (Lf) = Entity (Rf)
4fb0b3f0 671 and then Ekind (Entity (Lf)) /= E_Discriminant
b49365b2 672 and then Present (Entity (Lf))
fbf5a39b 673 and then not Is_Floating_Point_Type (Etype (L))
c800f862
RD
674 and then not Is_Volatile_Reference (L)
675 and then not Is_Volatile_Reference (R)
996ae0b0
RK
676 then
677 return True;
678
679 -- Or if they are compile time known and identical
680
681 elsif Compile_Time_Known_Value (Lf)
682 and then
683 Compile_Time_Known_Value (Rf)
684 and then Expr_Value (Lf) = Expr_Value (Rf)
685 then
686 return True;
687
b49365b2
RD
688 -- False if Nkind of the two nodes is different for remaining cases
689
690 elsif Nkind (Lf) /= Nkind (Rf) then
691 return False;
692
693 -- True if both 'First or 'Last values applying to the same entity
694 -- (first and last don't change even if value does). Note that we
695 -- need this even with the calls to Compare_Fixup, to handle the
696 -- case of unconstrained array attributes where Compare_Fixup
697 -- cannot find useful bounds.
996ae0b0
RK
698
699 elsif Nkind (Lf) = N_Attribute_Reference
996ae0b0
RK
700 and then Attribute_Name (Lf) = Attribute_Name (Rf)
701 and then (Attribute_Name (Lf) = Name_First
702 or else
703 Attribute_Name (Lf) = Name_Last)
b49365b2
RD
704 and then Nkind_In (Prefix (Lf), N_Identifier, N_Expanded_Name)
705 and then Nkind_In (Prefix (Rf), N_Identifier, N_Expanded_Name)
996ae0b0 706 and then Entity (Prefix (Lf)) = Entity (Prefix (Rf))
fbf5a39b 707 and then Is_Same_Subscript (Expressions (Lf), Expressions (Rf))
996ae0b0
RK
708 then
709 return True;
710
b49365b2
RD
711 -- True if the same selected component from the same record
712
713 elsif Nkind (Lf) = N_Selected_Component
714 and then Selector_Name (Lf) = Selector_Name (Rf)
715 and then Is_Same_Value (Prefix (Lf), Prefix (Rf))
716 then
717 return True;
718
719 -- True if the same unary operator applied to the same operand
720
721 elsif Nkind (Lf) in N_Unary_Op
722 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
723 then
724 return True;
725
8682d22c 726 -- True if the same binary operator applied to the same operands
b49365b2
RD
727
728 elsif Nkind (Lf) in N_Binary_Op
729 and then Is_Same_Value (Left_Opnd (Lf), Left_Opnd (Rf))
730 and then Is_Same_Value (Right_Opnd (Lf), Right_Opnd (Rf))
731 then
732 return True;
733
8682d22c 734 -- All other cases, we can't tell, so return False
996ae0b0
RK
735
736 else
737 return False;
738 end if;
739 end Is_Same_Value;
740
741 -- Start of processing for Compile_Time_Compare
742
743 begin
af02a866
RD
744 Diff.all := No_Uint;
745
07fc65c4
GB
746 -- If either operand could raise constraint error, then we cannot
747 -- know the result at compile time (since CE may be raised!)
748
749 if not (Cannot_Raise_Constraint_Error (L)
750 and then
751 Cannot_Raise_Constraint_Error (R))
752 then
753 return Unknown;
754 end if;
755
756 -- Identical operands are most certainly equal
757
996ae0b0
RK
758 if L = R then
759 return EQ;
760
93c3fca7
AC
761 -- If expressions have no types, then do not attempt to determine if
762 -- they are the same, since something funny is going on. One case in
763 -- which this happens is during generic template analysis, when bounds
764 -- are not fully analyzed.
996ae0b0
RK
765
766 elsif No (Ltyp) or else No (Rtyp) then
767 return Unknown;
768
93c3fca7
AC
769 -- We do not attempt comparisons for packed arrays arrays represented as
770 -- modular types, where the semantics of comparison is quite different.
996ae0b0 771
93c3fca7
AC
772 elsif Is_Packed_Array_Type (Ltyp)
773 and then Is_Modular_Integer_Type (Ltyp)
996ae0b0
RK
774 then
775 return Unknown;
776
93c3fca7 777 -- For access types, the only time we know the result at compile time
f61580d4 778 -- (apart from identical operands, which we handled already) is if we
93c3fca7
AC
779 -- know one operand is null and the other is not, or both operands are
780 -- known null.
781
782 elsif Is_Access_Type (Ltyp) then
783 if Known_Null (L) then
784 if Known_Null (R) then
785 return EQ;
786 elsif Known_Non_Null (R) then
787 return NE;
788 else
789 return Unknown;
790 end if;
791
f61580d4 792 elsif Known_Non_Null (L) and then Known_Null (R) then
93c3fca7
AC
793 return NE;
794
795 else
796 return Unknown;
797 end if;
798
996ae0b0
RK
799 -- Case where comparison involves two compile time known values
800
801 elsif Compile_Time_Known_Value (L)
802 and then Compile_Time_Known_Value (R)
803 then
804 -- For the floating-point case, we have to be a little careful, since
805 -- at compile time we are dealing with universal exact values, but at
806 -- runtime, these will be in non-exact target form. That's why the
807 -- returned results are LE and GE below instead of LT and GT.
808
809 if Is_Floating_Point_Type (Ltyp)
810 or else
811 Is_Floating_Point_Type (Rtyp)
812 then
813 declare
814 Lo : constant Ureal := Expr_Value_R (L);
815 Hi : constant Ureal := Expr_Value_R (R);
816
817 begin
818 if Lo < Hi then
819 return LE;
820 elsif Lo = Hi then
821 return EQ;
822 else
823 return GE;
824 end if;
825 end;
826
93c3fca7
AC
827 -- For string types, we have two string literals and we proceed to
828 -- compare them using the Ada style dictionary string comparison.
829
830 elsif not Is_Scalar_Type (Ltyp) then
831 declare
832 Lstring : constant String_Id := Strval (Expr_Value_S (L));
833 Rstring : constant String_Id := Strval (Expr_Value_S (R));
834 Llen : constant Nat := String_Length (Lstring);
835 Rlen : constant Nat := String_Length (Rstring);
836
837 begin
838 for J in 1 .. Nat'Min (Llen, Rlen) loop
839 declare
840 LC : constant Char_Code := Get_String_Char (Lstring, J);
841 RC : constant Char_Code := Get_String_Char (Rstring, J);
842 begin
843 if LC < RC then
844 return LT;
845 elsif LC > RC then
846 return GT;
847 end if;
848 end;
849 end loop;
850
851 if Llen < Rlen then
852 return LT;
853 elsif Llen > Rlen then
854 return GT;
855 else
856 return EQ;
857 end if;
858 end;
859
860 -- For remaining scalar cases we know exactly (note that this does
861 -- include the fixed-point case, where we know the run time integer
f61580d4 862 -- values now).
996ae0b0
RK
863
864 else
865 declare
866 Lo : constant Uint := Expr_Value (L);
867 Hi : constant Uint := Expr_Value (R);
868
869 begin
870 if Lo < Hi then
af02a866 871 Diff.all := Hi - Lo;
996ae0b0 872 return LT;
af02a866 873
996ae0b0
RK
874 elsif Lo = Hi then
875 return EQ;
af02a866 876
996ae0b0 877 else
af02a866 878 Diff.all := Lo - Hi;
996ae0b0
RK
879 return GT;
880 end if;
881 end;
882 end if;
883
884 -- Cases where at least one operand is not known at compile time
885
886 else
93c3fca7 887 -- Remaining checks apply only for discrete types
29797f34
RD
888
889 if not Is_Discrete_Type (Ltyp)
890 or else not Is_Discrete_Type (Rtyp)
93c3fca7
AC
891 then
892 return Unknown;
893 end if;
894
895 -- Defend against generic types, or actually any expressions that
896 -- contain a reference to a generic type from within a generic
897 -- template. We don't want to do any range analysis of such
898 -- expressions for two reasons. First, the bounds of a generic type
899 -- itself are junk and cannot be used for any kind of analysis.
900 -- Second, we may have a case where the range at run time is indeed
901 -- known, but we don't want to do compile time analysis in the
902 -- template based on that range since in an instance the value may be
903 -- static, and able to be elaborated without reference to the bounds
904 -- of types involved. As an example, consider:
905
906 -- (F'Pos (F'Last) + 1) > Integer'Last
907
908 -- The expression on the left side of > is Universal_Integer and thus
909 -- acquires the type Integer for evaluation at run time, and at run
910 -- time it is true that this condition is always False, but within
911 -- an instance F may be a type with a static range greater than the
912 -- range of Integer, and the expression statically evaluates to True.
913
914 if References_Generic_Formal_Type (L)
915 or else
916 References_Generic_Formal_Type (R)
29797f34
RD
917 then
918 return Unknown;
919 end if;
920
1c7717c3
AC
921 -- Replace types by base types for the case of entities which are
922 -- not known to have valid representations. This takes care of
923 -- properly dealing with invalid representations.
924
c800f862 925 if not Assume_Valid and then not Assume_No_Invalid_Values then
1c7717c3 926 if Is_Entity_Name (L) and then not Is_Known_Valid (Entity (L)) then
93c3fca7 927 Ltyp := Underlying_Type (Base_Type (Ltyp));
1c7717c3
AC
928 end if;
929
930 if Is_Entity_Name (R) and then not Is_Known_Valid (Entity (R)) then
93c3fca7 931 Rtyp := Underlying_Type (Base_Type (Rtyp));
1c7717c3
AC
932 end if;
933 end if;
934
c800f862
RD
935 -- Try range analysis on variables and see if ranges are disjoint
936
937 declare
938 LOK, ROK : Boolean;
939 LLo, LHi : Uint;
940 RLo, RHi : Uint;
941
942 begin
943 Determine_Range (L, LOK, LLo, LHi, Assume_Valid);
944 Determine_Range (R, ROK, RLo, RHi, Assume_Valid);
945
946 if LOK and ROK then
947 if LHi < RLo then
948 return LT;
949
950 elsif RHi < LLo then
951 return GT;
952
953 elsif LLo = LHi
954 and then RLo = RHi
955 and then LLo = RLo
956 then
e27b834b 957
75ba322d
AC
958 -- If the range includes a single literal and we can assume
959 -- validity then the result is known even if an operand is
960 -- not static.
e27b834b
AC
961
962 if Assume_Valid then
963 return EQ;
e27b834b
AC
964 else
965 return Unknown;
966 end if;
c800f862
RD
967
968 elsif LHi = RLo then
969 return LE;
970
971 elsif RHi = LLo then
972 return GE;
57036dcc
ES
973
974 elsif not Is_Known_Valid_Operand (L)
975 and then not Assume_Valid
976 then
977 if Is_Same_Value (L, R) then
978 return EQ;
979 else
980 return Unknown;
981 end if;
c800f862 982 end if;
f9ad6b62 983
2c1b72d7
AC
984 -- If the range of either operand cannot be determined, nothing
985 -- further can be inferred.
f9ad6b62 986
2c1b72d7 987 else
f9ad6b62 988 return Unknown;
c800f862
RD
989 end if;
990 end;
991
996ae0b0
RK
992 -- Here is where we check for comparisons against maximum bounds of
993 -- types, where we know that no value can be outside the bounds of
994 -- the subtype. Note that this routine is allowed to assume that all
995 -- expressions are within their subtype bounds. Callers wishing to
996 -- deal with possibly invalid values must in any case take special
997 -- steps (e.g. conversions to larger types) to avoid this kind of
998 -- optimization, which is always considered to be valid. We do not
999 -- attempt this optimization with generic types, since the type
1000 -- bounds may not be meaningful in this case.
1001
93c3fca7 1002 -- We are in danger of an infinite recursion here. It does not seem
fbf5a39b
AC
1003 -- useful to go more than one level deep, so the parameter Rec is
1004 -- used to protect ourselves against this infinite recursion.
1005
29797f34
RD
1006 if not Rec then
1007
fbf5a39b
AC
1008 -- See if we can get a decisive check against one operand and
1009 -- a bound of the other operand (four possible tests here).
93c3fca7
AC
1010 -- Note that we avoid testing junk bounds of a generic type.
1011
1012 if not Is_Generic_Type (Rtyp) then
1013 case Compile_Time_Compare (L, Type_Low_Bound (Rtyp),
1014 Discard'Access,
1015 Assume_Valid, Rec => True)
1016 is
1017 when LT => return LT;
1018 when LE => return LE;
1019 when EQ => return LE;
1020 when others => null;
1021 end case;
fbf5a39b 1022
93c3fca7
AC
1023 case Compile_Time_Compare (L, Type_High_Bound (Rtyp),
1024 Discard'Access,
1025 Assume_Valid, Rec => True)
1026 is
1027 when GT => return GT;
1028 when GE => return GE;
1029 when EQ => return GE;
1030 when others => null;
1031 end case;
1032 end if;
996ae0b0 1033
93c3fca7
AC
1034 if not Is_Generic_Type (Ltyp) then
1035 case Compile_Time_Compare (Type_Low_Bound (Ltyp), R,
1036 Discard'Access,
1037 Assume_Valid, Rec => True)
1038 is
1039 when GT => return GT;
1040 when GE => return GE;
1041 when EQ => return GE;
1042 when others => null;
1043 end case;
996ae0b0 1044
93c3fca7
AC
1045 case Compile_Time_Compare (Type_High_Bound (Ltyp), R,
1046 Discard'Access,
1047 Assume_Valid, Rec => True)
1048 is
1049 when LT => return LT;
1050 when LE => return LE;
1051 when EQ => return LE;
1052 when others => null;
1053 end case;
1054 end if;
996ae0b0
RK
1055 end if;
1056
1057 -- Next attempt is to decompose the expressions to extract
1058 -- a constant offset resulting from the use of any of the forms:
1059
1060 -- expr + literal
1061 -- expr - literal
1062 -- typ'Succ (expr)
1063 -- typ'Pred (expr)
1064
1065 -- Then we see if the two expressions are the same value, and if so
1066 -- the result is obtained by comparing the offsets.
1067
1068 declare
1069 Lnode : Node_Id;
1070 Loffs : Uint;
1071 Rnode : Node_Id;
1072 Roffs : Uint;
1073
1074 begin
1075 Compare_Decompose (L, Lnode, Loffs);
1076 Compare_Decompose (R, Rnode, Roffs);
1077
1078 if Is_Same_Value (Lnode, Rnode) then
1079 if Loffs = Roffs then
1080 return EQ;
1081
1082 elsif Loffs < Roffs then
af02a866 1083 Diff.all := Roffs - Loffs;
996ae0b0
RK
1084 return LT;
1085
1086 else
af02a866 1087 Diff.all := Loffs - Roffs;
996ae0b0
RK
1088 return GT;
1089 end if;
29797f34
RD
1090 end if;
1091 end;
1092
1093 -- Next attempt is to see if we have an entity compared with a
1094 -- compile time known value, where there is a current value
1095 -- conditional for the entity which can tell us the result.
1096
1097 declare
1098 Var : Node_Id;
1099 -- Entity variable (left operand)
1100
1101 Val : Uint;
1102 -- Value (right operand)
1103
1104 Inv : Boolean;
1105 -- If False, we have reversed the operands
1106
1107 Op : Node_Kind;
1108 -- Comparison operator kind from Get_Current_Value_Condition call
996ae0b0 1109
29797f34
RD
1110 Opn : Node_Id;
1111 -- Value from Get_Current_Value_Condition call
1112
1113 Opv : Uint;
1114 -- Value of Opn
1115
1116 Result : Compare_Result;
1117 -- Known result before inversion
1118
1119 begin
1120 if Is_Entity_Name (L)
1121 and then Compile_Time_Known_Value (R)
1122 then
1123 Var := L;
1124 Val := Expr_Value (R);
1125 Inv := False;
1126
1127 elsif Is_Entity_Name (R)
1128 and then Compile_Time_Known_Value (L)
1129 then
1130 Var := R;
1131 Val := Expr_Value (L);
1132 Inv := True;
1133
1134 -- That was the last chance at finding a compile time result
996ae0b0
RK
1135
1136 else
1137 return Unknown;
1138 end if;
29797f34
RD
1139
1140 Get_Current_Value_Condition (Var, Op, Opn);
1141
1142 -- That was the last chance, so if we got nothing return
1143
1144 if No (Opn) then
1145 return Unknown;
1146 end if;
1147
1148 Opv := Expr_Value (Opn);
1149
1150 -- We got a comparison, so we might have something interesting
1151
1152 -- Convert LE to LT and GE to GT, just so we have fewer cases
1153
1154 if Op = N_Op_Le then
1155 Op := N_Op_Lt;
1156 Opv := Opv + 1;
af02a866 1157
29797f34
RD
1158 elsif Op = N_Op_Ge then
1159 Op := N_Op_Gt;
1160 Opv := Opv - 1;
1161 end if;
1162
1163 -- Deal with equality case
1164
1165 if Op = N_Op_Eq then
1166 if Val = Opv then
1167 Result := EQ;
1168 elsif Opv < Val then
1169 Result := LT;
1170 else
1171 Result := GT;
1172 end if;
1173
1174 -- Deal with inequality case
1175
1176 elsif Op = N_Op_Ne then
1177 if Val = Opv then
1178 Result := NE;
1179 else
1180 return Unknown;
1181 end if;
1182
1183 -- Deal with greater than case
1184
1185 elsif Op = N_Op_Gt then
1186 if Opv >= Val then
1187 Result := GT;
1188 elsif Opv = Val - 1 then
1189 Result := GE;
1190 else
1191 return Unknown;
1192 end if;
1193
1194 -- Deal with less than case
1195
1196 else pragma Assert (Op = N_Op_Lt);
1197 if Opv <= Val then
1198 Result := LT;
1199 elsif Opv = Val + 1 then
1200 Result := LE;
1201 else
1202 return Unknown;
1203 end if;
1204 end if;
1205
1206 -- Deal with inverting result
1207
1208 if Inv then
1209 case Result is
1210 when GT => return LT;
1211 when GE => return LE;
1212 when LT => return GT;
1213 when LE => return GE;
1214 when others => return Result;
1215 end case;
1216 end if;
1217
1218 return Result;
996ae0b0
RK
1219 end;
1220 end if;
1221 end Compile_Time_Compare;
1222
f44fe430
RD
1223 -------------------------------
1224 -- Compile_Time_Known_Bounds --
1225 -------------------------------
1226
1227 function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean is
1228 Indx : Node_Id;
1229 Typ : Entity_Id;
1230
1231 begin
1232 if not Is_Array_Type (T) then
1233 return False;
1234 end if;
1235
1236 Indx := First_Index (T);
1237 while Present (Indx) loop
1238 Typ := Underlying_Type (Etype (Indx));
93c3fca7
AC
1239
1240 -- Never look at junk bounds of a generic type
1241
1242 if Is_Generic_Type (Typ) then
1243 return False;
1244 end if;
1245
1246 -- Otherwise check bounds for compile time known
1247
f44fe430
RD
1248 if not Compile_Time_Known_Value (Type_Low_Bound (Typ)) then
1249 return False;
1250 elsif not Compile_Time_Known_Value (Type_High_Bound (Typ)) then
1251 return False;
1252 else
1253 Next_Index (Indx);
1254 end if;
1255 end loop;
1256
1257 return True;
1258 end Compile_Time_Known_Bounds;
1259
996ae0b0
RK
1260 ------------------------------
1261 -- Compile_Time_Known_Value --
1262 ------------------------------
1263
1264 function Compile_Time_Known_Value (Op : Node_Id) return Boolean is
07fc65c4
GB
1265 K : constant Node_Kind := Nkind (Op);
1266 CV_Ent : CV_Entry renames CV_Cache (Nat (Op) mod CV_Cache_Size);
996ae0b0
RK
1267
1268 begin
1269 -- Never known at compile time if bad type or raises constraint error
1270 -- or empty (latter case occurs only as a result of a previous error)
1271
1272 if No (Op)
1273 or else Op = Error
1274 or else Etype (Op) = Any_Type
1275 or else Raises_Constraint_Error (Op)
1276 then
1277 return False;
1278 end if;
1279
597d7158
GD
1280 -- If this is not a static expression or a null literal, and we are in
1281 -- configurable run-time mode, then we consider it not known at compile
1282 -- time. This avoids anomalies where whether something is allowed with a
1283 -- given configurable run-time library depends on how good the compiler
1284 -- is at optimizing and knowing that things are constant when they are
1285 -- nonstatic.
1286
1287 if Configurable_Run_Time_Mode
1288 and then K /= N_Null
1289 and then not Is_Static_Expression (Op)
1290 then
fbf5a39b
AC
1291 return False;
1292 end if;
1293
996ae0b0
RK
1294 -- If we have an entity name, then see if it is the name of a constant
1295 -- and if so, test the corresponding constant value, or the name of
1296 -- an enumeration literal, which is always a constant.
1297
1298 if Present (Etype (Op)) and then Is_Entity_Name (Op) then
1299 declare
1300 E : constant Entity_Id := Entity (Op);
1301 V : Node_Id;
1302
1303 begin
1304 -- Never known at compile time if it is a packed array value.
1305 -- We might want to try to evaluate these at compile time one
1306 -- day, but we do not make that attempt now.
1307
1308 if Is_Packed_Array_Type (Etype (Op)) then
1309 return False;
1310 end if;
1311
1312 if Ekind (E) = E_Enumeration_Literal then
1313 return True;
1314
db664118
AC
1315 -- In Alfa mode, the value of deferred constants should be ignored
1316 -- outside the scope of their full view. This allows parameterized
1317 -- formal verification, in which a deferred constant value if not
1318 -- known from client units.
1319
1320 elsif Ekind (E) = E_Constant
1321 and then not (Alfa_Mode
1322 and then Present (Full_View (E))
1323 and then not In_Open_Scopes (Scope (E)))
1324 then
996ae0b0
RK
1325 V := Constant_Value (E);
1326 return Present (V) and then Compile_Time_Known_Value (V);
1327 end if;
1328 end;
1329
1330 -- We have a value, see if it is compile time known
1331
1332 else
07fc65c4 1333 -- Integer literals are worth storing in the cache
996ae0b0 1334
07fc65c4
GB
1335 if K = N_Integer_Literal then
1336 CV_Ent.N := Op;
1337 CV_Ent.V := Intval (Op);
1338 return True;
1339
1340 -- Other literals and NULL are known at compile time
1341
1342 elsif
996ae0b0
RK
1343 K = N_Character_Literal
1344 or else
1345 K = N_Real_Literal
1346 or else
1347 K = N_String_Literal
1348 or else
1349 K = N_Null
1350 then
1351 return True;
1352
1353 -- Any reference to Null_Parameter is known at compile time. No
1354 -- other attribute references (that have not already been folded)
1355 -- are known at compile time.
1356
1357 elsif K = N_Attribute_Reference then
1358 return Attribute_Name (Op) = Name_Null_Parameter;
07fc65c4 1359 end if;
996ae0b0 1360 end if;
07fc65c4
GB
1361
1362 -- If we fall through, not known at compile time
1363
1364 return False;
1365
1366 -- If we get an exception while trying to do this test, then some error
1367 -- has occurred, and we simply say that the value is not known after all
1368
1369 exception
1370 when others =>
1371 return False;
996ae0b0
RK
1372 end Compile_Time_Known_Value;
1373
1374 --------------------------------------
1375 -- Compile_Time_Known_Value_Or_Aggr --
1376 --------------------------------------
1377
1378 function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean is
1379 begin
1380 -- If we have an entity name, then see if it is the name of a constant
1381 -- and if so, test the corresponding constant value, or the name of
1382 -- an enumeration literal, which is always a constant.
1383
1384 if Is_Entity_Name (Op) then
1385 declare
1386 E : constant Entity_Id := Entity (Op);
1387 V : Node_Id;
1388
1389 begin
1390 if Ekind (E) = E_Enumeration_Literal then
1391 return True;
1392
1393 elsif Ekind (E) /= E_Constant then
1394 return False;
1395
1396 else
1397 V := Constant_Value (E);
1398 return Present (V)
1399 and then Compile_Time_Known_Value_Or_Aggr (V);
1400 end if;
1401 end;
1402
1403 -- We have a value, see if it is compile time known
1404
1405 else
1406 if Compile_Time_Known_Value (Op) then
1407 return True;
1408
1409 elsif Nkind (Op) = N_Aggregate then
1410
1411 if Present (Expressions (Op)) then
1412 declare
1413 Expr : Node_Id;
1414
1415 begin
1416 Expr := First (Expressions (Op));
1417 while Present (Expr) loop
1418 if not Compile_Time_Known_Value_Or_Aggr (Expr) then
1419 return False;
1420 end if;
1421
1422 Next (Expr);
1423 end loop;
1424 end;
1425 end if;
1426
1427 if Present (Component_Associations (Op)) then
1428 declare
1429 Cass : Node_Id;
1430
1431 begin
1432 Cass := First (Component_Associations (Op));
1433 while Present (Cass) loop
1434 if not
1435 Compile_Time_Known_Value_Or_Aggr (Expression (Cass))
1436 then
1437 return False;
1438 end if;
1439
1440 Next (Cass);
1441 end loop;
1442 end;
1443 end if;
1444
1445 return True;
1446
1447 -- All other types of values are not known at compile time
1448
1449 else
1450 return False;
1451 end if;
1452
1453 end if;
1454 end Compile_Time_Known_Value_Or_Aggr;
1455
1456 -----------------
1457 -- Eval_Actual --
1458 -----------------
1459
1460 -- This is only called for actuals of functions that are not predefined
1461 -- operators (which have already been rewritten as operators at this
1462 -- stage), so the call can never be folded, and all that needs doing for
1463 -- the actual is to do the check for a non-static context.
1464
1465 procedure Eval_Actual (N : Node_Id) is
1466 begin
1467 Check_Non_Static_Context (N);
1468 end Eval_Actual;
1469
1470 --------------------
1471 -- Eval_Allocator --
1472 --------------------
1473
1474 -- Allocators are never static, so all we have to do is to do the
1475 -- check for a non-static context if an expression is present.
1476
1477 procedure Eval_Allocator (N : Node_Id) is
1478 Expr : constant Node_Id := Expression (N);
1479
1480 begin
1481 if Nkind (Expr) = N_Qualified_Expression then
1482 Check_Non_Static_Context (Expression (Expr));
1483 end if;
1484 end Eval_Allocator;
1485
1486 ------------------------
1487 -- Eval_Arithmetic_Op --
1488 ------------------------
1489
1490 -- Arithmetic operations are static functions, so the result is static
1491 -- if both operands are static (RM 4.9(7), 4.9(20)).
1492
1493 procedure Eval_Arithmetic_Op (N : Node_Id) is
1494 Left : constant Node_Id := Left_Opnd (N);
1495 Right : constant Node_Id := Right_Opnd (N);
1496 Ltype : constant Entity_Id := Etype (Left);
1497 Rtype : constant Entity_Id := Etype (Right);
d7567964 1498 Otype : Entity_Id := Empty;
996ae0b0
RK
1499 Stat : Boolean;
1500 Fold : Boolean;
1501
1502 begin
1503 -- If not foldable we are done
1504
1505 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1506
1507 if not Fold then
1508 return;
1509 end if;
1510
d7567964
TQ
1511 if Is_Universal_Numeric_Type (Etype (Left))
1512 and then
1513 Is_Universal_Numeric_Type (Etype (Right))
602a7ec0 1514 then
d7567964 1515 Otype := Find_Universal_Operator_Type (N);
602a7ec0
AC
1516 end if;
1517
996ae0b0
RK
1518 -- Fold for cases where both operands are of integer type
1519
1520 if Is_Integer_Type (Ltype) and then Is_Integer_Type (Rtype) then
1521 declare
1522 Left_Int : constant Uint := Expr_Value (Left);
1523 Right_Int : constant Uint := Expr_Value (Right);
1524 Result : Uint;
1525
1526 begin
1527 case Nkind (N) is
1528
1529 when N_Op_Add =>
1530 Result := Left_Int + Right_Int;
1531
1532 when N_Op_Subtract =>
1533 Result := Left_Int - Right_Int;
1534
1535 when N_Op_Multiply =>
1536 if OK_Bits
1537 (N, UI_From_Int
1538 (Num_Bits (Left_Int) + Num_Bits (Right_Int)))
1539 then
1540 Result := Left_Int * Right_Int;
1541 else
1542 Result := Left_Int;
1543 end if;
1544
1545 when N_Op_Divide =>
1546
1547 -- The exception Constraint_Error is raised by integer
1548 -- division, rem and mod if the right operand is zero.
1549
1550 if Right_Int = 0 then
1551 Apply_Compile_Time_Constraint_Error
fbf5a39b
AC
1552 (N, "division by zero",
1553 CE_Divide_By_Zero,
1554 Warn => not Stat);
996ae0b0 1555 return;
fbf5a39b 1556
996ae0b0
RK
1557 else
1558 Result := Left_Int / Right_Int;
1559 end if;
1560
1561 when N_Op_Mod =>
1562
1563 -- The exception Constraint_Error is raised by integer
1564 -- division, rem and mod if the right operand is zero.
1565
1566 if Right_Int = 0 then
1567 Apply_Compile_Time_Constraint_Error
fbf5a39b
AC
1568 (N, "mod with zero divisor",
1569 CE_Divide_By_Zero,
1570 Warn => not Stat);
996ae0b0
RK
1571 return;
1572 else
1573 Result := Left_Int mod Right_Int;
1574 end if;
1575
1576 when N_Op_Rem =>
1577
1578 -- The exception Constraint_Error is raised by integer
1579 -- division, rem and mod if the right operand is zero.
1580
1581 if Right_Int = 0 then
1582 Apply_Compile_Time_Constraint_Error
fbf5a39b
AC
1583 (N, "rem with zero divisor",
1584 CE_Divide_By_Zero,
1585 Warn => not Stat);
996ae0b0 1586 return;
fbf5a39b 1587
996ae0b0
RK
1588 else
1589 Result := Left_Int rem Right_Int;
1590 end if;
1591
1592 when others =>
1593 raise Program_Error;
1594 end case;
1595
1596 -- Adjust the result by the modulus if the type is a modular type
1597
1598 if Is_Modular_Integer_Type (Ltype) then
1599 Result := Result mod Modulus (Ltype);
82c80734
RD
1600
1601 -- For a signed integer type, check non-static overflow
1602
1603 elsif (not Stat) and then Is_Signed_Integer_Type (Ltype) then
1604 declare
1605 BT : constant Entity_Id := Base_Type (Ltype);
1606 Lo : constant Uint := Expr_Value (Type_Low_Bound (BT));
1607 Hi : constant Uint := Expr_Value (Type_High_Bound (BT));
1608 begin
1609 if Result < Lo or else Result > Hi then
1610 Apply_Compile_Time_Constraint_Error
1611 (N, "value not in range of }?",
1612 CE_Overflow_Check_Failed,
1613 Ent => BT);
1614 return;
1615 end if;
1616 end;
996ae0b0
RK
1617 end if;
1618
82c80734
RD
1619 -- If we get here we can fold the result
1620
fbf5a39b 1621 Fold_Uint (N, Result, Stat);
996ae0b0
RK
1622 end;
1623
d7567964
TQ
1624 -- Cases where at least one operand is a real. We handle the cases of
1625 -- both reals, or mixed/real integer cases (the latter happen only for
1626 -- divide and multiply, and the result is always real).
996ae0b0
RK
1627
1628 elsif Is_Real_Type (Ltype) or else Is_Real_Type (Rtype) then
1629 declare
1630 Left_Real : Ureal;
1631 Right_Real : Ureal;
1632 Result : Ureal;
1633
1634 begin
1635 if Is_Real_Type (Ltype) then
1636 Left_Real := Expr_Value_R (Left);
1637 else
1638 Left_Real := UR_From_Uint (Expr_Value (Left));
1639 end if;
1640
1641 if Is_Real_Type (Rtype) then
1642 Right_Real := Expr_Value_R (Right);
1643 else
1644 Right_Real := UR_From_Uint (Expr_Value (Right));
1645 end if;
1646
1647 if Nkind (N) = N_Op_Add then
1648 Result := Left_Real + Right_Real;
1649
1650 elsif Nkind (N) = N_Op_Subtract then
1651 Result := Left_Real - Right_Real;
1652
1653 elsif Nkind (N) = N_Op_Multiply then
1654 Result := Left_Real * Right_Real;
1655
1656 else pragma Assert (Nkind (N) = N_Op_Divide);
1657 if UR_Is_Zero (Right_Real) then
1658 Apply_Compile_Time_Constraint_Error
07fc65c4 1659 (N, "division by zero", CE_Divide_By_Zero);
996ae0b0
RK
1660 return;
1661 end if;
1662
1663 Result := Left_Real / Right_Real;
1664 end if;
1665
fbf5a39b 1666 Fold_Ureal (N, Result, Stat);
996ae0b0
RK
1667 end;
1668 end if;
d7567964
TQ
1669
1670 -- If the operator was resolved to a specific type, make sure that type
1671 -- is frozen even if the expression is folded into a literal (which has
1672 -- a universal type).
1673
1674 if Present (Otype) then
1675 Freeze_Before (N, Otype);
1676 end if;
996ae0b0
RK
1677 end Eval_Arithmetic_Op;
1678
1679 ----------------------------
1680 -- Eval_Character_Literal --
1681 ----------------------------
1682
1683 -- Nothing to be done!
1684
1685 procedure Eval_Character_Literal (N : Node_Id) is
07fc65c4 1686 pragma Warnings (Off, N);
996ae0b0
RK
1687 begin
1688 null;
1689 end Eval_Character_Literal;
1690
c01a9391
AC
1691 ---------------
1692 -- Eval_Call --
1693 ---------------
1694
1695 -- Static function calls are either calls to predefined operators
1696 -- with static arguments, or calls to functions that rename a literal.
1697 -- Only the latter case is handled here, predefined operators are
1698 -- constant-folded elsewhere.
29797f34 1699
c01a9391
AC
1700 -- If the function is itself inherited (see 7423-001) the literal of
1701 -- the parent type must be explicitly converted to the return type
1702 -- of the function.
1703
1704 procedure Eval_Call (N : Node_Id) is
1705 Loc : constant Source_Ptr := Sloc (N);
1706 Typ : constant Entity_Id := Etype (N);
1707 Lit : Entity_Id;
1708
1709 begin
1710 if Nkind (N) = N_Function_Call
1711 and then No (Parameter_Associations (N))
1712 and then Is_Entity_Name (Name (N))
1713 and then Present (Alias (Entity (Name (N))))
1714 and then Is_Enumeration_Type (Base_Type (Typ))
1715 then
b81a5940 1716 Lit := Ultimate_Alias (Entity (Name (N)));
c01a9391
AC
1717
1718 if Ekind (Lit) = E_Enumeration_Literal then
1719 if Base_Type (Etype (Lit)) /= Base_Type (Typ) then
1720 Rewrite
1721 (N, Convert_To (Typ, New_Occurrence_Of (Lit, Loc)));
1722 else
1723 Rewrite (N, New_Occurrence_Of (Lit, Loc));
1724 end if;
1725
1726 Resolve (N, Typ);
1727 end if;
1728 end if;
1729 end Eval_Call;
1730
19d846a0
RD
1731 --------------------------
1732 -- Eval_Case_Expression --
1733 --------------------------
1734
1735 -- Right now we do not attempt folding of any case expressions, and the
1736 -- language does not require it, so the only required processing is to
1737 -- do the check for all expressions appearing in the case expression.
1738
1739 procedure Eval_Case_Expression (N : Node_Id) is
1740 Alt : Node_Id;
1741
1742 begin
1743 Check_Non_Static_Context (Expression (N));
1744
1745 Alt := First (Alternatives (N));
1746 while Present (Alt) loop
1747 Check_Non_Static_Context (Expression (Alt));
1748 Next (Alt);
1749 end loop;
1750 end Eval_Case_Expression;
1751
996ae0b0
RK
1752 ------------------------
1753 -- Eval_Concatenation --
1754 ------------------------
1755
3996951a
TQ
1756 -- Concatenation is a static function, so the result is static if both
1757 -- operands are static (RM 4.9(7), 4.9(21)).
996ae0b0
RK
1758
1759 procedure Eval_Concatenation (N : Node_Id) is
f91b40db
GB
1760 Left : constant Node_Id := Left_Opnd (N);
1761 Right : constant Node_Id := Right_Opnd (N);
1762 C_Typ : constant Entity_Id := Root_Type (Component_Type (Etype (N)));
996ae0b0
RK
1763 Stat : Boolean;
1764 Fold : Boolean;
996ae0b0
RK
1765
1766 begin
3996951a
TQ
1767 -- Concatenation is never static in Ada 83, so if Ada 83 check operand
1768 -- non-static context.
996ae0b0 1769
0ab80019 1770 if Ada_Version = Ada_83
996ae0b0
RK
1771 and then Comes_From_Source (N)
1772 then
1773 Check_Non_Static_Context (Left);
1774 Check_Non_Static_Context (Right);
1775 return;
1776 end if;
1777
1778 -- If not foldable we are done. In principle concatenation that yields
1779 -- any string type is static (i.e. an array type of character types).
1780 -- However, character types can include enumeration literals, and
1781 -- concatenation in that case cannot be described by a literal, so we
1782 -- only consider the operation static if the result is an array of
1783 -- (a descendant of) a predefined character type.
1784
1785 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
1786
3996951a 1787 if not (Is_Standard_Character_Type (C_Typ) and then Fold) then
996ae0b0
RK
1788 Set_Is_Static_Expression (N, False);
1789 return;
1790 end if;
1791
82c80734 1792 -- Compile time string concatenation
996ae0b0 1793
3996951a
TQ
1794 -- ??? Note that operands that are aggregates can be marked as static,
1795 -- so we should attempt at a later stage to fold concatenations with
1796 -- such aggregates.
996ae0b0
RK
1797
1798 declare
b54ddf5a
BD
1799 Left_Str : constant Node_Id := Get_String_Val (Left);
1800 Left_Len : Nat;
1801 Right_Str : constant Node_Id := Get_String_Val (Right);
1802 Folded_Val : String_Id;
996ae0b0
RK
1803
1804 begin
1805 -- Establish new string literal, and store left operand. We make
1806 -- sure to use the special Start_String that takes an operand if
1807 -- the left operand is a string literal. Since this is optimized
1808 -- in the case where that is the most recently created string
1809 -- literal, we ensure efficient time/space behavior for the
1810 -- case of a concatenation of a series of string literals.
1811
1812 if Nkind (Left_Str) = N_String_Literal then
f91b40db 1813 Left_Len := String_Length (Strval (Left_Str));
b54ddf5a
BD
1814
1815 -- If the left operand is the empty string, and the right operand
1816 -- is a string literal (the case of "" & "..."), the result is the
1817 -- value of the right operand. This optimization is important when
1818 -- Is_Folded_In_Parser, to avoid copying an enormous right
1819 -- operand.
1820
1821 if Left_Len = 0 and then Nkind (Right_Str) = N_String_Literal then
1822 Folded_Val := Strval (Right_Str);
1823 else
1824 Start_String (Strval (Left_Str));
1825 end if;
1826
996ae0b0
RK
1827 else
1828 Start_String;
82c80734 1829 Store_String_Char (UI_To_CC (Char_Literal_Value (Left_Str)));
f91b40db 1830 Left_Len := 1;
996ae0b0
RK
1831 end if;
1832
b54ddf5a
BD
1833 -- Now append the characters of the right operand, unless we
1834 -- optimized the "" & "..." case above.
996ae0b0
RK
1835
1836 if Nkind (Right_Str) = N_String_Literal then
b54ddf5a
BD
1837 if Left_Len /= 0 then
1838 Store_String_Chars (Strval (Right_Str));
1839 Folded_Val := End_String;
1840 end if;
996ae0b0 1841 else
82c80734 1842 Store_String_Char (UI_To_CC (Char_Literal_Value (Right_Str)));
b54ddf5a 1843 Folded_Val := End_String;
996ae0b0
RK
1844 end if;
1845
1846 Set_Is_Static_Expression (N, Stat);
1847
1848 if Stat then
f91b40db
GB
1849
1850 -- If left operand is the empty string, the result is the
1851 -- right operand, including its bounds if anomalous.
1852
1853 if Left_Len = 0
1854 and then Is_Array_Type (Etype (Right))
1855 and then Etype (Right) /= Any_String
1856 then
1857 Set_Etype (N, Etype (Right));
1858 end if;
1859
b54ddf5a 1860 Fold_Str (N, Folded_Val, Static => True);
996ae0b0
RK
1861 end if;
1862 end;
1863 end Eval_Concatenation;
1864
1865 ---------------------------------
1866 -- Eval_Conditional_Expression --
1867 ---------------------------------
1868
4d777a71 1869 -- We can fold to a static expression if the condition and both constituent
1cf3727f 1870 -- expressions are static. Otherwise, the only required processing is to do
4d777a71 1871 -- the check for non-static context for the then and else expressions.
996ae0b0
RK
1872
1873 procedure Eval_Conditional_Expression (N : Node_Id) is
4d777a71
AC
1874 Condition : constant Node_Id := First (Expressions (N));
1875 Then_Expr : constant Node_Id := Next (Condition);
1876 Else_Expr : constant Node_Id := Next (Then_Expr);
1877 Result : Node_Id;
1878 Non_Result : Node_Id;
1879
1880 Rstat : constant Boolean :=
1881 Is_Static_Expression (Condition)
1882 and then
1883 Is_Static_Expression (Then_Expr)
1884 and then
1885 Is_Static_Expression (Else_Expr);
1886
996ae0b0 1887 begin
4d777a71
AC
1888 -- If any operand is Any_Type, just propagate to result and do not try
1889 -- to fold, this prevents cascaded errors.
1890
1891 if Etype (Condition) = Any_Type or else
1892 Etype (Then_Expr) = Any_Type or else
1893 Etype (Else_Expr) = Any_Type
1894 then
1895 Set_Etype (N, Any_Type);
1896 Set_Is_Static_Expression (N, False);
1897 return;
1898
1899 -- Static case where we can fold. Note that we don't try to fold cases
1900 -- where the condition is known at compile time, but the result is
1901 -- non-static. This avoids possible cases of infinite recursion where
1902 -- the expander puts in a redundant test and we remove it. Instead we
1903 -- deal with these cases in the expander.
1904
1905 elsif Rstat then
1906
1907 -- Select result operand
1908
1909 if Is_True (Expr_Value (Condition)) then
1910 Result := Then_Expr;
1911 Non_Result := Else_Expr;
1912 else
1913 Result := Else_Expr;
1914 Non_Result := Then_Expr;
1915 end if;
1916
1917 -- Note that it does not matter if the non-result operand raises a
1918 -- Constraint_Error, but if the result raises constraint error then
1919 -- we replace the node with a raise constraint error. This will
1920 -- properly propagate Raises_Constraint_Error since this flag is
1921 -- set in Result.
1922
1923 if Raises_Constraint_Error (Result) then
1924 Rewrite_In_Raise_CE (N, Result);
1925 Check_Non_Static_Context (Non_Result);
1926
1927 -- Otherwise the result operand replaces the original node
1928
1929 else
1930 Rewrite (N, Relocate_Node (Result));
1931 end if;
1932
1933 -- Case of condition not known at compile time
1934
1935 else
1936 Check_Non_Static_Context (Condition);
1937 Check_Non_Static_Context (Then_Expr);
1938 Check_Non_Static_Context (Else_Expr);
1939 end if;
1940
1941 Set_Is_Static_Expression (N, Rstat);
996ae0b0
RK
1942 end Eval_Conditional_Expression;
1943
1944 ----------------------
1945 -- Eval_Entity_Name --
1946 ----------------------
1947
1948 -- This procedure is used for identifiers and expanded names other than
1949 -- named numbers (see Eval_Named_Integer, Eval_Named_Real. These are
1950 -- static if they denote a static constant (RM 4.9(6)) or if the name
1951 -- denotes an enumeration literal (RM 4.9(22)).
1952
1953 procedure Eval_Entity_Name (N : Node_Id) is
1954 Def_Id : constant Entity_Id := Entity (N);
1955 Val : Node_Id;
1956
1957 begin
1958 -- Enumeration literals are always considered to be constants
1959 -- and cannot raise constraint error (RM 4.9(22)).
1960
1961 if Ekind (Def_Id) = E_Enumeration_Literal then
1962 Set_Is_Static_Expression (N);
1963 return;
1964
1965 -- A name is static if it denotes a static constant (RM 4.9(5)), and
1966 -- we also copy Raise_Constraint_Error. Notice that even if non-static,
1967 -- it does not violate 10.2.1(8) here, since this is not a variable.
1968
1969 elsif Ekind (Def_Id) = E_Constant then
1970
1971 -- Deferred constants must always be treated as nonstatic
1972 -- outside the scope of their full view.
1973
1974 if Present (Full_View (Def_Id))
1975 and then not In_Open_Scopes (Scope (Def_Id))
1976 then
1977 Val := Empty;
1978 else
1979 Val := Constant_Value (Def_Id);
1980 end if;
1981
1982 if Present (Val) then
1983 Set_Is_Static_Expression
1984 (N, Is_Static_Expression (Val)
1985 and then Is_Static_Subtype (Etype (Def_Id)));
1986 Set_Raises_Constraint_Error (N, Raises_Constraint_Error (Val));
1987
1988 if not Is_Static_Expression (N)
1989 and then not Is_Generic_Type (Etype (N))
1990 then
1991 Validate_Static_Object_Name (N);
1992 end if;
1993
1994 return;
1995 end if;
1996 end if;
1997
82c80734 1998 -- Fall through if the name is not static
996ae0b0
RK
1999
2000 Validate_Static_Object_Name (N);
2001 end Eval_Entity_Name;
2002
2003 ----------------------------
2004 -- Eval_Indexed_Component --
2005 ----------------------------
2006
8cbb664e
MG
2007 -- Indexed components are never static, so we need to perform the check
2008 -- for non-static context on the index values. Then, we check if the
2009 -- value can be obtained at compile time, even though it is non-static.
996ae0b0
RK
2010
2011 procedure Eval_Indexed_Component (N : Node_Id) is
2012 Expr : Node_Id;
2013
2014 begin
fbf5a39b
AC
2015 -- Check for non-static context on index values
2016
996ae0b0
RK
2017 Expr := First (Expressions (N));
2018 while Present (Expr) loop
2019 Check_Non_Static_Context (Expr);
2020 Next (Expr);
2021 end loop;
2022
fbf5a39b
AC
2023 -- If the indexed component appears in an object renaming declaration
2024 -- then we do not want to try to evaluate it, since in this case we
2025 -- need the identity of the array element.
2026
2027 if Nkind (Parent (N)) = N_Object_Renaming_Declaration then
2028 return;
2029
2030 -- Similarly if the indexed component appears as the prefix of an
2031 -- attribute we don't want to evaluate it, because at least for
2032 -- some cases of attributes we need the identify (e.g. Access, Size)
2033
2034 elsif Nkind (Parent (N)) = N_Attribute_Reference then
2035 return;
2036 end if;
2037
2038 -- Note: there are other cases, such as the left side of an assignment,
2039 -- or an OUT parameter for a call, where the replacement results in the
2040 -- illegal use of a constant, But these cases are illegal in the first
2041 -- place, so the replacement, though silly, is harmless.
2042
2043 -- Now see if this is a constant array reference
8cbb664e
MG
2044
2045 if List_Length (Expressions (N)) = 1
2046 and then Is_Entity_Name (Prefix (N))
2047 and then Ekind (Entity (Prefix (N))) = E_Constant
2048 and then Present (Constant_Value (Entity (Prefix (N))))
2049 then
2050 declare
2051 Loc : constant Source_Ptr := Sloc (N);
2052 Arr : constant Node_Id := Constant_Value (Entity (Prefix (N)));
2053 Sub : constant Node_Id := First (Expressions (N));
2054
2055 Atyp : Entity_Id;
2056 -- Type of array
2057
2058 Lin : Nat;
2059 -- Linear one's origin subscript value for array reference
2060
2061 Lbd : Node_Id;
2062 -- Lower bound of the first array index
2063
2064 Elm : Node_Id;
2065 -- Value from constant array
2066
2067 begin
2068 Atyp := Etype (Arr);
2069
2070 if Is_Access_Type (Atyp) then
2071 Atyp := Designated_Type (Atyp);
2072 end if;
2073
9dbf1c3e
RD
2074 -- If we have an array type (we should have but perhaps there are
2075 -- error cases where this is not the case), then see if we can do
2076 -- a constant evaluation of the array reference.
8cbb664e 2077
ebd34478 2078 if Is_Array_Type (Atyp) and then Atyp /= Any_Composite then
8cbb664e
MG
2079 if Ekind (Atyp) = E_String_Literal_Subtype then
2080 Lbd := String_Literal_Low_Bound (Atyp);
2081 else
2082 Lbd := Type_Low_Bound (Etype (First_Index (Atyp)));
2083 end if;
2084
2085 if Compile_Time_Known_Value (Sub)
2086 and then Nkind (Arr) = N_Aggregate
2087 and then Compile_Time_Known_Value (Lbd)
2088 and then Is_Discrete_Type (Component_Type (Atyp))
2089 then
2090 Lin := UI_To_Int (Expr_Value (Sub) - Expr_Value (Lbd)) + 1;
2091
2092 if List_Length (Expressions (Arr)) >= Lin then
2093 Elm := Pick (Expressions (Arr), Lin);
2094
2095 -- If the resulting expression is compile time known,
2096 -- then we can rewrite the indexed component with this
2097 -- value, being sure to mark the result as non-static.
2098 -- We also reset the Sloc, in case this generates an
2099 -- error later on (e.g. 136'Access).
2100
2101 if Compile_Time_Known_Value (Elm) then
2102 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2103 Set_Is_Static_Expression (N, False);
2104 Set_Sloc (N, Loc);
2105 end if;
2106 end if;
9fbb3ae6
AC
2107
2108 -- We can also constant-fold if the prefix is a string literal.
2109 -- This will be useful in an instantiation or an inlining.
2110
2111 elsif Compile_Time_Known_Value (Sub)
2112 and then Nkind (Arr) = N_String_Literal
2113 and then Compile_Time_Known_Value (Lbd)
2114 and then Expr_Value (Lbd) = 1
2115 and then Expr_Value (Sub) <=
2116 String_Literal_Length (Etype (Arr))
2117 then
2118 declare
2119 C : constant Char_Code :=
2120 Get_String_Char (Strval (Arr),
2121 UI_To_Int (Expr_Value (Sub)));
2122 begin
2123 Set_Character_Literal_Name (C);
2124
2125 Elm :=
2126 Make_Character_Literal (Loc,
2127 Chars => Name_Find,
2128 Char_Literal_Value => UI_From_CC (C));
2129 Set_Etype (Elm, Component_Type (Atyp));
2130 Rewrite (N, Duplicate_Subexpr_No_Checks (Elm));
2131 Set_Is_Static_Expression (N, False);
2132 end;
8cbb664e
MG
2133 end if;
2134 end if;
2135 end;
2136 end if;
996ae0b0
RK
2137 end Eval_Indexed_Component;
2138
2139 --------------------------
2140 -- Eval_Integer_Literal --
2141 --------------------------
2142
2143 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2144 -- as static by the analyzer. The reason we did it that early is to allow
2145 -- the possibility of turning off the Is_Static_Expression flag after
9dbf1c3e
RD
2146 -- analysis, but before resolution, when integer literals are generated in
2147 -- the expander that do not correspond to static expressions.
996ae0b0
RK
2148
2149 procedure Eval_Integer_Literal (N : Node_Id) is
2150 T : constant Entity_Id := Etype (N);
2151
5d09245e 2152 function In_Any_Integer_Context return Boolean;
1d1bd8ad
AC
2153 -- If the literal is resolved with a specific type in a context where
2154 -- the expected type is Any_Integer, there are no range checks on the
2155 -- literal. By the time the literal is evaluated, it carries the type
2156 -- imposed by the enclosing expression, and we must recover the context
2157 -- to determine that Any_Integer is meant.
5d09245e
AC
2158
2159 ----------------------------
09494c32 2160 -- In_Any_Integer_Context --
5d09245e
AC
2161 ----------------------------
2162
2163 function In_Any_Integer_Context return Boolean is
2164 Par : constant Node_Id := Parent (N);
2165 K : constant Node_Kind := Nkind (Par);
2166
2167 begin
2168 -- Any_Integer also appears in digits specifications for real types,
1d1bd8ad
AC
2169 -- but those have bounds smaller that those of any integer base type,
2170 -- so we can safely ignore these cases.
5d09245e
AC
2171
2172 return K = N_Number_Declaration
2173 or else K = N_Attribute_Reference
2174 or else K = N_Attribute_Definition_Clause
2175 or else K = N_Modular_Type_Definition
2176 or else K = N_Signed_Integer_Type_Definition;
2177 end In_Any_Integer_Context;
2178
2179 -- Start of processing for Eval_Integer_Literal
2180
996ae0b0 2181 begin
5d09245e 2182
996ae0b0 2183 -- If the literal appears in a non-expression context, then it is
1d1bd8ad
AC
2184 -- certainly appearing in a non-static context, so check it. This is
2185 -- actually a redundant check, since Check_Non_Static_Context would
2186 -- check it, but it seems worth while avoiding the call.
996ae0b0 2187
5d09245e
AC
2188 if Nkind (Parent (N)) not in N_Subexpr
2189 and then not In_Any_Integer_Context
2190 then
996ae0b0
RK
2191 Check_Non_Static_Context (N);
2192 end if;
2193
2194 -- Modular integer literals must be in their base range
2195
2196 if Is_Modular_Integer_Type (T)
c800f862 2197 and then Is_Out_Of_Range (N, Base_Type (T), Assume_Valid => True)
996ae0b0
RK
2198 then
2199 Out_Of_Range (N);
2200 end if;
2201 end Eval_Integer_Literal;
2202
2203 ---------------------
2204 -- Eval_Logical_Op --
2205 ---------------------
2206
2207 -- Logical operations are static functions, so the result is potentially
2208 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2209
2210 procedure Eval_Logical_Op (N : Node_Id) is
2211 Left : constant Node_Id := Left_Opnd (N);
2212 Right : constant Node_Id := Right_Opnd (N);
2213 Stat : Boolean;
2214 Fold : Boolean;
2215
2216 begin
2217 -- If not foldable we are done
2218
2219 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2220
2221 if not Fold then
2222 return;
2223 end if;
2224
2225 -- Compile time evaluation of logical operation
2226
2227 declare
2228 Left_Int : constant Uint := Expr_Value (Left);
2229 Right_Int : constant Uint := Expr_Value (Right);
2230
2231 begin
22cb89b5 2232 -- VMS includes bitwise operations on signed types
001c7783
AC
2233
2234 if Is_Modular_Integer_Type (Etype (N))
2235 or else Is_VMS_Operator (Entity (N))
2236 then
996ae0b0
RK
2237 declare
2238 Left_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2239 Right_Bits : Bits (0 .. UI_To_Int (Esize (Etype (N))) - 1);
2240
2241 begin
2242 To_Bits (Left_Int, Left_Bits);
2243 To_Bits (Right_Int, Right_Bits);
2244
2245 -- Note: should really be able to use array ops instead of
2246 -- these loops, but they weren't working at the time ???
2247
2248 if Nkind (N) = N_Op_And then
2249 for J in Left_Bits'Range loop
2250 Left_Bits (J) := Left_Bits (J) and Right_Bits (J);
2251 end loop;
2252
2253 elsif Nkind (N) = N_Op_Or then
2254 for J in Left_Bits'Range loop
2255 Left_Bits (J) := Left_Bits (J) or Right_Bits (J);
2256 end loop;
2257
2258 else
2259 pragma Assert (Nkind (N) = N_Op_Xor);
2260
2261 for J in Left_Bits'Range loop
2262 Left_Bits (J) := Left_Bits (J) xor Right_Bits (J);
2263 end loop;
2264 end if;
2265
fbf5a39b 2266 Fold_Uint (N, From_Bits (Left_Bits, Etype (N)), Stat);
996ae0b0
RK
2267 end;
2268
2269 else
2270 pragma Assert (Is_Boolean_Type (Etype (N)));
2271
2272 if Nkind (N) = N_Op_And then
2273 Fold_Uint (N,
fbf5a39b 2274 Test (Is_True (Left_Int) and then Is_True (Right_Int)), Stat);
996ae0b0
RK
2275
2276 elsif Nkind (N) = N_Op_Or then
2277 Fold_Uint (N,
fbf5a39b 2278 Test (Is_True (Left_Int) or else Is_True (Right_Int)), Stat);
996ae0b0
RK
2279
2280 else
2281 pragma Assert (Nkind (N) = N_Op_Xor);
2282 Fold_Uint (N,
fbf5a39b 2283 Test (Is_True (Left_Int) xor Is_True (Right_Int)), Stat);
996ae0b0
RK
2284 end if;
2285 end if;
996ae0b0
RK
2286 end;
2287 end Eval_Logical_Op;
2288
2289 ------------------------
2290 -- Eval_Membership_Op --
2291 ------------------------
2292
1d1bd8ad
AC
2293 -- A membership test is potentially static if the expression is static, and
2294 -- the range is a potentially static range, or is a subtype mark denoting a
2295 -- static subtype (RM 4.9(12)).
996ae0b0
RK
2296
2297 procedure Eval_Membership_Op (N : Node_Id) is
2298 Left : constant Node_Id := Left_Opnd (N);
2299 Right : constant Node_Id := Right_Opnd (N);
2300 Def_Id : Entity_Id;
2301 Lo : Node_Id;
2302 Hi : Node_Id;
2303 Result : Boolean;
2304 Stat : Boolean;
2305 Fold : Boolean;
2306
2307 begin
1d1bd8ad
AC
2308 -- Ignore if error in either operand, except to make sure that Any_Type
2309 -- is properly propagated to avoid junk cascaded errors.
996ae0b0 2310
22cb89b5 2311 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
996ae0b0
RK
2312 Set_Etype (N, Any_Type);
2313 return;
2314 end if;
2315
4818e7b9
RD
2316 -- Ignore if types involved have predicates
2317
2318 if Present (Predicate_Function (Etype (Left)))
2319 or else
2320 Present (Predicate_Function (Etype (Right)))
2321 then
2322 return;
2323 end if;
2324
996ae0b0
RK
2325 -- Case of right operand is a subtype name
2326
2327 if Is_Entity_Name (Right) then
2328 Def_Id := Entity (Right);
2329
2330 if (Is_Scalar_Type (Def_Id) or else Is_String_Type (Def_Id))
2331 and then Is_OK_Static_Subtype (Def_Id)
2332 then
2333 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2334
2335 if not Fold or else not Stat then
2336 return;
2337 end if;
2338 else
2339 Check_Non_Static_Context (Left);
2340 return;
2341 end if;
2342
1d1bd8ad 2343 -- For string membership tests we will check the length further on
996ae0b0
RK
2344
2345 if not Is_String_Type (Def_Id) then
2346 Lo := Type_Low_Bound (Def_Id);
2347 Hi := Type_High_Bound (Def_Id);
2348
2349 else
2350 Lo := Empty;
2351 Hi := Empty;
2352 end if;
2353
2354 -- Case of right operand is a range
2355
2356 else
2357 if Is_Static_Range (Right) then
2358 Test_Expression_Is_Foldable (N, Left, Stat, Fold);
2359
2360 if not Fold or else not Stat then
2361 return;
2362
2363 -- If one bound of range raises CE, then don't try to fold
2364
2365 elsif not Is_OK_Static_Range (Right) then
2366 Check_Non_Static_Context (Left);
2367 return;
2368 end if;
2369
2370 else
2371 Check_Non_Static_Context (Left);
2372 return;
2373 end if;
2374
2375 -- Here we know range is an OK static range
2376
2377 Lo := Low_Bound (Right);
2378 Hi := High_Bound (Right);
2379 end if;
2380
2381 -- For strings we check that the length of the string expression is
2382 -- compatible with the string subtype if the subtype is constrained,
2383 -- or if unconstrained then the test is always true.
2384
2385 if Is_String_Type (Etype (Right)) then
2386 if not Is_Constrained (Etype (Right)) then
2387 Result := True;
2388
2389 else
2390 declare
2391 Typlen : constant Uint := String_Type_Len (Etype (Right));
2392 Strlen : constant Uint :=
22cb89b5
AC
2393 UI_From_Int
2394 (String_Length (Strval (Get_String_Val (Left))));
996ae0b0
RK
2395 begin
2396 Result := (Typlen = Strlen);
2397 end;
2398 end if;
2399
1d1bd8ad
AC
2400 -- Fold the membership test. We know we have a static range and Lo and
2401 -- Hi are set to the expressions for the end points of this range.
996ae0b0
RK
2402
2403 elsif Is_Real_Type (Etype (Right)) then
2404 declare
2405 Leftval : constant Ureal := Expr_Value_R (Left);
2406
2407 begin
2408 Result := Expr_Value_R (Lo) <= Leftval
2409 and then Leftval <= Expr_Value_R (Hi);
2410 end;
2411
2412 else
2413 declare
2414 Leftval : constant Uint := Expr_Value (Left);
2415
2416 begin
2417 Result := Expr_Value (Lo) <= Leftval
2418 and then Leftval <= Expr_Value (Hi);
2419 end;
2420 end if;
2421
2422 if Nkind (N) = N_Not_In then
2423 Result := not Result;
2424 end if;
2425
fbf5a39b 2426 Fold_Uint (N, Test (Result), True);
d7567964 2427
996ae0b0 2428 Warn_On_Known_Condition (N);
996ae0b0
RK
2429 end Eval_Membership_Op;
2430
2431 ------------------------
2432 -- Eval_Named_Integer --
2433 ------------------------
2434
2435 procedure Eval_Named_Integer (N : Node_Id) is
2436 begin
2437 Fold_Uint (N,
fbf5a39b 2438 Expr_Value (Expression (Declaration_Node (Entity (N)))), True);
996ae0b0
RK
2439 end Eval_Named_Integer;
2440
2441 ---------------------
2442 -- Eval_Named_Real --
2443 ---------------------
2444
2445 procedure Eval_Named_Real (N : Node_Id) is
2446 begin
2447 Fold_Ureal (N,
fbf5a39b 2448 Expr_Value_R (Expression (Declaration_Node (Entity (N)))), True);
996ae0b0
RK
2449 end Eval_Named_Real;
2450
2451 -------------------
2452 -- Eval_Op_Expon --
2453 -------------------
2454
2455 -- Exponentiation is a static functions, so the result is potentially
2456 -- static if both operands are potentially static (RM 4.9(7), 4.9(20)).
2457
2458 procedure Eval_Op_Expon (N : Node_Id) is
2459 Left : constant Node_Id := Left_Opnd (N);
2460 Right : constant Node_Id := Right_Opnd (N);
2461 Stat : Boolean;
2462 Fold : Boolean;
2463
2464 begin
2465 -- If not foldable we are done
2466
2467 Test_Expression_Is_Foldable (N, Left, Right, Stat, Fold);
2468
2469 if not Fold then
2470 return;
2471 end if;
2472
2473 -- Fold exponentiation operation
2474
2475 declare
2476 Right_Int : constant Uint := Expr_Value (Right);
2477
2478 begin
2479 -- Integer case
2480
2481 if Is_Integer_Type (Etype (Left)) then
2482 declare
2483 Left_Int : constant Uint := Expr_Value (Left);
2484 Result : Uint;
2485
2486 begin
22cb89b5
AC
2487 -- Exponentiation of an integer raises Constraint_Error for a
2488 -- negative exponent (RM 4.5.6).
996ae0b0
RK
2489
2490 if Right_Int < 0 then
2491 Apply_Compile_Time_Constraint_Error
fbf5a39b
AC
2492 (N, "integer exponent negative",
2493 CE_Range_Check_Failed,
2494 Warn => not Stat);
996ae0b0
RK
2495 return;
2496
2497 else
2498 if OK_Bits (N, Num_Bits (Left_Int) * Right_Int) then
2499 Result := Left_Int ** Right_Int;
2500 else
2501 Result := Left_Int;
2502 end if;
2503
2504 if Is_Modular_Integer_Type (Etype (N)) then
2505 Result := Result mod Modulus (Etype (N));
2506 end if;
2507
fbf5a39b 2508 Fold_Uint (N, Result, Stat);
996ae0b0
RK
2509 end if;
2510 end;
2511
2512 -- Real case
2513
2514 else
2515 declare
2516 Left_Real : constant Ureal := Expr_Value_R (Left);
2517
2518 begin
2519 -- Cannot have a zero base with a negative exponent
2520
2521 if UR_Is_Zero (Left_Real) then
2522
2523 if Right_Int < 0 then
2524 Apply_Compile_Time_Constraint_Error
fbf5a39b
AC
2525 (N, "zero ** negative integer",
2526 CE_Range_Check_Failed,
2527 Warn => not Stat);
996ae0b0
RK
2528 return;
2529 else
fbf5a39b 2530 Fold_Ureal (N, Ureal_0, Stat);
996ae0b0
RK
2531 end if;
2532
2533 else
fbf5a39b 2534 Fold_Ureal (N, Left_Real ** Right_Int, Stat);
996ae0b0
RK
2535 end if;
2536 end;
2537 end if;
996ae0b0
RK
2538 end;
2539 end Eval_Op_Expon;
2540
2541 -----------------
2542 -- Eval_Op_Not --
2543 -----------------
2544
2545 -- The not operation is a static functions, so the result is potentially
2546 -- static if the operand is potentially static (RM 4.9(7), 4.9(20)).
2547
2548 procedure Eval_Op_Not (N : Node_Id) is
2549 Right : constant Node_Id := Right_Opnd (N);
2550 Stat : Boolean;
2551 Fold : Boolean;
2552
2553 begin
2554 -- If not foldable we are done
2555
2556 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
2557
2558 if not Fold then
2559 return;
2560 end if;
2561
2562 -- Fold not operation
2563
2564 declare
2565 Rint : constant Uint := Expr_Value (Right);
2566 Typ : constant Entity_Id := Etype (N);
2567
2568 begin
1d1bd8ad
AC
2569 -- Negation is equivalent to subtracting from the modulus minus one.
2570 -- For a binary modulus this is equivalent to the ones-complement of
2571 -- the original value. For non-binary modulus this is an arbitrary
2572 -- but consistent definition.
996ae0b0
RK
2573
2574 if Is_Modular_Integer_Type (Typ) then
fbf5a39b 2575 Fold_Uint (N, Modulus (Typ) - 1 - Rint, Stat);
996ae0b0
RK
2576
2577 else
2578 pragma Assert (Is_Boolean_Type (Typ));
fbf5a39b 2579 Fold_Uint (N, Test (not Is_True (Rint)), Stat);
996ae0b0
RK
2580 end if;
2581
2582 Set_Is_Static_Expression (N, Stat);
2583 end;
2584 end Eval_Op_Not;
2585
2586 -------------------------------
2587 -- Eval_Qualified_Expression --
2588 -------------------------------
2589
2590 -- A qualified expression is potentially static if its subtype mark denotes
2591 -- a static subtype and its expression is potentially static (RM 4.9 (11)).
2592
2593 procedure Eval_Qualified_Expression (N : Node_Id) is
2594 Operand : constant Node_Id := Expression (N);
2595 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
2596
07fc65c4
GB
2597 Stat : Boolean;
2598 Fold : Boolean;
2599 Hex : Boolean;
996ae0b0
RK
2600
2601 begin
1d1bd8ad 2602 -- Can only fold if target is string or scalar and subtype is static.
22cb89b5
AC
2603 -- Also, do not fold if our parent is an allocator (this is because the
2604 -- qualified expression is really part of the syntactic structure of an
2605 -- allocator, and we do not want to end up with something that
996ae0b0
RK
2606 -- corresponds to "new 1" where the 1 is the result of folding a
2607 -- qualified expression).
2608
2609 if not Is_Static_Subtype (Target_Type)
2610 or else Nkind (Parent (N)) = N_Allocator
2611 then
2612 Check_Non_Static_Context (Operand);
af152989 2613
1d1bd8ad
AC
2614 -- If operand is known to raise constraint_error, set the flag on the
2615 -- expression so it does not get optimized away.
af152989
AC
2616
2617 if Nkind (Operand) = N_Raise_Constraint_Error then
2618 Set_Raises_Constraint_Error (N);
2619 end if;
7324bf49 2620
996ae0b0
RK
2621 return;
2622 end if;
2623
2624 -- If not foldable we are done
2625
2626 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
2627
2628 if not Fold then
2629 return;
2630
2631 -- Don't try fold if target type has constraint error bounds
2632
2633 elsif not Is_OK_Static_Subtype (Target_Type) then
2634 Set_Raises_Constraint_Error (N);
2635 return;
2636 end if;
2637
07fc65c4
GB
2638 -- Here we will fold, save Print_In_Hex indication
2639
2640 Hex := Nkind (Operand) = N_Integer_Literal
2641 and then Print_In_Hex (Operand);
2642
996ae0b0
RK
2643 -- Fold the result of qualification
2644
2645 if Is_Discrete_Type (Target_Type) then
fbf5a39b 2646 Fold_Uint (N, Expr_Value (Operand), Stat);
996ae0b0 2647
07fc65c4
GB
2648 -- Preserve Print_In_Hex indication
2649
2650 if Hex and then Nkind (N) = N_Integer_Literal then
2651 Set_Print_In_Hex (N);
2652 end if;
2653
996ae0b0 2654 elsif Is_Real_Type (Target_Type) then
fbf5a39b 2655 Fold_Ureal (N, Expr_Value_R (Operand), Stat);
996ae0b0
RK
2656
2657 else
fbf5a39b 2658 Fold_Str (N, Strval (Get_String_Val (Operand)), Stat);
996ae0b0
RK
2659
2660 if not Stat then
2661 Set_Is_Static_Expression (N, False);
2662 else
2663 Check_String_Literal_Length (N, Target_Type);
2664 end if;
2665
2666 return;
2667 end if;
2668
fbf5a39b
AC
2669 -- The expression may be foldable but not static
2670
2671 Set_Is_Static_Expression (N, Stat);
2672
c800f862 2673 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
996ae0b0
RK
2674 Out_Of_Range (N);
2675 end if;
996ae0b0
RK
2676 end Eval_Qualified_Expression;
2677
2678 -----------------------
2679 -- Eval_Real_Literal --
2680 -----------------------
2681
2682 -- Numeric literals are static (RM 4.9(1)), and have already been marked
2683 -- as static by the analyzer. The reason we did it that early is to allow
2684 -- the possibility of turning off the Is_Static_Expression flag after
2685 -- analysis, but before resolution, when integer literals are generated
2686 -- in the expander that do not correspond to static expressions.
2687
2688 procedure Eval_Real_Literal (N : Node_Id) is
a1980be8
GB
2689 PK : constant Node_Kind := Nkind (Parent (N));
2690
996ae0b0 2691 begin
1d1bd8ad
AC
2692 -- If the literal appears in a non-expression context and not as part of
2693 -- a number declaration, then it is appearing in a non-static context,
2694 -- so check it.
996ae0b0 2695
a1980be8 2696 if PK not in N_Subexpr and then PK /= N_Number_Declaration then
996ae0b0
RK
2697 Check_Non_Static_Context (N);
2698 end if;
996ae0b0
RK
2699 end Eval_Real_Literal;
2700
2701 ------------------------
2702 -- Eval_Relational_Op --
2703 ------------------------
2704
8a95f4e8
RD
2705 -- Relational operations are static functions, so the result is static if
2706 -- both operands are static (RM 4.9(7), 4.9(20)), except that for strings,
2707 -- the result is never static, even if the operands are.
996ae0b0
RK
2708
2709 procedure Eval_Relational_Op (N : Node_Id) is
2710 Left : constant Node_Id := Left_Opnd (N);
2711 Right : constant Node_Id := Right_Opnd (N);
2712 Typ : constant Entity_Id := Etype (Left);
d7567964 2713 Otype : Entity_Id := Empty;
996ae0b0 2714 Result : Boolean;
996ae0b0
RK
2715
2716 begin
45fc7ddb
HK
2717 -- One special case to deal with first. If we can tell that the result
2718 -- will be false because the lengths of one or more index subtypes are
2719 -- compile time known and different, then we can replace the entire
2720 -- result by False. We only do this for one dimensional arrays, because
2721 -- the case of multi-dimensional arrays is rare and too much trouble! If
2722 -- one of the operands is an illegal aggregate, its type might still be
2723 -- an arbitrary composite type, so nothing to do.
996ae0b0
RK
2724
2725 if Is_Array_Type (Typ)
13f34a3f 2726 and then Typ /= Any_Composite
996ae0b0 2727 and then Number_Dimensions (Typ) = 1
13f34a3f 2728 and then (Nkind (N) = N_Op_Eq or else Nkind (N) = N_Op_Ne)
996ae0b0
RK
2729 then
2730 if Raises_Constraint_Error (Left)
2731 or else Raises_Constraint_Error (Right)
2732 then
2733 return;
2734 end if;
2735
45fc7ddb
HK
2736 -- OK, we have the case where we may be able to do this fold
2737
2738 Length_Mismatch : declare
996ae0b0 2739 procedure Get_Static_Length (Op : Node_Id; Len : out Uint);
1d1bd8ad
AC
2740 -- If Op is an expression for a constrained array with a known at
2741 -- compile time length, then Len is set to this (non-negative
13f34a3f 2742 -- length). Otherwise Len is set to minus 1.
996ae0b0 2743
fbf5a39b
AC
2744 -----------------------
2745 -- Get_Static_Length --
2746 -----------------------
2747
996ae0b0
RK
2748 procedure Get_Static_Length (Op : Node_Id; Len : out Uint) is
2749 T : Entity_Id;
2750
2751 begin
45fc7ddb
HK
2752 -- First easy case string literal
2753
996ae0b0
RK
2754 if Nkind (Op) = N_String_Literal then
2755 Len := UI_From_Int (String_Length (Strval (Op)));
45fc7ddb
HK
2756 return;
2757 end if;
2758
2759 -- Second easy case, not constrained subtype, so no length
996ae0b0 2760
45fc7ddb 2761 if not Is_Constrained (Etype (Op)) then
996ae0b0 2762 Len := Uint_Minus_1;
45fc7ddb
HK
2763 return;
2764 end if;
996ae0b0 2765
45fc7ddb
HK
2766 -- General case
2767
5f44f0d4 2768 T := Etype (First_Index (Etype (Op)));
45fc7ddb
HK
2769
2770 -- The simple case, both bounds are known at compile time
2771
2772 if Is_Discrete_Type (T)
2773 and then
2774 Compile_Time_Known_Value (Type_Low_Bound (T))
2775 and then
2776 Compile_Time_Known_Value (Type_High_Bound (T))
2777 then
2778 Len := UI_Max (Uint_0,
2779 Expr_Value (Type_High_Bound (T)) -
2780 Expr_Value (Type_Low_Bound (T)) + 1);
2781 return;
2782 end if;
2783
2784 -- A more complex case, where the bounds are of the form
2785 -- X [+/- K1] .. X [+/- K2]), where X is an expression that is
2786 -- either A'First or A'Last (with A an entity name), or X is an
2787 -- entity name, and the two X's are the same and K1 and K2 are
2788 -- known at compile time, in this case, the length can also be
2789 -- computed at compile time, even though the bounds are not
22cb89b5 2790 -- known. A common case of this is e.g. (X'First .. X'First+5).
45fc7ddb
HK
2791
2792 Extract_Length : declare
2793 procedure Decompose_Expr
2794 (Expr : Node_Id;
2795 Ent : out Entity_Id;
2796 Kind : out Character;
2797 Cons : out Uint);
2798 -- Given an expression, see if is of the form above,
2799 -- X [+/- K]. If so Ent is set to the entity in X,
2800 -- Kind is 'F','L','E' for 'First/'Last/simple entity,
2801 -- and Cons is the value of K. If the expression is
2802 -- not of the required form, Ent is set to Empty.
2803
2804 --------------------
2805 -- Decompose_Expr --
2806 --------------------
2807
2808 procedure Decompose_Expr
2809 (Expr : Node_Id;
2810 Ent : out Entity_Id;
2811 Kind : out Character;
2812 Cons : out Uint)
2813 is
2814 Exp : Node_Id;
2815
2816 begin
2817 if Nkind (Expr) = N_Op_Add
2818 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2819 then
8a95f4e8 2820 Exp := Left_Opnd (Expr);
45fc7ddb
HK
2821 Cons := Expr_Value (Right_Opnd (Expr));
2822
2823 elsif Nkind (Expr) = N_Op_Subtract
2824 and then Compile_Time_Known_Value (Right_Opnd (Expr))
2825 then
8a95f4e8 2826 Exp := Left_Opnd (Expr);
45fc7ddb 2827 Cons := -Expr_Value (Right_Opnd (Expr));
996ae0b0 2828
8a95f4e8
RD
2829 -- If the bound is a constant created to remove side
2830 -- effects, recover original expression to see if it has
2831 -- one of the recognizable forms.
2832
2833 elsif Nkind (Expr) = N_Identifier
2834 and then not Comes_From_Source (Entity (Expr))
2835 and then Ekind (Entity (Expr)) = E_Constant
2836 and then
2837 Nkind (Parent (Entity (Expr))) = N_Object_Declaration
2838 then
2839 Exp := Expression (Parent (Entity (Expr)));
2840 Decompose_Expr (Exp, Ent, Kind, Cons);
2841
2842 -- If original expression includes an entity, create a
2843 -- reference to it for use below.
2844
2845 if Present (Ent) then
2846 Exp := New_Occurrence_Of (Ent, Sloc (Ent));
2847 end if;
2848
45fc7ddb 2849 else
8a95f4e8 2850 Exp := Expr;
45fc7ddb
HK
2851 Cons := Uint_0;
2852 end if;
2853
2854 -- At this stage Exp is set to the potential X
2855
2856 if Nkind (Exp) = N_Attribute_Reference then
2857 if Attribute_Name (Exp) = Name_First then
2858 Kind := 'F';
8a95f4e8 2859
45fc7ddb
HK
2860 elsif Attribute_Name (Exp) = Name_Last then
2861 Kind := 'L';
8a95f4e8 2862
45fc7ddb
HK
2863 else
2864 Ent := Empty;
2865 return;
2866 end if;
2867
2868 Exp := Prefix (Exp);
2869
2870 else
2871 Kind := 'E';
2872 end if;
2873
2874 if Is_Entity_Name (Exp)
2875 and then Present (Entity (Exp))
2876 then
2877 Ent := Entity (Exp);
2878 else
2879 Ent := Empty;
2880 end if;
2881 end Decompose_Expr;
2882
2883 -- Local Variables
2884
2885 Ent1, Ent2 : Entity_Id;
2886 Kind1, Kind2 : Character;
2887 Cons1, Cons2 : Uint;
2888
2889 -- Start of processing for Extract_Length
2890
2891 begin
bafc9e1d
AC
2892 Decompose_Expr
2893 (Original_Node (Type_Low_Bound (T)), Ent1, Kind1, Cons1);
2894 Decompose_Expr
2895 (Original_Node (Type_High_Bound (T)), Ent2, Kind2, Cons2);
45fc7ddb
HK
2896
2897 if Present (Ent1)
2898 and then Kind1 = Kind2
2899 and then Ent1 = Ent2
996ae0b0 2900 then
45fc7ddb 2901 Len := Cons2 - Cons1 + 1;
996ae0b0
RK
2902 else
2903 Len := Uint_Minus_1;
2904 end if;
45fc7ddb 2905 end Extract_Length;
996ae0b0
RK
2906 end Get_Static_Length;
2907
45fc7ddb
HK
2908 -- Local Variables
2909
996ae0b0
RK
2910 Len_L : Uint;
2911 Len_R : Uint;
2912
45fc7ddb
HK
2913 -- Start of processing for Length_Mismatch
2914
996ae0b0
RK
2915 begin
2916 Get_Static_Length (Left, Len_L);
2917 Get_Static_Length (Right, Len_R);
2918
2919 if Len_L /= Uint_Minus_1
2920 and then Len_R /= Uint_Minus_1
2921 and then Len_L /= Len_R
2922 then
fbf5a39b 2923 Fold_Uint (N, Test (Nkind (N) = N_Op_Ne), False);
996ae0b0
RK
2924 Warn_On_Known_Condition (N);
2925 return;
2926 end if;
45fc7ddb
HK
2927 end Length_Mismatch;
2928 end if;
6eaf4095 2929
5df1266a
AC
2930 declare
2931 Is_Static_Expression : Boolean;
2932 Is_Foldable : Boolean;
2933 pragma Unreferenced (Is_Foldable);
996ae0b0 2934
5df1266a
AC
2935 begin
2936 -- Initialize the value of Is_Static_Expression. The value of
2937 -- Is_Foldable returned by Test_Expression_Is_Foldable is not needed
2938 -- since, even when some operand is a variable, we can still perform
2939 -- the static evaluation of the expression in some cases (for
2940 -- example, for a variable of a subtype of Integer we statically
2941 -- know that any value stored in such variable is smaller than
2942 -- Integer'Last).
2943
2944 Test_Expression_Is_Foldable
2945 (N, Left, Right, Is_Static_Expression, Is_Foldable);
2946
2947 -- Only comparisons of scalars can give static results. In
2948 -- particular, comparisons of strings never yield a static
2949 -- result, even if both operands are static strings.
2950
2951 if not Is_Scalar_Type (Typ) then
2952 Is_Static_Expression := False;
2953 Set_Is_Static_Expression (N, False);
2954 end if;
d7567964 2955
5df1266a
AC
2956 -- For operators on universal numeric types called as functions with
2957 -- an explicit scope, determine appropriate specific numeric type,
2958 -- and diagnose possible ambiguity.
d7567964 2959
5df1266a
AC
2960 if Is_Universal_Numeric_Type (Etype (Left))
2961 and then
2962 Is_Universal_Numeric_Type (Etype (Right))
2963 then
2964 Otype := Find_Universal_Operator_Type (N);
2965 end if;
996ae0b0 2966
5df1266a
AC
2967 -- For static real type expressions, we cannot use
2968 -- Compile_Time_Compare since it worries about run-time
2969 -- results which are not exact.
996ae0b0 2970
5df1266a
AC
2971 if Is_Static_Expression and then Is_Real_Type (Typ) then
2972 declare
2973 Left_Real : constant Ureal := Expr_Value_R (Left);
2974 Right_Real : constant Ureal := Expr_Value_R (Right);
996ae0b0 2975
5df1266a
AC
2976 begin
2977 case Nkind (N) is
2978 when N_Op_Eq => Result := (Left_Real = Right_Real);
2979 when N_Op_Ne => Result := (Left_Real /= Right_Real);
2980 when N_Op_Lt => Result := (Left_Real < Right_Real);
2981 when N_Op_Le => Result := (Left_Real <= Right_Real);
2982 when N_Op_Gt => Result := (Left_Real > Right_Real);
2983 when N_Op_Ge => Result := (Left_Real >= Right_Real);
2984
2985 when others =>
2986 raise Program_Error;
2987 end case;
996ae0b0 2988
5df1266a
AC
2989 Fold_Uint (N, Test (Result), True);
2990 end;
996ae0b0 2991
5df1266a 2992 -- For all other cases, we use Compile_Time_Compare to do the compare
996ae0b0 2993
5df1266a
AC
2994 else
2995 declare
2996 CR : constant Compare_Result :=
2997 Compile_Time_Compare
2998 (Left, Right, Assume_Valid => False);
996ae0b0 2999
5df1266a
AC
3000 begin
3001 if CR = Unknown then
3002 return;
3003 end if;
93c3fca7 3004
5df1266a
AC
3005 case Nkind (N) is
3006 when N_Op_Eq =>
3007 if CR = EQ then
3008 Result := True;
3009 elsif CR = NE or else CR = GT or else CR = LT then
3010 Result := False;
3011 else
3012 return;
3013 end if;
93c3fca7 3014
5df1266a
AC
3015 when N_Op_Ne =>
3016 if CR = NE or else CR = GT or else CR = LT then
3017 Result := True;
3018 elsif CR = EQ then
3019 Result := False;
3020 else
3021 return;
3022 end if;
93c3fca7 3023
5df1266a
AC
3024 when N_Op_Lt =>
3025 if CR = LT then
3026 Result := True;
3027 elsif CR = EQ or else CR = GT or else CR = GE then
3028 Result := False;
3029 else
3030 return;
3031 end if;
93c3fca7 3032
5df1266a
AC
3033 when N_Op_Le =>
3034 if CR = LT or else CR = EQ or else CR = LE then
3035 Result := True;
3036 elsif CR = GT then
3037 Result := False;
3038 else
3039 return;
3040 end if;
93c3fca7 3041
5df1266a
AC
3042 when N_Op_Gt =>
3043 if CR = GT then
3044 Result := True;
3045 elsif CR = EQ or else CR = LT or else CR = LE then
3046 Result := False;
3047 else
3048 return;
3049 end if;
93c3fca7 3050
5df1266a
AC
3051 when N_Op_Ge =>
3052 if CR = GT or else CR = EQ or else CR = GE then
3053 Result := True;
3054 elsif CR = LT then
3055 Result := False;
3056 else
3057 return;
3058 end if;
996ae0b0 3059
5df1266a
AC
3060 when others =>
3061 raise Program_Error;
3062 end case;
3063 end;
93c3fca7 3064
5df1266a
AC
3065 Fold_Uint (N, Test (Result), Is_Static_Expression);
3066 end if;
3067 end;
996ae0b0 3068
d7567964
TQ
3069 -- For the case of a folded relational operator on a specific numeric
3070 -- type, freeze operand type now.
3071
3072 if Present (Otype) then
3073 Freeze_Before (N, Otype);
3074 end if;
3075
996ae0b0
RK
3076 Warn_On_Known_Condition (N);
3077 end Eval_Relational_Op;
3078
3079 ----------------
3080 -- Eval_Shift --
3081 ----------------
3082
22cb89b5
AC
3083 -- Shift operations are intrinsic operations that can never be static, so
3084 -- the only processing required is to perform the required check for a non
3085 -- static context for the two operands.
996ae0b0
RK
3086
3087 -- Actually we could do some compile time evaluation here some time ???
3088
3089 procedure Eval_Shift (N : Node_Id) is
3090 begin
3091 Check_Non_Static_Context (Left_Opnd (N));
3092 Check_Non_Static_Context (Right_Opnd (N));
3093 end Eval_Shift;
3094
3095 ------------------------
3096 -- Eval_Short_Circuit --
3097 ------------------------
3098
22cb89b5
AC
3099 -- A short circuit operation is potentially static if both operands are
3100 -- potentially static (RM 4.9 (13)).
996ae0b0
RK
3101
3102 procedure Eval_Short_Circuit (N : Node_Id) is
3103 Kind : constant Node_Kind := Nkind (N);
3104 Left : constant Node_Id := Left_Opnd (N);
3105 Right : constant Node_Id := Right_Opnd (N);
3106 Left_Int : Uint;
4d777a71
AC
3107
3108 Rstat : constant Boolean :=
3109 Is_Static_Expression (Left)
3110 and then
3111 Is_Static_Expression (Right);
996ae0b0
RK
3112
3113 begin
3114 -- Short circuit operations are never static in Ada 83
3115
22cb89b5 3116 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
996ae0b0
RK
3117 Check_Non_Static_Context (Left);
3118 Check_Non_Static_Context (Right);
3119 return;
3120 end if;
3121
3122 -- Now look at the operands, we can't quite use the normal call to
3123 -- Test_Expression_Is_Foldable here because short circuit operations
3124 -- are a special case, they can still be foldable, even if the right
3125 -- operand raises constraint error.
3126
22cb89b5
AC
3127 -- If either operand is Any_Type, just propagate to result and do not
3128 -- try to fold, this prevents cascaded errors.
996ae0b0
RK
3129
3130 if Etype (Left) = Any_Type or else Etype (Right) = Any_Type then
3131 Set_Etype (N, Any_Type);
3132 return;
3133
3134 -- If left operand raises constraint error, then replace node N with
3135 -- the raise constraint error node, and we are obviously not foldable.
3136 -- Is_Static_Expression is set from the two operands in the normal way,
3137 -- and we check the right operand if it is in a non-static context.
3138
3139 elsif Raises_Constraint_Error (Left) then
3140 if not Rstat then
3141 Check_Non_Static_Context (Right);
3142 end if;
3143
3144 Rewrite_In_Raise_CE (N, Left);
3145 Set_Is_Static_Expression (N, Rstat);
3146 return;
3147
3148 -- If the result is not static, then we won't in any case fold
3149
3150 elsif not Rstat then
3151 Check_Non_Static_Context (Left);
3152 Check_Non_Static_Context (Right);
3153 return;
3154 end if;
3155
3156 -- Here the result is static, note that, unlike the normal processing
3157 -- in Test_Expression_Is_Foldable, we did *not* check above to see if
3158 -- the right operand raises constraint error, that's because it is not
3159 -- significant if the left operand is decisive.
3160
3161 Set_Is_Static_Expression (N);
3162
3163 -- It does not matter if the right operand raises constraint error if
3164 -- it will not be evaluated. So deal specially with the cases where
3165 -- the right operand is not evaluated. Note that we will fold these
3166 -- cases even if the right operand is non-static, which is fine, but
3167 -- of course in these cases the result is not potentially static.
3168
3169 Left_Int := Expr_Value (Left);
3170
3171 if (Kind = N_And_Then and then Is_False (Left_Int))
514d0fc5 3172 or else
4d777a71 3173 (Kind = N_Or_Else and then Is_True (Left_Int))
996ae0b0 3174 then
fbf5a39b 3175 Fold_Uint (N, Left_Int, Rstat);
996ae0b0
RK
3176 return;
3177 end if;
3178
3179 -- If first operand not decisive, then it does matter if the right
3180 -- operand raises constraint error, since it will be evaluated, so
3181 -- we simply replace the node with the right operand. Note that this
3182 -- properly propagates Is_Static_Expression and Raises_Constraint_Error
3183 -- (both are set to True in Right).
3184
3185 if Raises_Constraint_Error (Right) then
3186 Rewrite_In_Raise_CE (N, Right);
3187 Check_Non_Static_Context (Left);
3188 return;
3189 end if;
3190
3191 -- Otherwise the result depends on the right operand
3192
fbf5a39b 3193 Fold_Uint (N, Expr_Value (Right), Rstat);
996ae0b0 3194 return;
996ae0b0
RK
3195 end Eval_Short_Circuit;
3196
3197 ----------------
3198 -- Eval_Slice --
3199 ----------------
3200
22cb89b5
AC
3201 -- Slices can never be static, so the only processing required is to check
3202 -- for non-static context if an explicit range is given.
996ae0b0
RK
3203
3204 procedure Eval_Slice (N : Node_Id) is
3205 Drange : constant Node_Id := Discrete_Range (N);
996ae0b0
RK
3206 begin
3207 if Nkind (Drange) = N_Range then
3208 Check_Non_Static_Context (Low_Bound (Drange));
3209 Check_Non_Static_Context (High_Bound (Drange));
3210 end if;
cd2fb920 3211
22cb89b5 3212 -- A slice of the form A (subtype), when the subtype is the index of
cd2fb920
ES
3213 -- the type of A, is redundant, the slice can be replaced with A, and
3214 -- this is worth a warning.
3215
3216 if Is_Entity_Name (Prefix (N)) then
3217 declare
3218 E : constant Entity_Id := Entity (Prefix (N));
3219 T : constant Entity_Id := Etype (E);
3220 begin
3221 if Ekind (E) = E_Constant
3222 and then Is_Array_Type (T)
3223 and then Is_Entity_Name (Drange)
3224 then
3225 if Is_Entity_Name (Original_Node (First_Index (T)))
3226 and then Entity (Original_Node (First_Index (T)))
3227 = Entity (Drange)
3228 then
3229 if Warn_On_Redundant_Constructs then
ed2233dc 3230 Error_Msg_N ("redundant slice denotes whole array?", N);
cd2fb920
ES
3231 end if;
3232
22cb89b5 3233 -- The following might be a useful optimization????
cd2fb920
ES
3234
3235 -- Rewrite (N, New_Occurrence_Of (E, Sloc (N)));
3236 end if;
3237 end if;
3238 end;
3239 end if;
996ae0b0
RK
3240 end Eval_Slice;
3241
3242 -------------------------
3243 -- Eval_String_Literal --
3244 -------------------------
3245
3246 procedure Eval_String_Literal (N : Node_Id) is
91b1417d
AC
3247 Typ : constant Entity_Id := Etype (N);
3248 Bas : constant Entity_Id := Base_Type (Typ);
3249 Xtp : Entity_Id;
3250 Len : Nat;
3251 Lo : Node_Id;
996ae0b0
RK
3252
3253 begin
3254 -- Nothing to do if error type (handles cases like default expressions
22cb89b5 3255 -- or generics where we have not yet fully resolved the type).
996ae0b0 3256
91b1417d 3257 if Bas = Any_Type or else Bas = Any_String then
996ae0b0 3258 return;
91b1417d 3259 end if;
996ae0b0
RK
3260
3261 -- String literals are static if the subtype is static (RM 4.9(2)), so
3262 -- reset the static expression flag (it was set unconditionally in
3263 -- Analyze_String_Literal) if the subtype is non-static. We tell if
3264 -- the subtype is static by looking at the lower bound.
3265
91b1417d
AC
3266 if Ekind (Typ) = E_String_Literal_Subtype then
3267 if not Is_OK_Static_Expression (String_Literal_Low_Bound (Typ)) then
3268 Set_Is_Static_Expression (N, False);
3269 return;
3270 end if;
3271
3272 -- Here if Etype of string literal is normal Etype (not yet possible,
22cb89b5 3273 -- but may be possible in future).
91b1417d
AC
3274
3275 elsif not Is_OK_Static_Expression
3276 (Type_Low_Bound (Etype (First_Index (Typ))))
3277 then
996ae0b0 3278 Set_Is_Static_Expression (N, False);
91b1417d
AC
3279 return;
3280 end if;
996ae0b0 3281
91b1417d
AC
3282 -- If original node was a type conversion, then result if non-static
3283
3284 if Nkind (Original_Node (N)) = N_Type_Conversion then
996ae0b0 3285 Set_Is_Static_Expression (N, False);
91b1417d
AC
3286 return;
3287 end if;
996ae0b0 3288
22cb89b5
AC
3289 -- Test for illegal Ada 95 cases. A string literal is illegal in Ada 95
3290 -- if its bounds are outside the index base type and this index type is
3291 -- static. This can happen in only two ways. Either the string literal
3292 -- is too long, or it is null, and the lower bound is type'First. In
3293 -- either case it is the upper bound that is out of range of the index
3294 -- type.
996ae0b0 3295
0ab80019 3296 if Ada_Version >= Ada_95 then
91b1417d
AC
3297 if Root_Type (Bas) = Standard_String
3298 or else
3299 Root_Type (Bas) = Standard_Wide_String
996ae0b0 3300 then
91b1417d 3301 Xtp := Standard_Positive;
996ae0b0 3302 else
91b1417d 3303 Xtp := Etype (First_Index (Bas));
996ae0b0
RK
3304 end if;
3305
91b1417d
AC
3306 if Ekind (Typ) = E_String_Literal_Subtype then
3307 Lo := String_Literal_Low_Bound (Typ);
3308 else
3309 Lo := Type_Low_Bound (Etype (First_Index (Typ)));
3310 end if;
3311
3312 Len := String_Length (Strval (N));
3313
3314 if UI_From_Int (Len) > String_Type_Len (Bas) then
996ae0b0 3315 Apply_Compile_Time_Constraint_Error
07fc65c4 3316 (N, "string literal too long for}", CE_Length_Check_Failed,
91b1417d
AC
3317 Ent => Bas,
3318 Typ => First_Subtype (Bas));
996ae0b0 3319
91b1417d
AC
3320 elsif Len = 0
3321 and then not Is_Generic_Type (Xtp)
3322 and then
3323 Expr_Value (Lo) = Expr_Value (Type_Low_Bound (Base_Type (Xtp)))
996ae0b0
RK
3324 then
3325 Apply_Compile_Time_Constraint_Error
3326 (N, "null string literal not allowed for}",
07fc65c4 3327 CE_Length_Check_Failed,
91b1417d
AC
3328 Ent => Bas,
3329 Typ => First_Subtype (Bas));
996ae0b0
RK
3330 end if;
3331 end if;
996ae0b0
RK
3332 end Eval_String_Literal;
3333
3334 --------------------------
3335 -- Eval_Type_Conversion --
3336 --------------------------
3337
3338 -- A type conversion is potentially static if its subtype mark is for a
3339 -- static scalar subtype, and its operand expression is potentially static
22cb89b5 3340 -- (RM 4.9(10)).
996ae0b0
RK
3341
3342 procedure Eval_Type_Conversion (N : Node_Id) is
3343 Operand : constant Node_Id := Expression (N);
3344 Source_Type : constant Entity_Id := Etype (Operand);
3345 Target_Type : constant Entity_Id := Etype (N);
3346
3347 Stat : Boolean;
3348 Fold : Boolean;
3349
3350 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean;
22cb89b5
AC
3351 -- Returns true if type T is an integer type, or if it is a fixed-point
3352 -- type to be treated as an integer (i.e. the flag Conversion_OK is set
3353 -- on the conversion node).
996ae0b0
RK
3354
3355 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean;
3356 -- Returns true if type T is a floating-point type, or if it is a
3357 -- fixed-point type that is not to be treated as an integer (i.e. the
3358 -- flag Conversion_OK is not set on the conversion node).
3359
fbf5a39b
AC
3360 ------------------------------
3361 -- To_Be_Treated_As_Integer --
3362 ------------------------------
3363
996ae0b0
RK
3364 function To_Be_Treated_As_Integer (T : Entity_Id) return Boolean is
3365 begin
3366 return
3367 Is_Integer_Type (T)
3368 or else (Is_Fixed_Point_Type (T) and then Conversion_OK (N));
3369 end To_Be_Treated_As_Integer;
3370
fbf5a39b
AC
3371 ---------------------------
3372 -- To_Be_Treated_As_Real --
3373 ---------------------------
3374
996ae0b0
RK
3375 function To_Be_Treated_As_Real (T : Entity_Id) return Boolean is
3376 begin
3377 return
3378 Is_Floating_Point_Type (T)
3379 or else (Is_Fixed_Point_Type (T) and then not Conversion_OK (N));
3380 end To_Be_Treated_As_Real;
3381
3382 -- Start of processing for Eval_Type_Conversion
3383
3384 begin
82c80734 3385 -- Cannot fold if target type is non-static or if semantic error
996ae0b0
RK
3386
3387 if not Is_Static_Subtype (Target_Type) then
3388 Check_Non_Static_Context (Operand);
3389 return;
3390
3391 elsif Error_Posted (N) then
3392 return;
3393 end if;
3394
3395 -- If not foldable we are done
3396
3397 Test_Expression_Is_Foldable (N, Operand, Stat, Fold);
3398
3399 if not Fold then
3400 return;
3401
3402 -- Don't try fold if target type has constraint error bounds
3403
3404 elsif not Is_OK_Static_Subtype (Target_Type) then
3405 Set_Raises_Constraint_Error (N);
3406 return;
3407 end if;
3408
3409 -- Remaining processing depends on operand types. Note that in the
3410 -- following type test, fixed-point counts as real unless the flag
3411 -- Conversion_OK is set, in which case it counts as integer.
3412
82c80734 3413 -- Fold conversion, case of string type. The result is not static
996ae0b0
RK
3414
3415 if Is_String_Type (Target_Type) then
b11e8d6f 3416 Fold_Str (N, Strval (Get_String_Val (Operand)), Static => False);
996ae0b0
RK
3417
3418 return;
3419
3420 -- Fold conversion, case of integer target type
3421
3422 elsif To_Be_Treated_As_Integer (Target_Type) then
3423 declare
3424 Result : Uint;
3425
3426 begin
3427 -- Integer to integer conversion
3428
3429 if To_Be_Treated_As_Integer (Source_Type) then
3430 Result := Expr_Value (Operand);
3431
3432 -- Real to integer conversion
3433
3434 else
3435 Result := UR_To_Uint (Expr_Value_R (Operand));
3436 end if;
3437
3438 -- If fixed-point type (Conversion_OK must be set), then the
3439 -- result is logically an integer, but we must replace the
3440 -- conversion with the corresponding real literal, since the
3441 -- type from a semantic point of view is still fixed-point.
3442
3443 if Is_Fixed_Point_Type (Target_Type) then
3444 Fold_Ureal
fbf5a39b 3445 (N, UR_From_Uint (Result) * Small_Value (Target_Type), Stat);
996ae0b0
RK
3446
3447 -- Otherwise result is integer literal
3448
3449 else
fbf5a39b 3450 Fold_Uint (N, Result, Stat);
996ae0b0
RK
3451 end if;
3452 end;
3453
3454 -- Fold conversion, case of real target type
3455
3456 elsif To_Be_Treated_As_Real (Target_Type) then
3457 declare
3458 Result : Ureal;
3459
3460 begin
3461 if To_Be_Treated_As_Real (Source_Type) then
3462 Result := Expr_Value_R (Operand);
3463 else
3464 Result := UR_From_Uint (Expr_Value (Operand));
3465 end if;
3466
fbf5a39b 3467 Fold_Ureal (N, Result, Stat);
996ae0b0
RK
3468 end;
3469
3470 -- Enumeration types
3471
3472 else
fbf5a39b 3473 Fold_Uint (N, Expr_Value (Operand), Stat);
996ae0b0
RK
3474 end if;
3475
c800f862 3476 if Is_Out_Of_Range (N, Etype (N), Assume_Valid => True) then
996ae0b0
RK
3477 Out_Of_Range (N);
3478 end if;
3479
3480 end Eval_Type_Conversion;
3481
3482 -------------------
3483 -- Eval_Unary_Op --
3484 -------------------
3485
3486 -- Predefined unary operators are static functions (RM 4.9(20)) and thus
22cb89b5 3487 -- are potentially static if the operand is potentially static (RM 4.9(7)).
996ae0b0
RK
3488
3489 procedure Eval_Unary_Op (N : Node_Id) is
3490 Right : constant Node_Id := Right_Opnd (N);
d7567964 3491 Otype : Entity_Id := Empty;
996ae0b0
RK
3492 Stat : Boolean;
3493 Fold : Boolean;
3494
3495 begin
3496 -- If not foldable we are done
3497
3498 Test_Expression_Is_Foldable (N, Right, Stat, Fold);
3499
3500 if not Fold then
3501 return;
3502 end if;
3503
602a7ec0 3504 if Etype (Right) = Universal_Integer
ae77c68b
AC
3505 or else
3506 Etype (Right) = Universal_Real
602a7ec0 3507 then
d7567964 3508 Otype := Find_Universal_Operator_Type (N);
602a7ec0
AC
3509 end if;
3510
996ae0b0
RK
3511 -- Fold for integer case
3512
3513 if Is_Integer_Type (Etype (N)) then
3514 declare
3515 Rint : constant Uint := Expr_Value (Right);
3516 Result : Uint;
3517
3518 begin
3519 -- In the case of modular unary plus and abs there is no need
3520 -- to adjust the result of the operation since if the original
3521 -- operand was in bounds the result will be in the bounds of the
3522 -- modular type. However, in the case of modular unary minus the
3523 -- result may go out of the bounds of the modular type and needs
3524 -- adjustment.
3525
3526 if Nkind (N) = N_Op_Plus then
3527 Result := Rint;
3528
3529 elsif Nkind (N) = N_Op_Minus then
3530 if Is_Modular_Integer_Type (Etype (N)) then
3531 Result := (-Rint) mod Modulus (Etype (N));
3532 else
3533 Result := (-Rint);
3534 end if;
3535
3536 else
3537 pragma Assert (Nkind (N) = N_Op_Abs);
3538 Result := abs Rint;
3539 end if;
3540
fbf5a39b 3541 Fold_Uint (N, Result, Stat);
996ae0b0
RK
3542 end;
3543
3544 -- Fold for real case
3545
3546 elsif Is_Real_Type (Etype (N)) then
3547 declare
3548 Rreal : constant Ureal := Expr_Value_R (Right);
3549 Result : Ureal;
3550
3551 begin
3552 if Nkind (N) = N_Op_Plus then
3553 Result := Rreal;
3554
3555 elsif Nkind (N) = N_Op_Minus then
3556 Result := UR_Negate (Rreal);
3557
3558 else
3559 pragma Assert (Nkind (N) = N_Op_Abs);
3560 Result := abs Rreal;
3561 end if;
3562
fbf5a39b 3563 Fold_Ureal (N, Result, Stat);
996ae0b0
RK
3564 end;
3565 end if;
d7567964
TQ
3566
3567 -- If the operator was resolved to a specific type, make sure that type
3568 -- is frozen even if the expression is folded into a literal (which has
3569 -- a universal type).
3570
3571 if Present (Otype) then
3572 Freeze_Before (N, Otype);
3573 end if;
996ae0b0
RK
3574 end Eval_Unary_Op;
3575
3576 -------------------------------
3577 -- Eval_Unchecked_Conversion --
3578 -------------------------------
3579
3580 -- Unchecked conversions can never be static, so the only required
3581 -- processing is to check for a non-static context for the operand.
3582
3583 procedure Eval_Unchecked_Conversion (N : Node_Id) is
3584 begin
3585 Check_Non_Static_Context (Expression (N));
3586 end Eval_Unchecked_Conversion;
3587
3588 --------------------
3589 -- Expr_Rep_Value --
3590 --------------------
3591
3592 function Expr_Rep_Value (N : Node_Id) return Uint is
07fc65c4
GB
3593 Kind : constant Node_Kind := Nkind (N);
3594 Ent : Entity_Id;
996ae0b0
RK
3595
3596 begin
3597 if Is_Entity_Name (N) then
3598 Ent := Entity (N);
3599
22cb89b5
AC
3600 -- An enumeration literal that was either in the source or created
3601 -- as a result of static evaluation.
996ae0b0
RK
3602
3603 if Ekind (Ent) = E_Enumeration_Literal then
3604 return Enumeration_Rep (Ent);
3605
3606 -- A user defined static constant
3607
3608 else
3609 pragma Assert (Ekind (Ent) = E_Constant);
3610 return Expr_Rep_Value (Constant_Value (Ent));
3611 end if;
3612
22cb89b5
AC
3613 -- An integer literal that was either in the source or created as a
3614 -- result of static evaluation.
996ae0b0
RK
3615
3616 elsif Kind = N_Integer_Literal then
3617 return Intval (N);
3618
3619 -- A real literal for a fixed-point type. This must be the fixed-point
3620 -- case, either the literal is of a fixed-point type, or it is a bound
3621 -- of a fixed-point type, with type universal real. In either case we
3622 -- obtain the desired value from Corresponding_Integer_Value.
3623
3624 elsif Kind = N_Real_Literal then
996ae0b0
RK
3625 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
3626 return Corresponding_Integer_Value (N);
3627
07fc65c4
GB
3628 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3629
3630 elsif Kind = N_Attribute_Reference
3631 and then Attribute_Name (N) = Name_Null_Parameter
3632 then
3633 return Uint_0;
3634
07fc65c4 3635 -- Otherwise must be character literal
8cbb664e 3636
996ae0b0
RK
3637 else
3638 pragma Assert (Kind = N_Character_Literal);
3639 Ent := Entity (N);
3640
22cb89b5
AC
3641 -- Since Character literals of type Standard.Character don't have any
3642 -- defining character literals built for them, they do not have their
3643 -- Entity set, so just use their Char code. Otherwise for user-
3644 -- defined character literals use their Pos value as usual which is
3645 -- the same as the Rep value.
996ae0b0
RK
3646
3647 if No (Ent) then
82c80734 3648 return Char_Literal_Value (N);
996ae0b0
RK
3649 else
3650 return Enumeration_Rep (Ent);
3651 end if;
3652 end if;
3653 end Expr_Rep_Value;
3654
3655 ----------------
3656 -- Expr_Value --
3657 ----------------
3658
3659 function Expr_Value (N : Node_Id) return Uint is
07fc65c4
GB
3660 Kind : constant Node_Kind := Nkind (N);
3661 CV_Ent : CV_Entry renames CV_Cache (Nat (N) mod CV_Cache_Size);
3662 Ent : Entity_Id;
3663 Val : Uint;
996ae0b0
RK
3664
3665 begin
13f34a3f
RD
3666 -- If already in cache, then we know it's compile time known and we can
3667 -- return the value that was previously stored in the cache since
3668 -- compile time known values cannot change.
07fc65c4
GB
3669
3670 if CV_Ent.N = N then
3671 return CV_Ent.V;
3672 end if;
3673
3674 -- Otherwise proceed to test value
3675
996ae0b0
RK
3676 if Is_Entity_Name (N) then
3677 Ent := Entity (N);
3678
22cb89b5
AC
3679 -- An enumeration literal that was either in the source or created as
3680 -- a result of static evaluation.
996ae0b0
RK
3681
3682 if Ekind (Ent) = E_Enumeration_Literal then
07fc65c4 3683 Val := Enumeration_Pos (Ent);
996ae0b0
RK
3684
3685 -- A user defined static constant
3686
3687 else
3688 pragma Assert (Ekind (Ent) = E_Constant);
07fc65c4 3689 Val := Expr_Value (Constant_Value (Ent));
996ae0b0
RK
3690 end if;
3691
22cb89b5
AC
3692 -- An integer literal that was either in the source or created as a
3693 -- result of static evaluation.
996ae0b0
RK
3694
3695 elsif Kind = N_Integer_Literal then
07fc65c4 3696 Val := Intval (N);
996ae0b0
RK
3697
3698 -- A real literal for a fixed-point type. This must be the fixed-point
3699 -- case, either the literal is of a fixed-point type, or it is a bound
3700 -- of a fixed-point type, with type universal real. In either case we
3701 -- obtain the desired value from Corresponding_Integer_Value.
3702
3703 elsif Kind = N_Real_Literal then
3704
996ae0b0 3705 pragma Assert (Is_Fixed_Point_Type (Underlying_Type (Etype (N))));
07fc65c4 3706 Val := Corresponding_Integer_Value (N);
996ae0b0
RK
3707
3708 -- Peculiar VMS case, if we have xxx'Null_Parameter, return zero
3709
3710 elsif Kind = N_Attribute_Reference
3711 and then Attribute_Name (N) = Name_Null_Parameter
3712 then
07fc65c4
GB
3713 Val := Uint_0;
3714
996ae0b0
RK
3715 -- Otherwise must be character literal
3716
3717 else
3718 pragma Assert (Kind = N_Character_Literal);
3719 Ent := Entity (N);
3720
3721 -- Since Character literals of type Standard.Character don't
3722 -- have any defining character literals built for them, they
3723 -- do not have their Entity set, so just use their Char
3724 -- code. Otherwise for user-defined character literals use
3725 -- their Pos value as usual.
3726
3727 if No (Ent) then
82c80734 3728 Val := Char_Literal_Value (N);
996ae0b0 3729 else
07fc65c4 3730 Val := Enumeration_Pos (Ent);
996ae0b0
RK
3731 end if;
3732 end if;
3733
07fc65c4
GB
3734 -- Come here with Val set to value to be returned, set cache
3735
3736 CV_Ent.N := N;
3737 CV_Ent.V := Val;
3738 return Val;
996ae0b0
RK
3739 end Expr_Value;
3740
3741 ------------------
3742 -- Expr_Value_E --
3743 ------------------
3744
3745 function Expr_Value_E (N : Node_Id) return Entity_Id is
3746 Ent : constant Entity_Id := Entity (N);
3747
3748 begin
3749 if Ekind (Ent) = E_Enumeration_Literal then
3750 return Ent;
3751 else
3752 pragma Assert (Ekind (Ent) = E_Constant);
3753 return Expr_Value_E (Constant_Value (Ent));
3754 end if;
3755 end Expr_Value_E;
3756
3757 ------------------
3758 -- Expr_Value_R --
3759 ------------------
3760
3761 function Expr_Value_R (N : Node_Id) return Ureal is
3762 Kind : constant Node_Kind := Nkind (N);
3763 Ent : Entity_Id;
3764 Expr : Node_Id;
3765
3766 begin
3767 if Kind = N_Real_Literal then
3768 return Realval (N);
3769
3770 elsif Kind = N_Identifier or else Kind = N_Expanded_Name then
3771 Ent := Entity (N);
3772 pragma Assert (Ekind (Ent) = E_Constant);
3773 return Expr_Value_R (Constant_Value (Ent));
3774
3775 elsif Kind = N_Integer_Literal then
3776 return UR_From_Uint (Expr_Value (N));
3777
3778 -- Strange case of VAX literals, which are at this stage transformed
3779 -- into Vax_Type!x_To_y(IEEE_Literal). See Expand_N_Real_Literal in
3780 -- Exp_Vfpt for further details.
3781
3782 elsif Vax_Float (Etype (N))
3783 and then Nkind (N) = N_Unchecked_Type_Conversion
3784 then
3785 Expr := Expression (N);
3786
3787 if Nkind (Expr) = N_Function_Call
3788 and then Present (Parameter_Associations (Expr))
3789 then
3790 Expr := First (Parameter_Associations (Expr));
3791
3792 if Nkind (Expr) = N_Real_Literal then
3793 return Realval (Expr);
3794 end if;
3795 end if;
3796
3797 -- Peculiar VMS case, if we have xxx'Null_Parameter, return 0.0
3798
3799 elsif Kind = N_Attribute_Reference
3800 and then Attribute_Name (N) = Name_Null_Parameter
3801 then
3802 return Ureal_0;
3803 end if;
3804
22cb89b5
AC
3805 -- If we fall through, we have a node that cannot be interpreted as a
3806 -- compile time constant. That is definitely an error.
996ae0b0
RK
3807
3808 raise Program_Error;
3809 end Expr_Value_R;
3810
3811 ------------------
3812 -- Expr_Value_S --
3813 ------------------
3814
3815 function Expr_Value_S (N : Node_Id) return Node_Id is
3816 begin
3817 if Nkind (N) = N_String_Literal then
3818 return N;
3819 else
3820 pragma Assert (Ekind (Entity (N)) = E_Constant);
3821 return Expr_Value_S (Constant_Value (Entity (N)));
3822 end if;
3823 end Expr_Value_S;
3824
74e7891f
RD
3825 ----------------------------------
3826 -- Find_Universal_Operator_Type --
3827 ----------------------------------
3828
3829 function Find_Universal_Operator_Type (N : Node_Id) return Entity_Id is
3830 PN : constant Node_Id := Parent (N);
3831 Call : constant Node_Id := Original_Node (N);
3832 Is_Int : constant Boolean := Is_Integer_Type (Etype (N));
3833
3834 Is_Fix : constant Boolean :=
3835 Nkind (N) in N_Binary_Op
3836 and then Nkind (Right_Opnd (N)) /= Nkind (Left_Opnd (N));
3837 -- A mixed-mode operation in this context indicates the presence of
3838 -- fixed-point type in the designated package.
3839
3840 Is_Relational : constant Boolean := Etype (N) = Standard_Boolean;
3841 -- Case where N is a relational (or membership) operator (else it is an
3842 -- arithmetic one).
3843
3844 In_Membership : constant Boolean :=
3845 Nkind (PN) in N_Membership_Test
3846 and then
3847 Nkind (Right_Opnd (PN)) = N_Range
3848 and then
3849 Is_Universal_Numeric_Type (Etype (Left_Opnd (PN)))
3850 and then
3851 Is_Universal_Numeric_Type
3852 (Etype (Low_Bound (Right_Opnd (PN))))
3853 and then
3854 Is_Universal_Numeric_Type
3855 (Etype (High_Bound (Right_Opnd (PN))));
3856 -- Case where N is part of a membership test with a universal range
3857
3858 E : Entity_Id;
3859 Pack : Entity_Id;
3860 Typ1 : Entity_Id := Empty;
3861 Priv_E : Entity_Id;
3862
3863 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean;
7ec8363d
RD
3864 -- Check whether one operand is a mixed-mode operation that requires the
3865 -- presence of a fixed-point type. Given that all operands are universal
3866 -- and have been constant-folded, retrieve the original function call.
74e7891f
RD
3867
3868 ---------------------------
3869 -- Is_Mixed_Mode_Operand --
3870 ---------------------------
3871
3872 function Is_Mixed_Mode_Operand (Op : Node_Id) return Boolean is
7ec8363d 3873 Onod : constant Node_Id := Original_Node (Op);
74e7891f 3874 begin
7ec8363d
RD
3875 return Nkind (Onod) = N_Function_Call
3876 and then Present (Next_Actual (First_Actual (Onod)))
3877 and then Etype (First_Actual (Onod)) /=
3878 Etype (Next_Actual (First_Actual (Onod)));
74e7891f
RD
3879 end Is_Mixed_Mode_Operand;
3880
7ec8363d
RD
3881 -- Start of processing for Find_Universal_Operator_Type
3882
74e7891f
RD
3883 begin
3884 if Nkind (Call) /= N_Function_Call
3885 or else Nkind (Name (Call)) /= N_Expanded_Name
3886 then
3887 return Empty;
3888
946db1e2
AC
3889 -- There are several cases where the context does not imply the type of
3890 -- the operands:
3891 -- - the universal expression appears in a type conversion;
3892 -- - the expression is a relational operator applied to universal
3893 -- operands;
3894 -- - the expression is a membership test with a universal operand
3895 -- and a range with universal bounds.
74e7891f
RD
3896
3897 elsif Nkind (Parent (N)) = N_Type_Conversion
7ec8363d
RD
3898 or else Is_Relational
3899 or else In_Membership
74e7891f
RD
3900 then
3901 Pack := Entity (Prefix (Name (Call)));
3902
7ec8363d
RD
3903 -- If the prefix is a package declared elsewhere, iterate over its
3904 -- visible entities, otherwise iterate over all declarations in the
3905 -- designated scope.
74e7891f
RD
3906
3907 if Ekind (Pack) = E_Package
3908 and then not In_Open_Scopes (Pack)
3909 then
3910 Priv_E := First_Private_Entity (Pack);
3911 else
3912 Priv_E := Empty;
3913 end if;
3914
3915 Typ1 := Empty;
3916 E := First_Entity (Pack);
3917 while Present (E) and then E /= Priv_E loop
3918 if Is_Numeric_Type (E)
3919 and then Nkind (Parent (E)) /= N_Subtype_Declaration
3920 and then Comes_From_Source (E)
3921 and then Is_Integer_Type (E) = Is_Int
3922 and then
3923 (Nkind (N) in N_Unary_Op
3924 or else Is_Relational
3925 or else Is_Fixed_Point_Type (E) = Is_Fix)
3926 then
3927 if No (Typ1) then
3928 Typ1 := E;
3929
676e8420
AC
3930 -- Before emitting an error, check for the presence of a
3931 -- mixed-mode operation that specifies a fixed point type.
74e7891f
RD
3932
3933 elsif Is_Relational
3934 and then
3935 (Is_Mixed_Mode_Operand (Left_Opnd (N))
676e8420 3936 or else Is_Mixed_Mode_Operand (Right_Opnd (N)))
74e7891f
RD
3937 and then Is_Fixed_Point_Type (E) /= Is_Fixed_Point_Type (Typ1)
3938
3939 then
3940 if Is_Fixed_Point_Type (E) then
3941 Typ1 := E;
3942 end if;
3943
3944 else
3945 -- More than one type of the proper class declared in P
3946
3947 Error_Msg_N ("ambiguous operation", N);
3948 Error_Msg_Sloc := Sloc (Typ1);
3949 Error_Msg_N ("\possible interpretation (inherited)#", N);
3950 Error_Msg_Sloc := Sloc (E);
3951 Error_Msg_N ("\possible interpretation (inherited)#", N);
3952 return Empty;
3953 end if;
3954 end if;
3955
3956 Next_Entity (E);
3957 end loop;
3958 end if;
3959
3960 return Typ1;
3961 end Find_Universal_Operator_Type;
3962
fbf5a39b
AC
3963 --------------------------
3964 -- Flag_Non_Static_Expr --
3965 --------------------------
3966
3967 procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id) is
3968 begin
3969 if Error_Posted (Expr) and then not All_Errors_Mode then
3970 return;
3971 else
3972 Error_Msg_F (Msg, Expr);
3973 Why_Not_Static (Expr);
3974 end if;
3975 end Flag_Non_Static_Expr;
3976
996ae0b0
RK
3977 --------------
3978 -- Fold_Str --
3979 --------------
3980
fbf5a39b 3981 procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean) is
996ae0b0
RK
3982 Loc : constant Source_Ptr := Sloc (N);
3983 Typ : constant Entity_Id := Etype (N);
3984
3985 begin
3986 Rewrite (N, Make_String_Literal (Loc, Strval => Val));
fbf5a39b
AC
3987
3988 -- We now have the literal with the right value, both the actual type
3989 -- and the expected type of this literal are taken from the expression
3990 -- that was evaluated.
3991
3992 Analyze (N);
3993 Set_Is_Static_Expression (N, Static);
3994 Set_Etype (N, Typ);
3995 Resolve (N);
996ae0b0
RK
3996 end Fold_Str;
3997
3998 ---------------
3999 -- Fold_Uint --
4000 ---------------
4001
fbf5a39b 4002 procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean) is
996ae0b0 4003 Loc : constant Source_Ptr := Sloc (N);
fbf5a39b
AC
4004 Typ : Entity_Id := Etype (N);
4005 Ent : Entity_Id;
996ae0b0
RK
4006
4007 begin
22cb89b5
AC
4008 -- If we are folding a named number, retain the entity in the literal,
4009 -- for ASIS use.
fbf5a39b
AC
4010
4011 if Is_Entity_Name (N)
4012 and then Ekind (Entity (N)) = E_Named_Integer
4013 then
4014 Ent := Entity (N);
4015 else
4016 Ent := Empty;
4017 end if;
4018
4019 if Is_Private_Type (Typ) then
4020 Typ := Full_View (Typ);
4021 end if;
4022
f3d57416 4023 -- For a result of type integer, substitute an N_Integer_Literal node
996ae0b0 4024 -- for the result of the compile time evaluation of the expression.
cd2fb920
ES
4025 -- For ASIS use, set a link to the original named number when not in
4026 -- a generic context.
996ae0b0 4027
fbf5a39b 4028 if Is_Integer_Type (Typ) then
996ae0b0 4029 Rewrite (N, Make_Integer_Literal (Loc, Val));
cd2fb920 4030
fbf5a39b 4031 Set_Original_Entity (N, Ent);
996ae0b0
RK
4032
4033 -- Otherwise we have an enumeration type, and we substitute either
4034 -- an N_Identifier or N_Character_Literal to represent the enumeration
4035 -- literal corresponding to the given value, which must always be in
4036 -- range, because appropriate tests have already been made for this.
4037
fbf5a39b 4038 else pragma Assert (Is_Enumeration_Type (Typ));
996ae0b0
RK
4039 Rewrite (N, Get_Enum_Lit_From_Pos (Etype (N), Val, Loc));
4040 end if;
4041
4042 -- We now have the literal with the right value, both the actual type
4043 -- and the expected type of this literal are taken from the expression
4044 -- that was evaluated.
4045
4046 Analyze (N);
fbf5a39b 4047 Set_Is_Static_Expression (N, Static);
996ae0b0 4048 Set_Etype (N, Typ);
fbf5a39b 4049 Resolve (N);
996ae0b0
RK
4050 end Fold_Uint;
4051
4052 ----------------
4053 -- Fold_Ureal --
4054 ----------------
4055
fbf5a39b 4056 procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean) is
996ae0b0
RK
4057 Loc : constant Source_Ptr := Sloc (N);
4058 Typ : constant Entity_Id := Etype (N);
fbf5a39b 4059 Ent : Entity_Id;
996ae0b0
RK
4060
4061 begin
22cb89b5
AC
4062 -- If we are folding a named number, retain the entity in the literal,
4063 -- for ASIS use.
fbf5a39b
AC
4064
4065 if Is_Entity_Name (N)
4066 and then Ekind (Entity (N)) = E_Named_Real
4067 then
4068 Ent := Entity (N);
4069 else
4070 Ent := Empty;
4071 end if;
4072
996ae0b0 4073 Rewrite (N, Make_Real_Literal (Loc, Realval => Val));
cd2fb920 4074
5a30024a 4075 -- Set link to original named number, for ASIS use
cd2fb920 4076
fbf5a39b 4077 Set_Original_Entity (N, Ent);
996ae0b0
RK
4078
4079 -- Both the actual and expected type comes from the original expression
4080
fbf5a39b
AC
4081 Analyze (N);
4082 Set_Is_Static_Expression (N, Static);
996ae0b0 4083 Set_Etype (N, Typ);
fbf5a39b 4084 Resolve (N);
996ae0b0
RK
4085 end Fold_Ureal;
4086
4087 ---------------
4088 -- From_Bits --
4089 ---------------
4090
4091 function From_Bits (B : Bits; T : Entity_Id) return Uint is
4092 V : Uint := Uint_0;
4093
4094 begin
4095 for J in 0 .. B'Last loop
4096 if B (J) then
4097 V := V + 2 ** J;
4098 end if;
4099 end loop;
4100
4101 if Non_Binary_Modulus (T) then
4102 V := V mod Modulus (T);
4103 end if;
4104
4105 return V;
4106 end From_Bits;
4107
4108 --------------------
4109 -- Get_String_Val --
4110 --------------------
4111
4112 function Get_String_Val (N : Node_Id) return Node_Id is
4113 begin
4114 if Nkind (N) = N_String_Literal then
4115 return N;
4116
4117 elsif Nkind (N) = N_Character_Literal then
4118 return N;
4119
4120 else
4121 pragma Assert (Is_Entity_Name (N));
4122 return Get_String_Val (Constant_Value (Entity (N)));
4123 end if;
4124 end Get_String_Val;
4125
fbf5a39b
AC
4126 ----------------
4127 -- Initialize --
4128 ----------------
4129
4130 procedure Initialize is
4131 begin
4132 CV_Cache := (others => (Node_High_Bound, Uint_0));
4133 end Initialize;
4134
996ae0b0
RK
4135 --------------------
4136 -- In_Subrange_Of --
4137 --------------------
4138
4139 function In_Subrange_Of
c27f2f15
RD
4140 (T1 : Entity_Id;
4141 T2 : Entity_Id;
4142 Fixed_Int : Boolean := False) return Boolean
996ae0b0
RK
4143 is
4144 L1 : Node_Id;
4145 H1 : Node_Id;
4146
4147 L2 : Node_Id;
4148 H2 : Node_Id;
4149
4150 begin
4151 if T1 = T2 or else Is_Subtype_Of (T1, T2) then
4152 return True;
4153
4154 -- Never in range if both types are not scalar. Don't know if this can
4155 -- actually happen, but just in case.
4156
4157 elsif not Is_Scalar_Type (T1) or else not Is_Scalar_Type (T1) then
4158 return False;
4159
d79e621a
GD
4160 -- If T1 has infinities but T2 doesn't have infinities, then T1 is
4161 -- definitely not compatible with T2.
4162
4163 elsif Is_Floating_Point_Type (T1)
4164 and then Has_Infinities (T1)
4165 and then Is_Floating_Point_Type (T2)
4166 and then not Has_Infinities (T2)
4167 then
4168 return False;
4169
996ae0b0
RK
4170 else
4171 L1 := Type_Low_Bound (T1);
4172 H1 := Type_High_Bound (T1);
4173
4174 L2 := Type_Low_Bound (T2);
4175 H2 := Type_High_Bound (T2);
4176
4177 -- Check bounds to see if comparison possible at compile time
4178
c27f2f15 4179 if Compile_Time_Compare (L1, L2, Assume_Valid => True) in Compare_GE
996ae0b0 4180 and then
c27f2f15 4181 Compile_Time_Compare (H1, H2, Assume_Valid => True) in Compare_LE
996ae0b0
RK
4182 then
4183 return True;
4184 end if;
4185
4186 -- If bounds not comparable at compile time, then the bounds of T2
4187 -- must be compile time known or we cannot answer the query.
4188
4189 if not Compile_Time_Known_Value (L2)
4190 or else not Compile_Time_Known_Value (H2)
4191 then
4192 return False;
4193 end if;
4194
4195 -- If the bounds of T1 are know at compile time then use these
4196 -- ones, otherwise use the bounds of the base type (which are of
4197 -- course always static).
4198
4199 if not Compile_Time_Known_Value (L1) then
4200 L1 := Type_Low_Bound (Base_Type (T1));
4201 end if;
4202
4203 if not Compile_Time_Known_Value (H1) then
4204 H1 := Type_High_Bound (Base_Type (T1));
4205 end if;
4206
4207 -- Fixed point types should be considered as such only if
4208 -- flag Fixed_Int is set to False.
4209
4210 if Is_Floating_Point_Type (T1) or else Is_Floating_Point_Type (T2)
4211 or else (Is_Fixed_Point_Type (T1) and then not Fixed_Int)
4212 or else (Is_Fixed_Point_Type (T2) and then not Fixed_Int)
4213 then
4214 return
4215 Expr_Value_R (L2) <= Expr_Value_R (L1)
4216 and then
4217 Expr_Value_R (H2) >= Expr_Value_R (H1);
4218
4219 else
4220 return
4221 Expr_Value (L2) <= Expr_Value (L1)
4222 and then
4223 Expr_Value (H2) >= Expr_Value (H1);
4224
4225 end if;
4226 end if;
4227
4228 -- If any exception occurs, it means that we have some bug in the compiler
f3d57416 4229 -- possibly triggered by a previous error, or by some unforeseen peculiar
996ae0b0
RK
4230 -- occurrence. However, this is only an optimization attempt, so there is
4231 -- really no point in crashing the compiler. Instead we just decide, too
4232 -- bad, we can't figure out the answer in this case after all.
4233
4234 exception
4235 when others =>
4236
4237 -- Debug flag K disables this behavior (useful for debugging)
4238
4239 if Debug_Flag_K then
4240 raise;
4241 else
4242 return False;
4243 end if;
4244 end In_Subrange_Of;
4245
4246 -----------------
4247 -- Is_In_Range --
4248 -----------------
4249
4250 function Is_In_Range
c800f862
RD
4251 (N : Node_Id;
4252 Typ : Entity_Id;
4253 Assume_Valid : Boolean := False;
4254 Fixed_Int : Boolean := False;
4255 Int_Real : Boolean := False) return Boolean
996ae0b0 4256 is
996ae0b0 4257 begin
305caf42
AC
4258 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4259 = In_Range;
996ae0b0
RK
4260 end Is_In_Range;
4261
4262 -------------------
4263 -- Is_Null_Range --
4264 -------------------
4265
4266 function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4267 Typ : constant Entity_Id := Etype (Lo);
4268
4269 begin
4270 if not Compile_Time_Known_Value (Lo)
4271 or else not Compile_Time_Known_Value (Hi)
4272 then
4273 return False;
4274 end if;
4275
4276 if Is_Discrete_Type (Typ) then
4277 return Expr_Value (Lo) > Expr_Value (Hi);
4278
4279 else
4280 pragma Assert (Is_Real_Type (Typ));
4281 return Expr_Value_R (Lo) > Expr_Value_R (Hi);
4282 end if;
4283 end Is_Null_Range;
4284
4285 -----------------------------
4286 -- Is_OK_Static_Expression --
4287 -----------------------------
4288
4289 function Is_OK_Static_Expression (N : Node_Id) return Boolean is
4290 begin
4291 return Is_Static_Expression (N)
4292 and then not Raises_Constraint_Error (N);
4293 end Is_OK_Static_Expression;
4294
4295 ------------------------
4296 -- Is_OK_Static_Range --
4297 ------------------------
4298
4299 -- A static range is a range whose bounds are static expressions, or a
4300 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4301 -- We have already converted range attribute references, so we get the
4302 -- "or" part of this rule without needing a special test.
4303
4304 function Is_OK_Static_Range (N : Node_Id) return Boolean is
4305 begin
4306 return Is_OK_Static_Expression (Low_Bound (N))
4307 and then Is_OK_Static_Expression (High_Bound (N));
4308 end Is_OK_Static_Range;
4309
4310 --------------------------
4311 -- Is_OK_Static_Subtype --
4312 --------------------------
4313
22cb89b5
AC
4314 -- Determines if Typ is a static subtype as defined in (RM 4.9(26)) where
4315 -- neither bound raises constraint error when evaluated.
996ae0b0
RK
4316
4317 function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean is
4318 Base_T : constant Entity_Id := Base_Type (Typ);
4319 Anc_Subt : Entity_Id;
4320
4321 begin
4322 -- First a quick check on the non static subtype flag. As described
4323 -- in further detail in Einfo, this flag is not decisive in all cases,
4324 -- but if it is set, then the subtype is definitely non-static.
4325
4326 if Is_Non_Static_Subtype (Typ) then
4327 return False;
4328 end if;
4329
4330 Anc_Subt := Ancestor_Subtype (Typ);
4331
4332 if Anc_Subt = Empty then
4333 Anc_Subt := Base_T;
4334 end if;
4335
4336 if Is_Generic_Type (Root_Type (Base_T))
4337 or else Is_Generic_Actual_Type (Base_T)
4338 then
4339 return False;
4340
4341 -- String types
4342
4343 elsif Is_String_Type (Typ) then
4344 return
4345 Ekind (Typ) = E_String_Literal_Subtype
4346 or else
011f9d5d
AC
4347 (Is_OK_Static_Subtype (Component_Type (Typ))
4348 and then Is_OK_Static_Subtype (Etype (First_Index (Typ))));
996ae0b0
RK
4349
4350 -- Scalar types
4351
4352 elsif Is_Scalar_Type (Typ) then
4353 if Base_T = Typ then
4354 return True;
4355
4356 else
22cb89b5
AC
4357 -- Scalar_Range (Typ) might be an N_Subtype_Indication, so use
4358 -- Get_Type_{Low,High}_Bound.
996ae0b0
RK
4359
4360 return Is_OK_Static_Subtype (Anc_Subt)
4361 and then Is_OK_Static_Expression (Type_Low_Bound (Typ))
4362 and then Is_OK_Static_Expression (Type_High_Bound (Typ));
4363 end if;
4364
4365 -- Types other than string and scalar types are never static
4366
4367 else
4368 return False;
4369 end if;
4370 end Is_OK_Static_Subtype;
4371
4372 ---------------------
4373 -- Is_Out_Of_Range --
4374 ---------------------
4375
4376 function Is_Out_Of_Range
1c7717c3
AC
4377 (N : Node_Id;
4378 Typ : Entity_Id;
c800f862 4379 Assume_Valid : Boolean := False;
1c7717c3
AC
4380 Fixed_Int : Boolean := False;
4381 Int_Real : Boolean := False) return Boolean
996ae0b0 4382 is
996ae0b0 4383 begin
305caf42
AC
4384 return Test_In_Range (N, Typ, Assume_Valid, Fixed_Int, Int_Real)
4385 = Out_Of_Range;
996ae0b0
RK
4386 end Is_Out_Of_Range;
4387
4388 ---------------------
4389 -- Is_Static_Range --
4390 ---------------------
4391
4392 -- A static range is a range whose bounds are static expressions, or a
4393 -- Range_Attribute_Reference equivalent to such a range (RM 4.9(26)).
4394 -- We have already converted range attribute references, so we get the
4395 -- "or" part of this rule without needing a special test.
4396
4397 function Is_Static_Range (N : Node_Id) return Boolean is
4398 begin
4399 return Is_Static_Expression (Low_Bound (N))
4400 and then Is_Static_Expression (High_Bound (N));
4401 end Is_Static_Range;
4402
4403 -----------------------
4404 -- Is_Static_Subtype --
4405 -----------------------
4406
82c80734 4407 -- Determines if Typ is a static subtype as defined in (RM 4.9(26))
996ae0b0
RK
4408
4409 function Is_Static_Subtype (Typ : Entity_Id) return Boolean is
4410 Base_T : constant Entity_Id := Base_Type (Typ);
4411 Anc_Subt : Entity_Id;
4412
4413 begin
4414 -- First a quick check on the non static subtype flag. As described
4415 -- in further detail in Einfo, this flag is not decisive in all cases,
4416 -- but if it is set, then the subtype is definitely non-static.
4417
4418 if Is_Non_Static_Subtype (Typ) then
4419 return False;
4420 end if;
4421
4422 Anc_Subt := Ancestor_Subtype (Typ);
4423
4424 if Anc_Subt = Empty then
4425 Anc_Subt := Base_T;
4426 end if;
4427
4428 if Is_Generic_Type (Root_Type (Base_T))
4429 or else Is_Generic_Actual_Type (Base_T)
4430 then
4431 return False;
4432
4433 -- String types
4434
4435 elsif Is_String_Type (Typ) then
4436 return
4437 Ekind (Typ) = E_String_Literal_Subtype
011f9d5d
AC
4438 or else (Is_Static_Subtype (Component_Type (Typ))
4439 and then Is_Static_Subtype (Etype (First_Index (Typ))));
996ae0b0
RK
4440
4441 -- Scalar types
4442
4443 elsif Is_Scalar_Type (Typ) then
4444 if Base_T = Typ then
4445 return True;
4446
4447 else
4448 return Is_Static_Subtype (Anc_Subt)
4449 and then Is_Static_Expression (Type_Low_Bound (Typ))
4450 and then Is_Static_Expression (Type_High_Bound (Typ));
4451 end if;
4452
4453 -- Types other than string and scalar types are never static
4454
4455 else
4456 return False;
4457 end if;
4458 end Is_Static_Subtype;
4459
4460 --------------------
4461 -- Not_Null_Range --
4462 --------------------
4463
4464 function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean is
4465 Typ : constant Entity_Id := Etype (Lo);
4466
4467 begin
4468 if not Compile_Time_Known_Value (Lo)
4469 or else not Compile_Time_Known_Value (Hi)
4470 then
4471 return False;
4472 end if;
4473
4474 if Is_Discrete_Type (Typ) then
4475 return Expr_Value (Lo) <= Expr_Value (Hi);
4476
4477 else
4478 pragma Assert (Is_Real_Type (Typ));
4479
4480 return Expr_Value_R (Lo) <= Expr_Value_R (Hi);
4481 end if;
4482 end Not_Null_Range;
4483
4484 -------------
4485 -- OK_Bits --
4486 -------------
4487
4488 function OK_Bits (N : Node_Id; Bits : Uint) return Boolean is
4489 begin
4490 -- We allow a maximum of 500,000 bits which seems a reasonable limit
4491
4492 if Bits < 500_000 then
4493 return True;
4494
4495 else
4496 Error_Msg_N ("static value too large, capacity exceeded", N);
4497 return False;
4498 end if;
4499 end OK_Bits;
4500
4501 ------------------
4502 -- Out_Of_Range --
4503 ------------------
4504
4505 procedure Out_Of_Range (N : Node_Id) is
4506 begin
4507 -- If we have the static expression case, then this is an illegality
4508 -- in Ada 95 mode, except that in an instance, we never generate an
22cb89b5
AC
4509 -- error (if the error is legitimate, it was already diagnosed in the
4510 -- template). The expression to compute the length of a packed array is
4511 -- attached to the array type itself, and deserves a separate message.
996ae0b0
RK
4512
4513 if Is_Static_Expression (N)
4514 and then not In_Instance
fbf5a39b 4515 and then not In_Inlined_Body
0ab80019 4516 and then Ada_Version >= Ada_95
996ae0b0 4517 then
996ae0b0
RK
4518 if Nkind (Parent (N)) = N_Defining_Identifier
4519 and then Is_Array_Type (Parent (N))
4520 and then Present (Packed_Array_Type (Parent (N)))
4521 and then Present (First_Rep_Item (Parent (N)))
4522 then
4523 Error_Msg_N
4524 ("length of packed array must not exceed Integer''Last",
4525 First_Rep_Item (Parent (N)));
4526 Rewrite (N, Make_Integer_Literal (Sloc (N), Uint_1));
4527
4528 else
4529 Apply_Compile_Time_Constraint_Error
07fc65c4 4530 (N, "value not in range of}", CE_Range_Check_Failed);
996ae0b0
RK
4531 end if;
4532
22cb89b5
AC
4533 -- Here we generate a warning for the Ada 83 case, or when we are in an
4534 -- instance, or when we have a non-static expression case.
996ae0b0
RK
4535
4536 else
996ae0b0 4537 Apply_Compile_Time_Constraint_Error
07fc65c4 4538 (N, "value not in range of}?", CE_Range_Check_Failed);
996ae0b0
RK
4539 end if;
4540 end Out_Of_Range;
4541
4542 -------------------------
4543 -- Rewrite_In_Raise_CE --
4544 -------------------------
4545
4546 procedure Rewrite_In_Raise_CE (N : Node_Id; Exp : Node_Id) is
4547 Typ : constant Entity_Id := Etype (N);
4548
4549 begin
22cb89b5
AC
4550 -- If we want to raise CE in the condition of a N_Raise_CE node
4551 -- we may as well get rid of the condition.
996ae0b0
RK
4552
4553 if Present (Parent (N))
4554 and then Nkind (Parent (N)) = N_Raise_Constraint_Error
4555 then
4556 Set_Condition (Parent (N), Empty);
4557
22cb89b5
AC
4558 -- If the expression raising CE is a N_Raise_CE node, we can use that
4559 -- one. We just preserve the type of the context.
996ae0b0
RK
4560
4561 elsif Nkind (Exp) = N_Raise_Constraint_Error then
4562 Rewrite (N, Exp);
4563 Set_Etype (N, Typ);
4564
22cb89b5 4565 -- Else build an explcit N_Raise_CE
996ae0b0
RK
4566
4567 else
07fc65c4
GB
4568 Rewrite (N,
4569 Make_Raise_Constraint_Error (Sloc (Exp),
4570 Reason => CE_Range_Check_Failed));
996ae0b0
RK
4571 Set_Raises_Constraint_Error (N);
4572 Set_Etype (N, Typ);
4573 end if;
4574 end Rewrite_In_Raise_CE;
4575
4576 ---------------------
4577 -- String_Type_Len --
4578 ---------------------
4579
4580 function String_Type_Len (Stype : Entity_Id) return Uint is
4581 NT : constant Entity_Id := Etype (First_Index (Stype));
4582 T : Entity_Id;
4583
4584 begin
4585 if Is_OK_Static_Subtype (NT) then
4586 T := NT;
4587 else
4588 T := Base_Type (NT);
4589 end if;
4590
4591 return Expr_Value (Type_High_Bound (T)) -
4592 Expr_Value (Type_Low_Bound (T)) + 1;
4593 end String_Type_Len;
4594
4595 ------------------------------------
4596 -- Subtypes_Statically_Compatible --
4597 ------------------------------------
4598
4599 function Subtypes_Statically_Compatible
f44fe430
RD
4600 (T1 : Entity_Id;
4601 T2 : Entity_Id) return Boolean
996ae0b0
RK
4602 is
4603 begin
437f8c1e
AC
4604 -- Scalar types
4605
996ae0b0
RK
4606 if Is_Scalar_Type (T1) then
4607
4608 -- Definitely compatible if we match
4609
4610 if Subtypes_Statically_Match (T1, T2) then
4611 return True;
4612
4613 -- If either subtype is nonstatic then they're not compatible
4614
4615 elsif not Is_Static_Subtype (T1)
4616 or else not Is_Static_Subtype (T2)
4617 then
4618 return False;
4619
4620 -- If either type has constraint error bounds, then consider that
4621 -- they match to avoid junk cascaded errors here.
4622
4623 elsif not Is_OK_Static_Subtype (T1)
4624 or else not Is_OK_Static_Subtype (T2)
4625 then
4626 return True;
4627
26df19ce
AC
4628 -- Base types must match, but we don't check that (should we???) but
4629 -- we do at least check that both types are real, or both types are
4630 -- not real.
996ae0b0 4631
fbf5a39b 4632 elsif Is_Real_Type (T1) /= Is_Real_Type (T2) then
996ae0b0
RK
4633 return False;
4634
4635 -- Here we check the bounds
4636
4637 else
4638 declare
4639 LB1 : constant Node_Id := Type_Low_Bound (T1);
4640 HB1 : constant Node_Id := Type_High_Bound (T1);
4641 LB2 : constant Node_Id := Type_Low_Bound (T2);
4642 HB2 : constant Node_Id := Type_High_Bound (T2);
4643
4644 begin
4645 if Is_Real_Type (T1) then
4646 return
4647 (Expr_Value_R (LB1) > Expr_Value_R (HB1))
4648 or else
4649 (Expr_Value_R (LB2) <= Expr_Value_R (LB1)
4650 and then
4651 Expr_Value_R (HB1) <= Expr_Value_R (HB2));
4652
4653 else
4654 return
4655 (Expr_Value (LB1) > Expr_Value (HB1))
4656 or else
4657 (Expr_Value (LB2) <= Expr_Value (LB1)
4658 and then
4659 Expr_Value (HB1) <= Expr_Value (HB2));
4660 end if;
4661 end;
4662 end if;
4663
437f8c1e
AC
4664 -- Access types
4665
996ae0b0 4666 elsif Is_Access_Type (T1) then
26df19ce
AC
4667 return (not Is_Constrained (T2)
4668 or else (Subtypes_Statically_Match
4669 (Designated_Type (T1), Designated_Type (T2))))
4670 and then not (Can_Never_Be_Null (T2)
4671 and then not Can_Never_Be_Null (T1));
437f8c1e
AC
4672
4673 -- All other cases
996ae0b0
RK
4674
4675 else
4676 return (Is_Composite_Type (T1) and then not Is_Constrained (T2))
4677 or else Subtypes_Statically_Match (T1, T2);
4678 end if;
4679 end Subtypes_Statically_Compatible;
4680
4681 -------------------------------
4682 -- Subtypes_Statically_Match --
4683 -------------------------------
4684
4685 -- Subtypes statically match if they have statically matching constraints
4686 -- (RM 4.9.1(2)). Constraints statically match if there are none, or if
4687 -- they are the same identical constraint, or if they are static and the
4688 -- values match (RM 4.9.1(1)).
4689
4690 function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean is
bb9c600b
AC
4691
4692 function Predicates_Match return Boolean;
4693 -- In Ada 2012, subtypes statically match if their static predicates
4694 -- match as well.
4695
6eaa926a
RD
4696 ----------------------
4697 -- Predicates_Match --
4698 ----------------------
4699
bb9c600b
AC
4700 function Predicates_Match return Boolean is
4701 Pred1 : Node_Id;
4702 Pred2 : Node_Id;
4703
4704 begin
4705 if Ada_Version < Ada_2012 then
4706 return True;
4707
4708 elsif Has_Predicates (T1) /= Has_Predicates (T2) then
4709 return False;
4710
4711 else
34f3a701
VP
4712 Pred1 :=
4713 Get_Rep_Item
4714 (T1, Name_Static_Predicate, Check_Parents => False);
4715 Pred2 :=
4716 Get_Rep_Item
4717 (T2, Name_Static_Predicate, Check_Parents => False);
bb9c600b
AC
4718
4719 -- Subtypes statically match if the predicate comes from the
4720 -- same declaration, which can only happen if one is a subtype
4721 -- of the other and has no explicit predicate.
4722
4723 -- Suppress warnings on order of actuals, which is otherwise
4724 -- triggered by one of the two calls below.
4725
4726 pragma Warnings (Off);
4727 return Pred1 = Pred2
4728 or else (No (Pred1) and then Is_Subtype_Of (T1, T2))
4729 or else (No (Pred2) and then Is_Subtype_Of (T2, T1));
4730 pragma Warnings (On);
4731 end if;
4732 end Predicates_Match;
4733
6eaa926a
RD
4734 -- Start of processing for Subtypes_Statically_Match
4735
996ae0b0
RK
4736 begin
4737 -- A type always statically matches itself
4738
4739 if T1 = T2 then
4740 return True;
4741
4742 -- Scalar types
4743
4744 elsif Is_Scalar_Type (T1) then
4745
4746 -- Base types must be the same
4747
4748 if Base_Type (T1) /= Base_Type (T2) then
4749 return False;
4750 end if;
4751
4752 -- A constrained numeric subtype never matches an unconstrained
4753 -- subtype, i.e. both types must be constrained or unconstrained.
4754
305caf42
AC
4755 -- To understand the requirement for this test, see RM 4.9.1(1).
4756 -- As is made clear in RM 3.5.4(11), type Integer, for example is
4757 -- a constrained subtype with constraint bounds matching the bounds
4758 -- of its corresponding unconstrained base type. In this situation,
4759 -- Integer and Integer'Base do not statically match, even though
4760 -- they have the same bounds.
996ae0b0 4761
22cb89b5
AC
4762 -- We only apply this test to types in Standard and types that appear
4763 -- in user programs. That way, we do not have to be too careful about
4764 -- setting Is_Constrained right for Itypes.
996ae0b0
RK
4765
4766 if Is_Numeric_Type (T1)
4767 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4768 and then (Scope (T1) = Standard_Standard
4769 or else Comes_From_Source (T1))
4770 and then (Scope (T2) = Standard_Standard
4771 or else Comes_From_Source (T2))
4772 then
4773 return False;
82c80734 4774
22cb89b5
AC
4775 -- A generic scalar type does not statically match its base type
4776 -- (AI-311). In this case we make sure that the formals, which are
4777 -- first subtypes of their bases, are constrained.
82c80734
RD
4778
4779 elsif Is_Generic_Type (T1)
4780 and then Is_Generic_Type (T2)
4781 and then (Is_Constrained (T1) /= Is_Constrained (T2))
4782 then
4783 return False;
996ae0b0
RK
4784 end if;
4785
22cb89b5
AC
4786 -- If there was an error in either range, then just assume the types
4787 -- statically match to avoid further junk errors.
996ae0b0 4788
199c6a10
AC
4789 if No (Scalar_Range (T1)) or else No (Scalar_Range (T2))
4790 or else Error_Posted (Scalar_Range (T1))
4791 or else Error_Posted (Scalar_Range (T2))
996ae0b0
RK
4792 then
4793 return True;
4794 end if;
4795
4796 -- Otherwise both types have bound that can be compared
4797
4798 declare
4799 LB1 : constant Node_Id := Type_Low_Bound (T1);
4800 HB1 : constant Node_Id := Type_High_Bound (T1);
4801 LB2 : constant Node_Id := Type_Low_Bound (T2);
4802 HB2 : constant Node_Id := Type_High_Bound (T2);
4803
4804 begin
6eaa926a
RD
4805 -- If the bounds are the same tree node, then match if and only
4806 -- if any predicates present also match.
996ae0b0
RK
4807
4808 if LB1 = LB2 and then HB1 = HB2 then
bb9c600b 4809 return Predicates_Match;
996ae0b0
RK
4810
4811 -- Otherwise bounds must be static and identical value
4812
4813 else
4814 if not Is_Static_Subtype (T1)
4815 or else not Is_Static_Subtype (T2)
4816 then
4817 return False;
4818
22cb89b5
AC
4819 -- If either type has constraint error bounds, then say that
4820 -- they match to avoid junk cascaded errors here.
996ae0b0
RK
4821
4822 elsif not Is_OK_Static_Subtype (T1)
4823 or else not Is_OK_Static_Subtype (T2)
4824 then
4825 return True;
4826
4827 elsif Is_Real_Type (T1) then
4828 return
4829 (Expr_Value_R (LB1) = Expr_Value_R (LB2))
4830 and then
4831 (Expr_Value_R (HB1) = Expr_Value_R (HB2));
4832
4833 else
4834 return
4835 Expr_Value (LB1) = Expr_Value (LB2)
4836 and then
4837 Expr_Value (HB1) = Expr_Value (HB2);
4838 end if;
4839 end if;
4840 end;
4841
4842 -- Type with discriminants
4843
4844 elsif Has_Discriminants (T1) or else Has_Discriminants (T2) then
6eaf4095 4845
c2bf339e
GD
4846 -- Because of view exchanges in multiple instantiations, conformance
4847 -- checking might try to match a partial view of a type with no
4848 -- discriminants with a full view that has defaulted discriminants.
4849 -- In such a case, use the discriminant constraint of the full view,
4850 -- which must exist because we know that the two subtypes have the
4851 -- same base type.
6eaf4095 4852
996ae0b0 4853 if Has_Discriminants (T1) /= Has_Discriminants (T2) then
c2bf339e
GD
4854 if In_Instance then
4855 if Is_Private_Type (T2)
4856 and then Present (Full_View (T2))
4857 and then Has_Discriminants (Full_View (T2))
4858 then
4859 return Subtypes_Statically_Match (T1, Full_View (T2));
4860
4861 elsif Is_Private_Type (T1)
4862 and then Present (Full_View (T1))
4863 and then Has_Discriminants (Full_View (T1))
4864 then
4865 return Subtypes_Statically_Match (Full_View (T1), T2);
4866
4867 else
4868 return False;
4869 end if;
6eaf4095
ES
4870 else
4871 return False;
4872 end if;
996ae0b0
RK
4873 end if;
4874
4875 declare
4876 DL1 : constant Elist_Id := Discriminant_Constraint (T1);
4877 DL2 : constant Elist_Id := Discriminant_Constraint (T2);
4878
13f34a3f
RD
4879 DA1 : Elmt_Id;
4880 DA2 : Elmt_Id;
996ae0b0
RK
4881
4882 begin
4883 if DL1 = DL2 then
4884 return True;
996ae0b0
RK
4885 elsif Is_Constrained (T1) /= Is_Constrained (T2) then
4886 return False;
4887 end if;
4888
13f34a3f 4889 -- Now loop through the discriminant constraints
996ae0b0 4890
13f34a3f
RD
4891 -- Note: the guard here seems necessary, since it is possible at
4892 -- least for DL1 to be No_Elist. Not clear this is reasonable ???
996ae0b0 4893
13f34a3f
RD
4894 if Present (DL1) and then Present (DL2) then
4895 DA1 := First_Elmt (DL1);
4896 DA2 := First_Elmt (DL2);
4897 while Present (DA1) loop
4898 declare
4899 Expr1 : constant Node_Id := Node (DA1);
4900 Expr2 : constant Node_Id := Node (DA2);
996ae0b0 4901
13f34a3f
RD
4902 begin
4903 if not Is_Static_Expression (Expr1)
4904 or else not Is_Static_Expression (Expr2)
4905 then
4906 return False;
996ae0b0 4907
13f34a3f
RD
4908 -- If either expression raised a constraint error,
4909 -- consider the expressions as matching, since this
4910 -- helps to prevent cascading errors.
4911
4912 elsif Raises_Constraint_Error (Expr1)
4913 or else Raises_Constraint_Error (Expr2)
4914 then
4915 null;
4916
4917 elsif Expr_Value (Expr1) /= Expr_Value (Expr2) then
4918 return False;
4919 end if;
4920 end;
996ae0b0 4921
13f34a3f
RD
4922 Next_Elmt (DA1);
4923 Next_Elmt (DA2);
4924 end loop;
4925 end if;
996ae0b0
RK
4926 end;
4927
4928 return True;
4929
22cb89b5 4930 -- A definite type does not match an indefinite or classwide type.
0356699b
RD
4931 -- However, a generic type with unknown discriminants may be
4932 -- instantiated with a type with no discriminants, and conformance
22cb89b5
AC
4933 -- checking on an inherited operation may compare the actual with the
4934 -- subtype that renames it in the instance.
996ae0b0
RK
4935
4936 elsif
4937 Has_Unknown_Discriminants (T1) /= Has_Unknown_Discriminants (T2)
4938 then
7a3f77d2
AC
4939 return
4940 Is_Generic_Actual_Type (T1) or else Is_Generic_Actual_Type (T2);
996ae0b0
RK
4941
4942 -- Array type
4943
4944 elsif Is_Array_Type (T1) then
4945
22cb89b5 4946 -- If either subtype is unconstrained then both must be, and if both
308e6f3a 4947 -- are unconstrained then no further checking is needed.
996ae0b0
RK
4948
4949 if not Is_Constrained (T1) or else not Is_Constrained (T2) then
4950 return not (Is_Constrained (T1) or else Is_Constrained (T2));
4951 end if;
4952
22cb89b5
AC
4953 -- Both subtypes are constrained, so check that the index subtypes
4954 -- statically match.
996ae0b0
RK
4955
4956 declare
4957 Index1 : Node_Id := First_Index (T1);
4958 Index2 : Node_Id := First_Index (T2);
4959
4960 begin
4961 while Present (Index1) loop
4962 if not
4963 Subtypes_Statically_Match (Etype (Index1), Etype (Index2))
4964 then
4965 return False;
4966 end if;
4967
4968 Next_Index (Index1);
4969 Next_Index (Index2);
4970 end loop;
4971
4972 return True;
4973 end;
4974
4975 elsif Is_Access_Type (T1) then
b5bd964f
ES
4976 if Can_Never_Be_Null (T1) /= Can_Never_Be_Null (T2) then
4977 return False;
4978
e1b871e9
AC
4979 elsif Ekind_In (T1, E_Access_Subprogram_Type,
4980 E_Anonymous_Access_Subprogram_Type)
7a3f77d2 4981 then
b5bd964f
ES
4982 return
4983 Subtype_Conformant
4984 (Designated_Type (T1),
bb98fe75 4985 Designated_Type (T2));
b5bd964f
ES
4986 else
4987 return
4988 Subtypes_Statically_Match
4989 (Designated_Type (T1),
4990 Designated_Type (T2))
4991 and then Is_Access_Constant (T1) = Is_Access_Constant (T2);
4992 end if;
996ae0b0
RK
4993
4994 -- All other types definitely match
4995
4996 else
4997 return True;
4998 end if;
4999 end Subtypes_Statically_Match;
5000
5001 ----------
5002 -- Test --
5003 ----------
5004
5005 function Test (Cond : Boolean) return Uint is
5006 begin
5007 if Cond then
5008 return Uint_1;
5009 else
5010 return Uint_0;
5011 end if;
5012 end Test;
5013
5014 ---------------------------------
5015 -- Test_Expression_Is_Foldable --
5016 ---------------------------------
5017
5018 -- One operand case
5019
5020 procedure Test_Expression_Is_Foldable
5021 (N : Node_Id;
5022 Op1 : Node_Id;
5023 Stat : out Boolean;
5024 Fold : out Boolean)
5025 is
5026 begin
5027 Stat := False;
0356699b
RD
5028 Fold := False;
5029
5030 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5031 return;
5032 end if;
996ae0b0
RK
5033
5034 -- If operand is Any_Type, just propagate to result and do not
5035 -- try to fold, this prevents cascaded errors.
5036
5037 if Etype (Op1) = Any_Type then
5038 Set_Etype (N, Any_Type);
996ae0b0
RK
5039 return;
5040
5041 -- If operand raises constraint error, then replace node N with the
5042 -- raise constraint error node, and we are obviously not foldable.
5043 -- Note that this replacement inherits the Is_Static_Expression flag
5044 -- from the operand.
5045
5046 elsif Raises_Constraint_Error (Op1) then
5047 Rewrite_In_Raise_CE (N, Op1);
996ae0b0
RK
5048 return;
5049
5050 -- If the operand is not static, then the result is not static, and
5051 -- all we have to do is to check the operand since it is now known
5052 -- to appear in a non-static context.
5053
5054 elsif not Is_Static_Expression (Op1) then
5055 Check_Non_Static_Context (Op1);
5056 Fold := Compile_Time_Known_Value (Op1);
5057 return;
5058
5059 -- An expression of a formal modular type is not foldable because
5060 -- the modulus is unknown.
5061
5062 elsif Is_Modular_Integer_Type (Etype (Op1))
5063 and then Is_Generic_Type (Etype (Op1))
5064 then
5065 Check_Non_Static_Context (Op1);
996ae0b0
RK
5066 return;
5067
5068 -- Here we have the case of an operand whose type is OK, which is
5069 -- static, and which does not raise constraint error, we can fold.
5070
5071 else
5072 Set_Is_Static_Expression (N);
5073 Fold := True;
5074 Stat := True;
5075 end if;
5076 end Test_Expression_Is_Foldable;
5077
5078 -- Two operand case
5079
5080 procedure Test_Expression_Is_Foldable
5081 (N : Node_Id;
5082 Op1 : Node_Id;
5083 Op2 : Node_Id;
5084 Stat : out Boolean;
5085 Fold : out Boolean)
5086 is
5087 Rstat : constant Boolean := Is_Static_Expression (Op1)
5088 and then Is_Static_Expression (Op2);
5089
5090 begin
5091 Stat := False;
0356699b
RD
5092 Fold := False;
5093
5094 if Debug_Flag_Dot_F and then In_Extended_Main_Source_Unit (N) then
5095 return;
5096 end if;
996ae0b0
RK
5097
5098 -- If either operand is Any_Type, just propagate to result and
5099 -- do not try to fold, this prevents cascaded errors.
5100
5101 if Etype (Op1) = Any_Type or else Etype (Op2) = Any_Type then
5102 Set_Etype (N, Any_Type);
996ae0b0
RK
5103 return;
5104
22cb89b5
AC
5105 -- If left operand raises constraint error, then replace node N with the
5106 -- Raise_Constraint_Error node, and we are obviously not foldable.
996ae0b0
RK
5107 -- Is_Static_Expression is set from the two operands in the normal way,
5108 -- and we check the right operand if it is in a non-static context.
5109
5110 elsif Raises_Constraint_Error (Op1) then
5111 if not Rstat then
5112 Check_Non_Static_Context (Op2);
5113 end if;
5114
5115 Rewrite_In_Raise_CE (N, Op1);
5116 Set_Is_Static_Expression (N, Rstat);
996ae0b0
RK
5117 return;
5118
22cb89b5
AC
5119 -- Similar processing for the case of the right operand. Note that we
5120 -- don't use this routine for the short-circuit case, so we do not have
5121 -- to worry about that special case here.
996ae0b0
RK
5122
5123 elsif Raises_Constraint_Error (Op2) then
5124 if not Rstat then
5125 Check_Non_Static_Context (Op1);
5126 end if;
5127
5128 Rewrite_In_Raise_CE (N, Op2);
5129 Set_Is_Static_Expression (N, Rstat);
996ae0b0
RK
5130 return;
5131
82c80734 5132 -- Exclude expressions of a generic modular type, as above
996ae0b0
RK
5133
5134 elsif Is_Modular_Integer_Type (Etype (Op1))
5135 and then Is_Generic_Type (Etype (Op1))
5136 then
5137 Check_Non_Static_Context (Op1);
996ae0b0
RK
5138 return;
5139
5140 -- If result is not static, then check non-static contexts on operands
22cb89b5 5141 -- since one of them may be static and the other one may not be static.
996ae0b0
RK
5142
5143 elsif not Rstat then
5144 Check_Non_Static_Context (Op1);
5145 Check_Non_Static_Context (Op2);
5146 Fold := Compile_Time_Known_Value (Op1)
5147 and then Compile_Time_Known_Value (Op2);
5148 return;
5149
22cb89b5
AC
5150 -- Else result is static and foldable. Both operands are static, and
5151 -- neither raises constraint error, so we can definitely fold.
996ae0b0
RK
5152
5153 else
5154 Set_Is_Static_Expression (N);
5155 Fold := True;
5156 Stat := True;
5157 return;
5158 end if;
5159 end Test_Expression_Is_Foldable;
5160
305caf42
AC
5161 -------------------
5162 -- Test_In_Range --
5163 -------------------
5164
5165 function Test_In_Range
5166 (N : Node_Id;
5167 Typ : Entity_Id;
5168 Assume_Valid : Boolean;
5169 Fixed_Int : Boolean;
5170 Int_Real : Boolean) return Range_Membership
5171 is
5172 Val : Uint;
5173 Valr : Ureal;
5174
5175 pragma Warnings (Off, Assume_Valid);
5176 -- For now Assume_Valid is unreferenced since the current implementation
5177 -- always returns Unknown if N is not a compile time known value, but we
5178 -- keep the parameter to allow for future enhancements in which we try
5179 -- to get the information in the variable case as well.
5180
5181 begin
5182 -- Universal types have no range limits, so always in range
5183
5184 if Typ = Universal_Integer or else Typ = Universal_Real then
5185 return In_Range;
5186
5187 -- Never known if not scalar type. Don't know if this can actually
5188 -- happen, but our spec allows it, so we must check!
5189
5190 elsif not Is_Scalar_Type (Typ) then
5191 return Unknown;
5192
5193 -- Never known if this is a generic type, since the bounds of generic
5194 -- types are junk. Note that if we only checked for static expressions
5195 -- (instead of compile time known values) below, we would not need this
5196 -- check, because values of a generic type can never be static, but they
5197 -- can be known at compile time.
5198
5199 elsif Is_Generic_Type (Typ) then
5200 return Unknown;
5201
5202 -- Never known unless we have a compile time known value
5203
5204 elsif not Compile_Time_Known_Value (N) then
5205 return Unknown;
5206
5207 -- General processing with a known compile time value
5208
5209 else
5210 declare
5211 Lo : Node_Id;
5212 Hi : Node_Id;
5213
5214 LB_Known : Boolean;
5215 HB_Known : Boolean;
5216
5217 begin
5218 Lo := Type_Low_Bound (Typ);
5219 Hi := Type_High_Bound (Typ);
5220
5221 LB_Known := Compile_Time_Known_Value (Lo);
5222 HB_Known := Compile_Time_Known_Value (Hi);
5223
5224 -- Fixed point types should be considered as such only if flag
5225 -- Fixed_Int is set to False.
5226
5227 if Is_Floating_Point_Type (Typ)
5228 or else (Is_Fixed_Point_Type (Typ) and then not Fixed_Int)
5229 or else Int_Real
5230 then
5231 Valr := Expr_Value_R (N);
5232
5233 if LB_Known and HB_Known then
5234 if Valr >= Expr_Value_R (Lo)
5235 and then
5236 Valr <= Expr_Value_R (Hi)
5237 then
5238 return In_Range;
5239 else
5240 return Out_Of_Range;
5241 end if;
5242
5243 elsif (LB_Known and then Valr < Expr_Value_R (Lo))
5244 or else
5245 (HB_Known and then Valr > Expr_Value_R (Hi))
5246 then
5247 return Out_Of_Range;
5248
5249 else
5250 return Unknown;
5251 end if;
5252
5253 else
5254 Val := Expr_Value (N);
5255
5256 if LB_Known and HB_Known then
5257 if Val >= Expr_Value (Lo)
5258 and then
5259 Val <= Expr_Value (Hi)
5260 then
5261 return In_Range;
5262 else
5263 return Out_Of_Range;
5264 end if;
5265
5266 elsif (LB_Known and then Val < Expr_Value (Lo))
5267 or else
5268 (HB_Known and then Val > Expr_Value (Hi))
5269 then
5270 return Out_Of_Range;
5271
5272 else
5273 return Unknown;
5274 end if;
5275 end if;
5276 end;
5277 end if;
5278 end Test_In_Range;
5279
996ae0b0
RK
5280 --------------
5281 -- To_Bits --
5282 --------------
5283
5284 procedure To_Bits (U : Uint; B : out Bits) is
5285 begin
5286 for J in 0 .. B'Last loop
5287 B (J) := (U / (2 ** J)) mod 2 /= 0;
5288 end loop;
5289 end To_Bits;
5290
fbf5a39b
AC
5291 --------------------
5292 -- Why_Not_Static --
5293 --------------------
5294
5295 procedure Why_Not_Static (Expr : Node_Id) is
5296 N : constant Node_Id := Original_Node (Expr);
5297 Typ : Entity_Id;
5298 E : Entity_Id;
5299
5300 procedure Why_Not_Static_List (L : List_Id);
22cb89b5
AC
5301 -- A version that can be called on a list of expressions. Finds all
5302 -- non-static violations in any element of the list.
fbf5a39b
AC
5303
5304 -------------------------
5305 -- Why_Not_Static_List --
5306 -------------------------
5307
5308 procedure Why_Not_Static_List (L : List_Id) is
5309 N : Node_Id;
5310
5311 begin
5312 if Is_Non_Empty_List (L) then
5313 N := First (L);
5314 while Present (N) loop
5315 Why_Not_Static (N);
5316 Next (N);
5317 end loop;
5318 end if;
5319 end Why_Not_Static_List;
5320
5321 -- Start of processing for Why_Not_Static
5322
5323 begin
22cb89b5
AC
5324 -- If in ACATS mode (debug flag 2), then suppress all these messages,
5325 -- this avoids massive updates to the ACATS base line.
fbf5a39b
AC
5326
5327 if Debug_Flag_2 then
5328 return;
5329 end if;
5330
5331 -- Ignore call on error or empty node
5332
5333 if No (Expr) or else Nkind (Expr) = N_Error then
5334 return;
5335 end if;
5336
5337 -- Preprocessing for sub expressions
5338
5339 if Nkind (Expr) in N_Subexpr then
5340
5341 -- Nothing to do if expression is static
5342
5343 if Is_OK_Static_Expression (Expr) then
5344 return;
5345 end if;
5346
5347 -- Test for constraint error raised
5348
5349 if Raises_Constraint_Error (Expr) then
5350 Error_Msg_N
5351 ("expression raises exception, cannot be static " &
b11e8d6f 5352 "(RM 4.9(34))!", N);
fbf5a39b
AC
5353 return;
5354 end if;
5355
5356 -- If no type, then something is pretty wrong, so ignore
5357
5358 Typ := Etype (Expr);
5359
5360 if No (Typ) then
5361 return;
5362 end if;
5363
5364 -- Type must be scalar or string type
5365
5366 if not Is_Scalar_Type (Typ)
5367 and then not Is_String_Type (Typ)
5368 then
5369 Error_Msg_N
5370 ("static expression must have scalar or string type " &
b11e8d6f 5371 "(RM 4.9(2))!", N);
fbf5a39b
AC
5372 return;
5373 end if;
5374 end if;
5375
5376 -- If we got through those checks, test particular node kind
5377
5378 case Nkind (N) is
5379 when N_Expanded_Name | N_Identifier | N_Operator_Symbol =>
5380 E := Entity (N);
5381
5382 if Is_Named_Number (E) then
5383 null;
5384
5385 elsif Ekind (E) = E_Constant then
5386 if not Is_Static_Expression (Constant_Value (E)) then
5387 Error_Msg_NE
b11e8d6f 5388 ("& is not a static constant (RM 4.9(5))!", N, E);
fbf5a39b
AC
5389 end if;
5390
5391 else
5392 Error_Msg_NE
5393 ("& is not static constant or named number " &
b11e8d6f 5394 "(RM 4.9(5))!", N, E);
fbf5a39b
AC
5395 end if;
5396
514d0fc5 5397 when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
fbf5a39b
AC
5398 if Nkind (N) in N_Op_Shift then
5399 Error_Msg_N
b11e8d6f 5400 ("shift functions are never static (RM 4.9(6,18))!", N);
fbf5a39b
AC
5401
5402 else
5403 Why_Not_Static (Left_Opnd (N));
5404 Why_Not_Static (Right_Opnd (N));
5405 end if;
5406
5407 when N_Unary_Op =>
5408 Why_Not_Static (Right_Opnd (N));
5409
5410 when N_Attribute_Reference =>
5411 Why_Not_Static_List (Expressions (N));
5412
5413 E := Etype (Prefix (N));
5414
5415 if E = Standard_Void_Type then
5416 return;
5417 end if;
5418
5419 -- Special case non-scalar'Size since this is a common error
5420
5421 if Attribute_Name (N) = Name_Size then
5422 Error_Msg_N
fdfea49e 5423 ("size attribute is only static for static scalar type " &
b11e8d6f 5424 "(RM 4.9(7,8))", N);
fbf5a39b
AC
5425
5426 -- Flag array cases
5427
5428 elsif Is_Array_Type (E) then
5429 if Attribute_Name (N) /= Name_First
5430 and then
5431 Attribute_Name (N) /= Name_Last
5432 and then
5433 Attribute_Name (N) /= Name_Length
5434 then
5435 Error_Msg_N
5436 ("static array attribute must be Length, First, or Last " &
b11e8d6f 5437 "(RM 4.9(8))!", N);
fbf5a39b
AC
5438
5439 -- Since we know the expression is not-static (we already
5440 -- tested for this, must mean array is not static).
5441
5442 else
5443 Error_Msg_N
b11e8d6f 5444 ("prefix is non-static array (RM 4.9(8))!", Prefix (N));
fbf5a39b
AC
5445 end if;
5446
5447 return;
5448
22cb89b5
AC
5449 -- Special case generic types, since again this is a common source
5450 -- of confusion.
fbf5a39b
AC
5451
5452 elsif Is_Generic_Actual_Type (E)
5453 or else
5454 Is_Generic_Type (E)
5455 then
5456 Error_Msg_N
5457 ("attribute of generic type is never static " &
b11e8d6f 5458 "(RM 4.9(7,8))!", N);
fbf5a39b
AC
5459
5460 elsif Is_Static_Subtype (E) then
5461 null;
5462
5463 elsif Is_Scalar_Type (E) then
5464 Error_Msg_N
5465 ("prefix type for attribute is not static scalar subtype " &
b11e8d6f 5466 "(RM 4.9(7))!", N);
fbf5a39b
AC
5467
5468 else
5469 Error_Msg_N
5470 ("static attribute must apply to array/scalar type " &
b11e8d6f 5471 "(RM 4.9(7,8))!", N);
fbf5a39b
AC
5472 end if;
5473
5474 when N_String_Literal =>
5475 Error_Msg_N
b11e8d6f 5476 ("subtype of string literal is non-static (RM 4.9(4))!", N);
fbf5a39b
AC
5477
5478 when N_Explicit_Dereference =>
5479 Error_Msg_N
b11e8d6f 5480 ("explicit dereference is never static (RM 4.9)!", N);
fbf5a39b
AC
5481
5482 when N_Function_Call =>
5483 Why_Not_Static_List (Parameter_Associations (N));
b11e8d6f 5484 Error_Msg_N ("non-static function call (RM 4.9(6,18))!", N);
fbf5a39b
AC
5485
5486 when N_Parameter_Association =>
5487 Why_Not_Static (Explicit_Actual_Parameter (N));
5488
5489 when N_Indexed_Component =>
5490 Error_Msg_N
b11e8d6f 5491 ("indexed component is never static (RM 4.9)!", N);
fbf5a39b
AC
5492
5493 when N_Procedure_Call_Statement =>
5494 Error_Msg_N
b11e8d6f 5495 ("procedure call is never static (RM 4.9)!", N);
fbf5a39b
AC
5496
5497 when N_Qualified_Expression =>
5498 Why_Not_Static (Expression (N));
5499
5500 when N_Aggregate | N_Extension_Aggregate =>
5501 Error_Msg_N
b11e8d6f 5502 ("an aggregate is never static (RM 4.9)!", N);
fbf5a39b
AC
5503
5504 when N_Range =>
5505 Why_Not_Static (Low_Bound (N));
5506 Why_Not_Static (High_Bound (N));
5507
5508 when N_Range_Constraint =>
5509 Why_Not_Static (Range_Expression (N));
5510
5511 when N_Subtype_Indication =>
5512 Why_Not_Static (Constraint (N));
5513
5514 when N_Selected_Component =>
5515 Error_Msg_N
b11e8d6f 5516 ("selected component is never static (RM 4.9)!", N);
fbf5a39b
AC
5517
5518 when N_Slice =>
5519 Error_Msg_N
b11e8d6f 5520 ("slice is never static (RM 4.9)!", N);
fbf5a39b
AC
5521
5522 when N_Type_Conversion =>
5523 Why_Not_Static (Expression (N));
5524
23b86353
AC
5525 if not Is_Scalar_Type (Entity (Subtype_Mark (N)))
5526 or else not Is_Static_Subtype (Entity (Subtype_Mark (N)))
fbf5a39b
AC
5527 then
5528 Error_Msg_N
5529 ("static conversion requires static scalar subtype result " &
b11e8d6f 5530 "(RM 4.9(9))!", N);
fbf5a39b
AC
5531 end if;
5532
5533 when N_Unchecked_Type_Conversion =>
5534 Error_Msg_N
b11e8d6f 5535 ("unchecked type conversion is never static (RM 4.9)!", N);
fbf5a39b
AC
5536
5537 when others =>
5538 null;
5539
5540 end case;
5541 end Why_Not_Static;
5542
996ae0b0 5543end Sem_Eval;