]>
Commit | Line | Data |
---|---|---|
70482933 RK |
1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- C H E C K S -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
cccef051 | 9 | -- Copyright (C) 1992-2023, Free Software Foundation, Inc. -- |
70482933 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- -- |
70482933 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. -- | |
70482933 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. -- |
70482933 RK |
23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
104f58db | 26 | with Atree; use Atree; |
104f58db BD |
27 | with Debug; use Debug; |
28 | with Einfo; use Einfo; | |
76f9c7f4 | 29 | with Einfo.Entities; use Einfo.Entities; |
104f58db BD |
30 | with Einfo.Utils; use Einfo.Utils; |
31 | with Elists; use Elists; | |
32 | with Eval_Fat; use Eval_Fat; | |
33 | with Exp_Ch11; use Exp_Ch11; | |
34 | with Exp_Ch4; use Exp_Ch4; | |
35 | with Exp_Pakd; use Exp_Pakd; | |
36 | with Exp_Util; use Exp_Util; | |
37 | with Expander; use Expander; | |
38 | with Freeze; use Freeze; | |
39 | with Lib; use Lib; | |
40 | with Nlists; use Nlists; | |
41 | with Nmake; use Nmake; | |
42 | with Opt; use Opt; | |
43 | with Output; use Output; | |
44 | with Restrict; use Restrict; | |
45 | with Rident; use Rident; | |
46 | with Rtsfind; use Rtsfind; | |
47 | with Sem; use Sem; | |
48 | with Sem_Aux; use Sem_Aux; | |
49 | with Sem_Ch3; use Sem_Ch3; | |
50 | with Sem_Ch8; use Sem_Ch8; | |
29d39651 | 51 | with Sem_Cat; use Sem_Cat; |
104f58db BD |
52 | with Sem_Disp; use Sem_Disp; |
53 | with Sem_Eval; use Sem_Eval; | |
54 | with Sem_Mech; use Sem_Mech; | |
55 | with Sem_Res; use Sem_Res; | |
56 | with Sem_Util; use Sem_Util; | |
57 | with Sem_Warn; use Sem_Warn; | |
58 | with Sinfo; use Sinfo; | |
59 | with Sinfo.Nodes; use Sinfo.Nodes; | |
60 | with Sinfo.Utils; use Sinfo.Utils; | |
61 | with Sinput; use Sinput; | |
62 | with Snames; use Snames; | |
63 | with Sprint; use Sprint; | |
64 | with Stand; use Stand; | |
65 | with Stringt; use Stringt; | |
66 | with Targparm; use Targparm; | |
67 | with Tbuild; use Tbuild; | |
68 | with Ttypes; use Ttypes; | |
69 | with Validsw; use Validsw; | |
70482933 RK |
70 | |
71 | package body Checks is | |
72 | ||
73 | -- General note: many of these routines are concerned with generating | |
74 | -- checking code to make sure that constraint error is raised at runtime. | |
75 | -- Clearly this code is only needed if the expander is active, since | |
76 | -- otherwise we will not be generating code or going into the runtime | |
77 | -- execution anyway. | |
78 | ||
79 | -- We therefore disconnect most of these checks if the expander is | |
80 | -- inactive. This has the additional benefit that we do not need to | |
81 | -- worry about the tree being messed up by previous errors (since errors | |
82 | -- turn off expansion anyway). | |
83 | ||
84 | -- There are a few exceptions to the above rule. For instance routines | |
85 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
86 | -- safely called even when the Expander is inactive (but Errors_Detected | |
87 | -- is 0). The benefit of executing this code when expansion is off, is | |
29d39651 | 88 | -- the ability to emit constraint error warnings for static expressions |
70482933 RK |
89 | -- even when we are not generating code. |
90 | ||
637a41a5 AC |
91 | -- The above is modified in gnatprove mode to ensure that proper check |
92 | -- flags are always placed, even if expansion is off. | |
93 | ||
fbf5a39b AC |
94 | ------------------------------------- |
95 | -- Suppression of Redundant Checks -- | |
96 | ------------------------------------- | |
97 | ||
98 | -- This unit implements a limited circuit for removal of redundant | |
99 | -- checks. The processing is based on a tracing of simple sequential | |
100 | -- flow. For any sequence of statements, we save expressions that are | |
101 | -- marked to be checked, and then if the same expression appears later | |
102 | -- with the same check, then under certain circumstances, the second | |
103 | -- check can be suppressed. | |
104 | ||
105 | -- Basically, we can suppress the check if we know for certain that | |
106 | -- the previous expression has been elaborated (together with its | |
107 | -- check), and we know that the exception frame is the same, and that | |
108 | -- nothing has happened to change the result of the exception. | |
109 | ||
110 | -- Let us examine each of these three conditions in turn to describe | |
111 | -- how we ensure that this condition is met. | |
112 | ||
113 | -- First, we need to know for certain that the previous expression has | |
308e6f3a | 114 | -- been executed. This is done principally by the mechanism of calling |
fbf5a39b AC |
115 | -- Conditional_Statements_Begin at the start of any statement sequence |
116 | -- and Conditional_Statements_End at the end. The End call causes all | |
117 | -- checks remembered since the Begin call to be discarded. This does | |
118 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
119 | -- no exception handlers. But the important thing is to be conservative. | |
120 | -- The other protection is that all checks are discarded if a label | |
121 | -- is encountered, since then the assumption of sequential execution | |
122 | -- is violated, and we don't know enough about the flow. | |
123 | ||
124 | -- Second, we need to know that the exception frame is the same. We | |
125 | -- do this by killing all remembered checks when we enter a new frame. | |
126 | -- Again, that's over-conservative, but generally the cases we can help | |
127 | -- with are pretty local anyway (like the body of a loop for example). | |
128 | ||
129 | -- Third, we must be sure to forget any checks which are no longer valid. | |
130 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
131 | -- used to note any changes to local variables. We only attempt to deal | |
132 | -- with checks involving local variables, so we do not need to worry | |
133 | -- about global variables. Second, a call to any non-global procedure | |
134 | -- causes us to abandon all stored checks, since such a all may affect | |
135 | -- the values of any local variables. | |
136 | ||
137 | -- The following define the data structures used to deal with remembering | |
138 | -- checks so that redundant checks can be eliminated as described above. | |
139 | ||
140 | -- Right now, the only expressions that we deal with are of the form of | |
141 | -- simple local objects (either declared locally, or IN parameters) or | |
142 | -- such objects plus/minus a compile time known constant. We can do | |
143 | -- more later on if it seems worthwhile, but this catches many simple | |
144 | -- cases in practice. | |
145 | ||
146 | -- The following record type reflects a single saved check. An entry | |
147 | -- is made in the stack of saved checks if and only if the expression | |
148 | -- has been elaborated with the indicated checks. | |
149 | ||
150 | type Saved_Check is record | |
151 | Killed : Boolean; | |
152 | -- Set True if entry is killed by Kill_Checks | |
153 | ||
154 | Entity : Entity_Id; | |
155 | -- The entity involved in the expression that is checked | |
156 | ||
157 | Offset : Uint; | |
158 | -- A compile time value indicating the result of adding or | |
159 | -- subtracting a compile time value. This value is to be | |
160 | -- added to the value of the Entity. A value of zero is | |
161 | -- used for the case of a simple entity reference. | |
162 | ||
163 | Check_Type : Character; | |
164 | -- This is set to 'R' for a range check (in which case Target_Type | |
165 | -- is set to the target type for the range check) or to 'O' for an | |
166 | -- overflow check (in which case Target_Type is set to Empty). | |
167 | ||
168 | Target_Type : Entity_Id; | |
169 | -- Used only if Do_Range_Check is set. Records the target type for | |
170 | -- the check. We need this, because a check is a duplicate only if | |
308e6f3a | 171 | -- it has the same target type (or more accurately one with a |
fbf5a39b AC |
172 | -- range that is smaller or equal to the stored target type of a |
173 | -- saved check). | |
174 | end record; | |
175 | ||
176 | -- The following table keeps track of saved checks. Rather than use an | |
d1915cb8 | 177 | -- extensible table, we just use a table of fixed size, and we discard |
fbf5a39b AC |
178 | -- any saved checks that do not fit. That's very unlikely to happen and |
179 | -- this is only an optimization in any case. | |
180 | ||
181 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
182 | -- Array of saved checks | |
183 | ||
184 | Num_Saved_Checks : Nat := 0; | |
185 | -- Number of saved checks | |
186 | ||
187 | -- The following stack keeps track of statement ranges. It is treated | |
188 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
189 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
190 | -- at the time of the call. Then when Conditional_Statements_End is | |
191 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
192 | ||
193 | -- Note: again, this is a fixed length stack with a size that should | |
194 | -- always be fine. If the value of the stack pointer goes above the | |
195 | -- limit, then we just forget all saved checks. | |
196 | ||
197 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
198 | Saved_Checks_TOS : Nat := 0; | |
199 | ||
200 | ----------------------- | |
201 | -- Local Subprograms -- | |
202 | ----------------------- | |
70482933 | 203 | |
a7f1b24f | 204 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
acad3c0a | 205 | -- Used to apply arithmetic overflow checks for all cases except operators |
3ada950b | 206 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
a7f1b24f RD |
207 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
208 | -- signed integer arithmetic operator (but not an if or case expression). | |
209 | -- It is also called for types other than signed integers. | |
acad3c0a AC |
210 | |
211 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
212 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
a7f1b24f RD |
213 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
214 | -- arithmetic op (which includes the case of if and case expressions). Note | |
215 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
216 | -- we have work to do even if overflow checking is suppressed. | |
acad3c0a | 217 | |
a91e9ac7 AC |
218 | procedure Apply_Division_Check |
219 | (N : Node_Id; | |
220 | Rlo : Uint; | |
221 | Rhi : Uint; | |
222 | ROK : Boolean); | |
223 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
224 | -- division checks as required if the Do_Division_Check flag is set. | |
225 | -- Rlo and Rhi give the possible range of the right operand, these values | |
226 | -- can be referenced and trusted only if ROK is set True. | |
227 | ||
228 | procedure Apply_Float_Conversion_Check | |
6c8e70fe | 229 | (Expr : Node_Id; |
a91e9ac7 AC |
230 | Target_Typ : Entity_Id); |
231 | -- The checks on a conversion from a floating-point type to an integer | |
232 | -- type are delicate. They have to be performed before conversion, they | |
233 | -- have to raise an exception when the operand is a NaN, and rounding must | |
234 | -- be taken into account to determine the safe bounds of the operand. | |
235 | ||
70482933 | 236 | procedure Apply_Selected_Length_Checks |
6c8e70fe | 237 | (Expr : Node_Id; |
70482933 RK |
238 | Target_Typ : Entity_Id; |
239 | Source_Typ : Entity_Id; | |
240 | Do_Static : Boolean); | |
241 | -- This is the subprogram that does all the work for Apply_Length_Check | |
242 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
243 | -- described for the above routines. The Do_Static flag indicates that | |
244 | -- only a static check is to be done. | |
245 | ||
12be130c EB |
246 | procedure Compute_Range_For_Arithmetic_Op |
247 | (Op : Node_Kind; | |
248 | Lo_Left : Uint; | |
249 | Hi_Left : Uint; | |
250 | Lo_Right : Uint; | |
251 | Hi_Right : Uint; | |
252 | OK : out Boolean; | |
253 | Lo : out Uint; | |
254 | Hi : out Uint); | |
255 | -- Given an integer arithmetical operation Op and the range of values of | |
256 | -- its operand(s), try to compute a conservative estimate of the possible | |
257 | -- range of values for the result of the operation. Thus if OK is True on | |
258 | -- return, the result is known to lie in the range Lo .. Hi (inclusive). | |
259 | -- If OK is false, both Lo and Hi are set to No_Uint. | |
260 | ||
939c12d2 | 261 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
2ede092b RD |
262 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
263 | -- This function is used to see if an access or division by zero check is | |
264 | -- needed. The check is to be applied to a single variable appearing in the | |
265 | -- source, and N is the node for the reference. If N is not of this form, | |
266 | -- True is returned with no further processing. If N is of the right form, | |
267 | -- then further processing determines if the given Check is needed. | |
268 | -- | |
269 | -- The particular circuit is to see if we have the case of a check that is | |
270 | -- not needed because it appears in the right operand of a short circuited | |
271 | -- conditional where the left operand guards the check. For example: | |
272 | -- | |
273 | -- if Var = 0 or else Q / Var > 12 then | |
274 | -- ... | |
275 | -- end if; | |
276 | -- | |
277 | -- In this example, the division check is not required. At the same time | |
278 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
279 | -- such as: | |
280 | -- | |
281 | -- if Var = 0 or Q / Var > 12 then | |
282 | -- ... | |
283 | -- end if; | |
284 | ||
fbf5a39b AC |
285 | procedure Find_Check |
286 | (Expr : Node_Id; | |
287 | Check_Type : Character; | |
288 | Target_Type : Entity_Id; | |
289 | Entry_OK : out Boolean; | |
290 | Check_Num : out Nat; | |
291 | Ent : out Entity_Id; | |
292 | Ofs : out Uint); | |
293 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
294 | -- to see if a check is of the form for optimization, and if so, to see | |
295 | -- if it has already been performed. Expr is the expression to check, | |
296 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
297 | -- Target_Type is the target type for a range check, and Empty for an | |
298 | -- overflow check. If the entry is not of the form for optimization, | |
299 | -- then Entry_OK is set to False, and the remaining out parameters | |
300 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
301 | -- entity and offset from the expression. Check_Num is the number of | |
302 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
303 | -- is located. | |
304 | ||
70482933 RK |
305 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
306 | -- If a discriminal is used in constraining a prival, Return reference | |
307 | -- to the discriminal of the protected body (which renames the parameter | |
308 | -- of the enclosing protected operation). This clumsy transformation is | |
309 | -- needed because privals are created too late and their actual subtypes | |
310 | -- are not available when analysing the bodies of the protected operations. | |
c064e066 RD |
311 | -- This function is called whenever the bound is an entity and the scope |
312 | -- indicates a protected operation. If the bound is an in-parameter of | |
313 | -- a protected operation that is not a prival, the function returns the | |
314 | -- bound itself. | |
70482933 RK |
315 | -- To be cleaned up??? |
316 | ||
317 | function Guard_Access | |
6c8e70fe EB |
318 | (Cond : Node_Id; |
319 | Loc : Source_Ptr; | |
320 | Expr : Node_Id) return Node_Id; | |
70482933 RK |
321 | -- In the access type case, guard the test with a test to ensure |
322 | -- that the access value is non-null, since the checks do not | |
323 | -- not apply to null access values. | |
324 | ||
325 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
326 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
327 | -- Constraint_Error node. | |
328 | ||
acad3c0a AC |
329 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
330 | -- Returns True if node N is for an arithmetic operation with signed | |
4b1c4f20 RD |
331 | -- integer operands. This includes unary and binary operators, and also |
332 | -- if and case expression nodes where the dependent expressions are of | |
333 | -- a signed integer type. These are the kinds of nodes for which special | |
3ada950b | 334 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
acad3c0a | 335 | |
c064e066 RD |
336 | function Range_Or_Validity_Checks_Suppressed |
337 | (Expr : Node_Id) return Boolean; | |
338 | -- Returns True if either range or validity checks or both are suppressed | |
339 | -- for the type of the given expression, or, if the expression is the name | |
340 | -- of an entity, if these checks are suppressed for the entity. | |
341 | ||
70482933 | 342 | function Selected_Length_Checks |
6c8e70fe | 343 | (Expr : Node_Id; |
70482933 RK |
344 | Target_Typ : Entity_Id; |
345 | Source_Typ : Entity_Id; | |
6b6fcd3e | 346 | Warn_Node : Node_Id) return Check_Result; |
70482933 RK |
347 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
348 | -- anything, just returns a list of nodes as described in the spec of | |
349 | -- this package for the Range_Check function. | |
66340e0e AC |
350 | -- ??? In fact it does construct the test and insert it into the tree, |
351 | -- and insert actions in various ways (calling Insert_Action directly | |
352 | -- in particular) so we do not call it in GNATprove mode, contrary to | |
353 | -- Selected_Range_Checks. | |
70482933 RK |
354 | |
355 | function Selected_Range_Checks | |
6c8e70fe | 356 | (Expr : Node_Id; |
70482933 RK |
357 | Target_Typ : Entity_Id; |
358 | Source_Typ : Entity_Id; | |
6b6fcd3e | 359 | Warn_Node : Node_Id) return Check_Result; |
451187a3 | 360 | -- Like Apply_Range_Check, except it does not modify anything, just |
ec170be1 | 361 | -- returns a list of nodes as described in the spec of this package |
70482933 RK |
362 | -- for the Range_Check function. |
363 | ||
364 | ------------------------------ | |
365 | -- Access_Checks_Suppressed -- | |
366 | ------------------------------ | |
367 | ||
368 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
369 | begin | |
fbf5a39b AC |
370 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
371 | return Is_Check_Suppressed (E, Access_Check); | |
372 | else | |
3217f71e | 373 | return Scope_Suppress.Suppress (Access_Check); |
fbf5a39b | 374 | end if; |
70482933 RK |
375 | end Access_Checks_Suppressed; |
376 | ||
377 | ------------------------------------- | |
378 | -- Accessibility_Checks_Suppressed -- | |
379 | ------------------------------------- | |
380 | ||
381 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
382 | begin | |
bcb8c3bb JS |
383 | if No_Dynamic_Accessibility_Checks_Enabled (E) then |
384 | return True; | |
385 | ||
386 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
fbf5a39b | 387 | return Is_Check_Suppressed (E, Accessibility_Check); |
bcb8c3bb | 388 | |
fbf5a39b | 389 | else |
3217f71e | 390 | return Scope_Suppress.Suppress (Accessibility_Check); |
fbf5a39b | 391 | end if; |
70482933 RK |
392 | end Accessibility_Checks_Suppressed; |
393 | ||
11b4899f JM |
394 | ----------------------------- |
395 | -- Activate_Division_Check -- | |
396 | ----------------------------- | |
397 | ||
398 | procedure Activate_Division_Check (N : Node_Id) is | |
399 | begin | |
400 | Set_Do_Division_Check (N, True); | |
401 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
402 | end Activate_Division_Check; | |
403 | ||
404 | ----------------------------- | |
405 | -- Activate_Overflow_Check -- | |
406 | ----------------------------- | |
407 | ||
408 | procedure Activate_Overflow_Check (N : Node_Id) is | |
bb304287 AC |
409 | Typ : constant Entity_Id := Etype (N); |
410 | ||
11b4899f | 411 | begin |
bb304287 AC |
412 | -- Floating-point case. If Etype is not set (this can happen when we |
413 | -- activate a check on a node that has not yet been analyzed), then | |
414 | -- we assume we do not have a floating-point type (as per our spec). | |
415 | ||
416 | if Present (Typ) and then Is_Floating_Point_Type (Typ) then | |
417 | ||
418 | -- Ignore call if we have no automatic overflow checks on the target | |
419 | -- and Check_Float_Overflow mode is not set. These are the cases in | |
420 | -- which we expect to generate infinities and NaN's with no check. | |
421 | ||
422 | if not (Machine_Overflows_On_Target or Check_Float_Overflow) then | |
423 | return; | |
424 | ||
425 | -- Ignore for unary operations ("+", "-", abs) since these can never | |
426 | -- result in overflow for floating-point cases. | |
396eb900 | 427 | |
bb304287 AC |
428 | elsif Nkind (N) in N_Unary_Op then |
429 | return; | |
430 | ||
431 | -- Otherwise we will set the flag | |
432 | ||
433 | else | |
434 | null; | |
435 | end if; | |
436 | ||
437 | -- Discrete case | |
438 | ||
439 | else | |
440 | -- Nothing to do for Rem/Mod/Plus (overflow not possible, the check | |
441 | -- for zero-divide is a divide check, not an overflow check). | |
396eb900 | 442 | |
4a08c95c | 443 | if Nkind (N) in N_Op_Rem | N_Op_Mod | N_Op_Plus then |
bb304287 AC |
444 | return; |
445 | end if; | |
396eb900 AC |
446 | end if; |
447 | ||
bb304287 | 448 | -- Fall through for cases where we do set the flag |
396eb900 | 449 | |
a7191e01 | 450 | Set_Do_Overflow_Check (N); |
396eb900 | 451 | Possible_Local_Raise (N, Standard_Constraint_Error); |
11b4899f JM |
452 | end Activate_Overflow_Check; |
453 | ||
454 | -------------------------- | |
455 | -- Activate_Range_Check -- | |
456 | -------------------------- | |
457 | ||
458 | procedure Activate_Range_Check (N : Node_Id) is | |
459 | begin | |
68c8d72a | 460 | Set_Do_Range_Check (N); |
11b4899f JM |
461 | Possible_Local_Raise (N, Standard_Constraint_Error); |
462 | end Activate_Range_Check; | |
463 | ||
c064e066 RD |
464 | --------------------------------- |
465 | -- Alignment_Checks_Suppressed -- | |
466 | --------------------------------- | |
467 | ||
468 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
469 | begin | |
470 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
471 | return Is_Check_Suppressed (E, Alignment_Check); | |
472 | else | |
3217f71e | 473 | return Scope_Suppress.Suppress (Alignment_Check); |
c064e066 RD |
474 | end if; |
475 | end Alignment_Checks_Suppressed; | |
476 | ||
b07b7ace AC |
477 | ---------------------------------- |
478 | -- Allocation_Checks_Suppressed -- | |
479 | ---------------------------------- | |
480 | ||
59f4d038 RD |
481 | -- Note: at the current time there are no calls to this function, because |
482 | -- the relevant check is in the run-time, so it is not a check that the | |
483 | -- compiler can suppress anyway, but we still have to recognize the check | |
484 | -- name Allocation_Check since it is part of the standard. | |
485 | ||
b07b7ace AC |
486 | function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
487 | begin | |
488 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
489 | return Is_Check_Suppressed (E, Allocation_Check); | |
490 | else | |
491 | return Scope_Suppress.Suppress (Allocation_Check); | |
492 | end if; | |
493 | end Allocation_Checks_Suppressed; | |
494 | ||
70482933 RK |
495 | ------------------------- |
496 | -- Append_Range_Checks -- | |
497 | ------------------------- | |
498 | ||
499 | procedure Append_Range_Checks | |
500 | (Checks : Check_Result; | |
501 | Stmts : List_Id; | |
502 | Suppress_Typ : Entity_Id; | |
bbe7d67f | 503 | Static_Sloc : Source_Ptr) |
70482933 | 504 | is |
e0666fc6 AC |
505 | Checks_On : constant Boolean := |
506 | not Index_Checks_Suppressed (Suppress_Typ) | |
507 | or else | |
508 | not Range_Checks_Suppressed (Suppress_Typ); | |
509 | ||
70482933 | 510 | begin |
869a06d9 | 511 | -- For now we just return if Checks_On is false, however this could be |
e0666fc6 | 512 | -- enhanced to check for an always True value in the condition and to |
869a06d9 | 513 | -- generate a compilation warning. |
70482933 RK |
514 | |
515 | if not Checks_On then | |
516 | return; | |
517 | end if; | |
518 | ||
519 | for J in 1 .. 2 loop | |
520 | exit when No (Checks (J)); | |
521 | ||
522 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
523 | and then Present (Condition (Checks (J))) | |
524 | then | |
bbe7d67f | 525 | Append_To (Stmts, Checks (J)); |
70482933 RK |
526 | else |
527 | Append_To | |
07fc65c4 | 528 | (Stmts, |
bbe7d67f | 529 | Make_Raise_Constraint_Error (Static_Sloc, |
07fc65c4 | 530 | Reason => CE_Range_Check_Failed)); |
70482933 RK |
531 | end if; |
532 | end loop; | |
533 | end Append_Range_Checks; | |
534 | ||
535 | ------------------------ | |
536 | -- Apply_Access_Check -- | |
537 | ------------------------ | |
538 | ||
539 | procedure Apply_Access_Check (N : Node_Id) is | |
540 | P : constant Node_Id := Prefix (N); | |
541 | ||
542 | begin | |
2ede092b RD |
543 | -- We do not need checks if we are not generating code (i.e. the |
544 | -- expander is not active). This is not just an optimization, there | |
545 | -- are cases (e.g. with pragma Debug) where generating the checks | |
cb9d41eb | 546 | -- can cause real trouble. |
6cdb2c6e | 547 | |
4460a9bc | 548 | if not Expander_Active then |
2ede092b | 549 | return; |
fbf5a39b | 550 | end if; |
70482933 | 551 | |
86ac5e79 | 552 | -- No check if short circuiting makes check unnecessary |
fbf5a39b | 553 | |
86ac5e79 ES |
554 | if not Check_Needed (P, Access_Check) then |
555 | return; | |
70482933 | 556 | end if; |
fbf5a39b | 557 | |
f2cbd970 JM |
558 | -- No check if accessing the Offset_To_Top component of a dispatch |
559 | -- table. They are safe by construction. | |
560 | ||
1be9633f AC |
561 | if Tagged_Type_Expansion |
562 | and then Present (Etype (P)) | |
3477e0b2 | 563 | and then Is_RTE (Etype (P), RE_Offset_To_Top_Ptr) |
f2cbd970 JM |
564 | then |
565 | return; | |
566 | end if; | |
567 | ||
86ac5e79 | 568 | -- Otherwise go ahead and install the check |
fbf5a39b | 569 | |
2820d220 | 570 | Install_Null_Excluding_Check (P); |
70482933 RK |
571 | end Apply_Access_Check; |
572 | ||
c064e066 RD |
573 | -------------------------------- |
574 | -- Apply_Address_Clause_Check -- | |
575 | -------------------------------- | |
576 | ||
577 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
6f5c2c4b AC |
578 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
579 | ||
80007176 AC |
580 | AC : constant Node_Id := Address_Clause (E); |
581 | Loc : constant Source_Ptr := Sloc (AC); | |
582 | Typ : constant Entity_Id := Etype (E); | |
980f237d | 583 | |
980f237d | 584 | Expr : Node_Id; |
c064e066 RD |
585 | -- Address expression (not necessarily the same as Aexp, for example |
586 | -- when Aexp is a reference to a constant, in which case Expr gets | |
aca670a0 | 587 | -- reset to reference the value expression of the constant). |
c064e066 | 588 | |
980f237d | 589 | begin |
f4cd2542 EB |
590 | -- See if alignment check needed. Note that we never need a check if the |
591 | -- maximum alignment is one, since the check will always succeed. | |
592 | ||
593 | -- Note: we do not check for checks suppressed here, since that check | |
594 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
595 | -- only called if checks were not suppressed. The reason for this is | |
596 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
597 | -- time (so that all types etc are elaborated), but we have to check | |
598 | -- the status of check suppressing at the point of the address clause. | |
599 | ||
600 | if No (AC) | |
601 | or else not Check_Address_Alignment (AC) | |
602 | or else Maximum_Alignment = 1 | |
603 | then | |
604 | return; | |
605 | end if; | |
606 | ||
607 | -- Obtain expression from address clause | |
fbf5a39b | 608 | |
f26a3587 | 609 | Expr := Address_Value (Expression (AC)); |
c064e066 | 610 | |
f26a3587 AC |
611 | -- See if we know that Expr has an acceptable value at compile time. If |
612 | -- it hasn't or we don't know, we defer issuing the warning until the | |
613 | -- end of the compilation to take into account back end annotations. | |
980f237d GB |
614 | |
615 | if Compile_Time_Known_Value (Expr) | |
ddda9d0f | 616 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
980f237d | 617 | then |
ddda9d0f AC |
618 | declare |
619 | AL : Uint := Alignment (Typ); | |
620 | ||
621 | begin | |
80007176 AC |
622 | -- The object alignment might be more restrictive than the type |
623 | -- alignment. | |
ddda9d0f AC |
624 | |
625 | if Known_Alignment (E) then | |
626 | AL := Alignment (E); | |
627 | end if; | |
628 | ||
f26a3587 | 629 | if Expr_Value (Expr) mod AL = 0 then |
c064e066 | 630 | return; |
ddda9d0f AC |
631 | end if; |
632 | end; | |
980f237d | 633 | |
e9c12b91 AC |
634 | -- If the expression has the form X'Address, then we can find out if the |
635 | -- object X has an alignment that is compatible with the object E. If it | |
636 | -- hasn't or we don't know, we defer issuing the warning until the end | |
637 | -- of the compilation to take into account back end annotations. | |
980f237d | 638 | |
c064e066 RD |
639 | elsif Nkind (Expr) = N_Attribute_Reference |
640 | and then Attribute_Name (Expr) = Name_Address | |
e9c12b91 AC |
641 | and then |
642 | Has_Compatible_Alignment (E, Prefix (Expr), False) = Known_Compatible | |
c064e066 | 643 | then |
f4cd2542 | 644 | return; |
c064e066 | 645 | end if; |
980f237d | 646 | |
308e6f3a RW |
647 | -- Here we do not know if the value is acceptable. Strictly we don't |
648 | -- have to do anything, since if the alignment is bad, we have an | |
649 | -- erroneous program. However we are allowed to check for erroneous | |
650 | -- conditions and we decide to do this by default if the check is not | |
651 | -- suppressed. | |
c064e066 RD |
652 | |
653 | -- However, don't do the check if elaboration code is unwanted | |
654 | ||
655 | if Restriction_Active (No_Elaboration_Code) then | |
656 | return; | |
657 | ||
658 | -- Generate a check to raise PE if alignment may be inappropriate | |
659 | ||
660 | else | |
43018f58 | 661 | -- If the original expression is a nonstatic constant, use the name |
80007176 AC |
662 | -- of the constant itself rather than duplicating its initialization |
663 | -- expression, which was extracted above. | |
c064e066 | 664 | |
11b4899f JM |
665 | -- Note: Expr is empty if the address-clause is applied to in-mode |
666 | -- actuals (allowed by 13.1(22)). | |
667 | ||
7d0d27d9 | 668 | if No (Expr) |
11b4899f JM |
669 | or else |
670 | (Is_Entity_Name (Expression (AC)) | |
671 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
80007176 AC |
672 | and then Nkind (Parent (Entity (Expression (AC)))) = |
673 | N_Object_Declaration) | |
c064e066 RD |
674 | then |
675 | Expr := New_Copy_Tree (Expression (AC)); | |
676 | else | |
677 | Remove_Side_Effects (Expr); | |
980f237d | 678 | end if; |
980f237d | 679 | |
6f5c2c4b AC |
680 | if No (Actions (N)) then |
681 | Set_Actions (N, New_List); | |
682 | end if; | |
683 | ||
684 | Prepend_To (Actions (N), | |
c064e066 RD |
685 | Make_Raise_Program_Error (Loc, |
686 | Condition => | |
687 | Make_Op_Ne (Loc, | |
80007176 | 688 | Left_Opnd => |
c064e066 | 689 | Make_Op_Mod (Loc, |
80007176 | 690 | Left_Opnd => |
c064e066 RD |
691 | Unchecked_Convert_To |
692 | (RTE (RE_Integer_Address), Expr), | |
693 | Right_Opnd => | |
694 | Make_Attribute_Reference (Loc, | |
6f5c2c4b | 695 | Prefix => New_Occurrence_Of (E, Loc), |
c064e066 RD |
696 | Attribute_Name => Name_Alignment)), |
697 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
80007176 | 698 | Reason => PE_Misaligned_Address_Value)); |
aca670a0 AC |
699 | |
700 | Warning_Msg := No_Error_Msg; | |
6f5c2c4b | 701 | Analyze (First (Actions (N)), Suppress => All_Checks); |
3b4598a7 | 702 | |
f26a3587 | 703 | -- If the above raise action generated a warning message (for example |
aca670a0 AC |
704 | -- from Warn_On_Non_Local_Exception mode with the active restriction |
705 | -- No_Exception_Propagation). | |
706 | ||
707 | if Warning_Msg /= No_Error_Msg then | |
80007176 | 708 | |
aca670a0 AC |
709 | -- If the expression has a known at compile time value, then |
710 | -- once we know the alignment of the type, we can check if the | |
711 | -- exception will be raised or not, and if not, we don't need | |
712 | -- the warning so we will kill the warning later on. | |
713 | ||
714 | if Compile_Time_Known_Value (Expr) then | |
715 | Alignment_Warnings.Append | |
5c13a04e EB |
716 | ((E => E, |
717 | A => Expr_Value (Expr), | |
718 | P => Empty, | |
719 | W => Warning_Msg)); | |
720 | ||
721 | -- Likewise if the expression is of the form X'Address | |
722 | ||
723 | elsif Nkind (Expr) = N_Attribute_Reference | |
724 | and then Attribute_Name (Expr) = Name_Address | |
725 | then | |
726 | Alignment_Warnings.Append | |
727 | ((E => E, | |
728 | A => No_Uint, | |
729 | P => Prefix (Expr), | |
730 | W => Warning_Msg)); | |
3b4598a7 | 731 | |
80007176 AC |
732 | -- Add explanation of the warning generated by the check |
733 | ||
734 | else | |
f26a3587 | 735 | Error_Msg_N |
80007176 | 736 | ("\address value may be incompatible with alignment of " |
ed00b051 | 737 | & "object?.x?", AC); |
f26a3587 | 738 | end if; |
3b4598a7 | 739 | end if; |
6fd0a72a | 740 | |
c064e066 RD |
741 | return; |
742 | end if; | |
fbf5a39b AC |
743 | |
744 | exception | |
80007176 | 745 | |
c064e066 RD |
746 | -- If we have some missing run time component in configurable run time |
747 | -- mode then just skip the check (it is not required in any case). | |
748 | ||
fbf5a39b AC |
749 | when RE_Not_Available => |
750 | return; | |
c064e066 | 751 | end Apply_Address_Clause_Check; |
980f237d | 752 | |
70482933 RK |
753 | ------------------------------------- |
754 | -- Apply_Arithmetic_Overflow_Check -- | |
755 | ------------------------------------- | |
756 | ||
acad3c0a AC |
757 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
758 | begin | |
759 | -- Use old routine in almost all cases (the only case we are treating | |
5707e389 | 760 | -- specially is the case of a signed integer arithmetic op with the |
a7f1b24f | 761 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
acad3c0a | 762 | |
a7f1b24f | 763 | if Overflow_Check_Mode = Strict |
acad3c0a AC |
764 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
765 | then | |
a7f1b24f | 766 | Apply_Arithmetic_Overflow_Strict (N); |
acad3c0a | 767 | |
5707e389 AC |
768 | -- Otherwise use the new routine for the case of a signed integer |
769 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
770 | -- mode is MINIMIZED or ELIMINATED. | |
acad3c0a AC |
771 | |
772 | else | |
773 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
774 | end if; | |
775 | end Apply_Arithmetic_Overflow_Check; | |
776 | ||
a7f1b24f RD |
777 | -------------------------------------- |
778 | -- Apply_Arithmetic_Overflow_Strict -- | |
779 | -------------------------------------- | |
acad3c0a | 780 | |
13230c68 AC |
781 | -- This routine is called only if the type is an integer type and an |
782 | -- arithmetic overflow check may be needed for op (add, subtract, or | |
783 | -- multiply). This check is performed if Backend_Overflow_Checks_On_Target | |
784 | -- is not enabled and Do_Overflow_Check is set. In this case we expand the | |
ec2dd67a RD |
785 | -- operation into a more complex sequence of tests that ensures that |
786 | -- overflow is properly caught. | |
70482933 | 787 | |
a7f1b24f RD |
788 | -- This is used in CHECKED modes. It is identical to the code for this |
789 | -- cases before the big overflow earthquake, thus ensuring that in this | |
790 | -- modes we have compatible behavior (and reliability) to what was there | |
791 | -- before. It is also called for types other than signed integers, and if | |
792 | -- the Do_Overflow_Check flag is off. | |
acad3c0a AC |
793 | |
794 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
795 | -- to give up and just generate an overflow check without any fuss. | |
796 | ||
a7f1b24f | 797 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
5707e389 AC |
798 | Loc : constant Source_Ptr := Sloc (N); |
799 | Typ : constant Entity_Id := Etype (N); | |
800 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
70482933 RK |
801 | |
802 | begin | |
a7f1b24f RD |
803 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
804 | -- suppressed. | |
805 | ||
806 | if not Do_Overflow_Check (N) then | |
807 | return; | |
808 | end if; | |
809 | ||
ec2dd67a RD |
810 | -- An interesting special case. If the arithmetic operation appears as |
811 | -- the operand of a type conversion: | |
812 | ||
813 | -- type1 (x op y) | |
814 | ||
815 | -- and all the following conditions apply: | |
816 | ||
817 | -- arithmetic operation is for a signed integer type | |
818 | -- target type type1 is a static integer subtype | |
819 | -- range of x and y are both included in the range of type1 | |
820 | -- range of x op y is included in the range of type1 | |
821 | -- size of type1 is at least twice the result size of op | |
822 | ||
4404c282 | 823 | -- then we don't do an overflow check in any case. Instead, we transform |
ec2dd67a RD |
824 | -- the operation so that we end up with: |
825 | ||
826 | -- type1 (type1 (x) op type1 (y)) | |
827 | ||
828 | -- This avoids intermediate overflow before the conversion. It is | |
829 | -- explicitly permitted by RM 3.5.4(24): | |
830 | ||
831 | -- For the execution of a predefined operation of a signed integer | |
832 | -- type, the implementation need not raise Constraint_Error if the | |
833 | -- result is outside the base range of the type, so long as the | |
834 | -- correct result is produced. | |
835 | ||
836 | -- It's hard to imagine that any programmer counts on the exception | |
837 | -- being raised in this case, and in any case it's wrong coding to | |
838 | -- have this expectation, given the RM permission. Furthermore, other | |
839 | -- Ada compilers do allow such out of range results. | |
840 | ||
841 | -- Note that we do this transformation even if overflow checking is | |
842 | -- off, since this is precisely about giving the "right" result and | |
843 | -- avoiding the need for an overflow check. | |
844 | ||
eaa826f8 RD |
845 | -- Note: this circuit is partially redundant with respect to the similar |
846 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
847 | -- with cases that do not come through here. We still need the following | |
848 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
849 | -- sure not to generate the arithmetic overflow check in these cases | |
850 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
851 | ||
ec2dd67a RD |
852 | if Is_Signed_Integer_Type (Typ) |
853 | and then Nkind (Parent (N)) = N_Type_Conversion | |
70482933 | 854 | then |
b6b5cca8 | 855 | Conversion_Optimization : declare |
ec2dd67a | 856 | Target_Type : constant Entity_Id := |
15f0f591 | 857 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
ec2dd67a RD |
858 | |
859 | Llo, Lhi : Uint; | |
860 | Rlo, Rhi : Uint; | |
861 | LOK, ROK : Boolean; | |
862 | ||
863 | Vlo : Uint; | |
864 | Vhi : Uint; | |
865 | VOK : Boolean; | |
866 | ||
867 | Tlo : Uint; | |
868 | Thi : Uint; | |
869 | ||
870 | begin | |
871 | if Is_Integer_Type (Target_Type) | |
872 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
873 | then | |
874 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
875 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
876 | ||
c800f862 RD |
877 | Determine_Range |
878 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
879 | Determine_Range | |
880 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
ec2dd67a RD |
881 | |
882 | if (LOK and ROK) | |
883 | and then Tlo <= Llo and then Lhi <= Thi | |
884 | and then Tlo <= Rlo and then Rhi <= Thi | |
885 | then | |
c800f862 | 886 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
ec2dd67a RD |
887 | |
888 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
4fb0b3f0 AC |
889 | -- Rewrite the conversion operand so that the original |
890 | -- node is retained, in order to avoid the warning for | |
891 | -- redundant conversions in Resolve_Type_Conversion. | |
892 | ||
03b4e4ae PT |
893 | declare |
894 | Op : constant Node_Id := New_Op_Node (Nkind (N), Loc); | |
895 | begin | |
896 | Set_Left_Opnd (Op, | |
897 | Make_Type_Conversion (Loc, | |
898 | Subtype_Mark => | |
899 | New_Occurrence_Of (Target_Type, Loc), | |
900 | Expression => Relocate_Node (Left_Opnd (N)))); | |
901 | Set_Right_Opnd (Op, | |
902 | Make_Type_Conversion (Loc, | |
903 | Subtype_Mark => | |
904 | New_Occurrence_Of (Target_Type, Loc), | |
905 | Expression => Relocate_Node (Right_Opnd (N)))); | |
906 | ||
907 | Rewrite (N, Op); | |
908 | end; | |
4fb0b3f0 | 909 | |
ec2dd67a | 910 | Set_Etype (N, Target_Type); |
4fb0b3f0 | 911 | |
ec2dd67a RD |
912 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
913 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
914 | ||
915 | -- Given that the target type is twice the size of the | |
916 | -- source type, overflow is now impossible, so we can | |
917 | -- safely kill the overflow check and return. | |
918 | ||
919 | Set_Do_Overflow_Check (N, False); | |
920 | return; | |
921 | end if; | |
922 | end if; | |
923 | end if; | |
b6b5cca8 | 924 | end Conversion_Optimization; |
70482933 RK |
925 | end if; |
926 | ||
ec2dd67a RD |
927 | -- Now see if an overflow check is required |
928 | ||
929 | declare | |
a5476382 | 930 | Dsiz : constant Uint := 2 * Esize (Rtyp); |
ec2dd67a RD |
931 | Opnod : Node_Id; |
932 | Ctyp : Entity_Id; | |
933 | Opnd : Node_Id; | |
934 | Cent : RE_Id; | |
70482933 | 935 | |
ec2dd67a RD |
936 | begin |
937 | -- Skip check if back end does overflow checks, or the overflow flag | |
fdfcc663 AC |
938 | -- is not set anyway, or we are not doing code expansion, or the |
939 | -- parent node is a type conversion whose operand is an arithmetic | |
940 | -- operation on signed integers on which the expander can promote | |
0c0c6f49 | 941 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
70482933 | 942 | |
ec2dd67a RD |
943 | if Backend_Overflow_Checks_On_Target |
944 | or else not Do_Overflow_Check (N) | |
4460a9bc | 945 | or else not Expander_Active |
fdfcc663 AC |
946 | or else (Present (Parent (N)) |
947 | and then Nkind (Parent (N)) = N_Type_Conversion | |
948 | and then Integer_Promotion_Possible (Parent (N))) | |
ec2dd67a RD |
949 | then |
950 | return; | |
951 | end if; | |
70482933 | 952 | |
ec2dd67a RD |
953 | -- Otherwise, generate the full general code for front end overflow |
954 | -- detection, which works by doing arithmetic in a larger type: | |
70482933 | 955 | |
ec2dd67a | 956 | -- x op y |
70482933 | 957 | |
ec2dd67a | 958 | -- is expanded into |
70482933 | 959 | |
ec2dd67a | 960 | -- Typ (Checktyp (x) op Checktyp (y)); |
70482933 | 961 | |
ec2dd67a RD |
962 | -- where Typ is the type of the original expression, and Checktyp is |
963 | -- an integer type of sufficient length to hold the largest possible | |
964 | -- result. | |
70482933 | 965 | |
a5476382 | 966 | -- If the size of the check type exceeds the maximum integer size, |
ec2dd67a | 967 | -- we use a different approach, expanding to: |
70482933 | 968 | |
a5476382 | 969 | -- typ (xxx_With_Ovflo_Check (Integer_NN (x), Integer_NN (y))) |
70482933 | 970 | |
ec2dd67a | 971 | -- where xxx is Add, Multiply or Subtract as appropriate |
70482933 | 972 | |
ec2dd67a RD |
973 | -- Find check type if one exists |
974 | ||
a5476382 EB |
975 | if Dsiz <= System_Max_Integer_Size then |
976 | Ctyp := Integer_Type_For (Dsiz, Uns => False); | |
ec2dd67a | 977 | |
ce532f42 | 978 | -- No check type exists, use runtime call |
70482933 RK |
979 | |
980 | else | |
a5476382 EB |
981 | if System_Max_Integer_Size = 64 then |
982 | Ctyp := RTE (RE_Integer_64); | |
983 | else | |
984 | Ctyp := RTE (RE_Integer_128); | |
985 | end if; | |
986 | ||
ec2dd67a | 987 | if Nkind (N) = N_Op_Add then |
a5476382 EB |
988 | if System_Max_Integer_Size = 64 then |
989 | Cent := RE_Add_With_Ovflo_Check64; | |
990 | else | |
991 | Cent := RE_Add_With_Ovflo_Check128; | |
992 | end if; | |
70482933 | 993 | |
a5476382 EB |
994 | elsif Nkind (N) = N_Op_Subtract then |
995 | if System_Max_Integer_Size = 64 then | |
996 | Cent := RE_Subtract_With_Ovflo_Check64; | |
997 | else | |
998 | Cent := RE_Subtract_With_Ovflo_Check128; | |
999 | end if; | |
70482933 | 1000 | |
a5476382 EB |
1001 | else pragma Assert (Nkind (N) = N_Op_Multiply); |
1002 | if System_Max_Integer_Size = 64 then | |
1003 | Cent := RE_Multiply_With_Ovflo_Check64; | |
1004 | else | |
1005 | Cent := RE_Multiply_With_Ovflo_Check128; | |
1006 | end if; | |
ec2dd67a RD |
1007 | end if; |
1008 | ||
1009 | Rewrite (N, | |
1010 | OK_Convert_To (Typ, | |
1011 | Make_Function_Call (Loc, | |
e4494292 | 1012 | Name => New_Occurrence_Of (RTE (Cent), Loc), |
ec2dd67a | 1013 | Parameter_Associations => New_List ( |
a5476382 EB |
1014 | OK_Convert_To (Ctyp, Left_Opnd (N)), |
1015 | OK_Convert_To (Ctyp, Right_Opnd (N)))))); | |
70482933 | 1016 | |
ec2dd67a RD |
1017 | Analyze_And_Resolve (N, Typ); |
1018 | return; | |
1019 | end if; | |
70482933 | 1020 | |
ec2dd67a RD |
1021 | -- If we fall through, we have the case where we do the arithmetic |
1022 | -- in the next higher type and get the check by conversion. In these | |
1023 | -- cases Ctyp is set to the type to be used as the check type. | |
70482933 | 1024 | |
ec2dd67a | 1025 | Opnod := Relocate_Node (N); |
70482933 | 1026 | |
ec2dd67a | 1027 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
70482933 | 1028 | |
ec2dd67a RD |
1029 | Analyze (Opnd); |
1030 | Set_Etype (Opnd, Ctyp); | |
1031 | Set_Analyzed (Opnd, True); | |
1032 | Set_Left_Opnd (Opnod, Opnd); | |
70482933 | 1033 | |
ec2dd67a | 1034 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
70482933 | 1035 | |
ec2dd67a RD |
1036 | Analyze (Opnd); |
1037 | Set_Etype (Opnd, Ctyp); | |
1038 | Set_Analyzed (Opnd, True); | |
1039 | Set_Right_Opnd (Opnod, Opnd); | |
70482933 | 1040 | |
ec2dd67a RD |
1041 | -- The type of the operation changes to the base type of the check |
1042 | -- type, and we reset the overflow check indication, since clearly no | |
1043 | -- overflow is possible now that we are using a double length type. | |
1044 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1045 | -- expand the node. | |
70482933 | 1046 | |
ec2dd67a RD |
1047 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1048 | Set_Do_Overflow_Check (Opnod, False); | |
1049 | Set_Analyzed (Opnod, True); | |
70482933 | 1050 | |
ec2dd67a | 1051 | -- Now build the outer conversion |
70482933 | 1052 | |
ec2dd67a RD |
1053 | Opnd := OK_Convert_To (Typ, Opnod); |
1054 | Analyze (Opnd); | |
1055 | Set_Etype (Opnd, Typ); | |
fbf5a39b | 1056 | |
ec2dd67a RD |
1057 | -- In the discrete type case, we directly generate the range check |
1058 | -- for the outer operand. This range check will implement the | |
1059 | -- required overflow check. | |
fbf5a39b | 1060 | |
ec2dd67a RD |
1061 | if Is_Discrete_Type (Typ) then |
1062 | Rewrite (N, Opnd); | |
1063 | Generate_Range_Check | |
1064 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
fbf5a39b | 1065 | |
ec2dd67a RD |
1066 | -- For other types, we enable overflow checking on the conversion, |
1067 | -- after setting the node as analyzed to prevent recursive attempts | |
1068 | -- to expand the conversion node. | |
fbf5a39b | 1069 | |
ec2dd67a RD |
1070 | else |
1071 | Set_Analyzed (Opnd, True); | |
1072 | Enable_Overflow_Check (Opnd); | |
1073 | Rewrite (N, Opnd); | |
1074 | end if; | |
1075 | ||
1076 | exception | |
1077 | when RE_Not_Available => | |
1078 | return; | |
1079 | end; | |
a7f1b24f | 1080 | end Apply_Arithmetic_Overflow_Strict; |
acad3c0a AC |
1081 | |
1082 | ---------------------------------------------------- | |
1083 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1084 | ---------------------------------------------------- | |
1085 | ||
1086 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1087 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
acad3c0a AC |
1088 | |
1089 | Loc : constant Source_Ptr := Sloc (Op); | |
1090 | P : constant Node_Id := Parent (Op); | |
1091 | ||
d79059a3 AC |
1092 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1093 | -- Operands and results are of this type when we convert | |
1094 | ||
acad3c0a AC |
1095 | Result_Type : constant Entity_Id := Etype (Op); |
1096 | -- Original result type | |
1097 | ||
15c94a55 | 1098 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
acad3c0a AC |
1099 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1100 | ||
1101 | Lo, Hi : Uint; | |
1102 | -- Ranges of values for result | |
1103 | ||
1104 | begin | |
1105 | -- Nothing to do if our parent is one of the following: | |
1106 | ||
4b1c4f20 | 1107 | -- Another signed integer arithmetic op |
acad3c0a AC |
1108 | -- A membership operation |
1109 | -- A comparison operation | |
1110 | ||
1111 | -- In all these cases, we will process at the higher level (and then | |
1112 | -- this node will be processed during the downwards recursion that | |
a7f1b24f | 1113 | -- is part of the processing in Minimize_Eliminate_Overflows). |
acad3c0a AC |
1114 | |
1115 | if Is_Signed_Integer_Arithmetic_Op (P) | |
71fb4dc8 AC |
1116 | or else Nkind (P) in N_Membership_Test |
1117 | or else Nkind (P) in N_Op_Compare | |
f6194278 | 1118 | |
f6636994 AC |
1119 | -- This is also true for an alternative in a case expression |
1120 | ||
1121 | or else Nkind (P) = N_Case_Expression_Alternative | |
1122 | ||
1123 | -- This is also true for a range operand in a membership test | |
f6194278 | 1124 | |
71fb4dc8 AC |
1125 | or else (Nkind (P) = N_Range |
1126 | and then Nkind (Parent (P)) in N_Membership_Test) | |
acad3c0a | 1127 | then |
fdc54be6 AC |
1128 | -- If_Expressions and Case_Expressions are treated as arithmetic |
1129 | -- ops, but if they appear in an assignment or similar contexts | |
1130 | -- there is no overflow check that starts from that parent node, | |
1131 | -- so apply check now. | |
ae745a0d | 1132 | -- Similarly, if these expressions are nested, we should go on. |
fdc54be6 | 1133 | |
4a08c95c | 1134 | if Nkind (P) in N_If_Expression | N_Case_Expression |
fdc54be6 AC |
1135 | and then not Is_Signed_Integer_Arithmetic_Op (Parent (P)) |
1136 | then | |
1137 | null; | |
ae745a0d ES |
1138 | elsif Nkind (P) in N_If_Expression | N_Case_Expression |
1139 | and then Nkind (Op) in N_If_Expression | N_Case_Expression | |
1140 | then | |
1141 | null; | |
fdc54be6 AC |
1142 | else |
1143 | return; | |
1144 | end if; | |
acad3c0a AC |
1145 | end if; |
1146 | ||
4b1c4f20 | 1147 | -- Otherwise, we have a top level arithmetic operation node, and this |
5707e389 AC |
1148 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1149 | -- modes. This is the case where we tell the machinery not to move into | |
1150 | -- Bignum mode at this top level (of course the top level operation | |
1151 | -- will still be in Bignum mode if either of its operands are of type | |
1152 | -- Bignum). | |
acad3c0a | 1153 | |
a7f1b24f | 1154 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
acad3c0a AC |
1155 | |
1156 | -- That call may but does not necessarily change the result type of Op. | |
1157 | -- It is the job of this routine to undo such changes, so that at the | |
1158 | -- top level, we have the proper type. This "undoing" is a point at | |
1159 | -- which a final overflow check may be applied. | |
1160 | ||
b6b5cca8 AC |
1161 | -- If the result type was not fiddled we are all set. We go to base |
1162 | -- types here because things may have been rewritten to generate the | |
1163 | -- base type of the operand types. | |
acad3c0a | 1164 | |
b6b5cca8 | 1165 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
acad3c0a AC |
1166 | return; |
1167 | ||
1168 | -- Bignum case | |
1169 | ||
d79059a3 | 1170 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
acad3c0a | 1171 | |
456cbfa5 | 1172 | -- We need a sequence that looks like: |
acad3c0a AC |
1173 | |
1174 | -- Rnn : Result_Type; | |
1175 | ||
1176 | -- declare | |
456cbfa5 | 1177 | -- M : Mark_Id := SS_Mark; |
acad3c0a | 1178 | -- begin |
d79059a3 | 1179 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
acad3c0a AC |
1180 | -- SS_Release (M); |
1181 | -- end; | |
1182 | ||
1183 | -- This block is inserted (using Insert_Actions), and then the node | |
1184 | -- is replaced with a reference to Rnn. | |
1185 | ||
e645cb39 | 1186 | -- If our parent is a conversion node then there is no point in |
4404c282 | 1187 | -- generating a conversion to Result_Type. Instead, we let the parent |
e645cb39 AC |
1188 | -- handle this. Note that this special case is not just about |
1189 | -- optimization. Consider | |
acad3c0a AC |
1190 | |
1191 | -- A,B,C : Integer; | |
1192 | -- ... | |
d79059a3 | 1193 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
acad3c0a AC |
1194 | |
1195 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
5707e389 AC |
1196 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1197 | -- overflow exception for this intermediate value. | |
acad3c0a AC |
1198 | |
1199 | declare | |
d79059a3 | 1200 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
acad3c0a AC |
1201 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1202 | RHS : Node_Id; | |
1203 | ||
1204 | Rtype : Entity_Id; | |
1205 | ||
1206 | begin | |
1207 | RHS := Convert_From_Bignum (Op); | |
1208 | ||
1209 | if Nkind (P) /= N_Type_Conversion then | |
d79059a3 | 1210 | Convert_To_And_Rewrite (Result_Type, RHS); |
acad3c0a AC |
1211 | Rtype := Result_Type; |
1212 | ||
1213 | -- Interesting question, do we need a check on that conversion | |
1214 | -- operation. Answer, not if we know the result is in range. | |
1215 | -- At the moment we are not taking advantage of this. To be | |
1216 | -- looked at later ??? | |
1217 | ||
1218 | else | |
d79059a3 | 1219 | Rtype := LLIB; |
acad3c0a AC |
1220 | end if; |
1221 | ||
1222 | Insert_Before | |
1223 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1224 | Make_Assignment_Statement (Loc, | |
1225 | Name => New_Occurrence_Of (Rnn, Loc), | |
1226 | Expression => RHS)); | |
1227 | ||
1228 | Insert_Actions (Op, New_List ( | |
1229 | Make_Object_Declaration (Loc, | |
1230 | Defining_Identifier => Rnn, | |
1231 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1232 | Blk)); | |
1233 | ||
1234 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1235 | Analyze_And_Resolve (Op); | |
1236 | end; | |
1237 | ||
fdc54be6 | 1238 | -- Here we know the result is Long_Long_Integer'Base, or that it has |
60b68e56 | 1239 | -- been rewritten because the parent operation is a conversion. See |
a7f1b24f | 1240 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
acad3c0a AC |
1241 | |
1242 | else | |
b6b5cca8 AC |
1243 | pragma Assert |
1244 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
acad3c0a AC |
1245 | |
1246 | -- All we need to do here is to convert the result to the proper | |
1247 | -- result type. As explained above for the Bignum case, we can | |
1248 | -- omit this if our parent is a type conversion. | |
1249 | ||
1250 | if Nkind (P) /= N_Type_Conversion then | |
1251 | Convert_To_And_Rewrite (Result_Type, Op); | |
1252 | end if; | |
1253 | ||
1254 | Analyze_And_Resolve (Op); | |
1255 | end if; | |
1256 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
70482933 | 1257 | |
70482933 RK |
1258 | ---------------------------- |
1259 | -- Apply_Constraint_Check -- | |
1260 | ---------------------------- | |
1261 | ||
1262 | procedure Apply_Constraint_Check | |
1263 | (N : Node_Id; | |
1264 | Typ : Entity_Id; | |
1265 | No_Sliding : Boolean := False) | |
1266 | is | |
1267 | Desig_Typ : Entity_Id; | |
1268 | ||
1269 | begin | |
48f91b44 RD |
1270 | -- No checks inside a generic (check the instantiations) |
1271 | ||
70482933 RK |
1272 | if Inside_A_Generic then |
1273 | return; | |
48f91b44 | 1274 | end if; |
70482933 | 1275 | |
308e6f3a | 1276 | -- Apply required constraint checks |
48f91b44 RD |
1277 | |
1278 | if Is_Scalar_Type (Typ) then | |
70482933 RK |
1279 | Apply_Scalar_Range_Check (N, Typ); |
1280 | ||
1281 | elsif Is_Array_Type (Typ) then | |
1282 | ||
d8b9660d | 1283 | -- A useful optimization: an aggregate with only an others clause |
c84700e7 ES |
1284 | -- always has the right bounds. |
1285 | ||
1286 | if Nkind (N) = N_Aggregate | |
1287 | and then No (Expressions (N)) | |
51ad879c PT |
1288 | and then Nkind (First (Component_Associations (N))) = |
1289 | N_Component_Association | |
c84700e7 ES |
1290 | and then Nkind |
1291 | (First (Choices (First (Component_Associations (N))))) | |
1292 | = N_Others_Choice | |
1293 | then | |
1294 | return; | |
1295 | end if; | |
1296 | ||
70482933 RK |
1297 | if Is_Constrained (Typ) then |
1298 | Apply_Length_Check (N, Typ); | |
1299 | ||
1300 | if No_Sliding then | |
1301 | Apply_Range_Check (N, Typ); | |
1302 | end if; | |
1303 | else | |
1304 | Apply_Range_Check (N, Typ); | |
1305 | end if; | |
1306 | ||
a40ada7e | 1307 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
70482933 RK |
1308 | and then Has_Discriminants (Base_Type (Typ)) |
1309 | and then Is_Constrained (Typ) | |
1310 | then | |
1311 | Apply_Discriminant_Check (N, Typ); | |
1312 | ||
1313 | elsif Is_Access_Type (Typ) then | |
1314 | ||
1315 | Desig_Typ := Designated_Type (Typ); | |
1316 | ||
1317 | -- No checks necessary if expression statically null | |
1318 | ||
939c12d2 | 1319 | if Known_Null (N) then |
11b4899f JM |
1320 | if Can_Never_Be_Null (Typ) then |
1321 | Install_Null_Excluding_Check (N); | |
1322 | end if; | |
70482933 RK |
1323 | |
1324 | -- No sliding possible on access to arrays | |
1325 | ||
1326 | elsif Is_Array_Type (Desig_Typ) then | |
1327 | if Is_Constrained (Desig_Typ) then | |
1328 | Apply_Length_Check (N, Typ); | |
1329 | end if; | |
1330 | ||
1331 | Apply_Range_Check (N, Typ); | |
1332 | ||
99bba92c AC |
1333 | -- Do not install a discriminant check for a constrained subtype |
1334 | -- created for an unconstrained nominal type because the subtype | |
1335 | -- has the correct constraints by construction. | |
1336 | ||
70482933 | 1337 | elsif Has_Discriminants (Base_Type (Desig_Typ)) |
99bba92c AC |
1338 | and then Is_Constrained (Desig_Typ) |
1339 | and then not Is_Constr_Subt_For_U_Nominal (Desig_Typ) | |
70482933 RK |
1340 | then |
1341 | Apply_Discriminant_Check (N, Typ); | |
1342 | end if; | |
2820d220 | 1343 | |
16b05213 | 1344 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
11b4899f JM |
1345 | -- this check if the constraint node is illegal, as shown by having |
1346 | -- an error posted. This additional guard prevents cascaded errors | |
1347 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1348 | ||
2820d220 AC |
1349 | if Can_Never_Be_Null (Typ) |
1350 | and then not Can_Never_Be_Null (Etype (N)) | |
11b4899f | 1351 | and then not Error_Posted (N) |
2820d220 AC |
1352 | then |
1353 | Install_Null_Excluding_Check (N); | |
1354 | end if; | |
70482933 RK |
1355 | end if; |
1356 | end Apply_Constraint_Check; | |
1357 | ||
1358 | ------------------------------ | |
1359 | -- Apply_Discriminant_Check -- | |
1360 | ------------------------------ | |
1361 | ||
1362 | procedure Apply_Discriminant_Check | |
1363 | (N : Node_Id; | |
1364 | Typ : Entity_Id; | |
1365 | Lhs : Node_Id := Empty) | |
1366 | is | |
1367 | Loc : constant Source_Ptr := Sloc (N); | |
1368 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1369 | S_Typ : Entity_Id := Etype (N); | |
1370 | Cond : Node_Id; | |
1371 | T_Typ : Entity_Id; | |
1372 | ||
438ff97c ES |
1373 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1374 | -- A heap object with an indefinite subtype is constrained by its | |
1375 | -- initial value, and assigning to it requires a constraint_check. | |
1376 | -- The target may be an explicit dereference, or a renaming of one. | |
1377 | ||
70482933 RK |
1378 | function Is_Aliased_Unconstrained_Component return Boolean; |
1379 | -- It is possible for an aliased component to have a nominal | |
1380 | -- unconstrained subtype (through instantiation). If this is a | |
1381 | -- discriminated component assigned in the expansion of an aggregate | |
1382 | -- in an initialization, the check must be suppressed. This unusual | |
939c12d2 | 1383 | -- situation requires a predicate of its own. |
70482933 | 1384 | |
438ff97c ES |
1385 | ---------------------------------- |
1386 | -- Denotes_Explicit_Dereference -- | |
1387 | ---------------------------------- | |
1388 | ||
1389 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1390 | begin | |
32841e7e EB |
1391 | if Is_Entity_Name (Obj) then |
1392 | return Present (Renamed_Object (Entity (Obj))) | |
1393 | and then | |
1394 | Denotes_Explicit_Dereference (Renamed_Object (Entity (Obj))); | |
1395 | ||
1396 | -- This routine uses the rules of the language so we need to exclude | |
1397 | -- rewritten constructs that introduce artificial dereferences. | |
1398 | ||
1399 | elsif Nkind (Obj) = N_Explicit_Dereference then | |
1400 | return not Is_Captured_Function_Call (Obj) | |
1401 | and then not | |
1402 | (Nkind (Parent (Obj)) = N_Object_Renaming_Declaration | |
1403 | and then Is_Return_Object (Defining_Entity (Parent (Obj)))); | |
1404 | ||
1405 | else | |
1406 | return False; | |
1407 | end if; | |
438ff97c ES |
1408 | end Denotes_Explicit_Dereference; |
1409 | ||
70482933 RK |
1410 | ---------------------------------------- |
1411 | -- Is_Aliased_Unconstrained_Component -- | |
1412 | ---------------------------------------- | |
1413 | ||
1414 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1415 | Comp : Entity_Id; | |
1416 | Pref : Node_Id; | |
1417 | ||
1418 | begin | |
1419 | if Nkind (Lhs) /= N_Selected_Component then | |
1420 | return False; | |
1421 | else | |
1422 | Comp := Entity (Selector_Name (Lhs)); | |
1423 | Pref := Prefix (Lhs); | |
1424 | end if; | |
1425 | ||
1426 | if Ekind (Comp) /= E_Component | |
1427 | or else not Is_Aliased (Comp) | |
1428 | then | |
1429 | return False; | |
1430 | end if; | |
1431 | ||
1432 | return not Comes_From_Source (Pref) | |
1433 | and then In_Instance | |
1434 | and then not Is_Constrained (Etype (Comp)); | |
1435 | end Is_Aliased_Unconstrained_Component; | |
1436 | ||
1437 | -- Start of processing for Apply_Discriminant_Check | |
1438 | ||
1439 | begin | |
1440 | if Do_Access then | |
1441 | T_Typ := Designated_Type (Typ); | |
1442 | else | |
1443 | T_Typ := Typ; | |
1444 | end if; | |
1445 | ||
6ae40af3 ES |
1446 | -- If the expression is a function call that returns a limited object |
1447 | -- it cannot be copied. It is not clear how to perform the proper | |
1448 | -- discriminant check in this case because the discriminant value must | |
1449 | -- be retrieved from the constructed object itself. | |
1450 | ||
1451 | if Nkind (N) = N_Function_Call | |
1452 | and then Is_Limited_Type (Typ) | |
1453 | and then Is_Entity_Name (Name (N)) | |
1454 | and then Returns_By_Ref (Entity (Name (N))) | |
1455 | then | |
1456 | return; | |
1457 | end if; | |
1458 | ||
27bb7941 AC |
1459 | -- Only apply checks when generating code and discriminant checks are |
1460 | -- not suppressed. In GNATprove mode, we do not apply the checks, but we | |
1461 | -- still analyze the expression to possibly issue errors on SPARK code | |
1462 | -- when a run-time error can be detected at compile time. | |
1463 | ||
1464 | if not GNATprove_Mode then | |
1465 | if not Expander_Active | |
1466 | or else Discriminant_Checks_Suppressed (T_Typ) | |
1467 | then | |
1468 | return; | |
1469 | end if; | |
70482933 RK |
1470 | end if; |
1471 | ||
675d6070 TQ |
1472 | -- No discriminant checks necessary for an access when expression is |
1473 | -- statically Null. This is not only an optimization, it is fundamental | |
1474 | -- because otherwise discriminant checks may be generated in init procs | |
1475 | -- for types containing an access to a not-yet-frozen record, causing a | |
1476 | -- deadly forward reference. | |
70482933 | 1477 | |
675d6070 TQ |
1478 | -- Also, if the expression is of an access type whose designated type is |
1479 | -- incomplete, then the access value must be null and we suppress the | |
1480 | -- check. | |
70482933 | 1481 | |
939c12d2 | 1482 | if Known_Null (N) then |
70482933 RK |
1483 | return; |
1484 | ||
1485 | elsif Is_Access_Type (S_Typ) then | |
1486 | S_Typ := Designated_Type (S_Typ); | |
1487 | ||
1488 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1489 | return; | |
1490 | end if; | |
1491 | end if; | |
1492 | ||
c064e066 RD |
1493 | -- If an assignment target is present, then we need to generate the |
1494 | -- actual subtype if the target is a parameter or aliased object with | |
1495 | -- an unconstrained nominal subtype. | |
1496 | ||
1497 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1498 | -- subtype to the parameter and dereference cases, since other aliased | |
1499 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
438ff97c | 1500 | -- constrained). |
70482933 RK |
1501 | |
1502 | if Present (Lhs) | |
1503 | and then (Present (Param_Entity (Lhs)) | |
0791fbe9 | 1504 | or else (Ada_Version < Ada_2005 |
c064e066 | 1505 | and then not Is_Constrained (T_Typ) |
70482933 | 1506 | and then Is_Aliased_View (Lhs) |
c064e066 | 1507 | and then not Is_Aliased_Unconstrained_Component) |
0791fbe9 | 1508 | or else (Ada_Version >= Ada_2005 |
c064e066 | 1509 | and then not Is_Constrained (T_Typ) |
32841e7e | 1510 | and then Denotes_Explicit_Dereference (Lhs))) |
70482933 RK |
1511 | then |
1512 | T_Typ := Get_Actual_Subtype (Lhs); | |
1513 | end if; | |
1514 | ||
675d6070 TQ |
1515 | -- Nothing to do if the type is unconstrained (this is the case where |
1516 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1517 | -- is required). | |
70482933 RK |
1518 | |
1519 | if not Is_Constrained (T_Typ) then | |
1520 | return; | |
d8b9660d ES |
1521 | |
1522 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1523 | -- partial view that is constrained. | |
1524 | ||
0791fbe9 | 1525 | elsif Ada_Version >= Ada_2005 |
0fbcb11c | 1526 | and then Object_Type_Has_Constrained_Partial_View |
414b312e AC |
1527 | (Typ => Base_Type (T_Typ), |
1528 | Scop => Current_Scope) | |
d8b9660d ES |
1529 | then |
1530 | return; | |
70482933 RK |
1531 | end if; |
1532 | ||
5d09245e AC |
1533 | -- Nothing to do if the type is an Unchecked_Union |
1534 | ||
1535 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1536 | return; | |
1537 | end if; | |
1538 | ||
6b6041ec | 1539 | -- Suppress checks if the subtypes are the same. The check must be |
675d6070 TQ |
1540 | -- preserved in an assignment to a formal, because the constraint is |
1541 | -- given by the actual. | |
70482933 RK |
1542 | |
1543 | if Nkind (Original_Node (N)) /= N_Allocator | |
1544 | and then (No (Lhs) | |
9972d439 RD |
1545 | or else not Is_Entity_Name (Lhs) |
1546 | or else No (Param_Entity (Lhs))) | |
70482933 RK |
1547 | then |
1548 | if (Etype (N) = Typ | |
1549 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1550 | and then not Is_Aliased_View (Lhs) | |
1551 | then | |
1552 | return; | |
1553 | end if; | |
1554 | ||
675d6070 TQ |
1555 | -- We can also eliminate checks on allocators with a subtype mark that |
1556 | -- coincides with the context type. The context type may be a subtype | |
1557 | -- without a constraint (common case, a generic actual). | |
70482933 RK |
1558 | |
1559 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1560 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1561 | then | |
1562 | declare | |
fbf5a39b | 1563 | Alloc_Typ : constant Entity_Id := |
15f0f591 | 1564 | Entity (Expression (Original_Node (N))); |
70482933 RK |
1565 | |
1566 | begin | |
1567 | if Alloc_Typ = T_Typ | |
1568 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1569 | and then Is_Entity_Name ( | |
1570 | Subtype_Indication (Parent (T_Typ))) | |
1571 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1572 | ||
1573 | then | |
1574 | return; | |
1575 | end if; | |
1576 | end; | |
1577 | end if; | |
1578 | ||
675d6070 TQ |
1579 | -- See if we have a case where the types are both constrained, and all |
1580 | -- the constraints are constants. In this case, we can do the check | |
1581 | -- successfully at compile time. | |
70482933 | 1582 | |
6b6041ec | 1583 | -- We skip this check for the case where the node is rewritten as |
c91dbd18 AC |
1584 | -- an allocator, because it already carries the context subtype, |
1585 | -- and extracting the discriminants from the aggregate is messy. | |
70482933 RK |
1586 | |
1587 | if Is_Constrained (S_Typ) | |
1588 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1589 | then | |
1590 | declare | |
1591 | DconT : Elmt_Id; | |
1592 | Discr : Entity_Id; | |
1593 | DconS : Elmt_Id; | |
1594 | ItemS : Node_Id; | |
1595 | ItemT : Node_Id; | |
1596 | ||
1597 | begin | |
1598 | -- S_Typ may not have discriminants in the case where it is a | |
675d6070 | 1599 | -- private type completed by a default discriminated type. In that |
6b6041ec | 1600 | -- case, we need to get the constraints from the underlying type. |
675d6070 TQ |
1601 | -- If the underlying type is unconstrained (i.e. has no default |
1602 | -- discriminants) no check is needed. | |
70482933 RK |
1603 | |
1604 | if Has_Discriminants (S_Typ) then | |
1605 | Discr := First_Discriminant (S_Typ); | |
1606 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1607 | ||
1608 | else | |
1609 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1610 | DconS := | |
1611 | First_Elmt | |
1612 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1613 | ||
1614 | if No (DconS) then | |
1615 | return; | |
1616 | end if; | |
65356e64 AC |
1617 | |
1618 | -- A further optimization: if T_Typ is derived from S_Typ | |
1619 | -- without imposing a constraint, no check is needed. | |
1620 | ||
1621 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1622 | N_Full_Type_Declaration | |
1623 | then | |
1624 | declare | |
91b1417d | 1625 | Type_Def : constant Node_Id := |
15f0f591 | 1626 | Type_Definition (Original_Node (Parent (T_Typ))); |
65356e64 AC |
1627 | begin |
1628 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1629 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1630 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1631 | then | |
1632 | return; | |
1633 | end if; | |
1634 | end; | |
1635 | end if; | |
70482933 RK |
1636 | end if; |
1637 | ||
d2a6bd6b AC |
1638 | -- Constraint may appear in full view of type |
1639 | ||
1640 | if Ekind (T_Typ) = E_Private_Subtype | |
1641 | and then Present (Full_View (T_Typ)) | |
1642 | then | |
c91dbd18 | 1643 | DconT := |
d2a6bd6b | 1644 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
d2a6bd6b | 1645 | else |
c91dbd18 AC |
1646 | DconT := |
1647 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
d2a6bd6b | 1648 | end if; |
70482933 RK |
1649 | |
1650 | while Present (Discr) loop | |
1651 | ItemS := Node (DconS); | |
1652 | ItemT := Node (DconT); | |
1653 | ||
11b4899f JM |
1654 | -- For a discriminated component type constrained by the |
1655 | -- current instance of an enclosing type, there is no | |
1656 | -- applicable discriminant check. | |
1657 | ||
1658 | if Nkind (ItemT) = N_Attribute_Reference | |
1659 | and then Is_Access_Type (Etype (ItemT)) | |
1660 | and then Is_Entity_Name (Prefix (ItemT)) | |
1661 | and then Is_Type (Entity (Prefix (ItemT))) | |
1662 | then | |
1663 | return; | |
1664 | end if; | |
1665 | ||
f2cbd970 | 1666 | -- If the expressions for the discriminants are identical |
e5eb84aa | 1667 | -- and it is side-effect-free (for now just an entity), |
f2cbd970 JM |
1668 | -- this may be a shared constraint, e.g. from a subtype |
1669 | -- without a constraint introduced as a generic actual. | |
1670 | -- Examine other discriminants if any. | |
1671 | ||
1672 | if ItemS = ItemT | |
1673 | and then Is_Entity_Name (ItemS) | |
1674 | then | |
1675 | null; | |
1676 | ||
1677 | elsif not Is_OK_Static_Expression (ItemS) | |
1678 | or else not Is_OK_Static_Expression (ItemT) | |
1679 | then | |
1680 | exit; | |
70482933 | 1681 | |
f2cbd970 | 1682 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
70482933 RK |
1683 | if Do_Access then -- needs run-time check. |
1684 | exit; | |
1685 | else | |
1686 | Apply_Compile_Time_Constraint_Error | |
685bc70f | 1687 | (N, "incorrect value for discriminant&??", |
07fc65c4 | 1688 | CE_Discriminant_Check_Failed, Ent => Discr); |
70482933 RK |
1689 | return; |
1690 | end if; | |
1691 | end if; | |
1692 | ||
1693 | Next_Elmt (DconS); | |
1694 | Next_Elmt (DconT); | |
1695 | Next_Discriminant (Discr); | |
1696 | end loop; | |
1697 | ||
1698 | if No (Discr) then | |
1699 | return; | |
1700 | end if; | |
1701 | end; | |
1702 | end if; | |
1703 | ||
27bb7941 AC |
1704 | -- In GNATprove mode, we do not apply the checks |
1705 | ||
1706 | if GNATprove_Mode then | |
1707 | return; | |
1708 | end if; | |
1709 | ||
70482933 RK |
1710 | -- Here we need a discriminant check. First build the expression |
1711 | -- for the comparisons of the discriminants: | |
1712 | ||
1713 | -- (n.disc1 /= typ.disc1) or else | |
1714 | -- (n.disc2 /= typ.disc2) or else | |
1715 | -- ... | |
1716 | -- (n.discn /= typ.discn) | |
1717 | ||
1718 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1719 | ||
acad3c0a AC |
1720 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1721 | -- lhs'constrained and then (condition built above) | |
70482933 RK |
1722 | |
1723 | if Present (Param_Entity (Lhs)) then | |
1724 | Cond := | |
1725 | Make_And_Then (Loc, | |
1726 | Left_Opnd => | |
1727 | Make_Attribute_Reference (Loc, | |
1728 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1729 | Attribute_Name => Name_Constrained), | |
1730 | Right_Opnd => Cond); | |
1731 | end if; | |
1732 | ||
1733 | if Do_Access then | |
1734 | Cond := Guard_Access (Cond, Loc, N); | |
1735 | end if; | |
1736 | ||
1737 | Insert_Action (N, | |
07fc65c4 GB |
1738 | Make_Raise_Constraint_Error (Loc, |
1739 | Condition => Cond, | |
1740 | Reason => CE_Discriminant_Check_Failed)); | |
70482933 RK |
1741 | end Apply_Discriminant_Check; |
1742 | ||
a91e9ac7 AC |
1743 | ------------------------- |
1744 | -- Apply_Divide_Checks -- | |
1745 | ------------------------- | |
70482933 | 1746 | |
a91e9ac7 | 1747 | procedure Apply_Divide_Checks (N : Node_Id) is |
70482933 RK |
1748 | Loc : constant Source_Ptr := Sloc (N); |
1749 | Typ : constant Entity_Id := Etype (N); | |
1750 | Left : constant Node_Id := Left_Opnd (N); | |
1751 | Right : constant Node_Id := Right_Opnd (N); | |
1752 | ||
15c94a55 | 1753 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
a91e9ac7 AC |
1754 | -- Current overflow checking mode |
1755 | ||
70482933 RK |
1756 | LLB : Uint; |
1757 | Llo : Uint; | |
1758 | Lhi : Uint; | |
1759 | LOK : Boolean; | |
1760 | Rlo : Uint; | |
1761 | Rhi : Uint; | |
a91e9ac7 | 1762 | ROK : Boolean; |
67ce0d7e RD |
1763 | |
1764 | pragma Warnings (Off, Lhi); | |
1765 | -- Don't actually use this value | |
70482933 RK |
1766 | |
1767 | begin | |
a7f1b24f RD |
1768 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1769 | -- operating on signed integer types, then the only thing this routine | |
1770 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1771 | -- procedure will (possibly later on during recursive downward calls), | |
1772 | -- ensure that any needed overflow/division checks are properly applied. | |
a91e9ac7 AC |
1773 | |
1774 | if Mode in Minimized_Or_Eliminated | |
a91e9ac7 AC |
1775 | and then Is_Signed_Integer_Type (Typ) |
1776 | then | |
1777 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1778 | return; | |
1779 | end if; | |
1780 | ||
1781 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1782 | ||
4460a9bc | 1783 | if Expander_Active |
2ede092b RD |
1784 | and then not Backend_Divide_Checks_On_Target |
1785 | and then Check_Needed (Right, Division_Check) | |
70482933 | 1786 | then |
c800f862 | 1787 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
70482933 | 1788 | |
a91e9ac7 | 1789 | -- Deal with division check |
70482933 | 1790 | |
a91e9ac7 AC |
1791 | if Do_Division_Check (N) |
1792 | and then not Division_Checks_Suppressed (Typ) | |
1793 | then | |
1794 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
70482933 RK |
1795 | end if; |
1796 | ||
a91e9ac7 AC |
1797 | -- Deal with overflow check |
1798 | ||
a7f1b24f RD |
1799 | if Do_Overflow_Check (N) |
1800 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1801 | then | |
b7c874a7 AC |
1802 | Set_Do_Overflow_Check (N, False); |
1803 | ||
a91e9ac7 AC |
1804 | -- Test for extremely annoying case of xxx'First divided by -1 |
1805 | -- for division of signed integer types (only overflow case). | |
70482933 | 1806 | |
70482933 RK |
1807 | if Nkind (N) = N_Op_Divide |
1808 | and then Is_Signed_Integer_Type (Typ) | |
1809 | then | |
c800f862 | 1810 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
70482933 RK |
1811 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1812 | ||
8f563162 | 1813 | if (not ROK or else (Rlo <= (-1) and then (-1) <= Rhi)) |
a91e9ac7 | 1814 | and then |
8f563162 | 1815 | (not LOK or else Llo = LLB) |
70482933 | 1816 | then |
a8697b27 EB |
1817 | -- Ensure that expressions are not evaluated twice (once |
1818 | -- for their runtime checks and once for their regular | |
1819 | -- computation). | |
1820 | ||
1821 | Force_Evaluation (Left, Mode => Strict); | |
1822 | Force_Evaluation (Right, Mode => Strict); | |
1823 | ||
70482933 RK |
1824 | Insert_Action (N, |
1825 | Make_Raise_Constraint_Error (Loc, | |
1826 | Condition => | |
1827 | Make_And_Then (Loc, | |
a91e9ac7 AC |
1828 | Left_Opnd => |
1829 | Make_Op_Eq (Loc, | |
1830 | Left_Opnd => | |
1831 | Duplicate_Subexpr_Move_Checks (Left), | |
1832 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
70482933 | 1833 | |
a91e9ac7 AC |
1834 | Right_Opnd => |
1835 | Make_Op_Eq (Loc, | |
1836 | Left_Opnd => Duplicate_Subexpr (Right), | |
1837 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
70482933 | 1838 | |
07fc65c4 | 1839 | Reason => CE_Overflow_Check_Failed)); |
70482933 RK |
1840 | end if; |
1841 | end if; | |
1842 | end if; | |
1843 | end if; | |
a91e9ac7 AC |
1844 | end Apply_Divide_Checks; |
1845 | ||
1846 | -------------------------- | |
1847 | -- Apply_Division_Check -- | |
1848 | -------------------------- | |
1849 | ||
1850 | procedure Apply_Division_Check | |
1851 | (N : Node_Id; | |
1852 | Rlo : Uint; | |
1853 | Rhi : Uint; | |
1854 | ROK : Boolean) | |
1855 | is | |
1856 | pragma Assert (Do_Division_Check (N)); | |
1857 | ||
1858 | Loc : constant Source_Ptr := Sloc (N); | |
61770974 HK |
1859 | Right : constant Node_Id := Right_Opnd (N); |
1860 | Opnd : Node_Id; | |
a91e9ac7 AC |
1861 | |
1862 | begin | |
4460a9bc | 1863 | if Expander_Active |
a91e9ac7 AC |
1864 | and then not Backend_Divide_Checks_On_Target |
1865 | and then Check_Needed (Right, Division_Check) | |
a91e9ac7 | 1866 | |
61770974 HK |
1867 | -- See if division by zero possible, and if so generate test. This |
1868 | -- part of the test is not controlled by the -gnato switch, since it | |
1869 | -- is a Division_Check and not an Overflow_Check. | |
b7c874a7 | 1870 | |
61770974 HK |
1871 | and then Do_Division_Check (N) |
1872 | then | |
1873 | Set_Do_Division_Check (N, False); | |
ed170742 | 1874 | |
8f563162 | 1875 | if not ROK or else (Rlo <= 0 and then 0 <= Rhi) then |
61770974 HK |
1876 | if Is_Floating_Point_Type (Etype (N)) then |
1877 | Opnd := Make_Real_Literal (Loc, Ureal_0); | |
1878 | else | |
1879 | Opnd := Make_Integer_Literal (Loc, 0); | |
a91e9ac7 | 1880 | end if; |
61770974 HK |
1881 | |
1882 | Insert_Action (N, | |
1883 | Make_Raise_Constraint_Error (Loc, | |
1884 | Condition => | |
1885 | Make_Op_Eq (Loc, | |
1886 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1887 | Right_Opnd => Opnd), | |
1888 | Reason => CE_Divide_By_Zero)); | |
a91e9ac7 AC |
1889 | end if; |
1890 | end if; | |
1891 | end Apply_Division_Check; | |
70482933 | 1892 | |
7324bf49 AC |
1893 | ---------------------------------- |
1894 | -- Apply_Float_Conversion_Check -- | |
1895 | ---------------------------------- | |
1896 | ||
675d6070 TQ |
1897 | -- Let F and I be the source and target types of the conversion. The RM |
1898 | -- specifies that a floating-point value X is rounded to the nearest | |
1899 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1900 | -- value of X is checked against I'Range. | |
1901 | ||
1902 | -- The catch in the above paragraph is that there is no good way to know | |
1903 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1904 | -- to perform a range check in the floating-point domain instead, however: | |
7324bf49 | 1905 | |
7324bf49 | 1906 | -- (1) The bounds may not be known at compile time |
939c12d2 | 1907 | -- (2) The check must take into account rounding or truncation. |
7324bf49 | 1908 | -- (3) The range of type I may not be exactly representable in F. |
e8bb6ff9 | 1909 | -- (4) For the rounding case, the end-points I'First - 0.5 and |
939c12d2 RD |
1910 | -- I'Last + 0.5 may or may not be in range, depending on the |
1911 | -- sign of I'First and I'Last. | |
7324bf49 AC |
1912 | -- (5) X may be a NaN, which will fail any comparison |
1913 | ||
939c12d2 | 1914 | -- The following steps correctly convert X with rounding: |
675d6070 | 1915 | |
7324bf49 AC |
1916 | -- (1) If either I'First or I'Last is not known at compile time, use |
1917 | -- I'Base instead of I in the next three steps and perform a | |
1918 | -- regular range check against I'Range after conversion. | |
1919 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1920 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
939c12d2 RD |
1921 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1922 | -- In other words, take one of the closest floating-point numbers | |
1923 | -- (which is an integer value) to I'First, and see if it is in | |
1924 | -- range or not. | |
7324bf49 AC |
1925 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1926 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
939c12d2 | 1927 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
7324bf49 AC |
1928 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1929 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1930 | ||
939c12d2 RD |
1931 | -- For the truncating case, replace steps (2) and (3) as follows: |
1932 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1933 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1934 | -- Lo_OK be True. | |
1935 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1936 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
1197ddb1 | 1937 | -- Hi_OK be True. |
939c12d2 | 1938 | |
7324bf49 | 1939 | procedure Apply_Float_Conversion_Check |
6c8e70fe | 1940 | (Expr : Node_Id; |
7324bf49 AC |
1941 | Target_Typ : Entity_Id) |
1942 | is | |
675d6070 TQ |
1943 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1944 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
6c8e70fe EB |
1945 | Loc : constant Source_Ptr := Sloc (Expr); |
1946 | Expr_Type : constant Entity_Id := Base_Type (Etype (Expr)); | |
675d6070 | 1947 | Target_Base : constant Entity_Id := |
15f0f591 | 1948 | Implementation_Base_Type (Target_Typ); |
675d6070 | 1949 | |
6c8e70fe | 1950 | Par : constant Node_Id := Parent (Expr); |
939c12d2 RD |
1951 | pragma Assert (Nkind (Par) = N_Type_Conversion); |
1952 | -- Parent of check node, must be a type conversion | |
1953 | ||
1954 | Truncate : constant Boolean := Float_Truncate (Par); | |
1955 | Max_Bound : constant Uint := | |
15f0f591 AC |
1956 | UI_Expon |
1957 | (Machine_Radix_Value (Expr_Type), | |
1958 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
939c12d2 | 1959 | |
7324bf49 AC |
1960 | -- Largest bound, so bound plus or minus half is a machine number of F |
1961 | ||
675d6070 TQ |
1962 | Ifirst, Ilast : Uint; |
1963 | -- Bounds of integer type | |
1964 | ||
1965 | Lo, Hi : Ureal; | |
1966 | -- Bounds to check in floating-point domain | |
7324bf49 | 1967 | |
675d6070 TQ |
1968 | Lo_OK, Hi_OK : Boolean; |
1969 | -- True iff Lo resp. Hi belongs to I'Range | |
7324bf49 | 1970 | |
675d6070 TQ |
1971 | Lo_Chk, Hi_Chk : Node_Id; |
1972 | -- Expressions that are False iff check fails | |
1973 | ||
1974 | Reason : RT_Exception_Code; | |
7324bf49 AC |
1975 | |
1976 | begin | |
b5bdffcc AC |
1977 | -- We do not need checks if we are not generating code (i.e. the full |
1978 | -- expander is not active). In SPARK mode, we specifically don't want | |
1979 | -- the frontend to expand these checks, which are dealt with directly | |
1980 | -- in the formal verification backend. | |
1981 | ||
4460a9bc | 1982 | if not Expander_Active then |
b5bdffcc AC |
1983 | return; |
1984 | end if; | |
1985 | ||
68c8d72a EB |
1986 | -- Here we will generate an explicit range check, so we don't want to |
1987 | -- set the Do_Range check flag, since the range check is taken care of | |
1988 | -- by the code we will generate. | |
1989 | ||
6c8e70fe | 1990 | Set_Do_Range_Check (Expr, False); |
68c8d72a | 1991 | |
7324bf49 AC |
1992 | if not Compile_Time_Known_Value (LB) |
1993 | or not Compile_Time_Known_Value (HB) | |
1994 | then | |
1995 | declare | |
675d6070 TQ |
1996 | -- First check that the value falls in the range of the base type, |
1997 | -- to prevent overflow during conversion and then perform a | |
1998 | -- regular range check against the (dynamic) bounds. | |
7324bf49 | 1999 | |
7324bf49 | 2000 | pragma Assert (Target_Base /= Target_Typ); |
7324bf49 | 2001 | |
191fcb3a | 2002 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
7324bf49 AC |
2003 | |
2004 | begin | |
6c8e70fe | 2005 | Apply_Float_Conversion_Check (Expr, Target_Base); |
7324bf49 AC |
2006 | Set_Etype (Temp, Target_Base); |
2007 | ||
ba58b776 GD |
2008 | -- Note: Previously the declaration was inserted above the parent |
2009 | -- of the conversion, apparently as a small optimization for the | |
2010 | -- subequent traversal in Insert_Actions. Unfortunately a similar | |
2011 | -- optimization takes place in Insert_Actions, assuming that the | |
2012 | -- insertion point must be above the expression that creates | |
2013 | -- actions. This is not correct in the presence of conditional | |
2014 | -- expressions, where the insertion must be in the list of actions | |
2015 | -- attached to the current alternative. | |
90393d3c ES |
2016 | |
2017 | Insert_Action (Par, | |
7324bf49 AC |
2018 | Make_Object_Declaration (Loc, |
2019 | Defining_Identifier => Temp, | |
2020 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
2021 | Expression => New_Copy_Tree (Par)), | |
2022 | Suppress => All_Checks); | |
2023 | ||
2024 | Insert_Action (Par, | |
2025 | Make_Raise_Constraint_Error (Loc, | |
2026 | Condition => | |
2027 | Make_Not_In (Loc, | |
2028 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
2029 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
2030 | Reason => CE_Range_Check_Failed)); | |
2031 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
2032 | ||
2033 | return; | |
2034 | end; | |
2035 | end if; | |
2036 | ||
44114dff | 2037 | -- Get the (static) bounds of the target type |
7324bf49 AC |
2038 | |
2039 | Ifirst := Expr_Value (LB); | |
2040 | Ilast := Expr_Value (HB); | |
2041 | ||
44114dff ES |
2042 | -- A simple optimization: if the expression is a universal literal, |
2043 | -- we can do the comparison with the bounds and the conversion to | |
2044 | -- an integer type statically. The range checks are unchanged. | |
2045 | ||
6c8e70fe EB |
2046 | if Nkind (Expr) = N_Real_Literal |
2047 | and then Etype (Expr) = Universal_Real | |
44114dff | 2048 | and then Is_Integer_Type (Target_Typ) |
44114dff ES |
2049 | then |
2050 | declare | |
6c8e70fe | 2051 | Int_Val : constant Uint := UR_To_Uint (Realval (Expr)); |
44114dff ES |
2052 | |
2053 | begin | |
2054 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
2055 | ||
6f2b033b | 2056 | -- Conversion is safe |
44114dff | 2057 | |
6c8e70fe | 2058 | Rewrite (Parent (Expr), |
44114dff | 2059 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); |
6c8e70fe | 2060 | Analyze_And_Resolve (Parent (Expr), Target_Typ); |
44114dff ES |
2061 | return; |
2062 | end if; | |
2063 | end; | |
2064 | end if; | |
2065 | ||
7324bf49 AC |
2066 | -- Check against lower bound |
2067 | ||
939c12d2 RD |
2068 | if Truncate and then Ifirst > 0 then |
2069 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
2070 | Lo_OK := False; | |
2071 | ||
2072 | elsif Truncate then | |
2073 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
2074 | Lo_OK := True; | |
2075 | ||
2076 | elsif abs (Ifirst) < Max_Bound then | |
7324bf49 AC |
2077 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
2078 | Lo_OK := (Ifirst > 0); | |
939c12d2 | 2079 | |
7324bf49 | 2080 | else |
60440d3c | 2081 | Lo := Machine_Number (Expr_Type, UR_From_Uint (Ifirst), Expr); |
7324bf49 AC |
2082 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); |
2083 | end if; | |
2084 | ||
46e54783 EB |
2085 | -- Saturate the lower bound to that of the expression's type, because |
2086 | -- we do not want to create an out-of-range value but we still need to | |
2087 | -- do a comparison to catch NaNs. | |
2088 | ||
2089 | if Lo < Expr_Value_R (Type_Low_Bound (Expr_Type)) then | |
2090 | Lo := Expr_Value_R (Type_Low_Bound (Expr_Type)); | |
2091 | Lo_OK := True; | |
2092 | end if; | |
2093 | ||
7324bf49 AC |
2094 | if Lo_OK then |
2095 | ||
2096 | -- Lo_Chk := (X >= Lo) | |
2097 | ||
2098 | Lo_Chk := Make_Op_Ge (Loc, | |
6c8e70fe | 2099 | Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
7324bf49 AC |
2100 | Right_Opnd => Make_Real_Literal (Loc, Lo)); |
2101 | ||
2102 | else | |
2103 | -- Lo_Chk := (X > Lo) | |
2104 | ||
2105 | Lo_Chk := Make_Op_Gt (Loc, | |
6c8e70fe | 2106 | Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
7324bf49 AC |
2107 | Right_Opnd => Make_Real_Literal (Loc, Lo)); |
2108 | end if; | |
2109 | ||
2110 | -- Check against higher bound | |
2111 | ||
939c12d2 RD |
2112 | if Truncate and then Ilast < 0 then |
2113 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
c2db4b32 | 2114 | Hi_OK := False; |
939c12d2 RD |
2115 | |
2116 | elsif Truncate then | |
2117 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2118 | Hi_OK := True; | |
2119 | ||
2120 | elsif abs (Ilast) < Max_Bound then | |
7324bf49 AC |
2121 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2122 | Hi_OK := (Ilast < 0); | |
2123 | else | |
60440d3c | 2124 | Hi := Machine_Number (Expr_Type, UR_From_Uint (Ilast), Expr); |
7324bf49 AC |
2125 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); |
2126 | end if; | |
2127 | ||
46e54783 EB |
2128 | -- Saturate the higher bound to that of the expression's type, because |
2129 | -- we do not want to create an out-of-range value but we still need to | |
2130 | -- do a comparison to catch NaNs. | |
2131 | ||
2132 | if Hi > Expr_Value_R (Type_High_Bound (Expr_Type)) then | |
2133 | Hi := Expr_Value_R (Type_High_Bound (Expr_Type)); | |
2134 | Hi_OK := True; | |
2135 | end if; | |
2136 | ||
7324bf49 AC |
2137 | if Hi_OK then |
2138 | ||
2139 | -- Hi_Chk := (X <= Hi) | |
2140 | ||
2141 | Hi_Chk := Make_Op_Le (Loc, | |
6c8e70fe | 2142 | Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
7324bf49 AC |
2143 | Right_Opnd => Make_Real_Literal (Loc, Hi)); |
2144 | ||
2145 | else | |
2146 | -- Hi_Chk := (X < Hi) | |
2147 | ||
2148 | Hi_Chk := Make_Op_Lt (Loc, | |
6c8e70fe | 2149 | Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
7324bf49 AC |
2150 | Right_Opnd => Make_Real_Literal (Loc, Hi)); |
2151 | end if; | |
2152 | ||
675d6070 TQ |
2153 | -- If the bounds of the target type are the same as those of the base |
2154 | -- type, the check is an overflow check as a range check is not | |
2155 | -- performed in these cases. | |
7324bf49 AC |
2156 | |
2157 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2158 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2159 | then | |
2160 | Reason := CE_Overflow_Check_Failed; | |
2161 | else | |
2162 | Reason := CE_Range_Check_Failed; | |
2163 | end if; | |
2164 | ||
2165 | -- Raise CE if either conditions does not hold | |
2166 | ||
6c8e70fe | 2167 | Insert_Action (Expr, |
7324bf49 | 2168 | Make_Raise_Constraint_Error (Loc, |
d8b9660d | 2169 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
7324bf49 AC |
2170 | Reason => Reason)); |
2171 | end Apply_Float_Conversion_Check; | |
2172 | ||
70482933 RK |
2173 | ------------------------ |
2174 | -- Apply_Length_Check -- | |
2175 | ------------------------ | |
2176 | ||
2177 | procedure Apply_Length_Check | |
6c8e70fe | 2178 | (Expr : Node_Id; |
70482933 RK |
2179 | Target_Typ : Entity_Id; |
2180 | Source_Typ : Entity_Id := Empty) | |
2181 | is | |
2182 | begin | |
2183 | Apply_Selected_Length_Checks | |
6c8e70fe | 2184 | (Expr, Target_Typ, Source_Typ, Do_Static => False); |
70482933 RK |
2185 | end Apply_Length_Check; |
2186 | ||
25f11dfe EB |
2187 | -------------------------------------- |
2188 | -- Apply_Length_Check_On_Assignment -- | |
2189 | -------------------------------------- | |
2190 | ||
2191 | procedure Apply_Length_Check_On_Assignment | |
2192 | (Expr : Node_Id; | |
2193 | Target_Typ : Entity_Id; | |
2194 | Target : Node_Id; | |
2195 | Source_Typ : Entity_Id := Empty) | |
2196 | is | |
2197 | Assign : constant Node_Id := Parent (Target); | |
2198 | ||
2199 | begin | |
a521dc37 EB |
2200 | -- Do not apply length checks if parent is still an assignment statement |
2201 | -- with Suppress_Assignment_Checks flag set. | |
2202 | ||
2203 | if Nkind (Assign) = N_Assignment_Statement | |
2204 | and then Suppress_Assignment_Checks (Assign) | |
2205 | then | |
2206 | return; | |
2207 | end if; | |
2208 | ||
25f11dfe EB |
2209 | -- No check is needed for the initialization of an object whose |
2210 | -- nominal subtype is unconstrained. | |
2211 | ||
2212 | if Is_Constr_Subt_For_U_Nominal (Target_Typ) | |
2213 | and then Nkind (Parent (Assign)) = N_Freeze_Entity | |
2214 | and then Is_Entity_Name (Target) | |
2215 | and then Entity (Target) = Entity (Parent (Assign)) | |
2216 | then | |
2217 | return; | |
2218 | end if; | |
2219 | ||
2220 | Apply_Selected_Length_Checks | |
2221 | (Expr, Target_Typ, Source_Typ, Do_Static => False); | |
2222 | end Apply_Length_Check_On_Assignment; | |
2223 | ||
5f49133f AC |
2224 | ------------------------------------- |
2225 | -- Apply_Parameter_Aliasing_Checks -- | |
2226 | ------------------------------------- | |
0ea55619 | 2227 | |
5f49133f AC |
2228 | procedure Apply_Parameter_Aliasing_Checks |
2229 | (Call : Node_Id; | |
2230 | Subp : Entity_Id) | |
2231 | is | |
baed70ac AC |
2232 | Loc : constant Source_Ptr := Sloc (Call); |
2233 | ||
cc9a7ae2 PT |
2234 | function Parameter_Passing_Mechanism_Specified |
2235 | (Typ : Entity_Id) | |
2236 | return Boolean; | |
2237 | -- Returns True if parameter-passing mechanism is specified for type Typ | |
2238 | ||
5f49133f AC |
2239 | function May_Cause_Aliasing |
2240 | (Formal_1 : Entity_Id; | |
2241 | Formal_2 : Entity_Id) return Boolean; | |
2242 | -- Determine whether two formal parameters can alias each other | |
2243 | -- depending on their modes. | |
2244 | ||
2245 | function Original_Actual (N : Node_Id) return Node_Id; | |
2246 | -- The expander may replace an actual with a temporary for the sake of | |
2247 | -- side effect removal. The temporary may hide a potential aliasing as | |
2248 | -- it does not share the address of the actual. This routine attempts | |
2249 | -- to retrieve the original actual. | |
2250 | ||
baed70ac AC |
2251 | procedure Overlap_Check |
2252 | (Actual_1 : Node_Id; | |
2253 | Actual_2 : Node_Id; | |
2254 | Formal_1 : Entity_Id; | |
2255 | Formal_2 : Entity_Id; | |
2256 | Check : in out Node_Id); | |
2257 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2258 | -- If detailed exception messages are enabled, the check is augmented to | |
2259 | -- provide information about the names of the corresponding formals. See | |
2260 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2261 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2262 | -- Check contains all and-ed simple tests generated so far or remains | |
2263 | -- unchanged in the case of detailed exception messaged. | |
2264 | ||
cc9a7ae2 PT |
2265 | ------------------------------------------- |
2266 | -- Parameter_Passing_Mechanism_Specified -- | |
2267 | ------------------------------------------- | |
2268 | ||
2269 | function Parameter_Passing_Mechanism_Specified | |
2270 | (Typ : Entity_Id) | |
2271 | return Boolean | |
2272 | is | |
2273 | begin | |
2274 | return Is_Elementary_Type (Typ) | |
2275 | or else Is_By_Reference_Type (Typ); | |
2276 | end Parameter_Passing_Mechanism_Specified; | |
2277 | ||
5f49133f AC |
2278 | ------------------------ |
2279 | -- May_Cause_Aliasing -- | |
2280 | ------------------------ | |
0ea55619 | 2281 | |
5f49133f | 2282 | function May_Cause_Aliasing |
e8dde875 | 2283 | (Formal_1 : Entity_Id; |
5f49133f AC |
2284 | Formal_2 : Entity_Id) return Boolean |
2285 | is | |
2286 | begin | |
2287 | -- The following combination cannot lead to aliasing | |
2288 | ||
2289 | -- Formal 1 Formal 2 | |
2290 | -- IN IN | |
2291 | ||
2292 | if Ekind (Formal_1) = E_In_Parameter | |
9a6dc470 RD |
2293 | and then |
2294 | Ekind (Formal_2) = E_In_Parameter | |
5f49133f AC |
2295 | then |
2296 | return False; | |
2297 | ||
2298 | -- The following combinations may lead to aliasing | |
2299 | ||
2300 | -- Formal 1 Formal 2 | |
2301 | -- IN OUT | |
2302 | -- IN IN OUT | |
2303 | -- OUT IN | |
2304 | -- OUT IN OUT | |
2305 | -- OUT OUT | |
2306 | ||
2307 | else | |
2308 | return True; | |
2309 | end if; | |
2310 | end May_Cause_Aliasing; | |
2311 | ||
2312 | --------------------- | |
2313 | -- Original_Actual -- | |
2314 | --------------------- | |
2315 | ||
2316 | function Original_Actual (N : Node_Id) return Node_Id is | |
2317 | begin | |
2318 | if Nkind (N) = N_Type_Conversion then | |
2319 | return Expression (N); | |
2320 | ||
2321 | -- The expander created a temporary to capture the result of a type | |
2322 | -- conversion where the expression is the real actual. | |
2323 | ||
2324 | elsif Nkind (N) = N_Identifier | |
2325 | and then Present (Original_Node (N)) | |
2326 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2327 | then | |
2328 | return Expression (Original_Node (N)); | |
2329 | end if; | |
2330 | ||
2331 | return N; | |
2332 | end Original_Actual; | |
2333 | ||
baed70ac AC |
2334 | ------------------- |
2335 | -- Overlap_Check -- | |
2336 | ------------------- | |
2337 | ||
2338 | procedure Overlap_Check | |
2339 | (Actual_1 : Node_Id; | |
2340 | Actual_2 : Node_Id; | |
2341 | Formal_1 : Entity_Id; | |
2342 | Formal_2 : Entity_Id; | |
2343 | Check : in out Node_Id) | |
2344 | is | |
f59ca9ee PT |
2345 | Cond : Node_Id; |
2346 | Formal_Name : Bounded_String; | |
baed70ac AC |
2347 | |
2348 | begin | |
2349 | -- Generate: | |
2350 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2351 | ||
2352 | Cond := | |
2353 | Make_Attribute_Reference (Loc, | |
2354 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2355 | Attribute_Name => Name_Overlaps_Storage, | |
2356 | Expressions => | |
2357 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2358 | ||
2359 | -- Generate the following check when detailed exception messages are | |
2360 | -- enabled: | |
2361 | ||
2362 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2363 | -- raise Program_Error with <detailed message>; | |
2364 | -- end if; | |
2365 | ||
2366 | if Exception_Extra_Info then | |
2367 | Start_String; | |
2368 | ||
2369 | -- Do not generate location information for internal calls | |
2370 | ||
2371 | if Comes_From_Source (Call) then | |
2372 | Store_String_Chars (Build_Location_String (Loc)); | |
2373 | Store_String_Char (' '); | |
2374 | end if; | |
2375 | ||
2376 | Store_String_Chars ("aliased parameters, actuals for """); | |
f7ea2603 | 2377 | |
f59ca9ee PT |
2378 | Append (Formal_Name, Chars (Formal_1)); |
2379 | Adjust_Name_Case (Formal_Name, Sloc (Formal_1)); | |
2380 | Store_String_Chars (To_String (Formal_Name)); | |
f7ea2603 | 2381 | |
baed70ac | 2382 | Store_String_Chars (""" and """); |
f7ea2603 | 2383 | |
f59ca9ee PT |
2384 | Formal_Name.Length := 0; |
2385 | ||
2386 | Append (Formal_Name, Chars (Formal_2)); | |
2387 | Adjust_Name_Case (Formal_Name, Sloc (Formal_2)); | |
2388 | Store_String_Chars (To_String (Formal_Name)); | |
f7ea2603 | 2389 | |
baed70ac AC |
2390 | Store_String_Chars (""" overlap"); |
2391 | ||
2392 | Insert_Action (Call, | |
2393 | Make_If_Statement (Loc, | |
2394 | Condition => Cond, | |
2395 | Then_Statements => New_List ( | |
2396 | Make_Raise_Statement (Loc, | |
2397 | Name => | |
e4494292 | 2398 | New_Occurrence_Of (Standard_Program_Error, Loc), |
baed70ac AC |
2399 | Expression => Make_String_Literal (Loc, End_String))))); |
2400 | ||
2401 | -- Create a sequence of overlapping checks by and-ing them all | |
2402 | -- together. | |
2403 | ||
2404 | else | |
2405 | if No (Check) then | |
2406 | Check := Cond; | |
2407 | else | |
2408 | Check := | |
2409 | Make_And_Then (Loc, | |
2410 | Left_Opnd => Check, | |
2411 | Right_Opnd => Cond); | |
2412 | end if; | |
2413 | end if; | |
2414 | end Overlap_Check; | |
2415 | ||
5f49133f AC |
2416 | -- Local variables |
2417 | ||
b4213ffd AC |
2418 | Actual_1 : Node_Id; |
2419 | Actual_2 : Node_Id; | |
2420 | Check : Node_Id; | |
2421 | Formal_1 : Entity_Id; | |
2422 | Formal_2 : Entity_Id; | |
2423 | Orig_Act_1 : Node_Id; | |
2424 | Orig_Act_2 : Node_Id; | |
5f49133f AC |
2425 | |
2426 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2427 | ||
2428 | begin | |
baed70ac | 2429 | Check := Empty; |
5f49133f AC |
2430 | |
2431 | Actual_1 := First_Actual (Call); | |
2432 | Formal_1 := First_Formal (Subp); | |
2433 | while Present (Actual_1) and then Present (Formal_1) loop | |
b4213ffd | 2434 | Orig_Act_1 := Original_Actual (Actual_1); |
5f49133f | 2435 | |
9e1ca4e3 | 2436 | if Is_Name_Reference (Orig_Act_1) then |
5f49133f AC |
2437 | Actual_2 := Next_Actual (Actual_1); |
2438 | Formal_2 := Next_Formal (Formal_1); | |
2439 | while Present (Actual_2) and then Present (Formal_2) loop | |
b4213ffd | 2440 | Orig_Act_2 := Original_Actual (Actual_2); |
5f49133f | 2441 | |
9e1ca4e3 PT |
2442 | -- Generate the check only when the mode of the two formals may |
2443 | -- lead to aliasing. | |
5f49133f | 2444 | |
9e1ca4e3 | 2445 | if Is_Name_Reference (Orig_Act_2) |
5f49133f AC |
2446 | and then May_Cause_Aliasing (Formal_1, Formal_2) |
2447 | then | |
cc9a7ae2 PT |
2448 | |
2449 | -- The aliasing check only applies when some of the formals | |
2450 | -- have their passing mechanism unspecified; RM 6.2 (12/3). | |
2451 | ||
2452 | if Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_1)) | |
2453 | and then | |
2454 | Parameter_Passing_Mechanism_Specified (Etype (Orig_Act_2)) | |
2455 | then | |
2456 | null; | |
2457 | else | |
2458 | Remove_Side_Effects (Actual_1); | |
2459 | Remove_Side_Effects (Actual_2); | |
2460 | ||
2461 | Overlap_Check | |
2462 | (Actual_1 => Actual_1, | |
2463 | Actual_2 => Actual_2, | |
2464 | Formal_1 => Formal_1, | |
2465 | Formal_2 => Formal_2, | |
2466 | Check => Check); | |
2467 | end if; | |
5f49133f AC |
2468 | end if; |
2469 | ||
2470 | Next_Actual (Actual_2); | |
2471 | Next_Formal (Formal_2); | |
2472 | end loop; | |
2473 | end if; | |
2474 | ||
2475 | Next_Actual (Actual_1); | |
2476 | Next_Formal (Formal_1); | |
2477 | end loop; | |
2478 | ||
baed70ac | 2479 | -- Place a simple check right before the call |
5f49133f | 2480 | |
baed70ac | 2481 | if Present (Check) and then not Exception_Extra_Info then |
5f49133f AC |
2482 | Insert_Action (Call, |
2483 | Make_Raise_Program_Error (Loc, | |
baed70ac AC |
2484 | Condition => Check, |
2485 | Reason => PE_Aliased_Parameters)); | |
5f49133f AC |
2486 | end if; |
2487 | end Apply_Parameter_Aliasing_Checks; | |
2488 | ||
2489 | ------------------------------------- | |
2490 | -- Apply_Parameter_Validity_Checks -- | |
2491 | ------------------------------------- | |
2492 | ||
2493 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2494 | Subp_Decl : Node_Id; | |
0ea55619 | 2495 | |
e8dde875 | 2496 | procedure Add_Validity_Check |
c9d70ab1 AC |
2497 | (Formal : Entity_Id; |
2498 | Prag_Nam : Name_Id; | |
e8dde875 | 2499 | For_Result : Boolean := False); |
31fde973 | 2500 | -- Add a single 'Valid[_Scalars] check which verifies the initialization |
c9d70ab1 | 2501 | -- of Formal. Prag_Nam denotes the pre or post condition pragma name. |
e8dde875 | 2502 | -- Set flag For_Result when to verify the result of a function. |
0ea55619 | 2503 | |
0ea55619 AC |
2504 | ------------------------ |
2505 | -- Add_Validity_Check -- | |
2506 | ------------------------ | |
2507 | ||
2508 | procedure Add_Validity_Check | |
c9d70ab1 AC |
2509 | (Formal : Entity_Id; |
2510 | Prag_Nam : Name_Id; | |
0ea55619 AC |
2511 | For_Result : Boolean := False) |
2512 | is | |
c9d70ab1 AC |
2513 | procedure Build_Pre_Post_Condition (Expr : Node_Id); |
2514 | -- Create a pre/postcondition pragma that tests expression Expr | |
2515 | ||
2516 | ------------------------------ | |
2517 | -- Build_Pre_Post_Condition -- | |
2518 | ------------------------------ | |
2519 | ||
2520 | procedure Build_Pre_Post_Condition (Expr : Node_Id) is | |
2521 | Loc : constant Source_Ptr := Sloc (Subp); | |
2522 | Decls : List_Id; | |
2523 | Prag : Node_Id; | |
2524 | ||
2525 | begin | |
2526 | Prag := | |
2527 | Make_Pragma (Loc, | |
88456bc1 | 2528 | Chars => Prag_Nam, |
c9d70ab1 AC |
2529 | Pragma_Argument_Associations => New_List ( |
2530 | Make_Pragma_Argument_Association (Loc, | |
2531 | Chars => Name_Check, | |
2532 | Expression => Expr))); | |
2533 | ||
2534 | -- Add a message unless exception messages are suppressed | |
2535 | ||
2536 | if not Exception_Locations_Suppressed then | |
2537 | Append_To (Pragma_Argument_Associations (Prag), | |
2538 | Make_Pragma_Argument_Association (Loc, | |
2539 | Chars => Name_Message, | |
2540 | Expression => | |
2541 | Make_String_Literal (Loc, | |
2542 | Strval => "failed " | |
2543 | & Get_Name_String (Prag_Nam) | |
2544 | & " from " | |
2545 | & Build_Location_String (Loc)))); | |
2546 | end if; | |
2547 | ||
2548 | -- Insert the pragma in the tree | |
2549 | ||
2550 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2551 | Add_Global_Declaration (Prag); | |
2552 | Analyze (Prag); | |
2553 | ||
2554 | -- PPC pragmas associated with subprogram bodies must be inserted | |
2555 | -- in the declarative part of the body. | |
2556 | ||
2557 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2558 | Decls := Declarations (Subp_Decl); | |
2559 | ||
2560 | if No (Decls) then | |
2561 | Decls := New_List; | |
2562 | Set_Declarations (Subp_Decl, Decls); | |
2563 | end if; | |
2564 | ||
2565 | Prepend_To (Decls, Prag); | |
2566 | Analyze (Prag); | |
2567 | ||
2568 | -- For subprogram declarations insert the PPC pragma right after | |
2569 | -- the declarative node. | |
2570 | ||
2571 | else | |
2572 | Insert_After_And_Analyze (Subp_Decl, Prag); | |
2573 | end if; | |
2574 | end Build_Pre_Post_Condition; | |
2575 | ||
2576 | -- Local variables | |
2577 | ||
e8dde875 | 2578 | Loc : constant Source_Ptr := Sloc (Subp); |
c9d70ab1 | 2579 | Typ : constant Entity_Id := Etype (Formal); |
0ea55619 AC |
2580 | Check : Node_Id; |
2581 | Nam : Name_Id; | |
2582 | ||
c9d70ab1 AC |
2583 | -- Start of processing for Add_Validity_Check |
2584 | ||
0ea55619 | 2585 | begin |
e5c4e2bc | 2586 | -- For scalars, generate 'Valid test |
0ea55619 AC |
2587 | |
2588 | if Is_Scalar_Type (Typ) then | |
2589 | Nam := Name_Valid; | |
e5c4e2bc AC |
2590 | |
2591 | -- For any non-scalar with scalar parts, generate 'Valid_Scalars test | |
2592 | ||
2593 | elsif Scalar_Part_Present (Typ) then | |
0ea55619 | 2594 | Nam := Name_Valid_Scalars; |
e5c4e2bc AC |
2595 | |
2596 | -- No test needed for other cases (no scalars to test) | |
2597 | ||
0ea55619 AC |
2598 | else |
2599 | return; | |
2600 | end if; | |
2601 | ||
2602 | -- Step 1: Create the expression to verify the validity of the | |
2603 | -- context. | |
2604 | ||
c9d70ab1 | 2605 | Check := New_Occurrence_Of (Formal, Loc); |
0ea55619 AC |
2606 | |
2607 | -- When processing a function result, use 'Result. Generate | |
2608 | -- Context'Result | |
2609 | ||
2610 | if For_Result then | |
2611 | Check := | |
2612 | Make_Attribute_Reference (Loc, | |
2613 | Prefix => Check, | |
2614 | Attribute_Name => Name_Result); | |
2615 | end if; | |
2616 | ||
2617 | -- Generate: | |
2618 | -- Context['Result]'Valid[_Scalars] | |
2619 | ||
2620 | Check := | |
2621 | Make_Attribute_Reference (Loc, | |
2622 | Prefix => Check, | |
2623 | Attribute_Name => Nam); | |
2624 | ||
e8dde875 AC |
2625 | -- Step 2: Create a pre or post condition pragma |
2626 | ||
c9d70ab1 | 2627 | Build_Pre_Post_Condition (Check); |
e8dde875 AC |
2628 | end Add_Validity_Check; |
2629 | ||
e8dde875 AC |
2630 | -- Local variables |
2631 | ||
2632 | Formal : Entity_Id; | |
e8dde875 AC |
2633 | Subp_Spec : Node_Id; |
2634 | ||
5f49133f | 2635 | -- Start of processing for Apply_Parameter_Validity_Checks |
0ea55619 AC |
2636 | |
2637 | begin | |
e8dde875 | 2638 | -- Extract the subprogram specification and declaration nodes |
0ea55619 | 2639 | |
e8dde875 | 2640 | Subp_Spec := Parent (Subp); |
9a6dc470 | 2641 | |
898edf75 BD |
2642 | if No (Subp_Spec) then |
2643 | return; | |
2644 | end if; | |
2645 | ||
e8dde875 AC |
2646 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2647 | Subp_Spec := Parent (Subp_Spec); | |
2648 | end if; | |
9a6dc470 | 2649 | |
e8dde875 | 2650 | Subp_Decl := Parent (Subp_Spec); |
8e983d80 | 2651 | |
0ea55619 | 2652 | if not Comes_From_Source (Subp) |
e8dde875 | 2653 | |
5af0271f PT |
2654 | -- Do not process formal subprograms because the corresponding actual |
2655 | -- will receive the proper checks when the instance is analyzed. | |
e8dde875 AC |
2656 | |
2657 | or else Is_Formal_Subprogram (Subp) | |
2658 | ||
e0c23ac7 | 2659 | -- Do not process imported subprograms since pre and postconditions |
9a6dc470 | 2660 | -- are never verified on routines coming from a different language. |
e8dde875 | 2661 | |
0ea55619 AC |
2662 | or else Is_Imported (Subp) |
2663 | or else Is_Intrinsic_Subprogram (Subp) | |
e8dde875 | 2664 | |
9a6dc470 RD |
2665 | -- The PPC pragmas generated by this routine do not correspond to |
2666 | -- source aspects, therefore they cannot be applied to abstract | |
2667 | -- subprograms. | |
e8dde875 | 2668 | |
c5a26133 | 2669 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
e8dde875 | 2670 | |
9a6dc470 RD |
2671 | -- Do not consider subprogram renaminds because the renamed entity |
2672 | -- already has the proper PPC pragmas. | |
d85be3ba AC |
2673 | |
2674 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2675 | ||
9a6dc470 RD |
2676 | -- Do not process null procedures because there is no benefit of |
2677 | -- adding the checks to a no action routine. | |
e8dde875 AC |
2678 | |
2679 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
9a6dc470 | 2680 | and then Null_Present (Subp_Spec)) |
0ea55619 AC |
2681 | then |
2682 | return; | |
2683 | end if; | |
2684 | ||
e8dde875 AC |
2685 | -- Inspect all the formals applying aliasing and scalar initialization |
2686 | -- checks where applicable. | |
0ea55619 AC |
2687 | |
2688 | Formal := First_Formal (Subp); | |
2689 | while Present (Formal) loop | |
e8dde875 AC |
2690 | |
2691 | -- Generate the following scalar initialization checks for each | |
2692 | -- formal parameter: | |
2693 | ||
2694 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2695 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2696 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2697 | ||
4a08c95c | 2698 | if Ekind (Formal) in E_In_Parameter | E_In_Out_Parameter then |
5af0271f PT |
2699 | Add_Validity_Check (Formal, Name_Precondition, False); |
2700 | end if; | |
e8dde875 | 2701 | |
4a08c95c | 2702 | if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then |
5af0271f | 2703 | Add_Validity_Check (Formal, Name_Postcondition, False); |
0ea55619 AC |
2704 | end if; |
2705 | ||
0ea55619 AC |
2706 | Next_Formal (Formal); |
2707 | end loop; | |
2708 | ||
9a6dc470 | 2709 | -- Generate following scalar initialization check for function result: |
e8dde875 AC |
2710 | |
2711 | -- Post => Subp'Result'Valid[_Scalars] | |
0ea55619 | 2712 | |
5af0271f | 2713 | if Ekind (Subp) = E_Function then |
e8dde875 | 2714 | Add_Validity_Check (Subp, Name_Postcondition, True); |
0ea55619 | 2715 | end if; |
5f49133f | 2716 | end Apply_Parameter_Validity_Checks; |
0ea55619 | 2717 | |
48f91b44 RD |
2718 | --------------------------- |
2719 | -- Apply_Predicate_Check -- | |
2720 | --------------------------- | |
2721 | ||
6eca51ce ES |
2722 | procedure Apply_Predicate_Check |
2723 | (N : Node_Id; | |
2724 | Typ : Entity_Id; | |
2725 | Fun : Entity_Id := Empty) | |
2726 | is | |
b97813ab EB |
2727 | Par : Node_Id; |
2728 | S : Entity_Id; | |
8e983d80 | 2729 | |
8f563162 | 2730 | Check_Disabled : constant Boolean := not Predicate_Enabled (Typ) |
8861bdd5 | 2731 | or else not Predicate_Check_In_Scope (N); |
48f91b44 | 2732 | begin |
152f64c2 AC |
2733 | S := Current_Scope; |
2734 | while Present (S) and then not Is_Subprogram (S) loop | |
2735 | S := Scope (S); | |
2736 | end loop; | |
62db841a | 2737 | |
152f64c2 AC |
2738 | -- If the check appears within the predicate function itself, it means |
2739 | -- that the user specified a check whose formal is the predicated | |
2740 | -- subtype itself, rather than some covering type. This is likely to be | |
8861bdd5 SB |
2741 | -- a common error, and thus deserves a warning. We want to emit this |
2742 | -- warning even if predicate checking is disabled (in which case the | |
2743 | -- warning is still useful even if it is not strictly accurate). | |
8e1e62e3 | 2744 | |
152f64c2 AC |
2745 | if Present (S) and then S = Predicate_Function (Typ) then |
2746 | Error_Msg_NE | |
2747 | ("predicate check includes a call to& that requires a " | |
2748 | & "predicate check??", Parent (N), Fun); | |
2749 | Error_Msg_N | |
2750 | ("\this will result in infinite recursion??", Parent (N)); | |
0929eaeb | 2751 | |
152f64c2 | 2752 | if Is_First_Subtype (Typ) then |
6eca51ce | 2753 | Error_Msg_NE |
152f64c2 AC |
2754 | ("\use an explicit subtype of& to carry the predicate", |
2755 | Parent (N), Typ); | |
2756 | end if; | |
6eca51ce | 2757 | |
8861bdd5 SB |
2758 | if not Check_Disabled then |
2759 | Insert_Action (N, | |
2760 | Make_Raise_Storage_Error (Sloc (N), | |
2761 | Reason => SE_Infinite_Recursion)); | |
2762 | return; | |
2763 | end if; | |
2764 | end if; | |
2765 | ||
2766 | if Check_Disabled then | |
152f64c2 AC |
2767 | return; |
2768 | end if; | |
0929eaeb | 2769 | |
152f64c2 | 2770 | -- Normal case of predicate active |
804fc056 | 2771 | |
152f64c2 AC |
2772 | -- If the expression is an IN parameter, the predicate will have |
2773 | -- been applied at the point of call. An additional check would | |
2774 | -- be redundant, or will lead to out-of-scope references if the | |
2775 | -- call appears within an aspect specification for a precondition. | |
db626148 | 2776 | |
152f64c2 AC |
2777 | -- However, if the reference is within the body of the subprogram |
2778 | -- that declares the formal, the predicate can safely be applied, | |
2779 | -- which may be necessary for a nested call whose formal has a | |
2780 | -- different predicate. | |
db626148 | 2781 | |
152f64c2 AC |
2782 | if Is_Entity_Name (N) |
2783 | and then Ekind (Entity (N)) = E_In_Parameter | |
2784 | then | |
2785 | declare | |
2786 | In_Body : Boolean := False; | |
2787 | P : Node_Id := Parent (N); | |
db626148 | 2788 | |
152f64c2 AC |
2789 | begin |
2790 | while Present (P) loop | |
2791 | if Nkind (P) = N_Subprogram_Body | |
2792 | and then | |
2793 | ((Present (Corresponding_Spec (P)) | |
2794 | and then | |
2795 | Corresponding_Spec (P) = Scope (Entity (N))) | |
2796 | or else | |
2797 | Defining_Unit_Name (Specification (P)) = | |
2798 | Scope (Entity (N))) | |
2799 | then | |
2800 | In_Body := True; | |
2801 | exit; | |
2802 | end if; | |
f197d2f2 | 2803 | |
152f64c2 AC |
2804 | P := Parent (P); |
2805 | end loop; | |
804fc056 | 2806 | |
152f64c2 | 2807 | if not In_Body then |
405b907c AC |
2808 | return; |
2809 | end if; | |
152f64c2 AC |
2810 | end; |
2811 | end if; | |
405b907c | 2812 | |
152f64c2 AC |
2813 | -- If the type has a static predicate and the expression is known |
2814 | -- at compile time, see if the expression satisfies the predicate. | |
b97813ab | 2815 | |
152f64c2 | 2816 | Check_Expression_Against_Static_Predicate (N, Typ); |
405b907c | 2817 | |
152f64c2 AC |
2818 | if not Expander_Active then |
2819 | return; | |
2820 | end if; | |
b97813ab | 2821 | |
152f64c2 AC |
2822 | Par := Parent (N); |
2823 | if Nkind (Par) = N_Qualified_Expression then | |
2824 | Par := Parent (Par); | |
2825 | end if; | |
b97813ab | 2826 | |
152f64c2 AC |
2827 | -- For an entity of the type, generate a call to the predicate |
2828 | -- function, unless its type is an actual subtype, which is not | |
2829 | -- visible outside of the enclosing subprogram. | |
b97813ab | 2830 | |
152f64c2 AC |
2831 | if Is_Entity_Name (N) |
2832 | and then not Is_Actual_Subtype (Typ) | |
2833 | then | |
2834 | Insert_Action (N, | |
2835 | Make_Predicate_Check | |
2836 | (Typ, New_Occurrence_Of (Entity (N), Sloc (N)))); | |
2837 | return; | |
4269edf0 | 2838 | |
4a08c95c | 2839 | elsif Nkind (N) in N_Aggregate | N_Extension_Aggregate then |
152f64c2 AC |
2840 | |
2841 | -- If the expression is an aggregate in an assignment, apply the | |
2842 | -- check to the LHS after the assignment, rather than create a | |
2843 | -- redundant temporary. This is only necessary in rare cases | |
2844 | -- of array types (including strings) initialized with an | |
2845 | -- aggregate with an "others" clause, either coming from source | |
2846 | -- or generated by an Initialize_Scalars pragma. | |
2847 | ||
2848 | if Nkind (Par) = N_Assignment_Statement then | |
2849 | Insert_Action_After (Par, | |
2850 | Make_Predicate_Check | |
2851 | (Typ, Duplicate_Subexpr (Name (Par)))); | |
2852 | return; | |
2853 | ||
2a50a4d4 EB |
2854 | -- Similarly, if the expression is a qualified aggregate in an |
2855 | -- allocator, apply the check to the dereference of the access | |
2856 | -- value, rather than create a temporary. This is necessary for | |
2857 | -- inherently limited types, for which the temporary is illegal. | |
2858 | ||
2859 | elsif Nkind (Par) = N_Allocator then | |
2860 | declare | |
2861 | Deref : constant Node_Id := | |
2862 | Make_Explicit_Dereference (Sloc (N), | |
2863 | Prefix => Duplicate_Subexpr (Par)); | |
2864 | ||
2865 | begin | |
2866 | -- This is required by Predicate_Check_In_Scope ??? | |
2867 | ||
2868 | Preserve_Comes_From_Source (Deref, N); | |
2869 | ||
2870 | Insert_Action_After (Parent (Par), | |
2871 | Make_Predicate_Check (Typ, Deref)); | |
2872 | return; | |
2873 | end; | |
2874 | ||
152f64c2 AC |
2875 | -- Similarly, if the expression is an aggregate in an object |
2876 | -- declaration, apply it to the object after the declaration. | |
2977b006 MP |
2877 | |
2878 | -- This is only necessary in cases of tagged extensions | |
2879 | -- initialized with an aggregate with an "others => <>" clause, | |
2880 | -- when the subtypes of LHS and RHS do not statically match or | |
2881 | -- when we know the object's type will be rewritten later. | |
2882 | -- The condition for the later is copied from the | |
2883 | -- Analyze_Object_Declaration procedure when it actually builds the | |
2884 | -- subtype. | |
152f64c2 AC |
2885 | |
2886 | elsif Nkind (Par) = N_Object_Declaration then | |
2977b006 MP |
2887 | if Subtypes_Statically_Match |
2888 | (Etype (Defining_Identifier (Par)), Typ) | |
2889 | and then (Nkind (N) = N_Extension_Aggregate | |
2890 | or else (Is_Definite_Subtype (Typ) | |
2891 | and then Build_Default_Subtype_OK (Typ))) | |
2892 | then | |
2893 | Insert_Action_After (Par, | |
2894 | Make_Predicate_Check (Typ, | |
2895 | New_Occurrence_Of (Defining_Identifier (Par), Sloc (N)))); | |
2896 | return; | |
2897 | end if; | |
2898 | ||
62db841a | 2899 | end if; |
48f91b44 | 2900 | end if; |
152f64c2 AC |
2901 | |
2902 | -- If the expression is not an entity it may have side effects, | |
2903 | -- and the following call will create an object declaration for | |
2904 | -- it. We disable checks during its analysis, to prevent an | |
2905 | -- infinite recursion. | |
2906 | ||
2907 | Insert_Action (N, | |
2908 | Make_Predicate_Check | |
2909 | (Typ, Duplicate_Subexpr (N)), Suppress => All_Checks); | |
48f91b44 RD |
2910 | end Apply_Predicate_Check; |
2911 | ||
70482933 RK |
2912 | ----------------------- |
2913 | -- Apply_Range_Check -- | |
2914 | ----------------------- | |
2915 | ||
2916 | procedure Apply_Range_Check | |
ec170be1 EB |
2917 | (Expr : Node_Id; |
2918 | Target_Typ : Entity_Id; | |
2919 | Source_Typ : Entity_Id := Empty; | |
2920 | Insert_Node : Node_Id := Empty) | |
70482933 | 2921 | is |
ec170be1 EB |
2922 | Checks_On : constant Boolean := |
2923 | not Index_Checks_Suppressed (Target_Typ) | |
2924 | or else | |
2925 | not Range_Checks_Suppressed (Target_Typ); | |
2926 | ||
2927 | Loc : constant Source_Ptr := Sloc (Expr); | |
2928 | ||
2929 | Cond : Node_Id; | |
2930 | R_Cno : Node_Id; | |
2931 | R_Result : Check_Result; | |
2932 | ||
70482933 | 2933 | begin |
ec170be1 EB |
2934 | -- Only apply checks when generating code. In GNATprove mode, we do not |
2935 | -- apply the checks, but we still call Selected_Range_Checks to possibly | |
2936 | -- issue errors on SPARK code when a run-time error can be detected at | |
2937 | -- compile time. | |
2938 | ||
2939 | if not GNATprove_Mode then | |
2940 | if not Expander_Active or not Checks_On then | |
2941 | return; | |
2942 | end if; | |
2943 | end if; | |
2944 | ||
2945 | R_Result := | |
2946 | Selected_Range_Checks (Expr, Target_Typ, Source_Typ, Insert_Node); | |
2947 | ||
2948 | if GNATprove_Mode then | |
2949 | return; | |
2950 | end if; | |
2951 | ||
2952 | for J in 1 .. 2 loop | |
2953 | R_Cno := R_Result (J); | |
2954 | exit when No (R_Cno); | |
2955 | ||
2956 | -- The range check requires runtime evaluation. Depending on what its | |
2957 | -- triggering condition is, the check may be converted into a compile | |
2958 | -- time constraint check. | |
2959 | ||
2960 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2961 | and then Present (Condition (R_Cno)) | |
2962 | then | |
2963 | Cond := Condition (R_Cno); | |
2964 | ||
2965 | -- Insert the range check before the related context. Note that | |
2966 | -- this action analyses the triggering condition. | |
2967 | ||
2968 | if Present (Insert_Node) then | |
2969 | Insert_Action (Insert_Node, R_Cno); | |
2970 | else | |
2971 | Insert_Action (Expr, R_Cno); | |
2972 | end if; | |
2973 | ||
2974 | -- The triggering condition evaluates to True, the range check | |
2975 | -- can be converted into a compile time constraint check. | |
2976 | ||
2977 | if Is_Entity_Name (Cond) | |
2978 | and then Entity (Cond) = Standard_True | |
2979 | then | |
2980 | -- Since an N_Range is technically not an expression, we have | |
2981 | -- to set one of the bounds to C_E and then just flag the | |
2982 | -- N_Range. The warning message will point to the lower bound | |
2983 | -- and complain about a range, which seems OK. | |
2984 | ||
2985 | if Nkind (Expr) = N_Range then | |
2986 | Apply_Compile_Time_Constraint_Error | |
2987 | (Low_Bound (Expr), | |
2988 | "static range out of bounds of}??", | |
2989 | CE_Range_Check_Failed, | |
2990 | Ent => Target_Typ, | |
2991 | Typ => Target_Typ); | |
2992 | ||
2993 | Set_Raises_Constraint_Error (Expr); | |
2994 | ||
2995 | else | |
2996 | Apply_Compile_Time_Constraint_Error | |
2997 | (Expr, | |
2998 | "static value out of range of}??", | |
2999 | CE_Range_Check_Failed, | |
3000 | Ent => Target_Typ, | |
3001 | Typ => Target_Typ); | |
3002 | end if; | |
3003 | end if; | |
3004 | ||
3005 | -- The range check raises Constraint_Error explicitly | |
3006 | ||
3007 | elsif Present (Insert_Node) then | |
3008 | R_Cno := | |
3009 | Make_Raise_Constraint_Error (Sloc (Insert_Node), | |
3010 | Reason => CE_Range_Check_Failed); | |
3011 | ||
3012 | Insert_Action (Insert_Node, R_Cno); | |
3013 | ||
3014 | else | |
3015 | Install_Static_Check (R_Cno, Loc); | |
3016 | end if; | |
3017 | end loop; | |
70482933 RK |
3018 | end Apply_Range_Check; |
3019 | ||
3020 | ------------------------------ | |
3021 | -- Apply_Scalar_Range_Check -- | |
3022 | ------------------------------ | |
3023 | ||
675d6070 TQ |
3024 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
3025 | -- off if it is already set on. | |
70482933 RK |
3026 | |
3027 | procedure Apply_Scalar_Range_Check | |
3028 | (Expr : Node_Id; | |
3029 | Target_Typ : Entity_Id; | |
3030 | Source_Typ : Entity_Id := Empty; | |
3031 | Fixed_Int : Boolean := False) | |
3032 | is | |
3033 | Parnt : constant Node_Id := Parent (Expr); | |
3034 | S_Typ : Entity_Id; | |
3035 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
3036 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
70482933 RK |
3037 | |
3038 | Is_Subscr_Ref : Boolean; | |
3039 | -- Set true if Expr is a subscript | |
3040 | ||
3041 | Is_Unconstrained_Subscr_Ref : Boolean; | |
3042 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
3043 | -- case we do not attempt to do an analysis of the value against the | |
3044 | -- range of the subscript, since we don't know the actual subtype. | |
3045 | ||
3046 | Int_Real : Boolean; | |
675d6070 TQ |
3047 | -- Set to True if Expr should be regarded as a real value even though |
3048 | -- the type of Expr might be discrete. | |
70482933 | 3049 | |
520c0201 AC |
3050 | procedure Bad_Value (Warn : Boolean := False); |
3051 | -- Procedure called if value is determined to be out of range. Warn is | |
3052 | -- True to force a warning instead of an error, even when SPARK_Mode is | |
3053 | -- On. | |
70482933 | 3054 | |
fbf5a39b AC |
3055 | --------------- |
3056 | -- Bad_Value -- | |
3057 | --------------- | |
3058 | ||
520c0201 | 3059 | procedure Bad_Value (Warn : Boolean := False) is |
70482933 RK |
3060 | begin |
3061 | Apply_Compile_Time_Constraint_Error | |
685bc70f | 3062 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
520c0201 AC |
3063 | Ent => Target_Typ, |
3064 | Typ => Target_Typ, | |
3065 | Warn => Warn); | |
70482933 RK |
3066 | end Bad_Value; |
3067 | ||
fbf5a39b AC |
3068 | -- Start of processing for Apply_Scalar_Range_Check |
3069 | ||
70482933 | 3070 | begin |
939c12d2 | 3071 | -- Return if check obviously not needed |
70482933 | 3072 | |
939c12d2 RD |
3073 | if |
3074 | -- Not needed inside generic | |
70482933 | 3075 | |
939c12d2 RD |
3076 | Inside_A_Generic |
3077 | ||
3078 | -- Not needed if previous error | |
3079 | ||
3080 | or else Target_Typ = Any_Type | |
3081 | or else Nkind (Expr) = N_Error | |
3082 | ||
3083 | -- Not needed for non-scalar type | |
3084 | ||
3085 | or else not Is_Scalar_Type (Target_Typ) | |
3086 | ||
3087 | -- Not needed if we know node raises CE already | |
3088 | ||
3089 | or else Raises_Constraint_Error (Expr) | |
70482933 RK |
3090 | then |
3091 | return; | |
3092 | end if; | |
3093 | ||
3094 | -- Now, see if checks are suppressed | |
3095 | ||
3096 | Is_Subscr_Ref := | |
3097 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
3098 | ||
3099 | if Is_Subscr_Ref then | |
3100 | Arr := Prefix (Parnt); | |
3101 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
ba759acd | 3102 | |
f4f92d9d | 3103 | if Is_Access_Type (Arr_Typ) then |
05c1e7d2 | 3104 | Arr_Typ := Designated_Type (Arr_Typ); |
f4f92d9d | 3105 | end if; |
70482933 RK |
3106 | end if; |
3107 | ||
3108 | if not Do_Range_Check (Expr) then | |
3109 | ||
3110 | -- Subscript reference. Check for Index_Checks suppressed | |
3111 | ||
3112 | if Is_Subscr_Ref then | |
3113 | ||
3114 | -- Check array type and its base type | |
3115 | ||
3116 | if Index_Checks_Suppressed (Arr_Typ) | |
fbf5a39b | 3117 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
70482933 RK |
3118 | then |
3119 | return; | |
3120 | ||
3121 | -- Check array itself if it is an entity name | |
3122 | ||
3123 | elsif Is_Entity_Name (Arr) | |
fbf5a39b | 3124 | and then Index_Checks_Suppressed (Entity (Arr)) |
70482933 RK |
3125 | then |
3126 | return; | |
3127 | ||
3128 | -- Check expression itself if it is an entity name | |
3129 | ||
3130 | elsif Is_Entity_Name (Expr) | |
fbf5a39b | 3131 | and then Index_Checks_Suppressed (Entity (Expr)) |
70482933 RK |
3132 | then |
3133 | return; | |
3134 | end if; | |
3135 | ||
3136 | -- All other cases, check for Range_Checks suppressed | |
3137 | ||
3138 | else | |
3139 | -- Check target type and its base type | |
3140 | ||
3141 | if Range_Checks_Suppressed (Target_Typ) | |
fbf5a39b | 3142 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
70482933 RK |
3143 | then |
3144 | return; | |
3145 | ||
3146 | -- Check expression itself if it is an entity name | |
3147 | ||
3148 | elsif Is_Entity_Name (Expr) | |
fbf5a39b | 3149 | and then Range_Checks_Suppressed (Entity (Expr)) |
70482933 RK |
3150 | then |
3151 | return; | |
3152 | ||
675d6070 TQ |
3153 | -- If Expr is part of an assignment statement, then check left |
3154 | -- side of assignment if it is an entity name. | |
70482933 RK |
3155 | |
3156 | elsif Nkind (Parnt) = N_Assignment_Statement | |
3157 | and then Is_Entity_Name (Name (Parnt)) | |
fbf5a39b | 3158 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
70482933 RK |
3159 | then |
3160 | return; | |
3161 | end if; | |
3162 | end if; | |
3163 | end if; | |
3164 | ||
fbf5a39b AC |
3165 | -- Do not set range checks if they are killed |
3166 | ||
3167 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
3168 | and then Kill_Range_Check (Expr) | |
3169 | then | |
3170 | return; | |
3171 | end if; | |
3172 | ||
3173 | -- Do not set range checks for any values from System.Scalar_Values | |
a90bd866 | 3174 | -- since the whole idea of such values is to avoid checking them. |
fbf5a39b AC |
3175 | |
3176 | if Is_Entity_Name (Expr) | |
3177 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
3178 | then | |
3179 | return; | |
3180 | end if; | |
3181 | ||
70482933 RK |
3182 | -- Now see if we need a check |
3183 | ||
3184 | if No (Source_Typ) then | |
3185 | S_Typ := Etype (Expr); | |
3186 | else | |
3187 | S_Typ := Source_Typ; | |
3188 | end if; | |
3189 | ||
3190 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
3191 | return; | |
3192 | end if; | |
3193 | ||
3194 | Is_Unconstrained_Subscr_Ref := | |
3195 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
3196 | ||
347c766a | 3197 | -- Special checks for floating-point type |
70482933 | 3198 | |
347c766a RD |
3199 | if Is_Floating_Point_Type (S_Typ) then |
3200 | ||
3201 | -- Always do a range check if the source type includes infinities and | |
3202 | -- the target type does not include infinities. We do not do this if | |
3203 | -- range checks are killed. | |
aff557c7 AC |
3204 | -- If the expression is a literal and the bounds of the type are |
3205 | -- static constants it may be possible to optimize the check. | |
347c766a RD |
3206 | |
3207 | if Has_Infinities (S_Typ) | |
3208 | and then not Has_Infinities (Target_Typ) | |
3209 | then | |
aff557c7 AC |
3210 | -- If the expression is a literal and the bounds of the type are |
3211 | -- static constants it may be possible to optimize the check. | |
3212 | ||
3213 | if Nkind (Expr) = N_Real_Literal then | |
3214 | declare | |
3215 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
3216 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
3217 | ||
3218 | begin | |
3219 | if Compile_Time_Known_Value (Tlo) | |
3220 | and then Compile_Time_Known_Value (Thi) | |
3221 | and then Expr_Value_R (Expr) >= Expr_Value_R (Tlo) | |
3222 | and then Expr_Value_R (Expr) <= Expr_Value_R (Thi) | |
3223 | then | |
3224 | return; | |
3225 | else | |
3226 | Enable_Range_Check (Expr); | |
3227 | end if; | |
3228 | end; | |
3229 | ||
3230 | else | |
3231 | Enable_Range_Check (Expr); | |
3232 | end if; | |
347c766a | 3233 | end if; |
70482933 RK |
3234 | end if; |
3235 | ||
675d6070 | 3236 | -- Return if we know expression is definitely in the range of the target |
84c54629 EB |
3237 | -- type as determined by Determine_Range_To_Discrete. Right now we only |
3238 | -- do this for discrete target types, i.e. neither for fixed-point nor | |
3239 | -- for floating-point types. But the additional less precise tests below | |
3240 | -- catch these cases. | |
d8ee014f | 3241 | |
675d6070 TQ |
3242 | -- Note: skip this if we are given a source_typ, since the point of |
3243 | -- supplying a Source_Typ is to stop us looking at the expression. | |
3244 | -- We could sharpen this test to be out parameters only ??? | |
70482933 RK |
3245 | |
3246 | if Is_Discrete_Type (Target_Typ) | |
70482933 RK |
3247 | and then not Is_Unconstrained_Subscr_Ref |
3248 | and then No (Source_Typ) | |
3249 | then | |
3250 | declare | |
70482933 | 3251 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); |
94295b25 | 3252 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); |
70482933 RK |
3253 | |
3254 | begin | |
3255 | if Compile_Time_Known_Value (Tlo) | |
3256 | and then Compile_Time_Known_Value (Thi) | |
3257 | then | |
fbf5a39b | 3258 | declare |
dcd5fd67 | 3259 | OK : Boolean := False; -- initialize to prevent warning |
fbf5a39b | 3260 | Hiv : constant Uint := Expr_Value (Thi); |
94295b25 | 3261 | Lov : constant Uint := Expr_Value (Tlo); |
dcd5fd67 PMR |
3262 | Hi : Uint := No_Uint; |
3263 | Lo : Uint := No_Uint; | |
70482933 | 3264 | |
fbf5a39b | 3265 | begin |
94295b25 AC |
3266 | -- If range is null, we for sure have a constraint error (we |
3267 | -- don't even need to look at the value involved, since all | |
3268 | -- possible values will raise CE). | |
fbf5a39b AC |
3269 | |
3270 | if Lov > Hiv then | |
39f0fa29 | 3271 | |
520c0201 AC |
3272 | -- When SPARK_Mode is On, force a warning instead of |
3273 | -- an error in that case, as this likely corresponds | |
3274 | -- to deactivated code. | |
3275 | ||
3276 | Bad_Value (Warn => SPARK_Mode = On); | |
3277 | ||
fbf5a39b AC |
3278 | return; |
3279 | end if; | |
3280 | ||
3281 | -- Otherwise determine range of value | |
3282 | ||
84c54629 EB |
3283 | Determine_Range_To_Discrete |
3284 | (Expr, OK, Lo, Hi, Fixed_Int, Assume_Valid => True); | |
fbf5a39b AC |
3285 | |
3286 | if OK then | |
3287 | ||
3288 | -- If definitely in range, all OK | |
70482933 | 3289 | |
70482933 RK |
3290 | if Lo >= Lov and then Hi <= Hiv then |
3291 | return; | |
3292 | ||
fbf5a39b AC |
3293 | -- If definitely not in range, warn |
3294 | ||
70482933 | 3295 | elsif Lov > Hi or else Hiv < Lo then |
0c506265 | 3296 | |
88ad52c9 AC |
3297 | -- Ignore out of range values for System.Priority in |
3298 | -- CodePeer mode since the actual target compiler may | |
3299 | -- provide a wider range. | |
3300 | ||
3301 | if not CodePeer_Mode | |
a9e48b0d | 3302 | or else not Is_RTE (Target_Typ, RE_Priority) |
88ad52c9 AC |
3303 | then |
3304 | Bad_Value; | |
3305 | end if; | |
3306 | ||
70482933 | 3307 | return; |
fbf5a39b AC |
3308 | |
3309 | -- Otherwise we don't know | |
3310 | ||
3311 | else | |
3312 | null; | |
70482933 | 3313 | end if; |
fbf5a39b AC |
3314 | end if; |
3315 | end; | |
70482933 RK |
3316 | end if; |
3317 | end; | |
3318 | end if; | |
3319 | ||
3320 | Int_Real := | |
3321 | Is_Floating_Point_Type (S_Typ) | |
3322 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
3323 | ||
3324 | -- Check if we can determine at compile time whether Expr is in the | |
fbf5a39b AC |
3325 | -- range of the target type. Note that if S_Typ is within the bounds |
3326 | -- of Target_Typ then this must be the case. This check is meaningful | |
84c54629 EB |
3327 | -- only if this is not a conversion between integer and real types, |
3328 | -- unless for a fixed-point type if Fixed_Int is set. | |
70482933 RK |
3329 | |
3330 | if not Is_Unconstrained_Subscr_Ref | |
84c54629 EB |
3331 | and then (Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
3332 | or else (Fixed_Int and then Is_Discrete_Type (Target_Typ))) | |
70482933 | 3333 | and then |
c27f2f15 | 3334 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
6d0b56ad AC |
3335 | |
3336 | -- Also check if the expression itself is in the range of the | |
3337 | -- target type if it is a known at compile time value. We skip | |
3338 | -- this test if S_Typ is set since for OUT and IN OUT parameters | |
3339 | -- the Expr itself is not relevant to the checking. | |
3340 | ||
70482933 | 3341 | or else |
6d0b56ad AC |
3342 | (No (Source_Typ) |
3343 | and then Is_In_Range (Expr, Target_Typ, | |
3344 | Assume_Valid => True, | |
3345 | Fixed_Int => Fixed_Int, | |
3346 | Int_Real => Int_Real))) | |
70482933 RK |
3347 | then |
3348 | return; | |
3349 | ||
c800f862 RD |
3350 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
3351 | Assume_Valid => True, | |
3352 | Fixed_Int => Fixed_Int, | |
3353 | Int_Real => Int_Real) | |
3354 | then | |
70482933 RK |
3355 | Bad_Value; |
3356 | return; | |
3357 | ||
347c766a | 3358 | -- Floating-point case |
675d6070 TQ |
3359 | -- In the floating-point case, we only do range checks if the type is |
3360 | -- constrained. We definitely do NOT want range checks for unconstrained | |
0da343bc AC |
3361 | -- types, since we want to have infinities, except when |
3362 | -- Check_Float_Overflow is set. | |
70482933 | 3363 | |
fbf5a39b | 3364 | elsif Is_Floating_Point_Type (S_Typ) then |
0da343bc | 3365 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then |
fbf5a39b AC |
3366 | Enable_Range_Check (Expr); |
3367 | end if; | |
70482933 | 3368 | |
fbf5a39b | 3369 | -- For all other cases we enable a range check unconditionally |
70482933 RK |
3370 | |
3371 | else | |
3372 | Enable_Range_Check (Expr); | |
3373 | return; | |
3374 | end if; | |
70482933 RK |
3375 | end Apply_Scalar_Range_Check; |
3376 | ||
3377 | ---------------------------------- | |
3378 | -- Apply_Selected_Length_Checks -- | |
3379 | ---------------------------------- | |
3380 | ||
3381 | procedure Apply_Selected_Length_Checks | |
6c8e70fe | 3382 | (Expr : Node_Id; |
70482933 RK |
3383 | Target_Typ : Entity_Id; |
3384 | Source_Typ : Entity_Id; | |
3385 | Do_Static : Boolean) | |
3386 | is | |
e0666fc6 AC |
3387 | Checks_On : constant Boolean := |
3388 | not Index_Checks_Suppressed (Target_Typ) | |
3389 | or else | |
3390 | not Length_Checks_Suppressed (Target_Typ); | |
3391 | ||
6c8e70fe | 3392 | Loc : constant Source_Ptr := Sloc (Expr); |
e0666fc6 | 3393 | |
70482933 | 3394 | Cond : Node_Id; |
70482933 | 3395 | R_Cno : Node_Id; |
e0666fc6 | 3396 | R_Result : Check_Result; |
70482933 RK |
3397 | |
3398 | begin | |
66340e0e | 3399 | -- Only apply checks when generating code |
27bb7941 | 3400 | |
4a28b181 | 3401 | -- Note: this means that we lose some useful warnings if the expander |
27bb7941 | 3402 | -- is not active. |
4a28b181 | 3403 | |
66340e0e | 3404 | if not Expander_Active then |
70482933 RK |
3405 | return; |
3406 | end if; | |
3407 | ||
3408 | R_Result := | |
6c8e70fe | 3409 | Selected_Length_Checks (Expr, Target_Typ, Source_Typ, Empty); |
70482933 RK |
3410 | |
3411 | for J in 1 .. 2 loop | |
70482933 RK |
3412 | R_Cno := R_Result (J); |
3413 | exit when No (R_Cno); | |
3414 | ||
3415 | -- A length check may mention an Itype which is attached to a | |
3416 | -- subsequent node. At the top level in a package this can cause | |
3417 | -- an order-of-elaboration problem, so we make sure that the itype | |
3418 | -- is referenced now. | |
3419 | ||
3420 | if Ekind (Current_Scope) = E_Package | |
3421 | and then Is_Compilation_Unit (Current_Scope) | |
3422 | then | |
6c8e70fe | 3423 | Ensure_Defined (Target_Typ, Expr); |
70482933 RK |
3424 | |
3425 | if Present (Source_Typ) then | |
6c8e70fe | 3426 | Ensure_Defined (Source_Typ, Expr); |
70482933 | 3427 | |
6c8e70fe EB |
3428 | elsif Is_Itype (Etype (Expr)) then |
3429 | Ensure_Defined (Etype (Expr), Expr); | |
70482933 RK |
3430 | end if; |
3431 | end if; | |
3432 | ||
70482933 RK |
3433 | if Nkind (R_Cno) = N_Raise_Constraint_Error |
3434 | and then Present (Condition (R_Cno)) | |
3435 | then | |
3436 | Cond := Condition (R_Cno); | |
3437 | ||
c064e066 | 3438 | -- Case where node does not now have a dynamic check |
70482933 | 3439 | |
6c8e70fe | 3440 | if not Has_Dynamic_Length_Check (Expr) then |
c064e066 RD |
3441 | |
3442 | -- If checks are on, just insert the check | |
3443 | ||
3444 | if Checks_On then | |
6c8e70fe | 3445 | Insert_Action (Expr, R_Cno); |
c064e066 RD |
3446 | |
3447 | if not Do_Static then | |
6c8e70fe | 3448 | Set_Has_Dynamic_Length_Check (Expr); |
c064e066 RD |
3449 | end if; |
3450 | ||
3451 | -- If checks are off, then analyze the length check after | |
3452 | -- temporarily attaching it to the tree in case the relevant | |
308e6f3a | 3453 | -- condition can be evaluated at compile time. We still want a |
c064e066 RD |
3454 | -- compile time warning in this case. |
3455 | ||
3456 | else | |
6c8e70fe | 3457 | Set_Parent (R_Cno, Expr); |
c064e066 | 3458 | Analyze (R_Cno); |
70482933 | 3459 | end if; |
70482933 RK |
3460 | end if; |
3461 | ||
3462 | -- Output a warning if the condition is known to be True | |
3463 | ||
3464 | if Is_Entity_Name (Cond) | |
3465 | and then Entity (Cond) = Standard_True | |
3466 | then | |
3467 | Apply_Compile_Time_Constraint_Error | |
6c8e70fe | 3468 | (Expr, "wrong length for array of}??", |
07fc65c4 | 3469 | CE_Length_Check_Failed, |
70482933 RK |
3470 | Ent => Target_Typ, |
3471 | Typ => Target_Typ); | |
3472 | ||
3473 | -- If we were only doing a static check, or if checks are not | |
3474 | -- on, then we want to delete the check, since it is not needed. | |
3475 | -- We do this by replacing the if statement by a null statement | |
3476 | ||
3477 | elsif Do_Static or else not Checks_On then | |
11b4899f | 3478 | Remove_Warning_Messages (R_Cno); |
70482933 RK |
3479 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3480 | end if; | |
3481 | ||
3482 | else | |
3483 | Install_Static_Check (R_Cno, Loc); | |
3484 | end if; | |
70482933 | 3485 | end loop; |
70482933 RK |
3486 | end Apply_Selected_Length_Checks; |
3487 | ||
70482933 RK |
3488 | ------------------------------- |
3489 | -- Apply_Static_Length_Check -- | |
3490 | ------------------------------- | |
3491 | ||
3492 | procedure Apply_Static_Length_Check | |
3493 | (Expr : Node_Id; | |
3494 | Target_Typ : Entity_Id; | |
3495 | Source_Typ : Entity_Id := Empty) | |
3496 | is | |
3497 | begin | |
3498 | Apply_Selected_Length_Checks | |
3499 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3500 | end Apply_Static_Length_Check; | |
3501 | ||
3502 | ------------------------------------- | |
3503 | -- Apply_Subscript_Validity_Checks -- | |
3504 | ------------------------------------- | |
3505 | ||
e02c8dff SB |
3506 | procedure Apply_Subscript_Validity_Checks |
3507 | (Expr : Node_Id; | |
3508 | No_Check_Needed : Dimension_Set := Empty_Dimension_Set) is | |
70482933 RK |
3509 | Sub : Node_Id; |
3510 | ||
e02c8dff | 3511 | Dimension : Pos := 1; |
70482933 RK |
3512 | begin |
3513 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3514 | ||
3515 | -- Loop through subscripts | |
3516 | ||
3517 | Sub := First (Expressions (Expr)); | |
3518 | while Present (Sub) loop | |
3519 | ||
675d6070 TQ |
3520 | -- Check one subscript. Note that we do not worry about enumeration |
3521 | -- type with holes, since we will convert the value to a Pos value | |
3522 | -- for the subscript, and that convert will do the necessary validity | |
3523 | -- check. | |
70482933 | 3524 | |
8f563162 | 3525 | if No_Check_Needed = Empty_Dimension_Set |
e02c8dff SB |
3526 | or else not No_Check_Needed.Elements (Dimension) |
3527 | then | |
3528 | Ensure_Valid (Sub, Holes_OK => True); | |
3529 | end if; | |
70482933 RK |
3530 | |
3531 | -- Move to next subscript | |
3532 | ||
cbbe41d1 | 3533 | Next (Sub); |
e02c8dff | 3534 | Dimension := Dimension + 1; |
70482933 RK |
3535 | end loop; |
3536 | end Apply_Subscript_Validity_Checks; | |
3537 | ||
3538 | ---------------------------------- | |
3539 | -- Apply_Type_Conversion_Checks -- | |
3540 | ---------------------------------- | |
3541 | ||
3542 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3543 | Target_Type : constant Entity_Id := Etype (N); | |
3544 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
fbf5a39b | 3545 | Expr : constant Node_Id := Expression (N); |
2c1a2cf3 RD |
3546 | |
3547 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
1197ddb1 AC |
3548 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3549 | -- full view might have discriminants with defaults, so we need the | |
3550 | -- full view here to retrieve the constraints. | |
70482933 | 3551 | |
4068698c JS |
3552 | procedure Make_Discriminant_Constraint_Check |
3553 | (Target_Type : Entity_Id; | |
3554 | Expr_Type : Entity_Id); | |
3555 | -- Generate a discriminant check based on the target type and expression | |
3556 | -- type for Expr. | |
3557 | ||
3558 | ---------------------------------------- | |
3559 | -- Make_Discriminant_Constraint_Check -- | |
3560 | ---------------------------------------- | |
3561 | ||
3562 | procedure Make_Discriminant_Constraint_Check | |
3563 | (Target_Type : Entity_Id; | |
3564 | Expr_Type : Entity_Id) | |
3565 | is | |
3566 | Loc : constant Source_Ptr := Sloc (N); | |
3567 | Cond : Node_Id; | |
3568 | Constraint : Elmt_Id; | |
3569 | Discr_Value : Node_Id; | |
3570 | Discr : Entity_Id; | |
3571 | ||
3572 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3573 | Old_Constraints : constant Elist_Id := | |
3574 | Discriminant_Constraint (Expr_Type); | |
3575 | ||
3576 | begin | |
3577 | -- Build an actual discriminant constraint list using the stored | |
3578 | -- constraint, to verify that the expression of the parent type | |
3579 | -- satisfies the constraints imposed by the (unconstrained) derived | |
3580 | -- type. This applies to value conversions, not to view conversions | |
3581 | -- of tagged types. | |
3582 | ||
3583 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); | |
3584 | while Present (Constraint) loop | |
3585 | Discr_Value := Node (Constraint); | |
3586 | ||
3587 | if Is_Entity_Name (Discr_Value) | |
3588 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3589 | then | |
3590 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3591 | ||
3592 | if Present (Discr) | |
3593 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3594 | then | |
3595 | -- Parent is constrained by new discriminant. Obtain | |
3596 | -- Value of original discriminant in expression. If the | |
3597 | -- new discriminant has been used to constrain more than | |
3598 | -- one of the stored discriminants, this will provide the | |
3599 | -- required consistency check. | |
3600 | ||
3601 | Append_Elmt | |
3602 | (Make_Selected_Component (Loc, | |
3603 | Prefix => | |
3604 | Duplicate_Subexpr_No_Checks | |
3605 | (Expr, Name_Req => True), | |
3606 | Selector_Name => | |
3607 | Make_Identifier (Loc, Chars (Discr))), | |
3608 | New_Constraints); | |
3609 | ||
3610 | else | |
3611 | -- Discriminant of more remote ancestor ??? | |
3612 | ||
3613 | return; | |
3614 | end if; | |
3615 | ||
3616 | -- Derived type definition has an explicit value for this | |
3617 | -- stored discriminant. | |
3618 | ||
3619 | else | |
3620 | Append_Elmt | |
3621 | (Duplicate_Subexpr_No_Checks (Discr_Value), | |
3622 | New_Constraints); | |
3623 | end if; | |
3624 | ||
3625 | Next_Elmt (Constraint); | |
3626 | end loop; | |
3627 | ||
3628 | -- Use the unconstrained expression type to retrieve the | |
3629 | -- discriminants of the parent, and apply momentarily the | |
3630 | -- discriminant constraint synthesized above. | |
3631 | ||
3632 | -- Note: We use Expr_Type instead of Target_Type since the number of | |
3633 | -- actual discriminants may be different due to the presence of | |
3634 | -- stored discriminants and cause Build_Discriminant_Checks to fail. | |
3635 | ||
3636 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3637 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3638 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3639 | ||
55b93bbc JS |
3640 | -- Conversion between access types requires that we check for null |
3641 | -- before checking discriminants. | |
3642 | ||
3643 | if Is_Access_Type (Etype (Expr)) then | |
3644 | Cond := Make_And_Then (Loc, | |
3645 | Left_Opnd => | |
3646 | Make_Op_Ne (Loc, | |
3647 | Left_Opnd => | |
3648 | Duplicate_Subexpr_No_Checks | |
3649 | (Expr, Name_Req => True), | |
3650 | Right_Opnd => Make_Null (Loc)), | |
3651 | Right_Opnd => Cond); | |
3652 | end if; | |
3653 | ||
4068698c JS |
3654 | Insert_Action (N, |
3655 | Make_Raise_Constraint_Error (Loc, | |
3656 | Condition => Cond, | |
3657 | Reason => CE_Discriminant_Check_Failed)); | |
3658 | end Make_Discriminant_Constraint_Check; | |
3659 | ||
3660 | -- Start of processing for Apply_Type_Conversion_Checks | |
3661 | ||
70482933 RK |
3662 | begin |
3663 | if Inside_A_Generic then | |
3664 | return; | |
3665 | ||
07fc65c4 | 3666 | -- Skip these checks if serious errors detected, there are some nasty |
70482933 RK |
3667 | -- situations of incomplete trees that blow things up. |
3668 | ||
07fc65c4 | 3669 | elsif Serious_Errors_Detected > 0 then |
70482933 RK |
3670 | return; |
3671 | ||
8e1e62e3 AC |
3672 | -- Never generate discriminant checks for Unchecked_Union types |
3673 | ||
3674 | elsif Present (Expr_Type) | |
3675 | and then Is_Unchecked_Union (Expr_Type) | |
3676 | then | |
3677 | return; | |
3678 | ||
675d6070 TQ |
3679 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3680 | -- range check if we cannot be sure that Expr is in the base type of | |
3681 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3682 | -- are not quite the same condition from an implementation point of | |
3683 | -- view, but clearly the second includes the first. | |
70482933 RK |
3684 | |
3685 | elsif Is_Scalar_Type (Target_Type) then | |
3686 | declare | |
23a9215f | 3687 | Conv_OK : constant Boolean := Conversion_OK (N); |
675d6070 | 3688 | -- If the Conversion_OK flag on the type conversion is set and no |
8e1e62e3 AC |
3689 | -- floating-point type is involved in the type conversion then |
3690 | -- fixed-point values must be read as integral values. | |
70482933 | 3691 | |
7324bf49 | 3692 | Float_To_Int : constant Boolean := |
15f0f591 AC |
3693 | Is_Floating_Point_Type (Expr_Type) |
3694 | and then Is_Integer_Type (Target_Type); | |
7324bf49 | 3695 | |
70482933 | 3696 | begin |
70482933 | 3697 | if not Overflow_Checks_Suppressed (Target_Base) |
a7f1b24f | 3698 | and then not Overflow_Checks_Suppressed (Target_Type) |
1c7717c3 | 3699 | and then not |
c27f2f15 | 3700 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
7324bf49 | 3701 | and then not Float_To_Int |
70482933 | 3702 | then |
82e5c243 | 3703 | -- A small optimization: the attribute 'Pos applied to an |
0929d66b AC |
3704 | -- enumeration type has a known range, even though its type is |
3705 | -- Universal_Integer. So in numeric conversions it is usually | |
3706 | -- within range of the target integer type. Use the static | |
3707 | -- bounds of the base types to check. Disable this optimization | |
90b9052e GD |
3708 | -- in case of a descendant of a generic formal discrete type, |
3709 | -- because we don't necessarily know the upper bound yet. | |
f8981f19 AC |
3710 | |
3711 | if Nkind (Expr) = N_Attribute_Reference | |
3712 | and then Attribute_Name (Expr) = Name_Pos | |
3713 | and then Is_Enumeration_Type (Etype (Prefix (Expr))) | |
90b9052e GD |
3714 | and then |
3715 | not Is_Generic_Type (Root_Type (Etype (Prefix (Expr)))) | |
f8981f19 AC |
3716 | and then Is_Integer_Type (Target_Type) |
3717 | then | |
3718 | declare | |
82e5c243 AC |
3719 | Enum_T : constant Entity_Id := |
3720 | Root_Type (Etype (Prefix (Expr))); | |
3721 | Int_T : constant Entity_Id := Base_Type (Target_Type); | |
3722 | Last_I : constant Uint := | |
3723 | Intval (High_Bound (Scalar_Range (Int_T))); | |
3724 | Last_E : Uint; | |
f8981f19 AC |
3725 | |
3726 | begin | |
82e5c243 | 3727 | -- Character types have no explicit literals, so we use |
f8981f19 AC |
3728 | -- the known number of characters in the type. |
3729 | ||
3730 | if Root_Type (Enum_T) = Standard_Character then | |
3731 | Last_E := UI_From_Int (255); | |
3732 | ||
3733 | elsif Enum_T = Standard_Wide_Character | |
3734 | or else Enum_T = Standard_Wide_Wide_Character | |
3735 | then | |
3736 | Last_E := UI_From_Int (65535); | |
3737 | ||
3738 | else | |
82e5c243 AC |
3739 | Last_E := |
3740 | Enumeration_Pos | |
f8981f19 AC |
3741 | (Entity (High_Bound (Scalar_Range (Enum_T)))); |
3742 | end if; | |
3743 | ||
17ea7fad | 3744 | if Last_E > Last_I then |
f8981f19 AC |
3745 | Activate_Overflow_Check (N); |
3746 | end if; | |
3747 | end; | |
f8981f19 AC |
3748 | else |
3749 | Activate_Overflow_Check (N); | |
3750 | end if; | |
70482933 RK |
3751 | end if; |
3752 | ||
3753 | if not Range_Checks_Suppressed (Target_Type) | |
3754 | and then not Range_Checks_Suppressed (Expr_Type) | |
3755 | then | |
d8ee014f YM |
3756 | if Float_To_Int |
3757 | and then not GNATprove_Mode | |
3758 | then | |
7324bf49 AC |
3759 | Apply_Float_Conversion_Check (Expr, Target_Type); |
3760 | else | |
84c54629 EB |
3761 | -- Raw conversions involving fixed-point types are expanded |
3762 | -- separately and do not need a Range_Check flag yet, except | |
3763 | -- in GNATprove_Mode where this expansion is not performed. | |
3764 | -- This does not apply to conversion where fixed-point types | |
3765 | -- are treated as integers, which are precisely generated by | |
3766 | -- this expansion. | |
241848fd | 3767 | |
94a98e80 | 3768 | if GNATprove_Mode |
84c54629 | 3769 | or else Conv_OK |
8113b0c7 EB |
3770 | or else (not Is_Fixed_Point_Type (Expr_Type) |
3771 | and then not Is_Fixed_Point_Type (Target_Type)) | |
241848fd ES |
3772 | then |
3773 | Apply_Scalar_Range_Check | |
3774 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
3775 | ||
3776 | else | |
8113b0c7 | 3777 | Set_Do_Range_Check (Expr, False); |
241848fd | 3778 | end if; |
b2009d46 AC |
3779 | |
3780 | -- If the target type has predicates, we need to indicate | |
8e1e62e3 AC |
3781 | -- the need for a check, even if Determine_Range finds that |
3782 | -- the value is within bounds. This may be the case e.g for | |
3783 | -- a division with a constant denominator. | |
b2009d46 AC |
3784 | |
3785 | if Has_Predicates (Target_Type) then | |
3786 | Enable_Range_Check (Expr); | |
3787 | end if; | |
7324bf49 | 3788 | end if; |
70482933 RK |
3789 | end if; |
3790 | end; | |
3791 | ||
4068698c JS |
3792 | -- Generate discriminant constraint checks for access types on the |
3793 | -- designated target type's stored constraints. | |
70482933 | 3794 | |
4068698c | 3795 | -- Do we need to generate subtype predicate checks here as well ??? |
70482933 | 3796 | |
4068698c JS |
3797 | elsif Comes_From_Source (N) |
3798 | and then Ekind (Target_Type) = E_General_Access_Type | |
70482933 | 3799 | |
4068698c JS |
3800 | -- Check that both of the designated types have known discriminants, |
3801 | -- and that such checks on the target type are not suppressed. | |
70482933 | 3802 | |
4068698c JS |
3803 | and then Has_Discriminants (Directly_Designated_Type (Target_Type)) |
3804 | and then Has_Discriminants (Directly_Designated_Type (Expr_Type)) | |
3805 | and then not Discriminant_Checks_Suppressed | |
3806 | (Directly_Designated_Type (Target_Type)) | |
70482933 | 3807 | |
4068698c | 3808 | -- Verify the designated type of the target has stored constraints |
70482933 | 3809 | |
4068698c JS |
3810 | and then Present |
3811 | (Stored_Constraint (Directly_Designated_Type (Target_Type))) | |
3812 | then | |
3813 | Make_Discriminant_Constraint_Check | |
3814 | (Target_Type => Directly_Designated_Type (Target_Type), | |
3815 | Expr_Type => Directly_Designated_Type (Expr_Type)); | |
70482933 | 3816 | |
4068698c | 3817 | -- Create discriminant checks for the Target_Type's stored constraints |
70482933 | 3818 | |
4068698c JS |
3819 | elsif Comes_From_Source (N) |
3820 | and then not Discriminant_Checks_Suppressed (Target_Type) | |
3821 | and then Is_Record_Type (Target_Type) | |
3822 | and then Is_Derived_Type (Target_Type) | |
3823 | and then not Is_Tagged_Type (Target_Type) | |
3824 | and then not Is_Constrained (Target_Type) | |
3825 | and then Present (Stored_Constraint (Target_Type)) | |
3826 | then | |
3827 | Make_Discriminant_Constraint_Check (Target_Type, Expr_Type); | |
70482933 | 3828 | |
8bfbd380 AC |
3829 | -- For arrays, checks are set now, but conversions are applied during |
3830 | -- expansion, to take into accounts changes of representation. The | |
3831 | -- checks become range checks on the base type or length checks on the | |
3832 | -- subtype, depending on whether the target type is unconstrained or | |
83851b23 AC |
3833 | -- constrained. Note that the range check is put on the expression of a |
3834 | -- type conversion, while the length check is put on the type conversion | |
3835 | -- itself. | |
8bfbd380 AC |
3836 | |
3837 | elsif Is_Array_Type (Target_Type) then | |
3838 | if Is_Constrained (Target_Type) then | |
3839 | Set_Do_Length_Check (N); | |
3840 | else | |
3841 | Set_Do_Range_Check (Expr); | |
3842 | end if; | |
70482933 | 3843 | end if; |
70482933 RK |
3844 | end Apply_Type_Conversion_Checks; |
3845 | ||
3846 | ---------------------------------------------- | |
3847 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3848 | ---------------------------------------------- | |
3849 | ||
3850 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3851 | Loc : constant Source_Ptr := Sloc (N); | |
3852 | Typ : constant Entity_Id := Etype (N); | |
3853 | ||
3854 | begin | |
3855 | if Inside_A_Generic then | |
3856 | return; | |
3857 | ||
48b6386f EB |
3858 | -- Nothing to do if the result type is universal integer |
3859 | ||
3860 | elsif Typ = Universal_Integer then | |
3861 | return; | |
3862 | ||
70482933 RK |
3863 | -- Nothing to do if checks are suppressed |
3864 | ||
3865 | elsif Range_Checks_Suppressed (Typ) | |
3866 | and then Overflow_Checks_Suppressed (Typ) | |
3867 | then | |
3868 | return; | |
3869 | ||
3870 | -- Nothing to do if the attribute does not come from source. The | |
3871 | -- internal attributes we generate of this type do not need checks, | |
3872 | -- and furthermore the attempt to check them causes some circular | |
3873 | -- elaboration orders when dealing with packed types. | |
3874 | ||
3875 | elsif not Comes_From_Source (N) then | |
3876 | return; | |
3877 | ||
fbf5a39b AC |
3878 | -- If the prefix is a selected component that depends on a discriminant |
3879 | -- the check may improperly expose a discriminant instead of using | |
3880 | -- the bounds of the object itself. Set the type of the attribute to | |
3881 | -- the base type of the context, so that a check will be imposed when | |
3882 | -- needed (e.g. if the node appears as an index). | |
3883 | ||
3884 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3885 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3886 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3887 | then | |
3888 | Set_Etype (N, Base_Type (Typ)); | |
3889 | ||
675d6070 TQ |
3890 | -- Otherwise, replace the attribute node with a type conversion node |
3891 | -- whose expression is the attribute, retyped to universal integer, and | |
3892 | -- whose subtype mark is the target type. The call to analyze this | |
3893 | -- conversion will set range and overflow checks as required for proper | |
3894 | -- detection of an out of range value. | |
70482933 RK |
3895 | |
3896 | else | |
3897 | Set_Etype (N, Universal_Integer); | |
3898 | Set_Analyzed (N, True); | |
3899 | ||
3900 | Rewrite (N, | |
3901 | Make_Type_Conversion (Loc, | |
3902 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3903 | Expression => Relocate_Node (N))); | |
3904 | ||
3905 | Analyze_And_Resolve (N, Typ); | |
3906 | return; | |
3907 | end if; | |
70482933 RK |
3908 | end Apply_Universal_Integer_Attribute_Checks; |
3909 | ||
12b4d338 AC |
3910 | ------------------------------------- |
3911 | -- Atomic_Synchronization_Disabled -- | |
3912 | ------------------------------------- | |
3913 | ||
3914 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3915 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3916 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3917 | ||
3918 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3919 | begin | |
4c318253 AC |
3920 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3921 | -- looks enabled, since it is never disabled. | |
3922 | ||
3923 | if Debug_Flag_Dot_E then | |
3924 | return False; | |
3925 | ||
3926 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3927 | -- sync looks disabled, since it always tests True. | |
3928 | ||
3929 | elsif Debug_Flag_Dot_D then | |
3930 | return True; | |
3931 | ||
3932 | -- If entity present, then check result for that entity | |
3933 | ||
3934 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
12b4d338 | 3935 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
4c318253 AC |
3936 | |
3937 | -- Otherwise result depends on current scope setting | |
3938 | ||
12b4d338 | 3939 | else |
3217f71e | 3940 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
12b4d338 AC |
3941 | end if; |
3942 | end Atomic_Synchronization_Disabled; | |
3943 | ||
70482933 RK |
3944 | ------------------------------- |
3945 | -- Build_Discriminant_Checks -- | |
3946 | ------------------------------- | |
3947 | ||
3948 | function Build_Discriminant_Checks | |
3949 | (N : Node_Id; | |
6b6fcd3e | 3950 | T_Typ : Entity_Id) return Node_Id |
70482933 RK |
3951 | is |
3952 | Loc : constant Source_Ptr := Sloc (N); | |
3953 | Cond : Node_Id; | |
3954 | Disc : Elmt_Id; | |
3955 | Disc_Ent : Entity_Id; | |
fbf5a39b | 3956 | Dref : Node_Id; |
70482933 RK |
3957 | Dval : Node_Id; |
3958 | ||
86ac5e79 ES |
3959 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3960 | ||
f6219730 JS |
3961 | function Replace_Current_Instance |
3962 | (N : Node_Id) return Traverse_Result; | |
3963 | -- Replace a reference to the current instance of the type with the | |
3964 | -- corresponding _init formal of the initialization procedure. Note: | |
3965 | -- this function relies on us currently being within the initialization | |
3966 | -- procedure. | |
3967 | ||
8016e567 PT |
3968 | -------------------------------- |
3969 | -- Aggregate_Discriminant_Val -- | |
3970 | -------------------------------- | |
86ac5e79 ES |
3971 | |
3972 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3973 | Assoc : Node_Id; | |
3974 | ||
3975 | begin | |
675d6070 TQ |
3976 | -- The aggregate has been normalized with named associations. We use |
3977 | -- the Chars field to locate the discriminant to take into account | |
3978 | -- discriminants in derived types, which carry the same name as those | |
3979 | -- in the parent. | |
86ac5e79 ES |
3980 | |
3981 | Assoc := First (Component_Associations (N)); | |
3982 | while Present (Assoc) loop | |
3983 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3984 | return Expression (Assoc); | |
3985 | else | |
3986 | Next (Assoc); | |
3987 | end if; | |
3988 | end loop; | |
3989 | ||
3990 | -- Discriminant must have been found in the loop above | |
3991 | ||
3992 | raise Program_Error; | |
3993 | end Aggregate_Discriminant_Val; | |
3994 | ||
f6219730 JS |
3995 | ------------------------------ |
3996 | -- Replace_Current_Instance -- | |
3997 | ------------------------------ | |
3998 | ||
3999 | function Replace_Current_Instance | |
4000 | (N : Node_Id) return Traverse_Result is | |
4001 | begin | |
4002 | if Is_Entity_Name (N) | |
4003 | and then Etype (N) = Entity (N) | |
4004 | then | |
4005 | Rewrite (N, | |
4006 | New_Occurrence_Of (First_Formal (Current_Subprogram), Loc)); | |
4007 | end if; | |
4008 | ||
4009 | return OK; | |
4010 | end Replace_Current_Instance; | |
4011 | ||
4012 | procedure Search_And_Replace_Current_Instance is new | |
4013 | Traverse_Proc (Replace_Current_Instance); | |
4014 | ||
86ac5e79 ES |
4015 | -- Start of processing for Build_Discriminant_Checks |
4016 | ||
70482933 | 4017 | begin |
86ac5e79 ES |
4018 | -- Loop through discriminants evolving the condition |
4019 | ||
70482933 RK |
4020 | Cond := Empty; |
4021 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
4022 | ||
fbf5a39b | 4023 | -- For a fully private type, use the discriminants of the parent type |
70482933 RK |
4024 | |
4025 | if Is_Private_Type (T_Typ) | |
4026 | and then No (Full_View (T_Typ)) | |
4027 | then | |
4028 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
4029 | else | |
4030 | Disc_Ent := First_Discriminant (T_Typ); | |
4031 | end if; | |
4032 | ||
4033 | while Present (Disc) loop | |
70482933 RK |
4034 | Dval := Node (Disc); |
4035 | ||
4036 | if Nkind (Dval) = N_Identifier | |
4037 | and then Ekind (Entity (Dval)) = E_Discriminant | |
4038 | then | |
4039 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
4040 | else | |
fbf5a39b | 4041 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
70482933 RK |
4042 | end if; |
4043 | ||
f6219730 JS |
4044 | -- Replace references to the current instance of the type with the |
4045 | -- corresponding _init formal of the initialization procedure. | |
4046 | ||
4047 | if Within_Init_Proc then | |
4048 | Search_And_Replace_Current_Instance (Dval); | |
4049 | end if; | |
4050 | ||
5d09245e AC |
4051 | -- If we have an Unchecked_Union node, we can infer the discriminants |
4052 | -- of the node. | |
fbf5a39b | 4053 | |
5d09245e AC |
4054 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
4055 | Dref := New_Copy ( | |
4056 | Get_Discriminant_Value ( | |
4057 | First_Discriminant (T_Typ), | |
4058 | T_Typ, | |
4059 | Stored_Constraint (T_Typ))); | |
4060 | ||
86ac5e79 ES |
4061 | elsif Nkind (N) = N_Aggregate then |
4062 | Dref := | |
4063 | Duplicate_Subexpr_No_Checks | |
4064 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
4065 | ||
f715a5bd EB |
4066 | elsif Is_Access_Type (Etype (N)) then |
4067 | Dref := | |
4068 | Make_Selected_Component (Loc, | |
4069 | Prefix => | |
4070 | Make_Explicit_Dereference (Loc, | |
4071 | Duplicate_Subexpr_No_Checks (N, Name_Req => True)), | |
4072 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); | |
4073 | ||
4074 | Set_Is_In_Discriminant_Check (Dref); | |
5d09245e AC |
4075 | else |
4076 | Dref := | |
4077 | Make_Selected_Component (Loc, | |
637a41a5 | 4078 | Prefix => |
5d09245e | 4079 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
637a41a5 | 4080 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
5d09245e AC |
4081 | |
4082 | Set_Is_In_Discriminant_Check (Dref); | |
4083 | end if; | |
fbf5a39b | 4084 | |
70482933 RK |
4085 | Evolve_Or_Else (Cond, |
4086 | Make_Op_Ne (Loc, | |
637a41a5 | 4087 | Left_Opnd => Dref, |
70482933 RK |
4088 | Right_Opnd => Dval)); |
4089 | ||
4090 | Next_Elmt (Disc); | |
4091 | Next_Discriminant (Disc_Ent); | |
4092 | end loop; | |
4093 | ||
4094 | return Cond; | |
4095 | end Build_Discriminant_Checks; | |
4096 | ||
2ede092b RD |
4097 | ------------------ |
4098 | -- Check_Needed -- | |
4099 | ------------------ | |
4100 | ||
4101 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
4102 | N : Node_Id; | |
4103 | P : Node_Id; | |
4104 | K : Node_Kind; | |
4105 | L : Node_Id; | |
4106 | R : Node_Id; | |
4107 | ||
ef163a0a AC |
4108 | function Left_Expression (Op : Node_Id) return Node_Id; |
4109 | -- Return the relevant expression from the left operand of the given | |
4110 | -- short circuit form: this is LO itself, except if LO is a qualified | |
4111 | -- expression, a type conversion, or an expression with actions, in | |
4112 | -- which case this is Left_Expression (Expression (LO)). | |
4113 | ||
4114 | --------------------- | |
4115 | -- Left_Expression -- | |
4116 | --------------------- | |
4117 | ||
4118 | function Left_Expression (Op : Node_Id) return Node_Id is | |
4119 | LE : Node_Id := Left_Opnd (Op); | |
4120 | begin | |
4a08c95c AC |
4121 | while Nkind (LE) in N_Qualified_Expression |
4122 | | N_Type_Conversion | |
4123 | | N_Expression_With_Actions | |
ef163a0a AC |
4124 | loop |
4125 | LE := Expression (LE); | |
4126 | end loop; | |
4127 | ||
4128 | return LE; | |
4129 | end Left_Expression; | |
4130 | ||
4131 | -- Start of processing for Check_Needed | |
4132 | ||
2ede092b RD |
4133 | begin |
4134 | -- Always check if not simple entity | |
4135 | ||
4136 | if Nkind (Nod) not in N_Has_Entity | |
4137 | or else not Comes_From_Source (Nod) | |
4138 | then | |
4139 | return True; | |
4140 | end if; | |
4141 | ||
4142 | -- Look up tree for short circuit | |
4143 | ||
4144 | N := Nod; | |
4145 | loop | |
4146 | P := Parent (N); | |
4147 | K := Nkind (P); | |
4148 | ||
16a55e63 RD |
4149 | -- Done if out of subexpression (note that we allow generated stuff |
4150 | -- such as itype declarations in this context, to keep the loop going | |
4151 | -- since we may well have generated such stuff in complex situations. | |
4152 | -- Also done if no parent (probably an error condition, but no point | |
a90bd866 | 4153 | -- in behaving nasty if we find it). |
16a55e63 RD |
4154 | |
4155 | if No (P) | |
4156 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
4157 | then | |
2ede092b RD |
4158 | return True; |
4159 | ||
16a55e63 RD |
4160 | -- Or/Or Else case, where test is part of the right operand, or is |
4161 | -- part of one of the actions associated with the right operand, and | |
4162 | -- the left operand is an equality test. | |
2ede092b | 4163 | |
16a55e63 | 4164 | elsif K = N_Op_Or then |
2ede092b | 4165 | exit when N = Right_Opnd (P) |
ef163a0a | 4166 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
2ede092b | 4167 | |
16a55e63 RD |
4168 | elsif K = N_Or_Else then |
4169 | exit when (N = Right_Opnd (P) | |
4170 | or else | |
4171 | (Is_List_Member (N) | |
4172 | and then List_Containing (N) = Actions (P))) | |
ef163a0a | 4173 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
2ede092b | 4174 | |
16a55e63 RD |
4175 | -- Similar test for the And/And then case, where the left operand |
4176 | -- is an inequality test. | |
4177 | ||
4178 | elsif K = N_Op_And then | |
2ede092b | 4179 | exit when N = Right_Opnd (P) |
ef163a0a | 4180 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
16a55e63 RD |
4181 | |
4182 | elsif K = N_And_Then then | |
4183 | exit when (N = Right_Opnd (P) | |
4184 | or else | |
4185 | (Is_List_Member (N) | |
637a41a5 | 4186 | and then List_Containing (N) = Actions (P))) |
ef163a0a | 4187 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
2ede092b RD |
4188 | end if; |
4189 | ||
4190 | N := P; | |
4191 | end loop; | |
4192 | ||
4193 | -- If we fall through the loop, then we have a conditional with an | |
ef163a0a AC |
4194 | -- appropriate test as its left operand, so look further. |
4195 | ||
4196 | L := Left_Expression (P); | |
4197 | ||
4198 | -- L is an "=" or "/=" operator: extract its operands | |
2ede092b | 4199 | |
2ede092b RD |
4200 | R := Right_Opnd (L); |
4201 | L := Left_Opnd (L); | |
4202 | ||
4203 | -- Left operand of test must match original variable | |
4204 | ||
637a41a5 | 4205 | if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
2ede092b RD |
4206 | return True; |
4207 | end if; | |
4208 | ||
939c12d2 | 4209 | -- Right operand of test must be key value (zero or null) |
2ede092b RD |
4210 | |
4211 | case Check is | |
4212 | when Access_Check => | |
939c12d2 | 4213 | if not Known_Null (R) then |
2ede092b RD |
4214 | return True; |
4215 | end if; | |
4216 | ||
4217 | when Division_Check => | |
4218 | if not Compile_Time_Known_Value (R) | |
4219 | or else Expr_Value (R) /= Uint_0 | |
4220 | then | |
4221 | return True; | |
4222 | end if; | |
939c12d2 RD |
4223 | |
4224 | when others => | |
4225 | raise Program_Error; | |
2ede092b RD |
4226 | end case; |
4227 | ||
4228 | -- Here we have the optimizable case, warn if not short-circuited | |
4229 | ||
4230 | if K = N_Op_And or else K = N_Op_Or then | |
43417b90 | 4231 | Error_Msg_Warn := SPARK_Mode /= On; |
4a28b181 | 4232 | |
2ede092b RD |
4233 | case Check is |
4234 | when Access_Check => | |
4a28b181 AC |
4235 | if GNATprove_Mode then |
4236 | Error_Msg_N | |
4237 | ("Constraint_Error might have been raised (access check)", | |
4238 | Parent (Nod)); | |
4239 | else | |
4240 | Error_Msg_N | |
4241 | ("Constraint_Error may be raised (access check)??", | |
4242 | Parent (Nod)); | |
4243 | end if; | |
4244 | ||
2ede092b | 4245 | when Division_Check => |
4a28b181 AC |
4246 | if GNATprove_Mode then |
4247 | Error_Msg_N | |
4248 | ("Constraint_Error might have been raised (zero divide)", | |
4249 | Parent (Nod)); | |
4250 | else | |
4251 | Error_Msg_N | |
4252 | ("Constraint_Error may be raised (zero divide)??", | |
4253 | Parent (Nod)); | |
4254 | end if; | |
939c12d2 RD |
4255 | |
4256 | when others => | |
4257 | raise Program_Error; | |
2ede092b RD |
4258 | end case; |
4259 | ||
4260 | if K = N_Op_And then | |
19d846a0 | 4261 | Error_Msg_N -- CODEFIX |
685bc70f | 4262 | ("use `AND THEN` instead of AND??", P); |
2ede092b | 4263 | else |
19d846a0 | 4264 | Error_Msg_N -- CODEFIX |
685bc70f | 4265 | ("use `OR ELSE` instead of OR??", P); |
2ede092b RD |
4266 | end if; |
4267 | ||
308e6f3a | 4268 | -- If not short-circuited, we need the check |
2ede092b RD |
4269 | |
4270 | return True; | |
4271 | ||
4272 | -- If short-circuited, we can omit the check | |
4273 | ||
4274 | else | |
4275 | return False; | |
4276 | end if; | |
4277 | end Check_Needed; | |
4278 | ||
70482933 RK |
4279 | ----------------------------------- |
4280 | -- Check_Valid_Lvalue_Subscripts -- | |
4281 | ----------------------------------- | |
4282 | ||
4283 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
4284 | begin | |
4285 | -- Skip this if range checks are suppressed | |
4286 | ||
4287 | if Range_Checks_Suppressed (Etype (Expr)) then | |
4288 | return; | |
4289 | ||
675d6070 TQ |
4290 | -- Only do this check for expressions that come from source. We assume |
4291 | -- that expander generated assignments explicitly include any necessary | |
4292 | -- checks. Note that this is not just an optimization, it avoids | |
a90bd866 | 4293 | -- infinite recursions. |
70482933 RK |
4294 | |
4295 | elsif not Comes_From_Source (Expr) then | |
4296 | return; | |
4297 | ||
4298 | -- For a selected component, check the prefix | |
4299 | ||
4300 | elsif Nkind (Expr) = N_Selected_Component then | |
4301 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4302 | return; | |
4303 | ||
4304 | -- Case of indexed component | |
4305 | ||
4306 | elsif Nkind (Expr) = N_Indexed_Component then | |
4307 | Apply_Subscript_Validity_Checks (Expr); | |
4308 | ||
675d6070 TQ |
4309 | -- Prefix may itself be or contain an indexed component, and these |
4310 | -- subscripts need checking as well. | |
70482933 RK |
4311 | |
4312 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
4313 | end if; | |
4314 | end Check_Valid_Lvalue_Subscripts; | |
4315 | ||
2820d220 AC |
4316 | ---------------------------------- |
4317 | -- Null_Exclusion_Static_Checks -- | |
4318 | ---------------------------------- | |
4319 | ||
d59179b1 | 4320 | procedure Null_Exclusion_Static_Checks |
62d40a7a AC |
4321 | (N : Node_Id; |
4322 | Comp : Node_Id := Empty; | |
4323 | Array_Comp : Boolean := False) | |
d59179b1 | 4324 | is |
97ac2d62 AC |
4325 | Has_Null : constant Boolean := Has_Null_Exclusion (N); |
4326 | Kind : constant Node_Kind := Nkind (N); | |
4327 | Error_Nod : Node_Id; | |
4328 | Expr : Node_Id; | |
4329 | Typ : Entity_Id; | |
2820d220 | 4330 | |
2ede092b | 4331 | begin |
c064e066 | 4332 | pragma Assert |
4a08c95c AC |
4333 | (Kind in N_Component_Declaration |
4334 | | N_Discriminant_Specification | |
4335 | | N_Function_Specification | |
4336 | | N_Object_Declaration | |
4337 | | N_Parameter_Specification); | |
c064e066 | 4338 | |
97ac2d62 | 4339 | if Kind = N_Function_Specification then |
c064e066 RD |
4340 | Typ := Etype (Defining_Entity (N)); |
4341 | else | |
4342 | Typ := Etype (Defining_Identifier (N)); | |
4343 | end if; | |
2820d220 | 4344 | |
97ac2d62 | 4345 | case Kind is |
2ede092b RD |
4346 | when N_Component_Declaration => |
4347 | if Present (Access_Definition (Component_Definition (N))) then | |
97ac2d62 | 4348 | Error_Nod := Component_Definition (N); |
2ede092b | 4349 | else |
97ac2d62 | 4350 | Error_Nod := Subtype_Indication (Component_Definition (N)); |
2ede092b | 4351 | end if; |
7324bf49 | 4352 | |
c064e066 | 4353 | when N_Discriminant_Specification => |
97ac2d62 | 4354 | Error_Nod := Discriminant_Type (N); |
c064e066 RD |
4355 | |
4356 | when N_Function_Specification => | |
97ac2d62 | 4357 | Error_Nod := Result_Definition (N); |
c064e066 RD |
4358 | |
4359 | when N_Object_Declaration => | |
97ac2d62 | 4360 | Error_Nod := Object_Definition (N); |
c064e066 RD |
4361 | |
4362 | when N_Parameter_Specification => | |
97ac2d62 | 4363 | Error_Nod := Parameter_Type (N); |
c064e066 | 4364 | |
2ede092b RD |
4365 | when others => |
4366 | raise Program_Error; | |
4367 | end case; | |
7324bf49 | 4368 | |
c064e066 | 4369 | if Has_Null then |
7324bf49 | 4370 | |
c064e066 RD |
4371 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
4372 | -- applied to an access [sub]type. | |
7324bf49 | 4373 | |
c064e066 | 4374 | if not Is_Access_Type (Typ) then |
ed2233dc | 4375 | Error_Msg_N |
97ac2d62 | 4376 | ("`NOT NULL` allowed only for an access type", Error_Nod); |
7324bf49 | 4377 | |
675d6070 | 4378 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
c064e066 RD |
4379 | -- be applied to a [sub]type that does not exclude null already. |
4380 | ||
97ac2d62 | 4381 | elsif Can_Never_Be_Null (Typ) and then Comes_From_Source (Typ) then |
ed2233dc | 4382 | Error_Msg_NE |
11b4899f | 4383 | ("`NOT NULL` not allowed (& already excludes null)", |
97ac2d62 | 4384 | Error_Nod, Typ); |
c064e066 | 4385 | end if; |
2ede092b | 4386 | end if; |
7324bf49 | 4387 | |
f2cbd970 JM |
4388 | -- Check that null-excluding objects are always initialized, except for |
4389 | -- deferred constants, for which the expression will appear in the full | |
4390 | -- declaration. | |
2ede092b | 4391 | |
97ac2d62 | 4392 | if Kind = N_Object_Declaration |
86ac5e79 | 4393 | and then No (Expression (N)) |
f2cbd970 | 4394 | and then not Constant_Present (N) |
675d6070 | 4395 | and then not No_Initialization (N) |
2ede092b | 4396 | then |
d59179b1 AC |
4397 | if Present (Comp) then |
4398 | ||
31e358e1 | 4399 | -- Specialize the warning message to indicate that we are dealing |
d59179b1 AC |
4400 | -- with an uninitialized composite object that has a defaulted |
4401 | -- null-excluding component. | |
4402 | ||
4403 | Error_Msg_Name_1 := Chars (Defining_Identifier (Comp)); | |
4404 | Error_Msg_Name_2 := Chars (Defining_Identifier (N)); | |
4405 | ||
62d40a7a AC |
4406 | Discard_Node |
4407 | (Compile_Time_Constraint_Error | |
cdfdd0de PT |
4408 | (N => N, |
4409 | Msg => | |
62d40a7a AC |
4410 | "(Ada 2005) null-excluding component % of object % must " |
4411 | & "be initialized??", | |
4412 | Ent => Defining_Identifier (Comp))); | |
4413 | ||
4414 | -- This is a case of an array with null-excluding components, so | |
4415 | -- indicate that in the warning. | |
4416 | ||
4417 | elsif Array_Comp then | |
4418 | Discard_Node | |
4419 | (Compile_Time_Constraint_Error | |
cdfdd0de PT |
4420 | (N => N, |
4421 | Msg => | |
62d40a7a AC |
4422 | "(Ada 2005) null-excluding array components must " |
4423 | & "be initialized??", | |
4424 | Ent => Defining_Identifier (N))); | |
4425 | ||
4426 | -- Normal case of object of a null-excluding access type | |
31e358e1 | 4427 | |
d59179b1 | 4428 | else |
62d40a7a AC |
4429 | -- Add an expression that assigns null. This node is needed by |
4430 | -- Apply_Compile_Time_Constraint_Error, which will replace this | |
4431 | -- with a Constraint_Error node. | |
4432 | ||
4433 | Set_Expression (N, Make_Null (Sloc (N))); | |
4434 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
4435 | ||
d59179b1 AC |
4436 | Apply_Compile_Time_Constraint_Error |
4437 | (N => Expression (N), | |
4438 | Msg => | |
4439 | "(Ada 2005) null-excluding objects must be initialized??", | |
4440 | Reason => CE_Null_Not_Allowed); | |
4441 | end if; | |
2ede092b | 4442 | end if; |
7324bf49 | 4443 | |
f2cbd970 JM |
4444 | -- Check that a null-excluding component, formal or object is not being |
4445 | -- assigned a null value. Otherwise generate a warning message and | |
f3d0f304 | 4446 | -- replace Expression (N) by an N_Constraint_Error node. |
2ede092b | 4447 | |
97ac2d62 | 4448 | if Kind /= N_Function_Specification then |
c064e066 | 4449 | Expr := Expression (N); |
7324bf49 | 4450 | |
939c12d2 | 4451 | if Present (Expr) and then Known_Null (Expr) then |
97ac2d62 | 4452 | case Kind is |
d8f43ee6 HK |
4453 | when N_Component_Declaration |
4454 | | N_Discriminant_Specification | |
4455 | => | |
82c80734 | 4456 | Apply_Compile_Time_Constraint_Error |
c064e066 | 4457 | (N => Expr, |
d8f43ee6 | 4458 | Msg => |
9ed2b86d | 4459 | "(Ada 2005) NULL not allowed in null-excluding " |
d8f43ee6 | 4460 | & "components??", |
c064e066 | 4461 | Reason => CE_Null_Not_Allowed); |
7324bf49 | 4462 | |
c064e066 | 4463 | when N_Object_Declaration => |
82c80734 | 4464 | Apply_Compile_Time_Constraint_Error |
c064e066 | 4465 | (N => Expr, |
d8f43ee6 | 4466 | Msg => |
9ed2b86d | 4467 | "(Ada 2005) NULL not allowed in null-excluding " |
d8f43ee6 | 4468 | & "objects??", |
c064e066 | 4469 | Reason => CE_Null_Not_Allowed); |
7324bf49 | 4470 | |
c064e066 | 4471 | when N_Parameter_Specification => |
82c80734 | 4472 | Apply_Compile_Time_Constraint_Error |
c064e066 | 4473 | (N => Expr, |
d8f43ee6 | 4474 | Msg => |
9ed2b86d | 4475 | "(Ada 2005) NULL not allowed in null-excluding " |
d8f43ee6 | 4476 | & "formals??", |
c064e066 | 4477 | Reason => CE_Null_Not_Allowed); |
2ede092b RD |
4478 | |
4479 | when others => | |
4480 | null; | |
7324bf49 AC |
4481 | end case; |
4482 | end if; | |
c064e066 | 4483 | end if; |
2820d220 AC |
4484 | end Null_Exclusion_Static_Checks; |
4485 | ||
12be130c EB |
4486 | ------------------------------------- |
4487 | -- Compute_Range_For_Arithmetic_Op -- | |
4488 | ------------------------------------- | |
4489 | ||
4490 | procedure Compute_Range_For_Arithmetic_Op | |
4491 | (Op : Node_Kind; | |
4492 | Lo_Left : Uint; | |
4493 | Hi_Left : Uint; | |
4494 | Lo_Right : Uint; | |
4495 | Hi_Right : Uint; | |
4496 | OK : out Boolean; | |
4497 | Lo : out Uint; | |
4498 | Hi : out Uint) | |
4499 | is | |
4500 | -- Use local variables for possible adjustments | |
4501 | ||
4502 | Llo : Uint renames Lo_Left; | |
4503 | Lhi : Uint renames Hi_Left; | |
4504 | Rlo : Uint := Lo_Right; | |
4505 | Rhi : Uint := Hi_Right; | |
4506 | ||
4507 | begin | |
4508 | -- We will compute a range for the result in almost all cases | |
4509 | ||
4510 | OK := True; | |
4511 | ||
4512 | case Op is | |
4513 | ||
4514 | -- Absolute value | |
4515 | ||
4516 | when N_Op_Abs => | |
4517 | Lo := Uint_0; | |
4518 | Hi := UI_Max (abs Rlo, abs Rhi); | |
4519 | ||
4520 | -- Addition | |
4521 | ||
4522 | when N_Op_Add => | |
4523 | Lo := Llo + Rlo; | |
4524 | Hi := Lhi + Rhi; | |
4525 | ||
4526 | -- Division | |
4527 | ||
4528 | when N_Op_Divide => | |
4529 | ||
4530 | -- If the right operand can only be zero, set 0..0 | |
4531 | ||
4532 | if Rlo = 0 and then Rhi = 0 then | |
4533 | Lo := Uint_0; | |
4534 | Hi := Uint_0; | |
4535 | ||
4536 | -- Possible bounds of division must come from dividing end | |
4537 | -- values of the input ranges (four possibilities), provided | |
4538 | -- zero is not included in the possible values of the right | |
4539 | -- operand. | |
4540 | ||
4541 | -- Otherwise, we just consider two intervals of values for | |
4542 | -- the right operand: the interval of negative values (up to | |
4543 | -- -1) and the interval of positive values (starting at 1). | |
4544 | -- Since division by 1 is the identity, and division by -1 | |
4545 | -- is negation, we get all possible bounds of division in that | |
4546 | -- case by considering: | |
4547 | -- - all values from the division of end values of input | |
4548 | -- ranges; | |
4549 | -- - the end values of the left operand; | |
4550 | -- - the negation of the end values of the left operand. | |
4551 | ||
4552 | else | |
4553 | declare | |
4554 | Mrk : constant Uintp.Save_Mark := Mark; | |
4555 | -- Mark so we can release the RR and Ev values | |
4556 | ||
4557 | Ev1 : Uint; | |
4558 | Ev2 : Uint; | |
4559 | Ev3 : Uint; | |
4560 | Ev4 : Uint; | |
4561 | ||
4562 | begin | |
4563 | -- Discard extreme values of zero for the divisor, since | |
4564 | -- they will simply result in an exception in any case. | |
4565 | ||
4566 | if Rlo = 0 then | |
4567 | Rlo := Uint_1; | |
4568 | elsif Rhi = 0 then | |
4569 | Rhi := -Uint_1; | |
4570 | end if; | |
4571 | ||
4572 | -- Compute possible bounds coming from dividing end | |
4573 | -- values of the input ranges. | |
4574 | ||
4575 | Ev1 := Llo / Rlo; | |
4576 | Ev2 := Llo / Rhi; | |
4577 | Ev3 := Lhi / Rlo; | |
4578 | Ev4 := Lhi / Rhi; | |
4579 | ||
4580 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
4581 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
4582 | ||
4583 | -- If the right operand can be both negative or positive, | |
4584 | -- include the end values of the left operand in the | |
4585 | -- extreme values, as well as their negation. | |
4586 | ||
4587 | if Rlo < 0 and then Rhi > 0 then | |
4588 | Ev1 := Llo; | |
4589 | Ev2 := -Llo; | |
4590 | Ev3 := Lhi; | |
4591 | Ev4 := -Lhi; | |
4592 | ||
4593 | Lo := UI_Min (Lo, | |
4594 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
4595 | Hi := UI_Max (Hi, | |
4596 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
4597 | end if; | |
4598 | ||
4599 | -- Release the RR and Ev values | |
4600 | ||
4601 | Release_And_Save (Mrk, Lo, Hi); | |
4602 | end; | |
4603 | end if; | |
4604 | ||
4605 | -- Exponentiation | |
4606 | ||
4607 | when N_Op_Expon => | |
4608 | ||
4609 | -- Discard negative values for the exponent, since they will | |
4610 | -- simply result in an exception in any case. | |
4611 | ||
4612 | if Rhi < 0 then | |
4613 | Rhi := Uint_0; | |
4614 | elsif Rlo < 0 then | |
4615 | Rlo := Uint_0; | |
4616 | end if; | |
4617 | ||
4618 | -- Estimate number of bits in result before we go computing | |
4619 | -- giant useless bounds. Basically the number of bits in the | |
4620 | -- result is the number of bits in the base multiplied by the | |
4621 | -- value of the exponent. If this is big enough that the result | |
4622 | -- definitely won't fit in Long_Long_Integer, return immediately | |
4623 | -- and avoid computing giant bounds. | |
4624 | ||
4625 | -- The comparison here is approximate, but conservative, it | |
4626 | -- only clicks on cases that are sure to exceed the bounds. | |
4627 | ||
4628 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
4629 | Lo := No_Uint; | |
4630 | Hi := No_Uint; | |
4631 | OK := False; | |
4632 | return; | |
4633 | ||
4634 | -- If right operand is zero then result is 1 | |
4635 | ||
4636 | elsif Rhi = 0 then | |
4637 | Lo := Uint_1; | |
4638 | Hi := Uint_1; | |
4639 | ||
4640 | else | |
4641 | -- High bound comes either from exponentiation of largest | |
4642 | -- positive value to largest exponent value, or from | |
4643 | -- the exponentiation of most negative value to an | |
4644 | -- even exponent. | |
4645 | ||
4646 | declare | |
4647 | Hi1, Hi2 : Uint; | |
4648 | ||
4649 | begin | |
4650 | if Lhi > 0 then | |
4651 | Hi1 := Lhi ** Rhi; | |
4652 | else | |
4653 | Hi1 := Uint_0; | |
4654 | end if; | |
4655 | ||
4656 | if Llo < 0 then | |
4657 | if Rhi mod 2 = 0 then | |
4658 | Hi2 := Llo ** Rhi; | |
4659 | else | |
4660 | Hi2 := Llo ** (Rhi - 1); | |
4661 | end if; | |
4662 | else | |
4663 | Hi2 := Uint_0; | |
4664 | end if; | |
4665 | ||
4666 | Hi := UI_Max (Hi1, Hi2); | |
4667 | end; | |
4668 | ||
4669 | -- Result can only be negative if base can be negative | |
4670 | ||
4671 | if Llo < 0 then | |
4672 | if Rhi mod 2 = 0 then | |
4673 | Lo := Llo ** (Rhi - 1); | |
4674 | else | |
4675 | Lo := Llo ** Rhi; | |
4676 | end if; | |
4677 | ||
4678 | -- Otherwise low bound is minimum ** minimum | |
4679 | ||
4680 | else | |
4681 | Lo := Llo ** Rlo; | |
4682 | end if; | |
4683 | end if; | |
4684 | ||
4685 | -- Negation | |
4686 | ||
4687 | when N_Op_Minus => | |
4688 | Lo := -Rhi; | |
4689 | Hi := -Rlo; | |
4690 | ||
4691 | -- Mod | |
4692 | ||
4693 | when N_Op_Mod => | |
4694 | declare | |
4695 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; | |
4696 | -- This is the maximum absolute value of the result | |
4697 | ||
4698 | begin | |
4699 | Lo := Uint_0; | |
4700 | Hi := Uint_0; | |
4701 | ||
4702 | -- The result depends only on the sign and magnitude of | |
4703 | -- the right operand, it does not depend on the sign or | |
4704 | -- magnitude of the left operand. | |
4705 | ||
4706 | if Rlo < 0 then | |
4707 | Lo := -Maxabs; | |
4708 | end if; | |
4709 | ||
4710 | if Rhi > 0 then | |
4711 | Hi := Maxabs; | |
4712 | end if; | |
4713 | end; | |
4714 | ||
4715 | -- Multiplication | |
4716 | ||
4717 | when N_Op_Multiply => | |
4718 | ||
4719 | -- Possible bounds of multiplication must come from multiplying | |
4720 | -- end values of the input ranges (four possibilities). | |
4721 | ||
4722 | declare | |
4723 | Mrk : constant Uintp.Save_Mark := Mark; | |
4724 | -- Mark so we can release the Ev values | |
4725 | ||
4726 | Ev1 : constant Uint := Llo * Rlo; | |
4727 | Ev2 : constant Uint := Llo * Rhi; | |
4728 | Ev3 : constant Uint := Lhi * Rlo; | |
4729 | Ev4 : constant Uint := Lhi * Rhi; | |
4730 | ||
4731 | begin | |
4732 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
4733 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
4734 | ||
4735 | -- Release the Ev values | |
4736 | ||
4737 | Release_And_Save (Mrk, Lo, Hi); | |
4738 | end; | |
4739 | ||
4740 | -- Plus operator (affirmation) | |
4741 | ||
4742 | when N_Op_Plus => | |
4743 | Lo := Rlo; | |
4744 | Hi := Rhi; | |
4745 | ||
4746 | -- Remainder | |
4747 | ||
4748 | when N_Op_Rem => | |
4749 | declare | |
4750 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; | |
4751 | -- This is the maximum absolute value of the result. Note | |
4752 | -- that the result range does not depend on the sign of the | |
4753 | -- right operand. | |
4754 | ||
4755 | begin | |
4756 | Lo := Uint_0; | |
4757 | Hi := Uint_0; | |
4758 | ||
4759 | -- Case of left operand negative, which results in a range | |
4760 | -- of -Maxabs .. 0 for those negative values. If there are | |
4761 | -- no negative values then Lo value of result is always 0. | |
4762 | ||
4763 | if Llo < 0 then | |
4764 | Lo := -Maxabs; | |
4765 | end if; | |
4766 | ||
4767 | -- Case of left operand positive | |
4768 | ||
4769 | if Lhi > 0 then | |
4770 | Hi := Maxabs; | |
4771 | end if; | |
4772 | end; | |
4773 | ||
4774 | -- Subtract | |
4775 | ||
4776 | when N_Op_Subtract => | |
4777 | Lo := Llo - Rhi; | |
4778 | Hi := Lhi - Rlo; | |
4779 | ||
4780 | -- Nothing else should be possible | |
4781 | ||
4782 | when others => | |
4783 | raise Program_Error; | |
4784 | end case; | |
4785 | end Compute_Range_For_Arithmetic_Op; | |
4786 | ||
fbf5a39b AC |
4787 | ---------------------------------- |
4788 | -- Conditional_Statements_Begin -- | |
4789 | ---------------------------------- | |
4790 | ||
4791 | procedure Conditional_Statements_Begin is | |
4792 | begin | |
4793 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
4794 | ||
675d6070 TQ |
4795 | -- If stack overflows, kill all checks, that way we know to simply reset |
4796 | -- the number of saved checks to zero on return. This should never occur | |
4797 | -- in practice. | |
fbf5a39b AC |
4798 | |
4799 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4800 | Kill_All_Checks; | |
4801 | ||
675d6070 TQ |
4802 | -- In the normal case, we just make a new stack entry saving the current |
4803 | -- number of saved checks for a later restore. | |
fbf5a39b AC |
4804 | |
4805 | else | |
4806 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
4807 | ||
4808 | if Debug_Flag_CC then | |
4809 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
4810 | Num_Saved_Checks); | |
4811 | end if; | |
4812 | end if; | |
4813 | end Conditional_Statements_Begin; | |
4814 | ||
4815 | -------------------------------- | |
4816 | -- Conditional_Statements_End -- | |
4817 | -------------------------------- | |
4818 | ||
4819 | procedure Conditional_Statements_End is | |
4820 | begin | |
4821 | pragma Assert (Saved_Checks_TOS > 0); | |
4822 | ||
675d6070 TQ |
4823 | -- If the saved checks stack overflowed, then we killed all checks, so |
4824 | -- setting the number of saved checks back to zero is correct. This | |
4825 | -- should never occur in practice. | |
fbf5a39b AC |
4826 | |
4827 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4828 | Num_Saved_Checks := 0; | |
4829 | ||
675d6070 TQ |
4830 | -- In the normal case, restore the number of saved checks from the top |
4831 | -- stack entry. | |
fbf5a39b AC |
4832 | |
4833 | else | |
4834 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
637a41a5 | 4835 | |
fbf5a39b AC |
4836 | if Debug_Flag_CC then |
4837 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
4838 | Num_Saved_Checks); | |
4839 | end if; | |
4840 | end if; | |
4841 | ||
4842 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
4843 | end Conditional_Statements_End; | |
4844 | ||
acad3c0a AC |
4845 | ------------------------- |
4846 | -- Convert_From_Bignum -- | |
4847 | ------------------------- | |
4848 | ||
4849 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
4850 | Loc : constant Source_Ptr := Sloc (N); | |
4851 | ||
4852 | begin | |
4853 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
4854 | ||
4855 | -- Construct call From Bignum | |
4856 | ||
4857 | return | |
4858 | Make_Function_Call (Loc, | |
4859 | Name => | |
4860 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4861 | Parameter_Associations => New_List (Relocate_Node (N))); | |
4862 | end Convert_From_Bignum; | |
4863 | ||
4864 | ----------------------- | |
4865 | -- Convert_To_Bignum -- | |
4866 | ----------------------- | |
4867 | ||
4868 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
4869 | Loc : constant Source_Ptr := Sloc (N); | |
4870 | ||
4871 | begin | |
4b1c4f20 | 4872 | -- Nothing to do if Bignum already except call Relocate_Node |
acad3c0a AC |
4873 | |
4874 | if Is_RTE (Etype (N), RE_Bignum) then | |
4875 | return Relocate_Node (N); | |
4876 | ||
5707e389 AC |
4877 | -- Otherwise construct call to To_Bignum, converting the operand to the |
4878 | -- required Long_Long_Integer form. | |
acad3c0a AC |
4879 | |
4880 | else | |
4881 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
4882 | return | |
4883 | Make_Function_Call (Loc, | |
4884 | Name => | |
4885 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
4886 | Parameter_Associations => New_List ( | |
4887 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
4888 | end if; | |
4889 | end Convert_To_Bignum; | |
4890 | ||
70482933 RK |
4891 | --------------------- |
4892 | -- Determine_Range -- | |
4893 | --------------------- | |
4894 | ||
c9a4817d | 4895 | Cache_Size : constant := 2 ** 10; |
70482933 | 4896 | type Cache_Index is range 0 .. Cache_Size - 1; |
a90bd866 | 4897 | -- Determine size of below cache (power of 2 is more efficient) |
70482933 | 4898 | |
6b6bce61 | 4899 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; |
12be130c | 4900 | Determine_Range_Cache_O : array (Cache_Index) of Node_Id; |
6b6bce61 AC |
4901 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
4902 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; | |
4903 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
4904 | Determine_Range_Cache_Lo_R : array (Cache_Index) of Ureal; | |
4905 | Determine_Range_Cache_Hi_R : array (Cache_Index) of Ureal; | |
675d6070 | 4906 | -- The above arrays are used to implement a small direct cache for |
6b6bce61 AC |
4907 | -- Determine_Range and Determine_Range_R calls. Because of the way these |
4908 | -- subprograms recursively traces subexpressions, and because overflow | |
4909 | -- checking calls the routine on the way up the tree, a quadratic behavior | |
4910 | -- can otherwise be encountered in large expressions. The cache entry for | |
4911 | -- node N is stored in the (N mod Cache_Size) entry, and can be validated | |
12be130c EB |
4912 | -- by checking the actual node value stored there. The Range_Cache_O array |
4913 | -- records the setting of Original_Node (N) so that the cache entry does | |
4914 | -- not become stale when the node N is rewritten. The Range_Cache_V array | |
6b6bce61 | 4915 | -- records the setting of Assume_Valid for the cache entry. |
70482933 RK |
4916 | |
4917 | procedure Determine_Range | |
c800f862 RD |
4918 | (N : Node_Id; |
4919 | OK : out Boolean; | |
4920 | Lo : out Uint; | |
4921 | Hi : out Uint; | |
4922 | Assume_Valid : Boolean := False) | |
70482933 | 4923 | is |
12be130c EB |
4924 | Kind : constant Node_Kind := Nkind (N); |
4925 | -- Kind of node | |
4926 | ||
4927 | function Half_Address_Space return Uint; | |
4928 | -- The size of half the total addressable memory space in storage units | |
4929 | -- (minus one, so that the size fits in a signed integer whose size is | |
4930 | -- System_Address_Size, which helps in various cases). | |
4931 | ||
4932 | ------------------------ | |
4933 | -- Half_Address_Space -- | |
4934 | ------------------------ | |
4935 | ||
4936 | function Half_Address_Space return Uint is | |
4937 | begin | |
4938 | return Uint_2 ** (System_Address_Size - 1) - 1; | |
4939 | end Half_Address_Space; | |
4940 | ||
4941 | -- Local variables | |
4942 | ||
1c7717c3 AC |
4943 | Typ : Entity_Id := Etype (N); |
4944 | -- Type to use, may get reset to base type for possibly invalid entity | |
c1c22e7a | 4945 | |
12be130c EB |
4946 | Lo_Left : Uint := No_Uint; |
4947 | Hi_Left : Uint := No_Uint; | |
c1c22e7a | 4948 | -- Lo and Hi bounds of left operand |
70482933 | 4949 | |
dcd5fd67 PMR |
4950 | Lo_Right : Uint := No_Uint; |
4951 | Hi_Right : Uint := No_Uint; | |
c1c22e7a GB |
4952 | -- Lo and Hi bounds of right (or only) operand |
4953 | ||
4954 | Bound : Node_Id; | |
4955 | -- Temp variable used to hold a bound node | |
4956 | ||
4957 | Hbound : Uint; | |
4958 | -- High bound of base type of expression | |
4959 | ||
4960 | Lor : Uint; | |
4961 | Hir : Uint; | |
4962 | -- Refined values for low and high bounds, after tightening | |
4963 | ||
4964 | OK1 : Boolean; | |
4965 | -- Used in lower level calls to indicate if call succeeded | |
4966 | ||
4967 | Cindex : Cache_Index; | |
4968 | -- Used to search cache | |
70482933 | 4969 | |
d7a44b14 AC |
4970 | Btyp : Entity_Id; |
4971 | -- Base type | |
4972 | ||
70482933 RK |
4973 | -- Start of processing for Determine_Range |
4974 | ||
4975 | begin | |
3e65bfab AC |
4976 | -- Prevent junk warnings by initializing range variables |
4977 | ||
4978 | Lo := No_Uint; | |
4979 | Hi := No_Uint; | |
4980 | Lor := No_Uint; | |
4981 | Hir := No_Uint; | |
4982 | ||
62be5d0a JM |
4983 | -- For temporary constants internally generated to remove side effects |
4984 | -- we must use the corresponding expression to determine the range of | |
3e65bfab AC |
4985 | -- the expression. But note that the expander can also generate |
4986 | -- constants in other cases, including deferred constants. | |
62be5d0a JM |
4987 | |
4988 | if Is_Entity_Name (N) | |
4989 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4990 | and then Ekind (Entity (N)) = E_Constant | |
4991 | and then Is_Internal_Name (Chars (Entity (N))) | |
4992 | then | |
3e65bfab AC |
4993 | if Present (Expression (Parent (Entity (N)))) then |
4994 | Determine_Range | |
4995 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
62be5d0a | 4996 | |
3e65bfab AC |
4997 | elsif Present (Full_View (Entity (N))) then |
4998 | Determine_Range | |
4999 | (Expression (Parent (Full_View (Entity (N)))), | |
5000 | OK, Lo, Hi, Assume_Valid); | |
70482933 | 5001 | |
3e65bfab AC |
5002 | else |
5003 | OK := False; | |
5004 | end if; | |
5005 | return; | |
5006 | end if; | |
70482933 | 5007 | |
1abad480 | 5008 | -- If type is not defined, we can't determine its range |
70482933 | 5009 | |
1abad480 AC |
5010 | if No (Typ) |
5011 | ||
5012 | -- We don't deal with anything except discrete types | |
5013 | ||
5014 | or else not Is_Discrete_Type (Typ) | |
5015 | ||
f20b5ef4 JM |
5016 | -- Don't deal with enumerated types with non-standard representation |
5017 | ||
5018 | or else (Is_Enumeration_Type (Typ) | |
c56c7d3a SB |
5019 | and then Present (Enum_Pos_To_Rep |
5020 | (Implementation_Base_Type (Typ)))) | |
f20b5ef4 | 5021 | |
1abad480 AC |
5022 | -- Ignore type for which an error has been posted, since range in |
5023 | -- this case may well be a bogosity deriving from the error. Also | |
5024 | -- ignore if error posted on the reference node. | |
5025 | ||
5026 | or else Error_Posted (N) or else Error_Posted (Typ) | |
70482933 RK |
5027 | then |
5028 | OK := False; | |
5029 | return; | |
5030 | end if; | |
5031 | ||
5032 | -- For all other cases, we can determine the range | |
5033 | ||
5034 | OK := True; | |
5035 | ||
675d6070 | 5036 | -- If value is compile time known, then the possible range is the one |
a90bd866 | 5037 | -- value that we know this expression definitely has. |
70482933 RK |
5038 | |
5039 | if Compile_Time_Known_Value (N) then | |
5040 | Lo := Expr_Value (N); | |
5041 | Hi := Lo; | |
5042 | return; | |
5043 | end if; | |
5044 | ||
5045 | -- Return if already in the cache | |
5046 | ||
5047 | Cindex := Cache_Index (N mod Cache_Size); | |
5048 | ||
c800f862 RD |
5049 | if Determine_Range_Cache_N (Cindex) = N |
5050 | and then | |
12be130c EB |
5051 | Determine_Range_Cache_O (Cindex) = Original_Node (N) |
5052 | and then | |
c800f862 RD |
5053 | Determine_Range_Cache_V (Cindex) = Assume_Valid |
5054 | then | |
70482933 RK |
5055 | Lo := Determine_Range_Cache_Lo (Cindex); |
5056 | Hi := Determine_Range_Cache_Hi (Cindex); | |
5057 | return; | |
5058 | end if; | |
5059 | ||
675d6070 TQ |
5060 | -- Otherwise, start by finding the bounds of the type of the expression, |
5061 | -- the value cannot be outside this range (if it is, then we have an | |
5062 | -- overflow situation, which is a separate check, we are talking here | |
5063 | -- only about the expression value). | |
70482933 | 5064 | |
93c3fca7 AC |
5065 | -- First a check, never try to find the bounds of a generic type, since |
5066 | -- these bounds are always junk values, and it is only valid to look at | |
5067 | -- the bounds in an instance. | |
5068 | ||
5069 | if Is_Generic_Type (Typ) then | |
5070 | OK := False; | |
5071 | return; | |
5072 | end if; | |
5073 | ||
c800f862 | 5074 | -- First step, change to use base type unless we know the value is valid |
1c7717c3 | 5075 | |
c800f862 RD |
5076 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
5077 | or else Assume_No_Invalid_Values | |
5078 | or else Assume_Valid | |
1c7717c3 | 5079 | then |
43018f58 ES |
5080 | -- If this is a known valid constant with a nonstatic value, it may |
5081 | -- have inherited a narrower subtype from its initial value; use this | |
5082 | -- saved subtype (see sem_ch3.adb). | |
5083 | ||
5084 | if Is_Entity_Name (N) | |
5085 | and then Ekind (Entity (N)) = E_Constant | |
5086 | and then Present (Actual_Subtype (Entity (N))) | |
5087 | then | |
5088 | Typ := Actual_Subtype (Entity (N)); | |
5089 | end if; | |
5090 | ||
c800f862 RD |
5091 | else |
5092 | Typ := Underlying_Type (Base_Type (Typ)); | |
1c7717c3 AC |
5093 | end if; |
5094 | ||
d7a44b14 AC |
5095 | -- Retrieve the base type. Handle the case where the base type is a |
5096 | -- private enumeration type. | |
5097 | ||
5098 | Btyp := Base_Type (Typ); | |
5099 | ||
5100 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
5101 | Btyp := Full_View (Btyp); | |
5102 | end if; | |
5103 | ||
675d6070 TQ |
5104 | -- We use the actual bound unless it is dynamic, in which case use the |
5105 | -- corresponding base type bound if possible. If we can't get a bound | |
5106 | -- then we figure we can't determine the range (a peculiar case, that | |
5107 | -- perhaps cannot happen, but there is no point in bombing in this | |
12be130c | 5108 | -- optimization circuit). |
c1c22e7a GB |
5109 | |
5110 | -- First the low bound | |
70482933 RK |
5111 | |
5112 | Bound := Type_Low_Bound (Typ); | |
5113 | ||
5114 | if Compile_Time_Known_Value (Bound) then | |
5115 | Lo := Expr_Value (Bound); | |
5116 | ||
d7a44b14 AC |
5117 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
5118 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
70482933 RK |
5119 | |
5120 | else | |
5121 | OK := False; | |
5122 | return; | |
5123 | end if; | |
5124 | ||
c1c22e7a GB |
5125 | -- Now the high bound |
5126 | ||
70482933 RK |
5127 | Bound := Type_High_Bound (Typ); |
5128 | ||
c1c22e7a GB |
5129 | -- We need the high bound of the base type later on, and this should |
5130 | -- always be compile time known. Again, it is not clear that this | |
5131 | -- can ever be false, but no point in bombing. | |
70482933 | 5132 | |
d7a44b14 AC |
5133 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
5134 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
70482933 RK |
5135 | Hi := Hbound; |
5136 | ||
5137 | else | |
5138 | OK := False; | |
5139 | return; | |
5140 | end if; | |
5141 | ||
675d6070 TQ |
5142 | -- If we have a static subtype, then that may have a tighter bound so |
5143 | -- use the upper bound of the subtype instead in this case. | |
c1c22e7a GB |
5144 | |
5145 | if Compile_Time_Known_Value (Bound) then | |
5146 | Hi := Expr_Value (Bound); | |
5147 | end if; | |
5148 | ||
675d6070 TQ |
5149 | -- We may be able to refine this value in certain situations. If any |
5150 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
5151 | -- bounds, and OK1 is set to True. | |
70482933 | 5152 | |
12be130c | 5153 | case Kind is |
70482933 | 5154 | |
12be130c | 5155 | -- Unary operation case |
70482933 | 5156 | |
12be130c EB |
5157 | when N_Op_Abs |
5158 | | N_Op_Minus | |
5159 | | N_Op_Plus | |
5160 | => | |
c800f862 RD |
5161 | Determine_Range |
5162 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
70482933 RK |
5163 | |
5164 | if OK1 then | |
12be130c EB |
5165 | Compute_Range_For_Arithmetic_Op |
5166 | (Kind, Lo_Left, Hi_Left, Lo_Right, Hi_Right, OK1, Lor, Hir); | |
70482933 RK |
5167 | end if; |
5168 | ||
12be130c | 5169 | -- Binary operation case |
70482933 | 5170 | |
12be130c EB |
5171 | when N_Op_Add |
5172 | | N_Op_Divide | |
5173 | | N_Op_Expon | |
5174 | | N_Op_Mod | |
5175 | | N_Op_Multiply | |
5176 | | N_Op_Rem | |
5177 | | N_Op_Subtract | |
5178 | => | |
5179 | Determine_Range | |
5180 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
70482933 | 5181 | |
12be130c EB |
5182 | if OK1 then |
5183 | Determine_Range | |
5184 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
70482933 RK |
5185 | end if; |
5186 | ||
12be130c EB |
5187 | if OK1 then |
5188 | Compute_Range_For_Arithmetic_Op | |
5189 | (Kind, Lo_Left, Hi_Left, Lo_Right, Hi_Right, OK1, Lor, Hir); | |
70482933 RK |
5190 | end if; |
5191 | ||
5192 | -- Attribute reference cases | |
5193 | ||
5194 | when N_Attribute_Reference => | |
12be130c | 5195 | case Get_Attribute_Id (Attribute_Name (N)) is |
70482933 | 5196 | |
b8989631 EB |
5197 | -- For Min/Max attributes, we can refine the range using the |
5198 | -- possible range of values of the attribute expressions. | |
5199 | ||
5200 | when Attribute_Min | |
5201 | | Attribute_Max | |
5202 | => | |
5203 | Determine_Range | |
5204 | (First (Expressions (N)), | |
5205 | OK1, Lo_Left, Hi_Left, Assume_Valid); | |
5206 | ||
5207 | if OK1 then | |
5208 | Determine_Range | |
5209 | (Next (First (Expressions (N))), | |
5210 | OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5211 | end if; | |
5212 | ||
5213 | if OK1 then | |
5214 | Lor := UI_Min (Lo_Left, Lo_Right); | |
5215 | Hir := UI_Max (Hi_Left, Hi_Right); | |
5216 | end if; | |
5217 | ||
70482933 | 5218 | -- For Pos/Val attributes, we can refine the range using the |
f26d5cd3 | 5219 | -- possible range of values of the attribute expression. |
70482933 | 5220 | |
12be130c EB |
5221 | when Attribute_Pos |
5222 | | Attribute_Val | |
d8f43ee6 | 5223 | => |
c800f862 RD |
5224 | Determine_Range |
5225 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
70482933 | 5226 | |
12be130c EB |
5227 | -- For Length and Range_Length attributes, use the bounds of |
5228 | -- the (corresponding index) type to refine the range. | |
70482933 | 5229 | |
12be130c EB |
5230 | when Attribute_Length |
5231 | | Attribute_Range_Length | |
5232 | => | |
70482933 | 5233 | declare |
12be130c EB |
5234 | Ptyp : Entity_Id; |
5235 | Ityp : Entity_Id; | |
70482933 RK |
5236 | |
5237 | LL, LU : Uint; | |
5238 | UL, UU : Uint; | |
5239 | ||
5240 | begin | |
12be130c EB |
5241 | Ptyp := Etype (Prefix (N)); |
5242 | if Is_Access_Type (Ptyp) then | |
5243 | Ptyp := Designated_Type (Ptyp); | |
70482933 RK |
5244 | end if; |
5245 | ||
5246 | -- For string literal, we know exact value | |
5247 | ||
12be130c | 5248 | if Ekind (Ptyp) = E_String_Literal_Subtype then |
70482933 | 5249 | OK := True; |
12be130c EB |
5250 | Lo := String_Literal_Length (Ptyp); |
5251 | Hi := String_Literal_Length (Ptyp); | |
70482933 RK |
5252 | return; |
5253 | end if; | |
5254 | ||
12be130c EB |
5255 | if Is_Array_Type (Ptyp) then |
5256 | Ityp := Get_Index_Subtype (N); | |
70482933 | 5257 | else |
12be130c | 5258 | Ityp := Ptyp; |
70482933 RK |
5259 | end if; |
5260 | ||
12be130c | 5261 | -- If the (index) type is a formal type or derived from |
b4d7b435 AC |
5262 | -- one, the bounds are not static. |
5263 | ||
12be130c | 5264 | if Is_Generic_Type (Root_Type (Ityp)) then |
b4d7b435 AC |
5265 | OK := False; |
5266 | return; | |
5267 | end if; | |
5268 | ||
70482933 | 5269 | Determine_Range |
12be130c | 5270 | (Type_Low_Bound (Ityp), OK1, LL, LU, Assume_Valid); |
70482933 RK |
5271 | |
5272 | if OK1 then | |
5273 | Determine_Range | |
12be130c | 5274 | (Type_High_Bound (Ityp), OK1, UL, UU, Assume_Valid); |
70482933 RK |
5275 | |
5276 | if OK1 then | |
70482933 RK |
5277 | -- The maximum value for Length is the biggest |
5278 | -- possible gap between the values of the bounds. | |
5279 | -- But of course, this value cannot be negative. | |
5280 | ||
c800f862 | 5281 | Hir := UI_Max (Uint_0, UU - LL + 1); |
70482933 | 5282 | |
12be130c | 5283 | -- For a constrained array, the minimum value for |
70482933 | 5284 | -- Length is taken from the actual value of the |
5b599df4 AC |
5285 | -- bounds, since the index will be exactly of this |
5286 | -- subtype. | |
70482933 | 5287 | |
12be130c | 5288 | if Is_Constrained (Ptyp) then |
c800f862 | 5289 | Lor := UI_Max (Uint_0, UL - LU + 1); |
70482933 RK |
5290 | |
5291 | -- For an unconstrained array, the minimum value | |
5292 | -- for length is always zero. | |
5293 | ||
5294 | else | |
5295 | Lor := Uint_0; | |
5296 | end if; | |
5297 | end if; | |
5298 | end if; | |
12be130c EB |
5299 | |
5300 | -- Small optimization: the maximum size in storage units | |
5301 | -- an object can have with GNAT is half of the address | |
5302 | -- space, so we can bound the length of an array declared | |
5303 | -- in Interfaces (or its children) because its component | |
5304 | -- size is at least the storage unit and it is meant to | |
5305 | -- be used to interface actual array objects. | |
5306 | ||
5307 | if Is_Array_Type (Ptyp) then | |
5308 | declare | |
5309 | S : constant Entity_Id := Scope (Base_Type (Ptyp)); | |
5310 | begin | |
5311 | if Is_RTU (S, Interfaces) | |
5312 | or else (S /= Standard_Standard | |
5313 | and then Is_RTU (Scope (S), Interfaces)) | |
5314 | then | |
5315 | Hir := UI_Min (Hir, Half_Address_Space); | |
5316 | end if; | |
5317 | end; | |
5318 | end if; | |
5319 | end; | |
5320 | ||
5321 | -- The maximum default alignment is quite low, but GNAT accepts | |
5322 | -- alignment clauses that are fairly large, but not as large as | |
5323 | -- the maximum size of objects, see below. | |
5324 | ||
5325 | when Attribute_Alignment => | |
5326 | Lor := Uint_0; | |
5327 | Hir := Half_Address_Space; | |
5328 | OK1 := True; | |
5329 | ||
5330 | -- The attribute should have been folded if a component clause | |
5331 | -- was specified, so we assume there is none. | |
5332 | ||
5333 | when Attribute_Bit | |
5334 | | Attribute_First_Bit | |
5335 | => | |
5336 | Lor := Uint_0; | |
5337 | Hir := UI_From_Int (System_Storage_Unit - 1); | |
5338 | OK1 := True; | |
5339 | ||
5340 | -- Likewise about the component clause. Note that Last_Bit | |
5341 | -- yields -1 for a field of size 0 if First_Bit is 0. | |
5342 | ||
5343 | when Attribute_Last_Bit => | |
5344 | Lor := Uint_Minus_1; | |
5345 | Hir := Hi; | |
5346 | OK1 := True; | |
5347 | ||
5348 | -- Likewise about the component clause for Position. The | |
5349 | -- maximum size in storage units that an object can have | |
5350 | -- with GNAT is half of the address space. | |
5351 | ||
5352 | when Attribute_Max_Size_In_Storage_Elements | |
5353 | | Attribute_Position | |
5354 | => | |
5355 | Lor := Uint_0; | |
5356 | Hir := Half_Address_Space; | |
5357 | OK1 := True; | |
5358 | ||
5359 | -- These attributes yield a nonnegative value (we do not set | |
5360 | -- the maximum value because it is too large to be useful). | |
5361 | ||
5362 | when Attribute_Bit_Position | |
5363 | | Attribute_Component_Size | |
5364 | | Attribute_Object_Size | |
5365 | | Attribute_Size | |
5366 | | Attribute_Value_Size | |
5367 | => | |
5368 | Lor := Uint_0; | |
5369 | Hir := Hi; | |
5370 | OK1 := True; | |
5371 | ||
5372 | -- The maximum size is the sum of twice the size of the largest | |
5373 | -- integer for every dimension, rounded up to the next multiple | |
5374 | -- of the maximum alignment, but we add instead of rounding. | |
5375 | ||
5376 | when Attribute_Descriptor_Size => | |
5377 | declare | |
5378 | Max_Align : constant Pos := | |
5379 | Maximum_Alignment * System_Storage_Unit; | |
5380 | Max_Size : constant Uint := | |
5381 | 2 * Esize (Universal_Integer); | |
5382 | Ndims : constant Pos := | |
5383 | Number_Dimensions (Etype (Prefix (N))); | |
5384 | begin | |
5385 | Lor := Uint_0; | |
5386 | Hir := Max_Size * Ndims + Max_Align; | |
5387 | OK1 := True; | |
70482933 RK |
5388 | end; |
5389 | ||
869a06d9 | 5390 | -- No special handling for other attributes for now |
70482933 RK |
5391 | |
5392 | when others => | |
5393 | OK1 := False; | |
5394 | ||
5395 | end case; | |
5396 | ||
70482933 | 5397 | when N_Type_Conversion => |
84c54629 EB |
5398 | -- For a type conversion, we can try to refine the range using the |
5399 | -- converted value. | |
d8ee014f | 5400 | |
84c54629 EB |
5401 | Determine_Range_To_Discrete |
5402 | (Expression (N), OK1, Lor, Hir, Conversion_OK (N), Assume_Valid); | |
70482933 RK |
5403 | |
5404 | -- Nothing special to do for all other expression kinds | |
5405 | ||
5406 | when others => | |
5407 | OK1 := False; | |
5408 | Lor := No_Uint; | |
5409 | Hir := No_Uint; | |
5410 | end case; | |
5411 | ||
5b599df4 AC |
5412 | -- At this stage, if OK1 is true, then we know that the actual result of |
5413 | -- the computed expression is in the range Lor .. Hir. We can use this | |
5414 | -- to restrict the possible range of results. | |
70482933 RK |
5415 | |
5416 | if OK1 then | |
5417 | ||
5b599df4 | 5418 | -- If the refined value of the low bound is greater than the type |
6b6bce61 | 5419 | -- low bound, then reset it to the more restrictive value. However, |
5b599df4 AC |
5420 | -- we do NOT do this for the case of a modular type where the |
5421 | -- possible upper bound on the value is above the base type high | |
5422 | -- bound, because that means the result could wrap. | |
50462286 | 5423 | -- Same applies for the lower bound if it is negative. |
70482933 | 5424 | |
50462286 MP |
5425 | if Is_Modular_Integer_Type (Typ) then |
5426 | if Lor > Lo and then Hir <= Hbound then | |
5427 | Lo := Lor; | |
5428 | end if; | |
70482933 | 5429 | |
50462286 MP |
5430 | if Hir < Hi and then Lor >= Uint_0 then |
5431 | Hi := Hir; | |
5432 | end if; | |
70482933 | 5433 | |
50462286 MP |
5434 | else |
5435 | if Lor > Hi or else Hir < Lo then | |
5436 | ||
5437 | -- If the ranges are disjoint, return the computed range. | |
5438 | ||
5439 | -- The current range-constraining logic would require returning | |
5440 | -- the base type's bounds. However, this would miss an | |
5441 | -- opportunity to warn about out-of-range values for some cases | |
5442 | -- (e.g. when type's upper bound is equal to base type upper | |
5443 | -- bound). | |
5444 | ||
5445 | -- The alternative of always returning the computed values, | |
5446 | -- even when ranges are intersecting, has unwanted effects | |
5447 | -- (mainly useless constraint checks are inserted) in the | |
5448 | -- Enable_Overflow_Check and Apply_Scalar_Range_Check as these | |
5449 | -- bounds have a special interpretation. | |
5450 | ||
5451 | Lo := Lor; | |
5452 | Hi := Hir; | |
5453 | else | |
5454 | ||
5455 | -- If the ranges Lor .. Hir and Lo .. Hi intersect, try to | |
5456 | -- refine the returned range. | |
5457 | ||
5458 | if Lor > Lo then | |
5459 | Lo := Lor; | |
5460 | end if; | |
5461 | ||
5462 | if Hir < Hi then | |
5463 | Hi := Hir; | |
5464 | end if; | |
5465 | end if; | |
70482933 RK |
5466 | end if; |
5467 | end if; | |
5468 | ||
5469 | -- Set cache entry for future call and we are all done | |
5470 | ||
5471 | Determine_Range_Cache_N (Cindex) := N; | |
12be130c | 5472 | Determine_Range_Cache_O (Cindex) := Original_Node (N); |
c800f862 | 5473 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
70482933 RK |
5474 | Determine_Range_Cache_Lo (Cindex) := Lo; |
5475 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
5476 | return; | |
5477 | ||
5b599df4 AC |
5478 | -- If any exception occurs, it means that we have some bug in the compiler, |
5479 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
70482933 RK |
5480 | -- occurrence. However, this is only an optimization attempt, so there is |
5481 | -- really no point in crashing the compiler. Instead we just decide, too | |
5482 | -- bad, we can't figure out a range in this case after all. | |
5483 | ||
5484 | exception | |
5485 | when others => | |
5486 | ||
5487 | -- Debug flag K disables this behavior (useful for debugging) | |
5488 | ||
5489 | if Debug_Flag_K then | |
5490 | raise; | |
5491 | else | |
5492 | OK := False; | |
5493 | Lo := No_Uint; | |
5494 | Hi := No_Uint; | |
5495 | return; | |
5496 | end if; | |
70482933 RK |
5497 | end Determine_Range; |
5498 | ||
6b6bce61 AC |
5499 | ----------------------- |
5500 | -- Determine_Range_R -- | |
5501 | ----------------------- | |
5502 | ||
5503 | procedure Determine_Range_R | |
5504 | (N : Node_Id; | |
5505 | OK : out Boolean; | |
5506 | Lo : out Ureal; | |
5507 | Hi : out Ureal; | |
5508 | Assume_Valid : Boolean := False) | |
5509 | is | |
5510 | Typ : Entity_Id := Etype (N); | |
5511 | -- Type to use, may get reset to base type for possibly invalid entity | |
5512 | ||
5513 | Lo_Left : Ureal; | |
5514 | Hi_Left : Ureal; | |
5515 | -- Lo and Hi bounds of left operand | |
5516 | ||
dcd5fd67 PMR |
5517 | Lo_Right : Ureal := No_Ureal; |
5518 | Hi_Right : Ureal := No_Ureal; | |
6b6bce61 AC |
5519 | -- Lo and Hi bounds of right (or only) operand |
5520 | ||
5521 | Bound : Node_Id; | |
5522 | -- Temp variable used to hold a bound node | |
5523 | ||
5524 | Hbound : Ureal; | |
5525 | -- High bound of base type of expression | |
5526 | ||
5527 | Lor : Ureal; | |
5528 | Hir : Ureal; | |
5529 | -- Refined values for low and high bounds, after tightening | |
5530 | ||
5531 | OK1 : Boolean; | |
5532 | -- Used in lower level calls to indicate if call succeeded | |
5533 | ||
5534 | Cindex : Cache_Index; | |
5535 | -- Used to search cache | |
5536 | ||
5537 | Btyp : Entity_Id; | |
5538 | -- Base type | |
5539 | ||
5540 | function OK_Operands return Boolean; | |
5541 | -- Used for binary operators. Determines the ranges of the left and | |
5542 | -- right operands, and if they are both OK, returns True, and puts | |
5543 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. | |
5544 | ||
5545 | function Round_Machine (B : Ureal) return Ureal; | |
60440d3c | 5546 | -- B is a real bound. Round it to the nearest machine number. |
6b6bce61 AC |
5547 | |
5548 | ----------------- | |
5549 | -- OK_Operands -- | |
5550 | ----------------- | |
5551 | ||
5552 | function OK_Operands return Boolean is | |
5553 | begin | |
5554 | Determine_Range_R | |
5555 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
5556 | ||
5557 | if not OK1 then | |
5558 | return False; | |
5559 | end if; | |
5560 | ||
5561 | Determine_Range_R | |
5562 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5563 | return OK1; | |
5564 | end OK_Operands; | |
5565 | ||
5566 | ------------------- | |
5567 | -- Round_Machine -- | |
5568 | ------------------- | |
5569 | ||
5570 | function Round_Machine (B : Ureal) return Ureal is | |
5571 | begin | |
60440d3c | 5572 | return Machine_Number (Typ, B, N); |
6b6bce61 AC |
5573 | end Round_Machine; |
5574 | ||
5575 | -- Start of processing for Determine_Range_R | |
5576 | ||
5577 | begin | |
5578 | -- Prevent junk warnings by initializing range variables | |
5579 | ||
5580 | Lo := No_Ureal; | |
5581 | Hi := No_Ureal; | |
5582 | Lor := No_Ureal; | |
5583 | Hir := No_Ureal; | |
5584 | ||
5585 | -- For temporary constants internally generated to remove side effects | |
5586 | -- we must use the corresponding expression to determine the range of | |
5587 | -- the expression. But note that the expander can also generate | |
5588 | -- constants in other cases, including deferred constants. | |
5589 | ||
5590 | if Is_Entity_Name (N) | |
5591 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
5592 | and then Ekind (Entity (N)) = E_Constant | |
5593 | and then Is_Internal_Name (Chars (Entity (N))) | |
5594 | then | |
5595 | if Present (Expression (Parent (Entity (N)))) then | |
5596 | Determine_Range_R | |
5597 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
5598 | ||
5599 | elsif Present (Full_View (Entity (N))) then | |
5600 | Determine_Range_R | |
5601 | (Expression (Parent (Full_View (Entity (N)))), | |
5602 | OK, Lo, Hi, Assume_Valid); | |
5603 | ||
5604 | else | |
5605 | OK := False; | |
5606 | end if; | |
d6e8719d | 5607 | |
6b6bce61 AC |
5608 | return; |
5609 | end if; | |
5610 | ||
5611 | -- If type is not defined, we can't determine its range | |
5612 | ||
9324e07d BD |
5613 | pragma Warnings (Off, "condition can only be True if invalid"); |
5614 | -- Otherwise the compiler warns on the check of Float_Rep below, because | |
5615 | -- there is only one value (see types.ads). | |
5616 | ||
6b6bce61 AC |
5617 | if No (Typ) |
5618 | ||
5619 | -- We don't deal with anything except IEEE floating-point types | |
5620 | ||
5621 | or else not Is_Floating_Point_Type (Typ) | |
5622 | or else Float_Rep (Typ) /= IEEE_Binary | |
5623 | ||
5624 | -- Ignore type for which an error has been posted, since range in | |
5625 | -- this case may well be a bogosity deriving from the error. Also | |
5626 | -- ignore if error posted on the reference node. | |
5627 | ||
5628 | or else Error_Posted (N) or else Error_Posted (Typ) | |
5629 | then | |
9324e07d | 5630 | pragma Warnings (On, "condition can only be True if invalid"); |
6b6bce61 AC |
5631 | OK := False; |
5632 | return; | |
5633 | end if; | |
5634 | ||
5635 | -- For all other cases, we can determine the range | |
5636 | ||
5637 | OK := True; | |
5638 | ||
5639 | -- If value is compile time known, then the possible range is the one | |
5640 | -- value that we know this expression definitely has. | |
5641 | ||
5642 | if Compile_Time_Known_Value (N) then | |
5643 | Lo := Expr_Value_R (N); | |
5644 | Hi := Lo; | |
5645 | return; | |
5646 | end if; | |
5647 | ||
5648 | -- Return if already in the cache | |
5649 | ||
5650 | Cindex := Cache_Index (N mod Cache_Size); | |
5651 | ||
5652 | if Determine_Range_Cache_N (Cindex) = N | |
5653 | and then | |
12be130c EB |
5654 | Determine_Range_Cache_O (Cindex) = Original_Node (N) |
5655 | and then | |
6b6bce61 AC |
5656 | Determine_Range_Cache_V (Cindex) = Assume_Valid |
5657 | then | |
5658 | Lo := Determine_Range_Cache_Lo_R (Cindex); | |
5659 | Hi := Determine_Range_Cache_Hi_R (Cindex); | |
5660 | return; | |
5661 | end if; | |
5662 | ||
5663 | -- Otherwise, start by finding the bounds of the type of the expression, | |
5664 | -- the value cannot be outside this range (if it is, then we have an | |
5665 | -- overflow situation, which is a separate check, we are talking here | |
5666 | -- only about the expression value). | |
5667 | ||
5668 | -- First a check, never try to find the bounds of a generic type, since | |
5669 | -- these bounds are always junk values, and it is only valid to look at | |
5670 | -- the bounds in an instance. | |
5671 | ||
5672 | if Is_Generic_Type (Typ) then | |
5673 | OK := False; | |
5674 | return; | |
5675 | end if; | |
5676 | ||
5677 | -- First step, change to use base type unless we know the value is valid | |
5678 | ||
5679 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) | |
5680 | or else Assume_No_Invalid_Values | |
5681 | or else Assume_Valid | |
5682 | then | |
5683 | null; | |
5684 | else | |
5685 | Typ := Underlying_Type (Base_Type (Typ)); | |
5686 | end if; | |
5687 | ||
5688 | -- Retrieve the base type. Handle the case where the base type is a | |
5689 | -- private type. | |
5690 | ||
5691 | Btyp := Base_Type (Typ); | |
5692 | ||
5693 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
5694 | Btyp := Full_View (Btyp); | |
5695 | end if; | |
5696 | ||
5697 | -- We use the actual bound unless it is dynamic, in which case use the | |
5698 | -- corresponding base type bound if possible. If we can't get a bound | |
5699 | -- then we figure we can't determine the range (a peculiar case, that | |
5700 | -- perhaps cannot happen, but there is no point in bombing in this | |
5701 | -- optimization circuit). | |
5702 | ||
5703 | -- First the low bound | |
5704 | ||
5705 | Bound := Type_Low_Bound (Typ); | |
5706 | ||
5707 | if Compile_Time_Known_Value (Bound) then | |
5708 | Lo := Expr_Value_R (Bound); | |
5709 | ||
5710 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then | |
5711 | Lo := Expr_Value_R (Type_Low_Bound (Btyp)); | |
5712 | ||
5713 | else | |
5714 | OK := False; | |
5715 | return; | |
5716 | end if; | |
5717 | ||
5718 | -- Now the high bound | |
5719 | ||
5720 | Bound := Type_High_Bound (Typ); | |
5721 | ||
5722 | -- We need the high bound of the base type later on, and this should | |
5723 | -- always be compile time known. Again, it is not clear that this | |
5724 | -- can ever be false, but no point in bombing. | |
5725 | ||
5726 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then | |
5727 | Hbound := Expr_Value_R (Type_High_Bound (Btyp)); | |
5728 | Hi := Hbound; | |
5729 | ||
5730 | else | |
5731 | OK := False; | |
5732 | return; | |
5733 | end if; | |
5734 | ||
5735 | -- If we have a static subtype, then that may have a tighter bound so | |
5736 | -- use the upper bound of the subtype instead in this case. | |
5737 | ||
5738 | if Compile_Time_Known_Value (Bound) then | |
5739 | Hi := Expr_Value_R (Bound); | |
5740 | end if; | |
5741 | ||
5742 | -- We may be able to refine this value in certain situations. If any | |
5743 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
5744 | -- bounds, and OK1 is set to True. | |
5745 | ||
5746 | case Nkind (N) is | |
5747 | ||
5748 | -- For unary plus, result is limited by range of operand | |
5749 | ||
5750 | when N_Op_Plus => | |
5751 | Determine_Range_R | |
5752 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
5753 | ||
5754 | -- For unary minus, determine range of operand, and negate it | |
5755 | ||
5756 | when N_Op_Minus => | |
5757 | Determine_Range_R | |
5758 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
5759 | ||
5760 | if OK1 then | |
5761 | Lor := -Hi_Right; | |
5762 | Hir := -Lo_Right; | |
5763 | end if; | |
5764 | ||
5765 | -- For binary addition, get range of each operand and do the | |
5766 | -- addition to get the result range. | |
5767 | ||
5768 | when N_Op_Add => | |
5769 | if OK_Operands then | |
5770 | Lor := Round_Machine (Lo_Left + Lo_Right); | |
5771 | Hir := Round_Machine (Hi_Left + Hi_Right); | |
5772 | end if; | |
5773 | ||
5774 | -- For binary subtraction, get range of each operand and do the worst | |
5775 | -- case subtraction to get the result range. | |
5776 | ||
5777 | when N_Op_Subtract => | |
5778 | if OK_Operands then | |
5779 | Lor := Round_Machine (Lo_Left - Hi_Right); | |
5780 | Hir := Round_Machine (Hi_Left - Lo_Right); | |
5781 | end if; | |
5782 | ||
5783 | -- For multiplication, get range of each operand and do the | |
5784 | -- four multiplications to get the result range. | |
5785 | ||
5786 | when N_Op_Multiply => | |
5787 | if OK_Operands then | |
5788 | declare | |
5789 | M1 : constant Ureal := Round_Machine (Lo_Left * Lo_Right); | |
5790 | M2 : constant Ureal := Round_Machine (Lo_Left * Hi_Right); | |
5791 | M3 : constant Ureal := Round_Machine (Hi_Left * Lo_Right); | |
5792 | M4 : constant Ureal := Round_Machine (Hi_Left * Hi_Right); | |
94295b25 | 5793 | |
6b6bce61 AC |
5794 | begin |
5795 | Lor := UR_Min (UR_Min (M1, M2), UR_Min (M3, M4)); | |
5796 | Hir := UR_Max (UR_Max (M1, M2), UR_Max (M3, M4)); | |
5797 | end; | |
5798 | end if; | |
5799 | ||
5800 | -- For division, consider separately the cases where the right | |
5801 | -- operand is positive or negative. Otherwise, the right operand | |
5802 | -- can be arbitrarily close to zero, so the result is likely to | |
5803 | -- be unbounded in one direction, do not attempt to compute it. | |
5804 | ||
5805 | when N_Op_Divide => | |
5806 | if OK_Operands then | |
5807 | ||
5808 | -- Right operand is positive | |
5809 | ||
5810 | if Lo_Right > Ureal_0 then | |
5811 | ||
5812 | -- If the low bound of the left operand is negative, obtain | |
5813 | -- the overall low bound by dividing it by the smallest | |
5814 | -- value of the right operand, and otherwise by the largest | |
5815 | -- value of the right operand. | |
5816 | ||
5817 | if Lo_Left < Ureal_0 then | |
5818 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5819 | else | |
5820 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5821 | end if; | |
5822 | ||
5823 | -- If the high bound of the left operand is negative, obtain | |
5824 | -- the overall high bound by dividing it by the largest | |
5825 | -- value of the right operand, and otherwise by the | |
5826 | -- smallest value of the right operand. | |
5827 | ||
5828 | if Hi_Left < Ureal_0 then | |
5829 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5830 | else | |
5831 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5832 | end if; | |
5833 | ||
5834 | -- Right operand is negative | |
5835 | ||
5836 | elsif Hi_Right < Ureal_0 then | |
5837 | ||
5838 | -- If the low bound of the left operand is negative, obtain | |
5839 | -- the overall low bound by dividing it by the largest | |
5840 | -- value of the right operand, and otherwise by the smallest | |
5841 | -- value of the right operand. | |
5842 | ||
5843 | if Lo_Left < Ureal_0 then | |
5844 | Lor := Round_Machine (Lo_Left / Hi_Right); | |
5845 | else | |
5846 | Lor := Round_Machine (Lo_Left / Lo_Right); | |
5847 | end if; | |
5848 | ||
5849 | -- If the high bound of the left operand is negative, obtain | |
5850 | -- the overall high bound by dividing it by the smallest | |
5851 | -- value of the right operand, and otherwise by the | |
5852 | -- largest value of the right operand. | |
5853 | ||
5854 | if Hi_Left < Ureal_0 then | |
5855 | Hir := Round_Machine (Hi_Left / Lo_Right); | |
5856 | else | |
5857 | Hir := Round_Machine (Hi_Left / Hi_Right); | |
5858 | end if; | |
5859 | ||
5860 | else | |
5861 | OK1 := False; | |
5862 | end if; | |
5863 | end if; | |
5864 | ||
6b6bce61 | 5865 | when N_Type_Conversion => |
3c77943e YM |
5866 | |
5867 | -- For type conversion from one floating-point type to another, we | |
5868 | -- can refine the range using the converted value. | |
5869 | ||
5870 | if Is_Floating_Point_Type (Etype (Expression (N))) then | |
5871 | Determine_Range_R (Expression (N), OK1, Lor, Hir, Assume_Valid); | |
5872 | ||
5873 | -- When converting an integer to a floating-point type, determine | |
5874 | -- the range in integer first, and then convert the bounds. | |
5875 | ||
5876 | elsif Is_Discrete_Type (Etype (Expression (N))) then | |
5877 | declare | |
94295b25 AC |
5878 | Hir_Int : Uint; |
5879 | Lor_Int : Uint; | |
5880 | ||
3c77943e | 5881 | begin |
94295b25 AC |
5882 | Determine_Range |
5883 | (Expression (N), OK1, Lor_Int, Hir_Int, Assume_Valid); | |
3c77943e YM |
5884 | |
5885 | if OK1 then | |
5886 | Lor := Round_Machine (UR_From_Uint (Lor_Int)); | |
5887 | Hir := Round_Machine (UR_From_Uint (Hir_Int)); | |
5888 | end if; | |
5889 | end; | |
5890 | ||
5891 | else | |
5892 | OK1 := False; | |
5893 | end if; | |
6b6bce61 AC |
5894 | |
5895 | -- Nothing special to do for all other expression kinds | |
5896 | ||
5897 | when others => | |
5898 | OK1 := False; | |
5899 | Lor := No_Ureal; | |
5900 | Hir := No_Ureal; | |
5901 | end case; | |
5902 | ||
5903 | -- At this stage, if OK1 is true, then we know that the actual result of | |
5904 | -- the computed expression is in the range Lor .. Hir. We can use this | |
5905 | -- to restrict the possible range of results. | |
5906 | ||
5907 | if OK1 then | |
5908 | ||
5909 | -- If the refined value of the low bound is greater than the type | |
5910 | -- low bound, then reset it to the more restrictive value. | |
5911 | ||
5912 | if Lor > Lo then | |
5913 | Lo := Lor; | |
5914 | end if; | |
5915 | ||
5916 | -- Similarly, if the refined value of the high bound is less than the | |
5917 | -- value so far, then reset it to the more restrictive value. | |
5918 | ||
5919 | if Hir < Hi then | |
5920 | Hi := Hir; | |
5921 | end if; | |
5922 | end if; | |
5923 | ||
5924 | -- Set cache entry for future call and we are all done | |
5925 | ||
5926 | Determine_Range_Cache_N (Cindex) := N; | |
12be130c | 5927 | Determine_Range_Cache_O (Cindex) := Original_Node (N); |
6b6bce61 AC |
5928 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
5929 | Determine_Range_Cache_Lo_R (Cindex) := Lo; | |
5930 | Determine_Range_Cache_Hi_R (Cindex) := Hi; | |
5931 | return; | |
5932 | ||
5933 | -- If any exception occurs, it means that we have some bug in the compiler, | |
5934 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
5935 | -- occurrence. However, this is only an optimization attempt, so there is | |
5936 | -- really no point in crashing the compiler. Instead we just decide, too | |
5937 | -- bad, we can't figure out a range in this case after all. | |
5938 | ||
5939 | exception | |
5940 | when others => | |
5941 | ||
5942 | -- Debug flag K disables this behavior (useful for debugging) | |
5943 | ||
5944 | if Debug_Flag_K then | |
5945 | raise; | |
5946 | else | |
5947 | OK := False; | |
5948 | Lo := No_Ureal; | |
5949 | Hi := No_Ureal; | |
5950 | return; | |
5951 | end if; | |
5952 | end Determine_Range_R; | |
5953 | ||
84c54629 EB |
5954 | --------------------------------- |
5955 | -- Determine_Range_To_Discrete -- | |
5956 | --------------------------------- | |
5957 | ||
5958 | procedure Determine_Range_To_Discrete | |
5959 | (N : Node_Id; | |
5960 | OK : out Boolean; | |
5961 | Lo : out Uint; | |
5962 | Hi : out Uint; | |
5963 | Fixed_Int : Boolean := False; | |
5964 | Assume_Valid : Boolean := False) | |
5965 | is | |
5966 | Typ : constant Entity_Id := Etype (N); | |
5967 | ||
5968 | begin | |
5969 | -- For a discrete type, simply defer to Determine_Range | |
5970 | ||
5971 | if Is_Discrete_Type (Typ) then | |
5972 | Determine_Range (N, OK, Lo, Hi, Assume_Valid); | |
5973 | ||
5974 | -- For a fixed point type treated as an integer, we can determine the | |
5975 | -- range using the Corresponding_Integer_Value of the bounds of the | |
5976 | -- type or base type. This is done by the calls to Expr_Value below. | |
5977 | ||
5978 | elsif Is_Fixed_Point_Type (Typ) and then Fixed_Int then | |
5979 | declare | |
5980 | Btyp, Ftyp : Entity_Id; | |
5981 | Bound : Node_Id; | |
5982 | ||
5983 | begin | |
5984 | if Assume_Valid then | |
5985 | Ftyp := Typ; | |
5986 | else | |
5987 | Ftyp := Underlying_Type (Base_Type (Typ)); | |
5988 | end if; | |
5989 | ||
5990 | Btyp := Base_Type (Ftyp); | |
5991 | ||
5992 | -- First the low bound | |
5993 | ||
5994 | Bound := Type_Low_Bound (Ftyp); | |
5995 | ||
5996 | if Compile_Time_Known_Value (Bound) then | |
5997 | Lo := Expr_Value (Bound); | |
5998 | else | |
5999 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
6000 | end if; | |
6001 | ||
6002 | -- Then the high bound | |
6003 | ||
6004 | Bound := Type_High_Bound (Ftyp); | |
6005 | ||
6006 | if Compile_Time_Known_Value (Bound) then | |
6007 | Hi := Expr_Value (Bound); | |
6008 | else | |
6009 | Hi := Expr_Value (Type_High_Bound (Btyp)); | |
6010 | end if; | |
6011 | ||
6012 | OK := True; | |
6013 | end; | |
6014 | ||
6015 | -- For a floating-point type, we can determine the range in real first, | |
6016 | -- and then convert the bounds using UR_To_Uint, which correctly rounds | |
6017 | -- away from zero when half way between two integers, as required by | |
6018 | -- normal Ada 95 rounding semantics. But this is only possible because | |
6019 | -- GNATprove's analysis rules out the possibility of a NaN or infinite. | |
6020 | ||
6021 | elsif GNATprove_Mode and then Is_Floating_Point_Type (Typ) then | |
6022 | declare | |
6023 | Lo_Real, Hi_Real : Ureal; | |
6024 | ||
6025 | begin | |
6026 | Determine_Range_R (N, OK, Lo_Real, Hi_Real, Assume_Valid); | |
6027 | ||
6028 | if OK then | |
6029 | Lo := UR_To_Uint (Lo_Real); | |
6030 | Hi := UR_To_Uint (Hi_Real); | |
6031 | else | |
6032 | Lo := No_Uint; | |
6033 | Hi := No_Uint; | |
6034 | end if; | |
6035 | end; | |
6036 | ||
6037 | else | |
6038 | Lo := No_Uint; | |
6039 | Hi := No_Uint; | |
6040 | OK := False; | |
6041 | end if; | |
6042 | end Determine_Range_To_Discrete; | |
6043 | ||
70482933 RK |
6044 | ------------------------------------ |
6045 | -- Discriminant_Checks_Suppressed -- | |
6046 | ------------------------------------ | |
6047 | ||
6048 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6049 | begin | |
fbf5a39b AC |
6050 | if Present (E) then |
6051 | if Is_Unchecked_Union (E) then | |
6052 | return True; | |
6053 | elsif Checks_May_Be_Suppressed (E) then | |
6054 | return Is_Check_Suppressed (E, Discriminant_Check); | |
6055 | end if; | |
6056 | end if; | |
6057 | ||
3217f71e | 6058 | return Scope_Suppress.Suppress (Discriminant_Check); |
70482933 RK |
6059 | end Discriminant_Checks_Suppressed; |
6060 | ||
6061 | -------------------------------- | |
6062 | -- Division_Checks_Suppressed -- | |
6063 | -------------------------------- | |
6064 | ||
6065 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6066 | begin | |
fbf5a39b AC |
6067 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6068 | return Is_Check_Suppressed (E, Division_Check); | |
6069 | else | |
3217f71e | 6070 | return Scope_Suppress.Suppress (Division_Check); |
fbf5a39b | 6071 | end if; |
70482933 RK |
6072 | end Division_Checks_Suppressed; |
6073 | ||
59f4d038 RD |
6074 | -------------------------------------- |
6075 | -- Duplicated_Tag_Checks_Suppressed -- | |
6076 | -------------------------------------- | |
6077 | ||
6078 | function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6079 | begin | |
6080 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
6081 | return Is_Check_Suppressed (E, Duplicated_Tag_Check); | |
6082 | else | |
6083 | return Scope_Suppress.Suppress (Duplicated_Tag_Check); | |
6084 | end if; | |
6085 | end Duplicated_Tag_Checks_Suppressed; | |
6086 | ||
70482933 RK |
6087 | ----------------------------------- |
6088 | -- Elaboration_Checks_Suppressed -- | |
6089 | ----------------------------------- | |
6090 | ||
6091 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6092 | begin | |
f02b8bb8 RD |
6093 | -- The complication in this routine is that if we are in the dynamic |
6094 | -- model of elaboration, we also check All_Checks, since All_Checks | |
6095 | -- does not set Elaboration_Check explicitly. | |
6096 | ||
fbf5a39b AC |
6097 | if Present (E) then |
6098 | if Kill_Elaboration_Checks (E) then | |
6099 | return True; | |
f02b8bb8 | 6100 | |
fbf5a39b | 6101 | elsif Checks_May_Be_Suppressed (E) then |
f02b8bb8 RD |
6102 | if Is_Check_Suppressed (E, Elaboration_Check) then |
6103 | return True; | |
90e491a7 | 6104 | |
f02b8bb8 RD |
6105 | elsif Dynamic_Elaboration_Checks then |
6106 | return Is_Check_Suppressed (E, All_Checks); | |
90e491a7 | 6107 | |
f02b8bb8 RD |
6108 | else |
6109 | return False; | |
6110 | end if; | |
fbf5a39b AC |
6111 | end if; |
6112 | end if; | |
6113 | ||
3217f71e | 6114 | if Scope_Suppress.Suppress (Elaboration_Check) then |
f02b8bb8 | 6115 | return True; |
90e491a7 | 6116 | |
f02b8bb8 | 6117 | elsif Dynamic_Elaboration_Checks then |
3217f71e | 6118 | return Scope_Suppress.Suppress (All_Checks); |
90e491a7 | 6119 | |
f02b8bb8 RD |
6120 | else |
6121 | return False; | |
6122 | end if; | |
70482933 RK |
6123 | end Elaboration_Checks_Suppressed; |
6124 | ||
fbf5a39b AC |
6125 | --------------------------- |
6126 | -- Enable_Overflow_Check -- | |
6127 | --------------------------- | |
6128 | ||
6129 | procedure Enable_Overflow_Check (N : Node_Id) is | |
d6e8719d | 6130 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
15c94a55 | 6131 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
acad3c0a AC |
6132 | Chk : Nat; |
6133 | OK : Boolean; | |
6134 | Ent : Entity_Id; | |
6135 | Ofs : Uint; | |
6136 | Lo : Uint; | |
6137 | Hi : Uint; | |
70482933 | 6138 | |
b7c874a7 AC |
6139 | Do_Ovflow_Check : Boolean; |
6140 | ||
70482933 | 6141 | begin |
fbf5a39b AC |
6142 | if Debug_Flag_CC then |
6143 | w ("Enable_Overflow_Check for node ", Int (N)); | |
6144 | Write_Str (" Source location = "); | |
6145 | wl (Sloc (N)); | |
11b4899f | 6146 | pg (Union_Id (N)); |
70482933 | 6147 | end if; |
70482933 | 6148 | |
3d5952be AC |
6149 | -- No check if overflow checks suppressed for type of node |
6150 | ||
a7f1b24f | 6151 | if Overflow_Checks_Suppressed (Etype (N)) then |
3d5952be AC |
6152 | return; |
6153 | ||
991395ab AC |
6154 | -- Nothing to do for unsigned integer types, which do not overflow |
6155 | ||
6156 | elsif Is_Modular_Integer_Type (Typ) then | |
6157 | return; | |
acad3c0a AC |
6158 | end if; |
6159 | ||
a7f1b24f | 6160 | -- This is the point at which processing for STRICT mode diverges |
5707e389 AC |
6161 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
6162 | -- probably more extreme that it needs to be, but what is going on here | |
6163 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
a7f1b24f | 6164 | -- to leave the processing for STRICT mode untouched. There were |
5707e389 | 6165 | -- two reasons for this. First it avoided any incompatible change of |
a7f1b24f | 6166 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
5707e389 | 6167 | -- legacy reliable. |
acad3c0a | 6168 | |
a7f1b24f | 6169 | -- The big difference is that in STRICT mode there is a fair amount of |
acad3c0a AC |
6170 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
6171 | -- know that no check is needed. We skip all that in the two new modes, | |
6172 | -- since really overflow checking happens over a whole subtree, and we | |
6173 | -- do the corresponding optimizations later on when applying the checks. | |
6174 | ||
6175 | if Mode in Minimized_Or_Eliminated then | |
a7f1b24f RD |
6176 | if not (Overflow_Checks_Suppressed (Etype (N))) |
6177 | and then not (Is_Entity_Name (N) | |
6178 | and then Overflow_Checks_Suppressed (Entity (N))) | |
6179 | then | |
6180 | Activate_Overflow_Check (N); | |
6181 | end if; | |
acad3c0a AC |
6182 | |
6183 | if Debug_Flag_CC then | |
6184 | w ("Minimized/Eliminated mode"); | |
6185 | end if; | |
6186 | ||
6187 | return; | |
6188 | end if; | |
6189 | ||
a7f1b24f | 6190 | -- Remainder of processing is for STRICT case, and is unchanged from |
3ada950b | 6191 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
991395ab | 6192 | |
675d6070 TQ |
6193 | -- Nothing to do if the range of the result is known OK. We skip this |
6194 | -- for conversions, since the caller already did the check, and in any | |
6195 | -- case the condition for deleting the check for a type conversion is | |
f2cbd970 | 6196 | -- different. |
70482933 | 6197 | |
acad3c0a | 6198 | if Nkind (N) /= N_Type_Conversion then |
c800f862 | 6199 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
70482933 | 6200 | |
f2cbd970 JM |
6201 | -- Note in the test below that we assume that the range is not OK |
6202 | -- if a bound of the range is equal to that of the type. That's not | |
6203 | -- quite accurate but we do this for the following reasons: | |
70482933 | 6204 | |
fbf5a39b AC |
6205 | -- a) The way that Determine_Range works, it will typically report |
6206 | -- the bounds of the value as being equal to the bounds of the | |
6207 | -- type, because it either can't tell anything more precise, or | |
6208 | -- does not think it is worth the effort to be more precise. | |
70482933 | 6209 | |
fbf5a39b AC |
6210 | -- b) It is very unusual to have a situation in which this would |
6211 | -- generate an unnecessary overflow check (an example would be | |
6212 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
f2cbd970 | 6213 | -- literal value one is added). |
70482933 | 6214 | |
fbf5a39b AC |
6215 | -- c) The alternative is a lot of special casing in this routine |
6216 | -- which would partially duplicate Determine_Range processing. | |
70482933 | 6217 | |
b7c874a7 AC |
6218 | if OK then |
6219 | Do_Ovflow_Check := True; | |
6220 | ||
6221 | -- Note that the following checks are quite deliberately > and < | |
6222 | -- rather than >= and <= as explained above. | |
6223 | ||
6224 | if Lo > Expr_Value (Type_Low_Bound (Typ)) | |
6225 | and then | |
6226 | Hi < Expr_Value (Type_High_Bound (Typ)) | |
6227 | then | |
6228 | Do_Ovflow_Check := False; | |
6229 | ||
6230 | -- Despite the comments above, it is worth dealing specially with | |
12be130c EB |
6231 | -- division. The only case where integer division can overflow is |
6232 | -- (largest negative number) / (-1). So we will do an extra range | |
6233 | -- analysis to see if this is possible. | |
b7c874a7 AC |
6234 | |
6235 | elsif Nkind (N) = N_Op_Divide then | |
6236 | Determine_Range | |
6237 | (Left_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
6238 | ||
6239 | if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then | |
6240 | Do_Ovflow_Check := False; | |
6241 | ||
6242 | else | |
6243 | Determine_Range | |
6244 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
6245 | ||
6246 | if OK and then (Lo > Uint_Minus_1 | |
6247 | or else | |
6248 | Hi < Uint_Minus_1) | |
6249 | then | |
6250 | Do_Ovflow_Check := False; | |
6251 | end if; | |
6252 | end if; | |
12be130c EB |
6253 | |
6254 | -- Likewise for Abs/Minus, the only case where the operation can | |
6255 | -- overflow is when the operand is the largest negative number. | |
6256 | ||
4a08c95c | 6257 | elsif Nkind (N) in N_Op_Abs | N_Op_Minus then |
12be130c EB |
6258 | Determine_Range |
6259 | (Right_Opnd (N), OK, Lo, Hi, Assume_Valid => True); | |
6260 | ||
6261 | if OK and then Lo > Expr_Value (Type_Low_Bound (Typ)) then | |
6262 | Do_Ovflow_Check := False; | |
6263 | end if; | |
fbf5a39b AC |
6264 | end if; |
6265 | ||
b7c874a7 AC |
6266 | -- If no overflow check required, we are done |
6267 | ||
6268 | if not Do_Ovflow_Check then | |
6269 | if Debug_Flag_CC then | |
6270 | w ("No overflow check required"); | |
6271 | end if; | |
6272 | ||
6273 | return; | |
6274 | end if; | |
fbf5a39b AC |
6275 | end if; |
6276 | end if; | |
6277 | ||
675d6070 TQ |
6278 | -- If not in optimizing mode, set flag and we are done. We are also done |
6279 | -- (and just set the flag) if the type is not a discrete type, since it | |
6280 | -- is not worth the effort to eliminate checks for other than discrete | |
6281 | -- types. In addition, we take this same path if we have stored the | |
6282 | -- maximum number of checks possible already (a very unlikely situation, | |
a90bd866 | 6283 | -- but we do not want to blow up). |
fbf5a39b AC |
6284 | |
6285 | if Optimization_Level = 0 | |
6286 | or else not Is_Discrete_Type (Etype (N)) | |
6287 | or else Num_Saved_Checks = Saved_Checks'Last | |
70482933 | 6288 | then |
11b4899f | 6289 | Activate_Overflow_Check (N); |
fbf5a39b AC |
6290 | |
6291 | if Debug_Flag_CC then | |
6292 | w ("Optimization off"); | |
6293 | end if; | |
6294 | ||
70482933 | 6295 | return; |
fbf5a39b | 6296 | end if; |
70482933 | 6297 | |
fbf5a39b AC |
6298 | -- Otherwise evaluate and check the expression |
6299 | ||
6300 | Find_Check | |
6301 | (Expr => N, | |
6302 | Check_Type => 'O', | |
6303 | Target_Type => Empty, | |
6304 | Entry_OK => OK, | |
6305 | Check_Num => Chk, | |
6306 | Ent => Ent, | |
6307 | Ofs => Ofs); | |
6308 | ||
6309 | if Debug_Flag_CC then | |
6310 | w ("Called Find_Check"); | |
6311 | w (" OK = ", OK); | |
6312 | ||
6313 | if OK then | |
6314 | w (" Check_Num = ", Chk); | |
6315 | w (" Ent = ", Int (Ent)); | |
6316 | Write_Str (" Ofs = "); | |
6317 | pid (Ofs); | |
6318 | end if; | |
6319 | end if; | |
70482933 | 6320 | |
fbf5a39b AC |
6321 | -- If check is not of form to optimize, then set flag and we are done |
6322 | ||
6323 | if not OK then | |
11b4899f | 6324 | Activate_Overflow_Check (N); |
70482933 | 6325 | return; |
fbf5a39b | 6326 | end if; |
70482933 | 6327 | |
fbf5a39b AC |
6328 | -- If check is already performed, then return without setting flag |
6329 | ||
6330 | if Chk /= 0 then | |
6331 | if Debug_Flag_CC then | |
6332 | w ("Check suppressed!"); | |
6333 | end if; | |
70482933 | 6334 | |
70482933 | 6335 | return; |
fbf5a39b | 6336 | end if; |
70482933 | 6337 | |
fbf5a39b AC |
6338 | -- Here we will make a new entry for the new check |
6339 | ||
11b4899f | 6340 | Activate_Overflow_Check (N); |
fbf5a39b AC |
6341 | Num_Saved_Checks := Num_Saved_Checks + 1; |
6342 | Saved_Checks (Num_Saved_Checks) := | |
6343 | (Killed => False, | |
6344 | Entity => Ent, | |
6345 | Offset => Ofs, | |
6346 | Check_Type => 'O', | |
6347 | Target_Type => Empty); | |
6348 | ||
6349 | if Debug_Flag_CC then | |
6350 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
6351 | w (" Entity = ", Int (Ent)); | |
6352 | Write_Str (" Offset = "); | |
6353 | pid (Ofs); | |
6354 | w (" Check_Type = O"); | |
6355 | w (" Target_Type = Empty"); | |
6356 | end if; | |
70482933 | 6357 | |
675d6070 | 6358 | -- If we get an exception, then something went wrong, probably because of |
637a41a5 AC |
6359 | -- an error in the structure of the tree due to an incorrect program. Or |
6360 | -- it may be a bug in the optimization circuit. In either case the safest | |
675d6070 | 6361 | -- thing is simply to set the check flag unconditionally. |
fbf5a39b AC |
6362 | |
6363 | exception | |
6364 | when others => | |
11b4899f | 6365 | Activate_Overflow_Check (N); |
fbf5a39b AC |
6366 | |
6367 | if Debug_Flag_CC then | |
6368 | w (" exception occurred, overflow flag set"); | |
6369 | end if; | |
6370 | ||
6371 | return; | |
6372 | end Enable_Overflow_Check; | |
6373 | ||
6374 | ------------------------ | |
6375 | -- Enable_Range_Check -- | |
6376 | ------------------------ | |
6377 | ||
6378 | procedure Enable_Range_Check (N : Node_Id) is | |
6379 | Chk : Nat; | |
6380 | OK : Boolean; | |
6381 | Ent : Entity_Id; | |
6382 | Ofs : Uint; | |
6383 | Ttyp : Entity_Id; | |
6384 | P : Node_Id; | |
6385 | ||
6386 | begin | |
675d6070 | 6387 | -- Return if unchecked type conversion with range check killed. In this |
a90bd866 | 6388 | -- case we never set the flag (that's what Kill_Range_Check is about). |
fbf5a39b AC |
6389 | |
6390 | if Nkind (N) = N_Unchecked_Type_Conversion | |
6391 | and then Kill_Range_Check (N) | |
70482933 RK |
6392 | then |
6393 | return; | |
fbf5a39b | 6394 | end if; |
70482933 | 6395 | |
c7532b2d | 6396 | -- Do not set range check flag if parent is assignment statement or |
a521dc37 | 6397 | -- object declaration with Suppress_Assignment_Checks flag set. |
c7532b2d | 6398 | |
4a08c95c | 6399 | if Nkind (Parent (N)) in N_Assignment_Statement | N_Object_Declaration |
c7532b2d AC |
6400 | and then Suppress_Assignment_Checks (Parent (N)) |
6401 | then | |
6402 | return; | |
6403 | end if; | |
6404 | ||
c064e066 RD |
6405 | -- Check for various cases where we should suppress the range check |
6406 | ||
6407 | -- No check if range checks suppressed for type of node | |
6408 | ||
637a41a5 | 6409 | if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
c064e066 RD |
6410 | return; |
6411 | ||
6412 | -- No check if node is an entity name, and range checks are suppressed | |
6413 | -- for this entity, or for the type of this entity. | |
6414 | ||
6415 | elsif Is_Entity_Name (N) | |
6416 | and then (Range_Checks_Suppressed (Entity (N)) | |
637a41a5 | 6417 | or else Range_Checks_Suppressed (Etype (Entity (N)))) |
c064e066 RD |
6418 | then |
6419 | return; | |
6420 | ||
6421 | -- No checks if index of array, and index checks are suppressed for | |
6422 | -- the array object or the type of the array. | |
6423 | ||
6424 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
6425 | declare | |
6426 | Pref : constant Node_Id := Prefix (Parent (N)); | |
6427 | begin | |
6428 | if Is_Entity_Name (Pref) | |
6429 | and then Index_Checks_Suppressed (Entity (Pref)) | |
6430 | then | |
6431 | return; | |
6432 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
6433 | return; | |
6434 | end if; | |
6435 | end; | |
6436 | end if; | |
6437 | ||
fbf5a39b | 6438 | -- Debug trace output |
70482933 | 6439 | |
fbf5a39b AC |
6440 | if Debug_Flag_CC then |
6441 | w ("Enable_Range_Check for node ", Int (N)); | |
6442 | Write_Str (" Source location = "); | |
6443 | wl (Sloc (N)); | |
11b4899f | 6444 | pg (Union_Id (N)); |
fbf5a39b AC |
6445 | end if; |
6446 | ||
675d6070 TQ |
6447 | -- If not in optimizing mode, set flag and we are done. We are also done |
6448 | -- (and just set the flag) if the type is not a discrete type, since it | |
6449 | -- is not worth the effort to eliminate checks for other than discrete | |
6450 | -- types. In addition, we take this same path if we have stored the | |
6451 | -- maximum number of checks possible already (a very unlikely situation, | |
a90bd866 | 6452 | -- but we do not want to blow up). |
fbf5a39b AC |
6453 | |
6454 | if Optimization_Level = 0 | |
6455 | or else No (Etype (N)) | |
6456 | or else not Is_Discrete_Type (Etype (N)) | |
6457 | or else Num_Saved_Checks = Saved_Checks'Last | |
70482933 | 6458 | then |
11b4899f | 6459 | Activate_Range_Check (N); |
fbf5a39b AC |
6460 | |
6461 | if Debug_Flag_CC then | |
6462 | w ("Optimization off"); | |
6463 | end if; | |
6464 | ||
70482933 | 6465 | return; |
fbf5a39b | 6466 | end if; |
70482933 | 6467 | |
fbf5a39b | 6468 | -- Otherwise find out the target type |
70482933 | 6469 | |
fbf5a39b | 6470 | P := Parent (N); |
70482933 | 6471 | |
fbf5a39b AC |
6472 | -- For assignment, use left side subtype |
6473 | ||
6474 | if Nkind (P) = N_Assignment_Statement | |
6475 | and then Expression (P) = N | |
6476 | then | |
6477 | Ttyp := Etype (Name (P)); | |
6478 | ||
6479 | -- For indexed component, use subscript subtype | |
6480 | ||
6481 | elsif Nkind (P) = N_Indexed_Component then | |
6482 | declare | |
6483 | Atyp : Entity_Id; | |
6484 | Indx : Node_Id; | |
6485 | Subs : Node_Id; | |
6486 | ||
6487 | begin | |
6488 | Atyp := Etype (Prefix (P)); | |
6489 | ||
6490 | if Is_Access_Type (Atyp) then | |
6491 | Atyp := Designated_Type (Atyp); | |
d935a36e AC |
6492 | |
6493 | -- If the prefix is an access to an unconstrained array, | |
675d6070 TQ |
6494 | -- perform check unconditionally: it depends on the bounds of |
6495 | -- an object and we cannot currently recognize whether the test | |
6496 | -- may be redundant. | |
d935a36e AC |
6497 | |
6498 | if not Is_Constrained (Atyp) then | |
11b4899f | 6499 | Activate_Range_Check (N); |
d935a36e AC |
6500 | return; |
6501 | end if; | |
82c80734 | 6502 | |
ef2c20e7 AC |
6503 | -- Ditto if prefix is simply an unconstrained array. We used |
6504 | -- to think this case was OK, if the prefix was not an explicit | |
6505 | -- dereference, but we have now seen a case where this is not | |
6506 | -- true, so it is safer to just suppress the optimization in this | |
6507 | -- case. The back end is getting better at eliminating redundant | |
6508 | -- checks in any case, so the loss won't be important. | |
82c80734 | 6509 | |
ef2c20e7 | 6510 | elsif Is_Array_Type (Atyp) |
82c80734 RD |
6511 | and then not Is_Constrained (Atyp) |
6512 | then | |
11b4899f | 6513 | Activate_Range_Check (N); |
82c80734 | 6514 | return; |
fbf5a39b AC |
6515 | end if; |
6516 | ||
6517 | Indx := First_Index (Atyp); | |
6518 | Subs := First (Expressions (P)); | |
6519 | loop | |
6520 | if Subs = N then | |
6521 | Ttyp := Etype (Indx); | |
6522 | exit; | |
6523 | end if; | |
6524 | ||
6525 | Next_Index (Indx); | |
6526 | Next (Subs); | |
6527 | end loop; | |
6528 | end; | |
6529 | ||
6530 | -- For now, ignore all other cases, they are not so interesting | |
6531 | ||
6532 | else | |
6533 | if Debug_Flag_CC then | |
6534 | w (" target type not found, flag set"); | |
6535 | end if; | |
6536 | ||
11b4899f | 6537 | Activate_Range_Check (N); |
fbf5a39b AC |
6538 | return; |
6539 | end if; | |
6540 | ||
6541 | -- Evaluate and check the expression | |
6542 | ||
6543 | Find_Check | |
6544 | (Expr => N, | |
6545 | Check_Type => 'R', | |
6546 | Target_Type => Ttyp, | |
6547 | Entry_OK => OK, | |
6548 | Check_Num => Chk, | |
6549 | Ent => Ent, | |
6550 | Ofs => Ofs); | |
6551 | ||
6552 | if Debug_Flag_CC then | |
6553 | w ("Called Find_Check"); | |
6554 | w ("Target_Typ = ", Int (Ttyp)); | |
6555 | w (" OK = ", OK); | |
6556 | ||
6557 | if OK then | |
6558 | w (" Check_Num = ", Chk); | |
6559 | w (" Ent = ", Int (Ent)); | |
6560 | Write_Str (" Ofs = "); | |
6561 | pid (Ofs); | |
6562 | end if; | |
6563 | end if; | |
6564 | ||
6565 | -- If check is not of form to optimize, then set flag and we are done | |
6566 | ||
6567 | if not OK then | |
6568 | if Debug_Flag_CC then | |
6569 | w (" expression not of optimizable type, flag set"); | |
6570 | end if; | |
6571 | ||
11b4899f | 6572 | Activate_Range_Check (N); |
fbf5a39b AC |
6573 | return; |
6574 | end if; | |
6575 | ||
6576 | -- If check is already performed, then return without setting flag | |
6577 | ||
6578 | if Chk /= 0 then | |
6579 | if Debug_Flag_CC then | |
6580 | w ("Check suppressed!"); | |
6581 | end if; | |
6582 | ||
6583 | return; | |
6584 | end if; | |
6585 | ||
6586 | -- Here we will make a new entry for the new check | |
6587 | ||
11b4899f | 6588 | Activate_Range_Check (N); |
fbf5a39b AC |
6589 | Num_Saved_Checks := Num_Saved_Checks + 1; |
6590 | Saved_Checks (Num_Saved_Checks) := | |
6591 | (Killed => False, | |
6592 | Entity => Ent, | |
6593 | Offset => Ofs, | |
6594 | Check_Type => 'R', | |
6595 | Target_Type => Ttyp); | |
6596 | ||
6597 | if Debug_Flag_CC then | |
6598 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
6599 | w (" Entity = ", Int (Ent)); | |
6600 | Write_Str (" Offset = "); | |
6601 | pid (Ofs); | |
6602 | w (" Check_Type = R"); | |
6603 | w (" Target_Type = ", Int (Ttyp)); | |
11b4899f | 6604 | pg (Union_Id (Ttyp)); |
fbf5a39b AC |
6605 | end if; |
6606 | ||
675d6070 TQ |
6607 | -- If we get an exception, then something went wrong, probably because of |
6608 | -- an error in the structure of the tree due to an incorrect program. Or | |
6609 | -- it may be a bug in the optimization circuit. In either case the safest | |
6610 | -- thing is simply to set the check flag unconditionally. | |
fbf5a39b AC |
6611 | |
6612 | exception | |
6613 | when others => | |
11b4899f | 6614 | Activate_Range_Check (N); |
fbf5a39b AC |
6615 | |
6616 | if Debug_Flag_CC then | |
6617 | w (" exception occurred, range flag set"); | |
6618 | end if; | |
6619 | ||
6620 | return; | |
6621 | end Enable_Range_Check; | |
6622 | ||
6623 | ------------------ | |
6624 | -- Ensure_Valid -- | |
6625 | ------------------ | |
6626 | ||
2934b84a AC |
6627 | procedure Ensure_Valid |
6628 | (Expr : Node_Id; | |
6629 | Holes_OK : Boolean := False; | |
6630 | Related_Id : Entity_Id := Empty; | |
6631 | Is_Low_Bound : Boolean := False; | |
6632 | Is_High_Bound : Boolean := False) | |
6633 | is | |
fbf5a39b AC |
6634 | Typ : constant Entity_Id := Etype (Expr); |
6635 | ||
6636 | begin | |
6637 | -- Ignore call if we are not doing any validity checking | |
6638 | ||
6639 | if not Validity_Checks_On then | |
6640 | return; | |
6641 | ||
c064e066 | 6642 | -- Ignore call if range or validity checks suppressed on entity or type |
fbf5a39b | 6643 | |
c064e066 | 6644 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
fbf5a39b AC |
6645 | return; |
6646 | ||
675d6070 TQ |
6647 | -- No check required if expression is from the expander, we assume the |
6648 | -- expander will generate whatever checks are needed. Note that this is | |
a90bd866 | 6649 | -- not just an optimization, it avoids infinite recursions. |
fbf5a39b AC |
6650 | |
6651 | -- Unchecked conversions must be checked, unless they are initialized | |
6652 | -- scalar values, as in a component assignment in an init proc. | |
6653 | ||
6654 | -- In addition, we force a check if Force_Validity_Checks is set | |
6655 | ||
6656 | elsif not Comes_From_Source (Expr) | |
2e60feb5 PMR |
6657 | and then not |
6658 | (Nkind (Expr) = N_Identifier | |
19e7eae5 BD |
6659 | and then Present (Renamed_Entity_Or_Object (Entity (Expr))) |
6660 | and then | |
6661 | Comes_From_Source (Renamed_Entity_Or_Object (Entity (Expr)))) | |
fbf5a39b AC |
6662 | and then not Force_Validity_Checks |
6663 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
6664 | or else Kill_Range_Check (Expr)) | |
6665 | then | |
6666 | return; | |
6667 | ||
6668 | -- No check required if expression is known to have valid value | |
6669 | ||
6670 | elsif Expr_Known_Valid (Expr) then | |
6671 | return; | |
6672 | ||
229fa5db AC |
6673 | -- No check needed within a generated predicate function. Validity |
6674 | -- of input value will have been checked earlier. | |
6675 | ||
6676 | elsif Ekind (Current_Scope) = E_Function | |
6677 | and then Is_Predicate_Function (Current_Scope) | |
6678 | then | |
6679 | return; | |
6680 | ||
675d6070 TQ |
6681 | -- Ignore case of enumeration with holes where the flag is set not to |
6682 | -- worry about holes, since no special validity check is needed | |
fbf5a39b AC |
6683 | |
6684 | elsif Is_Enumeration_Type (Typ) | |
6685 | and then Has_Non_Standard_Rep (Typ) | |
6686 | and then Holes_OK | |
6687 | then | |
6688 | return; | |
6689 | ||
ddda9d0f | 6690 | -- No check required on the left-hand side of an assignment |
fbf5a39b AC |
6691 | |
6692 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
6693 | and then Expr = Name (Parent (Expr)) | |
6694 | then | |
6695 | return; | |
6696 | ||
308e6f3a | 6697 | -- No check on a universal real constant. The context will eventually |
f02b8bb8 RD |
6698 | -- convert it to a machine number for some target type, or report an |
6699 | -- illegality. | |
6700 | ||
6701 | elsif Nkind (Expr) = N_Real_Literal | |
6702 | and then Etype (Expr) = Universal_Real | |
6703 | then | |
6704 | return; | |
6705 | ||
308e6f3a | 6706 | -- If the expression denotes a component of a packed boolean array, |
c064e066 RD |
6707 | -- no possible check applies. We ignore the old ACATS chestnuts that |
6708 | -- involve Boolean range True..True. | |
6709 | ||
6710 | -- Note: validity checks are generated for expressions that yield a | |
6711 | -- scalar type, when it is possible to create a value that is outside of | |
6712 | -- the type. If this is a one-bit boolean no such value exists. This is | |
6713 | -- an optimization, and it also prevents compiler blowing up during the | |
6714 | -- elaboration of improperly expanded packed array references. | |
6715 | ||
6716 | elsif Nkind (Expr) = N_Indexed_Component | |
6717 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
6718 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
6719 | then | |
6720 | return; | |
6721 | ||
064f4527 TQ |
6722 | -- For an expression with actions, we want to insert the validity check |
6723 | -- on the final Expression. | |
6724 | ||
6725 | elsif Nkind (Expr) = N_Expression_With_Actions then | |
6726 | Ensure_Valid (Expression (Expr)); | |
6727 | return; | |
6728 | ||
fbf5a39b AC |
6729 | -- An annoying special case. If this is an out parameter of a scalar |
6730 | -- type, then the value is not going to be accessed, therefore it is | |
155f4f34 HK |
6731 | -- inappropriate to do any validity check at the call site. Likewise |
6732 | -- if the parameter is passed by reference. | |
fbf5a39b AC |
6733 | |
6734 | else | |
6735 | -- Only need to worry about scalar types | |
6736 | ||
6737 | if Is_Scalar_Type (Typ) then | |
70482933 RK |
6738 | declare |
6739 | P : Node_Id; | |
6740 | N : Node_Id; | |
6741 | E : Entity_Id; | |
6742 | F : Entity_Id; | |
6743 | A : Node_Id; | |
6744 | L : List_Id; | |
6745 | ||
6746 | begin | |
6747 | -- Find actual argument (which may be a parameter association) | |
6748 | -- and the parent of the actual argument (the call statement) | |
6749 | ||
6750 | N := Expr; | |
6751 | P := Parent (Expr); | |
6752 | ||
6753 | if Nkind (P) = N_Parameter_Association then | |
6754 | N := P; | |
6755 | P := Parent (N); | |
6756 | end if; | |
6757 | ||
155f4f34 HK |
6758 | -- If this is an indirect or dispatching call, get signature |
6759 | -- from the subprogram type. | |
70482933 | 6760 | |
4a08c95c AC |
6761 | if Nkind (P) in N_Entry_Call_Statement |
6762 | | N_Function_Call | |
6763 | | N_Procedure_Call_Statement | |
155f4f34 HK |
6764 | then |
6765 | E := Get_Called_Entity (P); | |
70482933 | 6766 | L := Parameter_Associations (P); |
fbf5a39b | 6767 | |
675d6070 | 6768 | -- Only need to worry if there are indeed actuals, and if |
155f4f34 HK |
6769 | -- this could be a subprogram call, otherwise we cannot get |
6770 | -- a match (either we are not an argument, or the mode of | |
6771 | -- the formal is not OUT). This test also filters out the | |
675d6070 | 6772 | -- generic case. |
70482933 | 6773 | |
637a41a5 AC |
6774 | if Is_Non_Empty_List (L) and then Is_Subprogram (E) then |
6775 | ||
675d6070 TQ |
6776 | -- This is the loop through parameters, looking for an |
6777 | -- OUT parameter for which we are the argument. | |
70482933 RK |
6778 | |
6779 | F := First_Formal (E); | |
6780 | A := First (L); | |
70482933 | 6781 | while Present (F) loop |
155f4f34 HK |
6782 | if A = N |
6783 | and then (Ekind (F) = E_Out_Parameter | |
6784 | or else Mechanism (F) = By_Reference) | |
6785 | then | |
70482933 RK |
6786 | return; |
6787 | end if; | |
6788 | ||
6789 | Next_Formal (F); | |
6790 | Next (A); | |
6791 | end loop; | |
6792 | end if; | |
6793 | end if; | |
6794 | end; | |
6795 | end if; | |
6796 | end if; | |
6797 | ||
1c218ac3 | 6798 | -- If this is a boolean expression, only its elementary operands need |
46f52a47 AC |
6799 | -- checking: if they are valid, a boolean or short-circuit operation |
6800 | -- with them will be valid as well. | |
38afef28 AC |
6801 | |
6802 | if Base_Type (Typ) = Standard_Boolean | |
96d2756f | 6803 | and then |
1c218ac3 | 6804 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
38afef28 AC |
6805 | then |
6806 | return; | |
6807 | end if; | |
6808 | ||
c064e066 | 6809 | -- If we fall through, a validity check is required |
70482933 | 6810 | |
2934b84a | 6811 | Insert_Valid_Check (Expr, Related_Id, Is_Low_Bound, Is_High_Bound); |
1c3340e6 RD |
6812 | |
6813 | if Is_Entity_Name (Expr) | |
6814 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
6815 | then | |
6816 | Set_Is_Known_Valid (Entity (Expr)); | |
6817 | end if; | |
70482933 RK |
6818 | end Ensure_Valid; |
6819 | ||
6820 | ---------------------- | |
6821 | -- Expr_Known_Valid -- | |
6822 | ---------------------- | |
6823 | ||
6824 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
6825 | Typ : constant Entity_Id := Etype (Expr); | |
6826 | ||
6827 | begin | |
675d6070 TQ |
6828 | -- Non-scalar types are always considered valid, since they never give |
6829 | -- rise to the issues of erroneous or bounded error behavior that are | |
6830 | -- the concern. In formal reference manual terms the notion of validity | |
6831 | -- only applies to scalar types. Note that even when packed arrays are | |
6832 | -- represented using modular types, they are still arrays semantically, | |
6833 | -- so they are also always valid (in particular, the unused bits can be | |
6834 | -- random rubbish without affecting the validity of the array value). | |
70482933 | 6835 | |
8ca597af | 6836 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Impl_Type (Typ) then |
70482933 RK |
6837 | return True; |
6838 | ||
6839 | -- If no validity checking, then everything is considered valid | |
6840 | ||
6841 | elsif not Validity_Checks_On then | |
6842 | return True; | |
6843 | ||
6844 | -- Floating-point types are considered valid unless floating-point | |
6845 | -- validity checks have been specifically turned on. | |
6846 | ||
6847 | elsif Is_Floating_Point_Type (Typ) | |
6848 | and then not Validity_Check_Floating_Point | |
6849 | then | |
6850 | return True; | |
6851 | ||
675d6070 TQ |
6852 | -- If the expression is the value of an object that is known to be |
6853 | -- valid, then clearly the expression value itself is valid. | |
70482933 RK |
6854 | |
6855 | elsif Is_Entity_Name (Expr) | |
6856 | and then Is_Known_Valid (Entity (Expr)) | |
fba9ebfc AC |
6857 | |
6858 | -- Exclude volatile variables | |
6859 | ||
6860 | and then not Treat_As_Volatile (Entity (Expr)) | |
70482933 RK |
6861 | then |
6862 | return True; | |
6863 | ||
c064e066 RD |
6864 | -- References to discriminants are always considered valid. The value |
6865 | -- of a discriminant gets checked when the object is built. Within the | |
6866 | -- record, we consider it valid, and it is important to do so, since | |
6867 | -- otherwise we can try to generate bogus validity checks which | |
675d6070 TQ |
6868 | -- reference discriminants out of scope. Discriminants of concurrent |
6869 | -- types are excluded for the same reason. | |
c064e066 RD |
6870 | |
6871 | elsif Is_Entity_Name (Expr) | |
675d6070 | 6872 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
c064e066 RD |
6873 | then |
6874 | return True; | |
6875 | ||
675d6070 TQ |
6876 | -- If the type is one for which all values are known valid, then we are |
6877 | -- sure that the value is valid except in the slightly odd case where | |
6878 | -- the expression is a reference to a variable whose size has been | |
6879 | -- explicitly set to a value greater than the object size. | |
70482933 RK |
6880 | |
6881 | elsif Is_Known_Valid (Typ) then | |
6882 | if Is_Entity_Name (Expr) | |
6883 | and then Ekind (Entity (Expr)) = E_Variable | |
b23cdc01 | 6884 | and then Known_Esize (Entity (Expr)) |
70482933 RK |
6885 | and then Esize (Entity (Expr)) > Esize (Typ) |
6886 | then | |
6887 | return False; | |
6888 | else | |
6889 | return True; | |
6890 | end if; | |
6891 | ||
6892 | -- Integer and character literals always have valid values, where | |
6893 | -- appropriate these will be range checked in any case. | |
6894 | ||
4a08c95c | 6895 | elsif Nkind (Expr) in N_Integer_Literal | N_Character_Literal then |
70482933 | 6896 | return True; |
cf427f02 | 6897 | |
70482933 RK |
6898 | -- If we have a type conversion or a qualification of a known valid |
6899 | -- value, then the result will always be valid. | |
6900 | ||
4a08c95c | 6901 | elsif Nkind (Expr) in N_Type_Conversion | N_Qualified_Expression then |
70482933 RK |
6902 | return Expr_Known_Valid (Expression (Expr)); |
6903 | ||
162c21d9 AC |
6904 | -- Case of expression is a non-floating-point operator. In this case we |
6905 | -- can assume the result is valid the generated code for the operator | |
6906 | -- will include whatever checks are needed (e.g. range checks) to ensure | |
6907 | -- validity. This assumption does not hold for the floating-point case, | |
6908 | -- since floating-point operators can generate Infinite or NaN results | |
6909 | -- which are considered invalid. | |
6910 | ||
6911 | -- Historical note: in older versions, the exemption of floating-point | |
6912 | -- types from this assumption was done only in cases where the parent | |
6913 | -- was an assignment, function call or parameter association. Presumably | |
6914 | -- the idea was that in other contexts, the result would be checked | |
6915 | -- elsewhere, but this list of cases was missing tests (at least the | |
6916 | -- N_Object_Declaration case, as shown by a reported missing validity | |
6917 | -- check), and it is not clear why function calls but not procedure | |
6918 | -- calls were tested for. It really seems more accurate and much | |
6919 | -- safer to recognize that expressions which are the result of a | |
6920 | -- floating-point operator can never be assumed to be valid. | |
6921 | ||
6922 | elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then | |
6923 | return True; | |
28e4d64e | 6924 | |
675d6070 TQ |
6925 | -- The result of a membership test is always valid, since it is true or |
6926 | -- false, there are no other possibilities. | |
c064e066 RD |
6927 | |
6928 | elsif Nkind (Expr) in N_Membership_Test then | |
6929 | return True; | |
6930 | ||
70482933 RK |
6931 | -- For all other cases, we do not know the expression is valid |
6932 | ||
6933 | else | |
6934 | return False; | |
6935 | end if; | |
6936 | end Expr_Known_Valid; | |
6937 | ||
fbf5a39b AC |
6938 | ---------------- |
6939 | -- Find_Check -- | |
6940 | ---------------- | |
6941 | ||
6942 | procedure Find_Check | |
6943 | (Expr : Node_Id; | |
6944 | Check_Type : Character; | |
6945 | Target_Type : Entity_Id; | |
6946 | Entry_OK : out Boolean; | |
6947 | Check_Num : out Nat; | |
6948 | Ent : out Entity_Id; | |
6949 | Ofs : out Uint) | |
6950 | is | |
6951 | function Within_Range_Of | |
6952 | (Target_Type : Entity_Id; | |
6b6fcd3e | 6953 | Check_Type : Entity_Id) return Boolean; |
fbf5a39b AC |
6954 | -- Given a requirement for checking a range against Target_Type, and |
6955 | -- and a range Check_Type against which a check has already been made, | |
6956 | -- determines if the check against check type is sufficient to ensure | |
6957 | -- that no check against Target_Type is required. | |
6958 | ||
6959 | --------------------- | |
6960 | -- Within_Range_Of -- | |
6961 | --------------------- | |
6962 | ||
6963 | function Within_Range_Of | |
6964 | (Target_Type : Entity_Id; | |
6b6fcd3e | 6965 | Check_Type : Entity_Id) return Boolean |
fbf5a39b AC |
6966 | is |
6967 | begin | |
6968 | if Target_Type = Check_Type then | |
6969 | return True; | |
6970 | ||
6971 | else | |
6972 | declare | |
6973 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
6974 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
6975 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
6976 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
6977 | ||
6978 | begin | |
6979 | if (Tlo = Clo | |
6980 | or else (Compile_Time_Known_Value (Tlo) | |
6981 | and then | |
6982 | Compile_Time_Known_Value (Clo) | |
6983 | and then | |
6984 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
6985 | and then | |
6986 | (Thi = Chi | |
6987 | or else (Compile_Time_Known_Value (Thi) | |
6988 | and then | |
6989 | Compile_Time_Known_Value (Chi) | |
6990 | and then | |
6991 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
6992 | then | |
6993 | return True; | |
6994 | else | |
6995 | return False; | |
6996 | end if; | |
6997 | end; | |
6998 | end if; | |
6999 | end Within_Range_Of; | |
7000 | ||
7001 | -- Start of processing for Find_Check | |
7002 | ||
7003 | begin | |
75ba322d | 7004 | -- Establish default, in case no entry is found |
fbf5a39b AC |
7005 | |
7006 | Check_Num := 0; | |
7007 | ||
7008 | -- Case of expression is simple entity reference | |
7009 | ||
7010 | if Is_Entity_Name (Expr) then | |
7011 | Ent := Entity (Expr); | |
7012 | Ofs := Uint_0; | |
7013 | ||
7014 | -- Case of expression is entity + known constant | |
7015 | ||
7016 | elsif Nkind (Expr) = N_Op_Add | |
7017 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
7018 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
7019 | then | |
7020 | Ent := Entity (Left_Opnd (Expr)); | |
7021 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
7022 | ||
7023 | -- Case of expression is entity - known constant | |
7024 | ||
7025 | elsif Nkind (Expr) = N_Op_Subtract | |
7026 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
7027 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
7028 | then | |
7029 | Ent := Entity (Left_Opnd (Expr)); | |
7030 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
7031 | ||
7032 | -- Any other expression is not of the right form | |
7033 | ||
7034 | else | |
7035 | Ent := Empty; | |
7036 | Ofs := Uint_0; | |
7037 | Entry_OK := False; | |
7038 | return; | |
7039 | end if; | |
7040 | ||
675d6070 TQ |
7041 | -- Come here with expression of appropriate form, check if entity is an |
7042 | -- appropriate one for our purposes. | |
fbf5a39b AC |
7043 | |
7044 | if (Ekind (Ent) = E_Variable | |
f2cbd970 | 7045 | or else Is_Constant_Object (Ent)) |
fbf5a39b AC |
7046 | and then not Is_Library_Level_Entity (Ent) |
7047 | then | |
7048 | Entry_OK := True; | |
7049 | else | |
7050 | Entry_OK := False; | |
7051 | return; | |
7052 | end if; | |
7053 | ||
7054 | -- See if there is matching check already | |
7055 | ||
7056 | for J in reverse 1 .. Num_Saved_Checks loop | |
7057 | declare | |
7058 | SC : Saved_Check renames Saved_Checks (J); | |
fbf5a39b AC |
7059 | begin |
7060 | if SC.Killed = False | |
7061 | and then SC.Entity = Ent | |
7062 | and then SC.Offset = Ofs | |
7063 | and then SC.Check_Type = Check_Type | |
7064 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
7065 | then | |
7066 | Check_Num := J; | |
7067 | return; | |
7068 | end if; | |
7069 | end; | |
7070 | end loop; | |
7071 | ||
7072 | -- If we fall through entry was not found | |
7073 | ||
fbf5a39b AC |
7074 | return; |
7075 | end Find_Check; | |
7076 | ||
7077 | --------------------------------- | |
7078 | -- Generate_Discriminant_Check -- | |
7079 | --------------------------------- | |
7080 | ||
fbf5a39b AC |
7081 | procedure Generate_Discriminant_Check (N : Node_Id) is |
7082 | Loc : constant Source_Ptr := Sloc (N); | |
7083 | Pref : constant Node_Id := Prefix (N); | |
7084 | Sel : constant Node_Id := Selector_Name (N); | |
7085 | ||
7086 | Orig_Comp : constant Entity_Id := | |
15f0f591 | 7087 | Original_Record_Component (Entity (Sel)); |
fbf5a39b AC |
7088 | -- The original component to be checked |
7089 | ||
7090 | Discr_Fct : constant Entity_Id := | |
15f0f591 | 7091 | Discriminant_Checking_Func (Orig_Comp); |
fbf5a39b AC |
7092 | -- The discriminant checking function |
7093 | ||
7094 | Discr : Entity_Id; | |
7095 | -- One discriminant to be checked in the type | |
7096 | ||
7097 | Real_Discr : Entity_Id; | |
7098 | -- Actual discriminant in the call | |
7099 | ||
7100 | Pref_Type : Entity_Id; | |
7101 | -- Type of relevant prefix (ignoring private/access stuff) | |
7102 | ||
7103 | Args : List_Id; | |
7104 | -- List of arguments for function call | |
7105 | ||
7106 | Formal : Entity_Id; | |
675d6070 TQ |
7107 | -- Keep track of the formal corresponding to the actual we build for |
7108 | -- each discriminant, in order to be able to perform the necessary type | |
7109 | -- conversions. | |
fbf5a39b AC |
7110 | |
7111 | Scomp : Node_Id; | |
7112 | -- Selected component reference for checking function argument | |
7113 | ||
7114 | begin | |
7115 | Pref_Type := Etype (Pref); | |
7116 | ||
7117 | -- Force evaluation of the prefix, so that it does not get evaluated | |
7118 | -- twice (once for the check, once for the actual reference). Such a | |
637a41a5 AC |
7119 | -- double evaluation is always a potential source of inefficiency, and |
7120 | -- is functionally incorrect in the volatile case, or when the prefix | |
2cc2e964 AC |
7121 | -- may have side effects. A nonvolatile entity or a component of a |
7122 | -- nonvolatile entity requires no evaluation. | |
fbf5a39b AC |
7123 | |
7124 | if Is_Entity_Name (Pref) then | |
7125 | if Treat_As_Volatile (Entity (Pref)) then | |
7126 | Force_Evaluation (Pref, Name_Req => True); | |
7127 | end if; | |
7128 | ||
7129 | elsif Treat_As_Volatile (Etype (Pref)) then | |
637a41a5 | 7130 | Force_Evaluation (Pref, Name_Req => True); |
fbf5a39b AC |
7131 | |
7132 | elsif Nkind (Pref) = N_Selected_Component | |
7133 | and then Is_Entity_Name (Prefix (Pref)) | |
7134 | then | |
7135 | null; | |
7136 | ||
7137 | else | |
7138 | Force_Evaluation (Pref, Name_Req => True); | |
7139 | end if; | |
7140 | ||
7141 | -- For a tagged type, use the scope of the original component to | |
7142 | -- obtain the type, because ??? | |
7143 | ||
7144 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
7145 | Pref_Type := Scope (Orig_Comp); | |
7146 | ||
675d6070 TQ |
7147 | -- For an untagged derived type, use the discriminants of the parent |
7148 | -- which have been renamed in the derivation, possibly by a one-to-many | |
1fb63e89 | 7149 | -- discriminant constraint. For untagged type, initially get the Etype |
675d6070 | 7150 | -- of the prefix |
fbf5a39b AC |
7151 | |
7152 | else | |
7153 | if Is_Derived_Type (Pref_Type) | |
7154 | and then Number_Discriminants (Pref_Type) /= | |
7155 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
7156 | then | |
7157 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
7158 | end if; | |
7159 | end if; | |
7160 | ||
7161 | -- We definitely should have a checking function, This routine should | |
7162 | -- not be called if no discriminant checking function is present. | |
7163 | ||
7164 | pragma Assert (Present (Discr_Fct)); | |
7165 | ||
7166 | -- Create the list of the actual parameters for the call. This list | |
7167 | -- is the list of the discriminant fields of the record expression to | |
7168 | -- be discriminant checked. | |
7169 | ||
7170 | Args := New_List; | |
7171 | Formal := First_Formal (Discr_Fct); | |
7172 | Discr := First_Discriminant (Pref_Type); | |
7173 | while Present (Discr) loop | |
7174 | ||
7175 | -- If we have a corresponding discriminant field, and a parent | |
7176 | -- subtype is present, then we want to use the corresponding | |
7177 | -- discriminant since this is the one with the useful value. | |
7178 | ||
7179 | if Present (Corresponding_Discriminant (Discr)) | |
7180 | and then Ekind (Pref_Type) = E_Record_Type | |
7181 | and then Present (Parent_Subtype (Pref_Type)) | |
7182 | then | |
7183 | Real_Discr := Corresponding_Discriminant (Discr); | |
7184 | else | |
7185 | Real_Discr := Discr; | |
7186 | end if; | |
7187 | ||
7188 | -- Construct the reference to the discriminant | |
7189 | ||
7190 | Scomp := | |
7191 | Make_Selected_Component (Loc, | |
7192 | Prefix => | |
7193 | Unchecked_Convert_To (Pref_Type, | |
7194 | Duplicate_Subexpr (Pref)), | |
7195 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
7196 | ||
7197 | -- Manually analyze and resolve this selected component. We really | |
7198 | -- want it just as it appears above, and do not want the expander | |
675d6070 TQ |
7199 | -- playing discriminal games etc with this reference. Then we append |
7200 | -- the argument to the list we are gathering. | |
fbf5a39b AC |
7201 | |
7202 | Set_Etype (Scomp, Etype (Real_Discr)); | |
7203 | Set_Analyzed (Scomp, True); | |
7204 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
7205 | ||
7206 | Next_Formal_With_Extras (Formal); | |
7207 | Next_Discriminant (Discr); | |
7208 | end loop; | |
7209 | ||
7210 | -- Now build and insert the call | |
7211 | ||
7212 | Insert_Action (N, | |
7213 | Make_Raise_Constraint_Error (Loc, | |
7214 | Condition => | |
7215 | Make_Function_Call (Loc, | |
637a41a5 | 7216 | Name => New_Occurrence_Of (Discr_Fct, Loc), |
fbf5a39b AC |
7217 | Parameter_Associations => Args), |
7218 | Reason => CE_Discriminant_Check_Failed)); | |
7219 | end Generate_Discriminant_Check; | |
7220 | ||
15ce9ca2 AC |
7221 | --------------------------- |
7222 | -- Generate_Index_Checks -- | |
7223 | --------------------------- | |
fbf5a39b | 7224 | |
e02c8dff SB |
7225 | procedure Generate_Index_Checks |
7226 | (N : Node_Id; | |
7227 | Checks_Generated : out Dimension_Set) | |
7228 | is | |
4230bdb7 AC |
7229 | |
7230 | function Entity_Of_Prefix return Entity_Id; | |
7231 | -- Returns the entity of the prefix of N (or Empty if not found) | |
7232 | ||
8ed68165 AC |
7233 | ---------------------- |
7234 | -- Entity_Of_Prefix -- | |
7235 | ---------------------- | |
7236 | ||
4230bdb7 | 7237 | function Entity_Of_Prefix return Entity_Id is |
0d53d36b AC |
7238 | P : Node_Id; |
7239 | ||
4230bdb7 | 7240 | begin |
0d53d36b | 7241 | P := Prefix (N); |
4230bdb7 | 7242 | while not Is_Entity_Name (P) loop |
4a08c95c | 7243 | if Nkind (P) not in N_Selected_Component | N_Indexed_Component then |
4230bdb7 AC |
7244 | return Empty; |
7245 | end if; | |
7246 | ||
7247 | P := Prefix (P); | |
7248 | end loop; | |
7249 | ||
7250 | return Entity (P); | |
7251 | end Entity_Of_Prefix; | |
7252 | ||
7253 | -- Local variables | |
7254 | ||
7255 | Loc : constant Source_Ptr := Sloc (N); | |
7256 | A : constant Node_Id := Prefix (N); | |
7257 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
7258 | Sub : Node_Id; | |
fbf5a39b | 7259 | |
8ed68165 AC |
7260 | -- Start of processing for Generate_Index_Checks |
7261 | ||
fbf5a39b | 7262 | begin |
e02c8dff SB |
7263 | Checks_Generated.Elements := (others => False); |
7264 | ||
4230bdb7 AC |
7265 | -- Ignore call if the prefix is not an array since we have a serious |
7266 | -- error in the sources. Ignore it also if index checks are suppressed | |
7267 | -- for array object or type. | |
c064e066 | 7268 | |
4230bdb7 | 7269 | if not Is_Array_Type (Etype (A)) |
637a41a5 | 7270 | or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
c064e066 RD |
7271 | or else Index_Checks_Suppressed (Etype (A)) |
7272 | then | |
7273 | return; | |
3a3af4c3 AC |
7274 | |
7275 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
7276 | -- prefix. This case arises when analysis has determined that constructs | |
7277 | -- such as | |
7278 | ||
7279 | -- Prefix'Loop_Entry (Expr) | |
7280 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
7281 | ||
7282 | -- require rewriting for error detection purposes. A side effect of this | |
7283 | -- action is the generation of index checks that mention 'Loop_Entry. | |
7284 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
7285 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
7286 | ||
7287 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
7288 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
7289 | then | |
7290 | return; | |
c064e066 RD |
7291 | end if; |
7292 | ||
4230bdb7 AC |
7293 | -- Generate a raise of constraint error with the appropriate reason and |
7294 | -- a condition of the form: | |
7295 | ||
8ed68165 | 7296 | -- Base_Type (Sub) not in Array'Range (Subscript) |
4230bdb7 AC |
7297 | |
7298 | -- Note that the reason we generate the conversion to the base type here | |
7299 | -- is that we definitely want the range check to take place, even if it | |
7300 | -- looks like the subtype is OK. Optimization considerations that allow | |
7301 | -- us to omit the check have already been taken into account in the | |
7302 | -- setting of the Do_Range_Check flag earlier on. | |
c064e066 | 7303 | |
fbf5a39b | 7304 | Sub := First (Expressions (N)); |
4230bdb7 AC |
7305 | |
7306 | -- Handle string literals | |
7307 | ||
7308 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
fbf5a39b AC |
7309 | if Do_Range_Check (Sub) then |
7310 | Set_Do_Range_Check (Sub, False); | |
7311 | ||
4230bdb7 AC |
7312 | -- For string literals we obtain the bounds of the string from the |
7313 | -- associated subtype. | |
fbf5a39b | 7314 | |
4230bdb7 | 7315 | Insert_Action (N, |
d7a44b14 AC |
7316 | Make_Raise_Constraint_Error (Loc, |
7317 | Condition => | |
7318 | Make_Not_In (Loc, | |
7319 | Left_Opnd => | |
7320 | Convert_To (Base_Type (Etype (Sub)), | |
7321 | Duplicate_Subexpr_Move_Checks (Sub)), | |
7322 | Right_Opnd => | |
7323 | Make_Attribute_Reference (Loc, | |
e4494292 | 7324 | Prefix => New_Occurrence_Of (Etype (A), Loc), |
d7a44b14 AC |
7325 | Attribute_Name => Name_Range)), |
7326 | Reason => CE_Index_Check_Failed)); | |
e02c8dff SB |
7327 | |
7328 | Checks_Generated.Elements (1) := True; | |
4230bdb7 | 7329 | end if; |
fbf5a39b | 7330 | |
4230bdb7 | 7331 | -- General case |
fbf5a39b | 7332 | |
4230bdb7 AC |
7333 | else |
7334 | declare | |
c94bbfbe | 7335 | A_Idx : Node_Id; |
4230bdb7 | 7336 | A_Range : Node_Id; |
c94bbfbe | 7337 | Ind : Pos; |
4230bdb7 AC |
7338 | Num : List_Id; |
7339 | Range_N : Node_Id; | |
fbf5a39b | 7340 | |
4230bdb7 AC |
7341 | begin |
7342 | A_Idx := First_Index (Etype (A)); | |
7343 | Ind := 1; | |
7344 | while Present (Sub) loop | |
7345 | if Do_Range_Check (Sub) then | |
7346 | Set_Do_Range_Check (Sub, False); | |
fbf5a39b | 7347 | |
4230bdb7 | 7348 | -- Force evaluation except for the case of a simple name of |
2cc2e964 | 7349 | -- a nonvolatile entity. |
fbf5a39b | 7350 | |
4230bdb7 AC |
7351 | if not Is_Entity_Name (Sub) |
7352 | or else Treat_As_Volatile (Entity (Sub)) | |
7353 | then | |
7354 | Force_Evaluation (Sub); | |
7355 | end if; | |
fbf5a39b | 7356 | |
4230bdb7 AC |
7357 | if Nkind (A_Idx) = N_Range then |
7358 | A_Range := A_Idx; | |
7359 | ||
4a08c95c | 7360 | elsif Nkind (A_Idx) in N_Identifier | N_Expanded_Name then |
4230bdb7 AC |
7361 | A_Range := Scalar_Range (Entity (A_Idx)); |
7362 | ||
a9fb2055 PT |
7363 | if Nkind (A_Range) = N_Subtype_Indication then |
7364 | A_Range := Range_Expression (Constraint (A_Range)); | |
7365 | end if; | |
7366 | ||
4230bdb7 AC |
7367 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); |
7368 | A_Range := Range_Expression (Constraint (A_Idx)); | |
7369 | end if; | |
7370 | ||
7371 | -- For array objects with constant bounds we can generate | |
7372 | -- the index check using the bounds of the type of the index | |
7373 | ||
7374 | if Present (A_Ent) | |
7375 | and then Ekind (A_Ent) = E_Variable | |
7376 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
7377 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
7378 | then | |
7379 | Range_N := | |
7380 | Make_Attribute_Reference (Loc, | |
8ed68165 | 7381 | Prefix => |
e4494292 | 7382 | New_Occurrence_Of (Etype (A_Idx), Loc), |
4230bdb7 AC |
7383 | Attribute_Name => Name_Range); |
7384 | ||
7385 | -- For arrays with non-constant bounds we cannot generate | |
7386 | -- the index check using the bounds of the type of the index | |
7387 | -- since it may reference discriminants of some enclosing | |
7388 | -- type. We obtain the bounds directly from the prefix | |
7389 | -- object. | |
7390 | ||
7391 | else | |
7392 | if Ind = 1 then | |
7393 | Num := No_List; | |
7394 | else | |
7395 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
7396 | end if; | |
7397 | ||
7398 | Range_N := | |
7399 | Make_Attribute_Reference (Loc, | |
7400 | Prefix => | |
7401 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
7402 | Attribute_Name => Name_Range, | |
7403 | Expressions => Num); | |
7404 | end if; | |
7405 | ||
7406 | Insert_Action (N, | |
d7a44b14 AC |
7407 | Make_Raise_Constraint_Error (Loc, |
7408 | Condition => | |
7409 | Make_Not_In (Loc, | |
7410 | Left_Opnd => | |
7411 | Convert_To (Base_Type (Etype (Sub)), | |
7412 | Duplicate_Subexpr_Move_Checks (Sub)), | |
7413 | Right_Opnd => Range_N), | |
7414 | Reason => CE_Index_Check_Failed)); | |
e02c8dff SB |
7415 | |
7416 | Checks_Generated.Elements (Ind) := True; | |
4230bdb7 AC |
7417 | end if; |
7418 | ||
99859ea7 | 7419 | Next_Index (A_Idx); |
4230bdb7 AC |
7420 | Ind := Ind + 1; |
7421 | Next (Sub); | |
7422 | end loop; | |
7423 | end; | |
7424 | end if; | |
fbf5a39b AC |
7425 | end Generate_Index_Checks; |
7426 | ||
7427 | -------------------------- | |
7428 | -- Generate_Range_Check -- | |
7429 | -------------------------- | |
7430 | ||
7431 | procedure Generate_Range_Check | |
7432 | (N : Node_Id; | |
7433 | Target_Type : Entity_Id; | |
7434 | Reason : RT_Exception_Code) | |
7435 | is | |
7436 | Loc : constant Source_Ptr := Sloc (N); | |
7437 | Source_Type : constant Entity_Id := Etype (N); | |
7438 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
7439 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
7440 | ||
67460d45 EB |
7441 | procedure Convert_And_Check_Range (Suppress : Check_Id); |
7442 | -- Convert N to the target base type and save the result in a temporary. | |
7443 | -- The action is analyzed using the default checks as modified by the | |
7444 | -- given Suppress argument. Then check the converted value against the | |
7445 | -- range of the target subtype. | |
f5655e4a | 7446 | |
cf9e3829 EB |
7447 | function Is_Single_Attribute_Reference (N : Node_Id) return Boolean; |
7448 | -- Return True if N is an expression that contains a single attribute | |
7449 | -- reference, possibly as operand among only integer literal operands. | |
7450 | ||
b6621d10 AC |
7451 | ----------------------------- |
7452 | -- Convert_And_Check_Range -- | |
7453 | ----------------------------- | |
f5655e4a | 7454 | |
67460d45 EB |
7455 | procedure Convert_And_Check_Range (Suppress : Check_Id) is |
7456 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); | |
7457 | Conv_N : Node_Id; | |
f5655e4a | 7458 | |
b6621d10 | 7459 | begin |
f20b5ef4 JM |
7460 | -- For enumeration types with non-standard representation this is a |
7461 | -- direct conversion from the enumeration type to the target integer | |
7462 | -- type, which is treated by the back end as a normal integer type | |
7463 | -- conversion, treating the enumeration type as an integer, which is | |
7464 | -- exactly what we want. We set Conversion_OK to make sure that the | |
7465 | -- analyzer does not complain about what otherwise might be an | |
7466 | -- illegal conversion. | |
7467 | ||
7468 | if Is_Enumeration_Type (Source_Base_Type) | |
7469 | and then Present (Enum_Pos_To_Rep (Source_Base_Type)) | |
7470 | and then Is_Integer_Type (Target_Base_Type) | |
7471 | then | |
67460d45 | 7472 | Conv_N := OK_Convert_To (Target_Base_Type, Duplicate_Subexpr (N)); |
f20b5ef4 | 7473 | else |
67460d45 | 7474 | Conv_N := Convert_To (Target_Base_Type, Duplicate_Subexpr (N)); |
f20b5ef4 JM |
7475 | end if; |
7476 | ||
67460d45 EB |
7477 | -- We make a temporary to hold the value of the conversion to the |
7478 | -- target base type, and then do the test against this temporary. | |
7479 | -- N itself is replaced by an occurrence of Tnn and followed by | |
7480 | -- the explicit range check. | |
f5655e4a | 7481 | |
b6621d10 AC |
7482 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); |
7483 | -- [constraint_error when Tnn not in Target_Type] | |
67460d45 | 7484 | -- Tnn |
b6621d10 | 7485 | |
f5655e4a AC |
7486 | Insert_Actions (N, New_List ( |
7487 | Make_Object_Declaration (Loc, | |
7488 | Defining_Identifier => Tnn, | |
7489 | Object_Definition => New_Occurrence_Of (Target_Base_Type, Loc), | |
7490 | Constant_Present => True, | |
67460d45 | 7491 | Expression => Conv_N), |
f5655e4a AC |
7492 | |
7493 | Make_Raise_Constraint_Error (Loc, | |
7494 | Condition => | |
7495 | Make_Not_In (Loc, | |
7496 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7497 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
7498 | Reason => Reason)), | |
67460d45 | 7499 | Suppress => Suppress); |
f5655e4a AC |
7500 | |
7501 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
7502 | ||
7503 | -- Set the type of N, because the declaration for Tnn might not | |
7504 | -- be analyzed yet, as is the case if N appears within a record | |
7505 | -- declaration, as a discriminant constraint or expression. | |
7506 | ||
7507 | Set_Etype (N, Target_Base_Type); | |
7508 | end Convert_And_Check_Range; | |
7509 | ||
cf9e3829 EB |
7510 | ------------------------------------- |
7511 | -- Is_Single_Attribute_Reference -- | |
7512 | ------------------------------------- | |
7513 | ||
7514 | function Is_Single_Attribute_Reference (N : Node_Id) return Boolean is | |
7515 | begin | |
7516 | if Nkind (N) = N_Attribute_Reference then | |
7517 | return True; | |
7518 | ||
7519 | elsif Nkind (N) in N_Binary_Op then | |
7520 | if Nkind (Right_Opnd (N)) = N_Integer_Literal then | |
7521 | return Is_Single_Attribute_Reference (Left_Opnd (N)); | |
7522 | ||
7523 | elsif Nkind (Left_Opnd (N)) = N_Integer_Literal then | |
7524 | return Is_Single_Attribute_Reference (Right_Opnd (N)); | |
7525 | ||
7526 | else | |
7527 | return False; | |
7528 | end if; | |
7529 | ||
7530 | else | |
7531 | return False; | |
7532 | end if; | |
7533 | end Is_Single_Attribute_Reference; | |
7534 | ||
f5655e4a AC |
7535 | -- Start of processing for Generate_Range_Check |
7536 | ||
fbf5a39b | 7537 | begin |
675d6070 TQ |
7538 | -- First special case, if the source type is already within the range |
7539 | -- of the target type, then no check is needed (probably we should have | |
7540 | -- stopped Do_Range_Check from being set in the first place, but better | |
67460d45 | 7541 | -- late than never in preventing junk code and junk flag settings). |
fbf5a39b | 7542 | |
c27f2f15 | 7543 | if In_Subrange_Of (Source_Type, Target_Type) |
347c766a RD |
7544 | |
7545 | -- We do NOT apply this if the source node is a literal, since in this | |
7546 | -- case the literal has already been labeled as having the subtype of | |
7547 | -- the target. | |
7548 | ||
fbf5a39b | 7549 | and then not |
4a08c95c AC |
7550 | (Nkind (N) in |
7551 | N_Integer_Literal | N_Real_Literal | N_Character_Literal | |
fbf5a39b | 7552 | or else |
347c766a RD |
7553 | (Is_Entity_Name (N) |
7554 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
fbf5a39b | 7555 | then |
edab6088 | 7556 | Set_Do_Range_Check (N, False); |
fbf5a39b AC |
7557 | return; |
7558 | end if; | |
7559 | ||
4ffefb70 PT |
7560 | -- Here a check is needed. If the expander is not active (which is also |
7561 | -- the case in GNATprove mode), then simply set the Do_Range_Check flag | |
7562 | -- and we are done. We just want to see the range check flag set, we do | |
7563 | -- not want to generate the explicit range check code. | |
edab6088 | 7564 | |
4ffefb70 | 7565 | if not Expander_Active then |
68c8d72a | 7566 | Set_Do_Range_Check (N); |
edab6088 RD |
7567 | return; |
7568 | end if; | |
7569 | ||
7570 | -- Here we will generate an explicit range check, so we don't want to | |
7571 | -- set the Do_Range check flag, since the range check is taken care of | |
7572 | -- by the code we will generate. | |
7573 | ||
7574 | Set_Do_Range_Check (N, False); | |
7575 | ||
7576 | -- Force evaluation of the node, so that it does not get evaluated twice | |
7577 | -- (once for the check, once for the actual reference). Such a double | |
7578 | -- evaluation is always a potential source of inefficiency, and is | |
7579 | -- functionally incorrect in the volatile case. | |
fbf5a39b | 7580 | |
967947ed PMR |
7581 | -- We skip the evaluation of attribute references because, after these |
7582 | -- runtime checks are generated, the expander may need to rewrite this | |
7583 | -- node (for example, see Attribute_Max_Size_In_Storage_Elements in | |
cf9e3829 EB |
7584 | -- Expand_N_Attribute_Reference) and, in many cases, their return type |
7585 | -- is universal integer, which is a very large type for a temporary. | |
967947ed | 7586 | |
cf9e3829 | 7587 | if not Is_Single_Attribute_Reference (N) |
967947ed | 7588 | and then (not Is_Entity_Name (N) |
c581c520 | 7589 | or else Treat_As_Volatile (Entity (N))) |
967947ed PMR |
7590 | then |
7591 | Force_Evaluation (N, Mode => Strict); | |
fbf5a39b AC |
7592 | end if; |
7593 | ||
675d6070 TQ |
7594 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
7595 | -- the same since in this case we can simply do a direct check of the | |
7596 | -- value of N against the bounds of Target_Type. | |
fbf5a39b AC |
7597 | |
7598 | -- [constraint_error when N not in Target_Type] | |
7599 | ||
7600 | -- Note: this is by far the most common case, for example all cases of | |
7601 | -- checks on the RHS of assignments are in this category, but not all | |
7602 | -- cases are like this. Notably conversions can involve two types. | |
7603 | ||
7604 | if Source_Base_Type = Target_Base_Type then | |
96e90ac1 RD |
7605 | |
7606 | -- Insert the explicit range check. Note that we suppress checks for | |
7607 | -- this code, since we don't want a recursive range check popping up. | |
7608 | ||
fbf5a39b AC |
7609 | Insert_Action (N, |
7610 | Make_Raise_Constraint_Error (Loc, | |
7611 | Condition => | |
7612 | Make_Not_In (Loc, | |
7613 | Left_Opnd => Duplicate_Subexpr (N), | |
7614 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
96e90ac1 RD |
7615 | Reason => Reason), |
7616 | Suppress => All_Checks); | |
fbf5a39b AC |
7617 | |
7618 | -- Next test for the case where the target type is within the bounds | |
7619 | -- of the base type of the source type, since in this case we can | |
7c2a44ae | 7620 | -- simply convert the bounds of the target type to this base type |
67460d45 | 7621 | -- to do the test. |
fbf5a39b AC |
7622 | |
7623 | -- [constraint_error when N not in | |
7624 | -- Source_Base_Type (Target_Type'First) | |
7625 | -- .. | |
7626 | -- Source_Base_Type(Target_Type'Last))] | |
7627 | ||
ddda9d0f | 7628 | -- The conversions will always work and need no check |
fbf5a39b | 7629 | |
d79e621a GD |
7630 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
7631 | -- of converting from an enumeration value to an integer type, such as | |
7632 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
7633 | -- (which used to be handled by gigi). This is OK, since the conversion | |
7634 | -- itself does not require a check. | |
7635 | ||
c27f2f15 | 7636 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
96e90ac1 RD |
7637 | |
7638 | -- Insert the explicit range check. Note that we suppress checks for | |
7639 | -- this code, since we don't want a recursive range check popping up. | |
7640 | ||
f5655e4a AC |
7641 | if Is_Discrete_Type (Source_Base_Type) |
7642 | and then | |
7643 | Is_Discrete_Type (Target_Base_Type) | |
7644 | then | |
7645 | Insert_Action (N, | |
7646 | Make_Raise_Constraint_Error (Loc, | |
7647 | Condition => | |
7648 | Make_Not_In (Loc, | |
7649 | Left_Opnd => Duplicate_Subexpr (N), | |
7650 | ||
7651 | Right_Opnd => | |
7652 | Make_Range (Loc, | |
7653 | Low_Bound => | |
7654 | Unchecked_Convert_To (Source_Base_Type, | |
7655 | Make_Attribute_Reference (Loc, | |
7656 | Prefix => | |
7657 | New_Occurrence_Of (Target_Type, Loc), | |
7658 | Attribute_Name => Name_First)), | |
7659 | ||
7660 | High_Bound => | |
7661 | Unchecked_Convert_To (Source_Base_Type, | |
7662 | Make_Attribute_Reference (Loc, | |
7663 | Prefix => | |
7664 | New_Occurrence_Of (Target_Type, Loc), | |
7665 | Attribute_Name => Name_Last)))), | |
7666 | Reason => Reason), | |
7667 | Suppress => All_Checks); | |
fbf5a39b | 7668 | |
f5655e4a | 7669 | -- For conversions involving at least one type that is not discrete, |
67460d45 EB |
7670 | -- first convert to the target base type and then generate the range |
7671 | -- check. This avoids problems with values that are close to a bound | |
7672 | -- of the target type that would fail a range check when done in a | |
7673 | -- larger source type before converting but pass if converted with | |
f5655e4a AC |
7674 | -- rounding and then checked (such as in float-to-float conversions). |
7675 | ||
67460d45 EB |
7676 | -- Note that overflow checks are not suppressed for this code because |
7677 | -- we do not know whether the source type is in range of the target | |
7678 | -- base type (unlike in the next case below). | |
7679 | ||
f5655e4a | 7680 | else |
67460d45 | 7681 | Convert_And_Check_Range (Suppress => Range_Check); |
f5655e4a | 7682 | end if; |
fbf5a39b | 7683 | |
929d5203 | 7684 | -- Note that at this stage we know that the Target_Base_Type is not in |
675d6070 TQ |
7685 | -- the range of the Source_Base_Type (since even the Target_Type itself |
7686 | -- is not in this range). It could still be the case that Source_Type is | |
7687 | -- in range of the target base type since we have not checked that case. | |
fbf5a39b | 7688 | |
675d6070 | 7689 | -- If that is the case, we can freely convert the source to the target, |
67460d45 EB |
7690 | -- and then test the target result against the bounds. Note that checks |
7691 | -- are suppressed for this code, since we don't want a recursive range | |
7692 | -- check popping up. | |
fbf5a39b | 7693 | |
c27f2f15 | 7694 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
67460d45 | 7695 | Convert_And_Check_Range (Suppress => All_Checks); |
fbf5a39b AC |
7696 | |
7697 | -- At this stage, we know that we have two scalar types, which are | |
7698 | -- directly convertible, and where neither scalar type has a base | |
7699 | -- range that is in the range of the other scalar type. | |
7700 | ||
7701 | -- The only way this can happen is with a signed and unsigned type. | |
7702 | -- So test for these two cases: | |
7703 | ||
7704 | else | |
7705 | -- Case of the source is unsigned and the target is signed | |
7706 | ||
7707 | if Is_Unsigned_Type (Source_Base_Type) | |
7708 | and then not Is_Unsigned_Type (Target_Base_Type) | |
7709 | then | |
7710 | -- If the source is unsigned and the target is signed, then we | |
7711 | -- know that the source is not shorter than the target (otherwise | |
7712 | -- the source base type would be in the target base type range). | |
7713 | ||
675d6070 TQ |
7714 | -- In other words, the unsigned type is either the same size as |
7715 | -- the target, or it is larger. It cannot be smaller. | |
fbf5a39b AC |
7716 | |
7717 | pragma Assert | |
7718 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
7719 | ||
7720 | -- We only need to check the low bound if the low bound of the | |
7721 | -- target type is non-negative. If the low bound of the target | |
7722 | -- type is negative, then we know that we will fit fine. | |
7723 | ||
7724 | -- If the high bound of the target type is negative, then we | |
7725 | -- know we have a constraint error, since we can't possibly | |
7726 | -- have a negative source. | |
7727 | ||
7728 | -- With these two checks out of the way, we can do the check | |
7729 | -- using the source type safely | |
7730 | ||
a90bd866 | 7731 | -- This is definitely the most annoying case. |
fbf5a39b AC |
7732 | |
7733 | -- [constraint_error | |
7734 | -- when (Target_Type'First >= 0 | |
7735 | -- and then | |
7736 | -- N < Source_Base_Type (Target_Type'First)) | |
7737 | -- or else Target_Type'Last < 0 | |
7738 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
7739 | ||
7740 | -- We turn off all checks since we know that the conversions | |
7741 | -- will work fine, given the guards for negative values. | |
7742 | ||
7743 | Insert_Action (N, | |
7744 | Make_Raise_Constraint_Error (Loc, | |
7745 | Condition => | |
7746 | Make_Or_Else (Loc, | |
7747 | Make_Or_Else (Loc, | |
7748 | Left_Opnd => | |
7749 | Make_And_Then (Loc, | |
7750 | Left_Opnd => Make_Op_Ge (Loc, | |
7751 | Left_Opnd => | |
7752 | Make_Attribute_Reference (Loc, | |
7753 | Prefix => | |
7754 | New_Occurrence_Of (Target_Type, Loc), | |
7755 | Attribute_Name => Name_First), | |
7756 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7757 | ||
7758 | Right_Opnd => | |
7759 | Make_Op_Lt (Loc, | |
7760 | Left_Opnd => Duplicate_Subexpr (N), | |
7761 | Right_Opnd => | |
7762 | Convert_To (Source_Base_Type, | |
7763 | Make_Attribute_Reference (Loc, | |
7764 | Prefix => | |
7765 | New_Occurrence_Of (Target_Type, Loc), | |
7766 | Attribute_Name => Name_First)))), | |
7767 | ||
7768 | Right_Opnd => | |
7769 | Make_Op_Lt (Loc, | |
7770 | Left_Opnd => | |
7771 | Make_Attribute_Reference (Loc, | |
7772 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7773 | Attribute_Name => Name_Last), | |
7774 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
7775 | ||
7776 | Right_Opnd => | |
7777 | Make_Op_Gt (Loc, | |
7778 | Left_Opnd => Duplicate_Subexpr (N), | |
7779 | Right_Opnd => | |
7780 | Convert_To (Source_Base_Type, | |
7781 | Make_Attribute_Reference (Loc, | |
7782 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
7783 | Attribute_Name => Name_Last)))), | |
7784 | ||
7785 | Reason => Reason), | |
7786 | Suppress => All_Checks); | |
7787 | ||
7788 | -- Only remaining possibility is that the source is signed and | |
b568955d | 7789 | -- the target is unsigned. |
fbf5a39b AC |
7790 | |
7791 | else | |
7792 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
637a41a5 | 7793 | and then Is_Unsigned_Type (Target_Base_Type)); |
fbf5a39b | 7794 | |
675d6070 TQ |
7795 | -- If the source is signed and the target is unsigned, then we |
7796 | -- know that the target is not shorter than the source (otherwise | |
7797 | -- the target base type would be in the source base type range). | |
fbf5a39b | 7798 | |
675d6070 TQ |
7799 | -- In other words, the unsigned type is either the same size as |
7800 | -- the target, or it is larger. It cannot be smaller. | |
fbf5a39b | 7801 | |
675d6070 TQ |
7802 | -- Clearly we have an error if the source value is negative since |
7803 | -- no unsigned type can have negative values. If the source type | |
7804 | -- is non-negative, then the check can be done using the target | |
7805 | -- type. | |
fbf5a39b AC |
7806 | |
7807 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
7808 | ||
7809 | -- [constraint_error | |
7810 | -- when N < 0 or else Tnn not in Target_Type]; | |
7811 | ||
675d6070 TQ |
7812 | -- We turn off all checks for the conversion of N to the target |
7813 | -- base type, since we generate the explicit check to ensure that | |
7814 | -- the value is non-negative | |
fbf5a39b AC |
7815 | |
7816 | declare | |
191fcb3a | 7817 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
fbf5a39b AC |
7818 | |
7819 | begin | |
7820 | Insert_Actions (N, New_List ( | |
7821 | Make_Object_Declaration (Loc, | |
7822 | Defining_Identifier => Tnn, | |
7823 | Object_Definition => | |
7824 | New_Occurrence_Of (Target_Base_Type, Loc), | |
7825 | Constant_Present => True, | |
7826 | Expression => | |
738a0e8d BD |
7827 | Unchecked_Convert_To |
7828 | (Target_Base_Type, Duplicate_Subexpr (N))), | |
fbf5a39b AC |
7829 | |
7830 | Make_Raise_Constraint_Error (Loc, | |
7831 | Condition => | |
7832 | Make_Or_Else (Loc, | |
7833 | Left_Opnd => | |
7834 | Make_Op_Lt (Loc, | |
7835 | Left_Opnd => Duplicate_Subexpr (N), | |
7836 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7837 | ||
7838 | Right_Opnd => | |
7839 | Make_Not_In (Loc, | |
7840 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
7841 | Right_Opnd => | |
7842 | New_Occurrence_Of (Target_Type, Loc))), | |
7843 | ||
637a41a5 | 7844 | Reason => Reason)), |
fbf5a39b AC |
7845 | Suppress => All_Checks); |
7846 | ||
675d6070 TQ |
7847 | -- Set the Etype explicitly, because Insert_Actions may have |
7848 | -- placed the declaration in the freeze list for an enclosing | |
7849 | -- construct, and thus it is not analyzed yet. | |
fbf5a39b AC |
7850 | |
7851 | Set_Etype (Tnn, Target_Base_Type); | |
7852 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
7853 | end; | |
7854 | end if; | |
7855 | end if; | |
7856 | end Generate_Range_Check; | |
7857 | ||
939c12d2 RD |
7858 | ------------------ |
7859 | -- Get_Check_Id -- | |
7860 | ------------------ | |
7861 | ||
7862 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
7863 | begin | |
7864 | -- For standard check name, we can do a direct computation | |
7865 | ||
7866 | if N in First_Check_Name .. Last_Check_Name then | |
7867 | return Check_Id (N - (First_Check_Name - 1)); | |
7868 | ||
7869 | -- For non-standard names added by pragma Check_Name, search table | |
7870 | ||
7871 | else | |
7872 | for J in All_Checks + 1 .. Check_Names.Last loop | |
7873 | if Check_Names.Table (J) = N then | |
7874 | return J; | |
7875 | end if; | |
7876 | end loop; | |
7877 | end if; | |
7878 | ||
7879 | -- No matching name found | |
7880 | ||
7881 | return No_Check_Id; | |
7882 | end Get_Check_Id; | |
7883 | ||
70482933 RK |
7884 | --------------------- |
7885 | -- Get_Discriminal -- | |
7886 | --------------------- | |
7887 | ||
7888 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
7889 | Loc : constant Source_Ptr := Sloc (E); | |
7890 | D : Entity_Id; | |
7891 | Sc : Entity_Id; | |
7892 | ||
7893 | begin | |
c064e066 RD |
7894 | -- The bound can be a bona fide parameter of a protected operation, |
7895 | -- rather than a prival encoded as an in-parameter. | |
7896 | ||
7897 | if No (Discriminal_Link (Entity (Bound))) then | |
7898 | return Bound; | |
7899 | end if; | |
7900 | ||
939c12d2 RD |
7901 | -- Climb the scope stack looking for an enclosing protected type. If |
7902 | -- we run out of scopes, return the bound itself. | |
7903 | ||
7904 | Sc := Scope (E); | |
7905 | while Present (Sc) loop | |
7906 | if Sc = Standard_Standard then | |
7907 | return Bound; | |
939c12d2 RD |
7908 | elsif Ekind (Sc) = E_Protected_Type then |
7909 | exit; | |
7910 | end if; | |
7911 | ||
7912 | Sc := Scope (Sc); | |
7913 | end loop; | |
7914 | ||
70482933 | 7915 | D := First_Discriminant (Sc); |
939c12d2 RD |
7916 | while Present (D) loop |
7917 | if Chars (D) = Chars (Bound) then | |
7918 | return New_Occurrence_Of (Discriminal (D), Loc); | |
7919 | end if; | |
70482933 | 7920 | |
70482933 RK |
7921 | Next_Discriminant (D); |
7922 | end loop; | |
7923 | ||
939c12d2 | 7924 | return Bound; |
70482933 RK |
7925 | end Get_Discriminal; |
7926 | ||
939c12d2 RD |
7927 | ---------------------- |
7928 | -- Get_Range_Checks -- | |
7929 | ---------------------- | |
7930 | ||
7931 | function Get_Range_Checks | |
6c8e70fe | 7932 | (Expr : Node_Id; |
939c12d2 RD |
7933 | Target_Typ : Entity_Id; |
7934 | Source_Typ : Entity_Id := Empty; | |
7935 | Warn_Node : Node_Id := Empty) return Check_Result | |
7936 | is | |
7937 | begin | |
637a41a5 | 7938 | return |
6c8e70fe | 7939 | Selected_Range_Checks (Expr, Target_Typ, Source_Typ, Warn_Node); |
939c12d2 RD |
7940 | end Get_Range_Checks; |
7941 | ||
70482933 RK |
7942 | ------------------ |
7943 | -- Guard_Access -- | |
7944 | ------------------ | |
7945 | ||
7946 | function Guard_Access | |
6c8e70fe EB |
7947 | (Cond : Node_Id; |
7948 | Loc : Source_Ptr; | |
7949 | Expr : Node_Id) return Node_Id | |
70482933 RK |
7950 | is |
7951 | begin | |
7952 | if Nkind (Cond) = N_Or_Else then | |
7953 | Set_Paren_Count (Cond, 1); | |
7954 | end if; | |
7955 | ||
6c8e70fe | 7956 | if Nkind (Expr) = N_Allocator then |
70482933 | 7957 | return Cond; |
637a41a5 | 7958 | |
70482933 RK |
7959 | else |
7960 | return | |
7961 | Make_And_Then (Loc, | |
7962 | Left_Opnd => | |
7963 | Make_Op_Ne (Loc, | |
6c8e70fe | 7964 | Left_Opnd => Duplicate_Subexpr_No_Checks (Expr), |
70482933 RK |
7965 | Right_Opnd => Make_Null (Loc)), |
7966 | Right_Opnd => Cond); | |
7967 | end if; | |
7968 | end Guard_Access; | |
7969 | ||
7970 | ----------------------------- | |
7971 | -- Index_Checks_Suppressed -- | |
7972 | ----------------------------- | |
7973 | ||
7974 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7975 | begin | |
fbf5a39b AC |
7976 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7977 | return Is_Check_Suppressed (E, Index_Check); | |
7978 | else | |
3217f71e | 7979 | return Scope_Suppress.Suppress (Index_Check); |
fbf5a39b | 7980 | end if; |
70482933 RK |
7981 | end Index_Checks_Suppressed; |
7982 | ||
7983 | ---------------- | |
7984 | -- Initialize -- | |
7985 | ---------------- | |
7986 | ||
7987 | procedure Initialize is | |
7988 | begin | |
7989 | for J in Determine_Range_Cache_N'Range loop | |
7990 | Determine_Range_Cache_N (J) := Empty; | |
7991 | end loop; | |
939c12d2 RD |
7992 | |
7993 | Check_Names.Init; | |
7994 | ||
7995 | for J in Int range 1 .. All_Checks loop | |
7996 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
7997 | end loop; | |
70482933 RK |
7998 | end Initialize; |
7999 | ||
8000 | ------------------------- | |
8001 | -- Insert_Range_Checks -- | |
8002 | ------------------------- | |
8003 | ||
8004 | procedure Insert_Range_Checks | |
8005 | (Checks : Check_Result; | |
8006 | Node : Node_Id; | |
8007 | Suppress_Typ : Entity_Id; | |
bbe7d67f | 8008 | Static_Sloc : Source_Ptr; |
70482933 RK |
8009 | Do_Before : Boolean := False) |
8010 | is | |
e0666fc6 AC |
8011 | Checks_On : constant Boolean := |
8012 | not Index_Checks_Suppressed (Suppress_Typ) | |
8013 | or else | |
8014 | not Range_Checks_Suppressed (Suppress_Typ); | |
8015 | ||
bbe7d67f | 8016 | Check_Node : Node_Id; |
70482933 | 8017 | |
70482933 | 8018 | begin |
675d6070 TQ |
8019 | -- For now we just return if Checks_On is false, however this should be |
8020 | -- enhanced to check for an always True value in the condition and to | |
869a06d9 | 8021 | -- generate a compilation warning. |
70482933 | 8022 | |
1f0b1e48 | 8023 | if not Expander_Active or not Checks_On then |
70482933 RK |
8024 | return; |
8025 | end if; | |
8026 | ||
70482933 RK |
8027 | for J in 1 .. 2 loop |
8028 | exit when No (Checks (J)); | |
8029 | ||
8030 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
8031 | and then Present (Condition (Checks (J))) | |
8032 | then | |
bbe7d67f | 8033 | Check_Node := Checks (J); |
70482933 RK |
8034 | else |
8035 | Check_Node := | |
bbe7d67f | 8036 | Make_Raise_Constraint_Error (Static_Sloc, |
07fc65c4 | 8037 | Reason => CE_Range_Check_Failed); |
bbe7d67f | 8038 | end if; |
70482933 | 8039 | |
bbe7d67f EB |
8040 | Mark_Rewrite_Insertion (Check_Node); |
8041 | ||
8042 | if Do_Before then | |
8043 | Insert_Before_And_Analyze (Node, Check_Node); | |
8044 | else | |
8045 | Insert_After_And_Analyze (Node, Check_Node); | |
70482933 RK |
8046 | end if; |
8047 | end loop; | |
8048 | end Insert_Range_Checks; | |
8049 | ||
8050 | ------------------------ | |
8051 | -- Insert_Valid_Check -- | |
8052 | ------------------------ | |
8053 | ||
2934b84a AC |
8054 | procedure Insert_Valid_Check |
8055 | (Expr : Node_Id; | |
8056 | Related_Id : Entity_Id := Empty; | |
8057 | Is_Low_Bound : Boolean := False; | |
8058 | Is_High_Bound : Boolean := False) | |
8059 | is | |
70482933 | 8060 | Loc : constant Source_Ptr := Sloc (Expr); |
d9fe0e53 | 8061 | Typ : Entity_Id := Etype (Expr); |
84157f51 | 8062 | Exp : Node_Id; |
70482933 RK |
8063 | |
8064 | begin | |
2934b84a AC |
8065 | -- Do not insert if checks off, or if not checking validity or if |
8066 | -- expression is known to be valid. | |
70482933 | 8067 | |
c064e066 RD |
8068 | if not Validity_Checks_On |
8069 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
8dc2ddaf | 8070 | or else Expr_Known_Valid (Expr) |
70482933 | 8071 | then |
84157f51 | 8072 | return; |
70482933 | 8073 | |
489c6e19 AC |
8074 | -- Do not insert checks within a predicate function. This will arise |
8075 | -- if the current unit and the predicate function are being compiled | |
8076 | -- with validity checks enabled. | |
d515aef3 | 8077 | |
b0cd50fd | 8078 | elsif Present (Predicate_Function (Typ)) |
d515aef3 AC |
8079 | and then Current_Scope = Predicate_Function (Typ) |
8080 | then | |
8081 | return; | |
d515aef3 | 8082 | |
9dc30a5f AC |
8083 | -- If the expression is a packed component of a modular type of the |
8084 | -- right size, the data is always valid. | |
8085 | ||
b0cd50fd | 8086 | elsif Nkind (Expr) = N_Selected_Component |
9dc30a5f AC |
8087 | and then Present (Component_Clause (Entity (Selector_Name (Expr)))) |
8088 | and then Is_Modular_Integer_Type (Typ) | |
8089 | and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) | |
8090 | then | |
8091 | return; | |
b0cd50fd AC |
8092 | |
8093 | -- Do not generate a validity check when inside a generic unit as this | |
8094 | -- is an expansion activity. | |
8095 | ||
8096 | elsif Inside_A_Generic then | |
8097 | return; | |
9dc30a5f AC |
8098 | end if; |
8099 | ||
6578a6bf HK |
8100 | -- Entities declared in Lock_free protected types must be treated as |
8101 | -- volatile, and we must inhibit validity checks to prevent improper | |
8102 | -- constant folding. | |
90fd73bb ES |
8103 | |
8104 | if Is_Entity_Name (Expr) | |
8105 | and then Is_Subprogram (Scope (Entity (Expr))) | |
8106 | and then Present (Protected_Subprogram (Scope (Entity (Expr)))) | |
8107 | and then Uses_Lock_Free | |
6578a6bf | 8108 | (Scope (Protected_Subprogram (Scope (Entity (Expr))))) |
90fd73bb ES |
8109 | then |
8110 | return; | |
8111 | end if; | |
8112 | ||
84157f51 GB |
8113 | -- If we have a checked conversion, then validity check applies to |
8114 | -- the expression inside the conversion, not the result, since if | |
8115 | -- the expression inside is valid, then so is the conversion result. | |
70482933 | 8116 | |
84157f51 GB |
8117 | Exp := Expr; |
8118 | while Nkind (Exp) = N_Type_Conversion loop | |
8119 | Exp := Expression (Exp); | |
8120 | end loop; | |
d9fe0e53 | 8121 | Typ := Etype (Exp); |
84157f51 | 8122 | |
89b6c83e AC |
8123 | -- Do not generate a check for a variable which already validates the |
8124 | -- value of an assignable object. | |
8125 | ||
8126 | if Is_Validation_Variable_Reference (Exp) then | |
8127 | return; | |
8128 | end if; | |
8129 | ||
c064e066 | 8130 | declare |
62e45e3e | 8131 | CE : Node_Id; |
62e45e3e HK |
8132 | PV : Node_Id; |
8133 | Var_Id : Entity_Id; | |
d8b9660d | 8134 | |
c064e066 | 8135 | begin |
89b6c83e AC |
8136 | -- If the expression denotes an assignable object, capture its value |
8137 | -- in a variable and replace the original expression by the variable. | |
8138 | -- This approach has several effects: | |
8dc2ddaf | 8139 | |
89b6c83e AC |
8140 | -- 1) The evaluation of the object results in only one read in the |
8141 | -- case where the object is atomic or volatile. | |
59f4d038 | 8142 | |
62e45e3e | 8143 | -- Var ... := Object; -- read |
59f4d038 | 8144 | |
89b6c83e AC |
8145 | -- 2) The captured value is the one verified by attribute 'Valid. |
8146 | -- As a result the object is not evaluated again, which would | |
8147 | -- result in an unwanted read in the case where the object is | |
8148 | -- atomic or volatile. | |
8149 | ||
62e45e3e | 8150 | -- if not Var'Valid then -- OK, no read of Object |
89b6c83e AC |
8151 | |
8152 | -- if not Object'Valid then -- Wrong, extra read of Object | |
8153 | ||
8154 | -- 3) The captured value replaces the original object reference. | |
8155 | -- As a result the object is not evaluated again, in the same | |
8156 | -- vein as 2). | |
8157 | ||
62e45e3e | 8158 | -- ... Var ... -- OK, no read of Object |
89b6c83e AC |
8159 | |
8160 | -- ... Object ... -- Wrong, extra read of Object | |
8dc2ddaf | 8161 | |
89b6c83e AC |
8162 | -- 4) The use of a variable to capture the value of the object |
8163 | -- allows the propagation of any changes back to the original | |
8164 | -- object. | |
8165 | ||
8166 | -- procedure Call (Val : in out ...); | |
8167 | ||
62e45e3e HK |
8168 | -- Var : ... := Object; -- read Object |
8169 | -- if not Var'Valid then -- validity check | |
8170 | -- Call (Var); -- modify Var | |
8171 | -- Object := Var; -- update Object | |
89b6c83e AC |
8172 | |
8173 | if Is_Variable (Exp) then | |
62e45e3e | 8174 | Var_Id := Make_Temporary (Loc, 'T', Exp); |
89b6c83e | 8175 | |
683af98c AC |
8176 | -- Because we could be dealing with a transient scope which would |
8177 | -- cause our object declaration to remain unanalyzed we must do | |
8178 | -- some manual decoration. | |
8179 | ||
2e02ab86 | 8180 | Mutate_Ekind (Var_Id, E_Variable); |
683af98c AC |
8181 | Set_Etype (Var_Id, Typ); |
8182 | ||
89b6c83e AC |
8183 | Insert_Action (Exp, |
8184 | Make_Object_Declaration (Loc, | |
62e45e3e | 8185 | Defining_Identifier => Var_Id, |
89b6c83e | 8186 | Object_Definition => New_Occurrence_Of (Typ, Loc), |
dd89dddf AC |
8187 | Expression => New_Copy_Tree (Exp)), |
8188 | Suppress => Validity_Check); | |
8189 | ||
8190 | Set_Validated_Object (Var_Id, New_Copy_Tree (Exp)); | |
13931a38 | 8191 | |
62e45e3e | 8192 | Rewrite (Exp, New_Occurrence_Of (Var_Id, Loc)); |
89b6c83e | 8193 | |
1361a4fb EB |
8194 | -- Move the Do_Range_Check flag over to the new Exp so it doesn't |
8195 | -- get lost and doesn't leak elsewhere. | |
3815f967 | 8196 | |
1361a4fb EB |
8197 | if Do_Range_Check (Validated_Object (Var_Id)) then |
8198 | Set_Do_Range_Check (Exp); | |
8199 | Set_Do_Range_Check (Validated_Object (Var_Id), False); | |
3815f967 AC |
8200 | end if; |
8201 | ||
d9fe0e53 MP |
8202 | -- In case of a type conversion, an expansion of the expr may be |
8203 | -- needed (eg. fixed-point as actual). | |
8204 | ||
8205 | if Exp /= Expr then | |
8206 | pragma Assert (Nkind (Expr) = N_Type_Conversion); | |
8207 | Analyze_And_Resolve (Expr); | |
8208 | end if; | |
8209 | ||
13931a38 EB |
8210 | PV := New_Occurrence_Of (Var_Id, Loc); |
8211 | ||
89b6c83e AC |
8212 | -- Otherwise the expression does not denote a variable. Force its |
8213 | -- evaluation by capturing its value in a constant. Generate: | |
8214 | ||
8215 | -- Temp : constant ... := Exp; | |
8216 | ||
8217 | else | |
8218 | Force_Evaluation | |
8219 | (Exp => Exp, | |
8220 | Related_Id => Related_Id, | |
8221 | Is_Low_Bound => Is_Low_Bound, | |
8222 | Is_High_Bound => Is_High_Bound); | |
8223 | ||
8224 | PV := New_Copy_Tree (Exp); | |
8225 | end if; | |
c064e066 | 8226 | |
e80f0cb0 RD |
8227 | -- A rather specialized test. If PV is an analyzed expression which |
8228 | -- is an indexed component of a packed array that has not been | |
8229 | -- properly expanded, turn off its Analyzed flag to make sure it | |
4bd4bb7f AC |
8230 | -- gets properly reexpanded. If the prefix is an access value, |
8231 | -- the dereference will be added later. | |
0e564ab4 AC |
8232 | |
8233 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
8234 | -- an analyze with the old parent pointer. This may point e.g. to | |
8235 | -- a subprogram call, which deactivates this expansion. | |
8236 | ||
8237 | if Analyzed (PV) | |
8238 | and then Nkind (PV) = N_Indexed_Component | |
4bd4bb7f | 8239 | and then Is_Array_Type (Etype (Prefix (PV))) |
8ca597af | 8240 | and then Present (Packed_Array_Impl_Type (Etype (Prefix (PV)))) |
0e564ab4 AC |
8241 | then |
8242 | Set_Analyzed (PV, False); | |
8243 | end if; | |
8244 | ||
59f4d038 RD |
8245 | -- Build the raise CE node to check for validity. We build a type |
8246 | -- qualification for the prefix, since it may not be of the form of | |
8247 | -- a name, and we don't care in this context! | |
0e564ab4 AC |
8248 | |
8249 | CE := | |
2934b84a AC |
8250 | Make_Raise_Constraint_Error (Loc, |
8251 | Condition => | |
8252 | Make_Op_Not (Loc, | |
8253 | Right_Opnd => | |
8254 | Make_Attribute_Reference (Loc, | |
8255 | Prefix => PV, | |
8256 | Attribute_Name => Name_Valid)), | |
8257 | Reason => CE_Invalid_Data); | |
0e564ab4 AC |
8258 | |
8259 | -- Insert the validity check. Note that we do this with validity | |
8260 | -- checks turned off, to avoid recursion, we do not want validity | |
a90bd866 | 8261 | -- checks on the validity checking code itself. |
0e564ab4 AC |
8262 | |
8263 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
c064e066 | 8264 | |
308e6f3a | 8265 | -- If the expression is a reference to an element of a bit-packed |
c064e066 RD |
8266 | -- array, then it is rewritten as a renaming declaration. If the |
8267 | -- expression is an actual in a call, it has not been expanded, | |
8268 | -- waiting for the proper point at which to do it. The same happens | |
8269 | -- with renamings, so that we have to force the expansion now. This | |
8270 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
8271 | -- and exp_ch6.adb. | |
8272 | ||
8273 | if Is_Entity_Name (Exp) | |
8274 | and then Nkind (Parent (Entity (Exp))) = | |
637a41a5 | 8275 | N_Object_Renaming_Declaration |
c064e066 RD |
8276 | then |
8277 | declare | |
8278 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
8279 | begin | |
8280 | if Nkind (Old_Exp) = N_Indexed_Component | |
8281 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
8282 | then | |
8283 | Expand_Packed_Element_Reference (Old_Exp); | |
8284 | end if; | |
8285 | end; | |
8286 | end if; | |
c064e066 | 8287 | end; |
70482933 RK |
8288 | end Insert_Valid_Check; |
8289 | ||
acad3c0a AC |
8290 | ------------------------------------- |
8291 | -- Is_Signed_Integer_Arithmetic_Op -- | |
8292 | ------------------------------------- | |
8293 | ||
8294 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
8295 | begin | |
8296 | case Nkind (N) is | |
d8f43ee6 HK |
8297 | when N_Op_Abs |
8298 | | N_Op_Add | |
8299 | | N_Op_Divide | |
8300 | | N_Op_Expon | |
8301 | | N_Op_Minus | |
8302 | | N_Op_Mod | |
8303 | | N_Op_Multiply | |
8304 | | N_Op_Plus | |
8305 | | N_Op_Rem | |
8306 | | N_Op_Subtract | |
8307 | => | |
acad3c0a AC |
8308 | return Is_Signed_Integer_Type (Etype (N)); |
8309 | ||
d8f43ee6 HK |
8310 | when N_Case_Expression |
8311 | | N_If_Expression | |
8312 | => | |
4b1c4f20 RD |
8313 | return Is_Signed_Integer_Type (Etype (N)); |
8314 | ||
acad3c0a AC |
8315 | when others => |
8316 | return False; | |
8317 | end case; | |
8318 | end Is_Signed_Integer_Arithmetic_Op; | |
8319 | ||
2820d220 AC |
8320 | ---------------------------------- |
8321 | -- Install_Null_Excluding_Check -- | |
8322 | ---------------------------------- | |
8323 | ||
8324 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
437f8c1e | 8325 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
86ac5e79 ES |
8326 | Typ : constant Entity_Id := Etype (N); |
8327 | ||
8328 | procedure Mark_Non_Null; | |
bb6e3d41 HK |
8329 | -- After installation of check, if the node in question is an entity |
8330 | -- name, then mark this entity as non-null if possible. | |
8331 | ||
86ac5e79 ES |
8332 | ------------------- |
8333 | -- Mark_Non_Null -- | |
8334 | ------------------- | |
8335 | ||
8336 | procedure Mark_Non_Null is | |
8337 | begin | |
bb6e3d41 HK |
8338 | -- Only case of interest is if node N is an entity name |
8339 | ||
86ac5e79 | 8340 | if Is_Entity_Name (N) then |
bb6e3d41 HK |
8341 | |
8342 | -- For sure, we want to clear an indication that this is known to | |
a90bd866 | 8343 | -- be null, since if we get past this check, it definitely is not. |
bb6e3d41 | 8344 | |
86ac5e79 ES |
8345 | Set_Is_Known_Null (Entity (N), False); |
8346 | ||
214b1cb8 PT |
8347 | -- We can mark the entity as known to be non-null if it is safe to |
8348 | -- capture the value. | |
bb6e3d41 | 8349 | |
214b1cb8 | 8350 | if Safe_To_Capture_Value (N, Entity (N)) then |
bb6e3d41 | 8351 | Set_Is_Known_Non_Null (Entity (N)); |
86ac5e79 ES |
8352 | end if; |
8353 | end if; | |
8354 | end Mark_Non_Null; | |
8355 | ||
8356 | -- Start of processing for Install_Null_Excluding_Check | |
2820d220 AC |
8357 | |
8358 | begin | |
fcf1dd74 JM |
8359 | -- No need to add null-excluding checks when the tree may not be fully |
8360 | -- decorated. | |
8361 | ||
8362 | if Serious_Errors_Detected > 0 then | |
8363 | return; | |
8364 | end if; | |
8365 | ||
86ac5e79 | 8366 | pragma Assert (Is_Access_Type (Typ)); |
2820d220 | 8367 | |
cca7f107 | 8368 | -- No check inside a generic, check will be emitted in instance |
2820d220 | 8369 | |
86ac5e79 | 8370 | if Inside_A_Generic then |
2820d220 | 8371 | return; |
86ac5e79 ES |
8372 | end if; |
8373 | ||
8374 | -- No check needed if known to be non-null | |
8375 | ||
8376 | if Known_Non_Null (N) then | |
d8b9660d | 8377 | return; |
86ac5e79 | 8378 | end if; |
2820d220 | 8379 | |
86ac5e79 ES |
8380 | -- If known to be null, here is where we generate a compile time check |
8381 | ||
8382 | if Known_Null (N) then | |
b1c11e0e | 8383 | |
637a41a5 AC |
8384 | -- Avoid generating warning message inside init procs. In SPARK mode |
8385 | -- we can go ahead and call Apply_Compile_Time_Constraint_Error | |
cca7f107 | 8386 | -- since it will be turned into an error in any case. |
b1c11e0e | 8387 | |
cca7f107 AC |
8388 | if (not Inside_Init_Proc or else SPARK_Mode = On) |
8389 | ||
1ae70618 ES |
8390 | -- Do not emit the warning within a conditional expression, |
8391 | -- where the expression might not be evaluated, and the warning | |
8392 | -- appear as extraneous noise. | |
cca7f107 AC |
8393 | |
8394 | and then not Within_Case_Or_If_Expression (N) | |
8395 | then | |
b1c11e0e | 8396 | Apply_Compile_Time_Constraint_Error |
4a28b181 | 8397 | (N, "null value not allowed here??", CE_Access_Check_Failed); |
cca7f107 AC |
8398 | |
8399 | -- Remaining cases, where we silently insert the raise | |
8400 | ||
b1c11e0e JM |
8401 | else |
8402 | Insert_Action (N, | |
8403 | Make_Raise_Constraint_Error (Loc, | |
8404 | Reason => CE_Access_Check_Failed)); | |
8405 | end if; | |
8406 | ||
86ac5e79 ES |
8407 | Mark_Non_Null; |
8408 | return; | |
8409 | end if; | |
8410 | ||
8411 | -- If entity is never assigned, for sure a warning is appropriate | |
8412 | ||
8413 | if Is_Entity_Name (N) then | |
8414 | Check_Unset_Reference (N); | |
2820d220 | 8415 | end if; |
86ac5e79 ES |
8416 | |
8417 | -- No check needed if checks are suppressed on the range. Note that we | |
8418 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
8419 | -- so, since the program is erroneous, but we don't like to casually | |
8420 | -- propagate such conclusions from erroneosity). | |
8421 | ||
8422 | if Access_Checks_Suppressed (Typ) then | |
8423 | return; | |
8424 | end if; | |
8425 | ||
939c12d2 RD |
8426 | -- No check needed for access to concurrent record types generated by |
8427 | -- the expander. This is not just an optimization (though it does indeed | |
8428 | -- remove junk checks). It also avoids generation of junk warnings. | |
8429 | ||
8430 | if Nkind (N) in N_Has_Chars | |
8431 | and then Chars (N) = Name_uObject | |
8432 | and then Is_Concurrent_Record_Type | |
8433 | (Directly_Designated_Type (Etype (N))) | |
8434 | then | |
8435 | return; | |
8436 | end if; | |
8437 | ||
0a376301 JM |
8438 | -- No check needed in interface thunks since the runtime check is |
8439 | -- already performed at the caller side. | |
8440 | ||
8441 | if Is_Thunk (Current_Scope) then | |
8442 | return; | |
8443 | end if; | |
8444 | ||
5c34f30d YM |
8445 | -- In GNATprove mode, we do not apply the check |
8446 | ||
8447 | if GNATprove_Mode then | |
74cab21a EB |
8448 | return; |
8449 | end if; | |
8450 | ||
86ac5e79 ES |
8451 | -- Otherwise install access check |
8452 | ||
8453 | Insert_Action (N, | |
8454 | Make_Raise_Constraint_Error (Loc, | |
8455 | Condition => | |
8456 | Make_Op_Eq (Loc, | |
8457 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
8458 | Right_Opnd => Make_Null (Loc)), | |
8459 | Reason => CE_Access_Check_Failed)); | |
8460 | ||
6675552b JS |
8461 | -- Mark the entity of N "non-null" except when assertions are enabled - |
8462 | -- since expansion becomes much more complicated (especially when it | |
8463 | -- comes to contracts) due to the generation of wrappers and wholesale | |
8464 | -- moving of declarations and statements which may happen. | |
8465 | ||
8466 | -- Additionally, it is assumed that extra checks will exist with | |
8467 | -- assertions enabled so some potentially redundant checks are | |
8468 | -- acceptable. | |
8469 | ||
8470 | if not Assertions_Enabled then | |
8471 | Mark_Non_Null; | |
8472 | end if; | |
2820d220 AC |
8473 | end Install_Null_Excluding_Check; |
8474 | ||
7327f5c2 AC |
8475 | ----------------------------------------- |
8476 | -- Install_Primitive_Elaboration_Check -- | |
8477 | ----------------------------------------- | |
8478 | ||
8479 | procedure Install_Primitive_Elaboration_Check (Subp_Body : Node_Id) is | |
7327f5c2 AC |
8480 | function Within_Compilation_Unit_Instance |
8481 | (Subp_Id : Entity_Id) return Boolean; | |
8482 | -- Determine whether subprogram Subp_Id appears within an instance which | |
8483 | -- acts as a compilation unit. | |
8484 | ||
8485 | -------------------------------------- | |
8486 | -- Within_Compilation_Unit_Instance -- | |
8487 | -------------------------------------- | |
8488 | ||
8489 | function Within_Compilation_Unit_Instance | |
8490 | (Subp_Id : Entity_Id) return Boolean | |
8491 | is | |
8492 | Pack : Entity_Id; | |
8493 | ||
8494 | begin | |
8495 | -- Examine the scope chain looking for a compilation-unit-level | |
8496 | -- instance. | |
8497 | ||
8498 | Pack := Scope (Subp_Id); | |
8499 | while Present (Pack) and then Pack /= Standard_Standard loop | |
8500 | if Ekind (Pack) = E_Package | |
8501 | and then Is_Generic_Instance (Pack) | |
8502 | and then Nkind (Parent (Unit_Declaration_Node (Pack))) = | |
8503 | N_Compilation_Unit | |
8504 | then | |
8505 | return True; | |
8506 | end if; | |
8507 | ||
8508 | Pack := Scope (Pack); | |
8509 | end loop; | |
8510 | ||
8511 | return False; | |
8512 | end Within_Compilation_Unit_Instance; | |
8513 | ||
8514 | -- Local declarations | |
8515 | ||
8516 | Context : constant Node_Id := Parent (Subp_Body); | |
8517 | Loc : constant Source_Ptr := Sloc (Subp_Body); | |
8518 | Subp_Id : constant Entity_Id := Unique_Defining_Entity (Subp_Body); | |
8519 | Subp_Decl : constant Node_Id := Unit_Declaration_Node (Subp_Id); | |
8520 | ||
8dce7371 PMR |
8521 | Decls : List_Id; |
8522 | Flag_Id : Entity_Id; | |
8523 | Set_Ins : Node_Id; | |
8524 | Set_Stmt : Node_Id; | |
8525 | Tag_Typ : Entity_Id; | |
7327f5c2 AC |
8526 | |
8527 | -- Start of processing for Install_Primitive_Elaboration_Check | |
8528 | ||
8529 | begin | |
8530 | -- Do not generate an elaboration check in compilation modes where | |
8531 | -- expansion is not desirable. | |
8532 | ||
65f1ca2e | 8533 | if GNATprove_Mode then |
7327f5c2 AC |
8534 | return; |
8535 | ||
85be939e AC |
8536 | -- Do not generate an elaboration check if all checks have been |
8537 | -- suppressed. | |
304757d2 | 8538 | |
85be939e | 8539 | elsif Suppress_Checks then |
304757d2 AC |
8540 | return; |
8541 | ||
7327f5c2 | 8542 | -- Do not generate an elaboration check if the related subprogram is |
29d39651 | 8543 | -- not subject to elaboration checks. |
7327f5c2 AC |
8544 | |
8545 | elsif Elaboration_Checks_Suppressed (Subp_Id) then | |
8546 | return; | |
85be939e AC |
8547 | |
8548 | -- Do not generate an elaboration check if such code is not desirable | |
8549 | ||
8550 | elsif Restriction_Active (No_Elaboration_Code) then | |
8551 | return; | |
7327f5c2 | 8552 | |
29d39651 BD |
8553 | -- If pragma Pure or Preelaborate applies, then these elaboration checks |
8554 | -- cannot fail, so do not generate them. | |
8555 | ||
8556 | elsif In_Preelaborated_Unit then | |
8557 | return; | |
8558 | ||
640ad9c2 HK |
8559 | -- Do not generate an elaboration check if exceptions cannot be used, |
8560 | -- caught, or propagated. | |
8561 | ||
8562 | elsif not Exceptions_OK then | |
8563 | return; | |
8564 | ||
29d39651 BD |
8565 | -- Do not consider subprograms that are compilation units, because they |
8566 | -- cannot be the target of a dispatching call. | |
7327f5c2 AC |
8567 | |
8568 | elsif Nkind (Context) = N_Compilation_Unit then | |
8569 | return; | |
8570 | ||
8dce7371 PMR |
8571 | -- Do not consider anything other than nonabstract library-level source |
8572 | -- primitives. | |
7327f5c2 AC |
8573 | |
8574 | elsif not | |
8575 | (Comes_From_Source (Subp_Id) | |
8576 | and then Is_Library_Level_Entity (Subp_Id) | |
8577 | and then Is_Primitive (Subp_Id) | |
8578 | and then not Is_Abstract_Subprogram (Subp_Id)) | |
8579 | then | |
8580 | return; | |
8581 | ||
8582 | -- Do not consider inlined primitives, because once the body is inlined | |
8583 | -- the reference to the elaboration flag will be out of place and will | |
8584 | -- result in an undefined symbol. | |
8585 | ||
8586 | elsif Is_Inlined (Subp_Id) or else Has_Pragma_Inline (Subp_Id) then | |
8587 | return; | |
8588 | ||
8589 | -- Do not generate a duplicate elaboration check. This happens only in | |
8590 | -- the case of primitives completed by an expression function, as the | |
8591 | -- corresponding body is apparently analyzed and expanded twice. | |
8592 | ||
8593 | elsif Analyzed (Subp_Body) then | |
8594 | return; | |
8595 | ||
29d39651 BD |
8596 | -- Do not consider primitives that occur within an instance that is a |
8597 | -- compilation unit. Such an instance defines its spec and body out of | |
8598 | -- order (body is first) within the tree, which causes the reference to | |
8599 | -- the elaboration flag to appear as an undefined symbol. | |
7327f5c2 AC |
8600 | |
8601 | elsif Within_Compilation_Unit_Instance (Subp_Id) then | |
8602 | return; | |
8603 | end if; | |
8604 | ||
8605 | Tag_Typ := Find_Dispatching_Type (Subp_Id); | |
8606 | ||
8607 | -- Only tagged primitives may be the target of a dispatching call | |
8608 | ||
8609 | if No (Tag_Typ) then | |
8610 | return; | |
8611 | ||
8612 | -- Do not consider finalization-related primitives, because they may | |
8613 | -- need to be called while elaboration is taking place. | |
8614 | ||
8615 | elsif Is_Controlled (Tag_Typ) | |
4a08c95c AC |
8616 | and then |
8617 | Chars (Subp_Id) in Name_Adjust | Name_Finalize | Name_Initialize | |
7327f5c2 AC |
8618 | then |
8619 | return; | |
8620 | end if; | |
8621 | ||
8622 | -- Create the declaration of the elaboration flag. The name carries a | |
8623 | -- unique counter in case of name overloading. | |
8624 | ||
8625 | Flag_Id := | |
8626 | Make_Defining_Identifier (Loc, | |
90e491a7 | 8627 | Chars => New_External_Name (Chars (Subp_Id), 'E', -1)); |
7327f5c2 AC |
8628 | Set_Is_Frozen (Flag_Id); |
8629 | ||
8630 | -- Insert the declaration of the elaboration flag in front of the | |
8631 | -- primitive spec and analyze it in the proper context. | |
8632 | ||
8633 | Push_Scope (Scope (Subp_Id)); | |
8634 | ||
8635 | -- Generate: | |
90e491a7 | 8636 | -- E : Boolean := False; |
7327f5c2 AC |
8637 | |
8638 | Insert_Action (Subp_Decl, | |
8639 | Make_Object_Declaration (Loc, | |
8640 | Defining_Identifier => Flag_Id, | |
8641 | Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc), | |
8642 | Expression => New_Occurrence_Of (Standard_False, Loc))); | |
8643 | Pop_Scope; | |
8644 | ||
8645 | -- Prevent the compiler from optimizing the elaboration check by killing | |
8646 | -- the current value of the flag and the associated assignment. | |
8647 | ||
8648 | Set_Current_Value (Flag_Id, Empty); | |
8649 | Set_Last_Assignment (Flag_Id, Empty); | |
8650 | ||
8651 | -- Add a check at the top of the body declarations to ensure that the | |
8652 | -- elaboration flag has been set. | |
8653 | ||
8654 | Decls := Declarations (Subp_Body); | |
8655 | ||
8656 | if No (Decls) then | |
8657 | Decls := New_List; | |
8658 | Set_Declarations (Subp_Body, Decls); | |
8659 | end if; | |
8660 | ||
8661 | -- Generate: | |
8662 | -- if not F then | |
8663 | -- raise Program_Error with "access before elaboration"; | |
8664 | -- end if; | |
8665 | ||
8666 | Prepend_To (Decls, | |
8667 | Make_Raise_Program_Error (Loc, | |
8668 | Condition => | |
8669 | Make_Op_Not (Loc, | |
8670 | Right_Opnd => New_Occurrence_Of (Flag_Id, Loc)), | |
8671 | Reason => PE_Access_Before_Elaboration)); | |
8672 | ||
8673 | Analyze (First (Decls)); | |
8674 | ||
8675 | -- Set the elaboration flag once the body has been elaborated. Insert | |
8676 | -- the statement after the subprogram stub when the primitive body is | |
8677 | -- a subunit. | |
8678 | ||
8679 | if Nkind (Context) = N_Subunit then | |
8680 | Set_Ins := Corresponding_Stub (Context); | |
8681 | else | |
8682 | Set_Ins := Subp_Body; | |
8683 | end if; | |
8684 | ||
8685 | -- Generate: | |
90e491a7 | 8686 | -- E := True; |
7327f5c2 | 8687 | |
8dce7371 | 8688 | Set_Stmt := |
7327f5c2 AC |
8689 | Make_Assignment_Statement (Loc, |
8690 | Name => New_Occurrence_Of (Flag_Id, Loc), | |
8dce7371 PMR |
8691 | Expression => New_Occurrence_Of (Standard_True, Loc)); |
8692 | ||
8693 | -- Mark the assignment statement as elaboration code. This allows the | |
8694 | -- early call region mechanism (see Sem_Elab) to properly ignore such | |
8695 | -- assignments even though they are non-preelaborable code. | |
8696 | ||
8697 | Set_Is_Elaboration_Code (Set_Stmt); | |
8698 | ||
8699 | Insert_After_And_Analyze (Set_Ins, Set_Stmt); | |
7327f5c2 AC |
8700 | end Install_Primitive_Elaboration_Check; |
8701 | ||
70482933 RK |
8702 | -------------------------- |
8703 | -- Install_Static_Check -- | |
8704 | -------------------------- | |
8705 | ||
8706 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
edab6088 | 8707 | Stat : constant Boolean := Is_OK_Static_Expression (R_Cno); |
70482933 RK |
8708 | Typ : constant Entity_Id := Etype (R_Cno); |
8709 | ||
8710 | begin | |
07fc65c4 GB |
8711 | Rewrite (R_Cno, |
8712 | Make_Raise_Constraint_Error (Loc, | |
8713 | Reason => CE_Range_Check_Failed)); | |
70482933 RK |
8714 | Set_Analyzed (R_Cno); |
8715 | Set_Etype (R_Cno, Typ); | |
8716 | Set_Raises_Constraint_Error (R_Cno); | |
8717 | Set_Is_Static_Expression (R_Cno, Stat); | |
3f92c93b AC |
8718 | |
8719 | -- Now deal with possible local raise handling | |
8720 | ||
8721 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
70482933 RK |
8722 | end Install_Static_Check; |
8723 | ||
acad3c0a AC |
8724 | ------------------------- |
8725 | -- Is_Check_Suppressed -- | |
8726 | ------------------------- | |
8727 | ||
8728 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
8729 | Ptr : Suppress_Stack_Entry_Ptr; | |
8730 | ||
8731 | begin | |
8732 | -- First search the local entity suppress stack. We search this from the | |
8733 | -- top of the stack down so that we get the innermost entry that applies | |
8734 | -- to this case if there are nested entries. | |
8735 | ||
8736 | Ptr := Local_Suppress_Stack_Top; | |
8737 | while Ptr /= null loop | |
8738 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8739 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8740 | then | |
8741 | return Ptr.Suppress; | |
8742 | end if; | |
8743 | ||
8744 | Ptr := Ptr.Prev; | |
8745 | end loop; | |
8746 | ||
8747 | -- Now search the global entity suppress table for a matching entry. | |
8748 | -- We also search this from the top down so that if there are multiple | |
8749 | -- pragmas for the same entity, the last one applies (not clear what | |
8750 | -- or whether the RM specifies this handling, but it seems reasonable). | |
8751 | ||
8752 | Ptr := Global_Suppress_Stack_Top; | |
8753 | while Ptr /= null loop | |
8754 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
8755 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
8756 | then | |
8757 | return Ptr.Suppress; | |
8758 | end if; | |
8759 | ||
8760 | Ptr := Ptr.Prev; | |
8761 | end loop; | |
8762 | ||
8763 | -- If we did not find a matching entry, then use the normal scope | |
8764 | -- suppress value after all (actually this will be the global setting | |
8765 | -- since it clearly was not overridden at any point). For a predefined | |
8766 | -- check, we test the specific flag. For a user defined check, we check | |
8767 | -- the All_Checks flag. The Overflow flag requires special handling to | |
90e491a7 | 8768 | -- deal with the General vs Assertion case. |
acad3c0a AC |
8769 | |
8770 | if C = Overflow_Check then | |
8771 | return Overflow_Checks_Suppressed (Empty); | |
90e491a7 | 8772 | |
acad3c0a AC |
8773 | elsif C in Predefined_Check_Id then |
8774 | return Scope_Suppress.Suppress (C); | |
90e491a7 | 8775 | |
acad3c0a AC |
8776 | else |
8777 | return Scope_Suppress.Suppress (All_Checks); | |
8778 | end if; | |
8779 | end Is_Check_Suppressed; | |
8780 | ||
fbf5a39b AC |
8781 | --------------------- |
8782 | -- Kill_All_Checks -- | |
8783 | --------------------- | |
8784 | ||
8785 | procedure Kill_All_Checks is | |
8786 | begin | |
8787 | if Debug_Flag_CC then | |
8788 | w ("Kill_All_Checks"); | |
8789 | end if; | |
8790 | ||
675d6070 TQ |
8791 | -- We reset the number of saved checks to zero, and also modify all |
8792 | -- stack entries for statement ranges to indicate that the number of | |
8793 | -- checks at each level is now zero. | |
fbf5a39b AC |
8794 | |
8795 | Num_Saved_Checks := 0; | |
8796 | ||
67ce0d7e RD |
8797 | -- Note: the Int'Min here avoids any possibility of J being out of |
8798 | -- range when called from e.g. Conditional_Statements_Begin. | |
8799 | ||
8800 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
fbf5a39b AC |
8801 | Saved_Checks_Stack (J) := 0; |
8802 | end loop; | |
8803 | end Kill_All_Checks; | |
8804 | ||
8805 | ----------------- | |
8806 | -- Kill_Checks -- | |
8807 | ----------------- | |
8808 | ||
8809 | procedure Kill_Checks (V : Entity_Id) is | |
8810 | begin | |
8811 | if Debug_Flag_CC then | |
8812 | w ("Kill_Checks for entity", Int (V)); | |
8813 | end if; | |
8814 | ||
8815 | for J in 1 .. Num_Saved_Checks loop | |
8816 | if Saved_Checks (J).Entity = V then | |
8817 | if Debug_Flag_CC then | |
8818 | w (" Checks killed for saved check ", J); | |
8819 | end if; | |
8820 | ||
8821 | Saved_Checks (J).Killed := True; | |
8822 | end if; | |
8823 | end loop; | |
8824 | end Kill_Checks; | |
8825 | ||
70482933 RK |
8826 | ------------------------------ |
8827 | -- Length_Checks_Suppressed -- | |
8828 | ------------------------------ | |
8829 | ||
8830 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8831 | begin | |
fbf5a39b AC |
8832 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
8833 | return Is_Check_Suppressed (E, Length_Check); | |
8834 | else | |
3217f71e | 8835 | return Scope_Suppress.Suppress (Length_Check); |
fbf5a39b | 8836 | end if; |
70482933 RK |
8837 | end Length_Checks_Suppressed; |
8838 | ||
acad3c0a AC |
8839 | ----------------------- |
8840 | -- Make_Bignum_Block -- | |
8841 | ----------------------- | |
8842 | ||
8843 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
8844 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
acad3c0a AC |
8845 | begin |
8846 | return | |
8847 | Make_Block_Statement (Loc, | |
8e888920 AC |
8848 | Declarations => |
8849 | New_List (Build_SS_Mark_Call (Loc, M)), | |
acad3c0a AC |
8850 | Handled_Statement_Sequence => |
8851 | Make_Handled_Sequence_Of_Statements (Loc, | |
8e888920 | 8852 | Statements => New_List (Build_SS_Release_Call (Loc, M)))); |
acad3c0a AC |
8853 | end Make_Bignum_Block; |
8854 | ||
a7f1b24f RD |
8855 | ---------------------------------- |
8856 | -- Minimize_Eliminate_Overflows -- | |
8857 | ---------------------------------- | |
acad3c0a | 8858 | |
b6b5cca8 AC |
8859 | -- This is a recursive routine that is called at the top of an expression |
8860 | -- tree to properly process overflow checking for a whole subtree by making | |
8861 | -- recursive calls to process operands. This processing may involve the use | |
8862 | -- of bignum or long long integer arithmetic, which will change the types | |
8863 | -- of operands and results. That's why we can't do this bottom up (since | |
5707e389 | 8864 | -- it would interfere with semantic analysis). |
b6b5cca8 | 8865 | |
5707e389 | 8866 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
a7f1b24f RD |
8867 | -- the operator expansion routines, as well as the expansion routines for |
8868 | -- if/case expression, do nothing (for the moment) except call the routine | |
8869 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
8870 | -- routine does nothing for non top-level nodes, so at the point where the | |
8871 | -- call is made for the top level node, the entire expression subtree has | |
8872 | -- not been expanded, or processed for overflow. All that has to happen as | |
8873 | -- a result of the top level call to this routine. | |
b6b5cca8 AC |
8874 | |
8875 | -- As noted above, the overflow processing works by making recursive calls | |
8876 | -- for the operands, and figuring out what to do, based on the processing | |
8877 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
8878 | -- to be done in bignum mode), and the determined ranges of the operands. | |
8879 | ||
8880 | -- After possible rewriting of a constituent subexpression node, a call is | |
a40ada7e | 8881 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
5707e389 AC |
8882 | -- the node (if it has been modified by the overflow check processing). The |
8883 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
8884 | -- a recursive call into the whole overflow apparatus, an important rule | |
a7f1b24f RD |
8885 | -- for this call is that the overflow handling mode must be temporarily set |
8886 | -- to STRICT. | |
b6b5cca8 | 8887 | |
a7f1b24f | 8888 | procedure Minimize_Eliminate_Overflows |
c7e152b5 AC |
8889 | (N : Node_Id; |
8890 | Lo : out Uint; | |
8891 | Hi : out Uint; | |
8892 | Top_Level : Boolean) | |
acad3c0a | 8893 | is |
4b1c4f20 RD |
8894 | Rtyp : constant Entity_Id := Etype (N); |
8895 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
8896 | -- Result type, must be a signed integer type | |
acad3c0a | 8897 | |
15c94a55 | 8898 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
acad3c0a AC |
8899 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
8900 | ||
8901 | Loc : constant Source_Ptr := Sloc (N); | |
8902 | ||
8903 | Rlo, Rhi : Uint; | |
4b1c4f20 | 8904 | -- Ranges of values for right operand (operator case) |
acad3c0a | 8905 | |
a6d25cad AC |
8906 | Llo : Uint := No_Uint; -- initialize to prevent warning |
8907 | Lhi : Uint := No_Uint; -- initialize to prevent warning | |
4b1c4f20 | 8908 | -- Ranges of values for left operand (operator case) |
acad3c0a | 8909 | |
d79059a3 AC |
8910 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
8911 | -- Operands and results are of this type when we convert | |
8912 | ||
4b1c4f20 RD |
8913 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
8914 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
acad3c0a AC |
8915 | -- Bounds of Long_Long_Integer |
8916 | ||
8917 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
8918 | -- Indicates binary operator case | |
8919 | ||
8920 | OK : Boolean; | |
8921 | -- Used in call to Determine_Range | |
8922 | ||
c7e152b5 AC |
8923 | Bignum_Operands : Boolean; |
8924 | -- Set True if one or more operands is already of type Bignum, meaning | |
8925 | -- that for sure (regardless of Top_Level setting) we are committed to | |
4b1c4f20 | 8926 | -- doing the operation in Bignum mode (or in the case of a case or if |
5707e389 | 8927 | -- expression, converting all the dependent expressions to Bignum). |
4b1c4f20 RD |
8928 | |
8929 | Long_Long_Integer_Operands : Boolean; | |
5707e389 | 8930 | -- Set True if one or more operands is already of type Long_Long_Integer |
4b1c4f20 RD |
8931 | -- which means that if the result is known to be in the result type |
8932 | -- range, then we must convert such operands back to the result type. | |
a7f1b24f RD |
8933 | |
8934 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
8935 | -- This is called when we have modified the node and we therefore need | |
8936 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
8937 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
a90bd866 | 8938 | -- we would reenter this routine recursively which would not be good. |
a7f1b24f RD |
8939 | -- The argument Suppress is set True if we also want to suppress |
8940 | -- overflow checking for the reexpansion (this is set when we know | |
8941 | -- overflow is not possible). Typ is the type for the reanalysis. | |
8942 | ||
8943 | procedure Reexpand (Suppress : Boolean := False); | |
8944 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
8945 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
8946 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
8947 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
8948 | -- Note that skipping reanalysis is not just an optimization, testing | |
8949 | -- has showed up several complex cases in which reanalyzing an already | |
8950 | -- analyzed node causes incorrect behavior. | |
a40ada7e | 8951 | |
4b1c4f20 RD |
8952 | function In_Result_Range return Boolean; |
8953 | -- Returns True iff Lo .. Hi are within range of the result type | |
c7e152b5 | 8954 | |
a91e9ac7 | 8955 | procedure Max (A : in out Uint; B : Uint); |
5707e389 | 8956 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
a91e9ac7 AC |
8957 | |
8958 | procedure Min (A : in out Uint; B : Uint); | |
5707e389 | 8959 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
a91e9ac7 | 8960 | |
4b1c4f20 RD |
8961 | --------------------- |
8962 | -- In_Result_Range -- | |
8963 | --------------------- | |
8964 | ||
8965 | function In_Result_Range return Boolean is | |
8966 | begin | |
2175b50b | 8967 | if No (Lo) or else No (Hi) then |
b6b5cca8 AC |
8968 | return False; |
8969 | ||
edab6088 | 8970 | elsif Is_OK_Static_Subtype (Etype (N)) then |
4b1c4f20 RD |
8971 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
8972 | and then | |
8973 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
b6b5cca8 | 8974 | |
4b1c4f20 RD |
8975 | else |
8976 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
8977 | and then | |
8978 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
8979 | end if; | |
8980 | end In_Result_Range; | |
8981 | ||
a91e9ac7 AC |
8982 | --------- |
8983 | -- Max -- | |
8984 | --------- | |
8985 | ||
8986 | procedure Max (A : in out Uint; B : Uint) is | |
8987 | begin | |
2175b50b | 8988 | if No (A) or else B > A then |
a91e9ac7 AC |
8989 | A := B; |
8990 | end if; | |
8991 | end Max; | |
8992 | ||
8993 | --------- | |
8994 | -- Min -- | |
8995 | --------- | |
8996 | ||
8997 | procedure Min (A : in out Uint; B : Uint) is | |
8998 | begin | |
2175b50b | 8999 | if No (A) or else B < A then |
a91e9ac7 AC |
9000 | A := B; |
9001 | end if; | |
9002 | end Min; | |
9003 | ||
a7f1b24f RD |
9004 | --------------- |
9005 | -- Reanalyze -- | |
9006 | --------------- | |
9007 | ||
9008 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
15c94a55 RD |
9009 | Svg : constant Overflow_Mode_Type := |
9010 | Scope_Suppress.Overflow_Mode_General; | |
9011 | Sva : constant Overflow_Mode_Type := | |
9012 | Scope_Suppress.Overflow_Mode_Assertions; | |
a7f1b24f RD |
9013 | Svo : constant Boolean := |
9014 | Scope_Suppress.Suppress (Overflow_Check); | |
9015 | ||
9016 | begin | |
15c94a55 RD |
9017 | Scope_Suppress.Overflow_Mode_General := Strict; |
9018 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
a7f1b24f RD |
9019 | |
9020 | if Suppress then | |
9021 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
9022 | end if; | |
9023 | ||
9024 | Analyze_And_Resolve (N, Typ); | |
9025 | ||
fed8bd87 RD |
9026 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
9027 | Scope_Suppress.Overflow_Mode_General := Svg; | |
9028 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
a7f1b24f RD |
9029 | end Reanalyze; |
9030 | ||
a40ada7e RD |
9031 | -------------- |
9032 | -- Reexpand -- | |
9033 | -------------- | |
9034 | ||
a7f1b24f | 9035 | procedure Reexpand (Suppress : Boolean := False) is |
15c94a55 RD |
9036 | Svg : constant Overflow_Mode_Type := |
9037 | Scope_Suppress.Overflow_Mode_General; | |
9038 | Sva : constant Overflow_Mode_Type := | |
9039 | Scope_Suppress.Overflow_Mode_Assertions; | |
a7f1b24f RD |
9040 | Svo : constant Boolean := |
9041 | Scope_Suppress.Suppress (Overflow_Check); | |
9042 | ||
a40ada7e | 9043 | begin |
15c94a55 RD |
9044 | Scope_Suppress.Overflow_Mode_General := Strict; |
9045 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
a40ada7e | 9046 | Set_Analyzed (N, False); |
a7f1b24f RD |
9047 | |
9048 | if Suppress then | |
9049 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
9050 | end if; | |
9051 | ||
a40ada7e | 9052 | Expand (N); |
a7f1b24f | 9053 | |
fed8bd87 RD |
9054 | Scope_Suppress.Suppress (Overflow_Check) := Svo; |
9055 | Scope_Suppress.Overflow_Mode_General := Svg; | |
9056 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
a40ada7e RD |
9057 | end Reexpand; |
9058 | ||
a7f1b24f | 9059 | -- Start of processing for Minimize_Eliminate_Overflows |
a91e9ac7 | 9060 | |
acad3c0a | 9061 | begin |
bc3fb397 AC |
9062 | -- Default initialize Lo and Hi since these are not guaranteed to be |
9063 | -- set otherwise. | |
9064 | ||
9065 | Lo := No_Uint; | |
9066 | Hi := No_Uint; | |
9067 | ||
4b1c4f20 | 9068 | -- Case where we do not have a signed integer arithmetic operation |
acad3c0a AC |
9069 | |
9070 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
9071 | ||
9072 | -- Use the normal Determine_Range routine to get the range. We | |
9073 | -- don't require operands to be valid, invalid values may result in | |
9074 | -- rubbish results where the result has not been properly checked for | |
a90bd866 | 9075 | -- overflow, that's fine. |
acad3c0a AC |
9076 | |
9077 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
9078 | ||
5707e389 | 9079 | -- If Determine_Range did not work (can this in fact happen? Not |
acad3c0a AC |
9080 | -- clear but might as well protect), use type bounds. |
9081 | ||
9082 | if not OK then | |
9083 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
9084 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
9085 | end if; | |
9086 | ||
9087 | -- If we don't have a binary operator, all we have to do is to set | |
637a41a5 | 9088 | -- the Hi/Lo range, so we are done. |
acad3c0a AC |
9089 | |
9090 | return; | |
9091 | ||
4b1c4f20 RD |
9092 | -- Processing for if expression |
9093 | ||
9b16cb57 | 9094 | elsif Nkind (N) = N_If_Expression then |
4b1c4f20 RD |
9095 | declare |
9096 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
9097 | Else_DE : constant Node_Id := Next (Then_DE); | |
9098 | ||
9099 | begin | |
9100 | Bignum_Operands := False; | |
9101 | ||
a7f1b24f | 9102 | Minimize_Eliminate_Overflows |
4b1c4f20 RD |
9103 | (Then_DE, Lo, Hi, Top_Level => False); |
9104 | ||
2175b50b | 9105 | if No (Lo) then |
4b1c4f20 RD |
9106 | Bignum_Operands := True; |
9107 | end if; | |
9108 | ||
a7f1b24f | 9109 | Minimize_Eliminate_Overflows |
4b1c4f20 RD |
9110 | (Else_DE, Rlo, Rhi, Top_Level => False); |
9111 | ||
2175b50b | 9112 | if No (Rlo) then |
4b1c4f20 RD |
9113 | Bignum_Operands := True; |
9114 | else | |
9115 | Long_Long_Integer_Operands := | |
9116 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
9117 | ||
9118 | Min (Lo, Rlo); | |
9119 | Max (Hi, Rhi); | |
9120 | end if; | |
9121 | ||
5707e389 AC |
9122 | -- If at least one of our operands is now Bignum, we must rebuild |
9123 | -- the if expression to use Bignum operands. We will analyze the | |
4b1c4f20 | 9124 | -- rebuilt if expression with overflow checks off, since once we |
a90bd866 | 9125 | -- are in bignum mode, we are all done with overflow checks. |
4b1c4f20 RD |
9126 | |
9127 | if Bignum_Operands then | |
9128 | Rewrite (N, | |
9b16cb57 | 9129 | Make_If_Expression (Loc, |
4b1c4f20 RD |
9130 | Expressions => New_List ( |
9131 | Remove_Head (Expressions (N)), | |
9132 | Convert_To_Bignum (Then_DE), | |
9133 | Convert_To_Bignum (Else_DE)), | |
9134 | Is_Elsif => Is_Elsif (N))); | |
9135 | ||
a7f1b24f | 9136 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
4b1c4f20 RD |
9137 | |
9138 | -- If we have no Long_Long_Integer operands, then we are in result | |
9139 | -- range, since it means that none of our operands felt the need | |
9140 | -- to worry about overflow (otherwise it would have already been | |
a40ada7e RD |
9141 | -- converted to long long integer or bignum). We reexpand to |
9142 | -- complete the expansion of the if expression (but we do not | |
9143 | -- need to reanalyze). | |
4b1c4f20 RD |
9144 | |
9145 | elsif not Long_Long_Integer_Operands then | |
9146 | Set_Do_Overflow_Check (N, False); | |
a7f1b24f | 9147 | Reexpand; |
4b1c4f20 RD |
9148 | |
9149 | -- Otherwise convert us to long long integer mode. Note that we | |
9150 | -- don't need any further overflow checking at this level. | |
9151 | ||
9152 | else | |
9153 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
9154 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
9155 | Set_Etype (N, LLIB); | |
b6b5cca8 AC |
9156 | |
9157 | -- Now reanalyze with overflow checks off | |
9158 | ||
4b1c4f20 | 9159 | Set_Do_Overflow_Check (N, False); |
a7f1b24f | 9160 | Reanalyze (LLIB, Suppress => True); |
4b1c4f20 RD |
9161 | end if; |
9162 | end; | |
9163 | ||
9164 | return; | |
9165 | ||
9166 | -- Here for case expression | |
9167 | ||
9168 | elsif Nkind (N) = N_Case_Expression then | |
9169 | Bignum_Operands := False; | |
9170 | Long_Long_Integer_Operands := False; | |
4b1c4f20 RD |
9171 | |
9172 | declare | |
b6b5cca8 | 9173 | Alt : Node_Id; |
4b1c4f20 RD |
9174 | |
9175 | begin | |
9176 | -- Loop through expressions applying recursive call | |
9177 | ||
9178 | Alt := First (Alternatives (N)); | |
9179 | while Present (Alt) loop | |
9180 | declare | |
9181 | Aexp : constant Node_Id := Expression (Alt); | |
9182 | ||
9183 | begin | |
a7f1b24f | 9184 | Minimize_Eliminate_Overflows |
4b1c4f20 RD |
9185 | (Aexp, Lo, Hi, Top_Level => False); |
9186 | ||
2175b50b | 9187 | if No (Lo) then |
4b1c4f20 RD |
9188 | Bignum_Operands := True; |
9189 | elsif Etype (Aexp) = LLIB then | |
9190 | Long_Long_Integer_Operands := True; | |
9191 | end if; | |
9192 | end; | |
9193 | ||
9194 | Next (Alt); | |
9195 | end loop; | |
9196 | ||
9197 | -- If we have no bignum or long long integer operands, it means | |
9198 | -- that none of our dependent expressions could raise overflow. | |
9199 | -- In this case, we simply return with no changes except for | |
9200 | -- resetting the overflow flag, since we are done with overflow | |
a40ada7e RD |
9201 | -- checks for this node. We will reexpand to get the needed |
9202 | -- expansion for the case expression, but we do not need to | |
5707e389 | 9203 | -- reanalyze, since nothing has changed. |
4b1c4f20 | 9204 | |
b6b5cca8 | 9205 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
4b1c4f20 | 9206 | Set_Do_Overflow_Check (N, False); |
a7f1b24f | 9207 | Reexpand (Suppress => True); |
4b1c4f20 RD |
9208 | |
9209 | -- Otherwise we are going to rebuild the case expression using | |
9210 | -- either bignum or long long integer operands throughout. | |
9211 | ||
9212 | else | |
b6b5cca8 | 9213 | declare |
a6b13d32 | 9214 | Rtype : Entity_Id := Empty; |
b6b5cca8 AC |
9215 | New_Alts : List_Id; |
9216 | New_Exp : Node_Id; | |
9217 | ||
9218 | begin | |
9219 | New_Alts := New_List; | |
9220 | Alt := First (Alternatives (N)); | |
9221 | while Present (Alt) loop | |
9222 | if Bignum_Operands then | |
9223 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
9224 | Rtype := RTE (RE_Bignum); | |
9225 | else | |
9226 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
9227 | Rtype := LLIB; | |
9228 | end if; | |
4b1c4f20 | 9229 | |
b6b5cca8 AC |
9230 | Append_To (New_Alts, |
9231 | Make_Case_Expression_Alternative (Sloc (Alt), | |
b6b5cca8 AC |
9232 | Discrete_Choices => Discrete_Choices (Alt), |
9233 | Expression => New_Exp)); | |
4b1c4f20 | 9234 | |
b6b5cca8 AC |
9235 | Next (Alt); |
9236 | end loop; | |
4b1c4f20 | 9237 | |
b6b5cca8 AC |
9238 | Rewrite (N, |
9239 | Make_Case_Expression (Loc, | |
9240 | Expression => Expression (N), | |
9241 | Alternatives => New_Alts)); | |
4b1c4f20 | 9242 | |
a6b13d32 | 9243 | pragma Assert (Present (Rtype)); |
a7f1b24f | 9244 | Reanalyze (Rtype, Suppress => True); |
b6b5cca8 | 9245 | end; |
4b1c4f20 RD |
9246 | end if; |
9247 | end; | |
9248 | ||
9249 | return; | |
9250 | end if; | |
9251 | ||
9252 | -- If we have an arithmetic operator we make recursive calls on the | |
acad3c0a | 9253 | -- operands to get the ranges (and to properly process the subtree |
637a41a5 | 9254 | -- that lies below us). |
acad3c0a | 9255 | |
a7f1b24f | 9256 | Minimize_Eliminate_Overflows |
4b1c4f20 | 9257 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
acad3c0a | 9258 | |
4b1c4f20 | 9259 | if Binary then |
a7f1b24f | 9260 | Minimize_Eliminate_Overflows |
4b1c4f20 | 9261 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
acad3c0a AC |
9262 | end if; |
9263 | ||
b6b5cca8 AC |
9264 | -- Record if we have Long_Long_Integer operands |
9265 | ||
9266 | Long_Long_Integer_Operands := | |
9267 | Etype (Right_Opnd (N)) = LLIB | |
9268 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
9269 | ||
9270 | -- If either operand is a bignum, then result will be a bignum and we | |
9271 | -- don't need to do any range analysis. As previously discussed we could | |
9272 | -- do range analysis in such cases, but it could mean working with giant | |
9273 | -- numbers at compile time for very little gain (the number of cases | |
5707e389 | 9274 | -- in which we could slip back from bignum mode is small). |
acad3c0a | 9275 | |
2175b50b | 9276 | if No (Rlo) or else (Binary and then No (Llo)) then |
acad3c0a AC |
9277 | Lo := No_Uint; |
9278 | Hi := No_Uint; | |
c7e152b5 | 9279 | Bignum_Operands := True; |
acad3c0a AC |
9280 | |
9281 | -- Otherwise compute result range | |
9282 | ||
9283 | else | |
12be130c EB |
9284 | Compute_Range_For_Arithmetic_Op |
9285 | (Nkind (N), Llo, Lhi, Rlo, Rhi, OK, Lo, Hi); | |
c7e152b5 | 9286 | Bignum_Operands := False; |
acad3c0a AC |
9287 | end if; |
9288 | ||
a40ada7e | 9289 | -- Here for the case where we have not rewritten anything (no bignum |
5707e389 AC |
9290 | -- operands or long long integer operands), and we know the result. |
9291 | -- If we know we are in the result range, and we do not have Bignum | |
9292 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
9293 | -- overflow checks turned off (since we know we cannot have overflow). | |
9294 | -- As always the reexpansion is required to complete expansion of the | |
9295 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
9296 | -- by suppressing the check. | |
b6b5cca8 AC |
9297 | |
9298 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
9299 | and then In_Result_Range | |
9300 | then | |
9301 | Set_Do_Overflow_Check (N, False); | |
a7f1b24f | 9302 | Reexpand (Suppress => True); |
b6b5cca8 AC |
9303 | return; |
9304 | ||
9305 | -- Here we know that we are not in the result range, and in the general | |
5707e389 AC |
9306 | -- case we will move into either the Bignum or Long_Long_Integer domain |
9307 | -- to compute the result. However, there is one exception. If we are | |
9308 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
9309 | -- operands, we will have to immediately convert the result back to | |
9310 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
9311 | -- fiddling. | |
b6b5cca8 AC |
9312 | |
9313 | elsif Top_Level | |
9314 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
2352eadb AC |
9315 | |
9316 | -- One further refinement. If we are at the top level, but our parent | |
9317 | -- is a type conversion, then go into bignum or long long integer node | |
9318 | -- since the result will be converted to that type directly without | |
9319 | -- going through the result type, and we may avoid an overflow. This | |
9320 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
9321 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
9322 | -- but does not fit in Integer. | |
9323 | ||
9324 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
b6b5cca8 | 9325 | then |
a7f1b24f | 9326 | -- Here keep original types, but we need to complete analysis |
b6b5cca8 AC |
9327 | |
9328 | -- One subtlety. We can't just go ahead and do an analyze operation | |
5707e389 AC |
9329 | -- here because it will cause recursion into the whole MINIMIZED/ |
9330 | -- ELIMINATED overflow processing which is not what we want. Here | |
b6b5cca8 | 9331 | -- we are at the top level, and we need a check against the result |
a90bd866 | 9332 | -- mode (i.e. we want to use STRICT mode). So do exactly that. |
a40ada7e RD |
9333 | -- Also, we have not modified the node, so this is a case where |
9334 | -- we need to reexpand, but not reanalyze. | |
b6b5cca8 | 9335 | |
a7f1b24f | 9336 | Reexpand; |
b6b5cca8 AC |
9337 | return; |
9338 | ||
9339 | -- Cases where we do the operation in Bignum mode. This happens either | |
acad3c0a | 9340 | -- because one of our operands is in Bignum mode already, or because |
6cb3037c AC |
9341 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
9342 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
acad3c0a AC |
9343 | |
9344 | -- Note: we could do better here and in some cases switch back from | |
9345 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
9346 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
9347 | -- Failing to do this switching back is only an efficiency issue. | |
9348 | ||
2175b50b | 9349 | elsif No (Lo) or else Lo < LLLo or else Hi > LLHi then |
acad3c0a | 9350 | |
c7e152b5 | 9351 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
b6b5cca8 | 9352 | -- question is whether to move to Bignum mode, or stay in the domain |
c7e152b5 AC |
9353 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
9354 | ||
9355 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
9356 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
9357 | -- into Bignum mode, but there is an exception if neither of our | |
9358 | -- operands is Bignum now, and we are at the top level (Top_Level | |
9359 | -- set True). In this case, there is no point in moving into Bignum | |
9360 | -- mode to prevent overflow if the caller will immediately convert | |
9361 | -- the Bignum value back to LLI with an overflow check. It's more | |
a7f1b24f | 9362 | -- efficient to stay in LLI mode with an overflow check (if needed) |
c7e152b5 AC |
9363 | |
9364 | if Check_Mode = Minimized | |
9365 | or else (Top_Level and not Bignum_Operands) | |
9366 | then | |
a7f1b24f RD |
9367 | if Do_Overflow_Check (N) then |
9368 | Enable_Overflow_Check (N); | |
9369 | end if; | |
acad3c0a | 9370 | |
a7f1b24f RD |
9371 | -- The result now has to be in Long_Long_Integer mode, so adjust |
9372 | -- the possible range to reflect this. Note these calls also | |
9373 | -- change No_Uint values from the top level case to LLI bounds. | |
c7e152b5 AC |
9374 | |
9375 | Max (Lo, LLLo); | |
9376 | Min (Hi, LLHi); | |
9377 | ||
9378 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
acad3c0a AC |
9379 | |
9380 | else | |
9381 | pragma Assert (Check_Mode = Eliminated); | |
9382 | ||
9383 | declare | |
9384 | Fent : Entity_Id; | |
9385 | Args : List_Id; | |
9386 | ||
9387 | begin | |
9388 | case Nkind (N) is | |
d8f43ee6 | 9389 | when N_Op_Abs => |
acad3c0a AC |
9390 | Fent := RTE (RE_Big_Abs); |
9391 | ||
d8f43ee6 | 9392 | when N_Op_Add => |
acad3c0a AC |
9393 | Fent := RTE (RE_Big_Add); |
9394 | ||
d8f43ee6 | 9395 | when N_Op_Divide => |
acad3c0a AC |
9396 | Fent := RTE (RE_Big_Div); |
9397 | ||
d8f43ee6 | 9398 | when N_Op_Expon => |
acad3c0a AC |
9399 | Fent := RTE (RE_Big_Exp); |
9400 | ||
d8f43ee6 | 9401 | when N_Op_Minus => |
acad3c0a AC |
9402 | Fent := RTE (RE_Big_Neg); |
9403 | ||
d8f43ee6 | 9404 | when N_Op_Mod => |
acad3c0a AC |
9405 | Fent := RTE (RE_Big_Mod); |
9406 | ||
9407 | when N_Op_Multiply => | |
9408 | Fent := RTE (RE_Big_Mul); | |
9409 | ||
d8f43ee6 | 9410 | when N_Op_Rem => |
acad3c0a AC |
9411 | Fent := RTE (RE_Big_Rem); |
9412 | ||
9413 | when N_Op_Subtract => | |
9414 | Fent := RTE (RE_Big_Sub); | |
9415 | ||
9416 | -- Anything else is an internal error, this includes the | |
9417 | -- N_Op_Plus case, since how can plus cause the result | |
9418 | -- to be out of range if the operand is in range? | |
9419 | ||
9420 | when others => | |
9421 | raise Program_Error; | |
9422 | end case; | |
9423 | ||
9424 | -- Construct argument list for Bignum call, converting our | |
9425 | -- operands to Bignum form if they are not already there. | |
9426 | ||
9427 | Args := New_List; | |
9428 | ||
9429 | if Binary then | |
9430 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
9431 | end if; | |
9432 | ||
9433 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
9434 | ||
9435 | -- Now rewrite the arithmetic operator with a call to the | |
9436 | -- corresponding bignum function. | |
9437 | ||
9438 | Rewrite (N, | |
9439 | Make_Function_Call (Loc, | |
9440 | Name => New_Occurrence_Of (Fent, Loc), | |
9441 | Parameter_Associations => Args)); | |
a7f1b24f | 9442 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
c7e152b5 AC |
9443 | |
9444 | -- Indicate result is Bignum mode | |
9445 | ||
9446 | Lo := No_Uint; | |
9447 | Hi := No_Uint; | |
6cb3037c | 9448 | return; |
acad3c0a AC |
9449 | end; |
9450 | end if; | |
9451 | ||
9452 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
6cb3037c | 9453 | -- check is required, at least not yet. |
acad3c0a AC |
9454 | |
9455 | else | |
6cb3037c AC |
9456 | Set_Do_Overflow_Check (N, False); |
9457 | end if; | |
acad3c0a | 9458 | |
b6b5cca8 AC |
9459 | -- Here we are not in Bignum territory, but we may have long long |
9460 | -- integer operands that need special handling. First a special check: | |
9461 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
9462 | -- it means we converted it to prevent overflow, but exponentiation | |
9463 | -- requires a Natural right operand, so convert it back to Natural. | |
9464 | -- This conversion may raise an exception which is fine. | |
4b1c4f20 | 9465 | |
b6b5cca8 AC |
9466 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
9467 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
4b1c4f20 RD |
9468 | end if; |
9469 | ||
6cb3037c AC |
9470 | -- Here we will do the operation in Long_Long_Integer. We do this even |
9471 | -- if we know an overflow check is required, better to do this in long | |
a90bd866 | 9472 | -- long integer mode, since we are less likely to overflow. |
acad3c0a | 9473 | |
6cb3037c AC |
9474 | -- Convert right or only operand to Long_Long_Integer, except that |
9475 | -- we do not touch the exponentiation right operand. | |
acad3c0a | 9476 | |
6cb3037c AC |
9477 | if Nkind (N) /= N_Op_Expon then |
9478 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
9479 | end if; | |
acad3c0a | 9480 | |
6cb3037c | 9481 | -- Convert left operand to Long_Long_Integer for binary case |
d79059a3 | 9482 | |
6cb3037c AC |
9483 | if Binary then |
9484 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
9485 | end if; | |
9486 | ||
9487 | -- Reset node to unanalyzed | |
9488 | ||
9489 | Set_Analyzed (N, False); | |
9490 | Set_Etype (N, Empty); | |
9491 | Set_Entity (N, Empty); | |
9492 | ||
a91e9ac7 AC |
9493 | -- Now analyze this new node. This reanalysis will complete processing |
9494 | -- for the node. In particular we will complete the expansion of an | |
9495 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
9496 | -- we will complete any division checks (since we have not changed the | |
9497 | -- setting of the Do_Division_Check flag). | |
acad3c0a | 9498 | |
a7f1b24f | 9499 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
a90bd866 | 9500 | -- MINIMIZED/ELIMINATED handling, since we are now done with that. |
acad3c0a | 9501 | |
a7f1b24f | 9502 | declare |
15c94a55 RD |
9503 | SG : constant Overflow_Mode_Type := |
9504 | Scope_Suppress.Overflow_Mode_General; | |
9505 | SA : constant Overflow_Mode_Type := | |
9506 | Scope_Suppress.Overflow_Mode_Assertions; | |
6cb3037c | 9507 | |
a7f1b24f | 9508 | begin |
15c94a55 RD |
9509 | Scope_Suppress.Overflow_Mode_General := Strict; |
9510 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
6cb3037c | 9511 | |
a7f1b24f RD |
9512 | if not Do_Overflow_Check (N) then |
9513 | Reanalyze (LLIB, Suppress => True); | |
9514 | else | |
9515 | Reanalyze (LLIB); | |
9516 | end if; | |
9517 | ||
15c94a55 RD |
9518 | Scope_Suppress.Overflow_Mode_General := SG; |
9519 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
a7f1b24f RD |
9520 | end; |
9521 | end Minimize_Eliminate_Overflows; | |
acad3c0a AC |
9522 | |
9523 | ------------------------- | |
9524 | -- Overflow_Check_Mode -- | |
9525 | ------------------------- | |
9526 | ||
15c94a55 | 9527 | function Overflow_Check_Mode return Overflow_Mode_Type is |
70482933 | 9528 | begin |
05b34c18 | 9529 | if In_Assertion_Expr = 0 then |
15c94a55 | 9530 | return Scope_Suppress.Overflow_Mode_General; |
fbf5a39b | 9531 | else |
15c94a55 | 9532 | return Scope_Suppress.Overflow_Mode_Assertions; |
fbf5a39b | 9533 | end if; |
acad3c0a AC |
9534 | end Overflow_Check_Mode; |
9535 | ||
9536 | -------------------------------- | |
9537 | -- Overflow_Checks_Suppressed -- | |
9538 | -------------------------------- | |
9539 | ||
9540 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9541 | begin | |
a7f1b24f RD |
9542 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9543 | return Is_Check_Suppressed (E, Overflow_Check); | |
9544 | else | |
9545 | return Scope_Suppress.Suppress (Overflow_Check); | |
9546 | end if; | |
70482933 | 9547 | end Overflow_Checks_Suppressed; |
b568955d | 9548 | |
f1c80977 AC |
9549 | --------------------------------- |
9550 | -- Predicate_Checks_Suppressed -- | |
9551 | --------------------------------- | |
9552 | ||
9553 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9554 | begin | |
9555 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9556 | return Is_Check_Suppressed (E, Predicate_Check); | |
9557 | else | |
9558 | return Scope_Suppress.Suppress (Predicate_Check); | |
9559 | end if; | |
9560 | end Predicate_Checks_Suppressed; | |
9561 | ||
70482933 RK |
9562 | ----------------------------- |
9563 | -- Range_Checks_Suppressed -- | |
9564 | ----------------------------- | |
9565 | ||
9566 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9567 | begin | |
fbf5a39b | 9568 | if Present (E) then |
21c51f53 | 9569 | if Kill_Range_Checks (E) then |
fbf5a39b | 9570 | return True; |
4bd4bb7f | 9571 | |
fbf5a39b AC |
9572 | elsif Checks_May_Be_Suppressed (E) then |
9573 | return Is_Check_Suppressed (E, Range_Check); | |
9574 | end if; | |
9575 | end if; | |
70482933 | 9576 | |
3217f71e | 9577 | return Scope_Suppress.Suppress (Range_Check); |
70482933 RK |
9578 | end Range_Checks_Suppressed; |
9579 | ||
c064e066 RD |
9580 | ----------------------------------------- |
9581 | -- Range_Or_Validity_Checks_Suppressed -- | |
9582 | ----------------------------------------- | |
9583 | ||
9584 | -- Note: the coding would be simpler here if we simply made appropriate | |
9585 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
9586 | -- duplicated checks which we prefer to avoid. | |
9587 | ||
9588 | function Range_Or_Validity_Checks_Suppressed | |
9589 | (Expr : Node_Id) return Boolean | |
9590 | is | |
9591 | begin | |
9592 | -- Immediate return if scope checks suppressed for either check | |
9593 | ||
3217f71e AC |
9594 | if Scope_Suppress.Suppress (Range_Check) |
9595 | or | |
9596 | Scope_Suppress.Suppress (Validity_Check) | |
9597 | then | |
c064e066 RD |
9598 | return True; |
9599 | end if; | |
9600 | ||
9601 | -- If no expression, that's odd, decide that checks are suppressed, | |
9602 | -- since we don't want anyone trying to do checks in this case, which | |
9603 | -- is most likely the result of some other error. | |
9604 | ||
9605 | if No (Expr) then | |
9606 | return True; | |
9607 | end if; | |
9608 | ||
9609 | -- Expression is present, so perform suppress checks on type | |
9610 | ||
9611 | declare | |
9612 | Typ : constant Entity_Id := Etype (Expr); | |
9613 | begin | |
21c51f53 | 9614 | if Checks_May_Be_Suppressed (Typ) |
c064e066 RD |
9615 | and then (Is_Check_Suppressed (Typ, Range_Check) |
9616 | or else | |
9617 | Is_Check_Suppressed (Typ, Validity_Check)) | |
9618 | then | |
9619 | return True; | |
9620 | end if; | |
9621 | end; | |
9622 | ||
9623 | -- If expression is an entity name, perform checks on this entity | |
9624 | ||
9625 | if Is_Entity_Name (Expr) then | |
9626 | declare | |
9627 | Ent : constant Entity_Id := Entity (Expr); | |
9628 | begin | |
9629 | if Checks_May_Be_Suppressed (Ent) then | |
9630 | return Is_Check_Suppressed (Ent, Range_Check) | |
9631 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
9632 | end if; | |
9633 | end; | |
9634 | end if; | |
9635 | ||
9636 | -- If we fall through, no checks suppressed | |
9637 | ||
9638 | return False; | |
9639 | end Range_Or_Validity_Checks_Suppressed; | |
9640 | ||
8cbb664e MG |
9641 | ------------------- |
9642 | -- Remove_Checks -- | |
9643 | ------------------- | |
9644 | ||
9645 | procedure Remove_Checks (Expr : Node_Id) is | |
8cbb664e MG |
9646 | function Process (N : Node_Id) return Traverse_Result; |
9647 | -- Process a single node during the traversal | |
9648 | ||
10303118 BD |
9649 | procedure Traverse is new Traverse_Proc (Process); |
9650 | -- The traversal procedure itself | |
8cbb664e MG |
9651 | |
9652 | ------------- | |
9653 | -- Process -- | |
9654 | ------------- | |
9655 | ||
9656 | function Process (N : Node_Id) return Traverse_Result is | |
9657 | begin | |
9658 | if Nkind (N) not in N_Subexpr then | |
9659 | return Skip; | |
9660 | end if; | |
9661 | ||
9662 | Set_Do_Range_Check (N, False); | |
9663 | ||
9664 | case Nkind (N) is | |
9665 | when N_And_Then => | |
10303118 | 9666 | Traverse (Left_Opnd (N)); |
8cbb664e MG |
9667 | return Skip; |
9668 | ||
9669 | when N_Attribute_Reference => | |
8cbb664e MG |
9670 | Set_Do_Overflow_Check (N, False); |
9671 | ||
8cbb664e MG |
9672 | when N_Op => |
9673 | Set_Do_Overflow_Check (N, False); | |
9674 | ||
9675 | case Nkind (N) is | |
9676 | when N_Op_Divide => | |
9677 | Set_Do_Division_Check (N, False); | |
9678 | ||
9679 | when N_Op_And => | |
9680 | Set_Do_Length_Check (N, False); | |
9681 | ||
9682 | when N_Op_Mod => | |
9683 | Set_Do_Division_Check (N, False); | |
9684 | ||
9685 | when N_Op_Or => | |
9686 | Set_Do_Length_Check (N, False); | |
9687 | ||
9688 | when N_Op_Rem => | |
9689 | Set_Do_Division_Check (N, False); | |
9690 | ||
9691 | when N_Op_Xor => | |
9692 | Set_Do_Length_Check (N, False); | |
9693 | ||
9694 | when others => | |
9695 | null; | |
9696 | end case; | |
9697 | ||
9698 | when N_Or_Else => | |
10303118 | 9699 | Traverse (Left_Opnd (N)); |
8cbb664e MG |
9700 | return Skip; |
9701 | ||
9702 | when N_Selected_Component => | |
8cbb664e MG |
9703 | Set_Do_Discriminant_Check (N, False); |
9704 | ||
8cbb664e | 9705 | when N_Type_Conversion => |
fbf5a39b | 9706 | Set_Do_Length_Check (N, False); |
8cbb664e | 9707 | Set_Do_Overflow_Check (N, False); |
8cbb664e MG |
9708 | |
9709 | when others => | |
9710 | null; | |
9711 | end case; | |
9712 | ||
9713 | return OK; | |
9714 | end Process; | |
9715 | ||
9716 | -- Start of processing for Remove_Checks | |
9717 | ||
9718 | begin | |
10303118 | 9719 | Traverse (Expr); |
8cbb664e MG |
9720 | end Remove_Checks; |
9721 | ||
70482933 RK |
9722 | ---------------------------- |
9723 | -- Selected_Length_Checks -- | |
9724 | ---------------------------- | |
9725 | ||
9726 | function Selected_Length_Checks | |
6c8e70fe | 9727 | (Expr : Node_Id; |
70482933 RK |
9728 | Target_Typ : Entity_Id; |
9729 | Source_Typ : Entity_Id; | |
6b6fcd3e | 9730 | Warn_Node : Node_Id) return Check_Result |
70482933 | 9731 | is |
6c8e70fe | 9732 | Loc : constant Source_Ptr := Sloc (Expr); |
70482933 RK |
9733 | S_Typ : Entity_Id; |
9734 | T_Typ : Entity_Id; | |
9735 | Expr_Actual : Node_Id; | |
9736 | Exptyp : Entity_Id; | |
9737 | Cond : Node_Id := Empty; | |
9738 | Do_Access : Boolean := False; | |
9739 | Wnode : Node_Id := Warn_Node; | |
9740 | Ret_Result : Check_Result := (Empty, Empty); | |
9741 | Num_Checks : Natural := 0; | |
9742 | ||
9743 | procedure Add_Check (N : Node_Id); | |
9744 | -- Adds the action given to Ret_Result if N is non-Empty | |
9745 | ||
9746 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
869a06d9 AC |
9747 | -- Return E'Length (Indx) |
9748 | ||
70482933 | 9749 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; |
869a06d9 | 9750 | -- Return N'Length (Indx) |
70482933 RK |
9751 | |
9752 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
9753 | -- True for equal literals and for nodes that denote the same constant | |
c84700e7 | 9754 | -- entity, even if its value is not a static constant. This includes the |
fbf5a39b | 9755 | -- case of a discriminal reference within an init proc. Removes some |
c84700e7 | 9756 | -- obviously superfluous checks. |
70482933 RK |
9757 | |
9758 | function Length_E_Cond | |
9759 | (Exptyp : Entity_Id; | |
9760 | Typ : Entity_Id; | |
6b6fcd3e | 9761 | Indx : Nat) return Node_Id; |
70482933 RK |
9762 | -- Returns expression to compute: |
9763 | -- Typ'Length /= Exptyp'Length | |
9764 | ||
9765 | function Length_N_Cond | |
6c8e70fe | 9766 | (Exp : Node_Id; |
70482933 | 9767 | Typ : Entity_Id; |
6b6fcd3e | 9768 | Indx : Nat) return Node_Id; |
70482933 | 9769 | -- Returns expression to compute: |
6c8e70fe | 9770 | -- Typ'Length /= Exp'Length |
70482933 | 9771 | |
eb6b9c9b | 9772 | function Length_Mismatch_Info_Message |
65d76c55 BD |
9773 | (Left_Element_Count : Unat; |
9774 | Right_Element_Count : Unat) return String; | |
eb6b9c9b GD |
9775 | -- Returns a message indicating how many elements were expected |
9776 | -- (Left_Element_Count) and how many were found (Right_Element_Count). | |
9777 | ||
70482933 RK |
9778 | --------------- |
9779 | -- Add_Check -- | |
9780 | --------------- | |
9781 | ||
9782 | procedure Add_Check (N : Node_Id) is | |
9783 | begin | |
9784 | if Present (N) then | |
9785 | ||
869a06d9 AC |
9786 | -- We do not support inserting more than 2 checks on the same |
9787 | -- node. If this happens it means we have already added an | |
9788 | -- unconditional raise, so we can skip the other checks safely | |
9789 | -- since N will always raise an exception. | |
70482933 RK |
9790 | |
9791 | if Num_Checks = 2 then | |
9792 | return; | |
9793 | end if; | |
9794 | ||
9795 | pragma Assert (Num_Checks <= 1); | |
9796 | Num_Checks := Num_Checks + 1; | |
9797 | Ret_Result (Num_Checks) := N; | |
9798 | end if; | |
9799 | end Add_Check; | |
9800 | ||
9801 | ------------------ | |
9802 | -- Get_E_Length -- | |
9803 | ------------------ | |
9804 | ||
9805 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
11b4899f | 9806 | SE : constant Entity_Id := Scope (E); |
70482933 RK |
9807 | N : Node_Id; |
9808 | E1 : Entity_Id := E; | |
70482933 RK |
9809 | |
9810 | begin | |
9811 | if Ekind (Scope (E)) = E_Record_Type | |
9812 | and then Has_Discriminants (Scope (E)) | |
9813 | then | |
9814 | N := Build_Discriminal_Subtype_Of_Component (E); | |
9815 | ||
9816 | if Present (N) then | |
6c8e70fe | 9817 | Insert_Action (Expr, N); |
70482933 RK |
9818 | E1 := Defining_Identifier (N); |
9819 | end if; | |
9820 | end if; | |
9821 | ||
9822 | if Ekind (E1) = E_String_Literal_Subtype then | |
9823 | return | |
9824 | Make_Integer_Literal (Loc, | |
9825 | Intval => String_Literal_Length (E1)); | |
9826 | ||
11b4899f JM |
9827 | elsif SE /= Standard_Standard |
9828 | and then Ekind (Scope (SE)) = E_Protected_Type | |
9829 | and then Has_Discriminants (Scope (SE)) | |
9830 | and then Has_Completion (Scope (SE)) | |
70482933 RK |
9831 | and then not Inside_Init_Proc |
9832 | then | |
70482933 RK |
9833 | -- If the type whose length is needed is a private component |
9834 | -- constrained by a discriminant, we must expand the 'Length | |
9835 | -- attribute into an explicit computation, using the discriminal | |
9836 | -- of the current protected operation. This is because the actual | |
9837 | -- type of the prival is constructed after the protected opera- | |
9838 | -- tion has been fully expanded. | |
9839 | ||
9840 | declare | |
9841 | Indx_Type : Node_Id; | |
7c4f3267 | 9842 | Bounds : Range_Nodes; |
70482933 RK |
9843 | Do_Expand : Boolean := False; |
9844 | ||
9845 | begin | |
9846 | Indx_Type := First_Index (E); | |
9847 | ||
9848 | for J in 1 .. Indx - 1 loop | |
9849 | Next_Index (Indx_Type); | |
9850 | end loop; | |
9851 | ||
7c4f3267 | 9852 | Bounds := Get_Index_Bounds (Indx_Type); |
70482933 | 9853 | |
7c4f3267 BD |
9854 | if Nkind (Bounds.First) = N_Identifier |
9855 | and then Ekind (Entity (Bounds.First)) = E_In_Parameter | |
70482933 | 9856 | then |
7c4f3267 | 9857 | Bounds.First := Get_Discriminal (E, Bounds.First); |
70482933 RK |
9858 | Do_Expand := True; |
9859 | end if; | |
9860 | ||
7c4f3267 BD |
9861 | if Nkind (Bounds.Last) = N_Identifier |
9862 | and then Ekind (Entity (Bounds.Last)) = E_In_Parameter | |
70482933 | 9863 | then |
7c4f3267 | 9864 | Bounds.Last := Get_Discriminal (E, Bounds.Last); |
70482933 RK |
9865 | Do_Expand := True; |
9866 | end if; | |
9867 | ||
9868 | if Do_Expand then | |
7c4f3267 BD |
9869 | if not Is_Entity_Name (Bounds.First) then |
9870 | Bounds.First := | |
9871 | Duplicate_Subexpr_No_Checks (Bounds.First); | |
70482933 RK |
9872 | end if; |
9873 | ||
7c4f3267 BD |
9874 | if not Is_Entity_Name (Bounds.Last) then |
9875 | Bounds.First := Duplicate_Subexpr_No_Checks (Bounds.Last); | |
70482933 RK |
9876 | end if; |
9877 | ||
9878 | N := | |
9879 | Make_Op_Add (Loc, | |
9880 | Left_Opnd => | |
9881 | Make_Op_Subtract (Loc, | |
7c4f3267 BD |
9882 | Left_Opnd => Bounds.Last, |
9883 | Right_Opnd => Bounds.First), | |
70482933 RK |
9884 | |
9885 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
9886 | return N; | |
9887 | ||
9888 | else | |
9889 | N := | |
9890 | Make_Attribute_Reference (Loc, | |
9891 | Attribute_Name => Name_Length, | |
9892 | Prefix => | |
9893 | New_Occurrence_Of (E1, Loc)); | |
9894 | ||
9895 | if Indx > 1 then | |
9896 | Set_Expressions (N, New_List ( | |
9897 | Make_Integer_Literal (Loc, Indx))); | |
9898 | end if; | |
9899 | ||
9900 | return N; | |
9901 | end if; | |
9902 | end; | |
9903 | ||
9904 | else | |
9905 | N := | |
9906 | Make_Attribute_Reference (Loc, | |
9907 | Attribute_Name => Name_Length, | |
9908 | Prefix => | |
9909 | New_Occurrence_Of (E1, Loc)); | |
9910 | ||
9911 | if Indx > 1 then | |
9912 | Set_Expressions (N, New_List ( | |
9913 | Make_Integer_Literal (Loc, Indx))); | |
9914 | end if; | |
9915 | ||
9916 | return N; | |
70482933 RK |
9917 | end if; |
9918 | end Get_E_Length; | |
9919 | ||
9920 | ------------------ | |
9921 | -- Get_N_Length -- | |
9922 | ------------------ | |
9923 | ||
9924 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
9925 | begin | |
9926 | return | |
9927 | Make_Attribute_Reference (Loc, | |
9928 | Attribute_Name => Name_Length, | |
9929 | Prefix => | |
fbf5a39b | 9930 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
70482933 RK |
9931 | Expressions => New_List ( |
9932 | Make_Integer_Literal (Loc, Indx))); | |
70482933 RK |
9933 | end Get_N_Length; |
9934 | ||
9935 | ------------------- | |
9936 | -- Length_E_Cond -- | |
9937 | ------------------- | |
9938 | ||
9939 | function Length_E_Cond | |
9940 | (Exptyp : Entity_Id; | |
9941 | Typ : Entity_Id; | |
6b6fcd3e | 9942 | Indx : Nat) return Node_Id |
70482933 RK |
9943 | is |
9944 | begin | |
9945 | return | |
9946 | Make_Op_Ne (Loc, | |
9947 | Left_Opnd => Get_E_Length (Typ, Indx), | |
9948 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
70482933 RK |
9949 | end Length_E_Cond; |
9950 | ||
9951 | ------------------- | |
9952 | -- Length_N_Cond -- | |
9953 | ------------------- | |
9954 | ||
9955 | function Length_N_Cond | |
6c8e70fe | 9956 | (Exp : Node_Id; |
70482933 | 9957 | Typ : Entity_Id; |
6b6fcd3e | 9958 | Indx : Nat) return Node_Id |
70482933 RK |
9959 | is |
9960 | begin | |
9961 | return | |
9962 | Make_Op_Ne (Loc, | |
9963 | Left_Opnd => Get_E_Length (Typ, Indx), | |
6c8e70fe | 9964 | Right_Opnd => Get_N_Length (Exp, Indx)); |
70482933 RK |
9965 | end Length_N_Cond; |
9966 | ||
eb6b9c9b GD |
9967 | ---------------------------------- |
9968 | -- Length_Mismatch_Info_Message -- | |
9969 | ---------------------------------- | |
9970 | ||
9971 | function Length_Mismatch_Info_Message | |
65d76c55 BD |
9972 | (Left_Element_Count : Unat; |
9973 | Right_Element_Count : Unat) return String | |
eb6b9c9b GD |
9974 | is |
9975 | ||
65d76c55 | 9976 | function Plural_Vs_Singular_Ending (Count : Unat) return String; |
eb6b9c9b GD |
9977 | -- Returns an empty string if Count is 1; otherwise returns "s" |
9978 | ||
65d76c55 | 9979 | function Plural_Vs_Singular_Ending (Count : Unat) return String is |
eb6b9c9b GD |
9980 | begin |
9981 | if Count = 1 then | |
9982 | return ""; | |
9983 | else | |
9984 | return "s"; | |
9985 | end if; | |
9986 | end Plural_Vs_Singular_Ending; | |
9987 | ||
9988 | begin | |
65d76c55 BD |
9989 | return "expected " |
9990 | & UI_Image (Left_Element_Count, Format => Decimal) | |
eb6b9c9b GD |
9991 | & " element" |
9992 | & Plural_Vs_Singular_Ending (Left_Element_Count) | |
65d76c55 BD |
9993 | & "; found " |
9994 | & UI_Image (Right_Element_Count, Format => Decimal) | |
eb6b9c9b GD |
9995 | & " element" |
9996 | & Plural_Vs_Singular_Ending (Right_Element_Count); | |
65d76c55 BD |
9997 | -- "Format => Decimal" above is needed because otherwise UI_Image |
9998 | -- can sometimes return a hexadecimal number 16#...#, but "#" means | |
9999 | -- something special to Errout. A previous version used the default | |
10000 | -- Auto, which was essentially the same bug as documented here: | |
10001 | -- https://xkcd.com/327/ . | |
eb6b9c9b GD |
10002 | end Length_Mismatch_Info_Message; |
10003 | ||
675d6070 TQ |
10004 | ----------------- |
10005 | -- Same_Bounds -- | |
10006 | ----------------- | |
10007 | ||
70482933 RK |
10008 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
10009 | begin | |
10010 | return | |
10011 | (Nkind (L) = N_Integer_Literal | |
10012 | and then Nkind (R) = N_Integer_Literal | |
10013 | and then Intval (L) = Intval (R)) | |
10014 | ||
10015 | or else | |
10016 | (Is_Entity_Name (L) | |
10017 | and then Ekind (Entity (L)) = E_Constant | |
10018 | and then ((Is_Entity_Name (R) | |
10019 | and then Entity (L) = Entity (R)) | |
10020 | or else | |
10021 | (Nkind (R) = N_Type_Conversion | |
10022 | and then Is_Entity_Name (Expression (R)) | |
10023 | and then Entity (L) = Entity (Expression (R))))) | |
10024 | ||
10025 | or else | |
10026 | (Is_Entity_Name (R) | |
10027 | and then Ekind (Entity (R)) = E_Constant | |
10028 | and then Nkind (L) = N_Type_Conversion | |
10029 | and then Is_Entity_Name (Expression (L)) | |
c84700e7 ES |
10030 | and then Entity (R) = Entity (Expression (L))) |
10031 | ||
10032 | or else | |
10033 | (Is_Entity_Name (L) | |
10034 | and then Is_Entity_Name (R) | |
10035 | and then Entity (L) = Entity (R) | |
10036 | and then Ekind (Entity (L)) = E_In_Parameter | |
10037 | and then Inside_Init_Proc); | |
70482933 RK |
10038 | end Same_Bounds; |
10039 | ||
10040 | -- Start of processing for Selected_Length_Checks | |
10041 | ||
10042 | begin | |
66340e0e | 10043 | -- Checks will be applied only when generating code |
27bb7941 | 10044 | |
66340e0e | 10045 | if not Expander_Active then |
70482933 RK |
10046 | return Ret_Result; |
10047 | end if; | |
10048 | ||
10049 | if Target_Typ = Any_Type | |
10050 | or else Target_Typ = Any_Composite | |
6c8e70fe | 10051 | or else Raises_Constraint_Error (Expr) |
70482933 RK |
10052 | then |
10053 | return Ret_Result; | |
10054 | end if; | |
10055 | ||
10056 | if No (Wnode) then | |
6c8e70fe | 10057 | Wnode := Expr; |
70482933 RK |
10058 | end if; |
10059 | ||
10060 | T_Typ := Target_Typ; | |
10061 | ||
10062 | if No (Source_Typ) then | |
6c8e70fe | 10063 | S_Typ := Etype (Expr); |
70482933 RK |
10064 | else |
10065 | S_Typ := Source_Typ; | |
10066 | end if; | |
10067 | ||
10068 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
10069 | return Ret_Result; | |
10070 | end if; | |
10071 | ||
10072 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
10073 | S_Typ := Designated_Type (S_Typ); | |
10074 | T_Typ := Designated_Type (T_Typ); | |
10075 | Do_Access := True; | |
10076 | ||
939c12d2 | 10077 | -- A simple optimization for the null case |
70482933 | 10078 | |
6c8e70fe | 10079 | if Known_Null (Expr) then |
70482933 RK |
10080 | return Ret_Result; |
10081 | end if; | |
10082 | end if; | |
10083 | ||
10084 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
10085 | if Is_Constrained (T_Typ) then | |
10086 | ||
9b16cb57 RD |
10087 | -- The checking code to be generated will freeze the corresponding |
10088 | -- array type. However, we must freeze the type now, so that the | |
10089 | -- freeze node does not appear within the generated if expression, | |
10090 | -- but ahead of it. | |
70482933 | 10091 | |
6c8e70fe | 10092 | Freeze_Before (Expr, T_Typ); |
70482933 | 10093 | |
6c8e70fe EB |
10094 | Expr_Actual := Get_Referenced_Object (Expr); |
10095 | Exptyp := Get_Actual_Subtype (Expr); | |
70482933 RK |
10096 | |
10097 | if Is_Access_Type (Exptyp) then | |
10098 | Exptyp := Designated_Type (Exptyp); | |
10099 | end if; | |
10100 | ||
10101 | -- String_Literal case. This needs to be handled specially be- | |
10102 | -- cause no index types are available for string literals. The | |
10103 | -- condition is simply: | |
10104 | ||
10105 | -- T_Typ'Length = string-literal-length | |
10106 | ||
fbf5a39b AC |
10107 | if Nkind (Expr_Actual) = N_String_Literal |
10108 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
10109 | then | |
70482933 RK |
10110 | Cond := |
10111 | Make_Op_Ne (Loc, | |
10112 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
10113 | Right_Opnd => | |
10114 | Make_Integer_Literal (Loc, | |
10115 | Intval => | |
10116 | String_Literal_Length (Etype (Expr_Actual)))); | |
10117 | ||
10118 | -- General array case. Here we have a usable actual subtype for | |
10119 | -- the expression, and the condition is built from the two types | |
10120 | -- (Do_Length): | |
10121 | ||
10122 | -- T_Typ'Length /= Exptyp'Length or else | |
10123 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
10124 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
10125 | -- ... | |
10126 | ||
10127 | elsif Is_Constrained (Exptyp) then | |
10128 | declare | |
fbf5a39b AC |
10129 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
10130 | ||
10131 | L_Index : Node_Id; | |
10132 | R_Index : Node_Id; | |
7c4f3267 BD |
10133 | L_Bounds : Range_Nodes; |
10134 | R_Bounds : Range_Nodes; | |
70482933 RK |
10135 | L_Length : Uint; |
10136 | R_Length : Uint; | |
fbf5a39b | 10137 | Ref_Node : Node_Id; |
70482933 RK |
10138 | |
10139 | begin | |
675d6070 TQ |
10140 | -- At the library level, we need to ensure that the type of |
10141 | -- the object is elaborated before the check itself is | |
10142 | -- emitted. This is only done if the object is in the | |
10143 | -- current compilation unit, otherwise the type is frozen | |
10144 | -- and elaborated in its unit. | |
fbf5a39b AC |
10145 | |
10146 | if Is_Itype (Exptyp) | |
10147 | and then | |
10148 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
10149 | and then | |
10150 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
891a6e79 | 10151 | and then In_Open_Scopes (Scope (Exptyp)) |
fbf5a39b | 10152 | then |
6c8e70fe | 10153 | Ref_Node := Make_Itype_Reference (Sloc (Expr)); |
fbf5a39b | 10154 | Set_Itype (Ref_Node, Exptyp); |
6c8e70fe | 10155 | Insert_Action (Expr, Ref_Node); |
fbf5a39b AC |
10156 | end if; |
10157 | ||
70482933 RK |
10158 | L_Index := First_Index (T_Typ); |
10159 | R_Index := First_Index (Exptyp); | |
10160 | ||
10161 | for Indx in 1 .. Ndims loop | |
10162 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
07fc65c4 GB |
10163 | or else |
10164 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
70482933 | 10165 | then |
7c4f3267 BD |
10166 | L_Bounds := Get_Index_Bounds (L_Index); |
10167 | R_Bounds := Get_Index_Bounds (R_Index); | |
70482933 RK |
10168 | |
10169 | -- Deal with compile time length check. Note that we | |
10170 | -- skip this in the access case, because the access | |
10171 | -- value may be null, so we cannot know statically. | |
10172 | ||
10173 | if not Do_Access | |
7c4f3267 BD |
10174 | and then Compile_Time_Known_Value (L_Bounds.First) |
10175 | and then Compile_Time_Known_Value (L_Bounds.Last) | |
10176 | and then Compile_Time_Known_Value (R_Bounds.First) | |
10177 | and then Compile_Time_Known_Value (R_Bounds.Last) | |
70482933 | 10178 | then |
7c4f3267 BD |
10179 | if Expr_Value (L_Bounds.Last) >= |
10180 | Expr_Value (L_Bounds.First) | |
10181 | then | |
10182 | L_Length := Expr_Value (L_Bounds.Last) - | |
10183 | Expr_Value (L_Bounds.First) + 1; | |
70482933 RK |
10184 | else |
10185 | L_Length := UI_From_Int (0); | |
10186 | end if; | |
10187 | ||
7c4f3267 BD |
10188 | if Expr_Value (R_Bounds.Last) >= |
10189 | Expr_Value (R_Bounds.First) | |
10190 | then | |
10191 | R_Length := Expr_Value (R_Bounds.Last) - | |
10192 | Expr_Value (R_Bounds.First) + 1; | |
70482933 RK |
10193 | else |
10194 | R_Length := UI_From_Int (0); | |
10195 | end if; | |
10196 | ||
10197 | if L_Length > R_Length then | |
10198 | Add_Check | |
10199 | (Compile_Time_Constraint_Error | |
65d76c55 | 10200 | (Wnode, "too few elements for}!!??", T_Typ, |
eb6b9c9b GD |
10201 | Extra_Msg => Length_Mismatch_Info_Message |
10202 | (L_Length, R_Length))); | |
70482933 | 10203 | |
9fe696a3 | 10204 | elsif L_Length < R_Length then |
70482933 RK |
10205 | Add_Check |
10206 | (Compile_Time_Constraint_Error | |
65d76c55 | 10207 | (Wnode, "too many elements for}!!??", T_Typ, |
eb6b9c9b GD |
10208 | Extra_Msg => Length_Mismatch_Info_Message |
10209 | (L_Length, R_Length))); | |
70482933 RK |
10210 | end if; |
10211 | ||
10212 | -- The comparison for an individual index subtype | |
10213 | -- is omitted if the corresponding index subtypes | |
10214 | -- statically match, since the result is known to | |
10215 | -- be true. Note that this test is worth while even | |
10216 | -- though we do static evaluation, because non-static | |
10217 | -- subtypes can statically match. | |
10218 | ||
10219 | elsif not | |
10220 | Subtypes_Statically_Match | |
10221 | (Etype (L_Index), Etype (R_Index)) | |
10222 | ||
10223 | and then not | |
7c4f3267 BD |
10224 | (Same_Bounds (L_Bounds.First, R_Bounds.First) |
10225 | and then | |
10226 | Same_Bounds (L_Bounds.Last, R_Bounds.Last)) | |
70482933 RK |
10227 | then |
10228 | Evolve_Or_Else | |
10229 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
10230 | end if; | |
10231 | ||
10232 | Next (L_Index); | |
10233 | Next (R_Index); | |
10234 | end if; | |
10235 | end loop; | |
10236 | end; | |
10237 | ||
10238 | -- Handle cases where we do not get a usable actual subtype that | |
10239 | -- is constrained. This happens for example in the function call | |
10240 | -- and explicit dereference cases. In these cases, we have to get | |
10241 | -- the length or range from the expression itself, making sure we | |
10242 | -- do not evaluate it more than once. | |
10243 | ||
6c8e70fe | 10244 | -- Here Expr is the original expression, or more properly the |
675d6070 TQ |
10245 | -- result of applying Duplicate_Expr to the original tree, forcing |
10246 | -- the result to be a name. | |
70482933 RK |
10247 | |
10248 | else | |
10249 | declare | |
12be130c | 10250 | Ndims : constant Pos := Number_Dimensions (T_Typ); |
70482933 RK |
10251 | |
10252 | begin | |
10253 | -- Build the condition for the explicit dereference case | |
10254 | ||
10255 | for Indx in 1 .. Ndims loop | |
10256 | Evolve_Or_Else | |
6c8e70fe | 10257 | (Cond, Length_N_Cond (Expr, T_Typ, Indx)); |
70482933 RK |
10258 | end loop; |
10259 | end; | |
10260 | end if; | |
10261 | end if; | |
10262 | end if; | |
10263 | ||
10264 | -- Construct the test and insert into the tree | |
10265 | ||
10266 | if Present (Cond) then | |
10267 | if Do_Access then | |
6c8e70fe | 10268 | Cond := Guard_Access (Cond, Loc, Expr); |
70482933 RK |
10269 | end if; |
10270 | ||
07fc65c4 GB |
10271 | Add_Check |
10272 | (Make_Raise_Constraint_Error (Loc, | |
10273 | Condition => Cond, | |
10274 | Reason => CE_Length_Check_Failed)); | |
70482933 RK |
10275 | end if; |
10276 | ||
10277 | return Ret_Result; | |
70482933 RK |
10278 | end Selected_Length_Checks; |
10279 | ||
10280 | --------------------------- | |
10281 | -- Selected_Range_Checks -- | |
10282 | --------------------------- | |
10283 | ||
10284 | function Selected_Range_Checks | |
6c8e70fe | 10285 | (Expr : Node_Id; |
70482933 RK |
10286 | Target_Typ : Entity_Id; |
10287 | Source_Typ : Entity_Id; | |
6b6fcd3e | 10288 | Warn_Node : Node_Id) return Check_Result |
70482933 | 10289 | is |
6c8e70fe | 10290 | Loc : constant Source_Ptr := Sloc (Expr); |
70482933 RK |
10291 | S_Typ : Entity_Id; |
10292 | T_Typ : Entity_Id; | |
10293 | Expr_Actual : Node_Id; | |
10294 | Exptyp : Entity_Id; | |
10295 | Cond : Node_Id := Empty; | |
10296 | Do_Access : Boolean := False; | |
8f721245 | 10297 | Wnode : Node_Id := Warn_Node; |
70482933 | 10298 | Ret_Result : Check_Result := (Empty, Empty); |
dcd5fd67 | 10299 | Num_Checks : Natural := 0; |
70482933 RK |
10300 | |
10301 | procedure Add_Check (N : Node_Id); | |
10302 | -- Adds the action given to Ret_Result if N is non-Empty | |
10303 | ||
10304 | function Discrete_Range_Cond | |
6c8e70fe EB |
10305 | (Exp : Node_Id; |
10306 | Typ : Entity_Id) return Node_Id; | |
70482933 | 10307 | -- Returns expression to compute: |
6c8e70fe | 10308 | -- Low_Bound (Exp) < Typ'First |
70482933 | 10309 | -- or else |
6c8e70fe | 10310 | -- High_Bound (Exp) > Typ'Last |
70482933 RK |
10311 | |
10312 | function Discrete_Expr_Cond | |
6c8e70fe EB |
10313 | (Exp : Node_Id; |
10314 | Typ : Entity_Id) return Node_Id; | |
70482933 | 10315 | -- Returns expression to compute: |
6c8e70fe | 10316 | -- Exp < Typ'First |
70482933 | 10317 | -- or else |
6c8e70fe | 10318 | -- Exp > Typ'Last |
70482933 RK |
10319 | |
10320 | function Get_E_First_Or_Last | |
5a153b27 AC |
10321 | (Loc : Source_Ptr; |
10322 | E : Entity_Id; | |
70482933 | 10323 | Indx : Nat; |
6b6fcd3e | 10324 | Nam : Name_Id) return Node_Id; |
a548f9ff | 10325 | -- Returns an attribute reference |
70482933 | 10326 | -- E'First or E'Last |
a548f9ff | 10327 | -- with a source location of Loc. |
6ca9ec9c | 10328 | -- |
a548f9ff TQ |
10329 | -- Nam is Name_First or Name_Last, according to which attribute is |
10330 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
10331 | -- Expressions of the attribute reference (identifying the desired | |
10332 | -- array dimension). | |
70482933 RK |
10333 | |
10334 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
10335 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
10336 | -- Returns expression to compute: | |
fbf5a39b | 10337 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
70482933 | 10338 | |
a521dc37 EB |
10339 | function Is_Cond_Expr_Ge (N : Node_Id; V : Node_Id) return Boolean; |
10340 | function Is_Cond_Expr_Le (N : Node_Id; V : Node_Id) return Boolean; | |
10341 | -- Return True if N is a conditional expression whose dependent | |
10342 | -- expressions are all known and greater/lower than or equal to V. | |
10343 | ||
70482933 RK |
10344 | function Range_E_Cond |
10345 | (Exptyp : Entity_Id; | |
10346 | Typ : Entity_Id; | |
10347 | Indx : Nat) | |
10348 | return Node_Id; | |
10349 | -- Returns expression to compute: | |
10350 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
10351 | ||
10352 | function Range_Equal_E_Cond | |
10353 | (Exptyp : Entity_Id; | |
10354 | Typ : Entity_Id; | |
6b6fcd3e | 10355 | Indx : Nat) return Node_Id; |
70482933 RK |
10356 | -- Returns expression to compute: |
10357 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
10358 | ||
10359 | function Range_N_Cond | |
6c8e70fe | 10360 | (Exp : Node_Id; |
70482933 | 10361 | Typ : Entity_Id; |
6b6fcd3e | 10362 | Indx : Nat) return Node_Id; |
70482933 | 10363 | -- Return expression to compute: |
6c8e70fe | 10364 | -- Exp'First < Typ'First or else Exp'Last > Typ'Last |
70482933 | 10365 | |
a521dc37 EB |
10366 | function "<" (Left, Right : Node_Id) return Boolean |
10367 | is (if Is_Floating_Point_Type (S_Typ) | |
10368 | then Expr_Value_R (Left) < Expr_Value_R (Right) | |
10369 | else Expr_Value (Left) < Expr_Value (Right)); | |
10370 | function "<=" (Left, Right : Node_Id) return Boolean | |
10371 | is (if Is_Floating_Point_Type (S_Typ) | |
10372 | then Expr_Value_R (Left) <= Expr_Value_R (Right) | |
10373 | else Expr_Value (Left) <= Expr_Value (Right)); | |
10374 | -- Convenience comparison functions of integer or floating point values | |
10375 | ||
70482933 RK |
10376 | --------------- |
10377 | -- Add_Check -- | |
10378 | --------------- | |
10379 | ||
10380 | procedure Add_Check (N : Node_Id) is | |
10381 | begin | |
10382 | if Present (N) then | |
10383 | ||
869a06d9 AC |
10384 | -- We do not support inserting more than 2 checks on the same |
10385 | -- node. If this happens it means we have already added an | |
10386 | -- unconditional raise, so we can skip the other checks safely | |
10387 | -- since N will always raise an exception. | |
70482933 RK |
10388 | |
10389 | if Num_Checks = 2 then | |
10390 | return; | |
10391 | end if; | |
10392 | ||
10393 | pragma Assert (Num_Checks <= 1); | |
10394 | Num_Checks := Num_Checks + 1; | |
10395 | Ret_Result (Num_Checks) := N; | |
10396 | end if; | |
10397 | end Add_Check; | |
10398 | ||
10399 | ------------------------- | |
10400 | -- Discrete_Expr_Cond -- | |
10401 | ------------------------- | |
10402 | ||
10403 | function Discrete_Expr_Cond | |
6c8e70fe EB |
10404 | (Exp : Node_Id; |
10405 | Typ : Entity_Id) return Node_Id | |
70482933 RK |
10406 | is |
10407 | begin | |
10408 | return | |
10409 | Make_Or_Else (Loc, | |
10410 | Left_Opnd => | |
10411 | Make_Op_Lt (Loc, | |
10412 | Left_Opnd => | |
fbf5a39b | 10413 | Convert_To (Base_Type (Typ), |
6c8e70fe | 10414 | Duplicate_Subexpr_No_Checks (Exp)), |
70482933 RK |
10415 | Right_Opnd => |
10416 | Convert_To (Base_Type (Typ), | |
5a153b27 | 10417 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
70482933 RK |
10418 | |
10419 | Right_Opnd => | |
10420 | Make_Op_Gt (Loc, | |
10421 | Left_Opnd => | |
fbf5a39b | 10422 | Convert_To (Base_Type (Typ), |
6c8e70fe | 10423 | Duplicate_Subexpr_No_Checks (Exp)), |
70482933 RK |
10424 | Right_Opnd => |
10425 | Convert_To | |
10426 | (Base_Type (Typ), | |
5a153b27 | 10427 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
70482933 RK |
10428 | end Discrete_Expr_Cond; |
10429 | ||
10430 | ------------------------- | |
10431 | -- Discrete_Range_Cond -- | |
10432 | ------------------------- | |
10433 | ||
10434 | function Discrete_Range_Cond | |
6c8e70fe EB |
10435 | (Exp : Node_Id; |
10436 | Typ : Entity_Id) return Node_Id | |
70482933 | 10437 | is |
6c8e70fe EB |
10438 | LB : Node_Id := Low_Bound (Exp); |
10439 | HB : Node_Id := High_Bound (Exp); | |
70482933 RK |
10440 | |
10441 | Left_Opnd : Node_Id; | |
10442 | Right_Opnd : Node_Id; | |
10443 | ||
10444 | begin | |
10445 | if Nkind (LB) = N_Identifier | |
675d6070 TQ |
10446 | and then Ekind (Entity (LB)) = E_Discriminant |
10447 | then | |
70482933 RK |
10448 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
10449 | end if; | |
10450 | ||
d32db3a7 GD |
10451 | -- If the index type has a fixed lower bound, then we require an |
10452 | -- exact match of the range's lower bound against that fixed lower | |
10453 | -- bound. | |
70482933 | 10454 | |
d32db3a7 GD |
10455 | if Is_Fixed_Lower_Bound_Index_Subtype (Typ) then |
10456 | Left_Opnd := | |
10457 | Make_Op_Ne (Loc, | |
10458 | Left_Opnd => | |
10459 | Convert_To | |
10460 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), | |
10461 | ||
10462 | Right_Opnd => | |
10463 | Convert_To | |
10464 | (Base_Type (Typ), | |
10465 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
10466 | ||
10467 | -- Otherwise we do the expected less-than comparison | |
10468 | ||
10469 | else | |
10470 | Left_Opnd := | |
10471 | Make_Op_Lt (Loc, | |
10472 | Left_Opnd => | |
10473 | Convert_To | |
10474 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), | |
10475 | ||
10476 | Right_Opnd => | |
10477 | Convert_To | |
10478 | (Base_Type (Typ), | |
10479 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
10480 | end if; | |
70482933 | 10481 | |
b3f96dc1 AC |
10482 | if Nkind (HB) = N_Identifier |
10483 | and then Ekind (Entity (HB)) = E_Discriminant | |
70482933 | 10484 | then |
b3f96dc1 | 10485 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
70482933 RK |
10486 | end if; |
10487 | ||
10488 | Right_Opnd := | |
10489 | Make_Op_Gt (Loc, | |
10490 | Left_Opnd => | |
10491 | Convert_To | |
fbf5a39b | 10492 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
70482933 RK |
10493 | |
10494 | Right_Opnd => | |
10495 | Convert_To | |
10496 | (Base_Type (Typ), | |
5a153b27 | 10497 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
70482933 RK |
10498 | |
10499 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
10500 | end Discrete_Range_Cond; | |
10501 | ||
10502 | ------------------------- | |
10503 | -- Get_E_First_Or_Last -- | |
10504 | ------------------------- | |
10505 | ||
10506 | function Get_E_First_Or_Last | |
5a153b27 AC |
10507 | (Loc : Source_Ptr; |
10508 | E : Entity_Id; | |
70482933 | 10509 | Indx : Nat; |
6b6fcd3e | 10510 | Nam : Name_Id) return Node_Id |
70482933 | 10511 | is |
5a153b27 | 10512 | Exprs : List_Id; |
70482933 | 10513 | begin |
5a153b27 AC |
10514 | if Indx > 0 then |
10515 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
70482933 | 10516 | else |
5a153b27 | 10517 | Exprs := No_List; |
70482933 RK |
10518 | end if; |
10519 | ||
5a153b27 AC |
10520 | return Make_Attribute_Reference (Loc, |
10521 | Prefix => New_Occurrence_Of (E, Loc), | |
10522 | Attribute_Name => Nam, | |
10523 | Expressions => Exprs); | |
70482933 RK |
10524 | end Get_E_First_Or_Last; |
10525 | ||
10526 | ----------------- | |
10527 | -- Get_N_First -- | |
10528 | ----------------- | |
10529 | ||
10530 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
10531 | begin | |
10532 | return | |
10533 | Make_Attribute_Reference (Loc, | |
10534 | Attribute_Name => Name_First, | |
10535 | Prefix => | |
fbf5a39b | 10536 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
70482933 RK |
10537 | Expressions => New_List ( |
10538 | Make_Integer_Literal (Loc, Indx))); | |
70482933 RK |
10539 | end Get_N_First; |
10540 | ||
10541 | ---------------- | |
10542 | -- Get_N_Last -- | |
10543 | ---------------- | |
10544 | ||
10545 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
10546 | begin | |
10547 | return | |
10548 | Make_Attribute_Reference (Loc, | |
10549 | Attribute_Name => Name_Last, | |
10550 | Prefix => | |
fbf5a39b | 10551 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
70482933 RK |
10552 | Expressions => New_List ( |
10553 | Make_Integer_Literal (Loc, Indx))); | |
70482933 RK |
10554 | end Get_N_Last; |
10555 | ||
a521dc37 EB |
10556 | --------------------- |
10557 | -- Is_Cond_Expr_Ge -- | |
10558 | --------------------- | |
10559 | ||
10560 | function Is_Cond_Expr_Ge (N : Node_Id; V : Node_Id) return Boolean is | |
10561 | begin | |
10562 | -- Only if expressions are relevant for the time being | |
10563 | ||
10564 | if Nkind (N) = N_If_Expression then | |
10565 | declare | |
10566 | Cond : constant Node_Id := First (Expressions (N)); | |
10567 | Thenx : constant Node_Id := Next (Cond); | |
10568 | Elsex : constant Node_Id := Next (Thenx); | |
10569 | ||
10570 | begin | |
10571 | return Compile_Time_Known_Value (Thenx) | |
10572 | and then V <= Thenx | |
10573 | and then | |
10574 | ((Compile_Time_Known_Value (Elsex) and then V <= Elsex) | |
10575 | or else Is_Cond_Expr_Ge (Elsex, V)); | |
10576 | end; | |
10577 | ||
10578 | else | |
10579 | return False; | |
10580 | end if; | |
10581 | end Is_Cond_Expr_Ge; | |
10582 | ||
10583 | --------------------- | |
10584 | -- Is_Cond_Expr_Le -- | |
10585 | --------------------- | |
10586 | ||
10587 | function Is_Cond_Expr_Le (N : Node_Id; V : Node_Id) return Boolean is | |
10588 | begin | |
10589 | -- Only if expressions are relevant for the time being | |
10590 | ||
10591 | if Nkind (N) = N_If_Expression then | |
10592 | declare | |
10593 | Cond : constant Node_Id := First (Expressions (N)); | |
10594 | Thenx : constant Node_Id := Next (Cond); | |
10595 | Elsex : constant Node_Id := Next (Thenx); | |
10596 | ||
10597 | begin | |
10598 | return Compile_Time_Known_Value (Thenx) | |
10599 | and then Thenx <= V | |
10600 | and then | |
10601 | ((Compile_Time_Known_Value (Elsex) and then Elsex <= V) | |
10602 | or else Is_Cond_Expr_Le (Elsex, V)); | |
10603 | end; | |
10604 | ||
10605 | else | |
10606 | return False; | |
10607 | end if; | |
10608 | end Is_Cond_Expr_Le; | |
10609 | ||
70482933 RK |
10610 | ------------------ |
10611 | -- Range_E_Cond -- | |
10612 | ------------------ | |
10613 | ||
10614 | function Range_E_Cond | |
10615 | (Exptyp : Entity_Id; | |
10616 | Typ : Entity_Id; | |
6b6fcd3e | 10617 | Indx : Nat) return Node_Id |
70482933 RK |
10618 | is |
10619 | begin | |
10620 | return | |
10621 | Make_Or_Else (Loc, | |
10622 | Left_Opnd => | |
10623 | Make_Op_Lt (Loc, | |
5a153b27 AC |
10624 | Left_Opnd => |
10625 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10626 | Right_Opnd => | |
10627 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
70482933 RK |
10628 | |
10629 | Right_Opnd => | |
10630 | Make_Op_Gt (Loc, | |
5a153b27 AC |
10631 | Left_Opnd => |
10632 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10633 | Right_Opnd => | |
10634 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
70482933 RK |
10635 | end Range_E_Cond; |
10636 | ||
10637 | ------------------------ | |
10638 | -- Range_Equal_E_Cond -- | |
10639 | ------------------------ | |
10640 | ||
10641 | function Range_Equal_E_Cond | |
10642 | (Exptyp : Entity_Id; | |
10643 | Typ : Entity_Id; | |
6b6fcd3e | 10644 | Indx : Nat) return Node_Id |
70482933 RK |
10645 | is |
10646 | begin | |
10647 | return | |
10648 | Make_Or_Else (Loc, | |
10649 | Left_Opnd => | |
10650 | Make_Op_Ne (Loc, | |
5a153b27 AC |
10651 | Left_Opnd => |
10652 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
10653 | Right_Opnd => | |
10654 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
10655 | ||
70482933 RK |
10656 | Right_Opnd => |
10657 | Make_Op_Ne (Loc, | |
5a153b27 AC |
10658 | Left_Opnd => |
10659 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
10660 | Right_Opnd => | |
10661 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
70482933 RK |
10662 | end Range_Equal_E_Cond; |
10663 | ||
10664 | ------------------ | |
10665 | -- Range_N_Cond -- | |
10666 | ------------------ | |
10667 | ||
10668 | function Range_N_Cond | |
6c8e70fe | 10669 | (Exp : Node_Id; |
70482933 | 10670 | Typ : Entity_Id; |
6b6fcd3e | 10671 | Indx : Nat) return Node_Id |
70482933 RK |
10672 | is |
10673 | begin | |
10674 | return | |
10675 | Make_Or_Else (Loc, | |
10676 | Left_Opnd => | |
10677 | Make_Op_Lt (Loc, | |
5a153b27 | 10678 | Left_Opnd => |
6c8e70fe | 10679 | Get_N_First (Exp, Indx), |
5a153b27 AC |
10680 | Right_Opnd => |
10681 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
70482933 RK |
10682 | |
10683 | Right_Opnd => | |
10684 | Make_Op_Gt (Loc, | |
5a153b27 | 10685 | Left_Opnd => |
6c8e70fe | 10686 | Get_N_Last (Exp, Indx), |
5a153b27 AC |
10687 | Right_Opnd => |
10688 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
70482933 RK |
10689 | end Range_N_Cond; |
10690 | ||
10691 | -- Start of processing for Selected_Range_Checks | |
10692 | ||
10693 | begin | |
27bb7941 AC |
10694 | -- Checks will be applied only when generating code. In GNATprove mode, |
10695 | -- we do not apply the checks, but we still call Selected_Range_Checks | |
fdd0a844 YM |
10696 | -- outside of generics to possibly issue errors on SPARK code when a |
10697 | -- run-time error can be detected at compile time. | |
27bb7941 | 10698 | |
fdd0a844 | 10699 | if Inside_A_Generic or (not GNATprove_Mode and not Expander_Active) then |
70482933 RK |
10700 | return Ret_Result; |
10701 | end if; | |
10702 | ||
10703 | if Target_Typ = Any_Type | |
10704 | or else Target_Typ = Any_Composite | |
6c8e70fe | 10705 | or else Raises_Constraint_Error (Expr) |
70482933 RK |
10706 | then |
10707 | return Ret_Result; | |
10708 | end if; | |
10709 | ||
10710 | if No (Wnode) then | |
6c8e70fe | 10711 | Wnode := Expr; |
70482933 RK |
10712 | end if; |
10713 | ||
10714 | T_Typ := Target_Typ; | |
10715 | ||
10716 | if No (Source_Typ) then | |
6c8e70fe | 10717 | S_Typ := Etype (Expr); |
70482933 RK |
10718 | else |
10719 | S_Typ := Source_Typ; | |
10720 | end if; | |
10721 | ||
10722 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
10723 | return Ret_Result; | |
10724 | end if; | |
10725 | ||
10726 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
6c8e70fe | 10727 | -- because S_Typ can be derived from Etype (Expr), if it's not passed |
70482933 RK |
10728 | -- in, and since Node can be an N_Range node, it might be invalid. |
10729 | -- Should there be an assert check somewhere for taking the Etype of | |
10730 | -- an N_Range node ??? | |
10731 | ||
10732 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
10733 | S_Typ := Designated_Type (S_Typ); | |
10734 | T_Typ := Designated_Type (T_Typ); | |
10735 | Do_Access := True; | |
10736 | ||
939c12d2 | 10737 | -- A simple optimization for the null case |
70482933 | 10738 | |
6c8e70fe | 10739 | if Known_Null (Expr) then |
70482933 RK |
10740 | return Ret_Result; |
10741 | end if; | |
10742 | end if; | |
10743 | ||
10744 | -- For an N_Range Node, check for a null range and then if not | |
10745 | -- null generate a range check action. | |
10746 | ||
6c8e70fe | 10747 | if Nkind (Expr) = N_Range then |
70482933 RK |
10748 | |
10749 | -- There's no point in checking a range against itself | |
10750 | ||
6c8e70fe | 10751 | if Expr = Scalar_Range (T_Typ) then |
70482933 RK |
10752 | return Ret_Result; |
10753 | end if; | |
10754 | ||
10755 | declare | |
10756 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
10757 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
10475800 EB |
10758 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
10759 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
70482933 | 10760 | |
869a06d9 AC |
10761 | LB : Node_Id := Low_Bound (Expr); |
10762 | HB : Node_Id := High_Bound (Expr); | |
10763 | Known_LB : Boolean := False; | |
10764 | Known_HB : Boolean := False; | |
10765 | Check_Added : Boolean := False; | |
10475800 | 10766 | |
869a06d9 AC |
10767 | Out_Of_Range_L : Boolean := False; |
10768 | Out_Of_Range_H : Boolean := False; | |
70482933 RK |
10769 | |
10770 | begin | |
10475800 EB |
10771 | -- Compute what is known at compile time |
10772 | ||
10773 | if Known_T_LB and Known_T_HB then | |
10774 | if Compile_Time_Known_Value (LB) then | |
10775 | Known_LB := True; | |
10776 | ||
10777 | -- There's no point in checking that a bound is within its | |
10778 | -- own range so pretend that it is known in this case. First | |
10779 | -- deal with low bound. | |
10780 | ||
10781 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
10782 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
10783 | then | |
10784 | LB := T_LB; | |
10785 | Known_LB := True; | |
a521dc37 EB |
10786 | |
10787 | -- Similarly; deal with the case where the low bound is a | |
10788 | -- conditional expression whose result is greater than or | |
10789 | -- equal to the target low bound. | |
10790 | ||
10791 | elsif Is_Cond_Expr_Ge (LB, T_LB) then | |
10792 | LB := T_LB; | |
10793 | Known_LB := True; | |
10475800 EB |
10794 | end if; |
10795 | ||
10796 | -- Likewise for the high bound | |
10797 | ||
10798 | if Compile_Time_Known_Value (HB) then | |
10799 | Known_HB := True; | |
10800 | ||
10801 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
10802 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
10803 | then | |
10804 | HB := T_HB; | |
10805 | Known_HB := True; | |
a521dc37 EB |
10806 | |
10807 | elsif Is_Cond_Expr_Le (HB, T_HB) then | |
10808 | HB := T_HB; | |
10809 | Known_HB := True; | |
10475800 EB |
10810 | end if; |
10811 | end if; | |
10812 | ||
869a06d9 AC |
10813 | -- Check for the simple cases where we can do the check at |
10814 | -- compile time. This is skipped if we have an access type, since | |
10815 | -- the access value may be null. | |
70482933 | 10816 | |
869a06d9 AC |
10817 | if not Do_Access and then Not_Null_Range (LB, HB) then |
10818 | if Known_LB then | |
10819 | if Known_T_LB then | |
10820 | Out_Of_Range_L := LB < T_LB; | |
10821 | end if; | |
70482933 | 10822 | |
869a06d9 AC |
10823 | if Known_T_HB and not Out_Of_Range_L then |
10824 | Out_Of_Range_L := T_HB < LB; | |
10825 | end if; | |
70482933 | 10826 | |
70482933 RK |
10827 | if Out_Of_Range_L then |
10828 | if No (Warn_Node) then | |
10829 | Add_Check | |
10830 | (Compile_Time_Constraint_Error | |
6c8e70fe | 10831 | (Low_Bound (Expr), |
685bc70f | 10832 | "static value out of range of}??", T_Typ)); |
869a06d9 | 10833 | Check_Added := True; |
70482933 RK |
10834 | |
10835 | else | |
10836 | Add_Check | |
10837 | (Compile_Time_Constraint_Error | |
10838 | (Wnode, | |
685bc70f | 10839 | "static range out of bounds of}??", T_Typ)); |
869a06d9 | 10840 | Check_Added := True; |
70482933 RK |
10841 | end if; |
10842 | end if; | |
869a06d9 AC |
10843 | end if; |
10844 | ||
d32db3a7 GD |
10845 | -- Flag the case of a fixed-lower-bound index where the static |
10846 | -- bounds are not equal. | |
10847 | ||
10848 | if not Check_Added | |
10849 | and then Is_Fixed_Lower_Bound_Index_Subtype (T_Typ) | |
0251292d EB |
10850 | and then Known_LB |
10851 | and then Known_T_LB | |
d32db3a7 GD |
10852 | and then Expr_Value (LB) /= Expr_Value (T_LB) |
10853 | then | |
10854 | Add_Check | |
10855 | (Compile_Time_Constraint_Error | |
10856 | ((if Present (Warn_Node) | |
cdfdd0de PT |
10857 | then Warn_Node else Low_Bound (Expr)), |
10858 | "static value does not equal lower bound of}??", | |
d32db3a7 GD |
10859 | T_Typ)); |
10860 | Check_Added := True; | |
10861 | end if; | |
10862 | ||
869a06d9 AC |
10863 | if Known_HB then |
10864 | if Known_T_HB then | |
10865 | Out_Of_Range_H := T_HB < HB; | |
10866 | end if; | |
10867 | ||
10868 | if Known_T_LB and not Out_Of_Range_H then | |
10869 | Out_Of_Range_H := HB < T_LB; | |
10870 | end if; | |
70482933 RK |
10871 | |
10872 | if Out_Of_Range_H then | |
10873 | if No (Warn_Node) then | |
10874 | Add_Check | |
10875 | (Compile_Time_Constraint_Error | |
6c8e70fe | 10876 | (High_Bound (Expr), |
685bc70f | 10877 | "static value out of range of}??", T_Typ)); |
869a06d9 | 10878 | Check_Added := True; |
70482933 RK |
10879 | |
10880 | else | |
10881 | Add_Check | |
10882 | (Compile_Time_Constraint_Error | |
10883 | (Wnode, | |
685bc70f | 10884 | "static range out of bounds of}??", T_Typ)); |
869a06d9 | 10885 | Check_Added := True; |
70482933 RK |
10886 | end if; |
10887 | end if; | |
70482933 | 10888 | end if; |
869a06d9 | 10889 | end if; |
70482933 | 10890 | |
869a06d9 AC |
10891 | -- Check for the case where not everything is static |
10892 | ||
10893 | if not Check_Added | |
10894 | and then | |
10895 | (Do_Access | |
10896 | or else not Known_T_LB | |
10897 | or else not Known_LB | |
10898 | or else not Known_T_HB | |
10899 | or else not Known_HB) | |
10900 | then | |
70482933 | 10901 | declare |
6c8e70fe EB |
10902 | LB : Node_Id := Low_Bound (Expr); |
10903 | HB : Node_Id := High_Bound (Expr); | |
70482933 RK |
10904 | |
10905 | begin | |
675d6070 TQ |
10906 | -- If either bound is a discriminant and we are within the |
10907 | -- record declaration, it is a use of the discriminant in a | |
10908 | -- constraint of a component, and nothing can be checked | |
10909 | -- here. The check will be emitted within the init proc. | |
10910 | -- Before then, the discriminal has no real meaning. | |
10911 | -- Similarly, if the entity is a discriminal, there is no | |
10912 | -- check to perform yet. | |
10913 | ||
10914 | -- The same holds within a discriminated synchronized type, | |
10915 | -- where the discriminant may constrain a component or an | |
10916 | -- entry family. | |
70482933 RK |
10917 | |
10918 | if Nkind (LB) = N_Identifier | |
c064e066 | 10919 | and then Denotes_Discriminant (LB, True) |
70482933 | 10920 | then |
c064e066 RD |
10921 | if Current_Scope = Scope (Entity (LB)) |
10922 | or else Is_Concurrent_Type (Current_Scope) | |
10923 | or else Ekind (Entity (LB)) /= E_Discriminant | |
10924 | then | |
70482933 RK |
10925 | return Ret_Result; |
10926 | else | |
10927 | LB := | |
10928 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
10929 | end if; | |
10930 | end if; | |
10931 | ||
10932 | if Nkind (HB) = N_Identifier | |
c064e066 | 10933 | and then Denotes_Discriminant (HB, True) |
70482933 | 10934 | then |
c064e066 RD |
10935 | if Current_Scope = Scope (Entity (HB)) |
10936 | or else Is_Concurrent_Type (Current_Scope) | |
10937 | or else Ekind (Entity (HB)) /= E_Discriminant | |
10938 | then | |
70482933 RK |
10939 | return Ret_Result; |
10940 | else | |
10941 | HB := | |
10942 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
10943 | end if; | |
10944 | end if; | |
10945 | ||
6c8e70fe | 10946 | Cond := Discrete_Range_Cond (Expr, T_Typ); |
70482933 RK |
10947 | Set_Paren_Count (Cond, 1); |
10948 | ||
10949 | Cond := | |
10950 | Make_And_Then (Loc, | |
10951 | Left_Opnd => | |
10952 | Make_Op_Ge (Loc, | |
4c51ff88 AC |
10953 | Left_Opnd => |
10954 | Convert_To (Base_Type (Etype (HB)), | |
10955 | Duplicate_Subexpr_No_Checks (HB)), | |
10956 | Right_Opnd => | |
10957 | Convert_To (Base_Type (Etype (LB)), | |
10958 | Duplicate_Subexpr_No_Checks (LB))), | |
70482933 RK |
10959 | Right_Opnd => Cond); |
10960 | end; | |
70482933 RK |
10961 | end if; |
10962 | end; | |
10963 | ||
10964 | elsif Is_Scalar_Type (S_Typ) then | |
10965 | ||
10966 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
869a06d9 AC |
10967 | -- except the above simply sets a flag in the node and lets the |
10968 | -- check be generated based on the Etype of the expression. | |
70482933 RK |
10969 | -- Sometimes, however we want to do a dynamic check against an |
10970 | -- arbitrary target type, so we do that here. | |
10971 | ||
10972 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
6c8e70fe | 10973 | Cond := Discrete_Expr_Cond (Expr, T_Typ); |
70482933 RK |
10974 | |
10975 | -- For literals, we can tell if the constraint error will be | |
10976 | -- raised at compile time, so we never need a dynamic check, but | |
10977 | -- if the exception will be raised, then post the usual warning, | |
10978 | -- and replace the literal with a raise constraint error | |
10979 | -- expression. As usual, skip this for access types | |
10980 | ||
6c8e70fe | 10981 | elsif Compile_Time_Known_Value (Expr) and then not Do_Access then |
869a06d9 | 10982 | if Is_Out_Of_Range (Expr, T_Typ) then |
70482933 | 10983 | |
869a06d9 AC |
10984 | -- Bounds of the type are static and the literal is out of |
10985 | -- range so output a warning message. | |
70482933 | 10986 | |
869a06d9 AC |
10987 | if No (Warn_Node) then |
10988 | Add_Check | |
10989 | (Compile_Time_Constraint_Error | |
10990 | (Expr, "static value out of range of}??", T_Typ)); | |
70482933 RK |
10991 | |
10992 | else | |
869a06d9 AC |
10993 | Add_Check |
10994 | (Compile_Time_Constraint_Error | |
10995 | (Wnode, "static value out of range of}??", T_Typ)); | |
70482933 | 10996 | end if; |
869a06d9 AC |
10997 | else |
10998 | Cond := Discrete_Expr_Cond (Expr, T_Typ); | |
10999 | end if; | |
70482933 RK |
11000 | |
11001 | -- Here for the case of a non-static expression, we need a runtime | |
11002 | -- check unless the source type range is guaranteed to be in the | |
11003 | -- range of the target type. | |
11004 | ||
11005 | else | |
c27f2f15 | 11006 | if not In_Subrange_Of (S_Typ, T_Typ) then |
6c8e70fe | 11007 | Cond := Discrete_Expr_Cond (Expr, T_Typ); |
70482933 RK |
11008 | end if; |
11009 | end if; | |
11010 | end if; | |
11011 | ||
11012 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
11013 | if Is_Constrained (T_Typ) then | |
6c8e70fe | 11014 | Expr_Actual := Get_Referenced_Object (Expr); |
70482933 RK |
11015 | Exptyp := Get_Actual_Subtype (Expr_Actual); |
11016 | ||
11017 | if Is_Access_Type (Exptyp) then | |
11018 | Exptyp := Designated_Type (Exptyp); | |
11019 | end if; | |
11020 | ||
11021 | -- String_Literal case. This needs to be handled specially be- | |
11022 | -- cause no index types are available for string literals. The | |
11023 | -- condition is simply: | |
11024 | ||
11025 | -- T_Typ'Length = string-literal-length | |
11026 | ||
11027 | if Nkind (Expr_Actual) = N_String_Literal then | |
11028 | null; | |
11029 | ||
11030 | -- General array case. Here we have a usable actual subtype for | |
11031 | -- the expression, and the condition is built from the two types | |
11032 | ||
11033 | -- T_Typ'First < Exptyp'First or else | |
11034 | -- T_Typ'Last > Exptyp'Last or else | |
11035 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
11036 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
11037 | -- ... | |
11038 | ||
11039 | elsif Is_Constrained (Exptyp) then | |
11040 | declare | |
12be130c | 11041 | Ndims : constant Pos := Number_Dimensions (T_Typ); |
fbf5a39b | 11042 | |
70482933 RK |
11043 | L_Index : Node_Id; |
11044 | R_Index : Node_Id; | |
70482933 RK |
11045 | |
11046 | begin | |
11047 | L_Index := First_Index (T_Typ); | |
11048 | R_Index := First_Index (Exptyp); | |
11049 | ||
11050 | for Indx in 1 .. Ndims loop | |
11051 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
07fc65c4 GB |
11052 | or else |
11053 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
70482933 | 11054 | then |
70482933 RK |
11055 | -- Deal with compile time length check. Note that we |
11056 | -- skip this in the access case, because the access | |
11057 | -- value may be null, so we cannot know statically. | |
11058 | ||
11059 | if not | |
11060 | Subtypes_Statically_Match | |
11061 | (Etype (L_Index), Etype (R_Index)) | |
11062 | then | |
11063 | -- If the target type is constrained then we | |
11064 | -- have to check for exact equality of bounds | |
11065 | -- (required for qualified expressions). | |
11066 | ||
11067 | if Is_Constrained (T_Typ) then | |
11068 | Evolve_Or_Else | |
11069 | (Cond, | |
11070 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
70482933 RK |
11071 | else |
11072 | Evolve_Or_Else | |
11073 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
11074 | end if; | |
11075 | end if; | |
11076 | ||
11077 | Next (L_Index); | |
11078 | Next (R_Index); | |
70482933 RK |
11079 | end if; |
11080 | end loop; | |
11081 | end; | |
11082 | ||
11083 | -- Handle cases where we do not get a usable actual subtype that | |
11084 | -- is constrained. This happens for example in the function call | |
11085 | -- and explicit dereference cases. In these cases, we have to get | |
11086 | -- the length or range from the expression itself, making sure we | |
11087 | -- do not evaluate it more than once. | |
11088 | ||
6c8e70fe | 11089 | -- Here Expr is the original expression, or more properly the |
70482933 RK |
11090 | -- result of applying Duplicate_Expr to the original tree, |
11091 | -- forcing the result to be a name. | |
11092 | ||
11093 | else | |
11094 | declare | |
12be130c | 11095 | Ndims : constant Pos := Number_Dimensions (T_Typ); |
70482933 RK |
11096 | |
11097 | begin | |
11098 | -- Build the condition for the explicit dereference case | |
11099 | ||
11100 | for Indx in 1 .. Ndims loop | |
11101 | Evolve_Or_Else | |
6c8e70fe | 11102 | (Cond, Range_N_Cond (Expr, T_Typ, Indx)); |
70482933 RK |
11103 | end loop; |
11104 | end; | |
70482933 RK |
11105 | end if; |
11106 | ||
62a3799f GD |
11107 | -- If the context is a qualified_expression where the subtype is |
11108 | -- an unconstrained array subtype with fixed-lower-bound indexes, | |
11109 | -- then consistency checks must be done between the lower bounds | |
11110 | -- of any such indexes and the corresponding lower bounds of the | |
11111 | -- qualified array object. | |
11112 | ||
11113 | elsif Is_Fixed_Lower_Bound_Array_Subtype (T_Typ) | |
11114 | and then Nkind (Parent (Expr)) = N_Qualified_Expression | |
11115 | and then not Do_Access | |
11116 | then | |
11117 | declare | |
11118 | Ndims : constant Pos := Number_Dimensions (T_Typ); | |
11119 | ||
11120 | Qual_Index : Node_Id; | |
11121 | Expr_Index : Node_Id; | |
11122 | ||
11123 | begin | |
11124 | Expr_Actual := Get_Referenced_Object (Expr); | |
11125 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
11126 | ||
11127 | Qual_Index := First_Index (T_Typ); | |
11128 | Expr_Index := First_Index (Exptyp); | |
11129 | ||
11130 | for Indx in 1 .. Ndims loop | |
11131 | if Nkind (Expr_Index) /= N_Raise_Constraint_Error then | |
11132 | ||
11133 | -- If this index of the qualifying array subtype has | |
11134 | -- a fixed lower bound, then apply a check that the | |
11135 | -- corresponding lower bound of the array expression | |
11136 | -- is equal to it. | |
11137 | ||
11138 | if Is_Fixed_Lower_Bound_Index_Subtype (Etype (Qual_Index)) | |
11139 | then | |
11140 | Evolve_Or_Else | |
11141 | (Cond, | |
11142 | Make_Op_Ne (Loc, | |
11143 | Left_Opnd => | |
11144 | Get_E_First_Or_Last | |
11145 | (Loc, Exptyp, Indx, Name_First), | |
11146 | Right_Opnd => | |
11147 | New_Copy_Tree | |
11148 | (Type_Low_Bound (Etype (Qual_Index))))); | |
11149 | end if; | |
11150 | ||
11151 | Next (Qual_Index); | |
11152 | Next (Expr_Index); | |
11153 | end if; | |
11154 | end loop; | |
11155 | end; | |
11156 | ||
70482933 | 11157 | else |
675d6070 TQ |
11158 | -- For a conversion to an unconstrained array type, generate an |
11159 | -- Action to check that the bounds of the source value are within | |
11160 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
11161 | -- check is needed for a conversion to an access to unconstrained | |
11162 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
11163 | -- of the two access types to statically match. | |
11164 | ||
6c8e70fe | 11165 | if Nkind (Parent (Expr)) = N_Type_Conversion |
675d6070 TQ |
11166 | and then not Do_Access |
11167 | then | |
70482933 RK |
11168 | declare |
11169 | Opnd_Index : Node_Id; | |
11170 | Targ_Index : Node_Id; | |
11b4899f | 11171 | Opnd_Range : Node_Id; |
70482933 RK |
11172 | |
11173 | begin | |
6c8e70fe | 11174 | Opnd_Index := First_Index (Get_Actual_Subtype (Expr)); |
70482933 | 11175 | Targ_Index := First_Index (T_Typ); |
11b4899f JM |
11176 | while Present (Opnd_Index) loop |
11177 | ||
11178 | -- If the index is a range, use its bounds. If it is an | |
11179 | -- entity (as will be the case if it is a named subtype | |
11180 | -- or an itype created for a slice) retrieve its range. | |
11181 | ||
11182 | if Is_Entity_Name (Opnd_Index) | |
11183 | and then Is_Type (Entity (Opnd_Index)) | |
11184 | then | |
11185 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
11186 | else | |
11187 | Opnd_Range := Opnd_Index; | |
11188 | end if; | |
11189 | ||
11190 | if Nkind (Opnd_Range) = N_Range then | |
c800f862 RD |
11191 | if Is_In_Range |
11192 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
11193 | Assume_Valid => True) | |
70482933 RK |
11194 | and then |
11195 | Is_In_Range | |
c800f862 RD |
11196 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
11197 | Assume_Valid => True) | |
70482933 RK |
11198 | then |
11199 | null; | |
11200 | ||
675d6070 | 11201 | -- If null range, no check needed |
ddda9d0f | 11202 | |
fbf5a39b | 11203 | elsif |
11b4899f | 11204 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
fbf5a39b | 11205 | and then |
11b4899f | 11206 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
fbf5a39b | 11207 | and then |
11b4899f JM |
11208 | Expr_Value (High_Bound (Opnd_Range)) < |
11209 | Expr_Value (Low_Bound (Opnd_Range)) | |
fbf5a39b AC |
11210 | then |
11211 | null; | |
11212 | ||
70482933 | 11213 | elsif Is_Out_Of_Range |
c800f862 RD |
11214 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
11215 | Assume_Valid => True) | |
70482933 RK |
11216 | or else |
11217 | Is_Out_Of_Range | |
c800f862 RD |
11218 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
11219 | Assume_Valid => True) | |
70482933 RK |
11220 | then |
11221 | Add_Check | |
11222 | (Compile_Time_Constraint_Error | |
685bc70f | 11223 | (Wnode, "value out of range of}??", T_Typ)); |
70482933 RK |
11224 | |
11225 | else | |
11226 | Evolve_Or_Else | |
11227 | (Cond, | |
11228 | Discrete_Range_Cond | |
11b4899f | 11229 | (Opnd_Range, Etype (Targ_Index))); |
70482933 RK |
11230 | end if; |
11231 | end if; | |
11232 | ||
11233 | Next_Index (Opnd_Index); | |
11234 | Next_Index (Targ_Index); | |
11235 | end loop; | |
11236 | end; | |
11237 | end if; | |
11238 | end if; | |
11239 | end if; | |
11240 | ||
11241 | -- Construct the test and insert into the tree | |
11242 | ||
11243 | if Present (Cond) then | |
11244 | if Do_Access then | |
6c8e70fe | 11245 | Cond := Guard_Access (Cond, Loc, Expr); |
70482933 RK |
11246 | end if; |
11247 | ||
07fc65c4 GB |
11248 | Add_Check |
11249 | (Make_Raise_Constraint_Error (Loc, | |
10475800 | 11250 | Condition => Cond, |
af6478c8 | 11251 | Reason => CE_Range_Check_Failed)); |
70482933 RK |
11252 | end if; |
11253 | ||
11254 | return Ret_Result; | |
70482933 RK |
11255 | end Selected_Range_Checks; |
11256 | ||
11257 | ------------------------------- | |
11258 | -- Storage_Checks_Suppressed -- | |
11259 | ------------------------------- | |
11260 | ||
11261 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
11262 | begin | |
fbf5a39b AC |
11263 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
11264 | return Is_Check_Suppressed (E, Storage_Check); | |
11265 | else | |
3217f71e | 11266 | return Scope_Suppress.Suppress (Storage_Check); |
fbf5a39b | 11267 | end if; |
70482933 RK |
11268 | end Storage_Checks_Suppressed; |
11269 | ||
11270 | --------------------------- | |
11271 | -- Tag_Checks_Suppressed -- | |
11272 | --------------------------- | |
11273 | ||
11274 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
11275 | begin | |
b98e2969 AC |
11276 | if Present (E) |
11277 | and then Checks_May_Be_Suppressed (E) | |
11278 | then | |
11279 | return Is_Check_Suppressed (E, Tag_Check); | |
637a41a5 AC |
11280 | else |
11281 | return Scope_Suppress.Suppress (Tag_Check); | |
fbf5a39b | 11282 | end if; |
70482933 RK |
11283 | end Tag_Checks_Suppressed; |
11284 | ||
aca670a0 AC |
11285 | --------------------------------------- |
11286 | -- Validate_Alignment_Check_Warnings -- | |
11287 | --------------------------------------- | |
11288 | ||
11289 | procedure Validate_Alignment_Check_Warnings is | |
11290 | begin | |
11291 | for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop | |
11292 | declare | |
11293 | AWR : Alignment_Warnings_Record | |
11294 | renames Alignment_Warnings.Table (J); | |
11295 | begin | |
11296 | if Known_Alignment (AWR.E) | |
2175b50b | 11297 | and then ((Present (AWR.A) |
5c13a04e EB |
11298 | and then AWR.A mod Alignment (AWR.E) = 0) |
11299 | or else (Present (AWR.P) | |
11300 | and then Has_Compatible_Alignment | |
11301 | (AWR.E, AWR.P, True) = | |
11302 | Known_Compatible)) | |
aca670a0 AC |
11303 | then |
11304 | Delete_Warning_And_Continuations (AWR.W); | |
11305 | end if; | |
11306 | end; | |
11307 | end loop; | |
11308 | end Validate_Alignment_Check_Warnings; | |
11309 | ||
c064e066 RD |
11310 | -------------------------- |
11311 | -- Validity_Check_Range -- | |
11312 | -------------------------- | |
11313 | ||
2934b84a AC |
11314 | procedure Validity_Check_Range |
11315 | (N : Node_Id; | |
68dd6649 | 11316 | Related_Id : Entity_Id := Empty) is |
c064e066 RD |
11317 | begin |
11318 | if Validity_Checks_On and Validity_Check_Operands then | |
11319 | if Nkind (N) = N_Range then | |
2934b84a AC |
11320 | Ensure_Valid |
11321 | (Expr => Low_Bound (N), | |
11322 | Related_Id => Related_Id, | |
11323 | Is_Low_Bound => True); | |
11324 | ||
11325 | Ensure_Valid | |
11326 | (Expr => High_Bound (N), | |
11327 | Related_Id => Related_Id, | |
11328 | Is_High_Bound => True); | |
c064e066 RD |
11329 | end if; |
11330 | end if; | |
11331 | end Validity_Check_Range; | |
11332 | ||
70482933 | 11333 | end Checks; |