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