]>
Commit | Line | Data |
---|---|---|
ee6ba406 | 1 | ------------------------------------------------------------------------------ |
2 | -- -- | |
3 | -- GNAT COMPILER COMPONENTS -- | |
4 | -- -- | |
5 | -- C H E C K S -- | |
6 | -- -- | |
7 | -- B o d y -- | |
8 | -- -- | |
301d5ec3 | 9 | -- Copyright (C) 1992-2012, Free Software Foundation, Inc. -- |
ee6ba406 | 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- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
ee6ba406 | 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 -- | |
80df182a | 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. -- | |
ee6ba406 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
ee6ba406 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Debug; use Debug; | |
28 | with Einfo; use Einfo; | |
29 | with Errout; use Errout; | |
30 | with Exp_Ch2; use Exp_Ch2; | |
df40eeb0 | 31 | with Exp_Ch4; use Exp_Ch4; |
00c403ee | 32 | with Exp_Ch11; use Exp_Ch11; |
05fcfafb | 33 | with Exp_Pakd; use Exp_Pakd; |
301d5ec3 | 34 | with Exp_Tss; use Exp_Tss; |
ee6ba406 | 35 | with Exp_Util; use Exp_Util; |
36 | with Elists; use Elists; | |
4fb5f0a0 | 37 | with Expander; use Expander; |
5329ca64 | 38 | with Eval_Fat; use Eval_Fat; |
ee6ba406 | 39 | with Freeze; use Freeze; |
9dfe12ae | 40 | with Lib; use Lib; |
ee6ba406 | 41 | with Nlists; use Nlists; |
42 | with Nmake; use Nmake; | |
43 | with Opt; use Opt; | |
9dfe12ae | 44 | with Output; use Output; |
c2b56224 | 45 | with Restrict; use Restrict; |
1e16c51c | 46 | with Rident; use Rident; |
ee6ba406 | 47 | with Rtsfind; use Rtsfind; |
48 | with Sem; use Sem; | |
d60c9ff7 | 49 | with Sem_Aux; use Sem_Aux; |
ee6ba406 | 50 | with Sem_Eval; use Sem_Eval; |
00f91aef | 51 | with Sem_Ch3; use Sem_Ch3; |
9dfe12ae | 52 | with Sem_Ch8; use Sem_Ch8; |
ee6ba406 | 53 | with Sem_Res; use Sem_Res; |
54 | with Sem_Util; use Sem_Util; | |
55 | with Sem_Warn; use Sem_Warn; | |
56 | with Sinfo; use Sinfo; | |
9dfe12ae | 57 | with Sinput; use Sinput; |
ee6ba406 | 58 | with Snames; use Snames; |
9dfe12ae | 59 | with Sprint; use Sprint; |
ee6ba406 | 60 | with Stand; use Stand; |
f15731c4 | 61 | with Targparm; use Targparm; |
ee6ba406 | 62 | with Tbuild; use Tbuild; |
63 | with Ttypes; use Ttypes; | |
64 | with Urealp; use Urealp; | |
65 | with Validsw; use Validsw; | |
66 | ||
67 | package body Checks is | |
68 | ||
69 | -- General note: many of these routines are concerned with generating | |
70 | -- checking code to make sure that constraint error is raised at runtime. | |
71 | -- Clearly this code is only needed if the expander is active, since | |
72 | -- otherwise we will not be generating code or going into the runtime | |
73 | -- execution anyway. | |
74 | ||
75 | -- We therefore disconnect most of these checks if the expander is | |
76 | -- inactive. This has the additional benefit that we do not need to | |
77 | -- worry about the tree being messed up by previous errors (since errors | |
78 | -- turn off expansion anyway). | |
79 | ||
80 | -- There are a few exceptions to the above rule. For instance routines | |
81 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
82 | -- safely called even when the Expander is inactive (but Errors_Detected | |
83 | -- is 0). The benefit of executing this code when expansion is off, is | |
84 | -- the ability to emit constraint error warning for static expressions | |
85 | -- even when we are not generating code. | |
86 | ||
9dfe12ae | 87 | ------------------------------------- |
88 | -- Suppression of Redundant Checks -- | |
89 | ------------------------------------- | |
90 | ||
91 | -- This unit implements a limited circuit for removal of redundant | |
92 | -- checks. The processing is based on a tracing of simple sequential | |
93 | -- flow. For any sequence of statements, we save expressions that are | |
94 | -- marked to be checked, and then if the same expression appears later | |
95 | -- with the same check, then under certain circumstances, the second | |
96 | -- check can be suppressed. | |
97 | ||
98 | -- Basically, we can suppress the check if we know for certain that | |
99 | -- the previous expression has been elaborated (together with its | |
100 | -- check), and we know that the exception frame is the same, and that | |
101 | -- nothing has happened to change the result of the exception. | |
102 | ||
103 | -- Let us examine each of these three conditions in turn to describe | |
104 | -- how we ensure that this condition is met. | |
105 | ||
106 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 107 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 108 | -- Conditional_Statements_Begin at the start of any statement sequence |
109 | -- and Conditional_Statements_End at the end. The End call causes all | |
110 | -- checks remembered since the Begin call to be discarded. This does | |
111 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
112 | -- no exception handlers. But the important thing is to be conservative. | |
113 | -- The other protection is that all checks are discarded if a label | |
114 | -- is encountered, since then the assumption of sequential execution | |
115 | -- is violated, and we don't know enough about the flow. | |
116 | ||
117 | -- Second, we need to know that the exception frame is the same. We | |
118 | -- do this by killing all remembered checks when we enter a new frame. | |
119 | -- Again, that's over-conservative, but generally the cases we can help | |
120 | -- with are pretty local anyway (like the body of a loop for example). | |
121 | ||
122 | -- Third, we must be sure to forget any checks which are no longer valid. | |
123 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
124 | -- used to note any changes to local variables. We only attempt to deal | |
125 | -- with checks involving local variables, so we do not need to worry | |
126 | -- about global variables. Second, a call to any non-global procedure | |
127 | -- causes us to abandon all stored checks, since such a all may affect | |
128 | -- the values of any local variables. | |
129 | ||
130 | -- The following define the data structures used to deal with remembering | |
131 | -- checks so that redundant checks can be eliminated as described above. | |
132 | ||
133 | -- Right now, the only expressions that we deal with are of the form of | |
134 | -- simple local objects (either declared locally, or IN parameters) or | |
135 | -- such objects plus/minus a compile time known constant. We can do | |
136 | -- more later on if it seems worthwhile, but this catches many simple | |
137 | -- cases in practice. | |
138 | ||
139 | -- The following record type reflects a single saved check. An entry | |
140 | -- is made in the stack of saved checks if and only if the expression | |
141 | -- has been elaborated with the indicated checks. | |
142 | ||
143 | type Saved_Check is record | |
144 | Killed : Boolean; | |
145 | -- Set True if entry is killed by Kill_Checks | |
146 | ||
147 | Entity : Entity_Id; | |
148 | -- The entity involved in the expression that is checked | |
149 | ||
150 | Offset : Uint; | |
151 | -- A compile time value indicating the result of adding or | |
152 | -- subtracting a compile time value. This value is to be | |
153 | -- added to the value of the Entity. A value of zero is | |
154 | -- used for the case of a simple entity reference. | |
155 | ||
156 | Check_Type : Character; | |
157 | -- This is set to 'R' for a range check (in which case Target_Type | |
158 | -- is set to the target type for the range check) or to 'O' for an | |
159 | -- overflow check (in which case Target_Type is set to Empty). | |
160 | ||
161 | Target_Type : Entity_Id; | |
162 | -- Used only if Do_Range_Check is set. Records the target type for | |
163 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 164 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 165 | -- range that is smaller or equal to the stored target type of a |
166 | -- saved check). | |
167 | end record; | |
168 | ||
169 | -- The following table keeps track of saved checks. Rather than use an | |
170 | -- extensible table. We just use a table of fixed size, and we discard | |
171 | -- any saved checks that do not fit. That's very unlikely to happen and | |
172 | -- this is only an optimization in any case. | |
173 | ||
174 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
175 | -- Array of saved checks | |
176 | ||
177 | Num_Saved_Checks : Nat := 0; | |
178 | -- Number of saved checks | |
179 | ||
180 | -- The following stack keeps track of statement ranges. It is treated | |
181 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
182 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
183 | -- at the time of the call. Then when Conditional_Statements_End is | |
184 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
185 | ||
186 | -- Note: again, this is a fixed length stack with a size that should | |
187 | -- always be fine. If the value of the stack pointer goes above the | |
188 | -- limit, then we just forget all saved checks. | |
189 | ||
190 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
191 | Saved_Checks_TOS : Nat := 0; | |
192 | ||
193 | ----------------------- | |
194 | -- Local Subprograms -- | |
195 | ----------------------- | |
ee6ba406 | 196 | |
0df9d43f | 197 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 198 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 199 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 200 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
201 | -- signed integer arithmetic operator (but not an if or case expression). | |
202 | -- It is also called for types other than signed integers. | |
3cce7f32 | 203 | |
204 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
205 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 206 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
207 | -- arithmetic op (which includes the case of if and case expressions). Note | |
208 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
209 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 210 | |
2fe22c69 | 211 | procedure Apply_Division_Check |
212 | (N : Node_Id; | |
213 | Rlo : Uint; | |
214 | Rhi : Uint; | |
215 | ROK : Boolean); | |
216 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
217 | -- division checks as required if the Do_Division_Check flag is set. | |
218 | -- Rlo and Rhi give the possible range of the right operand, these values | |
219 | -- can be referenced and trusted only if ROK is set True. | |
220 | ||
221 | procedure Apply_Float_Conversion_Check | |
222 | (Ck_Node : Node_Id; | |
223 | Target_Typ : Entity_Id); | |
224 | -- The checks on a conversion from a floating-point type to an integer | |
225 | -- type are delicate. They have to be performed before conversion, they | |
226 | -- have to raise an exception when the operand is a NaN, and rounding must | |
227 | -- be taken into account to determine the safe bounds of the operand. | |
228 | ||
ee6ba406 | 229 | procedure Apply_Selected_Length_Checks |
230 | (Ck_Node : Node_Id; | |
231 | Target_Typ : Entity_Id; | |
232 | Source_Typ : Entity_Id; | |
233 | Do_Static : Boolean); | |
234 | -- This is the subprogram that does all the work for Apply_Length_Check | |
235 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
236 | -- described for the above routines. The Do_Static flag indicates that | |
237 | -- only a static check is to be done. | |
238 | ||
239 | procedure Apply_Selected_Range_Checks | |
240 | (Ck_Node : Node_Id; | |
241 | Target_Typ : Entity_Id; | |
242 | Source_Typ : Entity_Id; | |
243 | Do_Static : Boolean); | |
244 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
245 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
246 | -- routine. The Do_Static flag indicates that only a static check is | |
247 | -- to be done. | |
248 | ||
2af58f67 | 249 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 250 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
251 | -- This function is used to see if an access or division by zero check is | |
252 | -- needed. The check is to be applied to a single variable appearing in the | |
253 | -- source, and N is the node for the reference. If N is not of this form, | |
254 | -- True is returned with no further processing. If N is of the right form, | |
255 | -- then further processing determines if the given Check is needed. | |
256 | -- | |
257 | -- The particular circuit is to see if we have the case of a check that is | |
258 | -- not needed because it appears in the right operand of a short circuited | |
259 | -- conditional where the left operand guards the check. For example: | |
260 | -- | |
261 | -- if Var = 0 or else Q / Var > 12 then | |
262 | -- ... | |
263 | -- end if; | |
264 | -- | |
265 | -- In this example, the division check is not required. At the same time | |
266 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
267 | -- such as: | |
268 | -- | |
269 | -- if Var = 0 or Q / Var > 12 then | |
270 | -- ... | |
271 | -- end if; | |
272 | ||
9dfe12ae | 273 | procedure Find_Check |
274 | (Expr : Node_Id; | |
275 | Check_Type : Character; | |
276 | Target_Type : Entity_Id; | |
277 | Entry_OK : out Boolean; | |
278 | Check_Num : out Nat; | |
279 | Ent : out Entity_Id; | |
280 | Ofs : out Uint); | |
281 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
282 | -- to see if a check is of the form for optimization, and if so, to see | |
283 | -- if it has already been performed. Expr is the expression to check, | |
284 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
285 | -- Target_Type is the target type for a range check, and Empty for an | |
286 | -- overflow check. If the entry is not of the form for optimization, | |
287 | -- then Entry_OK is set to False, and the remaining out parameters | |
288 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
289 | -- entity and offset from the expression. Check_Num is the number of | |
290 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
291 | -- is located. | |
292 | ||
ee6ba406 | 293 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
294 | -- If a discriminal is used in constraining a prival, Return reference | |
295 | -- to the discriminal of the protected body (which renames the parameter | |
296 | -- of the enclosing protected operation). This clumsy transformation is | |
297 | -- needed because privals are created too late and their actual subtypes | |
298 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 299 | -- This function is called whenever the bound is an entity and the scope |
300 | -- indicates a protected operation. If the bound is an in-parameter of | |
301 | -- a protected operation that is not a prival, the function returns the | |
302 | -- bound itself. | |
ee6ba406 | 303 | -- To be cleaned up??? |
304 | ||
305 | function Guard_Access | |
306 | (Cond : Node_Id; | |
307 | Loc : Source_Ptr; | |
314a23b6 | 308 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 309 | -- In the access type case, guard the test with a test to ensure |
310 | -- that the access value is non-null, since the checks do not | |
311 | -- not apply to null access values. | |
312 | ||
313 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
314 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
315 | -- Constraint_Error node. | |
316 | ||
3cce7f32 | 317 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
318 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 319 | -- integer operands. This includes unary and binary operators, and also |
320 | -- if and case expression nodes where the dependent expressions are of | |
321 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 322 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 323 | |
0577b0b1 | 324 | function Range_Or_Validity_Checks_Suppressed |
325 | (Expr : Node_Id) return Boolean; | |
326 | -- Returns True if either range or validity checks or both are suppressed | |
327 | -- for the type of the given expression, or, if the expression is the name | |
328 | -- of an entity, if these checks are suppressed for the entity. | |
329 | ||
ee6ba406 | 330 | function Selected_Length_Checks |
331 | (Ck_Node : Node_Id; | |
332 | Target_Typ : Entity_Id; | |
333 | Source_Typ : Entity_Id; | |
314a23b6 | 334 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 335 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
336 | -- anything, just returns a list of nodes as described in the spec of | |
337 | -- this package for the Range_Check function. | |
338 | ||
339 | function Selected_Range_Checks | |
340 | (Ck_Node : Node_Id; | |
341 | Target_Typ : Entity_Id; | |
342 | Source_Typ : Entity_Id; | |
314a23b6 | 343 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 344 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
345 | -- just returns a list of nodes as described in the spec of this package | |
346 | -- for the Range_Check function. | |
347 | ||
348 | ------------------------------ | |
349 | -- Access_Checks_Suppressed -- | |
350 | ------------------------------ | |
351 | ||
352 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
353 | begin | |
9dfe12ae | 354 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
355 | return Is_Check_Suppressed (E, Access_Check); | |
356 | else | |
fafc6b97 | 357 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 358 | end if; |
ee6ba406 | 359 | end Access_Checks_Suppressed; |
360 | ||
361 | ------------------------------------- | |
362 | -- Accessibility_Checks_Suppressed -- | |
363 | ------------------------------------- | |
364 | ||
365 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
366 | begin | |
9dfe12ae | 367 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
368 | return Is_Check_Suppressed (E, Accessibility_Check); | |
369 | else | |
fafc6b97 | 370 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 371 | end if; |
ee6ba406 | 372 | end Accessibility_Checks_Suppressed; |
373 | ||
00c403ee | 374 | ----------------------------- |
375 | -- Activate_Division_Check -- | |
376 | ----------------------------- | |
377 | ||
378 | procedure Activate_Division_Check (N : Node_Id) is | |
379 | begin | |
380 | Set_Do_Division_Check (N, True); | |
381 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
382 | end Activate_Division_Check; | |
383 | ||
384 | ----------------------------- | |
385 | -- Activate_Overflow_Check -- | |
386 | ----------------------------- | |
387 | ||
388 | procedure Activate_Overflow_Check (N : Node_Id) is | |
389 | begin | |
d32ceaf3 | 390 | if not Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then |
391 | Set_Do_Overflow_Check (N, True); | |
392 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
393 | end if; | |
00c403ee | 394 | end Activate_Overflow_Check; |
395 | ||
396 | -------------------------- | |
397 | -- Activate_Range_Check -- | |
398 | -------------------------- | |
399 | ||
400 | procedure Activate_Range_Check (N : Node_Id) is | |
401 | begin | |
402 | Set_Do_Range_Check (N, True); | |
403 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
404 | end Activate_Range_Check; | |
405 | ||
0577b0b1 | 406 | --------------------------------- |
407 | -- Alignment_Checks_Suppressed -- | |
408 | --------------------------------- | |
409 | ||
410 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
411 | begin | |
412 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
413 | return Is_Check_Suppressed (E, Alignment_Check); | |
414 | else | |
fafc6b97 | 415 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 416 | end if; |
417 | end Alignment_Checks_Suppressed; | |
418 | ||
ee6ba406 | 419 | ------------------------- |
420 | -- Append_Range_Checks -- | |
421 | ------------------------- | |
422 | ||
423 | procedure Append_Range_Checks | |
424 | (Checks : Check_Result; | |
425 | Stmts : List_Id; | |
426 | Suppress_Typ : Entity_Id; | |
427 | Static_Sloc : Source_Ptr; | |
428 | Flag_Node : Node_Id) | |
429 | is | |
9dfe12ae | 430 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
431 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
432 | ||
ee6ba406 | 433 | Checks_On : constant Boolean := |
b6341c67 | 434 | (not Index_Checks_Suppressed (Suppress_Typ)) |
435 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 436 | |
437 | begin | |
438 | -- For now we just return if Checks_On is false, however this should | |
439 | -- be enhanced to check for an always True value in the condition | |
440 | -- and to generate a compilation warning??? | |
441 | ||
442 | if not Checks_On then | |
443 | return; | |
444 | end if; | |
445 | ||
446 | for J in 1 .. 2 loop | |
447 | exit when No (Checks (J)); | |
448 | ||
449 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
450 | and then Present (Condition (Checks (J))) | |
451 | then | |
452 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
453 | Append_To (Stmts, Checks (J)); | |
454 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
455 | end if; | |
456 | ||
457 | else | |
458 | Append_To | |
f15731c4 | 459 | (Stmts, |
460 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
461 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 462 | end if; |
463 | end loop; | |
464 | end Append_Range_Checks; | |
465 | ||
466 | ------------------------ | |
467 | -- Apply_Access_Check -- | |
468 | ------------------------ | |
469 | ||
470 | procedure Apply_Access_Check (N : Node_Id) is | |
471 | P : constant Node_Id := Prefix (N); | |
472 | ||
473 | begin | |
13dbf220 | 474 | -- We do not need checks if we are not generating code (i.e. the |
475 | -- expander is not active). This is not just an optimization, there | |
476 | -- are cases (e.g. with pragma Debug) where generating the checks | |
477 | -- can cause real trouble). | |
284faf8b | 478 | |
6dbcfcd9 | 479 | if not Full_Expander_Active then |
13dbf220 | 480 | return; |
9dfe12ae | 481 | end if; |
ee6ba406 | 482 | |
84d0d4a5 | 483 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 484 | |
84d0d4a5 | 485 | if not Check_Needed (P, Access_Check) then |
486 | return; | |
ee6ba406 | 487 | end if; |
9dfe12ae | 488 | |
cc60bd16 | 489 | -- No check if accessing the Offset_To_Top component of a dispatch |
490 | -- table. They are safe by construction. | |
491 | ||
040277b1 | 492 | if Tagged_Type_Expansion |
493 | and then Present (Etype (P)) | |
cc60bd16 | 494 | and then RTU_Loaded (Ada_Tags) |
495 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
496 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
497 | then | |
498 | return; | |
499 | end if; | |
500 | ||
84d0d4a5 | 501 | -- Otherwise go ahead and install the check |
9dfe12ae | 502 | |
fa7497e8 | 503 | Install_Null_Excluding_Check (P); |
ee6ba406 | 504 | end Apply_Access_Check; |
505 | ||
506 | ------------------------------- | |
507 | -- Apply_Accessibility_Check -- | |
508 | ------------------------------- | |
509 | ||
55dc6dc2 | 510 | procedure Apply_Accessibility_Check |
511 | (N : Node_Id; | |
512 | Typ : Entity_Id; | |
513 | Insert_Node : Node_Id) | |
514 | is | |
ee6ba406 | 515 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 516 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 517 | Param_Level : Node_Id; |
518 | Type_Level : Node_Id; | |
519 | ||
520 | begin | |
47d210a3 | 521 | if Ada_Version >= Ada_2012 |
522 | and then not Present (Param_Ent) | |
523 | and then Is_Entity_Name (N) | |
524 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
525 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
526 | then | |
527 | Param_Ent := Entity (N); | |
528 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 529 | |
47d210a3 | 530 | -- Renamed_Object must return an Entity_Name here |
531 | -- because of preceding "Present (E_E_A (...))" test. | |
532 | ||
533 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
534 | end loop; | |
535 | end if; | |
536 | ||
ee6ba406 | 537 | if Inside_A_Generic then |
538 | return; | |
539 | ||
6ffc64fc | 540 | -- Only apply the run-time check if the access parameter has an |
541 | -- associated extra access level parameter and when the level of the | |
542 | -- type is less deep than the level of the access parameter, and | |
543 | -- accessibility checks are not suppressed. | |
ee6ba406 | 544 | |
545 | elsif Present (Param_Ent) | |
546 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 547 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 548 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 549 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
550 | and then not Accessibility_Checks_Suppressed (Typ) | |
551 | then | |
552 | Param_Level := | |
553 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
554 | ||
1a9cc6cd | 555 | Type_Level := |
556 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
ee6ba406 | 557 | |
bf3e1520 | 558 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 559 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 560 | |
55dc6dc2 | 561 | Insert_Action (Insert_Node, |
ee6ba406 | 562 | Make_Raise_Program_Error (Loc, |
563 | Condition => | |
564 | Make_Op_Gt (Loc, | |
565 | Left_Opnd => Param_Level, | |
f15731c4 | 566 | Right_Opnd => Type_Level), |
567 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 568 | |
569 | Analyze_And_Resolve (N); | |
570 | end if; | |
571 | end Apply_Accessibility_Check; | |
572 | ||
0577b0b1 | 573 | -------------------------------- |
574 | -- Apply_Address_Clause_Check -- | |
575 | -------------------------------- | |
576 | ||
577 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 578 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
579 | ||
0577b0b1 | 580 | AC : constant Node_Id := Address_Clause (E); |
581 | Loc : constant Source_Ptr := Sloc (AC); | |
582 | Typ : constant Entity_Id := Etype (E); | |
583 | Aexp : constant Node_Id := Expression (AC); | |
c2b56224 | 584 | |
c2b56224 | 585 | Expr : Node_Id; |
0577b0b1 | 586 | -- Address expression (not necessarily the same as Aexp, for example |
587 | -- when Aexp is a reference to a constant, in which case Expr gets | |
588 | -- reset to reference the value expression of the constant. | |
589 | ||
0577b0b1 | 590 | procedure Compile_Time_Bad_Alignment; |
591 | -- Post error warnings when alignment is known to be incompatible. Note | |
592 | -- that we do not go as far as inserting a raise of Program_Error since | |
593 | -- this is an erroneous case, and it may happen that we are lucky and an | |
d6da7448 | 594 | -- underaligned address turns out to be OK after all. |
0577b0b1 | 595 | |
596 | -------------------------------- | |
597 | -- Compile_Time_Bad_Alignment -- | |
598 | -------------------------------- | |
599 | ||
600 | procedure Compile_Time_Bad_Alignment is | |
601 | begin | |
d6da7448 | 602 | if Address_Clause_Overlay_Warnings then |
0577b0b1 | 603 | Error_Msg_FE |
cb97ae5c | 604 | ("?o?specified address for& may be inconsistent with alignment", |
0577b0b1 | 605 | Aexp, E); |
606 | Error_Msg_FE | |
cb97ae5c | 607 | ("\?o?program execution may be erroneous (RM 13.3(27))", |
0577b0b1 | 608 | Aexp, E); |
83f8f0a6 | 609 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 610 | end if; |
611 | end Compile_Time_Bad_Alignment; | |
c2b56224 | 612 | |
2af58f67 | 613 | -- Start of processing for Apply_Address_Clause_Check |
5c61a0ff | 614 | |
c2b56224 | 615 | begin |
d6da7448 | 616 | -- See if alignment check needed. Note that we never need a check if the |
617 | -- maximum alignment is one, since the check will always succeed. | |
618 | ||
619 | -- Note: we do not check for checks suppressed here, since that check | |
620 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
621 | -- only called if checks were not suppressed. The reason for this is | |
622 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
623 | -- time (so that all types etc are elaborated), but we have to check | |
624 | -- the status of check suppressing at the point of the address clause. | |
625 | ||
626 | if No (AC) | |
627 | or else not Check_Address_Alignment (AC) | |
628 | or else Maximum_Alignment = 1 | |
629 | then | |
630 | return; | |
631 | end if; | |
632 | ||
633 | -- Obtain expression from address clause | |
9dfe12ae | 634 | |
0577b0b1 | 635 | Expr := Expression (AC); |
636 | ||
637 | -- The following loop digs for the real expression to use in the check | |
638 | ||
639 | loop | |
640 | -- For constant, get constant expression | |
641 | ||
642 | if Is_Entity_Name (Expr) | |
643 | and then Ekind (Entity (Expr)) = E_Constant | |
644 | then | |
645 | Expr := Constant_Value (Entity (Expr)); | |
646 | ||
647 | -- For unchecked conversion, get result to convert | |
648 | ||
649 | elsif Nkind (Expr) = N_Unchecked_Type_Conversion then | |
650 | Expr := Expression (Expr); | |
651 | ||
652 | -- For (common case) of To_Address call, get argument | |
653 | ||
654 | elsif Nkind (Expr) = N_Function_Call | |
655 | and then Is_Entity_Name (Name (Expr)) | |
656 | and then Is_RTE (Entity (Name (Expr)), RE_To_Address) | |
657 | then | |
658 | Expr := First (Parameter_Associations (Expr)); | |
659 | ||
660 | if Nkind (Expr) = N_Parameter_Association then | |
661 | Expr := Explicit_Actual_Parameter (Expr); | |
662 | end if; | |
663 | ||
664 | -- We finally have the real expression | |
665 | ||
666 | else | |
667 | exit; | |
668 | end if; | |
669 | end loop; | |
670 | ||
d6da7448 | 671 | -- See if we know that Expr has a bad alignment at compile time |
c2b56224 | 672 | |
673 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 674 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 675 | then |
f2a06be9 | 676 | declare |
677 | AL : Uint := Alignment (Typ); | |
678 | ||
679 | begin | |
680 | -- The object alignment might be more restrictive than the | |
681 | -- type alignment. | |
682 | ||
683 | if Known_Alignment (E) then | |
684 | AL := Alignment (E); | |
685 | end if; | |
686 | ||
687 | if Expr_Value (Expr) mod AL /= 0 then | |
0577b0b1 | 688 | Compile_Time_Bad_Alignment; |
689 | else | |
690 | return; | |
f2a06be9 | 691 | end if; |
692 | end; | |
c2b56224 | 693 | |
0577b0b1 | 694 | -- If the expression has the form X'Address, then we can find out if |
695 | -- the object X has an alignment that is compatible with the object E. | |
d6da7448 | 696 | -- If it hasn't or we don't know, we defer issuing the warning until |
697 | -- the end of the compilation to take into account back end annotations. | |
c2b56224 | 698 | |
0577b0b1 | 699 | elsif Nkind (Expr) = N_Attribute_Reference |
700 | and then Attribute_Name (Expr) = Name_Address | |
d6da7448 | 701 | and then Has_Compatible_Alignment (E, Prefix (Expr)) = Known_Compatible |
0577b0b1 | 702 | then |
d6da7448 | 703 | return; |
0577b0b1 | 704 | end if; |
c2b56224 | 705 | |
6fb3c314 | 706 | -- Here we do not know if the value is acceptable. Strictly we don't |
707 | -- have to do anything, since if the alignment is bad, we have an | |
708 | -- erroneous program. However we are allowed to check for erroneous | |
709 | -- conditions and we decide to do this by default if the check is not | |
710 | -- suppressed. | |
0577b0b1 | 711 | |
712 | -- However, don't do the check if elaboration code is unwanted | |
713 | ||
714 | if Restriction_Active (No_Elaboration_Code) then | |
715 | return; | |
716 | ||
717 | -- Generate a check to raise PE if alignment may be inappropriate | |
718 | ||
719 | else | |
720 | -- If the original expression is a non-static constant, use the | |
721 | -- name of the constant itself rather than duplicating its | |
00c403ee | 722 | -- defining expression, which was extracted above. |
0577b0b1 | 723 | |
00c403ee | 724 | -- Note: Expr is empty if the address-clause is applied to in-mode |
725 | -- actuals (allowed by 13.1(22)). | |
726 | ||
727 | if not Present (Expr) | |
728 | or else | |
729 | (Is_Entity_Name (Expression (AC)) | |
730 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
731 | and then Nkind (Parent (Entity (Expression (AC)))) | |
732 | = N_Object_Declaration) | |
0577b0b1 | 733 | then |
734 | Expr := New_Copy_Tree (Expression (AC)); | |
735 | else | |
736 | Remove_Side_Effects (Expr); | |
c2b56224 | 737 | end if; |
c2b56224 | 738 | |
d950dc79 | 739 | if No (Actions (N)) then |
740 | Set_Actions (N, New_List); | |
741 | end if; | |
742 | ||
743 | Prepend_To (Actions (N), | |
0577b0b1 | 744 | Make_Raise_Program_Error (Loc, |
745 | Condition => | |
746 | Make_Op_Ne (Loc, | |
747 | Left_Opnd => | |
748 | Make_Op_Mod (Loc, | |
749 | Left_Opnd => | |
750 | Unchecked_Convert_To | |
751 | (RTE (RE_Integer_Address), Expr), | |
752 | Right_Opnd => | |
753 | Make_Attribute_Reference (Loc, | |
d950dc79 | 754 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 755 | Attribute_Name => Name_Alignment)), |
756 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
d950dc79 | 757 | Reason => PE_Misaligned_Address_Value)); |
758 | Analyze (First (Actions (N)), Suppress => All_Checks); | |
0577b0b1 | 759 | return; |
760 | end if; | |
9dfe12ae | 761 | |
762 | exception | |
0577b0b1 | 763 | -- If we have some missing run time component in configurable run time |
764 | -- mode then just skip the check (it is not required in any case). | |
765 | ||
9dfe12ae | 766 | when RE_Not_Available => |
767 | return; | |
0577b0b1 | 768 | end Apply_Address_Clause_Check; |
c2b56224 | 769 | |
ee6ba406 | 770 | ------------------------------------- |
771 | -- Apply_Arithmetic_Overflow_Check -- | |
772 | ------------------------------------- | |
773 | ||
3cce7f32 | 774 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
775 | begin | |
776 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 777 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 778 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 779 | |
0df9d43f | 780 | if Overflow_Check_Mode = Strict |
3cce7f32 | 781 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
782 | then | |
0df9d43f | 783 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 784 | |
21a55437 | 785 | -- Otherwise use the new routine for the case of a signed integer |
786 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
787 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 788 | |
789 | else | |
790 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
791 | end if; | |
792 | end Apply_Arithmetic_Overflow_Check; | |
793 | ||
0df9d43f | 794 | -------------------------------------- |
795 | -- Apply_Arithmetic_Overflow_Strict -- | |
796 | -------------------------------------- | |
3cce7f32 | 797 | |
f40f9731 | 798 | -- This routine is called only if the type is an integer type, and a |
799 | -- software arithmetic overflow check may be needed for op (add, subtract, | |
800 | -- or multiply). This check is performed only if Software_Overflow_Checking | |
801 | -- is enabled and Do_Overflow_Check is set. In this case we expand the | |
802 | -- operation into a more complex sequence of tests that ensures that | |
803 | -- overflow is properly caught. | |
ee6ba406 | 804 | |
0df9d43f | 805 | -- This is used in CHECKED modes. It is identical to the code for this |
806 | -- cases before the big overflow earthquake, thus ensuring that in this | |
807 | -- modes we have compatible behavior (and reliability) to what was there | |
808 | -- before. It is also called for types other than signed integers, and if | |
809 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 810 | |
811 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
812 | -- to give up and just generate an overflow check without any fuss. | |
813 | ||
0df9d43f | 814 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 815 | Loc : constant Source_Ptr := Sloc (N); |
816 | Typ : constant Entity_Id := Etype (N); | |
817 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 818 | |
819 | begin | |
0df9d43f | 820 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
821 | -- suppressed. | |
822 | ||
823 | if not Do_Overflow_Check (N) then | |
824 | return; | |
825 | end if; | |
826 | ||
f40f9731 | 827 | -- An interesting special case. If the arithmetic operation appears as |
828 | -- the operand of a type conversion: | |
829 | ||
830 | -- type1 (x op y) | |
831 | ||
832 | -- and all the following conditions apply: | |
833 | ||
834 | -- arithmetic operation is for a signed integer type | |
835 | -- target type type1 is a static integer subtype | |
836 | -- range of x and y are both included in the range of type1 | |
837 | -- range of x op y is included in the range of type1 | |
838 | -- size of type1 is at least twice the result size of op | |
839 | ||
840 | -- then we don't do an overflow check in any case, instead we transform | |
841 | -- the operation so that we end up with: | |
842 | ||
843 | -- type1 (type1 (x) op type1 (y)) | |
844 | ||
845 | -- This avoids intermediate overflow before the conversion. It is | |
846 | -- explicitly permitted by RM 3.5.4(24): | |
847 | ||
848 | -- For the execution of a predefined operation of a signed integer | |
849 | -- type, the implementation need not raise Constraint_Error if the | |
850 | -- result is outside the base range of the type, so long as the | |
851 | -- correct result is produced. | |
852 | ||
853 | -- It's hard to imagine that any programmer counts on the exception | |
854 | -- being raised in this case, and in any case it's wrong coding to | |
855 | -- have this expectation, given the RM permission. Furthermore, other | |
856 | -- Ada compilers do allow such out of range results. | |
857 | ||
858 | -- Note that we do this transformation even if overflow checking is | |
859 | -- off, since this is precisely about giving the "right" result and | |
860 | -- avoiding the need for an overflow check. | |
861 | ||
8eb4a5eb | 862 | -- Note: this circuit is partially redundant with respect to the similar |
863 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
864 | -- with cases that do not come through here. We still need the following | |
865 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
866 | -- sure not to generate the arithmetic overflow check in these cases | |
867 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
868 | ||
f40f9731 | 869 | if Is_Signed_Integer_Type (Typ) |
870 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 871 | then |
f32c377d | 872 | Conversion_Optimization : declare |
f40f9731 | 873 | Target_Type : constant Entity_Id := |
b6341c67 | 874 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 875 | |
876 | Llo, Lhi : Uint; | |
877 | Rlo, Rhi : Uint; | |
878 | LOK, ROK : Boolean; | |
879 | ||
880 | Vlo : Uint; | |
881 | Vhi : Uint; | |
882 | VOK : Boolean; | |
883 | ||
884 | Tlo : Uint; | |
885 | Thi : Uint; | |
886 | ||
887 | begin | |
888 | if Is_Integer_Type (Target_Type) | |
889 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
890 | then | |
891 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
892 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
893 | ||
9c486805 | 894 | Determine_Range |
895 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
896 | Determine_Range | |
897 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 898 | |
899 | if (LOK and ROK) | |
900 | and then Tlo <= Llo and then Lhi <= Thi | |
901 | and then Tlo <= Rlo and then Rhi <= Thi | |
902 | then | |
9c486805 | 903 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 904 | |
905 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
906 | Rewrite (Left_Opnd (N), | |
907 | Make_Type_Conversion (Loc, | |
908 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
909 | Expression => Relocate_Node (Left_Opnd (N)))); | |
910 | ||
911 | Rewrite (Right_Opnd (N), | |
912 | Make_Type_Conversion (Loc, | |
913 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
914 | Expression => Relocate_Node (Right_Opnd (N)))); | |
915 | ||
780bfb21 | 916 | -- Rewrite the conversion operand so that the original |
917 | -- node is retained, in order to avoid the warning for | |
918 | -- redundant conversions in Resolve_Type_Conversion. | |
919 | ||
920 | Rewrite (N, Relocate_Node (N)); | |
921 | ||
f40f9731 | 922 | Set_Etype (N, Target_Type); |
780bfb21 | 923 | |
f40f9731 | 924 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
925 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
926 | ||
927 | -- Given that the target type is twice the size of the | |
928 | -- source type, overflow is now impossible, so we can | |
929 | -- safely kill the overflow check and return. | |
930 | ||
931 | Set_Do_Overflow_Check (N, False); | |
932 | return; | |
933 | end if; | |
934 | end if; | |
935 | end if; | |
f32c377d | 936 | end Conversion_Optimization; |
ee6ba406 | 937 | end if; |
938 | ||
f40f9731 | 939 | -- Now see if an overflow check is required |
940 | ||
941 | declare | |
942 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
943 | Dsiz : constant Int := Siz * 2; | |
944 | Opnod : Node_Id; | |
945 | Ctyp : Entity_Id; | |
946 | Opnd : Node_Id; | |
947 | Cent : RE_Id; | |
ee6ba406 | 948 | |
f40f9731 | 949 | begin |
950 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 951 | -- is not set anyway, or we are not doing code expansion, or the |
952 | -- parent node is a type conversion whose operand is an arithmetic | |
953 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 954 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 955 | |
f40f9731 | 956 | -- Special case CLI target, where arithmetic overflow checks can be |
957 | -- performed for integer and long_integer | |
ee6ba406 | 958 | |
f40f9731 | 959 | if Backend_Overflow_Checks_On_Target |
960 | or else not Do_Overflow_Check (N) | |
6dbcfcd9 | 961 | or else not Full_Expander_Active |
df40eeb0 | 962 | or else (Present (Parent (N)) |
963 | and then Nkind (Parent (N)) = N_Type_Conversion | |
964 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 965 | or else |
966 | (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size) | |
967 | then | |
968 | return; | |
969 | end if; | |
ee6ba406 | 970 | |
f40f9731 | 971 | -- Otherwise, generate the full general code for front end overflow |
972 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 973 | |
f40f9731 | 974 | -- x op y |
ee6ba406 | 975 | |
f40f9731 | 976 | -- is expanded into |
ee6ba406 | 977 | |
f40f9731 | 978 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 979 | |
f40f9731 | 980 | -- where Typ is the type of the original expression, and Checktyp is |
981 | -- an integer type of sufficient length to hold the largest possible | |
982 | -- result. | |
ee6ba406 | 983 | |
f40f9731 | 984 | -- If the size of check type exceeds the size of Long_Long_Integer, |
985 | -- we use a different approach, expanding to: | |
ee6ba406 | 986 | |
f40f9731 | 987 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 988 | |
f40f9731 | 989 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 990 | |
f40f9731 | 991 | -- Find check type if one exists |
992 | ||
993 | if Dsiz <= Standard_Integer_Size then | |
994 | Ctyp := Standard_Integer; | |
ee6ba406 | 995 | |
f40f9731 | 996 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
997 | Ctyp := Standard_Long_Long_Integer; | |
998 | ||
999 | -- No check type exists, use runtime call | |
ee6ba406 | 1000 | |
1001 | else | |
f40f9731 | 1002 | if Nkind (N) = N_Op_Add then |
1003 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1004 | |
f40f9731 | 1005 | elsif Nkind (N) = N_Op_Multiply then |
1006 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1007 | |
f40f9731 | 1008 | else |
1009 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1010 | Cent := RE_Subtract_With_Ovflo_Check; | |
1011 | end if; | |
1012 | ||
1013 | Rewrite (N, | |
1014 | OK_Convert_To (Typ, | |
1015 | Make_Function_Call (Loc, | |
1016 | Name => New_Reference_To (RTE (Cent), Loc), | |
1017 | Parameter_Associations => New_List ( | |
1018 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1019 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1020 | |
f40f9731 | 1021 | Analyze_And_Resolve (N, Typ); |
1022 | return; | |
1023 | end if; | |
ee6ba406 | 1024 | |
f40f9731 | 1025 | -- If we fall through, we have the case where we do the arithmetic |
1026 | -- in the next higher type and get the check by conversion. In these | |
1027 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1028 | |
f40f9731 | 1029 | Opnod := Relocate_Node (N); |
ee6ba406 | 1030 | |
f40f9731 | 1031 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1032 | |
f40f9731 | 1033 | Analyze (Opnd); |
1034 | Set_Etype (Opnd, Ctyp); | |
1035 | Set_Analyzed (Opnd, True); | |
1036 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1037 | |
f40f9731 | 1038 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1039 | |
f40f9731 | 1040 | Analyze (Opnd); |
1041 | Set_Etype (Opnd, Ctyp); | |
1042 | Set_Analyzed (Opnd, True); | |
1043 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1044 | |
f40f9731 | 1045 | -- The type of the operation changes to the base type of the check |
1046 | -- type, and we reset the overflow check indication, since clearly no | |
1047 | -- overflow is possible now that we are using a double length type. | |
1048 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1049 | -- expand the node. | |
ee6ba406 | 1050 | |
f40f9731 | 1051 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1052 | Set_Do_Overflow_Check (Opnod, False); | |
1053 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1054 | |
f40f9731 | 1055 | -- Now build the outer conversion |
ee6ba406 | 1056 | |
f40f9731 | 1057 | Opnd := OK_Convert_To (Typ, Opnod); |
1058 | Analyze (Opnd); | |
1059 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1060 | |
f40f9731 | 1061 | -- In the discrete type case, we directly generate the range check |
1062 | -- for the outer operand. This range check will implement the | |
1063 | -- required overflow check. | |
9dfe12ae | 1064 | |
f40f9731 | 1065 | if Is_Discrete_Type (Typ) then |
1066 | Rewrite (N, Opnd); | |
1067 | Generate_Range_Check | |
1068 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1069 | |
f40f9731 | 1070 | -- For other types, we enable overflow checking on the conversion, |
1071 | -- after setting the node as analyzed to prevent recursive attempts | |
1072 | -- to expand the conversion node. | |
9dfe12ae | 1073 | |
f40f9731 | 1074 | else |
1075 | Set_Analyzed (Opnd, True); | |
1076 | Enable_Overflow_Check (Opnd); | |
1077 | Rewrite (N, Opnd); | |
1078 | end if; | |
1079 | ||
1080 | exception | |
1081 | when RE_Not_Available => | |
1082 | return; | |
1083 | end; | |
0df9d43f | 1084 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1085 | |
1086 | ---------------------------------------------------- | |
1087 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1088 | ---------------------------------------------------- | |
1089 | ||
1090 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1091 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1092 | |
1093 | Loc : constant Source_Ptr := Sloc (Op); | |
1094 | P : constant Node_Id := Parent (Op); | |
1095 | ||
49b3a812 | 1096 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1097 | -- Operands and results are of this type when we convert | |
1098 | ||
3cce7f32 | 1099 | Result_Type : constant Entity_Id := Etype (Op); |
1100 | -- Original result type | |
1101 | ||
db415383 | 1102 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1103 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1104 | ||
1105 | Lo, Hi : Uint; | |
1106 | -- Ranges of values for result | |
1107 | ||
1108 | begin | |
1109 | -- Nothing to do if our parent is one of the following: | |
1110 | ||
0326b4d4 | 1111 | -- Another signed integer arithmetic op |
3cce7f32 | 1112 | -- A membership operation |
1113 | -- A comparison operation | |
1114 | ||
1115 | -- In all these cases, we will process at the higher level (and then | |
1116 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1117 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1118 | |
1119 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1120 | or else Nkind (P) in N_Membership_Test |
1121 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1122 | |
70a2dff4 | 1123 | -- This is also true for an alternative in a case expression |
1124 | ||
1125 | or else Nkind (P) = N_Case_Expression_Alternative | |
1126 | ||
1127 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1128 | |
b8a17a21 | 1129 | or else (Nkind (P) = N_Range |
1130 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1131 | then |
1132 | return; | |
1133 | end if; | |
1134 | ||
0326b4d4 | 1135 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1136 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1137 | -- modes. This is the case where we tell the machinery not to move into | |
1138 | -- Bignum mode at this top level (of course the top level operation | |
1139 | -- will still be in Bignum mode if either of its operands are of type | |
1140 | -- Bignum). | |
3cce7f32 | 1141 | |
0df9d43f | 1142 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1143 | |
1144 | -- That call may but does not necessarily change the result type of Op. | |
1145 | -- It is the job of this routine to undo such changes, so that at the | |
1146 | -- top level, we have the proper type. This "undoing" is a point at | |
1147 | -- which a final overflow check may be applied. | |
1148 | ||
f32c377d | 1149 | -- If the result type was not fiddled we are all set. We go to base |
1150 | -- types here because things may have been rewritten to generate the | |
1151 | -- base type of the operand types. | |
3cce7f32 | 1152 | |
f32c377d | 1153 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1154 | return; |
1155 | ||
1156 | -- Bignum case | |
1157 | ||
49b3a812 | 1158 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1159 | |
d94b5da2 | 1160 | -- We need a sequence that looks like: |
3cce7f32 | 1161 | |
1162 | -- Rnn : Result_Type; | |
1163 | ||
1164 | -- declare | |
d94b5da2 | 1165 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1166 | -- begin |
49b3a812 | 1167 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1168 | -- SS_Release (M); |
1169 | -- end; | |
1170 | ||
1171 | -- This block is inserted (using Insert_Actions), and then the node | |
1172 | -- is replaced with a reference to Rnn. | |
1173 | ||
1174 | -- A special case arises if our parent is a conversion node. In this | |
1175 | -- case no point in generating a conversion to Result_Type, we will | |
1176 | -- let the parent handle this. Note that this special case is not | |
1177 | -- just about optimization. Consider | |
1178 | ||
1179 | -- A,B,C : Integer; | |
1180 | -- ... | |
49b3a812 | 1181 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1182 | |
1183 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1184 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1185 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1186 | |
1187 | declare | |
49b3a812 | 1188 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1189 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1190 | RHS : Node_Id; | |
1191 | ||
1192 | Rtype : Entity_Id; | |
1193 | ||
1194 | begin | |
1195 | RHS := Convert_From_Bignum (Op); | |
1196 | ||
1197 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1198 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1199 | Rtype := Result_Type; |
1200 | ||
1201 | -- Interesting question, do we need a check on that conversion | |
1202 | -- operation. Answer, not if we know the result is in range. | |
1203 | -- At the moment we are not taking advantage of this. To be | |
1204 | -- looked at later ??? | |
1205 | ||
1206 | else | |
49b3a812 | 1207 | Rtype := LLIB; |
3cce7f32 | 1208 | end if; |
1209 | ||
1210 | Insert_Before | |
1211 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1212 | Make_Assignment_Statement (Loc, | |
1213 | Name => New_Occurrence_Of (Rnn, Loc), | |
1214 | Expression => RHS)); | |
1215 | ||
1216 | Insert_Actions (Op, New_List ( | |
1217 | Make_Object_Declaration (Loc, | |
1218 | Defining_Identifier => Rnn, | |
1219 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1220 | Blk)); | |
1221 | ||
1222 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1223 | Analyze_And_Resolve (Op); | |
1224 | end; | |
1225 | ||
412f75eb | 1226 | -- Here we know the result is Long_Long_Integer'Base, of that it has |
1227 | -- been rewritten because the parent operation is a conversion. See | |
0df9d43f | 1228 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1229 | |
1230 | else | |
f32c377d | 1231 | pragma Assert |
1232 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1233 | |
1234 | -- All we need to do here is to convert the result to the proper | |
1235 | -- result type. As explained above for the Bignum case, we can | |
1236 | -- omit this if our parent is a type conversion. | |
1237 | ||
1238 | if Nkind (P) /= N_Type_Conversion then | |
1239 | Convert_To_And_Rewrite (Result_Type, Op); | |
1240 | end if; | |
1241 | ||
1242 | Analyze_And_Resolve (Op); | |
1243 | end if; | |
1244 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1245 | |
ee6ba406 | 1246 | ---------------------------- |
1247 | -- Apply_Constraint_Check -- | |
1248 | ---------------------------- | |
1249 | ||
1250 | procedure Apply_Constraint_Check | |
1251 | (N : Node_Id; | |
1252 | Typ : Entity_Id; | |
1253 | No_Sliding : Boolean := False) | |
1254 | is | |
1255 | Desig_Typ : Entity_Id; | |
1256 | ||
1257 | begin | |
7aafae1c | 1258 | -- No checks inside a generic (check the instantiations) |
1259 | ||
ee6ba406 | 1260 | if Inside_A_Generic then |
1261 | return; | |
7aafae1c | 1262 | end if; |
ee6ba406 | 1263 | |
6fb3c314 | 1264 | -- Apply required constraint checks |
7aafae1c | 1265 | |
1266 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1267 | Apply_Scalar_Range_Check (N, Typ); |
1268 | ||
1269 | elsif Is_Array_Type (Typ) then | |
1270 | ||
05fcfafb | 1271 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1272 | -- always has the right bounds. |
1273 | ||
1274 | if Nkind (N) = N_Aggregate | |
1275 | and then No (Expressions (N)) | |
1276 | and then Nkind | |
1277 | (First (Choices (First (Component_Associations (N))))) | |
1278 | = N_Others_Choice | |
1279 | then | |
1280 | return; | |
1281 | end if; | |
1282 | ||
ee6ba406 | 1283 | if Is_Constrained (Typ) then |
1284 | Apply_Length_Check (N, Typ); | |
1285 | ||
1286 | if No_Sliding then | |
1287 | Apply_Range_Check (N, Typ); | |
1288 | end if; | |
1289 | else | |
1290 | Apply_Range_Check (N, Typ); | |
1291 | end if; | |
1292 | ||
4fb5f0a0 | 1293 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1294 | and then Has_Discriminants (Base_Type (Typ)) |
1295 | and then Is_Constrained (Typ) | |
1296 | then | |
1297 | Apply_Discriminant_Check (N, Typ); | |
1298 | ||
1299 | elsif Is_Access_Type (Typ) then | |
1300 | ||
1301 | Desig_Typ := Designated_Type (Typ); | |
1302 | ||
1303 | -- No checks necessary if expression statically null | |
1304 | ||
2af58f67 | 1305 | if Known_Null (N) then |
00c403ee | 1306 | if Can_Never_Be_Null (Typ) then |
1307 | Install_Null_Excluding_Check (N); | |
1308 | end if; | |
ee6ba406 | 1309 | |
1310 | -- No sliding possible on access to arrays | |
1311 | ||
1312 | elsif Is_Array_Type (Desig_Typ) then | |
1313 | if Is_Constrained (Desig_Typ) then | |
1314 | Apply_Length_Check (N, Typ); | |
1315 | end if; | |
1316 | ||
1317 | Apply_Range_Check (N, Typ); | |
1318 | ||
1319 | elsif Has_Discriminants (Base_Type (Desig_Typ)) | |
1320 | and then Is_Constrained (Desig_Typ) | |
1321 | then | |
1322 | Apply_Discriminant_Check (N, Typ); | |
1323 | end if; | |
fa7497e8 | 1324 | |
bf3e1520 | 1325 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1326 | -- this check if the constraint node is illegal, as shown by having |
1327 | -- an error posted. This additional guard prevents cascaded errors | |
1328 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1329 | ||
fa7497e8 | 1330 | if Can_Never_Be_Null (Typ) |
1331 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1332 | and then not Error_Posted (N) |
fa7497e8 | 1333 | then |
1334 | Install_Null_Excluding_Check (N); | |
1335 | end if; | |
ee6ba406 | 1336 | end if; |
1337 | end Apply_Constraint_Check; | |
1338 | ||
1339 | ------------------------------ | |
1340 | -- Apply_Discriminant_Check -- | |
1341 | ------------------------------ | |
1342 | ||
1343 | procedure Apply_Discriminant_Check | |
1344 | (N : Node_Id; | |
1345 | Typ : Entity_Id; | |
1346 | Lhs : Node_Id := Empty) | |
1347 | is | |
1348 | Loc : constant Source_Ptr := Sloc (N); | |
1349 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1350 | S_Typ : Entity_Id := Etype (N); | |
1351 | Cond : Node_Id; | |
1352 | T_Typ : Entity_Id; | |
1353 | ||
7be5088a | 1354 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1355 | -- A heap object with an indefinite subtype is constrained by its | |
1356 | -- initial value, and assigning to it requires a constraint_check. | |
1357 | -- The target may be an explicit dereference, or a renaming of one. | |
1358 | ||
ee6ba406 | 1359 | function Is_Aliased_Unconstrained_Component return Boolean; |
1360 | -- It is possible for an aliased component to have a nominal | |
1361 | -- unconstrained subtype (through instantiation). If this is a | |
1362 | -- discriminated component assigned in the expansion of an aggregate | |
1363 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1364 | -- situation requires a predicate of its own. |
ee6ba406 | 1365 | |
7be5088a | 1366 | ---------------------------------- |
1367 | -- Denotes_Explicit_Dereference -- | |
1368 | ---------------------------------- | |
1369 | ||
1370 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1371 | begin | |
1372 | return | |
1373 | Nkind (Obj) = N_Explicit_Dereference | |
1374 | or else | |
1375 | (Is_Entity_Name (Obj) | |
1376 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1377 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1378 | N_Explicit_Dereference); | |
7be5088a | 1379 | end Denotes_Explicit_Dereference; |
1380 | ||
ee6ba406 | 1381 | ---------------------------------------- |
1382 | -- Is_Aliased_Unconstrained_Component -- | |
1383 | ---------------------------------------- | |
1384 | ||
1385 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1386 | Comp : Entity_Id; | |
1387 | Pref : Node_Id; | |
1388 | ||
1389 | begin | |
1390 | if Nkind (Lhs) /= N_Selected_Component then | |
1391 | return False; | |
1392 | else | |
1393 | Comp := Entity (Selector_Name (Lhs)); | |
1394 | Pref := Prefix (Lhs); | |
1395 | end if; | |
1396 | ||
1397 | if Ekind (Comp) /= E_Component | |
1398 | or else not Is_Aliased (Comp) | |
1399 | then | |
1400 | return False; | |
1401 | end if; | |
1402 | ||
1403 | return not Comes_From_Source (Pref) | |
1404 | and then In_Instance | |
1405 | and then not Is_Constrained (Etype (Comp)); | |
1406 | end Is_Aliased_Unconstrained_Component; | |
1407 | ||
1408 | -- Start of processing for Apply_Discriminant_Check | |
1409 | ||
1410 | begin | |
1411 | if Do_Access then | |
1412 | T_Typ := Designated_Type (Typ); | |
1413 | else | |
1414 | T_Typ := Typ; | |
1415 | end if; | |
1416 | ||
1417 | -- Nothing to do if discriminant checks are suppressed or else no code | |
1418 | -- is to be generated | |
1419 | ||
6dbcfcd9 | 1420 | if not Full_Expander_Active |
ee6ba406 | 1421 | or else Discriminant_Checks_Suppressed (T_Typ) |
1422 | then | |
1423 | return; | |
1424 | end if; | |
1425 | ||
feff2f05 | 1426 | -- No discriminant checks necessary for an access when expression is |
1427 | -- statically Null. This is not only an optimization, it is fundamental | |
1428 | -- because otherwise discriminant checks may be generated in init procs | |
1429 | -- for types containing an access to a not-yet-frozen record, causing a | |
1430 | -- deadly forward reference. | |
ee6ba406 | 1431 | |
feff2f05 | 1432 | -- Also, if the expression is of an access type whose designated type is |
1433 | -- incomplete, then the access value must be null and we suppress the | |
1434 | -- check. | |
ee6ba406 | 1435 | |
2af58f67 | 1436 | if Known_Null (N) then |
ee6ba406 | 1437 | return; |
1438 | ||
1439 | elsif Is_Access_Type (S_Typ) then | |
1440 | S_Typ := Designated_Type (S_Typ); | |
1441 | ||
1442 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1443 | return; | |
1444 | end if; | |
1445 | end if; | |
1446 | ||
0577b0b1 | 1447 | -- If an assignment target is present, then we need to generate the |
1448 | -- actual subtype if the target is a parameter or aliased object with | |
1449 | -- an unconstrained nominal subtype. | |
1450 | ||
1451 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1452 | -- subtype to the parameter and dereference cases, since other aliased | |
1453 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1454 | -- constrained). |
ee6ba406 | 1455 | |
1456 | if Present (Lhs) | |
1457 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1458 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1459 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1460 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1461 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1462 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1463 | and then not Is_Constrained (T_Typ) |
7be5088a | 1464 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1465 | and then Nkind (Original_Node (Lhs)) /= |
1466 | N_Function_Call)) | |
ee6ba406 | 1467 | then |
1468 | T_Typ := Get_Actual_Subtype (Lhs); | |
1469 | end if; | |
1470 | ||
feff2f05 | 1471 | -- Nothing to do if the type is unconstrained (this is the case where |
1472 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1473 | -- is required). | |
ee6ba406 | 1474 | |
1475 | if not Is_Constrained (T_Typ) then | |
1476 | return; | |
05fcfafb | 1477 | |
1478 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1479 | -- partial view that is constrained. | |
1480 | ||
de54c5ab | 1481 | elsif Ada_Version >= Ada_2005 |
d41a3f41 | 1482 | and then Effectively_Has_Constrained_Partial_View |
1483 | (Typ => Base_Type (T_Typ), | |
1484 | Scop => Current_Scope) | |
05fcfafb | 1485 | then |
1486 | return; | |
ee6ba406 | 1487 | end if; |
1488 | ||
00f91aef | 1489 | -- Nothing to do if the type is an Unchecked_Union |
1490 | ||
1491 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1492 | return; | |
1493 | end if; | |
1494 | ||
feff2f05 | 1495 | -- Suppress checks if the subtypes are the same. the check must be |
1496 | -- preserved in an assignment to a formal, because the constraint is | |
1497 | -- given by the actual. | |
ee6ba406 | 1498 | |
1499 | if Nkind (Original_Node (N)) /= N_Allocator | |
1500 | and then (No (Lhs) | |
1501 | or else not Is_Entity_Name (Lhs) | |
9dfe12ae | 1502 | or else No (Param_Entity (Lhs))) |
ee6ba406 | 1503 | then |
1504 | if (Etype (N) = Typ | |
1505 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1506 | and then not Is_Aliased_View (Lhs) | |
1507 | then | |
1508 | return; | |
1509 | end if; | |
1510 | ||
feff2f05 | 1511 | -- We can also eliminate checks on allocators with a subtype mark that |
1512 | -- coincides with the context type. The context type may be a subtype | |
1513 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1514 | |
1515 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1516 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1517 | then | |
1518 | declare | |
9dfe12ae | 1519 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1520 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1521 | |
1522 | begin | |
1523 | if Alloc_Typ = T_Typ | |
1524 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1525 | and then Is_Entity_Name ( | |
1526 | Subtype_Indication (Parent (T_Typ))) | |
1527 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1528 | ||
1529 | then | |
1530 | return; | |
1531 | end if; | |
1532 | end; | |
1533 | end if; | |
1534 | ||
feff2f05 | 1535 | -- See if we have a case where the types are both constrained, and all |
1536 | -- the constraints are constants. In this case, we can do the check | |
1537 | -- successfully at compile time. | |
ee6ba406 | 1538 | |
9dfe12ae | 1539 | -- We skip this check for the case where the node is a rewritten` |
ee6ba406 | 1540 | -- allocator, because it already carries the context subtype, and |
1541 | -- extracting the discriminants from the aggregate is messy. | |
1542 | ||
1543 | if Is_Constrained (S_Typ) | |
1544 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1545 | then | |
1546 | declare | |
1547 | DconT : Elmt_Id; | |
1548 | Discr : Entity_Id; | |
1549 | DconS : Elmt_Id; | |
1550 | ItemS : Node_Id; | |
1551 | ItemT : Node_Id; | |
1552 | ||
1553 | begin | |
1554 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1555 | -- private type completed by a default discriminated type. In that |
1556 | -- case, we need to get the constraints from the underlying_type. | |
1557 | -- If the underlying type is unconstrained (i.e. has no default | |
1558 | -- discriminants) no check is needed. | |
ee6ba406 | 1559 | |
1560 | if Has_Discriminants (S_Typ) then | |
1561 | Discr := First_Discriminant (S_Typ); | |
1562 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1563 | ||
1564 | else | |
1565 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1566 | DconS := | |
1567 | First_Elmt | |
1568 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1569 | ||
1570 | if No (DconS) then | |
1571 | return; | |
1572 | end if; | |
fccb5da7 | 1573 | |
1574 | -- A further optimization: if T_Typ is derived from S_Typ | |
1575 | -- without imposing a constraint, no check is needed. | |
1576 | ||
1577 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1578 | N_Full_Type_Declaration | |
1579 | then | |
1580 | declare | |
5c61a0ff | 1581 | Type_Def : constant Node_Id := |
b6341c67 | 1582 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1583 | begin |
1584 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1585 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1586 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1587 | then | |
1588 | return; | |
1589 | end if; | |
1590 | end; | |
1591 | end if; | |
ee6ba406 | 1592 | end if; |
1593 | ||
1594 | DconT := First_Elmt (Discriminant_Constraint (T_Typ)); | |
1595 | ||
1596 | while Present (Discr) loop | |
1597 | ItemS := Node (DconS); | |
1598 | ItemT := Node (DconT); | |
1599 | ||
00c403ee | 1600 | -- For a discriminated component type constrained by the |
1601 | -- current instance of an enclosing type, there is no | |
1602 | -- applicable discriminant check. | |
1603 | ||
1604 | if Nkind (ItemT) = N_Attribute_Reference | |
1605 | and then Is_Access_Type (Etype (ItemT)) | |
1606 | and then Is_Entity_Name (Prefix (ItemT)) | |
1607 | and then Is_Type (Entity (Prefix (ItemT))) | |
1608 | then | |
1609 | return; | |
1610 | end if; | |
1611 | ||
cc60bd16 | 1612 | -- If the expressions for the discriminants are identical |
1613 | -- and it is side-effect free (for now just an entity), | |
1614 | -- this may be a shared constraint, e.g. from a subtype | |
1615 | -- without a constraint introduced as a generic actual. | |
1616 | -- Examine other discriminants if any. | |
1617 | ||
1618 | if ItemS = ItemT | |
1619 | and then Is_Entity_Name (ItemS) | |
1620 | then | |
1621 | null; | |
1622 | ||
1623 | elsif not Is_OK_Static_Expression (ItemS) | |
1624 | or else not Is_OK_Static_Expression (ItemT) | |
1625 | then | |
1626 | exit; | |
ee6ba406 | 1627 | |
cc60bd16 | 1628 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1629 | if Do_Access then -- needs run-time check. |
1630 | exit; | |
1631 | else | |
1632 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1633 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1634 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1635 | return; |
1636 | end if; | |
1637 | end if; | |
1638 | ||
1639 | Next_Elmt (DconS); | |
1640 | Next_Elmt (DconT); | |
1641 | Next_Discriminant (Discr); | |
1642 | end loop; | |
1643 | ||
1644 | if No (Discr) then | |
1645 | return; | |
1646 | end if; | |
1647 | end; | |
1648 | end if; | |
1649 | ||
1650 | -- Here we need a discriminant check. First build the expression | |
1651 | -- for the comparisons of the discriminants: | |
1652 | ||
1653 | -- (n.disc1 /= typ.disc1) or else | |
1654 | -- (n.disc2 /= typ.disc2) or else | |
1655 | -- ... | |
1656 | -- (n.discn /= typ.discn) | |
1657 | ||
1658 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1659 | ||
3cce7f32 | 1660 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1661 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1662 | |
1663 | if Present (Param_Entity (Lhs)) then | |
1664 | Cond := | |
1665 | Make_And_Then (Loc, | |
1666 | Left_Opnd => | |
1667 | Make_Attribute_Reference (Loc, | |
1668 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1669 | Attribute_Name => Name_Constrained), | |
1670 | Right_Opnd => Cond); | |
1671 | end if; | |
1672 | ||
1673 | if Do_Access then | |
1674 | Cond := Guard_Access (Cond, Loc, N); | |
1675 | end if; | |
1676 | ||
1677 | Insert_Action (N, | |
f15731c4 | 1678 | Make_Raise_Constraint_Error (Loc, |
1679 | Condition => Cond, | |
1680 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1681 | end Apply_Discriminant_Check; |
1682 | ||
2fe22c69 | 1683 | ------------------------- |
1684 | -- Apply_Divide_Checks -- | |
1685 | ------------------------- | |
ee6ba406 | 1686 | |
2fe22c69 | 1687 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1688 | Loc : constant Source_Ptr := Sloc (N); |
1689 | Typ : constant Entity_Id := Etype (N); | |
1690 | Left : constant Node_Id := Left_Opnd (N); | |
1691 | Right : constant Node_Id := Right_Opnd (N); | |
1692 | ||
db415383 | 1693 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1694 | -- Current overflow checking mode |
1695 | ||
ee6ba406 | 1696 | LLB : Uint; |
1697 | Llo : Uint; | |
1698 | Lhi : Uint; | |
1699 | LOK : Boolean; | |
1700 | Rlo : Uint; | |
1701 | Rhi : Uint; | |
2fe22c69 | 1702 | ROK : Boolean; |
96da3284 | 1703 | |
1704 | pragma Warnings (Off, Lhi); | |
1705 | -- Don't actually use this value | |
ee6ba406 | 1706 | |
1707 | begin | |
0df9d43f | 1708 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1709 | -- operating on signed integer types, then the only thing this routine | |
1710 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1711 | -- procedure will (possibly later on during recursive downward calls), | |
1712 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1713 | |
1714 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1715 | and then Is_Signed_Integer_Type (Typ) |
1716 | then | |
1717 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1718 | return; | |
1719 | end if; | |
1720 | ||
1721 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1722 | ||
6dbcfcd9 | 1723 | if Full_Expander_Active |
13dbf220 | 1724 | and then not Backend_Divide_Checks_On_Target |
1725 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1726 | then |
9c486805 | 1727 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1728 | |
2fe22c69 | 1729 | -- Deal with division check |
ee6ba406 | 1730 | |
2fe22c69 | 1731 | if Do_Division_Check (N) |
1732 | and then not Division_Checks_Suppressed (Typ) | |
1733 | then | |
1734 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1735 | end if; |
1736 | ||
2fe22c69 | 1737 | -- Deal with overflow check |
1738 | ||
0df9d43f | 1739 | if Do_Overflow_Check (N) |
1740 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1741 | then | |
2fe22c69 | 1742 | |
1743 | -- Test for extremely annoying case of xxx'First divided by -1 | |
1744 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1745 | |
ee6ba406 | 1746 | if Nkind (N) = N_Op_Divide |
1747 | and then Is_Signed_Integer_Type (Typ) | |
1748 | then | |
9c486805 | 1749 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1750 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1751 | ||
1752 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1753 | and then |
1754 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1755 | then |
1756 | Insert_Action (N, | |
1757 | Make_Raise_Constraint_Error (Loc, | |
1758 | Condition => | |
1759 | Make_And_Then (Loc, | |
2fe22c69 | 1760 | Left_Opnd => |
1761 | Make_Op_Eq (Loc, | |
1762 | Left_Opnd => | |
1763 | Duplicate_Subexpr_Move_Checks (Left), | |
1764 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1765 | |
2fe22c69 | 1766 | Right_Opnd => |
1767 | Make_Op_Eq (Loc, | |
1768 | Left_Opnd => Duplicate_Subexpr (Right), | |
1769 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1770 | |
f15731c4 | 1771 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1772 | end if; |
1773 | end if; | |
1774 | end if; | |
1775 | end if; | |
2fe22c69 | 1776 | end Apply_Divide_Checks; |
1777 | ||
1778 | -------------------------- | |
1779 | -- Apply_Division_Check -- | |
1780 | -------------------------- | |
1781 | ||
1782 | procedure Apply_Division_Check | |
1783 | (N : Node_Id; | |
1784 | Rlo : Uint; | |
1785 | Rhi : Uint; | |
1786 | ROK : Boolean) | |
1787 | is | |
1788 | pragma Assert (Do_Division_Check (N)); | |
1789 | ||
1790 | Loc : constant Source_Ptr := Sloc (N); | |
1791 | Right : constant Node_Id := Right_Opnd (N); | |
1792 | ||
1793 | begin | |
1794 | if Full_Expander_Active | |
1795 | and then not Backend_Divide_Checks_On_Target | |
1796 | and then Check_Needed (Right, Division_Check) | |
1797 | then | |
1798 | -- See if division by zero possible, and if so generate test. This | |
1799 | -- part of the test is not controlled by the -gnato switch, since | |
1800 | -- it is a Division_Check and not an Overflow_Check. | |
1801 | ||
1802 | if Do_Division_Check (N) then | |
1803 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then | |
1804 | Insert_Action (N, | |
1805 | Make_Raise_Constraint_Error (Loc, | |
1806 | Condition => | |
1807 | Make_Op_Eq (Loc, | |
1808 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1809 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
1810 | Reason => CE_Divide_By_Zero)); | |
1811 | end if; | |
1812 | end if; | |
1813 | end if; | |
1814 | end Apply_Division_Check; | |
ee6ba406 | 1815 | |
5329ca64 | 1816 | ---------------------------------- |
1817 | -- Apply_Float_Conversion_Check -- | |
1818 | ---------------------------------- | |
1819 | ||
feff2f05 | 1820 | -- Let F and I be the source and target types of the conversion. The RM |
1821 | -- specifies that a floating-point value X is rounded to the nearest | |
1822 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1823 | -- value of X is checked against I'Range. | |
1824 | ||
1825 | -- The catch in the above paragraph is that there is no good way to know | |
1826 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1827 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1828 | |
5329ca64 | 1829 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1830 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1831 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1832 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1833 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1834 | -- sign of I'First and I'Last. | |
5329ca64 | 1835 | -- (5) X may be a NaN, which will fail any comparison |
1836 | ||
2af58f67 | 1837 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1838 | |
5329ca64 | 1839 | -- (1) If either I'First or I'Last is not known at compile time, use |
1840 | -- I'Base instead of I in the next three steps and perform a | |
1841 | -- regular range check against I'Range after conversion. | |
1842 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1843 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1844 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1845 | -- In other words, take one of the closest floating-point numbers | |
1846 | -- (which is an integer value) to I'First, and see if it is in | |
1847 | -- range or not. | |
5329ca64 | 1848 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1849 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1850 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1851 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1852 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1853 | ||
2af58f67 | 1854 | -- For the truncating case, replace steps (2) and (3) as follows: |
1855 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1856 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1857 | -- Lo_OK be True. | |
1858 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1859 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1860 | -- Hi_OK be True. |
2af58f67 | 1861 | |
5329ca64 | 1862 | procedure Apply_Float_Conversion_Check |
1863 | (Ck_Node : Node_Id; | |
1864 | Target_Typ : Entity_Id) | |
1865 | is | |
feff2f05 | 1866 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1867 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1868 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1869 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1870 | Target_Base : constant Entity_Id := |
b6341c67 | 1871 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1872 | |
2af58f67 | 1873 | Par : constant Node_Id := Parent (Ck_Node); |
1874 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1875 | -- Parent of check node, must be a type conversion | |
1876 | ||
1877 | Truncate : constant Boolean := Float_Truncate (Par); | |
1878 | Max_Bound : constant Uint := | |
b6341c67 | 1879 | UI_Expon |
1880 | (Machine_Radix_Value (Expr_Type), | |
1881 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 1882 | |
5329ca64 | 1883 | -- Largest bound, so bound plus or minus half is a machine number of F |
1884 | ||
feff2f05 | 1885 | Ifirst, Ilast : Uint; |
1886 | -- Bounds of integer type | |
1887 | ||
1888 | Lo, Hi : Ureal; | |
1889 | -- Bounds to check in floating-point domain | |
5329ca64 | 1890 | |
feff2f05 | 1891 | Lo_OK, Hi_OK : Boolean; |
1892 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1893 | |
feff2f05 | 1894 | Lo_Chk, Hi_Chk : Node_Id; |
1895 | -- Expressions that are False iff check fails | |
1896 | ||
1897 | Reason : RT_Exception_Code; | |
5329ca64 | 1898 | |
1899 | begin | |
1900 | if not Compile_Time_Known_Value (LB) | |
1901 | or not Compile_Time_Known_Value (HB) | |
1902 | then | |
1903 | declare | |
feff2f05 | 1904 | -- First check that the value falls in the range of the base type, |
1905 | -- to prevent overflow during conversion and then perform a | |
1906 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 1907 | |
5329ca64 | 1908 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 1909 | |
46eb6933 | 1910 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 1911 | |
1912 | begin | |
1913 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
1914 | Set_Etype (Temp, Target_Base); | |
1915 | ||
1916 | Insert_Action (Parent (Par), | |
1917 | Make_Object_Declaration (Loc, | |
1918 | Defining_Identifier => Temp, | |
1919 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
1920 | Expression => New_Copy_Tree (Par)), | |
1921 | Suppress => All_Checks); | |
1922 | ||
1923 | Insert_Action (Par, | |
1924 | Make_Raise_Constraint_Error (Loc, | |
1925 | Condition => | |
1926 | Make_Not_In (Loc, | |
1927 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
1928 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
1929 | Reason => CE_Range_Check_Failed)); | |
1930 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
1931 | ||
1932 | return; | |
1933 | end; | |
1934 | end if; | |
1935 | ||
7d86aa98 | 1936 | -- Get the (static) bounds of the target type |
5329ca64 | 1937 | |
1938 | Ifirst := Expr_Value (LB); | |
1939 | Ilast := Expr_Value (HB); | |
1940 | ||
7d86aa98 | 1941 | -- A simple optimization: if the expression is a universal literal, |
1942 | -- we can do the comparison with the bounds and the conversion to | |
1943 | -- an integer type statically. The range checks are unchanged. | |
1944 | ||
1945 | if Nkind (Ck_Node) = N_Real_Literal | |
1946 | and then Etype (Ck_Node) = Universal_Real | |
1947 | and then Is_Integer_Type (Target_Typ) | |
1948 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
1949 | then | |
1950 | declare | |
1951 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
1952 | ||
1953 | begin | |
1954 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
1955 | ||
4309515d | 1956 | -- Conversion is safe |
7d86aa98 | 1957 | |
1958 | Rewrite (Parent (Ck_Node), | |
1959 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
1960 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
1961 | return; | |
1962 | end if; | |
1963 | end; | |
1964 | end if; | |
1965 | ||
5329ca64 | 1966 | -- Check against lower bound |
1967 | ||
2af58f67 | 1968 | if Truncate and then Ifirst > 0 then |
1969 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
1970 | Lo_OK := False; | |
1971 | ||
1972 | elsif Truncate then | |
1973 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
1974 | Lo_OK := True; | |
1975 | ||
1976 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 1977 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
1978 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 1979 | |
5329ca64 | 1980 | else |
1981 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
1982 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
1983 | end if; | |
1984 | ||
1985 | if Lo_OK then | |
1986 | ||
1987 | -- Lo_Chk := (X >= Lo) | |
1988 | ||
1989 | Lo_Chk := Make_Op_Ge (Loc, | |
1990 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1991 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
1992 | ||
1993 | else | |
1994 | -- Lo_Chk := (X > Lo) | |
1995 | ||
1996 | Lo_Chk := Make_Op_Gt (Loc, | |
1997 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1998 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
1999 | end if; | |
2000 | ||
2001 | -- Check against higher bound | |
2002 | ||
2af58f67 | 2003 | if Truncate and then Ilast < 0 then |
2004 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2005 | Hi_OK := False; |
2af58f67 | 2006 | |
2007 | elsif Truncate then | |
2008 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2009 | Hi_OK := True; | |
2010 | ||
2011 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2012 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2013 | Hi_OK := (Ilast < 0); | |
2014 | else | |
2015 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2016 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2017 | end if; | |
2018 | ||
2019 | if Hi_OK then | |
2020 | ||
2021 | -- Hi_Chk := (X <= Hi) | |
2022 | ||
2023 | Hi_Chk := Make_Op_Le (Loc, | |
2024 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2025 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2026 | ||
2027 | else | |
2028 | -- Hi_Chk := (X < Hi) | |
2029 | ||
2030 | Hi_Chk := Make_Op_Lt (Loc, | |
2031 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2032 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2033 | end if; | |
2034 | ||
feff2f05 | 2035 | -- If the bounds of the target type are the same as those of the base |
2036 | -- type, the check is an overflow check as a range check is not | |
2037 | -- performed in these cases. | |
5329ca64 | 2038 | |
2039 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2040 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2041 | then | |
2042 | Reason := CE_Overflow_Check_Failed; | |
2043 | else | |
2044 | Reason := CE_Range_Check_Failed; | |
2045 | end if; | |
2046 | ||
2047 | -- Raise CE if either conditions does not hold | |
2048 | ||
2049 | Insert_Action (Ck_Node, | |
2050 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2051 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2052 | Reason => Reason)); |
2053 | end Apply_Float_Conversion_Check; | |
2054 | ||
ee6ba406 | 2055 | ------------------------ |
2056 | -- Apply_Length_Check -- | |
2057 | ------------------------ | |
2058 | ||
2059 | procedure Apply_Length_Check | |
2060 | (Ck_Node : Node_Id; | |
2061 | Target_Typ : Entity_Id; | |
2062 | Source_Typ : Entity_Id := Empty) | |
2063 | is | |
2064 | begin | |
2065 | Apply_Selected_Length_Checks | |
2066 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2067 | end Apply_Length_Check; | |
2068 | ||
3b045963 | 2069 | ------------------------------------- |
2070 | -- Apply_Parameter_Aliasing_Checks -- | |
2071 | ------------------------------------- | |
b73adb97 | 2072 | |
3b045963 | 2073 | procedure Apply_Parameter_Aliasing_Checks |
2074 | (Call : Node_Id; | |
2075 | Subp : Entity_Id) | |
2076 | is | |
2077 | function May_Cause_Aliasing | |
2078 | (Formal_1 : Entity_Id; | |
2079 | Formal_2 : Entity_Id) return Boolean; | |
2080 | -- Determine whether two formal parameters can alias each other | |
2081 | -- depending on their modes. | |
2082 | ||
2083 | function Original_Actual (N : Node_Id) return Node_Id; | |
2084 | -- The expander may replace an actual with a temporary for the sake of | |
2085 | -- side effect removal. The temporary may hide a potential aliasing as | |
2086 | -- it does not share the address of the actual. This routine attempts | |
2087 | -- to retrieve the original actual. | |
2088 | ||
2089 | ------------------------ | |
2090 | -- May_Cause_Aliasing -- | |
2091 | ------------------------ | |
b73adb97 | 2092 | |
3b045963 | 2093 | function May_Cause_Aliasing |
4a9e7f0c | 2094 | (Formal_1 : Entity_Id; |
3b045963 | 2095 | Formal_2 : Entity_Id) return Boolean |
2096 | is | |
2097 | begin | |
2098 | -- The following combination cannot lead to aliasing | |
2099 | ||
2100 | -- Formal 1 Formal 2 | |
2101 | -- IN IN | |
2102 | ||
2103 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2104 | and then |
2105 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2106 | then |
2107 | return False; | |
2108 | ||
2109 | -- The following combinations may lead to aliasing | |
2110 | ||
2111 | -- Formal 1 Formal 2 | |
2112 | -- IN OUT | |
2113 | -- IN IN OUT | |
2114 | -- OUT IN | |
2115 | -- OUT IN OUT | |
2116 | -- OUT OUT | |
2117 | ||
2118 | else | |
2119 | return True; | |
2120 | end if; | |
2121 | end May_Cause_Aliasing; | |
2122 | ||
2123 | --------------------- | |
2124 | -- Original_Actual -- | |
2125 | --------------------- | |
2126 | ||
2127 | function Original_Actual (N : Node_Id) return Node_Id is | |
2128 | begin | |
2129 | if Nkind (N) = N_Type_Conversion then | |
2130 | return Expression (N); | |
2131 | ||
2132 | -- The expander created a temporary to capture the result of a type | |
2133 | -- conversion where the expression is the real actual. | |
2134 | ||
2135 | elsif Nkind (N) = N_Identifier | |
2136 | and then Present (Original_Node (N)) | |
2137 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2138 | then | |
2139 | return Expression (Original_Node (N)); | |
2140 | end if; | |
2141 | ||
2142 | return N; | |
2143 | end Original_Actual; | |
2144 | ||
2145 | -- Local variables | |
2146 | ||
2147 | Loc : constant Source_Ptr := Sloc (Call); | |
2148 | Actual_1 : Node_Id; | |
2149 | Actual_2 : Node_Id; | |
2150 | Check : Node_Id; | |
2151 | Cond : Node_Id; | |
2152 | Formal_1 : Entity_Id; | |
2153 | Formal_2 : Entity_Id; | |
2154 | ||
2155 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2156 | ||
2157 | begin | |
2158 | Cond := Empty; | |
2159 | ||
2160 | Actual_1 := First_Actual (Call); | |
2161 | Formal_1 := First_Formal (Subp); | |
2162 | while Present (Actual_1) and then Present (Formal_1) loop | |
2163 | ||
2164 | -- Ensure that the actual is an object that is not passed by value. | |
2165 | -- Elementary types are always passed by value, therefore actuals of | |
2166 | -- such types cannot lead to aliasing. | |
2167 | ||
2168 | if Is_Object_Reference (Original_Actual (Actual_1)) | |
2169 | and then not Is_Elementary_Type (Etype (Original_Actual (Actual_1))) | |
2170 | then | |
2171 | Actual_2 := Next_Actual (Actual_1); | |
2172 | Formal_2 := Next_Formal (Formal_1); | |
2173 | while Present (Actual_2) and then Present (Formal_2) loop | |
2174 | ||
2175 | -- The other actual we are testing against must also denote | |
2176 | -- a non pass-by-value object. Generate the check only when | |
2177 | -- the mode of the two formals may lead to aliasing. | |
2178 | ||
2179 | if Is_Object_Reference (Original_Actual (Actual_2)) | |
2180 | and then not | |
2181 | Is_Elementary_Type (Etype (Original_Actual (Actual_2))) | |
2182 | and then May_Cause_Aliasing (Formal_1, Formal_2) | |
2183 | then | |
2184 | -- Generate: | |
2185 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2186 | ||
2187 | Check := | |
2188 | Make_Attribute_Reference (Loc, | |
2189 | Prefix => | |
2190 | New_Copy_Tree (Original_Actual (Actual_1)), | |
2191 | Attribute_Name => Name_Overlaps_Storage, | |
2192 | Expressions => | |
2193 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2194 | ||
2195 | if No (Cond) then | |
2196 | Cond := Check; | |
2197 | else | |
2198 | Cond := | |
2199 | Make_And_Then (Loc, | |
2200 | Left_Opnd => Cond, | |
2201 | Right_Opnd => Check); | |
2202 | end if; | |
2203 | end if; | |
2204 | ||
2205 | Next_Actual (Actual_2); | |
2206 | Next_Formal (Formal_2); | |
2207 | end loop; | |
2208 | end if; | |
2209 | ||
2210 | Next_Actual (Actual_1); | |
2211 | Next_Formal (Formal_1); | |
2212 | end loop; | |
2213 | ||
2214 | -- Place the check right before the call | |
2215 | ||
2216 | if Present (Cond) then | |
2217 | Insert_Action (Call, | |
2218 | Make_Raise_Program_Error (Loc, | |
2219 | Condition => Cond, | |
2220 | Reason => PE_Explicit_Raise)); | |
2221 | end if; | |
2222 | end Apply_Parameter_Aliasing_Checks; | |
2223 | ||
2224 | ------------------------------------- | |
2225 | -- Apply_Parameter_Validity_Checks -- | |
2226 | ------------------------------------- | |
2227 | ||
2228 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2229 | Subp_Decl : Node_Id; | |
b73adb97 | 2230 | |
4a9e7f0c | 2231 | procedure Add_Validity_Check |
2232 | (Context : Entity_Id; | |
2233 | PPC_Nam : Name_Id; | |
2234 | For_Result : Boolean := False); | |
2235 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
2236 | -- of Context. PPC_Nam denotes the pre or post condition pragma name. | |
2237 | -- Set flag For_Result when to verify the result of a function. | |
b73adb97 | 2238 | |
4a9e7f0c | 2239 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id); |
2240 | -- Create a pre or post condition pragma with name PPC_Nam which | |
2241 | -- tests expression Check. | |
b73adb97 | 2242 | |
b73adb97 | 2243 | ------------------------ |
2244 | -- Add_Validity_Check -- | |
2245 | ------------------------ | |
2246 | ||
2247 | procedure Add_Validity_Check | |
2248 | (Context : Entity_Id; | |
4a9e7f0c | 2249 | PPC_Nam : Name_Id; |
b73adb97 | 2250 | For_Result : Boolean := False) |
2251 | is | |
4a9e7f0c | 2252 | Loc : constant Source_Ptr := Sloc (Subp); |
2253 | Typ : constant Entity_Id := Etype (Context); | |
b73adb97 | 2254 | Check : Node_Id; |
2255 | Nam : Name_Id; | |
2256 | ||
2257 | begin | |
2258 | -- Pick the proper version of 'Valid depending on the type of the | |
2259 | -- context. If the context is not eligible for such a check, return. | |
2260 | ||
2261 | if Is_Scalar_Type (Typ) then | |
2262 | Nam := Name_Valid; | |
2263 | elsif not No_Scalar_Parts (Typ) then | |
2264 | Nam := Name_Valid_Scalars; | |
2265 | else | |
2266 | return; | |
2267 | end if; | |
2268 | ||
2269 | -- Step 1: Create the expression to verify the validity of the | |
2270 | -- context. | |
2271 | ||
2272 | Check := New_Reference_To (Context, Loc); | |
2273 | ||
2274 | -- When processing a function result, use 'Result. Generate | |
2275 | -- Context'Result | |
2276 | ||
2277 | if For_Result then | |
2278 | Check := | |
2279 | Make_Attribute_Reference (Loc, | |
2280 | Prefix => Check, | |
2281 | Attribute_Name => Name_Result); | |
2282 | end if; | |
2283 | ||
2284 | -- Generate: | |
2285 | -- Context['Result]'Valid[_Scalars] | |
2286 | ||
2287 | Check := | |
2288 | Make_Attribute_Reference (Loc, | |
2289 | Prefix => Check, | |
2290 | Attribute_Name => Nam); | |
2291 | ||
4a9e7f0c | 2292 | -- Step 2: Create a pre or post condition pragma |
2293 | ||
2294 | Build_PPC_Pragma (PPC_Nam, Check); | |
2295 | end Add_Validity_Check; | |
2296 | ||
2297 | ---------------------- | |
2298 | -- Build_PPC_Pragma -- | |
2299 | ---------------------- | |
b73adb97 | 2300 | |
4a9e7f0c | 2301 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id) is |
7c443ae8 | 2302 | Loc : constant Source_Ptr := Sloc (Subp); |
2303 | Decls : List_Id; | |
2304 | Prag : Node_Id; | |
4a9e7f0c | 2305 | |
2306 | begin | |
2307 | Prag := | |
2308 | Make_Pragma (Loc, | |
2309 | Pragma_Identifier => Make_Identifier (Loc, PPC_Nam), | |
2310 | Pragma_Argument_Associations => New_List ( | |
2311 | Make_Pragma_Argument_Association (Loc, | |
2312 | Chars => Name_Check, | |
2313 | Expression => Check))); | |
2314 | ||
2315 | -- Add a message unless exception messages are suppressed | |
2316 | ||
2317 | if not Exception_Locations_Suppressed then | |
2318 | Append_To (Pragma_Argument_Associations (Prag), | |
2319 | Make_Pragma_Argument_Association (Loc, | |
2320 | Chars => Name_Message, | |
2321 | Expression => | |
2322 | Make_String_Literal (Loc, | |
2323 | Strval => "failed " & Get_Name_String (PPC_Nam) & | |
2324 | " from " & Build_Location_String (Loc)))); | |
2325 | end if; | |
2326 | ||
2327 | -- Insert the pragma in the tree | |
2328 | ||
2329 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2330 | Add_Global_Declaration (Prag); | |
7c443ae8 | 2331 | Analyze (Prag); |
2332 | ||
2333 | -- PPC pragmas associated with subprogram bodies must be inserted in | |
2334 | -- the declarative part of the body. | |
2335 | ||
2336 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2337 | Decls := Declarations (Subp_Decl); | |
2338 | ||
2339 | if No (Decls) then | |
2340 | Decls := New_List; | |
2341 | Set_Declarations (Subp_Decl, Decls); | |
2342 | end if; | |
2343 | ||
1bd93de5 | 2344 | Prepend_To (Decls, Prag); |
7c443ae8 | 2345 | |
2346 | -- Ensure the proper visibility of the subprogram body and its | |
2347 | -- parameters. | |
2348 | ||
2349 | Push_Scope (Subp); | |
2350 | Analyze (Prag); | |
2351 | Pop_Scope; | |
2352 | ||
2353 | -- For subprogram declarations insert the PPC pragma right after the | |
2354 | -- declarative node. | |
2355 | ||
b73adb97 | 2356 | else |
7c443ae8 | 2357 | Insert_After_And_Analyze (Subp_Decl, Prag); |
b73adb97 | 2358 | end if; |
4a9e7f0c | 2359 | end Build_PPC_Pragma; |
2360 | ||
2361 | -- Local variables | |
2362 | ||
2363 | Formal : Entity_Id; | |
4a9e7f0c | 2364 | Subp_Spec : Node_Id; |
2365 | ||
3b045963 | 2366 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2367 | |
2368 | begin | |
4a9e7f0c | 2369 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2370 | |
4a9e7f0c | 2371 | Subp_Spec := Parent (Subp); |
a45d946f | 2372 | |
4a9e7f0c | 2373 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2374 | Subp_Spec := Parent (Subp_Spec); | |
2375 | end if; | |
a45d946f | 2376 | |
4a9e7f0c | 2377 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2378 | |
b73adb97 | 2379 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2380 | |
2381 | -- Do not process formal subprograms because the corresponding actual | |
2382 | -- will receive the proper checks when the instance is analyzed. | |
2383 | ||
2384 | or else Is_Formal_Subprogram (Subp) | |
2385 | ||
a45d946f | 2386 | -- Do not process imported subprograms since pre and post conditions |
2387 | -- are never verified on routines coming from a different language. | |
4a9e7f0c | 2388 | |
b73adb97 | 2389 | or else Is_Imported (Subp) |
2390 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2391 | |
a45d946f | 2392 | -- The PPC pragmas generated by this routine do not correspond to |
2393 | -- source aspects, therefore they cannot be applied to abstract | |
2394 | -- subprograms. | |
4a9e7f0c | 2395 | |
7c443ae8 | 2396 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2397 | |
a45d946f | 2398 | -- Do not consider subprogram renaminds because the renamed entity |
2399 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2400 | |
2401 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2402 | ||
a45d946f | 2403 | -- Do not process null procedures because there is no benefit of |
2404 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2405 | |
2406 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2407 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2408 | then |
2409 | return; | |
2410 | end if; | |
2411 | ||
4a9e7f0c | 2412 | -- Inspect all the formals applying aliasing and scalar initialization |
2413 | -- checks where applicable. | |
b73adb97 | 2414 | |
2415 | Formal := First_Formal (Subp); | |
2416 | while Present (Formal) loop | |
4a9e7f0c | 2417 | |
2418 | -- Generate the following scalar initialization checks for each | |
2419 | -- formal parameter: | |
2420 | ||
2421 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2422 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2423 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2424 | ||
2425 | if Check_Validity_Of_Parameters then | |
2426 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2427 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2428 | end if; | |
2429 | ||
2430 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2431 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2432 | end if; | |
b73adb97 | 2433 | end if; |
2434 | ||
b73adb97 | 2435 | Next_Formal (Formal); |
2436 | end loop; | |
2437 | ||
a45d946f | 2438 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2439 | |
2440 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2441 | |
a45d946f | 2442 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2443 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2444 | end if; |
3b045963 | 2445 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2446 | |
7aafae1c | 2447 | --------------------------- |
2448 | -- Apply_Predicate_Check -- | |
2449 | --------------------------- | |
2450 | ||
2451 | procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id) is | |
301d5ec3 | 2452 | S : Entity_Id; |
9e58d7ed | 2453 | |
7aafae1c | 2454 | begin |
701d57a4 | 2455 | if Present (Predicate_Function (Typ)) then |
301d5ec3 | 2456 | |
2457 | -- A predicate check does not apply within internally generated | |
2458 | -- subprograms, such as TSS functions. | |
2459 | ||
2460 | S := Current_Scope; | |
9e58d7ed | 2461 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2462 | S := Scope (S); |
2463 | end loop; | |
2464 | ||
9e58d7ed | 2465 | if Present (S) and then Get_TSS_Name (S) /= TSS_Null then |
301d5ec3 | 2466 | return; |
22631b41 | 2467 | |
96a2d100 | 2468 | -- If the check appears within the predicate function itself, it |
2469 | -- means that the user specified a check whose formal is the | |
2470 | -- predicated subtype itself, rather than some covering type. This | |
2471 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2472 | |
2473 | elsif S = Predicate_Function (Typ) then | |
96a2d100 | 2474 | Error_Msg_N |
2475 | ("predicate check includes a function call that " | |
cb97ae5c | 2476 | & "requires a predicate check??", Parent (N)); |
96a2d100 | 2477 | Error_Msg_N |
cb97ae5c | 2478 | ("\this will result in infinite recursion??", Parent (N)); |
96a2d100 | 2479 | Insert_Action (N, |
61016a7a | 2480 | Make_Raise_Storage_Error (Sloc (N), |
2481 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2482 | |
61016a7a | 2483 | -- Here for normal case of predicate active. |
e6281d47 | 2484 | |
61016a7a | 2485 | else |
e6281d47 | 2486 | -- If the predicate is a static predicate and the operand is |
2487 | -- static, the predicate must be evaluated statically. If the | |
cac18f71 | 2488 | -- evaluation fails this is a static constraint error. This check |
2489 | -- is disabled in -gnatc mode, because the compiler is incapable | |
2490 | -- of evaluating static expressions in that case. | |
e6281d47 | 2491 | |
2492 | if Is_OK_Static_Expression (N) then | |
61016a7a | 2493 | if Present (Static_Predicate (Typ)) then |
a45d946f | 2494 | if Operating_Mode < Generate_Code |
2495 | or else Eval_Static_Predicate_Check (N, Typ) | |
cac18f71 | 2496 | then |
e6281d47 | 2497 | return; |
2498 | else | |
2499 | Error_Msg_NE | |
2500 | ("static expression fails static predicate check on&", | |
61016a7a | 2501 | N, Typ); |
e6281d47 | 2502 | end if; |
2503 | end if; | |
2504 | end if; | |
2505 | ||
301d5ec3 | 2506 | Insert_Action (N, |
2507 | Make_Predicate_Check (Typ, Duplicate_Subexpr (N))); | |
2508 | end if; | |
7aafae1c | 2509 | end if; |
2510 | end Apply_Predicate_Check; | |
2511 | ||
ee6ba406 | 2512 | ----------------------- |
2513 | -- Apply_Range_Check -- | |
2514 | ----------------------- | |
2515 | ||
2516 | procedure Apply_Range_Check | |
2517 | (Ck_Node : Node_Id; | |
2518 | Target_Typ : Entity_Id; | |
2519 | Source_Typ : Entity_Id := Empty) | |
2520 | is | |
2521 | begin | |
2522 | Apply_Selected_Range_Checks | |
2523 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2524 | end Apply_Range_Check; | |
2525 | ||
2526 | ------------------------------ | |
2527 | -- Apply_Scalar_Range_Check -- | |
2528 | ------------------------------ | |
2529 | ||
feff2f05 | 2530 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2531 | -- off if it is already set on. | |
ee6ba406 | 2532 | |
2533 | procedure Apply_Scalar_Range_Check | |
2534 | (Expr : Node_Id; | |
2535 | Target_Typ : Entity_Id; | |
2536 | Source_Typ : Entity_Id := Empty; | |
2537 | Fixed_Int : Boolean := False) | |
2538 | is | |
2539 | Parnt : constant Node_Id := Parent (Expr); | |
2540 | S_Typ : Entity_Id; | |
2541 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2542 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
2543 | OK : Boolean; | |
2544 | ||
2545 | Is_Subscr_Ref : Boolean; | |
2546 | -- Set true if Expr is a subscript | |
2547 | ||
2548 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2549 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2550 | -- case we do not attempt to do an analysis of the value against the | |
2551 | -- range of the subscript, since we don't know the actual subtype. | |
2552 | ||
2553 | Int_Real : Boolean; | |
feff2f05 | 2554 | -- Set to True if Expr should be regarded as a real value even though |
2555 | -- the type of Expr might be discrete. | |
ee6ba406 | 2556 | |
2557 | procedure Bad_Value; | |
2558 | -- Procedure called if value is determined to be out of range | |
2559 | ||
9dfe12ae | 2560 | --------------- |
2561 | -- Bad_Value -- | |
2562 | --------------- | |
2563 | ||
ee6ba406 | 2564 | procedure Bad_Value is |
2565 | begin | |
2566 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2567 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
ee6ba406 | 2568 | Ent => Target_Typ, |
2569 | Typ => Target_Typ); | |
2570 | end Bad_Value; | |
2571 | ||
9dfe12ae | 2572 | -- Start of processing for Apply_Scalar_Range_Check |
2573 | ||
ee6ba406 | 2574 | begin |
2af58f67 | 2575 | -- Return if check obviously not needed |
ee6ba406 | 2576 | |
2af58f67 | 2577 | if |
2578 | -- Not needed inside generic | |
ee6ba406 | 2579 | |
2af58f67 | 2580 | Inside_A_Generic |
2581 | ||
2582 | -- Not needed if previous error | |
2583 | ||
2584 | or else Target_Typ = Any_Type | |
2585 | or else Nkind (Expr) = N_Error | |
2586 | ||
2587 | -- Not needed for non-scalar type | |
2588 | ||
2589 | or else not Is_Scalar_Type (Target_Typ) | |
2590 | ||
2591 | -- Not needed if we know node raises CE already | |
2592 | ||
2593 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2594 | then |
2595 | return; | |
2596 | end if; | |
2597 | ||
2598 | -- Now, see if checks are suppressed | |
2599 | ||
2600 | Is_Subscr_Ref := | |
2601 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2602 | ||
2603 | if Is_Subscr_Ref then | |
2604 | Arr := Prefix (Parnt); | |
2605 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2606 | |
a3a76ccc | 2607 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2608 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2609 | end if; |
ee6ba406 | 2610 | end if; |
2611 | ||
2612 | if not Do_Range_Check (Expr) then | |
2613 | ||
2614 | -- Subscript reference. Check for Index_Checks suppressed | |
2615 | ||
2616 | if Is_Subscr_Ref then | |
2617 | ||
2618 | -- Check array type and its base type | |
2619 | ||
2620 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2621 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2622 | then |
2623 | return; | |
2624 | ||
2625 | -- Check array itself if it is an entity name | |
2626 | ||
2627 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2628 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2629 | then |
2630 | return; | |
2631 | ||
2632 | -- Check expression itself if it is an entity name | |
2633 | ||
2634 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2635 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2636 | then |
2637 | return; | |
2638 | end if; | |
2639 | ||
2640 | -- All other cases, check for Range_Checks suppressed | |
2641 | ||
2642 | else | |
2643 | -- Check target type and its base type | |
2644 | ||
2645 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2646 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2647 | then |
2648 | return; | |
2649 | ||
2650 | -- Check expression itself if it is an entity name | |
2651 | ||
2652 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2653 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2654 | then |
2655 | return; | |
2656 | ||
feff2f05 | 2657 | -- If Expr is part of an assignment statement, then check left |
2658 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2659 | |
2660 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2661 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2662 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2663 | then |
2664 | return; | |
2665 | end if; | |
2666 | end if; | |
2667 | end if; | |
2668 | ||
9dfe12ae | 2669 | -- Do not set range checks if they are killed |
2670 | ||
2671 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2672 | and then Kill_Range_Check (Expr) | |
2673 | then | |
2674 | return; | |
2675 | end if; | |
2676 | ||
2677 | -- Do not set range checks for any values from System.Scalar_Values | |
2678 | -- since the whole idea of such values is to avoid checking them! | |
2679 | ||
2680 | if Is_Entity_Name (Expr) | |
2681 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2682 | then | |
2683 | return; | |
2684 | end if; | |
2685 | ||
ee6ba406 | 2686 | -- Now see if we need a check |
2687 | ||
2688 | if No (Source_Typ) then | |
2689 | S_Typ := Etype (Expr); | |
2690 | else | |
2691 | S_Typ := Source_Typ; | |
2692 | end if; | |
2693 | ||
2694 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
2695 | return; | |
2696 | end if; | |
2697 | ||
2698 | Is_Unconstrained_Subscr_Ref := | |
2699 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
2700 | ||
b40670e1 | 2701 | -- Special checks for floating-point type |
ee6ba406 | 2702 | |
b40670e1 | 2703 | if Is_Floating_Point_Type (S_Typ) then |
2704 | ||
2705 | -- Always do a range check if the source type includes infinities and | |
2706 | -- the target type does not include infinities. We do not do this if | |
2707 | -- range checks are killed. | |
2708 | ||
2709 | if Has_Infinities (S_Typ) | |
2710 | and then not Has_Infinities (Target_Typ) | |
2711 | then | |
2712 | Enable_Range_Check (Expr); | |
2713 | ||
2714 | -- Always do a range check for operators if option set | |
2715 | ||
2716 | elsif Check_Float_Overflow and then Nkind (Expr) in N_Op then | |
2717 | Enable_Range_Check (Expr); | |
2718 | end if; | |
ee6ba406 | 2719 | end if; |
2720 | ||
feff2f05 | 2721 | -- Return if we know expression is definitely in the range of the target |
2722 | -- type as determined by Determine_Range. Right now we only do this for | |
2723 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 2724 | |
f2a06be9 | 2725 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 2726 | |
feff2f05 | 2727 | -- Note: skip this if we are given a source_typ, since the point of |
2728 | -- supplying a Source_Typ is to stop us looking at the expression. | |
2729 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 2730 | |
2731 | if Is_Discrete_Type (Target_Typ) | |
2732 | and then Is_Discrete_Type (Etype (Expr)) | |
2733 | and then not Is_Unconstrained_Subscr_Ref | |
2734 | and then No (Source_Typ) | |
2735 | then | |
2736 | declare | |
2737 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
2738 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
2739 | Lo : Uint; | |
2740 | Hi : Uint; | |
2741 | ||
2742 | begin | |
2743 | if Compile_Time_Known_Value (Tlo) | |
2744 | and then Compile_Time_Known_Value (Thi) | |
2745 | then | |
9dfe12ae | 2746 | declare |
2747 | Lov : constant Uint := Expr_Value (Tlo); | |
2748 | Hiv : constant Uint := Expr_Value (Thi); | |
ee6ba406 | 2749 | |
9dfe12ae | 2750 | begin |
2751 | -- If range is null, we for sure have a constraint error | |
2752 | -- (we don't even need to look at the value involved, | |
2753 | -- since all possible values will raise CE). | |
2754 | ||
2755 | if Lov > Hiv then | |
2756 | Bad_Value; | |
2757 | return; | |
2758 | end if; | |
2759 | ||
2760 | -- Otherwise determine range of value | |
2761 | ||
9c486805 | 2762 | Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True); |
9dfe12ae | 2763 | |
2764 | if OK then | |
2765 | ||
2766 | -- If definitely in range, all OK | |
ee6ba406 | 2767 | |
ee6ba406 | 2768 | if Lo >= Lov and then Hi <= Hiv then |
2769 | return; | |
2770 | ||
9dfe12ae | 2771 | -- If definitely not in range, warn |
2772 | ||
ee6ba406 | 2773 | elsif Lov > Hi or else Hiv < Lo then |
2774 | Bad_Value; | |
2775 | return; | |
9dfe12ae | 2776 | |
2777 | -- Otherwise we don't know | |
2778 | ||
2779 | else | |
2780 | null; | |
ee6ba406 | 2781 | end if; |
9dfe12ae | 2782 | end if; |
2783 | end; | |
ee6ba406 | 2784 | end if; |
2785 | end; | |
2786 | end if; | |
2787 | ||
2788 | Int_Real := | |
2789 | Is_Floating_Point_Type (S_Typ) | |
2790 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
2791 | ||
2792 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 2793 | -- range of the target type. Note that if S_Typ is within the bounds |
2794 | -- of Target_Typ then this must be the case. This check is meaningful | |
2795 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 2796 | |
2797 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 2798 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 2799 | and then |
7a1dabb3 | 2800 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ee6ba406 | 2801 | or else |
9c486805 | 2802 | Is_In_Range (Expr, Target_Typ, |
2803 | Assume_Valid => True, | |
b40670e1 | 2804 | Fixed_Int => Fixed_Int, |
2805 | Int_Real => Int_Real)) | |
ee6ba406 | 2806 | then |
2807 | return; | |
2808 | ||
9c486805 | 2809 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
2810 | Assume_Valid => True, | |
2811 | Fixed_Int => Fixed_Int, | |
2812 | Int_Real => Int_Real) | |
2813 | then | |
ee6ba406 | 2814 | Bad_Value; |
2815 | return; | |
2816 | ||
b40670e1 | 2817 | -- Floating-point case |
feff2f05 | 2818 | -- In the floating-point case, we only do range checks if the type is |
2819 | -- constrained. We definitely do NOT want range checks for unconstrained | |
2820 | -- types, since we want to have infinities | |
ee6ba406 | 2821 | |
9dfe12ae | 2822 | elsif Is_Floating_Point_Type (S_Typ) then |
b40670e1 | 2823 | |
2824 | -- Normally, we only do range checks if the type is constrained. We do | |
2825 | -- NOT want range checks for unconstrained types, since we want to have | |
2826 | -- infinities. Override this decision in Check_Float_Overflow mode. | |
2827 | ||
2828 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then | |
9dfe12ae | 2829 | Enable_Range_Check (Expr); |
2830 | end if; | |
ee6ba406 | 2831 | |
9dfe12ae | 2832 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 2833 | |
2834 | else | |
2835 | Enable_Range_Check (Expr); | |
2836 | return; | |
2837 | end if; | |
ee6ba406 | 2838 | end Apply_Scalar_Range_Check; |
2839 | ||
2840 | ---------------------------------- | |
2841 | -- Apply_Selected_Length_Checks -- | |
2842 | ---------------------------------- | |
2843 | ||
2844 | procedure Apply_Selected_Length_Checks | |
2845 | (Ck_Node : Node_Id; | |
2846 | Target_Typ : Entity_Id; | |
2847 | Source_Typ : Entity_Id; | |
2848 | Do_Static : Boolean) | |
2849 | is | |
2850 | Cond : Node_Id; | |
2851 | R_Result : Check_Result; | |
2852 | R_Cno : Node_Id; | |
2853 | ||
2854 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2855 | Checks_On : constant Boolean := | |
b6341c67 | 2856 | (not Index_Checks_Suppressed (Target_Typ)) |
2857 | or else (not Length_Checks_Suppressed (Target_Typ)); | |
ee6ba406 | 2858 | |
2859 | begin | |
6dbcfcd9 | 2860 | if not Full_Expander_Active then |
ee6ba406 | 2861 | return; |
2862 | end if; | |
2863 | ||
2864 | R_Result := | |
2865 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
2866 | ||
2867 | for J in 1 .. 2 loop | |
ee6ba406 | 2868 | R_Cno := R_Result (J); |
2869 | exit when No (R_Cno); | |
2870 | ||
2871 | -- A length check may mention an Itype which is attached to a | |
2872 | -- subsequent node. At the top level in a package this can cause | |
2873 | -- an order-of-elaboration problem, so we make sure that the itype | |
2874 | -- is referenced now. | |
2875 | ||
2876 | if Ekind (Current_Scope) = E_Package | |
2877 | and then Is_Compilation_Unit (Current_Scope) | |
2878 | then | |
2879 | Ensure_Defined (Target_Typ, Ck_Node); | |
2880 | ||
2881 | if Present (Source_Typ) then | |
2882 | Ensure_Defined (Source_Typ, Ck_Node); | |
2883 | ||
2884 | elsif Is_Itype (Etype (Ck_Node)) then | |
2885 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
2886 | end if; | |
2887 | end if; | |
2888 | ||
feff2f05 | 2889 | -- If the item is a conditional raise of constraint error, then have |
2890 | -- a look at what check is being performed and ??? | |
ee6ba406 | 2891 | |
2892 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2893 | and then Present (Condition (R_Cno)) | |
2894 | then | |
2895 | Cond := Condition (R_Cno); | |
2896 | ||
0577b0b1 | 2897 | -- Case where node does not now have a dynamic check |
ee6ba406 | 2898 | |
0577b0b1 | 2899 | if not Has_Dynamic_Length_Check (Ck_Node) then |
2900 | ||
2901 | -- If checks are on, just insert the check | |
2902 | ||
2903 | if Checks_On then | |
2904 | Insert_Action (Ck_Node, R_Cno); | |
2905 | ||
2906 | if not Do_Static then | |
2907 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
2908 | end if; | |
2909 | ||
2910 | -- If checks are off, then analyze the length check after | |
2911 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 2912 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 2913 | -- compile time warning in this case. |
2914 | ||
2915 | else | |
2916 | Set_Parent (R_Cno, Ck_Node); | |
2917 | Analyze (R_Cno); | |
ee6ba406 | 2918 | end if; |
ee6ba406 | 2919 | end if; |
2920 | ||
2921 | -- Output a warning if the condition is known to be True | |
2922 | ||
2923 | if Is_Entity_Name (Cond) | |
2924 | and then Entity (Cond) = Standard_True | |
2925 | then | |
2926 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2927 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 2928 | CE_Length_Check_Failed, |
ee6ba406 | 2929 | Ent => Target_Typ, |
2930 | Typ => Target_Typ); | |
2931 | ||
2932 | -- If we were only doing a static check, or if checks are not | |
2933 | -- on, then we want to delete the check, since it is not needed. | |
2934 | -- We do this by replacing the if statement by a null statement | |
2935 | ||
2936 | elsif Do_Static or else not Checks_On then | |
00c403ee | 2937 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 2938 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
2939 | end if; | |
2940 | ||
2941 | else | |
2942 | Install_Static_Check (R_Cno, Loc); | |
2943 | end if; | |
ee6ba406 | 2944 | end loop; |
ee6ba406 | 2945 | end Apply_Selected_Length_Checks; |
2946 | ||
2947 | --------------------------------- | |
2948 | -- Apply_Selected_Range_Checks -- | |
2949 | --------------------------------- | |
2950 | ||
2951 | procedure Apply_Selected_Range_Checks | |
2952 | (Ck_Node : Node_Id; | |
2953 | Target_Typ : Entity_Id; | |
2954 | Source_Typ : Entity_Id; | |
2955 | Do_Static : Boolean) | |
2956 | is | |
2957 | Cond : Node_Id; | |
2958 | R_Result : Check_Result; | |
2959 | R_Cno : Node_Id; | |
2960 | ||
2961 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2962 | Checks_On : constant Boolean := | |
b6341c67 | 2963 | (not Index_Checks_Suppressed (Target_Typ)) |
2964 | or else (not Range_Checks_Suppressed (Target_Typ)); | |
ee6ba406 | 2965 | |
2966 | begin | |
6dbcfcd9 | 2967 | if not Full_Expander_Active or else not Checks_On then |
ee6ba406 | 2968 | return; |
2969 | end if; | |
2970 | ||
2971 | R_Result := | |
2972 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
2973 | ||
2974 | for J in 1 .. 2 loop | |
2975 | ||
2976 | R_Cno := R_Result (J); | |
2977 | exit when No (R_Cno); | |
2978 | ||
feff2f05 | 2979 | -- If the item is a conditional raise of constraint error, then have |
2980 | -- a look at what check is being performed and ??? | |
ee6ba406 | 2981 | |
2982 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2983 | and then Present (Condition (R_Cno)) | |
2984 | then | |
2985 | Cond := Condition (R_Cno); | |
2986 | ||
2987 | if not Has_Dynamic_Range_Check (Ck_Node) then | |
2988 | Insert_Action (Ck_Node, R_Cno); | |
2989 | ||
2990 | if not Do_Static then | |
2991 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
2992 | end if; | |
2993 | end if; | |
2994 | ||
2995 | -- Output a warning if the condition is known to be True | |
2996 | ||
2997 | if Is_Entity_Name (Cond) | |
2998 | and then Entity (Cond) = Standard_True | |
2999 | then | |
feff2f05 | 3000 | -- Since an N_Range is technically not an expression, we have |
3001 | -- to set one of the bounds to C_E and then just flag the | |
3002 | -- N_Range. The warning message will point to the lower bound | |
3003 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3004 | |
3005 | if Nkind (Ck_Node) = N_Range then | |
3006 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3007 | (Low_Bound (Ck_Node), "static range out of bounds of}??", |
f15731c4 | 3008 | CE_Range_Check_Failed, |
ee6ba406 | 3009 | Ent => Target_Typ, |
3010 | Typ => Target_Typ); | |
3011 | ||
3012 | Set_Raises_Constraint_Error (Ck_Node); | |
3013 | ||
3014 | else | |
3015 | Apply_Compile_Time_Constraint_Error | |
3016 | (Ck_Node, "static value out of range of}?", | |
f15731c4 | 3017 | CE_Range_Check_Failed, |
ee6ba406 | 3018 | Ent => Target_Typ, |
3019 | Typ => Target_Typ); | |
3020 | end if; | |
3021 | ||
3022 | -- If we were only doing a static check, or if checks are not | |
3023 | -- on, then we want to delete the check, since it is not needed. | |
3024 | -- We do this by replacing the if statement by a null statement | |
3025 | ||
3026 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3027 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3028 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3029 | end if; | |
3030 | ||
3031 | else | |
3032 | Install_Static_Check (R_Cno, Loc); | |
3033 | end if; | |
ee6ba406 | 3034 | end loop; |
ee6ba406 | 3035 | end Apply_Selected_Range_Checks; |
3036 | ||
3037 | ------------------------------- | |
3038 | -- Apply_Static_Length_Check -- | |
3039 | ------------------------------- | |
3040 | ||
3041 | procedure Apply_Static_Length_Check | |
3042 | (Expr : Node_Id; | |
3043 | Target_Typ : Entity_Id; | |
3044 | Source_Typ : Entity_Id := Empty) | |
3045 | is | |
3046 | begin | |
3047 | Apply_Selected_Length_Checks | |
3048 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3049 | end Apply_Static_Length_Check; | |
3050 | ||
3051 | ------------------------------------- | |
3052 | -- Apply_Subscript_Validity_Checks -- | |
3053 | ------------------------------------- | |
3054 | ||
3055 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3056 | Sub : Node_Id; | |
3057 | ||
3058 | begin | |
3059 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3060 | ||
3061 | -- Loop through subscripts | |
3062 | ||
3063 | Sub := First (Expressions (Expr)); | |
3064 | while Present (Sub) loop | |
3065 | ||
feff2f05 | 3066 | -- Check one subscript. Note that we do not worry about enumeration |
3067 | -- type with holes, since we will convert the value to a Pos value | |
3068 | -- for the subscript, and that convert will do the necessary validity | |
3069 | -- check. | |
ee6ba406 | 3070 | |
3071 | Ensure_Valid (Sub, Holes_OK => True); | |
3072 | ||
3073 | -- Move to next subscript | |
3074 | ||
3075 | Sub := Next (Sub); | |
3076 | end loop; | |
3077 | end Apply_Subscript_Validity_Checks; | |
3078 | ||
3079 | ---------------------------------- | |
3080 | -- Apply_Type_Conversion_Checks -- | |
3081 | ---------------------------------- | |
3082 | ||
3083 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3084 | Target_Type : constant Entity_Id := Etype (N); | |
3085 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3086 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3087 | |
3088 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3089 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3090 | -- full view might have discriminants with defaults, so we need the | |
3091 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3092 | |
3093 | begin | |
3094 | if Inside_A_Generic then | |
3095 | return; | |
3096 | ||
f15731c4 | 3097 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3098 | -- situations of incomplete trees that blow things up. |
3099 | ||
f15731c4 | 3100 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3101 | return; |
3102 | ||
feff2f05 | 3103 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3104 | -- range check if we cannot be sure that Expr is in the base type of | |
3105 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3106 | -- are not quite the same condition from an implementation point of | |
3107 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3108 | |
3109 | elsif Is_Scalar_Type (Target_Type) then | |
3110 | declare | |
3111 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3112 | -- If the Conversion_OK flag on the type conversion is set and no |
3113 | -- floating point type is involved in the type conversion then | |
3114 | -- fixed point values must be read as integral values. | |
ee6ba406 | 3115 | |
5329ca64 | 3116 | Float_To_Int : constant Boolean := |
b6341c67 | 3117 | Is_Floating_Point_Type (Expr_Type) |
3118 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3119 | |
ee6ba406 | 3120 | begin |
ee6ba406 | 3121 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3122 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3123 | and then not |
7a1dabb3 | 3124 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3125 | and then not Float_To_Int |
ee6ba406 | 3126 | then |
00c403ee | 3127 | Activate_Overflow_Check (N); |
ee6ba406 | 3128 | end if; |
3129 | ||
3130 | if not Range_Checks_Suppressed (Target_Type) | |
3131 | and then not Range_Checks_Suppressed (Expr_Type) | |
3132 | then | |
5329ca64 | 3133 | if Float_To_Int then |
3134 | Apply_Float_Conversion_Check (Expr, Target_Type); | |
3135 | else | |
3136 | Apply_Scalar_Range_Check | |
3137 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
798afddc | 3138 | |
3139 | -- If the target type has predicates, we need to indicate | |
3140 | -- the need for a check, even if Determine_Range finds | |
3141 | -- that the value is within bounds. This may be the case | |
3142 | -- e.g for a division with a constant denominator. | |
3143 | ||
3144 | if Has_Predicates (Target_Type) then | |
3145 | Enable_Range_Check (Expr); | |
3146 | end if; | |
5329ca64 | 3147 | end if; |
ee6ba406 | 3148 | end if; |
3149 | end; | |
3150 | ||
3151 | elsif Comes_From_Source (N) | |
f40f9731 | 3152 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3153 | and then Is_Record_Type (Target_Type) |
3154 | and then Is_Derived_Type (Target_Type) | |
3155 | and then not Is_Tagged_Type (Target_Type) | |
3156 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3157 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3158 | then |
141d591a | 3159 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3160 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3161 | -- constraint, to verify that the expression of the parent type |
3162 | -- satisfies the constraints imposed by the (unconstrained!) | |
3163 | -- derived type. This applies to value conversions, not to view | |
3164 | -- conversions of tagged types. | |
3165 | ||
3166 | declare | |
9dfe12ae | 3167 | Loc : constant Source_Ptr := Sloc (N); |
3168 | Cond : Node_Id; | |
3169 | Constraint : Elmt_Id; | |
3170 | Discr_Value : Node_Id; | |
3171 | Discr : Entity_Id; | |
3172 | ||
3173 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3174 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3175 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3176 | |
3177 | begin | |
9dfe12ae | 3178 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3179 | while Present (Constraint) loop |
3180 | Discr_Value := Node (Constraint); | |
3181 | ||
3182 | if Is_Entity_Name (Discr_Value) | |
3183 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3184 | then | |
3185 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3186 | ||
3187 | if Present (Discr) | |
3188 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3189 | then | |
3190 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3191 | -- Value of original discriminant in expression. If the |
3192 | -- new discriminant has been used to constrain more than | |
3193 | -- one of the stored discriminants, this will provide the | |
3194 | -- required consistency check. | |
ee6ba406 | 3195 | |
55868293 | 3196 | Append_Elmt |
3197 | (Make_Selected_Component (Loc, | |
3198 | Prefix => | |
9dfe12ae | 3199 | Duplicate_Subexpr_No_Checks |
3200 | (Expr, Name_Req => True), | |
ee6ba406 | 3201 | Selector_Name => |
3202 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3203 | New_Constraints); |
ee6ba406 | 3204 | |
3205 | else | |
3206 | -- Discriminant of more remote ancestor ??? | |
3207 | ||
3208 | return; | |
3209 | end if; | |
3210 | ||
feff2f05 | 3211 | -- Derived type definition has an explicit value for this |
3212 | -- stored discriminant. | |
ee6ba406 | 3213 | |
3214 | else | |
3215 | Append_Elmt | |
9dfe12ae | 3216 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3217 | New_Constraints); | |
ee6ba406 | 3218 | end if; |
3219 | ||
3220 | Next_Elmt (Constraint); | |
3221 | end loop; | |
3222 | ||
3223 | -- Use the unconstrained expression type to retrieve the | |
3224 | -- discriminants of the parent, and apply momentarily the | |
3225 | -- discriminant constraint synthesized above. | |
3226 | ||
3227 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3228 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3229 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3230 | ||
3231 | Insert_Action (N, | |
f15731c4 | 3232 | Make_Raise_Constraint_Error (Loc, |
3233 | Condition => Cond, | |
3234 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3235 | end; |
3236 | ||
feff2f05 | 3237 | -- For arrays, conversions are applied during expansion, to take into |
3238 | -- accounts changes of representation. The checks become range checks on | |
3239 | -- the base type or length checks on the subtype, depending on whether | |
3240 | -- the target type is unconstrained or constrained. | |
ee6ba406 | 3241 | |
3242 | else | |
3243 | null; | |
3244 | end if; | |
ee6ba406 | 3245 | end Apply_Type_Conversion_Checks; |
3246 | ||
3247 | ---------------------------------------------- | |
3248 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3249 | ---------------------------------------------- | |
3250 | ||
3251 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3252 | Loc : constant Source_Ptr := Sloc (N); | |
3253 | Typ : constant Entity_Id := Etype (N); | |
3254 | ||
3255 | begin | |
3256 | if Inside_A_Generic then | |
3257 | return; | |
3258 | ||
3259 | -- Nothing to do if checks are suppressed | |
3260 | ||
3261 | elsif Range_Checks_Suppressed (Typ) | |
3262 | and then Overflow_Checks_Suppressed (Typ) | |
3263 | then | |
3264 | return; | |
3265 | ||
3266 | -- Nothing to do if the attribute does not come from source. The | |
3267 | -- internal attributes we generate of this type do not need checks, | |
3268 | -- and furthermore the attempt to check them causes some circular | |
3269 | -- elaboration orders when dealing with packed types. | |
3270 | ||
3271 | elsif not Comes_From_Source (N) then | |
3272 | return; | |
3273 | ||
9dfe12ae | 3274 | -- If the prefix is a selected component that depends on a discriminant |
3275 | -- the check may improperly expose a discriminant instead of using | |
3276 | -- the bounds of the object itself. Set the type of the attribute to | |
3277 | -- the base type of the context, so that a check will be imposed when | |
3278 | -- needed (e.g. if the node appears as an index). | |
3279 | ||
3280 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3281 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3282 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3283 | then | |
3284 | Set_Etype (N, Base_Type (Typ)); | |
3285 | ||
feff2f05 | 3286 | -- Otherwise, replace the attribute node with a type conversion node |
3287 | -- whose expression is the attribute, retyped to universal integer, and | |
3288 | -- whose subtype mark is the target type. The call to analyze this | |
3289 | -- conversion will set range and overflow checks as required for proper | |
3290 | -- detection of an out of range value. | |
ee6ba406 | 3291 | |
3292 | else | |
3293 | Set_Etype (N, Universal_Integer); | |
3294 | Set_Analyzed (N, True); | |
3295 | ||
3296 | Rewrite (N, | |
3297 | Make_Type_Conversion (Loc, | |
3298 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3299 | Expression => Relocate_Node (N))); | |
3300 | ||
3301 | Analyze_And_Resolve (N, Typ); | |
3302 | return; | |
3303 | end if; | |
ee6ba406 | 3304 | end Apply_Universal_Integer_Attribute_Checks; |
3305 | ||
07c191b0 | 3306 | ------------------------------------- |
3307 | -- Atomic_Synchronization_Disabled -- | |
3308 | ------------------------------------- | |
3309 | ||
3310 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3311 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3312 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3313 | ||
3314 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3315 | begin | |
b444f81d | 3316 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3317 | -- looks enabled, since it is never disabled. | |
3318 | ||
3319 | if Debug_Flag_Dot_E then | |
3320 | return False; | |
3321 | ||
3322 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3323 | -- sync looks disabled, since it always tests True. | |
3324 | ||
3325 | elsif Debug_Flag_Dot_D then | |
3326 | return True; | |
3327 | ||
3328 | -- If entity present, then check result for that entity | |
3329 | ||
3330 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3331 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3332 | |
3333 | -- Otherwise result depends on current scope setting | |
3334 | ||
07c191b0 | 3335 | else |
fafc6b97 | 3336 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3337 | end if; |
3338 | end Atomic_Synchronization_Disabled; | |
3339 | ||
ee6ba406 | 3340 | ------------------------------- |
3341 | -- Build_Discriminant_Checks -- | |
3342 | ------------------------------- | |
3343 | ||
3344 | function Build_Discriminant_Checks | |
3345 | (N : Node_Id; | |
314a23b6 | 3346 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3347 | is |
3348 | Loc : constant Source_Ptr := Sloc (N); | |
3349 | Cond : Node_Id; | |
3350 | Disc : Elmt_Id; | |
3351 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3352 | Dref : Node_Id; |
ee6ba406 | 3353 | Dval : Node_Id; |
3354 | ||
84d0d4a5 | 3355 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3356 | ||
3357 | ---------------------------------- | |
3358 | -- Aggregate_Discriminant_Value -- | |
3359 | ---------------------------------- | |
3360 | ||
3361 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3362 | Assoc : Node_Id; | |
3363 | ||
3364 | begin | |
feff2f05 | 3365 | -- The aggregate has been normalized with named associations. We use |
3366 | -- the Chars field to locate the discriminant to take into account | |
3367 | -- discriminants in derived types, which carry the same name as those | |
3368 | -- in the parent. | |
84d0d4a5 | 3369 | |
3370 | Assoc := First (Component_Associations (N)); | |
3371 | while Present (Assoc) loop | |
3372 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3373 | return Expression (Assoc); | |
3374 | else | |
3375 | Next (Assoc); | |
3376 | end if; | |
3377 | end loop; | |
3378 | ||
3379 | -- Discriminant must have been found in the loop above | |
3380 | ||
3381 | raise Program_Error; | |
3382 | end Aggregate_Discriminant_Val; | |
3383 | ||
3384 | -- Start of processing for Build_Discriminant_Checks | |
3385 | ||
ee6ba406 | 3386 | begin |
84d0d4a5 | 3387 | -- Loop through discriminants evolving the condition |
3388 | ||
ee6ba406 | 3389 | Cond := Empty; |
3390 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3391 | ||
9dfe12ae | 3392 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3393 | |
3394 | if Is_Private_Type (T_Typ) | |
3395 | and then No (Full_View (T_Typ)) | |
3396 | then | |
3397 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3398 | else | |
3399 | Disc_Ent := First_Discriminant (T_Typ); | |
3400 | end if; | |
3401 | ||
3402 | while Present (Disc) loop | |
ee6ba406 | 3403 | Dval := Node (Disc); |
3404 | ||
3405 | if Nkind (Dval) = N_Identifier | |
3406 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3407 | then | |
3408 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3409 | else | |
9dfe12ae | 3410 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3411 | end if; |
3412 | ||
00f91aef | 3413 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3414 | -- of the node. | |
9dfe12ae | 3415 | |
00f91aef | 3416 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3417 | Dref := New_Copy ( | |
3418 | Get_Discriminant_Value ( | |
3419 | First_Discriminant (T_Typ), | |
3420 | T_Typ, | |
3421 | Stored_Constraint (T_Typ))); | |
3422 | ||
84d0d4a5 | 3423 | elsif Nkind (N) = N_Aggregate then |
3424 | Dref := | |
3425 | Duplicate_Subexpr_No_Checks | |
3426 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3427 | ||
00f91aef | 3428 | else |
3429 | Dref := | |
3430 | Make_Selected_Component (Loc, | |
3431 | Prefix => | |
3432 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), | |
3433 | Selector_Name => | |
3434 | Make_Identifier (Loc, Chars (Disc_Ent))); | |
3435 | ||
3436 | Set_Is_In_Discriminant_Check (Dref); | |
3437 | end if; | |
9dfe12ae | 3438 | |
ee6ba406 | 3439 | Evolve_Or_Else (Cond, |
3440 | Make_Op_Ne (Loc, | |
9dfe12ae | 3441 | Left_Opnd => Dref, |
ee6ba406 | 3442 | Right_Opnd => Dval)); |
3443 | ||
3444 | Next_Elmt (Disc); | |
3445 | Next_Discriminant (Disc_Ent); | |
3446 | end loop; | |
3447 | ||
3448 | return Cond; | |
3449 | end Build_Discriminant_Checks; | |
3450 | ||
13dbf220 | 3451 | ------------------ |
3452 | -- Check_Needed -- | |
3453 | ------------------ | |
3454 | ||
3455 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3456 | N : Node_Id; | |
3457 | P : Node_Id; | |
3458 | K : Node_Kind; | |
3459 | L : Node_Id; | |
3460 | R : Node_Id; | |
3461 | ||
3462 | begin | |
3463 | -- Always check if not simple entity | |
3464 | ||
3465 | if Nkind (Nod) not in N_Has_Entity | |
3466 | or else not Comes_From_Source (Nod) | |
3467 | then | |
3468 | return True; | |
3469 | end if; | |
3470 | ||
3471 | -- Look up tree for short circuit | |
3472 | ||
3473 | N := Nod; | |
3474 | loop | |
3475 | P := Parent (N); | |
3476 | K := Nkind (P); | |
3477 | ||
7b17e51b | 3478 | -- Done if out of subexpression (note that we allow generated stuff |
3479 | -- such as itype declarations in this context, to keep the loop going | |
3480 | -- since we may well have generated such stuff in complex situations. | |
3481 | -- Also done if no parent (probably an error condition, but no point | |
3482 | -- in behaving nasty if we find it!) | |
3483 | ||
3484 | if No (P) | |
3485 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
3486 | then | |
13dbf220 | 3487 | return True; |
3488 | ||
7b17e51b | 3489 | -- Or/Or Else case, where test is part of the right operand, or is |
3490 | -- part of one of the actions associated with the right operand, and | |
3491 | -- the left operand is an equality test. | |
13dbf220 | 3492 | |
7b17e51b | 3493 | elsif K = N_Op_Or then |
13dbf220 | 3494 | exit when N = Right_Opnd (P) |
3495 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
3496 | ||
7b17e51b | 3497 | elsif K = N_Or_Else then |
3498 | exit when (N = Right_Opnd (P) | |
3499 | or else | |
3500 | (Is_List_Member (N) | |
3501 | and then List_Containing (N) = Actions (P))) | |
3502 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
13dbf220 | 3503 | |
7b17e51b | 3504 | -- Similar test for the And/And then case, where the left operand |
3505 | -- is an inequality test. | |
3506 | ||
3507 | elsif K = N_Op_And then | |
13dbf220 | 3508 | exit when N = Right_Opnd (P) |
38f5559f | 3509 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; |
7b17e51b | 3510 | |
3511 | elsif K = N_And_Then then | |
3512 | exit when (N = Right_Opnd (P) | |
3513 | or else | |
3514 | (Is_List_Member (N) | |
3515 | and then List_Containing (N) = Actions (P))) | |
3516 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; | |
13dbf220 | 3517 | end if; |
3518 | ||
3519 | N := P; | |
3520 | end loop; | |
3521 | ||
3522 | -- If we fall through the loop, then we have a conditional with an | |
3523 | -- appropriate test as its left operand. So test further. | |
3524 | ||
3525 | L := Left_Opnd (P); | |
13dbf220 | 3526 | R := Right_Opnd (L); |
3527 | L := Left_Opnd (L); | |
3528 | ||
3529 | -- Left operand of test must match original variable | |
3530 | ||
3531 | if Nkind (L) not in N_Has_Entity | |
3532 | or else Entity (L) /= Entity (Nod) | |
3533 | then | |
3534 | return True; | |
3535 | end if; | |
3536 | ||
2af58f67 | 3537 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 3538 | |
3539 | case Check is | |
3540 | when Access_Check => | |
2af58f67 | 3541 | if not Known_Null (R) then |
13dbf220 | 3542 | return True; |
3543 | end if; | |
3544 | ||
3545 | when Division_Check => | |
3546 | if not Compile_Time_Known_Value (R) | |
3547 | or else Expr_Value (R) /= Uint_0 | |
3548 | then | |
3549 | return True; | |
3550 | end if; | |
2af58f67 | 3551 | |
3552 | when others => | |
3553 | raise Program_Error; | |
13dbf220 | 3554 | end case; |
3555 | ||
3556 | -- Here we have the optimizable case, warn if not short-circuited | |
3557 | ||
3558 | if K = N_Op_And or else K = N_Op_Or then | |
3559 | case Check is | |
3560 | when Access_Check => | |
3561 | Error_Msg_N | |
cb97ae5c | 3562 | ("Constraint_Error may be raised (access check)??", |
13dbf220 | 3563 | Parent (Nod)); |
3564 | when Division_Check => | |
3565 | Error_Msg_N | |
cb97ae5c | 3566 | ("Constraint_Error may be raised (zero divide)??", |
13dbf220 | 3567 | Parent (Nod)); |
2af58f67 | 3568 | |
3569 | when others => | |
3570 | raise Program_Error; | |
13dbf220 | 3571 | end case; |
3572 | ||
3573 | if K = N_Op_And then | |
e977c0cf | 3574 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3575 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 3576 | else |
e977c0cf | 3577 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3578 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 3579 | end if; |
3580 | ||
6fb3c314 | 3581 | -- If not short-circuited, we need the check |
13dbf220 | 3582 | |
3583 | return True; | |
3584 | ||
3585 | -- If short-circuited, we can omit the check | |
3586 | ||
3587 | else | |
3588 | return False; | |
3589 | end if; | |
3590 | end Check_Needed; | |
3591 | ||
ee6ba406 | 3592 | ----------------------------------- |
3593 | -- Check_Valid_Lvalue_Subscripts -- | |
3594 | ----------------------------------- | |
3595 | ||
3596 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
3597 | begin | |
3598 | -- Skip this if range checks are suppressed | |
3599 | ||
3600 | if Range_Checks_Suppressed (Etype (Expr)) then | |
3601 | return; | |
3602 | ||
feff2f05 | 3603 | -- Only do this check for expressions that come from source. We assume |
3604 | -- that expander generated assignments explicitly include any necessary | |
3605 | -- checks. Note that this is not just an optimization, it avoids | |
3606 | -- infinite recursions! | |
ee6ba406 | 3607 | |
3608 | elsif not Comes_From_Source (Expr) then | |
3609 | return; | |
3610 | ||
3611 | -- For a selected component, check the prefix | |
3612 | ||
3613 | elsif Nkind (Expr) = N_Selected_Component then | |
3614 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3615 | return; | |
3616 | ||
3617 | -- Case of indexed component | |
3618 | ||
3619 | elsif Nkind (Expr) = N_Indexed_Component then | |
3620 | Apply_Subscript_Validity_Checks (Expr); | |
3621 | ||
feff2f05 | 3622 | -- Prefix may itself be or contain an indexed component, and these |
3623 | -- subscripts need checking as well. | |
ee6ba406 | 3624 | |
3625 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3626 | end if; | |
3627 | end Check_Valid_Lvalue_Subscripts; | |
3628 | ||
fa7497e8 | 3629 | ---------------------------------- |
3630 | -- Null_Exclusion_Static_Checks -- | |
3631 | ---------------------------------- | |
3632 | ||
3633 | procedure Null_Exclusion_Static_Checks (N : Node_Id) is | |
0577b0b1 | 3634 | Error_Node : Node_Id; |
3635 | Expr : Node_Id; | |
3636 | Has_Null : constant Boolean := Has_Null_Exclusion (N); | |
3637 | K : constant Node_Kind := Nkind (N); | |
3638 | Typ : Entity_Id; | |
fa7497e8 | 3639 | |
13dbf220 | 3640 | begin |
0577b0b1 | 3641 | pragma Assert |
3642 | (K = N_Component_Declaration | |
3643 | or else K = N_Discriminant_Specification | |
3644 | or else K = N_Function_Specification | |
3645 | or else K = N_Object_Declaration | |
3646 | or else K = N_Parameter_Specification); | |
3647 | ||
3648 | if K = N_Function_Specification then | |
3649 | Typ := Etype (Defining_Entity (N)); | |
3650 | else | |
3651 | Typ := Etype (Defining_Identifier (N)); | |
3652 | end if; | |
fa7497e8 | 3653 | |
13dbf220 | 3654 | case K is |
13dbf220 | 3655 | when N_Component_Declaration => |
3656 | if Present (Access_Definition (Component_Definition (N))) then | |
0577b0b1 | 3657 | Error_Node := Component_Definition (N); |
13dbf220 | 3658 | else |
0577b0b1 | 3659 | Error_Node := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 3660 | end if; |
5329ca64 | 3661 | |
0577b0b1 | 3662 | when N_Discriminant_Specification => |
3663 | Error_Node := Discriminant_Type (N); | |
3664 | ||
3665 | when N_Function_Specification => | |
3666 | Error_Node := Result_Definition (N); | |
3667 | ||
3668 | when N_Object_Declaration => | |
3669 | Error_Node := Object_Definition (N); | |
3670 | ||
3671 | when N_Parameter_Specification => | |
3672 | Error_Node := Parameter_Type (N); | |
3673 | ||
13dbf220 | 3674 | when others => |
3675 | raise Program_Error; | |
3676 | end case; | |
5329ca64 | 3677 | |
0577b0b1 | 3678 | if Has_Null then |
5329ca64 | 3679 | |
0577b0b1 | 3680 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
3681 | -- applied to an access [sub]type. | |
5329ca64 | 3682 | |
0577b0b1 | 3683 | if not Is_Access_Type (Typ) then |
503f7fd3 | 3684 | Error_Msg_N |
00c403ee | 3685 | ("`NOT NULL` allowed only for an access type", Error_Node); |
5329ca64 | 3686 | |
feff2f05 | 3687 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 3688 | -- be applied to a [sub]type that does not exclude null already. |
3689 | ||
3690 | elsif Can_Never_Be_Null (Typ) | |
d16989f1 | 3691 | and then Comes_From_Source (Typ) |
0577b0b1 | 3692 | then |
503f7fd3 | 3693 | Error_Msg_NE |
00c403ee | 3694 | ("`NOT NULL` not allowed (& already excludes null)", |
3695 | Error_Node, Typ); | |
0577b0b1 | 3696 | end if; |
13dbf220 | 3697 | end if; |
5329ca64 | 3698 | |
cc60bd16 | 3699 | -- Check that null-excluding objects are always initialized, except for |
3700 | -- deferred constants, for which the expression will appear in the full | |
3701 | -- declaration. | |
13dbf220 | 3702 | |
3703 | if K = N_Object_Declaration | |
84d0d4a5 | 3704 | and then No (Expression (N)) |
cc60bd16 | 3705 | and then not Constant_Present (N) |
feff2f05 | 3706 | and then not No_Initialization (N) |
13dbf220 | 3707 | then |
feff2f05 | 3708 | -- Add an expression that assigns null. This node is needed by |
3709 | -- Apply_Compile_Time_Constraint_Error, which will replace this with | |
3710 | -- a Constraint_Error node. | |
13dbf220 | 3711 | |
3712 | Set_Expression (N, Make_Null (Sloc (N))); | |
3713 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
5329ca64 | 3714 | |
13dbf220 | 3715 | Apply_Compile_Time_Constraint_Error |
3716 | (N => Expression (N), | |
cb97ae5c | 3717 | Msg => |
3718 | "(Ada 2005) null-excluding objects must be initialized??", | |
13dbf220 | 3719 | Reason => CE_Null_Not_Allowed); |
3720 | end if; | |
5329ca64 | 3721 | |
cc60bd16 | 3722 | -- Check that a null-excluding component, formal or object is not being |
3723 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 3724 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 3725 | |
0577b0b1 | 3726 | if K /= N_Function_Specification then |
3727 | Expr := Expression (N); | |
5329ca64 | 3728 | |
2af58f67 | 3729 | if Present (Expr) and then Known_Null (Expr) then |
13dbf220 | 3730 | case K is |
0577b0b1 | 3731 | when N_Component_Declaration | |
3732 | N_Discriminant_Specification => | |
7189d17f | 3733 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3734 | (N => Expr, |
2af58f67 | 3735 | Msg => "(Ada 2005) null not allowed " & |
cb97ae5c | 3736 | "in null-excluding components??", |
0577b0b1 | 3737 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3738 | |
0577b0b1 | 3739 | when N_Object_Declaration => |
7189d17f | 3740 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3741 | (N => Expr, |
2af58f67 | 3742 | Msg => "(Ada 2005) null not allowed " & |
0577b0b1 | 3743 | "in null-excluding objects?", |
3744 | Reason => CE_Null_Not_Allowed); | |
5329ca64 | 3745 | |
0577b0b1 | 3746 | when N_Parameter_Specification => |
7189d17f | 3747 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3748 | (N => Expr, |
2af58f67 | 3749 | Msg => "(Ada 2005) null not allowed " & |
cb97ae5c | 3750 | "in null-excluding formals??", |
0577b0b1 | 3751 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 3752 | |
3753 | when others => | |
3754 | null; | |
5329ca64 | 3755 | end case; |
3756 | end if; | |
0577b0b1 | 3757 | end if; |
fa7497e8 | 3758 | end Null_Exclusion_Static_Checks; |
3759 | ||
9dfe12ae | 3760 | ---------------------------------- |
3761 | -- Conditional_Statements_Begin -- | |
3762 | ---------------------------------- | |
3763 | ||
3764 | procedure Conditional_Statements_Begin is | |
3765 | begin | |
3766 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
3767 | ||
feff2f05 | 3768 | -- If stack overflows, kill all checks, that way we know to simply reset |
3769 | -- the number of saved checks to zero on return. This should never occur | |
3770 | -- in practice. | |
9dfe12ae | 3771 | |
3772 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
3773 | Kill_All_Checks; | |
3774 | ||
feff2f05 | 3775 | -- In the normal case, we just make a new stack entry saving the current |
3776 | -- number of saved checks for a later restore. | |
9dfe12ae | 3777 | |
3778 | else | |
3779 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
3780 | ||
3781 | if Debug_Flag_CC then | |
3782 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
3783 | Num_Saved_Checks); | |
3784 | end if; | |
3785 | end if; | |
3786 | end Conditional_Statements_Begin; | |
3787 | ||
3788 | -------------------------------- | |
3789 | -- Conditional_Statements_End -- | |
3790 | -------------------------------- | |
3791 | ||
3792 | procedure Conditional_Statements_End is | |
3793 | begin | |
3794 | pragma Assert (Saved_Checks_TOS > 0); | |
3795 | ||
feff2f05 | 3796 | -- If the saved checks stack overflowed, then we killed all checks, so |
3797 | -- setting the number of saved checks back to zero is correct. This | |
3798 | -- should never occur in practice. | |
9dfe12ae | 3799 | |
3800 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
3801 | Num_Saved_Checks := 0; | |
3802 | ||
feff2f05 | 3803 | -- In the normal case, restore the number of saved checks from the top |
3804 | -- stack entry. | |
9dfe12ae | 3805 | |
3806 | else | |
3807 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
3808 | if Debug_Flag_CC then | |
3809 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
3810 | Num_Saved_Checks); | |
3811 | end if; | |
3812 | end if; | |
3813 | ||
3814 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
3815 | end Conditional_Statements_End; | |
3816 | ||
3cce7f32 | 3817 | ------------------------- |
3818 | -- Convert_From_Bignum -- | |
3819 | ------------------------- | |
3820 | ||
3821 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
3822 | Loc : constant Source_Ptr := Sloc (N); | |
3823 | ||
3824 | begin | |
3825 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
3826 | ||
3827 | -- Construct call From Bignum | |
3828 | ||
3829 | return | |
3830 | Make_Function_Call (Loc, | |
3831 | Name => | |
3832 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
3833 | Parameter_Associations => New_List (Relocate_Node (N))); | |
3834 | end Convert_From_Bignum; | |
3835 | ||
3836 | ----------------------- | |
3837 | -- Convert_To_Bignum -- | |
3838 | ----------------------- | |
3839 | ||
3840 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
3841 | Loc : constant Source_Ptr := Sloc (N); | |
3842 | ||
3843 | begin | |
0326b4d4 | 3844 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 3845 | |
3846 | if Is_RTE (Etype (N), RE_Bignum) then | |
3847 | return Relocate_Node (N); | |
3848 | ||
21a55437 | 3849 | -- Otherwise construct call to To_Bignum, converting the operand to the |
3850 | -- required Long_Long_Integer form. | |
3cce7f32 | 3851 | |
3852 | else | |
3853 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
3854 | return | |
3855 | Make_Function_Call (Loc, | |
3856 | Name => | |
3857 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
3858 | Parameter_Associations => New_List ( | |
3859 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
3860 | end if; | |
3861 | end Convert_To_Bignum; | |
3862 | ||
ee6ba406 | 3863 | --------------------- |
3864 | -- Determine_Range -- | |
3865 | --------------------- | |
3866 | ||
6af1bdbc | 3867 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 3868 | type Cache_Index is range 0 .. Cache_Size - 1; |
3869 | -- Determine size of below cache (power of 2 is more efficient!) | |
3870 | ||
3871 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; | |
9c486805 | 3872 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
ee6ba406 | 3873 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; |
3874 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
feff2f05 | 3875 | -- The above arrays are used to implement a small direct cache for |
3876 | -- Determine_Range calls. Because of the way Determine_Range recursively | |
3877 | -- traces subexpressions, and because overflow checking calls the routine | |
3878 | -- on the way up the tree, a quadratic behavior can otherwise be | |
3879 | -- encountered in large expressions. The cache entry for node N is stored | |
3880 | -- in the (N mod Cache_Size) entry, and can be validated by checking the | |
9c486805 | 3881 | -- actual node value stored there. The Range_Cache_V array records the |
3882 | -- setting of Assume_Valid for the cache entry. | |
ee6ba406 | 3883 | |
3884 | procedure Determine_Range | |
9c486805 | 3885 | (N : Node_Id; |
3886 | OK : out Boolean; | |
3887 | Lo : out Uint; | |
3888 | Hi : out Uint; | |
3889 | Assume_Valid : Boolean := False) | |
ee6ba406 | 3890 | is |
e254d721 | 3891 | Typ : Entity_Id := Etype (N); |
3892 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 3893 | |
3894 | Lo_Left : Uint; | |
3895 | Hi_Left : Uint; | |
3896 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 3897 | |
ee6ba406 | 3898 | Lo_Right : Uint; |
ee6ba406 | 3899 | Hi_Right : Uint; |
8880be85 | 3900 | -- Lo and Hi bounds of right (or only) operand |
3901 | ||
3902 | Bound : Node_Id; | |
3903 | -- Temp variable used to hold a bound node | |
3904 | ||
3905 | Hbound : Uint; | |
3906 | -- High bound of base type of expression | |
3907 | ||
3908 | Lor : Uint; | |
3909 | Hir : Uint; | |
3910 | -- Refined values for low and high bounds, after tightening | |
3911 | ||
3912 | OK1 : Boolean; | |
3913 | -- Used in lower level calls to indicate if call succeeded | |
3914 | ||
3915 | Cindex : Cache_Index; | |
3916 | -- Used to search cache | |
ee6ba406 | 3917 | |
094ed68e | 3918 | Btyp : Entity_Id; |
3919 | -- Base type | |
3920 | ||
ee6ba406 | 3921 | function OK_Operands return Boolean; |
3922 | -- Used for binary operators. Determines the ranges of the left and | |
3923 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 3924 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 3925 | |
3926 | ----------------- | |
3927 | -- OK_Operands -- | |
3928 | ----------------- | |
3929 | ||
3930 | function OK_Operands return Boolean is | |
3931 | begin | |
9c486805 | 3932 | Determine_Range |
3933 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 3934 | |
3935 | if not OK1 then | |
3936 | return False; | |
3937 | end if; | |
3938 | ||
9c486805 | 3939 | Determine_Range |
3940 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 3941 | return OK1; |
3942 | end OK_Operands; | |
3943 | ||
3944 | -- Start of processing for Determine_Range | |
3945 | ||
3946 | begin | |
87bdc21d | 3947 | -- For temporary constants internally generated to remove side effects |
3948 | -- we must use the corresponding expression to determine the range of | |
3949 | -- the expression. | |
3950 | ||
3951 | if Is_Entity_Name (N) | |
3952 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
3953 | and then Ekind (Entity (N)) = E_Constant | |
3954 | and then Is_Internal_Name (Chars (Entity (N))) | |
3955 | then | |
3956 | Determine_Range | |
3957 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
3958 | return; | |
3959 | end if; | |
3960 | ||
ee6ba406 | 3961 | -- Prevent junk warnings by initializing range variables |
3962 | ||
3963 | Lo := No_Uint; | |
3964 | Hi := No_Uint; | |
3965 | Lor := No_Uint; | |
3966 | Hir := No_Uint; | |
3967 | ||
a781c0fc | 3968 | -- If type is not defined, we can't determine its range |
ee6ba406 | 3969 | |
a781c0fc | 3970 | if No (Typ) |
3971 | ||
3972 | -- We don't deal with anything except discrete types | |
3973 | ||
3974 | or else not Is_Discrete_Type (Typ) | |
3975 | ||
3976 | -- Ignore type for which an error has been posted, since range in | |
3977 | -- this case may well be a bogosity deriving from the error. Also | |
3978 | -- ignore if error posted on the reference node. | |
3979 | ||
3980 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 3981 | then |
3982 | OK := False; | |
3983 | return; | |
3984 | end if; | |
3985 | ||
3986 | -- For all other cases, we can determine the range | |
3987 | ||
3988 | OK := True; | |
3989 | ||
feff2f05 | 3990 | -- If value is compile time known, then the possible range is the one |
3991 | -- value that we know this expression definitely has! | |
ee6ba406 | 3992 | |
3993 | if Compile_Time_Known_Value (N) then | |
3994 | Lo := Expr_Value (N); | |
3995 | Hi := Lo; | |
3996 | return; | |
3997 | end if; | |
3998 | ||
3999 | -- Return if already in the cache | |
4000 | ||
4001 | Cindex := Cache_Index (N mod Cache_Size); | |
4002 | ||
9c486805 | 4003 | if Determine_Range_Cache_N (Cindex) = N |
4004 | and then | |
4005 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4006 | then | |
ee6ba406 | 4007 | Lo := Determine_Range_Cache_Lo (Cindex); |
4008 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4009 | return; | |
4010 | end if; | |
4011 | ||
feff2f05 | 4012 | -- Otherwise, start by finding the bounds of the type of the expression, |
4013 | -- the value cannot be outside this range (if it is, then we have an | |
4014 | -- overflow situation, which is a separate check, we are talking here | |
4015 | -- only about the expression value). | |
ee6ba406 | 4016 | |
341bd953 | 4017 | -- First a check, never try to find the bounds of a generic type, since |
4018 | -- these bounds are always junk values, and it is only valid to look at | |
4019 | -- the bounds in an instance. | |
4020 | ||
4021 | if Is_Generic_Type (Typ) then | |
4022 | OK := False; | |
4023 | return; | |
4024 | end if; | |
4025 | ||
9c486805 | 4026 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4027 | |
9c486805 | 4028 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4029 | or else Assume_No_Invalid_Values | |
4030 | or else Assume_Valid | |
e254d721 | 4031 | then |
9c486805 | 4032 | null; |
4033 | else | |
4034 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4035 | end if; |
4036 | ||
094ed68e | 4037 | -- Retrieve the base type. Handle the case where the base type is a |
4038 | -- private enumeration type. | |
4039 | ||
4040 | Btyp := Base_Type (Typ); | |
4041 | ||
4042 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4043 | Btyp := Full_View (Btyp); | |
4044 | end if; | |
4045 | ||
feff2f05 | 4046 | -- We use the actual bound unless it is dynamic, in which case use the |
4047 | -- corresponding base type bound if possible. If we can't get a bound | |
4048 | -- then we figure we can't determine the range (a peculiar case, that | |
4049 | -- perhaps cannot happen, but there is no point in bombing in this | |
4050 | -- optimization circuit. | |
8880be85 | 4051 | |
4052 | -- First the low bound | |
ee6ba406 | 4053 | |
4054 | Bound := Type_Low_Bound (Typ); | |
4055 | ||
4056 | if Compile_Time_Known_Value (Bound) then | |
4057 | Lo := Expr_Value (Bound); | |
4058 | ||
094ed68e | 4059 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4060 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4061 | |
4062 | else | |
4063 | OK := False; | |
4064 | return; | |
4065 | end if; | |
4066 | ||
8880be85 | 4067 | -- Now the high bound |
4068 | ||
ee6ba406 | 4069 | Bound := Type_High_Bound (Typ); |
4070 | ||
8880be85 | 4071 | -- We need the high bound of the base type later on, and this should |
4072 | -- always be compile time known. Again, it is not clear that this | |
4073 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4074 | |
094ed68e | 4075 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4076 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4077 | Hi := Hbound; |
4078 | ||
4079 | else | |
4080 | OK := False; | |
4081 | return; | |
4082 | end if; | |
4083 | ||
feff2f05 | 4084 | -- If we have a static subtype, then that may have a tighter bound so |
4085 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4086 | |
4087 | if Compile_Time_Known_Value (Bound) then | |
4088 | Hi := Expr_Value (Bound); | |
4089 | end if; | |
4090 | ||
feff2f05 | 4091 | -- We may be able to refine this value in certain situations. If any |
4092 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4093 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4094 | |
4095 | case Nkind (N) is | |
4096 | ||
4097 | -- For unary plus, result is limited by range of operand | |
4098 | ||
4099 | when N_Op_Plus => | |
9c486805 | 4100 | Determine_Range |
4101 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4102 | |
4103 | -- For unary minus, determine range of operand, and negate it | |
4104 | ||
4105 | when N_Op_Minus => | |
9c486805 | 4106 | Determine_Range |
4107 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4108 | |
4109 | if OK1 then | |
4110 | Lor := -Hi_Right; | |
4111 | Hir := -Lo_Right; | |
4112 | end if; | |
4113 | ||
4114 | -- For binary addition, get range of each operand and do the | |
4115 | -- addition to get the result range. | |
4116 | ||
4117 | when N_Op_Add => | |
4118 | if OK_Operands then | |
4119 | Lor := Lo_Left + Lo_Right; | |
4120 | Hir := Hi_Left + Hi_Right; | |
4121 | end if; | |
4122 | ||
feff2f05 | 4123 | -- Division is tricky. The only case we consider is where the right |
4124 | -- operand is a positive constant, and in this case we simply divide | |
4125 | -- the bounds of the left operand | |
ee6ba406 | 4126 | |
4127 | when N_Op_Divide => | |
4128 | if OK_Operands then | |
4129 | if Lo_Right = Hi_Right | |
4130 | and then Lo_Right > 0 | |
4131 | then | |
4132 | Lor := Lo_Left / Lo_Right; | |
4133 | Hir := Hi_Left / Lo_Right; | |
4134 | ||
4135 | else | |
4136 | OK1 := False; | |
4137 | end if; | |
4138 | end if; | |
4139 | ||
feff2f05 | 4140 | -- For binary subtraction, get range of each operand and do the worst |
4141 | -- case subtraction to get the result range. | |
ee6ba406 | 4142 | |
4143 | when N_Op_Subtract => | |
4144 | if OK_Operands then | |
4145 | Lor := Lo_Left - Hi_Right; | |
4146 | Hir := Hi_Left - Lo_Right; | |
4147 | end if; | |
4148 | ||
feff2f05 | 4149 | -- For MOD, if right operand is a positive constant, then result must |
4150 | -- be in the allowable range of mod results. | |
ee6ba406 | 4151 | |
4152 | when N_Op_Mod => | |
4153 | if OK_Operands then | |
9dfe12ae | 4154 | if Lo_Right = Hi_Right |
4155 | and then Lo_Right /= 0 | |
4156 | then | |
ee6ba406 | 4157 | if Lo_Right > 0 then |
4158 | Lor := Uint_0; | |
4159 | Hir := Lo_Right - 1; | |
4160 | ||
9dfe12ae | 4161 | else -- Lo_Right < 0 |
ee6ba406 | 4162 | Lor := Lo_Right + 1; |
4163 | Hir := Uint_0; | |
4164 | end if; | |
4165 | ||
4166 | else | |
4167 | OK1 := False; | |
4168 | end if; | |
4169 | end if; | |
4170 | ||
feff2f05 | 4171 | -- For REM, if right operand is a positive constant, then result must |
4172 | -- be in the allowable range of mod results. | |
ee6ba406 | 4173 | |
4174 | when N_Op_Rem => | |
4175 | if OK_Operands then | |
9dfe12ae | 4176 | if Lo_Right = Hi_Right |
4177 | and then Lo_Right /= 0 | |
4178 | then | |
ee6ba406 | 4179 | declare |
4180 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4181 | ||
4182 | begin | |
4183 | -- The sign of the result depends on the sign of the | |
4184 | -- dividend (but not on the sign of the divisor, hence | |
4185 | -- the abs operation above). | |
4186 | ||
4187 | if Lo_Left < 0 then | |
4188 | Lor := -Dval; | |
4189 | else | |
4190 | Lor := Uint_0; | |
4191 | end if; | |
4192 | ||
4193 | if Hi_Left < 0 then | |
4194 | Hir := Uint_0; | |
4195 | else | |
4196 | Hir := Dval; | |
4197 | end if; | |
4198 | end; | |
4199 | ||
4200 | else | |
4201 | OK1 := False; | |
4202 | end if; | |
4203 | end if; | |
4204 | ||
4205 | -- Attribute reference cases | |
4206 | ||
4207 | when N_Attribute_Reference => | |
4208 | case Attribute_Name (N) is | |
4209 | ||
4210 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4211 | -- possible range of values of the attribute expression. |
ee6ba406 | 4212 | |
4213 | when Name_Pos | Name_Val => | |
9c486805 | 4214 | Determine_Range |
4215 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4216 | |
4217 | -- For Length attribute, use the bounds of the corresponding | |
4218 | -- index type to refine the range. | |
4219 | ||
4220 | when Name_Length => | |
4221 | declare | |
4222 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4223 | Inum : Nat; | |
4224 | Indx : Node_Id; | |
4225 | ||
4226 | LL, LU : Uint; | |
4227 | UL, UU : Uint; | |
4228 | ||
4229 | begin | |
4230 | if Is_Access_Type (Atyp) then | |
4231 | Atyp := Designated_Type (Atyp); | |
4232 | end if; | |
4233 | ||
4234 | -- For string literal, we know exact value | |
4235 | ||
4236 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4237 | OK := True; | |
4238 | Lo := String_Literal_Length (Atyp); | |
4239 | Hi := String_Literal_Length (Atyp); | |
4240 | return; | |
4241 | end if; | |
4242 | ||
4243 | -- Otherwise check for expression given | |
4244 | ||
4245 | if No (Expressions (N)) then | |
4246 | Inum := 1; | |
4247 | else | |
4248 | Inum := | |
4249 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4250 | end if; | |
4251 | ||
4252 | Indx := First_Index (Atyp); | |
4253 | for J in 2 .. Inum loop | |
4254 | Indx := Next_Index (Indx); | |
4255 | end loop; | |
4256 | ||
9116df93 | 4257 | -- If the index type is a formal type or derived from |
c8da6114 | 4258 | -- one, the bounds are not static. |
4259 | ||
4260 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4261 | OK := False; | |
4262 | return; | |
4263 | end if; | |
4264 | ||
ee6ba406 | 4265 | Determine_Range |
9c486805 | 4266 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4267 | Assume_Valid); | |
ee6ba406 | 4268 | |
4269 | if OK1 then | |
4270 | Determine_Range | |
9c486805 | 4271 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4272 | Assume_Valid); | |
ee6ba406 | 4273 | |
4274 | if OK1 then | |
4275 | ||
4276 | -- The maximum value for Length is the biggest | |
4277 | -- possible gap between the values of the bounds. | |
4278 | -- But of course, this value cannot be negative. | |
4279 | ||
9c486805 | 4280 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4281 | |
4282 | -- For constrained arrays, the minimum value for | |
4283 | -- Length is taken from the actual value of the | |
9116df93 | 4284 | -- bounds, since the index will be exactly of this |
4285 | -- subtype. | |
ee6ba406 | 4286 | |
4287 | if Is_Constrained (Atyp) then | |
9c486805 | 4288 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4289 | |
4290 | -- For an unconstrained array, the minimum value | |
4291 | -- for length is always zero. | |
4292 | ||
4293 | else | |
4294 | Lor := Uint_0; | |
4295 | end if; | |
4296 | end if; | |
4297 | end if; | |
4298 | end; | |
4299 | ||
4300 | -- No special handling for other attributes | |
9116df93 | 4301 | -- Probably more opportunities exist here??? |
ee6ba406 | 4302 | |
4303 | when others => | |
4304 | OK1 := False; | |
4305 | ||
4306 | end case; | |
4307 | ||
feff2f05 | 4308 | -- For type conversion from one discrete type to another, we can |
4309 | -- refine the range using the converted value. | |
ee6ba406 | 4310 | |
4311 | when N_Type_Conversion => | |
9c486805 | 4312 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); |
ee6ba406 | 4313 | |
4314 | -- Nothing special to do for all other expression kinds | |
4315 | ||
4316 | when others => | |
4317 | OK1 := False; | |
4318 | Lor := No_Uint; | |
4319 | Hir := No_Uint; | |
4320 | end case; | |
4321 | ||
9116df93 | 4322 | -- At this stage, if OK1 is true, then we know that the actual result of |
4323 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4324 | -- to restrict the possible range of results. | |
ee6ba406 | 4325 | |
4326 | if OK1 then | |
4327 | ||
9116df93 | 4328 | -- If the refined value of the low bound is greater than the type |
4329 | -- high bound, then reset it to the more restrictive value. However, | |
4330 | -- we do NOT do this for the case of a modular type where the | |
4331 | -- possible upper bound on the value is above the base type high | |
4332 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4333 | |
4334 | if Lor > Lo | |
9116df93 | 4335 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4336 | then |
4337 | Lo := Lor; | |
4338 | end if; | |
4339 | ||
9116df93 | 4340 | -- Similarly, if the refined value of the high bound is less than the |
4341 | -- value so far, then reset it to the more restrictive value. Again, | |
4342 | -- we do not do this if the refined low bound is negative for a | |
4343 | -- modular type, since this would wrap. | |
ee6ba406 | 4344 | |
4345 | if Hir < Hi | |
9116df93 | 4346 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4347 | then |
4348 | Hi := Hir; | |
4349 | end if; | |
4350 | end if; | |
4351 | ||
4352 | -- Set cache entry for future call and we are all done | |
4353 | ||
4354 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4355 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4356 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4357 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4358 | return; | |
4359 | ||
9116df93 | 4360 | -- If any exception occurs, it means that we have some bug in the compiler, |
4361 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4362 | -- occurrence. However, this is only an optimization attempt, so there is |
4363 | -- really no point in crashing the compiler. Instead we just decide, too | |
4364 | -- bad, we can't figure out a range in this case after all. | |
4365 | ||
4366 | exception | |
4367 | when others => | |
4368 | ||
4369 | -- Debug flag K disables this behavior (useful for debugging) | |
4370 | ||
4371 | if Debug_Flag_K then | |
4372 | raise; | |
4373 | else | |
4374 | OK := False; | |
4375 | Lo := No_Uint; | |
4376 | Hi := No_Uint; | |
4377 | return; | |
4378 | end if; | |
ee6ba406 | 4379 | end Determine_Range; |
4380 | ||
4381 | ------------------------------------ | |
4382 | -- Discriminant_Checks_Suppressed -- | |
4383 | ------------------------------------ | |
4384 | ||
4385 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4386 | begin | |
9dfe12ae | 4387 | if Present (E) then |
4388 | if Is_Unchecked_Union (E) then | |
4389 | return True; | |
4390 | elsif Checks_May_Be_Suppressed (E) then | |
4391 | return Is_Check_Suppressed (E, Discriminant_Check); | |
4392 | end if; | |
4393 | end if; | |
4394 | ||
fafc6b97 | 4395 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 4396 | end Discriminant_Checks_Suppressed; |
4397 | ||
4398 | -------------------------------- | |
4399 | -- Division_Checks_Suppressed -- | |
4400 | -------------------------------- | |
4401 | ||
4402 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4403 | begin | |
9dfe12ae | 4404 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
4405 | return Is_Check_Suppressed (E, Division_Check); | |
4406 | else | |
fafc6b97 | 4407 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 4408 | end if; |
ee6ba406 | 4409 | end Division_Checks_Suppressed; |
4410 | ||
4411 | ----------------------------------- | |
4412 | -- Elaboration_Checks_Suppressed -- | |
4413 | ----------------------------------- | |
4414 | ||
4415 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4416 | begin | |
38f5559f | 4417 | -- The complication in this routine is that if we are in the dynamic |
4418 | -- model of elaboration, we also check All_Checks, since All_Checks | |
4419 | -- does not set Elaboration_Check explicitly. | |
4420 | ||
9dfe12ae | 4421 | if Present (E) then |
4422 | if Kill_Elaboration_Checks (E) then | |
4423 | return True; | |
38f5559f | 4424 | |
9dfe12ae | 4425 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 4426 | if Is_Check_Suppressed (E, Elaboration_Check) then |
4427 | return True; | |
4428 | elsif Dynamic_Elaboration_Checks then | |
4429 | return Is_Check_Suppressed (E, All_Checks); | |
4430 | else | |
4431 | return False; | |
4432 | end if; | |
9dfe12ae | 4433 | end if; |
4434 | end if; | |
4435 | ||
fafc6b97 | 4436 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 4437 | return True; |
4438 | elsif Dynamic_Elaboration_Checks then | |
fafc6b97 | 4439 | return Scope_Suppress.Suppress (All_Checks); |
38f5559f | 4440 | else |
4441 | return False; | |
4442 | end if; | |
ee6ba406 | 4443 | end Elaboration_Checks_Suppressed; |
4444 | ||
9dfe12ae | 4445 | --------------------------- |
4446 | -- Enable_Overflow_Check -- | |
4447 | --------------------------- | |
4448 | ||
4449 | procedure Enable_Overflow_Check (N : Node_Id) is | |
3cce7f32 | 4450 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 4451 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 4452 | Chk : Nat; |
4453 | OK : Boolean; | |
4454 | Ent : Entity_Id; | |
4455 | Ofs : Uint; | |
4456 | Lo : Uint; | |
4457 | Hi : Uint; | |
ee6ba406 | 4458 | |
ee6ba406 | 4459 | begin |
9dfe12ae | 4460 | if Debug_Flag_CC then |
4461 | w ("Enable_Overflow_Check for node ", Int (N)); | |
4462 | Write_Str (" Source location = "); | |
4463 | wl (Sloc (N)); | |
00c403ee | 4464 | pg (Union_Id (N)); |
ee6ba406 | 4465 | end if; |
ee6ba406 | 4466 | |
75209ec5 | 4467 | -- No check if overflow checks suppressed for type of node |
4468 | ||
0df9d43f | 4469 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 4470 | return; |
4471 | ||
49260fa5 | 4472 | -- Nothing to do for unsigned integer types, which do not overflow |
4473 | ||
4474 | elsif Is_Modular_Integer_Type (Typ) then | |
4475 | return; | |
3cce7f32 | 4476 | end if; |
4477 | ||
0df9d43f | 4478 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 4479 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
4480 | -- probably more extreme that it needs to be, but what is going on here | |
4481 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 4482 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 4483 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 4484 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 4485 | -- legacy reliable. |
3cce7f32 | 4486 | |
0df9d43f | 4487 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 4488 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
4489 | -- know that no check is needed. We skip all that in the two new modes, | |
4490 | -- since really overflow checking happens over a whole subtree, and we | |
4491 | -- do the corresponding optimizations later on when applying the checks. | |
4492 | ||
4493 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 4494 | if not (Overflow_Checks_Suppressed (Etype (N))) |
4495 | and then not (Is_Entity_Name (N) | |
4496 | and then Overflow_Checks_Suppressed (Entity (N))) | |
4497 | then | |
4498 | Activate_Overflow_Check (N); | |
4499 | end if; | |
3cce7f32 | 4500 | |
4501 | if Debug_Flag_CC then | |
4502 | w ("Minimized/Eliminated mode"); | |
4503 | end if; | |
4504 | ||
4505 | return; | |
4506 | end if; | |
4507 | ||
0df9d43f | 4508 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 4509 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 4510 | |
feff2f05 | 4511 | -- Nothing to do if the range of the result is known OK. We skip this |
4512 | -- for conversions, since the caller already did the check, and in any | |
4513 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 4514 | -- different. |
ee6ba406 | 4515 | |
3cce7f32 | 4516 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 4517 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 4518 | |
cc60bd16 | 4519 | -- Note in the test below that we assume that the range is not OK |
4520 | -- if a bound of the range is equal to that of the type. That's not | |
4521 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 4522 | |
9dfe12ae | 4523 | -- a) The way that Determine_Range works, it will typically report |
4524 | -- the bounds of the value as being equal to the bounds of the | |
4525 | -- type, because it either can't tell anything more precise, or | |
4526 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 4527 | |
9dfe12ae | 4528 | -- b) It is very unusual to have a situation in which this would |
4529 | -- generate an unnecessary overflow check (an example would be | |
4530 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 4531 | -- literal value one is added). |
ee6ba406 | 4532 | |
9dfe12ae | 4533 | -- c) The alternative is a lot of special casing in this routine |
4534 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 4535 | |
9dfe12ae | 4536 | if OK |
4537 | and then Lo > Expr_Value (Type_Low_Bound (Typ)) | |
4538 | and then Hi < Expr_Value (Type_High_Bound (Typ)) | |
4539 | then | |
4540 | if Debug_Flag_CC then | |
4541 | w ("No overflow check required"); | |
4542 | end if; | |
4543 | ||
4544 | return; | |
4545 | end if; | |
4546 | end if; | |
4547 | ||
feff2f05 | 4548 | -- If not in optimizing mode, set flag and we are done. We are also done |
4549 | -- (and just set the flag) if the type is not a discrete type, since it | |
4550 | -- is not worth the effort to eliminate checks for other than discrete | |
4551 | -- types. In addition, we take this same path if we have stored the | |
4552 | -- maximum number of checks possible already (a very unlikely situation, | |
4553 | -- but we do not want to blow up!) | |
9dfe12ae | 4554 | |
4555 | if Optimization_Level = 0 | |
4556 | or else not Is_Discrete_Type (Etype (N)) | |
4557 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4558 | then |
00c403ee | 4559 | Activate_Overflow_Check (N); |
9dfe12ae | 4560 | |
4561 | if Debug_Flag_CC then | |
4562 | w ("Optimization off"); | |
4563 | end if; | |
4564 | ||
ee6ba406 | 4565 | return; |
9dfe12ae | 4566 | end if; |
ee6ba406 | 4567 | |
9dfe12ae | 4568 | -- Otherwise evaluate and check the expression |
4569 | ||
4570 | Find_Check | |
4571 | (Expr => N, | |
4572 | Check_Type => 'O', | |
4573 | Target_Type => Empty, | |
4574 | Entry_OK => OK, | |
4575 | Check_Num => Chk, | |
4576 | Ent => Ent, | |
4577 | Ofs => Ofs); | |
4578 | ||
4579 | if Debug_Flag_CC then | |
4580 | w ("Called Find_Check"); | |
4581 | w (" OK = ", OK); | |
4582 | ||
4583 | if OK then | |
4584 | w (" Check_Num = ", Chk); | |
4585 | w (" Ent = ", Int (Ent)); | |
4586 | Write_Str (" Ofs = "); | |
4587 | pid (Ofs); | |
4588 | end if; | |
4589 | end if; | |
ee6ba406 | 4590 | |
9dfe12ae | 4591 | -- If check is not of form to optimize, then set flag and we are done |
4592 | ||
4593 | if not OK then | |
00c403ee | 4594 | Activate_Overflow_Check (N); |
ee6ba406 | 4595 | return; |
9dfe12ae | 4596 | end if; |
ee6ba406 | 4597 | |
9dfe12ae | 4598 | -- If check is already performed, then return without setting flag |
4599 | ||
4600 | if Chk /= 0 then | |
4601 | if Debug_Flag_CC then | |
4602 | w ("Check suppressed!"); | |
4603 | end if; | |
ee6ba406 | 4604 | |
ee6ba406 | 4605 | return; |
9dfe12ae | 4606 | end if; |
ee6ba406 | 4607 | |
9dfe12ae | 4608 | -- Here we will make a new entry for the new check |
4609 | ||
00c403ee | 4610 | Activate_Overflow_Check (N); |
9dfe12ae | 4611 | Num_Saved_Checks := Num_Saved_Checks + 1; |
4612 | Saved_Checks (Num_Saved_Checks) := | |
4613 | (Killed => False, | |
4614 | Entity => Ent, | |
4615 | Offset => Ofs, | |
4616 | Check_Type => 'O', | |
4617 | Target_Type => Empty); | |
4618 | ||
4619 | if Debug_Flag_CC then | |
4620 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
4621 | w (" Entity = ", Int (Ent)); | |
4622 | Write_Str (" Offset = "); | |
4623 | pid (Ofs); | |
4624 | w (" Check_Type = O"); | |
4625 | w (" Target_Type = Empty"); | |
4626 | end if; | |
ee6ba406 | 4627 | |
feff2f05 | 4628 | -- If we get an exception, then something went wrong, probably because of |
4629 | -- an error in the structure of the tree due to an incorrect program. Or it | |
4630 | -- may be a bug in the optimization circuit. In either case the safest | |
4631 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 4632 | |
4633 | exception | |
4634 | when others => | |
00c403ee | 4635 | Activate_Overflow_Check (N); |
9dfe12ae | 4636 | |
4637 | if Debug_Flag_CC then | |
4638 | w (" exception occurred, overflow flag set"); | |
4639 | end if; | |
4640 | ||
4641 | return; | |
4642 | end Enable_Overflow_Check; | |
4643 | ||
4644 | ------------------------ | |
4645 | -- Enable_Range_Check -- | |
4646 | ------------------------ | |
4647 | ||
4648 | procedure Enable_Range_Check (N : Node_Id) is | |
4649 | Chk : Nat; | |
4650 | OK : Boolean; | |
4651 | Ent : Entity_Id; | |
4652 | Ofs : Uint; | |
4653 | Ttyp : Entity_Id; | |
4654 | P : Node_Id; | |
4655 | ||
4656 | begin | |
feff2f05 | 4657 | -- Return if unchecked type conversion with range check killed. In this |
4658 | -- case we never set the flag (that's what Kill_Range_Check is about!) | |
9dfe12ae | 4659 | |
4660 | if Nkind (N) = N_Unchecked_Type_Conversion | |
4661 | and then Kill_Range_Check (N) | |
ee6ba406 | 4662 | then |
4663 | return; | |
9dfe12ae | 4664 | end if; |
ee6ba406 | 4665 | |
55e8372b | 4666 | -- Do not set range check flag if parent is assignment statement or |
4667 | -- object declaration with Suppress_Assignment_Checks flag set | |
4668 | ||
4669 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
4670 | and then Suppress_Assignment_Checks (Parent (N)) | |
4671 | then | |
4672 | return; | |
4673 | end if; | |
4674 | ||
0577b0b1 | 4675 | -- Check for various cases where we should suppress the range check |
4676 | ||
4677 | -- No check if range checks suppressed for type of node | |
4678 | ||
4679 | if Present (Etype (N)) | |
4680 | and then Range_Checks_Suppressed (Etype (N)) | |
4681 | then | |
4682 | return; | |
4683 | ||
4684 | -- No check if node is an entity name, and range checks are suppressed | |
4685 | -- for this entity, or for the type of this entity. | |
4686 | ||
4687 | elsif Is_Entity_Name (N) | |
4688 | and then (Range_Checks_Suppressed (Entity (N)) | |
4689 | or else Range_Checks_Suppressed (Etype (Entity (N)))) | |
4690 | then | |
4691 | return; | |
4692 | ||
4693 | -- No checks if index of array, and index checks are suppressed for | |
4694 | -- the array object or the type of the array. | |
4695 | ||
4696 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
4697 | declare | |
4698 | Pref : constant Node_Id := Prefix (Parent (N)); | |
4699 | begin | |
4700 | if Is_Entity_Name (Pref) | |
4701 | and then Index_Checks_Suppressed (Entity (Pref)) | |
4702 | then | |
4703 | return; | |
4704 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
4705 | return; | |
4706 | end if; | |
4707 | end; | |
4708 | end if; | |
4709 | ||
9dfe12ae | 4710 | -- Debug trace output |
ee6ba406 | 4711 | |
9dfe12ae | 4712 | if Debug_Flag_CC then |
4713 | w ("Enable_Range_Check for node ", Int (N)); | |
4714 | Write_Str (" Source location = "); | |
4715 | wl (Sloc (N)); | |
00c403ee | 4716 | pg (Union_Id (N)); |
9dfe12ae | 4717 | end if; |
4718 | ||
feff2f05 | 4719 | -- If not in optimizing mode, set flag and we are done. We are also done |
4720 | -- (and just set the flag) if the type is not a discrete type, since it | |
4721 | -- is not worth the effort to eliminate checks for other than discrete | |
4722 | -- types. In addition, we take this same path if we have stored the | |
4723 | -- maximum number of checks possible already (a very unlikely situation, | |
4724 | -- but we do not want to blow up!) | |
9dfe12ae | 4725 | |
4726 | if Optimization_Level = 0 | |
4727 | or else No (Etype (N)) | |
4728 | or else not Is_Discrete_Type (Etype (N)) | |
4729 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4730 | then |
00c403ee | 4731 | Activate_Range_Check (N); |
9dfe12ae | 4732 | |
4733 | if Debug_Flag_CC then | |
4734 | w ("Optimization off"); | |
4735 | end if; | |
4736 | ||
ee6ba406 | 4737 | return; |
9dfe12ae | 4738 | end if; |
ee6ba406 | 4739 | |
9dfe12ae | 4740 | -- Otherwise find out the target type |
ee6ba406 | 4741 | |
9dfe12ae | 4742 | P := Parent (N); |
ee6ba406 | 4743 | |
9dfe12ae | 4744 | -- For assignment, use left side subtype |
4745 | ||
4746 | if Nkind (P) = N_Assignment_Statement | |
4747 | and then Expression (P) = N | |
4748 | then | |
4749 | Ttyp := Etype (Name (P)); | |
4750 | ||
4751 | -- For indexed component, use subscript subtype | |
4752 | ||
4753 | elsif Nkind (P) = N_Indexed_Component then | |
4754 | declare | |
4755 | Atyp : Entity_Id; | |
4756 | Indx : Node_Id; | |
4757 | Subs : Node_Id; | |
4758 | ||
4759 | begin | |
4760 | Atyp := Etype (Prefix (P)); | |
4761 | ||
4762 | if Is_Access_Type (Atyp) then | |
4763 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 4764 | |
4765 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 4766 | -- perform check unconditionally: it depends on the bounds of |
4767 | -- an object and we cannot currently recognize whether the test | |
4768 | -- may be redundant. | |
f07ea091 | 4769 | |
4770 | if not Is_Constrained (Atyp) then | |
00c403ee | 4771 | Activate_Range_Check (N); |
f07ea091 | 4772 | return; |
4773 | end if; | |
7189d17f | 4774 | |
feff2f05 | 4775 | -- Ditto if the prefix is an explicit dereference whose designated |
4776 | -- type is unconstrained. | |
7189d17f | 4777 | |
4778 | elsif Nkind (Prefix (P)) = N_Explicit_Dereference | |
4779 | and then not Is_Constrained (Atyp) | |
4780 | then | |
00c403ee | 4781 | Activate_Range_Check (N); |
7189d17f | 4782 | return; |
9dfe12ae | 4783 | end if; |
4784 | ||
4785 | Indx := First_Index (Atyp); | |
4786 | Subs := First (Expressions (P)); | |
4787 | loop | |
4788 | if Subs = N then | |
4789 | Ttyp := Etype (Indx); | |
4790 | exit; | |
4791 | end if; | |
4792 | ||
4793 | Next_Index (Indx); | |
4794 | Next (Subs); | |
4795 | end loop; | |
4796 | end; | |
4797 | ||
4798 | -- For now, ignore all other cases, they are not so interesting | |
4799 | ||
4800 | else | |
4801 | if Debug_Flag_CC then | |
4802 | w (" target type not found, flag set"); | |
4803 | end if; | |
4804 | ||
00c403ee | 4805 | Activate_Range_Check (N); |
9dfe12ae | 4806 | return; |
4807 | end if; | |
4808 | ||
4809 | -- Evaluate and check the expression | |
4810 | ||
4811 | Find_Check | |
4812 | (Expr => N, | |
4813 | Check_Type => 'R', | |
4814 | Target_Type => Ttyp, | |
4815 | Entry_OK => OK, | |
4816 | Check_Num => Chk, | |
4817 | Ent => Ent, | |
4818 | Ofs => Ofs); | |
4819 | ||
4820 | if Debug_Flag_CC then | |
4821 | w ("Called Find_Check"); | |
4822 | w ("Target_Typ = ", Int (Ttyp)); | |
4823 | w (" OK = ", OK); | |
4824 | ||
4825 | if OK then | |
4826 | w (" Check_Num = ", Chk); | |
4827 | w (" Ent = ", Int (Ent)); | |
4828 | Write_Str (" Ofs = "); | |
4829 | pid (Ofs); | |
4830 | end if; | |
4831 | end if; | |
4832 | ||
4833 | -- If check is not of form to optimize, then set flag and we are done | |
4834 | ||
4835 | if not OK then | |
4836 | if Debug_Flag_CC then | |
4837 | w (" expression not of optimizable type, flag set"); | |
4838 | end if; | |
4839 | ||
00c403ee | 4840 | Activate_Range_Check (N); |
9dfe12ae | 4841 | return; |
4842 | end if; | |
4843 | ||
4844 | -- If check is already performed, then return without setting flag | |
4845 | ||
4846 | if Chk /= 0 then | |
4847 | if Debug_Flag_CC then | |
4848 | w ("Check suppressed!"); | |
4849 | end if; | |
4850 | ||
4851 | return; | |
4852 | end if; | |
4853 | ||
4854 | -- Here we will make a new entry for the new check | |
4855 | ||
00c403ee | 4856 | Activate_Range_Check (N); |
9dfe12ae | 4857 | Num_Saved_Checks := Num_Saved_Checks + 1; |
4858 | Saved_Checks (Num_Saved_Checks) := | |
4859 | (Killed => False, | |
4860 | Entity => Ent, | |
4861 | Offset => Ofs, | |
4862 | Check_Type => 'R', | |
4863 | Target_Type => Ttyp); | |
4864 | ||
4865 | if Debug_Flag_CC then | |
4866 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
4867 | w (" Entity = ", Int (Ent)); | |
4868 | Write_Str (" Offset = "); | |
4869 | pid (Ofs); | |
4870 | w (" Check_Type = R"); | |
4871 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 4872 | pg (Union_Id (Ttyp)); |
9dfe12ae | 4873 | end if; |
4874 | ||
feff2f05 | 4875 | -- If we get an exception, then something went wrong, probably because of |
4876 | -- an error in the structure of the tree due to an incorrect program. Or | |
4877 | -- it may be a bug in the optimization circuit. In either case the safest | |
4878 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 4879 | |
4880 | exception | |
4881 | when others => | |
00c403ee | 4882 | Activate_Range_Check (N); |
9dfe12ae | 4883 | |
4884 | if Debug_Flag_CC then | |
4885 | w (" exception occurred, range flag set"); | |
4886 | end if; | |
4887 | ||
4888 | return; | |
4889 | end Enable_Range_Check; | |
4890 | ||
4891 | ------------------ | |
4892 | -- Ensure_Valid -- | |
4893 | ------------------ | |
4894 | ||
4895 | procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is | |
4896 | Typ : constant Entity_Id := Etype (Expr); | |
4897 | ||
4898 | begin | |
4899 | -- Ignore call if we are not doing any validity checking | |
4900 | ||
4901 | if not Validity_Checks_On then | |
4902 | return; | |
4903 | ||
0577b0b1 | 4904 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 4905 | |
0577b0b1 | 4906 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 4907 | return; |
4908 | ||
feff2f05 | 4909 | -- No check required if expression is from the expander, we assume the |
4910 | -- expander will generate whatever checks are needed. Note that this is | |
4911 | -- not just an optimization, it avoids infinite recursions! | |
9dfe12ae | 4912 | |
4913 | -- Unchecked conversions must be checked, unless they are initialized | |
4914 | -- scalar values, as in a component assignment in an init proc. | |
4915 | ||
4916 | -- In addition, we force a check if Force_Validity_Checks is set | |
4917 | ||
4918 | elsif not Comes_From_Source (Expr) | |
4919 | and then not Force_Validity_Checks | |
4920 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
4921 | or else Kill_Range_Check (Expr)) | |
4922 | then | |
4923 | return; | |
4924 | ||
4925 | -- No check required if expression is known to have valid value | |
4926 | ||
4927 | elsif Expr_Known_Valid (Expr) then | |
4928 | return; | |
4929 | ||
feff2f05 | 4930 | -- Ignore case of enumeration with holes where the flag is set not to |
4931 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 4932 | |
4933 | elsif Is_Enumeration_Type (Typ) | |
4934 | and then Has_Non_Standard_Rep (Typ) | |
4935 | and then Holes_OK | |
4936 | then | |
4937 | return; | |
4938 | ||
f2a06be9 | 4939 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 4940 | |
4941 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
4942 | and then Expr = Name (Parent (Expr)) | |
4943 | then | |
4944 | return; | |
4945 | ||
6fb3c314 | 4946 | -- No check on a universal real constant. The context will eventually |
38f5559f | 4947 | -- convert it to a machine number for some target type, or report an |
4948 | -- illegality. | |
4949 | ||
4950 | elsif Nkind (Expr) = N_Real_Literal | |
4951 | and then Etype (Expr) = Universal_Real | |
4952 | then | |
4953 | return; | |
4954 | ||
6fb3c314 | 4955 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 4956 | -- no possible check applies. We ignore the old ACATS chestnuts that |
4957 | -- involve Boolean range True..True. | |
4958 | ||
4959 | -- Note: validity checks are generated for expressions that yield a | |
4960 | -- scalar type, when it is possible to create a value that is outside of | |
4961 | -- the type. If this is a one-bit boolean no such value exists. This is | |
4962 | -- an optimization, and it also prevents compiler blowing up during the | |
4963 | -- elaboration of improperly expanded packed array references. | |
4964 | ||
4965 | elsif Nkind (Expr) = N_Indexed_Component | |
4966 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
4967 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
4968 | then | |
4969 | return; | |
4970 | ||
9dfe12ae | 4971 | -- An annoying special case. If this is an out parameter of a scalar |
4972 | -- type, then the value is not going to be accessed, therefore it is | |
4973 | -- inappropriate to do any validity check at the call site. | |
4974 | ||
4975 | else | |
4976 | -- Only need to worry about scalar types | |
4977 | ||
4978 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 4979 | declare |
4980 | P : Node_Id; | |
4981 | N : Node_Id; | |
4982 | E : Entity_Id; | |
4983 | F : Entity_Id; | |
4984 | A : Node_Id; | |
4985 | L : List_Id; | |
4986 | ||
4987 | begin | |
4988 | -- Find actual argument (which may be a parameter association) | |
4989 | -- and the parent of the actual argument (the call statement) | |
4990 | ||
4991 | N := Expr; | |
4992 | P := Parent (Expr); | |
4993 | ||
4994 | if Nkind (P) = N_Parameter_Association then | |
4995 | N := P; | |
4996 | P := Parent (N); | |
4997 | end if; | |
4998 | ||
feff2f05 | 4999 | -- Only need to worry if we are argument of a procedure call |
5000 | -- since functions don't have out parameters. If this is an | |
5001 | -- indirect or dispatching call, get signature from the | |
5002 | -- subprogram type. | |
ee6ba406 | 5003 | |
5004 | if Nkind (P) = N_Procedure_Call_Statement then | |
5005 | L := Parameter_Associations (P); | |
9dfe12ae | 5006 | |
5007 | if Is_Entity_Name (Name (P)) then | |
5008 | E := Entity (Name (P)); | |
5009 | else | |
5010 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
5011 | E := Etype (Name (P)); | |
5012 | end if; | |
ee6ba406 | 5013 | |
feff2f05 | 5014 | -- Only need to worry if there are indeed actuals, and if |
5015 | -- this could be a procedure call, otherwise we cannot get a | |
5016 | -- match (either we are not an argument, or the mode of the | |
5017 | -- formal is not OUT). This test also filters out the | |
5018 | -- generic case. | |
ee6ba406 | 5019 | |
5020 | if Is_Non_Empty_List (L) | |
5021 | and then Is_Subprogram (E) | |
5022 | then | |
feff2f05 | 5023 | -- This is the loop through parameters, looking for an |
5024 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 5025 | |
5026 | F := First_Formal (E); | |
5027 | A := First (L); | |
ee6ba406 | 5028 | while Present (F) loop |
5029 | if Ekind (F) = E_Out_Parameter and then A = N then | |
5030 | return; | |
5031 | end if; | |
5032 | ||
5033 | Next_Formal (F); | |
5034 | Next (A); | |
5035 | end loop; | |
5036 | end if; | |
5037 | end if; | |
5038 | end; | |
5039 | end if; | |
5040 | end if; | |
5041 | ||
fa6a6949 | 5042 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 5043 | -- checking: if they are valid, a boolean or short-circuit operation |
5044 | -- with them will be valid as well. | |
784d4230 | 5045 | |
5046 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 5047 | and then |
fa6a6949 | 5048 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 5049 | then |
5050 | return; | |
5051 | end if; | |
5052 | ||
0577b0b1 | 5053 | -- If we fall through, a validity check is required |
ee6ba406 | 5054 | |
5055 | Insert_Valid_Check (Expr); | |
ce7498d3 | 5056 | |
5057 | if Is_Entity_Name (Expr) | |
5058 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
5059 | then | |
5060 | Set_Is_Known_Valid (Entity (Expr)); | |
5061 | end if; | |
ee6ba406 | 5062 | end Ensure_Valid; |
5063 | ||
5064 | ---------------------- | |
5065 | -- Expr_Known_Valid -- | |
5066 | ---------------------- | |
5067 | ||
5068 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
5069 | Typ : constant Entity_Id := Etype (Expr); | |
5070 | ||
5071 | begin | |
feff2f05 | 5072 | -- Non-scalar types are always considered valid, since they never give |
5073 | -- rise to the issues of erroneous or bounded error behavior that are | |
5074 | -- the concern. In formal reference manual terms the notion of validity | |
5075 | -- only applies to scalar types. Note that even when packed arrays are | |
5076 | -- represented using modular types, they are still arrays semantically, | |
5077 | -- so they are also always valid (in particular, the unused bits can be | |
5078 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 5079 | |
fa814356 | 5080 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then |
ee6ba406 | 5081 | return True; |
5082 | ||
5083 | -- If no validity checking, then everything is considered valid | |
5084 | ||
5085 | elsif not Validity_Checks_On then | |
5086 | return True; | |
5087 | ||
5088 | -- Floating-point types are considered valid unless floating-point | |
5089 | -- validity checks have been specifically turned on. | |
5090 | ||
5091 | elsif Is_Floating_Point_Type (Typ) | |
5092 | and then not Validity_Check_Floating_Point | |
5093 | then | |
5094 | return True; | |
5095 | ||
feff2f05 | 5096 | -- If the expression is the value of an object that is known to be |
5097 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 5098 | |
5099 | elsif Is_Entity_Name (Expr) | |
5100 | and then Is_Known_Valid (Entity (Expr)) | |
5101 | then | |
5102 | return True; | |
5103 | ||
0577b0b1 | 5104 | -- References to discriminants are always considered valid. The value |
5105 | -- of a discriminant gets checked when the object is built. Within the | |
5106 | -- record, we consider it valid, and it is important to do so, since | |
5107 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 5108 | -- reference discriminants out of scope. Discriminants of concurrent |
5109 | -- types are excluded for the same reason. | |
0577b0b1 | 5110 | |
5111 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 5112 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 5113 | then |
5114 | return True; | |
5115 | ||
feff2f05 | 5116 | -- If the type is one for which all values are known valid, then we are |
5117 | -- sure that the value is valid except in the slightly odd case where | |
5118 | -- the expression is a reference to a variable whose size has been | |
5119 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 5120 | |
5121 | elsif Is_Known_Valid (Typ) then | |
5122 | if Is_Entity_Name (Expr) | |
5123 | and then Ekind (Entity (Expr)) = E_Variable | |
5124 | and then Esize (Entity (Expr)) > Esize (Typ) | |
5125 | then | |
5126 | return False; | |
5127 | else | |
5128 | return True; | |
5129 | end if; | |
5130 | ||
5131 | -- Integer and character literals always have valid values, where | |
5132 | -- appropriate these will be range checked in any case. | |
5133 | ||
5134 | elsif Nkind (Expr) = N_Integer_Literal | |
5135 | or else | |
5136 | Nkind (Expr) = N_Character_Literal | |
5137 | then | |
5138 | return True; | |
5139 | ||
91e47010 | 5140 | -- Real literals are assumed to be valid in VM targets |
5141 | ||
5142 | elsif VM_Target /= No_VM | |
5143 | and then Nkind (Expr) = N_Real_Literal | |
5144 | then | |
5145 | return True; | |
5146 | ||
ee6ba406 | 5147 | -- If we have a type conversion or a qualification of a known valid |
5148 | -- value, then the result will always be valid. | |
5149 | ||
5150 | elsif Nkind (Expr) = N_Type_Conversion | |
5151 | or else | |
5152 | Nkind (Expr) = N_Qualified_Expression | |
5153 | then | |
5154 | return Expr_Known_Valid (Expression (Expr)); | |
5155 | ||
38f5559f | 5156 | -- The result of any operator is always considered valid, since we |
5157 | -- assume the necessary checks are done by the operator. For operators | |
5158 | -- on floating-point operations, we must also check when the operation | |
5159 | -- is the right-hand side of an assignment, or is an actual in a call. | |
ee6ba406 | 5160 | |
0577b0b1 | 5161 | elsif Nkind (Expr) in N_Op then |
1d90d657 | 5162 | if Is_Floating_Point_Type (Typ) |
5163 | and then Validity_Check_Floating_Point | |
5164 | and then | |
5165 | (Nkind (Parent (Expr)) = N_Assignment_Statement | |
5166 | or else Nkind (Parent (Expr)) = N_Function_Call | |
5167 | or else Nkind (Parent (Expr)) = N_Parameter_Association) | |
5168 | then | |
5169 | return False; | |
5170 | else | |
5171 | return True; | |
5172 | end if; | |
5173 | ||
feff2f05 | 5174 | -- The result of a membership test is always valid, since it is true or |
5175 | -- false, there are no other possibilities. | |
0577b0b1 | 5176 | |
5177 | elsif Nkind (Expr) in N_Membership_Test then | |
5178 | return True; | |
5179 | ||
ee6ba406 | 5180 | -- For all other cases, we do not know the expression is valid |
5181 | ||
5182 | else | |
5183 | return False; | |
5184 | end if; | |
5185 | end Expr_Known_Valid; | |
5186 | ||
9dfe12ae | 5187 | ---------------- |
5188 | -- Find_Check -- | |
5189 | ---------------- | |
5190 | ||
5191 | procedure Find_Check | |
5192 | (Expr : Node_Id; | |
5193 | Check_Type : Character; | |
5194 | Target_Type : Entity_Id; | |
5195 | Entry_OK : out Boolean; | |
5196 | Check_Num : out Nat; | |
5197 | Ent : out Entity_Id; | |
5198 | Ofs : out Uint) | |
5199 | is | |
5200 | function Within_Range_Of | |
5201 | (Target_Type : Entity_Id; | |
314a23b6 | 5202 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 5203 | -- Given a requirement for checking a range against Target_Type, and |
5204 | -- and a range Check_Type against which a check has already been made, | |
5205 | -- determines if the check against check type is sufficient to ensure | |
5206 | -- that no check against Target_Type is required. | |
5207 | ||
5208 | --------------------- | |
5209 | -- Within_Range_Of -- | |
5210 | --------------------- | |
5211 | ||
5212 | function Within_Range_Of | |
5213 | (Target_Type : Entity_Id; | |
314a23b6 | 5214 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 5215 | is |
5216 | begin | |
5217 | if Target_Type = Check_Type then | |
5218 | return True; | |
5219 | ||
5220 | else | |
5221 | declare | |
5222 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
5223 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
5224 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
5225 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
5226 | ||
5227 | begin | |
5228 | if (Tlo = Clo | |
5229 | or else (Compile_Time_Known_Value (Tlo) | |
5230 | and then | |
5231 | Compile_Time_Known_Value (Clo) | |
5232 | and then | |
5233 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
5234 | and then | |
5235 | (Thi = Chi | |
5236 | or else (Compile_Time_Known_Value (Thi) | |
5237 | and then | |
5238 | Compile_Time_Known_Value (Chi) | |
5239 | and then | |
5240 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
5241 | then | |
5242 | return True; | |
5243 | else | |
5244 | return False; | |
5245 | end if; | |
5246 | end; | |
5247 | end if; | |
5248 | end Within_Range_Of; | |
5249 | ||
5250 | -- Start of processing for Find_Check | |
5251 | ||
5252 | begin | |
ed195555 | 5253 | -- Establish default, in case no entry is found |
9dfe12ae | 5254 | |
5255 | Check_Num := 0; | |
5256 | ||
5257 | -- Case of expression is simple entity reference | |
5258 | ||
5259 | if Is_Entity_Name (Expr) then | |
5260 | Ent := Entity (Expr); | |
5261 | Ofs := Uint_0; | |
5262 | ||
5263 | -- Case of expression is entity + known constant | |
5264 | ||
5265 | elsif Nkind (Expr) = N_Op_Add | |
5266 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5267 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5268 | then | |
5269 | Ent := Entity (Left_Opnd (Expr)); | |
5270 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
5271 | ||
5272 | -- Case of expression is entity - known constant | |
5273 | ||
5274 | elsif Nkind (Expr) = N_Op_Subtract | |
5275 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5276 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5277 | then | |
5278 | Ent := Entity (Left_Opnd (Expr)); | |
5279 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
5280 | ||
5281 | -- Any other expression is not of the right form | |
5282 | ||
5283 | else | |
5284 | Ent := Empty; | |
5285 | Ofs := Uint_0; | |
5286 | Entry_OK := False; | |
5287 | return; | |
5288 | end if; | |
5289 | ||
feff2f05 | 5290 | -- Come here with expression of appropriate form, check if entity is an |
5291 | -- appropriate one for our purposes. | |
9dfe12ae | 5292 | |
5293 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 5294 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 5295 | and then not Is_Library_Level_Entity (Ent) |
5296 | then | |
5297 | Entry_OK := True; | |
5298 | else | |
5299 | Entry_OK := False; | |
5300 | return; | |
5301 | end if; | |
5302 | ||
5303 | -- See if there is matching check already | |
5304 | ||
5305 | for J in reverse 1 .. Num_Saved_Checks loop | |
5306 | declare | |
5307 | SC : Saved_Check renames Saved_Checks (J); | |
5308 | ||
5309 | begin | |
5310 | if SC.Killed = False | |
5311 | and then SC.Entity = Ent | |
5312 | and then SC.Offset = Ofs | |
5313 | and then SC.Check_Type = Check_Type | |
5314 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
5315 | then | |
5316 | Check_Num := J; | |
5317 | return; | |
5318 | end if; | |
5319 | end; | |
5320 | end loop; | |
5321 | ||
5322 | -- If we fall through entry was not found | |
5323 | ||
9dfe12ae | 5324 | return; |
5325 | end Find_Check; | |
5326 | ||
5327 | --------------------------------- | |
5328 | -- Generate_Discriminant_Check -- | |
5329 | --------------------------------- | |
5330 | ||
5331 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 5332 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 5333 | |
5334 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
5335 | Loc : constant Source_Ptr := Sloc (N); | |
5336 | Pref : constant Node_Id := Prefix (N); | |
5337 | Sel : constant Node_Id := Selector_Name (N); | |
5338 | ||
5339 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 5340 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 5341 | -- The original component to be checked |
5342 | ||
5343 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 5344 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 5345 | -- The discriminant checking function |
5346 | ||
5347 | Discr : Entity_Id; | |
5348 | -- One discriminant to be checked in the type | |
5349 | ||
5350 | Real_Discr : Entity_Id; | |
5351 | -- Actual discriminant in the call | |
5352 | ||
5353 | Pref_Type : Entity_Id; | |
5354 | -- Type of relevant prefix (ignoring private/access stuff) | |
5355 | ||
5356 | Args : List_Id; | |
5357 | -- List of arguments for function call | |
5358 | ||
5359 | Formal : Entity_Id; | |
feff2f05 | 5360 | -- Keep track of the formal corresponding to the actual we build for |
5361 | -- each discriminant, in order to be able to perform the necessary type | |
5362 | -- conversions. | |
9dfe12ae | 5363 | |
5364 | Scomp : Node_Id; | |
5365 | -- Selected component reference for checking function argument | |
5366 | ||
5367 | begin | |
5368 | Pref_Type := Etype (Pref); | |
5369 | ||
5370 | -- Force evaluation of the prefix, so that it does not get evaluated | |
5371 | -- twice (once for the check, once for the actual reference). Such a | |
5372 | -- double evaluation is always a potential source of inefficiency, | |
5373 | -- and is functionally incorrect in the volatile case, or when the | |
5374 | -- prefix may have side-effects. An entity or a component of an | |
5375 | -- entity requires no evaluation. | |
5376 | ||
5377 | if Is_Entity_Name (Pref) then | |
5378 | if Treat_As_Volatile (Entity (Pref)) then | |
5379 | Force_Evaluation (Pref, Name_Req => True); | |
5380 | end if; | |
5381 | ||
5382 | elsif Treat_As_Volatile (Etype (Pref)) then | |
5383 | Force_Evaluation (Pref, Name_Req => True); | |
5384 | ||
5385 | elsif Nkind (Pref) = N_Selected_Component | |
5386 | and then Is_Entity_Name (Prefix (Pref)) | |
5387 | then | |
5388 | null; | |
5389 | ||
5390 | else | |
5391 | Force_Evaluation (Pref, Name_Req => True); | |
5392 | end if; | |
5393 | ||
5394 | -- For a tagged type, use the scope of the original component to | |
5395 | -- obtain the type, because ??? | |
5396 | ||
5397 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
5398 | Pref_Type := Scope (Orig_Comp); | |
5399 | ||
feff2f05 | 5400 | -- For an untagged derived type, use the discriminants of the parent |
5401 | -- which have been renamed in the derivation, possibly by a one-to-many | |
5402 | -- discriminant constraint. For non-tagged type, initially get the Etype | |
5403 | -- of the prefix | |
9dfe12ae | 5404 | |
5405 | else | |
5406 | if Is_Derived_Type (Pref_Type) | |
5407 | and then Number_Discriminants (Pref_Type) /= | |
5408 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
5409 | then | |
5410 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
5411 | end if; | |
5412 | end if; | |
5413 | ||
5414 | -- We definitely should have a checking function, This routine should | |
5415 | -- not be called if no discriminant checking function is present. | |
5416 | ||
5417 | pragma Assert (Present (Discr_Fct)); | |
5418 | ||
5419 | -- Create the list of the actual parameters for the call. This list | |
5420 | -- is the list of the discriminant fields of the record expression to | |
5421 | -- be discriminant checked. | |
5422 | ||
5423 | Args := New_List; | |
5424 | Formal := First_Formal (Discr_Fct); | |
5425 | Discr := First_Discriminant (Pref_Type); | |
5426 | while Present (Discr) loop | |
5427 | ||
5428 | -- If we have a corresponding discriminant field, and a parent | |
5429 | -- subtype is present, then we want to use the corresponding | |
5430 | -- discriminant since this is the one with the useful value. | |
5431 | ||
5432 | if Present (Corresponding_Discriminant (Discr)) | |
5433 | and then Ekind (Pref_Type) = E_Record_Type | |
5434 | and then Present (Parent_Subtype (Pref_Type)) | |
5435 | then | |
5436 | Real_Discr := Corresponding_Discriminant (Discr); | |
5437 | else | |
5438 | Real_Discr := Discr; | |
5439 | end if; | |
5440 | ||
5441 | -- Construct the reference to the discriminant | |
5442 | ||
5443 | Scomp := | |
5444 | Make_Selected_Component (Loc, | |
5445 | Prefix => | |
5446 | Unchecked_Convert_To (Pref_Type, | |
5447 | Duplicate_Subexpr (Pref)), | |
5448 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
5449 | ||
5450 | -- Manually analyze and resolve this selected component. We really | |
5451 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 5452 | -- playing discriminal games etc with this reference. Then we append |
5453 | -- the argument to the list we are gathering. | |
9dfe12ae | 5454 | |
5455 | Set_Etype (Scomp, Etype (Real_Discr)); | |
5456 | Set_Analyzed (Scomp, True); | |
5457 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
5458 | ||
5459 | Next_Formal_With_Extras (Formal); | |
5460 | Next_Discriminant (Discr); | |
5461 | end loop; | |
5462 | ||
5463 | -- Now build and insert the call | |
5464 | ||
5465 | Insert_Action (N, | |
5466 | Make_Raise_Constraint_Error (Loc, | |
5467 | Condition => | |
5468 | Make_Function_Call (Loc, | |
5469 | Name => New_Occurrence_Of (Discr_Fct, Loc), | |
5470 | Parameter_Associations => Args), | |
5471 | Reason => CE_Discriminant_Check_Failed)); | |
5472 | end Generate_Discriminant_Check; | |
5473 | ||
5c99c290 | 5474 | --------------------------- |
5475 | -- Generate_Index_Checks -- | |
5476 | --------------------------- | |
9dfe12ae | 5477 | |
5478 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 5479 | |
5480 | function Entity_Of_Prefix return Entity_Id; | |
5481 | -- Returns the entity of the prefix of N (or Empty if not found) | |
5482 | ||
3f42e2a7 | 5483 | ---------------------- |
5484 | -- Entity_Of_Prefix -- | |
5485 | ---------------------- | |
5486 | ||
05f3e139 | 5487 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 5488 | P : Node_Id; |
5489 | ||
05f3e139 | 5490 | begin |
e5d38095 | 5491 | P := Prefix (N); |
05f3e139 | 5492 | while not Is_Entity_Name (P) loop |
5493 | if not Nkind_In (P, N_Selected_Component, | |
5494 | N_Indexed_Component) | |
5495 | then | |
5496 | return Empty; | |
5497 | end if; | |
5498 | ||
5499 | P := Prefix (P); | |
5500 | end loop; | |
5501 | ||
5502 | return Entity (P); | |
5503 | end Entity_Of_Prefix; | |
5504 | ||
5505 | -- Local variables | |
5506 | ||
5507 | Loc : constant Source_Ptr := Sloc (N); | |
5508 | A : constant Node_Id := Prefix (N); | |
5509 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
5510 | Sub : Node_Id; | |
9dfe12ae | 5511 | |
3f42e2a7 | 5512 | -- Start of processing for Generate_Index_Checks |
5513 | ||
9dfe12ae | 5514 | begin |
05f3e139 | 5515 | -- Ignore call if the prefix is not an array since we have a serious |
5516 | -- error in the sources. Ignore it also if index checks are suppressed | |
5517 | -- for array object or type. | |
0577b0b1 | 5518 | |
05f3e139 | 5519 | if not Is_Array_Type (Etype (A)) |
5520 | or else (Present (A_Ent) | |
e5d38095 | 5521 | and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 5522 | or else Index_Checks_Suppressed (Etype (A)) |
5523 | then | |
5524 | return; | |
df9fba45 | 5525 | |
5526 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
5527 | -- prefix. This case arises when analysis has determined that constructs | |
5528 | -- such as | |
5529 | ||
5530 | -- Prefix'Loop_Entry (Expr) | |
5531 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
5532 | ||
5533 | -- require rewriting for error detection purposes. A side effect of this | |
5534 | -- action is the generation of index checks that mention 'Loop_Entry. | |
5535 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
5536 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
5537 | ||
5538 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
5539 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
5540 | then | |
5541 | return; | |
0577b0b1 | 5542 | end if; |
5543 | ||
05f3e139 | 5544 | -- Generate a raise of constraint error with the appropriate reason and |
5545 | -- a condition of the form: | |
5546 | ||
3f42e2a7 | 5547 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 5548 | |
5549 | -- Note that the reason we generate the conversion to the base type here | |
5550 | -- is that we definitely want the range check to take place, even if it | |
5551 | -- looks like the subtype is OK. Optimization considerations that allow | |
5552 | -- us to omit the check have already been taken into account in the | |
5553 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 5554 | |
9dfe12ae | 5555 | Sub := First (Expressions (N)); |
05f3e139 | 5556 | |
5557 | -- Handle string literals | |
5558 | ||
5559 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 5560 | if Do_Range_Check (Sub) then |
5561 | Set_Do_Range_Check (Sub, False); | |
5562 | ||
05f3e139 | 5563 | -- For string literals we obtain the bounds of the string from the |
5564 | -- associated subtype. | |
9dfe12ae | 5565 | |
05f3e139 | 5566 | Insert_Action (N, |
094ed68e | 5567 | Make_Raise_Constraint_Error (Loc, |
5568 | Condition => | |
5569 | Make_Not_In (Loc, | |
5570 | Left_Opnd => | |
5571 | Convert_To (Base_Type (Etype (Sub)), | |
5572 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5573 | Right_Opnd => | |
5574 | Make_Attribute_Reference (Loc, | |
5575 | Prefix => New_Reference_To (Etype (A), Loc), | |
5576 | Attribute_Name => Name_Range)), | |
5577 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5578 | end if; |
9dfe12ae | 5579 | |
05f3e139 | 5580 | -- General case |
9dfe12ae | 5581 | |
05f3e139 | 5582 | else |
5583 | declare | |
5584 | A_Idx : Node_Id := Empty; | |
5585 | A_Range : Node_Id; | |
5586 | Ind : Nat; | |
5587 | Num : List_Id; | |
5588 | Range_N : Node_Id; | |
9dfe12ae | 5589 | |
05f3e139 | 5590 | begin |
5591 | A_Idx := First_Index (Etype (A)); | |
5592 | Ind := 1; | |
5593 | while Present (Sub) loop | |
5594 | if Do_Range_Check (Sub) then | |
5595 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 5596 | |
05f3e139 | 5597 | -- Force evaluation except for the case of a simple name of |
5598 | -- a non-volatile entity. | |
9dfe12ae | 5599 | |
05f3e139 | 5600 | if not Is_Entity_Name (Sub) |
5601 | or else Treat_As_Volatile (Entity (Sub)) | |
5602 | then | |
5603 | Force_Evaluation (Sub); | |
5604 | end if; | |
9dfe12ae | 5605 | |
05f3e139 | 5606 | if Nkind (A_Idx) = N_Range then |
5607 | A_Range := A_Idx; | |
5608 | ||
5609 | elsif Nkind (A_Idx) = N_Identifier | |
5610 | or else Nkind (A_Idx) = N_Expanded_Name | |
5611 | then | |
5612 | A_Range := Scalar_Range (Entity (A_Idx)); | |
5613 | ||
5614 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
5615 | A_Range := Range_Expression (Constraint (A_Idx)); | |
5616 | end if; | |
5617 | ||
5618 | -- For array objects with constant bounds we can generate | |
5619 | -- the index check using the bounds of the type of the index | |
5620 | ||
5621 | if Present (A_Ent) | |
5622 | and then Ekind (A_Ent) = E_Variable | |
5623 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
5624 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
5625 | then | |
5626 | Range_N := | |
5627 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 5628 | Prefix => |
5629 | New_Reference_To (Etype (A_Idx), Loc), | |
05f3e139 | 5630 | Attribute_Name => Name_Range); |
5631 | ||
5632 | -- For arrays with non-constant bounds we cannot generate | |
5633 | -- the index check using the bounds of the type of the index | |
5634 | -- since it may reference discriminants of some enclosing | |
5635 | -- type. We obtain the bounds directly from the prefix | |
5636 | -- object. | |
5637 | ||
5638 | else | |
5639 | if Ind = 1 then | |
5640 | Num := No_List; | |
5641 | else | |
5642 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
5643 | end if; | |
5644 | ||
5645 | Range_N := | |
5646 | Make_Attribute_Reference (Loc, | |
5647 | Prefix => | |
5648 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
5649 | Attribute_Name => Name_Range, | |
5650 | Expressions => Num); | |
5651 | end if; | |
5652 | ||
5653 | Insert_Action (N, | |
094ed68e | 5654 | Make_Raise_Constraint_Error (Loc, |
5655 | Condition => | |
5656 | Make_Not_In (Loc, | |
5657 | Left_Opnd => | |
5658 | Convert_To (Base_Type (Etype (Sub)), | |
5659 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5660 | Right_Opnd => Range_N), | |
5661 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5662 | end if; |
5663 | ||
5664 | A_Idx := Next_Index (A_Idx); | |
5665 | Ind := Ind + 1; | |
5666 | Next (Sub); | |
5667 | end loop; | |
5668 | end; | |
5669 | end if; | |
9dfe12ae | 5670 | end Generate_Index_Checks; |
5671 | ||
5672 | -------------------------- | |
5673 | -- Generate_Range_Check -- | |
5674 | -------------------------- | |
5675 | ||
5676 | procedure Generate_Range_Check | |
5677 | (N : Node_Id; | |
5678 | Target_Type : Entity_Id; | |
5679 | Reason : RT_Exception_Code) | |
5680 | is | |
5681 | Loc : constant Source_Ptr := Sloc (N); | |
5682 | Source_Type : constant Entity_Id := Etype (N); | |
5683 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
5684 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
5685 | ||
5686 | begin | |
feff2f05 | 5687 | -- First special case, if the source type is already within the range |
5688 | -- of the target type, then no check is needed (probably we should have | |
5689 | -- stopped Do_Range_Check from being set in the first place, but better | |
b40670e1 | 5690 | -- late than never in preventing junk code! |
9dfe12ae | 5691 | |
7a1dabb3 | 5692 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 5693 | |
5694 | -- We do NOT apply this if the source node is a literal, since in this | |
5695 | -- case the literal has already been labeled as having the subtype of | |
5696 | -- the target. | |
5697 | ||
9dfe12ae | 5698 | and then not |
b40670e1 | 5699 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 5700 | or else |
b40670e1 | 5701 | (Is_Entity_Name (N) |
5702 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
5703 | ||
5704 | -- Also do not apply this for floating-point if Check_Float_Overflow | |
5705 | ||
5706 | and then not | |
5707 | (Is_Floating_Point_Type (Source_Type) and Check_Float_Overflow) | |
9dfe12ae | 5708 | then |
5709 | return; | |
5710 | end if; | |
5711 | ||
5712 | -- We need a check, so force evaluation of the node, so that it does | |
5713 | -- not get evaluated twice (once for the check, once for the actual | |
5714 | -- reference). Such a double evaluation is always a potential source | |
5715 | -- of inefficiency, and is functionally incorrect in the volatile case. | |
5716 | ||
b40670e1 | 5717 | if not Is_Entity_Name (N) or else Treat_As_Volatile (Entity (N)) then |
9dfe12ae | 5718 | Force_Evaluation (N); |
5719 | end if; | |
5720 | ||
feff2f05 | 5721 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
5722 | -- the same since in this case we can simply do a direct check of the | |
5723 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 5724 | |
5725 | -- [constraint_error when N not in Target_Type] | |
5726 | ||
5727 | -- Note: this is by far the most common case, for example all cases of | |
5728 | -- checks on the RHS of assignments are in this category, but not all | |
5729 | -- cases are like this. Notably conversions can involve two types. | |
5730 | ||
5731 | if Source_Base_Type = Target_Base_Type then | |
5732 | Insert_Action (N, | |
5733 | Make_Raise_Constraint_Error (Loc, | |
5734 | Condition => | |
5735 | Make_Not_In (Loc, | |
5736 | Left_Opnd => Duplicate_Subexpr (N), | |
5737 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
5738 | Reason => Reason)); | |
5739 | ||
5740 | -- Next test for the case where the target type is within the bounds | |
5741 | -- of the base type of the source type, since in this case we can | |
5742 | -- simply convert these bounds to the base type of T to do the test. | |
5743 | ||
5744 | -- [constraint_error when N not in | |
5745 | -- Source_Base_Type (Target_Type'First) | |
5746 | -- .. | |
5747 | -- Source_Base_Type(Target_Type'Last))] | |
5748 | ||
f2a06be9 | 5749 | -- The conversions will always work and need no check |
9dfe12ae | 5750 | |
a9b57347 | 5751 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
5752 | -- of converting from an enumeration value to an integer type, such as | |
5753 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
5754 | -- (which used to be handled by gigi). This is OK, since the conversion | |
5755 | -- itself does not require a check. | |
5756 | ||
7a1dabb3 | 5757 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
9dfe12ae | 5758 | Insert_Action (N, |
5759 | Make_Raise_Constraint_Error (Loc, | |
5760 | Condition => | |
5761 | Make_Not_In (Loc, | |
5762 | Left_Opnd => Duplicate_Subexpr (N), | |
5763 | ||
5764 | Right_Opnd => | |
5765 | Make_Range (Loc, | |
5766 | Low_Bound => | |
a9b57347 | 5767 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 5768 | Make_Attribute_Reference (Loc, |
5769 | Prefix => | |
5770 | New_Occurrence_Of (Target_Type, Loc), | |
5771 | Attribute_Name => Name_First)), | |
5772 | ||
5773 | High_Bound => | |
a9b57347 | 5774 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 5775 | Make_Attribute_Reference (Loc, |
5776 | Prefix => | |
5777 | New_Occurrence_Of (Target_Type, Loc), | |
5778 | Attribute_Name => Name_Last)))), | |
5779 | Reason => Reason)); | |
5780 | ||
feff2f05 | 5781 | -- Note that at this stage we now that the Target_Base_Type is not in |
5782 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
5783 | -- is not in this range). It could still be the case that Source_Type is | |
5784 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 5785 | |
feff2f05 | 5786 | -- If that is the case, we can freely convert the source to the target, |
5787 | -- and then test the target result against the bounds. | |
9dfe12ae | 5788 | |
7a1dabb3 | 5789 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
9dfe12ae | 5790 | |
feff2f05 | 5791 | -- We make a temporary to hold the value of the converted value |
5792 | -- (converted to the base type), and then we will do the test against | |
5793 | -- this temporary. | |
9dfe12ae | 5794 | |
5795 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); | |
5796 | -- [constraint_error when Tnn not in Target_Type] | |
5797 | ||
5798 | -- Then the conversion itself is replaced by an occurrence of Tnn | |
5799 | ||
5800 | declare | |
46eb6933 | 5801 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 5802 | |
5803 | begin | |
5804 | Insert_Actions (N, New_List ( | |
5805 | Make_Object_Declaration (Loc, | |
5806 | Defining_Identifier => Tnn, | |
5807 | Object_Definition => | |
5808 | New_Occurrence_Of (Target_Base_Type, Loc), | |
5809 | Constant_Present => True, | |
5810 | Expression => | |
5811 | Make_Type_Conversion (Loc, | |
5812 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
5813 | Expression => Duplicate_Subexpr (N))), | |
5814 | ||
5815 | Make_Raise_Constraint_Error (Loc, | |
5816 | Condition => | |
5817 | Make_Not_In (Loc, | |
5818 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
5819 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
5820 | ||
5821 | Reason => Reason))); | |
5822 | ||
5823 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
2af58f67 | 5824 | |
5825 | -- Set the type of N, because the declaration for Tnn might not | |
5826 | -- be analyzed yet, as is the case if N appears within a record | |
5827 | -- declaration, as a discriminant constraint or expression. | |
5828 | ||
5829 | Set_Etype (N, Target_Base_Type); | |
9dfe12ae | 5830 | end; |
5831 | ||
5832 | -- At this stage, we know that we have two scalar types, which are | |
5833 | -- directly convertible, and where neither scalar type has a base | |
5834 | -- range that is in the range of the other scalar type. | |
5835 | ||
5836 | -- The only way this can happen is with a signed and unsigned type. | |
5837 | -- So test for these two cases: | |
5838 | ||
5839 | else | |
5840 | -- Case of the source is unsigned and the target is signed | |
5841 | ||
5842 | if Is_Unsigned_Type (Source_Base_Type) | |
5843 | and then not Is_Unsigned_Type (Target_Base_Type) | |
5844 | then | |
5845 | -- If the source is unsigned and the target is signed, then we | |
5846 | -- know that the source is not shorter than the target (otherwise | |
5847 | -- the source base type would be in the target base type range). | |
5848 | ||
feff2f05 | 5849 | -- In other words, the unsigned type is either the same size as |
5850 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 5851 | |
5852 | pragma Assert | |
5853 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
5854 | ||
5855 | -- We only need to check the low bound if the low bound of the | |
5856 | -- target type is non-negative. If the low bound of the target | |
5857 | -- type is negative, then we know that we will fit fine. | |
5858 | ||
5859 | -- If the high bound of the target type is negative, then we | |
5860 | -- know we have a constraint error, since we can't possibly | |
5861 | -- have a negative source. | |
5862 | ||
5863 | -- With these two checks out of the way, we can do the check | |
5864 | -- using the source type safely | |
5865 | ||
5866 | -- This is definitely the most annoying case! | |
5867 | ||
5868 | -- [constraint_error | |
5869 | -- when (Target_Type'First >= 0 | |
5870 | -- and then | |
5871 | -- N < Source_Base_Type (Target_Type'First)) | |
5872 | -- or else Target_Type'Last < 0 | |
5873 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
5874 | ||
5875 | -- We turn off all checks since we know that the conversions | |
5876 | -- will work fine, given the guards for negative values. | |
5877 | ||
5878 | Insert_Action (N, | |
5879 | Make_Raise_Constraint_Error (Loc, | |
5880 | Condition => | |
5881 | Make_Or_Else (Loc, | |
5882 | Make_Or_Else (Loc, | |
5883 | Left_Opnd => | |
5884 | Make_And_Then (Loc, | |
5885 | Left_Opnd => Make_Op_Ge (Loc, | |
5886 | Left_Opnd => | |
5887 | Make_Attribute_Reference (Loc, | |
5888 | Prefix => | |
5889 | New_Occurrence_Of (Target_Type, Loc), | |
5890 | Attribute_Name => Name_First), | |
5891 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
5892 | ||
5893 | Right_Opnd => | |
5894 | Make_Op_Lt (Loc, | |
5895 | Left_Opnd => Duplicate_Subexpr (N), | |
5896 | Right_Opnd => | |
5897 | Convert_To (Source_Base_Type, | |
5898 | Make_Attribute_Reference (Loc, | |
5899 | Prefix => | |
5900 | New_Occurrence_Of (Target_Type, Loc), | |
5901 | Attribute_Name => Name_First)))), | |
5902 | ||
5903 | Right_Opnd => | |
5904 | Make_Op_Lt (Loc, | |
5905 | Left_Opnd => | |
5906 | Make_Attribute_Reference (Loc, | |
5907 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
5908 | Attribute_Name => Name_Last), | |
5909 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
5910 | ||
5911 | Right_Opnd => | |
5912 | Make_Op_Gt (Loc, | |
5913 | Left_Opnd => Duplicate_Subexpr (N), | |
5914 | Right_Opnd => | |
5915 | Convert_To (Source_Base_Type, | |
5916 | Make_Attribute_Reference (Loc, | |
5917 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
5918 | Attribute_Name => Name_Last)))), | |
5919 | ||
5920 | Reason => Reason), | |
5921 | Suppress => All_Checks); | |
5922 | ||
5923 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 5924 | -- the target is unsigned. |
9dfe12ae | 5925 | |
5926 | else | |
5927 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
5928 | and then Is_Unsigned_Type (Target_Base_Type)); | |
5929 | ||
feff2f05 | 5930 | -- If the source is signed and the target is unsigned, then we |
5931 | -- know that the target is not shorter than the source (otherwise | |
5932 | -- the target base type would be in the source base type range). | |
9dfe12ae | 5933 | |
feff2f05 | 5934 | -- In other words, the unsigned type is either the same size as |
5935 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 5936 | |
feff2f05 | 5937 | -- Clearly we have an error if the source value is negative since |
5938 | -- no unsigned type can have negative values. If the source type | |
5939 | -- is non-negative, then the check can be done using the target | |
5940 | -- type. | |
9dfe12ae | 5941 | |
5942 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
5943 | ||
5944 | -- [constraint_error | |
5945 | -- when N < 0 or else Tnn not in Target_Type]; | |
5946 | ||
feff2f05 | 5947 | -- We turn off all checks for the conversion of N to the target |
5948 | -- base type, since we generate the explicit check to ensure that | |
5949 | -- the value is non-negative | |
9dfe12ae | 5950 | |
5951 | declare | |
46eb6933 | 5952 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 5953 | |
5954 | begin | |
5955 | Insert_Actions (N, New_List ( | |
5956 | Make_Object_Declaration (Loc, | |
5957 | Defining_Identifier => Tnn, | |
5958 | Object_Definition => | |
5959 | New_Occurrence_Of (Target_Base_Type, Loc), | |
5960 | Constant_Present => True, | |
5961 | Expression => | |
a9b57347 | 5962 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 5963 | Subtype_Mark => |
5964 | New_Occurrence_Of (Target_Base_Type, Loc), | |
5965 | Expression => Duplicate_Subexpr (N))), | |
5966 | ||
5967 | Make_Raise_Constraint_Error (Loc, | |
5968 | Condition => | |
5969 | Make_Or_Else (Loc, | |
5970 | Left_Opnd => | |
5971 | Make_Op_Lt (Loc, | |
5972 | Left_Opnd => Duplicate_Subexpr (N), | |
5973 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
5974 | ||
5975 | Right_Opnd => | |
5976 | Make_Not_In (Loc, | |
5977 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
5978 | Right_Opnd => | |
5979 | New_Occurrence_Of (Target_Type, Loc))), | |
5980 | ||
5981 | Reason => Reason)), | |
5982 | Suppress => All_Checks); | |
5983 | ||
feff2f05 | 5984 | -- Set the Etype explicitly, because Insert_Actions may have |
5985 | -- placed the declaration in the freeze list for an enclosing | |
5986 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 5987 | |
5988 | Set_Etype (Tnn, Target_Base_Type); | |
5989 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
5990 | end; | |
5991 | end if; | |
5992 | end if; | |
5993 | end Generate_Range_Check; | |
5994 | ||
2af58f67 | 5995 | ------------------ |
5996 | -- Get_Check_Id -- | |
5997 | ------------------ | |
5998 | ||
5999 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
6000 | begin | |
6001 | -- For standard check name, we can do a direct computation | |
6002 | ||
6003 | if N in First_Check_Name .. Last_Check_Name then | |
6004 | return Check_Id (N - (First_Check_Name - 1)); | |
6005 | ||
6006 | -- For non-standard names added by pragma Check_Name, search table | |
6007 | ||
6008 | else | |
6009 | for J in All_Checks + 1 .. Check_Names.Last loop | |
6010 | if Check_Names.Table (J) = N then | |
6011 | return J; | |
6012 | end if; | |
6013 | end loop; | |
6014 | end if; | |
6015 | ||
6016 | -- No matching name found | |
6017 | ||
6018 | return No_Check_Id; | |
6019 | end Get_Check_Id; | |
6020 | ||
ee6ba406 | 6021 | --------------------- |
6022 | -- Get_Discriminal -- | |
6023 | --------------------- | |
6024 | ||
6025 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
6026 | Loc : constant Source_Ptr := Sloc (E); | |
6027 | D : Entity_Id; | |
6028 | Sc : Entity_Id; | |
6029 | ||
6030 | begin | |
0577b0b1 | 6031 | -- The bound can be a bona fide parameter of a protected operation, |
6032 | -- rather than a prival encoded as an in-parameter. | |
6033 | ||
6034 | if No (Discriminal_Link (Entity (Bound))) then | |
6035 | return Bound; | |
6036 | end if; | |
6037 | ||
2af58f67 | 6038 | -- Climb the scope stack looking for an enclosing protected type. If |
6039 | -- we run out of scopes, return the bound itself. | |
6040 | ||
6041 | Sc := Scope (E); | |
6042 | while Present (Sc) loop | |
6043 | if Sc = Standard_Standard then | |
6044 | return Bound; | |
6045 | ||
6046 | elsif Ekind (Sc) = E_Protected_Type then | |
6047 | exit; | |
6048 | end if; | |
6049 | ||
6050 | Sc := Scope (Sc); | |
6051 | end loop; | |
6052 | ||
ee6ba406 | 6053 | D := First_Discriminant (Sc); |
2af58f67 | 6054 | while Present (D) loop |
6055 | if Chars (D) = Chars (Bound) then | |
6056 | return New_Occurrence_Of (Discriminal (D), Loc); | |
6057 | end if; | |
ee6ba406 | 6058 | |
ee6ba406 | 6059 | Next_Discriminant (D); |
6060 | end loop; | |
6061 | ||
2af58f67 | 6062 | return Bound; |
ee6ba406 | 6063 | end Get_Discriminal; |
6064 | ||
2af58f67 | 6065 | ---------------------- |
6066 | -- Get_Range_Checks -- | |
6067 | ---------------------- | |
6068 | ||
6069 | function Get_Range_Checks | |
6070 | (Ck_Node : Node_Id; | |
6071 | Target_Typ : Entity_Id; | |
6072 | Source_Typ : Entity_Id := Empty; | |
6073 | Warn_Node : Node_Id := Empty) return Check_Result | |
6074 | is | |
6075 | begin | |
6076 | return Selected_Range_Checks | |
6077 | (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
6078 | end Get_Range_Checks; | |
6079 | ||
ee6ba406 | 6080 | ------------------ |
6081 | -- Guard_Access -- | |
6082 | ------------------ | |
6083 | ||
6084 | function Guard_Access | |
6085 | (Cond : Node_Id; | |
6086 | Loc : Source_Ptr; | |
314a23b6 | 6087 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 6088 | is |
6089 | begin | |
6090 | if Nkind (Cond) = N_Or_Else then | |
6091 | Set_Paren_Count (Cond, 1); | |
6092 | end if; | |
6093 | ||
6094 | if Nkind (Ck_Node) = N_Allocator then | |
6095 | return Cond; | |
6096 | else | |
6097 | return | |
6098 | Make_And_Then (Loc, | |
6099 | Left_Opnd => | |
6100 | Make_Op_Ne (Loc, | |
9dfe12ae | 6101 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 6102 | Right_Opnd => Make_Null (Loc)), |
6103 | Right_Opnd => Cond); | |
6104 | end if; | |
6105 | end Guard_Access; | |
6106 | ||
6107 | ----------------------------- | |
6108 | -- Index_Checks_Suppressed -- | |
6109 | ----------------------------- | |
6110 | ||
6111 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6112 | begin | |
9dfe12ae | 6113 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6114 | return Is_Check_Suppressed (E, Index_Check); | |
6115 | else | |
fafc6b97 | 6116 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 6117 | end if; |
ee6ba406 | 6118 | end Index_Checks_Suppressed; |
6119 | ||
6120 | ---------------- | |
6121 | -- Initialize -- | |
6122 | ---------------- | |
6123 | ||
6124 | procedure Initialize is | |
6125 | begin | |
6126 | for J in Determine_Range_Cache_N'Range loop | |
6127 | Determine_Range_Cache_N (J) := Empty; | |
6128 | end loop; | |
2af58f67 | 6129 | |
6130 | Check_Names.Init; | |
6131 | ||
6132 | for J in Int range 1 .. All_Checks loop | |
6133 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
6134 | end loop; | |
ee6ba406 | 6135 | end Initialize; |
6136 | ||
6137 | ------------------------- | |
6138 | -- Insert_Range_Checks -- | |
6139 | ------------------------- | |
6140 | ||
6141 | procedure Insert_Range_Checks | |
6142 | (Checks : Check_Result; | |
6143 | Node : Node_Id; | |
6144 | Suppress_Typ : Entity_Id; | |
6145 | Static_Sloc : Source_Ptr := No_Location; | |
6146 | Flag_Node : Node_Id := Empty; | |
6147 | Do_Before : Boolean := False) | |
6148 | is | |
6149 | Internal_Flag_Node : Node_Id := Flag_Node; | |
6150 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
6151 | ||
6152 | Check_Node : Node_Id; | |
6153 | Checks_On : constant Boolean := | |
b6341c67 | 6154 | (not Index_Checks_Suppressed (Suppress_Typ)) |
6155 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 6156 | |
6157 | begin | |
feff2f05 | 6158 | -- For now we just return if Checks_On is false, however this should be |
6159 | -- enhanced to check for an always True value in the condition and to | |
6160 | -- generate a compilation warning??? | |
ee6ba406 | 6161 | |
6dbcfcd9 | 6162 | if not Full_Expander_Active or else not Checks_On then |
ee6ba406 | 6163 | return; |
6164 | end if; | |
6165 | ||
6166 | if Static_Sloc = No_Location then | |
6167 | Internal_Static_Sloc := Sloc (Node); | |
6168 | end if; | |
6169 | ||
6170 | if No (Flag_Node) then | |
6171 | Internal_Flag_Node := Node; | |
6172 | end if; | |
6173 | ||
6174 | for J in 1 .. 2 loop | |
6175 | exit when No (Checks (J)); | |
6176 | ||
6177 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
6178 | and then Present (Condition (Checks (J))) | |
6179 | then | |
6180 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
6181 | Check_Node := Checks (J); | |
6182 | Mark_Rewrite_Insertion (Check_Node); | |
6183 | ||
6184 | if Do_Before then | |
6185 | Insert_Before_And_Analyze (Node, Check_Node); | |
6186 | else | |
6187 | Insert_After_And_Analyze (Node, Check_Node); | |
6188 | end if; | |
6189 | ||
6190 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
6191 | end if; | |
6192 | ||
6193 | else | |
6194 | Check_Node := | |
f15731c4 | 6195 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
6196 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 6197 | Mark_Rewrite_Insertion (Check_Node); |
6198 | ||
6199 | if Do_Before then | |
6200 | Insert_Before_And_Analyze (Node, Check_Node); | |
6201 | else | |
6202 | Insert_After_And_Analyze (Node, Check_Node); | |
6203 | end if; | |
6204 | end if; | |
6205 | end loop; | |
6206 | end Insert_Range_Checks; | |
6207 | ||
6208 | ------------------------ | |
6209 | -- Insert_Valid_Check -- | |
6210 | ------------------------ | |
6211 | ||
6212 | procedure Insert_Valid_Check (Expr : Node_Id) is | |
6213 | Loc : constant Source_Ptr := Sloc (Expr); | |
8b718dab | 6214 | Exp : Node_Id; |
ee6ba406 | 6215 | |
6216 | begin | |
06ad5813 | 6217 | -- Do not insert if checks off, or if not checking validity or |
6218 | -- if expression is known to be valid | |
ee6ba406 | 6219 | |
0577b0b1 | 6220 | if not Validity_Checks_On |
6221 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 6222 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 6223 | then |
8b718dab | 6224 | return; |
6225 | end if; | |
ee6ba406 | 6226 | |
8b718dab | 6227 | -- If we have a checked conversion, then validity check applies to |
6228 | -- the expression inside the conversion, not the result, since if | |
6229 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 6230 | |
8b718dab | 6231 | Exp := Expr; |
6232 | while Nkind (Exp) = N_Type_Conversion loop | |
6233 | Exp := Expression (Exp); | |
6234 | end loop; | |
6235 | ||
0577b0b1 | 6236 | -- We are about to insert the validity check for Exp. We save and |
6237 | -- reset the Do_Range_Check flag over this validity check, and then | |
6238 | -- put it back for the final original reference (Exp may be rewritten). | |
6239 | ||
6240 | declare | |
6241 | DRC : constant Boolean := Do_Range_Check (Exp); | |
23abd64d | 6242 | PV : Node_Id; |
6243 | CE : Node_Id; | |
05fcfafb | 6244 | |
0577b0b1 | 6245 | begin |
6246 | Set_Do_Range_Check (Exp, False); | |
6247 | ||
06ad5813 | 6248 | -- Force evaluation to avoid multiple reads for atomic/volatile |
6249 | ||
6250 | if Is_Entity_Name (Exp) | |
6251 | and then Is_Volatile (Entity (Exp)) | |
6252 | then | |
6253 | Force_Evaluation (Exp, Name_Req => True); | |
6254 | end if; | |
6255 | ||
23abd64d | 6256 | -- Build the prefix for the 'Valid call |
6257 | ||
6258 | PV := Duplicate_Subexpr_No_Checks (Exp, Name_Req => True); | |
0577b0b1 | 6259 | |
23abd64d | 6260 | -- A rather specialized kludge. If PV is an analyzed expression |
6261 | -- which is an indexed component of a packed array that has not | |
6262 | -- been properly expanded, turn off its Analyzed flag to make sure | |
6263 | -- it gets properly reexpanded. | |
6264 | ||
6265 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
6266 | -- an analyze with the old parent pointer. This may point e.g. to | |
6267 | -- a subprogram call, which deactivates this expansion. | |
6268 | ||
6269 | if Analyzed (PV) | |
6270 | and then Nkind (PV) = N_Indexed_Component | |
6271 | and then Present (Packed_Array_Type (Etype (Prefix (PV)))) | |
6272 | then | |
6273 | Set_Analyzed (PV, False); | |
6274 | end if; | |
6275 | ||
6276 | -- Build the raise CE node to check for validity | |
6277 | ||
6278 | CE := | |
0577b0b1 | 6279 | Make_Raise_Constraint_Error (Loc, |
6280 | Condition => | |
6281 | Make_Op_Not (Loc, | |
6282 | Right_Opnd => | |
6283 | Make_Attribute_Reference (Loc, | |
23abd64d | 6284 | Prefix => PV, |
0577b0b1 | 6285 | Attribute_Name => Name_Valid)), |
23abd64d | 6286 | Reason => CE_Invalid_Data); |
6287 | ||
6288 | -- Insert the validity check. Note that we do this with validity | |
6289 | -- checks turned off, to avoid recursion, we do not want validity | |
6290 | -- checks on the validity checking code itself! | |
6291 | ||
6292 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 6293 | |
6fb3c314 | 6294 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 6295 | -- array, then it is rewritten as a renaming declaration. If the |
6296 | -- expression is an actual in a call, it has not been expanded, | |
6297 | -- waiting for the proper point at which to do it. The same happens | |
6298 | -- with renamings, so that we have to force the expansion now. This | |
6299 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
6300 | -- and exp_ch6.adb. | |
6301 | ||
6302 | if Is_Entity_Name (Exp) | |
6303 | and then Nkind (Parent (Entity (Exp))) = | |
6304 | N_Object_Renaming_Declaration | |
6305 | then | |
6306 | declare | |
6307 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
6308 | begin | |
6309 | if Nkind (Old_Exp) = N_Indexed_Component | |
6310 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
6311 | then | |
6312 | Expand_Packed_Element_Reference (Old_Exp); | |
6313 | end if; | |
6314 | end; | |
6315 | end if; | |
6316 | ||
6317 | -- Put back the Do_Range_Check flag on the resulting (possibly | |
6318 | -- rewritten) expression. | |
6319 | ||
6320 | -- Note: it might be thought that a validity check is not required | |
6321 | -- when a range check is present, but that's not the case, because | |
6322 | -- the back end is allowed to assume for the range check that the | |
6323 | -- operand is within its declared range (an assumption that validity | |
6324 | -- checking is all about NOT assuming!) | |
6325 | ||
00c403ee | 6326 | -- Note: no need to worry about Possible_Local_Raise here, it will |
6327 | -- already have been called if original node has Do_Range_Check set. | |
6328 | ||
0577b0b1 | 6329 | Set_Do_Range_Check (Exp, DRC); |
6330 | end; | |
ee6ba406 | 6331 | end Insert_Valid_Check; |
6332 | ||
3cce7f32 | 6333 | ------------------------------------- |
6334 | -- Is_Signed_Integer_Arithmetic_Op -- | |
6335 | ------------------------------------- | |
6336 | ||
6337 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
6338 | begin | |
6339 | case Nkind (N) is | |
6340 | when N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon | | |
6341 | N_Op_Minus | N_Op_Mod | N_Op_Multiply | N_Op_Plus | | |
6342 | N_Op_Rem | N_Op_Subtract => | |
6343 | return Is_Signed_Integer_Type (Etype (N)); | |
6344 | ||
92f1631f | 6345 | when N_If_Expression | N_Case_Expression => |
0326b4d4 | 6346 | return Is_Signed_Integer_Type (Etype (N)); |
6347 | ||
3cce7f32 | 6348 | when others => |
6349 | return False; | |
6350 | end case; | |
6351 | end Is_Signed_Integer_Arithmetic_Op; | |
6352 | ||
fa7497e8 | 6353 | ---------------------------------- |
6354 | -- Install_Null_Excluding_Check -- | |
6355 | ---------------------------------- | |
6356 | ||
6357 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 6358 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 6359 | Typ : constant Entity_Id := Etype (N); |
6360 | ||
7b31b357 | 6361 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
6362 | -- Determines if it is safe to capture Known_Non_Null status for an | |
6363 | -- the entity referenced by node N. The caller ensures that N is indeed | |
6364 | -- an entity name. It is safe to capture the non-null status for an IN | |
6365 | -- parameter when the reference occurs within a declaration that is sure | |
6366 | -- to be executed as part of the declarative region. | |
7870823d | 6367 | |
84d0d4a5 | 6368 | procedure Mark_Non_Null; |
7870823d | 6369 | -- After installation of check, if the node in question is an entity |
6370 | -- name, then mark this entity as non-null if possible. | |
6371 | ||
7b31b357 | 6372 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 6373 | E : constant Entity_Id := Entity (N); |
6374 | S : constant Entity_Id := Current_Scope; | |
6375 | S_Par : Node_Id; | |
6376 | ||
6377 | begin | |
7b31b357 | 6378 | if Ekind (E) /= E_In_Parameter then |
6379 | return False; | |
6380 | end if; | |
7870823d | 6381 | |
6382 | -- Two initial context checks. We must be inside a subprogram body | |
6383 | -- with declarations and reference must not appear in nested scopes. | |
6384 | ||
7b31b357 | 6385 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 6386 | or else Scope (E) /= S |
6387 | then | |
6388 | return False; | |
6389 | end if; | |
6390 | ||
6391 | S_Par := Parent (Parent (S)); | |
6392 | ||
6393 | if Nkind (S_Par) /= N_Subprogram_Body | |
6394 | or else No (Declarations (S_Par)) | |
6395 | then | |
6396 | return False; | |
6397 | end if; | |
6398 | ||
6399 | declare | |
6400 | N_Decl : Node_Id; | |
6401 | P : Node_Id; | |
6402 | ||
6403 | begin | |
6404 | -- Retrieve the declaration node of N (if any). Note that N | |
6405 | -- may be a part of a complex initialization expression. | |
6406 | ||
6407 | P := Parent (N); | |
6408 | N_Decl := Empty; | |
6409 | while Present (P) loop | |
6410 | ||
7b31b357 | 6411 | -- If we have a short circuit form, and we are within the right |
6412 | -- hand expression, we return false, since the right hand side | |
6413 | -- is not guaranteed to be elaborated. | |
6414 | ||
6415 | if Nkind (P) in N_Short_Circuit | |
6416 | and then N = Right_Opnd (P) | |
6417 | then | |
6418 | return False; | |
6419 | end if; | |
6420 | ||
92f1631f | 6421 | -- Similarly, if we are in an if expression and not part of the |
6422 | -- condition, then we return False, since neither the THEN or | |
6423 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 6424 | |
92f1631f | 6425 | if Nkind (P) = N_If_Expression |
7b31b357 | 6426 | and then N /= First (Expressions (P)) |
6427 | then | |
6428 | return False; | |
e977c0cf | 6429 | end if; |
6430 | ||
6fb3c314 | 6431 | -- If we are in a case expression, and not part of the |
e977c0cf | 6432 | -- expression, then we return False, since a particular |
92f1631f | 6433 | -- dependent expression may not always be elaborated |
e977c0cf | 6434 | |
6435 | if Nkind (P) = N_Case_Expression | |
6436 | and then N /= Expression (P) | |
6437 | then | |
6438 | return False; | |
7b31b357 | 6439 | end if; |
6440 | ||
7870823d | 6441 | -- While traversing the parent chain, we find that N |
6442 | -- belongs to a statement, thus it may never appear in | |
6443 | -- a declarative region. | |
6444 | ||
6445 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
6446 | or else Nkind (P) = N_Procedure_Call_Statement | |
6447 | then | |
6448 | return False; | |
6449 | end if; | |
6450 | ||
7b31b357 | 6451 | -- If we are at a declaration, record it and exit |
6452 | ||
7870823d | 6453 | if Nkind (P) in N_Declaration |
6454 | and then Nkind (P) not in N_Subprogram_Specification | |
6455 | then | |
6456 | N_Decl := P; | |
6457 | exit; | |
6458 | end if; | |
6459 | ||
6460 | P := Parent (P); | |
6461 | end loop; | |
6462 | ||
6463 | if No (N_Decl) then | |
6464 | return False; | |
6465 | end if; | |
6466 | ||
6467 | return List_Containing (N_Decl) = Declarations (S_Par); | |
6468 | end; | |
7b31b357 | 6469 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 6470 | |
6471 | ------------------- | |
6472 | -- Mark_Non_Null -- | |
6473 | ------------------- | |
6474 | ||
6475 | procedure Mark_Non_Null is | |
6476 | begin | |
7870823d | 6477 | -- Only case of interest is if node N is an entity name |
6478 | ||
84d0d4a5 | 6479 | if Is_Entity_Name (N) then |
7870823d | 6480 | |
6481 | -- For sure, we want to clear an indication that this is known to | |
6482 | -- be null, since if we get past this check, it definitely is not! | |
6483 | ||
84d0d4a5 | 6484 | Set_Is_Known_Null (Entity (N), False); |
6485 | ||
7870823d | 6486 | -- We can mark the entity as known to be non-null if either it is |
6487 | -- safe to capture the value, or in the case of an IN parameter, | |
6488 | -- which is a constant, if the check we just installed is in the | |
6489 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 6490 | -- a check is decisive for the rest of the body if the expression |
6491 | -- is sure to be elaborated, since we know we have to elaborate | |
6492 | -- all declarations before executing the body. | |
6493 | ||
6494 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 6495 | |
6496 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 6497 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 6498 | then |
6499 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 6500 | end if; |
6501 | end if; | |
6502 | end Mark_Non_Null; | |
6503 | ||
6504 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 6505 | |
6506 | begin | |
84d0d4a5 | 6507 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 6508 | |
84d0d4a5 | 6509 | -- No check inside a generic (why not???) |
fa7497e8 | 6510 | |
84d0d4a5 | 6511 | if Inside_A_Generic then |
fa7497e8 | 6512 | return; |
84d0d4a5 | 6513 | end if; |
6514 | ||
6515 | -- No check needed if known to be non-null | |
6516 | ||
6517 | if Known_Non_Null (N) then | |
05fcfafb | 6518 | return; |
84d0d4a5 | 6519 | end if; |
fa7497e8 | 6520 | |
84d0d4a5 | 6521 | -- If known to be null, here is where we generate a compile time check |
6522 | ||
6523 | if Known_Null (N) then | |
d16989f1 | 6524 | |
6525 | -- Avoid generating warning message inside init procs | |
6526 | ||
6527 | if not Inside_Init_Proc then | |
6528 | Apply_Compile_Time_Constraint_Error | |
6529 | (N, | |
cb97ae5c | 6530 | "null value not allowed here??", |
d16989f1 | 6531 | CE_Access_Check_Failed); |
6532 | else | |
6533 | Insert_Action (N, | |
6534 | Make_Raise_Constraint_Error (Loc, | |
6535 | Reason => CE_Access_Check_Failed)); | |
6536 | end if; | |
6537 | ||
84d0d4a5 | 6538 | Mark_Non_Null; |
6539 | return; | |
6540 | end if; | |
6541 | ||
6542 | -- If entity is never assigned, for sure a warning is appropriate | |
6543 | ||
6544 | if Is_Entity_Name (N) then | |
6545 | Check_Unset_Reference (N); | |
fa7497e8 | 6546 | end if; |
84d0d4a5 | 6547 | |
6548 | -- No check needed if checks are suppressed on the range. Note that we | |
6549 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
6550 | -- so, since the program is erroneous, but we don't like to casually | |
6551 | -- propagate such conclusions from erroneosity). | |
6552 | ||
6553 | if Access_Checks_Suppressed (Typ) then | |
6554 | return; | |
6555 | end if; | |
6556 | ||
2af58f67 | 6557 | -- No check needed for access to concurrent record types generated by |
6558 | -- the expander. This is not just an optimization (though it does indeed | |
6559 | -- remove junk checks). It also avoids generation of junk warnings. | |
6560 | ||
6561 | if Nkind (N) in N_Has_Chars | |
6562 | and then Chars (N) = Name_uObject | |
6563 | and then Is_Concurrent_Record_Type | |
6564 | (Directly_Designated_Type (Etype (N))) | |
6565 | then | |
6566 | return; | |
6567 | end if; | |
6568 | ||
472ea160 | 6569 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
6570 | -- the expander within exception handlers, since we know that the value | |
6571 | -- can never be null. | |
6572 | ||
6573 | -- Is this really the right way to do this? Normally we generate such | |
6574 | -- code in the expander with checks off, and that's how we suppress this | |
6575 | -- kind of junk check ??? | |
6576 | ||
6577 | if Nkind (N) = N_Function_Call | |
6578 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
6579 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
6580 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
6581 | then | |
6582 | return; | |
6583 | end if; | |
6584 | ||
84d0d4a5 | 6585 | -- Otherwise install access check |
6586 | ||
6587 | Insert_Action (N, | |
6588 | Make_Raise_Constraint_Error (Loc, | |
6589 | Condition => | |
6590 | Make_Op_Eq (Loc, | |
6591 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
6592 | Right_Opnd => Make_Null (Loc)), | |
6593 | Reason => CE_Access_Check_Failed)); | |
6594 | ||
6595 | Mark_Non_Null; | |
fa7497e8 | 6596 | end Install_Null_Excluding_Check; |
6597 | ||
ee6ba406 | 6598 | -------------------------- |
6599 | -- Install_Static_Check -- | |
6600 | -------------------------- | |
6601 | ||
6602 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
6603 | Stat : constant Boolean := Is_Static_Expression (R_Cno); | |
6604 | Typ : constant Entity_Id := Etype (R_Cno); | |
6605 | ||
6606 | begin | |
f15731c4 | 6607 | Rewrite (R_Cno, |
6608 | Make_Raise_Constraint_Error (Loc, | |
6609 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 6610 | Set_Analyzed (R_Cno); |
6611 | Set_Etype (R_Cno, Typ); | |
6612 | Set_Raises_Constraint_Error (R_Cno); | |
6613 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 6614 | |
6615 | -- Now deal with possible local raise handling | |
6616 | ||
6617 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 6618 | end Install_Static_Check; |
6619 | ||
3cce7f32 | 6620 | ------------------------- |
6621 | -- Is_Check_Suppressed -- | |
6622 | ------------------------- | |
6623 | ||
6624 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
6625 | Ptr : Suppress_Stack_Entry_Ptr; | |
6626 | ||
6627 | begin | |
6628 | -- First search the local entity suppress stack. We search this from the | |
6629 | -- top of the stack down so that we get the innermost entry that applies | |
6630 | -- to this case if there are nested entries. | |
6631 | ||
6632 | Ptr := Local_Suppress_Stack_Top; | |
6633 | while Ptr /= null loop | |
6634 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6635 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6636 | then | |
6637 | return Ptr.Suppress; | |
6638 | end if; | |
6639 | ||
6640 | Ptr := Ptr.Prev; | |
6641 | end loop; | |
6642 | ||
6643 | -- Now search the global entity suppress table for a matching entry. | |
6644 | -- We also search this from the top down so that if there are multiple | |
6645 | -- pragmas for the same entity, the last one applies (not clear what | |
6646 | -- or whether the RM specifies this handling, but it seems reasonable). | |
6647 | ||
6648 | Ptr := Global_Suppress_Stack_Top; | |
6649 | while Ptr /= null loop | |
6650 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6651 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6652 | then | |
6653 | return Ptr.Suppress; | |
6654 | end if; | |
6655 | ||
6656 | Ptr := Ptr.Prev; | |
6657 | end loop; | |
6658 | ||
6659 | -- If we did not find a matching entry, then use the normal scope | |
6660 | -- suppress value after all (actually this will be the global setting | |
6661 | -- since it clearly was not overridden at any point). For a predefined | |
6662 | -- check, we test the specific flag. For a user defined check, we check | |
6663 | -- the All_Checks flag. The Overflow flag requires special handling to | |
6664 | -- deal with the General vs Assertion case | |
6665 | ||
6666 | if C = Overflow_Check then | |
6667 | return Overflow_Checks_Suppressed (Empty); | |
6668 | elsif C in Predefined_Check_Id then | |
6669 | return Scope_Suppress.Suppress (C); | |
6670 | else | |
6671 | return Scope_Suppress.Suppress (All_Checks); | |
6672 | end if; | |
6673 | end Is_Check_Suppressed; | |
6674 | ||
9dfe12ae | 6675 | --------------------- |
6676 | -- Kill_All_Checks -- | |
6677 | --------------------- | |
6678 | ||
6679 | procedure Kill_All_Checks is | |
6680 | begin | |
6681 | if Debug_Flag_CC then | |
6682 | w ("Kill_All_Checks"); | |
6683 | end if; | |
6684 | ||
feff2f05 | 6685 | -- We reset the number of saved checks to zero, and also modify all |
6686 | -- stack entries for statement ranges to indicate that the number of | |
6687 | -- checks at each level is now zero. | |
9dfe12ae | 6688 | |
6689 | Num_Saved_Checks := 0; | |
6690 | ||
96da3284 | 6691 | -- Note: the Int'Min here avoids any possibility of J being out of |
6692 | -- range when called from e.g. Conditional_Statements_Begin. | |
6693 | ||
6694 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 6695 | Saved_Checks_Stack (J) := 0; |
6696 | end loop; | |
6697 | end Kill_All_Checks; | |
6698 | ||
6699 | ----------------- | |
6700 | -- Kill_Checks -- | |
6701 | ----------------- | |
6702 | ||
6703 | procedure Kill_Checks (V : Entity_Id) is | |
6704 | begin | |
6705 | if Debug_Flag_CC then | |
6706 | w ("Kill_Checks for entity", Int (V)); | |
6707 | end if; | |
6708 | ||
6709 | for J in 1 .. Num_Saved_Checks loop | |
6710 | if Saved_Checks (J).Entity = V then | |
6711 | if Debug_Flag_CC then | |
6712 | w (" Checks killed for saved check ", J); | |
6713 | end if; | |
6714 | ||
6715 | Saved_Checks (J).Killed := True; | |
6716 | end if; | |
6717 | end loop; | |
6718 | end Kill_Checks; | |
6719 | ||
ee6ba406 | 6720 | ------------------------------ |
6721 | -- Length_Checks_Suppressed -- | |
6722 | ------------------------------ | |
6723 | ||
6724 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6725 | begin | |
9dfe12ae | 6726 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6727 | return Is_Check_Suppressed (E, Length_Check); | |
6728 | else | |
fafc6b97 | 6729 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 6730 | end if; |
ee6ba406 | 6731 | end Length_Checks_Suppressed; |
6732 | ||
3cce7f32 | 6733 | ----------------------- |
6734 | -- Make_Bignum_Block -- | |
6735 | ----------------------- | |
6736 | ||
6737 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
6738 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
ee6ba406 | 6739 | |
3cce7f32 | 6740 | begin |
6741 | return | |
6742 | Make_Block_Statement (Loc, | |
6743 | Declarations => New_List ( | |
6744 | Make_Object_Declaration (Loc, | |
6745 | Defining_Identifier => M, | |
6746 | Object_Definition => | |
6747 | New_Occurrence_Of (RTE (RE_Mark_Id), Loc), | |
6748 | Expression => | |
6749 | Make_Function_Call (Loc, | |
6750 | Name => New_Reference_To (RTE (RE_SS_Mark), Loc)))), | |
6751 | ||
6752 | Handled_Statement_Sequence => | |
6753 | Make_Handled_Sequence_Of_Statements (Loc, | |
6754 | Statements => New_List ( | |
6755 | Make_Procedure_Call_Statement (Loc, | |
6756 | Name => New_Occurrence_Of (RTE (RE_SS_Release), Loc), | |
6757 | Parameter_Associations => New_List ( | |
6758 | New_Reference_To (M, Loc)))))); | |
6759 | end Make_Bignum_Block; | |
6760 | ||
0df9d43f | 6761 | ---------------------------------- |
6762 | -- Minimize_Eliminate_Overflows -- | |
6763 | ---------------------------------- | |
3cce7f32 | 6764 | |
f32c377d | 6765 | -- This is a recursive routine that is called at the top of an expression |
6766 | -- tree to properly process overflow checking for a whole subtree by making | |
6767 | -- recursive calls to process operands. This processing may involve the use | |
6768 | -- of bignum or long long integer arithmetic, which will change the types | |
6769 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 6770 | -- it would interfere with semantic analysis). |
f32c377d | 6771 | |
21a55437 | 6772 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 6773 | -- the operator expansion routines, as well as the expansion routines for |
6774 | -- if/case expression, do nothing (for the moment) except call the routine | |
6775 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
6776 | -- routine does nothing for non top-level nodes, so at the point where the | |
6777 | -- call is made for the top level node, the entire expression subtree has | |
6778 | -- not been expanded, or processed for overflow. All that has to happen as | |
6779 | -- a result of the top level call to this routine. | |
f32c377d | 6780 | |
6781 | -- As noted above, the overflow processing works by making recursive calls | |
6782 | -- for the operands, and figuring out what to do, based on the processing | |
6783 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
6784 | -- to be done in bignum mode), and the determined ranges of the operands. | |
6785 | ||
6786 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 6787 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 6788 | -- the node (if it has been modified by the overflow check processing). The |
6789 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
6790 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 6791 | -- for this call is that the overflow handling mode must be temporarily set |
6792 | -- to STRICT. | |
f32c377d | 6793 | |
0df9d43f | 6794 | procedure Minimize_Eliminate_Overflows |
61016a7a | 6795 | (N : Node_Id; |
6796 | Lo : out Uint; | |
6797 | Hi : out Uint; | |
6798 | Top_Level : Boolean) | |
3cce7f32 | 6799 | is |
0326b4d4 | 6800 | Rtyp : constant Entity_Id := Etype (N); |
6801 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
6802 | -- Result type, must be a signed integer type | |
3cce7f32 | 6803 | |
db415383 | 6804 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 6805 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
6806 | ||
6807 | Loc : constant Source_Ptr := Sloc (N); | |
6808 | ||
6809 | Rlo, Rhi : Uint; | |
0326b4d4 | 6810 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 6811 | |
6812 | Llo, Lhi : Uint; | |
0326b4d4 | 6813 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 6814 | |
49b3a812 | 6815 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
6816 | -- Operands and results are of this type when we convert | |
6817 | ||
0326b4d4 | 6818 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
6819 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 6820 | -- Bounds of Long_Long_Integer |
6821 | ||
6822 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
6823 | -- Indicates binary operator case | |
6824 | ||
6825 | OK : Boolean; | |
6826 | -- Used in call to Determine_Range | |
6827 | ||
61016a7a | 6828 | Bignum_Operands : Boolean; |
6829 | -- Set True if one or more operands is already of type Bignum, meaning | |
6830 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 6831 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 6832 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 6833 | |
6834 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 6835 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 6836 | -- which means that if the result is known to be in the result type |
6837 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 6838 | |
6839 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
6840 | -- This is called when we have modified the node and we therefore need | |
6841 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
6842 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
6843 | -- we would reenter this routine recursively which would not be good! | |
6844 | -- The argument Suppress is set True if we also want to suppress | |
6845 | -- overflow checking for the reexpansion (this is set when we know | |
6846 | -- overflow is not possible). Typ is the type for the reanalysis. | |
6847 | ||
6848 | procedure Reexpand (Suppress : Boolean := False); | |
6849 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
6850 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
6851 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
6852 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
6853 | -- Note that skipping reanalysis is not just an optimization, testing | |
6854 | -- has showed up several complex cases in which reanalyzing an already | |
6855 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 6856 | |
0326b4d4 | 6857 | function In_Result_Range return Boolean; |
6858 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 6859 | |
2fe22c69 | 6860 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 6861 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 6862 | |
6863 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 6864 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 6865 | |
0326b4d4 | 6866 | --------------------- |
6867 | -- In_Result_Range -- | |
6868 | --------------------- | |
6869 | ||
6870 | function In_Result_Range return Boolean is | |
6871 | begin | |
f32c377d | 6872 | if Lo = No_Uint or else Hi = No_Uint then |
6873 | return False; | |
6874 | ||
6875 | elsif Is_Static_Subtype (Etype (N)) then | |
0326b4d4 | 6876 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
6877 | and then | |
6878 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 6879 | |
0326b4d4 | 6880 | else |
6881 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
6882 | and then | |
6883 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
6884 | end if; | |
6885 | end In_Result_Range; | |
6886 | ||
2fe22c69 | 6887 | --------- |
6888 | -- Max -- | |
6889 | --------- | |
6890 | ||
6891 | procedure Max (A : in out Uint; B : Uint) is | |
6892 | begin | |
6893 | if A = No_Uint or else B > A then | |
6894 | A := B; | |
6895 | end if; | |
6896 | end Max; | |
6897 | ||
6898 | --------- | |
6899 | -- Min -- | |
6900 | --------- | |
6901 | ||
6902 | procedure Min (A : in out Uint; B : Uint) is | |
6903 | begin | |
6904 | if A = No_Uint or else B < A then | |
6905 | A := B; | |
6906 | end if; | |
6907 | end Min; | |
6908 | ||
0df9d43f | 6909 | --------------- |
6910 | -- Reanalyze -- | |
6911 | --------------- | |
6912 | ||
6913 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 6914 | Svg : constant Overflow_Mode_Type := |
6915 | Scope_Suppress.Overflow_Mode_General; | |
6916 | Sva : constant Overflow_Mode_Type := | |
6917 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 6918 | Svo : constant Boolean := |
6919 | Scope_Suppress.Suppress (Overflow_Check); | |
6920 | ||
6921 | begin | |
db415383 | 6922 | Scope_Suppress.Overflow_Mode_General := Strict; |
6923 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 6924 | |
6925 | if Suppress then | |
6926 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
6927 | end if; | |
6928 | ||
6929 | Analyze_And_Resolve (N, Typ); | |
6930 | ||
6931 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 6932 | Scope_Suppress.Overflow_Mode_General := Svg; |
6933 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 6934 | end Reanalyze; |
6935 | ||
4fb5f0a0 | 6936 | -------------- |
6937 | -- Reexpand -- | |
6938 | -------------- | |
6939 | ||
0df9d43f | 6940 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 6941 | Svg : constant Overflow_Mode_Type := |
6942 | Scope_Suppress.Overflow_Mode_General; | |
6943 | Sva : constant Overflow_Mode_Type := | |
6944 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 6945 | Svo : constant Boolean := |
6946 | Scope_Suppress.Suppress (Overflow_Check); | |
6947 | ||
4fb5f0a0 | 6948 | begin |
db415383 | 6949 | Scope_Suppress.Overflow_Mode_General := Strict; |
6950 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 6951 | Set_Analyzed (N, False); |
0df9d43f | 6952 | |
6953 | if Suppress then | |
6954 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
6955 | end if; | |
6956 | ||
4fb5f0a0 | 6957 | Expand (N); |
0df9d43f | 6958 | |
6959 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 6960 | Scope_Suppress.Overflow_Mode_General := Svg; |
6961 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 6962 | end Reexpand; |
6963 | ||
0df9d43f | 6964 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 6965 | |
3cce7f32 | 6966 | begin |
0326b4d4 | 6967 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 6968 | |
6969 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
6970 | ||
6971 | -- Use the normal Determine_Range routine to get the range. We | |
6972 | -- don't require operands to be valid, invalid values may result in | |
6973 | -- rubbish results where the result has not been properly checked for | |
6974 | -- overflow, that's fine! | |
6975 | ||
6976 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
6977 | ||
21a55437 | 6978 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 6979 | -- clear but might as well protect), use type bounds. |
6980 | ||
6981 | if not OK then | |
6982 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
6983 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
6984 | end if; | |
6985 | ||
6986 | -- If we don't have a binary operator, all we have to do is to set | |
6987 | -- the Hi/Lo range, so we are done | |
6988 | ||
6989 | return; | |
6990 | ||
0326b4d4 | 6991 | -- Processing for if expression |
6992 | ||
92f1631f | 6993 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 6994 | declare |
6995 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
6996 | Else_DE : constant Node_Id := Next (Then_DE); | |
6997 | ||
6998 | begin | |
6999 | Bignum_Operands := False; | |
7000 | ||
0df9d43f | 7001 | Minimize_Eliminate_Overflows |
0326b4d4 | 7002 | (Then_DE, Lo, Hi, Top_Level => False); |
7003 | ||
7004 | if Lo = No_Uint then | |
7005 | Bignum_Operands := True; | |
7006 | end if; | |
7007 | ||
0df9d43f | 7008 | Minimize_Eliminate_Overflows |
0326b4d4 | 7009 | (Else_DE, Rlo, Rhi, Top_Level => False); |
7010 | ||
7011 | if Rlo = No_Uint then | |
7012 | Bignum_Operands := True; | |
7013 | else | |
7014 | Long_Long_Integer_Operands := | |
7015 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
7016 | ||
7017 | Min (Lo, Rlo); | |
7018 | Max (Hi, Rhi); | |
7019 | end if; | |
7020 | ||
21a55437 | 7021 | -- If at least one of our operands is now Bignum, we must rebuild |
7022 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 7023 | -- rebuilt if expression with overflow checks off, since once we |
7024 | -- are in bignum mode, we are all done with overflow checks! | |
7025 | ||
7026 | if Bignum_Operands then | |
7027 | Rewrite (N, | |
92f1631f | 7028 | Make_If_Expression (Loc, |
0326b4d4 | 7029 | Expressions => New_List ( |
7030 | Remove_Head (Expressions (N)), | |
7031 | Convert_To_Bignum (Then_DE), | |
7032 | Convert_To_Bignum (Else_DE)), | |
7033 | Is_Elsif => Is_Elsif (N))); | |
7034 | ||
0df9d43f | 7035 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 7036 | |
7037 | -- If we have no Long_Long_Integer operands, then we are in result | |
7038 | -- range, since it means that none of our operands felt the need | |
7039 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 7040 | -- converted to long long integer or bignum). We reexpand to |
7041 | -- complete the expansion of the if expression (but we do not | |
7042 | -- need to reanalyze). | |
0326b4d4 | 7043 | |
7044 | elsif not Long_Long_Integer_Operands then | |
7045 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7046 | Reexpand; |
0326b4d4 | 7047 | |
7048 | -- Otherwise convert us to long long integer mode. Note that we | |
7049 | -- don't need any further overflow checking at this level. | |
7050 | ||
7051 | else | |
7052 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
7053 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
7054 | Set_Etype (N, LLIB); | |
f32c377d | 7055 | |
7056 | -- Now reanalyze with overflow checks off | |
7057 | ||
0326b4d4 | 7058 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7059 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 7060 | end if; |
7061 | end; | |
7062 | ||
7063 | return; | |
7064 | ||
7065 | -- Here for case expression | |
7066 | ||
7067 | elsif Nkind (N) = N_Case_Expression then | |
7068 | Bignum_Operands := False; | |
7069 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 7070 | |
7071 | declare | |
f32c377d | 7072 | Alt : Node_Id; |
0326b4d4 | 7073 | |
7074 | begin | |
7075 | -- Loop through expressions applying recursive call | |
7076 | ||
7077 | Alt := First (Alternatives (N)); | |
7078 | while Present (Alt) loop | |
7079 | declare | |
7080 | Aexp : constant Node_Id := Expression (Alt); | |
7081 | ||
7082 | begin | |
0df9d43f | 7083 | Minimize_Eliminate_Overflows |
0326b4d4 | 7084 | (Aexp, Lo, Hi, Top_Level => False); |
7085 | ||
7086 | if Lo = No_Uint then | |
7087 | Bignum_Operands := True; | |
7088 | elsif Etype (Aexp) = LLIB then | |
7089 | Long_Long_Integer_Operands := True; | |
7090 | end if; | |
7091 | end; | |
7092 | ||
7093 | Next (Alt); | |
7094 | end loop; | |
7095 | ||
7096 | -- If we have no bignum or long long integer operands, it means | |
7097 | -- that none of our dependent expressions could raise overflow. | |
7098 | -- In this case, we simply return with no changes except for | |
7099 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 7100 | -- checks for this node. We will reexpand to get the needed |
7101 | -- expansion for the case expression, but we do not need to | |
21a55437 | 7102 | -- reanalyze, since nothing has changed. |
0326b4d4 | 7103 | |
f32c377d | 7104 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 7105 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7106 | Reexpand (Suppress => True); |
0326b4d4 | 7107 | |
7108 | -- Otherwise we are going to rebuild the case expression using | |
7109 | -- either bignum or long long integer operands throughout. | |
7110 | ||
7111 | else | |
f32c377d | 7112 | declare |
7113 | Rtype : Entity_Id; | |
7114 | New_Alts : List_Id; | |
7115 | New_Exp : Node_Id; | |
7116 | ||
7117 | begin | |
7118 | New_Alts := New_List; | |
7119 | Alt := First (Alternatives (N)); | |
7120 | while Present (Alt) loop | |
7121 | if Bignum_Operands then | |
7122 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
7123 | Rtype := RTE (RE_Bignum); | |
7124 | else | |
7125 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
7126 | Rtype := LLIB; | |
7127 | end if; | |
0326b4d4 | 7128 | |
f32c377d | 7129 | Append_To (New_Alts, |
7130 | Make_Case_Expression_Alternative (Sloc (Alt), | |
7131 | Actions => No_List, | |
7132 | Discrete_Choices => Discrete_Choices (Alt), | |
7133 | Expression => New_Exp)); | |
0326b4d4 | 7134 | |
f32c377d | 7135 | Next (Alt); |
7136 | end loop; | |
0326b4d4 | 7137 | |
f32c377d | 7138 | Rewrite (N, |
7139 | Make_Case_Expression (Loc, | |
7140 | Expression => Expression (N), | |
7141 | Alternatives => New_Alts)); | |
0326b4d4 | 7142 | |
0df9d43f | 7143 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 7144 | end; |
0326b4d4 | 7145 | end if; |
7146 | end; | |
7147 | ||
7148 | return; | |
7149 | end if; | |
7150 | ||
7151 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 7152 | -- operands to get the ranges (and to properly process the subtree |
7153 | -- that lies below us!) | |
7154 | ||
0df9d43f | 7155 | Minimize_Eliminate_Overflows |
0326b4d4 | 7156 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 7157 | |
0326b4d4 | 7158 | if Binary then |
0df9d43f | 7159 | Minimize_Eliminate_Overflows |
0326b4d4 | 7160 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 7161 | end if; |
7162 | ||
f32c377d | 7163 | -- Record if we have Long_Long_Integer operands |
7164 | ||
7165 | Long_Long_Integer_Operands := | |
7166 | Etype (Right_Opnd (N)) = LLIB | |
7167 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
7168 | ||
7169 | -- If either operand is a bignum, then result will be a bignum and we | |
7170 | -- don't need to do any range analysis. As previously discussed we could | |
7171 | -- do range analysis in such cases, but it could mean working with giant | |
7172 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 7173 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 7174 | |
7175 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
7176 | Lo := No_Uint; | |
7177 | Hi := No_Uint; | |
61016a7a | 7178 | Bignum_Operands := True; |
3cce7f32 | 7179 | |
7180 | -- Otherwise compute result range | |
7181 | ||
7182 | else | |
61016a7a | 7183 | Bignum_Operands := False; |
7184 | ||
3cce7f32 | 7185 | case Nkind (N) is |
7186 | ||
7187 | -- Absolute value | |
7188 | ||
7189 | when N_Op_Abs => | |
7190 | Lo := Uint_0; | |
de922300 | 7191 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 7192 | |
7193 | -- Addition | |
7194 | ||
7195 | when N_Op_Add => | |
7196 | Lo := Llo + Rlo; | |
7197 | Hi := Lhi + Rhi; | |
7198 | ||
7199 | -- Division | |
7200 | ||
7201 | when N_Op_Divide => | |
2fe22c69 | 7202 | |
5f4275e1 | 7203 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 7204 | |
5f4275e1 | 7205 | if Rlo = 0 and then Rhi = 0 then |
7206 | Lo := Uint_0; | |
7207 | Hi := Uint_0; | |
2fe22c69 | 7208 | |
5f4275e1 | 7209 | -- Possible bounds of division must come from dividing end |
7210 | -- values of the input ranges (four possibilities), provided | |
7211 | -- zero is not included in the possible values of the right | |
7212 | -- operand. | |
7213 | ||
7214 | -- Otherwise, we just consider two intervals of values for | |
7215 | -- the right operand: the interval of negative values (up to | |
7216 | -- -1) and the interval of positive values (starting at 1). | |
7217 | -- Since division by 1 is the identity, and division by -1 | |
7218 | -- is negation, we get all possible bounds of division in that | |
7219 | -- case by considering: | |
7220 | -- - all values from the division of end values of input | |
7221 | -- ranges; | |
7222 | -- - the end values of the left operand; | |
7223 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 7224 | |
5f4275e1 | 7225 | else |
7226 | declare | |
7227 | Mrk : constant Uintp.Save_Mark := Mark; | |
7228 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 7229 | |
5f4275e1 | 7230 | Ev1 : Uint; |
7231 | Ev2 : Uint; | |
7232 | Ev3 : Uint; | |
7233 | Ev4 : Uint; | |
2fe22c69 | 7234 | |
5f4275e1 | 7235 | begin |
7236 | -- Discard extreme values of zero for the divisor, since | |
7237 | -- they will simply result in an exception in any case. | |
2fe22c69 | 7238 | |
5f4275e1 | 7239 | if Rlo = 0 then |
7240 | Rlo := Uint_1; | |
7241 | elsif Rhi = 0 then | |
7242 | Rhi := -Uint_1; | |
2fe22c69 | 7243 | end if; |
2fe22c69 | 7244 | |
5f4275e1 | 7245 | -- Compute possible bounds coming from dividing end |
7246 | -- values of the input ranges. | |
2fe22c69 | 7247 | |
5f4275e1 | 7248 | Ev1 := Llo / Rlo; |
7249 | Ev2 := Llo / Rhi; | |
7250 | Ev3 := Lhi / Rlo; | |
7251 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 7252 | |
5f4275e1 | 7253 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
7254 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 7255 | |
5f4275e1 | 7256 | -- If the right operand can be both negative or positive, |
7257 | -- include the end values of the left operand in the | |
7258 | -- extreme values, as well as their negation. | |
2fe22c69 | 7259 | |
5f4275e1 | 7260 | if Rlo < 0 and then Rhi > 0 then |
7261 | Ev1 := Llo; | |
7262 | Ev2 := -Llo; | |
7263 | Ev3 := Lhi; | |
7264 | Ev4 := -Lhi; | |
2fe22c69 | 7265 | |
5f4275e1 | 7266 | Min (Lo, |
7267 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
7268 | Max (Hi, | |
7269 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 7270 | end if; |
2fe22c69 | 7271 | |
5f4275e1 | 7272 | -- Release the RR and Ev values |
2fe22c69 | 7273 | |
5f4275e1 | 7274 | Release_And_Save (Mrk, Lo, Hi); |
7275 | end; | |
7276 | end if; | |
3cce7f32 | 7277 | |
7278 | -- Exponentiation | |
7279 | ||
7280 | when N_Op_Expon => | |
de922300 | 7281 | |
7282 | -- Discard negative values for the exponent, since they will | |
7283 | -- simply result in an exception in any case. | |
7284 | ||
7285 | if Rhi < 0 then | |
7286 | Rhi := Uint_0; | |
7287 | elsif Rlo < 0 then | |
7288 | Rlo := Uint_0; | |
7289 | end if; | |
7290 | ||
7291 | -- Estimate number of bits in result before we go computing | |
7292 | -- giant useless bounds. Basically the number of bits in the | |
7293 | -- result is the number of bits in the base multiplied by the | |
7294 | -- value of the exponent. If this is big enough that the result | |
7295 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
7296 | -- mode immediately, and avoid computing giant bounds. | |
7297 | ||
7298 | -- The comparison here is approximate, but conservative, it | |
7299 | -- only clicks on cases that are sure to exceed the bounds. | |
7300 | ||
7301 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
7302 | Lo := No_Uint; | |
7303 | Hi := No_Uint; | |
7304 | ||
7305 | -- If right operand is zero then result is 1 | |
7306 | ||
7307 | elsif Rhi = 0 then | |
7308 | Lo := Uint_1; | |
7309 | Hi := Uint_1; | |
7310 | ||
7311 | else | |
7312 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 7313 | -- positive value to largest exponent value, or from |
7314 | -- the exponentiation of most negative value to an | |
7315 | -- even exponent. | |
de922300 | 7316 | |
7317 | declare | |
7318 | Hi1, Hi2 : Uint; | |
7319 | ||
7320 | begin | |
5f4275e1 | 7321 | if Lhi > 0 then |
de922300 | 7322 | Hi1 := Lhi ** Rhi; |
7323 | else | |
7324 | Hi1 := Uint_0; | |
7325 | end if; | |
7326 | ||
7327 | if Llo < 0 then | |
7328 | if Rhi mod 2 = 0 then | |
de922300 | 7329 | Hi2 := Llo ** Rhi; |
5f4275e1 | 7330 | else |
7331 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 7332 | end if; |
7333 | else | |
7334 | Hi2 := Uint_0; | |
7335 | end if; | |
7336 | ||
7337 | Hi := UI_Max (Hi1, Hi2); | |
7338 | end; | |
7339 | ||
7340 | -- Result can only be negative if base can be negative | |
7341 | ||
7342 | if Llo < 0 then | |
21a55437 | 7343 | if Rhi mod 2 = 0 then |
de922300 | 7344 | Lo := Llo ** (Rhi - 1); |
7345 | else | |
7346 | Lo := Llo ** Rhi; | |
7347 | end if; | |
7348 | ||
21a55437 | 7349 | -- Otherwise low bound is minimum ** minimum |
de922300 | 7350 | |
7351 | else | |
7352 | Lo := Llo ** Rlo; | |
7353 | end if; | |
7354 | end if; | |
3cce7f32 | 7355 | |
7356 | -- Negation | |
7357 | ||
7358 | when N_Op_Minus => | |
7359 | Lo := -Rhi; | |
7360 | Hi := -Rlo; | |
7361 | ||
7362 | -- Mod | |
7363 | ||
7364 | when N_Op_Mod => | |
2fe22c69 | 7365 | declare |
5f4275e1 | 7366 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7367 | -- This is the maximum absolute value of the result |
7368 | ||
7369 | begin | |
7370 | Lo := Uint_0; | |
7371 | Hi := Uint_0; | |
7372 | ||
7373 | -- The result depends only on the sign and magnitude of | |
7374 | -- the right operand, it does not depend on the sign or | |
7375 | -- magnitude of the left operand. | |
7376 | ||
7377 | if Rlo < 0 then | |
7378 | Lo := -Maxabs; | |
7379 | end if; | |
7380 | ||
7381 | if Rhi > 0 then | |
7382 | Hi := Maxabs; | |
7383 | end if; | |
7384 | end; | |
3cce7f32 | 7385 | |
7386 | -- Multiplication | |
7387 | ||
7388 | when N_Op_Multiply => | |
49b3a812 | 7389 | |
7390 | -- Possible bounds of multiplication must come from multiplying | |
7391 | -- end values of the input ranges (four possibilities). | |
7392 | ||
7393 | declare | |
7394 | Mrk : constant Uintp.Save_Mark := Mark; | |
7395 | -- Mark so we can release the Ev values | |
7396 | ||
7397 | Ev1 : constant Uint := Llo * Rlo; | |
7398 | Ev2 : constant Uint := Llo * Rhi; | |
7399 | Ev3 : constant Uint := Lhi * Rlo; | |
7400 | Ev4 : constant Uint := Lhi * Rhi; | |
7401 | ||
7402 | begin | |
7403 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
7404 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
7405 | ||
7406 | -- Release the Ev values | |
7407 | ||
7408 | Release_And_Save (Mrk, Lo, Hi); | |
7409 | end; | |
3cce7f32 | 7410 | |
7411 | -- Plus operator (affirmation) | |
7412 | ||
7413 | when N_Op_Plus => | |
7414 | Lo := Rlo; | |
7415 | Hi := Rhi; | |
7416 | ||
7417 | -- Remainder | |
7418 | ||
7419 | when N_Op_Rem => | |
2fe22c69 | 7420 | declare |
5f4275e1 | 7421 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7422 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 7423 | -- that the result range does not depend on the sign of the |
7424 | -- right operand. | |
2fe22c69 | 7425 | |
7426 | begin | |
7427 | Lo := Uint_0; | |
7428 | Hi := Uint_0; | |
7429 | ||
7430 | -- Case of left operand negative, which results in a range | |
7431 | -- of -Maxabs .. 0 for those negative values. If there are | |
7432 | -- no negative values then Lo value of result is always 0. | |
7433 | ||
7434 | if Llo < 0 then | |
7435 | Lo := -Maxabs; | |
7436 | end if; | |
7437 | ||
7438 | -- Case of left operand positive | |
7439 | ||
7440 | if Lhi > 0 then | |
7441 | Hi := Maxabs; | |
7442 | end if; | |
7443 | end; | |
3cce7f32 | 7444 | |
7445 | -- Subtract | |
7446 | ||
7447 | when N_Op_Subtract => | |
7448 | Lo := Llo - Rhi; | |
7449 | Hi := Lhi - Rlo; | |
7450 | ||
7451 | -- Nothing else should be possible | |
7452 | ||
7453 | when others => | |
7454 | raise Program_Error; | |
3cce7f32 | 7455 | end case; |
7456 | end if; | |
7457 | ||
4fb5f0a0 | 7458 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 7459 | -- operands or long long integer operands), and we know the result. |
7460 | -- If we know we are in the result range, and we do not have Bignum | |
7461 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
7462 | -- overflow checks turned off (since we know we cannot have overflow). | |
7463 | -- As always the reexpansion is required to complete expansion of the | |
7464 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
7465 | -- by suppressing the check. | |
f32c377d | 7466 | |
7467 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
7468 | and then In_Result_Range | |
7469 | then | |
7470 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7471 | Reexpand (Suppress => True); |
f32c377d | 7472 | return; |
7473 | ||
7474 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 7475 | -- case we will move into either the Bignum or Long_Long_Integer domain |
7476 | -- to compute the result. However, there is one exception. If we are | |
7477 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
7478 | -- operands, we will have to immediately convert the result back to | |
7479 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
7480 | -- fiddling. | |
f32c377d | 7481 | |
7482 | elsif Top_Level | |
7483 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 7484 | |
7485 | -- One further refinement. If we are at the top level, but our parent | |
7486 | -- is a type conversion, then go into bignum or long long integer node | |
7487 | -- since the result will be converted to that type directly without | |
7488 | -- going through the result type, and we may avoid an overflow. This | |
7489 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
7490 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
7491 | -- but does not fit in Integer. | |
7492 | ||
7493 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 7494 | then |
0df9d43f | 7495 | -- Here keep original types, but we need to complete analysis |
f32c377d | 7496 | |
7497 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 7498 | -- here because it will cause recursion into the whole MINIMIZED/ |
7499 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 7500 | -- we are at the top level, and we need a check against the result |
0df9d43f | 7501 | -- mode (i.e. we want to use STRICT mode). So do exactly that! |
4fb5f0a0 | 7502 | -- Also, we have not modified the node, so this is a case where |
7503 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 7504 | |
0df9d43f | 7505 | Reexpand; |
f32c377d | 7506 | return; |
7507 | ||
7508 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 7509 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 7510 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
7511 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 7512 | |
7513 | -- Note: we could do better here and in some cases switch back from | |
7514 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
7515 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
7516 | -- Failing to do this switching back is only an efficiency issue. | |
7517 | ||
f32c377d | 7518 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 7519 | |
61016a7a | 7520 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 7521 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 7522 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
7523 | ||
7524 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
7525 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
7526 | -- into Bignum mode, but there is an exception if neither of our | |
7527 | -- operands is Bignum now, and we are at the top level (Top_Level | |
7528 | -- set True). In this case, there is no point in moving into Bignum | |
7529 | -- mode to prevent overflow if the caller will immediately convert | |
7530 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 7531 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 7532 | |
7533 | if Check_Mode = Minimized | |
7534 | or else (Top_Level and not Bignum_Operands) | |
7535 | then | |
0df9d43f | 7536 | if Do_Overflow_Check (N) then |
7537 | Enable_Overflow_Check (N); | |
7538 | end if; | |
3cce7f32 | 7539 | |
0df9d43f | 7540 | -- The result now has to be in Long_Long_Integer mode, so adjust |
7541 | -- the possible range to reflect this. Note these calls also | |
7542 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 7543 | |
7544 | Max (Lo, LLLo); | |
7545 | Min (Hi, LLHi); | |
7546 | ||
7547 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 7548 | |
7549 | else | |
7550 | pragma Assert (Check_Mode = Eliminated); | |
7551 | ||
7552 | declare | |
7553 | Fent : Entity_Id; | |
7554 | Args : List_Id; | |
7555 | ||
7556 | begin | |
7557 | case Nkind (N) is | |
7558 | when N_Op_Abs => | |
7559 | Fent := RTE (RE_Big_Abs); | |
7560 | ||
7561 | when N_Op_Add => | |
7562 | Fent := RTE (RE_Big_Add); | |
7563 | ||
7564 | when N_Op_Divide => | |
7565 | Fent := RTE (RE_Big_Div); | |
7566 | ||
7567 | when N_Op_Expon => | |
7568 | Fent := RTE (RE_Big_Exp); | |
7569 | ||
7570 | when N_Op_Minus => | |
7571 | Fent := RTE (RE_Big_Neg); | |
7572 | ||
7573 | when N_Op_Mod => | |
7574 | Fent := RTE (RE_Big_Mod); | |
7575 | ||
7576 | when N_Op_Multiply => | |
7577 | Fent := RTE (RE_Big_Mul); | |
7578 | ||
7579 | when N_Op_Rem => | |
7580 | Fent := RTE (RE_Big_Rem); | |
7581 | ||
7582 | when N_Op_Subtract => | |
7583 | Fent := RTE (RE_Big_Sub); | |
7584 | ||
7585 | -- Anything else is an internal error, this includes the | |
7586 | -- N_Op_Plus case, since how can plus cause the result | |
7587 | -- to be out of range if the operand is in range? | |
7588 | ||
7589 | when others => | |
7590 | raise Program_Error; | |
7591 | end case; | |
7592 | ||
7593 | -- Construct argument list for Bignum call, converting our | |
7594 | -- operands to Bignum form if they are not already there. | |
7595 | ||
7596 | Args := New_List; | |
7597 | ||
7598 | if Binary then | |
7599 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
7600 | end if; | |
7601 | ||
7602 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
7603 | ||
7604 | -- Now rewrite the arithmetic operator with a call to the | |
7605 | -- corresponding bignum function. | |
7606 | ||
7607 | Rewrite (N, | |
7608 | Make_Function_Call (Loc, | |
7609 | Name => New_Occurrence_Of (Fent, Loc), | |
7610 | Parameter_Associations => Args)); | |
0df9d43f | 7611 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 7612 | |
7613 | -- Indicate result is Bignum mode | |
7614 | ||
7615 | Lo := No_Uint; | |
7616 | Hi := No_Uint; | |
de922300 | 7617 | return; |
3cce7f32 | 7618 | end; |
7619 | end if; | |
7620 | ||
7621 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 7622 | -- check is required, at least not yet. |
3cce7f32 | 7623 | |
7624 | else | |
de922300 | 7625 | Set_Do_Overflow_Check (N, False); |
7626 | end if; | |
3cce7f32 | 7627 | |
f32c377d | 7628 | -- Here we are not in Bignum territory, but we may have long long |
7629 | -- integer operands that need special handling. First a special check: | |
7630 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
7631 | -- it means we converted it to prevent overflow, but exponentiation | |
7632 | -- requires a Natural right operand, so convert it back to Natural. | |
7633 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 7634 | |
f32c377d | 7635 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
7636 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 7637 | end if; |
7638 | ||
de922300 | 7639 | -- Here we will do the operation in Long_Long_Integer. We do this even |
7640 | -- if we know an overflow check is required, better to do this in long | |
7641 | -- long integer mode, since we are less likely to overflow! | |
3cce7f32 | 7642 | |
de922300 | 7643 | -- Convert right or only operand to Long_Long_Integer, except that |
7644 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 7645 | |
de922300 | 7646 | if Nkind (N) /= N_Op_Expon then |
7647 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
7648 | end if; | |
3cce7f32 | 7649 | |
de922300 | 7650 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 7651 | |
de922300 | 7652 | if Binary then |
7653 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
7654 | end if; | |
7655 | ||
7656 | -- Reset node to unanalyzed | |
7657 | ||
7658 | Set_Analyzed (N, False); | |
7659 | Set_Etype (N, Empty); | |
7660 | Set_Entity (N, Empty); | |
7661 | ||
2fe22c69 | 7662 | -- Now analyze this new node. This reanalysis will complete processing |
7663 | -- for the node. In particular we will complete the expansion of an | |
7664 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
7665 | -- we will complete any division checks (since we have not changed the | |
7666 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 7667 | |
0df9d43f | 7668 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
7669 | -- MINIMIZED/ELIMINATED handling, since we are now done with that! | |
3cce7f32 | 7670 | |
0df9d43f | 7671 | declare |
db415383 | 7672 | SG : constant Overflow_Mode_Type := |
7673 | Scope_Suppress.Overflow_Mode_General; | |
7674 | SA : constant Overflow_Mode_Type := | |
7675 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 7676 | |
0df9d43f | 7677 | begin |
db415383 | 7678 | Scope_Suppress.Overflow_Mode_General := Strict; |
7679 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 7680 | |
0df9d43f | 7681 | if not Do_Overflow_Check (N) then |
7682 | Reanalyze (LLIB, Suppress => True); | |
7683 | else | |
7684 | Reanalyze (LLIB); | |
7685 | end if; | |
7686 | ||
db415383 | 7687 | Scope_Suppress.Overflow_Mode_General := SG; |
7688 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 7689 | end; |
7690 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 7691 | |
7692 | ------------------------- | |
7693 | -- Overflow_Check_Mode -- | |
7694 | ------------------------- | |
7695 | ||
db415383 | 7696 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 7697 | begin |
724d2bd8 | 7698 | if In_Assertion_Expr = 0 then |
db415383 | 7699 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 7700 | else |
db415383 | 7701 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 7702 | end if; |
3cce7f32 | 7703 | end Overflow_Check_Mode; |
7704 | ||
7705 | -------------------------------- | |
7706 | -- Overflow_Checks_Suppressed -- | |
7707 | -------------------------------- | |
7708 | ||
7709 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7710 | begin | |
0df9d43f | 7711 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7712 | return Is_Check_Suppressed (E, Overflow_Check); | |
7713 | else | |
7714 | return Scope_Suppress.Suppress (Overflow_Check); | |
7715 | end if; | |
ee6ba406 | 7716 | end Overflow_Checks_Suppressed; |
fc75802a | 7717 | |
ee6ba406 | 7718 | ----------------------------- |
7719 | -- Range_Checks_Suppressed -- | |
7720 | ----------------------------- | |
7721 | ||
7722 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7723 | begin | |
9dfe12ae | 7724 | if Present (E) then |
7725 | ||
7726 | -- Note: for now we always suppress range checks on Vax float types, | |
7727 | -- since Gigi does not know how to generate these checks. | |
7728 | ||
7729 | if Vax_Float (E) then | |
7730 | return True; | |
7731 | elsif Kill_Range_Checks (E) then | |
7732 | return True; | |
7733 | elsif Checks_May_Be_Suppressed (E) then | |
7734 | return Is_Check_Suppressed (E, Range_Check); | |
7735 | end if; | |
7736 | end if; | |
ee6ba406 | 7737 | |
fafc6b97 | 7738 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 7739 | end Range_Checks_Suppressed; |
7740 | ||
0577b0b1 | 7741 | ----------------------------------------- |
7742 | -- Range_Or_Validity_Checks_Suppressed -- | |
7743 | ----------------------------------------- | |
7744 | ||
7745 | -- Note: the coding would be simpler here if we simply made appropriate | |
7746 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
7747 | -- duplicated checks which we prefer to avoid. | |
7748 | ||
7749 | function Range_Or_Validity_Checks_Suppressed | |
7750 | (Expr : Node_Id) return Boolean | |
7751 | is | |
7752 | begin | |
7753 | -- Immediate return if scope checks suppressed for either check | |
7754 | ||
fafc6b97 | 7755 | if Scope_Suppress.Suppress (Range_Check) |
7756 | or | |
7757 | Scope_Suppress.Suppress (Validity_Check) | |
7758 | then | |
0577b0b1 | 7759 | return True; |
7760 | end if; | |
7761 | ||
7762 | -- If no expression, that's odd, decide that checks are suppressed, | |
7763 | -- since we don't want anyone trying to do checks in this case, which | |
7764 | -- is most likely the result of some other error. | |
7765 | ||
7766 | if No (Expr) then | |
7767 | return True; | |
7768 | end if; | |
7769 | ||
7770 | -- Expression is present, so perform suppress checks on type | |
7771 | ||
7772 | declare | |
7773 | Typ : constant Entity_Id := Etype (Expr); | |
7774 | begin | |
7775 | if Vax_Float (Typ) then | |
7776 | return True; | |
7777 | elsif Checks_May_Be_Suppressed (Typ) | |
7778 | and then (Is_Check_Suppressed (Typ, Range_Check) | |
7779 | or else | |
7780 | Is_Check_Suppressed (Typ, Validity_Check)) | |
7781 | then | |
7782 | return True; | |
7783 | end if; | |
7784 | end; | |
7785 | ||
7786 | -- If expression is an entity name, perform checks on this entity | |
7787 | ||
7788 | if Is_Entity_Name (Expr) then | |
7789 | declare | |
7790 | Ent : constant Entity_Id := Entity (Expr); | |
7791 | begin | |
7792 | if Checks_May_Be_Suppressed (Ent) then | |
7793 | return Is_Check_Suppressed (Ent, Range_Check) | |
7794 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
7795 | end if; | |
7796 | end; | |
7797 | end if; | |
7798 | ||
7799 | -- If we fall through, no checks suppressed | |
7800 | ||
7801 | return False; | |
7802 | end Range_Or_Validity_Checks_Suppressed; | |
7803 | ||
226494a3 | 7804 | ------------------- |
7805 | -- Remove_Checks -- | |
7806 | ------------------- | |
7807 | ||
7808 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 7809 | function Process (N : Node_Id) return Traverse_Result; |
7810 | -- Process a single node during the traversal | |
7811 | ||
8f6e4fd5 | 7812 | procedure Traverse is new Traverse_Proc (Process); |
7813 | -- The traversal procedure itself | |
226494a3 | 7814 | |
7815 | ------------- | |
7816 | -- Process -- | |
7817 | ------------- | |
7818 | ||
7819 | function Process (N : Node_Id) return Traverse_Result is | |
7820 | begin | |
7821 | if Nkind (N) not in N_Subexpr then | |
7822 | return Skip; | |
7823 | end if; | |
7824 | ||
7825 | Set_Do_Range_Check (N, False); | |
7826 | ||
7827 | case Nkind (N) is | |
7828 | when N_And_Then => | |
8f6e4fd5 | 7829 | Traverse (Left_Opnd (N)); |
226494a3 | 7830 | return Skip; |
7831 | ||
7832 | when N_Attribute_Reference => | |
226494a3 | 7833 | Set_Do_Overflow_Check (N, False); |
7834 | ||
226494a3 | 7835 | when N_Function_Call => |
7836 | Set_Do_Tag_Check (N, False); | |
7837 | ||
226494a3 | 7838 | when N_Op => |
7839 | Set_Do_Overflow_Check (N, False); | |
7840 | ||
7841 | case Nkind (N) is | |
7842 | when N_Op_Divide => | |
7843 | Set_Do_Division_Check (N, False); | |
7844 | ||
7845 | when N_Op_And => | |
7846 | Set_Do_Length_Check (N, False); | |
7847 | ||
7848 | when N_Op_Mod => | |
7849 | Set_Do_Division_Check (N, False); | |
7850 | ||
7851 | when N_Op_Or => | |
7852 | Set_Do_Length_Check (N, False); | |
7853 | ||
7854 | when N_Op_Rem => | |
7855 | Set_Do_Division_Check (N, False); | |
7856 | ||
7857 | when N_Op_Xor => | |
7858 | Set_Do_Length_Check (N, False); | |
7859 | ||
7860 | when others => | |
7861 | null; | |
7862 | end case; | |
7863 | ||
7864 | when N_Or_Else => | |
8f6e4fd5 | 7865 | Traverse (Left_Opnd (N)); |
226494a3 | 7866 | return Skip; |
7867 | ||
7868 | when N_Selected_Component => | |
226494a3 | 7869 | Set_Do_Discriminant_Check (N, False); |
7870 | ||
226494a3 | 7871 | when N_Type_Conversion => |
9dfe12ae | 7872 | Set_Do_Length_Check (N, False); |
7873 | Set_Do_Tag_Check (N, False); | |
226494a3 | 7874 | Set_Do_Overflow_Check (N, False); |
226494a3 | 7875 | |
7876 | when others => | |
7877 | null; | |
7878 | end case; | |
7879 | ||
7880 | return OK; | |
7881 | end Process; | |
7882 | ||
7883 | -- Start of processing for Remove_Checks | |
7884 | ||
7885 | begin | |
8f6e4fd5 | 7886 | Traverse (Expr); |
226494a3 | 7887 | end Remove_Checks; |
7888 | ||
ee6ba406 | 7889 | ---------------------------- |
7890 | -- Selected_Length_Checks -- | |
7891 | ---------------------------- | |
7892 | ||
7893 | function Selected_Length_Checks | |
7894 | (Ck_Node : Node_Id; | |
7895 | Target_Typ : Entity_Id; | |
7896 | Source_Typ : Entity_Id; | |
314a23b6 | 7897 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 7898 | is |
7899 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
7900 | S_Typ : Entity_Id; | |
7901 | T_Typ : Entity_Id; | |
7902 | Expr_Actual : Node_Id; | |
7903 | Exptyp : Entity_Id; | |
7904 | Cond : Node_Id := Empty; | |
7905 | Do_Access : Boolean := False; | |
7906 | Wnode : Node_Id := Warn_Node; | |
7907 | Ret_Result : Check_Result := (Empty, Empty); | |
7908 | Num_Checks : Natural := 0; | |
7909 | ||
7910 | procedure Add_Check (N : Node_Id); | |
7911 | -- Adds the action given to Ret_Result if N is non-Empty | |
7912 | ||
7913 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
7914 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 7915 | -- Comments required ??? |
ee6ba406 | 7916 | |
7917 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
7918 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 7919 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 7920 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 7921 | -- obviously superfluous checks. |
ee6ba406 | 7922 | |
7923 | function Length_E_Cond | |
7924 | (Exptyp : Entity_Id; | |
7925 | Typ : Entity_Id; | |
314a23b6 | 7926 | Indx : Nat) return Node_Id; |
ee6ba406 | 7927 | -- Returns expression to compute: |
7928 | -- Typ'Length /= Exptyp'Length | |
7929 | ||
7930 | function Length_N_Cond | |
7931 | (Expr : Node_Id; | |
7932 | Typ : Entity_Id; | |
314a23b6 | 7933 | Indx : Nat) return Node_Id; |
ee6ba406 | 7934 | -- Returns expression to compute: |
7935 | -- Typ'Length /= Expr'Length | |
7936 | ||
7937 | --------------- | |
7938 | -- Add_Check -- | |
7939 | --------------- | |
7940 | ||
7941 | procedure Add_Check (N : Node_Id) is | |
7942 | begin | |
7943 | if Present (N) then | |
7944 | ||
7945 | -- For now, ignore attempt to place more than 2 checks ??? | |
7946 | ||
7947 | if Num_Checks = 2 then | |
7948 | return; | |
7949 | end if; | |
7950 | ||
7951 | pragma Assert (Num_Checks <= 1); | |
7952 | Num_Checks := Num_Checks + 1; | |
7953 | Ret_Result (Num_Checks) := N; | |
7954 | end if; | |
7955 | end Add_Check; | |
7956 | ||
7957 | ------------------ | |
7958 | -- Get_E_Length -- | |
7959 | ------------------ | |
7960 | ||
7961 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 7962 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 7963 | N : Node_Id; |
7964 | E1 : Entity_Id := E; | |
ee6ba406 | 7965 | |
7966 | begin | |
7967 | if Ekind (Scope (E)) = E_Record_Type | |
7968 | and then Has_Discriminants (Scope (E)) | |
7969 | then | |
7970 | N := Build_Discriminal_Subtype_Of_Component (E); | |
7971 | ||
7972 | if Present (N) then | |
7973 | Insert_Action (Ck_Node, N); | |
7974 | E1 := Defining_Identifier (N); | |
7975 | end if; | |
7976 | end if; | |
7977 | ||
7978 | if Ekind (E1) = E_String_Literal_Subtype then | |
7979 | return | |
7980 | Make_Integer_Literal (Loc, | |
7981 | Intval => String_Literal_Length (E1)); | |
7982 | ||
00c403ee | 7983 | elsif SE /= Standard_Standard |
7984 | and then Ekind (Scope (SE)) = E_Protected_Type | |
7985 | and then Has_Discriminants (Scope (SE)) | |
7986 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 7987 | and then not Inside_Init_Proc |
7988 | then | |
ee6ba406 | 7989 | -- If the type whose length is needed is a private component |
7990 | -- constrained by a discriminant, we must expand the 'Length | |
7991 | -- attribute into an explicit computation, using the discriminal | |
7992 | -- of the current protected operation. This is because the actual | |
7993 | -- type of the prival is constructed after the protected opera- | |
7994 | -- tion has been fully expanded. | |
7995 | ||
7996 | declare | |
7997 | Indx_Type : Node_Id; | |
7998 | Lo : Node_Id; | |
7999 | Hi : Node_Id; | |
8000 | Do_Expand : Boolean := False; | |
8001 | ||
8002 | begin | |
8003 | Indx_Type := First_Index (E); | |
8004 | ||
8005 | for J in 1 .. Indx - 1 loop | |
8006 | Next_Index (Indx_Type); | |
8007 | end loop; | |
8008 | ||
2af58f67 | 8009 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 8010 | |
8011 | if Nkind (Lo) = N_Identifier | |
8012 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
8013 | then | |
8014 | Lo := Get_Discriminal (E, Lo); | |
8015 | Do_Expand := True; | |
8016 | end if; | |
8017 | ||
8018 | if Nkind (Hi) = N_Identifier | |
8019 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
8020 | then | |
8021 | Hi := Get_Discriminal (E, Hi); | |
8022 | Do_Expand := True; | |
8023 | end if; | |
8024 | ||
8025 | if Do_Expand then | |
8026 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 8027 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 8028 | end if; |
8029 | ||
8030 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 8031 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 8032 | end if; |
8033 | ||
8034 | N := | |
8035 | Make_Op_Add (Loc, | |
8036 | Left_Opnd => | |
8037 | Make_Op_Subtract (Loc, | |
8038 | Left_Opnd => Hi, | |
8039 | Right_Opnd => Lo), | |
8040 | ||
8041 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
8042 | return N; | |
8043 | ||
8044 | else | |
8045 | N := | |
8046 | Make_Attribute_Reference (Loc, | |
8047 | Attribute_Name => Name_Length, | |
8048 | Prefix => | |
8049 | New_Occurrence_Of (E1, Loc)); | |
8050 | ||
8051 | if Indx > 1 then | |
8052 | Set_Expressions (N, New_List ( | |
8053 | Make_Integer_Literal (Loc, Indx))); | |
8054 | end if; | |
8055 | ||
8056 | return N; | |
8057 | end if; | |
8058 | end; | |
8059 | ||
8060 | else | |
8061 | N := | |
8062 | Make_Attribute_Reference (Loc, | |
8063 | Attribute_Name => Name_Length, | |
8064 | Prefix => | |
8065 | New_Occurrence_Of (E1, Loc)); | |
8066 | ||
8067 | if Indx > 1 then | |
8068 | Set_Expressions (N, New_List ( | |
8069 | Make_Integer_Literal (Loc, Indx))); | |
8070 | end if; | |
8071 | ||
8072 | return N; | |
ee6ba406 | 8073 | end if; |
8074 | end Get_E_Length; | |
8075 | ||
8076 | ------------------ | |
8077 | -- Get_N_Length -- | |
8078 | ------------------ | |
8079 | ||
8080 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
8081 | begin | |
8082 | return | |
8083 | Make_Attribute_Reference (Loc, | |
8084 | Attribute_Name => Name_Length, | |
8085 | Prefix => | |
9dfe12ae | 8086 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8087 | Expressions => New_List ( |
8088 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8089 | end Get_N_Length; |
8090 | ||
8091 | ------------------- | |
8092 | -- Length_E_Cond -- | |
8093 | ------------------- | |
8094 | ||
8095 | function Length_E_Cond | |
8096 | (Exptyp : Entity_Id; | |
8097 | Typ : Entity_Id; | |
314a23b6 | 8098 | Indx : Nat) return Node_Id |
ee6ba406 | 8099 | is |
8100 | begin | |
8101 | return | |
8102 | Make_Op_Ne (Loc, | |
8103 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8104 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 8105 | end Length_E_Cond; |
8106 | ||
8107 | ------------------- | |
8108 | -- Length_N_Cond -- | |
8109 | ------------------- | |
8110 | ||
8111 | function Length_N_Cond | |
8112 | (Expr : Node_Id; | |
8113 | Typ : Entity_Id; | |
314a23b6 | 8114 | Indx : Nat) return Node_Id |
ee6ba406 | 8115 | is |
8116 | begin | |
8117 | return | |
8118 | Make_Op_Ne (Loc, | |
8119 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8120 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 8121 | end Length_N_Cond; |
8122 | ||
feff2f05 | 8123 | ----------------- |
8124 | -- Same_Bounds -- | |
8125 | ----------------- | |
8126 | ||
ee6ba406 | 8127 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
8128 | begin | |
8129 | return | |
8130 | (Nkind (L) = N_Integer_Literal | |
8131 | and then Nkind (R) = N_Integer_Literal | |
8132 | and then Intval (L) = Intval (R)) | |
8133 | ||
8134 | or else | |
8135 | (Is_Entity_Name (L) | |
8136 | and then Ekind (Entity (L)) = E_Constant | |
8137 | and then ((Is_Entity_Name (R) | |
8138 | and then Entity (L) = Entity (R)) | |
8139 | or else | |
8140 | (Nkind (R) = N_Type_Conversion | |
8141 | and then Is_Entity_Name (Expression (R)) | |
8142 | and then Entity (L) = Entity (Expression (R))))) | |
8143 | ||
8144 | or else | |
8145 | (Is_Entity_Name (R) | |
8146 | and then Ekind (Entity (R)) = E_Constant | |
8147 | and then Nkind (L) = N_Type_Conversion | |
8148 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 8149 | and then Entity (R) = Entity (Expression (L))) |
8150 | ||
8151 | or else | |
8152 | (Is_Entity_Name (L) | |
8153 | and then Is_Entity_Name (R) | |
8154 | and then Entity (L) = Entity (R) | |
8155 | and then Ekind (Entity (L)) = E_In_Parameter | |
8156 | and then Inside_Init_Proc); | |
ee6ba406 | 8157 | end Same_Bounds; |
8158 | ||
8159 | -- Start of processing for Selected_Length_Checks | |
8160 | ||
8161 | begin | |
6dbcfcd9 | 8162 | if not Full_Expander_Active then |
ee6ba406 | 8163 | return Ret_Result; |
8164 | end if; | |
8165 | ||
8166 | if Target_Typ = Any_Type | |
8167 | or else Target_Typ = Any_Composite | |
8168 | or else Raises_Constraint_Error (Ck_Node) | |
8169 | then | |
8170 | return Ret_Result; | |
8171 | end if; | |
8172 | ||
8173 | if No (Wnode) then | |
8174 | Wnode := Ck_Node; | |
8175 | end if; | |
8176 | ||
8177 | T_Typ := Target_Typ; | |
8178 | ||
8179 | if No (Source_Typ) then | |
8180 | S_Typ := Etype (Ck_Node); | |
8181 | else | |
8182 | S_Typ := Source_Typ; | |
8183 | end if; | |
8184 | ||
8185 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8186 | return Ret_Result; | |
8187 | end if; | |
8188 | ||
8189 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8190 | S_Typ := Designated_Type (S_Typ); | |
8191 | T_Typ := Designated_Type (T_Typ); | |
8192 | Do_Access := True; | |
8193 | ||
2af58f67 | 8194 | -- A simple optimization for the null case |
ee6ba406 | 8195 | |
2af58f67 | 8196 | if Known_Null (Ck_Node) then |
ee6ba406 | 8197 | return Ret_Result; |
8198 | end if; | |
8199 | end if; | |
8200 | ||
8201 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
8202 | if Is_Constrained (T_Typ) then | |
8203 | ||
92f1631f | 8204 | -- The checking code to be generated will freeze the corresponding |
8205 | -- array type. However, we must freeze the type now, so that the | |
8206 | -- freeze node does not appear within the generated if expression, | |
8207 | -- but ahead of it. | |
ee6ba406 | 8208 | |
8209 | Freeze_Before (Ck_Node, T_Typ); | |
8210 | ||
8211 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 8212 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 8213 | |
8214 | if Is_Access_Type (Exptyp) then | |
8215 | Exptyp := Designated_Type (Exptyp); | |
8216 | end if; | |
8217 | ||
8218 | -- String_Literal case. This needs to be handled specially be- | |
8219 | -- cause no index types are available for string literals. The | |
8220 | -- condition is simply: | |
8221 | ||
8222 | -- T_Typ'Length = string-literal-length | |
8223 | ||
9dfe12ae | 8224 | if Nkind (Expr_Actual) = N_String_Literal |
8225 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
8226 | then | |
ee6ba406 | 8227 | Cond := |
8228 | Make_Op_Ne (Loc, | |
8229 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
8230 | Right_Opnd => | |
8231 | Make_Integer_Literal (Loc, | |
8232 | Intval => | |
8233 | String_Literal_Length (Etype (Expr_Actual)))); | |
8234 | ||
8235 | -- General array case. Here we have a usable actual subtype for | |
8236 | -- the expression, and the condition is built from the two types | |
8237 | -- (Do_Length): | |
8238 | ||
8239 | -- T_Typ'Length /= Exptyp'Length or else | |
8240 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
8241 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
8242 | -- ... | |
8243 | ||
8244 | elsif Is_Constrained (Exptyp) then | |
8245 | declare | |
9dfe12ae | 8246 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
8247 | ||
8248 | L_Index : Node_Id; | |
8249 | R_Index : Node_Id; | |
8250 | L_Low : Node_Id; | |
8251 | L_High : Node_Id; | |
8252 | R_Low : Node_Id; | |
8253 | R_High : Node_Id; | |
ee6ba406 | 8254 | L_Length : Uint; |
8255 | R_Length : Uint; | |
9dfe12ae | 8256 | Ref_Node : Node_Id; |
ee6ba406 | 8257 | |
8258 | begin | |
feff2f05 | 8259 | -- At the library level, we need to ensure that the type of |
8260 | -- the object is elaborated before the check itself is | |
8261 | -- emitted. This is only done if the object is in the | |
8262 | -- current compilation unit, otherwise the type is frozen | |
8263 | -- and elaborated in its unit. | |
9dfe12ae | 8264 | |
8265 | if Is_Itype (Exptyp) | |
8266 | and then | |
8267 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
8268 | and then | |
8269 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 8270 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 8271 | then |
8272 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
8273 | Set_Itype (Ref_Node, Exptyp); | |
8274 | Insert_Action (Ck_Node, Ref_Node); | |
8275 | end if; | |
8276 | ||
ee6ba406 | 8277 | L_Index := First_Index (T_Typ); |
8278 | R_Index := First_Index (Exptyp); | |
8279 | ||
8280 | for Indx in 1 .. Ndims loop | |
8281 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 8282 | or else |
8283 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 8284 | then |
8285 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
8286 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
8287 | ||
8288 | -- Deal with compile time length check. Note that we | |
8289 | -- skip this in the access case, because the access | |
8290 | -- value may be null, so we cannot know statically. | |
8291 | ||
8292 | if not Do_Access | |
8293 | and then Compile_Time_Known_Value (L_Low) | |
8294 | and then Compile_Time_Known_Value (L_High) | |
8295 | and then Compile_Time_Known_Value (R_Low) | |
8296 | and then Compile_Time_Known_Value (R_High) | |
8297 | then | |
8298 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
8299 | L_Length := Expr_Value (L_High) - | |
8300 | Expr_Value (L_Low) + 1; | |
8301 | else | |
8302 | L_Length := UI_From_Int (0); | |
8303 | end if; | |
8304 | ||
8305 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
8306 | R_Length := Expr_Value (R_High) - | |
8307 | Expr_Value (R_Low) + 1; | |
8308 | else | |
8309 | R_Length := UI_From_Int (0); | |
8310 | end if; | |
8311 | ||
8312 | if L_Length > R_Length then | |
8313 | Add_Check | |
8314 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8315 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 8316 | |
8317 | elsif L_Length < R_Length then | |
8318 | Add_Check | |
8319 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8320 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 8321 | end if; |
8322 | ||
8323 | -- The comparison for an individual index subtype | |
8324 | -- is omitted if the corresponding index subtypes | |
8325 | -- statically match, since the result is known to | |
8326 | -- be true. Note that this test is worth while even | |
8327 | -- though we do static evaluation, because non-static | |
8328 | -- subtypes can statically match. | |
8329 | ||
8330 | elsif not | |
8331 | Subtypes_Statically_Match | |
8332 | (Etype (L_Index), Etype (R_Index)) | |
8333 | ||
8334 | and then not | |
8335 | (Same_Bounds (L_Low, R_Low) | |
8336 | and then Same_Bounds (L_High, R_High)) | |
8337 | then | |
8338 | Evolve_Or_Else | |
8339 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
8340 | end if; | |
8341 | ||
8342 | Next (L_Index); | |
8343 | Next (R_Index); | |
8344 | end if; | |
8345 | end loop; | |
8346 | end; | |
8347 | ||
8348 | -- Handle cases where we do not get a usable actual subtype that | |
8349 | -- is constrained. This happens for example in the function call | |
8350 | -- and explicit dereference cases. In these cases, we have to get | |
8351 | -- the length or range from the expression itself, making sure we | |
8352 | -- do not evaluate it more than once. | |
8353 | ||
8354 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 8355 | -- result of applying Duplicate_Expr to the original tree, forcing |
8356 | -- the result to be a name. | |
ee6ba406 | 8357 | |
8358 | else | |
8359 | declare | |
9dfe12ae | 8360 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 8361 | |
8362 | begin | |
8363 | -- Build the condition for the explicit dereference case | |
8364 | ||
8365 | for Indx in 1 .. Ndims loop | |
8366 | Evolve_Or_Else | |
8367 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
8368 | end loop; | |
8369 | end; | |
8370 | end if; | |
8371 | end if; | |
8372 | end if; | |
8373 | ||
8374 | -- Construct the test and insert into the tree | |
8375 | ||
8376 | if Present (Cond) then | |
8377 | if Do_Access then | |
8378 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
8379 | end if; | |
8380 | ||
f15731c4 | 8381 | Add_Check |
8382 | (Make_Raise_Constraint_Error (Loc, | |
8383 | Condition => Cond, | |
8384 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 8385 | end if; |
8386 | ||
8387 | return Ret_Result; | |
ee6ba406 | 8388 | end Selected_Length_Checks; |
8389 | ||
8390 | --------------------------- | |
8391 | -- Selected_Range_Checks -- | |
8392 | --------------------------- | |
8393 | ||
8394 | function Selected_Range_Checks | |
8395 | (Ck_Node : Node_Id; | |
8396 | Target_Typ : Entity_Id; | |
8397 | Source_Typ : Entity_Id; | |
314a23b6 | 8398 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8399 | is |
8400 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8401 | S_Typ : Entity_Id; | |
8402 | T_Typ : Entity_Id; | |
8403 | Expr_Actual : Node_Id; | |
8404 | Exptyp : Entity_Id; | |
8405 | Cond : Node_Id := Empty; | |
8406 | Do_Access : Boolean := False; | |
8407 | Wnode : Node_Id := Warn_Node; | |
8408 | Ret_Result : Check_Result := (Empty, Empty); | |
8409 | Num_Checks : Integer := 0; | |
8410 | ||
8411 | procedure Add_Check (N : Node_Id); | |
8412 | -- Adds the action given to Ret_Result if N is non-Empty | |
8413 | ||
8414 | function Discrete_Range_Cond | |
8415 | (Expr : Node_Id; | |
314a23b6 | 8416 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8417 | -- Returns expression to compute: |
8418 | -- Low_Bound (Expr) < Typ'First | |
8419 | -- or else | |
8420 | -- High_Bound (Expr) > Typ'Last | |
8421 | ||
8422 | function Discrete_Expr_Cond | |
8423 | (Expr : Node_Id; | |
314a23b6 | 8424 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8425 | -- Returns expression to compute: |
8426 | -- Expr < Typ'First | |
8427 | -- or else | |
8428 | -- Expr > Typ'Last | |
8429 | ||
8430 | function Get_E_First_Or_Last | |
3cb12758 | 8431 | (Loc : Source_Ptr; |
8432 | E : Entity_Id; | |
ee6ba406 | 8433 | Indx : Nat; |
314a23b6 | 8434 | Nam : Name_Id) return Node_Id; |
79212397 | 8435 | -- Returns an attribute reference |
ee6ba406 | 8436 | -- E'First or E'Last |
79212397 | 8437 | -- with a source location of Loc. |
f73ee678 | 8438 | -- |
79212397 | 8439 | -- Nam is Name_First or Name_Last, according to which attribute is |
8440 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
8441 | -- Expressions of the attribute reference (identifying the desired | |
8442 | -- array dimension). | |
ee6ba406 | 8443 | |
8444 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
8445 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
8446 | -- Returns expression to compute: | |
9dfe12ae | 8447 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 8448 | |
8449 | function Range_E_Cond | |
8450 | (Exptyp : Entity_Id; | |
8451 | Typ : Entity_Id; | |
8452 | Indx : Nat) | |
8453 | return Node_Id; | |
8454 | -- Returns expression to compute: | |
8455 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
8456 | ||
8457 | function Range_Equal_E_Cond | |
8458 | (Exptyp : Entity_Id; | |
8459 | Typ : Entity_Id; | |
314a23b6 | 8460 | Indx : Nat) return Node_Id; |
ee6ba406 | 8461 | -- Returns expression to compute: |
8462 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
8463 | ||
8464 | function Range_N_Cond | |
8465 | (Expr : Node_Id; | |
8466 | Typ : Entity_Id; | |
314a23b6 | 8467 | Indx : Nat) return Node_Id; |
ee6ba406 | 8468 | -- Return expression to compute: |
8469 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
8470 | ||
8471 | --------------- | |
8472 | -- Add_Check -- | |
8473 | --------------- | |
8474 | ||
8475 | procedure Add_Check (N : Node_Id) is | |
8476 | begin | |
8477 | if Present (N) then | |
8478 | ||
8479 | -- For now, ignore attempt to place more than 2 checks ??? | |
8480 | ||
8481 | if Num_Checks = 2 then | |
8482 | return; | |
8483 | end if; | |
8484 | ||
8485 | pragma Assert (Num_Checks <= 1); | |
8486 | Num_Checks := Num_Checks + 1; | |
8487 | Ret_Result (Num_Checks) := N; | |
8488 | end if; | |
8489 | end Add_Check; | |
8490 | ||
8491 | ------------------------- | |
8492 | -- Discrete_Expr_Cond -- | |
8493 | ------------------------- | |
8494 | ||
8495 | function Discrete_Expr_Cond | |
8496 | (Expr : Node_Id; | |
314a23b6 | 8497 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8498 | is |
8499 | begin | |
8500 | return | |
8501 | Make_Or_Else (Loc, | |
8502 | Left_Opnd => | |
8503 | Make_Op_Lt (Loc, | |
8504 | Left_Opnd => | |
9dfe12ae | 8505 | Convert_To (Base_Type (Typ), |
8506 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8507 | Right_Opnd => |
8508 | Convert_To (Base_Type (Typ), | |
3cb12758 | 8509 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 8510 | |
8511 | Right_Opnd => | |
8512 | Make_Op_Gt (Loc, | |
8513 | Left_Opnd => | |
9dfe12ae | 8514 | Convert_To (Base_Type (Typ), |
8515 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8516 | Right_Opnd => |
8517 | Convert_To | |
8518 | (Base_Type (Typ), | |
3cb12758 | 8519 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 8520 | end Discrete_Expr_Cond; |
8521 | ||
8522 | ------------------------- | |
8523 | -- Discrete_Range_Cond -- | |
8524 | ------------------------- | |
8525 | ||
8526 | function Discrete_Range_Cond | |
8527 | (Expr : Node_Id; | |
314a23b6 | 8528 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8529 | is |
8530 | LB : Node_Id := Low_Bound (Expr); | |
8531 | HB : Node_Id := High_Bound (Expr); | |
8532 | ||
8533 | Left_Opnd : Node_Id; | |
8534 | Right_Opnd : Node_Id; | |
8535 | ||
8536 | begin | |
8537 | if Nkind (LB) = N_Identifier | |
feff2f05 | 8538 | and then Ekind (Entity (LB)) = E_Discriminant |
8539 | then | |
ee6ba406 | 8540 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
8541 | end if; | |
8542 | ||
ee6ba406 | 8543 | Left_Opnd := |
8544 | Make_Op_Lt (Loc, | |
8545 | Left_Opnd => | |
8546 | Convert_To | |
9dfe12ae | 8547 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 8548 | |
8549 | Right_Opnd => | |
8550 | Convert_To | |
3cb12758 | 8551 | (Base_Type (Typ), |
8552 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 8553 | |
ba9b1a39 | 8554 | if Nkind (HB) = N_Identifier |
8555 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 8556 | then |
ba9b1a39 | 8557 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 8558 | end if; |
8559 | ||
8560 | Right_Opnd := | |
8561 | Make_Op_Gt (Loc, | |
8562 | Left_Opnd => | |
8563 | Convert_To | |
9dfe12ae | 8564 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 8565 | |
8566 | Right_Opnd => | |
8567 | Convert_To | |
8568 | (Base_Type (Typ), | |
3cb12758 | 8569 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 8570 | |
8571 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
8572 | end Discrete_Range_Cond; | |
8573 | ||
8574 | ------------------------- | |
8575 | -- Get_E_First_Or_Last -- | |
8576 | ------------------------- | |
8577 | ||
8578 | function Get_E_First_Or_Last | |
3cb12758 | 8579 | (Loc : Source_Ptr; |
8580 | E : Entity_Id; | |
ee6ba406 | 8581 | Indx : Nat; |
314a23b6 | 8582 | Nam : Name_Id) return Node_Id |
ee6ba406 | 8583 | is |
3cb12758 | 8584 | Exprs : List_Id; |
ee6ba406 | 8585 | begin |
3cb12758 | 8586 | if Indx > 0 then |
8587 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 8588 | else |
3cb12758 | 8589 | Exprs := No_List; |
ee6ba406 | 8590 | end if; |
8591 | ||
3cb12758 | 8592 | return Make_Attribute_Reference (Loc, |
8593 | Prefix => New_Occurrence_Of (E, Loc), | |
8594 | Attribute_Name => Nam, | |
8595 | Expressions => Exprs); | |
ee6ba406 | 8596 | end Get_E_First_Or_Last; |
8597 | ||
8598 | ----------------- | |
8599 | -- Get_N_First -- | |
8600 | ----------------- | |
8601 | ||
8602 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
8603 | begin | |
8604 | return | |
8605 | Make_Attribute_Reference (Loc, | |
8606 | Attribute_Name => Name_First, | |
8607 | Prefix => | |
9dfe12ae | 8608 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8609 | Expressions => New_List ( |
8610 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8611 | end Get_N_First; |
8612 | ||
8613 | ---------------- | |
8614 | -- Get_N_Last -- | |
8615 | ---------------- | |
8616 | ||
8617 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
8618 | begin | |
8619 | return | |
8620 | Make_Attribute_Reference (Loc, | |
8621 | Attribute_Name => Name_Last, | |
8622 | Prefix => | |
9dfe12ae | 8623 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8624 | Expressions => New_List ( |
8625 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8626 | end Get_N_Last; |
8627 | ||
8628 | ------------------ | |
8629 | -- Range_E_Cond -- | |
8630 | ------------------ | |
8631 | ||
8632 | function Range_E_Cond | |
8633 | (Exptyp : Entity_Id; | |
8634 | Typ : Entity_Id; | |
314a23b6 | 8635 | Indx : Nat) return Node_Id |
ee6ba406 | 8636 | is |
8637 | begin | |
8638 | return | |
8639 | Make_Or_Else (Loc, | |
8640 | Left_Opnd => | |
8641 | Make_Op_Lt (Loc, | |
3cb12758 | 8642 | Left_Opnd => |
8643 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8644 | Right_Opnd => | |
8645 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8646 | |
8647 | Right_Opnd => | |
8648 | Make_Op_Gt (Loc, | |
3cb12758 | 8649 | Left_Opnd => |
8650 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8651 | Right_Opnd => | |
8652 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8653 | end Range_E_Cond; |
8654 | ||
8655 | ------------------------ | |
8656 | -- Range_Equal_E_Cond -- | |
8657 | ------------------------ | |
8658 | ||
8659 | function Range_Equal_E_Cond | |
8660 | (Exptyp : Entity_Id; | |
8661 | Typ : Entity_Id; | |
314a23b6 | 8662 | Indx : Nat) return Node_Id |
ee6ba406 | 8663 | is |
8664 | begin | |
8665 | return | |
8666 | Make_Or_Else (Loc, | |
8667 | Left_Opnd => | |
8668 | Make_Op_Ne (Loc, | |
3cb12758 | 8669 | Left_Opnd => |
8670 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8671 | Right_Opnd => | |
8672 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
8673 | ||
ee6ba406 | 8674 | Right_Opnd => |
8675 | Make_Op_Ne (Loc, | |
3cb12758 | 8676 | Left_Opnd => |
8677 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8678 | Right_Opnd => | |
8679 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8680 | end Range_Equal_E_Cond; |
8681 | ||
8682 | ------------------ | |
8683 | -- Range_N_Cond -- | |
8684 | ------------------ | |
8685 | ||
8686 | function Range_N_Cond | |
8687 | (Expr : Node_Id; | |
8688 | Typ : Entity_Id; | |
314a23b6 | 8689 | Indx : Nat) return Node_Id |
ee6ba406 | 8690 | is |
8691 | begin | |
8692 | return | |
8693 | Make_Or_Else (Loc, | |
8694 | Left_Opnd => | |
8695 | Make_Op_Lt (Loc, | |
3cb12758 | 8696 | Left_Opnd => |
8697 | Get_N_First (Expr, Indx), | |
8698 | Right_Opnd => | |
8699 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8700 | |
8701 | Right_Opnd => | |
8702 | Make_Op_Gt (Loc, | |
3cb12758 | 8703 | Left_Opnd => |
8704 | Get_N_Last (Expr, Indx), | |
8705 | Right_Opnd => | |
8706 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8707 | end Range_N_Cond; |
8708 | ||
8709 | -- Start of processing for Selected_Range_Checks | |
8710 | ||
8711 | begin | |
6dbcfcd9 | 8712 | if not Full_Expander_Active then |
ee6ba406 | 8713 | return Ret_Result; |
8714 | end if; | |
8715 | ||
8716 | if Target_Typ = Any_Type | |
8717 | or else Target_Typ = Any_Composite | |
8718 | or else Raises_Constraint_Error (Ck_Node) | |
8719 | then | |
8720 | return Ret_Result; | |
8721 | end if; | |
8722 | ||
8723 | if No (Wnode) then | |
8724 | Wnode := Ck_Node; | |
8725 | end if; | |
8726 | ||
8727 | T_Typ := Target_Typ; | |
8728 | ||
8729 | if No (Source_Typ) then | |
8730 | S_Typ := Etype (Ck_Node); | |
8731 | else | |
8732 | S_Typ := Source_Typ; | |
8733 | end if; | |
8734 | ||
8735 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8736 | return Ret_Result; | |
8737 | end if; | |
8738 | ||
8739 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
8740 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
8741 | -- in, and since Node can be an N_Range node, it might be invalid. | |
8742 | -- Should there be an assert check somewhere for taking the Etype of | |
8743 | -- an N_Range node ??? | |
8744 | ||
8745 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8746 | S_Typ := Designated_Type (S_Typ); | |
8747 | T_Typ := Designated_Type (T_Typ); | |
8748 | Do_Access := True; | |
8749 | ||
2af58f67 | 8750 | -- A simple optimization for the null case |
ee6ba406 | 8751 | |
2af58f67 | 8752 | if Known_Null (Ck_Node) then |
ee6ba406 | 8753 | return Ret_Result; |
8754 | end if; | |
8755 | end if; | |
8756 | ||
8757 | -- For an N_Range Node, check for a null range and then if not | |
8758 | -- null generate a range check action. | |
8759 | ||
8760 | if Nkind (Ck_Node) = N_Range then | |
8761 | ||
8762 | -- There's no point in checking a range against itself | |
8763 | ||
8764 | if Ck_Node = Scalar_Range (T_Typ) then | |
8765 | return Ret_Result; | |
8766 | end if; | |
8767 | ||
8768 | declare | |
8769 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
8770 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 8771 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
8772 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 8773 | |
eefa141b | 8774 | LB : Node_Id := Low_Bound (Ck_Node); |
8775 | HB : Node_Id := High_Bound (Ck_Node); | |
8776 | Known_LB : Boolean; | |
8777 | Known_HB : Boolean; | |
8778 | ||
8779 | Null_Range : Boolean; | |
ee6ba406 | 8780 | Out_Of_Range_L : Boolean; |
8781 | Out_Of_Range_H : Boolean; | |
8782 | ||
8783 | begin | |
eefa141b | 8784 | -- Compute what is known at compile time |
8785 | ||
8786 | if Known_T_LB and Known_T_HB then | |
8787 | if Compile_Time_Known_Value (LB) then | |
8788 | Known_LB := True; | |
8789 | ||
8790 | -- There's no point in checking that a bound is within its | |
8791 | -- own range so pretend that it is known in this case. First | |
8792 | -- deal with low bound. | |
8793 | ||
8794 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
8795 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
8796 | then | |
8797 | LB := T_LB; | |
8798 | Known_LB := True; | |
8799 | ||
8800 | else | |
8801 | Known_LB := False; | |
8802 | end if; | |
8803 | ||
8804 | -- Likewise for the high bound | |
8805 | ||
8806 | if Compile_Time_Known_Value (HB) then | |
8807 | Known_HB := True; | |
8808 | ||
8809 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
8810 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
8811 | then | |
8812 | HB := T_HB; | |
8813 | Known_HB := True; | |
8814 | ||
8815 | else | |
8816 | Known_HB := False; | |
8817 | end if; | |
8818 | end if; | |
8819 | ||
8820 | -- Check for case where everything is static and we can do the | |
8821 | -- check at compile time. This is skipped if we have an access | |
8822 | -- type, since the access value may be null. | |
8823 | ||
8824 | -- ??? This code can be improved since you only need to know that | |
8825 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
8826 | -- compile time to emit pertinent messages. | |
8827 | ||
8828 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
8829 | and not Do_Access | |
ee6ba406 | 8830 | then |
8831 | -- Floating-point case | |
8832 | ||
8833 | if Is_Floating_Point_Type (S_Typ) then | |
8834 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
8835 | Out_Of_Range_L := | |
8836 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 8837 | or else |
ee6ba406 | 8838 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
8839 | ||
8840 | Out_Of_Range_H := | |
8841 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 8842 | or else |
ee6ba406 | 8843 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
8844 | ||
8845 | -- Fixed or discrete type case | |
8846 | ||
8847 | else | |
8848 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
8849 | Out_Of_Range_L := | |
8850 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 8851 | or else |
ee6ba406 | 8852 | (Expr_Value (LB) > Expr_Value (T_HB)); |
8853 | ||
8854 | Out_Of_Range_H := | |
8855 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 8856 | or else |
ee6ba406 | 8857 | (Expr_Value (HB) < Expr_Value (T_LB)); |
8858 | end if; | |
8859 | ||
8860 | if not Null_Range then | |
8861 | if Out_Of_Range_L then | |
8862 | if No (Warn_Node) then | |
8863 | Add_Check | |
8864 | (Compile_Time_Constraint_Error | |
8865 | (Low_Bound (Ck_Node), | |
cb97ae5c | 8866 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 8867 | |
8868 | else | |
8869 | Add_Check | |
8870 | (Compile_Time_Constraint_Error | |
8871 | (Wnode, | |
cb97ae5c | 8872 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 8873 | end if; |
8874 | end if; | |
8875 | ||
8876 | if Out_Of_Range_H then | |
8877 | if No (Warn_Node) then | |
8878 | Add_Check | |
8879 | (Compile_Time_Constraint_Error | |
8880 | (High_Bound (Ck_Node), | |
cb97ae5c | 8881 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 8882 | |
8883 | else | |
8884 | Add_Check | |
8885 | (Compile_Time_Constraint_Error | |
8886 | (Wnode, | |
cb97ae5c | 8887 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 8888 | end if; |
8889 | end if; | |
ee6ba406 | 8890 | end if; |
8891 | ||
8892 | else | |
8893 | declare | |
8894 | LB : Node_Id := Low_Bound (Ck_Node); | |
8895 | HB : Node_Id := High_Bound (Ck_Node); | |
8896 | ||
8897 | begin | |
feff2f05 | 8898 | -- If either bound is a discriminant and we are within the |
8899 | -- record declaration, it is a use of the discriminant in a | |
8900 | -- constraint of a component, and nothing can be checked | |
8901 | -- here. The check will be emitted within the init proc. | |
8902 | -- Before then, the discriminal has no real meaning. | |
8903 | -- Similarly, if the entity is a discriminal, there is no | |
8904 | -- check to perform yet. | |
8905 | ||
8906 | -- The same holds within a discriminated synchronized type, | |
8907 | -- where the discriminant may constrain a component or an | |
8908 | -- entry family. | |
ee6ba406 | 8909 | |
8910 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 8911 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 8912 | then |
0577b0b1 | 8913 | if Current_Scope = Scope (Entity (LB)) |
8914 | or else Is_Concurrent_Type (Current_Scope) | |
8915 | or else Ekind (Entity (LB)) /= E_Discriminant | |
8916 | then | |
ee6ba406 | 8917 | return Ret_Result; |
8918 | else | |
8919 | LB := | |
8920 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
8921 | end if; | |
8922 | end if; | |
8923 | ||
8924 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 8925 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 8926 | then |
0577b0b1 | 8927 | if Current_Scope = Scope (Entity (HB)) |
8928 | or else Is_Concurrent_Type (Current_Scope) | |
8929 | or else Ekind (Entity (HB)) /= E_Discriminant | |
8930 | then | |
ee6ba406 | 8931 | return Ret_Result; |
8932 | else | |
8933 | HB := | |
8934 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
8935 | end if; | |
8936 | end if; | |
8937 | ||
8938 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
8939 | Set_Paren_Count (Cond, 1); | |
8940 | ||
8941 | Cond := | |
8942 | Make_And_Then (Loc, | |
8943 | Left_Opnd => | |
8944 | Make_Op_Ge (Loc, | |
9dfe12ae | 8945 | Left_Opnd => Duplicate_Subexpr_No_Checks (HB), |
8946 | Right_Opnd => Duplicate_Subexpr_No_Checks (LB)), | |
ee6ba406 | 8947 | Right_Opnd => Cond); |
8948 | end; | |
ee6ba406 | 8949 | end if; |
8950 | end; | |
8951 | ||
8952 | elsif Is_Scalar_Type (S_Typ) then | |
8953 | ||
8954 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
8955 | -- except the above simply sets a flag in the node and lets | |
8956 | -- gigi generate the check base on the Etype of the expression. | |
8957 | -- Sometimes, however we want to do a dynamic check against an | |
8958 | -- arbitrary target type, so we do that here. | |
8959 | ||
8960 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
8961 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
8962 | ||
8963 | -- For literals, we can tell if the constraint error will be | |
8964 | -- raised at compile time, so we never need a dynamic check, but | |
8965 | -- if the exception will be raised, then post the usual warning, | |
8966 | -- and replace the literal with a raise constraint error | |
8967 | -- expression. As usual, skip this for access types | |
8968 | ||
8969 | elsif Compile_Time_Known_Value (Ck_Node) | |
8970 | and then not Do_Access | |
8971 | then | |
8972 | declare | |
8973 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
8974 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
8975 | ||
8976 | Out_Of_Range : Boolean; | |
8977 | Static_Bounds : constant Boolean := | |
b6341c67 | 8978 | Compile_Time_Known_Value (LB) |
8979 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 8980 | |
8981 | begin | |
8982 | -- Following range tests should use Sem_Eval routine ??? | |
8983 | ||
8984 | if Static_Bounds then | |
8985 | if Is_Floating_Point_Type (S_Typ) then | |
8986 | Out_Of_Range := | |
8987 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
8988 | or else | |
8989 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
8990 | ||
eefa141b | 8991 | -- Fixed or discrete type |
8992 | ||
8993 | else | |
ee6ba406 | 8994 | Out_Of_Range := |
8995 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
8996 | or else | |
8997 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
8998 | end if; | |
8999 | ||
eefa141b | 9000 | -- Bounds of the type are static and the literal is out of |
9001 | -- range so output a warning message. | |
ee6ba406 | 9002 | |
9003 | if Out_Of_Range then | |
9004 | if No (Warn_Node) then | |
9005 | Add_Check | |
9006 | (Compile_Time_Constraint_Error | |
9007 | (Ck_Node, | |
cb97ae5c | 9008 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9009 | |
9010 | else | |
9011 | Add_Check | |
9012 | (Compile_Time_Constraint_Error | |
9013 | (Wnode, | |
cb97ae5c | 9014 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9015 | end if; |
9016 | end if; | |
9017 | ||
9018 | else | |
9019 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9020 | end if; | |
9021 | end; | |
9022 | ||
9023 | -- Here for the case of a non-static expression, we need a runtime | |
9024 | -- check unless the source type range is guaranteed to be in the | |
9025 | -- range of the target type. | |
9026 | ||
9027 | else | |
7a1dabb3 | 9028 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 9029 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
9030 | end if; | |
9031 | end if; | |
9032 | end if; | |
9033 | ||
9034 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9035 | if Is_Constrained (T_Typ) then | |
9036 | ||
9037 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
9038 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
9039 | ||
9040 | if Is_Access_Type (Exptyp) then | |
9041 | Exptyp := Designated_Type (Exptyp); | |
9042 | end if; | |
9043 | ||
9044 | -- String_Literal case. This needs to be handled specially be- | |
9045 | -- cause no index types are available for string literals. The | |
9046 | -- condition is simply: | |
9047 | ||
9048 | -- T_Typ'Length = string-literal-length | |
9049 | ||
9050 | if Nkind (Expr_Actual) = N_String_Literal then | |
9051 | null; | |
9052 | ||
9053 | -- General array case. Here we have a usable actual subtype for | |
9054 | -- the expression, and the condition is built from the two types | |
9055 | ||
9056 | -- T_Typ'First < Exptyp'First or else | |
9057 | -- T_Typ'Last > Exptyp'Last or else | |
9058 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
9059 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
9060 | -- ... | |
9061 | ||
9062 | elsif Is_Constrained (Exptyp) then | |
9063 | declare | |
9dfe12ae | 9064 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9065 | ||
ee6ba406 | 9066 | L_Index : Node_Id; |
9067 | R_Index : Node_Id; | |
ee6ba406 | 9068 | |
9069 | begin | |
9070 | L_Index := First_Index (T_Typ); | |
9071 | R_Index := First_Index (Exptyp); | |
9072 | ||
9073 | for Indx in 1 .. Ndims loop | |
9074 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9075 | or else |
9076 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9077 | then |
ee6ba406 | 9078 | -- Deal with compile time length check. Note that we |
9079 | -- skip this in the access case, because the access | |
9080 | -- value may be null, so we cannot know statically. | |
9081 | ||
9082 | if not | |
9083 | Subtypes_Statically_Match | |
9084 | (Etype (L_Index), Etype (R_Index)) | |
9085 | then | |
9086 | -- If the target type is constrained then we | |
9087 | -- have to check for exact equality of bounds | |
9088 | -- (required for qualified expressions). | |
9089 | ||
9090 | if Is_Constrained (T_Typ) then | |
9091 | Evolve_Or_Else | |
9092 | (Cond, | |
9093 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 9094 | else |
9095 | Evolve_Or_Else | |
9096 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
9097 | end if; | |
9098 | end if; | |
9099 | ||
9100 | Next (L_Index); | |
9101 | Next (R_Index); | |
ee6ba406 | 9102 | end if; |
9103 | end loop; | |
9104 | end; | |
9105 | ||
9106 | -- Handle cases where we do not get a usable actual subtype that | |
9107 | -- is constrained. This happens for example in the function call | |
9108 | -- and explicit dereference cases. In these cases, we have to get | |
9109 | -- the length or range from the expression itself, making sure we | |
9110 | -- do not evaluate it more than once. | |
9111 | ||
9112 | -- Here Ck_Node is the original expression, or more properly the | |
9113 | -- result of applying Duplicate_Expr to the original tree, | |
9114 | -- forcing the result to be a name. | |
9115 | ||
9116 | else | |
9117 | declare | |
9dfe12ae | 9118 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9119 | |
9120 | begin | |
9121 | -- Build the condition for the explicit dereference case | |
9122 | ||
9123 | for Indx in 1 .. Ndims loop | |
9124 | Evolve_Or_Else | |
9125 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
9126 | end loop; | |
9127 | end; | |
ee6ba406 | 9128 | end if; |
9129 | ||
9130 | else | |
feff2f05 | 9131 | -- For a conversion to an unconstrained array type, generate an |
9132 | -- Action to check that the bounds of the source value are within | |
9133 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
9134 | -- check is needed for a conversion to an access to unconstrained | |
9135 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
9136 | -- of the two access types to statically match. | |
9137 | ||
9138 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
9139 | and then not Do_Access | |
9140 | then | |
ee6ba406 | 9141 | declare |
9142 | Opnd_Index : Node_Id; | |
9143 | Targ_Index : Node_Id; | |
00c403ee | 9144 | Opnd_Range : Node_Id; |
ee6ba406 | 9145 | |
9146 | begin | |
feff2f05 | 9147 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 9148 | Targ_Index := First_Index (T_Typ); |
00c403ee | 9149 | while Present (Opnd_Index) loop |
9150 | ||
9151 | -- If the index is a range, use its bounds. If it is an | |
9152 | -- entity (as will be the case if it is a named subtype | |
9153 | -- or an itype created for a slice) retrieve its range. | |
9154 | ||
9155 | if Is_Entity_Name (Opnd_Index) | |
9156 | and then Is_Type (Entity (Opnd_Index)) | |
9157 | then | |
9158 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
9159 | else | |
9160 | Opnd_Range := Opnd_Index; | |
9161 | end if; | |
9162 | ||
9163 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 9164 | if Is_In_Range |
9165 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
9166 | Assume_Valid => True) | |
ee6ba406 | 9167 | and then |
9168 | Is_In_Range | |
9c486805 | 9169 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9170 | Assume_Valid => True) | |
ee6ba406 | 9171 | then |
9172 | null; | |
9173 | ||
feff2f05 | 9174 | -- If null range, no check needed |
f2a06be9 | 9175 | |
9dfe12ae | 9176 | elsif |
00c403ee | 9177 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 9178 | and then |
00c403ee | 9179 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 9180 | and then |
00c403ee | 9181 | Expr_Value (High_Bound (Opnd_Range)) < |
9182 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 9183 | then |
9184 | null; | |
9185 | ||
ee6ba406 | 9186 | elsif Is_Out_Of_Range |
9c486805 | 9187 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
9188 | Assume_Valid => True) | |
ee6ba406 | 9189 | or else |
9190 | Is_Out_Of_Range | |
9c486805 | 9191 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9192 | Assume_Valid => True) | |
ee6ba406 | 9193 | then |
9194 | Add_Check | |
9195 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9196 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 9197 | |
9198 | else | |
9199 | Evolve_Or_Else | |
9200 | (Cond, | |
9201 | Discrete_Range_Cond | |
00c403ee | 9202 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 9203 | end if; |
9204 | end if; | |
9205 | ||
9206 | Next_Index (Opnd_Index); | |
9207 | Next_Index (Targ_Index); | |
9208 | end loop; | |
9209 | end; | |
9210 | end if; | |
9211 | end if; | |
9212 | end if; | |
9213 | ||
9214 | -- Construct the test and insert into the tree | |
9215 | ||
9216 | if Present (Cond) then | |
9217 | if Do_Access then | |
9218 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9219 | end if; | |
9220 | ||
f15731c4 | 9221 | Add_Check |
9222 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 9223 | Condition => Cond, |
9224 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 9225 | end if; |
9226 | ||
9227 | return Ret_Result; | |
ee6ba406 | 9228 | end Selected_Range_Checks; |
9229 | ||
9230 | ------------------------------- | |
9231 | -- Storage_Checks_Suppressed -- | |
9232 | ------------------------------- | |
9233 | ||
9234 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9235 | begin | |
9dfe12ae | 9236 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9237 | return Is_Check_Suppressed (E, Storage_Check); | |
9238 | else | |
fafc6b97 | 9239 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 9240 | end if; |
ee6ba406 | 9241 | end Storage_Checks_Suppressed; |
9242 | ||
9243 | --------------------------- | |
9244 | -- Tag_Checks_Suppressed -- | |
9245 | --------------------------- | |
9246 | ||
9247 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9248 | begin | |
89f1e35c | 9249 | if Present (E) |
9250 | and then Checks_May_Be_Suppressed (E) | |
9251 | then | |
9252 | return Is_Check_Suppressed (E, Tag_Check); | |
9dfe12ae | 9253 | end if; |
9254 | ||
fafc6b97 | 9255 | return Scope_Suppress.Suppress (Tag_Check); |
ee6ba406 | 9256 | end Tag_Checks_Suppressed; |
9257 | ||
0577b0b1 | 9258 | -------------------------- |
9259 | -- Validity_Check_Range -- | |
9260 | -------------------------- | |
9261 | ||
9262 | procedure Validity_Check_Range (N : Node_Id) is | |
9263 | begin | |
9264 | if Validity_Checks_On and Validity_Check_Operands then | |
9265 | if Nkind (N) = N_Range then | |
9266 | Ensure_Valid (Low_Bound (N)); | |
9267 | Ensure_Valid (High_Bound (N)); | |
9268 | end if; | |
9269 | end if; | |
9270 | end Validity_Check_Range; | |
9271 | ||
9272 | -------------------------------- | |
9273 | -- Validity_Checks_Suppressed -- | |
9274 | -------------------------------- | |
9275 | ||
9276 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9277 | begin | |
9278 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9279 | return Is_Check_Suppressed (E, Validity_Check); | |
9280 | else | |
fafc6b97 | 9281 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 9282 | end if; |
9283 | end Validity_Checks_Suppressed; | |
9284 | ||
ee6ba406 | 9285 | end Checks; |