]>
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 | -- -- | |
1581f2d7 | 9 | -- Copyright (C) 1992-2014, 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; | |
29448168 | 27 | with Casing; use Casing; |
ee6ba406 | 28 | with Debug; use Debug; |
29 | with Einfo; use Einfo; | |
7b8fa048 | 30 | with Elists; use Elists; |
31 | with Eval_Fat; use Eval_Fat; | |
32 | with Exp_Ch11; use Exp_Ch11; | |
ee6ba406 | 33 | with Exp_Ch2; use Exp_Ch2; |
df40eeb0 | 34 | with Exp_Ch4; use Exp_Ch4; |
05fcfafb | 35 | with Exp_Pakd; use Exp_Pakd; |
ee6ba406 | 36 | with Exp_Util; use Exp_Util; |
4fb5f0a0 | 37 | with Expander; use Expander; |
ee6ba406 | 38 | with Freeze; use Freeze; |
9dfe12ae | 39 | with Lib; use Lib; |
ee6ba406 | 40 | with Nlists; use Nlists; |
41 | with Nmake; use Nmake; | |
42 | with Opt; use Opt; | |
9dfe12ae | 43 | with Output; use Output; |
c2b56224 | 44 | with Restrict; use Restrict; |
1e16c51c | 45 | with Rident; use Rident; |
ee6ba406 | 46 | with Rtsfind; use Rtsfind; |
47 | with Sem; use Sem; | |
d60c9ff7 | 48 | with Sem_Aux; use Sem_Aux; |
00f91aef | 49 | with Sem_Ch3; use Sem_Ch3; |
9dfe12ae | 50 | with Sem_Ch8; use Sem_Ch8; |
7b8fa048 | 51 | with Sem_Eval; use Sem_Eval; |
ee6ba406 | 52 | with Sem_Res; use Sem_Res; |
53 | with Sem_Util; use Sem_Util; | |
54 | with Sem_Warn; use Sem_Warn; | |
55 | with Sinfo; use Sinfo; | |
9dfe12ae | 56 | with Sinput; use Sinput; |
ee6ba406 | 57 | with Snames; use Snames; |
9dfe12ae | 58 | with Sprint; use Sprint; |
ee6ba406 | 59 | with Stand; use Stand; |
bb569db0 | 60 | with Stringt; use Stringt; |
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 | ||
20cf157b | 87 | -- The above is modified in gnatprove mode to ensure that proper check |
88 | -- flags are always placed, even if expansion is off. | |
89 | ||
9dfe12ae | 90 | ------------------------------------- |
91 | -- Suppression of Redundant Checks -- | |
92 | ------------------------------------- | |
93 | ||
94 | -- This unit implements a limited circuit for removal of redundant | |
95 | -- checks. The processing is based on a tracing of simple sequential | |
96 | -- flow. For any sequence of statements, we save expressions that are | |
97 | -- marked to be checked, and then if the same expression appears later | |
98 | -- with the same check, then under certain circumstances, the second | |
99 | -- check can be suppressed. | |
100 | ||
101 | -- Basically, we can suppress the check if we know for certain that | |
102 | -- the previous expression has been elaborated (together with its | |
103 | -- check), and we know that the exception frame is the same, and that | |
104 | -- nothing has happened to change the result of the exception. | |
105 | ||
106 | -- Let us examine each of these three conditions in turn to describe | |
107 | -- how we ensure that this condition is met. | |
108 | ||
109 | -- First, we need to know for certain that the previous expression has | |
6fb3c314 | 110 | -- been executed. This is done principally by the mechanism of calling |
9dfe12ae | 111 | -- Conditional_Statements_Begin at the start of any statement sequence |
112 | -- and Conditional_Statements_End at the end. The End call causes all | |
113 | -- checks remembered since the Begin call to be discarded. This does | |
114 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
115 | -- no exception handlers. But the important thing is to be conservative. | |
116 | -- The other protection is that all checks are discarded if a label | |
117 | -- is encountered, since then the assumption of sequential execution | |
118 | -- is violated, and we don't know enough about the flow. | |
119 | ||
120 | -- Second, we need to know that the exception frame is the same. We | |
121 | -- do this by killing all remembered checks when we enter a new frame. | |
122 | -- Again, that's over-conservative, but generally the cases we can help | |
123 | -- with are pretty local anyway (like the body of a loop for example). | |
124 | ||
125 | -- Third, we must be sure to forget any checks which are no longer valid. | |
126 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
127 | -- used to note any changes to local variables. We only attempt to deal | |
128 | -- with checks involving local variables, so we do not need to worry | |
129 | -- about global variables. Second, a call to any non-global procedure | |
130 | -- causes us to abandon all stored checks, since such a all may affect | |
131 | -- the values of any local variables. | |
132 | ||
133 | -- The following define the data structures used to deal with remembering | |
134 | -- checks so that redundant checks can be eliminated as described above. | |
135 | ||
136 | -- Right now, the only expressions that we deal with are of the form of | |
137 | -- simple local objects (either declared locally, or IN parameters) or | |
138 | -- such objects plus/minus a compile time known constant. We can do | |
139 | -- more later on if it seems worthwhile, but this catches many simple | |
140 | -- cases in practice. | |
141 | ||
142 | -- The following record type reflects a single saved check. An entry | |
143 | -- is made in the stack of saved checks if and only if the expression | |
144 | -- has been elaborated with the indicated checks. | |
145 | ||
146 | type Saved_Check is record | |
147 | Killed : Boolean; | |
148 | -- Set True if entry is killed by Kill_Checks | |
149 | ||
150 | Entity : Entity_Id; | |
151 | -- The entity involved in the expression that is checked | |
152 | ||
153 | Offset : Uint; | |
154 | -- A compile time value indicating the result of adding or | |
155 | -- subtracting a compile time value. This value is to be | |
156 | -- added to the value of the Entity. A value of zero is | |
157 | -- used for the case of a simple entity reference. | |
158 | ||
159 | Check_Type : Character; | |
160 | -- This is set to 'R' for a range check (in which case Target_Type | |
161 | -- is set to the target type for the range check) or to 'O' for an | |
162 | -- overflow check (in which case Target_Type is set to Empty). | |
163 | ||
164 | Target_Type : Entity_Id; | |
165 | -- Used only if Do_Range_Check is set. Records the target type for | |
166 | -- the check. We need this, because a check is a duplicate only if | |
6fb3c314 | 167 | -- it has the same target type (or more accurately one with a |
9dfe12ae | 168 | -- range that is smaller or equal to the stored target type of a |
169 | -- saved check). | |
170 | end record; | |
171 | ||
172 | -- The following table keeps track of saved checks. Rather than use an | |
173 | -- extensible table. We just use a table of fixed size, and we discard | |
174 | -- any saved checks that do not fit. That's very unlikely to happen and | |
175 | -- this is only an optimization in any case. | |
176 | ||
177 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
178 | -- Array of saved checks | |
179 | ||
180 | Num_Saved_Checks : Nat := 0; | |
181 | -- Number of saved checks | |
182 | ||
183 | -- The following stack keeps track of statement ranges. It is treated | |
184 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
185 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
186 | -- at the time of the call. Then when Conditional_Statements_End is | |
187 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
188 | ||
189 | -- Note: again, this is a fixed length stack with a size that should | |
190 | -- always be fine. If the value of the stack pointer goes above the | |
191 | -- limit, then we just forget all saved checks. | |
192 | ||
193 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
194 | Saved_Checks_TOS : Nat := 0; | |
195 | ||
196 | ----------------------- | |
197 | -- Local Subprograms -- | |
198 | ----------------------- | |
ee6ba406 | 199 | |
0df9d43f | 200 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id); |
3cce7f32 | 201 | -- Used to apply arithmetic overflow checks for all cases except operators |
691fe9e0 | 202 | -- on signed arithmetic types in MINIMIZED/ELIMINATED case (for which we |
0df9d43f | 203 | -- call Apply_Arithmetic_Overflow_Minimized_Eliminated below). N can be a |
204 | -- signed integer arithmetic operator (but not an if or case expression). | |
205 | -- It is also called for types other than signed integers. | |
3cce7f32 | 206 | |
207 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id); | |
208 | -- Used to apply arithmetic overflow checks for the case where the overflow | |
0df9d43f | 209 | -- checking mode is MINIMIZED or ELIMINATED and we have a signed integer |
210 | -- arithmetic op (which includes the case of if and case expressions). Note | |
211 | -- that Do_Overflow_Check may or may not be set for node Op. In these modes | |
212 | -- we have work to do even if overflow checking is suppressed. | |
3cce7f32 | 213 | |
2fe22c69 | 214 | procedure Apply_Division_Check |
215 | (N : Node_Id; | |
216 | Rlo : Uint; | |
217 | Rhi : Uint; | |
218 | ROK : Boolean); | |
219 | -- N is an N_Op_Div, N_Op_Rem, or N_Op_Mod node. This routine applies | |
220 | -- division checks as required if the Do_Division_Check flag is set. | |
221 | -- Rlo and Rhi give the possible range of the right operand, these values | |
222 | -- can be referenced and trusted only if ROK is set True. | |
223 | ||
224 | procedure Apply_Float_Conversion_Check | |
225 | (Ck_Node : Node_Id; | |
226 | Target_Typ : Entity_Id); | |
227 | -- The checks on a conversion from a floating-point type to an integer | |
228 | -- type are delicate. They have to be performed before conversion, they | |
229 | -- have to raise an exception when the operand is a NaN, and rounding must | |
230 | -- be taken into account to determine the safe bounds of the operand. | |
231 | ||
ee6ba406 | 232 | procedure Apply_Selected_Length_Checks |
233 | (Ck_Node : Node_Id; | |
234 | Target_Typ : Entity_Id; | |
235 | Source_Typ : Entity_Id; | |
236 | Do_Static : Boolean); | |
237 | -- This is the subprogram that does all the work for Apply_Length_Check | |
238 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
239 | -- described for the above routines. The Do_Static flag indicates that | |
240 | -- only a static check is to be done. | |
241 | ||
242 | procedure Apply_Selected_Range_Checks | |
243 | (Ck_Node : Node_Id; | |
244 | Target_Typ : Entity_Id; | |
245 | Source_Typ : Entity_Id; | |
246 | Do_Static : Boolean); | |
247 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
248 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
249 | -- routine. The Do_Static flag indicates that only a static check is | |
250 | -- to be done. | |
251 | ||
2af58f67 | 252 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 253 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
254 | -- This function is used to see if an access or division by zero check is | |
255 | -- needed. The check is to be applied to a single variable appearing in the | |
256 | -- source, and N is the node for the reference. If N is not of this form, | |
257 | -- True is returned with no further processing. If N is of the right form, | |
258 | -- then further processing determines if the given Check is needed. | |
259 | -- | |
260 | -- The particular circuit is to see if we have the case of a check that is | |
261 | -- not needed because it appears in the right operand of a short circuited | |
262 | -- conditional where the left operand guards the check. For example: | |
263 | -- | |
264 | -- if Var = 0 or else Q / Var > 12 then | |
265 | -- ... | |
266 | -- end if; | |
267 | -- | |
268 | -- In this example, the division check is not required. At the same time | |
269 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
270 | -- such as: | |
271 | -- | |
272 | -- if Var = 0 or Q / Var > 12 then | |
273 | -- ... | |
274 | -- end if; | |
275 | ||
9dfe12ae | 276 | procedure Find_Check |
277 | (Expr : Node_Id; | |
278 | Check_Type : Character; | |
279 | Target_Type : Entity_Id; | |
280 | Entry_OK : out Boolean; | |
281 | Check_Num : out Nat; | |
282 | Ent : out Entity_Id; | |
283 | Ofs : out Uint); | |
284 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
285 | -- to see if a check is of the form for optimization, and if so, to see | |
286 | -- if it has already been performed. Expr is the expression to check, | |
287 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
288 | -- Target_Type is the target type for a range check, and Empty for an | |
289 | -- overflow check. If the entry is not of the form for optimization, | |
290 | -- then Entry_OK is set to False, and the remaining out parameters | |
291 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
292 | -- entity and offset from the expression. Check_Num is the number of | |
293 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
294 | -- is located. | |
295 | ||
ee6ba406 | 296 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
297 | -- If a discriminal is used in constraining a prival, Return reference | |
298 | -- to the discriminal of the protected body (which renames the parameter | |
299 | -- of the enclosing protected operation). This clumsy transformation is | |
300 | -- needed because privals are created too late and their actual subtypes | |
301 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 302 | -- This function is called whenever the bound is an entity and the scope |
303 | -- indicates a protected operation. If the bound is an in-parameter of | |
304 | -- a protected operation that is not a prival, the function returns the | |
305 | -- bound itself. | |
ee6ba406 | 306 | -- To be cleaned up??? |
307 | ||
308 | function Guard_Access | |
309 | (Cond : Node_Id; | |
310 | Loc : Source_Ptr; | |
314a23b6 | 311 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 312 | -- In the access type case, guard the test with a test to ensure |
313 | -- that the access value is non-null, since the checks do not | |
314 | -- not apply to null access values. | |
315 | ||
316 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
317 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
318 | -- Constraint_Error node. | |
319 | ||
3cce7f32 | 320 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean; |
321 | -- Returns True if node N is for an arithmetic operation with signed | |
0326b4d4 | 322 | -- integer operands. This includes unary and binary operators, and also |
323 | -- if and case expression nodes where the dependent expressions are of | |
324 | -- a signed integer type. These are the kinds of nodes for which special | |
691fe9e0 | 325 | -- handling applies in MINIMIZED or ELIMINATED overflow checking mode. |
3cce7f32 | 326 | |
0577b0b1 | 327 | function Range_Or_Validity_Checks_Suppressed |
328 | (Expr : Node_Id) return Boolean; | |
329 | -- Returns True if either range or validity checks or both are suppressed | |
330 | -- for the type of the given expression, or, if the expression is the name | |
331 | -- of an entity, if these checks are suppressed for the entity. | |
332 | ||
ee6ba406 | 333 | function Selected_Length_Checks |
334 | (Ck_Node : Node_Id; | |
335 | Target_Typ : Entity_Id; | |
336 | Source_Typ : Entity_Id; | |
314a23b6 | 337 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 338 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
339 | -- anything, just returns a list of nodes as described in the spec of | |
340 | -- this package for the Range_Check function. | |
341 | ||
342 | function Selected_Range_Checks | |
343 | (Ck_Node : Node_Id; | |
344 | Target_Typ : Entity_Id; | |
345 | Source_Typ : Entity_Id; | |
314a23b6 | 346 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 347 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
348 | -- just returns a list of nodes as described in the spec of this package | |
349 | -- for the Range_Check function. | |
350 | ||
351 | ------------------------------ | |
352 | -- Access_Checks_Suppressed -- | |
353 | ------------------------------ | |
354 | ||
355 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
356 | begin | |
9dfe12ae | 357 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
358 | return Is_Check_Suppressed (E, Access_Check); | |
359 | else | |
fafc6b97 | 360 | return Scope_Suppress.Suppress (Access_Check); |
9dfe12ae | 361 | end if; |
ee6ba406 | 362 | end Access_Checks_Suppressed; |
363 | ||
364 | ------------------------------------- | |
365 | -- Accessibility_Checks_Suppressed -- | |
366 | ------------------------------------- | |
367 | ||
368 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
369 | begin | |
9dfe12ae | 370 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
371 | return Is_Check_Suppressed (E, Accessibility_Check); | |
372 | else | |
fafc6b97 | 373 | return Scope_Suppress.Suppress (Accessibility_Check); |
9dfe12ae | 374 | end if; |
ee6ba406 | 375 | end Accessibility_Checks_Suppressed; |
376 | ||
00c403ee | 377 | ----------------------------- |
378 | -- Activate_Division_Check -- | |
379 | ----------------------------- | |
380 | ||
381 | procedure Activate_Division_Check (N : Node_Id) is | |
382 | begin | |
383 | Set_Do_Division_Check (N, True); | |
384 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
385 | end Activate_Division_Check; | |
386 | ||
387 | ----------------------------- | |
388 | -- Activate_Overflow_Check -- | |
389 | ----------------------------- | |
390 | ||
391 | procedure Activate_Overflow_Check (N : Node_Id) is | |
392 | begin | |
d32ceaf3 | 393 | if not Nkind_In (N, N_Op_Rem, N_Op_Mod, N_Op_Plus) then |
394 | Set_Do_Overflow_Check (N, True); | |
395 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
396 | end if; | |
00c403ee | 397 | end Activate_Overflow_Check; |
398 | ||
399 | -------------------------- | |
400 | -- Activate_Range_Check -- | |
401 | -------------------------- | |
402 | ||
403 | procedure Activate_Range_Check (N : Node_Id) is | |
404 | begin | |
405 | Set_Do_Range_Check (N, True); | |
406 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
407 | end Activate_Range_Check; | |
408 | ||
0577b0b1 | 409 | --------------------------------- |
410 | -- Alignment_Checks_Suppressed -- | |
411 | --------------------------------- | |
412 | ||
413 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
414 | begin | |
415 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
416 | return Is_Check_Suppressed (E, Alignment_Check); | |
417 | else | |
fafc6b97 | 418 | return Scope_Suppress.Suppress (Alignment_Check); |
0577b0b1 | 419 | end if; |
420 | end Alignment_Checks_Suppressed; | |
421 | ||
2d70530c | 422 | ---------------------------------- |
423 | -- Allocation_Checks_Suppressed -- | |
424 | ---------------------------------- | |
425 | ||
fa771c05 | 426 | -- Note: at the current time there are no calls to this function, because |
427 | -- the relevant check is in the run-time, so it is not a check that the | |
428 | -- compiler can suppress anyway, but we still have to recognize the check | |
429 | -- name Allocation_Check since it is part of the standard. | |
430 | ||
2d70530c | 431 | function Allocation_Checks_Suppressed (E : Entity_Id) return Boolean is |
432 | begin | |
433 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
434 | return Is_Check_Suppressed (E, Allocation_Check); | |
435 | else | |
436 | return Scope_Suppress.Suppress (Allocation_Check); | |
437 | end if; | |
438 | end Allocation_Checks_Suppressed; | |
439 | ||
ee6ba406 | 440 | ------------------------- |
441 | -- Append_Range_Checks -- | |
442 | ------------------------- | |
443 | ||
444 | procedure Append_Range_Checks | |
445 | (Checks : Check_Result; | |
446 | Stmts : List_Id; | |
447 | Suppress_Typ : Entity_Id; | |
448 | Static_Sloc : Source_Ptr; | |
449 | Flag_Node : Node_Id) | |
450 | is | |
9dfe12ae | 451 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
452 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
453 | ||
ee6ba406 | 454 | Checks_On : constant Boolean := |
b6341c67 | 455 | (not Index_Checks_Suppressed (Suppress_Typ)) |
456 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 457 | |
458 | begin | |
459 | -- For now we just return if Checks_On is false, however this should | |
460 | -- be enhanced to check for an always True value in the condition | |
461 | -- and to generate a compilation warning??? | |
462 | ||
463 | if not Checks_On then | |
464 | return; | |
465 | end if; | |
466 | ||
467 | for J in 1 .. 2 loop | |
468 | exit when No (Checks (J)); | |
469 | ||
470 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
471 | and then Present (Condition (Checks (J))) | |
472 | then | |
473 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
474 | Append_To (Stmts, Checks (J)); | |
475 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
476 | end if; | |
477 | ||
478 | else | |
479 | Append_To | |
f15731c4 | 480 | (Stmts, |
481 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
482 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 483 | end if; |
484 | end loop; | |
485 | end Append_Range_Checks; | |
486 | ||
487 | ------------------------ | |
488 | -- Apply_Access_Check -- | |
489 | ------------------------ | |
490 | ||
491 | procedure Apply_Access_Check (N : Node_Id) is | |
492 | P : constant Node_Id := Prefix (N); | |
493 | ||
494 | begin | |
13dbf220 | 495 | -- We do not need checks if we are not generating code (i.e. the |
496 | -- expander is not active). This is not just an optimization, there | |
497 | -- are cases (e.g. with pragma Debug) where generating the checks | |
498 | -- can cause real trouble). | |
284faf8b | 499 | |
a33565dd | 500 | if not Expander_Active then |
13dbf220 | 501 | return; |
9dfe12ae | 502 | end if; |
ee6ba406 | 503 | |
84d0d4a5 | 504 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 505 | |
84d0d4a5 | 506 | if not Check_Needed (P, Access_Check) then |
507 | return; | |
ee6ba406 | 508 | end if; |
9dfe12ae | 509 | |
cc60bd16 | 510 | -- No check if accessing the Offset_To_Top component of a dispatch |
511 | -- table. They are safe by construction. | |
512 | ||
040277b1 | 513 | if Tagged_Type_Expansion |
514 | and then Present (Etype (P)) | |
cc60bd16 | 515 | and then RTU_Loaded (Ada_Tags) |
516 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
517 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
518 | then | |
519 | return; | |
520 | end if; | |
521 | ||
84d0d4a5 | 522 | -- Otherwise go ahead and install the check |
9dfe12ae | 523 | |
fa7497e8 | 524 | Install_Null_Excluding_Check (P); |
ee6ba406 | 525 | end Apply_Access_Check; |
526 | ||
527 | ------------------------------- | |
528 | -- Apply_Accessibility_Check -- | |
529 | ------------------------------- | |
530 | ||
55dc6dc2 | 531 | procedure Apply_Accessibility_Check |
532 | (N : Node_Id; | |
533 | Typ : Entity_Id; | |
534 | Insert_Node : Node_Id) | |
535 | is | |
ee6ba406 | 536 | Loc : constant Source_Ptr := Sloc (N); |
1a9cc6cd | 537 | Param_Ent : Entity_Id := Param_Entity (N); |
ee6ba406 | 538 | Param_Level : Node_Id; |
539 | Type_Level : Node_Id; | |
540 | ||
541 | begin | |
47d210a3 | 542 | if Ada_Version >= Ada_2012 |
543 | and then not Present (Param_Ent) | |
544 | and then Is_Entity_Name (N) | |
545 | and then Ekind_In (Entity (N), E_Constant, E_Variable) | |
546 | and then Present (Effective_Extra_Accessibility (Entity (N))) | |
547 | then | |
548 | Param_Ent := Entity (N); | |
549 | while Present (Renamed_Object (Param_Ent)) loop | |
1a9cc6cd | 550 | |
47d210a3 | 551 | -- Renamed_Object must return an Entity_Name here |
552 | -- because of preceding "Present (E_E_A (...))" test. | |
553 | ||
554 | Param_Ent := Entity (Renamed_Object (Param_Ent)); | |
555 | end loop; | |
556 | end if; | |
557 | ||
ee6ba406 | 558 | if Inside_A_Generic then |
559 | return; | |
560 | ||
6ffc64fc | 561 | -- Only apply the run-time check if the access parameter has an |
562 | -- associated extra access level parameter and when the level of the | |
563 | -- type is less deep than the level of the access parameter, and | |
564 | -- accessibility checks are not suppressed. | |
ee6ba406 | 565 | |
566 | elsif Present (Param_Ent) | |
567 | and then Present (Extra_Accessibility (Param_Ent)) | |
47d210a3 | 568 | and then UI_Gt (Object_Access_Level (N), |
1a9cc6cd | 569 | Deepest_Type_Access_Level (Typ)) |
ee6ba406 | 570 | and then not Accessibility_Checks_Suppressed (Param_Ent) |
571 | and then not Accessibility_Checks_Suppressed (Typ) | |
572 | then | |
573 | Param_Level := | |
574 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
575 | ||
1a9cc6cd | 576 | Type_Level := |
577 | Make_Integer_Literal (Loc, Deepest_Type_Access_Level (Typ)); | |
ee6ba406 | 578 | |
bf3e1520 | 579 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 580 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 581 | |
55dc6dc2 | 582 | Insert_Action (Insert_Node, |
ee6ba406 | 583 | Make_Raise_Program_Error (Loc, |
584 | Condition => | |
585 | Make_Op_Gt (Loc, | |
586 | Left_Opnd => Param_Level, | |
f15731c4 | 587 | Right_Opnd => Type_Level), |
588 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 589 | |
590 | Analyze_And_Resolve (N); | |
591 | end if; | |
592 | end Apply_Accessibility_Check; | |
593 | ||
0577b0b1 | 594 | -------------------------------- |
595 | -- Apply_Address_Clause_Check -- | |
596 | -------------------------------- | |
597 | ||
598 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
d950dc79 | 599 | pragma Assert (Nkind (N) = N_Freeze_Entity); |
600 | ||
0577b0b1 | 601 | AC : constant Node_Id := Address_Clause (E); |
602 | Loc : constant Source_Ptr := Sloc (AC); | |
603 | Typ : constant Entity_Id := Etype (E); | |
604 | Aexp : constant Node_Id := Expression (AC); | |
c2b56224 | 605 | |
c2b56224 | 606 | Expr : Node_Id; |
0577b0b1 | 607 | -- Address expression (not necessarily the same as Aexp, for example |
608 | -- when Aexp is a reference to a constant, in which case Expr gets | |
7b8fa048 | 609 | -- reset to reference the value expression of the constant). |
0577b0b1 | 610 | |
0577b0b1 | 611 | procedure Compile_Time_Bad_Alignment; |
612 | -- Post error warnings when alignment is known to be incompatible. Note | |
613 | -- that we do not go as far as inserting a raise of Program_Error since | |
614 | -- this is an erroneous case, and it may happen that we are lucky and an | |
d6da7448 | 615 | -- underaligned address turns out to be OK after all. |
0577b0b1 | 616 | |
617 | -------------------------------- | |
618 | -- Compile_Time_Bad_Alignment -- | |
619 | -------------------------------- | |
620 | ||
621 | procedure Compile_Time_Bad_Alignment is | |
622 | begin | |
d6da7448 | 623 | if Address_Clause_Overlay_Warnings then |
0577b0b1 | 624 | Error_Msg_FE |
cb97ae5c | 625 | ("?o?specified address for& may be inconsistent with alignment", |
0577b0b1 | 626 | Aexp, E); |
627 | Error_Msg_FE | |
cb97ae5c | 628 | ("\?o?program execution may be erroneous (RM 13.3(27))", |
0577b0b1 | 629 | Aexp, E); |
83f8f0a6 | 630 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 631 | end if; |
632 | end Compile_Time_Bad_Alignment; | |
c2b56224 | 633 | |
2af58f67 | 634 | -- Start of processing for Apply_Address_Clause_Check |
5c61a0ff | 635 | |
c2b56224 | 636 | begin |
d6da7448 | 637 | -- See if alignment check needed. Note that we never need a check if the |
638 | -- maximum alignment is one, since the check will always succeed. | |
639 | ||
640 | -- Note: we do not check for checks suppressed here, since that check | |
641 | -- was done in Sem_Ch13 when the address clause was processed. We are | |
642 | -- only called if checks were not suppressed. The reason for this is | |
643 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
644 | -- time (so that all types etc are elaborated), but we have to check | |
645 | -- the status of check suppressing at the point of the address clause. | |
646 | ||
647 | if No (AC) | |
648 | or else not Check_Address_Alignment (AC) | |
649 | or else Maximum_Alignment = 1 | |
650 | then | |
651 | return; | |
652 | end if; | |
653 | ||
654 | -- Obtain expression from address clause | |
9dfe12ae | 655 | |
0577b0b1 | 656 | Expr := Expression (AC); |
657 | ||
658 | -- The following loop digs for the real expression to use in the check | |
659 | ||
660 | loop | |
661 | -- For constant, get constant expression | |
662 | ||
663 | if Is_Entity_Name (Expr) | |
664 | and then Ekind (Entity (Expr)) = E_Constant | |
665 | then | |
666 | Expr := Constant_Value (Entity (Expr)); | |
667 | ||
668 | -- For unchecked conversion, get result to convert | |
669 | ||
670 | elsif Nkind (Expr) = N_Unchecked_Type_Conversion then | |
671 | Expr := Expression (Expr); | |
672 | ||
673 | -- For (common case) of To_Address call, get argument | |
674 | ||
675 | elsif Nkind (Expr) = N_Function_Call | |
676 | and then Is_Entity_Name (Name (Expr)) | |
677 | and then Is_RTE (Entity (Name (Expr)), RE_To_Address) | |
678 | then | |
679 | Expr := First (Parameter_Associations (Expr)); | |
680 | ||
681 | if Nkind (Expr) = N_Parameter_Association then | |
682 | Expr := Explicit_Actual_Parameter (Expr); | |
683 | end if; | |
684 | ||
685 | -- We finally have the real expression | |
686 | ||
687 | else | |
688 | exit; | |
689 | end if; | |
690 | end loop; | |
691 | ||
d6da7448 | 692 | -- See if we know that Expr has a bad alignment at compile time |
c2b56224 | 693 | |
694 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 695 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 696 | then |
f2a06be9 | 697 | declare |
698 | AL : Uint := Alignment (Typ); | |
699 | ||
700 | begin | |
701 | -- The object alignment might be more restrictive than the | |
702 | -- type alignment. | |
703 | ||
704 | if Known_Alignment (E) then | |
705 | AL := Alignment (E); | |
706 | end if; | |
707 | ||
708 | if Expr_Value (Expr) mod AL /= 0 then | |
0577b0b1 | 709 | Compile_Time_Bad_Alignment; |
710 | else | |
711 | return; | |
f2a06be9 | 712 | end if; |
713 | end; | |
c2b56224 | 714 | |
0577b0b1 | 715 | -- If the expression has the form X'Address, then we can find out if |
716 | -- the object X has an alignment that is compatible with the object E. | |
d6da7448 | 717 | -- If it hasn't or we don't know, we defer issuing the warning until |
718 | -- the end of the compilation to take into account back end annotations. | |
c2b56224 | 719 | |
0577b0b1 | 720 | elsif Nkind (Expr) = N_Attribute_Reference |
721 | and then Attribute_Name (Expr) = Name_Address | |
d6da7448 | 722 | and then Has_Compatible_Alignment (E, Prefix (Expr)) = Known_Compatible |
0577b0b1 | 723 | then |
d6da7448 | 724 | return; |
0577b0b1 | 725 | end if; |
c2b56224 | 726 | |
6fb3c314 | 727 | -- Here we do not know if the value is acceptable. Strictly we don't |
728 | -- have to do anything, since if the alignment is bad, we have an | |
729 | -- erroneous program. However we are allowed to check for erroneous | |
730 | -- conditions and we decide to do this by default if the check is not | |
731 | -- suppressed. | |
0577b0b1 | 732 | |
733 | -- However, don't do the check if elaboration code is unwanted | |
734 | ||
735 | if Restriction_Active (No_Elaboration_Code) then | |
736 | return; | |
737 | ||
738 | -- Generate a check to raise PE if alignment may be inappropriate | |
739 | ||
740 | else | |
741 | -- If the original expression is a non-static constant, use the | |
742 | -- name of the constant itself rather than duplicating its | |
00c403ee | 743 | -- defining expression, which was extracted above. |
0577b0b1 | 744 | |
00c403ee | 745 | -- Note: Expr is empty if the address-clause is applied to in-mode |
746 | -- actuals (allowed by 13.1(22)). | |
747 | ||
748 | if not Present (Expr) | |
749 | or else | |
750 | (Is_Entity_Name (Expression (AC)) | |
751 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
752 | and then Nkind (Parent (Entity (Expression (AC)))) | |
753 | = N_Object_Declaration) | |
0577b0b1 | 754 | then |
755 | Expr := New_Copy_Tree (Expression (AC)); | |
756 | else | |
757 | Remove_Side_Effects (Expr); | |
c2b56224 | 758 | end if; |
c2b56224 | 759 | |
d950dc79 | 760 | if No (Actions (N)) then |
761 | Set_Actions (N, New_List); | |
762 | end if; | |
763 | ||
764 | Prepend_To (Actions (N), | |
0577b0b1 | 765 | Make_Raise_Program_Error (Loc, |
766 | Condition => | |
767 | Make_Op_Ne (Loc, | |
768 | Left_Opnd => | |
769 | Make_Op_Mod (Loc, | |
770 | Left_Opnd => | |
771 | Unchecked_Convert_To | |
772 | (RTE (RE_Integer_Address), Expr), | |
773 | Right_Opnd => | |
774 | Make_Attribute_Reference (Loc, | |
d950dc79 | 775 | Prefix => New_Occurrence_Of (E, Loc), |
0577b0b1 | 776 | Attribute_Name => Name_Alignment)), |
777 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
7b8fa048 | 778 | Reason => PE_Misaligned_Address_Value)); |
779 | ||
780 | Warning_Msg := No_Error_Msg; | |
d950dc79 | 781 | Analyze (First (Actions (N)), Suppress => All_Checks); |
cd309f05 | 782 | |
7b8fa048 | 783 | -- If the address clause generated a warning message (for example, |
784 | -- from Warn_On_Non_Local_Exception mode with the active restriction | |
785 | -- No_Exception_Propagation). | |
786 | ||
787 | if Warning_Msg /= No_Error_Msg then | |
788 | ||
789 | -- If the expression has a known at compile time value, then | |
790 | -- once we know the alignment of the type, we can check if the | |
791 | -- exception will be raised or not, and if not, we don't need | |
792 | -- the warning so we will kill the warning later on. | |
793 | ||
794 | if Compile_Time_Known_Value (Expr) then | |
795 | Alignment_Warnings.Append | |
796 | ((E => E, A => Expr_Value (Expr), W => Warning_Msg)); | |
797 | end if; | |
798 | ||
799 | -- Add explanation of the warning that is generated by the check | |
cd309f05 | 800 | |
78be29d1 | 801 | Error_Msg_N |
7b8fa048 | 802 | ("\address value may be incompatible with alignment " |
803 | & "of object?X?", AC); | |
cd309f05 | 804 | end if; |
78be29d1 | 805 | |
0577b0b1 | 806 | return; |
807 | end if; | |
9dfe12ae | 808 | |
809 | exception | |
0577b0b1 | 810 | -- If we have some missing run time component in configurable run time |
811 | -- mode then just skip the check (it is not required in any case). | |
812 | ||
9dfe12ae | 813 | when RE_Not_Available => |
814 | return; | |
0577b0b1 | 815 | end Apply_Address_Clause_Check; |
c2b56224 | 816 | |
ee6ba406 | 817 | ------------------------------------- |
818 | -- Apply_Arithmetic_Overflow_Check -- | |
819 | ------------------------------------- | |
820 | ||
3cce7f32 | 821 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is |
822 | begin | |
823 | -- Use old routine in almost all cases (the only case we are treating | |
21a55437 | 824 | -- specially is the case of a signed integer arithmetic op with the |
0df9d43f | 825 | -- overflow checking mode set to MINIMIZED or ELIMINATED). |
3cce7f32 | 826 | |
0df9d43f | 827 | if Overflow_Check_Mode = Strict |
3cce7f32 | 828 | or else not Is_Signed_Integer_Arithmetic_Op (N) |
829 | then | |
0df9d43f | 830 | Apply_Arithmetic_Overflow_Strict (N); |
3cce7f32 | 831 | |
21a55437 | 832 | -- Otherwise use the new routine for the case of a signed integer |
833 | -- arithmetic op, with Do_Overflow_Check set to True, and the checking | |
834 | -- mode is MINIMIZED or ELIMINATED. | |
3cce7f32 | 835 | |
836 | else | |
837 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
838 | end if; | |
839 | end Apply_Arithmetic_Overflow_Check; | |
840 | ||
0df9d43f | 841 | -------------------------------------- |
842 | -- Apply_Arithmetic_Overflow_Strict -- | |
843 | -------------------------------------- | |
3cce7f32 | 844 | |
f40f9731 | 845 | -- This routine is called only if the type is an integer type, and a |
846 | -- software arithmetic overflow check may be needed for op (add, subtract, | |
847 | -- or multiply). This check is performed only if Software_Overflow_Checking | |
848 | -- is enabled and Do_Overflow_Check is set. In this case we expand the | |
849 | -- operation into a more complex sequence of tests that ensures that | |
850 | -- overflow is properly caught. | |
ee6ba406 | 851 | |
0df9d43f | 852 | -- This is used in CHECKED modes. It is identical to the code for this |
853 | -- cases before the big overflow earthquake, thus ensuring that in this | |
854 | -- modes we have compatible behavior (and reliability) to what was there | |
855 | -- before. It is also called for types other than signed integers, and if | |
856 | -- the Do_Overflow_Check flag is off. | |
3cce7f32 | 857 | |
858 | -- Note: we also call this routine if we decide in the MINIMIZED case | |
859 | -- to give up and just generate an overflow check without any fuss. | |
860 | ||
0df9d43f | 861 | procedure Apply_Arithmetic_Overflow_Strict (N : Node_Id) is |
21a55437 | 862 | Loc : constant Source_Ptr := Sloc (N); |
863 | Typ : constant Entity_Id := Etype (N); | |
864 | Rtyp : constant Entity_Id := Root_Type (Typ); | |
ee6ba406 | 865 | |
866 | begin | |
0df9d43f | 867 | -- Nothing to do if Do_Overflow_Check not set or overflow checks |
868 | -- suppressed. | |
869 | ||
870 | if not Do_Overflow_Check (N) then | |
871 | return; | |
872 | end if; | |
873 | ||
f40f9731 | 874 | -- An interesting special case. If the arithmetic operation appears as |
875 | -- the operand of a type conversion: | |
876 | ||
877 | -- type1 (x op y) | |
878 | ||
879 | -- and all the following conditions apply: | |
880 | ||
881 | -- arithmetic operation is for a signed integer type | |
882 | -- target type type1 is a static integer subtype | |
883 | -- range of x and y are both included in the range of type1 | |
884 | -- range of x op y is included in the range of type1 | |
885 | -- size of type1 is at least twice the result size of op | |
886 | ||
887 | -- then we don't do an overflow check in any case, instead we transform | |
888 | -- the operation so that we end up with: | |
889 | ||
890 | -- type1 (type1 (x) op type1 (y)) | |
891 | ||
892 | -- This avoids intermediate overflow before the conversion. It is | |
893 | -- explicitly permitted by RM 3.5.4(24): | |
894 | ||
895 | -- For the execution of a predefined operation of a signed integer | |
896 | -- type, the implementation need not raise Constraint_Error if the | |
897 | -- result is outside the base range of the type, so long as the | |
898 | -- correct result is produced. | |
899 | ||
900 | -- It's hard to imagine that any programmer counts on the exception | |
901 | -- being raised in this case, and in any case it's wrong coding to | |
902 | -- have this expectation, given the RM permission. Furthermore, other | |
903 | -- Ada compilers do allow such out of range results. | |
904 | ||
905 | -- Note that we do this transformation even if overflow checking is | |
906 | -- off, since this is precisely about giving the "right" result and | |
907 | -- avoiding the need for an overflow check. | |
908 | ||
8eb4a5eb | 909 | -- Note: this circuit is partially redundant with respect to the similar |
910 | -- processing in Exp_Ch4.Expand_N_Type_Conversion, but the latter deals | |
911 | -- with cases that do not come through here. We still need the following | |
912 | -- processing even with the Exp_Ch4 code in place, since we want to be | |
913 | -- sure not to generate the arithmetic overflow check in these cases | |
914 | -- (Exp_Ch4 would have a hard time removing them once generated). | |
915 | ||
f40f9731 | 916 | if Is_Signed_Integer_Type (Typ) |
917 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 918 | then |
f32c377d | 919 | Conversion_Optimization : declare |
f40f9731 | 920 | Target_Type : constant Entity_Id := |
b6341c67 | 921 | Base_Type (Entity (Subtype_Mark (Parent (N)))); |
f40f9731 | 922 | |
923 | Llo, Lhi : Uint; | |
924 | Rlo, Rhi : Uint; | |
925 | LOK, ROK : Boolean; | |
926 | ||
927 | Vlo : Uint; | |
928 | Vhi : Uint; | |
929 | VOK : Boolean; | |
930 | ||
931 | Tlo : Uint; | |
932 | Thi : Uint; | |
933 | ||
934 | begin | |
935 | if Is_Integer_Type (Target_Type) | |
936 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
937 | then | |
938 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
939 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
940 | ||
9c486805 | 941 | Determine_Range |
942 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
943 | Determine_Range | |
944 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 945 | |
946 | if (LOK and ROK) | |
947 | and then Tlo <= Llo and then Lhi <= Thi | |
948 | and then Tlo <= Rlo and then Rhi <= Thi | |
949 | then | |
9c486805 | 950 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 951 | |
952 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
953 | Rewrite (Left_Opnd (N), | |
954 | Make_Type_Conversion (Loc, | |
955 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
956 | Expression => Relocate_Node (Left_Opnd (N)))); | |
957 | ||
958 | Rewrite (Right_Opnd (N), | |
959 | Make_Type_Conversion (Loc, | |
960 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
961 | Expression => Relocate_Node (Right_Opnd (N)))); | |
962 | ||
780bfb21 | 963 | -- Rewrite the conversion operand so that the original |
964 | -- node is retained, in order to avoid the warning for | |
965 | -- redundant conversions in Resolve_Type_Conversion. | |
966 | ||
967 | Rewrite (N, Relocate_Node (N)); | |
968 | ||
f40f9731 | 969 | Set_Etype (N, Target_Type); |
780bfb21 | 970 | |
f40f9731 | 971 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); |
972 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
973 | ||
974 | -- Given that the target type is twice the size of the | |
975 | -- source type, overflow is now impossible, so we can | |
976 | -- safely kill the overflow check and return. | |
977 | ||
978 | Set_Do_Overflow_Check (N, False); | |
979 | return; | |
980 | end if; | |
981 | end if; | |
982 | end if; | |
f32c377d | 983 | end Conversion_Optimization; |
ee6ba406 | 984 | end if; |
985 | ||
f40f9731 | 986 | -- Now see if an overflow check is required |
987 | ||
988 | declare | |
989 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
990 | Dsiz : constant Int := Siz * 2; | |
991 | Opnod : Node_Id; | |
992 | Ctyp : Entity_Id; | |
993 | Opnd : Node_Id; | |
994 | Cent : RE_Id; | |
ee6ba406 | 995 | |
f40f9731 | 996 | begin |
997 | -- Skip check if back end does overflow checks, or the overflow flag | |
df40eeb0 | 998 | -- is not set anyway, or we are not doing code expansion, or the |
999 | -- parent node is a type conversion whose operand is an arithmetic | |
1000 | -- operation on signed integers on which the expander can promote | |
bbbed24b | 1001 | -- later the operands to type Integer (see Expand_N_Type_Conversion). |
ee6ba406 | 1002 | |
f40f9731 | 1003 | -- Special case CLI target, where arithmetic overflow checks can be |
1004 | -- performed for integer and long_integer | |
ee6ba406 | 1005 | |
f40f9731 | 1006 | if Backend_Overflow_Checks_On_Target |
1007 | or else not Do_Overflow_Check (N) | |
a33565dd | 1008 | or else not Expander_Active |
df40eeb0 | 1009 | or else (Present (Parent (N)) |
1010 | and then Nkind (Parent (N)) = N_Type_Conversion | |
1011 | and then Integer_Promotion_Possible (Parent (N))) | |
f40f9731 | 1012 | or else |
1013 | (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size) | |
1014 | then | |
1015 | return; | |
1016 | end if; | |
ee6ba406 | 1017 | |
f40f9731 | 1018 | -- Otherwise, generate the full general code for front end overflow |
1019 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 1020 | |
f40f9731 | 1021 | -- x op y |
ee6ba406 | 1022 | |
f40f9731 | 1023 | -- is expanded into |
ee6ba406 | 1024 | |
f40f9731 | 1025 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 1026 | |
f40f9731 | 1027 | -- where Typ is the type of the original expression, and Checktyp is |
1028 | -- an integer type of sufficient length to hold the largest possible | |
1029 | -- result. | |
ee6ba406 | 1030 | |
f40f9731 | 1031 | -- If the size of check type exceeds the size of Long_Long_Integer, |
1032 | -- we use a different approach, expanding to: | |
ee6ba406 | 1033 | |
f40f9731 | 1034 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 1035 | |
f40f9731 | 1036 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 1037 | |
f40f9731 | 1038 | -- Find check type if one exists |
1039 | ||
1040 | if Dsiz <= Standard_Integer_Size then | |
1041 | Ctyp := Standard_Integer; | |
ee6ba406 | 1042 | |
f40f9731 | 1043 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
1044 | Ctyp := Standard_Long_Long_Integer; | |
1045 | ||
c9f84db7 | 1046 | -- No check type exists, use runtime call |
ee6ba406 | 1047 | |
1048 | else | |
f40f9731 | 1049 | if Nkind (N) = N_Op_Add then |
1050 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 1051 | |
f40f9731 | 1052 | elsif Nkind (N) = N_Op_Multiply then |
1053 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 1054 | |
f40f9731 | 1055 | else |
1056 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
1057 | Cent := RE_Subtract_With_Ovflo_Check; | |
1058 | end if; | |
1059 | ||
1060 | Rewrite (N, | |
1061 | OK_Convert_To (Typ, | |
1062 | Make_Function_Call (Loc, | |
83c6c069 | 1063 | Name => New_Occurrence_Of (RTE (Cent), Loc), |
f40f9731 | 1064 | Parameter_Associations => New_List ( |
1065 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
1066 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 1067 | |
f40f9731 | 1068 | Analyze_And_Resolve (N, Typ); |
1069 | return; | |
1070 | end if; | |
ee6ba406 | 1071 | |
f40f9731 | 1072 | -- If we fall through, we have the case where we do the arithmetic |
1073 | -- in the next higher type and get the check by conversion. In these | |
1074 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 1075 | |
f40f9731 | 1076 | Opnod := Relocate_Node (N); |
ee6ba406 | 1077 | |
f40f9731 | 1078 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 1079 | |
f40f9731 | 1080 | Analyze (Opnd); |
1081 | Set_Etype (Opnd, Ctyp); | |
1082 | Set_Analyzed (Opnd, True); | |
1083 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 1084 | |
f40f9731 | 1085 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 1086 | |
f40f9731 | 1087 | Analyze (Opnd); |
1088 | Set_Etype (Opnd, Ctyp); | |
1089 | Set_Analyzed (Opnd, True); | |
1090 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 1091 | |
f40f9731 | 1092 | -- The type of the operation changes to the base type of the check |
1093 | -- type, and we reset the overflow check indication, since clearly no | |
1094 | -- overflow is possible now that we are using a double length type. | |
1095 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
1096 | -- expand the node. | |
ee6ba406 | 1097 | |
f40f9731 | 1098 | Set_Etype (Opnod, Base_Type (Ctyp)); |
1099 | Set_Do_Overflow_Check (Opnod, False); | |
1100 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 1101 | |
f40f9731 | 1102 | -- Now build the outer conversion |
ee6ba406 | 1103 | |
f40f9731 | 1104 | Opnd := OK_Convert_To (Typ, Opnod); |
1105 | Analyze (Opnd); | |
1106 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1107 | |
f40f9731 | 1108 | -- In the discrete type case, we directly generate the range check |
1109 | -- for the outer operand. This range check will implement the | |
1110 | -- required overflow check. | |
9dfe12ae | 1111 | |
f40f9731 | 1112 | if Is_Discrete_Type (Typ) then |
1113 | Rewrite (N, Opnd); | |
1114 | Generate_Range_Check | |
1115 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1116 | |
f40f9731 | 1117 | -- For other types, we enable overflow checking on the conversion, |
1118 | -- after setting the node as analyzed to prevent recursive attempts | |
1119 | -- to expand the conversion node. | |
9dfe12ae | 1120 | |
f40f9731 | 1121 | else |
1122 | Set_Analyzed (Opnd, True); | |
1123 | Enable_Overflow_Check (Opnd); | |
1124 | Rewrite (N, Opnd); | |
1125 | end if; | |
1126 | ||
1127 | exception | |
1128 | when RE_Not_Available => | |
1129 | return; | |
1130 | end; | |
0df9d43f | 1131 | end Apply_Arithmetic_Overflow_Strict; |
3cce7f32 | 1132 | |
1133 | ---------------------------------------------------- | |
1134 | -- Apply_Arithmetic_Overflow_Minimized_Eliminated -- | |
1135 | ---------------------------------------------------- | |
1136 | ||
1137 | procedure Apply_Arithmetic_Overflow_Minimized_Eliminated (Op : Node_Id) is | |
1138 | pragma Assert (Is_Signed_Integer_Arithmetic_Op (Op)); | |
3cce7f32 | 1139 | |
1140 | Loc : constant Source_Ptr := Sloc (Op); | |
1141 | P : constant Node_Id := Parent (Op); | |
1142 | ||
49b3a812 | 1143 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
1144 | -- Operands and results are of this type when we convert | |
1145 | ||
3cce7f32 | 1146 | Result_Type : constant Entity_Id := Etype (Op); |
1147 | -- Original result type | |
1148 | ||
db415383 | 1149 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 1150 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
1151 | ||
1152 | Lo, Hi : Uint; | |
1153 | -- Ranges of values for result | |
1154 | ||
1155 | begin | |
1156 | -- Nothing to do if our parent is one of the following: | |
1157 | ||
0326b4d4 | 1158 | -- Another signed integer arithmetic op |
3cce7f32 | 1159 | -- A membership operation |
1160 | -- A comparison operation | |
1161 | ||
1162 | -- In all these cases, we will process at the higher level (and then | |
1163 | -- this node will be processed during the downwards recursion that | |
0df9d43f | 1164 | -- is part of the processing in Minimize_Eliminate_Overflows). |
3cce7f32 | 1165 | |
1166 | if Is_Signed_Integer_Arithmetic_Op (P) | |
b8a17a21 | 1167 | or else Nkind (P) in N_Membership_Test |
1168 | or else Nkind (P) in N_Op_Compare | |
aa4b16cb | 1169 | |
70a2dff4 | 1170 | -- This is also true for an alternative in a case expression |
1171 | ||
1172 | or else Nkind (P) = N_Case_Expression_Alternative | |
1173 | ||
1174 | -- This is also true for a range operand in a membership test | |
aa4b16cb | 1175 | |
b8a17a21 | 1176 | or else (Nkind (P) = N_Range |
1177 | and then Nkind (Parent (P)) in N_Membership_Test) | |
3cce7f32 | 1178 | then |
1179 | return; | |
1180 | end if; | |
1181 | ||
0326b4d4 | 1182 | -- Otherwise, we have a top level arithmetic operation node, and this |
21a55437 | 1183 | -- is where we commence the special processing for MINIMIZED/ELIMINATED |
1184 | -- modes. This is the case where we tell the machinery not to move into | |
1185 | -- Bignum mode at this top level (of course the top level operation | |
1186 | -- will still be in Bignum mode if either of its operands are of type | |
1187 | -- Bignum). | |
3cce7f32 | 1188 | |
0df9d43f | 1189 | Minimize_Eliminate_Overflows (Op, Lo, Hi, Top_Level => True); |
3cce7f32 | 1190 | |
1191 | -- That call may but does not necessarily change the result type of Op. | |
1192 | -- It is the job of this routine to undo such changes, so that at the | |
1193 | -- top level, we have the proper type. This "undoing" is a point at | |
1194 | -- which a final overflow check may be applied. | |
1195 | ||
f32c377d | 1196 | -- If the result type was not fiddled we are all set. We go to base |
1197 | -- types here because things may have been rewritten to generate the | |
1198 | -- base type of the operand types. | |
3cce7f32 | 1199 | |
f32c377d | 1200 | if Base_Type (Etype (Op)) = Base_Type (Result_Type) then |
3cce7f32 | 1201 | return; |
1202 | ||
1203 | -- Bignum case | |
1204 | ||
49b3a812 | 1205 | elsif Is_RTE (Etype (Op), RE_Bignum) then |
3cce7f32 | 1206 | |
d94b5da2 | 1207 | -- We need a sequence that looks like: |
3cce7f32 | 1208 | |
1209 | -- Rnn : Result_Type; | |
1210 | ||
1211 | -- declare | |
d94b5da2 | 1212 | -- M : Mark_Id := SS_Mark; |
3cce7f32 | 1213 | -- begin |
49b3a812 | 1214 | -- Rnn := Long_Long_Integer'Base (From_Bignum (Op)); |
3cce7f32 | 1215 | -- SS_Release (M); |
1216 | -- end; | |
1217 | ||
1218 | -- This block is inserted (using Insert_Actions), and then the node | |
1219 | -- is replaced with a reference to Rnn. | |
1220 | ||
1221 | -- A special case arises if our parent is a conversion node. In this | |
1222 | -- case no point in generating a conversion to Result_Type, we will | |
1223 | -- let the parent handle this. Note that this special case is not | |
1224 | -- just about optimization. Consider | |
1225 | ||
1226 | -- A,B,C : Integer; | |
1227 | -- ... | |
49b3a812 | 1228 | -- X := Long_Long_Integer'Base (A * (B ** C)); |
3cce7f32 | 1229 | |
1230 | -- Now the product may fit in Long_Long_Integer but not in Integer. | |
21a55437 | 1231 | -- In MINIMIZED/ELIMINATED mode, we don't want to introduce an |
1232 | -- overflow exception for this intermediate value. | |
3cce7f32 | 1233 | |
1234 | declare | |
49b3a812 | 1235 | Blk : constant Node_Id := Make_Bignum_Block (Loc); |
3cce7f32 | 1236 | Rnn : constant Entity_Id := Make_Temporary (Loc, 'R', Op); |
1237 | RHS : Node_Id; | |
1238 | ||
1239 | Rtype : Entity_Id; | |
1240 | ||
1241 | begin | |
1242 | RHS := Convert_From_Bignum (Op); | |
1243 | ||
1244 | if Nkind (P) /= N_Type_Conversion then | |
49b3a812 | 1245 | Convert_To_And_Rewrite (Result_Type, RHS); |
3cce7f32 | 1246 | Rtype := Result_Type; |
1247 | ||
1248 | -- Interesting question, do we need a check on that conversion | |
1249 | -- operation. Answer, not if we know the result is in range. | |
1250 | -- At the moment we are not taking advantage of this. To be | |
1251 | -- looked at later ??? | |
1252 | ||
1253 | else | |
49b3a812 | 1254 | Rtype := LLIB; |
3cce7f32 | 1255 | end if; |
1256 | ||
1257 | Insert_Before | |
1258 | (First (Statements (Handled_Statement_Sequence (Blk))), | |
1259 | Make_Assignment_Statement (Loc, | |
1260 | Name => New_Occurrence_Of (Rnn, Loc), | |
1261 | Expression => RHS)); | |
1262 | ||
1263 | Insert_Actions (Op, New_List ( | |
1264 | Make_Object_Declaration (Loc, | |
1265 | Defining_Identifier => Rnn, | |
1266 | Object_Definition => New_Occurrence_Of (Rtype, Loc)), | |
1267 | Blk)); | |
1268 | ||
1269 | Rewrite (Op, New_Occurrence_Of (Rnn, Loc)); | |
1270 | Analyze_And_Resolve (Op); | |
1271 | end; | |
1272 | ||
412f75eb | 1273 | -- Here we know the result is Long_Long_Integer'Base, of that it has |
1274 | -- been rewritten because the parent operation is a conversion. See | |
0df9d43f | 1275 | -- Apply_Arithmetic_Overflow_Strict.Conversion_Optimization. |
3cce7f32 | 1276 | |
1277 | else | |
f32c377d | 1278 | pragma Assert |
1279 | (Etype (Op) = LLIB or else Nkind (Parent (Op)) = N_Type_Conversion); | |
3cce7f32 | 1280 | |
1281 | -- All we need to do here is to convert the result to the proper | |
1282 | -- result type. As explained above for the Bignum case, we can | |
1283 | -- omit this if our parent is a type conversion. | |
1284 | ||
1285 | if Nkind (P) /= N_Type_Conversion then | |
1286 | Convert_To_And_Rewrite (Result_Type, Op); | |
1287 | end if; | |
1288 | ||
1289 | Analyze_And_Resolve (Op); | |
1290 | end if; | |
1291 | end Apply_Arithmetic_Overflow_Minimized_Eliminated; | |
ee6ba406 | 1292 | |
ee6ba406 | 1293 | ---------------------------- |
1294 | -- Apply_Constraint_Check -- | |
1295 | ---------------------------- | |
1296 | ||
1297 | procedure Apply_Constraint_Check | |
1298 | (N : Node_Id; | |
1299 | Typ : Entity_Id; | |
1300 | No_Sliding : Boolean := False) | |
1301 | is | |
1302 | Desig_Typ : Entity_Id; | |
1303 | ||
1304 | begin | |
7aafae1c | 1305 | -- No checks inside a generic (check the instantiations) |
1306 | ||
ee6ba406 | 1307 | if Inside_A_Generic then |
1308 | return; | |
7aafae1c | 1309 | end if; |
ee6ba406 | 1310 | |
6fb3c314 | 1311 | -- Apply required constraint checks |
7aafae1c | 1312 | |
1313 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 1314 | Apply_Scalar_Range_Check (N, Typ); |
1315 | ||
1316 | elsif Is_Array_Type (Typ) then | |
1317 | ||
05fcfafb | 1318 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1319 | -- always has the right bounds. |
1320 | ||
1321 | if Nkind (N) = N_Aggregate | |
1322 | and then No (Expressions (N)) | |
1323 | and then Nkind | |
1324 | (First (Choices (First (Component_Associations (N))))) | |
1325 | = N_Others_Choice | |
1326 | then | |
1327 | return; | |
1328 | end if; | |
1329 | ||
ee6ba406 | 1330 | if Is_Constrained (Typ) then |
1331 | Apply_Length_Check (N, Typ); | |
1332 | ||
1333 | if No_Sliding then | |
1334 | Apply_Range_Check (N, Typ); | |
1335 | end if; | |
1336 | else | |
1337 | Apply_Range_Check (N, Typ); | |
1338 | end if; | |
1339 | ||
4fb5f0a0 | 1340 | elsif (Is_Record_Type (Typ) or else Is_Private_Type (Typ)) |
ee6ba406 | 1341 | and then Has_Discriminants (Base_Type (Typ)) |
1342 | and then Is_Constrained (Typ) | |
1343 | then | |
1344 | Apply_Discriminant_Check (N, Typ); | |
1345 | ||
1346 | elsif Is_Access_Type (Typ) then | |
1347 | ||
1348 | Desig_Typ := Designated_Type (Typ); | |
1349 | ||
1350 | -- No checks necessary if expression statically null | |
1351 | ||
2af58f67 | 1352 | if Known_Null (N) then |
00c403ee | 1353 | if Can_Never_Be_Null (Typ) then |
1354 | Install_Null_Excluding_Check (N); | |
1355 | end if; | |
ee6ba406 | 1356 | |
1357 | -- No sliding possible on access to arrays | |
1358 | ||
1359 | elsif Is_Array_Type (Desig_Typ) then | |
1360 | if Is_Constrained (Desig_Typ) then | |
1361 | Apply_Length_Check (N, Typ); | |
1362 | end if; | |
1363 | ||
1364 | Apply_Range_Check (N, Typ); | |
1365 | ||
1366 | elsif Has_Discriminants (Base_Type (Desig_Typ)) | |
1367 | and then Is_Constrained (Desig_Typ) | |
1368 | then | |
1369 | Apply_Discriminant_Check (N, Typ); | |
1370 | end if; | |
fa7497e8 | 1371 | |
bf3e1520 | 1372 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1373 | -- this check if the constraint node is illegal, as shown by having |
1374 | -- an error posted. This additional guard prevents cascaded errors | |
1375 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1376 | ||
fa7497e8 | 1377 | if Can_Never_Be_Null (Typ) |
1378 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1379 | and then not Error_Posted (N) |
fa7497e8 | 1380 | then |
1381 | Install_Null_Excluding_Check (N); | |
1382 | end if; | |
ee6ba406 | 1383 | end if; |
1384 | end Apply_Constraint_Check; | |
1385 | ||
1386 | ------------------------------ | |
1387 | -- Apply_Discriminant_Check -- | |
1388 | ------------------------------ | |
1389 | ||
1390 | procedure Apply_Discriminant_Check | |
1391 | (N : Node_Id; | |
1392 | Typ : Entity_Id; | |
1393 | Lhs : Node_Id := Empty) | |
1394 | is | |
1395 | Loc : constant Source_Ptr := Sloc (N); | |
1396 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1397 | S_Typ : Entity_Id := Etype (N); | |
1398 | Cond : Node_Id; | |
1399 | T_Typ : Entity_Id; | |
1400 | ||
7be5088a | 1401 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean; |
1402 | -- A heap object with an indefinite subtype is constrained by its | |
1403 | -- initial value, and assigning to it requires a constraint_check. | |
1404 | -- The target may be an explicit dereference, or a renaming of one. | |
1405 | ||
ee6ba406 | 1406 | function Is_Aliased_Unconstrained_Component return Boolean; |
1407 | -- It is possible for an aliased component to have a nominal | |
1408 | -- unconstrained subtype (through instantiation). If this is a | |
1409 | -- discriminated component assigned in the expansion of an aggregate | |
1410 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1411 | -- situation requires a predicate of its own. |
ee6ba406 | 1412 | |
7be5088a | 1413 | ---------------------------------- |
1414 | -- Denotes_Explicit_Dereference -- | |
1415 | ---------------------------------- | |
1416 | ||
1417 | function Denotes_Explicit_Dereference (Obj : Node_Id) return Boolean is | |
1418 | begin | |
1419 | return | |
1420 | Nkind (Obj) = N_Explicit_Dereference | |
1421 | or else | |
1422 | (Is_Entity_Name (Obj) | |
1423 | and then Present (Renamed_Object (Entity (Obj))) | |
9474aa9c | 1424 | and then Nkind (Renamed_Object (Entity (Obj))) = |
1425 | N_Explicit_Dereference); | |
7be5088a | 1426 | end Denotes_Explicit_Dereference; |
1427 | ||
ee6ba406 | 1428 | ---------------------------------------- |
1429 | -- Is_Aliased_Unconstrained_Component -- | |
1430 | ---------------------------------------- | |
1431 | ||
1432 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1433 | Comp : Entity_Id; | |
1434 | Pref : Node_Id; | |
1435 | ||
1436 | begin | |
1437 | if Nkind (Lhs) /= N_Selected_Component then | |
1438 | return False; | |
1439 | else | |
1440 | Comp := Entity (Selector_Name (Lhs)); | |
1441 | Pref := Prefix (Lhs); | |
1442 | end if; | |
1443 | ||
1444 | if Ekind (Comp) /= E_Component | |
1445 | or else not Is_Aliased (Comp) | |
1446 | then | |
1447 | return False; | |
1448 | end if; | |
1449 | ||
1450 | return not Comes_From_Source (Pref) | |
1451 | and then In_Instance | |
1452 | and then not Is_Constrained (Etype (Comp)); | |
1453 | end Is_Aliased_Unconstrained_Component; | |
1454 | ||
1455 | -- Start of processing for Apply_Discriminant_Check | |
1456 | ||
1457 | begin | |
1458 | if Do_Access then | |
1459 | T_Typ := Designated_Type (Typ); | |
1460 | else | |
1461 | T_Typ := Typ; | |
1462 | end if; | |
1463 | ||
1464 | -- Nothing to do if discriminant checks are suppressed or else no code | |
1465 | -- is to be generated | |
1466 | ||
a33565dd | 1467 | if not Expander_Active |
ee6ba406 | 1468 | or else Discriminant_Checks_Suppressed (T_Typ) |
1469 | then | |
1470 | return; | |
1471 | end if; | |
1472 | ||
feff2f05 | 1473 | -- No discriminant checks necessary for an access when expression is |
1474 | -- statically Null. This is not only an optimization, it is fundamental | |
1475 | -- because otherwise discriminant checks may be generated in init procs | |
1476 | -- for types containing an access to a not-yet-frozen record, causing a | |
1477 | -- deadly forward reference. | |
ee6ba406 | 1478 | |
feff2f05 | 1479 | -- Also, if the expression is of an access type whose designated type is |
1480 | -- incomplete, then the access value must be null and we suppress the | |
1481 | -- check. | |
ee6ba406 | 1482 | |
2af58f67 | 1483 | if Known_Null (N) then |
ee6ba406 | 1484 | return; |
1485 | ||
1486 | elsif Is_Access_Type (S_Typ) then | |
1487 | S_Typ := Designated_Type (S_Typ); | |
1488 | ||
1489 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1490 | return; | |
1491 | end if; | |
1492 | end if; | |
1493 | ||
0577b0b1 | 1494 | -- If an assignment target is present, then we need to generate the |
1495 | -- actual subtype if the target is a parameter or aliased object with | |
1496 | -- an unconstrained nominal subtype. | |
1497 | ||
1498 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1499 | -- subtype to the parameter and dereference cases, since other aliased | |
1500 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
7be5088a | 1501 | -- constrained). |
ee6ba406 | 1502 | |
1503 | if Present (Lhs) | |
1504 | and then (Present (Param_Entity (Lhs)) | |
de54c5ab | 1505 | or else (Ada_Version < Ada_2005 |
0577b0b1 | 1506 | and then not Is_Constrained (T_Typ) |
ee6ba406 | 1507 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1508 | and then not Is_Aliased_Unconstrained_Component) |
de54c5ab | 1509 | or else (Ada_Version >= Ada_2005 |
0577b0b1 | 1510 | and then not Is_Constrained (T_Typ) |
7be5088a | 1511 | and then Denotes_Explicit_Dereference (Lhs) |
0577b0b1 | 1512 | and then Nkind (Original_Node (Lhs)) /= |
1513 | N_Function_Call)) | |
ee6ba406 | 1514 | then |
1515 | T_Typ := Get_Actual_Subtype (Lhs); | |
1516 | end if; | |
1517 | ||
feff2f05 | 1518 | -- Nothing to do if the type is unconstrained (this is the case where |
1519 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1520 | -- is required). | |
ee6ba406 | 1521 | |
1522 | if not Is_Constrained (T_Typ) then | |
1523 | return; | |
05fcfafb | 1524 | |
1525 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1526 | -- partial view that is constrained. | |
1527 | ||
de54c5ab | 1528 | elsif Ada_Version >= Ada_2005 |
0d78d2d4 | 1529 | and then Object_Type_Has_Constrained_Partial_View |
d41a3f41 | 1530 | (Typ => Base_Type (T_Typ), |
1531 | Scop => Current_Scope) | |
05fcfafb | 1532 | then |
1533 | return; | |
ee6ba406 | 1534 | end if; |
1535 | ||
00f91aef | 1536 | -- Nothing to do if the type is an Unchecked_Union |
1537 | ||
1538 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1539 | return; | |
1540 | end if; | |
1541 | ||
8d11916f | 1542 | -- Suppress checks if the subtypes are the same. The check must be |
feff2f05 | 1543 | -- preserved in an assignment to a formal, because the constraint is |
1544 | -- given by the actual. | |
ee6ba406 | 1545 | |
1546 | if Nkind (Original_Node (N)) /= N_Allocator | |
1547 | and then (No (Lhs) | |
8143bf7c | 1548 | or else not Is_Entity_Name (Lhs) |
1549 | or else No (Param_Entity (Lhs))) | |
ee6ba406 | 1550 | then |
1551 | if (Etype (N) = Typ | |
1552 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1553 | and then not Is_Aliased_View (Lhs) | |
1554 | then | |
1555 | return; | |
1556 | end if; | |
1557 | ||
feff2f05 | 1558 | -- We can also eliminate checks on allocators with a subtype mark that |
1559 | -- coincides with the context type. The context type may be a subtype | |
1560 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1561 | |
1562 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1563 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1564 | then | |
1565 | declare | |
9dfe12ae | 1566 | Alloc_Typ : constant Entity_Id := |
b6341c67 | 1567 | Entity (Expression (Original_Node (N))); |
ee6ba406 | 1568 | |
1569 | begin | |
1570 | if Alloc_Typ = T_Typ | |
1571 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1572 | and then Is_Entity_Name ( | |
1573 | Subtype_Indication (Parent (T_Typ))) | |
1574 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1575 | ||
1576 | then | |
1577 | return; | |
1578 | end if; | |
1579 | end; | |
1580 | end if; | |
1581 | ||
feff2f05 | 1582 | -- See if we have a case where the types are both constrained, and all |
1583 | -- the constraints are constants. In this case, we can do the check | |
1584 | -- successfully at compile time. | |
ee6ba406 | 1585 | |
8d11916f | 1586 | -- We skip this check for the case where the node is rewritten as |
d7ec9a29 | 1587 | -- an allocator, because it already carries the context subtype, |
1588 | -- and extracting the discriminants from the aggregate is messy. | |
ee6ba406 | 1589 | |
1590 | if Is_Constrained (S_Typ) | |
1591 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1592 | then | |
1593 | declare | |
1594 | DconT : Elmt_Id; | |
1595 | Discr : Entity_Id; | |
1596 | DconS : Elmt_Id; | |
1597 | ItemS : Node_Id; | |
1598 | ItemT : Node_Id; | |
1599 | ||
1600 | begin | |
1601 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1602 | -- private type completed by a default discriminated type. In that |
8d11916f | 1603 | -- case, we need to get the constraints from the underlying type. |
feff2f05 | 1604 | -- If the underlying type is unconstrained (i.e. has no default |
1605 | -- discriminants) no check is needed. | |
ee6ba406 | 1606 | |
1607 | if Has_Discriminants (S_Typ) then | |
1608 | Discr := First_Discriminant (S_Typ); | |
1609 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1610 | ||
1611 | else | |
1612 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1613 | DconS := | |
1614 | First_Elmt | |
1615 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1616 | ||
1617 | if No (DconS) then | |
1618 | return; | |
1619 | end if; | |
fccb5da7 | 1620 | |
1621 | -- A further optimization: if T_Typ is derived from S_Typ | |
1622 | -- without imposing a constraint, no check is needed. | |
1623 | ||
1624 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1625 | N_Full_Type_Declaration | |
1626 | then | |
1627 | declare | |
5c61a0ff | 1628 | Type_Def : constant Node_Id := |
b6341c67 | 1629 | Type_Definition (Original_Node (Parent (T_Typ))); |
fccb5da7 | 1630 | begin |
1631 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1632 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1633 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1634 | then | |
1635 | return; | |
1636 | end if; | |
1637 | end; | |
1638 | end if; | |
ee6ba406 | 1639 | end if; |
1640 | ||
86594966 | 1641 | -- Constraint may appear in full view of type |
1642 | ||
1643 | if Ekind (T_Typ) = E_Private_Subtype | |
1644 | and then Present (Full_View (T_Typ)) | |
1645 | then | |
d7ec9a29 | 1646 | DconT := |
86594966 | 1647 | First_Elmt (Discriminant_Constraint (Full_View (T_Typ))); |
86594966 | 1648 | else |
d7ec9a29 | 1649 | DconT := |
1650 | First_Elmt (Discriminant_Constraint (T_Typ)); | |
86594966 | 1651 | end if; |
ee6ba406 | 1652 | |
1653 | while Present (Discr) loop | |
1654 | ItemS := Node (DconS); | |
1655 | ItemT := Node (DconT); | |
1656 | ||
00c403ee | 1657 | -- For a discriminated component type constrained by the |
1658 | -- current instance of an enclosing type, there is no | |
1659 | -- applicable discriminant check. | |
1660 | ||
1661 | if Nkind (ItemT) = N_Attribute_Reference | |
1662 | and then Is_Access_Type (Etype (ItemT)) | |
1663 | and then Is_Entity_Name (Prefix (ItemT)) | |
1664 | and then Is_Type (Entity (Prefix (ItemT))) | |
1665 | then | |
1666 | return; | |
1667 | end if; | |
1668 | ||
cc60bd16 | 1669 | -- If the expressions for the discriminants are identical |
1670 | -- and it is side-effect free (for now just an entity), | |
1671 | -- this may be a shared constraint, e.g. from a subtype | |
1672 | -- without a constraint introduced as a generic actual. | |
1673 | -- Examine other discriminants if any. | |
1674 | ||
1675 | if ItemS = ItemT | |
1676 | and then Is_Entity_Name (ItemS) | |
1677 | then | |
1678 | null; | |
1679 | ||
1680 | elsif not Is_OK_Static_Expression (ItemS) | |
1681 | or else not Is_OK_Static_Expression (ItemT) | |
1682 | then | |
1683 | exit; | |
ee6ba406 | 1684 | |
cc60bd16 | 1685 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1686 | if Do_Access then -- needs run-time check. |
1687 | exit; | |
1688 | else | |
1689 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 1690 | (N, "incorrect value for discriminant&??", |
f15731c4 | 1691 | CE_Discriminant_Check_Failed, Ent => Discr); |
ee6ba406 | 1692 | return; |
1693 | end if; | |
1694 | end if; | |
1695 | ||
1696 | Next_Elmt (DconS); | |
1697 | Next_Elmt (DconT); | |
1698 | Next_Discriminant (Discr); | |
1699 | end loop; | |
1700 | ||
1701 | if No (Discr) then | |
1702 | return; | |
1703 | end if; | |
1704 | end; | |
1705 | end if; | |
1706 | ||
1707 | -- Here we need a discriminant check. First build the expression | |
1708 | -- for the comparisons of the discriminants: | |
1709 | ||
1710 | -- (n.disc1 /= typ.disc1) or else | |
1711 | -- (n.disc2 /= typ.disc2) or else | |
1712 | -- ... | |
1713 | -- (n.discn /= typ.discn) | |
1714 | ||
1715 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1716 | ||
3cce7f32 | 1717 | -- If Lhs is set and is a parameter, then the condition is guarded by: |
1718 | -- lhs'constrained and then (condition built above) | |
ee6ba406 | 1719 | |
1720 | if Present (Param_Entity (Lhs)) then | |
1721 | Cond := | |
1722 | Make_And_Then (Loc, | |
1723 | Left_Opnd => | |
1724 | Make_Attribute_Reference (Loc, | |
1725 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1726 | Attribute_Name => Name_Constrained), | |
1727 | Right_Opnd => Cond); | |
1728 | end if; | |
1729 | ||
1730 | if Do_Access then | |
1731 | Cond := Guard_Access (Cond, Loc, N); | |
1732 | end if; | |
1733 | ||
1734 | Insert_Action (N, | |
f15731c4 | 1735 | Make_Raise_Constraint_Error (Loc, |
1736 | Condition => Cond, | |
1737 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1738 | end Apply_Discriminant_Check; |
1739 | ||
2fe22c69 | 1740 | ------------------------- |
1741 | -- Apply_Divide_Checks -- | |
1742 | ------------------------- | |
ee6ba406 | 1743 | |
2fe22c69 | 1744 | procedure Apply_Divide_Checks (N : Node_Id) is |
ee6ba406 | 1745 | Loc : constant Source_Ptr := Sloc (N); |
1746 | Typ : constant Entity_Id := Etype (N); | |
1747 | Left : constant Node_Id := Left_Opnd (N); | |
1748 | Right : constant Node_Id := Right_Opnd (N); | |
1749 | ||
db415383 | 1750 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
2fe22c69 | 1751 | -- Current overflow checking mode |
1752 | ||
ee6ba406 | 1753 | LLB : Uint; |
1754 | Llo : Uint; | |
1755 | Lhi : Uint; | |
1756 | LOK : Boolean; | |
1757 | Rlo : Uint; | |
1758 | Rhi : Uint; | |
2fe22c69 | 1759 | ROK : Boolean; |
96da3284 | 1760 | |
1761 | pragma Warnings (Off, Lhi); | |
1762 | -- Don't actually use this value | |
ee6ba406 | 1763 | |
1764 | begin | |
0df9d43f | 1765 | -- If we are operating in MINIMIZED or ELIMINATED mode, and we are |
1766 | -- operating on signed integer types, then the only thing this routine | |
1767 | -- does is to call Apply_Arithmetic_Overflow_Minimized_Eliminated. That | |
1768 | -- procedure will (possibly later on during recursive downward calls), | |
1769 | -- ensure that any needed overflow/division checks are properly applied. | |
2fe22c69 | 1770 | |
1771 | if Mode in Minimized_Or_Eliminated | |
2fe22c69 | 1772 | and then Is_Signed_Integer_Type (Typ) |
1773 | then | |
1774 | Apply_Arithmetic_Overflow_Minimized_Eliminated (N); | |
1775 | return; | |
1776 | end if; | |
1777 | ||
1778 | -- Proceed here in SUPPRESSED or CHECKED modes | |
1779 | ||
a33565dd | 1780 | if Expander_Active |
13dbf220 | 1781 | and then not Backend_Divide_Checks_On_Target |
1782 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1783 | then |
9c486805 | 1784 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1785 | |
2fe22c69 | 1786 | -- Deal with division check |
ee6ba406 | 1787 | |
2fe22c69 | 1788 | if Do_Division_Check (N) |
1789 | and then not Division_Checks_Suppressed (Typ) | |
1790 | then | |
1791 | Apply_Division_Check (N, Rlo, Rhi, ROK); | |
ee6ba406 | 1792 | end if; |
1793 | ||
2fe22c69 | 1794 | -- Deal with overflow check |
1795 | ||
0df9d43f | 1796 | if Do_Overflow_Check (N) |
1797 | and then not Overflow_Checks_Suppressed (Etype (N)) | |
1798 | then | |
2fe22c69 | 1799 | -- Test for extremely annoying case of xxx'First divided by -1 |
1800 | -- for division of signed integer types (only overflow case). | |
ee6ba406 | 1801 | |
ee6ba406 | 1802 | if Nkind (N) = N_Op_Divide |
1803 | and then Is_Signed_Integer_Type (Typ) | |
1804 | then | |
9c486805 | 1805 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1806 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1807 | ||
1808 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
2fe22c69 | 1809 | and then |
1810 | ((not LOK) or else (Llo = LLB)) | |
ee6ba406 | 1811 | then |
1812 | Insert_Action (N, | |
1813 | Make_Raise_Constraint_Error (Loc, | |
1814 | Condition => | |
1815 | Make_And_Then (Loc, | |
2fe22c69 | 1816 | Left_Opnd => |
1817 | Make_Op_Eq (Loc, | |
1818 | Left_Opnd => | |
1819 | Duplicate_Subexpr_Move_Checks (Left), | |
1820 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), | |
ee6ba406 | 1821 | |
2fe22c69 | 1822 | Right_Opnd => |
1823 | Make_Op_Eq (Loc, | |
1824 | Left_Opnd => Duplicate_Subexpr (Right), | |
1825 | Right_Opnd => Make_Integer_Literal (Loc, -1))), | |
ee6ba406 | 1826 | |
f15731c4 | 1827 | Reason => CE_Overflow_Check_Failed)); |
ee6ba406 | 1828 | end if; |
1829 | end if; | |
1830 | end if; | |
1831 | end if; | |
2fe22c69 | 1832 | end Apply_Divide_Checks; |
1833 | ||
1834 | -------------------------- | |
1835 | -- Apply_Division_Check -- | |
1836 | -------------------------- | |
1837 | ||
1838 | procedure Apply_Division_Check | |
1839 | (N : Node_Id; | |
1840 | Rlo : Uint; | |
1841 | Rhi : Uint; | |
1842 | ROK : Boolean) | |
1843 | is | |
1844 | pragma Assert (Do_Division_Check (N)); | |
1845 | ||
1846 | Loc : constant Source_Ptr := Sloc (N); | |
1847 | Right : constant Node_Id := Right_Opnd (N); | |
1848 | ||
1849 | begin | |
a33565dd | 1850 | if Expander_Active |
2fe22c69 | 1851 | and then not Backend_Divide_Checks_On_Target |
1852 | and then Check_Needed (Right, Division_Check) | |
1853 | then | |
1854 | -- See if division by zero possible, and if so generate test. This | |
1855 | -- part of the test is not controlled by the -gnato switch, since | |
1856 | -- it is a Division_Check and not an Overflow_Check. | |
1857 | ||
1858 | if Do_Division_Check (N) then | |
1859 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then | |
1860 | Insert_Action (N, | |
1861 | Make_Raise_Constraint_Error (Loc, | |
1862 | Condition => | |
1863 | Make_Op_Eq (Loc, | |
1864 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), | |
1865 | Right_Opnd => Make_Integer_Literal (Loc, 0)), | |
1866 | Reason => CE_Divide_By_Zero)); | |
1867 | end if; | |
1868 | end if; | |
1869 | end if; | |
1870 | end Apply_Division_Check; | |
ee6ba406 | 1871 | |
5329ca64 | 1872 | ---------------------------------- |
1873 | -- Apply_Float_Conversion_Check -- | |
1874 | ---------------------------------- | |
1875 | ||
feff2f05 | 1876 | -- Let F and I be the source and target types of the conversion. The RM |
1877 | -- specifies that a floating-point value X is rounded to the nearest | |
1878 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1879 | -- value of X is checked against I'Range. | |
1880 | ||
1881 | -- The catch in the above paragraph is that there is no good way to know | |
1882 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1883 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1884 | |
5329ca64 | 1885 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1886 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1887 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1888 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1889 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1890 | -- sign of I'First and I'Last. | |
5329ca64 | 1891 | -- (5) X may be a NaN, which will fail any comparison |
1892 | ||
2af58f67 | 1893 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1894 | |
5329ca64 | 1895 | -- (1) If either I'First or I'Last is not known at compile time, use |
1896 | -- I'Base instead of I in the next three steps and perform a | |
1897 | -- regular range check against I'Range after conversion. | |
1898 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1899 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1900 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1901 | -- In other words, take one of the closest floating-point numbers | |
1902 | -- (which is an integer value) to I'First, and see if it is in | |
1903 | -- range or not. | |
5329ca64 | 1904 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1905 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1906 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1907 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1908 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1909 | ||
2af58f67 | 1910 | -- For the truncating case, replace steps (2) and (3) as follows: |
1911 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1912 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1913 | -- Lo_OK be True. | |
1914 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1915 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
141d591a | 1916 | -- Hi_OK be True. |
2af58f67 | 1917 | |
5329ca64 | 1918 | procedure Apply_Float_Conversion_Check |
1919 | (Ck_Node : Node_Id; | |
1920 | Target_Typ : Entity_Id) | |
1921 | is | |
feff2f05 | 1922 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1923 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1924 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1925 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1926 | Target_Base : constant Entity_Id := |
b6341c67 | 1927 | Implementation_Base_Type (Target_Typ); |
feff2f05 | 1928 | |
2af58f67 | 1929 | Par : constant Node_Id := Parent (Ck_Node); |
1930 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1931 | -- Parent of check node, must be a type conversion | |
1932 | ||
1933 | Truncate : constant Boolean := Float_Truncate (Par); | |
1934 | Max_Bound : constant Uint := | |
b6341c67 | 1935 | UI_Expon |
1936 | (Machine_Radix_Value (Expr_Type), | |
1937 | Machine_Mantissa_Value (Expr_Type) - 1) - 1; | |
2af58f67 | 1938 | |
5329ca64 | 1939 | -- Largest bound, so bound plus or minus half is a machine number of F |
1940 | ||
feff2f05 | 1941 | Ifirst, Ilast : Uint; |
1942 | -- Bounds of integer type | |
1943 | ||
1944 | Lo, Hi : Ureal; | |
1945 | -- Bounds to check in floating-point domain | |
5329ca64 | 1946 | |
feff2f05 | 1947 | Lo_OK, Hi_OK : Boolean; |
1948 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1949 | |
feff2f05 | 1950 | Lo_Chk, Hi_Chk : Node_Id; |
1951 | -- Expressions that are False iff check fails | |
1952 | ||
1953 | Reason : RT_Exception_Code; | |
5329ca64 | 1954 | |
1955 | begin | |
41f06abf | 1956 | -- We do not need checks if we are not generating code (i.e. the full |
1957 | -- expander is not active). In SPARK mode, we specifically don't want | |
1958 | -- the frontend to expand these checks, which are dealt with directly | |
1959 | -- in the formal verification backend. | |
1960 | ||
a33565dd | 1961 | if not Expander_Active then |
41f06abf | 1962 | return; |
1963 | end if; | |
1964 | ||
5329ca64 | 1965 | if not Compile_Time_Known_Value (LB) |
1966 | or not Compile_Time_Known_Value (HB) | |
1967 | then | |
1968 | declare | |
feff2f05 | 1969 | -- First check that the value falls in the range of the base type, |
1970 | -- to prevent overflow during conversion and then perform a | |
1971 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 1972 | |
5329ca64 | 1973 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 1974 | |
46eb6933 | 1975 | Temp : constant Entity_Id := Make_Temporary (Loc, 'T', Par); |
5329ca64 | 1976 | |
1977 | begin | |
1978 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
1979 | Set_Etype (Temp, Target_Base); | |
1980 | ||
1981 | Insert_Action (Parent (Par), | |
1982 | Make_Object_Declaration (Loc, | |
1983 | Defining_Identifier => Temp, | |
1984 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
1985 | Expression => New_Copy_Tree (Par)), | |
1986 | Suppress => All_Checks); | |
1987 | ||
1988 | Insert_Action (Par, | |
1989 | Make_Raise_Constraint_Error (Loc, | |
1990 | Condition => | |
1991 | Make_Not_In (Loc, | |
1992 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
1993 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
1994 | Reason => CE_Range_Check_Failed)); | |
1995 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
1996 | ||
1997 | return; | |
1998 | end; | |
1999 | end if; | |
2000 | ||
7d86aa98 | 2001 | -- Get the (static) bounds of the target type |
5329ca64 | 2002 | |
2003 | Ifirst := Expr_Value (LB); | |
2004 | Ilast := Expr_Value (HB); | |
2005 | ||
7d86aa98 | 2006 | -- A simple optimization: if the expression is a universal literal, |
2007 | -- we can do the comparison with the bounds and the conversion to | |
2008 | -- an integer type statically. The range checks are unchanged. | |
2009 | ||
2010 | if Nkind (Ck_Node) = N_Real_Literal | |
2011 | and then Etype (Ck_Node) = Universal_Real | |
2012 | and then Is_Integer_Type (Target_Typ) | |
2013 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
2014 | then | |
2015 | declare | |
2016 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
2017 | ||
2018 | begin | |
2019 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
2020 | ||
4309515d | 2021 | -- Conversion is safe |
7d86aa98 | 2022 | |
2023 | Rewrite (Parent (Ck_Node), | |
2024 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
2025 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
2026 | return; | |
2027 | end if; | |
2028 | end; | |
2029 | end if; | |
2030 | ||
5329ca64 | 2031 | -- Check against lower bound |
2032 | ||
2af58f67 | 2033 | if Truncate and then Ifirst > 0 then |
2034 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
2035 | Lo_OK := False; | |
2036 | ||
2037 | elsif Truncate then | |
2038 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
2039 | Lo_OK := True; | |
2040 | ||
2041 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 2042 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
2043 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 2044 | |
5329ca64 | 2045 | else |
2046 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
2047 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
2048 | end if; | |
2049 | ||
2050 | if Lo_OK then | |
2051 | ||
2052 | -- Lo_Chk := (X >= Lo) | |
2053 | ||
2054 | Lo_Chk := Make_Op_Ge (Loc, | |
2055 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2056 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2057 | ||
2058 | else | |
2059 | -- Lo_Chk := (X > Lo) | |
2060 | ||
2061 | Lo_Chk := Make_Op_Gt (Loc, | |
2062 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2063 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
2064 | end if; | |
2065 | ||
2066 | -- Check against higher bound | |
2067 | ||
2af58f67 | 2068 | if Truncate and then Ilast < 0 then |
2069 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
b2c42753 | 2070 | Hi_OK := False; |
2af58f67 | 2071 | |
2072 | elsif Truncate then | |
2073 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
2074 | Hi_OK := True; | |
2075 | ||
2076 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 2077 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
2078 | Hi_OK := (Ilast < 0); | |
2079 | else | |
2080 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
2081 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
2082 | end if; | |
2083 | ||
2084 | if Hi_OK then | |
2085 | ||
2086 | -- Hi_Chk := (X <= Hi) | |
2087 | ||
2088 | Hi_Chk := Make_Op_Le (Loc, | |
2089 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2090 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2091 | ||
2092 | else | |
2093 | -- Hi_Chk := (X < Hi) | |
2094 | ||
2095 | Hi_Chk := Make_Op_Lt (Loc, | |
2096 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
2097 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
2098 | end if; | |
2099 | ||
feff2f05 | 2100 | -- If the bounds of the target type are the same as those of the base |
2101 | -- type, the check is an overflow check as a range check is not | |
2102 | -- performed in these cases. | |
5329ca64 | 2103 | |
2104 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
2105 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
2106 | then | |
2107 | Reason := CE_Overflow_Check_Failed; | |
2108 | else | |
2109 | Reason := CE_Range_Check_Failed; | |
2110 | end if; | |
2111 | ||
2112 | -- Raise CE if either conditions does not hold | |
2113 | ||
2114 | Insert_Action (Ck_Node, | |
2115 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 2116 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 2117 | Reason => Reason)); |
2118 | end Apply_Float_Conversion_Check; | |
2119 | ||
ee6ba406 | 2120 | ------------------------ |
2121 | -- Apply_Length_Check -- | |
2122 | ------------------------ | |
2123 | ||
2124 | procedure Apply_Length_Check | |
2125 | (Ck_Node : Node_Id; | |
2126 | Target_Typ : Entity_Id; | |
2127 | Source_Typ : Entity_Id := Empty) | |
2128 | is | |
2129 | begin | |
2130 | Apply_Selected_Length_Checks | |
2131 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2132 | end Apply_Length_Check; | |
2133 | ||
3b045963 | 2134 | ------------------------------------- |
2135 | -- Apply_Parameter_Aliasing_Checks -- | |
2136 | ------------------------------------- | |
b73adb97 | 2137 | |
3b045963 | 2138 | procedure Apply_Parameter_Aliasing_Checks |
2139 | (Call : Node_Id; | |
2140 | Subp : Entity_Id) | |
2141 | is | |
bb569db0 | 2142 | Loc : constant Source_Ptr := Sloc (Call); |
2143 | ||
3b045963 | 2144 | function May_Cause_Aliasing |
2145 | (Formal_1 : Entity_Id; | |
2146 | Formal_2 : Entity_Id) return Boolean; | |
2147 | -- Determine whether two formal parameters can alias each other | |
2148 | -- depending on their modes. | |
2149 | ||
2150 | function Original_Actual (N : Node_Id) return Node_Id; | |
2151 | -- The expander may replace an actual with a temporary for the sake of | |
2152 | -- side effect removal. The temporary may hide a potential aliasing as | |
2153 | -- it does not share the address of the actual. This routine attempts | |
2154 | -- to retrieve the original actual. | |
2155 | ||
bb569db0 | 2156 | procedure Overlap_Check |
2157 | (Actual_1 : Node_Id; | |
2158 | Actual_2 : Node_Id; | |
2159 | Formal_1 : Entity_Id; | |
2160 | Formal_2 : Entity_Id; | |
2161 | Check : in out Node_Id); | |
2162 | -- Create a check to determine whether Actual_1 overlaps with Actual_2. | |
2163 | -- If detailed exception messages are enabled, the check is augmented to | |
2164 | -- provide information about the names of the corresponding formals. See | |
2165 | -- the body for details. Actual_1 and Actual_2 denote the two actuals to | |
2166 | -- be tested. Formal_1 and Formal_2 denote the corresponding formals. | |
2167 | -- Check contains all and-ed simple tests generated so far or remains | |
2168 | -- unchanged in the case of detailed exception messaged. | |
2169 | ||
3b045963 | 2170 | ------------------------ |
2171 | -- May_Cause_Aliasing -- | |
2172 | ------------------------ | |
b73adb97 | 2173 | |
3b045963 | 2174 | function May_Cause_Aliasing |
4a9e7f0c | 2175 | (Formal_1 : Entity_Id; |
3b045963 | 2176 | Formal_2 : Entity_Id) return Boolean |
2177 | is | |
2178 | begin | |
2179 | -- The following combination cannot lead to aliasing | |
2180 | ||
2181 | -- Formal 1 Formal 2 | |
2182 | -- IN IN | |
2183 | ||
2184 | if Ekind (Formal_1) = E_In_Parameter | |
a45d946f | 2185 | and then |
2186 | Ekind (Formal_2) = E_In_Parameter | |
3b045963 | 2187 | then |
2188 | return False; | |
2189 | ||
2190 | -- The following combinations may lead to aliasing | |
2191 | ||
2192 | -- Formal 1 Formal 2 | |
2193 | -- IN OUT | |
2194 | -- IN IN OUT | |
2195 | -- OUT IN | |
2196 | -- OUT IN OUT | |
2197 | -- OUT OUT | |
2198 | ||
2199 | else | |
2200 | return True; | |
2201 | end if; | |
2202 | end May_Cause_Aliasing; | |
2203 | ||
2204 | --------------------- | |
2205 | -- Original_Actual -- | |
2206 | --------------------- | |
2207 | ||
2208 | function Original_Actual (N : Node_Id) return Node_Id is | |
2209 | begin | |
2210 | if Nkind (N) = N_Type_Conversion then | |
2211 | return Expression (N); | |
2212 | ||
2213 | -- The expander created a temporary to capture the result of a type | |
2214 | -- conversion where the expression is the real actual. | |
2215 | ||
2216 | elsif Nkind (N) = N_Identifier | |
2217 | and then Present (Original_Node (N)) | |
2218 | and then Nkind (Original_Node (N)) = N_Type_Conversion | |
2219 | then | |
2220 | return Expression (Original_Node (N)); | |
2221 | end if; | |
2222 | ||
2223 | return N; | |
2224 | end Original_Actual; | |
2225 | ||
bb569db0 | 2226 | ------------------- |
2227 | -- Overlap_Check -- | |
2228 | ------------------- | |
2229 | ||
2230 | procedure Overlap_Check | |
2231 | (Actual_1 : Node_Id; | |
2232 | Actual_2 : Node_Id; | |
2233 | Formal_1 : Entity_Id; | |
2234 | Formal_2 : Entity_Id; | |
2235 | Check : in out Node_Id) | |
2236 | is | |
29448168 | 2237 | Cond : Node_Id; |
2238 | ID_Casing : constant Casing_Type := | |
2239 | Identifier_Casing (Source_Index (Current_Sem_Unit)); | |
bb569db0 | 2240 | |
2241 | begin | |
2242 | -- Generate: | |
2243 | -- Actual_1'Overlaps_Storage (Actual_2) | |
2244 | ||
2245 | Cond := | |
2246 | Make_Attribute_Reference (Loc, | |
2247 | Prefix => New_Copy_Tree (Original_Actual (Actual_1)), | |
2248 | Attribute_Name => Name_Overlaps_Storage, | |
2249 | Expressions => | |
2250 | New_List (New_Copy_Tree (Original_Actual (Actual_2)))); | |
2251 | ||
2252 | -- Generate the following check when detailed exception messages are | |
2253 | -- enabled: | |
2254 | ||
2255 | -- if Actual_1'Overlaps_Storage (Actual_2) then | |
2256 | -- raise Program_Error with <detailed message>; | |
2257 | -- end if; | |
2258 | ||
2259 | if Exception_Extra_Info then | |
2260 | Start_String; | |
2261 | ||
2262 | -- Do not generate location information for internal calls | |
2263 | ||
2264 | if Comes_From_Source (Call) then | |
2265 | Store_String_Chars (Build_Location_String (Loc)); | |
2266 | Store_String_Char (' '); | |
2267 | end if; | |
2268 | ||
2269 | Store_String_Chars ("aliased parameters, actuals for """); | |
29448168 | 2270 | |
2271 | Get_Name_String (Chars (Formal_1)); | |
2272 | Set_Casing (ID_Casing); | |
2273 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2274 | ||
bb569db0 | 2275 | Store_String_Chars (""" and """); |
29448168 | 2276 | |
2277 | Get_Name_String (Chars (Formal_2)); | |
2278 | Set_Casing (ID_Casing); | |
2279 | Store_String_Chars (Name_Buffer (1 .. Name_Len)); | |
2280 | ||
bb569db0 | 2281 | Store_String_Chars (""" overlap"); |
2282 | ||
2283 | Insert_Action (Call, | |
2284 | Make_If_Statement (Loc, | |
2285 | Condition => Cond, | |
2286 | Then_Statements => New_List ( | |
2287 | Make_Raise_Statement (Loc, | |
2288 | Name => | |
83c6c069 | 2289 | New_Occurrence_Of (Standard_Program_Error, Loc), |
bb569db0 | 2290 | Expression => Make_String_Literal (Loc, End_String))))); |
2291 | ||
2292 | -- Create a sequence of overlapping checks by and-ing them all | |
2293 | -- together. | |
2294 | ||
2295 | else | |
2296 | if No (Check) then | |
2297 | Check := Cond; | |
2298 | else | |
2299 | Check := | |
2300 | Make_And_Then (Loc, | |
2301 | Left_Opnd => Check, | |
2302 | Right_Opnd => Cond); | |
2303 | end if; | |
2304 | end if; | |
2305 | end Overlap_Check; | |
2306 | ||
3b045963 | 2307 | -- Local variables |
2308 | ||
3b045963 | 2309 | Actual_1 : Node_Id; |
2310 | Actual_2 : Node_Id; | |
2311 | Check : Node_Id; | |
3b045963 | 2312 | Formal_1 : Entity_Id; |
2313 | Formal_2 : Entity_Id; | |
2314 | ||
2315 | -- Start of processing for Apply_Parameter_Aliasing_Checks | |
2316 | ||
2317 | begin | |
bb569db0 | 2318 | Check := Empty; |
3b045963 | 2319 | |
2320 | Actual_1 := First_Actual (Call); | |
2321 | Formal_1 := First_Formal (Subp); | |
2322 | while Present (Actual_1) and then Present (Formal_1) loop | |
2323 | ||
2324 | -- Ensure that the actual is an object that is not passed by value. | |
2325 | -- Elementary types are always passed by value, therefore actuals of | |
2326 | -- such types cannot lead to aliasing. | |
2327 | ||
2328 | if Is_Object_Reference (Original_Actual (Actual_1)) | |
2329 | and then not Is_Elementary_Type (Etype (Original_Actual (Actual_1))) | |
2330 | then | |
2331 | Actual_2 := Next_Actual (Actual_1); | |
2332 | Formal_2 := Next_Formal (Formal_1); | |
2333 | while Present (Actual_2) and then Present (Formal_2) loop | |
2334 | ||
2335 | -- The other actual we are testing against must also denote | |
2336 | -- a non pass-by-value object. Generate the check only when | |
2337 | -- the mode of the two formals may lead to aliasing. | |
2338 | ||
2339 | if Is_Object_Reference (Original_Actual (Actual_2)) | |
2340 | and then not | |
2341 | Is_Elementary_Type (Etype (Original_Actual (Actual_2))) | |
2342 | and then May_Cause_Aliasing (Formal_1, Formal_2) | |
2343 | then | |
bb569db0 | 2344 | Overlap_Check |
2345 | (Actual_1 => Actual_1, | |
2346 | Actual_2 => Actual_2, | |
2347 | Formal_1 => Formal_1, | |
2348 | Formal_2 => Formal_2, | |
2349 | Check => Check); | |
3b045963 | 2350 | end if; |
2351 | ||
2352 | Next_Actual (Actual_2); | |
2353 | Next_Formal (Formal_2); | |
2354 | end loop; | |
2355 | end if; | |
2356 | ||
2357 | Next_Actual (Actual_1); | |
2358 | Next_Formal (Formal_1); | |
2359 | end loop; | |
2360 | ||
bb569db0 | 2361 | -- Place a simple check right before the call |
3b045963 | 2362 | |
bb569db0 | 2363 | if Present (Check) and then not Exception_Extra_Info then |
3b045963 | 2364 | Insert_Action (Call, |
2365 | Make_Raise_Program_Error (Loc, | |
bb569db0 | 2366 | Condition => Check, |
2367 | Reason => PE_Aliased_Parameters)); | |
3b045963 | 2368 | end if; |
2369 | end Apply_Parameter_Aliasing_Checks; | |
2370 | ||
2371 | ------------------------------------- | |
2372 | -- Apply_Parameter_Validity_Checks -- | |
2373 | ------------------------------------- | |
2374 | ||
2375 | procedure Apply_Parameter_Validity_Checks (Subp : Entity_Id) is | |
2376 | Subp_Decl : Node_Id; | |
b73adb97 | 2377 | |
4a9e7f0c | 2378 | procedure Add_Validity_Check |
2379 | (Context : Entity_Id; | |
2380 | PPC_Nam : Name_Id; | |
2381 | For_Result : Boolean := False); | |
2382 | -- Add a single 'Valid[_Scalar] check which verifies the initialization | |
2383 | -- of Context. PPC_Nam denotes the pre or post condition pragma name. | |
2384 | -- Set flag For_Result when to verify the result of a function. | |
b73adb97 | 2385 | |
4a9e7f0c | 2386 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id); |
2387 | -- Create a pre or post condition pragma with name PPC_Nam which | |
2388 | -- tests expression Check. | |
b73adb97 | 2389 | |
b73adb97 | 2390 | ------------------------ |
2391 | -- Add_Validity_Check -- | |
2392 | ------------------------ | |
2393 | ||
2394 | procedure Add_Validity_Check | |
2395 | (Context : Entity_Id; | |
4a9e7f0c | 2396 | PPC_Nam : Name_Id; |
b73adb97 | 2397 | For_Result : Boolean := False) |
2398 | is | |
4a9e7f0c | 2399 | Loc : constant Source_Ptr := Sloc (Subp); |
2400 | Typ : constant Entity_Id := Etype (Context); | |
b73adb97 | 2401 | Check : Node_Id; |
2402 | Nam : Name_Id; | |
2403 | ||
2404 | begin | |
2405 | -- Pick the proper version of 'Valid depending on the type of the | |
2406 | -- context. If the context is not eligible for such a check, return. | |
2407 | ||
2408 | if Is_Scalar_Type (Typ) then | |
2409 | Nam := Name_Valid; | |
2410 | elsif not No_Scalar_Parts (Typ) then | |
2411 | Nam := Name_Valid_Scalars; | |
2412 | else | |
2413 | return; | |
2414 | end if; | |
2415 | ||
2416 | -- Step 1: Create the expression to verify the validity of the | |
2417 | -- context. | |
2418 | ||
83c6c069 | 2419 | Check := New_Occurrence_Of (Context, Loc); |
b73adb97 | 2420 | |
2421 | -- When processing a function result, use 'Result. Generate | |
2422 | -- Context'Result | |
2423 | ||
2424 | if For_Result then | |
2425 | Check := | |
2426 | Make_Attribute_Reference (Loc, | |
2427 | Prefix => Check, | |
2428 | Attribute_Name => Name_Result); | |
2429 | end if; | |
2430 | ||
2431 | -- Generate: | |
2432 | -- Context['Result]'Valid[_Scalars] | |
2433 | ||
2434 | Check := | |
2435 | Make_Attribute_Reference (Loc, | |
2436 | Prefix => Check, | |
2437 | Attribute_Name => Nam); | |
2438 | ||
4a9e7f0c | 2439 | -- Step 2: Create a pre or post condition pragma |
2440 | ||
2441 | Build_PPC_Pragma (PPC_Nam, Check); | |
2442 | end Add_Validity_Check; | |
2443 | ||
2444 | ---------------------- | |
2445 | -- Build_PPC_Pragma -- | |
2446 | ---------------------- | |
b73adb97 | 2447 | |
4a9e7f0c | 2448 | procedure Build_PPC_Pragma (PPC_Nam : Name_Id; Check : Node_Id) is |
7c443ae8 | 2449 | Loc : constant Source_Ptr := Sloc (Subp); |
2450 | Decls : List_Id; | |
2451 | Prag : Node_Id; | |
4a9e7f0c | 2452 | |
2453 | begin | |
2454 | Prag := | |
2455 | Make_Pragma (Loc, | |
2456 | Pragma_Identifier => Make_Identifier (Loc, PPC_Nam), | |
2457 | Pragma_Argument_Associations => New_List ( | |
2458 | Make_Pragma_Argument_Association (Loc, | |
2459 | Chars => Name_Check, | |
2460 | Expression => Check))); | |
2461 | ||
2462 | -- Add a message unless exception messages are suppressed | |
2463 | ||
2464 | if not Exception_Locations_Suppressed then | |
2465 | Append_To (Pragma_Argument_Associations (Prag), | |
2466 | Make_Pragma_Argument_Association (Loc, | |
2467 | Chars => Name_Message, | |
2468 | Expression => | |
2469 | Make_String_Literal (Loc, | |
2470 | Strval => "failed " & Get_Name_String (PPC_Nam) & | |
2471 | " from " & Build_Location_String (Loc)))); | |
2472 | end if; | |
2473 | ||
2474 | -- Insert the pragma in the tree | |
2475 | ||
2476 | if Nkind (Parent (Subp_Decl)) = N_Compilation_Unit then | |
2477 | Add_Global_Declaration (Prag); | |
7c443ae8 | 2478 | Analyze (Prag); |
2479 | ||
2480 | -- PPC pragmas associated with subprogram bodies must be inserted in | |
2481 | -- the declarative part of the body. | |
2482 | ||
2483 | elsif Nkind (Subp_Decl) = N_Subprogram_Body then | |
2484 | Decls := Declarations (Subp_Decl); | |
2485 | ||
2486 | if No (Decls) then | |
2487 | Decls := New_List; | |
2488 | Set_Declarations (Subp_Decl, Decls); | |
2489 | end if; | |
2490 | ||
1bd93de5 | 2491 | Prepend_To (Decls, Prag); |
7c443ae8 | 2492 | |
2493 | -- Ensure the proper visibility of the subprogram body and its | |
2494 | -- parameters. | |
2495 | ||
2496 | Push_Scope (Subp); | |
2497 | Analyze (Prag); | |
2498 | Pop_Scope; | |
2499 | ||
2500 | -- For subprogram declarations insert the PPC pragma right after the | |
2501 | -- declarative node. | |
2502 | ||
b73adb97 | 2503 | else |
7c443ae8 | 2504 | Insert_After_And_Analyze (Subp_Decl, Prag); |
b73adb97 | 2505 | end if; |
4a9e7f0c | 2506 | end Build_PPC_Pragma; |
2507 | ||
2508 | -- Local variables | |
2509 | ||
2510 | Formal : Entity_Id; | |
4a9e7f0c | 2511 | Subp_Spec : Node_Id; |
2512 | ||
3b045963 | 2513 | -- Start of processing for Apply_Parameter_Validity_Checks |
b73adb97 | 2514 | |
2515 | begin | |
4a9e7f0c | 2516 | -- Extract the subprogram specification and declaration nodes |
b73adb97 | 2517 | |
4a9e7f0c | 2518 | Subp_Spec := Parent (Subp); |
a45d946f | 2519 | |
4a9e7f0c | 2520 | if Nkind (Subp_Spec) = N_Defining_Program_Unit_Name then |
2521 | Subp_Spec := Parent (Subp_Spec); | |
2522 | end if; | |
a45d946f | 2523 | |
4a9e7f0c | 2524 | Subp_Decl := Parent (Subp_Spec); |
9e58d7ed | 2525 | |
b73adb97 | 2526 | if not Comes_From_Source (Subp) |
4a9e7f0c | 2527 | |
2528 | -- Do not process formal subprograms because the corresponding actual | |
2529 | -- will receive the proper checks when the instance is analyzed. | |
2530 | ||
2531 | or else Is_Formal_Subprogram (Subp) | |
2532 | ||
a45d946f | 2533 | -- Do not process imported subprograms since pre and post conditions |
2534 | -- are never verified on routines coming from a different language. | |
4a9e7f0c | 2535 | |
b73adb97 | 2536 | or else Is_Imported (Subp) |
2537 | or else Is_Intrinsic_Subprogram (Subp) | |
4a9e7f0c | 2538 | |
a45d946f | 2539 | -- The PPC pragmas generated by this routine do not correspond to |
2540 | -- source aspects, therefore they cannot be applied to abstract | |
2541 | -- subprograms. | |
4a9e7f0c | 2542 | |
7c443ae8 | 2543 | or else Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration |
4a9e7f0c | 2544 | |
a45d946f | 2545 | -- Do not consider subprogram renaminds because the renamed entity |
2546 | -- already has the proper PPC pragmas. | |
1bd93de5 | 2547 | |
2548 | or else Nkind (Subp_Decl) = N_Subprogram_Renaming_Declaration | |
2549 | ||
a45d946f | 2550 | -- Do not process null procedures because there is no benefit of |
2551 | -- adding the checks to a no action routine. | |
4a9e7f0c | 2552 | |
2553 | or else (Nkind (Subp_Spec) = N_Procedure_Specification | |
a45d946f | 2554 | and then Null_Present (Subp_Spec)) |
b73adb97 | 2555 | then |
2556 | return; | |
2557 | end if; | |
2558 | ||
4a9e7f0c | 2559 | -- Inspect all the formals applying aliasing and scalar initialization |
2560 | -- checks where applicable. | |
b73adb97 | 2561 | |
2562 | Formal := First_Formal (Subp); | |
2563 | while Present (Formal) loop | |
4a9e7f0c | 2564 | |
2565 | -- Generate the following scalar initialization checks for each | |
2566 | -- formal parameter: | |
2567 | ||
2568 | -- mode IN - Pre => Formal'Valid[_Scalars] | |
2569 | -- mode IN OUT - Pre, Post => Formal'Valid[_Scalars] | |
2570 | -- mode OUT - Post => Formal'Valid[_Scalars] | |
2571 | ||
2572 | if Check_Validity_Of_Parameters then | |
2573 | if Ekind_In (Formal, E_In_Parameter, E_In_Out_Parameter) then | |
2574 | Add_Validity_Check (Formal, Name_Precondition, False); | |
2575 | end if; | |
2576 | ||
2577 | if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then | |
2578 | Add_Validity_Check (Formal, Name_Postcondition, False); | |
2579 | end if; | |
b73adb97 | 2580 | end if; |
2581 | ||
b73adb97 | 2582 | Next_Formal (Formal); |
2583 | end loop; | |
2584 | ||
a45d946f | 2585 | -- Generate following scalar initialization check for function result: |
4a9e7f0c | 2586 | |
2587 | -- Post => Subp'Result'Valid[_Scalars] | |
b73adb97 | 2588 | |
a45d946f | 2589 | if Check_Validity_Of_Parameters and then Ekind (Subp) = E_Function then |
4a9e7f0c | 2590 | Add_Validity_Check (Subp, Name_Postcondition, True); |
b73adb97 | 2591 | end if; |
3b045963 | 2592 | end Apply_Parameter_Validity_Checks; |
b73adb97 | 2593 | |
7aafae1c | 2594 | --------------------------- |
2595 | -- Apply_Predicate_Check -- | |
2596 | --------------------------- | |
2597 | ||
2598 | procedure Apply_Predicate_Check (N : Node_Id; Typ : Entity_Id) is | |
301d5ec3 | 2599 | S : Entity_Id; |
9e58d7ed | 2600 | |
7aafae1c | 2601 | begin |
701d57a4 | 2602 | if Present (Predicate_Function (Typ)) then |
301d5ec3 | 2603 | |
301d5ec3 | 2604 | S := Current_Scope; |
9e58d7ed | 2605 | while Present (S) and then not Is_Subprogram (S) loop |
301d5ec3 | 2606 | S := Scope (S); |
2607 | end loop; | |
2608 | ||
ea822fd4 | 2609 | -- A predicate check does not apply within internally generated |
2610 | -- subprograms, such as TSS functions. | |
2611 | ||
2612 | if Within_Internal_Subprogram then | |
301d5ec3 | 2613 | return; |
22631b41 | 2614 | |
96a2d100 | 2615 | -- If the check appears within the predicate function itself, it |
2616 | -- means that the user specified a check whose formal is the | |
2617 | -- predicated subtype itself, rather than some covering type. This | |
2618 | -- is likely to be a common error, and thus deserves a warning. | |
22631b41 | 2619 | |
0e9014a7 | 2620 | elsif Present (S) and then S = Predicate_Function (Typ) then |
96a2d100 | 2621 | Error_Msg_N |
2622 | ("predicate check includes a function call that " | |
cb97ae5c | 2623 | & "requires a predicate check??", Parent (N)); |
96a2d100 | 2624 | Error_Msg_N |
cb97ae5c | 2625 | ("\this will result in infinite recursion??", Parent (N)); |
96a2d100 | 2626 | Insert_Action (N, |
61016a7a | 2627 | Make_Raise_Storage_Error (Sloc (N), |
2628 | Reason => SE_Infinite_Recursion)); | |
22631b41 | 2629 | |
64cc9e5d | 2630 | -- Here for normal case of predicate active |
e6281d47 | 2631 | |
61016a7a | 2632 | else |
b04165c4 | 2633 | -- If the type has a static predicate and the expression is known |
2634 | -- at compile time, see if the expression satisfies the predicate. | |
3a75f20b | 2635 | |
2636 | Check_Expression_Against_Static_Predicate (N, Typ); | |
e6281d47 | 2637 | |
301d5ec3 | 2638 | Insert_Action (N, |
2639 | Make_Predicate_Check (Typ, Duplicate_Subexpr (N))); | |
2640 | end if; | |
7aafae1c | 2641 | end if; |
2642 | end Apply_Predicate_Check; | |
2643 | ||
ee6ba406 | 2644 | ----------------------- |
2645 | -- Apply_Range_Check -- | |
2646 | ----------------------- | |
2647 | ||
2648 | procedure Apply_Range_Check | |
2649 | (Ck_Node : Node_Id; | |
2650 | Target_Typ : Entity_Id; | |
2651 | Source_Typ : Entity_Id := Empty) | |
2652 | is | |
2653 | begin | |
2654 | Apply_Selected_Range_Checks | |
2655 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
2656 | end Apply_Range_Check; | |
2657 | ||
2658 | ------------------------------ | |
2659 | -- Apply_Scalar_Range_Check -- | |
2660 | ------------------------------ | |
2661 | ||
feff2f05 | 2662 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
2663 | -- off if it is already set on. | |
ee6ba406 | 2664 | |
2665 | procedure Apply_Scalar_Range_Check | |
2666 | (Expr : Node_Id; | |
2667 | Target_Typ : Entity_Id; | |
2668 | Source_Typ : Entity_Id := Empty; | |
2669 | Fixed_Int : Boolean := False) | |
2670 | is | |
2671 | Parnt : constant Node_Id := Parent (Expr); | |
2672 | S_Typ : Entity_Id; | |
2673 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
2674 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
2675 | OK : Boolean; | |
2676 | ||
2677 | Is_Subscr_Ref : Boolean; | |
2678 | -- Set true if Expr is a subscript | |
2679 | ||
2680 | Is_Unconstrained_Subscr_Ref : Boolean; | |
2681 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
2682 | -- case we do not attempt to do an analysis of the value against the | |
2683 | -- range of the subscript, since we don't know the actual subtype. | |
2684 | ||
2685 | Int_Real : Boolean; | |
feff2f05 | 2686 | -- Set to True if Expr should be regarded as a real value even though |
2687 | -- the type of Expr might be discrete. | |
ee6ba406 | 2688 | |
2689 | procedure Bad_Value; | |
2690 | -- Procedure called if value is determined to be out of range | |
2691 | ||
9dfe12ae | 2692 | --------------- |
2693 | -- Bad_Value -- | |
2694 | --------------- | |
2695 | ||
ee6ba406 | 2696 | procedure Bad_Value is |
2697 | begin | |
2698 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 2699 | (Expr, "value not in range of}??", CE_Range_Check_Failed, |
ee6ba406 | 2700 | Ent => Target_Typ, |
2701 | Typ => Target_Typ); | |
2702 | end Bad_Value; | |
2703 | ||
9dfe12ae | 2704 | -- Start of processing for Apply_Scalar_Range_Check |
2705 | ||
ee6ba406 | 2706 | begin |
2af58f67 | 2707 | -- Return if check obviously not needed |
ee6ba406 | 2708 | |
2af58f67 | 2709 | if |
2710 | -- Not needed inside generic | |
ee6ba406 | 2711 | |
2af58f67 | 2712 | Inside_A_Generic |
2713 | ||
2714 | -- Not needed if previous error | |
2715 | ||
2716 | or else Target_Typ = Any_Type | |
2717 | or else Nkind (Expr) = N_Error | |
2718 | ||
2719 | -- Not needed for non-scalar type | |
2720 | ||
2721 | or else not Is_Scalar_Type (Target_Typ) | |
2722 | ||
2723 | -- Not needed if we know node raises CE already | |
2724 | ||
2725 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 2726 | then |
2727 | return; | |
2728 | end if; | |
2729 | ||
2730 | -- Now, see if checks are suppressed | |
2731 | ||
2732 | Is_Subscr_Ref := | |
2733 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
2734 | ||
2735 | if Is_Subscr_Ref then | |
2736 | Arr := Prefix (Parnt); | |
2737 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
cce84b09 | 2738 | |
a3a76ccc | 2739 | if Is_Access_Type (Arr_Typ) then |
245e87df | 2740 | Arr_Typ := Designated_Type (Arr_Typ); |
a3a76ccc | 2741 | end if; |
ee6ba406 | 2742 | end if; |
2743 | ||
2744 | if not Do_Range_Check (Expr) then | |
2745 | ||
2746 | -- Subscript reference. Check for Index_Checks suppressed | |
2747 | ||
2748 | if Is_Subscr_Ref then | |
2749 | ||
2750 | -- Check array type and its base type | |
2751 | ||
2752 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 2753 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 2754 | then |
2755 | return; | |
2756 | ||
2757 | -- Check array itself if it is an entity name | |
2758 | ||
2759 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 2760 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 2761 | then |
2762 | return; | |
2763 | ||
2764 | -- Check expression itself if it is an entity name | |
2765 | ||
2766 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2767 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2768 | then |
2769 | return; | |
2770 | end if; | |
2771 | ||
2772 | -- All other cases, check for Range_Checks suppressed | |
2773 | ||
2774 | else | |
2775 | -- Check target type and its base type | |
2776 | ||
2777 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 2778 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 2779 | then |
2780 | return; | |
2781 | ||
2782 | -- Check expression itself if it is an entity name | |
2783 | ||
2784 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 2785 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 2786 | then |
2787 | return; | |
2788 | ||
feff2f05 | 2789 | -- If Expr is part of an assignment statement, then check left |
2790 | -- side of assignment if it is an entity name. | |
ee6ba406 | 2791 | |
2792 | elsif Nkind (Parnt) = N_Assignment_Statement | |
2793 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 2794 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 2795 | then |
2796 | return; | |
2797 | end if; | |
2798 | end if; | |
2799 | end if; | |
2800 | ||
9dfe12ae | 2801 | -- Do not set range checks if they are killed |
2802 | ||
2803 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
2804 | and then Kill_Range_Check (Expr) | |
2805 | then | |
2806 | return; | |
2807 | end if; | |
2808 | ||
2809 | -- Do not set range checks for any values from System.Scalar_Values | |
39a0c1d3 | 2810 | -- since the whole idea of such values is to avoid checking them. |
9dfe12ae | 2811 | |
2812 | if Is_Entity_Name (Expr) | |
2813 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
2814 | then | |
2815 | return; | |
2816 | end if; | |
2817 | ||
ee6ba406 | 2818 | -- Now see if we need a check |
2819 | ||
2820 | if No (Source_Typ) then | |
2821 | S_Typ := Etype (Expr); | |
2822 | else | |
2823 | S_Typ := Source_Typ; | |
2824 | end if; | |
2825 | ||
2826 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
2827 | return; | |
2828 | end if; | |
2829 | ||
2830 | Is_Unconstrained_Subscr_Ref := | |
2831 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
2832 | ||
b40670e1 | 2833 | -- Special checks for floating-point type |
ee6ba406 | 2834 | |
b40670e1 | 2835 | if Is_Floating_Point_Type (S_Typ) then |
2836 | ||
2837 | -- Always do a range check if the source type includes infinities and | |
2838 | -- the target type does not include infinities. We do not do this if | |
2839 | -- range checks are killed. | |
2840 | ||
2841 | if Has_Infinities (S_Typ) | |
2842 | and then not Has_Infinities (Target_Typ) | |
2843 | then | |
2844 | Enable_Range_Check (Expr); | |
2845 | ||
2846 | -- Always do a range check for operators if option set | |
2847 | ||
2848 | elsif Check_Float_Overflow and then Nkind (Expr) in N_Op then | |
2849 | Enable_Range_Check (Expr); | |
2850 | end if; | |
ee6ba406 | 2851 | end if; |
2852 | ||
feff2f05 | 2853 | -- Return if we know expression is definitely in the range of the target |
2854 | -- type as determined by Determine_Range. Right now we only do this for | |
2855 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 2856 | |
f2a06be9 | 2857 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 2858 | |
feff2f05 | 2859 | -- Note: skip this if we are given a source_typ, since the point of |
2860 | -- supplying a Source_Typ is to stop us looking at the expression. | |
2861 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 2862 | |
2863 | if Is_Discrete_Type (Target_Typ) | |
2864 | and then Is_Discrete_Type (Etype (Expr)) | |
2865 | and then not Is_Unconstrained_Subscr_Ref | |
2866 | and then No (Source_Typ) | |
2867 | then | |
2868 | declare | |
2869 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
2870 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
2871 | Lo : Uint; | |
2872 | Hi : Uint; | |
2873 | ||
2874 | begin | |
2875 | if Compile_Time_Known_Value (Tlo) | |
2876 | and then Compile_Time_Known_Value (Thi) | |
2877 | then | |
9dfe12ae | 2878 | declare |
2879 | Lov : constant Uint := Expr_Value (Tlo); | |
2880 | Hiv : constant Uint := Expr_Value (Thi); | |
ee6ba406 | 2881 | |
9dfe12ae | 2882 | begin |
2883 | -- If range is null, we for sure have a constraint error | |
2884 | -- (we don't even need to look at the value involved, | |
2885 | -- since all possible values will raise CE). | |
2886 | ||
2887 | if Lov > Hiv then | |
2888 | Bad_Value; | |
2889 | return; | |
2890 | end if; | |
2891 | ||
2892 | -- Otherwise determine range of value | |
2893 | ||
9c486805 | 2894 | Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True); |
9dfe12ae | 2895 | |
2896 | if OK then | |
2897 | ||
2898 | -- If definitely in range, all OK | |
ee6ba406 | 2899 | |
ee6ba406 | 2900 | if Lo >= Lov and then Hi <= Hiv then |
2901 | return; | |
2902 | ||
9dfe12ae | 2903 | -- If definitely not in range, warn |
2904 | ||
ee6ba406 | 2905 | elsif Lov > Hi or else Hiv < Lo then |
2906 | Bad_Value; | |
2907 | return; | |
9dfe12ae | 2908 | |
2909 | -- Otherwise we don't know | |
2910 | ||
2911 | else | |
2912 | null; | |
ee6ba406 | 2913 | end if; |
9dfe12ae | 2914 | end if; |
2915 | end; | |
ee6ba406 | 2916 | end if; |
2917 | end; | |
2918 | end if; | |
2919 | ||
2920 | Int_Real := | |
2921 | Is_Floating_Point_Type (S_Typ) | |
2922 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
2923 | ||
2924 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 2925 | -- range of the target type. Note that if S_Typ is within the bounds |
2926 | -- of Target_Typ then this must be the case. This check is meaningful | |
2927 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 2928 | |
2929 | if not Is_Unconstrained_Subscr_Ref | |
b40670e1 | 2930 | and then Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) |
ee6ba406 | 2931 | and then |
7a1dabb3 | 2932 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ee6ba406 | 2933 | or else |
9c486805 | 2934 | Is_In_Range (Expr, Target_Typ, |
2935 | Assume_Valid => True, | |
b40670e1 | 2936 | Fixed_Int => Fixed_Int, |
2937 | Int_Real => Int_Real)) | |
ee6ba406 | 2938 | then |
2939 | return; | |
2940 | ||
9c486805 | 2941 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
2942 | Assume_Valid => True, | |
2943 | Fixed_Int => Fixed_Int, | |
2944 | Int_Real => Int_Real) | |
2945 | then | |
ee6ba406 | 2946 | Bad_Value; |
2947 | return; | |
2948 | ||
b40670e1 | 2949 | -- Floating-point case |
feff2f05 | 2950 | -- In the floating-point case, we only do range checks if the type is |
2951 | -- constrained. We definitely do NOT want range checks for unconstrained | |
2952 | -- types, since we want to have infinities | |
ee6ba406 | 2953 | |
9dfe12ae | 2954 | elsif Is_Floating_Point_Type (S_Typ) then |
b40670e1 | 2955 | |
2956 | -- Normally, we only do range checks if the type is constrained. We do | |
2957 | -- NOT want range checks for unconstrained types, since we want to have | |
2958 | -- infinities. Override this decision in Check_Float_Overflow mode. | |
2959 | ||
2960 | if Is_Constrained (S_Typ) or else Check_Float_Overflow then | |
9dfe12ae | 2961 | Enable_Range_Check (Expr); |
2962 | end if; | |
ee6ba406 | 2963 | |
9dfe12ae | 2964 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 2965 | |
2966 | else | |
2967 | Enable_Range_Check (Expr); | |
2968 | return; | |
2969 | end if; | |
ee6ba406 | 2970 | end Apply_Scalar_Range_Check; |
2971 | ||
2972 | ---------------------------------- | |
2973 | -- Apply_Selected_Length_Checks -- | |
2974 | ---------------------------------- | |
2975 | ||
2976 | procedure Apply_Selected_Length_Checks | |
2977 | (Ck_Node : Node_Id; | |
2978 | Target_Typ : Entity_Id; | |
2979 | Source_Typ : Entity_Id; | |
2980 | Do_Static : Boolean) | |
2981 | is | |
2982 | Cond : Node_Id; | |
2983 | R_Result : Check_Result; | |
2984 | R_Cno : Node_Id; | |
2985 | ||
2986 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2987 | Checks_On : constant Boolean := | |
b6341c67 | 2988 | (not Index_Checks_Suppressed (Target_Typ)) |
4098232e | 2989 | or else (not Length_Checks_Suppressed (Target_Typ)); |
ee6ba406 | 2990 | |
2991 | begin | |
4098232e | 2992 | -- Note: this means that we lose some useful warnings if the expander |
2993 | -- is not active, and we also lose these warnings in SPARK mode ??? | |
2994 | ||
a33565dd | 2995 | if not Expander_Active then |
ee6ba406 | 2996 | return; |
2997 | end if; | |
2998 | ||
2999 | R_Result := | |
3000 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3001 | ||
3002 | for J in 1 .. 2 loop | |
ee6ba406 | 3003 | R_Cno := R_Result (J); |
3004 | exit when No (R_Cno); | |
3005 | ||
3006 | -- A length check may mention an Itype which is attached to a | |
3007 | -- subsequent node. At the top level in a package this can cause | |
3008 | -- an order-of-elaboration problem, so we make sure that the itype | |
3009 | -- is referenced now. | |
3010 | ||
3011 | if Ekind (Current_Scope) = E_Package | |
3012 | and then Is_Compilation_Unit (Current_Scope) | |
3013 | then | |
3014 | Ensure_Defined (Target_Typ, Ck_Node); | |
3015 | ||
3016 | if Present (Source_Typ) then | |
3017 | Ensure_Defined (Source_Typ, Ck_Node); | |
3018 | ||
3019 | elsif Is_Itype (Etype (Ck_Node)) then | |
3020 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
3021 | end if; | |
3022 | end if; | |
3023 | ||
feff2f05 | 3024 | -- If the item is a conditional raise of constraint error, then have |
3025 | -- a look at what check is being performed and ??? | |
ee6ba406 | 3026 | |
3027 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3028 | and then Present (Condition (R_Cno)) | |
3029 | then | |
3030 | Cond := Condition (R_Cno); | |
3031 | ||
0577b0b1 | 3032 | -- Case where node does not now have a dynamic check |
ee6ba406 | 3033 | |
0577b0b1 | 3034 | if not Has_Dynamic_Length_Check (Ck_Node) then |
3035 | ||
3036 | -- If checks are on, just insert the check | |
3037 | ||
3038 | if Checks_On then | |
3039 | Insert_Action (Ck_Node, R_Cno); | |
3040 | ||
3041 | if not Do_Static then | |
3042 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
3043 | end if; | |
3044 | ||
3045 | -- If checks are off, then analyze the length check after | |
3046 | -- temporarily attaching it to the tree in case the relevant | |
6fb3c314 | 3047 | -- condition can be evaluated at compile time. We still want a |
0577b0b1 | 3048 | -- compile time warning in this case. |
3049 | ||
3050 | else | |
3051 | Set_Parent (R_Cno, Ck_Node); | |
3052 | Analyze (R_Cno); | |
ee6ba406 | 3053 | end if; |
ee6ba406 | 3054 | end if; |
3055 | ||
3056 | -- Output a warning if the condition is known to be True | |
3057 | ||
3058 | if Is_Entity_Name (Cond) | |
3059 | and then Entity (Cond) = Standard_True | |
3060 | then | |
3061 | Apply_Compile_Time_Constraint_Error | |
cb97ae5c | 3062 | (Ck_Node, "wrong length for array of}??", |
f15731c4 | 3063 | CE_Length_Check_Failed, |
ee6ba406 | 3064 | Ent => Target_Typ, |
3065 | Typ => Target_Typ); | |
3066 | ||
3067 | -- If we were only doing a static check, or if checks are not | |
3068 | -- on, then we want to delete the check, since it is not needed. | |
3069 | -- We do this by replacing the if statement by a null statement | |
3070 | ||
3071 | elsif Do_Static or else not Checks_On then | |
00c403ee | 3072 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3073 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3074 | end if; | |
3075 | ||
3076 | else | |
3077 | Install_Static_Check (R_Cno, Loc); | |
3078 | end if; | |
ee6ba406 | 3079 | end loop; |
ee6ba406 | 3080 | end Apply_Selected_Length_Checks; |
3081 | ||
3082 | --------------------------------- | |
3083 | -- Apply_Selected_Range_Checks -- | |
3084 | --------------------------------- | |
3085 | ||
3086 | procedure Apply_Selected_Range_Checks | |
3087 | (Ck_Node : Node_Id; | |
3088 | Target_Typ : Entity_Id; | |
3089 | Source_Typ : Entity_Id; | |
3090 | Do_Static : Boolean) | |
3091 | is | |
ee6ba406 | 3092 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
3093 | Checks_On : constant Boolean := | |
f9bcba0d | 3094 | not Index_Checks_Suppressed (Target_Typ) |
5372d110 | 3095 | or else |
3096 | not Range_Checks_Suppressed (Target_Typ); | |
f9bcba0d | 3097 | |
3098 | Cond : Node_Id; | |
3099 | R_Cno : Node_Id; | |
3100 | R_Result : Check_Result; | |
ee6ba406 | 3101 | |
3102 | begin | |
5372d110 | 3103 | if not Expander_Active or not Checks_On then |
ee6ba406 | 3104 | return; |
3105 | end if; | |
3106 | ||
3107 | R_Result := | |
3108 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
3109 | ||
3110 | for J in 1 .. 2 loop | |
ee6ba406 | 3111 | R_Cno := R_Result (J); |
3112 | exit when No (R_Cno); | |
3113 | ||
f9bcba0d | 3114 | -- The range check requires runtime evaluation. Depending on what its |
3115 | -- triggering condition is, the check may be converted into a compile | |
3116 | -- time constraint check. | |
ee6ba406 | 3117 | |
3118 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
3119 | and then Present (Condition (R_Cno)) | |
3120 | then | |
3121 | Cond := Condition (R_Cno); | |
3122 | ||
f9bcba0d | 3123 | -- Insert the range check before the related context. Note that |
3124 | -- this action analyses the triggering condition. | |
ee6ba406 | 3125 | |
f9bcba0d | 3126 | Insert_Action (Ck_Node, R_Cno); |
3127 | ||
3128 | -- This old code doesn't make sense, why is the context flagged as | |
3129 | -- requiring dynamic range checks now in the middle of generating | |
3130 | -- them ??? | |
3131 | ||
3132 | if not Do_Static then | |
3133 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
ee6ba406 | 3134 | end if; |
3135 | ||
f9bcba0d | 3136 | -- The triggering condition evaluates to True, the range check |
3137 | -- can be converted into a compile time constraint check. | |
ee6ba406 | 3138 | |
3139 | if Is_Entity_Name (Cond) | |
3140 | and then Entity (Cond) = Standard_True | |
3141 | then | |
feff2f05 | 3142 | -- Since an N_Range is technically not an expression, we have |
3143 | -- to set one of the bounds to C_E and then just flag the | |
3144 | -- N_Range. The warning message will point to the lower bound | |
3145 | -- and complain about a range, which seems OK. | |
ee6ba406 | 3146 | |
3147 | if Nkind (Ck_Node) = N_Range then | |
3148 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3149 | (Low_Bound (Ck_Node), |
3150 | "static range out of bounds of}??", | |
f15731c4 | 3151 | CE_Range_Check_Failed, |
ee6ba406 | 3152 | Ent => Target_Typ, |
3153 | Typ => Target_Typ); | |
3154 | ||
3155 | Set_Raises_Constraint_Error (Ck_Node); | |
3156 | ||
3157 | else | |
3158 | Apply_Compile_Time_Constraint_Error | |
5372d110 | 3159 | (Ck_Node, |
1581f2d7 | 3160 | "static value out of range of}??", |
f15731c4 | 3161 | CE_Range_Check_Failed, |
ee6ba406 | 3162 | Ent => Target_Typ, |
3163 | Typ => Target_Typ); | |
3164 | end if; | |
3165 | ||
3166 | -- If we were only doing a static check, or if checks are not | |
3167 | -- on, then we want to delete the check, since it is not needed. | |
3168 | -- We do this by replacing the if statement by a null statement | |
3169 | ||
f9bcba0d | 3170 | -- Why are we even generating checks if checks are turned off ??? |
3171 | ||
ee6ba406 | 3172 | elsif Do_Static or else not Checks_On then |
00c403ee | 3173 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 3174 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
3175 | end if; | |
3176 | ||
f9bcba0d | 3177 | -- The range check raises Constrant_Error explicitly |
3178 | ||
ee6ba406 | 3179 | else |
3180 | Install_Static_Check (R_Cno, Loc); | |
3181 | end if; | |
ee6ba406 | 3182 | end loop; |
ee6ba406 | 3183 | end Apply_Selected_Range_Checks; |
3184 | ||
3185 | ------------------------------- | |
3186 | -- Apply_Static_Length_Check -- | |
3187 | ------------------------------- | |
3188 | ||
3189 | procedure Apply_Static_Length_Check | |
3190 | (Expr : Node_Id; | |
3191 | Target_Typ : Entity_Id; | |
3192 | Source_Typ : Entity_Id := Empty) | |
3193 | is | |
3194 | begin | |
3195 | Apply_Selected_Length_Checks | |
3196 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
3197 | end Apply_Static_Length_Check; | |
3198 | ||
3199 | ------------------------------------- | |
3200 | -- Apply_Subscript_Validity_Checks -- | |
3201 | ------------------------------------- | |
3202 | ||
3203 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
3204 | Sub : Node_Id; | |
3205 | ||
3206 | begin | |
3207 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
3208 | ||
3209 | -- Loop through subscripts | |
3210 | ||
3211 | Sub := First (Expressions (Expr)); | |
3212 | while Present (Sub) loop | |
3213 | ||
feff2f05 | 3214 | -- Check one subscript. Note that we do not worry about enumeration |
3215 | -- type with holes, since we will convert the value to a Pos value | |
3216 | -- for the subscript, and that convert will do the necessary validity | |
3217 | -- check. | |
ee6ba406 | 3218 | |
3219 | Ensure_Valid (Sub, Holes_OK => True); | |
3220 | ||
3221 | -- Move to next subscript | |
3222 | ||
3223 | Sub := Next (Sub); | |
3224 | end loop; | |
3225 | end Apply_Subscript_Validity_Checks; | |
3226 | ||
3227 | ---------------------------------- | |
3228 | -- Apply_Type_Conversion_Checks -- | |
3229 | ---------------------------------- | |
3230 | ||
3231 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
3232 | Target_Type : constant Entity_Id := Etype (N); | |
3233 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 3234 | Expr : constant Node_Id := Expression (N); |
f4532fe1 | 3235 | |
3236 | Expr_Type : constant Entity_Id := Underlying_Type (Etype (Expr)); | |
141d591a | 3237 | -- Note: if Etype (Expr) is a private type without discriminants, its |
3238 | -- full view might have discriminants with defaults, so we need the | |
3239 | -- full view here to retrieve the constraints. | |
ee6ba406 | 3240 | |
3241 | begin | |
3242 | if Inside_A_Generic then | |
3243 | return; | |
3244 | ||
f15731c4 | 3245 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 3246 | -- situations of incomplete trees that blow things up. |
3247 | ||
f15731c4 | 3248 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 3249 | return; |
3250 | ||
ea822fd4 | 3251 | -- Never generate discriminant checks for Unchecked_Union types |
3252 | ||
3253 | elsif Present (Expr_Type) | |
3254 | and then Is_Unchecked_Union (Expr_Type) | |
3255 | then | |
3256 | return; | |
3257 | ||
feff2f05 | 3258 | -- Scalar type conversions of the form Target_Type (Expr) require a |
3259 | -- range check if we cannot be sure that Expr is in the base type of | |
3260 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
3261 | -- are not quite the same condition from an implementation point of | |
3262 | -- view, but clearly the second includes the first. | |
ee6ba406 | 3263 | |
3264 | elsif Is_Scalar_Type (Target_Type) then | |
3265 | declare | |
3266 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 3267 | -- If the Conversion_OK flag on the type conversion is set and no |
ea822fd4 | 3268 | -- floating-point type is involved in the type conversion then |
3269 | -- fixed-point values must be read as integral values. | |
ee6ba406 | 3270 | |
5329ca64 | 3271 | Float_To_Int : constant Boolean := |
b6341c67 | 3272 | Is_Floating_Point_Type (Expr_Type) |
3273 | and then Is_Integer_Type (Target_Type); | |
5329ca64 | 3274 | |
ee6ba406 | 3275 | begin |
ee6ba406 | 3276 | if not Overflow_Checks_Suppressed (Target_Base) |
0df9d43f | 3277 | and then not Overflow_Checks_Suppressed (Target_Type) |
e254d721 | 3278 | and then not |
7a1dabb3 | 3279 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 3280 | and then not Float_To_Int |
ee6ba406 | 3281 | then |
00c403ee | 3282 | Activate_Overflow_Check (N); |
ee6ba406 | 3283 | end if; |
3284 | ||
3285 | if not Range_Checks_Suppressed (Target_Type) | |
3286 | and then not Range_Checks_Suppressed (Expr_Type) | |
3287 | then | |
5329ca64 | 3288 | if Float_To_Int then |
3289 | Apply_Float_Conversion_Check (Expr, Target_Type); | |
3290 | else | |
3291 | Apply_Scalar_Range_Check | |
3292 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
798afddc | 3293 | |
3294 | -- If the target type has predicates, we need to indicate | |
ea822fd4 | 3295 | -- the need for a check, even if Determine_Range finds that |
3296 | -- the value is within bounds. This may be the case e.g for | |
3297 | -- a division with a constant denominator. | |
798afddc | 3298 | |
3299 | if Has_Predicates (Target_Type) then | |
3300 | Enable_Range_Check (Expr); | |
3301 | end if; | |
5329ca64 | 3302 | end if; |
ee6ba406 | 3303 | end if; |
3304 | end; | |
3305 | ||
3306 | elsif Comes_From_Source (N) | |
f40f9731 | 3307 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 3308 | and then Is_Record_Type (Target_Type) |
3309 | and then Is_Derived_Type (Target_Type) | |
3310 | and then not Is_Tagged_Type (Target_Type) | |
3311 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 3312 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 3313 | then |
141d591a | 3314 | -- An unconstrained derived type may have inherited discriminant. |
9dfe12ae | 3315 | -- Build an actual discriminant constraint list using the stored |
ee6ba406 | 3316 | -- constraint, to verify that the expression of the parent type |
ea822fd4 | 3317 | -- satisfies the constraints imposed by the (unconstrained) derived |
3318 | -- type. This applies to value conversions, not to view conversions | |
3319 | -- of tagged types. | |
ee6ba406 | 3320 | |
3321 | declare | |
9dfe12ae | 3322 | Loc : constant Source_Ptr := Sloc (N); |
3323 | Cond : Node_Id; | |
3324 | Constraint : Elmt_Id; | |
3325 | Discr_Value : Node_Id; | |
3326 | Discr : Entity_Id; | |
3327 | ||
3328 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
3329 | Old_Constraints : constant Elist_Id := | |
b6341c67 | 3330 | Discriminant_Constraint (Expr_Type); |
ee6ba406 | 3331 | |
3332 | begin | |
9dfe12ae | 3333 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 3334 | while Present (Constraint) loop |
3335 | Discr_Value := Node (Constraint); | |
3336 | ||
3337 | if Is_Entity_Name (Discr_Value) | |
3338 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
3339 | then | |
3340 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
3341 | ||
3342 | if Present (Discr) | |
3343 | and then Scope (Discr) = Base_Type (Expr_Type) | |
3344 | then | |
3345 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 3346 | -- Value of original discriminant in expression. If the |
3347 | -- new discriminant has been used to constrain more than | |
3348 | -- one of the stored discriminants, this will provide the | |
3349 | -- required consistency check. | |
ee6ba406 | 3350 | |
55868293 | 3351 | Append_Elmt |
3352 | (Make_Selected_Component (Loc, | |
3353 | Prefix => | |
9dfe12ae | 3354 | Duplicate_Subexpr_No_Checks |
3355 | (Expr, Name_Req => True), | |
ee6ba406 | 3356 | Selector_Name => |
3357 | Make_Identifier (Loc, Chars (Discr))), | |
55868293 | 3358 | New_Constraints); |
ee6ba406 | 3359 | |
3360 | else | |
3361 | -- Discriminant of more remote ancestor ??? | |
3362 | ||
3363 | return; | |
3364 | end if; | |
3365 | ||
feff2f05 | 3366 | -- Derived type definition has an explicit value for this |
3367 | -- stored discriminant. | |
ee6ba406 | 3368 | |
3369 | else | |
3370 | Append_Elmt | |
9dfe12ae | 3371 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
3372 | New_Constraints); | |
ee6ba406 | 3373 | end if; |
3374 | ||
3375 | Next_Elmt (Constraint); | |
3376 | end loop; | |
3377 | ||
3378 | -- Use the unconstrained expression type to retrieve the | |
3379 | -- discriminants of the parent, and apply momentarily the | |
3380 | -- discriminant constraint synthesized above. | |
3381 | ||
3382 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
3383 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
3384 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
3385 | ||
3386 | Insert_Action (N, | |
f15731c4 | 3387 | Make_Raise_Constraint_Error (Loc, |
3388 | Condition => Cond, | |
3389 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 3390 | end; |
3391 | ||
175a6969 | 3392 | -- For arrays, checks are set now, but conversions are applied during |
3393 | -- expansion, to take into accounts changes of representation. The | |
3394 | -- checks become range checks on the base type or length checks on the | |
3395 | -- subtype, depending on whether the target type is unconstrained or | |
8e802312 | 3396 | -- constrained. Note that the range check is put on the expression of a |
3397 | -- type conversion, while the length check is put on the type conversion | |
3398 | -- itself. | |
175a6969 | 3399 | |
3400 | elsif Is_Array_Type (Target_Type) then | |
3401 | if Is_Constrained (Target_Type) then | |
3402 | Set_Do_Length_Check (N); | |
3403 | else | |
3404 | Set_Do_Range_Check (Expr); | |
3405 | end if; | |
ee6ba406 | 3406 | end if; |
ee6ba406 | 3407 | end Apply_Type_Conversion_Checks; |
3408 | ||
3409 | ---------------------------------------------- | |
3410 | -- Apply_Universal_Integer_Attribute_Checks -- | |
3411 | ---------------------------------------------- | |
3412 | ||
3413 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
3414 | Loc : constant Source_Ptr := Sloc (N); | |
3415 | Typ : constant Entity_Id := Etype (N); | |
3416 | ||
3417 | begin | |
3418 | if Inside_A_Generic then | |
3419 | return; | |
3420 | ||
3421 | -- Nothing to do if checks are suppressed | |
3422 | ||
3423 | elsif Range_Checks_Suppressed (Typ) | |
3424 | and then Overflow_Checks_Suppressed (Typ) | |
3425 | then | |
3426 | return; | |
3427 | ||
3428 | -- Nothing to do if the attribute does not come from source. The | |
3429 | -- internal attributes we generate of this type do not need checks, | |
3430 | -- and furthermore the attempt to check them causes some circular | |
3431 | -- elaboration orders when dealing with packed types. | |
3432 | ||
3433 | elsif not Comes_From_Source (N) then | |
3434 | return; | |
3435 | ||
9dfe12ae | 3436 | -- If the prefix is a selected component that depends on a discriminant |
3437 | -- the check may improperly expose a discriminant instead of using | |
3438 | -- the bounds of the object itself. Set the type of the attribute to | |
3439 | -- the base type of the context, so that a check will be imposed when | |
3440 | -- needed (e.g. if the node appears as an index). | |
3441 | ||
3442 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
3443 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
3444 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
3445 | then | |
3446 | Set_Etype (N, Base_Type (Typ)); | |
3447 | ||
feff2f05 | 3448 | -- Otherwise, replace the attribute node with a type conversion node |
3449 | -- whose expression is the attribute, retyped to universal integer, and | |
3450 | -- whose subtype mark is the target type. The call to analyze this | |
3451 | -- conversion will set range and overflow checks as required for proper | |
3452 | -- detection of an out of range value. | |
ee6ba406 | 3453 | |
3454 | else | |
3455 | Set_Etype (N, Universal_Integer); | |
3456 | Set_Analyzed (N, True); | |
3457 | ||
3458 | Rewrite (N, | |
3459 | Make_Type_Conversion (Loc, | |
3460 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
3461 | Expression => Relocate_Node (N))); | |
3462 | ||
3463 | Analyze_And_Resolve (N, Typ); | |
3464 | return; | |
3465 | end if; | |
ee6ba406 | 3466 | end Apply_Universal_Integer_Attribute_Checks; |
3467 | ||
07c191b0 | 3468 | ------------------------------------- |
3469 | -- Atomic_Synchronization_Disabled -- | |
3470 | ------------------------------------- | |
3471 | ||
3472 | -- Note: internally Disable/Enable_Atomic_Synchronization is implemented | |
3473 | -- using a bogus check called Atomic_Synchronization. This is to make it | |
3474 | -- more convenient to get exactly the same semantics as [Un]Suppress. | |
3475 | ||
3476 | function Atomic_Synchronization_Disabled (E : Entity_Id) return Boolean is | |
3477 | begin | |
b444f81d | 3478 | -- If debug flag d.e is set, always return False, i.e. all atomic sync |
3479 | -- looks enabled, since it is never disabled. | |
3480 | ||
3481 | if Debug_Flag_Dot_E then | |
3482 | return False; | |
3483 | ||
3484 | -- If debug flag d.d is set then always return True, i.e. all atomic | |
3485 | -- sync looks disabled, since it always tests True. | |
3486 | ||
3487 | elsif Debug_Flag_Dot_D then | |
3488 | return True; | |
3489 | ||
3490 | -- If entity present, then check result for that entity | |
3491 | ||
3492 | elsif Present (E) and then Checks_May_Be_Suppressed (E) then | |
07c191b0 | 3493 | return Is_Check_Suppressed (E, Atomic_Synchronization); |
b444f81d | 3494 | |
3495 | -- Otherwise result depends on current scope setting | |
3496 | ||
07c191b0 | 3497 | else |
fafc6b97 | 3498 | return Scope_Suppress.Suppress (Atomic_Synchronization); |
07c191b0 | 3499 | end if; |
3500 | end Atomic_Synchronization_Disabled; | |
3501 | ||
ee6ba406 | 3502 | ------------------------------- |
3503 | -- Build_Discriminant_Checks -- | |
3504 | ------------------------------- | |
3505 | ||
3506 | function Build_Discriminant_Checks | |
3507 | (N : Node_Id; | |
314a23b6 | 3508 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 3509 | is |
3510 | Loc : constant Source_Ptr := Sloc (N); | |
3511 | Cond : Node_Id; | |
3512 | Disc : Elmt_Id; | |
3513 | Disc_Ent : Entity_Id; | |
9dfe12ae | 3514 | Dref : Node_Id; |
ee6ba406 | 3515 | Dval : Node_Id; |
3516 | ||
84d0d4a5 | 3517 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
3518 | ||
3519 | ---------------------------------- | |
3520 | -- Aggregate_Discriminant_Value -- | |
3521 | ---------------------------------- | |
3522 | ||
3523 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
3524 | Assoc : Node_Id; | |
3525 | ||
3526 | begin | |
feff2f05 | 3527 | -- The aggregate has been normalized with named associations. We use |
3528 | -- the Chars field to locate the discriminant to take into account | |
3529 | -- discriminants in derived types, which carry the same name as those | |
3530 | -- in the parent. | |
84d0d4a5 | 3531 | |
3532 | Assoc := First (Component_Associations (N)); | |
3533 | while Present (Assoc) loop | |
3534 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
3535 | return Expression (Assoc); | |
3536 | else | |
3537 | Next (Assoc); | |
3538 | end if; | |
3539 | end loop; | |
3540 | ||
3541 | -- Discriminant must have been found in the loop above | |
3542 | ||
3543 | raise Program_Error; | |
3544 | end Aggregate_Discriminant_Val; | |
3545 | ||
3546 | -- Start of processing for Build_Discriminant_Checks | |
3547 | ||
ee6ba406 | 3548 | begin |
84d0d4a5 | 3549 | -- Loop through discriminants evolving the condition |
3550 | ||
ee6ba406 | 3551 | Cond := Empty; |
3552 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
3553 | ||
9dfe12ae | 3554 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 3555 | |
3556 | if Is_Private_Type (T_Typ) | |
3557 | and then No (Full_View (T_Typ)) | |
3558 | then | |
3559 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
3560 | else | |
3561 | Disc_Ent := First_Discriminant (T_Typ); | |
3562 | end if; | |
3563 | ||
3564 | while Present (Disc) loop | |
ee6ba406 | 3565 | Dval := Node (Disc); |
3566 | ||
3567 | if Nkind (Dval) = N_Identifier | |
3568 | and then Ekind (Entity (Dval)) = E_Discriminant | |
3569 | then | |
3570 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
3571 | else | |
9dfe12ae | 3572 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 3573 | end if; |
3574 | ||
00f91aef | 3575 | -- If we have an Unchecked_Union node, we can infer the discriminants |
3576 | -- of the node. | |
9dfe12ae | 3577 | |
00f91aef | 3578 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
3579 | Dref := New_Copy ( | |
3580 | Get_Discriminant_Value ( | |
3581 | First_Discriminant (T_Typ), | |
3582 | T_Typ, | |
3583 | Stored_Constraint (T_Typ))); | |
3584 | ||
84d0d4a5 | 3585 | elsif Nkind (N) = N_Aggregate then |
3586 | Dref := | |
3587 | Duplicate_Subexpr_No_Checks | |
3588 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
3589 | ||
00f91aef | 3590 | else |
3591 | Dref := | |
3592 | Make_Selected_Component (Loc, | |
20cf157b | 3593 | Prefix => |
00f91aef | 3594 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
20cf157b | 3595 | Selector_Name => Make_Identifier (Loc, Chars (Disc_Ent))); |
00f91aef | 3596 | |
3597 | Set_Is_In_Discriminant_Check (Dref); | |
3598 | end if; | |
9dfe12ae | 3599 | |
ee6ba406 | 3600 | Evolve_Or_Else (Cond, |
3601 | Make_Op_Ne (Loc, | |
20cf157b | 3602 | Left_Opnd => Dref, |
ee6ba406 | 3603 | Right_Opnd => Dval)); |
3604 | ||
3605 | Next_Elmt (Disc); | |
3606 | Next_Discriminant (Disc_Ent); | |
3607 | end loop; | |
3608 | ||
3609 | return Cond; | |
3610 | end Build_Discriminant_Checks; | |
3611 | ||
13dbf220 | 3612 | ------------------ |
3613 | -- Check_Needed -- | |
3614 | ------------------ | |
3615 | ||
3616 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
3617 | N : Node_Id; | |
3618 | P : Node_Id; | |
3619 | K : Node_Kind; | |
3620 | L : Node_Id; | |
3621 | R : Node_Id; | |
3622 | ||
9b2068d4 | 3623 | function Left_Expression (Op : Node_Id) return Node_Id; |
3624 | -- Return the relevant expression from the left operand of the given | |
3625 | -- short circuit form: this is LO itself, except if LO is a qualified | |
3626 | -- expression, a type conversion, or an expression with actions, in | |
3627 | -- which case this is Left_Expression (Expression (LO)). | |
3628 | ||
3629 | --------------------- | |
3630 | -- Left_Expression -- | |
3631 | --------------------- | |
3632 | ||
3633 | function Left_Expression (Op : Node_Id) return Node_Id is | |
3634 | LE : Node_Id := Left_Opnd (Op); | |
3635 | begin | |
20cf157b | 3636 | while Nkind_In (LE, N_Qualified_Expression, |
3637 | N_Type_Conversion, | |
3638 | N_Expression_With_Actions) | |
9b2068d4 | 3639 | loop |
3640 | LE := Expression (LE); | |
3641 | end loop; | |
3642 | ||
3643 | return LE; | |
3644 | end Left_Expression; | |
3645 | ||
3646 | -- Start of processing for Check_Needed | |
3647 | ||
13dbf220 | 3648 | begin |
3649 | -- Always check if not simple entity | |
3650 | ||
3651 | if Nkind (Nod) not in N_Has_Entity | |
3652 | or else not Comes_From_Source (Nod) | |
3653 | then | |
3654 | return True; | |
3655 | end if; | |
3656 | ||
3657 | -- Look up tree for short circuit | |
3658 | ||
3659 | N := Nod; | |
3660 | loop | |
3661 | P := Parent (N); | |
3662 | K := Nkind (P); | |
3663 | ||
7b17e51b | 3664 | -- Done if out of subexpression (note that we allow generated stuff |
3665 | -- such as itype declarations in this context, to keep the loop going | |
3666 | -- since we may well have generated such stuff in complex situations. | |
3667 | -- Also done if no parent (probably an error condition, but no point | |
39a0c1d3 | 3668 | -- in behaving nasty if we find it). |
7b17e51b | 3669 | |
3670 | if No (P) | |
3671 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
3672 | then | |
13dbf220 | 3673 | return True; |
3674 | ||
7b17e51b | 3675 | -- Or/Or Else case, where test is part of the right operand, or is |
3676 | -- part of one of the actions associated with the right operand, and | |
3677 | -- the left operand is an equality test. | |
13dbf220 | 3678 | |
7b17e51b | 3679 | elsif K = N_Op_Or then |
13dbf220 | 3680 | exit when N = Right_Opnd (P) |
9b2068d4 | 3681 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3682 | |
7b17e51b | 3683 | elsif K = N_Or_Else then |
3684 | exit when (N = Right_Opnd (P) | |
3685 | or else | |
3686 | (Is_List_Member (N) | |
3687 | and then List_Containing (N) = Actions (P))) | |
9b2068d4 | 3688 | and then Nkind (Left_Expression (P)) = N_Op_Eq; |
13dbf220 | 3689 | |
7b17e51b | 3690 | -- Similar test for the And/And then case, where the left operand |
3691 | -- is an inequality test. | |
3692 | ||
3693 | elsif K = N_Op_And then | |
13dbf220 | 3694 | exit when N = Right_Opnd (P) |
9b2068d4 | 3695 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
7b17e51b | 3696 | |
3697 | elsif K = N_And_Then then | |
3698 | exit when (N = Right_Opnd (P) | |
3699 | or else | |
3700 | (Is_List_Member (N) | |
20cf157b | 3701 | and then List_Containing (N) = Actions (P))) |
9b2068d4 | 3702 | and then Nkind (Left_Expression (P)) = N_Op_Ne; |
13dbf220 | 3703 | end if; |
3704 | ||
3705 | N := P; | |
3706 | end loop; | |
3707 | ||
3708 | -- If we fall through the loop, then we have a conditional with an | |
9b2068d4 | 3709 | -- appropriate test as its left operand, so look further. |
3710 | ||
3711 | L := Left_Expression (P); | |
3712 | ||
3713 | -- L is an "=" or "/=" operator: extract its operands | |
13dbf220 | 3714 | |
13dbf220 | 3715 | R := Right_Opnd (L); |
3716 | L := Left_Opnd (L); | |
3717 | ||
3718 | -- Left operand of test must match original variable | |
3719 | ||
20cf157b | 3720 | if Nkind (L) not in N_Has_Entity or else Entity (L) /= Entity (Nod) then |
13dbf220 | 3721 | return True; |
3722 | end if; | |
3723 | ||
2af58f67 | 3724 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 3725 | |
3726 | case Check is | |
3727 | when Access_Check => | |
2af58f67 | 3728 | if not Known_Null (R) then |
13dbf220 | 3729 | return True; |
3730 | end if; | |
3731 | ||
3732 | when Division_Check => | |
3733 | if not Compile_Time_Known_Value (R) | |
3734 | or else Expr_Value (R) /= Uint_0 | |
3735 | then | |
3736 | return True; | |
3737 | end if; | |
2af58f67 | 3738 | |
3739 | when others => | |
3740 | raise Program_Error; | |
13dbf220 | 3741 | end case; |
3742 | ||
3743 | -- Here we have the optimizable case, warn if not short-circuited | |
3744 | ||
3745 | if K = N_Op_And or else K = N_Op_Or then | |
c4968aa2 | 3746 | Error_Msg_Warn := SPARK_Mode /= On; |
4098232e | 3747 | |
13dbf220 | 3748 | case Check is |
3749 | when Access_Check => | |
4098232e | 3750 | if GNATprove_Mode then |
3751 | Error_Msg_N | |
3752 | ("Constraint_Error might have been raised (access check)", | |
3753 | Parent (Nod)); | |
3754 | else | |
3755 | Error_Msg_N | |
3756 | ("Constraint_Error may be raised (access check)??", | |
3757 | Parent (Nod)); | |
3758 | end if; | |
3759 | ||
13dbf220 | 3760 | when Division_Check => |
4098232e | 3761 | if GNATprove_Mode then |
3762 | Error_Msg_N | |
3763 | ("Constraint_Error might have been raised (zero divide)", | |
3764 | Parent (Nod)); | |
3765 | else | |
3766 | Error_Msg_N | |
3767 | ("Constraint_Error may be raised (zero divide)??", | |
3768 | Parent (Nod)); | |
3769 | end if; | |
2af58f67 | 3770 | |
3771 | when others => | |
3772 | raise Program_Error; | |
13dbf220 | 3773 | end case; |
3774 | ||
3775 | if K = N_Op_And then | |
e977c0cf | 3776 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3777 | ("use `AND THEN` instead of AND??", P); |
13dbf220 | 3778 | else |
e977c0cf | 3779 | Error_Msg_N -- CODEFIX |
cb97ae5c | 3780 | ("use `OR ELSE` instead of OR??", P); |
13dbf220 | 3781 | end if; |
3782 | ||
6fb3c314 | 3783 | -- If not short-circuited, we need the check |
13dbf220 | 3784 | |
3785 | return True; | |
3786 | ||
3787 | -- If short-circuited, we can omit the check | |
3788 | ||
3789 | else | |
3790 | return False; | |
3791 | end if; | |
3792 | end Check_Needed; | |
3793 | ||
ee6ba406 | 3794 | ----------------------------------- |
3795 | -- Check_Valid_Lvalue_Subscripts -- | |
3796 | ----------------------------------- | |
3797 | ||
3798 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
3799 | begin | |
3800 | -- Skip this if range checks are suppressed | |
3801 | ||
3802 | if Range_Checks_Suppressed (Etype (Expr)) then | |
3803 | return; | |
3804 | ||
feff2f05 | 3805 | -- Only do this check for expressions that come from source. We assume |
3806 | -- that expander generated assignments explicitly include any necessary | |
3807 | -- checks. Note that this is not just an optimization, it avoids | |
39a0c1d3 | 3808 | -- infinite recursions. |
ee6ba406 | 3809 | |
3810 | elsif not Comes_From_Source (Expr) then | |
3811 | return; | |
3812 | ||
3813 | -- For a selected component, check the prefix | |
3814 | ||
3815 | elsif Nkind (Expr) = N_Selected_Component then | |
3816 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3817 | return; | |
3818 | ||
3819 | -- Case of indexed component | |
3820 | ||
3821 | elsif Nkind (Expr) = N_Indexed_Component then | |
3822 | Apply_Subscript_Validity_Checks (Expr); | |
3823 | ||
feff2f05 | 3824 | -- Prefix may itself be or contain an indexed component, and these |
3825 | -- subscripts need checking as well. | |
ee6ba406 | 3826 | |
3827 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
3828 | end if; | |
3829 | end Check_Valid_Lvalue_Subscripts; | |
3830 | ||
fa7497e8 | 3831 | ---------------------------------- |
3832 | -- Null_Exclusion_Static_Checks -- | |
3833 | ---------------------------------- | |
3834 | ||
3835 | procedure Null_Exclusion_Static_Checks (N : Node_Id) is | |
0577b0b1 | 3836 | Error_Node : Node_Id; |
3837 | Expr : Node_Id; | |
3838 | Has_Null : constant Boolean := Has_Null_Exclusion (N); | |
3839 | K : constant Node_Kind := Nkind (N); | |
3840 | Typ : Entity_Id; | |
fa7497e8 | 3841 | |
13dbf220 | 3842 | begin |
0577b0b1 | 3843 | pragma Assert |
ea822fd4 | 3844 | (Nkind_In (K, N_Component_Declaration, |
3845 | N_Discriminant_Specification, | |
3846 | N_Function_Specification, | |
3847 | N_Object_Declaration, | |
3848 | N_Parameter_Specification)); | |
0577b0b1 | 3849 | |
3850 | if K = N_Function_Specification then | |
3851 | Typ := Etype (Defining_Entity (N)); | |
3852 | else | |
3853 | Typ := Etype (Defining_Identifier (N)); | |
3854 | end if; | |
fa7497e8 | 3855 | |
13dbf220 | 3856 | case K is |
13dbf220 | 3857 | when N_Component_Declaration => |
3858 | if Present (Access_Definition (Component_Definition (N))) then | |
0577b0b1 | 3859 | Error_Node := Component_Definition (N); |
13dbf220 | 3860 | else |
0577b0b1 | 3861 | Error_Node := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 3862 | end if; |
5329ca64 | 3863 | |
0577b0b1 | 3864 | when N_Discriminant_Specification => |
3865 | Error_Node := Discriminant_Type (N); | |
3866 | ||
3867 | when N_Function_Specification => | |
3868 | Error_Node := Result_Definition (N); | |
3869 | ||
3870 | when N_Object_Declaration => | |
3871 | Error_Node := Object_Definition (N); | |
3872 | ||
3873 | when N_Parameter_Specification => | |
3874 | Error_Node := Parameter_Type (N); | |
3875 | ||
13dbf220 | 3876 | when others => |
3877 | raise Program_Error; | |
3878 | end case; | |
5329ca64 | 3879 | |
0577b0b1 | 3880 | if Has_Null then |
5329ca64 | 3881 | |
0577b0b1 | 3882 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
3883 | -- applied to an access [sub]type. | |
5329ca64 | 3884 | |
0577b0b1 | 3885 | if not Is_Access_Type (Typ) then |
503f7fd3 | 3886 | Error_Msg_N |
00c403ee | 3887 | ("`NOT NULL` allowed only for an access type", Error_Node); |
5329ca64 | 3888 | |
feff2f05 | 3889 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 3890 | -- be applied to a [sub]type that does not exclude null already. |
3891 | ||
3892 | elsif Can_Never_Be_Null (Typ) | |
d16989f1 | 3893 | and then Comes_From_Source (Typ) |
0577b0b1 | 3894 | then |
503f7fd3 | 3895 | Error_Msg_NE |
00c403ee | 3896 | ("`NOT NULL` not allowed (& already excludes null)", |
3897 | Error_Node, Typ); | |
0577b0b1 | 3898 | end if; |
13dbf220 | 3899 | end if; |
5329ca64 | 3900 | |
cc60bd16 | 3901 | -- Check that null-excluding objects are always initialized, except for |
3902 | -- deferred constants, for which the expression will appear in the full | |
3903 | -- declaration. | |
13dbf220 | 3904 | |
3905 | if K = N_Object_Declaration | |
84d0d4a5 | 3906 | and then No (Expression (N)) |
cc60bd16 | 3907 | and then not Constant_Present (N) |
feff2f05 | 3908 | and then not No_Initialization (N) |
13dbf220 | 3909 | then |
feff2f05 | 3910 | -- Add an expression that assigns null. This node is needed by |
3911 | -- Apply_Compile_Time_Constraint_Error, which will replace this with | |
3912 | -- a Constraint_Error node. | |
13dbf220 | 3913 | |
3914 | Set_Expression (N, Make_Null (Sloc (N))); | |
3915 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
5329ca64 | 3916 | |
13dbf220 | 3917 | Apply_Compile_Time_Constraint_Error |
3918 | (N => Expression (N), | |
cb97ae5c | 3919 | Msg => |
3920 | "(Ada 2005) null-excluding objects must be initialized??", | |
13dbf220 | 3921 | Reason => CE_Null_Not_Allowed); |
3922 | end if; | |
5329ca64 | 3923 | |
cc60bd16 | 3924 | -- Check that a null-excluding component, formal or object is not being |
3925 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 3926 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 3927 | |
0577b0b1 | 3928 | if K /= N_Function_Specification then |
3929 | Expr := Expression (N); | |
5329ca64 | 3930 | |
2af58f67 | 3931 | if Present (Expr) and then Known_Null (Expr) then |
13dbf220 | 3932 | case K is |
0577b0b1 | 3933 | when N_Component_Declaration | |
3934 | N_Discriminant_Specification => | |
7189d17f | 3935 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3936 | (N => Expr, |
4098232e | 3937 | Msg => "(Ada 2005) null not allowed " |
3938 | & "in null-excluding components??", | |
0577b0b1 | 3939 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3940 | |
0577b0b1 | 3941 | when N_Object_Declaration => |
7189d17f | 3942 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3943 | (N => Expr, |
4098232e | 3944 | Msg => "(Ada 2005) null not allowed " |
1581f2d7 | 3945 | & "in null-excluding objects??", |
0577b0b1 | 3946 | Reason => CE_Null_Not_Allowed); |
5329ca64 | 3947 | |
0577b0b1 | 3948 | when N_Parameter_Specification => |
7189d17f | 3949 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 3950 | (N => Expr, |
4098232e | 3951 | Msg => "(Ada 2005) null not allowed " |
3952 | & "in null-excluding formals??", | |
0577b0b1 | 3953 | Reason => CE_Null_Not_Allowed); |
13dbf220 | 3954 | |
3955 | when others => | |
3956 | null; | |
5329ca64 | 3957 | end case; |
3958 | end if; | |
0577b0b1 | 3959 | end if; |
fa7497e8 | 3960 | end Null_Exclusion_Static_Checks; |
3961 | ||
9dfe12ae | 3962 | ---------------------------------- |
3963 | -- Conditional_Statements_Begin -- | |
3964 | ---------------------------------- | |
3965 | ||
3966 | procedure Conditional_Statements_Begin is | |
3967 | begin | |
3968 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
3969 | ||
feff2f05 | 3970 | -- If stack overflows, kill all checks, that way we know to simply reset |
3971 | -- the number of saved checks to zero on return. This should never occur | |
3972 | -- in practice. | |
9dfe12ae | 3973 | |
3974 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
3975 | Kill_All_Checks; | |
3976 | ||
feff2f05 | 3977 | -- In the normal case, we just make a new stack entry saving the current |
3978 | -- number of saved checks for a later restore. | |
9dfe12ae | 3979 | |
3980 | else | |
3981 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
3982 | ||
3983 | if Debug_Flag_CC then | |
3984 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
3985 | Num_Saved_Checks); | |
3986 | end if; | |
3987 | end if; | |
3988 | end Conditional_Statements_Begin; | |
3989 | ||
3990 | -------------------------------- | |
3991 | -- Conditional_Statements_End -- | |
3992 | -------------------------------- | |
3993 | ||
3994 | procedure Conditional_Statements_End is | |
3995 | begin | |
3996 | pragma Assert (Saved_Checks_TOS > 0); | |
3997 | ||
feff2f05 | 3998 | -- If the saved checks stack overflowed, then we killed all checks, so |
3999 | -- setting the number of saved checks back to zero is correct. This | |
4000 | -- should never occur in practice. | |
9dfe12ae | 4001 | |
4002 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
4003 | Num_Saved_Checks := 0; | |
4004 | ||
feff2f05 | 4005 | -- In the normal case, restore the number of saved checks from the top |
4006 | -- stack entry. | |
9dfe12ae | 4007 | |
4008 | else | |
4009 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
20cf157b | 4010 | |
9dfe12ae | 4011 | if Debug_Flag_CC then |
4012 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
4013 | Num_Saved_Checks); | |
4014 | end if; | |
4015 | end if; | |
4016 | ||
4017 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
4018 | end Conditional_Statements_End; | |
4019 | ||
3cce7f32 | 4020 | ------------------------- |
4021 | -- Convert_From_Bignum -- | |
4022 | ------------------------- | |
4023 | ||
4024 | function Convert_From_Bignum (N : Node_Id) return Node_Id is | |
4025 | Loc : constant Source_Ptr := Sloc (N); | |
4026 | ||
4027 | begin | |
4028 | pragma Assert (Is_RTE (Etype (N), RE_Bignum)); | |
4029 | ||
4030 | -- Construct call From Bignum | |
4031 | ||
4032 | return | |
4033 | Make_Function_Call (Loc, | |
4034 | Name => | |
4035 | New_Occurrence_Of (RTE (RE_From_Bignum), Loc), | |
4036 | Parameter_Associations => New_List (Relocate_Node (N))); | |
4037 | end Convert_From_Bignum; | |
4038 | ||
4039 | ----------------------- | |
4040 | -- Convert_To_Bignum -- | |
4041 | ----------------------- | |
4042 | ||
4043 | function Convert_To_Bignum (N : Node_Id) return Node_Id is | |
4044 | Loc : constant Source_Ptr := Sloc (N); | |
4045 | ||
4046 | begin | |
0326b4d4 | 4047 | -- Nothing to do if Bignum already except call Relocate_Node |
3cce7f32 | 4048 | |
4049 | if Is_RTE (Etype (N), RE_Bignum) then | |
4050 | return Relocate_Node (N); | |
4051 | ||
21a55437 | 4052 | -- Otherwise construct call to To_Bignum, converting the operand to the |
4053 | -- required Long_Long_Integer form. | |
3cce7f32 | 4054 | |
4055 | else | |
4056 | pragma Assert (Is_Signed_Integer_Type (Etype (N))); | |
4057 | return | |
4058 | Make_Function_Call (Loc, | |
4059 | Name => | |
4060 | New_Occurrence_Of (RTE (RE_To_Bignum), Loc), | |
4061 | Parameter_Associations => New_List ( | |
4062 | Convert_To (Standard_Long_Long_Integer, Relocate_Node (N)))); | |
4063 | end if; | |
4064 | end Convert_To_Bignum; | |
4065 | ||
ee6ba406 | 4066 | --------------------- |
4067 | -- Determine_Range -- | |
4068 | --------------------- | |
4069 | ||
6af1bdbc | 4070 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 4071 | type Cache_Index is range 0 .. Cache_Size - 1; |
39a0c1d3 | 4072 | -- Determine size of below cache (power of 2 is more efficient) |
ee6ba406 | 4073 | |
4074 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; | |
9c486805 | 4075 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
ee6ba406 | 4076 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; |
4077 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
feff2f05 | 4078 | -- The above arrays are used to implement a small direct cache for |
4079 | -- Determine_Range calls. Because of the way Determine_Range recursively | |
4080 | -- traces subexpressions, and because overflow checking calls the routine | |
4081 | -- on the way up the tree, a quadratic behavior can otherwise be | |
4082 | -- encountered in large expressions. The cache entry for node N is stored | |
4083 | -- in the (N mod Cache_Size) entry, and can be validated by checking the | |
9c486805 | 4084 | -- actual node value stored there. The Range_Cache_V array records the |
4085 | -- setting of Assume_Valid for the cache entry. | |
ee6ba406 | 4086 | |
4087 | procedure Determine_Range | |
9c486805 | 4088 | (N : Node_Id; |
4089 | OK : out Boolean; | |
4090 | Lo : out Uint; | |
4091 | Hi : out Uint; | |
4092 | Assume_Valid : Boolean := False) | |
ee6ba406 | 4093 | is |
e254d721 | 4094 | Typ : Entity_Id := Etype (N); |
4095 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 4096 | |
4097 | Lo_Left : Uint; | |
4098 | Hi_Left : Uint; | |
4099 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 4100 | |
ee6ba406 | 4101 | Lo_Right : Uint; |
ee6ba406 | 4102 | Hi_Right : Uint; |
8880be85 | 4103 | -- Lo and Hi bounds of right (or only) operand |
4104 | ||
4105 | Bound : Node_Id; | |
4106 | -- Temp variable used to hold a bound node | |
4107 | ||
4108 | Hbound : Uint; | |
4109 | -- High bound of base type of expression | |
4110 | ||
4111 | Lor : Uint; | |
4112 | Hir : Uint; | |
4113 | -- Refined values for low and high bounds, after tightening | |
4114 | ||
4115 | OK1 : Boolean; | |
4116 | -- Used in lower level calls to indicate if call succeeded | |
4117 | ||
4118 | Cindex : Cache_Index; | |
4119 | -- Used to search cache | |
ee6ba406 | 4120 | |
094ed68e | 4121 | Btyp : Entity_Id; |
4122 | -- Base type | |
4123 | ||
ee6ba406 | 4124 | function OK_Operands return Boolean; |
4125 | -- Used for binary operators. Determines the ranges of the left and | |
4126 | -- right operands, and if they are both OK, returns True, and puts | |
341bd953 | 4127 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left. |
ee6ba406 | 4128 | |
4129 | ----------------- | |
4130 | -- OK_Operands -- | |
4131 | ----------------- | |
4132 | ||
4133 | function OK_Operands return Boolean is | |
4134 | begin | |
9c486805 | 4135 | Determine_Range |
4136 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 4137 | |
4138 | if not OK1 then | |
4139 | return False; | |
4140 | end if; | |
4141 | ||
9c486805 | 4142 | Determine_Range |
4143 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4144 | return OK1; |
4145 | end OK_Operands; | |
4146 | ||
4147 | -- Start of processing for Determine_Range | |
4148 | ||
4149 | begin | |
e12b2502 | 4150 | -- Prevent junk warnings by initializing range variables |
4151 | ||
4152 | Lo := No_Uint; | |
4153 | Hi := No_Uint; | |
4154 | Lor := No_Uint; | |
4155 | Hir := No_Uint; | |
4156 | ||
87bdc21d | 4157 | -- For temporary constants internally generated to remove side effects |
4158 | -- we must use the corresponding expression to determine the range of | |
e12b2502 | 4159 | -- the expression. But note that the expander can also generate |
4160 | -- constants in other cases, including deferred constants. | |
87bdc21d | 4161 | |
4162 | if Is_Entity_Name (N) | |
4163 | and then Nkind (Parent (Entity (N))) = N_Object_Declaration | |
4164 | and then Ekind (Entity (N)) = E_Constant | |
4165 | and then Is_Internal_Name (Chars (Entity (N))) | |
4166 | then | |
e12b2502 | 4167 | if Present (Expression (Parent (Entity (N)))) then |
4168 | Determine_Range | |
4169 | (Expression (Parent (Entity (N))), OK, Lo, Hi, Assume_Valid); | |
87bdc21d | 4170 | |
e12b2502 | 4171 | elsif Present (Full_View (Entity (N))) then |
4172 | Determine_Range | |
4173 | (Expression (Parent (Full_View (Entity (N)))), | |
4174 | OK, Lo, Hi, Assume_Valid); | |
ee6ba406 | 4175 | |
e12b2502 | 4176 | else |
4177 | OK := False; | |
4178 | end if; | |
4179 | return; | |
4180 | end if; | |
ee6ba406 | 4181 | |
a781c0fc | 4182 | -- If type is not defined, we can't determine its range |
ee6ba406 | 4183 | |
a781c0fc | 4184 | if No (Typ) |
4185 | ||
4186 | -- We don't deal with anything except discrete types | |
4187 | ||
4188 | or else not Is_Discrete_Type (Typ) | |
4189 | ||
4190 | -- Ignore type for which an error has been posted, since range in | |
4191 | -- this case may well be a bogosity deriving from the error. Also | |
4192 | -- ignore if error posted on the reference node. | |
4193 | ||
4194 | or else Error_Posted (N) or else Error_Posted (Typ) | |
ee6ba406 | 4195 | then |
4196 | OK := False; | |
4197 | return; | |
4198 | end if; | |
4199 | ||
4200 | -- For all other cases, we can determine the range | |
4201 | ||
4202 | OK := True; | |
4203 | ||
feff2f05 | 4204 | -- If value is compile time known, then the possible range is the one |
39a0c1d3 | 4205 | -- value that we know this expression definitely has. |
ee6ba406 | 4206 | |
4207 | if Compile_Time_Known_Value (N) then | |
4208 | Lo := Expr_Value (N); | |
4209 | Hi := Lo; | |
4210 | return; | |
4211 | end if; | |
4212 | ||
4213 | -- Return if already in the cache | |
4214 | ||
4215 | Cindex := Cache_Index (N mod Cache_Size); | |
4216 | ||
9c486805 | 4217 | if Determine_Range_Cache_N (Cindex) = N |
4218 | and then | |
4219 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
4220 | then | |
ee6ba406 | 4221 | Lo := Determine_Range_Cache_Lo (Cindex); |
4222 | Hi := Determine_Range_Cache_Hi (Cindex); | |
4223 | return; | |
4224 | end if; | |
4225 | ||
feff2f05 | 4226 | -- Otherwise, start by finding the bounds of the type of the expression, |
4227 | -- the value cannot be outside this range (if it is, then we have an | |
4228 | -- overflow situation, which is a separate check, we are talking here | |
4229 | -- only about the expression value). | |
ee6ba406 | 4230 | |
341bd953 | 4231 | -- First a check, never try to find the bounds of a generic type, since |
4232 | -- these bounds are always junk values, and it is only valid to look at | |
4233 | -- the bounds in an instance. | |
4234 | ||
4235 | if Is_Generic_Type (Typ) then | |
4236 | OK := False; | |
4237 | return; | |
4238 | end if; | |
4239 | ||
9c486805 | 4240 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 4241 | |
9c486805 | 4242 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
4243 | or else Assume_No_Invalid_Values | |
4244 | or else Assume_Valid | |
e254d721 | 4245 | then |
9c486805 | 4246 | null; |
4247 | else | |
4248 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 4249 | end if; |
4250 | ||
094ed68e | 4251 | -- Retrieve the base type. Handle the case where the base type is a |
4252 | -- private enumeration type. | |
4253 | ||
4254 | Btyp := Base_Type (Typ); | |
4255 | ||
4256 | if Is_Private_Type (Btyp) and then Present (Full_View (Btyp)) then | |
4257 | Btyp := Full_View (Btyp); | |
4258 | end if; | |
4259 | ||
feff2f05 | 4260 | -- We use the actual bound unless it is dynamic, in which case use the |
4261 | -- corresponding base type bound if possible. If we can't get a bound | |
4262 | -- then we figure we can't determine the range (a peculiar case, that | |
4263 | -- perhaps cannot happen, but there is no point in bombing in this | |
4264 | -- optimization circuit. | |
8880be85 | 4265 | |
4266 | -- First the low bound | |
ee6ba406 | 4267 | |
4268 | Bound := Type_Low_Bound (Typ); | |
4269 | ||
4270 | if Compile_Time_Known_Value (Bound) then | |
4271 | Lo := Expr_Value (Bound); | |
4272 | ||
094ed68e | 4273 | elsif Compile_Time_Known_Value (Type_Low_Bound (Btyp)) then |
4274 | Lo := Expr_Value (Type_Low_Bound (Btyp)); | |
ee6ba406 | 4275 | |
4276 | else | |
4277 | OK := False; | |
4278 | return; | |
4279 | end if; | |
4280 | ||
8880be85 | 4281 | -- Now the high bound |
4282 | ||
ee6ba406 | 4283 | Bound := Type_High_Bound (Typ); |
4284 | ||
8880be85 | 4285 | -- We need the high bound of the base type later on, and this should |
4286 | -- always be compile time known. Again, it is not clear that this | |
4287 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 4288 | |
094ed68e | 4289 | if Compile_Time_Known_Value (Type_High_Bound (Btyp)) then |
4290 | Hbound := Expr_Value (Type_High_Bound (Btyp)); | |
ee6ba406 | 4291 | Hi := Hbound; |
4292 | ||
4293 | else | |
4294 | OK := False; | |
4295 | return; | |
4296 | end if; | |
4297 | ||
feff2f05 | 4298 | -- If we have a static subtype, then that may have a tighter bound so |
4299 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 4300 | |
4301 | if Compile_Time_Known_Value (Bound) then | |
4302 | Hi := Expr_Value (Bound); | |
4303 | end if; | |
4304 | ||
feff2f05 | 4305 | -- We may be able to refine this value in certain situations. If any |
4306 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
4307 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 4308 | |
4309 | case Nkind (N) is | |
4310 | ||
4311 | -- For unary plus, result is limited by range of operand | |
4312 | ||
4313 | when N_Op_Plus => | |
9c486805 | 4314 | Determine_Range |
4315 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4316 | |
4317 | -- For unary minus, determine range of operand, and negate it | |
4318 | ||
4319 | when N_Op_Minus => | |
9c486805 | 4320 | Determine_Range |
4321 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 4322 | |
4323 | if OK1 then | |
4324 | Lor := -Hi_Right; | |
4325 | Hir := -Lo_Right; | |
4326 | end if; | |
4327 | ||
4328 | -- For binary addition, get range of each operand and do the | |
4329 | -- addition to get the result range. | |
4330 | ||
4331 | when N_Op_Add => | |
4332 | if OK_Operands then | |
4333 | Lor := Lo_Left + Lo_Right; | |
4334 | Hir := Hi_Left + Hi_Right; | |
4335 | end if; | |
4336 | ||
feff2f05 | 4337 | -- Division is tricky. The only case we consider is where the right |
4338 | -- operand is a positive constant, and in this case we simply divide | |
4339 | -- the bounds of the left operand | |
ee6ba406 | 4340 | |
4341 | when N_Op_Divide => | |
4342 | if OK_Operands then | |
4343 | if Lo_Right = Hi_Right | |
4344 | and then Lo_Right > 0 | |
4345 | then | |
4346 | Lor := Lo_Left / Lo_Right; | |
4347 | Hir := Hi_Left / Lo_Right; | |
ee6ba406 | 4348 | else |
4349 | OK1 := False; | |
4350 | end if; | |
4351 | end if; | |
4352 | ||
feff2f05 | 4353 | -- For binary subtraction, get range of each operand and do the worst |
4354 | -- case subtraction to get the result range. | |
ee6ba406 | 4355 | |
4356 | when N_Op_Subtract => | |
4357 | if OK_Operands then | |
4358 | Lor := Lo_Left - Hi_Right; | |
4359 | Hir := Hi_Left - Lo_Right; | |
4360 | end if; | |
4361 | ||
feff2f05 | 4362 | -- For MOD, if right operand is a positive constant, then result must |
4363 | -- be in the allowable range of mod results. | |
ee6ba406 | 4364 | |
4365 | when N_Op_Mod => | |
4366 | if OK_Operands then | |
9dfe12ae | 4367 | if Lo_Right = Hi_Right |
4368 | and then Lo_Right /= 0 | |
4369 | then | |
ee6ba406 | 4370 | if Lo_Right > 0 then |
4371 | Lor := Uint_0; | |
4372 | Hir := Lo_Right - 1; | |
4373 | ||
9dfe12ae | 4374 | else -- Lo_Right < 0 |
ee6ba406 | 4375 | Lor := Lo_Right + 1; |
4376 | Hir := Uint_0; | |
4377 | end if; | |
4378 | ||
4379 | else | |
4380 | OK1 := False; | |
4381 | end if; | |
4382 | end if; | |
4383 | ||
feff2f05 | 4384 | -- For REM, if right operand is a positive constant, then result must |
4385 | -- be in the allowable range of mod results. | |
ee6ba406 | 4386 | |
4387 | when N_Op_Rem => | |
4388 | if OK_Operands then | |
9dfe12ae | 4389 | if Lo_Right = Hi_Right |
4390 | and then Lo_Right /= 0 | |
4391 | then | |
ee6ba406 | 4392 | declare |
4393 | Dval : constant Uint := (abs Lo_Right) - 1; | |
4394 | ||
4395 | begin | |
4396 | -- The sign of the result depends on the sign of the | |
4397 | -- dividend (but not on the sign of the divisor, hence | |
4398 | -- the abs operation above). | |
4399 | ||
4400 | if Lo_Left < 0 then | |
4401 | Lor := -Dval; | |
4402 | else | |
4403 | Lor := Uint_0; | |
4404 | end if; | |
4405 | ||
4406 | if Hi_Left < 0 then | |
4407 | Hir := Uint_0; | |
4408 | else | |
4409 | Hir := Dval; | |
4410 | end if; | |
4411 | end; | |
4412 | ||
4413 | else | |
4414 | OK1 := False; | |
4415 | end if; | |
4416 | end if; | |
4417 | ||
4418 | -- Attribute reference cases | |
4419 | ||
4420 | when N_Attribute_Reference => | |
4421 | case Attribute_Name (N) is | |
4422 | ||
4423 | -- For Pos/Val attributes, we can refine the range using the | |
ddbf7f2e | 4424 | -- possible range of values of the attribute expression. |
ee6ba406 | 4425 | |
4426 | when Name_Pos | Name_Val => | |
9c486805 | 4427 | Determine_Range |
4428 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 4429 | |
4430 | -- For Length attribute, use the bounds of the corresponding | |
4431 | -- index type to refine the range. | |
4432 | ||
4433 | when Name_Length => | |
4434 | declare | |
4435 | Atyp : Entity_Id := Etype (Prefix (N)); | |
4436 | Inum : Nat; | |
4437 | Indx : Node_Id; | |
4438 | ||
4439 | LL, LU : Uint; | |
4440 | UL, UU : Uint; | |
4441 | ||
4442 | begin | |
4443 | if Is_Access_Type (Atyp) then | |
4444 | Atyp := Designated_Type (Atyp); | |
4445 | end if; | |
4446 | ||
4447 | -- For string literal, we know exact value | |
4448 | ||
4449 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
4450 | OK := True; | |
4451 | Lo := String_Literal_Length (Atyp); | |
4452 | Hi := String_Literal_Length (Atyp); | |
4453 | return; | |
4454 | end if; | |
4455 | ||
4456 | -- Otherwise check for expression given | |
4457 | ||
4458 | if No (Expressions (N)) then | |
4459 | Inum := 1; | |
4460 | else | |
4461 | Inum := | |
4462 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
4463 | end if; | |
4464 | ||
4465 | Indx := First_Index (Atyp); | |
4466 | for J in 2 .. Inum loop | |
4467 | Indx := Next_Index (Indx); | |
4468 | end loop; | |
4469 | ||
9116df93 | 4470 | -- If the index type is a formal type or derived from |
c8da6114 | 4471 | -- one, the bounds are not static. |
4472 | ||
4473 | if Is_Generic_Type (Root_Type (Etype (Indx))) then | |
4474 | OK := False; | |
4475 | return; | |
4476 | end if; | |
4477 | ||
ee6ba406 | 4478 | Determine_Range |
9c486805 | 4479 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
4480 | Assume_Valid); | |
ee6ba406 | 4481 | |
4482 | if OK1 then | |
4483 | Determine_Range | |
9c486805 | 4484 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
4485 | Assume_Valid); | |
ee6ba406 | 4486 | |
4487 | if OK1 then | |
4488 | ||
4489 | -- The maximum value for Length is the biggest | |
4490 | -- possible gap between the values of the bounds. | |
4491 | -- But of course, this value cannot be negative. | |
4492 | ||
9c486805 | 4493 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 4494 | |
4495 | -- For constrained arrays, the minimum value for | |
4496 | -- Length is taken from the actual value of the | |
9116df93 | 4497 | -- bounds, since the index will be exactly of this |
4498 | -- subtype. | |
ee6ba406 | 4499 | |
4500 | if Is_Constrained (Atyp) then | |
9c486805 | 4501 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 4502 | |
4503 | -- For an unconstrained array, the minimum value | |
4504 | -- for length is always zero. | |
4505 | ||
4506 | else | |
4507 | Lor := Uint_0; | |
4508 | end if; | |
4509 | end if; | |
4510 | end if; | |
4511 | end; | |
4512 | ||
4513 | -- No special handling for other attributes | |
9116df93 | 4514 | -- Probably more opportunities exist here??? |
ee6ba406 | 4515 | |
4516 | when others => | |
4517 | OK1 := False; | |
4518 | ||
4519 | end case; | |
4520 | ||
feff2f05 | 4521 | -- For type conversion from one discrete type to another, we can |
4522 | -- refine the range using the converted value. | |
ee6ba406 | 4523 | |
4524 | when N_Type_Conversion => | |
9c486805 | 4525 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); |
ee6ba406 | 4526 | |
4527 | -- Nothing special to do for all other expression kinds | |
4528 | ||
4529 | when others => | |
4530 | OK1 := False; | |
4531 | Lor := No_Uint; | |
4532 | Hir := No_Uint; | |
4533 | end case; | |
4534 | ||
9116df93 | 4535 | -- At this stage, if OK1 is true, then we know that the actual result of |
4536 | -- the computed expression is in the range Lor .. Hir. We can use this | |
4537 | -- to restrict the possible range of results. | |
ee6ba406 | 4538 | |
4539 | if OK1 then | |
4540 | ||
9116df93 | 4541 | -- If the refined value of the low bound is greater than the type |
4542 | -- high bound, then reset it to the more restrictive value. However, | |
4543 | -- we do NOT do this for the case of a modular type where the | |
4544 | -- possible upper bound on the value is above the base type high | |
4545 | -- bound, because that means the result could wrap. | |
ee6ba406 | 4546 | |
4547 | if Lor > Lo | |
9116df93 | 4548 | and then not (Is_Modular_Integer_Type (Typ) and then Hir > Hbound) |
ee6ba406 | 4549 | then |
4550 | Lo := Lor; | |
4551 | end if; | |
4552 | ||
9116df93 | 4553 | -- Similarly, if the refined value of the high bound is less than the |
4554 | -- value so far, then reset it to the more restrictive value. Again, | |
4555 | -- we do not do this if the refined low bound is negative for a | |
4556 | -- modular type, since this would wrap. | |
ee6ba406 | 4557 | |
4558 | if Hir < Hi | |
9116df93 | 4559 | and then not (Is_Modular_Integer_Type (Typ) and then Lor < Uint_0) |
ee6ba406 | 4560 | then |
4561 | Hi := Hir; | |
4562 | end if; | |
4563 | end if; | |
4564 | ||
4565 | -- Set cache entry for future call and we are all done | |
4566 | ||
4567 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 4568 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 4569 | Determine_Range_Cache_Lo (Cindex) := Lo; |
4570 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
4571 | return; | |
4572 | ||
9116df93 | 4573 | -- If any exception occurs, it means that we have some bug in the compiler, |
4574 | -- possibly triggered by a previous error, or by some unforeseen peculiar | |
ee6ba406 | 4575 | -- occurrence. However, this is only an optimization attempt, so there is |
4576 | -- really no point in crashing the compiler. Instead we just decide, too | |
4577 | -- bad, we can't figure out a range in this case after all. | |
4578 | ||
4579 | exception | |
4580 | when others => | |
4581 | ||
4582 | -- Debug flag K disables this behavior (useful for debugging) | |
4583 | ||
4584 | if Debug_Flag_K then | |
4585 | raise; | |
4586 | else | |
4587 | OK := False; | |
4588 | Lo := No_Uint; | |
4589 | Hi := No_Uint; | |
4590 | return; | |
4591 | end if; | |
ee6ba406 | 4592 | end Determine_Range; |
4593 | ||
4594 | ------------------------------------ | |
4595 | -- Discriminant_Checks_Suppressed -- | |
4596 | ------------------------------------ | |
4597 | ||
4598 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4599 | begin | |
9dfe12ae | 4600 | if Present (E) then |
4601 | if Is_Unchecked_Union (E) then | |
4602 | return True; | |
4603 | elsif Checks_May_Be_Suppressed (E) then | |
4604 | return Is_Check_Suppressed (E, Discriminant_Check); | |
4605 | end if; | |
4606 | end if; | |
4607 | ||
fafc6b97 | 4608 | return Scope_Suppress.Suppress (Discriminant_Check); |
ee6ba406 | 4609 | end Discriminant_Checks_Suppressed; |
4610 | ||
4611 | -------------------------------- | |
4612 | -- Division_Checks_Suppressed -- | |
4613 | -------------------------------- | |
4614 | ||
4615 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4616 | begin | |
9dfe12ae | 4617 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
4618 | return Is_Check_Suppressed (E, Division_Check); | |
4619 | else | |
fafc6b97 | 4620 | return Scope_Suppress.Suppress (Division_Check); |
9dfe12ae | 4621 | end if; |
ee6ba406 | 4622 | end Division_Checks_Suppressed; |
4623 | ||
fa771c05 | 4624 | -------------------------------------- |
4625 | -- Duplicated_Tag_Checks_Suppressed -- | |
4626 | -------------------------------------- | |
4627 | ||
4628 | function Duplicated_Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4629 | begin | |
4630 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
4631 | return Is_Check_Suppressed (E, Duplicated_Tag_Check); | |
4632 | else | |
4633 | return Scope_Suppress.Suppress (Duplicated_Tag_Check); | |
4634 | end if; | |
4635 | end Duplicated_Tag_Checks_Suppressed; | |
4636 | ||
ee6ba406 | 4637 | ----------------------------------- |
4638 | -- Elaboration_Checks_Suppressed -- | |
4639 | ----------------------------------- | |
4640 | ||
4641 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
4642 | begin | |
38f5559f | 4643 | -- The complication in this routine is that if we are in the dynamic |
4644 | -- model of elaboration, we also check All_Checks, since All_Checks | |
4645 | -- does not set Elaboration_Check explicitly. | |
4646 | ||
9dfe12ae | 4647 | if Present (E) then |
4648 | if Kill_Elaboration_Checks (E) then | |
4649 | return True; | |
38f5559f | 4650 | |
9dfe12ae | 4651 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 4652 | if Is_Check_Suppressed (E, Elaboration_Check) then |
4653 | return True; | |
4654 | elsif Dynamic_Elaboration_Checks then | |
4655 | return Is_Check_Suppressed (E, All_Checks); | |
4656 | else | |
4657 | return False; | |
4658 | end if; | |
9dfe12ae | 4659 | end if; |
4660 | end if; | |
4661 | ||
fafc6b97 | 4662 | if Scope_Suppress.Suppress (Elaboration_Check) then |
38f5559f | 4663 | return True; |
4664 | elsif Dynamic_Elaboration_Checks then | |
fafc6b97 | 4665 | return Scope_Suppress.Suppress (All_Checks); |
38f5559f | 4666 | else |
4667 | return False; | |
4668 | end if; | |
ee6ba406 | 4669 | end Elaboration_Checks_Suppressed; |
4670 | ||
9dfe12ae | 4671 | --------------------------- |
4672 | -- Enable_Overflow_Check -- | |
4673 | --------------------------- | |
4674 | ||
4675 | procedure Enable_Overflow_Check (N : Node_Id) is | |
3cce7f32 | 4676 | Typ : constant Entity_Id := Base_Type (Etype (N)); |
db415383 | 4677 | Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 4678 | Chk : Nat; |
4679 | OK : Boolean; | |
4680 | Ent : Entity_Id; | |
4681 | Ofs : Uint; | |
4682 | Lo : Uint; | |
4683 | Hi : Uint; | |
ee6ba406 | 4684 | |
ee6ba406 | 4685 | begin |
9dfe12ae | 4686 | if Debug_Flag_CC then |
4687 | w ("Enable_Overflow_Check for node ", Int (N)); | |
4688 | Write_Str (" Source location = "); | |
4689 | wl (Sloc (N)); | |
00c403ee | 4690 | pg (Union_Id (N)); |
ee6ba406 | 4691 | end if; |
ee6ba406 | 4692 | |
75209ec5 | 4693 | -- No check if overflow checks suppressed for type of node |
4694 | ||
0df9d43f | 4695 | if Overflow_Checks_Suppressed (Etype (N)) then |
75209ec5 | 4696 | return; |
4697 | ||
49260fa5 | 4698 | -- Nothing to do for unsigned integer types, which do not overflow |
4699 | ||
4700 | elsif Is_Modular_Integer_Type (Typ) then | |
4701 | return; | |
3cce7f32 | 4702 | end if; |
4703 | ||
0df9d43f | 4704 | -- This is the point at which processing for STRICT mode diverges |
21a55437 | 4705 | -- from processing for MINIMIZED/ELIMINATED modes. This divergence is |
4706 | -- probably more extreme that it needs to be, but what is going on here | |
4707 | -- is that when we introduced MINIMIZED/ELIMINATED modes, we wanted | |
0df9d43f | 4708 | -- to leave the processing for STRICT mode untouched. There were |
21a55437 | 4709 | -- two reasons for this. First it avoided any incompatible change of |
0df9d43f | 4710 | -- behavior. Second, it guaranteed that STRICT mode continued to be |
21a55437 | 4711 | -- legacy reliable. |
3cce7f32 | 4712 | |
0df9d43f | 4713 | -- The big difference is that in STRICT mode there is a fair amount of |
3cce7f32 | 4714 | -- circuitry to try to avoid setting the Do_Overflow_Check flag if we |
4715 | -- know that no check is needed. We skip all that in the two new modes, | |
4716 | -- since really overflow checking happens over a whole subtree, and we | |
4717 | -- do the corresponding optimizations later on when applying the checks. | |
4718 | ||
4719 | if Mode in Minimized_Or_Eliminated then | |
0df9d43f | 4720 | if not (Overflow_Checks_Suppressed (Etype (N))) |
4721 | and then not (Is_Entity_Name (N) | |
4722 | and then Overflow_Checks_Suppressed (Entity (N))) | |
4723 | then | |
4724 | Activate_Overflow_Check (N); | |
4725 | end if; | |
3cce7f32 | 4726 | |
4727 | if Debug_Flag_CC then | |
4728 | w ("Minimized/Eliminated mode"); | |
4729 | end if; | |
4730 | ||
4731 | return; | |
4732 | end if; | |
4733 | ||
0df9d43f | 4734 | -- Remainder of processing is for STRICT case, and is unchanged from |
691fe9e0 | 4735 | -- earlier versions preceding the addition of MINIMIZED/ELIMINATED. |
49260fa5 | 4736 | |
feff2f05 | 4737 | -- Nothing to do if the range of the result is known OK. We skip this |
4738 | -- for conversions, since the caller already did the check, and in any | |
4739 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 4740 | -- different. |
ee6ba406 | 4741 | |
3cce7f32 | 4742 | if Nkind (N) /= N_Type_Conversion then |
9c486805 | 4743 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 4744 | |
cc60bd16 | 4745 | -- Note in the test below that we assume that the range is not OK |
4746 | -- if a bound of the range is equal to that of the type. That's not | |
4747 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 4748 | |
9dfe12ae | 4749 | -- a) The way that Determine_Range works, it will typically report |
4750 | -- the bounds of the value as being equal to the bounds of the | |
4751 | -- type, because it either can't tell anything more precise, or | |
4752 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 4753 | |
9dfe12ae | 4754 | -- b) It is very unusual to have a situation in which this would |
4755 | -- generate an unnecessary overflow check (an example would be | |
4756 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 4757 | -- literal value one is added). |
ee6ba406 | 4758 | |
9dfe12ae | 4759 | -- c) The alternative is a lot of special casing in this routine |
4760 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 4761 | |
9dfe12ae | 4762 | if OK |
4763 | and then Lo > Expr_Value (Type_Low_Bound (Typ)) | |
4764 | and then Hi < Expr_Value (Type_High_Bound (Typ)) | |
4765 | then | |
4766 | if Debug_Flag_CC then | |
4767 | w ("No overflow check required"); | |
4768 | end if; | |
4769 | ||
4770 | return; | |
4771 | end if; | |
4772 | end if; | |
4773 | ||
feff2f05 | 4774 | -- If not in optimizing mode, set flag and we are done. We are also done |
4775 | -- (and just set the flag) if the type is not a discrete type, since it | |
4776 | -- is not worth the effort to eliminate checks for other than discrete | |
4777 | -- types. In addition, we take this same path if we have stored the | |
4778 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 4779 | -- but we do not want to blow up). |
9dfe12ae | 4780 | |
4781 | if Optimization_Level = 0 | |
4782 | or else not Is_Discrete_Type (Etype (N)) | |
4783 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4784 | then |
00c403ee | 4785 | Activate_Overflow_Check (N); |
9dfe12ae | 4786 | |
4787 | if Debug_Flag_CC then | |
4788 | w ("Optimization off"); | |
4789 | end if; | |
4790 | ||
ee6ba406 | 4791 | return; |
9dfe12ae | 4792 | end if; |
ee6ba406 | 4793 | |
9dfe12ae | 4794 | -- Otherwise evaluate and check the expression |
4795 | ||
4796 | Find_Check | |
4797 | (Expr => N, | |
4798 | Check_Type => 'O', | |
4799 | Target_Type => Empty, | |
4800 | Entry_OK => OK, | |
4801 | Check_Num => Chk, | |
4802 | Ent => Ent, | |
4803 | Ofs => Ofs); | |
4804 | ||
4805 | if Debug_Flag_CC then | |
4806 | w ("Called Find_Check"); | |
4807 | w (" OK = ", OK); | |
4808 | ||
4809 | if OK then | |
4810 | w (" Check_Num = ", Chk); | |
4811 | w (" Ent = ", Int (Ent)); | |
4812 | Write_Str (" Ofs = "); | |
4813 | pid (Ofs); | |
4814 | end if; | |
4815 | end if; | |
ee6ba406 | 4816 | |
9dfe12ae | 4817 | -- If check is not of form to optimize, then set flag and we are done |
4818 | ||
4819 | if not OK then | |
00c403ee | 4820 | Activate_Overflow_Check (N); |
ee6ba406 | 4821 | return; |
9dfe12ae | 4822 | end if; |
ee6ba406 | 4823 | |
9dfe12ae | 4824 | -- If check is already performed, then return without setting flag |
4825 | ||
4826 | if Chk /= 0 then | |
4827 | if Debug_Flag_CC then | |
4828 | w ("Check suppressed!"); | |
4829 | end if; | |
ee6ba406 | 4830 | |
ee6ba406 | 4831 | return; |
9dfe12ae | 4832 | end if; |
ee6ba406 | 4833 | |
9dfe12ae | 4834 | -- Here we will make a new entry for the new check |
4835 | ||
00c403ee | 4836 | Activate_Overflow_Check (N); |
9dfe12ae | 4837 | Num_Saved_Checks := Num_Saved_Checks + 1; |
4838 | Saved_Checks (Num_Saved_Checks) := | |
4839 | (Killed => False, | |
4840 | Entity => Ent, | |
4841 | Offset => Ofs, | |
4842 | Check_Type => 'O', | |
4843 | Target_Type => Empty); | |
4844 | ||
4845 | if Debug_Flag_CC then | |
4846 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
4847 | w (" Entity = ", Int (Ent)); | |
4848 | Write_Str (" Offset = "); | |
4849 | pid (Ofs); | |
4850 | w (" Check_Type = O"); | |
4851 | w (" Target_Type = Empty"); | |
4852 | end if; | |
ee6ba406 | 4853 | |
feff2f05 | 4854 | -- If we get an exception, then something went wrong, probably because of |
20cf157b | 4855 | -- an error in the structure of the tree due to an incorrect program. Or |
4856 | -- it may be a bug in the optimization circuit. In either case the safest | |
feff2f05 | 4857 | -- thing is simply to set the check flag unconditionally. |
9dfe12ae | 4858 | |
4859 | exception | |
4860 | when others => | |
00c403ee | 4861 | Activate_Overflow_Check (N); |
9dfe12ae | 4862 | |
4863 | if Debug_Flag_CC then | |
4864 | w (" exception occurred, overflow flag set"); | |
4865 | end if; | |
4866 | ||
4867 | return; | |
4868 | end Enable_Overflow_Check; | |
4869 | ||
4870 | ------------------------ | |
4871 | -- Enable_Range_Check -- | |
4872 | ------------------------ | |
4873 | ||
4874 | procedure Enable_Range_Check (N : Node_Id) is | |
4875 | Chk : Nat; | |
4876 | OK : Boolean; | |
4877 | Ent : Entity_Id; | |
4878 | Ofs : Uint; | |
4879 | Ttyp : Entity_Id; | |
4880 | P : Node_Id; | |
4881 | ||
4882 | begin | |
feff2f05 | 4883 | -- Return if unchecked type conversion with range check killed. In this |
39a0c1d3 | 4884 | -- case we never set the flag (that's what Kill_Range_Check is about). |
9dfe12ae | 4885 | |
4886 | if Nkind (N) = N_Unchecked_Type_Conversion | |
4887 | and then Kill_Range_Check (N) | |
ee6ba406 | 4888 | then |
4889 | return; | |
9dfe12ae | 4890 | end if; |
ee6ba406 | 4891 | |
55e8372b | 4892 | -- Do not set range check flag if parent is assignment statement or |
4893 | -- object declaration with Suppress_Assignment_Checks flag set | |
4894 | ||
4895 | if Nkind_In (Parent (N), N_Assignment_Statement, N_Object_Declaration) | |
4896 | and then Suppress_Assignment_Checks (Parent (N)) | |
4897 | then | |
4898 | return; | |
4899 | end if; | |
4900 | ||
0577b0b1 | 4901 | -- Check for various cases where we should suppress the range check |
4902 | ||
4903 | -- No check if range checks suppressed for type of node | |
4904 | ||
20cf157b | 4905 | if Present (Etype (N)) and then Range_Checks_Suppressed (Etype (N)) then |
0577b0b1 | 4906 | return; |
4907 | ||
4908 | -- No check if node is an entity name, and range checks are suppressed | |
4909 | -- for this entity, or for the type of this entity. | |
4910 | ||
4911 | elsif Is_Entity_Name (N) | |
4912 | and then (Range_Checks_Suppressed (Entity (N)) | |
20cf157b | 4913 | or else Range_Checks_Suppressed (Etype (Entity (N)))) |
0577b0b1 | 4914 | then |
4915 | return; | |
4916 | ||
4917 | -- No checks if index of array, and index checks are suppressed for | |
4918 | -- the array object or the type of the array. | |
4919 | ||
4920 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
4921 | declare | |
4922 | Pref : constant Node_Id := Prefix (Parent (N)); | |
4923 | begin | |
4924 | if Is_Entity_Name (Pref) | |
4925 | and then Index_Checks_Suppressed (Entity (Pref)) | |
4926 | then | |
4927 | return; | |
4928 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
4929 | return; | |
4930 | end if; | |
4931 | end; | |
4932 | end if; | |
4933 | ||
9dfe12ae | 4934 | -- Debug trace output |
ee6ba406 | 4935 | |
9dfe12ae | 4936 | if Debug_Flag_CC then |
4937 | w ("Enable_Range_Check for node ", Int (N)); | |
4938 | Write_Str (" Source location = "); | |
4939 | wl (Sloc (N)); | |
00c403ee | 4940 | pg (Union_Id (N)); |
9dfe12ae | 4941 | end if; |
4942 | ||
feff2f05 | 4943 | -- If not in optimizing mode, set flag and we are done. We are also done |
4944 | -- (and just set the flag) if the type is not a discrete type, since it | |
4945 | -- is not worth the effort to eliminate checks for other than discrete | |
4946 | -- types. In addition, we take this same path if we have stored the | |
4947 | -- maximum number of checks possible already (a very unlikely situation, | |
39a0c1d3 | 4948 | -- but we do not want to blow up). |
9dfe12ae | 4949 | |
4950 | if Optimization_Level = 0 | |
4951 | or else No (Etype (N)) | |
4952 | or else not Is_Discrete_Type (Etype (N)) | |
4953 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 4954 | then |
00c403ee | 4955 | Activate_Range_Check (N); |
9dfe12ae | 4956 | |
4957 | if Debug_Flag_CC then | |
4958 | w ("Optimization off"); | |
4959 | end if; | |
4960 | ||
ee6ba406 | 4961 | return; |
9dfe12ae | 4962 | end if; |
ee6ba406 | 4963 | |
9dfe12ae | 4964 | -- Otherwise find out the target type |
ee6ba406 | 4965 | |
9dfe12ae | 4966 | P := Parent (N); |
ee6ba406 | 4967 | |
9dfe12ae | 4968 | -- For assignment, use left side subtype |
4969 | ||
4970 | if Nkind (P) = N_Assignment_Statement | |
4971 | and then Expression (P) = N | |
4972 | then | |
4973 | Ttyp := Etype (Name (P)); | |
4974 | ||
4975 | -- For indexed component, use subscript subtype | |
4976 | ||
4977 | elsif Nkind (P) = N_Indexed_Component then | |
4978 | declare | |
4979 | Atyp : Entity_Id; | |
4980 | Indx : Node_Id; | |
4981 | Subs : Node_Id; | |
4982 | ||
4983 | begin | |
4984 | Atyp := Etype (Prefix (P)); | |
4985 | ||
4986 | if Is_Access_Type (Atyp) then | |
4987 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 4988 | |
4989 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 4990 | -- perform check unconditionally: it depends on the bounds of |
4991 | -- an object and we cannot currently recognize whether the test | |
4992 | -- may be redundant. | |
f07ea091 | 4993 | |
4994 | if not Is_Constrained (Atyp) then | |
00c403ee | 4995 | Activate_Range_Check (N); |
f07ea091 | 4996 | return; |
4997 | end if; | |
7189d17f | 4998 | |
feff2f05 | 4999 | -- Ditto if the prefix is an explicit dereference whose designated |
5000 | -- type is unconstrained. | |
7189d17f | 5001 | |
5002 | elsif Nkind (Prefix (P)) = N_Explicit_Dereference | |
5003 | and then not Is_Constrained (Atyp) | |
5004 | then | |
00c403ee | 5005 | Activate_Range_Check (N); |
7189d17f | 5006 | return; |
9dfe12ae | 5007 | end if; |
5008 | ||
5009 | Indx := First_Index (Atyp); | |
5010 | Subs := First (Expressions (P)); | |
5011 | loop | |
5012 | if Subs = N then | |
5013 | Ttyp := Etype (Indx); | |
5014 | exit; | |
5015 | end if; | |
5016 | ||
5017 | Next_Index (Indx); | |
5018 | Next (Subs); | |
5019 | end loop; | |
5020 | end; | |
5021 | ||
5022 | -- For now, ignore all other cases, they are not so interesting | |
5023 | ||
5024 | else | |
5025 | if Debug_Flag_CC then | |
5026 | w (" target type not found, flag set"); | |
5027 | end if; | |
5028 | ||
00c403ee | 5029 | Activate_Range_Check (N); |
9dfe12ae | 5030 | return; |
5031 | end if; | |
5032 | ||
5033 | -- Evaluate and check the expression | |
5034 | ||
5035 | Find_Check | |
5036 | (Expr => N, | |
5037 | Check_Type => 'R', | |
5038 | Target_Type => Ttyp, | |
5039 | Entry_OK => OK, | |
5040 | Check_Num => Chk, | |
5041 | Ent => Ent, | |
5042 | Ofs => Ofs); | |
5043 | ||
5044 | if Debug_Flag_CC then | |
5045 | w ("Called Find_Check"); | |
5046 | w ("Target_Typ = ", Int (Ttyp)); | |
5047 | w (" OK = ", OK); | |
5048 | ||
5049 | if OK then | |
5050 | w (" Check_Num = ", Chk); | |
5051 | w (" Ent = ", Int (Ent)); | |
5052 | Write_Str (" Ofs = "); | |
5053 | pid (Ofs); | |
5054 | end if; | |
5055 | end if; | |
5056 | ||
5057 | -- If check is not of form to optimize, then set flag and we are done | |
5058 | ||
5059 | if not OK then | |
5060 | if Debug_Flag_CC then | |
5061 | w (" expression not of optimizable type, flag set"); | |
5062 | end if; | |
5063 | ||
00c403ee | 5064 | Activate_Range_Check (N); |
9dfe12ae | 5065 | return; |
5066 | end if; | |
5067 | ||
5068 | -- If check is already performed, then return without setting flag | |
5069 | ||
5070 | if Chk /= 0 then | |
5071 | if Debug_Flag_CC then | |
5072 | w ("Check suppressed!"); | |
5073 | end if; | |
5074 | ||
5075 | return; | |
5076 | end if; | |
5077 | ||
5078 | -- Here we will make a new entry for the new check | |
5079 | ||
00c403ee | 5080 | Activate_Range_Check (N); |
9dfe12ae | 5081 | Num_Saved_Checks := Num_Saved_Checks + 1; |
5082 | Saved_Checks (Num_Saved_Checks) := | |
5083 | (Killed => False, | |
5084 | Entity => Ent, | |
5085 | Offset => Ofs, | |
5086 | Check_Type => 'R', | |
5087 | Target_Type => Ttyp); | |
5088 | ||
5089 | if Debug_Flag_CC then | |
5090 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
5091 | w (" Entity = ", Int (Ent)); | |
5092 | Write_Str (" Offset = "); | |
5093 | pid (Ofs); | |
5094 | w (" Check_Type = R"); | |
5095 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 5096 | pg (Union_Id (Ttyp)); |
9dfe12ae | 5097 | end if; |
5098 | ||
feff2f05 | 5099 | -- If we get an exception, then something went wrong, probably because of |
5100 | -- an error in the structure of the tree due to an incorrect program. Or | |
5101 | -- it may be a bug in the optimization circuit. In either case the safest | |
5102 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 5103 | |
5104 | exception | |
5105 | when others => | |
00c403ee | 5106 | Activate_Range_Check (N); |
9dfe12ae | 5107 | |
5108 | if Debug_Flag_CC then | |
5109 | w (" exception occurred, range flag set"); | |
5110 | end if; | |
5111 | ||
5112 | return; | |
5113 | end Enable_Range_Check; | |
5114 | ||
5115 | ------------------ | |
5116 | -- Ensure_Valid -- | |
5117 | ------------------ | |
5118 | ||
5119 | procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is | |
5120 | Typ : constant Entity_Id := Etype (Expr); | |
5121 | ||
5122 | begin | |
5123 | -- Ignore call if we are not doing any validity checking | |
5124 | ||
5125 | if not Validity_Checks_On then | |
5126 | return; | |
5127 | ||
0577b0b1 | 5128 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 5129 | |
0577b0b1 | 5130 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 5131 | return; |
5132 | ||
feff2f05 | 5133 | -- No check required if expression is from the expander, we assume the |
5134 | -- expander will generate whatever checks are needed. Note that this is | |
39a0c1d3 | 5135 | -- not just an optimization, it avoids infinite recursions. |
9dfe12ae | 5136 | |
5137 | -- Unchecked conversions must be checked, unless they are initialized | |
5138 | -- scalar values, as in a component assignment in an init proc. | |
5139 | ||
5140 | -- In addition, we force a check if Force_Validity_Checks is set | |
5141 | ||
5142 | elsif not Comes_From_Source (Expr) | |
5143 | and then not Force_Validity_Checks | |
5144 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
5145 | or else Kill_Range_Check (Expr)) | |
5146 | then | |
5147 | return; | |
5148 | ||
5149 | -- No check required if expression is known to have valid value | |
5150 | ||
5151 | elsif Expr_Known_Valid (Expr) then | |
5152 | return; | |
5153 | ||
feff2f05 | 5154 | -- Ignore case of enumeration with holes where the flag is set not to |
5155 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 5156 | |
5157 | elsif Is_Enumeration_Type (Typ) | |
5158 | and then Has_Non_Standard_Rep (Typ) | |
5159 | and then Holes_OK | |
5160 | then | |
5161 | return; | |
5162 | ||
f2a06be9 | 5163 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 5164 | |
5165 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
5166 | and then Expr = Name (Parent (Expr)) | |
5167 | then | |
5168 | return; | |
5169 | ||
6fb3c314 | 5170 | -- No check on a universal real constant. The context will eventually |
38f5559f | 5171 | -- convert it to a machine number for some target type, or report an |
5172 | -- illegality. | |
5173 | ||
5174 | elsif Nkind (Expr) = N_Real_Literal | |
5175 | and then Etype (Expr) = Universal_Real | |
5176 | then | |
5177 | return; | |
5178 | ||
6fb3c314 | 5179 | -- If the expression denotes a component of a packed boolean array, |
0577b0b1 | 5180 | -- no possible check applies. We ignore the old ACATS chestnuts that |
5181 | -- involve Boolean range True..True. | |
5182 | ||
5183 | -- Note: validity checks are generated for expressions that yield a | |
5184 | -- scalar type, when it is possible to create a value that is outside of | |
5185 | -- the type. If this is a one-bit boolean no such value exists. This is | |
5186 | -- an optimization, and it also prevents compiler blowing up during the | |
5187 | -- elaboration of improperly expanded packed array references. | |
5188 | ||
5189 | elsif Nkind (Expr) = N_Indexed_Component | |
5190 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
5191 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
5192 | then | |
5193 | return; | |
5194 | ||
737e8460 | 5195 | -- For an expression with actions, we want to insert the validity check |
5196 | -- on the final Expression. | |
5197 | ||
5198 | elsif Nkind (Expr) = N_Expression_With_Actions then | |
5199 | Ensure_Valid (Expression (Expr)); | |
5200 | return; | |
5201 | ||
9dfe12ae | 5202 | -- An annoying special case. If this is an out parameter of a scalar |
5203 | -- type, then the value is not going to be accessed, therefore it is | |
5204 | -- inappropriate to do any validity check at the call site. | |
5205 | ||
5206 | else | |
5207 | -- Only need to worry about scalar types | |
5208 | ||
5209 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 5210 | declare |
5211 | P : Node_Id; | |
5212 | N : Node_Id; | |
5213 | E : Entity_Id; | |
5214 | F : Entity_Id; | |
5215 | A : Node_Id; | |
5216 | L : List_Id; | |
5217 | ||
5218 | begin | |
5219 | -- Find actual argument (which may be a parameter association) | |
5220 | -- and the parent of the actual argument (the call statement) | |
5221 | ||
5222 | N := Expr; | |
5223 | P := Parent (Expr); | |
5224 | ||
5225 | if Nkind (P) = N_Parameter_Association then | |
5226 | N := P; | |
5227 | P := Parent (N); | |
5228 | end if; | |
5229 | ||
feff2f05 | 5230 | -- Only need to worry if we are argument of a procedure call |
5231 | -- since functions don't have out parameters. If this is an | |
5232 | -- indirect or dispatching call, get signature from the | |
5233 | -- subprogram type. | |
ee6ba406 | 5234 | |
5235 | if Nkind (P) = N_Procedure_Call_Statement then | |
5236 | L := Parameter_Associations (P); | |
9dfe12ae | 5237 | |
5238 | if Is_Entity_Name (Name (P)) then | |
5239 | E := Entity (Name (P)); | |
5240 | else | |
5241 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
5242 | E := Etype (Name (P)); | |
5243 | end if; | |
ee6ba406 | 5244 | |
feff2f05 | 5245 | -- Only need to worry if there are indeed actuals, and if |
5246 | -- this could be a procedure call, otherwise we cannot get a | |
5247 | -- match (either we are not an argument, or the mode of the | |
5248 | -- formal is not OUT). This test also filters out the | |
5249 | -- generic case. | |
ee6ba406 | 5250 | |
20cf157b | 5251 | if Is_Non_Empty_List (L) and then Is_Subprogram (E) then |
5252 | ||
feff2f05 | 5253 | -- This is the loop through parameters, looking for an |
5254 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 5255 | |
5256 | F := First_Formal (E); | |
5257 | A := First (L); | |
ee6ba406 | 5258 | while Present (F) loop |
5259 | if Ekind (F) = E_Out_Parameter and then A = N then | |
5260 | return; | |
5261 | end if; | |
5262 | ||
5263 | Next_Formal (F); | |
5264 | Next (A); | |
5265 | end loop; | |
5266 | end if; | |
5267 | end if; | |
5268 | end; | |
5269 | end if; | |
5270 | end if; | |
5271 | ||
fa6a6949 | 5272 | -- If this is a boolean expression, only its elementary operands need |
90a07d4c | 5273 | -- checking: if they are valid, a boolean or short-circuit operation |
5274 | -- with them will be valid as well. | |
784d4230 | 5275 | |
5276 | if Base_Type (Typ) = Standard_Boolean | |
7af38999 | 5277 | and then |
fa6a6949 | 5278 | (Nkind (Expr) in N_Op or else Nkind (Expr) in N_Short_Circuit) |
784d4230 | 5279 | then |
5280 | return; | |
5281 | end if; | |
5282 | ||
0577b0b1 | 5283 | -- If we fall through, a validity check is required |
ee6ba406 | 5284 | |
5285 | Insert_Valid_Check (Expr); | |
ce7498d3 | 5286 | |
5287 | if Is_Entity_Name (Expr) | |
5288 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
5289 | then | |
5290 | Set_Is_Known_Valid (Entity (Expr)); | |
5291 | end if; | |
ee6ba406 | 5292 | end Ensure_Valid; |
5293 | ||
5294 | ---------------------- | |
5295 | -- Expr_Known_Valid -- | |
5296 | ---------------------- | |
5297 | ||
5298 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
5299 | Typ : constant Entity_Id := Etype (Expr); | |
5300 | ||
5301 | begin | |
feff2f05 | 5302 | -- Non-scalar types are always considered valid, since they never give |
5303 | -- rise to the issues of erroneous or bounded error behavior that are | |
5304 | -- the concern. In formal reference manual terms the notion of validity | |
5305 | -- only applies to scalar types. Note that even when packed arrays are | |
5306 | -- represented using modular types, they are still arrays semantically, | |
5307 | -- so they are also always valid (in particular, the unused bits can be | |
5308 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 5309 | |
fa814356 | 5310 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then |
ee6ba406 | 5311 | return True; |
5312 | ||
5313 | -- If no validity checking, then everything is considered valid | |
5314 | ||
5315 | elsif not Validity_Checks_On then | |
5316 | return True; | |
5317 | ||
5318 | -- Floating-point types are considered valid unless floating-point | |
5319 | -- validity checks have been specifically turned on. | |
5320 | ||
5321 | elsif Is_Floating_Point_Type (Typ) | |
5322 | and then not Validity_Check_Floating_Point | |
5323 | then | |
5324 | return True; | |
5325 | ||
feff2f05 | 5326 | -- If the expression is the value of an object that is known to be |
5327 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 5328 | |
5329 | elsif Is_Entity_Name (Expr) | |
5330 | and then Is_Known_Valid (Entity (Expr)) | |
985fe5d6 | 5331 | |
5332 | -- Exclude volatile variables | |
5333 | ||
5334 | and then not Treat_As_Volatile (Entity (Expr)) | |
ee6ba406 | 5335 | then |
5336 | return True; | |
5337 | ||
0577b0b1 | 5338 | -- References to discriminants are always considered valid. The value |
5339 | -- of a discriminant gets checked when the object is built. Within the | |
5340 | -- record, we consider it valid, and it is important to do so, since | |
5341 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 5342 | -- reference discriminants out of scope. Discriminants of concurrent |
5343 | -- types are excluded for the same reason. | |
0577b0b1 | 5344 | |
5345 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 5346 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 5347 | then |
5348 | return True; | |
5349 | ||
feff2f05 | 5350 | -- If the type is one for which all values are known valid, then we are |
5351 | -- sure that the value is valid except in the slightly odd case where | |
5352 | -- the expression is a reference to a variable whose size has been | |
5353 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 5354 | |
5355 | elsif Is_Known_Valid (Typ) then | |
5356 | if Is_Entity_Name (Expr) | |
5357 | and then Ekind (Entity (Expr)) = E_Variable | |
5358 | and then Esize (Entity (Expr)) > Esize (Typ) | |
5359 | then | |
5360 | return False; | |
5361 | else | |
5362 | return True; | |
5363 | end if; | |
5364 | ||
5365 | -- Integer and character literals always have valid values, where | |
5366 | -- appropriate these will be range checked in any case. | |
5367 | ||
20cf157b | 5368 | elsif Nkind_In (Expr, N_Integer_Literal, N_Character_Literal) then |
ee6ba406 | 5369 | return True; |
5370 | ||
91e47010 | 5371 | -- Real literals are assumed to be valid in VM targets |
5372 | ||
20cf157b | 5373 | elsif VM_Target /= No_VM and then Nkind (Expr) = N_Real_Literal then |
91e47010 | 5374 | return True; |
5375 | ||
ee6ba406 | 5376 | -- If we have a type conversion or a qualification of a known valid |
5377 | -- value, then the result will always be valid. | |
5378 | ||
20cf157b | 5379 | elsif Nkind_In (Expr, N_Type_Conversion, N_Qualified_Expression) then |
ee6ba406 | 5380 | return Expr_Known_Valid (Expression (Expr)); |
5381 | ||
1eb1395f | 5382 | -- Case of expression is a non-floating-point operator. In this case we |
5383 | -- can assume the result is valid the generated code for the operator | |
5384 | -- will include whatever checks are needed (e.g. range checks) to ensure | |
5385 | -- validity. This assumption does not hold for the floating-point case, | |
5386 | -- since floating-point operators can generate Infinite or NaN results | |
5387 | -- which are considered invalid. | |
5388 | ||
5389 | -- Historical note: in older versions, the exemption of floating-point | |
5390 | -- types from this assumption was done only in cases where the parent | |
5391 | -- was an assignment, function call or parameter association. Presumably | |
5392 | -- the idea was that in other contexts, the result would be checked | |
5393 | -- elsewhere, but this list of cases was missing tests (at least the | |
5394 | -- N_Object_Declaration case, as shown by a reported missing validity | |
5395 | -- check), and it is not clear why function calls but not procedure | |
5396 | -- calls were tested for. It really seems more accurate and much | |
5397 | -- safer to recognize that expressions which are the result of a | |
5398 | -- floating-point operator can never be assumed to be valid. | |
5399 | ||
5400 | elsif Nkind (Expr) in N_Op and then not Is_Floating_Point_Type (Typ) then | |
5401 | return True; | |
1d90d657 | 5402 | |
feff2f05 | 5403 | -- The result of a membership test is always valid, since it is true or |
5404 | -- false, there are no other possibilities. | |
0577b0b1 | 5405 | |
5406 | elsif Nkind (Expr) in N_Membership_Test then | |
5407 | return True; | |
5408 | ||
ee6ba406 | 5409 | -- For all other cases, we do not know the expression is valid |
5410 | ||
5411 | else | |
5412 | return False; | |
5413 | end if; | |
5414 | end Expr_Known_Valid; | |
5415 | ||
9dfe12ae | 5416 | ---------------- |
5417 | -- Find_Check -- | |
5418 | ---------------- | |
5419 | ||
5420 | procedure Find_Check | |
5421 | (Expr : Node_Id; | |
5422 | Check_Type : Character; | |
5423 | Target_Type : Entity_Id; | |
5424 | Entry_OK : out Boolean; | |
5425 | Check_Num : out Nat; | |
5426 | Ent : out Entity_Id; | |
5427 | Ofs : out Uint) | |
5428 | is | |
5429 | function Within_Range_Of | |
5430 | (Target_Type : Entity_Id; | |
314a23b6 | 5431 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 5432 | -- Given a requirement for checking a range against Target_Type, and |
5433 | -- and a range Check_Type against which a check has already been made, | |
5434 | -- determines if the check against check type is sufficient to ensure | |
5435 | -- that no check against Target_Type is required. | |
5436 | ||
5437 | --------------------- | |
5438 | -- Within_Range_Of -- | |
5439 | --------------------- | |
5440 | ||
5441 | function Within_Range_Of | |
5442 | (Target_Type : Entity_Id; | |
314a23b6 | 5443 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 5444 | is |
5445 | begin | |
5446 | if Target_Type = Check_Type then | |
5447 | return True; | |
5448 | ||
5449 | else | |
5450 | declare | |
5451 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
5452 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
5453 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
5454 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
5455 | ||
5456 | begin | |
5457 | if (Tlo = Clo | |
5458 | or else (Compile_Time_Known_Value (Tlo) | |
5459 | and then | |
5460 | Compile_Time_Known_Value (Clo) | |
5461 | and then | |
5462 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
5463 | and then | |
5464 | (Thi = Chi | |
5465 | or else (Compile_Time_Known_Value (Thi) | |
5466 | and then | |
5467 | Compile_Time_Known_Value (Chi) | |
5468 | and then | |
5469 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
5470 | then | |
5471 | return True; | |
5472 | else | |
5473 | return False; | |
5474 | end if; | |
5475 | end; | |
5476 | end if; | |
5477 | end Within_Range_Of; | |
5478 | ||
5479 | -- Start of processing for Find_Check | |
5480 | ||
5481 | begin | |
ed195555 | 5482 | -- Establish default, in case no entry is found |
9dfe12ae | 5483 | |
5484 | Check_Num := 0; | |
5485 | ||
5486 | -- Case of expression is simple entity reference | |
5487 | ||
5488 | if Is_Entity_Name (Expr) then | |
5489 | Ent := Entity (Expr); | |
5490 | Ofs := Uint_0; | |
5491 | ||
5492 | -- Case of expression is entity + known constant | |
5493 | ||
5494 | elsif Nkind (Expr) = N_Op_Add | |
5495 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5496 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5497 | then | |
5498 | Ent := Entity (Left_Opnd (Expr)); | |
5499 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
5500 | ||
5501 | -- Case of expression is entity - known constant | |
5502 | ||
5503 | elsif Nkind (Expr) = N_Op_Subtract | |
5504 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
5505 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
5506 | then | |
5507 | Ent := Entity (Left_Opnd (Expr)); | |
5508 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
5509 | ||
5510 | -- Any other expression is not of the right form | |
5511 | ||
5512 | else | |
5513 | Ent := Empty; | |
5514 | Ofs := Uint_0; | |
5515 | Entry_OK := False; | |
5516 | return; | |
5517 | end if; | |
5518 | ||
feff2f05 | 5519 | -- Come here with expression of appropriate form, check if entity is an |
5520 | -- appropriate one for our purposes. | |
9dfe12ae | 5521 | |
5522 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 5523 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 5524 | and then not Is_Library_Level_Entity (Ent) |
5525 | then | |
5526 | Entry_OK := True; | |
5527 | else | |
5528 | Entry_OK := False; | |
5529 | return; | |
5530 | end if; | |
5531 | ||
5532 | -- See if there is matching check already | |
5533 | ||
5534 | for J in reverse 1 .. Num_Saved_Checks loop | |
5535 | declare | |
5536 | SC : Saved_Check renames Saved_Checks (J); | |
9dfe12ae | 5537 | begin |
5538 | if SC.Killed = False | |
5539 | and then SC.Entity = Ent | |
5540 | and then SC.Offset = Ofs | |
5541 | and then SC.Check_Type = Check_Type | |
5542 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
5543 | then | |
5544 | Check_Num := J; | |
5545 | return; | |
5546 | end if; | |
5547 | end; | |
5548 | end loop; | |
5549 | ||
5550 | -- If we fall through entry was not found | |
5551 | ||
9dfe12ae | 5552 | return; |
5553 | end Find_Check; | |
5554 | ||
5555 | --------------------------------- | |
5556 | -- Generate_Discriminant_Check -- | |
5557 | --------------------------------- | |
5558 | ||
5559 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 5560 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 5561 | |
5562 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
5563 | Loc : constant Source_Ptr := Sloc (N); | |
5564 | Pref : constant Node_Id := Prefix (N); | |
5565 | Sel : constant Node_Id := Selector_Name (N); | |
5566 | ||
5567 | Orig_Comp : constant Entity_Id := | |
b6341c67 | 5568 | Original_Record_Component (Entity (Sel)); |
9dfe12ae | 5569 | -- The original component to be checked |
5570 | ||
5571 | Discr_Fct : constant Entity_Id := | |
b6341c67 | 5572 | Discriminant_Checking_Func (Orig_Comp); |
9dfe12ae | 5573 | -- The discriminant checking function |
5574 | ||
5575 | Discr : Entity_Id; | |
5576 | -- One discriminant to be checked in the type | |
5577 | ||
5578 | Real_Discr : Entity_Id; | |
5579 | -- Actual discriminant in the call | |
5580 | ||
5581 | Pref_Type : Entity_Id; | |
5582 | -- Type of relevant prefix (ignoring private/access stuff) | |
5583 | ||
5584 | Args : List_Id; | |
5585 | -- List of arguments for function call | |
5586 | ||
5587 | Formal : Entity_Id; | |
feff2f05 | 5588 | -- Keep track of the formal corresponding to the actual we build for |
5589 | -- each discriminant, in order to be able to perform the necessary type | |
5590 | -- conversions. | |
9dfe12ae | 5591 | |
5592 | Scomp : Node_Id; | |
5593 | -- Selected component reference for checking function argument | |
5594 | ||
5595 | begin | |
5596 | Pref_Type := Etype (Pref); | |
5597 | ||
5598 | -- Force evaluation of the prefix, so that it does not get evaluated | |
5599 | -- twice (once for the check, once for the actual reference). Such a | |
20cf157b | 5600 | -- double evaluation is always a potential source of inefficiency, and |
5601 | -- is functionally incorrect in the volatile case, or when the prefix | |
5602 | -- may have side-effects. A non-volatile entity or a component of a | |
5603 | -- non-volatile entity requires no evaluation. | |
9dfe12ae | 5604 | |
5605 | if Is_Entity_Name (Pref) then | |
5606 | if Treat_As_Volatile (Entity (Pref)) then | |
5607 | Force_Evaluation (Pref, Name_Req => True); | |
5608 | end if; | |
5609 | ||
5610 | elsif Treat_As_Volatile (Etype (Pref)) then | |
20cf157b | 5611 | Force_Evaluation (Pref, Name_Req => True); |
9dfe12ae | 5612 | |
5613 | elsif Nkind (Pref) = N_Selected_Component | |
5614 | and then Is_Entity_Name (Prefix (Pref)) | |
5615 | then | |
5616 | null; | |
5617 | ||
5618 | else | |
5619 | Force_Evaluation (Pref, Name_Req => True); | |
5620 | end if; | |
5621 | ||
5622 | -- For a tagged type, use the scope of the original component to | |
5623 | -- obtain the type, because ??? | |
5624 | ||
5625 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
5626 | Pref_Type := Scope (Orig_Comp); | |
5627 | ||
feff2f05 | 5628 | -- For an untagged derived type, use the discriminants of the parent |
5629 | -- which have been renamed in the derivation, possibly by a one-to-many | |
5630 | -- discriminant constraint. For non-tagged type, initially get the Etype | |
5631 | -- of the prefix | |
9dfe12ae | 5632 | |
5633 | else | |
5634 | if Is_Derived_Type (Pref_Type) | |
5635 | and then Number_Discriminants (Pref_Type) /= | |
5636 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
5637 | then | |
5638 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
5639 | end if; | |
5640 | end if; | |
5641 | ||
5642 | -- We definitely should have a checking function, This routine should | |
5643 | -- not be called if no discriminant checking function is present. | |
5644 | ||
5645 | pragma Assert (Present (Discr_Fct)); | |
5646 | ||
5647 | -- Create the list of the actual parameters for the call. This list | |
5648 | -- is the list of the discriminant fields of the record expression to | |
5649 | -- be discriminant checked. | |
5650 | ||
5651 | Args := New_List; | |
5652 | Formal := First_Formal (Discr_Fct); | |
5653 | Discr := First_Discriminant (Pref_Type); | |
5654 | while Present (Discr) loop | |
5655 | ||
5656 | -- If we have a corresponding discriminant field, and a parent | |
5657 | -- subtype is present, then we want to use the corresponding | |
5658 | -- discriminant since this is the one with the useful value. | |
5659 | ||
5660 | if Present (Corresponding_Discriminant (Discr)) | |
5661 | and then Ekind (Pref_Type) = E_Record_Type | |
5662 | and then Present (Parent_Subtype (Pref_Type)) | |
5663 | then | |
5664 | Real_Discr := Corresponding_Discriminant (Discr); | |
5665 | else | |
5666 | Real_Discr := Discr; | |
5667 | end if; | |
5668 | ||
5669 | -- Construct the reference to the discriminant | |
5670 | ||
5671 | Scomp := | |
5672 | Make_Selected_Component (Loc, | |
5673 | Prefix => | |
5674 | Unchecked_Convert_To (Pref_Type, | |
5675 | Duplicate_Subexpr (Pref)), | |
5676 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
5677 | ||
5678 | -- Manually analyze and resolve this selected component. We really | |
5679 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 5680 | -- playing discriminal games etc with this reference. Then we append |
5681 | -- the argument to the list we are gathering. | |
9dfe12ae | 5682 | |
5683 | Set_Etype (Scomp, Etype (Real_Discr)); | |
5684 | Set_Analyzed (Scomp, True); | |
5685 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
5686 | ||
5687 | Next_Formal_With_Extras (Formal); | |
5688 | Next_Discriminant (Discr); | |
5689 | end loop; | |
5690 | ||
5691 | -- Now build and insert the call | |
5692 | ||
5693 | Insert_Action (N, | |
5694 | Make_Raise_Constraint_Error (Loc, | |
5695 | Condition => | |
5696 | Make_Function_Call (Loc, | |
20cf157b | 5697 | Name => New_Occurrence_Of (Discr_Fct, Loc), |
9dfe12ae | 5698 | Parameter_Associations => Args), |
5699 | Reason => CE_Discriminant_Check_Failed)); | |
5700 | end Generate_Discriminant_Check; | |
5701 | ||
5c99c290 | 5702 | --------------------------- |
5703 | -- Generate_Index_Checks -- | |
5704 | --------------------------- | |
9dfe12ae | 5705 | |
5706 | procedure Generate_Index_Checks (N : Node_Id) is | |
05f3e139 | 5707 | |
5708 | function Entity_Of_Prefix return Entity_Id; | |
5709 | -- Returns the entity of the prefix of N (or Empty if not found) | |
5710 | ||
3f42e2a7 | 5711 | ---------------------- |
5712 | -- Entity_Of_Prefix -- | |
5713 | ---------------------- | |
5714 | ||
05f3e139 | 5715 | function Entity_Of_Prefix return Entity_Id is |
e5d38095 | 5716 | P : Node_Id; |
5717 | ||
05f3e139 | 5718 | begin |
e5d38095 | 5719 | P := Prefix (N); |
05f3e139 | 5720 | while not Is_Entity_Name (P) loop |
5721 | if not Nkind_In (P, N_Selected_Component, | |
5722 | N_Indexed_Component) | |
5723 | then | |
5724 | return Empty; | |
5725 | end if; | |
5726 | ||
5727 | P := Prefix (P); | |
5728 | end loop; | |
5729 | ||
5730 | return Entity (P); | |
5731 | end Entity_Of_Prefix; | |
5732 | ||
5733 | -- Local variables | |
5734 | ||
5735 | Loc : constant Source_Ptr := Sloc (N); | |
5736 | A : constant Node_Id := Prefix (N); | |
5737 | A_Ent : constant Entity_Id := Entity_Of_Prefix; | |
5738 | Sub : Node_Id; | |
9dfe12ae | 5739 | |
3f42e2a7 | 5740 | -- Start of processing for Generate_Index_Checks |
5741 | ||
9dfe12ae | 5742 | begin |
05f3e139 | 5743 | -- Ignore call if the prefix is not an array since we have a serious |
5744 | -- error in the sources. Ignore it also if index checks are suppressed | |
5745 | -- for array object or type. | |
0577b0b1 | 5746 | |
05f3e139 | 5747 | if not Is_Array_Type (Etype (A)) |
20cf157b | 5748 | or else (Present (A_Ent) and then Index_Checks_Suppressed (A_Ent)) |
0577b0b1 | 5749 | or else Index_Checks_Suppressed (Etype (A)) |
5750 | then | |
5751 | return; | |
df9fba45 | 5752 | |
5753 | -- The indexed component we are dealing with contains 'Loop_Entry in its | |
5754 | -- prefix. This case arises when analysis has determined that constructs | |
5755 | -- such as | |
5756 | ||
5757 | -- Prefix'Loop_Entry (Expr) | |
5758 | -- Prefix'Loop_Entry (Expr1, Expr2, ... ExprN) | |
5759 | ||
5760 | -- require rewriting for error detection purposes. A side effect of this | |
5761 | -- action is the generation of index checks that mention 'Loop_Entry. | |
5762 | -- Delay the generation of the check until 'Loop_Entry has been properly | |
5763 | -- expanded. This is done in Expand_Loop_Entry_Attributes. | |
5764 | ||
5765 | elsif Nkind (Prefix (N)) = N_Attribute_Reference | |
5766 | and then Attribute_Name (Prefix (N)) = Name_Loop_Entry | |
5767 | then | |
5768 | return; | |
0577b0b1 | 5769 | end if; |
5770 | ||
05f3e139 | 5771 | -- Generate a raise of constraint error with the appropriate reason and |
5772 | -- a condition of the form: | |
5773 | ||
3f42e2a7 | 5774 | -- Base_Type (Sub) not in Array'Range (Subscript) |
05f3e139 | 5775 | |
5776 | -- Note that the reason we generate the conversion to the base type here | |
5777 | -- is that we definitely want the range check to take place, even if it | |
5778 | -- looks like the subtype is OK. Optimization considerations that allow | |
5779 | -- us to omit the check have already been taken into account in the | |
5780 | -- setting of the Do_Range_Check flag earlier on. | |
0577b0b1 | 5781 | |
9dfe12ae | 5782 | Sub := First (Expressions (N)); |
05f3e139 | 5783 | |
5784 | -- Handle string literals | |
5785 | ||
5786 | if Ekind (Etype (A)) = E_String_Literal_Subtype then | |
9dfe12ae | 5787 | if Do_Range_Check (Sub) then |
5788 | Set_Do_Range_Check (Sub, False); | |
5789 | ||
05f3e139 | 5790 | -- For string literals we obtain the bounds of the string from the |
5791 | -- associated subtype. | |
9dfe12ae | 5792 | |
05f3e139 | 5793 | Insert_Action (N, |
094ed68e | 5794 | Make_Raise_Constraint_Error (Loc, |
5795 | Condition => | |
5796 | Make_Not_In (Loc, | |
5797 | Left_Opnd => | |
5798 | Convert_To (Base_Type (Etype (Sub)), | |
5799 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5800 | Right_Opnd => | |
5801 | Make_Attribute_Reference (Loc, | |
83c6c069 | 5802 | Prefix => New_Occurrence_Of (Etype (A), Loc), |
094ed68e | 5803 | Attribute_Name => Name_Range)), |
5804 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5805 | end if; |
9dfe12ae | 5806 | |
05f3e139 | 5807 | -- General case |
9dfe12ae | 5808 | |
05f3e139 | 5809 | else |
5810 | declare | |
5811 | A_Idx : Node_Id := Empty; | |
5812 | A_Range : Node_Id; | |
5813 | Ind : Nat; | |
5814 | Num : List_Id; | |
5815 | Range_N : Node_Id; | |
9dfe12ae | 5816 | |
05f3e139 | 5817 | begin |
5818 | A_Idx := First_Index (Etype (A)); | |
5819 | Ind := 1; | |
5820 | while Present (Sub) loop | |
5821 | if Do_Range_Check (Sub) then | |
5822 | Set_Do_Range_Check (Sub, False); | |
9dfe12ae | 5823 | |
05f3e139 | 5824 | -- Force evaluation except for the case of a simple name of |
5825 | -- a non-volatile entity. | |
9dfe12ae | 5826 | |
05f3e139 | 5827 | if not Is_Entity_Name (Sub) |
5828 | or else Treat_As_Volatile (Entity (Sub)) | |
5829 | then | |
5830 | Force_Evaluation (Sub); | |
5831 | end if; | |
9dfe12ae | 5832 | |
05f3e139 | 5833 | if Nkind (A_Idx) = N_Range then |
5834 | A_Range := A_Idx; | |
5835 | ||
5836 | elsif Nkind (A_Idx) = N_Identifier | |
5837 | or else Nkind (A_Idx) = N_Expanded_Name | |
5838 | then | |
5839 | A_Range := Scalar_Range (Entity (A_Idx)); | |
5840 | ||
5841 | else pragma Assert (Nkind (A_Idx) = N_Subtype_Indication); | |
5842 | A_Range := Range_Expression (Constraint (A_Idx)); | |
5843 | end if; | |
5844 | ||
5845 | -- For array objects with constant bounds we can generate | |
5846 | -- the index check using the bounds of the type of the index | |
5847 | ||
5848 | if Present (A_Ent) | |
5849 | and then Ekind (A_Ent) = E_Variable | |
5850 | and then Is_Constant_Bound (Low_Bound (A_Range)) | |
5851 | and then Is_Constant_Bound (High_Bound (A_Range)) | |
5852 | then | |
5853 | Range_N := | |
5854 | Make_Attribute_Reference (Loc, | |
3f42e2a7 | 5855 | Prefix => |
83c6c069 | 5856 | New_Occurrence_Of (Etype (A_Idx), Loc), |
05f3e139 | 5857 | Attribute_Name => Name_Range); |
5858 | ||
5859 | -- For arrays with non-constant bounds we cannot generate | |
5860 | -- the index check using the bounds of the type of the index | |
5861 | -- since it may reference discriminants of some enclosing | |
5862 | -- type. We obtain the bounds directly from the prefix | |
5863 | -- object. | |
5864 | ||
5865 | else | |
5866 | if Ind = 1 then | |
5867 | Num := No_List; | |
5868 | else | |
5869 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
5870 | end if; | |
5871 | ||
5872 | Range_N := | |
5873 | Make_Attribute_Reference (Loc, | |
5874 | Prefix => | |
5875 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
5876 | Attribute_Name => Name_Range, | |
5877 | Expressions => Num); | |
5878 | end if; | |
5879 | ||
5880 | Insert_Action (N, | |
094ed68e | 5881 | Make_Raise_Constraint_Error (Loc, |
5882 | Condition => | |
5883 | Make_Not_In (Loc, | |
5884 | Left_Opnd => | |
5885 | Convert_To (Base_Type (Etype (Sub)), | |
5886 | Duplicate_Subexpr_Move_Checks (Sub)), | |
5887 | Right_Opnd => Range_N), | |
5888 | Reason => CE_Index_Check_Failed)); | |
05f3e139 | 5889 | end if; |
5890 | ||
5891 | A_Idx := Next_Index (A_Idx); | |
5892 | Ind := Ind + 1; | |
5893 | Next (Sub); | |
5894 | end loop; | |
5895 | end; | |
5896 | end if; | |
9dfe12ae | 5897 | end Generate_Index_Checks; |
5898 | ||
5899 | -------------------------- | |
5900 | -- Generate_Range_Check -- | |
5901 | -------------------------- | |
5902 | ||
5903 | procedure Generate_Range_Check | |
5904 | (N : Node_Id; | |
5905 | Target_Type : Entity_Id; | |
5906 | Reason : RT_Exception_Code) | |
5907 | is | |
5908 | Loc : constant Source_Ptr := Sloc (N); | |
5909 | Source_Type : constant Entity_Id := Etype (N); | |
5910 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
5911 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
5912 | ||
5913 | begin | |
feff2f05 | 5914 | -- First special case, if the source type is already within the range |
5915 | -- of the target type, then no check is needed (probably we should have | |
5916 | -- stopped Do_Range_Check from being set in the first place, but better | |
39a0c1d3 | 5917 | -- late than never in preventing junk code. |
9dfe12ae | 5918 | |
7a1dabb3 | 5919 | if In_Subrange_Of (Source_Type, Target_Type) |
b40670e1 | 5920 | |
5921 | -- We do NOT apply this if the source node is a literal, since in this | |
5922 | -- case the literal has already been labeled as having the subtype of | |
5923 | -- the target. | |
5924 | ||
9dfe12ae | 5925 | and then not |
b40670e1 | 5926 | (Nkind_In (N, N_Integer_Literal, N_Real_Literal, N_Character_Literal) |
9dfe12ae | 5927 | or else |
b40670e1 | 5928 | (Is_Entity_Name (N) |
5929 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
5930 | ||
5931 | -- Also do not apply this for floating-point if Check_Float_Overflow | |
5932 | ||
5933 | and then not | |
5934 | (Is_Floating_Point_Type (Source_Type) and Check_Float_Overflow) | |
9dfe12ae | 5935 | then |
5936 | return; | |
5937 | end if; | |
5938 | ||
5939 | -- We need a check, so force evaluation of the node, so that it does | |
5940 | -- not get evaluated twice (once for the check, once for the actual | |
5941 | -- reference). Such a double evaluation is always a potential source | |
5942 | -- of inefficiency, and is functionally incorrect in the volatile case. | |
5943 | ||
b40670e1 | 5944 | if not Is_Entity_Name (N) or else Treat_As_Volatile (Entity (N)) then |
9dfe12ae | 5945 | Force_Evaluation (N); |
5946 | end if; | |
5947 | ||
feff2f05 | 5948 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
5949 | -- the same since in this case we can simply do a direct check of the | |
5950 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 5951 | |
5952 | -- [constraint_error when N not in Target_Type] | |
5953 | ||
5954 | -- Note: this is by far the most common case, for example all cases of | |
5955 | -- checks on the RHS of assignments are in this category, but not all | |
5956 | -- cases are like this. Notably conversions can involve two types. | |
5957 | ||
5958 | if Source_Base_Type = Target_Base_Type then | |
5959 | Insert_Action (N, | |
5960 | Make_Raise_Constraint_Error (Loc, | |
5961 | Condition => | |
5962 | Make_Not_In (Loc, | |
5963 | Left_Opnd => Duplicate_Subexpr (N), | |
5964 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
5965 | Reason => Reason)); | |
5966 | ||
5967 | -- Next test for the case where the target type is within the bounds | |
5968 | -- of the base type of the source type, since in this case we can | |
5969 | -- simply convert these bounds to the base type of T to do the test. | |
5970 | ||
5971 | -- [constraint_error when N not in | |
5972 | -- Source_Base_Type (Target_Type'First) | |
5973 | -- .. | |
5974 | -- Source_Base_Type(Target_Type'Last))] | |
5975 | ||
f2a06be9 | 5976 | -- The conversions will always work and need no check |
9dfe12ae | 5977 | |
a9b57347 | 5978 | -- Unchecked_Convert_To is used instead of Convert_To to handle the case |
5979 | -- of converting from an enumeration value to an integer type, such as | |
5980 | -- occurs for the case of generating a range check on Enum'Val(Exp) | |
5981 | -- (which used to be handled by gigi). This is OK, since the conversion | |
5982 | -- itself does not require a check. | |
5983 | ||
7a1dabb3 | 5984 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
9dfe12ae | 5985 | Insert_Action (N, |
5986 | Make_Raise_Constraint_Error (Loc, | |
5987 | Condition => | |
5988 | Make_Not_In (Loc, | |
5989 | Left_Opnd => Duplicate_Subexpr (N), | |
5990 | ||
5991 | Right_Opnd => | |
5992 | Make_Range (Loc, | |
5993 | Low_Bound => | |
a9b57347 | 5994 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 5995 | Make_Attribute_Reference (Loc, |
5996 | Prefix => | |
5997 | New_Occurrence_Of (Target_Type, Loc), | |
5998 | Attribute_Name => Name_First)), | |
5999 | ||
6000 | High_Bound => | |
a9b57347 | 6001 | Unchecked_Convert_To (Source_Base_Type, |
9dfe12ae | 6002 | Make_Attribute_Reference (Loc, |
6003 | Prefix => | |
6004 | New_Occurrence_Of (Target_Type, Loc), | |
6005 | Attribute_Name => Name_Last)))), | |
6006 | Reason => Reason)); | |
6007 | ||
feff2f05 | 6008 | -- Note that at this stage we now that the Target_Base_Type is not in |
6009 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
6010 | -- is not in this range). It could still be the case that Source_Type is | |
6011 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 6012 | |
feff2f05 | 6013 | -- If that is the case, we can freely convert the source to the target, |
6014 | -- and then test the target result against the bounds. | |
9dfe12ae | 6015 | |
7a1dabb3 | 6016 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
9dfe12ae | 6017 | |
feff2f05 | 6018 | -- We make a temporary to hold the value of the converted value |
6019 | -- (converted to the base type), and then we will do the test against | |
6020 | -- this temporary. | |
9dfe12ae | 6021 | |
6022 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); | |
6023 | -- [constraint_error when Tnn not in Target_Type] | |
6024 | ||
6025 | -- Then the conversion itself is replaced by an occurrence of Tnn | |
6026 | ||
6027 | declare | |
46eb6933 | 6028 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 6029 | |
6030 | begin | |
6031 | Insert_Actions (N, New_List ( | |
6032 | Make_Object_Declaration (Loc, | |
6033 | Defining_Identifier => Tnn, | |
6034 | Object_Definition => | |
6035 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6036 | Constant_Present => True, | |
6037 | Expression => | |
6038 | Make_Type_Conversion (Loc, | |
6039 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
6040 | Expression => Duplicate_Subexpr (N))), | |
6041 | ||
6042 | Make_Raise_Constraint_Error (Loc, | |
6043 | Condition => | |
6044 | Make_Not_In (Loc, | |
6045 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6046 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
6047 | ||
6048 | Reason => Reason))); | |
6049 | ||
6050 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
2af58f67 | 6051 | |
6052 | -- Set the type of N, because the declaration for Tnn might not | |
6053 | -- be analyzed yet, as is the case if N appears within a record | |
6054 | -- declaration, as a discriminant constraint or expression. | |
6055 | ||
6056 | Set_Etype (N, Target_Base_Type); | |
9dfe12ae | 6057 | end; |
6058 | ||
6059 | -- At this stage, we know that we have two scalar types, which are | |
6060 | -- directly convertible, and where neither scalar type has a base | |
6061 | -- range that is in the range of the other scalar type. | |
6062 | ||
6063 | -- The only way this can happen is with a signed and unsigned type. | |
6064 | -- So test for these two cases: | |
6065 | ||
6066 | else | |
6067 | -- Case of the source is unsigned and the target is signed | |
6068 | ||
6069 | if Is_Unsigned_Type (Source_Base_Type) | |
6070 | and then not Is_Unsigned_Type (Target_Base_Type) | |
6071 | then | |
6072 | -- If the source is unsigned and the target is signed, then we | |
6073 | -- know that the source is not shorter than the target (otherwise | |
6074 | -- the source base type would be in the target base type range). | |
6075 | ||
feff2f05 | 6076 | -- In other words, the unsigned type is either the same size as |
6077 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 6078 | |
6079 | pragma Assert | |
6080 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
6081 | ||
6082 | -- We only need to check the low bound if the low bound of the | |
6083 | -- target type is non-negative. If the low bound of the target | |
6084 | -- type is negative, then we know that we will fit fine. | |
6085 | ||
6086 | -- If the high bound of the target type is negative, then we | |
6087 | -- know we have a constraint error, since we can't possibly | |
6088 | -- have a negative source. | |
6089 | ||
6090 | -- With these two checks out of the way, we can do the check | |
6091 | -- using the source type safely | |
6092 | ||
39a0c1d3 | 6093 | -- This is definitely the most annoying case. |
9dfe12ae | 6094 | |
6095 | -- [constraint_error | |
6096 | -- when (Target_Type'First >= 0 | |
6097 | -- and then | |
6098 | -- N < Source_Base_Type (Target_Type'First)) | |
6099 | -- or else Target_Type'Last < 0 | |
6100 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
6101 | ||
6102 | -- We turn off all checks since we know that the conversions | |
6103 | -- will work fine, given the guards for negative values. | |
6104 | ||
6105 | Insert_Action (N, | |
6106 | Make_Raise_Constraint_Error (Loc, | |
6107 | Condition => | |
6108 | Make_Or_Else (Loc, | |
6109 | Make_Or_Else (Loc, | |
6110 | Left_Opnd => | |
6111 | Make_And_Then (Loc, | |
6112 | Left_Opnd => Make_Op_Ge (Loc, | |
6113 | Left_Opnd => | |
6114 | Make_Attribute_Reference (Loc, | |
6115 | Prefix => | |
6116 | New_Occurrence_Of (Target_Type, Loc), | |
6117 | Attribute_Name => Name_First), | |
6118 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
6119 | ||
6120 | Right_Opnd => | |
6121 | Make_Op_Lt (Loc, | |
6122 | Left_Opnd => Duplicate_Subexpr (N), | |
6123 | Right_Opnd => | |
6124 | Convert_To (Source_Base_Type, | |
6125 | Make_Attribute_Reference (Loc, | |
6126 | Prefix => | |
6127 | New_Occurrence_Of (Target_Type, Loc), | |
6128 | Attribute_Name => Name_First)))), | |
6129 | ||
6130 | Right_Opnd => | |
6131 | Make_Op_Lt (Loc, | |
6132 | Left_Opnd => | |
6133 | Make_Attribute_Reference (Loc, | |
6134 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6135 | Attribute_Name => Name_Last), | |
6136 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
6137 | ||
6138 | Right_Opnd => | |
6139 | Make_Op_Gt (Loc, | |
6140 | Left_Opnd => Duplicate_Subexpr (N), | |
6141 | Right_Opnd => | |
6142 | Convert_To (Source_Base_Type, | |
6143 | Make_Attribute_Reference (Loc, | |
6144 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
6145 | Attribute_Name => Name_Last)))), | |
6146 | ||
6147 | Reason => Reason), | |
6148 | Suppress => All_Checks); | |
6149 | ||
6150 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 6151 | -- the target is unsigned. |
9dfe12ae | 6152 | |
6153 | else | |
6154 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
20cf157b | 6155 | and then Is_Unsigned_Type (Target_Base_Type)); |
9dfe12ae | 6156 | |
feff2f05 | 6157 | -- If the source is signed and the target is unsigned, then we |
6158 | -- know that the target is not shorter than the source (otherwise | |
6159 | -- the target base type would be in the source base type range). | |
9dfe12ae | 6160 | |
feff2f05 | 6161 | -- In other words, the unsigned type is either the same size as |
6162 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 6163 | |
feff2f05 | 6164 | -- Clearly we have an error if the source value is negative since |
6165 | -- no unsigned type can have negative values. If the source type | |
6166 | -- is non-negative, then the check can be done using the target | |
6167 | -- type. | |
9dfe12ae | 6168 | |
6169 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
6170 | ||
6171 | -- [constraint_error | |
6172 | -- when N < 0 or else Tnn not in Target_Type]; | |
6173 | ||
feff2f05 | 6174 | -- We turn off all checks for the conversion of N to the target |
6175 | -- base type, since we generate the explicit check to ensure that | |
6176 | -- the value is non-negative | |
9dfe12ae | 6177 | |
6178 | declare | |
46eb6933 | 6179 | Tnn : constant Entity_Id := Make_Temporary (Loc, 'T', N); |
9dfe12ae | 6180 | |
6181 | begin | |
6182 | Insert_Actions (N, New_List ( | |
6183 | Make_Object_Declaration (Loc, | |
6184 | Defining_Identifier => Tnn, | |
6185 | Object_Definition => | |
6186 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6187 | Constant_Present => True, | |
6188 | Expression => | |
a9b57347 | 6189 | Make_Unchecked_Type_Conversion (Loc, |
9dfe12ae | 6190 | Subtype_Mark => |
6191 | New_Occurrence_Of (Target_Base_Type, Loc), | |
6192 | Expression => Duplicate_Subexpr (N))), | |
6193 | ||
6194 | Make_Raise_Constraint_Error (Loc, | |
6195 | Condition => | |
6196 | Make_Or_Else (Loc, | |
6197 | Left_Opnd => | |
6198 | Make_Op_Lt (Loc, | |
6199 | Left_Opnd => Duplicate_Subexpr (N), | |
6200 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
6201 | ||
6202 | Right_Opnd => | |
6203 | Make_Not_In (Loc, | |
6204 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
6205 | Right_Opnd => | |
6206 | New_Occurrence_Of (Target_Type, Loc))), | |
6207 | ||
20cf157b | 6208 | Reason => Reason)), |
9dfe12ae | 6209 | Suppress => All_Checks); |
6210 | ||
feff2f05 | 6211 | -- Set the Etype explicitly, because Insert_Actions may have |
6212 | -- placed the declaration in the freeze list for an enclosing | |
6213 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 6214 | |
6215 | Set_Etype (Tnn, Target_Base_Type); | |
6216 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
6217 | end; | |
6218 | end if; | |
6219 | end if; | |
6220 | end Generate_Range_Check; | |
6221 | ||
2af58f67 | 6222 | ------------------ |
6223 | -- Get_Check_Id -- | |
6224 | ------------------ | |
6225 | ||
6226 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
6227 | begin | |
6228 | -- For standard check name, we can do a direct computation | |
6229 | ||
6230 | if N in First_Check_Name .. Last_Check_Name then | |
6231 | return Check_Id (N - (First_Check_Name - 1)); | |
6232 | ||
6233 | -- For non-standard names added by pragma Check_Name, search table | |
6234 | ||
6235 | else | |
6236 | for J in All_Checks + 1 .. Check_Names.Last loop | |
6237 | if Check_Names.Table (J) = N then | |
6238 | return J; | |
6239 | end if; | |
6240 | end loop; | |
6241 | end if; | |
6242 | ||
6243 | -- No matching name found | |
6244 | ||
6245 | return No_Check_Id; | |
6246 | end Get_Check_Id; | |
6247 | ||
ee6ba406 | 6248 | --------------------- |
6249 | -- Get_Discriminal -- | |
6250 | --------------------- | |
6251 | ||
6252 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
6253 | Loc : constant Source_Ptr := Sloc (E); | |
6254 | D : Entity_Id; | |
6255 | Sc : Entity_Id; | |
6256 | ||
6257 | begin | |
0577b0b1 | 6258 | -- The bound can be a bona fide parameter of a protected operation, |
6259 | -- rather than a prival encoded as an in-parameter. | |
6260 | ||
6261 | if No (Discriminal_Link (Entity (Bound))) then | |
6262 | return Bound; | |
6263 | end if; | |
6264 | ||
2af58f67 | 6265 | -- Climb the scope stack looking for an enclosing protected type. If |
6266 | -- we run out of scopes, return the bound itself. | |
6267 | ||
6268 | Sc := Scope (E); | |
6269 | while Present (Sc) loop | |
6270 | if Sc = Standard_Standard then | |
6271 | return Bound; | |
2af58f67 | 6272 | elsif Ekind (Sc) = E_Protected_Type then |
6273 | exit; | |
6274 | end if; | |
6275 | ||
6276 | Sc := Scope (Sc); | |
6277 | end loop; | |
6278 | ||
ee6ba406 | 6279 | D := First_Discriminant (Sc); |
2af58f67 | 6280 | while Present (D) loop |
6281 | if Chars (D) = Chars (Bound) then | |
6282 | return New_Occurrence_Of (Discriminal (D), Loc); | |
6283 | end if; | |
ee6ba406 | 6284 | |
ee6ba406 | 6285 | Next_Discriminant (D); |
6286 | end loop; | |
6287 | ||
2af58f67 | 6288 | return Bound; |
ee6ba406 | 6289 | end Get_Discriminal; |
6290 | ||
2af58f67 | 6291 | ---------------------- |
6292 | -- Get_Range_Checks -- | |
6293 | ---------------------- | |
6294 | ||
6295 | function Get_Range_Checks | |
6296 | (Ck_Node : Node_Id; | |
6297 | Target_Typ : Entity_Id; | |
6298 | Source_Typ : Entity_Id := Empty; | |
6299 | Warn_Node : Node_Id := Empty) return Check_Result | |
6300 | is | |
6301 | begin | |
20cf157b | 6302 | return |
6303 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
2af58f67 | 6304 | end Get_Range_Checks; |
6305 | ||
ee6ba406 | 6306 | ------------------ |
6307 | -- Guard_Access -- | |
6308 | ------------------ | |
6309 | ||
6310 | function Guard_Access | |
6311 | (Cond : Node_Id; | |
6312 | Loc : Source_Ptr; | |
314a23b6 | 6313 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 6314 | is |
6315 | begin | |
6316 | if Nkind (Cond) = N_Or_Else then | |
6317 | Set_Paren_Count (Cond, 1); | |
6318 | end if; | |
6319 | ||
6320 | if Nkind (Ck_Node) = N_Allocator then | |
6321 | return Cond; | |
20cf157b | 6322 | |
ee6ba406 | 6323 | else |
6324 | return | |
6325 | Make_And_Then (Loc, | |
6326 | Left_Opnd => | |
6327 | Make_Op_Ne (Loc, | |
9dfe12ae | 6328 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 6329 | Right_Opnd => Make_Null (Loc)), |
6330 | Right_Opnd => Cond); | |
6331 | end if; | |
6332 | end Guard_Access; | |
6333 | ||
6334 | ----------------------------- | |
6335 | -- Index_Checks_Suppressed -- | |
6336 | ----------------------------- | |
6337 | ||
6338 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
6339 | begin | |
9dfe12ae | 6340 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
6341 | return Is_Check_Suppressed (E, Index_Check); | |
6342 | else | |
fafc6b97 | 6343 | return Scope_Suppress.Suppress (Index_Check); |
9dfe12ae | 6344 | end if; |
ee6ba406 | 6345 | end Index_Checks_Suppressed; |
6346 | ||
6347 | ---------------- | |
6348 | -- Initialize -- | |
6349 | ---------------- | |
6350 | ||
6351 | procedure Initialize is | |
6352 | begin | |
6353 | for J in Determine_Range_Cache_N'Range loop | |
6354 | Determine_Range_Cache_N (J) := Empty; | |
6355 | end loop; | |
2af58f67 | 6356 | |
6357 | Check_Names.Init; | |
6358 | ||
6359 | for J in Int range 1 .. All_Checks loop | |
6360 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
6361 | end loop; | |
ee6ba406 | 6362 | end Initialize; |
6363 | ||
6364 | ------------------------- | |
6365 | -- Insert_Range_Checks -- | |
6366 | ------------------------- | |
6367 | ||
6368 | procedure Insert_Range_Checks | |
6369 | (Checks : Check_Result; | |
6370 | Node : Node_Id; | |
6371 | Suppress_Typ : Entity_Id; | |
6372 | Static_Sloc : Source_Ptr := No_Location; | |
6373 | Flag_Node : Node_Id := Empty; | |
6374 | Do_Before : Boolean := False) | |
6375 | is | |
6376 | Internal_Flag_Node : Node_Id := Flag_Node; | |
6377 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
6378 | ||
6379 | Check_Node : Node_Id; | |
6380 | Checks_On : constant Boolean := | |
b6341c67 | 6381 | (not Index_Checks_Suppressed (Suppress_Typ)) |
6382 | or else (not Range_Checks_Suppressed (Suppress_Typ)); | |
ee6ba406 | 6383 | |
6384 | begin | |
feff2f05 | 6385 | -- For now we just return if Checks_On is false, however this should be |
6386 | -- enhanced to check for an always True value in the condition and to | |
6387 | -- generate a compilation warning??? | |
ee6ba406 | 6388 | |
ac9184ed | 6389 | if not Expander_Active or not Checks_On then |
ee6ba406 | 6390 | return; |
6391 | end if; | |
6392 | ||
6393 | if Static_Sloc = No_Location then | |
6394 | Internal_Static_Sloc := Sloc (Node); | |
6395 | end if; | |
6396 | ||
6397 | if No (Flag_Node) then | |
6398 | Internal_Flag_Node := Node; | |
6399 | end if; | |
6400 | ||
6401 | for J in 1 .. 2 loop | |
6402 | exit when No (Checks (J)); | |
6403 | ||
6404 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
6405 | and then Present (Condition (Checks (J))) | |
6406 | then | |
6407 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
6408 | Check_Node := Checks (J); | |
6409 | Mark_Rewrite_Insertion (Check_Node); | |
6410 | ||
6411 | if Do_Before then | |
6412 | Insert_Before_And_Analyze (Node, Check_Node); | |
6413 | else | |
6414 | Insert_After_And_Analyze (Node, Check_Node); | |
6415 | end if; | |
6416 | ||
6417 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
6418 | end if; | |
6419 | ||
6420 | else | |
6421 | Check_Node := | |
f15731c4 | 6422 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
6423 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 6424 | Mark_Rewrite_Insertion (Check_Node); |
6425 | ||
6426 | if Do_Before then | |
6427 | Insert_Before_And_Analyze (Node, Check_Node); | |
6428 | else | |
6429 | Insert_After_And_Analyze (Node, Check_Node); | |
6430 | end if; | |
6431 | end if; | |
6432 | end loop; | |
6433 | end Insert_Range_Checks; | |
6434 | ||
6435 | ------------------------ | |
6436 | -- Insert_Valid_Check -- | |
6437 | ------------------------ | |
6438 | ||
6439 | procedure Insert_Valid_Check (Expr : Node_Id) is | |
6440 | Loc : constant Source_Ptr := Sloc (Expr); | |
70580828 | 6441 | Typ : constant Entity_Id := Etype (Expr); |
8b718dab | 6442 | Exp : Node_Id; |
ee6ba406 | 6443 | |
6444 | begin | |
06ad5813 | 6445 | -- Do not insert if checks off, or if not checking validity or |
6446 | -- if expression is known to be valid | |
ee6ba406 | 6447 | |
0577b0b1 | 6448 | if not Validity_Checks_On |
6449 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
06ad5813 | 6450 | or else Expr_Known_Valid (Expr) |
ee6ba406 | 6451 | then |
8b718dab | 6452 | return; |
6453 | end if; | |
ee6ba406 | 6454 | |
42c57d55 | 6455 | -- Do not insert checks within a predicate function. This will arise |
6456 | -- if the current unit and the predicate function are being compiled | |
6457 | -- with validity checks enabled. | |
70580828 | 6458 | |
6459 | if Present (Predicate_Function (Typ)) | |
6460 | and then Current_Scope = Predicate_Function (Typ) | |
6461 | then | |
6462 | return; | |
6463 | end if; | |
6464 | ||
310c1cde | 6465 | -- If the expression is a packed component of a modular type of the |
6466 | -- right size, the data is always valid. | |
6467 | ||
6468 | if Nkind (Expr) = N_Selected_Component | |
6469 | and then Present (Component_Clause (Entity (Selector_Name (Expr)))) | |
6470 | and then Is_Modular_Integer_Type (Typ) | |
6471 | and then Modulus (Typ) = 2 ** Esize (Entity (Selector_Name (Expr))) | |
6472 | then | |
6473 | return; | |
6474 | end if; | |
6475 | ||
8b718dab | 6476 | -- If we have a checked conversion, then validity check applies to |
6477 | -- the expression inside the conversion, not the result, since if | |
6478 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 6479 | |
8b718dab | 6480 | Exp := Expr; |
6481 | while Nkind (Exp) = N_Type_Conversion loop | |
6482 | Exp := Expression (Exp); | |
6483 | end loop; | |
6484 | ||
0577b0b1 | 6485 | -- We are about to insert the validity check for Exp. We save and |
6486 | -- reset the Do_Range_Check flag over this validity check, and then | |
6487 | -- put it back for the final original reference (Exp may be rewritten). | |
6488 | ||
6489 | declare | |
6490 | DRC : constant Boolean := Do_Range_Check (Exp); | |
23abd64d | 6491 | PV : Node_Id; |
6492 | CE : Node_Id; | |
05fcfafb | 6493 | |
0577b0b1 | 6494 | begin |
6495 | Set_Do_Range_Check (Exp, False); | |
6496 | ||
06ad5813 | 6497 | -- Force evaluation to avoid multiple reads for atomic/volatile |
6498 | ||
fa771c05 | 6499 | -- Note: we set Name_Req to False. We used to set it to True, with |
6500 | -- the thinking that a name is required as the prefix of the 'Valid | |
6501 | -- call, but in fact the check that the prefix of an attribute is | |
6502 | -- a name is in the parser, and we just don't require it here. | |
6503 | -- Moreover, when we set Name_Req to True, that interfered with the | |
6504 | -- checking for Volatile, since we couldn't just capture the value. | |
6505 | ||
06ad5813 | 6506 | if Is_Entity_Name (Exp) |
6507 | and then Is_Volatile (Entity (Exp)) | |
6508 | then | |
fa771c05 | 6509 | -- Same reasoning as above for setting Name_Req to False |
6510 | ||
6511 | Force_Evaluation (Exp, Name_Req => False); | |
06ad5813 | 6512 | end if; |
6513 | ||
23abd64d | 6514 | -- Build the prefix for the 'Valid call |
6515 | ||
fa771c05 | 6516 | PV := Duplicate_Subexpr_No_Checks (Exp, Name_Req => False); |
0577b0b1 | 6517 | |
23abd64d | 6518 | -- A rather specialized kludge. If PV is an analyzed expression |
6519 | -- which is an indexed component of a packed array that has not | |
6520 | -- been properly expanded, turn off its Analyzed flag to make sure | |
6521 | -- it gets properly reexpanded. | |
6522 | ||
6523 | -- The reason this arises is that Duplicate_Subexpr_No_Checks did | |
6524 | -- an analyze with the old parent pointer. This may point e.g. to | |
6525 | -- a subprogram call, which deactivates this expansion. | |
6526 | ||
6527 | if Analyzed (PV) | |
6528 | and then Nkind (PV) = N_Indexed_Component | |
6529 | and then Present (Packed_Array_Type (Etype (Prefix (PV)))) | |
6530 | then | |
6531 | Set_Analyzed (PV, False); | |
6532 | end if; | |
6533 | ||
fa771c05 | 6534 | -- Build the raise CE node to check for validity. We build a type |
6535 | -- qualification for the prefix, since it may not be of the form of | |
6536 | -- a name, and we don't care in this context! | |
23abd64d | 6537 | |
6538 | CE := | |
0577b0b1 | 6539 | Make_Raise_Constraint_Error (Loc, |
6540 | Condition => | |
6541 | Make_Op_Not (Loc, | |
6542 | Right_Opnd => | |
6543 | Make_Attribute_Reference (Loc, | |
23abd64d | 6544 | Prefix => PV, |
0577b0b1 | 6545 | Attribute_Name => Name_Valid)), |
23abd64d | 6546 | Reason => CE_Invalid_Data); |
6547 | ||
6548 | -- Insert the validity check. Note that we do this with validity | |
6549 | -- checks turned off, to avoid recursion, we do not want validity | |
39a0c1d3 | 6550 | -- checks on the validity checking code itself. |
23abd64d | 6551 | |
6552 | Insert_Action (Expr, CE, Suppress => Validity_Check); | |
0577b0b1 | 6553 | |
6fb3c314 | 6554 | -- If the expression is a reference to an element of a bit-packed |
0577b0b1 | 6555 | -- array, then it is rewritten as a renaming declaration. If the |
6556 | -- expression is an actual in a call, it has not been expanded, | |
6557 | -- waiting for the proper point at which to do it. The same happens | |
6558 | -- with renamings, so that we have to force the expansion now. This | |
6559 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
6560 | -- and exp_ch6.adb. | |
6561 | ||
6562 | if Is_Entity_Name (Exp) | |
6563 | and then Nkind (Parent (Entity (Exp))) = | |
20cf157b | 6564 | N_Object_Renaming_Declaration |
0577b0b1 | 6565 | then |
6566 | declare | |
6567 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
6568 | begin | |
6569 | if Nkind (Old_Exp) = N_Indexed_Component | |
6570 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
6571 | then | |
6572 | Expand_Packed_Element_Reference (Old_Exp); | |
6573 | end if; | |
6574 | end; | |
6575 | end if; | |
6576 | ||
6577 | -- Put back the Do_Range_Check flag on the resulting (possibly | |
6578 | -- rewritten) expression. | |
6579 | ||
6580 | -- Note: it might be thought that a validity check is not required | |
6581 | -- when a range check is present, but that's not the case, because | |
6582 | -- the back end is allowed to assume for the range check that the | |
6583 | -- operand is within its declared range (an assumption that validity | |
39a0c1d3 | 6584 | -- checking is all about NOT assuming). |
0577b0b1 | 6585 | |
00c403ee | 6586 | -- Note: no need to worry about Possible_Local_Raise here, it will |
6587 | -- already have been called if original node has Do_Range_Check set. | |
6588 | ||
0577b0b1 | 6589 | Set_Do_Range_Check (Exp, DRC); |
6590 | end; | |
ee6ba406 | 6591 | end Insert_Valid_Check; |
6592 | ||
3cce7f32 | 6593 | ------------------------------------- |
6594 | -- Is_Signed_Integer_Arithmetic_Op -- | |
6595 | ------------------------------------- | |
6596 | ||
6597 | function Is_Signed_Integer_Arithmetic_Op (N : Node_Id) return Boolean is | |
6598 | begin | |
6599 | case Nkind (N) is | |
6600 | when N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon | | |
6601 | N_Op_Minus | N_Op_Mod | N_Op_Multiply | N_Op_Plus | | |
6602 | N_Op_Rem | N_Op_Subtract => | |
6603 | return Is_Signed_Integer_Type (Etype (N)); | |
6604 | ||
92f1631f | 6605 | when N_If_Expression | N_Case_Expression => |
0326b4d4 | 6606 | return Is_Signed_Integer_Type (Etype (N)); |
6607 | ||
3cce7f32 | 6608 | when others => |
6609 | return False; | |
6610 | end case; | |
6611 | end Is_Signed_Integer_Arithmetic_Op; | |
6612 | ||
fa7497e8 | 6613 | ---------------------------------- |
6614 | -- Install_Null_Excluding_Check -- | |
6615 | ---------------------------------- | |
6616 | ||
6617 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
9f294c82 | 6618 | Loc : constant Source_Ptr := Sloc (Parent (N)); |
84d0d4a5 | 6619 | Typ : constant Entity_Id := Etype (N); |
6620 | ||
7b31b357 | 6621 | function Safe_To_Capture_In_Parameter_Value return Boolean; |
6622 | -- Determines if it is safe to capture Known_Non_Null status for an | |
6623 | -- the entity referenced by node N. The caller ensures that N is indeed | |
6624 | -- an entity name. It is safe to capture the non-null status for an IN | |
6625 | -- parameter when the reference occurs within a declaration that is sure | |
6626 | -- to be executed as part of the declarative region. | |
7870823d | 6627 | |
84d0d4a5 | 6628 | procedure Mark_Non_Null; |
7870823d | 6629 | -- After installation of check, if the node in question is an entity |
6630 | -- name, then mark this entity as non-null if possible. | |
6631 | ||
7b31b357 | 6632 | function Safe_To_Capture_In_Parameter_Value return Boolean is |
7870823d | 6633 | E : constant Entity_Id := Entity (N); |
6634 | S : constant Entity_Id := Current_Scope; | |
6635 | S_Par : Node_Id; | |
6636 | ||
6637 | begin | |
7b31b357 | 6638 | if Ekind (E) /= E_In_Parameter then |
6639 | return False; | |
6640 | end if; | |
7870823d | 6641 | |
6642 | -- Two initial context checks. We must be inside a subprogram body | |
6643 | -- with declarations and reference must not appear in nested scopes. | |
6644 | ||
7b31b357 | 6645 | if (Ekind (S) /= E_Function and then Ekind (S) /= E_Procedure) |
7870823d | 6646 | or else Scope (E) /= S |
6647 | then | |
6648 | return False; | |
6649 | end if; | |
6650 | ||
6651 | S_Par := Parent (Parent (S)); | |
6652 | ||
6653 | if Nkind (S_Par) /= N_Subprogram_Body | |
6654 | or else No (Declarations (S_Par)) | |
6655 | then | |
6656 | return False; | |
6657 | end if; | |
6658 | ||
6659 | declare | |
6660 | N_Decl : Node_Id; | |
6661 | P : Node_Id; | |
6662 | ||
6663 | begin | |
6664 | -- Retrieve the declaration node of N (if any). Note that N | |
6665 | -- may be a part of a complex initialization expression. | |
6666 | ||
6667 | P := Parent (N); | |
6668 | N_Decl := Empty; | |
6669 | while Present (P) loop | |
6670 | ||
7b31b357 | 6671 | -- If we have a short circuit form, and we are within the right |
6672 | -- hand expression, we return false, since the right hand side | |
6673 | -- is not guaranteed to be elaborated. | |
6674 | ||
6675 | if Nkind (P) in N_Short_Circuit | |
6676 | and then N = Right_Opnd (P) | |
6677 | then | |
6678 | return False; | |
6679 | end if; | |
6680 | ||
92f1631f | 6681 | -- Similarly, if we are in an if expression and not part of the |
6682 | -- condition, then we return False, since neither the THEN or | |
6683 | -- ELSE dependent expressions will always be elaborated. | |
7b31b357 | 6684 | |
92f1631f | 6685 | if Nkind (P) = N_If_Expression |
7b31b357 | 6686 | and then N /= First (Expressions (P)) |
6687 | then | |
6688 | return False; | |
e977c0cf | 6689 | end if; |
6690 | ||
20cf157b | 6691 | -- If within a case expression, and not part of the expression, |
6692 | -- then return False, since a particular dependent expression | |
6693 | -- may not always be elaborated | |
e977c0cf | 6694 | |
6695 | if Nkind (P) = N_Case_Expression | |
6696 | and then N /= Expression (P) | |
6697 | then | |
6698 | return False; | |
7b31b357 | 6699 | end if; |
6700 | ||
20cf157b | 6701 | -- While traversing the parent chain, if node N belongs to a |
6702 | -- statement, then it may never appear in a declarative region. | |
7870823d | 6703 | |
6704 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
6705 | or else Nkind (P) = N_Procedure_Call_Statement | |
6706 | then | |
6707 | return False; | |
6708 | end if; | |
6709 | ||
7b31b357 | 6710 | -- If we are at a declaration, record it and exit |
6711 | ||
7870823d | 6712 | if Nkind (P) in N_Declaration |
6713 | and then Nkind (P) not in N_Subprogram_Specification | |
6714 | then | |
6715 | N_Decl := P; | |
6716 | exit; | |
6717 | end if; | |
6718 | ||
6719 | P := Parent (P); | |
6720 | end loop; | |
6721 | ||
6722 | if No (N_Decl) then | |
6723 | return False; | |
6724 | end if; | |
6725 | ||
6726 | return List_Containing (N_Decl) = Declarations (S_Par); | |
6727 | end; | |
7b31b357 | 6728 | end Safe_To_Capture_In_Parameter_Value; |
84d0d4a5 | 6729 | |
6730 | ------------------- | |
6731 | -- Mark_Non_Null -- | |
6732 | ------------------- | |
6733 | ||
6734 | procedure Mark_Non_Null is | |
6735 | begin | |
7870823d | 6736 | -- Only case of interest is if node N is an entity name |
6737 | ||
84d0d4a5 | 6738 | if Is_Entity_Name (N) then |
7870823d | 6739 | |
6740 | -- For sure, we want to clear an indication that this is known to | |
39a0c1d3 | 6741 | -- be null, since if we get past this check, it definitely is not. |
7870823d | 6742 | |
84d0d4a5 | 6743 | Set_Is_Known_Null (Entity (N), False); |
6744 | ||
7870823d | 6745 | -- We can mark the entity as known to be non-null if either it is |
6746 | -- safe to capture the value, or in the case of an IN parameter, | |
6747 | -- which is a constant, if the check we just installed is in the | |
6748 | -- declarative region of the subprogram body. In this latter case, | |
7b31b357 | 6749 | -- a check is decisive for the rest of the body if the expression |
6750 | -- is sure to be elaborated, since we know we have to elaborate | |
6751 | -- all declarations before executing the body. | |
6752 | ||
6753 | -- Couldn't this always be part of Safe_To_Capture_Value ??? | |
7870823d | 6754 | |
6755 | if Safe_To_Capture_Value (N, Entity (N)) | |
7b31b357 | 6756 | or else Safe_To_Capture_In_Parameter_Value |
7870823d | 6757 | then |
6758 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 6759 | end if; |
6760 | end if; | |
6761 | end Mark_Non_Null; | |
6762 | ||
6763 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 6764 | |
6765 | begin | |
84d0d4a5 | 6766 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 6767 | |
46e32b5e | 6768 | -- No check inside a generic, check will be emitted in instance |
fa7497e8 | 6769 | |
84d0d4a5 | 6770 | if Inside_A_Generic then |
fa7497e8 | 6771 | return; |
84d0d4a5 | 6772 | end if; |
6773 | ||
6774 | -- No check needed if known to be non-null | |
6775 | ||
6776 | if Known_Non_Null (N) then | |
05fcfafb | 6777 | return; |
84d0d4a5 | 6778 | end if; |
fa7497e8 | 6779 | |
84d0d4a5 | 6780 | -- If known to be null, here is where we generate a compile time check |
6781 | ||
6782 | if Known_Null (N) then | |
d16989f1 | 6783 | |
20cf157b | 6784 | -- Avoid generating warning message inside init procs. In SPARK mode |
6785 | -- we can go ahead and call Apply_Compile_Time_Constraint_Error | |
46e32b5e | 6786 | -- since it will be turned into an error in any case. |
d16989f1 | 6787 | |
46e32b5e | 6788 | if (not Inside_Init_Proc or else SPARK_Mode = On) |
6789 | ||
28d5d68f | 6790 | -- Do not emit the warning within a conditional expression, |
6791 | -- where the expression might not be evaluated, and the warning | |
6792 | -- appear as extraneous noise. | |
46e32b5e | 6793 | |
6794 | and then not Within_Case_Or_If_Expression (N) | |
6795 | then | |
d16989f1 | 6796 | Apply_Compile_Time_Constraint_Error |
4098232e | 6797 | (N, "null value not allowed here??", CE_Access_Check_Failed); |
46e32b5e | 6798 | |
6799 | -- Remaining cases, where we silently insert the raise | |
6800 | ||
d16989f1 | 6801 | else |
6802 | Insert_Action (N, | |
6803 | Make_Raise_Constraint_Error (Loc, | |
6804 | Reason => CE_Access_Check_Failed)); | |
6805 | end if; | |
6806 | ||
84d0d4a5 | 6807 | Mark_Non_Null; |
6808 | return; | |
6809 | end if; | |
6810 | ||
6811 | -- If entity is never assigned, for sure a warning is appropriate | |
6812 | ||
6813 | if Is_Entity_Name (N) then | |
6814 | Check_Unset_Reference (N); | |
fa7497e8 | 6815 | end if; |
84d0d4a5 | 6816 | |
6817 | -- No check needed if checks are suppressed on the range. Note that we | |
6818 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
6819 | -- so, since the program is erroneous, but we don't like to casually | |
6820 | -- propagate such conclusions from erroneosity). | |
6821 | ||
6822 | if Access_Checks_Suppressed (Typ) then | |
6823 | return; | |
6824 | end if; | |
6825 | ||
2af58f67 | 6826 | -- No check needed for access to concurrent record types generated by |
6827 | -- the expander. This is not just an optimization (though it does indeed | |
6828 | -- remove junk checks). It also avoids generation of junk warnings. | |
6829 | ||
6830 | if Nkind (N) in N_Has_Chars | |
6831 | and then Chars (N) = Name_uObject | |
6832 | and then Is_Concurrent_Record_Type | |
6833 | (Directly_Designated_Type (Etype (N))) | |
6834 | then | |
6835 | return; | |
6836 | end if; | |
6837 | ||
228836e8 | 6838 | -- No check needed in interface thunks since the runtime check is |
6839 | -- already performed at the caller side. | |
6840 | ||
6841 | if Is_Thunk (Current_Scope) then | |
6842 | return; | |
6843 | end if; | |
6844 | ||
472ea160 | 6845 | -- No check needed for the Get_Current_Excep.all.all idiom generated by |
6846 | -- the expander within exception handlers, since we know that the value | |
6847 | -- can never be null. | |
6848 | ||
6849 | -- Is this really the right way to do this? Normally we generate such | |
6850 | -- code in the expander with checks off, and that's how we suppress this | |
6851 | -- kind of junk check ??? | |
6852 | ||
6853 | if Nkind (N) = N_Function_Call | |
6854 | and then Nkind (Name (N)) = N_Explicit_Dereference | |
6855 | and then Nkind (Prefix (Name (N))) = N_Identifier | |
6856 | and then Is_RTE (Entity (Prefix (Name (N))), RE_Get_Current_Excep) | |
6857 | then | |
6858 | return; | |
6859 | end if; | |
6860 | ||
84d0d4a5 | 6861 | -- Otherwise install access check |
6862 | ||
6863 | Insert_Action (N, | |
6864 | Make_Raise_Constraint_Error (Loc, | |
6865 | Condition => | |
6866 | Make_Op_Eq (Loc, | |
6867 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
6868 | Right_Opnd => Make_Null (Loc)), | |
6869 | Reason => CE_Access_Check_Failed)); | |
6870 | ||
6871 | Mark_Non_Null; | |
fa7497e8 | 6872 | end Install_Null_Excluding_Check; |
6873 | ||
ee6ba406 | 6874 | -------------------------- |
6875 | -- Install_Static_Check -- | |
6876 | -------------------------- | |
6877 | ||
6878 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
6879 | Stat : constant Boolean := Is_Static_Expression (R_Cno); | |
6880 | Typ : constant Entity_Id := Etype (R_Cno); | |
6881 | ||
6882 | begin | |
f15731c4 | 6883 | Rewrite (R_Cno, |
6884 | Make_Raise_Constraint_Error (Loc, | |
6885 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 6886 | Set_Analyzed (R_Cno); |
6887 | Set_Etype (R_Cno, Typ); | |
6888 | Set_Raises_Constraint_Error (R_Cno); | |
6889 | Set_Is_Static_Expression (R_Cno, Stat); | |
840ab274 | 6890 | |
6891 | -- Now deal with possible local raise handling | |
6892 | ||
6893 | Possible_Local_Raise (R_Cno, Standard_Constraint_Error); | |
ee6ba406 | 6894 | end Install_Static_Check; |
6895 | ||
3cce7f32 | 6896 | ------------------------- |
6897 | -- Is_Check_Suppressed -- | |
6898 | ------------------------- | |
6899 | ||
6900 | function Is_Check_Suppressed (E : Entity_Id; C : Check_Id) return Boolean is | |
6901 | Ptr : Suppress_Stack_Entry_Ptr; | |
6902 | ||
6903 | begin | |
6904 | -- First search the local entity suppress stack. We search this from the | |
6905 | -- top of the stack down so that we get the innermost entry that applies | |
6906 | -- to this case if there are nested entries. | |
6907 | ||
6908 | Ptr := Local_Suppress_Stack_Top; | |
6909 | while Ptr /= null loop | |
6910 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6911 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6912 | then | |
6913 | return Ptr.Suppress; | |
6914 | end if; | |
6915 | ||
6916 | Ptr := Ptr.Prev; | |
6917 | end loop; | |
6918 | ||
6919 | -- Now search the global entity suppress table for a matching entry. | |
6920 | -- We also search this from the top down so that if there are multiple | |
6921 | -- pragmas for the same entity, the last one applies (not clear what | |
6922 | -- or whether the RM specifies this handling, but it seems reasonable). | |
6923 | ||
6924 | Ptr := Global_Suppress_Stack_Top; | |
6925 | while Ptr /= null loop | |
6926 | if (Ptr.Entity = Empty or else Ptr.Entity = E) | |
6927 | and then (Ptr.Check = All_Checks or else Ptr.Check = C) | |
6928 | then | |
6929 | return Ptr.Suppress; | |
6930 | end if; | |
6931 | ||
6932 | Ptr := Ptr.Prev; | |
6933 | end loop; | |
6934 | ||
6935 | -- If we did not find a matching entry, then use the normal scope | |
6936 | -- suppress value after all (actually this will be the global setting | |
6937 | -- since it clearly was not overridden at any point). For a predefined | |
6938 | -- check, we test the specific flag. For a user defined check, we check | |
6939 | -- the All_Checks flag. The Overflow flag requires special handling to | |
6940 | -- deal with the General vs Assertion case | |
6941 | ||
6942 | if C = Overflow_Check then | |
6943 | return Overflow_Checks_Suppressed (Empty); | |
6944 | elsif C in Predefined_Check_Id then | |
6945 | return Scope_Suppress.Suppress (C); | |
6946 | else | |
6947 | return Scope_Suppress.Suppress (All_Checks); | |
6948 | end if; | |
6949 | end Is_Check_Suppressed; | |
6950 | ||
9dfe12ae | 6951 | --------------------- |
6952 | -- Kill_All_Checks -- | |
6953 | --------------------- | |
6954 | ||
6955 | procedure Kill_All_Checks is | |
6956 | begin | |
6957 | if Debug_Flag_CC then | |
6958 | w ("Kill_All_Checks"); | |
6959 | end if; | |
6960 | ||
feff2f05 | 6961 | -- We reset the number of saved checks to zero, and also modify all |
6962 | -- stack entries for statement ranges to indicate that the number of | |
6963 | -- checks at each level is now zero. | |
9dfe12ae | 6964 | |
6965 | Num_Saved_Checks := 0; | |
6966 | ||
96da3284 | 6967 | -- Note: the Int'Min here avoids any possibility of J being out of |
6968 | -- range when called from e.g. Conditional_Statements_Begin. | |
6969 | ||
6970 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 6971 | Saved_Checks_Stack (J) := 0; |
6972 | end loop; | |
6973 | end Kill_All_Checks; | |
6974 | ||
6975 | ----------------- | |
6976 | -- Kill_Checks -- | |
6977 | ----------------- | |
6978 | ||
6979 | procedure Kill_Checks (V : Entity_Id) is | |
6980 | begin | |
6981 | if Debug_Flag_CC then | |
6982 | w ("Kill_Checks for entity", Int (V)); | |
6983 | end if; | |
6984 | ||
6985 | for J in 1 .. Num_Saved_Checks loop | |
6986 | if Saved_Checks (J).Entity = V then | |
6987 | if Debug_Flag_CC then | |
6988 | w (" Checks killed for saved check ", J); | |
6989 | end if; | |
6990 | ||
6991 | Saved_Checks (J).Killed := True; | |
6992 | end if; | |
6993 | end loop; | |
6994 | end Kill_Checks; | |
6995 | ||
ee6ba406 | 6996 | ------------------------------ |
6997 | -- Length_Checks_Suppressed -- | |
6998 | ------------------------------ | |
6999 | ||
7000 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7001 | begin | |
9dfe12ae | 7002 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7003 | return Is_Check_Suppressed (E, Length_Check); | |
7004 | else | |
fafc6b97 | 7005 | return Scope_Suppress.Suppress (Length_Check); |
9dfe12ae | 7006 | end if; |
ee6ba406 | 7007 | end Length_Checks_Suppressed; |
7008 | ||
3cce7f32 | 7009 | ----------------------- |
7010 | -- Make_Bignum_Block -- | |
7011 | ----------------------- | |
7012 | ||
7013 | function Make_Bignum_Block (Loc : Source_Ptr) return Node_Id is | |
7014 | M : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uM); | |
ee6ba406 | 7015 | |
3cce7f32 | 7016 | begin |
7017 | return | |
7018 | Make_Block_Statement (Loc, | |
7019 | Declarations => New_List ( | |
7020 | Make_Object_Declaration (Loc, | |
7021 | Defining_Identifier => M, | |
7022 | Object_Definition => | |
7023 | New_Occurrence_Of (RTE (RE_Mark_Id), Loc), | |
7024 | Expression => | |
7025 | Make_Function_Call (Loc, | |
83c6c069 | 7026 | Name => New_Occurrence_Of (RTE (RE_SS_Mark), Loc)))), |
3cce7f32 | 7027 | |
7028 | Handled_Statement_Sequence => | |
7029 | Make_Handled_Sequence_Of_Statements (Loc, | |
7030 | Statements => New_List ( | |
7031 | Make_Procedure_Call_Statement (Loc, | |
7032 | Name => New_Occurrence_Of (RTE (RE_SS_Release), Loc), | |
7033 | Parameter_Associations => New_List ( | |
83c6c069 | 7034 | New_Occurrence_Of (M, Loc)))))); |
3cce7f32 | 7035 | end Make_Bignum_Block; |
7036 | ||
0df9d43f | 7037 | ---------------------------------- |
7038 | -- Minimize_Eliminate_Overflows -- | |
7039 | ---------------------------------- | |
3cce7f32 | 7040 | |
f32c377d | 7041 | -- This is a recursive routine that is called at the top of an expression |
7042 | -- tree to properly process overflow checking for a whole subtree by making | |
7043 | -- recursive calls to process operands. This processing may involve the use | |
7044 | -- of bignum or long long integer arithmetic, which will change the types | |
7045 | -- of operands and results. That's why we can't do this bottom up (since | |
21a55437 | 7046 | -- it would interfere with semantic analysis). |
f32c377d | 7047 | |
21a55437 | 7048 | -- What happens is that if MINIMIZED/ELIMINATED mode is in effect then |
0df9d43f | 7049 | -- the operator expansion routines, as well as the expansion routines for |
7050 | -- if/case expression, do nothing (for the moment) except call the routine | |
7051 | -- to apply the overflow check (Apply_Arithmetic_Overflow_Check). That | |
7052 | -- routine does nothing for non top-level nodes, so at the point where the | |
7053 | -- call is made for the top level node, the entire expression subtree has | |
7054 | -- not been expanded, or processed for overflow. All that has to happen as | |
7055 | -- a result of the top level call to this routine. | |
f32c377d | 7056 | |
7057 | -- As noted above, the overflow processing works by making recursive calls | |
7058 | -- for the operands, and figuring out what to do, based on the processing | |
7059 | -- of these operands (e.g. if a bignum operand appears, the parent op has | |
7060 | -- to be done in bignum mode), and the determined ranges of the operands. | |
7061 | ||
7062 | -- After possible rewriting of a constituent subexpression node, a call is | |
4fb5f0a0 | 7063 | -- made to either reexpand the node (if nothing has changed) or reanalyze |
21a55437 | 7064 | -- the node (if it has been modified by the overflow check processing). The |
7065 | -- Analyzed_Flag is set to False before the reexpand/reanalyze. To avoid | |
7066 | -- a recursive call into the whole overflow apparatus, an important rule | |
0df9d43f | 7067 | -- for this call is that the overflow handling mode must be temporarily set |
7068 | -- to STRICT. | |
f32c377d | 7069 | |
0df9d43f | 7070 | procedure Minimize_Eliminate_Overflows |
61016a7a | 7071 | (N : Node_Id; |
7072 | Lo : out Uint; | |
7073 | Hi : out Uint; | |
7074 | Top_Level : Boolean) | |
3cce7f32 | 7075 | is |
0326b4d4 | 7076 | Rtyp : constant Entity_Id := Etype (N); |
7077 | pragma Assert (Is_Signed_Integer_Type (Rtyp)); | |
7078 | -- Result type, must be a signed integer type | |
3cce7f32 | 7079 | |
db415383 | 7080 | Check_Mode : constant Overflow_Mode_Type := Overflow_Check_Mode; |
3cce7f32 | 7081 | pragma Assert (Check_Mode in Minimized_Or_Eliminated); |
7082 | ||
7083 | Loc : constant Source_Ptr := Sloc (N); | |
7084 | ||
7085 | Rlo, Rhi : Uint; | |
0326b4d4 | 7086 | -- Ranges of values for right operand (operator case) |
3cce7f32 | 7087 | |
7088 | Llo, Lhi : Uint; | |
0326b4d4 | 7089 | -- Ranges of values for left operand (operator case) |
3cce7f32 | 7090 | |
49b3a812 | 7091 | LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer); |
7092 | -- Operands and results are of this type when we convert | |
7093 | ||
0326b4d4 | 7094 | LLLo : constant Uint := Intval (Type_Low_Bound (LLIB)); |
7095 | LLHi : constant Uint := Intval (Type_High_Bound (LLIB)); | |
3cce7f32 | 7096 | -- Bounds of Long_Long_Integer |
7097 | ||
7098 | Binary : constant Boolean := Nkind (N) in N_Binary_Op; | |
7099 | -- Indicates binary operator case | |
7100 | ||
7101 | OK : Boolean; | |
7102 | -- Used in call to Determine_Range | |
7103 | ||
61016a7a | 7104 | Bignum_Operands : Boolean; |
7105 | -- Set True if one or more operands is already of type Bignum, meaning | |
7106 | -- that for sure (regardless of Top_Level setting) we are committed to | |
0326b4d4 | 7107 | -- doing the operation in Bignum mode (or in the case of a case or if |
21a55437 | 7108 | -- expression, converting all the dependent expressions to Bignum). |
0326b4d4 | 7109 | |
7110 | Long_Long_Integer_Operands : Boolean; | |
21a55437 | 7111 | -- Set True if one or more operands is already of type Long_Long_Integer |
0326b4d4 | 7112 | -- which means that if the result is known to be in the result type |
7113 | -- range, then we must convert such operands back to the result type. | |
0df9d43f | 7114 | |
7115 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False); | |
7116 | -- This is called when we have modified the node and we therefore need | |
7117 | -- to reanalyze it. It is important that we reset the mode to STRICT for | |
7118 | -- this reanalysis, since if we leave it in MINIMIZED or ELIMINATED mode | |
39a0c1d3 | 7119 | -- we would reenter this routine recursively which would not be good. |
0df9d43f | 7120 | -- The argument Suppress is set True if we also want to suppress |
7121 | -- overflow checking for the reexpansion (this is set when we know | |
7122 | -- overflow is not possible). Typ is the type for the reanalysis. | |
7123 | ||
7124 | procedure Reexpand (Suppress : Boolean := False); | |
7125 | -- This is like Reanalyze, but does not do the Analyze step, it only | |
7126 | -- does a reexpansion. We do this reexpansion in STRICT mode, so that | |
7127 | -- instead of reentering the MINIMIZED/ELIMINATED mode processing, we | |
7128 | -- follow the normal expansion path (e.g. converting A**4 to A**2**2). | |
7129 | -- Note that skipping reanalysis is not just an optimization, testing | |
7130 | -- has showed up several complex cases in which reanalyzing an already | |
7131 | -- analyzed node causes incorrect behavior. | |
4fb5f0a0 | 7132 | |
0326b4d4 | 7133 | function In_Result_Range return Boolean; |
7134 | -- Returns True iff Lo .. Hi are within range of the result type | |
61016a7a | 7135 | |
2fe22c69 | 7136 | procedure Max (A : in out Uint; B : Uint); |
21a55437 | 7137 | -- If A is No_Uint, sets A to B, else to UI_Max (A, B) |
2fe22c69 | 7138 | |
7139 | procedure Min (A : in out Uint; B : Uint); | |
21a55437 | 7140 | -- If A is No_Uint, sets A to B, else to UI_Min (A, B) |
2fe22c69 | 7141 | |
0326b4d4 | 7142 | --------------------- |
7143 | -- In_Result_Range -- | |
7144 | --------------------- | |
7145 | ||
7146 | function In_Result_Range return Boolean is | |
7147 | begin | |
f32c377d | 7148 | if Lo = No_Uint or else Hi = No_Uint then |
7149 | return False; | |
7150 | ||
7151 | elsif Is_Static_Subtype (Etype (N)) then | |
0326b4d4 | 7152 | return Lo >= Expr_Value (Type_Low_Bound (Rtyp)) |
7153 | and then | |
7154 | Hi <= Expr_Value (Type_High_Bound (Rtyp)); | |
f32c377d | 7155 | |
0326b4d4 | 7156 | else |
7157 | return Lo >= Expr_Value (Type_Low_Bound (Base_Type (Rtyp))) | |
7158 | and then | |
7159 | Hi <= Expr_Value (Type_High_Bound (Base_Type (Rtyp))); | |
7160 | end if; | |
7161 | end In_Result_Range; | |
7162 | ||
2fe22c69 | 7163 | --------- |
7164 | -- Max -- | |
7165 | --------- | |
7166 | ||
7167 | procedure Max (A : in out Uint; B : Uint) is | |
7168 | begin | |
7169 | if A = No_Uint or else B > A then | |
7170 | A := B; | |
7171 | end if; | |
7172 | end Max; | |
7173 | ||
7174 | --------- | |
7175 | -- Min -- | |
7176 | --------- | |
7177 | ||
7178 | procedure Min (A : in out Uint; B : Uint) is | |
7179 | begin | |
7180 | if A = No_Uint or else B < A then | |
7181 | A := B; | |
7182 | end if; | |
7183 | end Min; | |
7184 | ||
0df9d43f | 7185 | --------------- |
7186 | -- Reanalyze -- | |
7187 | --------------- | |
7188 | ||
7189 | procedure Reanalyze (Typ : Entity_Id; Suppress : Boolean := False) is | |
db415383 | 7190 | Svg : constant Overflow_Mode_Type := |
7191 | Scope_Suppress.Overflow_Mode_General; | |
7192 | Sva : constant Overflow_Mode_Type := | |
7193 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7194 | Svo : constant Boolean := |
7195 | Scope_Suppress.Suppress (Overflow_Check); | |
7196 | ||
7197 | begin | |
db415383 | 7198 | Scope_Suppress.Overflow_Mode_General := Strict; |
7199 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
0df9d43f | 7200 | |
7201 | if Suppress then | |
7202 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7203 | end if; | |
7204 | ||
7205 | Analyze_And_Resolve (N, Typ); | |
7206 | ||
7207 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7208 | Scope_Suppress.Overflow_Mode_General := Svg; |
7209 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
0df9d43f | 7210 | end Reanalyze; |
7211 | ||
4fb5f0a0 | 7212 | -------------- |
7213 | -- Reexpand -- | |
7214 | -------------- | |
7215 | ||
0df9d43f | 7216 | procedure Reexpand (Suppress : Boolean := False) is |
db415383 | 7217 | Svg : constant Overflow_Mode_Type := |
7218 | Scope_Suppress.Overflow_Mode_General; | |
7219 | Sva : constant Overflow_Mode_Type := | |
7220 | Scope_Suppress.Overflow_Mode_Assertions; | |
0df9d43f | 7221 | Svo : constant Boolean := |
7222 | Scope_Suppress.Suppress (Overflow_Check); | |
7223 | ||
4fb5f0a0 | 7224 | begin |
db415383 | 7225 | Scope_Suppress.Overflow_Mode_General := Strict; |
7226 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
4fb5f0a0 | 7227 | Set_Analyzed (N, False); |
0df9d43f | 7228 | |
7229 | if Suppress then | |
7230 | Scope_Suppress.Suppress (Overflow_Check) := True; | |
7231 | end if; | |
7232 | ||
4fb5f0a0 | 7233 | Expand (N); |
0df9d43f | 7234 | |
7235 | Scope_Suppress.Suppress (Overflow_Check) := Svo; | |
db415383 | 7236 | Scope_Suppress.Overflow_Mode_General := Svg; |
7237 | Scope_Suppress.Overflow_Mode_Assertions := Sva; | |
4fb5f0a0 | 7238 | end Reexpand; |
7239 | ||
0df9d43f | 7240 | -- Start of processing for Minimize_Eliminate_Overflows |
2fe22c69 | 7241 | |
3cce7f32 | 7242 | begin |
0326b4d4 | 7243 | -- Case where we do not have a signed integer arithmetic operation |
3cce7f32 | 7244 | |
7245 | if not Is_Signed_Integer_Arithmetic_Op (N) then | |
7246 | ||
7247 | -- Use the normal Determine_Range routine to get the range. We | |
7248 | -- don't require operands to be valid, invalid values may result in | |
7249 | -- rubbish results where the result has not been properly checked for | |
39a0c1d3 | 7250 | -- overflow, that's fine. |
3cce7f32 | 7251 | |
7252 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => False); | |
7253 | ||
21a55437 | 7254 | -- If Determine_Range did not work (can this in fact happen? Not |
3cce7f32 | 7255 | -- clear but might as well protect), use type bounds. |
7256 | ||
7257 | if not OK then | |
7258 | Lo := Intval (Type_Low_Bound (Base_Type (Etype (N)))); | |
7259 | Hi := Intval (Type_High_Bound (Base_Type (Etype (N)))); | |
7260 | end if; | |
7261 | ||
7262 | -- If we don't have a binary operator, all we have to do is to set | |
20cf157b | 7263 | -- the Hi/Lo range, so we are done. |
3cce7f32 | 7264 | |
7265 | return; | |
7266 | ||
0326b4d4 | 7267 | -- Processing for if expression |
7268 | ||
92f1631f | 7269 | elsif Nkind (N) = N_If_Expression then |
0326b4d4 | 7270 | declare |
7271 | Then_DE : constant Node_Id := Next (First (Expressions (N))); | |
7272 | Else_DE : constant Node_Id := Next (Then_DE); | |
7273 | ||
7274 | begin | |
7275 | Bignum_Operands := False; | |
7276 | ||
0df9d43f | 7277 | Minimize_Eliminate_Overflows |
0326b4d4 | 7278 | (Then_DE, Lo, Hi, Top_Level => False); |
7279 | ||
7280 | if Lo = No_Uint then | |
7281 | Bignum_Operands := True; | |
7282 | end if; | |
7283 | ||
0df9d43f | 7284 | Minimize_Eliminate_Overflows |
0326b4d4 | 7285 | (Else_DE, Rlo, Rhi, Top_Level => False); |
7286 | ||
7287 | if Rlo = No_Uint then | |
7288 | Bignum_Operands := True; | |
7289 | else | |
7290 | Long_Long_Integer_Operands := | |
7291 | Etype (Then_DE) = LLIB or else Etype (Else_DE) = LLIB; | |
7292 | ||
7293 | Min (Lo, Rlo); | |
7294 | Max (Hi, Rhi); | |
7295 | end if; | |
7296 | ||
21a55437 | 7297 | -- If at least one of our operands is now Bignum, we must rebuild |
7298 | -- the if expression to use Bignum operands. We will analyze the | |
0326b4d4 | 7299 | -- rebuilt if expression with overflow checks off, since once we |
39a0c1d3 | 7300 | -- are in bignum mode, we are all done with overflow checks. |
0326b4d4 | 7301 | |
7302 | if Bignum_Operands then | |
7303 | Rewrite (N, | |
92f1631f | 7304 | Make_If_Expression (Loc, |
0326b4d4 | 7305 | Expressions => New_List ( |
7306 | Remove_Head (Expressions (N)), | |
7307 | Convert_To_Bignum (Then_DE), | |
7308 | Convert_To_Bignum (Else_DE)), | |
7309 | Is_Elsif => Is_Elsif (N))); | |
7310 | ||
0df9d43f | 7311 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
0326b4d4 | 7312 | |
7313 | -- If we have no Long_Long_Integer operands, then we are in result | |
7314 | -- range, since it means that none of our operands felt the need | |
7315 | -- to worry about overflow (otherwise it would have already been | |
4fb5f0a0 | 7316 | -- converted to long long integer or bignum). We reexpand to |
7317 | -- complete the expansion of the if expression (but we do not | |
7318 | -- need to reanalyze). | |
0326b4d4 | 7319 | |
7320 | elsif not Long_Long_Integer_Operands then | |
7321 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7322 | Reexpand; |
0326b4d4 | 7323 | |
7324 | -- Otherwise convert us to long long integer mode. Note that we | |
7325 | -- don't need any further overflow checking at this level. | |
7326 | ||
7327 | else | |
7328 | Convert_To_And_Rewrite (LLIB, Then_DE); | |
7329 | Convert_To_And_Rewrite (LLIB, Else_DE); | |
7330 | Set_Etype (N, LLIB); | |
f32c377d | 7331 | |
7332 | -- Now reanalyze with overflow checks off | |
7333 | ||
0326b4d4 | 7334 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7335 | Reanalyze (LLIB, Suppress => True); |
0326b4d4 | 7336 | end if; |
7337 | end; | |
7338 | ||
7339 | return; | |
7340 | ||
7341 | -- Here for case expression | |
7342 | ||
7343 | elsif Nkind (N) = N_Case_Expression then | |
7344 | Bignum_Operands := False; | |
7345 | Long_Long_Integer_Operands := False; | |
0326b4d4 | 7346 | |
7347 | declare | |
f32c377d | 7348 | Alt : Node_Id; |
0326b4d4 | 7349 | |
7350 | begin | |
7351 | -- Loop through expressions applying recursive call | |
7352 | ||
7353 | Alt := First (Alternatives (N)); | |
7354 | while Present (Alt) loop | |
7355 | declare | |
7356 | Aexp : constant Node_Id := Expression (Alt); | |
7357 | ||
7358 | begin | |
0df9d43f | 7359 | Minimize_Eliminate_Overflows |
0326b4d4 | 7360 | (Aexp, Lo, Hi, Top_Level => False); |
7361 | ||
7362 | if Lo = No_Uint then | |
7363 | Bignum_Operands := True; | |
7364 | elsif Etype (Aexp) = LLIB then | |
7365 | Long_Long_Integer_Operands := True; | |
7366 | end if; | |
7367 | end; | |
7368 | ||
7369 | Next (Alt); | |
7370 | end loop; | |
7371 | ||
7372 | -- If we have no bignum or long long integer operands, it means | |
7373 | -- that none of our dependent expressions could raise overflow. | |
7374 | -- In this case, we simply return with no changes except for | |
7375 | -- resetting the overflow flag, since we are done with overflow | |
4fb5f0a0 | 7376 | -- checks for this node. We will reexpand to get the needed |
7377 | -- expansion for the case expression, but we do not need to | |
21a55437 | 7378 | -- reanalyze, since nothing has changed. |
0326b4d4 | 7379 | |
f32c377d | 7380 | if not (Bignum_Operands or Long_Long_Integer_Operands) then |
0326b4d4 | 7381 | Set_Do_Overflow_Check (N, False); |
0df9d43f | 7382 | Reexpand (Suppress => True); |
0326b4d4 | 7383 | |
7384 | -- Otherwise we are going to rebuild the case expression using | |
7385 | -- either bignum or long long integer operands throughout. | |
7386 | ||
7387 | else | |
f32c377d | 7388 | declare |
7389 | Rtype : Entity_Id; | |
7390 | New_Alts : List_Id; | |
7391 | New_Exp : Node_Id; | |
7392 | ||
7393 | begin | |
7394 | New_Alts := New_List; | |
7395 | Alt := First (Alternatives (N)); | |
7396 | while Present (Alt) loop | |
7397 | if Bignum_Operands then | |
7398 | New_Exp := Convert_To_Bignum (Expression (Alt)); | |
7399 | Rtype := RTE (RE_Bignum); | |
7400 | else | |
7401 | New_Exp := Convert_To (LLIB, Expression (Alt)); | |
7402 | Rtype := LLIB; | |
7403 | end if; | |
0326b4d4 | 7404 | |
f32c377d | 7405 | Append_To (New_Alts, |
7406 | Make_Case_Expression_Alternative (Sloc (Alt), | |
7407 | Actions => No_List, | |
7408 | Discrete_Choices => Discrete_Choices (Alt), | |
7409 | Expression => New_Exp)); | |
0326b4d4 | 7410 | |
f32c377d | 7411 | Next (Alt); |
7412 | end loop; | |
0326b4d4 | 7413 | |
f32c377d | 7414 | Rewrite (N, |
7415 | Make_Case_Expression (Loc, | |
7416 | Expression => Expression (N), | |
7417 | Alternatives => New_Alts)); | |
0326b4d4 | 7418 | |
0df9d43f | 7419 | Reanalyze (Rtype, Suppress => True); |
f32c377d | 7420 | end; |
0326b4d4 | 7421 | end if; |
7422 | end; | |
7423 | ||
7424 | return; | |
7425 | end if; | |
7426 | ||
7427 | -- If we have an arithmetic operator we make recursive calls on the | |
3cce7f32 | 7428 | -- operands to get the ranges (and to properly process the subtree |
20cf157b | 7429 | -- that lies below us). |
3cce7f32 | 7430 | |
0df9d43f | 7431 | Minimize_Eliminate_Overflows |
0326b4d4 | 7432 | (Right_Opnd (N), Rlo, Rhi, Top_Level => False); |
3cce7f32 | 7433 | |
0326b4d4 | 7434 | if Binary then |
0df9d43f | 7435 | Minimize_Eliminate_Overflows |
0326b4d4 | 7436 | (Left_Opnd (N), Llo, Lhi, Top_Level => False); |
3cce7f32 | 7437 | end if; |
7438 | ||
f32c377d | 7439 | -- Record if we have Long_Long_Integer operands |
7440 | ||
7441 | Long_Long_Integer_Operands := | |
7442 | Etype (Right_Opnd (N)) = LLIB | |
7443 | or else (Binary and then Etype (Left_Opnd (N)) = LLIB); | |
7444 | ||
7445 | -- If either operand is a bignum, then result will be a bignum and we | |
7446 | -- don't need to do any range analysis. As previously discussed we could | |
7447 | -- do range analysis in such cases, but it could mean working with giant | |
7448 | -- numbers at compile time for very little gain (the number of cases | |
21a55437 | 7449 | -- in which we could slip back from bignum mode is small). |
3cce7f32 | 7450 | |
7451 | if Rlo = No_Uint or else (Binary and then Llo = No_Uint) then | |
7452 | Lo := No_Uint; | |
7453 | Hi := No_Uint; | |
61016a7a | 7454 | Bignum_Operands := True; |
3cce7f32 | 7455 | |
7456 | -- Otherwise compute result range | |
7457 | ||
7458 | else | |
61016a7a | 7459 | Bignum_Operands := False; |
7460 | ||
3cce7f32 | 7461 | case Nkind (N) is |
7462 | ||
7463 | -- Absolute value | |
7464 | ||
7465 | when N_Op_Abs => | |
7466 | Lo := Uint_0; | |
de922300 | 7467 | Hi := UI_Max (abs Rlo, abs Rhi); |
3cce7f32 | 7468 | |
7469 | -- Addition | |
7470 | ||
7471 | when N_Op_Add => | |
7472 | Lo := Llo + Rlo; | |
7473 | Hi := Lhi + Rhi; | |
7474 | ||
7475 | -- Division | |
7476 | ||
7477 | when N_Op_Divide => | |
2fe22c69 | 7478 | |
5f4275e1 | 7479 | -- If the right operand can only be zero, set 0..0 |
2fe22c69 | 7480 | |
5f4275e1 | 7481 | if Rlo = 0 and then Rhi = 0 then |
7482 | Lo := Uint_0; | |
7483 | Hi := Uint_0; | |
2fe22c69 | 7484 | |
5f4275e1 | 7485 | -- Possible bounds of division must come from dividing end |
7486 | -- values of the input ranges (four possibilities), provided | |
7487 | -- zero is not included in the possible values of the right | |
7488 | -- operand. | |
7489 | ||
7490 | -- Otherwise, we just consider two intervals of values for | |
7491 | -- the right operand: the interval of negative values (up to | |
7492 | -- -1) and the interval of positive values (starting at 1). | |
7493 | -- Since division by 1 is the identity, and division by -1 | |
7494 | -- is negation, we get all possible bounds of division in that | |
7495 | -- case by considering: | |
7496 | -- - all values from the division of end values of input | |
7497 | -- ranges; | |
7498 | -- - the end values of the left operand; | |
7499 | -- - the negation of the end values of the left operand. | |
2fe22c69 | 7500 | |
5f4275e1 | 7501 | else |
7502 | declare | |
7503 | Mrk : constant Uintp.Save_Mark := Mark; | |
7504 | -- Mark so we can release the RR and Ev values | |
2fe22c69 | 7505 | |
5f4275e1 | 7506 | Ev1 : Uint; |
7507 | Ev2 : Uint; | |
7508 | Ev3 : Uint; | |
7509 | Ev4 : Uint; | |
2fe22c69 | 7510 | |
5f4275e1 | 7511 | begin |
7512 | -- Discard extreme values of zero for the divisor, since | |
7513 | -- they will simply result in an exception in any case. | |
2fe22c69 | 7514 | |
5f4275e1 | 7515 | if Rlo = 0 then |
7516 | Rlo := Uint_1; | |
7517 | elsif Rhi = 0 then | |
7518 | Rhi := -Uint_1; | |
2fe22c69 | 7519 | end if; |
2fe22c69 | 7520 | |
5f4275e1 | 7521 | -- Compute possible bounds coming from dividing end |
7522 | -- values of the input ranges. | |
2fe22c69 | 7523 | |
5f4275e1 | 7524 | Ev1 := Llo / Rlo; |
7525 | Ev2 := Llo / Rhi; | |
7526 | Ev3 := Lhi / Rlo; | |
7527 | Ev4 := Lhi / Rhi; | |
2fe22c69 | 7528 | |
5f4275e1 | 7529 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); |
7530 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
2fe22c69 | 7531 | |
5f4275e1 | 7532 | -- If the right operand can be both negative or positive, |
7533 | -- include the end values of the left operand in the | |
7534 | -- extreme values, as well as their negation. | |
2fe22c69 | 7535 | |
5f4275e1 | 7536 | if Rlo < 0 and then Rhi > 0 then |
7537 | Ev1 := Llo; | |
7538 | Ev2 := -Llo; | |
7539 | Ev3 := Lhi; | |
7540 | Ev4 := -Lhi; | |
2fe22c69 | 7541 | |
5f4275e1 | 7542 | Min (Lo, |
7543 | UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4))); | |
7544 | Max (Hi, | |
7545 | UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4))); | |
2fe22c69 | 7546 | end if; |
2fe22c69 | 7547 | |
5f4275e1 | 7548 | -- Release the RR and Ev values |
2fe22c69 | 7549 | |
5f4275e1 | 7550 | Release_And_Save (Mrk, Lo, Hi); |
7551 | end; | |
7552 | end if; | |
3cce7f32 | 7553 | |
7554 | -- Exponentiation | |
7555 | ||
7556 | when N_Op_Expon => | |
de922300 | 7557 | |
7558 | -- Discard negative values for the exponent, since they will | |
7559 | -- simply result in an exception in any case. | |
7560 | ||
7561 | if Rhi < 0 then | |
7562 | Rhi := Uint_0; | |
7563 | elsif Rlo < 0 then | |
7564 | Rlo := Uint_0; | |
7565 | end if; | |
7566 | ||
7567 | -- Estimate number of bits in result before we go computing | |
7568 | -- giant useless bounds. Basically the number of bits in the | |
7569 | -- result is the number of bits in the base multiplied by the | |
7570 | -- value of the exponent. If this is big enough that the result | |
7571 | -- definitely won't fit in Long_Long_Integer, switch to bignum | |
7572 | -- mode immediately, and avoid computing giant bounds. | |
7573 | ||
7574 | -- The comparison here is approximate, but conservative, it | |
7575 | -- only clicks on cases that are sure to exceed the bounds. | |
7576 | ||
7577 | if Num_Bits (UI_Max (abs Llo, abs Lhi)) * Rhi + 1 > 100 then | |
7578 | Lo := No_Uint; | |
7579 | Hi := No_Uint; | |
7580 | ||
7581 | -- If right operand is zero then result is 1 | |
7582 | ||
7583 | elsif Rhi = 0 then | |
7584 | Lo := Uint_1; | |
7585 | Hi := Uint_1; | |
7586 | ||
7587 | else | |
7588 | -- High bound comes either from exponentiation of largest | |
5f4275e1 | 7589 | -- positive value to largest exponent value, or from |
7590 | -- the exponentiation of most negative value to an | |
7591 | -- even exponent. | |
de922300 | 7592 | |
7593 | declare | |
7594 | Hi1, Hi2 : Uint; | |
7595 | ||
7596 | begin | |
5f4275e1 | 7597 | if Lhi > 0 then |
de922300 | 7598 | Hi1 := Lhi ** Rhi; |
7599 | else | |
7600 | Hi1 := Uint_0; | |
7601 | end if; | |
7602 | ||
7603 | if Llo < 0 then | |
7604 | if Rhi mod 2 = 0 then | |
de922300 | 7605 | Hi2 := Llo ** Rhi; |
5f4275e1 | 7606 | else |
7607 | Hi2 := Llo ** (Rhi - 1); | |
de922300 | 7608 | end if; |
7609 | else | |
7610 | Hi2 := Uint_0; | |
7611 | end if; | |
7612 | ||
7613 | Hi := UI_Max (Hi1, Hi2); | |
7614 | end; | |
7615 | ||
7616 | -- Result can only be negative if base can be negative | |
7617 | ||
7618 | if Llo < 0 then | |
21a55437 | 7619 | if Rhi mod 2 = 0 then |
de922300 | 7620 | Lo := Llo ** (Rhi - 1); |
7621 | else | |
7622 | Lo := Llo ** Rhi; | |
7623 | end if; | |
7624 | ||
21a55437 | 7625 | -- Otherwise low bound is minimum ** minimum |
de922300 | 7626 | |
7627 | else | |
7628 | Lo := Llo ** Rlo; | |
7629 | end if; | |
7630 | end if; | |
3cce7f32 | 7631 | |
7632 | -- Negation | |
7633 | ||
7634 | when N_Op_Minus => | |
7635 | Lo := -Rhi; | |
7636 | Hi := -Rlo; | |
7637 | ||
7638 | -- Mod | |
7639 | ||
7640 | when N_Op_Mod => | |
2fe22c69 | 7641 | declare |
5f4275e1 | 7642 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7643 | -- This is the maximum absolute value of the result |
7644 | ||
7645 | begin | |
7646 | Lo := Uint_0; | |
7647 | Hi := Uint_0; | |
7648 | ||
7649 | -- The result depends only on the sign and magnitude of | |
7650 | -- the right operand, it does not depend on the sign or | |
7651 | -- magnitude of the left operand. | |
7652 | ||
7653 | if Rlo < 0 then | |
7654 | Lo := -Maxabs; | |
7655 | end if; | |
7656 | ||
7657 | if Rhi > 0 then | |
7658 | Hi := Maxabs; | |
7659 | end if; | |
7660 | end; | |
3cce7f32 | 7661 | |
7662 | -- Multiplication | |
7663 | ||
7664 | when N_Op_Multiply => | |
49b3a812 | 7665 | |
7666 | -- Possible bounds of multiplication must come from multiplying | |
7667 | -- end values of the input ranges (four possibilities). | |
7668 | ||
7669 | declare | |
7670 | Mrk : constant Uintp.Save_Mark := Mark; | |
7671 | -- Mark so we can release the Ev values | |
7672 | ||
7673 | Ev1 : constant Uint := Llo * Rlo; | |
7674 | Ev2 : constant Uint := Llo * Rhi; | |
7675 | Ev3 : constant Uint := Lhi * Rlo; | |
7676 | Ev4 : constant Uint := Lhi * Rhi; | |
7677 | ||
7678 | begin | |
7679 | Lo := UI_Min (UI_Min (Ev1, Ev2), UI_Min (Ev3, Ev4)); | |
7680 | Hi := UI_Max (UI_Max (Ev1, Ev2), UI_Max (Ev3, Ev4)); | |
7681 | ||
7682 | -- Release the Ev values | |
7683 | ||
7684 | Release_And_Save (Mrk, Lo, Hi); | |
7685 | end; | |
3cce7f32 | 7686 | |
7687 | -- Plus operator (affirmation) | |
7688 | ||
7689 | when N_Op_Plus => | |
7690 | Lo := Rlo; | |
7691 | Hi := Rhi; | |
7692 | ||
7693 | -- Remainder | |
7694 | ||
7695 | when N_Op_Rem => | |
2fe22c69 | 7696 | declare |
5f4275e1 | 7697 | Maxabs : constant Uint := UI_Max (abs Rlo, abs Rhi) - 1; |
2fe22c69 | 7698 | -- This is the maximum absolute value of the result. Note |
5f4275e1 | 7699 | -- that the result range does not depend on the sign of the |
7700 | -- right operand. | |
2fe22c69 | 7701 | |
7702 | begin | |
7703 | Lo := Uint_0; | |
7704 | Hi := Uint_0; | |
7705 | ||
7706 | -- Case of left operand negative, which results in a range | |
7707 | -- of -Maxabs .. 0 for those negative values. If there are | |
7708 | -- no negative values then Lo value of result is always 0. | |
7709 | ||
7710 | if Llo < 0 then | |
7711 | Lo := -Maxabs; | |
7712 | end if; | |
7713 | ||
7714 | -- Case of left operand positive | |
7715 | ||
7716 | if Lhi > 0 then | |
7717 | Hi := Maxabs; | |
7718 | end if; | |
7719 | end; | |
3cce7f32 | 7720 | |
7721 | -- Subtract | |
7722 | ||
7723 | when N_Op_Subtract => | |
7724 | Lo := Llo - Rhi; | |
7725 | Hi := Lhi - Rlo; | |
7726 | ||
7727 | -- Nothing else should be possible | |
7728 | ||
7729 | when others => | |
7730 | raise Program_Error; | |
3cce7f32 | 7731 | end case; |
7732 | end if; | |
7733 | ||
4fb5f0a0 | 7734 | -- Here for the case where we have not rewritten anything (no bignum |
21a55437 | 7735 | -- operands or long long integer operands), and we know the result. |
7736 | -- If we know we are in the result range, and we do not have Bignum | |
7737 | -- operands or Long_Long_Integer operands, we can just reexpand with | |
7738 | -- overflow checks turned off (since we know we cannot have overflow). | |
7739 | -- As always the reexpansion is required to complete expansion of the | |
7740 | -- operator, but we do not need to reanalyze, and we prevent recursion | |
7741 | -- by suppressing the check. | |
f32c377d | 7742 | |
7743 | if not (Bignum_Operands or Long_Long_Integer_Operands) | |
7744 | and then In_Result_Range | |
7745 | then | |
7746 | Set_Do_Overflow_Check (N, False); | |
0df9d43f | 7747 | Reexpand (Suppress => True); |
f32c377d | 7748 | return; |
7749 | ||
7750 | -- Here we know that we are not in the result range, and in the general | |
21a55437 | 7751 | -- case we will move into either the Bignum or Long_Long_Integer domain |
7752 | -- to compute the result. However, there is one exception. If we are | |
7753 | -- at the top level, and we do not have Bignum or Long_Long_Integer | |
7754 | -- operands, we will have to immediately convert the result back to | |
7755 | -- the result type, so there is no point in Bignum/Long_Long_Integer | |
7756 | -- fiddling. | |
f32c377d | 7757 | |
7758 | elsif Top_Level | |
7759 | and then not (Bignum_Operands or Long_Long_Integer_Operands) | |
b6a8f264 | 7760 | |
7761 | -- One further refinement. If we are at the top level, but our parent | |
7762 | -- is a type conversion, then go into bignum or long long integer node | |
7763 | -- since the result will be converted to that type directly without | |
7764 | -- going through the result type, and we may avoid an overflow. This | |
7765 | -- is the case for example of Long_Long_Integer (A ** 4), where A is | |
7766 | -- of type Integer, and the result A ** 4 fits in Long_Long_Integer | |
7767 | -- but does not fit in Integer. | |
7768 | ||
7769 | and then Nkind (Parent (N)) /= N_Type_Conversion | |
f32c377d | 7770 | then |
0df9d43f | 7771 | -- Here keep original types, but we need to complete analysis |
f32c377d | 7772 | |
7773 | -- One subtlety. We can't just go ahead and do an analyze operation | |
21a55437 | 7774 | -- here because it will cause recursion into the whole MINIMIZED/ |
7775 | -- ELIMINATED overflow processing which is not what we want. Here | |
f32c377d | 7776 | -- we are at the top level, and we need a check against the result |
39a0c1d3 | 7777 | -- mode (i.e. we want to use STRICT mode). So do exactly that. |
4fb5f0a0 | 7778 | -- Also, we have not modified the node, so this is a case where |
7779 | -- we need to reexpand, but not reanalyze. | |
f32c377d | 7780 | |
0df9d43f | 7781 | Reexpand; |
f32c377d | 7782 | return; |
7783 | ||
7784 | -- Cases where we do the operation in Bignum mode. This happens either | |
3cce7f32 | 7785 | -- because one of our operands is in Bignum mode already, or because |
de922300 | 7786 | -- the computed bounds are outside the bounds of Long_Long_Integer, |
7787 | -- which in some cases can be indicated by Hi and Lo being No_Uint. | |
3cce7f32 | 7788 | |
7789 | -- Note: we could do better here and in some cases switch back from | |
7790 | -- Bignum mode to normal mode, e.g. big mod 2 must be in the range | |
7791 | -- 0 .. 1, but the cases are rare and it is not worth the effort. | |
7792 | -- Failing to do this switching back is only an efficiency issue. | |
7793 | ||
f32c377d | 7794 | elsif Lo = No_Uint or else Lo < LLLo or else Hi > LLHi then |
3cce7f32 | 7795 | |
61016a7a | 7796 | -- OK, we are definitely outside the range of Long_Long_Integer. The |
f32c377d | 7797 | -- question is whether to move to Bignum mode, or stay in the domain |
61016a7a | 7798 | -- of Long_Long_Integer, signalling that an overflow check is needed. |
7799 | ||
7800 | -- Obviously in MINIMIZED mode we stay with LLI, since we are not in | |
7801 | -- the Bignum business. In ELIMINATED mode, we will normally move | |
7802 | -- into Bignum mode, but there is an exception if neither of our | |
7803 | -- operands is Bignum now, and we are at the top level (Top_Level | |
7804 | -- set True). In this case, there is no point in moving into Bignum | |
7805 | -- mode to prevent overflow if the caller will immediately convert | |
7806 | -- the Bignum value back to LLI with an overflow check. It's more | |
0df9d43f | 7807 | -- efficient to stay in LLI mode with an overflow check (if needed) |
61016a7a | 7808 | |
7809 | if Check_Mode = Minimized | |
7810 | or else (Top_Level and not Bignum_Operands) | |
7811 | then | |
0df9d43f | 7812 | if Do_Overflow_Check (N) then |
7813 | Enable_Overflow_Check (N); | |
7814 | end if; | |
3cce7f32 | 7815 | |
0df9d43f | 7816 | -- The result now has to be in Long_Long_Integer mode, so adjust |
7817 | -- the possible range to reflect this. Note these calls also | |
7818 | -- change No_Uint values from the top level case to LLI bounds. | |
61016a7a | 7819 | |
7820 | Max (Lo, LLLo); | |
7821 | Min (Hi, LLHi); | |
7822 | ||
7823 | -- Otherwise we are in ELIMINATED mode and we switch to Bignum mode | |
3cce7f32 | 7824 | |
7825 | else | |
7826 | pragma Assert (Check_Mode = Eliminated); | |
7827 | ||
7828 | declare | |
7829 | Fent : Entity_Id; | |
7830 | Args : List_Id; | |
7831 | ||
7832 | begin | |
7833 | case Nkind (N) is | |
7834 | when N_Op_Abs => | |
7835 | Fent := RTE (RE_Big_Abs); | |
7836 | ||
7837 | when N_Op_Add => | |
7838 | Fent := RTE (RE_Big_Add); | |
7839 | ||
7840 | when N_Op_Divide => | |
7841 | Fent := RTE (RE_Big_Div); | |
7842 | ||
7843 | when N_Op_Expon => | |
7844 | Fent := RTE (RE_Big_Exp); | |
7845 | ||
7846 | when N_Op_Minus => | |
7847 | Fent := RTE (RE_Big_Neg); | |
7848 | ||
7849 | when N_Op_Mod => | |
7850 | Fent := RTE (RE_Big_Mod); | |
7851 | ||
7852 | when N_Op_Multiply => | |
7853 | Fent := RTE (RE_Big_Mul); | |
7854 | ||
7855 | when N_Op_Rem => | |
7856 | Fent := RTE (RE_Big_Rem); | |
7857 | ||
7858 | when N_Op_Subtract => | |
7859 | Fent := RTE (RE_Big_Sub); | |
7860 | ||
7861 | -- Anything else is an internal error, this includes the | |
7862 | -- N_Op_Plus case, since how can plus cause the result | |
7863 | -- to be out of range if the operand is in range? | |
7864 | ||
7865 | when others => | |
7866 | raise Program_Error; | |
7867 | end case; | |
7868 | ||
7869 | -- Construct argument list for Bignum call, converting our | |
7870 | -- operands to Bignum form if they are not already there. | |
7871 | ||
7872 | Args := New_List; | |
7873 | ||
7874 | if Binary then | |
7875 | Append_To (Args, Convert_To_Bignum (Left_Opnd (N))); | |
7876 | end if; | |
7877 | ||
7878 | Append_To (Args, Convert_To_Bignum (Right_Opnd (N))); | |
7879 | ||
7880 | -- Now rewrite the arithmetic operator with a call to the | |
7881 | -- corresponding bignum function. | |
7882 | ||
7883 | Rewrite (N, | |
7884 | Make_Function_Call (Loc, | |
7885 | Name => New_Occurrence_Of (Fent, Loc), | |
7886 | Parameter_Associations => Args)); | |
0df9d43f | 7887 | Reanalyze (RTE (RE_Bignum), Suppress => True); |
61016a7a | 7888 | |
7889 | -- Indicate result is Bignum mode | |
7890 | ||
7891 | Lo := No_Uint; | |
7892 | Hi := No_Uint; | |
de922300 | 7893 | return; |
3cce7f32 | 7894 | end; |
7895 | end if; | |
7896 | ||
7897 | -- Otherwise we are in range of Long_Long_Integer, so no overflow | |
de922300 | 7898 | -- check is required, at least not yet. |
3cce7f32 | 7899 | |
7900 | else | |
de922300 | 7901 | Set_Do_Overflow_Check (N, False); |
7902 | end if; | |
3cce7f32 | 7903 | |
f32c377d | 7904 | -- Here we are not in Bignum territory, but we may have long long |
7905 | -- integer operands that need special handling. First a special check: | |
7906 | -- If an exponentiation operator exponent is of type Long_Long_Integer, | |
7907 | -- it means we converted it to prevent overflow, but exponentiation | |
7908 | -- requires a Natural right operand, so convert it back to Natural. | |
7909 | -- This conversion may raise an exception which is fine. | |
0326b4d4 | 7910 | |
f32c377d | 7911 | if Nkind (N) = N_Op_Expon and then Etype (Right_Opnd (N)) = LLIB then |
7912 | Convert_To_And_Rewrite (Standard_Natural, Right_Opnd (N)); | |
0326b4d4 | 7913 | end if; |
7914 | ||
de922300 | 7915 | -- Here we will do the operation in Long_Long_Integer. We do this even |
7916 | -- if we know an overflow check is required, better to do this in long | |
39a0c1d3 | 7917 | -- long integer mode, since we are less likely to overflow. |
3cce7f32 | 7918 | |
de922300 | 7919 | -- Convert right or only operand to Long_Long_Integer, except that |
7920 | -- we do not touch the exponentiation right operand. | |
3cce7f32 | 7921 | |
de922300 | 7922 | if Nkind (N) /= N_Op_Expon then |
7923 | Convert_To_And_Rewrite (LLIB, Right_Opnd (N)); | |
7924 | end if; | |
3cce7f32 | 7925 | |
de922300 | 7926 | -- Convert left operand to Long_Long_Integer for binary case |
49b3a812 | 7927 | |
de922300 | 7928 | if Binary then |
7929 | Convert_To_And_Rewrite (LLIB, Left_Opnd (N)); | |
7930 | end if; | |
7931 | ||
7932 | -- Reset node to unanalyzed | |
7933 | ||
7934 | Set_Analyzed (N, False); | |
7935 | Set_Etype (N, Empty); | |
7936 | Set_Entity (N, Empty); | |
7937 | ||
2fe22c69 | 7938 | -- Now analyze this new node. This reanalysis will complete processing |
7939 | -- for the node. In particular we will complete the expansion of an | |
7940 | -- exponentiation operator (e.g. changing A ** 2 to A * A), and also | |
7941 | -- we will complete any division checks (since we have not changed the | |
7942 | -- setting of the Do_Division_Check flag). | |
3cce7f32 | 7943 | |
0df9d43f | 7944 | -- We do this reanalysis in STRICT mode to avoid recursion into the |
39a0c1d3 | 7945 | -- MINIMIZED/ELIMINATED handling, since we are now done with that. |
3cce7f32 | 7946 | |
0df9d43f | 7947 | declare |
db415383 | 7948 | SG : constant Overflow_Mode_Type := |
7949 | Scope_Suppress.Overflow_Mode_General; | |
7950 | SA : constant Overflow_Mode_Type := | |
7951 | Scope_Suppress.Overflow_Mode_Assertions; | |
de922300 | 7952 | |
0df9d43f | 7953 | begin |
db415383 | 7954 | Scope_Suppress.Overflow_Mode_General := Strict; |
7955 | Scope_Suppress.Overflow_Mode_Assertions := Strict; | |
de922300 | 7956 | |
0df9d43f | 7957 | if not Do_Overflow_Check (N) then |
7958 | Reanalyze (LLIB, Suppress => True); | |
7959 | else | |
7960 | Reanalyze (LLIB); | |
7961 | end if; | |
7962 | ||
db415383 | 7963 | Scope_Suppress.Overflow_Mode_General := SG; |
7964 | Scope_Suppress.Overflow_Mode_Assertions := SA; | |
0df9d43f | 7965 | end; |
7966 | end Minimize_Eliminate_Overflows; | |
3cce7f32 | 7967 | |
7968 | ------------------------- | |
7969 | -- Overflow_Check_Mode -- | |
7970 | ------------------------- | |
7971 | ||
db415383 | 7972 | function Overflow_Check_Mode return Overflow_Mode_Type is |
ee6ba406 | 7973 | begin |
724d2bd8 | 7974 | if In_Assertion_Expr = 0 then |
db415383 | 7975 | return Scope_Suppress.Overflow_Mode_General; |
9dfe12ae | 7976 | else |
db415383 | 7977 | return Scope_Suppress.Overflow_Mode_Assertions; |
9dfe12ae | 7978 | end if; |
3cce7f32 | 7979 | end Overflow_Check_Mode; |
7980 | ||
7981 | -------------------------------- | |
7982 | -- Overflow_Checks_Suppressed -- | |
7983 | -------------------------------- | |
7984 | ||
7985 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7986 | begin | |
0df9d43f | 7987 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7988 | return Is_Check_Suppressed (E, Overflow_Check); | |
7989 | else | |
7990 | return Scope_Suppress.Suppress (Overflow_Check); | |
7991 | end if; | |
ee6ba406 | 7992 | end Overflow_Checks_Suppressed; |
fc75802a | 7993 | |
37baba83 | 7994 | --------------------------------- |
7995 | -- Predicate_Checks_Suppressed -- | |
7996 | --------------------------------- | |
7997 | ||
7998 | function Predicate_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7999 | begin | |
8000 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
8001 | return Is_Check_Suppressed (E, Predicate_Check); | |
8002 | else | |
8003 | return Scope_Suppress.Suppress (Predicate_Check); | |
8004 | end if; | |
8005 | end Predicate_Checks_Suppressed; | |
8006 | ||
ee6ba406 | 8007 | ----------------------------- |
8008 | -- Range_Checks_Suppressed -- | |
8009 | ----------------------------- | |
8010 | ||
8011 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
8012 | begin | |
9dfe12ae | 8013 | if Present (E) then |
8014 | ||
8015 | -- Note: for now we always suppress range checks on Vax float types, | |
8016 | -- since Gigi does not know how to generate these checks. | |
8017 | ||
8018 | if Vax_Float (E) then | |
8019 | return True; | |
8020 | elsif Kill_Range_Checks (E) then | |
8021 | return True; | |
8022 | elsif Checks_May_Be_Suppressed (E) then | |
8023 | return Is_Check_Suppressed (E, Range_Check); | |
8024 | end if; | |
8025 | end if; | |
ee6ba406 | 8026 | |
fafc6b97 | 8027 | return Scope_Suppress.Suppress (Range_Check); |
ee6ba406 | 8028 | end Range_Checks_Suppressed; |
8029 | ||
0577b0b1 | 8030 | ----------------------------------------- |
8031 | -- Range_Or_Validity_Checks_Suppressed -- | |
8032 | ----------------------------------------- | |
8033 | ||
8034 | -- Note: the coding would be simpler here if we simply made appropriate | |
8035 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
8036 | -- duplicated checks which we prefer to avoid. | |
8037 | ||
8038 | function Range_Or_Validity_Checks_Suppressed | |
8039 | (Expr : Node_Id) return Boolean | |
8040 | is | |
8041 | begin | |
8042 | -- Immediate return if scope checks suppressed for either check | |
8043 | ||
fafc6b97 | 8044 | if Scope_Suppress.Suppress (Range_Check) |
8045 | or | |
8046 | Scope_Suppress.Suppress (Validity_Check) | |
8047 | then | |
0577b0b1 | 8048 | return True; |
8049 | end if; | |
8050 | ||
8051 | -- If no expression, that's odd, decide that checks are suppressed, | |
8052 | -- since we don't want anyone trying to do checks in this case, which | |
8053 | -- is most likely the result of some other error. | |
8054 | ||
8055 | if No (Expr) then | |
8056 | return True; | |
8057 | end if; | |
8058 | ||
8059 | -- Expression is present, so perform suppress checks on type | |
8060 | ||
8061 | declare | |
8062 | Typ : constant Entity_Id := Etype (Expr); | |
8063 | begin | |
8064 | if Vax_Float (Typ) then | |
8065 | return True; | |
8066 | elsif Checks_May_Be_Suppressed (Typ) | |
8067 | and then (Is_Check_Suppressed (Typ, Range_Check) | |
8068 | or else | |
8069 | Is_Check_Suppressed (Typ, Validity_Check)) | |
8070 | then | |
8071 | return True; | |
8072 | end if; | |
8073 | end; | |
8074 | ||
8075 | -- If expression is an entity name, perform checks on this entity | |
8076 | ||
8077 | if Is_Entity_Name (Expr) then | |
8078 | declare | |
8079 | Ent : constant Entity_Id := Entity (Expr); | |
8080 | begin | |
8081 | if Checks_May_Be_Suppressed (Ent) then | |
8082 | return Is_Check_Suppressed (Ent, Range_Check) | |
8083 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
8084 | end if; | |
8085 | end; | |
8086 | end if; | |
8087 | ||
8088 | -- If we fall through, no checks suppressed | |
8089 | ||
8090 | return False; | |
8091 | end Range_Or_Validity_Checks_Suppressed; | |
8092 | ||
226494a3 | 8093 | ------------------- |
8094 | -- Remove_Checks -- | |
8095 | ------------------- | |
8096 | ||
8097 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 8098 | function Process (N : Node_Id) return Traverse_Result; |
8099 | -- Process a single node during the traversal | |
8100 | ||
8f6e4fd5 | 8101 | procedure Traverse is new Traverse_Proc (Process); |
8102 | -- The traversal procedure itself | |
226494a3 | 8103 | |
8104 | ------------- | |
8105 | -- Process -- | |
8106 | ------------- | |
8107 | ||
8108 | function Process (N : Node_Id) return Traverse_Result is | |
8109 | begin | |
8110 | if Nkind (N) not in N_Subexpr then | |
8111 | return Skip; | |
8112 | end if; | |
8113 | ||
8114 | Set_Do_Range_Check (N, False); | |
8115 | ||
8116 | case Nkind (N) is | |
8117 | when N_And_Then => | |
8f6e4fd5 | 8118 | Traverse (Left_Opnd (N)); |
226494a3 | 8119 | return Skip; |
8120 | ||
8121 | when N_Attribute_Reference => | |
226494a3 | 8122 | Set_Do_Overflow_Check (N, False); |
8123 | ||
226494a3 | 8124 | when N_Function_Call => |
8125 | Set_Do_Tag_Check (N, False); | |
8126 | ||
226494a3 | 8127 | when N_Op => |
8128 | Set_Do_Overflow_Check (N, False); | |
8129 | ||
8130 | case Nkind (N) is | |
8131 | when N_Op_Divide => | |
8132 | Set_Do_Division_Check (N, False); | |
8133 | ||
8134 | when N_Op_And => | |
8135 | Set_Do_Length_Check (N, False); | |
8136 | ||
8137 | when N_Op_Mod => | |
8138 | Set_Do_Division_Check (N, False); | |
8139 | ||
8140 | when N_Op_Or => | |
8141 | Set_Do_Length_Check (N, False); | |
8142 | ||
8143 | when N_Op_Rem => | |
8144 | Set_Do_Division_Check (N, False); | |
8145 | ||
8146 | when N_Op_Xor => | |
8147 | Set_Do_Length_Check (N, False); | |
8148 | ||
8149 | when others => | |
8150 | null; | |
8151 | end case; | |
8152 | ||
8153 | when N_Or_Else => | |
8f6e4fd5 | 8154 | Traverse (Left_Opnd (N)); |
226494a3 | 8155 | return Skip; |
8156 | ||
8157 | when N_Selected_Component => | |
226494a3 | 8158 | Set_Do_Discriminant_Check (N, False); |
8159 | ||
226494a3 | 8160 | when N_Type_Conversion => |
9dfe12ae | 8161 | Set_Do_Length_Check (N, False); |
8162 | Set_Do_Tag_Check (N, False); | |
226494a3 | 8163 | Set_Do_Overflow_Check (N, False); |
226494a3 | 8164 | |
8165 | when others => | |
8166 | null; | |
8167 | end case; | |
8168 | ||
8169 | return OK; | |
8170 | end Process; | |
8171 | ||
8172 | -- Start of processing for Remove_Checks | |
8173 | ||
8174 | begin | |
8f6e4fd5 | 8175 | Traverse (Expr); |
226494a3 | 8176 | end Remove_Checks; |
8177 | ||
ee6ba406 | 8178 | ---------------------------- |
8179 | -- Selected_Length_Checks -- | |
8180 | ---------------------------- | |
8181 | ||
8182 | function Selected_Length_Checks | |
8183 | (Ck_Node : Node_Id; | |
8184 | Target_Typ : Entity_Id; | |
8185 | Source_Typ : Entity_Id; | |
314a23b6 | 8186 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8187 | is |
8188 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8189 | S_Typ : Entity_Id; | |
8190 | T_Typ : Entity_Id; | |
8191 | Expr_Actual : Node_Id; | |
8192 | Exptyp : Entity_Id; | |
8193 | Cond : Node_Id := Empty; | |
8194 | Do_Access : Boolean := False; | |
8195 | Wnode : Node_Id := Warn_Node; | |
8196 | Ret_Result : Check_Result := (Empty, Empty); | |
8197 | Num_Checks : Natural := 0; | |
8198 | ||
8199 | procedure Add_Check (N : Node_Id); | |
8200 | -- Adds the action given to Ret_Result if N is non-Empty | |
8201 | ||
8202 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
8203 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 8204 | -- Comments required ??? |
ee6ba406 | 8205 | |
8206 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
8207 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 8208 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 8209 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 8210 | -- obviously superfluous checks. |
ee6ba406 | 8211 | |
8212 | function Length_E_Cond | |
8213 | (Exptyp : Entity_Id; | |
8214 | Typ : Entity_Id; | |
314a23b6 | 8215 | Indx : Nat) return Node_Id; |
ee6ba406 | 8216 | -- Returns expression to compute: |
8217 | -- Typ'Length /= Exptyp'Length | |
8218 | ||
8219 | function Length_N_Cond | |
8220 | (Expr : Node_Id; | |
8221 | Typ : Entity_Id; | |
314a23b6 | 8222 | Indx : Nat) return Node_Id; |
ee6ba406 | 8223 | -- Returns expression to compute: |
8224 | -- Typ'Length /= Expr'Length | |
8225 | ||
8226 | --------------- | |
8227 | -- Add_Check -- | |
8228 | --------------- | |
8229 | ||
8230 | procedure Add_Check (N : Node_Id) is | |
8231 | begin | |
8232 | if Present (N) then | |
8233 | ||
20cf157b | 8234 | -- For now, ignore attempt to place more than two checks ??? |
8235 | -- This is really worrisome, are we really discarding checks ??? | |
ee6ba406 | 8236 | |
8237 | if Num_Checks = 2 then | |
8238 | return; | |
8239 | end if; | |
8240 | ||
8241 | pragma Assert (Num_Checks <= 1); | |
8242 | Num_Checks := Num_Checks + 1; | |
8243 | Ret_Result (Num_Checks) := N; | |
8244 | end if; | |
8245 | end Add_Check; | |
8246 | ||
8247 | ------------------ | |
8248 | -- Get_E_Length -- | |
8249 | ------------------ | |
8250 | ||
8251 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 8252 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 8253 | N : Node_Id; |
8254 | E1 : Entity_Id := E; | |
ee6ba406 | 8255 | |
8256 | begin | |
8257 | if Ekind (Scope (E)) = E_Record_Type | |
8258 | and then Has_Discriminants (Scope (E)) | |
8259 | then | |
8260 | N := Build_Discriminal_Subtype_Of_Component (E); | |
8261 | ||
8262 | if Present (N) then | |
8263 | Insert_Action (Ck_Node, N); | |
8264 | E1 := Defining_Identifier (N); | |
8265 | end if; | |
8266 | end if; | |
8267 | ||
8268 | if Ekind (E1) = E_String_Literal_Subtype then | |
8269 | return | |
8270 | Make_Integer_Literal (Loc, | |
8271 | Intval => String_Literal_Length (E1)); | |
8272 | ||
00c403ee | 8273 | elsif SE /= Standard_Standard |
8274 | and then Ekind (Scope (SE)) = E_Protected_Type | |
8275 | and then Has_Discriminants (Scope (SE)) | |
8276 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 8277 | and then not Inside_Init_Proc |
8278 | then | |
ee6ba406 | 8279 | -- If the type whose length is needed is a private component |
8280 | -- constrained by a discriminant, we must expand the 'Length | |
8281 | -- attribute into an explicit computation, using the discriminal | |
8282 | -- of the current protected operation. This is because the actual | |
8283 | -- type of the prival is constructed after the protected opera- | |
8284 | -- tion has been fully expanded. | |
8285 | ||
8286 | declare | |
8287 | Indx_Type : Node_Id; | |
8288 | Lo : Node_Id; | |
8289 | Hi : Node_Id; | |
8290 | Do_Expand : Boolean := False; | |
8291 | ||
8292 | begin | |
8293 | Indx_Type := First_Index (E); | |
8294 | ||
8295 | for J in 1 .. Indx - 1 loop | |
8296 | Next_Index (Indx_Type); | |
8297 | end loop; | |
8298 | ||
2af58f67 | 8299 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 8300 | |
8301 | if Nkind (Lo) = N_Identifier | |
8302 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
8303 | then | |
8304 | Lo := Get_Discriminal (E, Lo); | |
8305 | Do_Expand := True; | |
8306 | end if; | |
8307 | ||
8308 | if Nkind (Hi) = N_Identifier | |
8309 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
8310 | then | |
8311 | Hi := Get_Discriminal (E, Hi); | |
8312 | Do_Expand := True; | |
8313 | end if; | |
8314 | ||
8315 | if Do_Expand then | |
8316 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 8317 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 8318 | end if; |
8319 | ||
8320 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 8321 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 8322 | end if; |
8323 | ||
8324 | N := | |
8325 | Make_Op_Add (Loc, | |
8326 | Left_Opnd => | |
8327 | Make_Op_Subtract (Loc, | |
8328 | Left_Opnd => Hi, | |
8329 | Right_Opnd => Lo), | |
8330 | ||
8331 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
8332 | return N; | |
8333 | ||
8334 | else | |
8335 | N := | |
8336 | Make_Attribute_Reference (Loc, | |
8337 | Attribute_Name => Name_Length, | |
8338 | Prefix => | |
8339 | New_Occurrence_Of (E1, Loc)); | |
8340 | ||
8341 | if Indx > 1 then | |
8342 | Set_Expressions (N, New_List ( | |
8343 | Make_Integer_Literal (Loc, Indx))); | |
8344 | end if; | |
8345 | ||
8346 | return N; | |
8347 | end if; | |
8348 | end; | |
8349 | ||
8350 | else | |
8351 | N := | |
8352 | Make_Attribute_Reference (Loc, | |
8353 | Attribute_Name => Name_Length, | |
8354 | Prefix => | |
8355 | New_Occurrence_Of (E1, Loc)); | |
8356 | ||
8357 | if Indx > 1 then | |
8358 | Set_Expressions (N, New_List ( | |
8359 | Make_Integer_Literal (Loc, Indx))); | |
8360 | end if; | |
8361 | ||
8362 | return N; | |
ee6ba406 | 8363 | end if; |
8364 | end Get_E_Length; | |
8365 | ||
8366 | ------------------ | |
8367 | -- Get_N_Length -- | |
8368 | ------------------ | |
8369 | ||
8370 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
8371 | begin | |
8372 | return | |
8373 | Make_Attribute_Reference (Loc, | |
8374 | Attribute_Name => Name_Length, | |
8375 | Prefix => | |
9dfe12ae | 8376 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8377 | Expressions => New_List ( |
8378 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8379 | end Get_N_Length; |
8380 | ||
8381 | ------------------- | |
8382 | -- Length_E_Cond -- | |
8383 | ------------------- | |
8384 | ||
8385 | function Length_E_Cond | |
8386 | (Exptyp : Entity_Id; | |
8387 | Typ : Entity_Id; | |
314a23b6 | 8388 | Indx : Nat) return Node_Id |
ee6ba406 | 8389 | is |
8390 | begin | |
8391 | return | |
8392 | Make_Op_Ne (Loc, | |
8393 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8394 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 8395 | end Length_E_Cond; |
8396 | ||
8397 | ------------------- | |
8398 | -- Length_N_Cond -- | |
8399 | ------------------- | |
8400 | ||
8401 | function Length_N_Cond | |
8402 | (Expr : Node_Id; | |
8403 | Typ : Entity_Id; | |
314a23b6 | 8404 | Indx : Nat) return Node_Id |
ee6ba406 | 8405 | is |
8406 | begin | |
8407 | return | |
8408 | Make_Op_Ne (Loc, | |
8409 | Left_Opnd => Get_E_Length (Typ, Indx), | |
8410 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 8411 | end Length_N_Cond; |
8412 | ||
feff2f05 | 8413 | ----------------- |
8414 | -- Same_Bounds -- | |
8415 | ----------------- | |
8416 | ||
ee6ba406 | 8417 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
8418 | begin | |
8419 | return | |
8420 | (Nkind (L) = N_Integer_Literal | |
8421 | and then Nkind (R) = N_Integer_Literal | |
8422 | and then Intval (L) = Intval (R)) | |
8423 | ||
8424 | or else | |
8425 | (Is_Entity_Name (L) | |
8426 | and then Ekind (Entity (L)) = E_Constant | |
8427 | and then ((Is_Entity_Name (R) | |
8428 | and then Entity (L) = Entity (R)) | |
8429 | or else | |
8430 | (Nkind (R) = N_Type_Conversion | |
8431 | and then Is_Entity_Name (Expression (R)) | |
8432 | and then Entity (L) = Entity (Expression (R))))) | |
8433 | ||
8434 | or else | |
8435 | (Is_Entity_Name (R) | |
8436 | and then Ekind (Entity (R)) = E_Constant | |
8437 | and then Nkind (L) = N_Type_Conversion | |
8438 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 8439 | and then Entity (R) = Entity (Expression (L))) |
8440 | ||
8441 | or else | |
8442 | (Is_Entity_Name (L) | |
8443 | and then Is_Entity_Name (R) | |
8444 | and then Entity (L) = Entity (R) | |
8445 | and then Ekind (Entity (L)) = E_In_Parameter | |
8446 | and then Inside_Init_Proc); | |
ee6ba406 | 8447 | end Same_Bounds; |
8448 | ||
8449 | -- Start of processing for Selected_Length_Checks | |
8450 | ||
8451 | begin | |
a33565dd | 8452 | if not Expander_Active then |
ee6ba406 | 8453 | return Ret_Result; |
8454 | end if; | |
8455 | ||
8456 | if Target_Typ = Any_Type | |
8457 | or else Target_Typ = Any_Composite | |
8458 | or else Raises_Constraint_Error (Ck_Node) | |
8459 | then | |
8460 | return Ret_Result; | |
8461 | end if; | |
8462 | ||
8463 | if No (Wnode) then | |
8464 | Wnode := Ck_Node; | |
8465 | end if; | |
8466 | ||
8467 | T_Typ := Target_Typ; | |
8468 | ||
8469 | if No (Source_Typ) then | |
8470 | S_Typ := Etype (Ck_Node); | |
8471 | else | |
8472 | S_Typ := Source_Typ; | |
8473 | end if; | |
8474 | ||
8475 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
8476 | return Ret_Result; | |
8477 | end if; | |
8478 | ||
8479 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
8480 | S_Typ := Designated_Type (S_Typ); | |
8481 | T_Typ := Designated_Type (T_Typ); | |
8482 | Do_Access := True; | |
8483 | ||
2af58f67 | 8484 | -- A simple optimization for the null case |
ee6ba406 | 8485 | |
2af58f67 | 8486 | if Known_Null (Ck_Node) then |
ee6ba406 | 8487 | return Ret_Result; |
8488 | end if; | |
8489 | end if; | |
8490 | ||
8491 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
8492 | if Is_Constrained (T_Typ) then | |
8493 | ||
92f1631f | 8494 | -- The checking code to be generated will freeze the corresponding |
8495 | -- array type. However, we must freeze the type now, so that the | |
8496 | -- freeze node does not appear within the generated if expression, | |
8497 | -- but ahead of it. | |
ee6ba406 | 8498 | |
8499 | Freeze_Before (Ck_Node, T_Typ); | |
8500 | ||
8501 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 8502 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 8503 | |
8504 | if Is_Access_Type (Exptyp) then | |
8505 | Exptyp := Designated_Type (Exptyp); | |
8506 | end if; | |
8507 | ||
8508 | -- String_Literal case. This needs to be handled specially be- | |
8509 | -- cause no index types are available for string literals. The | |
8510 | -- condition is simply: | |
8511 | ||
8512 | -- T_Typ'Length = string-literal-length | |
8513 | ||
9dfe12ae | 8514 | if Nkind (Expr_Actual) = N_String_Literal |
8515 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
8516 | then | |
ee6ba406 | 8517 | Cond := |
8518 | Make_Op_Ne (Loc, | |
8519 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
8520 | Right_Opnd => | |
8521 | Make_Integer_Literal (Loc, | |
8522 | Intval => | |
8523 | String_Literal_Length (Etype (Expr_Actual)))); | |
8524 | ||
8525 | -- General array case. Here we have a usable actual subtype for | |
8526 | -- the expression, and the condition is built from the two types | |
8527 | -- (Do_Length): | |
8528 | ||
8529 | -- T_Typ'Length /= Exptyp'Length or else | |
8530 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
8531 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
8532 | -- ... | |
8533 | ||
8534 | elsif Is_Constrained (Exptyp) then | |
8535 | declare | |
9dfe12ae | 8536 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
8537 | ||
8538 | L_Index : Node_Id; | |
8539 | R_Index : Node_Id; | |
8540 | L_Low : Node_Id; | |
8541 | L_High : Node_Id; | |
8542 | R_Low : Node_Id; | |
8543 | R_High : Node_Id; | |
ee6ba406 | 8544 | L_Length : Uint; |
8545 | R_Length : Uint; | |
9dfe12ae | 8546 | Ref_Node : Node_Id; |
ee6ba406 | 8547 | |
8548 | begin | |
feff2f05 | 8549 | -- At the library level, we need to ensure that the type of |
8550 | -- the object is elaborated before the check itself is | |
8551 | -- emitted. This is only done if the object is in the | |
8552 | -- current compilation unit, otherwise the type is frozen | |
8553 | -- and elaborated in its unit. | |
9dfe12ae | 8554 | |
8555 | if Is_Itype (Exptyp) | |
8556 | and then | |
8557 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
8558 | and then | |
8559 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 8560 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 8561 | then |
8562 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
8563 | Set_Itype (Ref_Node, Exptyp); | |
8564 | Insert_Action (Ck_Node, Ref_Node); | |
8565 | end if; | |
8566 | ||
ee6ba406 | 8567 | L_Index := First_Index (T_Typ); |
8568 | R_Index := First_Index (Exptyp); | |
8569 | ||
8570 | for Indx in 1 .. Ndims loop | |
8571 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 8572 | or else |
8573 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 8574 | then |
8575 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
8576 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
8577 | ||
8578 | -- Deal with compile time length check. Note that we | |
8579 | -- skip this in the access case, because the access | |
8580 | -- value may be null, so we cannot know statically. | |
8581 | ||
8582 | if not Do_Access | |
8583 | and then Compile_Time_Known_Value (L_Low) | |
8584 | and then Compile_Time_Known_Value (L_High) | |
8585 | and then Compile_Time_Known_Value (R_Low) | |
8586 | and then Compile_Time_Known_Value (R_High) | |
8587 | then | |
8588 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
8589 | L_Length := Expr_Value (L_High) - | |
8590 | Expr_Value (L_Low) + 1; | |
8591 | else | |
8592 | L_Length := UI_From_Int (0); | |
8593 | end if; | |
8594 | ||
8595 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
8596 | R_Length := Expr_Value (R_High) - | |
8597 | Expr_Value (R_Low) + 1; | |
8598 | else | |
8599 | R_Length := UI_From_Int (0); | |
8600 | end if; | |
8601 | ||
8602 | if L_Length > R_Length then | |
8603 | Add_Check | |
8604 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8605 | (Wnode, "too few elements for}??", T_Typ)); |
ee6ba406 | 8606 | |
8607 | elsif L_Length < R_Length then | |
8608 | Add_Check | |
8609 | (Compile_Time_Constraint_Error | |
cb97ae5c | 8610 | (Wnode, "too many elements for}??", T_Typ)); |
ee6ba406 | 8611 | end if; |
8612 | ||
8613 | -- The comparison for an individual index subtype | |
8614 | -- is omitted if the corresponding index subtypes | |
8615 | -- statically match, since the result is known to | |
8616 | -- be true. Note that this test is worth while even | |
8617 | -- though we do static evaluation, because non-static | |
8618 | -- subtypes can statically match. | |
8619 | ||
8620 | elsif not | |
8621 | Subtypes_Statically_Match | |
8622 | (Etype (L_Index), Etype (R_Index)) | |
8623 | ||
8624 | and then not | |
8625 | (Same_Bounds (L_Low, R_Low) | |
8626 | and then Same_Bounds (L_High, R_High)) | |
8627 | then | |
8628 | Evolve_Or_Else | |
8629 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
8630 | end if; | |
8631 | ||
8632 | Next (L_Index); | |
8633 | Next (R_Index); | |
8634 | end if; | |
8635 | end loop; | |
8636 | end; | |
8637 | ||
8638 | -- Handle cases where we do not get a usable actual subtype that | |
8639 | -- is constrained. This happens for example in the function call | |
8640 | -- and explicit dereference cases. In these cases, we have to get | |
8641 | -- the length or range from the expression itself, making sure we | |
8642 | -- do not evaluate it more than once. | |
8643 | ||
8644 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 8645 | -- result of applying Duplicate_Expr to the original tree, forcing |
8646 | -- the result to be a name. | |
ee6ba406 | 8647 | |
8648 | else | |
8649 | declare | |
9dfe12ae | 8650 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 8651 | |
8652 | begin | |
8653 | -- Build the condition for the explicit dereference case | |
8654 | ||
8655 | for Indx in 1 .. Ndims loop | |
8656 | Evolve_Or_Else | |
8657 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
8658 | end loop; | |
8659 | end; | |
8660 | end if; | |
8661 | end if; | |
8662 | end if; | |
8663 | ||
8664 | -- Construct the test and insert into the tree | |
8665 | ||
8666 | if Present (Cond) then | |
8667 | if Do_Access then | |
8668 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
8669 | end if; | |
8670 | ||
f15731c4 | 8671 | Add_Check |
8672 | (Make_Raise_Constraint_Error (Loc, | |
8673 | Condition => Cond, | |
8674 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 8675 | end if; |
8676 | ||
8677 | return Ret_Result; | |
ee6ba406 | 8678 | end Selected_Length_Checks; |
8679 | ||
8680 | --------------------------- | |
8681 | -- Selected_Range_Checks -- | |
8682 | --------------------------- | |
8683 | ||
8684 | function Selected_Range_Checks | |
8685 | (Ck_Node : Node_Id; | |
8686 | Target_Typ : Entity_Id; | |
8687 | Source_Typ : Entity_Id; | |
314a23b6 | 8688 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 8689 | is |
8690 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
8691 | S_Typ : Entity_Id; | |
8692 | T_Typ : Entity_Id; | |
8693 | Expr_Actual : Node_Id; | |
8694 | Exptyp : Entity_Id; | |
8695 | Cond : Node_Id := Empty; | |
8696 | Do_Access : Boolean := False; | |
8697 | Wnode : Node_Id := Warn_Node; | |
8698 | Ret_Result : Check_Result := (Empty, Empty); | |
8699 | Num_Checks : Integer := 0; | |
8700 | ||
8701 | procedure Add_Check (N : Node_Id); | |
8702 | -- Adds the action given to Ret_Result if N is non-Empty | |
8703 | ||
8704 | function Discrete_Range_Cond | |
8705 | (Expr : Node_Id; | |
314a23b6 | 8706 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8707 | -- Returns expression to compute: |
8708 | -- Low_Bound (Expr) < Typ'First | |
8709 | -- or else | |
8710 | -- High_Bound (Expr) > Typ'Last | |
8711 | ||
8712 | function Discrete_Expr_Cond | |
8713 | (Expr : Node_Id; | |
314a23b6 | 8714 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 8715 | -- Returns expression to compute: |
8716 | -- Expr < Typ'First | |
8717 | -- or else | |
8718 | -- Expr > Typ'Last | |
8719 | ||
8720 | function Get_E_First_Or_Last | |
3cb12758 | 8721 | (Loc : Source_Ptr; |
8722 | E : Entity_Id; | |
ee6ba406 | 8723 | Indx : Nat; |
314a23b6 | 8724 | Nam : Name_Id) return Node_Id; |
79212397 | 8725 | -- Returns an attribute reference |
ee6ba406 | 8726 | -- E'First or E'Last |
79212397 | 8727 | -- with a source location of Loc. |
f73ee678 | 8728 | -- |
79212397 | 8729 | -- Nam is Name_First or Name_Last, according to which attribute is |
8730 | -- desired. If Indx is non-zero, it is passed as a literal in the | |
8731 | -- Expressions of the attribute reference (identifying the desired | |
8732 | -- array dimension). | |
ee6ba406 | 8733 | |
8734 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
8735 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
8736 | -- Returns expression to compute: | |
9dfe12ae | 8737 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 8738 | |
8739 | function Range_E_Cond | |
8740 | (Exptyp : Entity_Id; | |
8741 | Typ : Entity_Id; | |
8742 | Indx : Nat) | |
8743 | return Node_Id; | |
8744 | -- Returns expression to compute: | |
8745 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
8746 | ||
8747 | function Range_Equal_E_Cond | |
8748 | (Exptyp : Entity_Id; | |
8749 | Typ : Entity_Id; | |
314a23b6 | 8750 | Indx : Nat) return Node_Id; |
ee6ba406 | 8751 | -- Returns expression to compute: |
8752 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
8753 | ||
8754 | function Range_N_Cond | |
8755 | (Expr : Node_Id; | |
8756 | Typ : Entity_Id; | |
314a23b6 | 8757 | Indx : Nat) return Node_Id; |
ee6ba406 | 8758 | -- Return expression to compute: |
8759 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
8760 | ||
8761 | --------------- | |
8762 | -- Add_Check -- | |
8763 | --------------- | |
8764 | ||
8765 | procedure Add_Check (N : Node_Id) is | |
8766 | begin | |
8767 | if Present (N) then | |
8768 | ||
8769 | -- For now, ignore attempt to place more than 2 checks ??? | |
8770 | ||
8771 | if Num_Checks = 2 then | |
8772 | return; | |
8773 | end if; | |
8774 | ||
8775 | pragma Assert (Num_Checks <= 1); | |
8776 | Num_Checks := Num_Checks + 1; | |
8777 | Ret_Result (Num_Checks) := N; | |
8778 | end if; | |
8779 | end Add_Check; | |
8780 | ||
8781 | ------------------------- | |
8782 | -- Discrete_Expr_Cond -- | |
8783 | ------------------------- | |
8784 | ||
8785 | function Discrete_Expr_Cond | |
8786 | (Expr : Node_Id; | |
314a23b6 | 8787 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8788 | is |
8789 | begin | |
8790 | return | |
8791 | Make_Or_Else (Loc, | |
8792 | Left_Opnd => | |
8793 | Make_Op_Lt (Loc, | |
8794 | Left_Opnd => | |
9dfe12ae | 8795 | Convert_To (Base_Type (Typ), |
8796 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8797 | Right_Opnd => |
8798 | Convert_To (Base_Type (Typ), | |
3cb12758 | 8799 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))), |
ee6ba406 | 8800 | |
8801 | Right_Opnd => | |
8802 | Make_Op_Gt (Loc, | |
8803 | Left_Opnd => | |
9dfe12ae | 8804 | Convert_To (Base_Type (Typ), |
8805 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 8806 | Right_Opnd => |
8807 | Convert_To | |
8808 | (Base_Type (Typ), | |
3cb12758 | 8809 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last)))); |
ee6ba406 | 8810 | end Discrete_Expr_Cond; |
8811 | ||
8812 | ------------------------- | |
8813 | -- Discrete_Range_Cond -- | |
8814 | ------------------------- | |
8815 | ||
8816 | function Discrete_Range_Cond | |
8817 | (Expr : Node_Id; | |
314a23b6 | 8818 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 8819 | is |
8820 | LB : Node_Id := Low_Bound (Expr); | |
8821 | HB : Node_Id := High_Bound (Expr); | |
8822 | ||
8823 | Left_Opnd : Node_Id; | |
8824 | Right_Opnd : Node_Id; | |
8825 | ||
8826 | begin | |
8827 | if Nkind (LB) = N_Identifier | |
feff2f05 | 8828 | and then Ekind (Entity (LB)) = E_Discriminant |
8829 | then | |
ee6ba406 | 8830 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
8831 | end if; | |
8832 | ||
ee6ba406 | 8833 | Left_Opnd := |
8834 | Make_Op_Lt (Loc, | |
8835 | Left_Opnd => | |
8836 | Convert_To | |
9dfe12ae | 8837 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 8838 | |
8839 | Right_Opnd => | |
8840 | Convert_To | |
3cb12758 | 8841 | (Base_Type (Typ), |
8842 | Get_E_First_Or_Last (Loc, Typ, 0, Name_First))); | |
ee6ba406 | 8843 | |
ba9b1a39 | 8844 | if Nkind (HB) = N_Identifier |
8845 | and then Ekind (Entity (HB)) = E_Discriminant | |
ee6ba406 | 8846 | then |
ba9b1a39 | 8847 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
ee6ba406 | 8848 | end if; |
8849 | ||
8850 | Right_Opnd := | |
8851 | Make_Op_Gt (Loc, | |
8852 | Left_Opnd => | |
8853 | Convert_To | |
9dfe12ae | 8854 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 8855 | |
8856 | Right_Opnd => | |
8857 | Convert_To | |
8858 | (Base_Type (Typ), | |
3cb12758 | 8859 | Get_E_First_Or_Last (Loc, Typ, 0, Name_Last))); |
ee6ba406 | 8860 | |
8861 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
8862 | end Discrete_Range_Cond; | |
8863 | ||
8864 | ------------------------- | |
8865 | -- Get_E_First_Or_Last -- | |
8866 | ------------------------- | |
8867 | ||
8868 | function Get_E_First_Or_Last | |
3cb12758 | 8869 | (Loc : Source_Ptr; |
8870 | E : Entity_Id; | |
ee6ba406 | 8871 | Indx : Nat; |
314a23b6 | 8872 | Nam : Name_Id) return Node_Id |
ee6ba406 | 8873 | is |
3cb12758 | 8874 | Exprs : List_Id; |
ee6ba406 | 8875 | begin |
3cb12758 | 8876 | if Indx > 0 then |
8877 | Exprs := New_List (Make_Integer_Literal (Loc, UI_From_Int (Indx))); | |
ee6ba406 | 8878 | else |
3cb12758 | 8879 | Exprs := No_List; |
ee6ba406 | 8880 | end if; |
8881 | ||
3cb12758 | 8882 | return Make_Attribute_Reference (Loc, |
8883 | Prefix => New_Occurrence_Of (E, Loc), | |
8884 | Attribute_Name => Nam, | |
8885 | Expressions => Exprs); | |
ee6ba406 | 8886 | end Get_E_First_Or_Last; |
8887 | ||
8888 | ----------------- | |
8889 | -- Get_N_First -- | |
8890 | ----------------- | |
8891 | ||
8892 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
8893 | begin | |
8894 | return | |
8895 | Make_Attribute_Reference (Loc, | |
8896 | Attribute_Name => Name_First, | |
8897 | Prefix => | |
9dfe12ae | 8898 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8899 | Expressions => New_List ( |
8900 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8901 | end Get_N_First; |
8902 | ||
8903 | ---------------- | |
8904 | -- Get_N_Last -- | |
8905 | ---------------- | |
8906 | ||
8907 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
8908 | begin | |
8909 | return | |
8910 | Make_Attribute_Reference (Loc, | |
8911 | Attribute_Name => Name_Last, | |
8912 | Prefix => | |
9dfe12ae | 8913 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 8914 | Expressions => New_List ( |
8915 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 8916 | end Get_N_Last; |
8917 | ||
8918 | ------------------ | |
8919 | -- Range_E_Cond -- | |
8920 | ------------------ | |
8921 | ||
8922 | function Range_E_Cond | |
8923 | (Exptyp : Entity_Id; | |
8924 | Typ : Entity_Id; | |
314a23b6 | 8925 | Indx : Nat) return Node_Id |
ee6ba406 | 8926 | is |
8927 | begin | |
8928 | return | |
8929 | Make_Or_Else (Loc, | |
8930 | Left_Opnd => | |
8931 | Make_Op_Lt (Loc, | |
3cb12758 | 8932 | Left_Opnd => |
8933 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8934 | Right_Opnd => | |
8935 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8936 | |
8937 | Right_Opnd => | |
8938 | Make_Op_Gt (Loc, | |
3cb12758 | 8939 | Left_Opnd => |
8940 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8941 | Right_Opnd => | |
8942 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8943 | end Range_E_Cond; |
8944 | ||
8945 | ------------------------ | |
8946 | -- Range_Equal_E_Cond -- | |
8947 | ------------------------ | |
8948 | ||
8949 | function Range_Equal_E_Cond | |
8950 | (Exptyp : Entity_Id; | |
8951 | Typ : Entity_Id; | |
314a23b6 | 8952 | Indx : Nat) return Node_Id |
ee6ba406 | 8953 | is |
8954 | begin | |
8955 | return | |
8956 | Make_Or_Else (Loc, | |
8957 | Left_Opnd => | |
8958 | Make_Op_Ne (Loc, | |
3cb12758 | 8959 | Left_Opnd => |
8960 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_First), | |
8961 | Right_Opnd => | |
8962 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
8963 | ||
ee6ba406 | 8964 | Right_Opnd => |
8965 | Make_Op_Ne (Loc, | |
3cb12758 | 8966 | Left_Opnd => |
8967 | Get_E_First_Or_Last (Loc, Exptyp, Indx, Name_Last), | |
8968 | Right_Opnd => | |
8969 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8970 | end Range_Equal_E_Cond; |
8971 | ||
8972 | ------------------ | |
8973 | -- Range_N_Cond -- | |
8974 | ------------------ | |
8975 | ||
8976 | function Range_N_Cond | |
8977 | (Expr : Node_Id; | |
8978 | Typ : Entity_Id; | |
314a23b6 | 8979 | Indx : Nat) return Node_Id |
ee6ba406 | 8980 | is |
8981 | begin | |
8982 | return | |
8983 | Make_Or_Else (Loc, | |
8984 | Left_Opnd => | |
8985 | Make_Op_Lt (Loc, | |
3cb12758 | 8986 | Left_Opnd => |
8987 | Get_N_First (Expr, Indx), | |
8988 | Right_Opnd => | |
8989 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_First)), | |
ee6ba406 | 8990 | |
8991 | Right_Opnd => | |
8992 | Make_Op_Gt (Loc, | |
3cb12758 | 8993 | Left_Opnd => |
8994 | Get_N_Last (Expr, Indx), | |
8995 | Right_Opnd => | |
8996 | Get_E_First_Or_Last (Loc, Typ, Indx, Name_Last))); | |
ee6ba406 | 8997 | end Range_N_Cond; |
8998 | ||
8999 | -- Start of processing for Selected_Range_Checks | |
9000 | ||
9001 | begin | |
a33565dd | 9002 | if not Expander_Active then |
ee6ba406 | 9003 | return Ret_Result; |
9004 | end if; | |
9005 | ||
9006 | if Target_Typ = Any_Type | |
9007 | or else Target_Typ = Any_Composite | |
9008 | or else Raises_Constraint_Error (Ck_Node) | |
9009 | then | |
9010 | return Ret_Result; | |
9011 | end if; | |
9012 | ||
9013 | if No (Wnode) then | |
9014 | Wnode := Ck_Node; | |
9015 | end if; | |
9016 | ||
9017 | T_Typ := Target_Typ; | |
9018 | ||
9019 | if No (Source_Typ) then | |
9020 | S_Typ := Etype (Ck_Node); | |
9021 | else | |
9022 | S_Typ := Source_Typ; | |
9023 | end if; | |
9024 | ||
9025 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
9026 | return Ret_Result; | |
9027 | end if; | |
9028 | ||
9029 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
9030 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
9031 | -- in, and since Node can be an N_Range node, it might be invalid. | |
9032 | -- Should there be an assert check somewhere for taking the Etype of | |
9033 | -- an N_Range node ??? | |
9034 | ||
9035 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
9036 | S_Typ := Designated_Type (S_Typ); | |
9037 | T_Typ := Designated_Type (T_Typ); | |
9038 | Do_Access := True; | |
9039 | ||
2af58f67 | 9040 | -- A simple optimization for the null case |
ee6ba406 | 9041 | |
2af58f67 | 9042 | if Known_Null (Ck_Node) then |
ee6ba406 | 9043 | return Ret_Result; |
9044 | end if; | |
9045 | end if; | |
9046 | ||
9047 | -- For an N_Range Node, check for a null range and then if not | |
9048 | -- null generate a range check action. | |
9049 | ||
9050 | if Nkind (Ck_Node) = N_Range then | |
9051 | ||
9052 | -- There's no point in checking a range against itself | |
9053 | ||
9054 | if Ck_Node = Scalar_Range (T_Typ) then | |
9055 | return Ret_Result; | |
9056 | end if; | |
9057 | ||
9058 | declare | |
9059 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
9060 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
eefa141b | 9061 | Known_T_LB : constant Boolean := Compile_Time_Known_Value (T_LB); |
9062 | Known_T_HB : constant Boolean := Compile_Time_Known_Value (T_HB); | |
ee6ba406 | 9063 | |
eefa141b | 9064 | LB : Node_Id := Low_Bound (Ck_Node); |
9065 | HB : Node_Id := High_Bound (Ck_Node); | |
9066 | Known_LB : Boolean; | |
9067 | Known_HB : Boolean; | |
9068 | ||
9069 | Null_Range : Boolean; | |
ee6ba406 | 9070 | Out_Of_Range_L : Boolean; |
9071 | Out_Of_Range_H : Boolean; | |
9072 | ||
9073 | begin | |
eefa141b | 9074 | -- Compute what is known at compile time |
9075 | ||
9076 | if Known_T_LB and Known_T_HB then | |
9077 | if Compile_Time_Known_Value (LB) then | |
9078 | Known_LB := True; | |
9079 | ||
9080 | -- There's no point in checking that a bound is within its | |
9081 | -- own range so pretend that it is known in this case. First | |
9082 | -- deal with low bound. | |
9083 | ||
9084 | elsif Ekind (Etype (LB)) = E_Signed_Integer_Subtype | |
9085 | and then Scalar_Range (Etype (LB)) = Scalar_Range (T_Typ) | |
9086 | then | |
9087 | LB := T_LB; | |
9088 | Known_LB := True; | |
9089 | ||
9090 | else | |
9091 | Known_LB := False; | |
9092 | end if; | |
9093 | ||
9094 | -- Likewise for the high bound | |
9095 | ||
9096 | if Compile_Time_Known_Value (HB) then | |
9097 | Known_HB := True; | |
9098 | ||
9099 | elsif Ekind (Etype (HB)) = E_Signed_Integer_Subtype | |
9100 | and then Scalar_Range (Etype (HB)) = Scalar_Range (T_Typ) | |
9101 | then | |
9102 | HB := T_HB; | |
9103 | Known_HB := True; | |
eefa141b | 9104 | else |
9105 | Known_HB := False; | |
9106 | end if; | |
9107 | end if; | |
9108 | ||
9109 | -- Check for case where everything is static and we can do the | |
9110 | -- check at compile time. This is skipped if we have an access | |
9111 | -- type, since the access value may be null. | |
9112 | ||
9113 | -- ??? This code can be improved since you only need to know that | |
9114 | -- the two respective bounds (LB & T_LB or HB & T_HB) are known at | |
9115 | -- compile time to emit pertinent messages. | |
9116 | ||
9117 | if Known_T_LB and Known_T_HB and Known_LB and Known_HB | |
9118 | and not Do_Access | |
ee6ba406 | 9119 | then |
9120 | -- Floating-point case | |
9121 | ||
9122 | if Is_Floating_Point_Type (S_Typ) then | |
9123 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
9124 | Out_Of_Range_L := | |
9125 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
eefa141b | 9126 | or else |
ee6ba406 | 9127 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); |
9128 | ||
9129 | Out_Of_Range_H := | |
9130 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
eefa141b | 9131 | or else |
ee6ba406 | 9132 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); |
9133 | ||
9134 | -- Fixed or discrete type case | |
9135 | ||
9136 | else | |
9137 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
9138 | Out_Of_Range_L := | |
9139 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
eefa141b | 9140 | or else |
ee6ba406 | 9141 | (Expr_Value (LB) > Expr_Value (T_HB)); |
9142 | ||
9143 | Out_Of_Range_H := | |
9144 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
eefa141b | 9145 | or else |
ee6ba406 | 9146 | (Expr_Value (HB) < Expr_Value (T_LB)); |
9147 | end if; | |
9148 | ||
9149 | if not Null_Range then | |
9150 | if Out_Of_Range_L then | |
9151 | if No (Warn_Node) then | |
9152 | Add_Check | |
9153 | (Compile_Time_Constraint_Error | |
9154 | (Low_Bound (Ck_Node), | |
cb97ae5c | 9155 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9156 | |
9157 | else | |
9158 | Add_Check | |
9159 | (Compile_Time_Constraint_Error | |
9160 | (Wnode, | |
cb97ae5c | 9161 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 9162 | end if; |
9163 | end if; | |
9164 | ||
9165 | if Out_Of_Range_H then | |
9166 | if No (Warn_Node) then | |
9167 | Add_Check | |
9168 | (Compile_Time_Constraint_Error | |
9169 | (High_Bound (Ck_Node), | |
cb97ae5c | 9170 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9171 | |
9172 | else | |
9173 | Add_Check | |
9174 | (Compile_Time_Constraint_Error | |
9175 | (Wnode, | |
cb97ae5c | 9176 | "static range out of bounds of}??", T_Typ)); |
ee6ba406 | 9177 | end if; |
9178 | end if; | |
ee6ba406 | 9179 | end if; |
9180 | ||
9181 | else | |
9182 | declare | |
9183 | LB : Node_Id := Low_Bound (Ck_Node); | |
9184 | HB : Node_Id := High_Bound (Ck_Node); | |
9185 | ||
9186 | begin | |
feff2f05 | 9187 | -- If either bound is a discriminant and we are within the |
9188 | -- record declaration, it is a use of the discriminant in a | |
9189 | -- constraint of a component, and nothing can be checked | |
9190 | -- here. The check will be emitted within the init proc. | |
9191 | -- Before then, the discriminal has no real meaning. | |
9192 | -- Similarly, if the entity is a discriminal, there is no | |
9193 | -- check to perform yet. | |
9194 | ||
9195 | -- The same holds within a discriminated synchronized type, | |
9196 | -- where the discriminant may constrain a component or an | |
9197 | -- entry family. | |
ee6ba406 | 9198 | |
9199 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 9200 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 9201 | then |
0577b0b1 | 9202 | if Current_Scope = Scope (Entity (LB)) |
9203 | or else Is_Concurrent_Type (Current_Scope) | |
9204 | or else Ekind (Entity (LB)) /= E_Discriminant | |
9205 | then | |
ee6ba406 | 9206 | return Ret_Result; |
9207 | else | |
9208 | LB := | |
9209 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
9210 | end if; | |
9211 | end if; | |
9212 | ||
9213 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 9214 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 9215 | then |
0577b0b1 | 9216 | if Current_Scope = Scope (Entity (HB)) |
9217 | or else Is_Concurrent_Type (Current_Scope) | |
9218 | or else Ekind (Entity (HB)) /= E_Discriminant | |
9219 | then | |
ee6ba406 | 9220 | return Ret_Result; |
9221 | else | |
9222 | HB := | |
9223 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
9224 | end if; | |
9225 | end if; | |
9226 | ||
9227 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
9228 | Set_Paren_Count (Cond, 1); | |
9229 | ||
9230 | Cond := | |
9231 | Make_And_Then (Loc, | |
9232 | Left_Opnd => | |
9233 | Make_Op_Ge (Loc, | |
86d32751 | 9234 | Left_Opnd => |
9235 | Convert_To (Base_Type (Etype (HB)), | |
9236 | Duplicate_Subexpr_No_Checks (HB)), | |
9237 | Right_Opnd => | |
9238 | Convert_To (Base_Type (Etype (LB)), | |
9239 | Duplicate_Subexpr_No_Checks (LB))), | |
ee6ba406 | 9240 | Right_Opnd => Cond); |
9241 | end; | |
ee6ba406 | 9242 | end if; |
9243 | end; | |
9244 | ||
9245 | elsif Is_Scalar_Type (S_Typ) then | |
9246 | ||
9247 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
9248 | -- except the above simply sets a flag in the node and lets | |
9249 | -- gigi generate the check base on the Etype of the expression. | |
9250 | -- Sometimes, however we want to do a dynamic check against an | |
9251 | -- arbitrary target type, so we do that here. | |
9252 | ||
9253 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
9254 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9255 | ||
9256 | -- For literals, we can tell if the constraint error will be | |
9257 | -- raised at compile time, so we never need a dynamic check, but | |
9258 | -- if the exception will be raised, then post the usual warning, | |
9259 | -- and replace the literal with a raise constraint error | |
9260 | -- expression. As usual, skip this for access types | |
9261 | ||
20cf157b | 9262 | elsif Compile_Time_Known_Value (Ck_Node) and then not Do_Access then |
ee6ba406 | 9263 | declare |
9264 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
9265 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
9266 | ||
9267 | Out_Of_Range : Boolean; | |
9268 | Static_Bounds : constant Boolean := | |
b6341c67 | 9269 | Compile_Time_Known_Value (LB) |
9270 | and Compile_Time_Known_Value (UB); | |
ee6ba406 | 9271 | |
9272 | begin | |
9273 | -- Following range tests should use Sem_Eval routine ??? | |
9274 | ||
9275 | if Static_Bounds then | |
9276 | if Is_Floating_Point_Type (S_Typ) then | |
9277 | Out_Of_Range := | |
9278 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
9279 | or else | |
9280 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
9281 | ||
eefa141b | 9282 | -- Fixed or discrete type |
9283 | ||
9284 | else | |
ee6ba406 | 9285 | Out_Of_Range := |
9286 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
9287 | or else | |
9288 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
9289 | end if; | |
9290 | ||
eefa141b | 9291 | -- Bounds of the type are static and the literal is out of |
9292 | -- range so output a warning message. | |
ee6ba406 | 9293 | |
9294 | if Out_Of_Range then | |
9295 | if No (Warn_Node) then | |
9296 | Add_Check | |
9297 | (Compile_Time_Constraint_Error | |
9298 | (Ck_Node, | |
cb97ae5c | 9299 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9300 | |
9301 | else | |
9302 | Add_Check | |
9303 | (Compile_Time_Constraint_Error | |
9304 | (Wnode, | |
cb97ae5c | 9305 | "static value out of range of}??", T_Typ)); |
ee6ba406 | 9306 | end if; |
9307 | end if; | |
9308 | ||
9309 | else | |
9310 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
9311 | end if; | |
9312 | end; | |
9313 | ||
9314 | -- Here for the case of a non-static expression, we need a runtime | |
9315 | -- check unless the source type range is guaranteed to be in the | |
9316 | -- range of the target type. | |
9317 | ||
9318 | else | |
7a1dabb3 | 9319 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 9320 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
9321 | end if; | |
9322 | end if; | |
9323 | end if; | |
9324 | ||
9325 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
9326 | if Is_Constrained (T_Typ) then | |
9327 | ||
9328 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
9329 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
9330 | ||
9331 | if Is_Access_Type (Exptyp) then | |
9332 | Exptyp := Designated_Type (Exptyp); | |
9333 | end if; | |
9334 | ||
9335 | -- String_Literal case. This needs to be handled specially be- | |
9336 | -- cause no index types are available for string literals. The | |
9337 | -- condition is simply: | |
9338 | ||
9339 | -- T_Typ'Length = string-literal-length | |
9340 | ||
9341 | if Nkind (Expr_Actual) = N_String_Literal then | |
9342 | null; | |
9343 | ||
9344 | -- General array case. Here we have a usable actual subtype for | |
9345 | -- the expression, and the condition is built from the two types | |
9346 | ||
9347 | -- T_Typ'First < Exptyp'First or else | |
9348 | -- T_Typ'Last > Exptyp'Last or else | |
9349 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
9350 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
9351 | -- ... | |
9352 | ||
9353 | elsif Is_Constrained (Exptyp) then | |
9354 | declare | |
9dfe12ae | 9355 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
9356 | ||
ee6ba406 | 9357 | L_Index : Node_Id; |
9358 | R_Index : Node_Id; | |
ee6ba406 | 9359 | |
9360 | begin | |
9361 | L_Index := First_Index (T_Typ); | |
9362 | R_Index := First_Index (Exptyp); | |
9363 | ||
9364 | for Indx in 1 .. Ndims loop | |
9365 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 9366 | or else |
9367 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 9368 | then |
ee6ba406 | 9369 | -- Deal with compile time length check. Note that we |
9370 | -- skip this in the access case, because the access | |
9371 | -- value may be null, so we cannot know statically. | |
9372 | ||
9373 | if not | |
9374 | Subtypes_Statically_Match | |
9375 | (Etype (L_Index), Etype (R_Index)) | |
9376 | then | |
9377 | -- If the target type is constrained then we | |
9378 | -- have to check for exact equality of bounds | |
9379 | -- (required for qualified expressions). | |
9380 | ||
9381 | if Is_Constrained (T_Typ) then | |
9382 | Evolve_Or_Else | |
9383 | (Cond, | |
9384 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 9385 | else |
9386 | Evolve_Or_Else | |
9387 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
9388 | end if; | |
9389 | end if; | |
9390 | ||
9391 | Next (L_Index); | |
9392 | Next (R_Index); | |
ee6ba406 | 9393 | end if; |
9394 | end loop; | |
9395 | end; | |
9396 | ||
9397 | -- Handle cases where we do not get a usable actual subtype that | |
9398 | -- is constrained. This happens for example in the function call | |
9399 | -- and explicit dereference cases. In these cases, we have to get | |
9400 | -- the length or range from the expression itself, making sure we | |
9401 | -- do not evaluate it more than once. | |
9402 | ||
9403 | -- Here Ck_Node is the original expression, or more properly the | |
9404 | -- result of applying Duplicate_Expr to the original tree, | |
9405 | -- forcing the result to be a name. | |
9406 | ||
9407 | else | |
9408 | declare | |
9dfe12ae | 9409 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 9410 | |
9411 | begin | |
9412 | -- Build the condition for the explicit dereference case | |
9413 | ||
9414 | for Indx in 1 .. Ndims loop | |
9415 | Evolve_Or_Else | |
9416 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
9417 | end loop; | |
9418 | end; | |
ee6ba406 | 9419 | end if; |
9420 | ||
9421 | else | |
feff2f05 | 9422 | -- For a conversion to an unconstrained array type, generate an |
9423 | -- Action to check that the bounds of the source value are within | |
9424 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
9425 | -- check is needed for a conversion to an access to unconstrained | |
9426 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
9427 | -- of the two access types to statically match. | |
9428 | ||
9429 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
9430 | and then not Do_Access | |
9431 | then | |
ee6ba406 | 9432 | declare |
9433 | Opnd_Index : Node_Id; | |
9434 | Targ_Index : Node_Id; | |
00c403ee | 9435 | Opnd_Range : Node_Id; |
ee6ba406 | 9436 | |
9437 | begin | |
feff2f05 | 9438 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 9439 | Targ_Index := First_Index (T_Typ); |
00c403ee | 9440 | while Present (Opnd_Index) loop |
9441 | ||
9442 | -- If the index is a range, use its bounds. If it is an | |
9443 | -- entity (as will be the case if it is a named subtype | |
9444 | -- or an itype created for a slice) retrieve its range. | |
9445 | ||
9446 | if Is_Entity_Name (Opnd_Index) | |
9447 | and then Is_Type (Entity (Opnd_Index)) | |
9448 | then | |
9449 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
9450 | else | |
9451 | Opnd_Range := Opnd_Index; | |
9452 | end if; | |
9453 | ||
9454 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 9455 | if Is_In_Range |
9456 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
9457 | Assume_Valid => True) | |
ee6ba406 | 9458 | and then |
9459 | Is_In_Range | |
9c486805 | 9460 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9461 | Assume_Valid => True) | |
ee6ba406 | 9462 | then |
9463 | null; | |
9464 | ||
feff2f05 | 9465 | -- If null range, no check needed |
f2a06be9 | 9466 | |
9dfe12ae | 9467 | elsif |
00c403ee | 9468 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 9469 | and then |
00c403ee | 9470 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 9471 | and then |
00c403ee | 9472 | Expr_Value (High_Bound (Opnd_Range)) < |
9473 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 9474 | then |
9475 | null; | |
9476 | ||
ee6ba406 | 9477 | elsif Is_Out_Of_Range |
9c486805 | 9478 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
9479 | Assume_Valid => True) | |
ee6ba406 | 9480 | or else |
9481 | Is_Out_Of_Range | |
9c486805 | 9482 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
9483 | Assume_Valid => True) | |
ee6ba406 | 9484 | then |
9485 | Add_Check | |
9486 | (Compile_Time_Constraint_Error | |
cb97ae5c | 9487 | (Wnode, "value out of range of}??", T_Typ)); |
ee6ba406 | 9488 | |
9489 | else | |
9490 | Evolve_Or_Else | |
9491 | (Cond, | |
9492 | Discrete_Range_Cond | |
00c403ee | 9493 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 9494 | end if; |
9495 | end if; | |
9496 | ||
9497 | Next_Index (Opnd_Index); | |
9498 | Next_Index (Targ_Index); | |
9499 | end loop; | |
9500 | end; | |
9501 | end if; | |
9502 | end if; | |
9503 | end if; | |
9504 | ||
9505 | -- Construct the test and insert into the tree | |
9506 | ||
9507 | if Present (Cond) then | |
9508 | if Do_Access then | |
9509 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
9510 | end if; | |
9511 | ||
f15731c4 | 9512 | Add_Check |
9513 | (Make_Raise_Constraint_Error (Loc, | |
eefa141b | 9514 | Condition => Cond, |
9515 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 9516 | end if; |
9517 | ||
9518 | return Ret_Result; | |
ee6ba406 | 9519 | end Selected_Range_Checks; |
9520 | ||
9521 | ------------------------------- | |
9522 | -- Storage_Checks_Suppressed -- | |
9523 | ------------------------------- | |
9524 | ||
9525 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9526 | begin | |
9dfe12ae | 9527 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
9528 | return Is_Check_Suppressed (E, Storage_Check); | |
9529 | else | |
fafc6b97 | 9530 | return Scope_Suppress.Suppress (Storage_Check); |
9dfe12ae | 9531 | end if; |
ee6ba406 | 9532 | end Storage_Checks_Suppressed; |
9533 | ||
9534 | --------------------------- | |
9535 | -- Tag_Checks_Suppressed -- | |
9536 | --------------------------- | |
9537 | ||
9538 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9539 | begin | |
89f1e35c | 9540 | if Present (E) |
9541 | and then Checks_May_Be_Suppressed (E) | |
9542 | then | |
9543 | return Is_Check_Suppressed (E, Tag_Check); | |
20cf157b | 9544 | else |
9545 | return Scope_Suppress.Suppress (Tag_Check); | |
9dfe12ae | 9546 | end if; |
ee6ba406 | 9547 | end Tag_Checks_Suppressed; |
9548 | ||
7b8fa048 | 9549 | --------------------------------------- |
9550 | -- Validate_Alignment_Check_Warnings -- | |
9551 | --------------------------------------- | |
9552 | ||
9553 | procedure Validate_Alignment_Check_Warnings is | |
9554 | begin | |
9555 | for J in Alignment_Warnings.First .. Alignment_Warnings.Last loop | |
9556 | declare | |
9557 | AWR : Alignment_Warnings_Record | |
9558 | renames Alignment_Warnings.Table (J); | |
9559 | begin | |
9560 | if Known_Alignment (AWR.E) | |
9561 | and then AWR.A mod Alignment (AWR.E) = 0 | |
9562 | then | |
9563 | Delete_Warning_And_Continuations (AWR.W); | |
9564 | end if; | |
9565 | end; | |
9566 | end loop; | |
9567 | end Validate_Alignment_Check_Warnings; | |
9568 | ||
0577b0b1 | 9569 | -------------------------- |
9570 | -- Validity_Check_Range -- | |
9571 | -------------------------- | |
9572 | ||
9573 | procedure Validity_Check_Range (N : Node_Id) is | |
9574 | begin | |
9575 | if Validity_Checks_On and Validity_Check_Operands then | |
9576 | if Nkind (N) = N_Range then | |
9577 | Ensure_Valid (Low_Bound (N)); | |
9578 | Ensure_Valid (High_Bound (N)); | |
9579 | end if; | |
9580 | end if; | |
9581 | end Validity_Check_Range; | |
9582 | ||
9583 | -------------------------------- | |
9584 | -- Validity_Checks_Suppressed -- | |
9585 | -------------------------------- | |
9586 | ||
9587 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
9588 | begin | |
9589 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
9590 | return Is_Check_Suppressed (E, Validity_Check); | |
9591 | else | |
fafc6b97 | 9592 | return Scope_Suppress.Suppress (Validity_Check); |
0577b0b1 | 9593 | end if; |
9594 | end Validity_Checks_Suppressed; | |
9595 | ||
ee6ba406 | 9596 | end Checks; |