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