]>
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 | -- -- | |
ce7498d3 | 9 | -- Copyright (C) 1992-2008, Free Software Foundation, Inc. -- |
ee6ba406 | 10 | -- -- |
11 | -- GNAT is free software; you can redistribute it and/or modify it under -- | |
12 | -- terms of the GNU General Public License as published by the Free Soft- -- | |
80df182a | 13 | -- ware Foundation; either version 3, or (at your option) any later ver- -- |
ee6ba406 | 14 | -- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
15 | -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- | |
16 | -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License -- | |
17 | -- for more details. You should have received a copy of the GNU General -- | |
80df182a | 18 | -- Public License distributed with GNAT; see file COPYING3. If not, go to -- |
19 | -- http://www.gnu.org/licenses for a complete copy of the license. -- | |
ee6ba406 | 20 | -- -- |
21 | -- GNAT was originally developed by the GNAT team at New York University. -- | |
e78e8c8e | 22 | -- Extensive contributions were provided by Ada Core Technologies Inc. -- |
ee6ba406 | 23 | -- -- |
24 | ------------------------------------------------------------------------------ | |
25 | ||
26 | with Atree; use Atree; | |
27 | with Debug; use Debug; | |
28 | with Einfo; use Einfo; | |
29 | with Errout; use Errout; | |
30 | with Exp_Ch2; use Exp_Ch2; | |
00c403ee | 31 | with Exp_Ch11; use Exp_Ch11; |
05fcfafb | 32 | with Exp_Pakd; use Exp_Pakd; |
ee6ba406 | 33 | with Exp_Util; use Exp_Util; |
34 | with Elists; use Elists; | |
5329ca64 | 35 | with Eval_Fat; use Eval_Fat; |
ee6ba406 | 36 | with Freeze; use Freeze; |
9dfe12ae | 37 | with Lib; use Lib; |
ee6ba406 | 38 | with Nlists; use Nlists; |
39 | with Nmake; use Nmake; | |
40 | with Opt; use Opt; | |
9dfe12ae | 41 | with Output; use Output; |
c2b56224 | 42 | with Restrict; use Restrict; |
1e16c51c | 43 | with Rident; use Rident; |
ee6ba406 | 44 | with Rtsfind; use Rtsfind; |
45 | with Sem; use Sem; | |
46 | with Sem_Eval; use Sem_Eval; | |
00f91aef | 47 | with Sem_Ch3; use Sem_Ch3; |
9dfe12ae | 48 | with Sem_Ch8; use Sem_Ch8; |
ee6ba406 | 49 | with Sem_Res; use Sem_Res; |
50 | with Sem_Util; use Sem_Util; | |
51 | with Sem_Warn; use Sem_Warn; | |
52 | with Sinfo; use Sinfo; | |
9dfe12ae | 53 | with Sinput; use Sinput; |
ee6ba406 | 54 | with Snames; use Snames; |
9dfe12ae | 55 | with Sprint; use Sprint; |
ee6ba406 | 56 | with Stand; use Stand; |
f15731c4 | 57 | with Targparm; use Targparm; |
ee6ba406 | 58 | with Tbuild; use Tbuild; |
59 | with Ttypes; use Ttypes; | |
60 | with Urealp; use Urealp; | |
61 | with Validsw; use Validsw; | |
62 | ||
63 | package body Checks is | |
64 | ||
65 | -- General note: many of these routines are concerned with generating | |
66 | -- checking code to make sure that constraint error is raised at runtime. | |
67 | -- Clearly this code is only needed if the expander is active, since | |
68 | -- otherwise we will not be generating code or going into the runtime | |
69 | -- execution anyway. | |
70 | ||
71 | -- We therefore disconnect most of these checks if the expander is | |
72 | -- inactive. This has the additional benefit that we do not need to | |
73 | -- worry about the tree being messed up by previous errors (since errors | |
74 | -- turn off expansion anyway). | |
75 | ||
76 | -- There are a few exceptions to the above rule. For instance routines | |
77 | -- such as Apply_Scalar_Range_Check that do not insert any code can be | |
78 | -- safely called even when the Expander is inactive (but Errors_Detected | |
79 | -- is 0). The benefit of executing this code when expansion is off, is | |
80 | -- the ability to emit constraint error warning for static expressions | |
81 | -- even when we are not generating code. | |
82 | ||
9dfe12ae | 83 | ------------------------------------- |
84 | -- Suppression of Redundant Checks -- | |
85 | ------------------------------------- | |
86 | ||
87 | -- This unit implements a limited circuit for removal of redundant | |
88 | -- checks. The processing is based on a tracing of simple sequential | |
89 | -- flow. For any sequence of statements, we save expressions that are | |
90 | -- marked to be checked, and then if the same expression appears later | |
91 | -- with the same check, then under certain circumstances, the second | |
92 | -- check can be suppressed. | |
93 | ||
94 | -- Basically, we can suppress the check if we know for certain that | |
95 | -- the previous expression has been elaborated (together with its | |
96 | -- check), and we know that the exception frame is the same, and that | |
97 | -- nothing has happened to change the result of the exception. | |
98 | ||
99 | -- Let us examine each of these three conditions in turn to describe | |
100 | -- how we ensure that this condition is met. | |
101 | ||
102 | -- First, we need to know for certain that the previous expression has | |
103 | -- been executed. This is done principly by the mechanism of calling | |
104 | -- Conditional_Statements_Begin at the start of any statement sequence | |
105 | -- and Conditional_Statements_End at the end. The End call causes all | |
106 | -- checks remembered since the Begin call to be discarded. This does | |
107 | -- miss a few cases, notably the case of a nested BEGIN-END block with | |
108 | -- no exception handlers. But the important thing is to be conservative. | |
109 | -- The other protection is that all checks are discarded if a label | |
110 | -- is encountered, since then the assumption of sequential execution | |
111 | -- is violated, and we don't know enough about the flow. | |
112 | ||
113 | -- Second, we need to know that the exception frame is the same. We | |
114 | -- do this by killing all remembered checks when we enter a new frame. | |
115 | -- Again, that's over-conservative, but generally the cases we can help | |
116 | -- with are pretty local anyway (like the body of a loop for example). | |
117 | ||
118 | -- Third, we must be sure to forget any checks which are no longer valid. | |
119 | -- This is done by two mechanisms, first the Kill_Checks_Variable call is | |
120 | -- used to note any changes to local variables. We only attempt to deal | |
121 | -- with checks involving local variables, so we do not need to worry | |
122 | -- about global variables. Second, a call to any non-global procedure | |
123 | -- causes us to abandon all stored checks, since such a all may affect | |
124 | -- the values of any local variables. | |
125 | ||
126 | -- The following define the data structures used to deal with remembering | |
127 | -- checks so that redundant checks can be eliminated as described above. | |
128 | ||
129 | -- Right now, the only expressions that we deal with are of the form of | |
130 | -- simple local objects (either declared locally, or IN parameters) or | |
131 | -- such objects plus/minus a compile time known constant. We can do | |
132 | -- more later on if it seems worthwhile, but this catches many simple | |
133 | -- cases in practice. | |
134 | ||
135 | -- The following record type reflects a single saved check. An entry | |
136 | -- is made in the stack of saved checks if and only if the expression | |
137 | -- has been elaborated with the indicated checks. | |
138 | ||
139 | type Saved_Check is record | |
140 | Killed : Boolean; | |
141 | -- Set True if entry is killed by Kill_Checks | |
142 | ||
143 | Entity : Entity_Id; | |
144 | -- The entity involved in the expression that is checked | |
145 | ||
146 | Offset : Uint; | |
147 | -- A compile time value indicating the result of adding or | |
148 | -- subtracting a compile time value. This value is to be | |
149 | -- added to the value of the Entity. A value of zero is | |
150 | -- used for the case of a simple entity reference. | |
151 | ||
152 | Check_Type : Character; | |
153 | -- This is set to 'R' for a range check (in which case Target_Type | |
154 | -- is set to the target type for the range check) or to 'O' for an | |
155 | -- overflow check (in which case Target_Type is set to Empty). | |
156 | ||
157 | Target_Type : Entity_Id; | |
158 | -- Used only if Do_Range_Check is set. Records the target type for | |
159 | -- the check. We need this, because a check is a duplicate only if | |
160 | -- it has a the same target type (or more accurately one with a | |
161 | -- range that is smaller or equal to the stored target type of a | |
162 | -- saved check). | |
163 | end record; | |
164 | ||
165 | -- The following table keeps track of saved checks. Rather than use an | |
166 | -- extensible table. We just use a table of fixed size, and we discard | |
167 | -- any saved checks that do not fit. That's very unlikely to happen and | |
168 | -- this is only an optimization in any case. | |
169 | ||
170 | Saved_Checks : array (Int range 1 .. 200) of Saved_Check; | |
171 | -- Array of saved checks | |
172 | ||
173 | Num_Saved_Checks : Nat := 0; | |
174 | -- Number of saved checks | |
175 | ||
176 | -- The following stack keeps track of statement ranges. It is treated | |
177 | -- as a stack. When Conditional_Statements_Begin is called, an entry | |
178 | -- is pushed onto this stack containing the value of Num_Saved_Checks | |
179 | -- at the time of the call. Then when Conditional_Statements_End is | |
180 | -- called, this value is popped off and used to reset Num_Saved_Checks. | |
181 | ||
182 | -- Note: again, this is a fixed length stack with a size that should | |
183 | -- always be fine. If the value of the stack pointer goes above the | |
184 | -- limit, then we just forget all saved checks. | |
185 | ||
186 | Saved_Checks_Stack : array (Int range 1 .. 100) of Nat; | |
187 | Saved_Checks_TOS : Nat := 0; | |
188 | ||
189 | ----------------------- | |
190 | -- Local Subprograms -- | |
191 | ----------------------- | |
ee6ba406 | 192 | |
5329ca64 | 193 | procedure Apply_Float_Conversion_Check |
194 | (Ck_Node : Node_Id; | |
195 | Target_Typ : Entity_Id); | |
196 | -- The checks on a conversion from a floating-point type to an integer | |
197 | -- type are delicate. They have to be performed before conversion, they | |
198 | -- have to raise an exception when the operand is a NaN, and rounding must | |
199 | -- be taken into account to determine the safe bounds of the operand. | |
200 | ||
ee6ba406 | 201 | procedure Apply_Selected_Length_Checks |
202 | (Ck_Node : Node_Id; | |
203 | Target_Typ : Entity_Id; | |
204 | Source_Typ : Entity_Id; | |
205 | Do_Static : Boolean); | |
206 | -- This is the subprogram that does all the work for Apply_Length_Check | |
207 | -- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as | |
208 | -- described for the above routines. The Do_Static flag indicates that | |
209 | -- only a static check is to be done. | |
210 | ||
211 | procedure Apply_Selected_Range_Checks | |
212 | (Ck_Node : Node_Id; | |
213 | Target_Typ : Entity_Id; | |
214 | Source_Typ : Entity_Id; | |
215 | Do_Static : Boolean); | |
216 | -- This is the subprogram that does all the work for Apply_Range_Check. | |
217 | -- Expr, Target_Typ and Source_Typ are as described for the above | |
218 | -- routine. The Do_Static flag indicates that only a static check is | |
219 | -- to be done. | |
220 | ||
2af58f67 | 221 | type Check_Type is new Check_Id range Access_Check .. Division_Check; |
13dbf220 | 222 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean; |
223 | -- This function is used to see if an access or division by zero check is | |
224 | -- needed. The check is to be applied to a single variable appearing in the | |
225 | -- source, and N is the node for the reference. If N is not of this form, | |
226 | -- True is returned with no further processing. If N is of the right form, | |
227 | -- then further processing determines if the given Check is needed. | |
228 | -- | |
229 | -- The particular circuit is to see if we have the case of a check that is | |
230 | -- not needed because it appears in the right operand of a short circuited | |
231 | -- conditional where the left operand guards the check. For example: | |
232 | -- | |
233 | -- if Var = 0 or else Q / Var > 12 then | |
234 | -- ... | |
235 | -- end if; | |
236 | -- | |
237 | -- In this example, the division check is not required. At the same time | |
238 | -- we can issue warnings for suspicious use of non-short-circuited forms, | |
239 | -- such as: | |
240 | -- | |
241 | -- if Var = 0 or Q / Var > 12 then | |
242 | -- ... | |
243 | -- end if; | |
244 | ||
9dfe12ae | 245 | procedure Find_Check |
246 | (Expr : Node_Id; | |
247 | Check_Type : Character; | |
248 | Target_Type : Entity_Id; | |
249 | Entry_OK : out Boolean; | |
250 | Check_Num : out Nat; | |
251 | Ent : out Entity_Id; | |
252 | Ofs : out Uint); | |
253 | -- This routine is used by Enable_Range_Check and Enable_Overflow_Check | |
254 | -- to see if a check is of the form for optimization, and if so, to see | |
255 | -- if it has already been performed. Expr is the expression to check, | |
256 | -- and Check_Type is 'R' for a range check, 'O' for an overflow check. | |
257 | -- Target_Type is the target type for a range check, and Empty for an | |
258 | -- overflow check. If the entry is not of the form for optimization, | |
259 | -- then Entry_OK is set to False, and the remaining out parameters | |
260 | -- are undefined. If the entry is OK, then Ent/Ofs are set to the | |
261 | -- entity and offset from the expression. Check_Num is the number of | |
262 | -- a matching saved entry in Saved_Checks, or zero if no such entry | |
263 | -- is located. | |
264 | ||
ee6ba406 | 265 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id; |
266 | -- If a discriminal is used in constraining a prival, Return reference | |
267 | -- to the discriminal of the protected body (which renames the parameter | |
268 | -- of the enclosing protected operation). This clumsy transformation is | |
269 | -- needed because privals are created too late and their actual subtypes | |
270 | -- are not available when analysing the bodies of the protected operations. | |
0577b0b1 | 271 | -- This function is called whenever the bound is an entity and the scope |
272 | -- indicates a protected operation. If the bound is an in-parameter of | |
273 | -- a protected operation that is not a prival, the function returns the | |
274 | -- bound itself. | |
ee6ba406 | 275 | -- To be cleaned up??? |
276 | ||
277 | function Guard_Access | |
278 | (Cond : Node_Id; | |
279 | Loc : Source_Ptr; | |
314a23b6 | 280 | Ck_Node : Node_Id) return Node_Id; |
ee6ba406 | 281 | -- In the access type case, guard the test with a test to ensure |
282 | -- that the access value is non-null, since the checks do not | |
283 | -- not apply to null access values. | |
284 | ||
285 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr); | |
286 | -- Called by Apply_{Length,Range}_Checks to rewrite the tree with the | |
287 | -- Constraint_Error node. | |
288 | ||
0577b0b1 | 289 | function Range_Or_Validity_Checks_Suppressed |
290 | (Expr : Node_Id) return Boolean; | |
291 | -- Returns True if either range or validity checks or both are suppressed | |
292 | -- for the type of the given expression, or, if the expression is the name | |
293 | -- of an entity, if these checks are suppressed for the entity. | |
294 | ||
ee6ba406 | 295 | function Selected_Length_Checks |
296 | (Ck_Node : Node_Id; | |
297 | Target_Typ : Entity_Id; | |
298 | Source_Typ : Entity_Id; | |
314a23b6 | 299 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 300 | -- Like Apply_Selected_Length_Checks, except it doesn't modify |
301 | -- anything, just returns a list of nodes as described in the spec of | |
302 | -- this package for the Range_Check function. | |
303 | ||
304 | function Selected_Range_Checks | |
305 | (Ck_Node : Node_Id; | |
306 | Target_Typ : Entity_Id; | |
307 | Source_Typ : Entity_Id; | |
314a23b6 | 308 | Warn_Node : Node_Id) return Check_Result; |
ee6ba406 | 309 | -- Like Apply_Selected_Range_Checks, except it doesn't modify anything, |
310 | -- just returns a list of nodes as described in the spec of this package | |
311 | -- for the Range_Check function. | |
312 | ||
313 | ------------------------------ | |
314 | -- Access_Checks_Suppressed -- | |
315 | ------------------------------ | |
316 | ||
317 | function Access_Checks_Suppressed (E : Entity_Id) return Boolean is | |
318 | begin | |
9dfe12ae | 319 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
320 | return Is_Check_Suppressed (E, Access_Check); | |
321 | else | |
322 | return Scope_Suppress (Access_Check); | |
323 | end if; | |
ee6ba406 | 324 | end Access_Checks_Suppressed; |
325 | ||
326 | ------------------------------------- | |
327 | -- Accessibility_Checks_Suppressed -- | |
328 | ------------------------------------- | |
329 | ||
330 | function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is | |
331 | begin | |
9dfe12ae | 332 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
333 | return Is_Check_Suppressed (E, Accessibility_Check); | |
334 | else | |
335 | return Scope_Suppress (Accessibility_Check); | |
336 | end if; | |
ee6ba406 | 337 | end Accessibility_Checks_Suppressed; |
338 | ||
00c403ee | 339 | ----------------------------- |
340 | -- Activate_Division_Check -- | |
341 | ----------------------------- | |
342 | ||
343 | procedure Activate_Division_Check (N : Node_Id) is | |
344 | begin | |
345 | Set_Do_Division_Check (N, True); | |
346 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
347 | end Activate_Division_Check; | |
348 | ||
349 | ----------------------------- | |
350 | -- Activate_Overflow_Check -- | |
351 | ----------------------------- | |
352 | ||
353 | procedure Activate_Overflow_Check (N : Node_Id) is | |
354 | begin | |
355 | Set_Do_Overflow_Check (N, True); | |
356 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
357 | end Activate_Overflow_Check; | |
358 | ||
359 | -------------------------- | |
360 | -- Activate_Range_Check -- | |
361 | -------------------------- | |
362 | ||
363 | procedure Activate_Range_Check (N : Node_Id) is | |
364 | begin | |
365 | Set_Do_Range_Check (N, True); | |
366 | Possible_Local_Raise (N, Standard_Constraint_Error); | |
367 | end Activate_Range_Check; | |
368 | ||
0577b0b1 | 369 | --------------------------------- |
370 | -- Alignment_Checks_Suppressed -- | |
371 | --------------------------------- | |
372 | ||
373 | function Alignment_Checks_Suppressed (E : Entity_Id) return Boolean is | |
374 | begin | |
375 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
376 | return Is_Check_Suppressed (E, Alignment_Check); | |
377 | else | |
378 | return Scope_Suppress (Alignment_Check); | |
379 | end if; | |
380 | end Alignment_Checks_Suppressed; | |
381 | ||
ee6ba406 | 382 | ------------------------- |
383 | -- Append_Range_Checks -- | |
384 | ------------------------- | |
385 | ||
386 | procedure Append_Range_Checks | |
387 | (Checks : Check_Result; | |
388 | Stmts : List_Id; | |
389 | Suppress_Typ : Entity_Id; | |
390 | Static_Sloc : Source_Ptr; | |
391 | Flag_Node : Node_Id) | |
392 | is | |
9dfe12ae | 393 | Internal_Flag_Node : constant Node_Id := Flag_Node; |
394 | Internal_Static_Sloc : constant Source_Ptr := Static_Sloc; | |
395 | ||
ee6ba406 | 396 | Checks_On : constant Boolean := |
397 | (not Index_Checks_Suppressed (Suppress_Typ)) | |
398 | or else | |
399 | (not Range_Checks_Suppressed (Suppress_Typ)); | |
400 | ||
401 | begin | |
402 | -- For now we just return if Checks_On is false, however this should | |
403 | -- be enhanced to check for an always True value in the condition | |
404 | -- and to generate a compilation warning??? | |
405 | ||
406 | if not Checks_On then | |
407 | return; | |
408 | end if; | |
409 | ||
410 | for J in 1 .. 2 loop | |
411 | exit when No (Checks (J)); | |
412 | ||
413 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
414 | and then Present (Condition (Checks (J))) | |
415 | then | |
416 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
417 | Append_To (Stmts, Checks (J)); | |
418 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
419 | end if; | |
420 | ||
421 | else | |
422 | Append_To | |
f15731c4 | 423 | (Stmts, |
424 | Make_Raise_Constraint_Error (Internal_Static_Sloc, | |
425 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 426 | end if; |
427 | end loop; | |
428 | end Append_Range_Checks; | |
429 | ||
430 | ------------------------ | |
431 | -- Apply_Access_Check -- | |
432 | ------------------------ | |
433 | ||
434 | procedure Apply_Access_Check (N : Node_Id) is | |
435 | P : constant Node_Id := Prefix (N); | |
436 | ||
437 | begin | |
13dbf220 | 438 | -- We do not need checks if we are not generating code (i.e. the |
439 | -- expander is not active). This is not just an optimization, there | |
440 | -- are cases (e.g. with pragma Debug) where generating the checks | |
441 | -- can cause real trouble). | |
284faf8b | 442 | |
84d0d4a5 | 443 | if not Expander_Active then |
13dbf220 | 444 | return; |
9dfe12ae | 445 | end if; |
ee6ba406 | 446 | |
84d0d4a5 | 447 | -- No check if short circuiting makes check unnecessary |
9dfe12ae | 448 | |
84d0d4a5 | 449 | if not Check_Needed (P, Access_Check) then |
450 | return; | |
ee6ba406 | 451 | end if; |
9dfe12ae | 452 | |
cc60bd16 | 453 | -- No check if accessing the Offset_To_Top component of a dispatch |
454 | -- table. They are safe by construction. | |
455 | ||
456 | if Present (Etype (P)) | |
457 | and then RTU_Loaded (Ada_Tags) | |
458 | and then RTE_Available (RE_Offset_To_Top_Ptr) | |
459 | and then Etype (P) = RTE (RE_Offset_To_Top_Ptr) | |
460 | then | |
461 | return; | |
462 | end if; | |
463 | ||
84d0d4a5 | 464 | -- Otherwise go ahead and install the check |
9dfe12ae | 465 | |
fa7497e8 | 466 | Install_Null_Excluding_Check (P); |
ee6ba406 | 467 | end Apply_Access_Check; |
468 | ||
469 | ------------------------------- | |
470 | -- Apply_Accessibility_Check -- | |
471 | ------------------------------- | |
472 | ||
55dc6dc2 | 473 | procedure Apply_Accessibility_Check |
474 | (N : Node_Id; | |
475 | Typ : Entity_Id; | |
476 | Insert_Node : Node_Id) | |
477 | is | |
ee6ba406 | 478 | Loc : constant Source_Ptr := Sloc (N); |
479 | Param_Ent : constant Entity_Id := Param_Entity (N); | |
480 | Param_Level : Node_Id; | |
481 | Type_Level : Node_Id; | |
482 | ||
483 | begin | |
484 | if Inside_A_Generic then | |
485 | return; | |
486 | ||
487 | -- Only apply the run-time check if the access parameter | |
488 | -- has an associated extra access level parameter and | |
489 | -- when the level of the type is less deep than the level | |
490 | -- of the access parameter. | |
491 | ||
492 | elsif Present (Param_Ent) | |
493 | and then Present (Extra_Accessibility (Param_Ent)) | |
494 | and then UI_Gt (Object_Access_Level (N), | |
495 | Type_Access_Level (Typ)) | |
496 | and then not Accessibility_Checks_Suppressed (Param_Ent) | |
497 | and then not Accessibility_Checks_Suppressed (Typ) | |
498 | then | |
499 | Param_Level := | |
500 | New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc); | |
501 | ||
502 | Type_Level := | |
503 | Make_Integer_Literal (Loc, Type_Access_Level (Typ)); | |
504 | ||
bf3e1520 | 505 | -- Raise Program_Error if the accessibility level of the access |
84d0d4a5 | 506 | -- parameter is deeper than the level of the target access type. |
ee6ba406 | 507 | |
55dc6dc2 | 508 | Insert_Action (Insert_Node, |
ee6ba406 | 509 | Make_Raise_Program_Error (Loc, |
510 | Condition => | |
511 | Make_Op_Gt (Loc, | |
512 | Left_Opnd => Param_Level, | |
f15731c4 | 513 | Right_Opnd => Type_Level), |
514 | Reason => PE_Accessibility_Check_Failed)); | |
ee6ba406 | 515 | |
516 | Analyze_And_Resolve (N); | |
517 | end if; | |
518 | end Apply_Accessibility_Check; | |
519 | ||
0577b0b1 | 520 | -------------------------------- |
521 | -- Apply_Address_Clause_Check -- | |
522 | -------------------------------- | |
523 | ||
524 | procedure Apply_Address_Clause_Check (E : Entity_Id; N : Node_Id) is | |
525 | AC : constant Node_Id := Address_Clause (E); | |
526 | Loc : constant Source_Ptr := Sloc (AC); | |
527 | Typ : constant Entity_Id := Etype (E); | |
528 | Aexp : constant Node_Id := Expression (AC); | |
c2b56224 | 529 | |
c2b56224 | 530 | Expr : Node_Id; |
0577b0b1 | 531 | -- Address expression (not necessarily the same as Aexp, for example |
532 | -- when Aexp is a reference to a constant, in which case Expr gets | |
533 | -- reset to reference the value expression of the constant. | |
534 | ||
535 | Size_Warning_Output : Boolean := False; | |
536 | -- If we output a size warning we set this True, to stop generating | |
537 | -- what is likely to be an unuseful redundant alignment warning. | |
538 | ||
539 | procedure Compile_Time_Bad_Alignment; | |
540 | -- Post error warnings when alignment is known to be incompatible. Note | |
541 | -- that we do not go as far as inserting a raise of Program_Error since | |
542 | -- this is an erroneous case, and it may happen that we are lucky and an | |
543 | -- underaligned address turns out to be OK after all. Also this warning | |
544 | -- is suppressed if we already complained about the size. | |
545 | ||
546 | -------------------------------- | |
547 | -- Compile_Time_Bad_Alignment -- | |
548 | -------------------------------- | |
549 | ||
550 | procedure Compile_Time_Bad_Alignment is | |
551 | begin | |
552 | if not Size_Warning_Output | |
553 | and then Address_Clause_Overlay_Warnings | |
554 | then | |
555 | Error_Msg_FE | |
556 | ("?specified address for& may be inconsistent with alignment ", | |
557 | Aexp, E); | |
558 | Error_Msg_FE | |
2af58f67 | 559 | ("\?program execution may be erroneous (RM 13.3(27))", |
0577b0b1 | 560 | Aexp, E); |
83f8f0a6 | 561 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 562 | end if; |
563 | end Compile_Time_Bad_Alignment; | |
c2b56224 | 564 | |
2af58f67 | 565 | -- Start of processing for Apply_Address_Clause_Check |
5c61a0ff | 566 | |
c2b56224 | 567 | begin |
0577b0b1 | 568 | -- First obtain expression from address clause |
9dfe12ae | 569 | |
0577b0b1 | 570 | Expr := Expression (AC); |
571 | ||
572 | -- The following loop digs for the real expression to use in the check | |
573 | ||
574 | loop | |
575 | -- For constant, get constant expression | |
576 | ||
577 | if Is_Entity_Name (Expr) | |
578 | and then Ekind (Entity (Expr)) = E_Constant | |
579 | then | |
580 | Expr := Constant_Value (Entity (Expr)); | |
581 | ||
582 | -- For unchecked conversion, get result to convert | |
583 | ||
584 | elsif Nkind (Expr) = N_Unchecked_Type_Conversion then | |
585 | Expr := Expression (Expr); | |
586 | ||
587 | -- For (common case) of To_Address call, get argument | |
588 | ||
589 | elsif Nkind (Expr) = N_Function_Call | |
590 | and then Is_Entity_Name (Name (Expr)) | |
591 | and then Is_RTE (Entity (Name (Expr)), RE_To_Address) | |
592 | then | |
593 | Expr := First (Parameter_Associations (Expr)); | |
594 | ||
595 | if Nkind (Expr) = N_Parameter_Association then | |
596 | Expr := Explicit_Actual_Parameter (Expr); | |
597 | end if; | |
598 | ||
599 | -- We finally have the real expression | |
600 | ||
601 | else | |
602 | exit; | |
603 | end if; | |
604 | end loop; | |
605 | ||
606 | -- Output a warning if we have the situation of | |
607 | ||
608 | -- for X'Address use Y'Address | |
609 | ||
610 | -- and X and Y both have known object sizes, and Y is smaller than X | |
611 | ||
612 | if Nkind (Expr) = N_Attribute_Reference | |
613 | and then Attribute_Name (Expr) = Name_Address | |
614 | and then Is_Entity_Name (Prefix (Expr)) | |
9dfe12ae | 615 | then |
0577b0b1 | 616 | declare |
617 | Exp_Ent : constant Entity_Id := Entity (Prefix (Expr)); | |
618 | Obj_Size : Uint := No_Uint; | |
619 | Exp_Size : Uint := No_Uint; | |
620 | ||
621 | begin | |
622 | if Known_Esize (E) then | |
623 | Obj_Size := Esize (E); | |
624 | elsif Known_Esize (Etype (E)) then | |
625 | Obj_Size := Esize (Etype (E)); | |
626 | end if; | |
627 | ||
628 | if Known_Esize (Exp_Ent) then | |
629 | Exp_Size := Esize (Exp_Ent); | |
630 | elsif Known_Esize (Etype (Exp_Ent)) then | |
631 | Exp_Size := Esize (Etype (Exp_Ent)); | |
632 | end if; | |
633 | ||
634 | if Obj_Size /= No_Uint | |
635 | and then Exp_Size /= No_Uint | |
636 | and then Obj_Size > Exp_Size | |
ce7498d3 | 637 | and then not Has_Warnings_Off (E) |
0577b0b1 | 638 | then |
639 | if Address_Clause_Overlay_Warnings then | |
640 | Error_Msg_FE | |
641 | ("?& overlays smaller object", Aexp, E); | |
642 | Error_Msg_FE | |
643 | ("\?program execution may be erroneous", Aexp, E); | |
644 | Size_Warning_Output := True; | |
83f8f0a6 | 645 | Set_Address_Warning_Posted (AC); |
0577b0b1 | 646 | end if; |
647 | end if; | |
648 | end; | |
c2b56224 | 649 | end if; |
650 | ||
0577b0b1 | 651 | -- See if alignment check needed. Note that we never need a check if the |
652 | -- maximum alignment is one, since the check will always succeed. | |
c2b56224 | 653 | |
0577b0b1 | 654 | -- Note: we do not check for checks suppressed here, since that check |
2af58f67 | 655 | -- was done in Sem_Ch13 when the address clause was processed. We are |
0577b0b1 | 656 | -- only called if checks were not suppressed. The reason for this is |
657 | -- that we have to delay the call to Apply_Alignment_Check till freeze | |
658 | -- time (so that all types etc are elaborated), but we have to check | |
659 | -- the status of check suppressing at the point of the address clause. | |
c2b56224 | 660 | |
0577b0b1 | 661 | if No (AC) |
662 | or else not Check_Address_Alignment (AC) | |
663 | or else Maximum_Alignment = 1 | |
c2b56224 | 664 | then |
0577b0b1 | 665 | return; |
c2b56224 | 666 | end if; |
667 | ||
0577b0b1 | 668 | -- See if we know that Expr is a bad alignment at compile time |
c2b56224 | 669 | |
670 | if Compile_Time_Known_Value (Expr) | |
f2a06be9 | 671 | and then (Known_Alignment (E) or else Known_Alignment (Typ)) |
c2b56224 | 672 | then |
f2a06be9 | 673 | declare |
674 | AL : Uint := Alignment (Typ); | |
675 | ||
676 | begin | |
677 | -- The object alignment might be more restrictive than the | |
678 | -- type alignment. | |
679 | ||
680 | if Known_Alignment (E) then | |
681 | AL := Alignment (E); | |
682 | end if; | |
683 | ||
684 | if Expr_Value (Expr) mod AL /= 0 then | |
0577b0b1 | 685 | Compile_Time_Bad_Alignment; |
686 | else | |
687 | return; | |
f2a06be9 | 688 | end if; |
689 | end; | |
c2b56224 | 690 | |
0577b0b1 | 691 | -- If the expression has the form X'Address, then we can find out if |
692 | -- the object X has an alignment that is compatible with the object E. | |
c2b56224 | 693 | |
0577b0b1 | 694 | elsif Nkind (Expr) = N_Attribute_Reference |
695 | and then Attribute_Name (Expr) = Name_Address | |
696 | then | |
697 | declare | |
698 | AR : constant Alignment_Result := | |
699 | Has_Compatible_Alignment (E, Prefix (Expr)); | |
700 | begin | |
701 | if AR = Known_Compatible then | |
702 | return; | |
703 | elsif AR = Known_Incompatible then | |
704 | Compile_Time_Bad_Alignment; | |
705 | end if; | |
706 | end; | |
707 | end if; | |
c2b56224 | 708 | |
0577b0b1 | 709 | -- Here we do not know if the value is acceptable. Stricly we don't have |
710 | -- to do anything, since if the alignment is bad, we have an erroneous | |
711 | -- program. However we are allowed to check for erroneous conditions and | |
712 | -- we decide to do this by default if the check is not suppressed. | |
713 | ||
714 | -- However, don't do the check if elaboration code is unwanted | |
715 | ||
716 | if Restriction_Active (No_Elaboration_Code) then | |
717 | return; | |
718 | ||
719 | -- Generate a check to raise PE if alignment may be inappropriate | |
720 | ||
721 | else | |
722 | -- If the original expression is a non-static constant, use the | |
723 | -- name of the constant itself rather than duplicating its | |
00c403ee | 724 | -- defining expression, which was extracted above. |
0577b0b1 | 725 | |
00c403ee | 726 | -- Note: Expr is empty if the address-clause is applied to in-mode |
727 | -- actuals (allowed by 13.1(22)). | |
728 | ||
729 | if not Present (Expr) | |
730 | or else | |
731 | (Is_Entity_Name (Expression (AC)) | |
732 | and then Ekind (Entity (Expression (AC))) = E_Constant | |
733 | and then Nkind (Parent (Entity (Expression (AC)))) | |
734 | = N_Object_Declaration) | |
0577b0b1 | 735 | then |
736 | Expr := New_Copy_Tree (Expression (AC)); | |
737 | else | |
738 | Remove_Side_Effects (Expr); | |
c2b56224 | 739 | end if; |
c2b56224 | 740 | |
0577b0b1 | 741 | Insert_After_And_Analyze (N, |
742 | Make_Raise_Program_Error (Loc, | |
743 | Condition => | |
744 | Make_Op_Ne (Loc, | |
745 | Left_Opnd => | |
746 | Make_Op_Mod (Loc, | |
747 | Left_Opnd => | |
748 | Unchecked_Convert_To | |
749 | (RTE (RE_Integer_Address), Expr), | |
750 | Right_Opnd => | |
751 | Make_Attribute_Reference (Loc, | |
752 | Prefix => New_Occurrence_Of (E, Loc), | |
753 | Attribute_Name => Name_Alignment)), | |
754 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
755 | Reason => PE_Misaligned_Address_Value), | |
756 | Suppress => All_Checks); | |
757 | return; | |
758 | end if; | |
9dfe12ae | 759 | |
760 | exception | |
0577b0b1 | 761 | -- If we have some missing run time component in configurable run time |
762 | -- mode then just skip the check (it is not required in any case). | |
763 | ||
9dfe12ae | 764 | when RE_Not_Available => |
765 | return; | |
0577b0b1 | 766 | end Apply_Address_Clause_Check; |
c2b56224 | 767 | |
ee6ba406 | 768 | ------------------------------------- |
769 | -- Apply_Arithmetic_Overflow_Check -- | |
770 | ------------------------------------- | |
771 | ||
f40f9731 | 772 | -- This routine is called only if the type is an integer type, and a |
773 | -- software arithmetic overflow check may be needed for op (add, subtract, | |
774 | -- or multiply). This check is performed only if Software_Overflow_Checking | |
775 | -- is enabled and Do_Overflow_Check is set. In this case we expand the | |
776 | -- operation into a more complex sequence of tests that ensures that | |
777 | -- overflow is properly caught. | |
ee6ba406 | 778 | |
779 | procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is | |
780 | Loc : constant Source_Ptr := Sloc (N); | |
f40f9731 | 781 | Typ : Entity_Id := Etype (N); |
782 | Rtyp : Entity_Id := Root_Type (Typ); | |
ee6ba406 | 783 | |
784 | begin | |
f40f9731 | 785 | -- An interesting special case. If the arithmetic operation appears as |
786 | -- the operand of a type conversion: | |
787 | ||
788 | -- type1 (x op y) | |
789 | ||
790 | -- and all the following conditions apply: | |
791 | ||
792 | -- arithmetic operation is for a signed integer type | |
793 | -- target type type1 is a static integer subtype | |
794 | -- range of x and y are both included in the range of type1 | |
795 | -- range of x op y is included in the range of type1 | |
796 | -- size of type1 is at least twice the result size of op | |
797 | ||
798 | -- then we don't do an overflow check in any case, instead we transform | |
799 | -- the operation so that we end up with: | |
800 | ||
801 | -- type1 (type1 (x) op type1 (y)) | |
802 | ||
803 | -- This avoids intermediate overflow before the conversion. It is | |
804 | -- explicitly permitted by RM 3.5.4(24): | |
805 | ||
806 | -- For the execution of a predefined operation of a signed integer | |
807 | -- type, the implementation need not raise Constraint_Error if the | |
808 | -- result is outside the base range of the type, so long as the | |
809 | -- correct result is produced. | |
810 | ||
811 | -- It's hard to imagine that any programmer counts on the exception | |
812 | -- being raised in this case, and in any case it's wrong coding to | |
813 | -- have this expectation, given the RM permission. Furthermore, other | |
814 | -- Ada compilers do allow such out of range results. | |
815 | ||
816 | -- Note that we do this transformation even if overflow checking is | |
817 | -- off, since this is precisely about giving the "right" result and | |
818 | -- avoiding the need for an overflow check. | |
819 | ||
820 | if Is_Signed_Integer_Type (Typ) | |
821 | and then Nkind (Parent (N)) = N_Type_Conversion | |
ee6ba406 | 822 | then |
f40f9731 | 823 | declare |
824 | Target_Type : constant Entity_Id := | |
825 | Base_Type (Entity (Subtype_Mark (Parent (N)))); | |
826 | ||
827 | Llo, Lhi : Uint; | |
828 | Rlo, Rhi : Uint; | |
829 | LOK, ROK : Boolean; | |
830 | ||
831 | Vlo : Uint; | |
832 | Vhi : Uint; | |
833 | VOK : Boolean; | |
834 | ||
835 | Tlo : Uint; | |
836 | Thi : Uint; | |
837 | ||
838 | begin | |
839 | if Is_Integer_Type (Target_Type) | |
840 | and then RM_Size (Root_Type (Target_Type)) >= 2 * RM_Size (Rtyp) | |
841 | then | |
842 | Tlo := Expr_Value (Type_Low_Bound (Target_Type)); | |
843 | Thi := Expr_Value (Type_High_Bound (Target_Type)); | |
844 | ||
9c486805 | 845 | Determine_Range |
846 | (Left_Opnd (N), LOK, Llo, Lhi, Assume_Valid => True); | |
847 | Determine_Range | |
848 | (Right_Opnd (N), ROK, Rlo, Rhi, Assume_Valid => True); | |
f40f9731 | 849 | |
850 | if (LOK and ROK) | |
851 | and then Tlo <= Llo and then Lhi <= Thi | |
852 | and then Tlo <= Rlo and then Rhi <= Thi | |
853 | then | |
9c486805 | 854 | Determine_Range (N, VOK, Vlo, Vhi, Assume_Valid => True); |
f40f9731 | 855 | |
856 | if VOK and then Tlo <= Vlo and then Vhi <= Thi then | |
857 | Rewrite (Left_Opnd (N), | |
858 | Make_Type_Conversion (Loc, | |
859 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
860 | Expression => Relocate_Node (Left_Opnd (N)))); | |
861 | ||
862 | Rewrite (Right_Opnd (N), | |
863 | Make_Type_Conversion (Loc, | |
864 | Subtype_Mark => New_Occurrence_Of (Target_Type, Loc), | |
865 | Expression => Relocate_Node (Right_Opnd (N)))); | |
866 | ||
867 | Set_Etype (N, Target_Type); | |
868 | Typ := Target_Type; | |
869 | Rtyp := Root_Type (Typ); | |
870 | Analyze_And_Resolve (Left_Opnd (N), Target_Type); | |
871 | Analyze_And_Resolve (Right_Opnd (N), Target_Type); | |
872 | ||
873 | -- Given that the target type is twice the size of the | |
874 | -- source type, overflow is now impossible, so we can | |
875 | -- safely kill the overflow check and return. | |
876 | ||
877 | Set_Do_Overflow_Check (N, False); | |
878 | return; | |
879 | end if; | |
880 | end if; | |
881 | end if; | |
882 | end; | |
ee6ba406 | 883 | end if; |
884 | ||
f40f9731 | 885 | -- Now see if an overflow check is required |
886 | ||
887 | declare | |
888 | Siz : constant Int := UI_To_Int (Esize (Rtyp)); | |
889 | Dsiz : constant Int := Siz * 2; | |
890 | Opnod : Node_Id; | |
891 | Ctyp : Entity_Id; | |
892 | Opnd : Node_Id; | |
893 | Cent : RE_Id; | |
ee6ba406 | 894 | |
f40f9731 | 895 | begin |
896 | -- Skip check if back end does overflow checks, or the overflow flag | |
897 | -- is not set anyway, or we are not doing code expansion. | |
ee6ba406 | 898 | |
f40f9731 | 899 | -- Special case CLI target, where arithmetic overflow checks can be |
900 | -- performed for integer and long_integer | |
ee6ba406 | 901 | |
f40f9731 | 902 | if Backend_Overflow_Checks_On_Target |
903 | or else not Do_Overflow_Check (N) | |
904 | or else not Expander_Active | |
905 | or else | |
906 | (VM_Target = CLI_Target and then Siz >= Standard_Integer_Size) | |
907 | then | |
908 | return; | |
909 | end if; | |
ee6ba406 | 910 | |
f40f9731 | 911 | -- Otherwise, generate the full general code for front end overflow |
912 | -- detection, which works by doing arithmetic in a larger type: | |
ee6ba406 | 913 | |
f40f9731 | 914 | -- x op y |
ee6ba406 | 915 | |
f40f9731 | 916 | -- is expanded into |
ee6ba406 | 917 | |
f40f9731 | 918 | -- Typ (Checktyp (x) op Checktyp (y)); |
ee6ba406 | 919 | |
f40f9731 | 920 | -- where Typ is the type of the original expression, and Checktyp is |
921 | -- an integer type of sufficient length to hold the largest possible | |
922 | -- result. | |
ee6ba406 | 923 | |
f40f9731 | 924 | -- If the size of check type exceeds the size of Long_Long_Integer, |
925 | -- we use a different approach, expanding to: | |
ee6ba406 | 926 | |
f40f9731 | 927 | -- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y))) |
ee6ba406 | 928 | |
f40f9731 | 929 | -- where xxx is Add, Multiply or Subtract as appropriate |
ee6ba406 | 930 | |
f40f9731 | 931 | -- Find check type if one exists |
932 | ||
933 | if Dsiz <= Standard_Integer_Size then | |
934 | Ctyp := Standard_Integer; | |
ee6ba406 | 935 | |
f40f9731 | 936 | elsif Dsiz <= Standard_Long_Long_Integer_Size then |
937 | Ctyp := Standard_Long_Long_Integer; | |
938 | ||
939 | -- No check type exists, use runtime call | |
ee6ba406 | 940 | |
941 | else | |
f40f9731 | 942 | if Nkind (N) = N_Op_Add then |
943 | Cent := RE_Add_With_Ovflo_Check; | |
ee6ba406 | 944 | |
f40f9731 | 945 | elsif Nkind (N) = N_Op_Multiply then |
946 | Cent := RE_Multiply_With_Ovflo_Check; | |
ee6ba406 | 947 | |
f40f9731 | 948 | else |
949 | pragma Assert (Nkind (N) = N_Op_Subtract); | |
950 | Cent := RE_Subtract_With_Ovflo_Check; | |
951 | end if; | |
952 | ||
953 | Rewrite (N, | |
954 | OK_Convert_To (Typ, | |
955 | Make_Function_Call (Loc, | |
956 | Name => New_Reference_To (RTE (Cent), Loc), | |
957 | Parameter_Associations => New_List ( | |
958 | OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)), | |
959 | OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N)))))); | |
ee6ba406 | 960 | |
f40f9731 | 961 | Analyze_And_Resolve (N, Typ); |
962 | return; | |
963 | end if; | |
ee6ba406 | 964 | |
f40f9731 | 965 | -- If we fall through, we have the case where we do the arithmetic |
966 | -- in the next higher type and get the check by conversion. In these | |
967 | -- cases Ctyp is set to the type to be used as the check type. | |
ee6ba406 | 968 | |
f40f9731 | 969 | Opnod := Relocate_Node (N); |
ee6ba406 | 970 | |
f40f9731 | 971 | Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod)); |
ee6ba406 | 972 | |
f40f9731 | 973 | Analyze (Opnd); |
974 | Set_Etype (Opnd, Ctyp); | |
975 | Set_Analyzed (Opnd, True); | |
976 | Set_Left_Opnd (Opnod, Opnd); | |
ee6ba406 | 977 | |
f40f9731 | 978 | Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod)); |
ee6ba406 | 979 | |
f40f9731 | 980 | Analyze (Opnd); |
981 | Set_Etype (Opnd, Ctyp); | |
982 | Set_Analyzed (Opnd, True); | |
983 | Set_Right_Opnd (Opnod, Opnd); | |
ee6ba406 | 984 | |
f40f9731 | 985 | -- The type of the operation changes to the base type of the check |
986 | -- type, and we reset the overflow check indication, since clearly no | |
987 | -- overflow is possible now that we are using a double length type. | |
988 | -- We also set the Analyzed flag to avoid a recursive attempt to | |
989 | -- expand the node. | |
ee6ba406 | 990 | |
f40f9731 | 991 | Set_Etype (Opnod, Base_Type (Ctyp)); |
992 | Set_Do_Overflow_Check (Opnod, False); | |
993 | Set_Analyzed (Opnod, True); | |
ee6ba406 | 994 | |
f40f9731 | 995 | -- Now build the outer conversion |
ee6ba406 | 996 | |
f40f9731 | 997 | Opnd := OK_Convert_To (Typ, Opnod); |
998 | Analyze (Opnd); | |
999 | Set_Etype (Opnd, Typ); | |
9dfe12ae | 1000 | |
f40f9731 | 1001 | -- In the discrete type case, we directly generate the range check |
1002 | -- for the outer operand. This range check will implement the | |
1003 | -- required overflow check. | |
9dfe12ae | 1004 | |
f40f9731 | 1005 | if Is_Discrete_Type (Typ) then |
1006 | Rewrite (N, Opnd); | |
1007 | Generate_Range_Check | |
1008 | (Expression (N), Typ, CE_Overflow_Check_Failed); | |
9dfe12ae | 1009 | |
f40f9731 | 1010 | -- For other types, we enable overflow checking on the conversion, |
1011 | -- after setting the node as analyzed to prevent recursive attempts | |
1012 | -- to expand the conversion node. | |
9dfe12ae | 1013 | |
f40f9731 | 1014 | else |
1015 | Set_Analyzed (Opnd, True); | |
1016 | Enable_Overflow_Check (Opnd); | |
1017 | Rewrite (N, Opnd); | |
1018 | end if; | |
1019 | ||
1020 | exception | |
1021 | when RE_Not_Available => | |
1022 | return; | |
1023 | end; | |
ee6ba406 | 1024 | end Apply_Arithmetic_Overflow_Check; |
1025 | ||
ee6ba406 | 1026 | ---------------------------- |
1027 | -- Apply_Constraint_Check -- | |
1028 | ---------------------------- | |
1029 | ||
1030 | procedure Apply_Constraint_Check | |
1031 | (N : Node_Id; | |
1032 | Typ : Entity_Id; | |
1033 | No_Sliding : Boolean := False) | |
1034 | is | |
1035 | Desig_Typ : Entity_Id; | |
1036 | ||
1037 | begin | |
1038 | if Inside_A_Generic then | |
1039 | return; | |
1040 | ||
1041 | elsif Is_Scalar_Type (Typ) then | |
1042 | Apply_Scalar_Range_Check (N, Typ); | |
1043 | ||
1044 | elsif Is_Array_Type (Typ) then | |
1045 | ||
05fcfafb | 1046 | -- A useful optimization: an aggregate with only an others clause |
5f260d20 | 1047 | -- always has the right bounds. |
1048 | ||
1049 | if Nkind (N) = N_Aggregate | |
1050 | and then No (Expressions (N)) | |
1051 | and then Nkind | |
1052 | (First (Choices (First (Component_Associations (N))))) | |
1053 | = N_Others_Choice | |
1054 | then | |
1055 | return; | |
1056 | end if; | |
1057 | ||
ee6ba406 | 1058 | if Is_Constrained (Typ) then |
1059 | Apply_Length_Check (N, Typ); | |
1060 | ||
1061 | if No_Sliding then | |
1062 | Apply_Range_Check (N, Typ); | |
1063 | end if; | |
1064 | else | |
1065 | Apply_Range_Check (N, Typ); | |
1066 | end if; | |
1067 | ||
1068 | elsif (Is_Record_Type (Typ) | |
1069 | or else Is_Private_Type (Typ)) | |
1070 | and then Has_Discriminants (Base_Type (Typ)) | |
1071 | and then Is_Constrained (Typ) | |
1072 | then | |
1073 | Apply_Discriminant_Check (N, Typ); | |
1074 | ||
1075 | elsif Is_Access_Type (Typ) then | |
1076 | ||
1077 | Desig_Typ := Designated_Type (Typ); | |
1078 | ||
1079 | -- No checks necessary if expression statically null | |
1080 | ||
2af58f67 | 1081 | if Known_Null (N) then |
00c403ee | 1082 | if Can_Never_Be_Null (Typ) then |
1083 | Install_Null_Excluding_Check (N); | |
1084 | end if; | |
ee6ba406 | 1085 | |
1086 | -- No sliding possible on access to arrays | |
1087 | ||
1088 | elsif Is_Array_Type (Desig_Typ) then | |
1089 | if Is_Constrained (Desig_Typ) then | |
1090 | Apply_Length_Check (N, Typ); | |
1091 | end if; | |
1092 | ||
1093 | Apply_Range_Check (N, Typ); | |
1094 | ||
1095 | elsif Has_Discriminants (Base_Type (Desig_Typ)) | |
1096 | and then Is_Constrained (Desig_Typ) | |
1097 | then | |
1098 | Apply_Discriminant_Check (N, Typ); | |
1099 | end if; | |
fa7497e8 | 1100 | |
bf3e1520 | 1101 | -- Apply the 2005 Null_Excluding check. Note that we do not apply |
00c403ee | 1102 | -- this check if the constraint node is illegal, as shown by having |
1103 | -- an error posted. This additional guard prevents cascaded errors | |
1104 | -- and compiler aborts on illegal programs involving Ada 2005 checks. | |
1105 | ||
fa7497e8 | 1106 | if Can_Never_Be_Null (Typ) |
1107 | and then not Can_Never_Be_Null (Etype (N)) | |
00c403ee | 1108 | and then not Error_Posted (N) |
fa7497e8 | 1109 | then |
1110 | Install_Null_Excluding_Check (N); | |
1111 | end if; | |
ee6ba406 | 1112 | end if; |
1113 | end Apply_Constraint_Check; | |
1114 | ||
1115 | ------------------------------ | |
1116 | -- Apply_Discriminant_Check -- | |
1117 | ------------------------------ | |
1118 | ||
1119 | procedure Apply_Discriminant_Check | |
1120 | (N : Node_Id; | |
1121 | Typ : Entity_Id; | |
1122 | Lhs : Node_Id := Empty) | |
1123 | is | |
1124 | Loc : constant Source_Ptr := Sloc (N); | |
1125 | Do_Access : constant Boolean := Is_Access_Type (Typ); | |
1126 | S_Typ : Entity_Id := Etype (N); | |
1127 | Cond : Node_Id; | |
1128 | T_Typ : Entity_Id; | |
1129 | ||
1130 | function Is_Aliased_Unconstrained_Component return Boolean; | |
1131 | -- It is possible for an aliased component to have a nominal | |
1132 | -- unconstrained subtype (through instantiation). If this is a | |
1133 | -- discriminated component assigned in the expansion of an aggregate | |
1134 | -- in an initialization, the check must be suppressed. This unusual | |
2af58f67 | 1135 | -- situation requires a predicate of its own. |
ee6ba406 | 1136 | |
1137 | ---------------------------------------- | |
1138 | -- Is_Aliased_Unconstrained_Component -- | |
1139 | ---------------------------------------- | |
1140 | ||
1141 | function Is_Aliased_Unconstrained_Component return Boolean is | |
1142 | Comp : Entity_Id; | |
1143 | Pref : Node_Id; | |
1144 | ||
1145 | begin | |
1146 | if Nkind (Lhs) /= N_Selected_Component then | |
1147 | return False; | |
1148 | else | |
1149 | Comp := Entity (Selector_Name (Lhs)); | |
1150 | Pref := Prefix (Lhs); | |
1151 | end if; | |
1152 | ||
1153 | if Ekind (Comp) /= E_Component | |
1154 | or else not Is_Aliased (Comp) | |
1155 | then | |
1156 | return False; | |
1157 | end if; | |
1158 | ||
1159 | return not Comes_From_Source (Pref) | |
1160 | and then In_Instance | |
1161 | and then not Is_Constrained (Etype (Comp)); | |
1162 | end Is_Aliased_Unconstrained_Component; | |
1163 | ||
1164 | -- Start of processing for Apply_Discriminant_Check | |
1165 | ||
1166 | begin | |
1167 | if Do_Access then | |
1168 | T_Typ := Designated_Type (Typ); | |
1169 | else | |
1170 | T_Typ := Typ; | |
1171 | end if; | |
1172 | ||
1173 | -- Nothing to do if discriminant checks are suppressed or else no code | |
1174 | -- is to be generated | |
1175 | ||
1176 | if not Expander_Active | |
1177 | or else Discriminant_Checks_Suppressed (T_Typ) | |
1178 | then | |
1179 | return; | |
1180 | end if; | |
1181 | ||
feff2f05 | 1182 | -- No discriminant checks necessary for an access when expression is |
1183 | -- statically Null. This is not only an optimization, it is fundamental | |
1184 | -- because otherwise discriminant checks may be generated in init procs | |
1185 | -- for types containing an access to a not-yet-frozen record, causing a | |
1186 | -- deadly forward reference. | |
ee6ba406 | 1187 | |
feff2f05 | 1188 | -- Also, if the expression is of an access type whose designated type is |
1189 | -- incomplete, then the access value must be null and we suppress the | |
1190 | -- check. | |
ee6ba406 | 1191 | |
2af58f67 | 1192 | if Known_Null (N) then |
ee6ba406 | 1193 | return; |
1194 | ||
1195 | elsif Is_Access_Type (S_Typ) then | |
1196 | S_Typ := Designated_Type (S_Typ); | |
1197 | ||
1198 | if Ekind (S_Typ) = E_Incomplete_Type then | |
1199 | return; | |
1200 | end if; | |
1201 | end if; | |
1202 | ||
0577b0b1 | 1203 | -- If an assignment target is present, then we need to generate the |
1204 | -- actual subtype if the target is a parameter or aliased object with | |
1205 | -- an unconstrained nominal subtype. | |
1206 | ||
1207 | -- Ada 2005 (AI-363): For Ada 2005, we limit the building of the actual | |
1208 | -- subtype to the parameter and dereference cases, since other aliased | |
1209 | -- objects are unconstrained (unless the nominal subtype is explicitly | |
1210 | -- constrained). (But we also need to test for renamings???) | |
ee6ba406 | 1211 | |
1212 | if Present (Lhs) | |
1213 | and then (Present (Param_Entity (Lhs)) | |
0577b0b1 | 1214 | or else (Ada_Version < Ada_05 |
1215 | and then not Is_Constrained (T_Typ) | |
ee6ba406 | 1216 | and then Is_Aliased_View (Lhs) |
0577b0b1 | 1217 | and then not Is_Aliased_Unconstrained_Component) |
1218 | or else (Ada_Version >= Ada_05 | |
1219 | and then not Is_Constrained (T_Typ) | |
1220 | and then Nkind (Lhs) = N_Explicit_Dereference | |
1221 | and then Nkind (Original_Node (Lhs)) /= | |
1222 | N_Function_Call)) | |
ee6ba406 | 1223 | then |
1224 | T_Typ := Get_Actual_Subtype (Lhs); | |
1225 | end if; | |
1226 | ||
feff2f05 | 1227 | -- Nothing to do if the type is unconstrained (this is the case where |
1228 | -- the actual subtype in the RM sense of N is unconstrained and no check | |
1229 | -- is required). | |
ee6ba406 | 1230 | |
1231 | if not Is_Constrained (T_Typ) then | |
1232 | return; | |
05fcfafb | 1233 | |
1234 | -- Ada 2005: nothing to do if the type is one for which there is a | |
1235 | -- partial view that is constrained. | |
1236 | ||
1237 | elsif Ada_Version >= Ada_05 | |
1238 | and then Has_Constrained_Partial_View (Base_Type (T_Typ)) | |
1239 | then | |
1240 | return; | |
ee6ba406 | 1241 | end if; |
1242 | ||
00f91aef | 1243 | -- Nothing to do if the type is an Unchecked_Union |
1244 | ||
1245 | if Is_Unchecked_Union (Base_Type (T_Typ)) then | |
1246 | return; | |
1247 | end if; | |
1248 | ||
feff2f05 | 1249 | -- Suppress checks if the subtypes are the same. the check must be |
1250 | -- preserved in an assignment to a formal, because the constraint is | |
1251 | -- given by the actual. | |
ee6ba406 | 1252 | |
1253 | if Nkind (Original_Node (N)) /= N_Allocator | |
1254 | and then (No (Lhs) | |
1255 | or else not Is_Entity_Name (Lhs) | |
9dfe12ae | 1256 | or else No (Param_Entity (Lhs))) |
ee6ba406 | 1257 | then |
1258 | if (Etype (N) = Typ | |
1259 | or else (Do_Access and then Designated_Type (Typ) = S_Typ)) | |
1260 | and then not Is_Aliased_View (Lhs) | |
1261 | then | |
1262 | return; | |
1263 | end if; | |
1264 | ||
feff2f05 | 1265 | -- We can also eliminate checks on allocators with a subtype mark that |
1266 | -- coincides with the context type. The context type may be a subtype | |
1267 | -- without a constraint (common case, a generic actual). | |
ee6ba406 | 1268 | |
1269 | elsif Nkind (Original_Node (N)) = N_Allocator | |
1270 | and then Is_Entity_Name (Expression (Original_Node (N))) | |
1271 | then | |
1272 | declare | |
9dfe12ae | 1273 | Alloc_Typ : constant Entity_Id := |
1274 | Entity (Expression (Original_Node (N))); | |
ee6ba406 | 1275 | |
1276 | begin | |
1277 | if Alloc_Typ = T_Typ | |
1278 | or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration | |
1279 | and then Is_Entity_Name ( | |
1280 | Subtype_Indication (Parent (T_Typ))) | |
1281 | and then Alloc_Typ = Base_Type (T_Typ)) | |
1282 | ||
1283 | then | |
1284 | return; | |
1285 | end if; | |
1286 | end; | |
1287 | end if; | |
1288 | ||
feff2f05 | 1289 | -- See if we have a case where the types are both constrained, and all |
1290 | -- the constraints are constants. In this case, we can do the check | |
1291 | -- successfully at compile time. | |
ee6ba406 | 1292 | |
9dfe12ae | 1293 | -- We skip this check for the case where the node is a rewritten` |
ee6ba406 | 1294 | -- allocator, because it already carries the context subtype, and |
1295 | -- extracting the discriminants from the aggregate is messy. | |
1296 | ||
1297 | if Is_Constrained (S_Typ) | |
1298 | and then Nkind (Original_Node (N)) /= N_Allocator | |
1299 | then | |
1300 | declare | |
1301 | DconT : Elmt_Id; | |
1302 | Discr : Entity_Id; | |
1303 | DconS : Elmt_Id; | |
1304 | ItemS : Node_Id; | |
1305 | ItemT : Node_Id; | |
1306 | ||
1307 | begin | |
1308 | -- S_Typ may not have discriminants in the case where it is a | |
feff2f05 | 1309 | -- private type completed by a default discriminated type. In that |
1310 | -- case, we need to get the constraints from the underlying_type. | |
1311 | -- If the underlying type is unconstrained (i.e. has no default | |
1312 | -- discriminants) no check is needed. | |
ee6ba406 | 1313 | |
1314 | if Has_Discriminants (S_Typ) then | |
1315 | Discr := First_Discriminant (S_Typ); | |
1316 | DconS := First_Elmt (Discriminant_Constraint (S_Typ)); | |
1317 | ||
1318 | else | |
1319 | Discr := First_Discriminant (Underlying_Type (S_Typ)); | |
1320 | DconS := | |
1321 | First_Elmt | |
1322 | (Discriminant_Constraint (Underlying_Type (S_Typ))); | |
1323 | ||
1324 | if No (DconS) then | |
1325 | return; | |
1326 | end if; | |
fccb5da7 | 1327 | |
1328 | -- A further optimization: if T_Typ is derived from S_Typ | |
1329 | -- without imposing a constraint, no check is needed. | |
1330 | ||
1331 | if Nkind (Original_Node (Parent (T_Typ))) = | |
1332 | N_Full_Type_Declaration | |
1333 | then | |
1334 | declare | |
5c61a0ff | 1335 | Type_Def : constant Node_Id := |
fccb5da7 | 1336 | Type_Definition |
1337 | (Original_Node (Parent (T_Typ))); | |
1338 | begin | |
1339 | if Nkind (Type_Def) = N_Derived_Type_Definition | |
1340 | and then Is_Entity_Name (Subtype_Indication (Type_Def)) | |
1341 | and then Entity (Subtype_Indication (Type_Def)) = S_Typ | |
1342 | then | |
1343 | return; | |
1344 | end if; | |
1345 | end; | |
1346 | end if; | |
ee6ba406 | 1347 | end if; |
1348 | ||
1349 | DconT := First_Elmt (Discriminant_Constraint (T_Typ)); | |
1350 | ||
1351 | while Present (Discr) loop | |
1352 | ItemS := Node (DconS); | |
1353 | ItemT := Node (DconT); | |
1354 | ||
00c403ee | 1355 | -- For a discriminated component type constrained by the |
1356 | -- current instance of an enclosing type, there is no | |
1357 | -- applicable discriminant check. | |
1358 | ||
1359 | if Nkind (ItemT) = N_Attribute_Reference | |
1360 | and then Is_Access_Type (Etype (ItemT)) | |
1361 | and then Is_Entity_Name (Prefix (ItemT)) | |
1362 | and then Is_Type (Entity (Prefix (ItemT))) | |
1363 | then | |
1364 | return; | |
1365 | end if; | |
1366 | ||
cc60bd16 | 1367 | -- If the expressions for the discriminants are identical |
1368 | -- and it is side-effect free (for now just an entity), | |
1369 | -- this may be a shared constraint, e.g. from a subtype | |
1370 | -- without a constraint introduced as a generic actual. | |
1371 | -- Examine other discriminants if any. | |
1372 | ||
1373 | if ItemS = ItemT | |
1374 | and then Is_Entity_Name (ItemS) | |
1375 | then | |
1376 | null; | |
1377 | ||
1378 | elsif not Is_OK_Static_Expression (ItemS) | |
1379 | or else not Is_OK_Static_Expression (ItemT) | |
1380 | then | |
1381 | exit; | |
ee6ba406 | 1382 | |
cc60bd16 | 1383 | elsif Expr_Value (ItemS) /= Expr_Value (ItemT) then |
ee6ba406 | 1384 | if Do_Access then -- needs run-time check. |
1385 | exit; | |
1386 | else | |
1387 | Apply_Compile_Time_Constraint_Error | |
f15731c4 | 1388 | (N, "incorrect value for discriminant&?", |
1389 | CE_Discriminant_Check_Failed, Ent => Discr); | |
ee6ba406 | 1390 | return; |
1391 | end if; | |
1392 | end if; | |
1393 | ||
1394 | Next_Elmt (DconS); | |
1395 | Next_Elmt (DconT); | |
1396 | Next_Discriminant (Discr); | |
1397 | end loop; | |
1398 | ||
1399 | if No (Discr) then | |
1400 | return; | |
1401 | end if; | |
1402 | end; | |
1403 | end if; | |
1404 | ||
1405 | -- Here we need a discriminant check. First build the expression | |
1406 | -- for the comparisons of the discriminants: | |
1407 | ||
1408 | -- (n.disc1 /= typ.disc1) or else | |
1409 | -- (n.disc2 /= typ.disc2) or else | |
1410 | -- ... | |
1411 | -- (n.discn /= typ.discn) | |
1412 | ||
1413 | Cond := Build_Discriminant_Checks (N, T_Typ); | |
1414 | ||
1415 | -- If Lhs is set and is a parameter, then the condition is | |
1416 | -- guarded by: lhs'constrained and then (condition built above) | |
1417 | ||
1418 | if Present (Param_Entity (Lhs)) then | |
1419 | Cond := | |
1420 | Make_And_Then (Loc, | |
1421 | Left_Opnd => | |
1422 | Make_Attribute_Reference (Loc, | |
1423 | Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc), | |
1424 | Attribute_Name => Name_Constrained), | |
1425 | Right_Opnd => Cond); | |
1426 | end if; | |
1427 | ||
1428 | if Do_Access then | |
1429 | Cond := Guard_Access (Cond, Loc, N); | |
1430 | end if; | |
1431 | ||
1432 | Insert_Action (N, | |
f15731c4 | 1433 | Make_Raise_Constraint_Error (Loc, |
1434 | Condition => Cond, | |
1435 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 1436 | end Apply_Discriminant_Check; |
1437 | ||
1438 | ------------------------ | |
1439 | -- Apply_Divide_Check -- | |
1440 | ------------------------ | |
1441 | ||
1442 | procedure Apply_Divide_Check (N : Node_Id) is | |
1443 | Loc : constant Source_Ptr := Sloc (N); | |
1444 | Typ : constant Entity_Id := Etype (N); | |
1445 | Left : constant Node_Id := Left_Opnd (N); | |
1446 | Right : constant Node_Id := Right_Opnd (N); | |
1447 | ||
1448 | LLB : Uint; | |
1449 | Llo : Uint; | |
1450 | Lhi : Uint; | |
1451 | LOK : Boolean; | |
1452 | Rlo : Uint; | |
1453 | Rhi : Uint; | |
96da3284 | 1454 | ROK : Boolean; |
1455 | ||
1456 | pragma Warnings (Off, Lhi); | |
1457 | -- Don't actually use this value | |
ee6ba406 | 1458 | |
1459 | begin | |
1460 | if Expander_Active | |
13dbf220 | 1461 | and then not Backend_Divide_Checks_On_Target |
1462 | and then Check_Needed (Right, Division_Check) | |
ee6ba406 | 1463 | then |
9c486805 | 1464 | Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True); |
ee6ba406 | 1465 | |
1466 | -- See if division by zero possible, and if so generate test. This | |
1467 | -- part of the test is not controlled by the -gnato switch. | |
1468 | ||
1469 | if Do_Division_Check (N) then | |
ee6ba406 | 1470 | if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then |
1471 | Insert_Action (N, | |
1472 | Make_Raise_Constraint_Error (Loc, | |
1473 | Condition => | |
1474 | Make_Op_Eq (Loc, | |
0577b0b1 | 1475 | Left_Opnd => Duplicate_Subexpr_Move_Checks (Right), |
f15731c4 | 1476 | Right_Opnd => Make_Integer_Literal (Loc, 0)), |
1477 | Reason => CE_Divide_By_Zero)); | |
ee6ba406 | 1478 | end if; |
1479 | end if; | |
1480 | ||
1481 | -- Test for extremely annoying case of xxx'First divided by -1 | |
1482 | ||
1483 | if Do_Overflow_Check (N) then | |
ee6ba406 | 1484 | if Nkind (N) = N_Op_Divide |
1485 | and then Is_Signed_Integer_Type (Typ) | |
1486 | then | |
9c486805 | 1487 | Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True); |
ee6ba406 | 1488 | LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ))); |
1489 | ||
1490 | if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi)) | |
1491 | and then | |
1492 | ((not LOK) or else (Llo = LLB)) | |
1493 | then | |
1494 | Insert_Action (N, | |
1495 | Make_Raise_Constraint_Error (Loc, | |
1496 | Condition => | |
1497 | Make_And_Then (Loc, | |
1498 | ||
1499 | Make_Op_Eq (Loc, | |
9dfe12ae | 1500 | Left_Opnd => |
1501 | Duplicate_Subexpr_Move_Checks (Left), | |
ee6ba406 | 1502 | Right_Opnd => Make_Integer_Literal (Loc, LLB)), |
1503 | ||
1504 | Make_Op_Eq (Loc, | |
9dfe12ae | 1505 | Left_Opnd => |
1506 | Duplicate_Subexpr (Right), | |
ee6ba406 | 1507 | Right_Opnd => |
f15731c4 | 1508 | Make_Integer_Literal (Loc, -1))), |
1509 | Reason => CE_Overflow_Check_Failed)); | |
ee6ba406 | 1510 | end if; |
1511 | end if; | |
1512 | end if; | |
1513 | end if; | |
1514 | end Apply_Divide_Check; | |
1515 | ||
5329ca64 | 1516 | ---------------------------------- |
1517 | -- Apply_Float_Conversion_Check -- | |
1518 | ---------------------------------- | |
1519 | ||
feff2f05 | 1520 | -- Let F and I be the source and target types of the conversion. The RM |
1521 | -- specifies that a floating-point value X is rounded to the nearest | |
1522 | -- integer, with halfway cases being rounded away from zero. The rounded | |
1523 | -- value of X is checked against I'Range. | |
1524 | ||
1525 | -- The catch in the above paragraph is that there is no good way to know | |
1526 | -- whether the round-to-integer operation resulted in overflow. A remedy is | |
1527 | -- to perform a range check in the floating-point domain instead, however: | |
5329ca64 | 1528 | |
5329ca64 | 1529 | -- (1) The bounds may not be known at compile time |
2af58f67 | 1530 | -- (2) The check must take into account rounding or truncation. |
5329ca64 | 1531 | -- (3) The range of type I may not be exactly representable in F. |
2af58f67 | 1532 | -- (4) For the rounding case, The end-points I'First - 0.5 and |
1533 | -- I'Last + 0.5 may or may not be in range, depending on the | |
1534 | -- sign of I'First and I'Last. | |
5329ca64 | 1535 | -- (5) X may be a NaN, which will fail any comparison |
1536 | ||
2af58f67 | 1537 | -- The following steps correctly convert X with rounding: |
feff2f05 | 1538 | |
5329ca64 | 1539 | -- (1) If either I'First or I'Last is not known at compile time, use |
1540 | -- I'Base instead of I in the next three steps and perform a | |
1541 | -- regular range check against I'Range after conversion. | |
1542 | -- (2) If I'First - 0.5 is representable in F then let Lo be that | |
1543 | -- value and define Lo_OK as (I'First > 0). Otherwise, let Lo be | |
2af58f67 | 1544 | -- F'Machine (I'First) and let Lo_OK be (Lo >= I'First). |
1545 | -- In other words, take one of the closest floating-point numbers | |
1546 | -- (which is an integer value) to I'First, and see if it is in | |
1547 | -- range or not. | |
5329ca64 | 1548 | -- (3) If I'Last + 0.5 is representable in F then let Hi be that value |
1549 | -- and define Hi_OK as (I'Last < 0). Otherwise, let Hi be | |
2af58f67 | 1550 | -- F'Machine (I'Last) and let Hi_OK be (Hi <= I'Last). |
5329ca64 | 1551 | -- (4) Raise CE when (Lo_OK and X < Lo) or (not Lo_OK and X <= Lo) |
1552 | -- or (Hi_OK and X > Hi) or (not Hi_OK and X >= Hi) | |
1553 | ||
2af58f67 | 1554 | -- For the truncating case, replace steps (2) and (3) as follows: |
1555 | -- (2) If I'First > 0, then let Lo be F'Pred (I'First) and let Lo_OK | |
1556 | -- be False. Otherwise, let Lo be F'Succ (I'First - 1) and let | |
1557 | -- Lo_OK be True. | |
1558 | -- (3) If I'Last < 0, then let Hi be F'Succ (I'Last) and let Hi_OK | |
1559 | -- be False. Otherwise let Hi be F'Pred (I'Last + 1) and let | |
1560 | -- Hi_OK be False | |
1561 | ||
5329ca64 | 1562 | procedure Apply_Float_Conversion_Check |
1563 | (Ck_Node : Node_Id; | |
1564 | Target_Typ : Entity_Id) | |
1565 | is | |
feff2f05 | 1566 | LB : constant Node_Id := Type_Low_Bound (Target_Typ); |
1567 | HB : constant Node_Id := Type_High_Bound (Target_Typ); | |
5329ca64 | 1568 | Loc : constant Source_Ptr := Sloc (Ck_Node); |
1569 | Expr_Type : constant Entity_Id := Base_Type (Etype (Ck_Node)); | |
feff2f05 | 1570 | Target_Base : constant Entity_Id := |
1571 | Implementation_Base_Type (Target_Typ); | |
1572 | ||
2af58f67 | 1573 | Par : constant Node_Id := Parent (Ck_Node); |
1574 | pragma Assert (Nkind (Par) = N_Type_Conversion); | |
1575 | -- Parent of check node, must be a type conversion | |
1576 | ||
1577 | Truncate : constant Boolean := Float_Truncate (Par); | |
1578 | Max_Bound : constant Uint := | |
1579 | UI_Expon | |
1580 | (Machine_Radix (Expr_Type), | |
1581 | Machine_Mantissa (Expr_Type) - 1) - 1; | |
1582 | ||
5329ca64 | 1583 | -- Largest bound, so bound plus or minus half is a machine number of F |
1584 | ||
feff2f05 | 1585 | Ifirst, Ilast : Uint; |
1586 | -- Bounds of integer type | |
1587 | ||
1588 | Lo, Hi : Ureal; | |
1589 | -- Bounds to check in floating-point domain | |
5329ca64 | 1590 | |
feff2f05 | 1591 | Lo_OK, Hi_OK : Boolean; |
1592 | -- True iff Lo resp. Hi belongs to I'Range | |
5329ca64 | 1593 | |
feff2f05 | 1594 | Lo_Chk, Hi_Chk : Node_Id; |
1595 | -- Expressions that are False iff check fails | |
1596 | ||
1597 | Reason : RT_Exception_Code; | |
5329ca64 | 1598 | |
1599 | begin | |
1600 | if not Compile_Time_Known_Value (LB) | |
1601 | or not Compile_Time_Known_Value (HB) | |
1602 | then | |
1603 | declare | |
feff2f05 | 1604 | -- First check that the value falls in the range of the base type, |
1605 | -- to prevent overflow during conversion and then perform a | |
1606 | -- regular range check against the (dynamic) bounds. | |
5329ca64 | 1607 | |
5329ca64 | 1608 | pragma Assert (Target_Base /= Target_Typ); |
5329ca64 | 1609 | |
1610 | Temp : constant Entity_Id := | |
1611 | Make_Defining_Identifier (Loc, | |
1612 | Chars => New_Internal_Name ('T')); | |
1613 | ||
1614 | begin | |
1615 | Apply_Float_Conversion_Check (Ck_Node, Target_Base); | |
1616 | Set_Etype (Temp, Target_Base); | |
1617 | ||
1618 | Insert_Action (Parent (Par), | |
1619 | Make_Object_Declaration (Loc, | |
1620 | Defining_Identifier => Temp, | |
1621 | Object_Definition => New_Occurrence_Of (Target_Typ, Loc), | |
1622 | Expression => New_Copy_Tree (Par)), | |
1623 | Suppress => All_Checks); | |
1624 | ||
1625 | Insert_Action (Par, | |
1626 | Make_Raise_Constraint_Error (Loc, | |
1627 | Condition => | |
1628 | Make_Not_In (Loc, | |
1629 | Left_Opnd => New_Occurrence_Of (Temp, Loc), | |
1630 | Right_Opnd => New_Occurrence_Of (Target_Typ, Loc)), | |
1631 | Reason => CE_Range_Check_Failed)); | |
1632 | Rewrite (Par, New_Occurrence_Of (Temp, Loc)); | |
1633 | ||
1634 | return; | |
1635 | end; | |
1636 | end if; | |
1637 | ||
7d86aa98 | 1638 | -- Get the (static) bounds of the target type |
5329ca64 | 1639 | |
1640 | Ifirst := Expr_Value (LB); | |
1641 | Ilast := Expr_Value (HB); | |
1642 | ||
7d86aa98 | 1643 | -- A simple optimization: if the expression is a universal literal, |
1644 | -- we can do the comparison with the bounds and the conversion to | |
1645 | -- an integer type statically. The range checks are unchanged. | |
1646 | ||
1647 | if Nkind (Ck_Node) = N_Real_Literal | |
1648 | and then Etype (Ck_Node) = Universal_Real | |
1649 | and then Is_Integer_Type (Target_Typ) | |
1650 | and then Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
1651 | then | |
1652 | declare | |
1653 | Int_Val : constant Uint := UR_To_Uint (Realval (Ck_Node)); | |
1654 | ||
1655 | begin | |
1656 | if Int_Val <= Ilast and then Int_Val >= Ifirst then | |
1657 | ||
4309515d | 1658 | -- Conversion is safe |
7d86aa98 | 1659 | |
1660 | Rewrite (Parent (Ck_Node), | |
1661 | Make_Integer_Literal (Loc, UI_To_Int (Int_Val))); | |
1662 | Analyze_And_Resolve (Parent (Ck_Node), Target_Typ); | |
1663 | return; | |
1664 | end if; | |
1665 | end; | |
1666 | end if; | |
1667 | ||
5329ca64 | 1668 | -- Check against lower bound |
1669 | ||
2af58f67 | 1670 | if Truncate and then Ifirst > 0 then |
1671 | Lo := Pred (Expr_Type, UR_From_Uint (Ifirst)); | |
1672 | Lo_OK := False; | |
1673 | ||
1674 | elsif Truncate then | |
1675 | Lo := Succ (Expr_Type, UR_From_Uint (Ifirst - 1)); | |
1676 | Lo_OK := True; | |
1677 | ||
1678 | elsif abs (Ifirst) < Max_Bound then | |
5329ca64 | 1679 | Lo := UR_From_Uint (Ifirst) - Ureal_Half; |
1680 | Lo_OK := (Ifirst > 0); | |
2af58f67 | 1681 | |
5329ca64 | 1682 | else |
1683 | Lo := Machine (Expr_Type, UR_From_Uint (Ifirst), Round_Even, Ck_Node); | |
1684 | Lo_OK := (Lo >= UR_From_Uint (Ifirst)); | |
1685 | end if; | |
1686 | ||
1687 | if Lo_OK then | |
1688 | ||
1689 | -- Lo_Chk := (X >= Lo) | |
1690 | ||
1691 | Lo_Chk := Make_Op_Ge (Loc, | |
1692 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1693 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
1694 | ||
1695 | else | |
1696 | -- Lo_Chk := (X > Lo) | |
1697 | ||
1698 | Lo_Chk := Make_Op_Gt (Loc, | |
1699 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1700 | Right_Opnd => Make_Real_Literal (Loc, Lo)); | |
1701 | end if; | |
1702 | ||
1703 | -- Check against higher bound | |
1704 | ||
2af58f67 | 1705 | if Truncate and then Ilast < 0 then |
1706 | Hi := Succ (Expr_Type, UR_From_Uint (Ilast)); | |
1707 | Lo_OK := False; | |
1708 | ||
1709 | elsif Truncate then | |
1710 | Hi := Pred (Expr_Type, UR_From_Uint (Ilast + 1)); | |
1711 | Hi_OK := True; | |
1712 | ||
1713 | elsif abs (Ilast) < Max_Bound then | |
5329ca64 | 1714 | Hi := UR_From_Uint (Ilast) + Ureal_Half; |
1715 | Hi_OK := (Ilast < 0); | |
1716 | else | |
1717 | Hi := Machine (Expr_Type, UR_From_Uint (Ilast), Round_Even, Ck_Node); | |
1718 | Hi_OK := (Hi <= UR_From_Uint (Ilast)); | |
1719 | end if; | |
1720 | ||
1721 | if Hi_OK then | |
1722 | ||
1723 | -- Hi_Chk := (X <= Hi) | |
1724 | ||
1725 | Hi_Chk := Make_Op_Le (Loc, | |
1726 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1727 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
1728 | ||
1729 | else | |
1730 | -- Hi_Chk := (X < Hi) | |
1731 | ||
1732 | Hi_Chk := Make_Op_Lt (Loc, | |
1733 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), | |
1734 | Right_Opnd => Make_Real_Literal (Loc, Hi)); | |
1735 | end if; | |
1736 | ||
feff2f05 | 1737 | -- If the bounds of the target type are the same as those of the base |
1738 | -- type, the check is an overflow check as a range check is not | |
1739 | -- performed in these cases. | |
5329ca64 | 1740 | |
1741 | if Expr_Value (Type_Low_Bound (Target_Base)) = Ifirst | |
1742 | and then Expr_Value (Type_High_Bound (Target_Base)) = Ilast | |
1743 | then | |
1744 | Reason := CE_Overflow_Check_Failed; | |
1745 | else | |
1746 | Reason := CE_Range_Check_Failed; | |
1747 | end if; | |
1748 | ||
1749 | -- Raise CE if either conditions does not hold | |
1750 | ||
1751 | Insert_Action (Ck_Node, | |
1752 | Make_Raise_Constraint_Error (Loc, | |
05fcfafb | 1753 | Condition => Make_Op_Not (Loc, Make_And_Then (Loc, Lo_Chk, Hi_Chk)), |
5329ca64 | 1754 | Reason => Reason)); |
1755 | end Apply_Float_Conversion_Check; | |
1756 | ||
ee6ba406 | 1757 | ------------------------ |
1758 | -- Apply_Length_Check -- | |
1759 | ------------------------ | |
1760 | ||
1761 | procedure Apply_Length_Check | |
1762 | (Ck_Node : Node_Id; | |
1763 | Target_Typ : Entity_Id; | |
1764 | Source_Typ : Entity_Id := Empty) | |
1765 | is | |
1766 | begin | |
1767 | Apply_Selected_Length_Checks | |
1768 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
1769 | end Apply_Length_Check; | |
1770 | ||
1771 | ----------------------- | |
1772 | -- Apply_Range_Check -- | |
1773 | ----------------------- | |
1774 | ||
1775 | procedure Apply_Range_Check | |
1776 | (Ck_Node : Node_Id; | |
1777 | Target_Typ : Entity_Id; | |
1778 | Source_Typ : Entity_Id := Empty) | |
1779 | is | |
1780 | begin | |
1781 | Apply_Selected_Range_Checks | |
1782 | (Ck_Node, Target_Typ, Source_Typ, Do_Static => False); | |
1783 | end Apply_Range_Check; | |
1784 | ||
1785 | ------------------------------ | |
1786 | -- Apply_Scalar_Range_Check -- | |
1787 | ------------------------------ | |
1788 | ||
feff2f05 | 1789 | -- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check flag |
1790 | -- off if it is already set on. | |
ee6ba406 | 1791 | |
1792 | procedure Apply_Scalar_Range_Check | |
1793 | (Expr : Node_Id; | |
1794 | Target_Typ : Entity_Id; | |
1795 | Source_Typ : Entity_Id := Empty; | |
1796 | Fixed_Int : Boolean := False) | |
1797 | is | |
1798 | Parnt : constant Node_Id := Parent (Expr); | |
1799 | S_Typ : Entity_Id; | |
1800 | Arr : Node_Id := Empty; -- initialize to prevent warning | |
1801 | Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning | |
1802 | OK : Boolean; | |
1803 | ||
1804 | Is_Subscr_Ref : Boolean; | |
1805 | -- Set true if Expr is a subscript | |
1806 | ||
1807 | Is_Unconstrained_Subscr_Ref : Boolean; | |
1808 | -- Set true if Expr is a subscript of an unconstrained array. In this | |
1809 | -- case we do not attempt to do an analysis of the value against the | |
1810 | -- range of the subscript, since we don't know the actual subtype. | |
1811 | ||
1812 | Int_Real : Boolean; | |
feff2f05 | 1813 | -- Set to True if Expr should be regarded as a real value even though |
1814 | -- the type of Expr might be discrete. | |
ee6ba406 | 1815 | |
1816 | procedure Bad_Value; | |
1817 | -- Procedure called if value is determined to be out of range | |
1818 | ||
9dfe12ae | 1819 | --------------- |
1820 | -- Bad_Value -- | |
1821 | --------------- | |
1822 | ||
ee6ba406 | 1823 | procedure Bad_Value is |
1824 | begin | |
1825 | Apply_Compile_Time_Constraint_Error | |
f15731c4 | 1826 | (Expr, "value not in range of}?", CE_Range_Check_Failed, |
ee6ba406 | 1827 | Ent => Target_Typ, |
1828 | Typ => Target_Typ); | |
1829 | end Bad_Value; | |
1830 | ||
9dfe12ae | 1831 | -- Start of processing for Apply_Scalar_Range_Check |
1832 | ||
ee6ba406 | 1833 | begin |
2af58f67 | 1834 | -- Return if check obviously not needed |
ee6ba406 | 1835 | |
2af58f67 | 1836 | if |
1837 | -- Not needed inside generic | |
ee6ba406 | 1838 | |
2af58f67 | 1839 | Inside_A_Generic |
1840 | ||
1841 | -- Not needed if previous error | |
1842 | ||
1843 | or else Target_Typ = Any_Type | |
1844 | or else Nkind (Expr) = N_Error | |
1845 | ||
1846 | -- Not needed for non-scalar type | |
1847 | ||
1848 | or else not Is_Scalar_Type (Target_Typ) | |
1849 | ||
1850 | -- Not needed if we know node raises CE already | |
1851 | ||
1852 | or else Raises_Constraint_Error (Expr) | |
ee6ba406 | 1853 | then |
1854 | return; | |
1855 | end if; | |
1856 | ||
1857 | -- Now, see if checks are suppressed | |
1858 | ||
1859 | Is_Subscr_Ref := | |
1860 | Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component; | |
1861 | ||
1862 | if Is_Subscr_Ref then | |
1863 | Arr := Prefix (Parnt); | |
1864 | Arr_Typ := Get_Actual_Subtype_If_Available (Arr); | |
1865 | end if; | |
1866 | ||
1867 | if not Do_Range_Check (Expr) then | |
1868 | ||
1869 | -- Subscript reference. Check for Index_Checks suppressed | |
1870 | ||
1871 | if Is_Subscr_Ref then | |
1872 | ||
1873 | -- Check array type and its base type | |
1874 | ||
1875 | if Index_Checks_Suppressed (Arr_Typ) | |
9dfe12ae | 1876 | or else Index_Checks_Suppressed (Base_Type (Arr_Typ)) |
ee6ba406 | 1877 | then |
1878 | return; | |
1879 | ||
1880 | -- Check array itself if it is an entity name | |
1881 | ||
1882 | elsif Is_Entity_Name (Arr) | |
9dfe12ae | 1883 | and then Index_Checks_Suppressed (Entity (Arr)) |
ee6ba406 | 1884 | then |
1885 | return; | |
1886 | ||
1887 | -- Check expression itself if it is an entity name | |
1888 | ||
1889 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 1890 | and then Index_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 1891 | then |
1892 | return; | |
1893 | end if; | |
1894 | ||
1895 | -- All other cases, check for Range_Checks suppressed | |
1896 | ||
1897 | else | |
1898 | -- Check target type and its base type | |
1899 | ||
1900 | if Range_Checks_Suppressed (Target_Typ) | |
9dfe12ae | 1901 | or else Range_Checks_Suppressed (Base_Type (Target_Typ)) |
ee6ba406 | 1902 | then |
1903 | return; | |
1904 | ||
1905 | -- Check expression itself if it is an entity name | |
1906 | ||
1907 | elsif Is_Entity_Name (Expr) | |
9dfe12ae | 1908 | and then Range_Checks_Suppressed (Entity (Expr)) |
ee6ba406 | 1909 | then |
1910 | return; | |
1911 | ||
feff2f05 | 1912 | -- If Expr is part of an assignment statement, then check left |
1913 | -- side of assignment if it is an entity name. | |
ee6ba406 | 1914 | |
1915 | elsif Nkind (Parnt) = N_Assignment_Statement | |
1916 | and then Is_Entity_Name (Name (Parnt)) | |
9dfe12ae | 1917 | and then Range_Checks_Suppressed (Entity (Name (Parnt))) |
ee6ba406 | 1918 | then |
1919 | return; | |
1920 | end if; | |
1921 | end if; | |
1922 | end if; | |
1923 | ||
9dfe12ae | 1924 | -- Do not set range checks if they are killed |
1925 | ||
1926 | if Nkind (Expr) = N_Unchecked_Type_Conversion | |
1927 | and then Kill_Range_Check (Expr) | |
1928 | then | |
1929 | return; | |
1930 | end if; | |
1931 | ||
1932 | -- Do not set range checks for any values from System.Scalar_Values | |
1933 | -- since the whole idea of such values is to avoid checking them! | |
1934 | ||
1935 | if Is_Entity_Name (Expr) | |
1936 | and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values) | |
1937 | then | |
1938 | return; | |
1939 | end if; | |
1940 | ||
ee6ba406 | 1941 | -- Now see if we need a check |
1942 | ||
1943 | if No (Source_Typ) then | |
1944 | S_Typ := Etype (Expr); | |
1945 | else | |
1946 | S_Typ := Source_Typ; | |
1947 | end if; | |
1948 | ||
1949 | if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then | |
1950 | return; | |
1951 | end if; | |
1952 | ||
1953 | Is_Unconstrained_Subscr_Ref := | |
1954 | Is_Subscr_Ref and then not Is_Constrained (Arr_Typ); | |
1955 | ||
feff2f05 | 1956 | -- Always do a range check if the source type includes infinities and |
1957 | -- the target type does not include infinities. We do not do this if | |
1958 | -- range checks are killed. | |
ee6ba406 | 1959 | |
1960 | if Is_Floating_Point_Type (S_Typ) | |
1961 | and then Has_Infinities (S_Typ) | |
1962 | and then not Has_Infinities (Target_Typ) | |
1963 | then | |
1964 | Enable_Range_Check (Expr); | |
1965 | end if; | |
1966 | ||
feff2f05 | 1967 | -- Return if we know expression is definitely in the range of the target |
1968 | -- type as determined by Determine_Range. Right now we only do this for | |
1969 | -- discrete types, and not fixed-point or floating-point types. | |
ee6ba406 | 1970 | |
f2a06be9 | 1971 | -- The additional less-precise tests below catch these cases |
ee6ba406 | 1972 | |
feff2f05 | 1973 | -- Note: skip this if we are given a source_typ, since the point of |
1974 | -- supplying a Source_Typ is to stop us looking at the expression. | |
1975 | -- We could sharpen this test to be out parameters only ??? | |
ee6ba406 | 1976 | |
1977 | if Is_Discrete_Type (Target_Typ) | |
1978 | and then Is_Discrete_Type (Etype (Expr)) | |
1979 | and then not Is_Unconstrained_Subscr_Ref | |
1980 | and then No (Source_Typ) | |
1981 | then | |
1982 | declare | |
1983 | Tlo : constant Node_Id := Type_Low_Bound (Target_Typ); | |
1984 | Thi : constant Node_Id := Type_High_Bound (Target_Typ); | |
1985 | Lo : Uint; | |
1986 | Hi : Uint; | |
1987 | ||
1988 | begin | |
1989 | if Compile_Time_Known_Value (Tlo) | |
1990 | and then Compile_Time_Known_Value (Thi) | |
1991 | then | |
9dfe12ae | 1992 | declare |
1993 | Lov : constant Uint := Expr_Value (Tlo); | |
1994 | Hiv : constant Uint := Expr_Value (Thi); | |
ee6ba406 | 1995 | |
9dfe12ae | 1996 | begin |
1997 | -- If range is null, we for sure have a constraint error | |
1998 | -- (we don't even need to look at the value involved, | |
1999 | -- since all possible values will raise CE). | |
2000 | ||
2001 | if Lov > Hiv then | |
2002 | Bad_Value; | |
2003 | return; | |
2004 | end if; | |
2005 | ||
2006 | -- Otherwise determine range of value | |
2007 | ||
9c486805 | 2008 | Determine_Range (Expr, OK, Lo, Hi, Assume_Valid => True); |
9dfe12ae | 2009 | |
2010 | if OK then | |
2011 | ||
2012 | -- If definitely in range, all OK | |
ee6ba406 | 2013 | |
ee6ba406 | 2014 | if Lo >= Lov and then Hi <= Hiv then |
2015 | return; | |
2016 | ||
9dfe12ae | 2017 | -- If definitely not in range, warn |
2018 | ||
ee6ba406 | 2019 | elsif Lov > Hi or else Hiv < Lo then |
2020 | Bad_Value; | |
2021 | return; | |
9dfe12ae | 2022 | |
2023 | -- Otherwise we don't know | |
2024 | ||
2025 | else | |
2026 | null; | |
ee6ba406 | 2027 | end if; |
9dfe12ae | 2028 | end if; |
2029 | end; | |
ee6ba406 | 2030 | end if; |
2031 | end; | |
2032 | end if; | |
2033 | ||
2034 | Int_Real := | |
2035 | Is_Floating_Point_Type (S_Typ) | |
2036 | or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int); | |
2037 | ||
2038 | -- Check if we can determine at compile time whether Expr is in the | |
9dfe12ae | 2039 | -- range of the target type. Note that if S_Typ is within the bounds |
2040 | -- of Target_Typ then this must be the case. This check is meaningful | |
2041 | -- only if this is not a conversion between integer and real types. | |
ee6ba406 | 2042 | |
2043 | if not Is_Unconstrained_Subscr_Ref | |
2044 | and then | |
2045 | Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ) | |
2046 | and then | |
7a1dabb3 | 2047 | (In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int) |
ee6ba406 | 2048 | or else |
9c486805 | 2049 | Is_In_Range (Expr, Target_Typ, |
2050 | Assume_Valid => True, | |
2051 | Fixed_Int => Fixed_Int, | |
2052 | Int_Real => Int_Real)) | |
ee6ba406 | 2053 | then |
2054 | return; | |
2055 | ||
9c486805 | 2056 | elsif Is_Out_Of_Range (Expr, Target_Typ, |
2057 | Assume_Valid => True, | |
2058 | Fixed_Int => Fixed_Int, | |
2059 | Int_Real => Int_Real) | |
2060 | then | |
ee6ba406 | 2061 | Bad_Value; |
2062 | return; | |
2063 | ||
feff2f05 | 2064 | -- In the floating-point case, we only do range checks if the type is |
2065 | -- constrained. We definitely do NOT want range checks for unconstrained | |
2066 | -- types, since we want to have infinities | |
ee6ba406 | 2067 | |
9dfe12ae | 2068 | elsif Is_Floating_Point_Type (S_Typ) then |
2069 | if Is_Constrained (S_Typ) then | |
2070 | Enable_Range_Check (Expr); | |
2071 | end if; | |
ee6ba406 | 2072 | |
9dfe12ae | 2073 | -- For all other cases we enable a range check unconditionally |
ee6ba406 | 2074 | |
2075 | else | |
2076 | Enable_Range_Check (Expr); | |
2077 | return; | |
2078 | end if; | |
ee6ba406 | 2079 | end Apply_Scalar_Range_Check; |
2080 | ||
2081 | ---------------------------------- | |
2082 | -- Apply_Selected_Length_Checks -- | |
2083 | ---------------------------------- | |
2084 | ||
2085 | procedure Apply_Selected_Length_Checks | |
2086 | (Ck_Node : Node_Id; | |
2087 | Target_Typ : Entity_Id; | |
2088 | Source_Typ : Entity_Id; | |
2089 | Do_Static : Boolean) | |
2090 | is | |
2091 | Cond : Node_Id; | |
2092 | R_Result : Check_Result; | |
2093 | R_Cno : Node_Id; | |
2094 | ||
2095 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2096 | Checks_On : constant Boolean := | |
2097 | (not Index_Checks_Suppressed (Target_Typ)) | |
2098 | or else | |
2099 | (not Length_Checks_Suppressed (Target_Typ)); | |
2100 | ||
2101 | begin | |
f15731c4 | 2102 | if not Expander_Active then |
ee6ba406 | 2103 | return; |
2104 | end if; | |
2105 | ||
2106 | R_Result := | |
2107 | Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
2108 | ||
2109 | for J in 1 .. 2 loop | |
ee6ba406 | 2110 | R_Cno := R_Result (J); |
2111 | exit when No (R_Cno); | |
2112 | ||
2113 | -- A length check may mention an Itype which is attached to a | |
2114 | -- subsequent node. At the top level in a package this can cause | |
2115 | -- an order-of-elaboration problem, so we make sure that the itype | |
2116 | -- is referenced now. | |
2117 | ||
2118 | if Ekind (Current_Scope) = E_Package | |
2119 | and then Is_Compilation_Unit (Current_Scope) | |
2120 | then | |
2121 | Ensure_Defined (Target_Typ, Ck_Node); | |
2122 | ||
2123 | if Present (Source_Typ) then | |
2124 | Ensure_Defined (Source_Typ, Ck_Node); | |
2125 | ||
2126 | elsif Is_Itype (Etype (Ck_Node)) then | |
2127 | Ensure_Defined (Etype (Ck_Node), Ck_Node); | |
2128 | end if; | |
2129 | end if; | |
2130 | ||
feff2f05 | 2131 | -- If the item is a conditional raise of constraint error, then have |
2132 | -- a look at what check is being performed and ??? | |
ee6ba406 | 2133 | |
2134 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2135 | and then Present (Condition (R_Cno)) | |
2136 | then | |
2137 | Cond := Condition (R_Cno); | |
2138 | ||
0577b0b1 | 2139 | -- Case where node does not now have a dynamic check |
ee6ba406 | 2140 | |
0577b0b1 | 2141 | if not Has_Dynamic_Length_Check (Ck_Node) then |
2142 | ||
2143 | -- If checks are on, just insert the check | |
2144 | ||
2145 | if Checks_On then | |
2146 | Insert_Action (Ck_Node, R_Cno); | |
2147 | ||
2148 | if not Do_Static then | |
2149 | Set_Has_Dynamic_Length_Check (Ck_Node); | |
2150 | end if; | |
2151 | ||
2152 | -- If checks are off, then analyze the length check after | |
2153 | -- temporarily attaching it to the tree in case the relevant | |
2154 | -- condition can be evaluted at compile time. We still want a | |
2155 | -- compile time warning in this case. | |
2156 | ||
2157 | else | |
2158 | Set_Parent (R_Cno, Ck_Node); | |
2159 | Analyze (R_Cno); | |
ee6ba406 | 2160 | end if; |
ee6ba406 | 2161 | end if; |
2162 | ||
2163 | -- Output a warning if the condition is known to be True | |
2164 | ||
2165 | if Is_Entity_Name (Cond) | |
2166 | and then Entity (Cond) = Standard_True | |
2167 | then | |
2168 | Apply_Compile_Time_Constraint_Error | |
2169 | (Ck_Node, "wrong length for array of}?", | |
f15731c4 | 2170 | CE_Length_Check_Failed, |
ee6ba406 | 2171 | Ent => Target_Typ, |
2172 | Typ => Target_Typ); | |
2173 | ||
2174 | -- If we were only doing a static check, or if checks are not | |
2175 | -- on, then we want to delete the check, since it is not needed. | |
2176 | -- We do this by replacing the if statement by a null statement | |
2177 | ||
2178 | elsif Do_Static or else not Checks_On then | |
00c403ee | 2179 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 2180 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
2181 | end if; | |
2182 | ||
2183 | else | |
2184 | Install_Static_Check (R_Cno, Loc); | |
2185 | end if; | |
ee6ba406 | 2186 | end loop; |
ee6ba406 | 2187 | end Apply_Selected_Length_Checks; |
2188 | ||
2189 | --------------------------------- | |
2190 | -- Apply_Selected_Range_Checks -- | |
2191 | --------------------------------- | |
2192 | ||
2193 | procedure Apply_Selected_Range_Checks | |
2194 | (Ck_Node : Node_Id; | |
2195 | Target_Typ : Entity_Id; | |
2196 | Source_Typ : Entity_Id; | |
2197 | Do_Static : Boolean) | |
2198 | is | |
2199 | Cond : Node_Id; | |
2200 | R_Result : Check_Result; | |
2201 | R_Cno : Node_Id; | |
2202 | ||
2203 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
2204 | Checks_On : constant Boolean := | |
2205 | (not Index_Checks_Suppressed (Target_Typ)) | |
2206 | or else | |
2207 | (not Range_Checks_Suppressed (Target_Typ)); | |
2208 | ||
2209 | begin | |
2210 | if not Expander_Active or else not Checks_On then | |
2211 | return; | |
2212 | end if; | |
2213 | ||
2214 | R_Result := | |
2215 | Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty); | |
2216 | ||
2217 | for J in 1 .. 2 loop | |
2218 | ||
2219 | R_Cno := R_Result (J); | |
2220 | exit when No (R_Cno); | |
2221 | ||
feff2f05 | 2222 | -- If the item is a conditional raise of constraint error, then have |
2223 | -- a look at what check is being performed and ??? | |
ee6ba406 | 2224 | |
2225 | if Nkind (R_Cno) = N_Raise_Constraint_Error | |
2226 | and then Present (Condition (R_Cno)) | |
2227 | then | |
2228 | Cond := Condition (R_Cno); | |
2229 | ||
2230 | if not Has_Dynamic_Range_Check (Ck_Node) then | |
2231 | Insert_Action (Ck_Node, R_Cno); | |
2232 | ||
2233 | if not Do_Static then | |
2234 | Set_Has_Dynamic_Range_Check (Ck_Node); | |
2235 | end if; | |
2236 | end if; | |
2237 | ||
2238 | -- Output a warning if the condition is known to be True | |
2239 | ||
2240 | if Is_Entity_Name (Cond) | |
2241 | and then Entity (Cond) = Standard_True | |
2242 | then | |
feff2f05 | 2243 | -- Since an N_Range is technically not an expression, we have |
2244 | -- to set one of the bounds to C_E and then just flag the | |
2245 | -- N_Range. The warning message will point to the lower bound | |
2246 | -- and complain about a range, which seems OK. | |
ee6ba406 | 2247 | |
2248 | if Nkind (Ck_Node) = N_Range then | |
2249 | Apply_Compile_Time_Constraint_Error | |
2250 | (Low_Bound (Ck_Node), "static range out of bounds of}?", | |
f15731c4 | 2251 | CE_Range_Check_Failed, |
ee6ba406 | 2252 | Ent => Target_Typ, |
2253 | Typ => Target_Typ); | |
2254 | ||
2255 | Set_Raises_Constraint_Error (Ck_Node); | |
2256 | ||
2257 | else | |
2258 | Apply_Compile_Time_Constraint_Error | |
2259 | (Ck_Node, "static value out of range of}?", | |
f15731c4 | 2260 | CE_Range_Check_Failed, |
ee6ba406 | 2261 | Ent => Target_Typ, |
2262 | Typ => Target_Typ); | |
2263 | end if; | |
2264 | ||
2265 | -- If we were only doing a static check, or if checks are not | |
2266 | -- on, then we want to delete the check, since it is not needed. | |
2267 | -- We do this by replacing the if statement by a null statement | |
2268 | ||
2269 | elsif Do_Static or else not Checks_On then | |
00c403ee | 2270 | Remove_Warning_Messages (R_Cno); |
ee6ba406 | 2271 | Rewrite (R_Cno, Make_Null_Statement (Loc)); |
2272 | end if; | |
2273 | ||
2274 | else | |
2275 | Install_Static_Check (R_Cno, Loc); | |
2276 | end if; | |
ee6ba406 | 2277 | end loop; |
ee6ba406 | 2278 | end Apply_Selected_Range_Checks; |
2279 | ||
2280 | ------------------------------- | |
2281 | -- Apply_Static_Length_Check -- | |
2282 | ------------------------------- | |
2283 | ||
2284 | procedure Apply_Static_Length_Check | |
2285 | (Expr : Node_Id; | |
2286 | Target_Typ : Entity_Id; | |
2287 | Source_Typ : Entity_Id := Empty) | |
2288 | is | |
2289 | begin | |
2290 | Apply_Selected_Length_Checks | |
2291 | (Expr, Target_Typ, Source_Typ, Do_Static => True); | |
2292 | end Apply_Static_Length_Check; | |
2293 | ||
2294 | ------------------------------------- | |
2295 | -- Apply_Subscript_Validity_Checks -- | |
2296 | ------------------------------------- | |
2297 | ||
2298 | procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is | |
2299 | Sub : Node_Id; | |
2300 | ||
2301 | begin | |
2302 | pragma Assert (Nkind (Expr) = N_Indexed_Component); | |
2303 | ||
2304 | -- Loop through subscripts | |
2305 | ||
2306 | Sub := First (Expressions (Expr)); | |
2307 | while Present (Sub) loop | |
2308 | ||
feff2f05 | 2309 | -- Check one subscript. Note that we do not worry about enumeration |
2310 | -- type with holes, since we will convert the value to a Pos value | |
2311 | -- for the subscript, and that convert will do the necessary validity | |
2312 | -- check. | |
ee6ba406 | 2313 | |
2314 | Ensure_Valid (Sub, Holes_OK => True); | |
2315 | ||
2316 | -- Move to next subscript | |
2317 | ||
2318 | Sub := Next (Sub); | |
2319 | end loop; | |
2320 | end Apply_Subscript_Validity_Checks; | |
2321 | ||
2322 | ---------------------------------- | |
2323 | -- Apply_Type_Conversion_Checks -- | |
2324 | ---------------------------------- | |
2325 | ||
2326 | procedure Apply_Type_Conversion_Checks (N : Node_Id) is | |
2327 | Target_Type : constant Entity_Id := Etype (N); | |
2328 | Target_Base : constant Entity_Id := Base_Type (Target_Type); | |
9dfe12ae | 2329 | Expr : constant Node_Id := Expression (N); |
2330 | Expr_Type : constant Entity_Id := Etype (Expr); | |
ee6ba406 | 2331 | |
2332 | begin | |
2333 | if Inside_A_Generic then | |
2334 | return; | |
2335 | ||
f15731c4 | 2336 | -- Skip these checks if serious errors detected, there are some nasty |
ee6ba406 | 2337 | -- situations of incomplete trees that blow things up. |
2338 | ||
f15731c4 | 2339 | elsif Serious_Errors_Detected > 0 then |
ee6ba406 | 2340 | return; |
2341 | ||
feff2f05 | 2342 | -- Scalar type conversions of the form Target_Type (Expr) require a |
2343 | -- range check if we cannot be sure that Expr is in the base type of | |
2344 | -- Target_Typ and also that Expr is in the range of Target_Typ. These | |
2345 | -- are not quite the same condition from an implementation point of | |
2346 | -- view, but clearly the second includes the first. | |
ee6ba406 | 2347 | |
2348 | elsif Is_Scalar_Type (Target_Type) then | |
2349 | declare | |
2350 | Conv_OK : constant Boolean := Conversion_OK (N); | |
feff2f05 | 2351 | -- If the Conversion_OK flag on the type conversion is set and no |
2352 | -- floating point type is involved in the type conversion then | |
2353 | -- fixed point values must be read as integral values. | |
ee6ba406 | 2354 | |
5329ca64 | 2355 | Float_To_Int : constant Boolean := |
2356 | Is_Floating_Point_Type (Expr_Type) | |
2357 | and then Is_Integer_Type (Target_Type); | |
2358 | ||
ee6ba406 | 2359 | begin |
ee6ba406 | 2360 | if not Overflow_Checks_Suppressed (Target_Base) |
e254d721 | 2361 | and then not |
7a1dabb3 | 2362 | In_Subrange_Of (Expr_Type, Target_Base, Fixed_Int => Conv_OK) |
5329ca64 | 2363 | and then not Float_To_Int |
ee6ba406 | 2364 | then |
00c403ee | 2365 | Activate_Overflow_Check (N); |
ee6ba406 | 2366 | end if; |
2367 | ||
2368 | if not Range_Checks_Suppressed (Target_Type) | |
2369 | and then not Range_Checks_Suppressed (Expr_Type) | |
2370 | then | |
5329ca64 | 2371 | if Float_To_Int then |
2372 | Apply_Float_Conversion_Check (Expr, Target_Type); | |
2373 | else | |
2374 | Apply_Scalar_Range_Check | |
2375 | (Expr, Target_Type, Fixed_Int => Conv_OK); | |
2376 | end if; | |
ee6ba406 | 2377 | end if; |
2378 | end; | |
2379 | ||
2380 | elsif Comes_From_Source (N) | |
f40f9731 | 2381 | and then not Discriminant_Checks_Suppressed (Target_Type) |
ee6ba406 | 2382 | and then Is_Record_Type (Target_Type) |
2383 | and then Is_Derived_Type (Target_Type) | |
2384 | and then not Is_Tagged_Type (Target_Type) | |
2385 | and then not Is_Constrained (Target_Type) | |
9dfe12ae | 2386 | and then Present (Stored_Constraint (Target_Type)) |
ee6ba406 | 2387 | then |
9dfe12ae | 2388 | -- An unconstrained derived type may have inherited discriminant |
2389 | -- Build an actual discriminant constraint list using the stored | |
ee6ba406 | 2390 | -- constraint, to verify that the expression of the parent type |
2391 | -- satisfies the constraints imposed by the (unconstrained!) | |
2392 | -- derived type. This applies to value conversions, not to view | |
2393 | -- conversions of tagged types. | |
2394 | ||
2395 | declare | |
9dfe12ae | 2396 | Loc : constant Source_Ptr := Sloc (N); |
2397 | Cond : Node_Id; | |
2398 | Constraint : Elmt_Id; | |
2399 | Discr_Value : Node_Id; | |
2400 | Discr : Entity_Id; | |
2401 | ||
2402 | New_Constraints : constant Elist_Id := New_Elmt_List; | |
2403 | Old_Constraints : constant Elist_Id := | |
2404 | Discriminant_Constraint (Expr_Type); | |
ee6ba406 | 2405 | |
2406 | begin | |
9dfe12ae | 2407 | Constraint := First_Elmt (Stored_Constraint (Target_Type)); |
ee6ba406 | 2408 | while Present (Constraint) loop |
2409 | Discr_Value := Node (Constraint); | |
2410 | ||
2411 | if Is_Entity_Name (Discr_Value) | |
2412 | and then Ekind (Entity (Discr_Value)) = E_Discriminant | |
2413 | then | |
2414 | Discr := Corresponding_Discriminant (Entity (Discr_Value)); | |
2415 | ||
2416 | if Present (Discr) | |
2417 | and then Scope (Discr) = Base_Type (Expr_Type) | |
2418 | then | |
2419 | -- Parent is constrained by new discriminant. Obtain | |
feff2f05 | 2420 | -- Value of original discriminant in expression. If the |
2421 | -- new discriminant has been used to constrain more than | |
2422 | -- one of the stored discriminants, this will provide the | |
2423 | -- required consistency check. | |
ee6ba406 | 2424 | |
2425 | Append_Elmt ( | |
2426 | Make_Selected_Component (Loc, | |
2427 | Prefix => | |
9dfe12ae | 2428 | Duplicate_Subexpr_No_Checks |
2429 | (Expr, Name_Req => True), | |
ee6ba406 | 2430 | Selector_Name => |
2431 | Make_Identifier (Loc, Chars (Discr))), | |
2432 | New_Constraints); | |
2433 | ||
2434 | else | |
2435 | -- Discriminant of more remote ancestor ??? | |
2436 | ||
2437 | return; | |
2438 | end if; | |
2439 | ||
feff2f05 | 2440 | -- Derived type definition has an explicit value for this |
2441 | -- stored discriminant. | |
ee6ba406 | 2442 | |
2443 | else | |
2444 | Append_Elmt | |
9dfe12ae | 2445 | (Duplicate_Subexpr_No_Checks (Discr_Value), |
2446 | New_Constraints); | |
ee6ba406 | 2447 | end if; |
2448 | ||
2449 | Next_Elmt (Constraint); | |
2450 | end loop; | |
2451 | ||
2452 | -- Use the unconstrained expression type to retrieve the | |
2453 | -- discriminants of the parent, and apply momentarily the | |
2454 | -- discriminant constraint synthesized above. | |
2455 | ||
2456 | Set_Discriminant_Constraint (Expr_Type, New_Constraints); | |
2457 | Cond := Build_Discriminant_Checks (Expr, Expr_Type); | |
2458 | Set_Discriminant_Constraint (Expr_Type, Old_Constraints); | |
2459 | ||
2460 | Insert_Action (N, | |
f15731c4 | 2461 | Make_Raise_Constraint_Error (Loc, |
2462 | Condition => Cond, | |
2463 | Reason => CE_Discriminant_Check_Failed)); | |
ee6ba406 | 2464 | end; |
2465 | ||
feff2f05 | 2466 | -- For arrays, conversions are applied during expansion, to take into |
2467 | -- accounts changes of representation. The checks become range checks on | |
2468 | -- the base type or length checks on the subtype, depending on whether | |
2469 | -- the target type is unconstrained or constrained. | |
ee6ba406 | 2470 | |
2471 | else | |
2472 | null; | |
2473 | end if; | |
ee6ba406 | 2474 | end Apply_Type_Conversion_Checks; |
2475 | ||
2476 | ---------------------------------------------- | |
2477 | -- Apply_Universal_Integer_Attribute_Checks -- | |
2478 | ---------------------------------------------- | |
2479 | ||
2480 | procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is | |
2481 | Loc : constant Source_Ptr := Sloc (N); | |
2482 | Typ : constant Entity_Id := Etype (N); | |
2483 | ||
2484 | begin | |
2485 | if Inside_A_Generic then | |
2486 | return; | |
2487 | ||
2488 | -- Nothing to do if checks are suppressed | |
2489 | ||
2490 | elsif Range_Checks_Suppressed (Typ) | |
2491 | and then Overflow_Checks_Suppressed (Typ) | |
2492 | then | |
2493 | return; | |
2494 | ||
2495 | -- Nothing to do if the attribute does not come from source. The | |
2496 | -- internal attributes we generate of this type do not need checks, | |
2497 | -- and furthermore the attempt to check them causes some circular | |
2498 | -- elaboration orders when dealing with packed types. | |
2499 | ||
2500 | elsif not Comes_From_Source (N) then | |
2501 | return; | |
2502 | ||
9dfe12ae | 2503 | -- If the prefix is a selected component that depends on a discriminant |
2504 | -- the check may improperly expose a discriminant instead of using | |
2505 | -- the bounds of the object itself. Set the type of the attribute to | |
2506 | -- the base type of the context, so that a check will be imposed when | |
2507 | -- needed (e.g. if the node appears as an index). | |
2508 | ||
2509 | elsif Nkind (Prefix (N)) = N_Selected_Component | |
2510 | and then Ekind (Typ) = E_Signed_Integer_Subtype | |
2511 | and then Depends_On_Discriminant (Scalar_Range (Typ)) | |
2512 | then | |
2513 | Set_Etype (N, Base_Type (Typ)); | |
2514 | ||
feff2f05 | 2515 | -- Otherwise, replace the attribute node with a type conversion node |
2516 | -- whose expression is the attribute, retyped to universal integer, and | |
2517 | -- whose subtype mark is the target type. The call to analyze this | |
2518 | -- conversion will set range and overflow checks as required for proper | |
2519 | -- detection of an out of range value. | |
ee6ba406 | 2520 | |
2521 | else | |
2522 | Set_Etype (N, Universal_Integer); | |
2523 | Set_Analyzed (N, True); | |
2524 | ||
2525 | Rewrite (N, | |
2526 | Make_Type_Conversion (Loc, | |
2527 | Subtype_Mark => New_Occurrence_Of (Typ, Loc), | |
2528 | Expression => Relocate_Node (N))); | |
2529 | ||
2530 | Analyze_And_Resolve (N, Typ); | |
2531 | return; | |
2532 | end if; | |
ee6ba406 | 2533 | end Apply_Universal_Integer_Attribute_Checks; |
2534 | ||
2535 | ------------------------------- | |
2536 | -- Build_Discriminant_Checks -- | |
2537 | ------------------------------- | |
2538 | ||
2539 | function Build_Discriminant_Checks | |
2540 | (N : Node_Id; | |
314a23b6 | 2541 | T_Typ : Entity_Id) return Node_Id |
ee6ba406 | 2542 | is |
2543 | Loc : constant Source_Ptr := Sloc (N); | |
2544 | Cond : Node_Id; | |
2545 | Disc : Elmt_Id; | |
2546 | Disc_Ent : Entity_Id; | |
9dfe12ae | 2547 | Dref : Node_Id; |
ee6ba406 | 2548 | Dval : Node_Id; |
2549 | ||
84d0d4a5 | 2550 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id; |
2551 | ||
2552 | ---------------------------------- | |
2553 | -- Aggregate_Discriminant_Value -- | |
2554 | ---------------------------------- | |
2555 | ||
2556 | function Aggregate_Discriminant_Val (Disc : Entity_Id) return Node_Id is | |
2557 | Assoc : Node_Id; | |
2558 | ||
2559 | begin | |
feff2f05 | 2560 | -- The aggregate has been normalized with named associations. We use |
2561 | -- the Chars field to locate the discriminant to take into account | |
2562 | -- discriminants in derived types, which carry the same name as those | |
2563 | -- in the parent. | |
84d0d4a5 | 2564 | |
2565 | Assoc := First (Component_Associations (N)); | |
2566 | while Present (Assoc) loop | |
2567 | if Chars (First (Choices (Assoc))) = Chars (Disc) then | |
2568 | return Expression (Assoc); | |
2569 | else | |
2570 | Next (Assoc); | |
2571 | end if; | |
2572 | end loop; | |
2573 | ||
2574 | -- Discriminant must have been found in the loop above | |
2575 | ||
2576 | raise Program_Error; | |
2577 | end Aggregate_Discriminant_Val; | |
2578 | ||
2579 | -- Start of processing for Build_Discriminant_Checks | |
2580 | ||
ee6ba406 | 2581 | begin |
84d0d4a5 | 2582 | -- Loop through discriminants evolving the condition |
2583 | ||
ee6ba406 | 2584 | Cond := Empty; |
2585 | Disc := First_Elmt (Discriminant_Constraint (T_Typ)); | |
2586 | ||
9dfe12ae | 2587 | -- For a fully private type, use the discriminants of the parent type |
ee6ba406 | 2588 | |
2589 | if Is_Private_Type (T_Typ) | |
2590 | and then No (Full_View (T_Typ)) | |
2591 | then | |
2592 | Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ))); | |
2593 | else | |
2594 | Disc_Ent := First_Discriminant (T_Typ); | |
2595 | end if; | |
2596 | ||
2597 | while Present (Disc) loop | |
ee6ba406 | 2598 | Dval := Node (Disc); |
2599 | ||
2600 | if Nkind (Dval) = N_Identifier | |
2601 | and then Ekind (Entity (Dval)) = E_Discriminant | |
2602 | then | |
2603 | Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc); | |
2604 | else | |
9dfe12ae | 2605 | Dval := Duplicate_Subexpr_No_Checks (Dval); |
ee6ba406 | 2606 | end if; |
2607 | ||
00f91aef | 2608 | -- If we have an Unchecked_Union node, we can infer the discriminants |
2609 | -- of the node. | |
9dfe12ae | 2610 | |
00f91aef | 2611 | if Is_Unchecked_Union (Base_Type (T_Typ)) then |
2612 | Dref := New_Copy ( | |
2613 | Get_Discriminant_Value ( | |
2614 | First_Discriminant (T_Typ), | |
2615 | T_Typ, | |
2616 | Stored_Constraint (T_Typ))); | |
2617 | ||
84d0d4a5 | 2618 | elsif Nkind (N) = N_Aggregate then |
2619 | Dref := | |
2620 | Duplicate_Subexpr_No_Checks | |
2621 | (Aggregate_Discriminant_Val (Disc_Ent)); | |
2622 | ||
00f91aef | 2623 | else |
2624 | Dref := | |
2625 | Make_Selected_Component (Loc, | |
2626 | Prefix => | |
2627 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), | |
2628 | Selector_Name => | |
2629 | Make_Identifier (Loc, Chars (Disc_Ent))); | |
2630 | ||
2631 | Set_Is_In_Discriminant_Check (Dref); | |
2632 | end if; | |
9dfe12ae | 2633 | |
ee6ba406 | 2634 | Evolve_Or_Else (Cond, |
2635 | Make_Op_Ne (Loc, | |
9dfe12ae | 2636 | Left_Opnd => Dref, |
ee6ba406 | 2637 | Right_Opnd => Dval)); |
2638 | ||
2639 | Next_Elmt (Disc); | |
2640 | Next_Discriminant (Disc_Ent); | |
2641 | end loop; | |
2642 | ||
2643 | return Cond; | |
2644 | end Build_Discriminant_Checks; | |
2645 | ||
13dbf220 | 2646 | ------------------ |
2647 | -- Check_Needed -- | |
2648 | ------------------ | |
2649 | ||
2650 | function Check_Needed (Nod : Node_Id; Check : Check_Type) return Boolean is | |
2651 | N : Node_Id; | |
2652 | P : Node_Id; | |
2653 | K : Node_Kind; | |
2654 | L : Node_Id; | |
2655 | R : Node_Id; | |
2656 | ||
2657 | begin | |
2658 | -- Always check if not simple entity | |
2659 | ||
2660 | if Nkind (Nod) not in N_Has_Entity | |
2661 | or else not Comes_From_Source (Nod) | |
2662 | then | |
2663 | return True; | |
2664 | end if; | |
2665 | ||
2666 | -- Look up tree for short circuit | |
2667 | ||
2668 | N := Nod; | |
2669 | loop | |
2670 | P := Parent (N); | |
2671 | K := Nkind (P); | |
2672 | ||
7b17e51b | 2673 | -- Done if out of subexpression (note that we allow generated stuff |
2674 | -- such as itype declarations in this context, to keep the loop going | |
2675 | -- since we may well have generated such stuff in complex situations. | |
2676 | -- Also done if no parent (probably an error condition, but no point | |
2677 | -- in behaving nasty if we find it!) | |
2678 | ||
2679 | if No (P) | |
2680 | or else (K not in N_Subexpr and then Comes_From_Source (P)) | |
2681 | then | |
13dbf220 | 2682 | return True; |
2683 | ||
7b17e51b | 2684 | -- Or/Or Else case, where test is part of the right operand, or is |
2685 | -- part of one of the actions associated with the right operand, and | |
2686 | -- the left operand is an equality test. | |
13dbf220 | 2687 | |
7b17e51b | 2688 | elsif K = N_Op_Or then |
13dbf220 | 2689 | exit when N = Right_Opnd (P) |
2690 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
2691 | ||
7b17e51b | 2692 | elsif K = N_Or_Else then |
2693 | exit when (N = Right_Opnd (P) | |
2694 | or else | |
2695 | (Is_List_Member (N) | |
2696 | and then List_Containing (N) = Actions (P))) | |
2697 | and then Nkind (Left_Opnd (P)) = N_Op_Eq; | |
13dbf220 | 2698 | |
7b17e51b | 2699 | -- Similar test for the And/And then case, where the left operand |
2700 | -- is an inequality test. | |
2701 | ||
2702 | elsif K = N_Op_And then | |
13dbf220 | 2703 | exit when N = Right_Opnd (P) |
38f5559f | 2704 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; |
7b17e51b | 2705 | |
2706 | elsif K = N_And_Then then | |
2707 | exit when (N = Right_Opnd (P) | |
2708 | or else | |
2709 | (Is_List_Member (N) | |
2710 | and then List_Containing (N) = Actions (P))) | |
2711 | and then Nkind (Left_Opnd (P)) = N_Op_Ne; | |
13dbf220 | 2712 | end if; |
2713 | ||
2714 | N := P; | |
2715 | end loop; | |
2716 | ||
2717 | -- If we fall through the loop, then we have a conditional with an | |
2718 | -- appropriate test as its left operand. So test further. | |
2719 | ||
2720 | L := Left_Opnd (P); | |
13dbf220 | 2721 | R := Right_Opnd (L); |
2722 | L := Left_Opnd (L); | |
2723 | ||
2724 | -- Left operand of test must match original variable | |
2725 | ||
2726 | if Nkind (L) not in N_Has_Entity | |
2727 | or else Entity (L) /= Entity (Nod) | |
2728 | then | |
2729 | return True; | |
2730 | end if; | |
2731 | ||
2af58f67 | 2732 | -- Right operand of test must be key value (zero or null) |
13dbf220 | 2733 | |
2734 | case Check is | |
2735 | when Access_Check => | |
2af58f67 | 2736 | if not Known_Null (R) then |
13dbf220 | 2737 | return True; |
2738 | end if; | |
2739 | ||
2740 | when Division_Check => | |
2741 | if not Compile_Time_Known_Value (R) | |
2742 | or else Expr_Value (R) /= Uint_0 | |
2743 | then | |
2744 | return True; | |
2745 | end if; | |
2af58f67 | 2746 | |
2747 | when others => | |
2748 | raise Program_Error; | |
13dbf220 | 2749 | end case; |
2750 | ||
2751 | -- Here we have the optimizable case, warn if not short-circuited | |
2752 | ||
2753 | if K = N_Op_And or else K = N_Op_Or then | |
2754 | case Check is | |
2755 | when Access_Check => | |
2756 | Error_Msg_N | |
2757 | ("Constraint_Error may be raised (access check)?", | |
2758 | Parent (Nod)); | |
2759 | when Division_Check => | |
2760 | Error_Msg_N | |
2761 | ("Constraint_Error may be raised (zero divide)?", | |
2762 | Parent (Nod)); | |
2af58f67 | 2763 | |
2764 | when others => | |
2765 | raise Program_Error; | |
13dbf220 | 2766 | end case; |
2767 | ||
2768 | if K = N_Op_And then | |
2769 | Error_Msg_N ("use `AND THEN` instead of AND?", P); | |
2770 | else | |
2771 | Error_Msg_N ("use `OR ELSE` instead of OR?", P); | |
2772 | end if; | |
2773 | ||
2774 | -- If not short-circuited, we need the ckeck | |
2775 | ||
2776 | return True; | |
2777 | ||
2778 | -- If short-circuited, we can omit the check | |
2779 | ||
2780 | else | |
2781 | return False; | |
2782 | end if; | |
2783 | end Check_Needed; | |
2784 | ||
ee6ba406 | 2785 | ----------------------------------- |
2786 | -- Check_Valid_Lvalue_Subscripts -- | |
2787 | ----------------------------------- | |
2788 | ||
2789 | procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is | |
2790 | begin | |
2791 | -- Skip this if range checks are suppressed | |
2792 | ||
2793 | if Range_Checks_Suppressed (Etype (Expr)) then | |
2794 | return; | |
2795 | ||
feff2f05 | 2796 | -- Only do this check for expressions that come from source. We assume |
2797 | -- that expander generated assignments explicitly include any necessary | |
2798 | -- checks. Note that this is not just an optimization, it avoids | |
2799 | -- infinite recursions! | |
ee6ba406 | 2800 | |
2801 | elsif not Comes_From_Source (Expr) then | |
2802 | return; | |
2803 | ||
2804 | -- For a selected component, check the prefix | |
2805 | ||
2806 | elsif Nkind (Expr) = N_Selected_Component then | |
2807 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
2808 | return; | |
2809 | ||
2810 | -- Case of indexed component | |
2811 | ||
2812 | elsif Nkind (Expr) = N_Indexed_Component then | |
2813 | Apply_Subscript_Validity_Checks (Expr); | |
2814 | ||
feff2f05 | 2815 | -- Prefix may itself be or contain an indexed component, and these |
2816 | -- subscripts need checking as well. | |
ee6ba406 | 2817 | |
2818 | Check_Valid_Lvalue_Subscripts (Prefix (Expr)); | |
2819 | end if; | |
2820 | end Check_Valid_Lvalue_Subscripts; | |
2821 | ||
fa7497e8 | 2822 | ---------------------------------- |
2823 | -- Null_Exclusion_Static_Checks -- | |
2824 | ---------------------------------- | |
2825 | ||
2826 | procedure Null_Exclusion_Static_Checks (N : Node_Id) is | |
0577b0b1 | 2827 | Error_Node : Node_Id; |
2828 | Expr : Node_Id; | |
2829 | Has_Null : constant Boolean := Has_Null_Exclusion (N); | |
2830 | K : constant Node_Kind := Nkind (N); | |
2831 | Typ : Entity_Id; | |
fa7497e8 | 2832 | |
13dbf220 | 2833 | begin |
0577b0b1 | 2834 | pragma Assert |
2835 | (K = N_Component_Declaration | |
2836 | or else K = N_Discriminant_Specification | |
2837 | or else K = N_Function_Specification | |
2838 | or else K = N_Object_Declaration | |
2839 | or else K = N_Parameter_Specification); | |
2840 | ||
2841 | if K = N_Function_Specification then | |
2842 | Typ := Etype (Defining_Entity (N)); | |
2843 | else | |
2844 | Typ := Etype (Defining_Identifier (N)); | |
2845 | end if; | |
fa7497e8 | 2846 | |
13dbf220 | 2847 | case K is |
13dbf220 | 2848 | when N_Component_Declaration => |
2849 | if Present (Access_Definition (Component_Definition (N))) then | |
0577b0b1 | 2850 | Error_Node := Component_Definition (N); |
13dbf220 | 2851 | else |
0577b0b1 | 2852 | Error_Node := Subtype_Indication (Component_Definition (N)); |
13dbf220 | 2853 | end if; |
5329ca64 | 2854 | |
0577b0b1 | 2855 | when N_Discriminant_Specification => |
2856 | Error_Node := Discriminant_Type (N); | |
2857 | ||
2858 | when N_Function_Specification => | |
2859 | Error_Node := Result_Definition (N); | |
2860 | ||
2861 | when N_Object_Declaration => | |
2862 | Error_Node := Object_Definition (N); | |
2863 | ||
2864 | when N_Parameter_Specification => | |
2865 | Error_Node := Parameter_Type (N); | |
2866 | ||
13dbf220 | 2867 | when others => |
2868 | raise Program_Error; | |
2869 | end case; | |
5329ca64 | 2870 | |
0577b0b1 | 2871 | if Has_Null then |
5329ca64 | 2872 | |
0577b0b1 | 2873 | -- Enforce legality rule 3.10 (13): A null exclusion can only be |
2874 | -- applied to an access [sub]type. | |
5329ca64 | 2875 | |
0577b0b1 | 2876 | if not Is_Access_Type (Typ) then |
2877 | Error_Msg_N | |
00c403ee | 2878 | ("`NOT NULL` allowed only for an access type", Error_Node); |
5329ca64 | 2879 | |
feff2f05 | 2880 | -- Enforce legality rule RM 3.10(14/1): A null exclusion can only |
0577b0b1 | 2881 | -- be applied to a [sub]type that does not exclude null already. |
2882 | ||
2883 | elsif Can_Never_Be_Null (Typ) | |
d16989f1 | 2884 | and then Comes_From_Source (Typ) |
0577b0b1 | 2885 | then |
00c403ee | 2886 | Error_Msg_NE |
2887 | ("`NOT NULL` not allowed (& already excludes null)", | |
2888 | Error_Node, Typ); | |
0577b0b1 | 2889 | end if; |
13dbf220 | 2890 | end if; |
5329ca64 | 2891 | |
cc60bd16 | 2892 | -- Check that null-excluding objects are always initialized, except for |
2893 | -- deferred constants, for which the expression will appear in the full | |
2894 | -- declaration. | |
13dbf220 | 2895 | |
2896 | if K = N_Object_Declaration | |
84d0d4a5 | 2897 | and then No (Expression (N)) |
cc60bd16 | 2898 | and then not Constant_Present (N) |
feff2f05 | 2899 | and then not No_Initialization (N) |
13dbf220 | 2900 | then |
feff2f05 | 2901 | -- Add an expression that assigns null. This node is needed by |
2902 | -- Apply_Compile_Time_Constraint_Error, which will replace this with | |
2903 | -- a Constraint_Error node. | |
13dbf220 | 2904 | |
2905 | Set_Expression (N, Make_Null (Sloc (N))); | |
2906 | Set_Etype (Expression (N), Etype (Defining_Identifier (N))); | |
5329ca64 | 2907 | |
13dbf220 | 2908 | Apply_Compile_Time_Constraint_Error |
2909 | (N => Expression (N), | |
2910 | Msg => "(Ada 2005) null-excluding objects must be initialized?", | |
2911 | Reason => CE_Null_Not_Allowed); | |
2912 | end if; | |
5329ca64 | 2913 | |
cc60bd16 | 2914 | -- Check that a null-excluding component, formal or object is not being |
2915 | -- assigned a null value. Otherwise generate a warning message and | |
2c145f84 | 2916 | -- replace Expression (N) by an N_Constraint_Error node. |
13dbf220 | 2917 | |
0577b0b1 | 2918 | if K /= N_Function_Specification then |
2919 | Expr := Expression (N); | |
5329ca64 | 2920 | |
2af58f67 | 2921 | if Present (Expr) and then Known_Null (Expr) then |
13dbf220 | 2922 | case K is |
0577b0b1 | 2923 | when N_Component_Declaration | |
2924 | N_Discriminant_Specification => | |
7189d17f | 2925 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 2926 | (N => Expr, |
2af58f67 | 2927 | Msg => "(Ada 2005) null not allowed " & |
0577b0b1 | 2928 | "in null-excluding components?", |
2929 | Reason => CE_Null_Not_Allowed); | |
5329ca64 | 2930 | |
0577b0b1 | 2931 | when N_Object_Declaration => |
7189d17f | 2932 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 2933 | (N => Expr, |
2af58f67 | 2934 | Msg => "(Ada 2005) null not allowed " & |
0577b0b1 | 2935 | "in null-excluding objects?", |
2936 | Reason => CE_Null_Not_Allowed); | |
5329ca64 | 2937 | |
0577b0b1 | 2938 | when N_Parameter_Specification => |
7189d17f | 2939 | Apply_Compile_Time_Constraint_Error |
0577b0b1 | 2940 | (N => Expr, |
2af58f67 | 2941 | Msg => "(Ada 2005) null not allowed " & |
0577b0b1 | 2942 | "in null-excluding formals?", |
2943 | Reason => CE_Null_Not_Allowed); | |
13dbf220 | 2944 | |
2945 | when others => | |
2946 | null; | |
5329ca64 | 2947 | end case; |
2948 | end if; | |
0577b0b1 | 2949 | end if; |
fa7497e8 | 2950 | end Null_Exclusion_Static_Checks; |
2951 | ||
9dfe12ae | 2952 | ---------------------------------- |
2953 | -- Conditional_Statements_Begin -- | |
2954 | ---------------------------------- | |
2955 | ||
2956 | procedure Conditional_Statements_Begin is | |
2957 | begin | |
2958 | Saved_Checks_TOS := Saved_Checks_TOS + 1; | |
2959 | ||
feff2f05 | 2960 | -- If stack overflows, kill all checks, that way we know to simply reset |
2961 | -- the number of saved checks to zero on return. This should never occur | |
2962 | -- in practice. | |
9dfe12ae | 2963 | |
2964 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
2965 | Kill_All_Checks; | |
2966 | ||
feff2f05 | 2967 | -- In the normal case, we just make a new stack entry saving the current |
2968 | -- number of saved checks for a later restore. | |
9dfe12ae | 2969 | |
2970 | else | |
2971 | Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks; | |
2972 | ||
2973 | if Debug_Flag_CC then | |
2974 | w ("Conditional_Statements_Begin: Num_Saved_Checks = ", | |
2975 | Num_Saved_Checks); | |
2976 | end if; | |
2977 | end if; | |
2978 | end Conditional_Statements_Begin; | |
2979 | ||
2980 | -------------------------------- | |
2981 | -- Conditional_Statements_End -- | |
2982 | -------------------------------- | |
2983 | ||
2984 | procedure Conditional_Statements_End is | |
2985 | begin | |
2986 | pragma Assert (Saved_Checks_TOS > 0); | |
2987 | ||
feff2f05 | 2988 | -- If the saved checks stack overflowed, then we killed all checks, so |
2989 | -- setting the number of saved checks back to zero is correct. This | |
2990 | -- should never occur in practice. | |
9dfe12ae | 2991 | |
2992 | if Saved_Checks_TOS > Saved_Checks_Stack'Last then | |
2993 | Num_Saved_Checks := 0; | |
2994 | ||
feff2f05 | 2995 | -- In the normal case, restore the number of saved checks from the top |
2996 | -- stack entry. | |
9dfe12ae | 2997 | |
2998 | else | |
2999 | Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS); | |
3000 | if Debug_Flag_CC then | |
3001 | w ("Conditional_Statements_End: Num_Saved_Checks = ", | |
3002 | Num_Saved_Checks); | |
3003 | end if; | |
3004 | end if; | |
3005 | ||
3006 | Saved_Checks_TOS := Saved_Checks_TOS - 1; | |
3007 | end Conditional_Statements_End; | |
3008 | ||
ee6ba406 | 3009 | --------------------- |
3010 | -- Determine_Range -- | |
3011 | --------------------- | |
3012 | ||
6af1bdbc | 3013 | Cache_Size : constant := 2 ** 10; |
ee6ba406 | 3014 | type Cache_Index is range 0 .. Cache_Size - 1; |
3015 | -- Determine size of below cache (power of 2 is more efficient!) | |
3016 | ||
3017 | Determine_Range_Cache_N : array (Cache_Index) of Node_Id; | |
9c486805 | 3018 | Determine_Range_Cache_V : array (Cache_Index) of Boolean; |
ee6ba406 | 3019 | Determine_Range_Cache_Lo : array (Cache_Index) of Uint; |
3020 | Determine_Range_Cache_Hi : array (Cache_Index) of Uint; | |
feff2f05 | 3021 | -- The above arrays are used to implement a small direct cache for |
3022 | -- Determine_Range calls. Because of the way Determine_Range recursively | |
3023 | -- traces subexpressions, and because overflow checking calls the routine | |
3024 | -- on the way up the tree, a quadratic behavior can otherwise be | |
3025 | -- encountered in large expressions. The cache entry for node N is stored | |
3026 | -- in the (N mod Cache_Size) entry, and can be validated by checking the | |
9c486805 | 3027 | -- actual node value stored there. The Range_Cache_V array records the |
3028 | -- setting of Assume_Valid for the cache entry. | |
ee6ba406 | 3029 | |
3030 | procedure Determine_Range | |
9c486805 | 3031 | (N : Node_Id; |
3032 | OK : out Boolean; | |
3033 | Lo : out Uint; | |
3034 | Hi : out Uint; | |
3035 | Assume_Valid : Boolean := False) | |
ee6ba406 | 3036 | is |
e254d721 | 3037 | Typ : Entity_Id := Etype (N); |
3038 | -- Type to use, may get reset to base type for possibly invalid entity | |
8880be85 | 3039 | |
3040 | Lo_Left : Uint; | |
3041 | Hi_Left : Uint; | |
3042 | -- Lo and Hi bounds of left operand | |
ee6ba406 | 3043 | |
ee6ba406 | 3044 | Lo_Right : Uint; |
ee6ba406 | 3045 | Hi_Right : Uint; |
8880be85 | 3046 | -- Lo and Hi bounds of right (or only) operand |
3047 | ||
3048 | Bound : Node_Id; | |
3049 | -- Temp variable used to hold a bound node | |
3050 | ||
3051 | Hbound : Uint; | |
3052 | -- High bound of base type of expression | |
3053 | ||
3054 | Lor : Uint; | |
3055 | Hir : Uint; | |
3056 | -- Refined values for low and high bounds, after tightening | |
3057 | ||
3058 | OK1 : Boolean; | |
3059 | -- Used in lower level calls to indicate if call succeeded | |
3060 | ||
3061 | Cindex : Cache_Index; | |
3062 | -- Used to search cache | |
ee6ba406 | 3063 | |
3064 | function OK_Operands return Boolean; | |
3065 | -- Used for binary operators. Determines the ranges of the left and | |
3066 | -- right operands, and if they are both OK, returns True, and puts | |
3067 | -- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left | |
3068 | ||
3069 | ----------------- | |
3070 | -- OK_Operands -- | |
3071 | ----------------- | |
3072 | ||
3073 | function OK_Operands return Boolean is | |
3074 | begin | |
9c486805 | 3075 | Determine_Range |
3076 | (Left_Opnd (N), OK1, Lo_Left, Hi_Left, Assume_Valid); | |
ee6ba406 | 3077 | |
3078 | if not OK1 then | |
3079 | return False; | |
3080 | end if; | |
3081 | ||
9c486805 | 3082 | Determine_Range |
3083 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 3084 | return OK1; |
3085 | end OK_Operands; | |
3086 | ||
3087 | -- Start of processing for Determine_Range | |
3088 | ||
3089 | begin | |
3090 | -- Prevent junk warnings by initializing range variables | |
3091 | ||
3092 | Lo := No_Uint; | |
3093 | Hi := No_Uint; | |
3094 | Lor := No_Uint; | |
3095 | Hir := No_Uint; | |
3096 | ||
feff2f05 | 3097 | -- If the type is not discrete, or is undefined, then we can't do |
3098 | -- anything about determining the range. | |
ee6ba406 | 3099 | |
3100 | if No (Typ) or else not Is_Discrete_Type (Typ) | |
3101 | or else Error_Posted (N) | |
3102 | then | |
3103 | OK := False; | |
3104 | return; | |
3105 | end if; | |
3106 | ||
3107 | -- For all other cases, we can determine the range | |
3108 | ||
3109 | OK := True; | |
3110 | ||
feff2f05 | 3111 | -- If value is compile time known, then the possible range is the one |
3112 | -- value that we know this expression definitely has! | |
ee6ba406 | 3113 | |
3114 | if Compile_Time_Known_Value (N) then | |
3115 | Lo := Expr_Value (N); | |
3116 | Hi := Lo; | |
3117 | return; | |
3118 | end if; | |
3119 | ||
3120 | -- Return if already in the cache | |
3121 | ||
3122 | Cindex := Cache_Index (N mod Cache_Size); | |
3123 | ||
9c486805 | 3124 | if Determine_Range_Cache_N (Cindex) = N |
3125 | and then | |
3126 | Determine_Range_Cache_V (Cindex) = Assume_Valid | |
3127 | then | |
ee6ba406 | 3128 | Lo := Determine_Range_Cache_Lo (Cindex); |
3129 | Hi := Determine_Range_Cache_Hi (Cindex); | |
3130 | return; | |
3131 | end if; | |
3132 | ||
feff2f05 | 3133 | -- Otherwise, start by finding the bounds of the type of the expression, |
3134 | -- the value cannot be outside this range (if it is, then we have an | |
3135 | -- overflow situation, which is a separate check, we are talking here | |
3136 | -- only about the expression value). | |
ee6ba406 | 3137 | |
9c486805 | 3138 | -- First step, change to use base type unless we know the value is valid |
e254d721 | 3139 | |
9c486805 | 3140 | if (Is_Entity_Name (N) and then Is_Known_Valid (Entity (N))) |
3141 | or else Assume_No_Invalid_Values | |
3142 | or else Assume_Valid | |
e254d721 | 3143 | then |
9c486805 | 3144 | null; |
3145 | else | |
3146 | Typ := Underlying_Type (Base_Type (Typ)); | |
e254d721 | 3147 | end if; |
3148 | ||
feff2f05 | 3149 | -- We use the actual bound unless it is dynamic, in which case use the |
3150 | -- corresponding base type bound if possible. If we can't get a bound | |
3151 | -- then we figure we can't determine the range (a peculiar case, that | |
3152 | -- perhaps cannot happen, but there is no point in bombing in this | |
3153 | -- optimization circuit. | |
8880be85 | 3154 | |
3155 | -- First the low bound | |
ee6ba406 | 3156 | |
3157 | Bound := Type_Low_Bound (Typ); | |
3158 | ||
3159 | if Compile_Time_Known_Value (Bound) then | |
3160 | Lo := Expr_Value (Bound); | |
3161 | ||
3162 | elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then | |
3163 | Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ))); | |
3164 | ||
3165 | else | |
3166 | OK := False; | |
3167 | return; | |
3168 | end if; | |
3169 | ||
8880be85 | 3170 | -- Now the high bound |
3171 | ||
ee6ba406 | 3172 | Bound := Type_High_Bound (Typ); |
3173 | ||
8880be85 | 3174 | -- We need the high bound of the base type later on, and this should |
3175 | -- always be compile time known. Again, it is not clear that this | |
3176 | -- can ever be false, but no point in bombing. | |
ee6ba406 | 3177 | |
8880be85 | 3178 | if Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then |
ee6ba406 | 3179 | Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ))); |
3180 | Hi := Hbound; | |
3181 | ||
3182 | else | |
3183 | OK := False; | |
3184 | return; | |
3185 | end if; | |
3186 | ||
feff2f05 | 3187 | -- If we have a static subtype, then that may have a tighter bound so |
3188 | -- use the upper bound of the subtype instead in this case. | |
8880be85 | 3189 | |
3190 | if Compile_Time_Known_Value (Bound) then | |
3191 | Hi := Expr_Value (Bound); | |
3192 | end if; | |
3193 | ||
feff2f05 | 3194 | -- We may be able to refine this value in certain situations. If any |
3195 | -- refinement is possible, then Lor and Hir are set to possibly tighter | |
3196 | -- bounds, and OK1 is set to True. | |
ee6ba406 | 3197 | |
3198 | case Nkind (N) is | |
3199 | ||
3200 | -- For unary plus, result is limited by range of operand | |
3201 | ||
3202 | when N_Op_Plus => | |
9c486805 | 3203 | Determine_Range |
3204 | (Right_Opnd (N), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 3205 | |
3206 | -- For unary minus, determine range of operand, and negate it | |
3207 | ||
3208 | when N_Op_Minus => | |
9c486805 | 3209 | Determine_Range |
3210 | (Right_Opnd (N), OK1, Lo_Right, Hi_Right, Assume_Valid); | |
ee6ba406 | 3211 | |
3212 | if OK1 then | |
3213 | Lor := -Hi_Right; | |
3214 | Hir := -Lo_Right; | |
3215 | end if; | |
3216 | ||
3217 | -- For binary addition, get range of each operand and do the | |
3218 | -- addition to get the result range. | |
3219 | ||
3220 | when N_Op_Add => | |
3221 | if OK_Operands then | |
3222 | Lor := Lo_Left + Lo_Right; | |
3223 | Hir := Hi_Left + Hi_Right; | |
3224 | end if; | |
3225 | ||
feff2f05 | 3226 | -- Division is tricky. The only case we consider is where the right |
3227 | -- operand is a positive constant, and in this case we simply divide | |
3228 | -- the bounds of the left operand | |
ee6ba406 | 3229 | |
3230 | when N_Op_Divide => | |
3231 | if OK_Operands then | |
3232 | if Lo_Right = Hi_Right | |
3233 | and then Lo_Right > 0 | |
3234 | then | |
3235 | Lor := Lo_Left / Lo_Right; | |
3236 | Hir := Hi_Left / Lo_Right; | |
3237 | ||
3238 | else | |
3239 | OK1 := False; | |
3240 | end if; | |
3241 | end if; | |
3242 | ||
feff2f05 | 3243 | -- For binary subtraction, get range of each operand and do the worst |
3244 | -- case subtraction to get the result range. | |
ee6ba406 | 3245 | |
3246 | when N_Op_Subtract => | |
3247 | if OK_Operands then | |
3248 | Lor := Lo_Left - Hi_Right; | |
3249 | Hir := Hi_Left - Lo_Right; | |
3250 | end if; | |
3251 | ||
feff2f05 | 3252 | -- For MOD, if right operand is a positive constant, then result must |
3253 | -- be in the allowable range of mod results. | |
ee6ba406 | 3254 | |
3255 | when N_Op_Mod => | |
3256 | if OK_Operands then | |
9dfe12ae | 3257 | if Lo_Right = Hi_Right |
3258 | and then Lo_Right /= 0 | |
3259 | then | |
ee6ba406 | 3260 | if Lo_Right > 0 then |
3261 | Lor := Uint_0; | |
3262 | Hir := Lo_Right - 1; | |
3263 | ||
9dfe12ae | 3264 | else -- Lo_Right < 0 |
ee6ba406 | 3265 | Lor := Lo_Right + 1; |
3266 | Hir := Uint_0; | |
3267 | end if; | |
3268 | ||
3269 | else | |
3270 | OK1 := False; | |
3271 | end if; | |
3272 | end if; | |
3273 | ||
feff2f05 | 3274 | -- For REM, if right operand is a positive constant, then result must |
3275 | -- be in the allowable range of mod results. | |
ee6ba406 | 3276 | |
3277 | when N_Op_Rem => | |
3278 | if OK_Operands then | |
9dfe12ae | 3279 | if Lo_Right = Hi_Right |
3280 | and then Lo_Right /= 0 | |
3281 | then | |
ee6ba406 | 3282 | declare |
3283 | Dval : constant Uint := (abs Lo_Right) - 1; | |
3284 | ||
3285 | begin | |
3286 | -- The sign of the result depends on the sign of the | |
3287 | -- dividend (but not on the sign of the divisor, hence | |
3288 | -- the abs operation above). | |
3289 | ||
3290 | if Lo_Left < 0 then | |
3291 | Lor := -Dval; | |
3292 | else | |
3293 | Lor := Uint_0; | |
3294 | end if; | |
3295 | ||
3296 | if Hi_Left < 0 then | |
3297 | Hir := Uint_0; | |
3298 | else | |
3299 | Hir := Dval; | |
3300 | end if; | |
3301 | end; | |
3302 | ||
3303 | else | |
3304 | OK1 := False; | |
3305 | end if; | |
3306 | end if; | |
3307 | ||
3308 | -- Attribute reference cases | |
3309 | ||
3310 | when N_Attribute_Reference => | |
3311 | case Attribute_Name (N) is | |
3312 | ||
3313 | -- For Pos/Val attributes, we can refine the range using the | |
3314 | -- possible range of values of the attribute expression | |
3315 | ||
3316 | when Name_Pos | Name_Val => | |
9c486805 | 3317 | Determine_Range |
3318 | (First (Expressions (N)), OK1, Lor, Hir, Assume_Valid); | |
ee6ba406 | 3319 | |
3320 | -- For Length attribute, use the bounds of the corresponding | |
3321 | -- index type to refine the range. | |
3322 | ||
3323 | when Name_Length => | |
3324 | declare | |
3325 | Atyp : Entity_Id := Etype (Prefix (N)); | |
3326 | Inum : Nat; | |
3327 | Indx : Node_Id; | |
3328 | ||
3329 | LL, LU : Uint; | |
3330 | UL, UU : Uint; | |
3331 | ||
3332 | begin | |
3333 | if Is_Access_Type (Atyp) then | |
3334 | Atyp := Designated_Type (Atyp); | |
3335 | end if; | |
3336 | ||
3337 | -- For string literal, we know exact value | |
3338 | ||
3339 | if Ekind (Atyp) = E_String_Literal_Subtype then | |
3340 | OK := True; | |
3341 | Lo := String_Literal_Length (Atyp); | |
3342 | Hi := String_Literal_Length (Atyp); | |
3343 | return; | |
3344 | end if; | |
3345 | ||
3346 | -- Otherwise check for expression given | |
3347 | ||
3348 | if No (Expressions (N)) then | |
3349 | Inum := 1; | |
3350 | else | |
3351 | Inum := | |
3352 | UI_To_Int (Expr_Value (First (Expressions (N)))); | |
3353 | end if; | |
3354 | ||
3355 | Indx := First_Index (Atyp); | |
3356 | for J in 2 .. Inum loop | |
3357 | Indx := Next_Index (Indx); | |
3358 | end loop; | |
3359 | ||
3360 | Determine_Range | |
9c486805 | 3361 | (Type_Low_Bound (Etype (Indx)), OK1, LL, LU, |
3362 | Assume_Valid); | |
ee6ba406 | 3363 | |
3364 | if OK1 then | |
3365 | Determine_Range | |
9c486805 | 3366 | (Type_High_Bound (Etype (Indx)), OK1, UL, UU, |
3367 | Assume_Valid); | |
ee6ba406 | 3368 | |
3369 | if OK1 then | |
3370 | ||
3371 | -- The maximum value for Length is the biggest | |
3372 | -- possible gap between the values of the bounds. | |
3373 | -- But of course, this value cannot be negative. | |
3374 | ||
9c486805 | 3375 | Hir := UI_Max (Uint_0, UU - LL + 1); |
ee6ba406 | 3376 | |
3377 | -- For constrained arrays, the minimum value for | |
3378 | -- Length is taken from the actual value of the | |
3379 | -- bounds, since the index will be exactly of | |
3380 | -- this subtype. | |
3381 | ||
3382 | if Is_Constrained (Atyp) then | |
9c486805 | 3383 | Lor := UI_Max (Uint_0, UL - LU + 1); |
ee6ba406 | 3384 | |
3385 | -- For an unconstrained array, the minimum value | |
3386 | -- for length is always zero. | |
3387 | ||
3388 | else | |
3389 | Lor := Uint_0; | |
3390 | end if; | |
3391 | end if; | |
3392 | end if; | |
3393 | end; | |
3394 | ||
3395 | -- No special handling for other attributes | |
3396 | -- Probably more opportunities exist here ??? | |
3397 | ||
3398 | when others => | |
3399 | OK1 := False; | |
3400 | ||
3401 | end case; | |
3402 | ||
feff2f05 | 3403 | -- For type conversion from one discrete type to another, we can |
3404 | -- refine the range using the converted value. | |
ee6ba406 | 3405 | |
3406 | when N_Type_Conversion => | |
9c486805 | 3407 | Determine_Range (Expression (N), OK1, Lor, Hir, Assume_Valid); |
ee6ba406 | 3408 | |
3409 | -- Nothing special to do for all other expression kinds | |
3410 | ||
3411 | when others => | |
3412 | OK1 := False; | |
3413 | Lor := No_Uint; | |
3414 | Hir := No_Uint; | |
3415 | end case; | |
3416 | ||
3417 | -- At this stage, if OK1 is true, then we know that the actual | |
3418 | -- result of the computed expression is in the range Lor .. Hir. | |
3419 | -- We can use this to restrict the possible range of results. | |
3420 | ||
3421 | if OK1 then | |
3422 | ||
3423 | -- If the refined value of the low bound is greater than the | |
3424 | -- type high bound, then reset it to the more restrictive | |
3425 | -- value. However, we do NOT do this for the case of a modular | |
3426 | -- type where the possible upper bound on the value is above the | |
3427 | -- base type high bound, because that means the result could wrap. | |
3428 | ||
3429 | if Lor > Lo | |
3430 | and then not (Is_Modular_Integer_Type (Typ) | |
3431 | and then Hir > Hbound) | |
3432 | then | |
3433 | Lo := Lor; | |
3434 | end if; | |
3435 | ||
3436 | -- Similarly, if the refined value of the high bound is less | |
3437 | -- than the value so far, then reset it to the more restrictive | |
3438 | -- value. Again, we do not do this if the refined low bound is | |
3439 | -- negative for a modular type, since this would wrap. | |
3440 | ||
3441 | if Hir < Hi | |
3442 | and then not (Is_Modular_Integer_Type (Typ) | |
3443 | and then Lor < Uint_0) | |
3444 | then | |
3445 | Hi := Hir; | |
3446 | end if; | |
3447 | end if; | |
3448 | ||
3449 | -- Set cache entry for future call and we are all done | |
3450 | ||
3451 | Determine_Range_Cache_N (Cindex) := N; | |
9c486805 | 3452 | Determine_Range_Cache_V (Cindex) := Assume_Valid; |
ee6ba406 | 3453 | Determine_Range_Cache_Lo (Cindex) := Lo; |
3454 | Determine_Range_Cache_Hi (Cindex) := Hi; | |
3455 | return; | |
3456 | ||
3457 | -- If any exception occurs, it means that we have some bug in the compiler | |
3458 | -- possibly triggered by a previous error, or by some unforseen peculiar | |
3459 | -- occurrence. However, this is only an optimization attempt, so there is | |
3460 | -- really no point in crashing the compiler. Instead we just decide, too | |
3461 | -- bad, we can't figure out a range in this case after all. | |
3462 | ||
3463 | exception | |
3464 | when others => | |
3465 | ||
3466 | -- Debug flag K disables this behavior (useful for debugging) | |
3467 | ||
3468 | if Debug_Flag_K then | |
3469 | raise; | |
3470 | else | |
3471 | OK := False; | |
3472 | Lo := No_Uint; | |
3473 | Hi := No_Uint; | |
3474 | return; | |
3475 | end if; | |
ee6ba406 | 3476 | end Determine_Range; |
3477 | ||
3478 | ------------------------------------ | |
3479 | -- Discriminant_Checks_Suppressed -- | |
3480 | ------------------------------------ | |
3481 | ||
3482 | function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is | |
3483 | begin | |
9dfe12ae | 3484 | if Present (E) then |
3485 | if Is_Unchecked_Union (E) then | |
3486 | return True; | |
3487 | elsif Checks_May_Be_Suppressed (E) then | |
3488 | return Is_Check_Suppressed (E, Discriminant_Check); | |
3489 | end if; | |
3490 | end if; | |
3491 | ||
3492 | return Scope_Suppress (Discriminant_Check); | |
ee6ba406 | 3493 | end Discriminant_Checks_Suppressed; |
3494 | ||
3495 | -------------------------------- | |
3496 | -- Division_Checks_Suppressed -- | |
3497 | -------------------------------- | |
3498 | ||
3499 | function Division_Checks_Suppressed (E : Entity_Id) return Boolean is | |
3500 | begin | |
9dfe12ae | 3501 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
3502 | return Is_Check_Suppressed (E, Division_Check); | |
3503 | else | |
3504 | return Scope_Suppress (Division_Check); | |
3505 | end if; | |
ee6ba406 | 3506 | end Division_Checks_Suppressed; |
3507 | ||
3508 | ----------------------------------- | |
3509 | -- Elaboration_Checks_Suppressed -- | |
3510 | ----------------------------------- | |
3511 | ||
3512 | function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is | |
3513 | begin | |
38f5559f | 3514 | -- The complication in this routine is that if we are in the dynamic |
3515 | -- model of elaboration, we also check All_Checks, since All_Checks | |
3516 | -- does not set Elaboration_Check explicitly. | |
3517 | ||
9dfe12ae | 3518 | if Present (E) then |
3519 | if Kill_Elaboration_Checks (E) then | |
3520 | return True; | |
38f5559f | 3521 | |
9dfe12ae | 3522 | elsif Checks_May_Be_Suppressed (E) then |
38f5559f | 3523 | if Is_Check_Suppressed (E, Elaboration_Check) then |
3524 | return True; | |
3525 | elsif Dynamic_Elaboration_Checks then | |
3526 | return Is_Check_Suppressed (E, All_Checks); | |
3527 | else | |
3528 | return False; | |
3529 | end if; | |
9dfe12ae | 3530 | end if; |
3531 | end if; | |
3532 | ||
38f5559f | 3533 | if Scope_Suppress (Elaboration_Check) then |
3534 | return True; | |
3535 | elsif Dynamic_Elaboration_Checks then | |
3536 | return Scope_Suppress (All_Checks); | |
3537 | else | |
3538 | return False; | |
3539 | end if; | |
ee6ba406 | 3540 | end Elaboration_Checks_Suppressed; |
3541 | ||
9dfe12ae | 3542 | --------------------------- |
3543 | -- Enable_Overflow_Check -- | |
3544 | --------------------------- | |
3545 | ||
3546 | procedure Enable_Overflow_Check (N : Node_Id) is | |
3547 | Typ : constant Entity_Id := Base_Type (Etype (N)); | |
3548 | Chk : Nat; | |
3549 | OK : Boolean; | |
3550 | Ent : Entity_Id; | |
3551 | Ofs : Uint; | |
3552 | Lo : Uint; | |
3553 | Hi : Uint; | |
ee6ba406 | 3554 | |
ee6ba406 | 3555 | begin |
9dfe12ae | 3556 | if Debug_Flag_CC then |
3557 | w ("Enable_Overflow_Check for node ", Int (N)); | |
3558 | Write_Str (" Source location = "); | |
3559 | wl (Sloc (N)); | |
00c403ee | 3560 | pg (Union_Id (N)); |
ee6ba406 | 3561 | end if; |
ee6ba406 | 3562 | |
75209ec5 | 3563 | -- No check if overflow checks suppressed for type of node |
3564 | ||
3565 | if Present (Etype (N)) | |
3566 | and then Overflow_Checks_Suppressed (Etype (N)) | |
3567 | then | |
3568 | return; | |
3569 | ||
feff2f05 | 3570 | -- Nothing to do if the range of the result is known OK. We skip this |
3571 | -- for conversions, since the caller already did the check, and in any | |
3572 | -- case the condition for deleting the check for a type conversion is | |
cc60bd16 | 3573 | -- different. |
ee6ba406 | 3574 | |
75209ec5 | 3575 | elsif Nkind (N) /= N_Type_Conversion then |
9c486805 | 3576 | Determine_Range (N, OK, Lo, Hi, Assume_Valid => True); |
ee6ba406 | 3577 | |
cc60bd16 | 3578 | -- Note in the test below that we assume that the range is not OK |
3579 | -- if a bound of the range is equal to that of the type. That's not | |
3580 | -- quite accurate but we do this for the following reasons: | |
ee6ba406 | 3581 | |
9dfe12ae | 3582 | -- a) The way that Determine_Range works, it will typically report |
3583 | -- the bounds of the value as being equal to the bounds of the | |
3584 | -- type, because it either can't tell anything more precise, or | |
3585 | -- does not think it is worth the effort to be more precise. | |
ee6ba406 | 3586 | |
9dfe12ae | 3587 | -- b) It is very unusual to have a situation in which this would |
3588 | -- generate an unnecessary overflow check (an example would be | |
3589 | -- a subtype with a range 0 .. Integer'Last - 1 to which the | |
cc60bd16 | 3590 | -- literal value one is added). |
ee6ba406 | 3591 | |
9dfe12ae | 3592 | -- c) The alternative is a lot of special casing in this routine |
3593 | -- which would partially duplicate Determine_Range processing. | |
ee6ba406 | 3594 | |
9dfe12ae | 3595 | if OK |
3596 | and then Lo > Expr_Value (Type_Low_Bound (Typ)) | |
3597 | and then Hi < Expr_Value (Type_High_Bound (Typ)) | |
3598 | then | |
3599 | if Debug_Flag_CC then | |
3600 | w ("No overflow check required"); | |
3601 | end if; | |
3602 | ||
3603 | return; | |
3604 | end if; | |
3605 | end if; | |
3606 | ||
feff2f05 | 3607 | -- If not in optimizing mode, set flag and we are done. We are also done |
3608 | -- (and just set the flag) if the type is not a discrete type, since it | |
3609 | -- is not worth the effort to eliminate checks for other than discrete | |
3610 | -- types. In addition, we take this same path if we have stored the | |
3611 | -- maximum number of checks possible already (a very unlikely situation, | |
3612 | -- but we do not want to blow up!) | |
9dfe12ae | 3613 | |
3614 | if Optimization_Level = 0 | |
3615 | or else not Is_Discrete_Type (Etype (N)) | |
3616 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 3617 | then |
00c403ee | 3618 | Activate_Overflow_Check (N); |
9dfe12ae | 3619 | |
3620 | if Debug_Flag_CC then | |
3621 | w ("Optimization off"); | |
3622 | end if; | |
3623 | ||
ee6ba406 | 3624 | return; |
9dfe12ae | 3625 | end if; |
ee6ba406 | 3626 | |
9dfe12ae | 3627 | -- Otherwise evaluate and check the expression |
3628 | ||
3629 | Find_Check | |
3630 | (Expr => N, | |
3631 | Check_Type => 'O', | |
3632 | Target_Type => Empty, | |
3633 | Entry_OK => OK, | |
3634 | Check_Num => Chk, | |
3635 | Ent => Ent, | |
3636 | Ofs => Ofs); | |
3637 | ||
3638 | if Debug_Flag_CC then | |
3639 | w ("Called Find_Check"); | |
3640 | w (" OK = ", OK); | |
3641 | ||
3642 | if OK then | |
3643 | w (" Check_Num = ", Chk); | |
3644 | w (" Ent = ", Int (Ent)); | |
3645 | Write_Str (" Ofs = "); | |
3646 | pid (Ofs); | |
3647 | end if; | |
3648 | end if; | |
ee6ba406 | 3649 | |
9dfe12ae | 3650 | -- If check is not of form to optimize, then set flag and we are done |
3651 | ||
3652 | if not OK then | |
00c403ee | 3653 | Activate_Overflow_Check (N); |
ee6ba406 | 3654 | return; |
9dfe12ae | 3655 | end if; |
ee6ba406 | 3656 | |
9dfe12ae | 3657 | -- If check is already performed, then return without setting flag |
3658 | ||
3659 | if Chk /= 0 then | |
3660 | if Debug_Flag_CC then | |
3661 | w ("Check suppressed!"); | |
3662 | end if; | |
ee6ba406 | 3663 | |
ee6ba406 | 3664 | return; |
9dfe12ae | 3665 | end if; |
ee6ba406 | 3666 | |
9dfe12ae | 3667 | -- Here we will make a new entry for the new check |
3668 | ||
00c403ee | 3669 | Activate_Overflow_Check (N); |
9dfe12ae | 3670 | Num_Saved_Checks := Num_Saved_Checks + 1; |
3671 | Saved_Checks (Num_Saved_Checks) := | |
3672 | (Killed => False, | |
3673 | Entity => Ent, | |
3674 | Offset => Ofs, | |
3675 | Check_Type => 'O', | |
3676 | Target_Type => Empty); | |
3677 | ||
3678 | if Debug_Flag_CC then | |
3679 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
3680 | w (" Entity = ", Int (Ent)); | |
3681 | Write_Str (" Offset = "); | |
3682 | pid (Ofs); | |
3683 | w (" Check_Type = O"); | |
3684 | w (" Target_Type = Empty"); | |
3685 | end if; | |
ee6ba406 | 3686 | |
feff2f05 | 3687 | -- If we get an exception, then something went wrong, probably because of |
3688 | -- an error in the structure of the tree due to an incorrect program. Or it | |
3689 | -- may be a bug in the optimization circuit. In either case the safest | |
3690 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 3691 | |
3692 | exception | |
3693 | when others => | |
00c403ee | 3694 | Activate_Overflow_Check (N); |
9dfe12ae | 3695 | |
3696 | if Debug_Flag_CC then | |
3697 | w (" exception occurred, overflow flag set"); | |
3698 | end if; | |
3699 | ||
3700 | return; | |
3701 | end Enable_Overflow_Check; | |
3702 | ||
3703 | ------------------------ | |
3704 | -- Enable_Range_Check -- | |
3705 | ------------------------ | |
3706 | ||
3707 | procedure Enable_Range_Check (N : Node_Id) is | |
3708 | Chk : Nat; | |
3709 | OK : Boolean; | |
3710 | Ent : Entity_Id; | |
3711 | Ofs : Uint; | |
3712 | Ttyp : Entity_Id; | |
3713 | P : Node_Id; | |
3714 | ||
3715 | begin | |
feff2f05 | 3716 | -- Return if unchecked type conversion with range check killed. In this |
3717 | -- case we never set the flag (that's what Kill_Range_Check is about!) | |
9dfe12ae | 3718 | |
3719 | if Nkind (N) = N_Unchecked_Type_Conversion | |
3720 | and then Kill_Range_Check (N) | |
ee6ba406 | 3721 | then |
3722 | return; | |
9dfe12ae | 3723 | end if; |
ee6ba406 | 3724 | |
0577b0b1 | 3725 | -- Check for various cases where we should suppress the range check |
3726 | ||
3727 | -- No check if range checks suppressed for type of node | |
3728 | ||
3729 | if Present (Etype (N)) | |
3730 | and then Range_Checks_Suppressed (Etype (N)) | |
3731 | then | |
3732 | return; | |
3733 | ||
3734 | -- No check if node is an entity name, and range checks are suppressed | |
3735 | -- for this entity, or for the type of this entity. | |
3736 | ||
3737 | elsif Is_Entity_Name (N) | |
3738 | and then (Range_Checks_Suppressed (Entity (N)) | |
3739 | or else Range_Checks_Suppressed (Etype (Entity (N)))) | |
3740 | then | |
3741 | return; | |
3742 | ||
3743 | -- No checks if index of array, and index checks are suppressed for | |
3744 | -- the array object or the type of the array. | |
3745 | ||
3746 | elsif Nkind (Parent (N)) = N_Indexed_Component then | |
3747 | declare | |
3748 | Pref : constant Node_Id := Prefix (Parent (N)); | |
3749 | begin | |
3750 | if Is_Entity_Name (Pref) | |
3751 | and then Index_Checks_Suppressed (Entity (Pref)) | |
3752 | then | |
3753 | return; | |
3754 | elsif Index_Checks_Suppressed (Etype (Pref)) then | |
3755 | return; | |
3756 | end if; | |
3757 | end; | |
3758 | end if; | |
3759 | ||
9dfe12ae | 3760 | -- Debug trace output |
ee6ba406 | 3761 | |
9dfe12ae | 3762 | if Debug_Flag_CC then |
3763 | w ("Enable_Range_Check for node ", Int (N)); | |
3764 | Write_Str (" Source location = "); | |
3765 | wl (Sloc (N)); | |
00c403ee | 3766 | pg (Union_Id (N)); |
9dfe12ae | 3767 | end if; |
3768 | ||
feff2f05 | 3769 | -- If not in optimizing mode, set flag and we are done. We are also done |
3770 | -- (and just set the flag) if the type is not a discrete type, since it | |
3771 | -- is not worth the effort to eliminate checks for other than discrete | |
3772 | -- types. In addition, we take this same path if we have stored the | |
3773 | -- maximum number of checks possible already (a very unlikely situation, | |
3774 | -- but we do not want to blow up!) | |
9dfe12ae | 3775 | |
3776 | if Optimization_Level = 0 | |
3777 | or else No (Etype (N)) | |
3778 | or else not Is_Discrete_Type (Etype (N)) | |
3779 | or else Num_Saved_Checks = Saved_Checks'Last | |
ee6ba406 | 3780 | then |
00c403ee | 3781 | Activate_Range_Check (N); |
9dfe12ae | 3782 | |
3783 | if Debug_Flag_CC then | |
3784 | w ("Optimization off"); | |
3785 | end if; | |
3786 | ||
ee6ba406 | 3787 | return; |
9dfe12ae | 3788 | end if; |
ee6ba406 | 3789 | |
9dfe12ae | 3790 | -- Otherwise find out the target type |
ee6ba406 | 3791 | |
9dfe12ae | 3792 | P := Parent (N); |
ee6ba406 | 3793 | |
9dfe12ae | 3794 | -- For assignment, use left side subtype |
3795 | ||
3796 | if Nkind (P) = N_Assignment_Statement | |
3797 | and then Expression (P) = N | |
3798 | then | |
3799 | Ttyp := Etype (Name (P)); | |
3800 | ||
3801 | -- For indexed component, use subscript subtype | |
3802 | ||
3803 | elsif Nkind (P) = N_Indexed_Component then | |
3804 | declare | |
3805 | Atyp : Entity_Id; | |
3806 | Indx : Node_Id; | |
3807 | Subs : Node_Id; | |
3808 | ||
3809 | begin | |
3810 | Atyp := Etype (Prefix (P)); | |
3811 | ||
3812 | if Is_Access_Type (Atyp) then | |
3813 | Atyp := Designated_Type (Atyp); | |
f07ea091 | 3814 | |
3815 | -- If the prefix is an access to an unconstrained array, | |
feff2f05 | 3816 | -- perform check unconditionally: it depends on the bounds of |
3817 | -- an object and we cannot currently recognize whether the test | |
3818 | -- may be redundant. | |
f07ea091 | 3819 | |
3820 | if not Is_Constrained (Atyp) then | |
00c403ee | 3821 | Activate_Range_Check (N); |
f07ea091 | 3822 | return; |
3823 | end if; | |
7189d17f | 3824 | |
feff2f05 | 3825 | -- Ditto if the prefix is an explicit dereference whose designated |
3826 | -- type is unconstrained. | |
7189d17f | 3827 | |
3828 | elsif Nkind (Prefix (P)) = N_Explicit_Dereference | |
3829 | and then not Is_Constrained (Atyp) | |
3830 | then | |
00c403ee | 3831 | Activate_Range_Check (N); |
7189d17f | 3832 | return; |
9dfe12ae | 3833 | end if; |
3834 | ||
3835 | Indx := First_Index (Atyp); | |
3836 | Subs := First (Expressions (P)); | |
3837 | loop | |
3838 | if Subs = N then | |
3839 | Ttyp := Etype (Indx); | |
3840 | exit; | |
3841 | end if; | |
3842 | ||
3843 | Next_Index (Indx); | |
3844 | Next (Subs); | |
3845 | end loop; | |
3846 | end; | |
3847 | ||
3848 | -- For now, ignore all other cases, they are not so interesting | |
3849 | ||
3850 | else | |
3851 | if Debug_Flag_CC then | |
3852 | w (" target type not found, flag set"); | |
3853 | end if; | |
3854 | ||
00c403ee | 3855 | Activate_Range_Check (N); |
9dfe12ae | 3856 | return; |
3857 | end if; | |
3858 | ||
3859 | -- Evaluate and check the expression | |
3860 | ||
3861 | Find_Check | |
3862 | (Expr => N, | |
3863 | Check_Type => 'R', | |
3864 | Target_Type => Ttyp, | |
3865 | Entry_OK => OK, | |
3866 | Check_Num => Chk, | |
3867 | Ent => Ent, | |
3868 | Ofs => Ofs); | |
3869 | ||
3870 | if Debug_Flag_CC then | |
3871 | w ("Called Find_Check"); | |
3872 | w ("Target_Typ = ", Int (Ttyp)); | |
3873 | w (" OK = ", OK); | |
3874 | ||
3875 | if OK then | |
3876 | w (" Check_Num = ", Chk); | |
3877 | w (" Ent = ", Int (Ent)); | |
3878 | Write_Str (" Ofs = "); | |
3879 | pid (Ofs); | |
3880 | end if; | |
3881 | end if; | |
3882 | ||
3883 | -- If check is not of form to optimize, then set flag and we are done | |
3884 | ||
3885 | if not OK then | |
3886 | if Debug_Flag_CC then | |
3887 | w (" expression not of optimizable type, flag set"); | |
3888 | end if; | |
3889 | ||
00c403ee | 3890 | Activate_Range_Check (N); |
9dfe12ae | 3891 | return; |
3892 | end if; | |
3893 | ||
3894 | -- If check is already performed, then return without setting flag | |
3895 | ||
3896 | if Chk /= 0 then | |
3897 | if Debug_Flag_CC then | |
3898 | w ("Check suppressed!"); | |
3899 | end if; | |
3900 | ||
3901 | return; | |
3902 | end if; | |
3903 | ||
3904 | -- Here we will make a new entry for the new check | |
3905 | ||
00c403ee | 3906 | Activate_Range_Check (N); |
9dfe12ae | 3907 | Num_Saved_Checks := Num_Saved_Checks + 1; |
3908 | Saved_Checks (Num_Saved_Checks) := | |
3909 | (Killed => False, | |
3910 | Entity => Ent, | |
3911 | Offset => Ofs, | |
3912 | Check_Type => 'R', | |
3913 | Target_Type => Ttyp); | |
3914 | ||
3915 | if Debug_Flag_CC then | |
3916 | w ("Make new entry, check number = ", Num_Saved_Checks); | |
3917 | w (" Entity = ", Int (Ent)); | |
3918 | Write_Str (" Offset = "); | |
3919 | pid (Ofs); | |
3920 | w (" Check_Type = R"); | |
3921 | w (" Target_Type = ", Int (Ttyp)); | |
00c403ee | 3922 | pg (Union_Id (Ttyp)); |
9dfe12ae | 3923 | end if; |
3924 | ||
feff2f05 | 3925 | -- If we get an exception, then something went wrong, probably because of |
3926 | -- an error in the structure of the tree due to an incorrect program. Or | |
3927 | -- it may be a bug in the optimization circuit. In either case the safest | |
3928 | -- thing is simply to set the check flag unconditionally. | |
9dfe12ae | 3929 | |
3930 | exception | |
3931 | when others => | |
00c403ee | 3932 | Activate_Range_Check (N); |
9dfe12ae | 3933 | |
3934 | if Debug_Flag_CC then | |
3935 | w (" exception occurred, range flag set"); | |
3936 | end if; | |
3937 | ||
3938 | return; | |
3939 | end Enable_Range_Check; | |
3940 | ||
3941 | ------------------ | |
3942 | -- Ensure_Valid -- | |
3943 | ------------------ | |
3944 | ||
3945 | procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is | |
3946 | Typ : constant Entity_Id := Etype (Expr); | |
3947 | ||
3948 | begin | |
3949 | -- Ignore call if we are not doing any validity checking | |
3950 | ||
3951 | if not Validity_Checks_On then | |
3952 | return; | |
3953 | ||
0577b0b1 | 3954 | -- Ignore call if range or validity checks suppressed on entity or type |
9dfe12ae | 3955 | |
0577b0b1 | 3956 | elsif Range_Or_Validity_Checks_Suppressed (Expr) then |
9dfe12ae | 3957 | return; |
3958 | ||
feff2f05 | 3959 | -- No check required if expression is from the expander, we assume the |
3960 | -- expander will generate whatever checks are needed. Note that this is | |
3961 | -- not just an optimization, it avoids infinite recursions! | |
9dfe12ae | 3962 | |
3963 | -- Unchecked conversions must be checked, unless they are initialized | |
3964 | -- scalar values, as in a component assignment in an init proc. | |
3965 | ||
3966 | -- In addition, we force a check if Force_Validity_Checks is set | |
3967 | ||
3968 | elsif not Comes_From_Source (Expr) | |
3969 | and then not Force_Validity_Checks | |
3970 | and then (Nkind (Expr) /= N_Unchecked_Type_Conversion | |
3971 | or else Kill_Range_Check (Expr)) | |
3972 | then | |
3973 | return; | |
3974 | ||
3975 | -- No check required if expression is known to have valid value | |
3976 | ||
3977 | elsif Expr_Known_Valid (Expr) then | |
3978 | return; | |
3979 | ||
feff2f05 | 3980 | -- Ignore case of enumeration with holes where the flag is set not to |
3981 | -- worry about holes, since no special validity check is needed | |
9dfe12ae | 3982 | |
3983 | elsif Is_Enumeration_Type (Typ) | |
3984 | and then Has_Non_Standard_Rep (Typ) | |
3985 | and then Holes_OK | |
3986 | then | |
3987 | return; | |
3988 | ||
f2a06be9 | 3989 | -- No check required on the left-hand side of an assignment |
9dfe12ae | 3990 | |
3991 | elsif Nkind (Parent (Expr)) = N_Assignment_Statement | |
3992 | and then Expr = Name (Parent (Expr)) | |
3993 | then | |
3994 | return; | |
3995 | ||
38f5559f | 3996 | -- No check on a univeral real constant. The context will eventually |
3997 | -- convert it to a machine number for some target type, or report an | |
3998 | -- illegality. | |
3999 | ||
4000 | elsif Nkind (Expr) = N_Real_Literal | |
4001 | and then Etype (Expr) = Universal_Real | |
4002 | then | |
4003 | return; | |
4004 | ||
0577b0b1 | 4005 | -- If the expression denotes a component of a packed boolean arrray, |
4006 | -- no possible check applies. We ignore the old ACATS chestnuts that | |
4007 | -- involve Boolean range True..True. | |
4008 | ||
4009 | -- Note: validity checks are generated for expressions that yield a | |
4010 | -- scalar type, when it is possible to create a value that is outside of | |
4011 | -- the type. If this is a one-bit boolean no such value exists. This is | |
4012 | -- an optimization, and it also prevents compiler blowing up during the | |
4013 | -- elaboration of improperly expanded packed array references. | |
4014 | ||
4015 | elsif Nkind (Expr) = N_Indexed_Component | |
4016 | and then Is_Bit_Packed_Array (Etype (Prefix (Expr))) | |
4017 | and then Root_Type (Etype (Expr)) = Standard_Boolean | |
4018 | then | |
4019 | return; | |
4020 | ||
9dfe12ae | 4021 | -- An annoying special case. If this is an out parameter of a scalar |
4022 | -- type, then the value is not going to be accessed, therefore it is | |
4023 | -- inappropriate to do any validity check at the call site. | |
4024 | ||
4025 | else | |
4026 | -- Only need to worry about scalar types | |
4027 | ||
4028 | if Is_Scalar_Type (Typ) then | |
ee6ba406 | 4029 | declare |
4030 | P : Node_Id; | |
4031 | N : Node_Id; | |
4032 | E : Entity_Id; | |
4033 | F : Entity_Id; | |
4034 | A : Node_Id; | |
4035 | L : List_Id; | |
4036 | ||
4037 | begin | |
4038 | -- Find actual argument (which may be a parameter association) | |
4039 | -- and the parent of the actual argument (the call statement) | |
4040 | ||
4041 | N := Expr; | |
4042 | P := Parent (Expr); | |
4043 | ||
4044 | if Nkind (P) = N_Parameter_Association then | |
4045 | N := P; | |
4046 | P := Parent (N); | |
4047 | end if; | |
4048 | ||
feff2f05 | 4049 | -- Only need to worry if we are argument of a procedure call |
4050 | -- since functions don't have out parameters. If this is an | |
4051 | -- indirect or dispatching call, get signature from the | |
4052 | -- subprogram type. | |
ee6ba406 | 4053 | |
4054 | if Nkind (P) = N_Procedure_Call_Statement then | |
4055 | L := Parameter_Associations (P); | |
9dfe12ae | 4056 | |
4057 | if Is_Entity_Name (Name (P)) then | |
4058 | E := Entity (Name (P)); | |
4059 | else | |
4060 | pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference); | |
4061 | E := Etype (Name (P)); | |
4062 | end if; | |
ee6ba406 | 4063 | |
feff2f05 | 4064 | -- Only need to worry if there are indeed actuals, and if |
4065 | -- this could be a procedure call, otherwise we cannot get a | |
4066 | -- match (either we are not an argument, or the mode of the | |
4067 | -- formal is not OUT). This test also filters out the | |
4068 | -- generic case. | |
ee6ba406 | 4069 | |
4070 | if Is_Non_Empty_List (L) | |
4071 | and then Is_Subprogram (E) | |
4072 | then | |
feff2f05 | 4073 | -- This is the loop through parameters, looking for an |
4074 | -- OUT parameter for which we are the argument. | |
ee6ba406 | 4075 | |
4076 | F := First_Formal (E); | |
4077 | A := First (L); | |
ee6ba406 | 4078 | while Present (F) loop |
4079 | if Ekind (F) = E_Out_Parameter and then A = N then | |
4080 | return; | |
4081 | end if; | |
4082 | ||
4083 | Next_Formal (F); | |
4084 | Next (A); | |
4085 | end loop; | |
4086 | end if; | |
4087 | end if; | |
4088 | end; | |
4089 | end if; | |
4090 | end if; | |
4091 | ||
0577b0b1 | 4092 | -- If we fall through, a validity check is required |
ee6ba406 | 4093 | |
4094 | Insert_Valid_Check (Expr); | |
ce7498d3 | 4095 | |
4096 | if Is_Entity_Name (Expr) | |
4097 | and then Safe_To_Capture_Value (Expr, Entity (Expr)) | |
4098 | then | |
4099 | Set_Is_Known_Valid (Entity (Expr)); | |
4100 | end if; | |
ee6ba406 | 4101 | end Ensure_Valid; |
4102 | ||
4103 | ---------------------- | |
4104 | -- Expr_Known_Valid -- | |
4105 | ---------------------- | |
4106 | ||
4107 | function Expr_Known_Valid (Expr : Node_Id) return Boolean is | |
4108 | Typ : constant Entity_Id := Etype (Expr); | |
4109 | ||
4110 | begin | |
feff2f05 | 4111 | -- Non-scalar types are always considered valid, since they never give |
4112 | -- rise to the issues of erroneous or bounded error behavior that are | |
4113 | -- the concern. In formal reference manual terms the notion of validity | |
4114 | -- only applies to scalar types. Note that even when packed arrays are | |
4115 | -- represented using modular types, they are still arrays semantically, | |
4116 | -- so they are also always valid (in particular, the unused bits can be | |
4117 | -- random rubbish without affecting the validity of the array value). | |
ee6ba406 | 4118 | |
fa814356 | 4119 | if not Is_Scalar_Type (Typ) or else Is_Packed_Array_Type (Typ) then |
ee6ba406 | 4120 | return True; |
4121 | ||
4122 | -- If no validity checking, then everything is considered valid | |
4123 | ||
4124 | elsif not Validity_Checks_On then | |
4125 | return True; | |
4126 | ||
4127 | -- Floating-point types are considered valid unless floating-point | |
4128 | -- validity checks have been specifically turned on. | |
4129 | ||
4130 | elsif Is_Floating_Point_Type (Typ) | |
4131 | and then not Validity_Check_Floating_Point | |
4132 | then | |
4133 | return True; | |
4134 | ||
feff2f05 | 4135 | -- If the expression is the value of an object that is known to be |
4136 | -- valid, then clearly the expression value itself is valid. | |
ee6ba406 | 4137 | |
4138 | elsif Is_Entity_Name (Expr) | |
4139 | and then Is_Known_Valid (Entity (Expr)) | |
4140 | then | |
4141 | return True; | |
4142 | ||
0577b0b1 | 4143 | -- References to discriminants are always considered valid. The value |
4144 | -- of a discriminant gets checked when the object is built. Within the | |
4145 | -- record, we consider it valid, and it is important to do so, since | |
4146 | -- otherwise we can try to generate bogus validity checks which | |
feff2f05 | 4147 | -- reference discriminants out of scope. Discriminants of concurrent |
4148 | -- types are excluded for the same reason. | |
0577b0b1 | 4149 | |
4150 | elsif Is_Entity_Name (Expr) | |
feff2f05 | 4151 | and then Denotes_Discriminant (Expr, Check_Concurrent => True) |
0577b0b1 | 4152 | then |
4153 | return True; | |
4154 | ||
feff2f05 | 4155 | -- If the type is one for which all values are known valid, then we are |
4156 | -- sure that the value is valid except in the slightly odd case where | |
4157 | -- the expression is a reference to a variable whose size has been | |
4158 | -- explicitly set to a value greater than the object size. | |
ee6ba406 | 4159 | |
4160 | elsif Is_Known_Valid (Typ) then | |
4161 | if Is_Entity_Name (Expr) | |
4162 | and then Ekind (Entity (Expr)) = E_Variable | |
4163 | and then Esize (Entity (Expr)) > Esize (Typ) | |
4164 | then | |
4165 | return False; | |
4166 | else | |
4167 | return True; | |
4168 | end if; | |
4169 | ||
4170 | -- Integer and character literals always have valid values, where | |
4171 | -- appropriate these will be range checked in any case. | |
4172 | ||
4173 | elsif Nkind (Expr) = N_Integer_Literal | |
4174 | or else | |
4175 | Nkind (Expr) = N_Character_Literal | |
4176 | then | |
4177 | return True; | |
4178 | ||
4179 | -- If we have a type conversion or a qualification of a known valid | |
4180 | -- value, then the result will always be valid. | |
4181 | ||
4182 | elsif Nkind (Expr) = N_Type_Conversion | |
4183 | or else | |
4184 | Nkind (Expr) = N_Qualified_Expression | |
4185 | then | |
4186 | return Expr_Known_Valid (Expression (Expr)); | |
4187 | ||
38f5559f | 4188 | -- The result of any operator is always considered valid, since we |
4189 | -- assume the necessary checks are done by the operator. For operators | |
4190 | -- on floating-point operations, we must also check when the operation | |
4191 | -- is the right-hand side of an assignment, or is an actual in a call. | |
ee6ba406 | 4192 | |
0577b0b1 | 4193 | elsif Nkind (Expr) in N_Op then |
1d90d657 | 4194 | if Is_Floating_Point_Type (Typ) |
4195 | and then Validity_Check_Floating_Point | |
4196 | and then | |
4197 | (Nkind (Parent (Expr)) = N_Assignment_Statement | |
4198 | or else Nkind (Parent (Expr)) = N_Function_Call | |
4199 | or else Nkind (Parent (Expr)) = N_Parameter_Association) | |
4200 | then | |
4201 | return False; | |
4202 | else | |
4203 | return True; | |
4204 | end if; | |
4205 | ||
feff2f05 | 4206 | -- The result of a membership test is always valid, since it is true or |
4207 | -- false, there are no other possibilities. | |
0577b0b1 | 4208 | |
4209 | elsif Nkind (Expr) in N_Membership_Test then | |
4210 | return True; | |
4211 | ||
ee6ba406 | 4212 | -- For all other cases, we do not know the expression is valid |
4213 | ||
4214 | else | |
4215 | return False; | |
4216 | end if; | |
4217 | end Expr_Known_Valid; | |
4218 | ||
9dfe12ae | 4219 | ---------------- |
4220 | -- Find_Check -- | |
4221 | ---------------- | |
4222 | ||
4223 | procedure Find_Check | |
4224 | (Expr : Node_Id; | |
4225 | Check_Type : Character; | |
4226 | Target_Type : Entity_Id; | |
4227 | Entry_OK : out Boolean; | |
4228 | Check_Num : out Nat; | |
4229 | Ent : out Entity_Id; | |
4230 | Ofs : out Uint) | |
4231 | is | |
4232 | function Within_Range_Of | |
4233 | (Target_Type : Entity_Id; | |
314a23b6 | 4234 | Check_Type : Entity_Id) return Boolean; |
9dfe12ae | 4235 | -- Given a requirement for checking a range against Target_Type, and |
4236 | -- and a range Check_Type against which a check has already been made, | |
4237 | -- determines if the check against check type is sufficient to ensure | |
4238 | -- that no check against Target_Type is required. | |
4239 | ||
4240 | --------------------- | |
4241 | -- Within_Range_Of -- | |
4242 | --------------------- | |
4243 | ||
4244 | function Within_Range_Of | |
4245 | (Target_Type : Entity_Id; | |
314a23b6 | 4246 | Check_Type : Entity_Id) return Boolean |
9dfe12ae | 4247 | is |
4248 | begin | |
4249 | if Target_Type = Check_Type then | |
4250 | return True; | |
4251 | ||
4252 | else | |
4253 | declare | |
4254 | Tlo : constant Node_Id := Type_Low_Bound (Target_Type); | |
4255 | Thi : constant Node_Id := Type_High_Bound (Target_Type); | |
4256 | Clo : constant Node_Id := Type_Low_Bound (Check_Type); | |
4257 | Chi : constant Node_Id := Type_High_Bound (Check_Type); | |
4258 | ||
4259 | begin | |
4260 | if (Tlo = Clo | |
4261 | or else (Compile_Time_Known_Value (Tlo) | |
4262 | and then | |
4263 | Compile_Time_Known_Value (Clo) | |
4264 | and then | |
4265 | Expr_Value (Clo) >= Expr_Value (Tlo))) | |
4266 | and then | |
4267 | (Thi = Chi | |
4268 | or else (Compile_Time_Known_Value (Thi) | |
4269 | and then | |
4270 | Compile_Time_Known_Value (Chi) | |
4271 | and then | |
4272 | Expr_Value (Chi) <= Expr_Value (Clo))) | |
4273 | then | |
4274 | return True; | |
4275 | else | |
4276 | return False; | |
4277 | end if; | |
4278 | end; | |
4279 | end if; | |
4280 | end Within_Range_Of; | |
4281 | ||
4282 | -- Start of processing for Find_Check | |
4283 | ||
4284 | begin | |
f2a06be9 | 4285 | -- Establish default, to avoid warnings from GCC |
9dfe12ae | 4286 | |
4287 | Check_Num := 0; | |
4288 | ||
4289 | -- Case of expression is simple entity reference | |
4290 | ||
4291 | if Is_Entity_Name (Expr) then | |
4292 | Ent := Entity (Expr); | |
4293 | Ofs := Uint_0; | |
4294 | ||
4295 | -- Case of expression is entity + known constant | |
4296 | ||
4297 | elsif Nkind (Expr) = N_Op_Add | |
4298 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
4299 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
4300 | then | |
4301 | Ent := Entity (Left_Opnd (Expr)); | |
4302 | Ofs := Expr_Value (Right_Opnd (Expr)); | |
4303 | ||
4304 | -- Case of expression is entity - known constant | |
4305 | ||
4306 | elsif Nkind (Expr) = N_Op_Subtract | |
4307 | and then Compile_Time_Known_Value (Right_Opnd (Expr)) | |
4308 | and then Is_Entity_Name (Left_Opnd (Expr)) | |
4309 | then | |
4310 | Ent := Entity (Left_Opnd (Expr)); | |
4311 | Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr))); | |
4312 | ||
4313 | -- Any other expression is not of the right form | |
4314 | ||
4315 | else | |
4316 | Ent := Empty; | |
4317 | Ofs := Uint_0; | |
4318 | Entry_OK := False; | |
4319 | return; | |
4320 | end if; | |
4321 | ||
feff2f05 | 4322 | -- Come here with expression of appropriate form, check if entity is an |
4323 | -- appropriate one for our purposes. | |
9dfe12ae | 4324 | |
4325 | if (Ekind (Ent) = E_Variable | |
cc60bd16 | 4326 | or else Is_Constant_Object (Ent)) |
9dfe12ae | 4327 | and then not Is_Library_Level_Entity (Ent) |
4328 | then | |
4329 | Entry_OK := True; | |
4330 | else | |
4331 | Entry_OK := False; | |
4332 | return; | |
4333 | end if; | |
4334 | ||
4335 | -- See if there is matching check already | |
4336 | ||
4337 | for J in reverse 1 .. Num_Saved_Checks loop | |
4338 | declare | |
4339 | SC : Saved_Check renames Saved_Checks (J); | |
4340 | ||
4341 | begin | |
4342 | if SC.Killed = False | |
4343 | and then SC.Entity = Ent | |
4344 | and then SC.Offset = Ofs | |
4345 | and then SC.Check_Type = Check_Type | |
4346 | and then Within_Range_Of (Target_Type, SC.Target_Type) | |
4347 | then | |
4348 | Check_Num := J; | |
4349 | return; | |
4350 | end if; | |
4351 | end; | |
4352 | end loop; | |
4353 | ||
4354 | -- If we fall through entry was not found | |
4355 | ||
4356 | Check_Num := 0; | |
4357 | return; | |
4358 | end Find_Check; | |
4359 | ||
4360 | --------------------------------- | |
4361 | -- Generate_Discriminant_Check -- | |
4362 | --------------------------------- | |
4363 | ||
4364 | -- Note: the code for this procedure is derived from the | |
feff2f05 | 4365 | -- Emit_Discriminant_Check Routine in trans.c. |
9dfe12ae | 4366 | |
4367 | procedure Generate_Discriminant_Check (N : Node_Id) is | |
4368 | Loc : constant Source_Ptr := Sloc (N); | |
4369 | Pref : constant Node_Id := Prefix (N); | |
4370 | Sel : constant Node_Id := Selector_Name (N); | |
4371 | ||
4372 | Orig_Comp : constant Entity_Id := | |
4373 | Original_Record_Component (Entity (Sel)); | |
4374 | -- The original component to be checked | |
4375 | ||
4376 | Discr_Fct : constant Entity_Id := | |
4377 | Discriminant_Checking_Func (Orig_Comp); | |
4378 | -- The discriminant checking function | |
4379 | ||
4380 | Discr : Entity_Id; | |
4381 | -- One discriminant to be checked in the type | |
4382 | ||
4383 | Real_Discr : Entity_Id; | |
4384 | -- Actual discriminant in the call | |
4385 | ||
4386 | Pref_Type : Entity_Id; | |
4387 | -- Type of relevant prefix (ignoring private/access stuff) | |
4388 | ||
4389 | Args : List_Id; | |
4390 | -- List of arguments for function call | |
4391 | ||
4392 | Formal : Entity_Id; | |
feff2f05 | 4393 | -- Keep track of the formal corresponding to the actual we build for |
4394 | -- each discriminant, in order to be able to perform the necessary type | |
4395 | -- conversions. | |
9dfe12ae | 4396 | |
4397 | Scomp : Node_Id; | |
4398 | -- Selected component reference for checking function argument | |
4399 | ||
4400 | begin | |
4401 | Pref_Type := Etype (Pref); | |
4402 | ||
4403 | -- Force evaluation of the prefix, so that it does not get evaluated | |
4404 | -- twice (once for the check, once for the actual reference). Such a | |
4405 | -- double evaluation is always a potential source of inefficiency, | |
4406 | -- and is functionally incorrect in the volatile case, or when the | |
4407 | -- prefix may have side-effects. An entity or a component of an | |
4408 | -- entity requires no evaluation. | |
4409 | ||
4410 | if Is_Entity_Name (Pref) then | |
4411 | if Treat_As_Volatile (Entity (Pref)) then | |
4412 | Force_Evaluation (Pref, Name_Req => True); | |
4413 | end if; | |
4414 | ||
4415 | elsif Treat_As_Volatile (Etype (Pref)) then | |
4416 | Force_Evaluation (Pref, Name_Req => True); | |
4417 | ||
4418 | elsif Nkind (Pref) = N_Selected_Component | |
4419 | and then Is_Entity_Name (Prefix (Pref)) | |
4420 | then | |
4421 | null; | |
4422 | ||
4423 | else | |
4424 | Force_Evaluation (Pref, Name_Req => True); | |
4425 | end if; | |
4426 | ||
4427 | -- For a tagged type, use the scope of the original component to | |
4428 | -- obtain the type, because ??? | |
4429 | ||
4430 | if Is_Tagged_Type (Scope (Orig_Comp)) then | |
4431 | Pref_Type := Scope (Orig_Comp); | |
4432 | ||
feff2f05 | 4433 | -- For an untagged derived type, use the discriminants of the parent |
4434 | -- which have been renamed in the derivation, possibly by a one-to-many | |
4435 | -- discriminant constraint. For non-tagged type, initially get the Etype | |
4436 | -- of the prefix | |
9dfe12ae | 4437 | |
4438 | else | |
4439 | if Is_Derived_Type (Pref_Type) | |
4440 | and then Number_Discriminants (Pref_Type) /= | |
4441 | Number_Discriminants (Etype (Base_Type (Pref_Type))) | |
4442 | then | |
4443 | Pref_Type := Etype (Base_Type (Pref_Type)); | |
4444 | end if; | |
4445 | end if; | |
4446 | ||
4447 | -- We definitely should have a checking function, This routine should | |
4448 | -- not be called if no discriminant checking function is present. | |
4449 | ||
4450 | pragma Assert (Present (Discr_Fct)); | |
4451 | ||
4452 | -- Create the list of the actual parameters for the call. This list | |
4453 | -- is the list of the discriminant fields of the record expression to | |
4454 | -- be discriminant checked. | |
4455 | ||
4456 | Args := New_List; | |
4457 | Formal := First_Formal (Discr_Fct); | |
4458 | Discr := First_Discriminant (Pref_Type); | |
4459 | while Present (Discr) loop | |
4460 | ||
4461 | -- If we have a corresponding discriminant field, and a parent | |
4462 | -- subtype is present, then we want to use the corresponding | |
4463 | -- discriminant since this is the one with the useful value. | |
4464 | ||
4465 | if Present (Corresponding_Discriminant (Discr)) | |
4466 | and then Ekind (Pref_Type) = E_Record_Type | |
4467 | and then Present (Parent_Subtype (Pref_Type)) | |
4468 | then | |
4469 | Real_Discr := Corresponding_Discriminant (Discr); | |
4470 | else | |
4471 | Real_Discr := Discr; | |
4472 | end if; | |
4473 | ||
4474 | -- Construct the reference to the discriminant | |
4475 | ||
4476 | Scomp := | |
4477 | Make_Selected_Component (Loc, | |
4478 | Prefix => | |
4479 | Unchecked_Convert_To (Pref_Type, | |
4480 | Duplicate_Subexpr (Pref)), | |
4481 | Selector_Name => New_Occurrence_Of (Real_Discr, Loc)); | |
4482 | ||
4483 | -- Manually analyze and resolve this selected component. We really | |
4484 | -- want it just as it appears above, and do not want the expander | |
feff2f05 | 4485 | -- playing discriminal games etc with this reference. Then we append |
4486 | -- the argument to the list we are gathering. | |
9dfe12ae | 4487 | |
4488 | Set_Etype (Scomp, Etype (Real_Discr)); | |
4489 | Set_Analyzed (Scomp, True); | |
4490 | Append_To (Args, Convert_To (Etype (Formal), Scomp)); | |
4491 | ||
4492 | Next_Formal_With_Extras (Formal); | |
4493 | Next_Discriminant (Discr); | |
4494 | end loop; | |
4495 | ||
4496 | -- Now build and insert the call | |
4497 | ||
4498 | Insert_Action (N, | |
4499 | Make_Raise_Constraint_Error (Loc, | |
4500 | Condition => | |
4501 | Make_Function_Call (Loc, | |
4502 | Name => New_Occurrence_Of (Discr_Fct, Loc), | |
4503 | Parameter_Associations => Args), | |
4504 | Reason => CE_Discriminant_Check_Failed)); | |
4505 | end Generate_Discriminant_Check; | |
4506 | ||
5c99c290 | 4507 | --------------------------- |
4508 | -- Generate_Index_Checks -- | |
4509 | --------------------------- | |
9dfe12ae | 4510 | |
4511 | procedure Generate_Index_Checks (N : Node_Id) is | |
4512 | Loc : constant Source_Ptr := Sloc (N); | |
4513 | A : constant Node_Id := Prefix (N); | |
4514 | Sub : Node_Id; | |
4515 | Ind : Nat; | |
4516 | Num : List_Id; | |
4517 | ||
4518 | begin | |
0577b0b1 | 4519 | -- Ignore call if index checks suppressed for array object or type |
4520 | ||
4521 | if (Is_Entity_Name (A) and then Index_Checks_Suppressed (Entity (A))) | |
4522 | or else Index_Checks_Suppressed (Etype (A)) | |
4523 | then | |
4524 | return; | |
4525 | end if; | |
4526 | ||
4527 | -- Generate the checks | |
4528 | ||
9dfe12ae | 4529 | Sub := First (Expressions (N)); |
4530 | Ind := 1; | |
4531 | while Present (Sub) loop | |
4532 | if Do_Range_Check (Sub) then | |
4533 | Set_Do_Range_Check (Sub, False); | |
4534 | ||
feff2f05 | 4535 | -- Force evaluation except for the case of a simple name of a |
4536 | -- non-volatile entity. | |
9dfe12ae | 4537 | |
4538 | if not Is_Entity_Name (Sub) | |
4539 | or else Treat_As_Volatile (Entity (Sub)) | |
4540 | then | |
4541 | Force_Evaluation (Sub); | |
4542 | end if; | |
4543 | ||
4544 | -- Generate a raise of constraint error with the appropriate | |
4545 | -- reason and a condition of the form: | |
4546 | ||
4547 | -- Base_Type(Sub) not in array'range (subscript) | |
4548 | ||
feff2f05 | 4549 | -- Note that the reason we generate the conversion to the base |
4550 | -- type here is that we definitely want the range check to take | |
4551 | -- place, even if it looks like the subtype is OK. Optimization | |
4552 | -- considerations that allow us to omit the check have already | |
4553 | -- been taken into account in the setting of the Do_Range_Check | |
4554 | -- flag earlier on. | |
9dfe12ae | 4555 | |
4556 | if Ind = 1 then | |
4557 | Num := No_List; | |
4558 | else | |
4559 | Num := New_List (Make_Integer_Literal (Loc, Ind)); | |
4560 | end if; | |
4561 | ||
4562 | Insert_Action (N, | |
4563 | Make_Raise_Constraint_Error (Loc, | |
4564 | Condition => | |
4565 | Make_Not_In (Loc, | |
4566 | Left_Opnd => | |
4567 | Convert_To (Base_Type (Etype (Sub)), | |
4568 | Duplicate_Subexpr_Move_Checks (Sub)), | |
4569 | Right_Opnd => | |
4570 | Make_Attribute_Reference (Loc, | |
cc60bd16 | 4571 | Prefix => |
4572 | Duplicate_Subexpr_Move_Checks (A, Name_Req => True), | |
9dfe12ae | 4573 | Attribute_Name => Name_Range, |
4574 | Expressions => Num)), | |
4575 | Reason => CE_Index_Check_Failed)); | |
4576 | end if; | |
4577 | ||
4578 | Ind := Ind + 1; | |
4579 | Next (Sub); | |
4580 | end loop; | |
4581 | end Generate_Index_Checks; | |
4582 | ||
4583 | -------------------------- | |
4584 | -- Generate_Range_Check -- | |
4585 | -------------------------- | |
4586 | ||
4587 | procedure Generate_Range_Check | |
4588 | (N : Node_Id; | |
4589 | Target_Type : Entity_Id; | |
4590 | Reason : RT_Exception_Code) | |
4591 | is | |
4592 | Loc : constant Source_Ptr := Sloc (N); | |
4593 | Source_Type : constant Entity_Id := Etype (N); | |
4594 | Source_Base_Type : constant Entity_Id := Base_Type (Source_Type); | |
4595 | Target_Base_Type : constant Entity_Id := Base_Type (Target_Type); | |
4596 | ||
4597 | begin | |
feff2f05 | 4598 | -- First special case, if the source type is already within the range |
4599 | -- of the target type, then no check is needed (probably we should have | |
4600 | -- stopped Do_Range_Check from being set in the first place, but better | |
4601 | -- late than later in preventing junk code! | |
9dfe12ae | 4602 | |
feff2f05 | 4603 | -- We do NOT apply this if the source node is a literal, since in this |
4604 | -- case the literal has already been labeled as having the subtype of | |
4605 | -- the target. | |
9dfe12ae | 4606 | |
7a1dabb3 | 4607 | if In_Subrange_Of (Source_Type, Target_Type) |
9dfe12ae | 4608 | and then not |
4609 | (Nkind (N) = N_Integer_Literal | |
4610 | or else | |
4611 | Nkind (N) = N_Real_Literal | |
4612 | or else | |
4613 | Nkind (N) = N_Character_Literal | |
4614 | or else | |
4615 | (Is_Entity_Name (N) | |
4616 | and then Ekind (Entity (N)) = E_Enumeration_Literal)) | |
4617 | then | |
4618 | return; | |
4619 | end if; | |
4620 | ||
4621 | -- We need a check, so force evaluation of the node, so that it does | |
4622 | -- not get evaluated twice (once for the check, once for the actual | |
4623 | -- reference). Such a double evaluation is always a potential source | |
4624 | -- of inefficiency, and is functionally incorrect in the volatile case. | |
4625 | ||
4626 | if not Is_Entity_Name (N) | |
4627 | or else Treat_As_Volatile (Entity (N)) | |
4628 | then | |
4629 | Force_Evaluation (N); | |
4630 | end if; | |
4631 | ||
feff2f05 | 4632 | -- The easiest case is when Source_Base_Type and Target_Base_Type are |
4633 | -- the same since in this case we can simply do a direct check of the | |
4634 | -- value of N against the bounds of Target_Type. | |
9dfe12ae | 4635 | |
4636 | -- [constraint_error when N not in Target_Type] | |
4637 | ||
4638 | -- Note: this is by far the most common case, for example all cases of | |
4639 | -- checks on the RHS of assignments are in this category, but not all | |
4640 | -- cases are like this. Notably conversions can involve two types. | |
4641 | ||
4642 | if Source_Base_Type = Target_Base_Type then | |
4643 | Insert_Action (N, | |
4644 | Make_Raise_Constraint_Error (Loc, | |
4645 | Condition => | |
4646 | Make_Not_In (Loc, | |
4647 | Left_Opnd => Duplicate_Subexpr (N), | |
4648 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
4649 | Reason => Reason)); | |
4650 | ||
4651 | -- Next test for the case where the target type is within the bounds | |
4652 | -- of the base type of the source type, since in this case we can | |
4653 | -- simply convert these bounds to the base type of T to do the test. | |
4654 | ||
4655 | -- [constraint_error when N not in | |
4656 | -- Source_Base_Type (Target_Type'First) | |
4657 | -- .. | |
4658 | -- Source_Base_Type(Target_Type'Last))] | |
4659 | ||
f2a06be9 | 4660 | -- The conversions will always work and need no check |
9dfe12ae | 4661 | |
7a1dabb3 | 4662 | elsif In_Subrange_Of (Target_Type, Source_Base_Type) then |
9dfe12ae | 4663 | Insert_Action (N, |
4664 | Make_Raise_Constraint_Error (Loc, | |
4665 | Condition => | |
4666 | Make_Not_In (Loc, | |
4667 | Left_Opnd => Duplicate_Subexpr (N), | |
4668 | ||
4669 | Right_Opnd => | |
4670 | Make_Range (Loc, | |
4671 | Low_Bound => | |
4672 | Convert_To (Source_Base_Type, | |
4673 | Make_Attribute_Reference (Loc, | |
4674 | Prefix => | |
4675 | New_Occurrence_Of (Target_Type, Loc), | |
4676 | Attribute_Name => Name_First)), | |
4677 | ||
4678 | High_Bound => | |
4679 | Convert_To (Source_Base_Type, | |
4680 | Make_Attribute_Reference (Loc, | |
4681 | Prefix => | |
4682 | New_Occurrence_Of (Target_Type, Loc), | |
4683 | Attribute_Name => Name_Last)))), | |
4684 | Reason => Reason)); | |
4685 | ||
feff2f05 | 4686 | -- Note that at this stage we now that the Target_Base_Type is not in |
4687 | -- the range of the Source_Base_Type (since even the Target_Type itself | |
4688 | -- is not in this range). It could still be the case that Source_Type is | |
4689 | -- in range of the target base type since we have not checked that case. | |
9dfe12ae | 4690 | |
feff2f05 | 4691 | -- If that is the case, we can freely convert the source to the target, |
4692 | -- and then test the target result against the bounds. | |
9dfe12ae | 4693 | |
7a1dabb3 | 4694 | elsif In_Subrange_Of (Source_Type, Target_Base_Type) then |
9dfe12ae | 4695 | |
feff2f05 | 4696 | -- We make a temporary to hold the value of the converted value |
4697 | -- (converted to the base type), and then we will do the test against | |
4698 | -- this temporary. | |
9dfe12ae | 4699 | |
4700 | -- Tnn : constant Target_Base_Type := Target_Base_Type (N); | |
4701 | -- [constraint_error when Tnn not in Target_Type] | |
4702 | ||
4703 | -- Then the conversion itself is replaced by an occurrence of Tnn | |
4704 | ||
4705 | declare | |
4706 | Tnn : constant Entity_Id := | |
4707 | Make_Defining_Identifier (Loc, | |
4708 | Chars => New_Internal_Name ('T')); | |
4709 | ||
4710 | begin | |
4711 | Insert_Actions (N, New_List ( | |
4712 | Make_Object_Declaration (Loc, | |
4713 | Defining_Identifier => Tnn, | |
4714 | Object_Definition => | |
4715 | New_Occurrence_Of (Target_Base_Type, Loc), | |
4716 | Constant_Present => True, | |
4717 | Expression => | |
4718 | Make_Type_Conversion (Loc, | |
4719 | Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc), | |
4720 | Expression => Duplicate_Subexpr (N))), | |
4721 | ||
4722 | Make_Raise_Constraint_Error (Loc, | |
4723 | Condition => | |
4724 | Make_Not_In (Loc, | |
4725 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
4726 | Right_Opnd => New_Occurrence_Of (Target_Type, Loc)), | |
4727 | ||
4728 | Reason => Reason))); | |
4729 | ||
4730 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
2af58f67 | 4731 | |
4732 | -- Set the type of N, because the declaration for Tnn might not | |
4733 | -- be analyzed yet, as is the case if N appears within a record | |
4734 | -- declaration, as a discriminant constraint or expression. | |
4735 | ||
4736 | Set_Etype (N, Target_Base_Type); | |
9dfe12ae | 4737 | end; |
4738 | ||
4739 | -- At this stage, we know that we have two scalar types, which are | |
4740 | -- directly convertible, and where neither scalar type has a base | |
4741 | -- range that is in the range of the other scalar type. | |
4742 | ||
4743 | -- The only way this can happen is with a signed and unsigned type. | |
4744 | -- So test for these two cases: | |
4745 | ||
4746 | else | |
4747 | -- Case of the source is unsigned and the target is signed | |
4748 | ||
4749 | if Is_Unsigned_Type (Source_Base_Type) | |
4750 | and then not Is_Unsigned_Type (Target_Base_Type) | |
4751 | then | |
4752 | -- If the source is unsigned and the target is signed, then we | |
4753 | -- know that the source is not shorter than the target (otherwise | |
4754 | -- the source base type would be in the target base type range). | |
4755 | ||
feff2f05 | 4756 | -- In other words, the unsigned type is either the same size as |
4757 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 4758 | |
4759 | pragma Assert | |
4760 | (Esize (Source_Base_Type) >= Esize (Target_Base_Type)); | |
4761 | ||
4762 | -- We only need to check the low bound if the low bound of the | |
4763 | -- target type is non-negative. If the low bound of the target | |
4764 | -- type is negative, then we know that we will fit fine. | |
4765 | ||
4766 | -- If the high bound of the target type is negative, then we | |
4767 | -- know we have a constraint error, since we can't possibly | |
4768 | -- have a negative source. | |
4769 | ||
4770 | -- With these two checks out of the way, we can do the check | |
4771 | -- using the source type safely | |
4772 | ||
4773 | -- This is definitely the most annoying case! | |
4774 | ||
4775 | -- [constraint_error | |
4776 | -- when (Target_Type'First >= 0 | |
4777 | -- and then | |
4778 | -- N < Source_Base_Type (Target_Type'First)) | |
4779 | -- or else Target_Type'Last < 0 | |
4780 | -- or else N > Source_Base_Type (Target_Type'Last)]; | |
4781 | ||
4782 | -- We turn off all checks since we know that the conversions | |
4783 | -- will work fine, given the guards for negative values. | |
4784 | ||
4785 | Insert_Action (N, | |
4786 | Make_Raise_Constraint_Error (Loc, | |
4787 | Condition => | |
4788 | Make_Or_Else (Loc, | |
4789 | Make_Or_Else (Loc, | |
4790 | Left_Opnd => | |
4791 | Make_And_Then (Loc, | |
4792 | Left_Opnd => Make_Op_Ge (Loc, | |
4793 | Left_Opnd => | |
4794 | Make_Attribute_Reference (Loc, | |
4795 | Prefix => | |
4796 | New_Occurrence_Of (Target_Type, Loc), | |
4797 | Attribute_Name => Name_First), | |
4798 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
4799 | ||
4800 | Right_Opnd => | |
4801 | Make_Op_Lt (Loc, | |
4802 | Left_Opnd => Duplicate_Subexpr (N), | |
4803 | Right_Opnd => | |
4804 | Convert_To (Source_Base_Type, | |
4805 | Make_Attribute_Reference (Loc, | |
4806 | Prefix => | |
4807 | New_Occurrence_Of (Target_Type, Loc), | |
4808 | Attribute_Name => Name_First)))), | |
4809 | ||
4810 | Right_Opnd => | |
4811 | Make_Op_Lt (Loc, | |
4812 | Left_Opnd => | |
4813 | Make_Attribute_Reference (Loc, | |
4814 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
4815 | Attribute_Name => Name_Last), | |
4816 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0))), | |
4817 | ||
4818 | Right_Opnd => | |
4819 | Make_Op_Gt (Loc, | |
4820 | Left_Opnd => Duplicate_Subexpr (N), | |
4821 | Right_Opnd => | |
4822 | Convert_To (Source_Base_Type, | |
4823 | Make_Attribute_Reference (Loc, | |
4824 | Prefix => New_Occurrence_Of (Target_Type, Loc), | |
4825 | Attribute_Name => Name_Last)))), | |
4826 | ||
4827 | Reason => Reason), | |
4828 | Suppress => All_Checks); | |
4829 | ||
4830 | -- Only remaining possibility is that the source is signed and | |
fc75802a | 4831 | -- the target is unsigned. |
9dfe12ae | 4832 | |
4833 | else | |
4834 | pragma Assert (not Is_Unsigned_Type (Source_Base_Type) | |
4835 | and then Is_Unsigned_Type (Target_Base_Type)); | |
4836 | ||
feff2f05 | 4837 | -- If the source is signed and the target is unsigned, then we |
4838 | -- know that the target is not shorter than the source (otherwise | |
4839 | -- the target base type would be in the source base type range). | |
9dfe12ae | 4840 | |
feff2f05 | 4841 | -- In other words, the unsigned type is either the same size as |
4842 | -- the target, or it is larger. It cannot be smaller. | |
9dfe12ae | 4843 | |
feff2f05 | 4844 | -- Clearly we have an error if the source value is negative since |
4845 | -- no unsigned type can have negative values. If the source type | |
4846 | -- is non-negative, then the check can be done using the target | |
4847 | -- type. | |
9dfe12ae | 4848 | |
4849 | -- Tnn : constant Target_Base_Type (N) := Target_Type; | |
4850 | ||
4851 | -- [constraint_error | |
4852 | -- when N < 0 or else Tnn not in Target_Type]; | |
4853 | ||
feff2f05 | 4854 | -- We turn off all checks for the conversion of N to the target |
4855 | -- base type, since we generate the explicit check to ensure that | |
4856 | -- the value is non-negative | |
9dfe12ae | 4857 | |
4858 | declare | |
4859 | Tnn : constant Entity_Id := | |
4860 | Make_Defining_Identifier (Loc, | |
4861 | Chars => New_Internal_Name ('T')); | |
4862 | ||
4863 | begin | |
4864 | Insert_Actions (N, New_List ( | |
4865 | Make_Object_Declaration (Loc, | |
4866 | Defining_Identifier => Tnn, | |
4867 | Object_Definition => | |
4868 | New_Occurrence_Of (Target_Base_Type, Loc), | |
4869 | Constant_Present => True, | |
4870 | Expression => | |
4871 | Make_Type_Conversion (Loc, | |
4872 | Subtype_Mark => | |
4873 | New_Occurrence_Of (Target_Base_Type, Loc), | |
4874 | Expression => Duplicate_Subexpr (N))), | |
4875 | ||
4876 | Make_Raise_Constraint_Error (Loc, | |
4877 | Condition => | |
4878 | Make_Or_Else (Loc, | |
4879 | Left_Opnd => | |
4880 | Make_Op_Lt (Loc, | |
4881 | Left_Opnd => Duplicate_Subexpr (N), | |
4882 | Right_Opnd => Make_Integer_Literal (Loc, Uint_0)), | |
4883 | ||
4884 | Right_Opnd => | |
4885 | Make_Not_In (Loc, | |
4886 | Left_Opnd => New_Occurrence_Of (Tnn, Loc), | |
4887 | Right_Opnd => | |
4888 | New_Occurrence_Of (Target_Type, Loc))), | |
4889 | ||
4890 | Reason => Reason)), | |
4891 | Suppress => All_Checks); | |
4892 | ||
feff2f05 | 4893 | -- Set the Etype explicitly, because Insert_Actions may have |
4894 | -- placed the declaration in the freeze list for an enclosing | |
4895 | -- construct, and thus it is not analyzed yet. | |
9dfe12ae | 4896 | |
4897 | Set_Etype (Tnn, Target_Base_Type); | |
4898 | Rewrite (N, New_Occurrence_Of (Tnn, Loc)); | |
4899 | end; | |
4900 | end if; | |
4901 | end if; | |
4902 | end Generate_Range_Check; | |
4903 | ||
2af58f67 | 4904 | ------------------ |
4905 | -- Get_Check_Id -- | |
4906 | ------------------ | |
4907 | ||
4908 | function Get_Check_Id (N : Name_Id) return Check_Id is | |
4909 | begin | |
4910 | -- For standard check name, we can do a direct computation | |
4911 | ||
4912 | if N in First_Check_Name .. Last_Check_Name then | |
4913 | return Check_Id (N - (First_Check_Name - 1)); | |
4914 | ||
4915 | -- For non-standard names added by pragma Check_Name, search table | |
4916 | ||
4917 | else | |
4918 | for J in All_Checks + 1 .. Check_Names.Last loop | |
4919 | if Check_Names.Table (J) = N then | |
4920 | return J; | |
4921 | end if; | |
4922 | end loop; | |
4923 | end if; | |
4924 | ||
4925 | -- No matching name found | |
4926 | ||
4927 | return No_Check_Id; | |
4928 | end Get_Check_Id; | |
4929 | ||
ee6ba406 | 4930 | --------------------- |
4931 | -- Get_Discriminal -- | |
4932 | --------------------- | |
4933 | ||
4934 | function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is | |
4935 | Loc : constant Source_Ptr := Sloc (E); | |
4936 | D : Entity_Id; | |
4937 | Sc : Entity_Id; | |
4938 | ||
4939 | begin | |
0577b0b1 | 4940 | -- The bound can be a bona fide parameter of a protected operation, |
4941 | -- rather than a prival encoded as an in-parameter. | |
4942 | ||
4943 | if No (Discriminal_Link (Entity (Bound))) then | |
4944 | return Bound; | |
4945 | end if; | |
4946 | ||
2af58f67 | 4947 | -- Climb the scope stack looking for an enclosing protected type. If |
4948 | -- we run out of scopes, return the bound itself. | |
4949 | ||
4950 | Sc := Scope (E); | |
4951 | while Present (Sc) loop | |
4952 | if Sc = Standard_Standard then | |
4953 | return Bound; | |
4954 | ||
4955 | elsif Ekind (Sc) = E_Protected_Type then | |
4956 | exit; | |
4957 | end if; | |
4958 | ||
4959 | Sc := Scope (Sc); | |
4960 | end loop; | |
4961 | ||
ee6ba406 | 4962 | D := First_Discriminant (Sc); |
2af58f67 | 4963 | while Present (D) loop |
4964 | if Chars (D) = Chars (Bound) then | |
4965 | return New_Occurrence_Of (Discriminal (D), Loc); | |
4966 | end if; | |
ee6ba406 | 4967 | |
ee6ba406 | 4968 | Next_Discriminant (D); |
4969 | end loop; | |
4970 | ||
2af58f67 | 4971 | return Bound; |
ee6ba406 | 4972 | end Get_Discriminal; |
4973 | ||
2af58f67 | 4974 | ---------------------- |
4975 | -- Get_Range_Checks -- | |
4976 | ---------------------- | |
4977 | ||
4978 | function Get_Range_Checks | |
4979 | (Ck_Node : Node_Id; | |
4980 | Target_Typ : Entity_Id; | |
4981 | Source_Typ : Entity_Id := Empty; | |
4982 | Warn_Node : Node_Id := Empty) return Check_Result | |
4983 | is | |
4984 | begin | |
4985 | return Selected_Range_Checks | |
4986 | (Ck_Node, Target_Typ, Source_Typ, Warn_Node); | |
4987 | end Get_Range_Checks; | |
4988 | ||
ee6ba406 | 4989 | ------------------ |
4990 | -- Guard_Access -- | |
4991 | ------------------ | |
4992 | ||
4993 | function Guard_Access | |
4994 | (Cond : Node_Id; | |
4995 | Loc : Source_Ptr; | |
314a23b6 | 4996 | Ck_Node : Node_Id) return Node_Id |
ee6ba406 | 4997 | is |
4998 | begin | |
4999 | if Nkind (Cond) = N_Or_Else then | |
5000 | Set_Paren_Count (Cond, 1); | |
5001 | end if; | |
5002 | ||
5003 | if Nkind (Ck_Node) = N_Allocator then | |
5004 | return Cond; | |
5005 | else | |
5006 | return | |
5007 | Make_And_Then (Loc, | |
5008 | Left_Opnd => | |
5009 | Make_Op_Ne (Loc, | |
9dfe12ae | 5010 | Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node), |
ee6ba406 | 5011 | Right_Opnd => Make_Null (Loc)), |
5012 | Right_Opnd => Cond); | |
5013 | end if; | |
5014 | end Guard_Access; | |
5015 | ||
5016 | ----------------------------- | |
5017 | -- Index_Checks_Suppressed -- | |
5018 | ----------------------------- | |
5019 | ||
5020 | function Index_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5021 | begin | |
9dfe12ae | 5022 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5023 | return Is_Check_Suppressed (E, Index_Check); | |
5024 | else | |
5025 | return Scope_Suppress (Index_Check); | |
5026 | end if; | |
ee6ba406 | 5027 | end Index_Checks_Suppressed; |
5028 | ||
5029 | ---------------- | |
5030 | -- Initialize -- | |
5031 | ---------------- | |
5032 | ||
5033 | procedure Initialize is | |
5034 | begin | |
5035 | for J in Determine_Range_Cache_N'Range loop | |
5036 | Determine_Range_Cache_N (J) := Empty; | |
5037 | end loop; | |
2af58f67 | 5038 | |
5039 | Check_Names.Init; | |
5040 | ||
5041 | for J in Int range 1 .. All_Checks loop | |
5042 | Check_Names.Append (Name_Id (Int (First_Check_Name) + J - 1)); | |
5043 | end loop; | |
ee6ba406 | 5044 | end Initialize; |
5045 | ||
5046 | ------------------------- | |
5047 | -- Insert_Range_Checks -- | |
5048 | ------------------------- | |
5049 | ||
5050 | procedure Insert_Range_Checks | |
5051 | (Checks : Check_Result; | |
5052 | Node : Node_Id; | |
5053 | Suppress_Typ : Entity_Id; | |
5054 | Static_Sloc : Source_Ptr := No_Location; | |
5055 | Flag_Node : Node_Id := Empty; | |
5056 | Do_Before : Boolean := False) | |
5057 | is | |
5058 | Internal_Flag_Node : Node_Id := Flag_Node; | |
5059 | Internal_Static_Sloc : Source_Ptr := Static_Sloc; | |
5060 | ||
5061 | Check_Node : Node_Id; | |
5062 | Checks_On : constant Boolean := | |
5063 | (not Index_Checks_Suppressed (Suppress_Typ)) | |
5064 | or else | |
5065 | (not Range_Checks_Suppressed (Suppress_Typ)); | |
5066 | ||
5067 | begin | |
feff2f05 | 5068 | -- For now we just return if Checks_On is false, however this should be |
5069 | -- enhanced to check for an always True value in the condition and to | |
5070 | -- generate a compilation warning??? | |
ee6ba406 | 5071 | |
5072 | if not Expander_Active or else not Checks_On then | |
5073 | return; | |
5074 | end if; | |
5075 | ||
5076 | if Static_Sloc = No_Location then | |
5077 | Internal_Static_Sloc := Sloc (Node); | |
5078 | end if; | |
5079 | ||
5080 | if No (Flag_Node) then | |
5081 | Internal_Flag_Node := Node; | |
5082 | end if; | |
5083 | ||
5084 | for J in 1 .. 2 loop | |
5085 | exit when No (Checks (J)); | |
5086 | ||
5087 | if Nkind (Checks (J)) = N_Raise_Constraint_Error | |
5088 | and then Present (Condition (Checks (J))) | |
5089 | then | |
5090 | if not Has_Dynamic_Range_Check (Internal_Flag_Node) then | |
5091 | Check_Node := Checks (J); | |
5092 | Mark_Rewrite_Insertion (Check_Node); | |
5093 | ||
5094 | if Do_Before then | |
5095 | Insert_Before_And_Analyze (Node, Check_Node); | |
5096 | else | |
5097 | Insert_After_And_Analyze (Node, Check_Node); | |
5098 | end if; | |
5099 | ||
5100 | Set_Has_Dynamic_Range_Check (Internal_Flag_Node); | |
5101 | end if; | |
5102 | ||
5103 | else | |
5104 | Check_Node := | |
f15731c4 | 5105 | Make_Raise_Constraint_Error (Internal_Static_Sloc, |
5106 | Reason => CE_Range_Check_Failed); | |
ee6ba406 | 5107 | Mark_Rewrite_Insertion (Check_Node); |
5108 | ||
5109 | if Do_Before then | |
5110 | Insert_Before_And_Analyze (Node, Check_Node); | |
5111 | else | |
5112 | Insert_After_And_Analyze (Node, Check_Node); | |
5113 | end if; | |
5114 | end if; | |
5115 | end loop; | |
5116 | end Insert_Range_Checks; | |
5117 | ||
5118 | ------------------------ | |
5119 | -- Insert_Valid_Check -- | |
5120 | ------------------------ | |
5121 | ||
5122 | procedure Insert_Valid_Check (Expr : Node_Id) is | |
5123 | Loc : constant Source_Ptr := Sloc (Expr); | |
8b718dab | 5124 | Exp : Node_Id; |
ee6ba406 | 5125 | |
5126 | begin | |
5127 | -- Do not insert if checks off, or if not checking validity | |
5128 | ||
0577b0b1 | 5129 | if not Validity_Checks_On |
5130 | or else Range_Or_Validity_Checks_Suppressed (Expr) | |
ee6ba406 | 5131 | then |
8b718dab | 5132 | return; |
5133 | end if; | |
ee6ba406 | 5134 | |
8b718dab | 5135 | -- If we have a checked conversion, then validity check applies to |
5136 | -- the expression inside the conversion, not the result, since if | |
5137 | -- the expression inside is valid, then so is the conversion result. | |
ee6ba406 | 5138 | |
8b718dab | 5139 | Exp := Expr; |
5140 | while Nkind (Exp) = N_Type_Conversion loop | |
5141 | Exp := Expression (Exp); | |
5142 | end loop; | |
5143 | ||
0577b0b1 | 5144 | -- We are about to insert the validity check for Exp. We save and |
5145 | -- reset the Do_Range_Check flag over this validity check, and then | |
5146 | -- put it back for the final original reference (Exp may be rewritten). | |
5147 | ||
5148 | declare | |
5149 | DRC : constant Boolean := Do_Range_Check (Exp); | |
05fcfafb | 5150 | |
0577b0b1 | 5151 | begin |
5152 | Set_Do_Range_Check (Exp, False); | |
5153 | ||
5154 | -- Insert the validity check. Note that we do this with validity | |
5155 | -- checks turned off, to avoid recursion, we do not want validity | |
5156 | -- checks on the validity checking code itself! | |
5157 | ||
5158 | Insert_Action | |
5159 | (Expr, | |
5160 | Make_Raise_Constraint_Error (Loc, | |
5161 | Condition => | |
5162 | Make_Op_Not (Loc, | |
5163 | Right_Opnd => | |
5164 | Make_Attribute_Reference (Loc, | |
5165 | Prefix => | |
5166 | Duplicate_Subexpr_No_Checks (Exp, Name_Req => True), | |
5167 | Attribute_Name => Name_Valid)), | |
5168 | Reason => CE_Invalid_Data), | |
5169 | Suppress => Validity_Check); | |
5170 | ||
5171 | -- If the expression is a a reference to an element of a bit-packed | |
5172 | -- array, then it is rewritten as a renaming declaration. If the | |
5173 | -- expression is an actual in a call, it has not been expanded, | |
5174 | -- waiting for the proper point at which to do it. The same happens | |
5175 | -- with renamings, so that we have to force the expansion now. This | |
5176 | -- non-local complication is due to code in exp_ch2,adb, exp_ch4.adb | |
5177 | -- and exp_ch6.adb. | |
5178 | ||
5179 | if Is_Entity_Name (Exp) | |
5180 | and then Nkind (Parent (Entity (Exp))) = | |
5181 | N_Object_Renaming_Declaration | |
5182 | then | |
5183 | declare | |
5184 | Old_Exp : constant Node_Id := Name (Parent (Entity (Exp))); | |
5185 | begin | |
5186 | if Nkind (Old_Exp) = N_Indexed_Component | |
5187 | and then Is_Bit_Packed_Array (Etype (Prefix (Old_Exp))) | |
5188 | then | |
5189 | Expand_Packed_Element_Reference (Old_Exp); | |
5190 | end if; | |
5191 | end; | |
5192 | end if; | |
5193 | ||
5194 | -- Put back the Do_Range_Check flag on the resulting (possibly | |
5195 | -- rewritten) expression. | |
5196 | ||
5197 | -- Note: it might be thought that a validity check is not required | |
5198 | -- when a range check is present, but that's not the case, because | |
5199 | -- the back end is allowed to assume for the range check that the | |
5200 | -- operand is within its declared range (an assumption that validity | |
5201 | -- checking is all about NOT assuming!) | |
5202 | ||
00c403ee | 5203 | -- Note: no need to worry about Possible_Local_Raise here, it will |
5204 | -- already have been called if original node has Do_Range_Check set. | |
5205 | ||
0577b0b1 | 5206 | Set_Do_Range_Check (Exp, DRC); |
5207 | end; | |
ee6ba406 | 5208 | end Insert_Valid_Check; |
5209 | ||
fa7497e8 | 5210 | ---------------------------------- |
5211 | -- Install_Null_Excluding_Check -- | |
5212 | ---------------------------------- | |
5213 | ||
5214 | procedure Install_Null_Excluding_Check (N : Node_Id) is | |
84d0d4a5 | 5215 | Loc : constant Source_Ptr := Sloc (N); |
5216 | Typ : constant Entity_Id := Etype (N); | |
5217 | ||
7870823d | 5218 | function In_Declarative_Region_Of_Subprogram_Body return Boolean; |
5219 | -- Determine whether node N, a reference to an *in* parameter, is | |
5220 | -- inside the declarative region of the current subprogram body. | |
5221 | ||
84d0d4a5 | 5222 | procedure Mark_Non_Null; |
7870823d | 5223 | -- After installation of check, if the node in question is an entity |
5224 | -- name, then mark this entity as non-null if possible. | |
5225 | ||
5226 | ---------------------------------------------- | |
5227 | -- In_Declarative_Region_Of_Subprogram_Body -- | |
5228 | ---------------------------------------------- | |
5229 | ||
5230 | function In_Declarative_Region_Of_Subprogram_Body return Boolean is | |
5231 | E : constant Entity_Id := Entity (N); | |
5232 | S : constant Entity_Id := Current_Scope; | |
5233 | S_Par : Node_Id; | |
5234 | ||
5235 | begin | |
5236 | pragma Assert (Ekind (E) = E_In_Parameter); | |
5237 | ||
5238 | -- Two initial context checks. We must be inside a subprogram body | |
5239 | -- with declarations and reference must not appear in nested scopes. | |
5240 | ||
5241 | if (Ekind (S) /= E_Function | |
5242 | and then Ekind (S) /= E_Procedure) | |
5243 | or else Scope (E) /= S | |
5244 | then | |
5245 | return False; | |
5246 | end if; | |
5247 | ||
5248 | S_Par := Parent (Parent (S)); | |
5249 | ||
5250 | if Nkind (S_Par) /= N_Subprogram_Body | |
5251 | or else No (Declarations (S_Par)) | |
5252 | then | |
5253 | return False; | |
5254 | end if; | |
5255 | ||
5256 | declare | |
5257 | N_Decl : Node_Id; | |
5258 | P : Node_Id; | |
5259 | ||
5260 | begin | |
5261 | -- Retrieve the declaration node of N (if any). Note that N | |
5262 | -- may be a part of a complex initialization expression. | |
5263 | ||
5264 | P := Parent (N); | |
5265 | N_Decl := Empty; | |
5266 | while Present (P) loop | |
5267 | ||
5268 | -- While traversing the parent chain, we find that N | |
5269 | -- belongs to a statement, thus it may never appear in | |
5270 | -- a declarative region. | |
5271 | ||
5272 | if Nkind (P) in N_Statement_Other_Than_Procedure_Call | |
5273 | or else Nkind (P) = N_Procedure_Call_Statement | |
5274 | then | |
5275 | return False; | |
5276 | end if; | |
5277 | ||
5278 | if Nkind (P) in N_Declaration | |
5279 | and then Nkind (P) not in N_Subprogram_Specification | |
5280 | then | |
5281 | N_Decl := P; | |
5282 | exit; | |
5283 | end if; | |
5284 | ||
5285 | P := Parent (P); | |
5286 | end loop; | |
5287 | ||
5288 | if No (N_Decl) then | |
5289 | return False; | |
5290 | end if; | |
5291 | ||
5292 | return List_Containing (N_Decl) = Declarations (S_Par); | |
5293 | end; | |
5294 | end In_Declarative_Region_Of_Subprogram_Body; | |
84d0d4a5 | 5295 | |
5296 | ------------------- | |
5297 | -- Mark_Non_Null -- | |
5298 | ------------------- | |
5299 | ||
5300 | procedure Mark_Non_Null is | |
5301 | begin | |
7870823d | 5302 | -- Only case of interest is if node N is an entity name |
5303 | ||
84d0d4a5 | 5304 | if Is_Entity_Name (N) then |
7870823d | 5305 | |
5306 | -- For sure, we want to clear an indication that this is known to | |
5307 | -- be null, since if we get past this check, it definitely is not! | |
5308 | ||
84d0d4a5 | 5309 | Set_Is_Known_Null (Entity (N), False); |
5310 | ||
7870823d | 5311 | -- We can mark the entity as known to be non-null if either it is |
5312 | -- safe to capture the value, or in the case of an IN parameter, | |
5313 | -- which is a constant, if the check we just installed is in the | |
5314 | -- declarative region of the subprogram body. In this latter case, | |
5315 | -- a check is decisive for the rest of the body, since we know we | |
5316 | -- must complete all declarations before executing the body. | |
5317 | ||
5318 | if Safe_To_Capture_Value (N, Entity (N)) | |
5319 | or else | |
5320 | (Ekind (Entity (N)) = E_In_Parameter | |
5321 | and then In_Declarative_Region_Of_Subprogram_Body) | |
5322 | then | |
5323 | Set_Is_Known_Non_Null (Entity (N)); | |
84d0d4a5 | 5324 | end if; |
5325 | end if; | |
5326 | end Mark_Non_Null; | |
5327 | ||
5328 | -- Start of processing for Install_Null_Excluding_Check | |
fa7497e8 | 5329 | |
5330 | begin | |
84d0d4a5 | 5331 | pragma Assert (Is_Access_Type (Typ)); |
fa7497e8 | 5332 | |
84d0d4a5 | 5333 | -- No check inside a generic (why not???) |
fa7497e8 | 5334 | |
84d0d4a5 | 5335 | if Inside_A_Generic then |
fa7497e8 | 5336 | return; |
84d0d4a5 | 5337 | end if; |
5338 | ||
5339 | -- No check needed if known to be non-null | |
5340 | ||
5341 | if Known_Non_Null (N) then | |
05fcfafb | 5342 | return; |
84d0d4a5 | 5343 | end if; |
fa7497e8 | 5344 | |
84d0d4a5 | 5345 | -- If known to be null, here is where we generate a compile time check |
5346 | ||
5347 | if Known_Null (N) then | |
d16989f1 | 5348 | |
5349 | -- Avoid generating warning message inside init procs | |
5350 | ||
5351 | if not Inside_Init_Proc then | |
5352 | Apply_Compile_Time_Constraint_Error | |
5353 | (N, | |
5354 | "null value not allowed here?", | |
5355 | CE_Access_Check_Failed); | |
5356 | else | |
5357 | Insert_Action (N, | |
5358 | Make_Raise_Constraint_Error (Loc, | |
5359 | Reason => CE_Access_Check_Failed)); | |
5360 | end if; | |
5361 | ||
84d0d4a5 | 5362 | Mark_Non_Null; |
5363 | return; | |
5364 | end if; | |
5365 | ||
5366 | -- If entity is never assigned, for sure a warning is appropriate | |
5367 | ||
5368 | if Is_Entity_Name (N) then | |
5369 | Check_Unset_Reference (N); | |
fa7497e8 | 5370 | end if; |
84d0d4a5 | 5371 | |
5372 | -- No check needed if checks are suppressed on the range. Note that we | |
5373 | -- don't set Is_Known_Non_Null in this case (we could legitimately do | |
5374 | -- so, since the program is erroneous, but we don't like to casually | |
5375 | -- propagate such conclusions from erroneosity). | |
5376 | ||
5377 | if Access_Checks_Suppressed (Typ) then | |
5378 | return; | |
5379 | end if; | |
5380 | ||
2af58f67 | 5381 | -- No check needed for access to concurrent record types generated by |
5382 | -- the expander. This is not just an optimization (though it does indeed | |
5383 | -- remove junk checks). It also avoids generation of junk warnings. | |
5384 | ||
5385 | if Nkind (N) in N_Has_Chars | |
5386 | and then Chars (N) = Name_uObject | |
5387 | and then Is_Concurrent_Record_Type | |
5388 | (Directly_Designated_Type (Etype (N))) | |
5389 | then | |
5390 | return; | |
5391 | end if; | |
5392 | ||
84d0d4a5 | 5393 | -- Otherwise install access check |
5394 | ||
5395 | Insert_Action (N, | |
5396 | Make_Raise_Constraint_Error (Loc, | |
5397 | Condition => | |
5398 | Make_Op_Eq (Loc, | |
5399 | Left_Opnd => Duplicate_Subexpr_Move_Checks (N), | |
5400 | Right_Opnd => Make_Null (Loc)), | |
5401 | Reason => CE_Access_Check_Failed)); | |
5402 | ||
5403 | Mark_Non_Null; | |
fa7497e8 | 5404 | end Install_Null_Excluding_Check; |
5405 | ||
ee6ba406 | 5406 | -------------------------- |
5407 | -- Install_Static_Check -- | |
5408 | -------------------------- | |
5409 | ||
5410 | procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is | |
5411 | Stat : constant Boolean := Is_Static_Expression (R_Cno); | |
5412 | Typ : constant Entity_Id := Etype (R_Cno); | |
5413 | ||
5414 | begin | |
f15731c4 | 5415 | Rewrite (R_Cno, |
5416 | Make_Raise_Constraint_Error (Loc, | |
5417 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 5418 | Set_Analyzed (R_Cno); |
5419 | Set_Etype (R_Cno, Typ); | |
5420 | Set_Raises_Constraint_Error (R_Cno); | |
5421 | Set_Is_Static_Expression (R_Cno, Stat); | |
5422 | end Install_Static_Check; | |
5423 | ||
9dfe12ae | 5424 | --------------------- |
5425 | -- Kill_All_Checks -- | |
5426 | --------------------- | |
5427 | ||
5428 | procedure Kill_All_Checks is | |
5429 | begin | |
5430 | if Debug_Flag_CC then | |
5431 | w ("Kill_All_Checks"); | |
5432 | end if; | |
5433 | ||
feff2f05 | 5434 | -- We reset the number of saved checks to zero, and also modify all |
5435 | -- stack entries for statement ranges to indicate that the number of | |
5436 | -- checks at each level is now zero. | |
9dfe12ae | 5437 | |
5438 | Num_Saved_Checks := 0; | |
5439 | ||
96da3284 | 5440 | -- Note: the Int'Min here avoids any possibility of J being out of |
5441 | -- range when called from e.g. Conditional_Statements_Begin. | |
5442 | ||
5443 | for J in 1 .. Int'Min (Saved_Checks_TOS, Saved_Checks_Stack'Last) loop | |
9dfe12ae | 5444 | Saved_Checks_Stack (J) := 0; |
5445 | end loop; | |
5446 | end Kill_All_Checks; | |
5447 | ||
5448 | ----------------- | |
5449 | -- Kill_Checks -- | |
5450 | ----------------- | |
5451 | ||
5452 | procedure Kill_Checks (V : Entity_Id) is | |
5453 | begin | |
5454 | if Debug_Flag_CC then | |
5455 | w ("Kill_Checks for entity", Int (V)); | |
5456 | end if; | |
5457 | ||
5458 | for J in 1 .. Num_Saved_Checks loop | |
5459 | if Saved_Checks (J).Entity = V then | |
5460 | if Debug_Flag_CC then | |
5461 | w (" Checks killed for saved check ", J); | |
5462 | end if; | |
5463 | ||
5464 | Saved_Checks (J).Killed := True; | |
5465 | end if; | |
5466 | end loop; | |
5467 | end Kill_Checks; | |
5468 | ||
ee6ba406 | 5469 | ------------------------------ |
5470 | -- Length_Checks_Suppressed -- | |
5471 | ------------------------------ | |
5472 | ||
5473 | function Length_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5474 | begin | |
9dfe12ae | 5475 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5476 | return Is_Check_Suppressed (E, Length_Check); | |
5477 | else | |
5478 | return Scope_Suppress (Length_Check); | |
5479 | end if; | |
ee6ba406 | 5480 | end Length_Checks_Suppressed; |
5481 | ||
5482 | -------------------------------- | |
5483 | -- Overflow_Checks_Suppressed -- | |
5484 | -------------------------------- | |
5485 | ||
5486 | function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5487 | begin | |
9dfe12ae | 5488 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
5489 | return Is_Check_Suppressed (E, Overflow_Check); | |
5490 | else | |
5491 | return Scope_Suppress (Overflow_Check); | |
5492 | end if; | |
ee6ba406 | 5493 | end Overflow_Checks_Suppressed; |
fc75802a | 5494 | |
ee6ba406 | 5495 | ----------------------------- |
5496 | -- Range_Checks_Suppressed -- | |
5497 | ----------------------------- | |
5498 | ||
5499 | function Range_Checks_Suppressed (E : Entity_Id) return Boolean is | |
5500 | begin | |
9dfe12ae | 5501 | if Present (E) then |
5502 | ||
5503 | -- Note: for now we always suppress range checks on Vax float types, | |
5504 | -- since Gigi does not know how to generate these checks. | |
5505 | ||
5506 | if Vax_Float (E) then | |
5507 | return True; | |
5508 | elsif Kill_Range_Checks (E) then | |
5509 | return True; | |
5510 | elsif Checks_May_Be_Suppressed (E) then | |
5511 | return Is_Check_Suppressed (E, Range_Check); | |
5512 | end if; | |
5513 | end if; | |
ee6ba406 | 5514 | |
9dfe12ae | 5515 | return Scope_Suppress (Range_Check); |
ee6ba406 | 5516 | end Range_Checks_Suppressed; |
5517 | ||
0577b0b1 | 5518 | ----------------------------------------- |
5519 | -- Range_Or_Validity_Checks_Suppressed -- | |
5520 | ----------------------------------------- | |
5521 | ||
5522 | -- Note: the coding would be simpler here if we simply made appropriate | |
5523 | -- calls to Range/Validity_Checks_Suppressed, but that would result in | |
5524 | -- duplicated checks which we prefer to avoid. | |
5525 | ||
5526 | function Range_Or_Validity_Checks_Suppressed | |
5527 | (Expr : Node_Id) return Boolean | |
5528 | is | |
5529 | begin | |
5530 | -- Immediate return if scope checks suppressed for either check | |
5531 | ||
5532 | if Scope_Suppress (Range_Check) or Scope_Suppress (Validity_Check) then | |
5533 | return True; | |
5534 | end if; | |
5535 | ||
5536 | -- If no expression, that's odd, decide that checks are suppressed, | |
5537 | -- since we don't want anyone trying to do checks in this case, which | |
5538 | -- is most likely the result of some other error. | |
5539 | ||
5540 | if No (Expr) then | |
5541 | return True; | |
5542 | end if; | |
5543 | ||
5544 | -- Expression is present, so perform suppress checks on type | |
5545 | ||
5546 | declare | |
5547 | Typ : constant Entity_Id := Etype (Expr); | |
5548 | begin | |
5549 | if Vax_Float (Typ) then | |
5550 | return True; | |
5551 | elsif Checks_May_Be_Suppressed (Typ) | |
5552 | and then (Is_Check_Suppressed (Typ, Range_Check) | |
5553 | or else | |
5554 | Is_Check_Suppressed (Typ, Validity_Check)) | |
5555 | then | |
5556 | return True; | |
5557 | end if; | |
5558 | end; | |
5559 | ||
5560 | -- If expression is an entity name, perform checks on this entity | |
5561 | ||
5562 | if Is_Entity_Name (Expr) then | |
5563 | declare | |
5564 | Ent : constant Entity_Id := Entity (Expr); | |
5565 | begin | |
5566 | if Checks_May_Be_Suppressed (Ent) then | |
5567 | return Is_Check_Suppressed (Ent, Range_Check) | |
5568 | or else Is_Check_Suppressed (Ent, Validity_Check); | |
5569 | end if; | |
5570 | end; | |
5571 | end if; | |
5572 | ||
5573 | -- If we fall through, no checks suppressed | |
5574 | ||
5575 | return False; | |
5576 | end Range_Or_Validity_Checks_Suppressed; | |
5577 | ||
226494a3 | 5578 | ------------------- |
5579 | -- Remove_Checks -- | |
5580 | ------------------- | |
5581 | ||
5582 | procedure Remove_Checks (Expr : Node_Id) is | |
226494a3 | 5583 | function Process (N : Node_Id) return Traverse_Result; |
5584 | -- Process a single node during the traversal | |
5585 | ||
8f6e4fd5 | 5586 | procedure Traverse is new Traverse_Proc (Process); |
5587 | -- The traversal procedure itself | |
226494a3 | 5588 | |
5589 | ------------- | |
5590 | -- Process -- | |
5591 | ------------- | |
5592 | ||
5593 | function Process (N : Node_Id) return Traverse_Result is | |
5594 | begin | |
5595 | if Nkind (N) not in N_Subexpr then | |
5596 | return Skip; | |
5597 | end if; | |
5598 | ||
5599 | Set_Do_Range_Check (N, False); | |
5600 | ||
5601 | case Nkind (N) is | |
5602 | when N_And_Then => | |
8f6e4fd5 | 5603 | Traverse (Left_Opnd (N)); |
226494a3 | 5604 | return Skip; |
5605 | ||
5606 | when N_Attribute_Reference => | |
226494a3 | 5607 | Set_Do_Overflow_Check (N, False); |
5608 | ||
226494a3 | 5609 | when N_Function_Call => |
5610 | Set_Do_Tag_Check (N, False); | |
5611 | ||
226494a3 | 5612 | when N_Op => |
5613 | Set_Do_Overflow_Check (N, False); | |
5614 | ||
5615 | case Nkind (N) is | |
5616 | when N_Op_Divide => | |
5617 | Set_Do_Division_Check (N, False); | |
5618 | ||
5619 | when N_Op_And => | |
5620 | Set_Do_Length_Check (N, False); | |
5621 | ||
5622 | when N_Op_Mod => | |
5623 | Set_Do_Division_Check (N, False); | |
5624 | ||
5625 | when N_Op_Or => | |
5626 | Set_Do_Length_Check (N, False); | |
5627 | ||
5628 | when N_Op_Rem => | |
5629 | Set_Do_Division_Check (N, False); | |
5630 | ||
5631 | when N_Op_Xor => | |
5632 | Set_Do_Length_Check (N, False); | |
5633 | ||
5634 | when others => | |
5635 | null; | |
5636 | end case; | |
5637 | ||
5638 | when N_Or_Else => | |
8f6e4fd5 | 5639 | Traverse (Left_Opnd (N)); |
226494a3 | 5640 | return Skip; |
5641 | ||
5642 | when N_Selected_Component => | |
226494a3 | 5643 | Set_Do_Discriminant_Check (N, False); |
5644 | ||
226494a3 | 5645 | when N_Type_Conversion => |
9dfe12ae | 5646 | Set_Do_Length_Check (N, False); |
5647 | Set_Do_Tag_Check (N, False); | |
226494a3 | 5648 | Set_Do_Overflow_Check (N, False); |
226494a3 | 5649 | |
5650 | when others => | |
5651 | null; | |
5652 | end case; | |
5653 | ||
5654 | return OK; | |
5655 | end Process; | |
5656 | ||
5657 | -- Start of processing for Remove_Checks | |
5658 | ||
5659 | begin | |
8f6e4fd5 | 5660 | Traverse (Expr); |
226494a3 | 5661 | end Remove_Checks; |
5662 | ||
ee6ba406 | 5663 | ---------------------------- |
5664 | -- Selected_Length_Checks -- | |
5665 | ---------------------------- | |
5666 | ||
5667 | function Selected_Length_Checks | |
5668 | (Ck_Node : Node_Id; | |
5669 | Target_Typ : Entity_Id; | |
5670 | Source_Typ : Entity_Id; | |
314a23b6 | 5671 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 5672 | is |
5673 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
5674 | S_Typ : Entity_Id; | |
5675 | T_Typ : Entity_Id; | |
5676 | Expr_Actual : Node_Id; | |
5677 | Exptyp : Entity_Id; | |
5678 | Cond : Node_Id := Empty; | |
5679 | Do_Access : Boolean := False; | |
5680 | Wnode : Node_Id := Warn_Node; | |
5681 | Ret_Result : Check_Result := (Empty, Empty); | |
5682 | Num_Checks : Natural := 0; | |
5683 | ||
5684 | procedure Add_Check (N : Node_Id); | |
5685 | -- Adds the action given to Ret_Result if N is non-Empty | |
5686 | ||
5687 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id; | |
5688 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id; | |
314a23b6 | 5689 | -- Comments required ??? |
ee6ba406 | 5690 | |
5691 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean; | |
5692 | -- True for equal literals and for nodes that denote the same constant | |
5f260d20 | 5693 | -- entity, even if its value is not a static constant. This includes the |
9dfe12ae | 5694 | -- case of a discriminal reference within an init proc. Removes some |
5f260d20 | 5695 | -- obviously superfluous checks. |
ee6ba406 | 5696 | |
5697 | function Length_E_Cond | |
5698 | (Exptyp : Entity_Id; | |
5699 | Typ : Entity_Id; | |
314a23b6 | 5700 | Indx : Nat) return Node_Id; |
ee6ba406 | 5701 | -- Returns expression to compute: |
5702 | -- Typ'Length /= Exptyp'Length | |
5703 | ||
5704 | function Length_N_Cond | |
5705 | (Expr : Node_Id; | |
5706 | Typ : Entity_Id; | |
314a23b6 | 5707 | Indx : Nat) return Node_Id; |
ee6ba406 | 5708 | -- Returns expression to compute: |
5709 | -- Typ'Length /= Expr'Length | |
5710 | ||
5711 | --------------- | |
5712 | -- Add_Check -- | |
5713 | --------------- | |
5714 | ||
5715 | procedure Add_Check (N : Node_Id) is | |
5716 | begin | |
5717 | if Present (N) then | |
5718 | ||
5719 | -- For now, ignore attempt to place more than 2 checks ??? | |
5720 | ||
5721 | if Num_Checks = 2 then | |
5722 | return; | |
5723 | end if; | |
5724 | ||
5725 | pragma Assert (Num_Checks <= 1); | |
5726 | Num_Checks := Num_Checks + 1; | |
5727 | Ret_Result (Num_Checks) := N; | |
5728 | end if; | |
5729 | end Add_Check; | |
5730 | ||
5731 | ------------------ | |
5732 | -- Get_E_Length -- | |
5733 | ------------------ | |
5734 | ||
5735 | function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is | |
00c403ee | 5736 | SE : constant Entity_Id := Scope (E); |
ee6ba406 | 5737 | N : Node_Id; |
5738 | E1 : Entity_Id := E; | |
ee6ba406 | 5739 | |
5740 | begin | |
5741 | if Ekind (Scope (E)) = E_Record_Type | |
5742 | and then Has_Discriminants (Scope (E)) | |
5743 | then | |
5744 | N := Build_Discriminal_Subtype_Of_Component (E); | |
5745 | ||
5746 | if Present (N) then | |
5747 | Insert_Action (Ck_Node, N); | |
5748 | E1 := Defining_Identifier (N); | |
5749 | end if; | |
5750 | end if; | |
5751 | ||
5752 | if Ekind (E1) = E_String_Literal_Subtype then | |
5753 | return | |
5754 | Make_Integer_Literal (Loc, | |
5755 | Intval => String_Literal_Length (E1)); | |
5756 | ||
00c403ee | 5757 | elsif SE /= Standard_Standard |
5758 | and then Ekind (Scope (SE)) = E_Protected_Type | |
5759 | and then Has_Discriminants (Scope (SE)) | |
5760 | and then Has_Completion (Scope (SE)) | |
ee6ba406 | 5761 | and then not Inside_Init_Proc |
5762 | then | |
ee6ba406 | 5763 | -- If the type whose length is needed is a private component |
5764 | -- constrained by a discriminant, we must expand the 'Length | |
5765 | -- attribute into an explicit computation, using the discriminal | |
5766 | -- of the current protected operation. This is because the actual | |
5767 | -- type of the prival is constructed after the protected opera- | |
5768 | -- tion has been fully expanded. | |
5769 | ||
5770 | declare | |
5771 | Indx_Type : Node_Id; | |
5772 | Lo : Node_Id; | |
5773 | Hi : Node_Id; | |
5774 | Do_Expand : Boolean := False; | |
5775 | ||
5776 | begin | |
5777 | Indx_Type := First_Index (E); | |
5778 | ||
5779 | for J in 1 .. Indx - 1 loop | |
5780 | Next_Index (Indx_Type); | |
5781 | end loop; | |
5782 | ||
2af58f67 | 5783 | Get_Index_Bounds (Indx_Type, Lo, Hi); |
ee6ba406 | 5784 | |
5785 | if Nkind (Lo) = N_Identifier | |
5786 | and then Ekind (Entity (Lo)) = E_In_Parameter | |
5787 | then | |
5788 | Lo := Get_Discriminal (E, Lo); | |
5789 | Do_Expand := True; | |
5790 | end if; | |
5791 | ||
5792 | if Nkind (Hi) = N_Identifier | |
5793 | and then Ekind (Entity (Hi)) = E_In_Parameter | |
5794 | then | |
5795 | Hi := Get_Discriminal (E, Hi); | |
5796 | Do_Expand := True; | |
5797 | end if; | |
5798 | ||
5799 | if Do_Expand then | |
5800 | if not Is_Entity_Name (Lo) then | |
9dfe12ae | 5801 | Lo := Duplicate_Subexpr_No_Checks (Lo); |
ee6ba406 | 5802 | end if; |
5803 | ||
5804 | if not Is_Entity_Name (Hi) then | |
9dfe12ae | 5805 | Lo := Duplicate_Subexpr_No_Checks (Hi); |
ee6ba406 | 5806 | end if; |
5807 | ||
5808 | N := | |
5809 | Make_Op_Add (Loc, | |
5810 | Left_Opnd => | |
5811 | Make_Op_Subtract (Loc, | |
5812 | Left_Opnd => Hi, | |
5813 | Right_Opnd => Lo), | |
5814 | ||
5815 | Right_Opnd => Make_Integer_Literal (Loc, 1)); | |
5816 | return N; | |
5817 | ||
5818 | else | |
5819 | N := | |
5820 | Make_Attribute_Reference (Loc, | |
5821 | Attribute_Name => Name_Length, | |
5822 | Prefix => | |
5823 | New_Occurrence_Of (E1, Loc)); | |
5824 | ||
5825 | if Indx > 1 then | |
5826 | Set_Expressions (N, New_List ( | |
5827 | Make_Integer_Literal (Loc, Indx))); | |
5828 | end if; | |
5829 | ||
5830 | return N; | |
5831 | end if; | |
5832 | end; | |
5833 | ||
5834 | else | |
5835 | N := | |
5836 | Make_Attribute_Reference (Loc, | |
5837 | Attribute_Name => Name_Length, | |
5838 | Prefix => | |
5839 | New_Occurrence_Of (E1, Loc)); | |
5840 | ||
5841 | if Indx > 1 then | |
5842 | Set_Expressions (N, New_List ( | |
5843 | Make_Integer_Literal (Loc, Indx))); | |
5844 | end if; | |
5845 | ||
5846 | return N; | |
ee6ba406 | 5847 | end if; |
5848 | end Get_E_Length; | |
5849 | ||
5850 | ------------------ | |
5851 | -- Get_N_Length -- | |
5852 | ------------------ | |
5853 | ||
5854 | function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is | |
5855 | begin | |
5856 | return | |
5857 | Make_Attribute_Reference (Loc, | |
5858 | Attribute_Name => Name_Length, | |
5859 | Prefix => | |
9dfe12ae | 5860 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 5861 | Expressions => New_List ( |
5862 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 5863 | end Get_N_Length; |
5864 | ||
5865 | ------------------- | |
5866 | -- Length_E_Cond -- | |
5867 | ------------------- | |
5868 | ||
5869 | function Length_E_Cond | |
5870 | (Exptyp : Entity_Id; | |
5871 | Typ : Entity_Id; | |
314a23b6 | 5872 | Indx : Nat) return Node_Id |
ee6ba406 | 5873 | is |
5874 | begin | |
5875 | return | |
5876 | Make_Op_Ne (Loc, | |
5877 | Left_Opnd => Get_E_Length (Typ, Indx), | |
5878 | Right_Opnd => Get_E_Length (Exptyp, Indx)); | |
ee6ba406 | 5879 | end Length_E_Cond; |
5880 | ||
5881 | ------------------- | |
5882 | -- Length_N_Cond -- | |
5883 | ------------------- | |
5884 | ||
5885 | function Length_N_Cond | |
5886 | (Expr : Node_Id; | |
5887 | Typ : Entity_Id; | |
314a23b6 | 5888 | Indx : Nat) return Node_Id |
ee6ba406 | 5889 | is |
5890 | begin | |
5891 | return | |
5892 | Make_Op_Ne (Loc, | |
5893 | Left_Opnd => Get_E_Length (Typ, Indx), | |
5894 | Right_Opnd => Get_N_Length (Expr, Indx)); | |
ee6ba406 | 5895 | end Length_N_Cond; |
5896 | ||
feff2f05 | 5897 | ----------------- |
5898 | -- Same_Bounds -- | |
5899 | ----------------- | |
5900 | ||
ee6ba406 | 5901 | function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is |
5902 | begin | |
5903 | return | |
5904 | (Nkind (L) = N_Integer_Literal | |
5905 | and then Nkind (R) = N_Integer_Literal | |
5906 | and then Intval (L) = Intval (R)) | |
5907 | ||
5908 | or else | |
5909 | (Is_Entity_Name (L) | |
5910 | and then Ekind (Entity (L)) = E_Constant | |
5911 | and then ((Is_Entity_Name (R) | |
5912 | and then Entity (L) = Entity (R)) | |
5913 | or else | |
5914 | (Nkind (R) = N_Type_Conversion | |
5915 | and then Is_Entity_Name (Expression (R)) | |
5916 | and then Entity (L) = Entity (Expression (R))))) | |
5917 | ||
5918 | or else | |
5919 | (Is_Entity_Name (R) | |
5920 | and then Ekind (Entity (R)) = E_Constant | |
5921 | and then Nkind (L) = N_Type_Conversion | |
5922 | and then Is_Entity_Name (Expression (L)) | |
5f260d20 | 5923 | and then Entity (R) = Entity (Expression (L))) |
5924 | ||
5925 | or else | |
5926 | (Is_Entity_Name (L) | |
5927 | and then Is_Entity_Name (R) | |
5928 | and then Entity (L) = Entity (R) | |
5929 | and then Ekind (Entity (L)) = E_In_Parameter | |
5930 | and then Inside_Init_Proc); | |
ee6ba406 | 5931 | end Same_Bounds; |
5932 | ||
5933 | -- Start of processing for Selected_Length_Checks | |
5934 | ||
5935 | begin | |
5936 | if not Expander_Active then | |
5937 | return Ret_Result; | |
5938 | end if; | |
5939 | ||
5940 | if Target_Typ = Any_Type | |
5941 | or else Target_Typ = Any_Composite | |
5942 | or else Raises_Constraint_Error (Ck_Node) | |
5943 | then | |
5944 | return Ret_Result; | |
5945 | end if; | |
5946 | ||
5947 | if No (Wnode) then | |
5948 | Wnode := Ck_Node; | |
5949 | end if; | |
5950 | ||
5951 | T_Typ := Target_Typ; | |
5952 | ||
5953 | if No (Source_Typ) then | |
5954 | S_Typ := Etype (Ck_Node); | |
5955 | else | |
5956 | S_Typ := Source_Typ; | |
5957 | end if; | |
5958 | ||
5959 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
5960 | return Ret_Result; | |
5961 | end if; | |
5962 | ||
5963 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
5964 | S_Typ := Designated_Type (S_Typ); | |
5965 | T_Typ := Designated_Type (T_Typ); | |
5966 | Do_Access := True; | |
5967 | ||
2af58f67 | 5968 | -- A simple optimization for the null case |
ee6ba406 | 5969 | |
2af58f67 | 5970 | if Known_Null (Ck_Node) then |
ee6ba406 | 5971 | return Ret_Result; |
5972 | end if; | |
5973 | end if; | |
5974 | ||
5975 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
5976 | if Is_Constrained (T_Typ) then | |
5977 | ||
5978 | -- The checking code to be generated will freeze the | |
5979 | -- corresponding array type. However, we must freeze the | |
5980 | -- type now, so that the freeze node does not appear within | |
5981 | -- the generated condional expression, but ahead of it. | |
5982 | ||
5983 | Freeze_Before (Ck_Node, T_Typ); | |
5984 | ||
5985 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
84d0d4a5 | 5986 | Exptyp := Get_Actual_Subtype (Ck_Node); |
ee6ba406 | 5987 | |
5988 | if Is_Access_Type (Exptyp) then | |
5989 | Exptyp := Designated_Type (Exptyp); | |
5990 | end if; | |
5991 | ||
5992 | -- String_Literal case. This needs to be handled specially be- | |
5993 | -- cause no index types are available for string literals. The | |
5994 | -- condition is simply: | |
5995 | ||
5996 | -- T_Typ'Length = string-literal-length | |
5997 | ||
9dfe12ae | 5998 | if Nkind (Expr_Actual) = N_String_Literal |
5999 | and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype | |
6000 | then | |
ee6ba406 | 6001 | Cond := |
6002 | Make_Op_Ne (Loc, | |
6003 | Left_Opnd => Get_E_Length (T_Typ, 1), | |
6004 | Right_Opnd => | |
6005 | Make_Integer_Literal (Loc, | |
6006 | Intval => | |
6007 | String_Literal_Length (Etype (Expr_Actual)))); | |
6008 | ||
6009 | -- General array case. Here we have a usable actual subtype for | |
6010 | -- the expression, and the condition is built from the two types | |
6011 | -- (Do_Length): | |
6012 | ||
6013 | -- T_Typ'Length /= Exptyp'Length or else | |
6014 | -- T_Typ'Length (2) /= Exptyp'Length (2) or else | |
6015 | -- T_Typ'Length (3) /= Exptyp'Length (3) or else | |
6016 | -- ... | |
6017 | ||
6018 | elsif Is_Constrained (Exptyp) then | |
6019 | declare | |
9dfe12ae | 6020 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
6021 | ||
6022 | L_Index : Node_Id; | |
6023 | R_Index : Node_Id; | |
6024 | L_Low : Node_Id; | |
6025 | L_High : Node_Id; | |
6026 | R_Low : Node_Id; | |
6027 | R_High : Node_Id; | |
ee6ba406 | 6028 | L_Length : Uint; |
6029 | R_Length : Uint; | |
9dfe12ae | 6030 | Ref_Node : Node_Id; |
ee6ba406 | 6031 | |
6032 | begin | |
feff2f05 | 6033 | -- At the library level, we need to ensure that the type of |
6034 | -- the object is elaborated before the check itself is | |
6035 | -- emitted. This is only done if the object is in the | |
6036 | -- current compilation unit, otherwise the type is frozen | |
6037 | -- and elaborated in its unit. | |
9dfe12ae | 6038 | |
6039 | if Is_Itype (Exptyp) | |
6040 | and then | |
6041 | Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package | |
6042 | and then | |
6043 | not In_Package_Body (Cunit_Entity (Current_Sem_Unit)) | |
d66aa9f6 | 6044 | and then In_Open_Scopes (Scope (Exptyp)) |
9dfe12ae | 6045 | then |
6046 | Ref_Node := Make_Itype_Reference (Sloc (Ck_Node)); | |
6047 | Set_Itype (Ref_Node, Exptyp); | |
6048 | Insert_Action (Ck_Node, Ref_Node); | |
6049 | end if; | |
6050 | ||
ee6ba406 | 6051 | L_Index := First_Index (T_Typ); |
6052 | R_Index := First_Index (Exptyp); | |
6053 | ||
6054 | for Indx in 1 .. Ndims loop | |
6055 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 6056 | or else |
6057 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 6058 | then |
6059 | Get_Index_Bounds (L_Index, L_Low, L_High); | |
6060 | Get_Index_Bounds (R_Index, R_Low, R_High); | |
6061 | ||
6062 | -- Deal with compile time length check. Note that we | |
6063 | -- skip this in the access case, because the access | |
6064 | -- value may be null, so we cannot know statically. | |
6065 | ||
6066 | if not Do_Access | |
6067 | and then Compile_Time_Known_Value (L_Low) | |
6068 | and then Compile_Time_Known_Value (L_High) | |
6069 | and then Compile_Time_Known_Value (R_Low) | |
6070 | and then Compile_Time_Known_Value (R_High) | |
6071 | then | |
6072 | if Expr_Value (L_High) >= Expr_Value (L_Low) then | |
6073 | L_Length := Expr_Value (L_High) - | |
6074 | Expr_Value (L_Low) + 1; | |
6075 | else | |
6076 | L_Length := UI_From_Int (0); | |
6077 | end if; | |
6078 | ||
6079 | if Expr_Value (R_High) >= Expr_Value (R_Low) then | |
6080 | R_Length := Expr_Value (R_High) - | |
6081 | Expr_Value (R_Low) + 1; | |
6082 | else | |
6083 | R_Length := UI_From_Int (0); | |
6084 | end if; | |
6085 | ||
6086 | if L_Length > R_Length then | |
6087 | Add_Check | |
6088 | (Compile_Time_Constraint_Error | |
6089 | (Wnode, "too few elements for}?", T_Typ)); | |
6090 | ||
6091 | elsif L_Length < R_Length then | |
6092 | Add_Check | |
6093 | (Compile_Time_Constraint_Error | |
6094 | (Wnode, "too many elements for}?", T_Typ)); | |
6095 | end if; | |
6096 | ||
6097 | -- The comparison for an individual index subtype | |
6098 | -- is omitted if the corresponding index subtypes | |
6099 | -- statically match, since the result is known to | |
6100 | -- be true. Note that this test is worth while even | |
6101 | -- though we do static evaluation, because non-static | |
6102 | -- subtypes can statically match. | |
6103 | ||
6104 | elsif not | |
6105 | Subtypes_Statically_Match | |
6106 | (Etype (L_Index), Etype (R_Index)) | |
6107 | ||
6108 | and then not | |
6109 | (Same_Bounds (L_Low, R_Low) | |
6110 | and then Same_Bounds (L_High, R_High)) | |
6111 | then | |
6112 | Evolve_Or_Else | |
6113 | (Cond, Length_E_Cond (Exptyp, T_Typ, Indx)); | |
6114 | end if; | |
6115 | ||
6116 | Next (L_Index); | |
6117 | Next (R_Index); | |
6118 | end if; | |
6119 | end loop; | |
6120 | end; | |
6121 | ||
6122 | -- Handle cases where we do not get a usable actual subtype that | |
6123 | -- is constrained. This happens for example in the function call | |
6124 | -- and explicit dereference cases. In these cases, we have to get | |
6125 | -- the length or range from the expression itself, making sure we | |
6126 | -- do not evaluate it more than once. | |
6127 | ||
6128 | -- Here Ck_Node is the original expression, or more properly the | |
feff2f05 | 6129 | -- result of applying Duplicate_Expr to the original tree, forcing |
6130 | -- the result to be a name. | |
ee6ba406 | 6131 | |
6132 | else | |
6133 | declare | |
9dfe12ae | 6134 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 6135 | |
6136 | begin | |
6137 | -- Build the condition for the explicit dereference case | |
6138 | ||
6139 | for Indx in 1 .. Ndims loop | |
6140 | Evolve_Or_Else | |
6141 | (Cond, Length_N_Cond (Ck_Node, T_Typ, Indx)); | |
6142 | end loop; | |
6143 | end; | |
6144 | end if; | |
6145 | end if; | |
6146 | end if; | |
6147 | ||
6148 | -- Construct the test and insert into the tree | |
6149 | ||
6150 | if Present (Cond) then | |
6151 | if Do_Access then | |
6152 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
6153 | end if; | |
6154 | ||
f15731c4 | 6155 | Add_Check |
6156 | (Make_Raise_Constraint_Error (Loc, | |
6157 | Condition => Cond, | |
6158 | Reason => CE_Length_Check_Failed)); | |
ee6ba406 | 6159 | end if; |
6160 | ||
6161 | return Ret_Result; | |
ee6ba406 | 6162 | end Selected_Length_Checks; |
6163 | ||
6164 | --------------------------- | |
6165 | -- Selected_Range_Checks -- | |
6166 | --------------------------- | |
6167 | ||
6168 | function Selected_Range_Checks | |
6169 | (Ck_Node : Node_Id; | |
6170 | Target_Typ : Entity_Id; | |
6171 | Source_Typ : Entity_Id; | |
314a23b6 | 6172 | Warn_Node : Node_Id) return Check_Result |
ee6ba406 | 6173 | is |
6174 | Loc : constant Source_Ptr := Sloc (Ck_Node); | |
6175 | S_Typ : Entity_Id; | |
6176 | T_Typ : Entity_Id; | |
6177 | Expr_Actual : Node_Id; | |
6178 | Exptyp : Entity_Id; | |
6179 | Cond : Node_Id := Empty; | |
6180 | Do_Access : Boolean := False; | |
6181 | Wnode : Node_Id := Warn_Node; | |
6182 | Ret_Result : Check_Result := (Empty, Empty); | |
6183 | Num_Checks : Integer := 0; | |
6184 | ||
6185 | procedure Add_Check (N : Node_Id); | |
6186 | -- Adds the action given to Ret_Result if N is non-Empty | |
6187 | ||
6188 | function Discrete_Range_Cond | |
6189 | (Expr : Node_Id; | |
314a23b6 | 6190 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 6191 | -- Returns expression to compute: |
6192 | -- Low_Bound (Expr) < Typ'First | |
6193 | -- or else | |
6194 | -- High_Bound (Expr) > Typ'Last | |
6195 | ||
6196 | function Discrete_Expr_Cond | |
6197 | (Expr : Node_Id; | |
314a23b6 | 6198 | Typ : Entity_Id) return Node_Id; |
ee6ba406 | 6199 | -- Returns expression to compute: |
6200 | -- Expr < Typ'First | |
6201 | -- or else | |
6202 | -- Expr > Typ'Last | |
6203 | ||
6204 | function Get_E_First_Or_Last | |
6205 | (E : Entity_Id; | |
6206 | Indx : Nat; | |
314a23b6 | 6207 | Nam : Name_Id) return Node_Id; |
ee6ba406 | 6208 | -- Returns expression to compute: |
6209 | -- E'First or E'Last | |
6210 | ||
6211 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id; | |
6212 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id; | |
6213 | -- Returns expression to compute: | |
9dfe12ae | 6214 | -- N'First or N'Last using Duplicate_Subexpr_No_Checks |
ee6ba406 | 6215 | |
6216 | function Range_E_Cond | |
6217 | (Exptyp : Entity_Id; | |
6218 | Typ : Entity_Id; | |
6219 | Indx : Nat) | |
6220 | return Node_Id; | |
6221 | -- Returns expression to compute: | |
6222 | -- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last | |
6223 | ||
6224 | function Range_Equal_E_Cond | |
6225 | (Exptyp : Entity_Id; | |
6226 | Typ : Entity_Id; | |
314a23b6 | 6227 | Indx : Nat) return Node_Id; |
ee6ba406 | 6228 | -- Returns expression to compute: |
6229 | -- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last | |
6230 | ||
6231 | function Range_N_Cond | |
6232 | (Expr : Node_Id; | |
6233 | Typ : Entity_Id; | |
314a23b6 | 6234 | Indx : Nat) return Node_Id; |
ee6ba406 | 6235 | -- Return expression to compute: |
6236 | -- Expr'First < Typ'First or else Expr'Last > Typ'Last | |
6237 | ||
6238 | --------------- | |
6239 | -- Add_Check -- | |
6240 | --------------- | |
6241 | ||
6242 | procedure Add_Check (N : Node_Id) is | |
6243 | begin | |
6244 | if Present (N) then | |
6245 | ||
6246 | -- For now, ignore attempt to place more than 2 checks ??? | |
6247 | ||
6248 | if Num_Checks = 2 then | |
6249 | return; | |
6250 | end if; | |
6251 | ||
6252 | pragma Assert (Num_Checks <= 1); | |
6253 | Num_Checks := Num_Checks + 1; | |
6254 | Ret_Result (Num_Checks) := N; | |
6255 | end if; | |
6256 | end Add_Check; | |
6257 | ||
6258 | ------------------------- | |
6259 | -- Discrete_Expr_Cond -- | |
6260 | ------------------------- | |
6261 | ||
6262 | function Discrete_Expr_Cond | |
6263 | (Expr : Node_Id; | |
314a23b6 | 6264 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 6265 | is |
6266 | begin | |
6267 | return | |
6268 | Make_Or_Else (Loc, | |
6269 | Left_Opnd => | |
6270 | Make_Op_Lt (Loc, | |
6271 | Left_Opnd => | |
9dfe12ae | 6272 | Convert_To (Base_Type (Typ), |
6273 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 6274 | Right_Opnd => |
6275 | Convert_To (Base_Type (Typ), | |
6276 | Get_E_First_Or_Last (Typ, 0, Name_First))), | |
6277 | ||
6278 | Right_Opnd => | |
6279 | Make_Op_Gt (Loc, | |
6280 | Left_Opnd => | |
9dfe12ae | 6281 | Convert_To (Base_Type (Typ), |
6282 | Duplicate_Subexpr_No_Checks (Expr)), | |
ee6ba406 | 6283 | Right_Opnd => |
6284 | Convert_To | |
6285 | (Base_Type (Typ), | |
6286 | Get_E_First_Or_Last (Typ, 0, Name_Last)))); | |
6287 | end Discrete_Expr_Cond; | |
6288 | ||
6289 | ------------------------- | |
6290 | -- Discrete_Range_Cond -- | |
6291 | ------------------------- | |
6292 | ||
6293 | function Discrete_Range_Cond | |
6294 | (Expr : Node_Id; | |
314a23b6 | 6295 | Typ : Entity_Id) return Node_Id |
ee6ba406 | 6296 | is |
6297 | LB : Node_Id := Low_Bound (Expr); | |
6298 | HB : Node_Id := High_Bound (Expr); | |
6299 | ||
6300 | Left_Opnd : Node_Id; | |
6301 | Right_Opnd : Node_Id; | |
6302 | ||
6303 | begin | |
6304 | if Nkind (LB) = N_Identifier | |
feff2f05 | 6305 | and then Ekind (Entity (LB)) = E_Discriminant |
6306 | then | |
ee6ba406 | 6307 | LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc); |
6308 | end if; | |
6309 | ||
6310 | if Nkind (HB) = N_Identifier | |
feff2f05 | 6311 | and then Ekind (Entity (HB)) = E_Discriminant |
6312 | then | |
ee6ba406 | 6313 | HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc); |
6314 | end if; | |
6315 | ||
6316 | Left_Opnd := | |
6317 | Make_Op_Lt (Loc, | |
6318 | Left_Opnd => | |
6319 | Convert_To | |
9dfe12ae | 6320 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)), |
ee6ba406 | 6321 | |
6322 | Right_Opnd => | |
6323 | Convert_To | |
6324 | (Base_Type (Typ), Get_E_First_Or_Last (Typ, 0, Name_First))); | |
6325 | ||
6326 | if Base_Type (Typ) = Typ then | |
6327 | return Left_Opnd; | |
6328 | ||
6329 | elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ))) | |
6330 | and then | |
6331 | Compile_Time_Known_Value (High_Bound (Scalar_Range | |
6332 | (Base_Type (Typ)))) | |
6333 | then | |
6334 | if Is_Floating_Point_Type (Typ) then | |
6335 | if Expr_Value_R (High_Bound (Scalar_Range (Typ))) = | |
6336 | Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ)))) | |
6337 | then | |
6338 | return Left_Opnd; | |
6339 | end if; | |
6340 | ||
6341 | else | |
6342 | if Expr_Value (High_Bound (Scalar_Range (Typ))) = | |
6343 | Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ)))) | |
6344 | then | |
6345 | return Left_Opnd; | |
6346 | end if; | |
6347 | end if; | |
6348 | end if; | |
6349 | ||
6350 | Right_Opnd := | |
6351 | Make_Op_Gt (Loc, | |
6352 | Left_Opnd => | |
6353 | Convert_To | |
9dfe12ae | 6354 | (Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)), |
ee6ba406 | 6355 | |
6356 | Right_Opnd => | |
6357 | Convert_To | |
6358 | (Base_Type (Typ), | |
6359 | Get_E_First_Or_Last (Typ, 0, Name_Last))); | |
6360 | ||
6361 | return Make_Or_Else (Loc, Left_Opnd, Right_Opnd); | |
6362 | end Discrete_Range_Cond; | |
6363 | ||
6364 | ------------------------- | |
6365 | -- Get_E_First_Or_Last -- | |
6366 | ------------------------- | |
6367 | ||
6368 | function Get_E_First_Or_Last | |
6369 | (E : Entity_Id; | |
6370 | Indx : Nat; | |
314a23b6 | 6371 | Nam : Name_Id) return Node_Id |
ee6ba406 | 6372 | is |
6373 | N : Node_Id; | |
6374 | LB : Node_Id; | |
6375 | HB : Node_Id; | |
6376 | Bound : Node_Id; | |
6377 | ||
6378 | begin | |
6379 | if Is_Array_Type (E) then | |
6380 | N := First_Index (E); | |
6381 | ||
6382 | for J in 2 .. Indx loop | |
6383 | Next_Index (N); | |
6384 | end loop; | |
6385 | ||
6386 | else | |
6387 | N := Scalar_Range (E); | |
6388 | end if; | |
6389 | ||
6390 | if Nkind (N) = N_Subtype_Indication then | |
6391 | LB := Low_Bound (Range_Expression (Constraint (N))); | |
6392 | HB := High_Bound (Range_Expression (Constraint (N))); | |
6393 | ||
6394 | elsif Is_Entity_Name (N) then | |
6395 | LB := Type_Low_Bound (Etype (N)); | |
6396 | HB := Type_High_Bound (Etype (N)); | |
6397 | ||
6398 | else | |
6399 | LB := Low_Bound (N); | |
6400 | HB := High_Bound (N); | |
6401 | end if; | |
6402 | ||
6403 | if Nam = Name_First then | |
6404 | Bound := LB; | |
6405 | else | |
6406 | Bound := HB; | |
6407 | end if; | |
6408 | ||
6409 | if Nkind (Bound) = N_Identifier | |
6410 | and then Ekind (Entity (Bound)) = E_Discriminant | |
6411 | then | |
9dfe12ae | 6412 | -- If this is a task discriminant, and we are the body, we must |
6413 | -- retrieve the corresponding body discriminal. This is another | |
6414 | -- consequence of the early creation of discriminals, and the | |
6415 | -- need to generate constraint checks before their declarations | |
6416 | -- are made visible. | |
6417 | ||
6418 | if Is_Concurrent_Record_Type (Scope (Entity (Bound))) then | |
6419 | declare | |
6420 | Tsk : constant Entity_Id := | |
6421 | Corresponding_Concurrent_Type | |
6422 | (Scope (Entity (Bound))); | |
6423 | Disc : Entity_Id; | |
6424 | ||
6425 | begin | |
6426 | if In_Open_Scopes (Tsk) | |
6427 | and then Has_Completion (Tsk) | |
6428 | then | |
6429 | -- Find discriminant of original task, and use its | |
6430 | -- current discriminal, which is the renaming within | |
6431 | -- the task body. | |
6432 | ||
6433 | Disc := First_Discriminant (Tsk); | |
6434 | while Present (Disc) loop | |
6435 | if Chars (Disc) = Chars (Entity (Bound)) then | |
6436 | Set_Scope (Discriminal (Disc), Tsk); | |
6437 | return New_Occurrence_Of (Discriminal (Disc), Loc); | |
6438 | end if; | |
6439 | ||
6440 | Next_Discriminant (Disc); | |
6441 | end loop; | |
6442 | ||
6443 | -- That loop should always succeed in finding a matching | |
6444 | -- entry and returning. Fatal error if not. | |
6445 | ||
6446 | raise Program_Error; | |
6447 | ||
6448 | else | |
6449 | return | |
6450 | New_Occurrence_Of (Discriminal (Entity (Bound)), Loc); | |
6451 | end if; | |
6452 | end; | |
6453 | else | |
6454 | return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc); | |
6455 | end if; | |
ee6ba406 | 6456 | |
6457 | elsif Nkind (Bound) = N_Identifier | |
6458 | and then Ekind (Entity (Bound)) = E_In_Parameter | |
6459 | and then not Inside_Init_Proc | |
6460 | then | |
6461 | return Get_Discriminal (E, Bound); | |
6462 | ||
6463 | elsif Nkind (Bound) = N_Integer_Literal then | |
18563cef | 6464 | return Make_Integer_Literal (Loc, Intval (Bound)); |
6465 | ||
feff2f05 | 6466 | -- Case of a bound rewritten to an N_Raise_Constraint_Error node |
6467 | -- because it is an out-of-range value. Duplicate_Subexpr cannot be | |
6468 | -- called on this node because an N_Raise_Constraint_Error is not | |
6469 | -- side effect free, and we may not assume that we are in the proper | |
6470 | -- context to remove side effects on it at the point of reference. | |
18563cef | 6471 | |
6472 | elsif Nkind (Bound) = N_Raise_Constraint_Error then | |
6473 | return New_Copy_Tree (Bound); | |
ee6ba406 | 6474 | |
6475 | else | |
9dfe12ae | 6476 | return Duplicate_Subexpr_No_Checks (Bound); |
ee6ba406 | 6477 | end if; |
6478 | end Get_E_First_Or_Last; | |
6479 | ||
6480 | ----------------- | |
6481 | -- Get_N_First -- | |
6482 | ----------------- | |
6483 | ||
6484 | function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is | |
6485 | begin | |
6486 | return | |
6487 | Make_Attribute_Reference (Loc, | |
6488 | Attribute_Name => Name_First, | |
6489 | Prefix => | |
9dfe12ae | 6490 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 6491 | Expressions => New_List ( |
6492 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 6493 | end Get_N_First; |
6494 | ||
6495 | ---------------- | |
6496 | -- Get_N_Last -- | |
6497 | ---------------- | |
6498 | ||
6499 | function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is | |
6500 | begin | |
6501 | return | |
6502 | Make_Attribute_Reference (Loc, | |
6503 | Attribute_Name => Name_Last, | |
6504 | Prefix => | |
9dfe12ae | 6505 | Duplicate_Subexpr_No_Checks (N, Name_Req => True), |
ee6ba406 | 6506 | Expressions => New_List ( |
6507 | Make_Integer_Literal (Loc, Indx))); | |
ee6ba406 | 6508 | end Get_N_Last; |
6509 | ||
6510 | ------------------ | |
6511 | -- Range_E_Cond -- | |
6512 | ------------------ | |
6513 | ||
6514 | function Range_E_Cond | |
6515 | (Exptyp : Entity_Id; | |
6516 | Typ : Entity_Id; | |
314a23b6 | 6517 | Indx : Nat) return Node_Id |
ee6ba406 | 6518 | is |
6519 | begin | |
6520 | return | |
6521 | Make_Or_Else (Loc, | |
6522 | Left_Opnd => | |
6523 | Make_Op_Lt (Loc, | |
6524 | Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First), | |
6525 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)), | |
6526 | ||
6527 | Right_Opnd => | |
6528 | Make_Op_Gt (Loc, | |
6529 | Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last), | |
6530 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last))); | |
ee6ba406 | 6531 | end Range_E_Cond; |
6532 | ||
6533 | ------------------------ | |
6534 | -- Range_Equal_E_Cond -- | |
6535 | ------------------------ | |
6536 | ||
6537 | function Range_Equal_E_Cond | |
6538 | (Exptyp : Entity_Id; | |
6539 | Typ : Entity_Id; | |
314a23b6 | 6540 | Indx : Nat) return Node_Id |
ee6ba406 | 6541 | is |
6542 | begin | |
6543 | return | |
6544 | Make_Or_Else (Loc, | |
6545 | Left_Opnd => | |
6546 | Make_Op_Ne (Loc, | |
6547 | Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First), | |
6548 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)), | |
6549 | Right_Opnd => | |
6550 | Make_Op_Ne (Loc, | |
6551 | Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last), | |
6552 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last))); | |
6553 | end Range_Equal_E_Cond; | |
6554 | ||
6555 | ------------------ | |
6556 | -- Range_N_Cond -- | |
6557 | ------------------ | |
6558 | ||
6559 | function Range_N_Cond | |
6560 | (Expr : Node_Id; | |
6561 | Typ : Entity_Id; | |
314a23b6 | 6562 | Indx : Nat) return Node_Id |
ee6ba406 | 6563 | is |
6564 | begin | |
6565 | return | |
6566 | Make_Or_Else (Loc, | |
6567 | Left_Opnd => | |
6568 | Make_Op_Lt (Loc, | |
6569 | Left_Opnd => Get_N_First (Expr, Indx), | |
6570 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)), | |
6571 | ||
6572 | Right_Opnd => | |
6573 | Make_Op_Gt (Loc, | |
6574 | Left_Opnd => Get_N_Last (Expr, Indx), | |
6575 | Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last))); | |
6576 | end Range_N_Cond; | |
6577 | ||
6578 | -- Start of processing for Selected_Range_Checks | |
6579 | ||
6580 | begin | |
6581 | if not Expander_Active then | |
6582 | return Ret_Result; | |
6583 | end if; | |
6584 | ||
6585 | if Target_Typ = Any_Type | |
6586 | or else Target_Typ = Any_Composite | |
6587 | or else Raises_Constraint_Error (Ck_Node) | |
6588 | then | |
6589 | return Ret_Result; | |
6590 | end if; | |
6591 | ||
6592 | if No (Wnode) then | |
6593 | Wnode := Ck_Node; | |
6594 | end if; | |
6595 | ||
6596 | T_Typ := Target_Typ; | |
6597 | ||
6598 | if No (Source_Typ) then | |
6599 | S_Typ := Etype (Ck_Node); | |
6600 | else | |
6601 | S_Typ := Source_Typ; | |
6602 | end if; | |
6603 | ||
6604 | if S_Typ = Any_Type or else S_Typ = Any_Composite then | |
6605 | return Ret_Result; | |
6606 | end if; | |
6607 | ||
6608 | -- The order of evaluating T_Typ before S_Typ seems to be critical | |
6609 | -- because S_Typ can be derived from Etype (Ck_Node), if it's not passed | |
6610 | -- in, and since Node can be an N_Range node, it might be invalid. | |
6611 | -- Should there be an assert check somewhere for taking the Etype of | |
6612 | -- an N_Range node ??? | |
6613 | ||
6614 | if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then | |
6615 | S_Typ := Designated_Type (S_Typ); | |
6616 | T_Typ := Designated_Type (T_Typ); | |
6617 | Do_Access := True; | |
6618 | ||
2af58f67 | 6619 | -- A simple optimization for the null case |
ee6ba406 | 6620 | |
2af58f67 | 6621 | if Known_Null (Ck_Node) then |
ee6ba406 | 6622 | return Ret_Result; |
6623 | end if; | |
6624 | end if; | |
6625 | ||
6626 | -- For an N_Range Node, check for a null range and then if not | |
6627 | -- null generate a range check action. | |
6628 | ||
6629 | if Nkind (Ck_Node) = N_Range then | |
6630 | ||
6631 | -- There's no point in checking a range against itself | |
6632 | ||
6633 | if Ck_Node = Scalar_Range (T_Typ) then | |
6634 | return Ret_Result; | |
6635 | end if; | |
6636 | ||
6637 | declare | |
6638 | T_LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
6639 | T_HB : constant Node_Id := Type_High_Bound (T_Typ); | |
6640 | LB : constant Node_Id := Low_Bound (Ck_Node); | |
6641 | HB : constant Node_Id := High_Bound (Ck_Node); | |
6642 | Null_Range : Boolean; | |
6643 | ||
6644 | Out_Of_Range_L : Boolean; | |
6645 | Out_Of_Range_H : Boolean; | |
6646 | ||
6647 | begin | |
6648 | -- Check for case where everything is static and we can | |
6649 | -- do the check at compile time. This is skipped if we | |
6650 | -- have an access type, since the access value may be null. | |
6651 | ||
6652 | -- ??? This code can be improved since you only need to know | |
6653 | -- that the two respective bounds (LB & T_LB or HB & T_HB) | |
6654 | -- are known at compile time to emit pertinent messages. | |
6655 | ||
6656 | if Compile_Time_Known_Value (LB) | |
6657 | and then Compile_Time_Known_Value (HB) | |
6658 | and then Compile_Time_Known_Value (T_LB) | |
6659 | and then Compile_Time_Known_Value (T_HB) | |
6660 | and then not Do_Access | |
6661 | then | |
6662 | -- Floating-point case | |
6663 | ||
6664 | if Is_Floating_Point_Type (S_Typ) then | |
6665 | Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB); | |
6666 | Out_Of_Range_L := | |
6667 | (Expr_Value_R (LB) < Expr_Value_R (T_LB)) | |
6668 | or else | |
6669 | (Expr_Value_R (LB) > Expr_Value_R (T_HB)); | |
6670 | ||
6671 | Out_Of_Range_H := | |
6672 | (Expr_Value_R (HB) > Expr_Value_R (T_HB)) | |
6673 | or else | |
6674 | (Expr_Value_R (HB) < Expr_Value_R (T_LB)); | |
6675 | ||
6676 | -- Fixed or discrete type case | |
6677 | ||
6678 | else | |
6679 | Null_Range := Expr_Value (HB) < Expr_Value (LB); | |
6680 | Out_Of_Range_L := | |
6681 | (Expr_Value (LB) < Expr_Value (T_LB)) | |
6682 | or else | |
6683 | (Expr_Value (LB) > Expr_Value (T_HB)); | |
6684 | ||
6685 | Out_Of_Range_H := | |
6686 | (Expr_Value (HB) > Expr_Value (T_HB)) | |
6687 | or else | |
6688 | (Expr_Value (HB) < Expr_Value (T_LB)); | |
6689 | end if; | |
6690 | ||
6691 | if not Null_Range then | |
6692 | if Out_Of_Range_L then | |
6693 | if No (Warn_Node) then | |
6694 | Add_Check | |
6695 | (Compile_Time_Constraint_Error | |
6696 | (Low_Bound (Ck_Node), | |
6697 | "static value out of range of}?", T_Typ)); | |
6698 | ||
6699 | else | |
6700 | Add_Check | |
6701 | (Compile_Time_Constraint_Error | |
6702 | (Wnode, | |
6703 | "static range out of bounds of}?", T_Typ)); | |
6704 | end if; | |
6705 | end if; | |
6706 | ||
6707 | if Out_Of_Range_H then | |
6708 | if No (Warn_Node) then | |
6709 | Add_Check | |
6710 | (Compile_Time_Constraint_Error | |
6711 | (High_Bound (Ck_Node), | |
6712 | "static value out of range of}?", T_Typ)); | |
6713 | ||
6714 | else | |
6715 | Add_Check | |
6716 | (Compile_Time_Constraint_Error | |
6717 | (Wnode, | |
6718 | "static range out of bounds of}?", T_Typ)); | |
6719 | end if; | |
6720 | end if; | |
6721 | ||
6722 | end if; | |
6723 | ||
6724 | else | |
6725 | declare | |
6726 | LB : Node_Id := Low_Bound (Ck_Node); | |
6727 | HB : Node_Id := High_Bound (Ck_Node); | |
6728 | ||
6729 | begin | |
feff2f05 | 6730 | -- If either bound is a discriminant and we are within the |
6731 | -- record declaration, it is a use of the discriminant in a | |
6732 | -- constraint of a component, and nothing can be checked | |
6733 | -- here. The check will be emitted within the init proc. | |
6734 | -- Before then, the discriminal has no real meaning. | |
6735 | -- Similarly, if the entity is a discriminal, there is no | |
6736 | -- check to perform yet. | |
6737 | ||
6738 | -- The same holds within a discriminated synchronized type, | |
6739 | -- where the discriminant may constrain a component or an | |
6740 | -- entry family. | |
ee6ba406 | 6741 | |
6742 | if Nkind (LB) = N_Identifier | |
0577b0b1 | 6743 | and then Denotes_Discriminant (LB, True) |
ee6ba406 | 6744 | then |
0577b0b1 | 6745 | if Current_Scope = Scope (Entity (LB)) |
6746 | or else Is_Concurrent_Type (Current_Scope) | |
6747 | or else Ekind (Entity (LB)) /= E_Discriminant | |
6748 | then | |
ee6ba406 | 6749 | return Ret_Result; |
6750 | else | |
6751 | LB := | |
6752 | New_Occurrence_Of (Discriminal (Entity (LB)), Loc); | |
6753 | end if; | |
6754 | end if; | |
6755 | ||
6756 | if Nkind (HB) = N_Identifier | |
0577b0b1 | 6757 | and then Denotes_Discriminant (HB, True) |
ee6ba406 | 6758 | then |
0577b0b1 | 6759 | if Current_Scope = Scope (Entity (HB)) |
6760 | or else Is_Concurrent_Type (Current_Scope) | |
6761 | or else Ekind (Entity (HB)) /= E_Discriminant | |
6762 | then | |
ee6ba406 | 6763 | return Ret_Result; |
6764 | else | |
6765 | HB := | |
6766 | New_Occurrence_Of (Discriminal (Entity (HB)), Loc); | |
6767 | end if; | |
6768 | end if; | |
6769 | ||
6770 | Cond := Discrete_Range_Cond (Ck_Node, T_Typ); | |
6771 | Set_Paren_Count (Cond, 1); | |
6772 | ||
6773 | Cond := | |
6774 | Make_And_Then (Loc, | |
6775 | Left_Opnd => | |
6776 | Make_Op_Ge (Loc, | |
9dfe12ae | 6777 | Left_Opnd => Duplicate_Subexpr_No_Checks (HB), |
6778 | Right_Opnd => Duplicate_Subexpr_No_Checks (LB)), | |
ee6ba406 | 6779 | Right_Opnd => Cond); |
6780 | end; | |
ee6ba406 | 6781 | end if; |
6782 | end; | |
6783 | ||
6784 | elsif Is_Scalar_Type (S_Typ) then | |
6785 | ||
6786 | -- This somewhat duplicates what Apply_Scalar_Range_Check does, | |
6787 | -- except the above simply sets a flag in the node and lets | |
6788 | -- gigi generate the check base on the Etype of the expression. | |
6789 | -- Sometimes, however we want to do a dynamic check against an | |
6790 | -- arbitrary target type, so we do that here. | |
6791 | ||
6792 | if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then | |
6793 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
6794 | ||
6795 | -- For literals, we can tell if the constraint error will be | |
6796 | -- raised at compile time, so we never need a dynamic check, but | |
6797 | -- if the exception will be raised, then post the usual warning, | |
6798 | -- and replace the literal with a raise constraint error | |
6799 | -- expression. As usual, skip this for access types | |
6800 | ||
6801 | elsif Compile_Time_Known_Value (Ck_Node) | |
6802 | and then not Do_Access | |
6803 | then | |
6804 | declare | |
6805 | LB : constant Node_Id := Type_Low_Bound (T_Typ); | |
6806 | UB : constant Node_Id := Type_High_Bound (T_Typ); | |
6807 | ||
6808 | Out_Of_Range : Boolean; | |
6809 | Static_Bounds : constant Boolean := | |
6810 | Compile_Time_Known_Value (LB) | |
6811 | and Compile_Time_Known_Value (UB); | |
6812 | ||
6813 | begin | |
6814 | -- Following range tests should use Sem_Eval routine ??? | |
6815 | ||
6816 | if Static_Bounds then | |
6817 | if Is_Floating_Point_Type (S_Typ) then | |
6818 | Out_Of_Range := | |
6819 | (Expr_Value_R (Ck_Node) < Expr_Value_R (LB)) | |
6820 | or else | |
6821 | (Expr_Value_R (Ck_Node) > Expr_Value_R (UB)); | |
6822 | ||
6823 | else -- fixed or discrete type | |
6824 | Out_Of_Range := | |
6825 | Expr_Value (Ck_Node) < Expr_Value (LB) | |
6826 | or else | |
6827 | Expr_Value (Ck_Node) > Expr_Value (UB); | |
6828 | end if; | |
6829 | ||
6830 | -- Bounds of the type are static and the literal is | |
6831 | -- out of range so make a warning message. | |
6832 | ||
6833 | if Out_Of_Range then | |
6834 | if No (Warn_Node) then | |
6835 | Add_Check | |
6836 | (Compile_Time_Constraint_Error | |
6837 | (Ck_Node, | |
6838 | "static value out of range of}?", T_Typ)); | |
6839 | ||
6840 | else | |
6841 | Add_Check | |
6842 | (Compile_Time_Constraint_Error | |
6843 | (Wnode, | |
6844 | "static value out of range of}?", T_Typ)); | |
6845 | end if; | |
6846 | end if; | |
6847 | ||
6848 | else | |
6849 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); | |
6850 | end if; | |
6851 | end; | |
6852 | ||
6853 | -- Here for the case of a non-static expression, we need a runtime | |
6854 | -- check unless the source type range is guaranteed to be in the | |
6855 | -- range of the target type. | |
6856 | ||
6857 | else | |
7a1dabb3 | 6858 | if not In_Subrange_Of (S_Typ, T_Typ) then |
ee6ba406 | 6859 | Cond := Discrete_Expr_Cond (Ck_Node, T_Typ); |
6860 | end if; | |
6861 | end if; | |
6862 | end if; | |
6863 | ||
6864 | if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then | |
6865 | if Is_Constrained (T_Typ) then | |
6866 | ||
6867 | Expr_Actual := Get_Referenced_Object (Ck_Node); | |
6868 | Exptyp := Get_Actual_Subtype (Expr_Actual); | |
6869 | ||
6870 | if Is_Access_Type (Exptyp) then | |
6871 | Exptyp := Designated_Type (Exptyp); | |
6872 | end if; | |
6873 | ||
6874 | -- String_Literal case. This needs to be handled specially be- | |
6875 | -- cause no index types are available for string literals. The | |
6876 | -- condition is simply: | |
6877 | ||
6878 | -- T_Typ'Length = string-literal-length | |
6879 | ||
6880 | if Nkind (Expr_Actual) = N_String_Literal then | |
6881 | null; | |
6882 | ||
6883 | -- General array case. Here we have a usable actual subtype for | |
6884 | -- the expression, and the condition is built from the two types | |
6885 | ||
6886 | -- T_Typ'First < Exptyp'First or else | |
6887 | -- T_Typ'Last > Exptyp'Last or else | |
6888 | -- T_Typ'First(1) < Exptyp'First(1) or else | |
6889 | -- T_Typ'Last(1) > Exptyp'Last(1) or else | |
6890 | -- ... | |
6891 | ||
6892 | elsif Is_Constrained (Exptyp) then | |
6893 | declare | |
9dfe12ae | 6894 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
6895 | ||
ee6ba406 | 6896 | L_Index : Node_Id; |
6897 | R_Index : Node_Id; | |
ee6ba406 | 6898 | |
6899 | begin | |
6900 | L_Index := First_Index (T_Typ); | |
6901 | R_Index := First_Index (Exptyp); | |
6902 | ||
6903 | for Indx in 1 .. Ndims loop | |
6904 | if not (Nkind (L_Index) = N_Raise_Constraint_Error | |
f15731c4 | 6905 | or else |
6906 | Nkind (R_Index) = N_Raise_Constraint_Error) | |
ee6ba406 | 6907 | then |
ee6ba406 | 6908 | -- Deal with compile time length check. Note that we |
6909 | -- skip this in the access case, because the access | |
6910 | -- value may be null, so we cannot know statically. | |
6911 | ||
6912 | if not | |
6913 | Subtypes_Statically_Match | |
6914 | (Etype (L_Index), Etype (R_Index)) | |
6915 | then | |
6916 | -- If the target type is constrained then we | |
6917 | -- have to check for exact equality of bounds | |
6918 | -- (required for qualified expressions). | |
6919 | ||
6920 | if Is_Constrained (T_Typ) then | |
6921 | Evolve_Or_Else | |
6922 | (Cond, | |
6923 | Range_Equal_E_Cond (Exptyp, T_Typ, Indx)); | |
ee6ba406 | 6924 | else |
6925 | Evolve_Or_Else | |
6926 | (Cond, Range_E_Cond (Exptyp, T_Typ, Indx)); | |
6927 | end if; | |
6928 | end if; | |
6929 | ||
6930 | Next (L_Index); | |
6931 | Next (R_Index); | |
6932 | ||
6933 | end if; | |
6934 | end loop; | |
6935 | end; | |
6936 | ||
6937 | -- Handle cases where we do not get a usable actual subtype that | |
6938 | -- is constrained. This happens for example in the function call | |
6939 | -- and explicit dereference cases. In these cases, we have to get | |
6940 | -- the length or range from the expression itself, making sure we | |
6941 | -- do not evaluate it more than once. | |
6942 | ||
6943 | -- Here Ck_Node is the original expression, or more properly the | |
6944 | -- result of applying Duplicate_Expr to the original tree, | |
6945 | -- forcing the result to be a name. | |
6946 | ||
6947 | else | |
6948 | declare | |
9dfe12ae | 6949 | Ndims : constant Nat := Number_Dimensions (T_Typ); |
ee6ba406 | 6950 | |
6951 | begin | |
6952 | -- Build the condition for the explicit dereference case | |
6953 | ||
6954 | for Indx in 1 .. Ndims loop | |
6955 | Evolve_Or_Else | |
6956 | (Cond, Range_N_Cond (Ck_Node, T_Typ, Indx)); | |
6957 | end loop; | |
6958 | end; | |
6959 | ||
6960 | end if; | |
6961 | ||
6962 | else | |
feff2f05 | 6963 | -- For a conversion to an unconstrained array type, generate an |
6964 | -- Action to check that the bounds of the source value are within | |
6965 | -- the constraints imposed by the target type (RM 4.6(38)). No | |
6966 | -- check is needed for a conversion to an access to unconstrained | |
6967 | -- array type, as 4.6(24.15/2) requires the designated subtypes | |
6968 | -- of the two access types to statically match. | |
6969 | ||
6970 | if Nkind (Parent (Ck_Node)) = N_Type_Conversion | |
6971 | and then not Do_Access | |
6972 | then | |
ee6ba406 | 6973 | declare |
6974 | Opnd_Index : Node_Id; | |
6975 | Targ_Index : Node_Id; | |
00c403ee | 6976 | Opnd_Range : Node_Id; |
ee6ba406 | 6977 | |
6978 | begin | |
feff2f05 | 6979 | Opnd_Index := First_Index (Get_Actual_Subtype (Ck_Node)); |
ee6ba406 | 6980 | Targ_Index := First_Index (T_Typ); |
00c403ee | 6981 | while Present (Opnd_Index) loop |
6982 | ||
6983 | -- If the index is a range, use its bounds. If it is an | |
6984 | -- entity (as will be the case if it is a named subtype | |
6985 | -- or an itype created for a slice) retrieve its range. | |
6986 | ||
6987 | if Is_Entity_Name (Opnd_Index) | |
6988 | and then Is_Type (Entity (Opnd_Index)) | |
6989 | then | |
6990 | Opnd_Range := Scalar_Range (Entity (Opnd_Index)); | |
6991 | else | |
6992 | Opnd_Range := Opnd_Index; | |
6993 | end if; | |
6994 | ||
6995 | if Nkind (Opnd_Range) = N_Range then | |
9c486805 | 6996 | if Is_In_Range |
6997 | (Low_Bound (Opnd_Range), Etype (Targ_Index), | |
6998 | Assume_Valid => True) | |
ee6ba406 | 6999 | and then |
7000 | Is_In_Range | |
9c486805 | 7001 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
7002 | Assume_Valid => True) | |
ee6ba406 | 7003 | then |
7004 | null; | |
7005 | ||
feff2f05 | 7006 | -- If null range, no check needed |
f2a06be9 | 7007 | |
9dfe12ae | 7008 | elsif |
00c403ee | 7009 | Compile_Time_Known_Value (High_Bound (Opnd_Range)) |
9dfe12ae | 7010 | and then |
00c403ee | 7011 | Compile_Time_Known_Value (Low_Bound (Opnd_Range)) |
9dfe12ae | 7012 | and then |
00c403ee | 7013 | Expr_Value (High_Bound (Opnd_Range)) < |
7014 | Expr_Value (Low_Bound (Opnd_Range)) | |
9dfe12ae | 7015 | then |
7016 | null; | |
7017 | ||
ee6ba406 | 7018 | elsif Is_Out_Of_Range |
9c486805 | 7019 | (Low_Bound (Opnd_Range), Etype (Targ_Index), |
7020 | Assume_Valid => True) | |
ee6ba406 | 7021 | or else |
7022 | Is_Out_Of_Range | |
9c486805 | 7023 | (High_Bound (Opnd_Range), Etype (Targ_Index), |
7024 | Assume_Valid => True) | |
ee6ba406 | 7025 | then |
7026 | Add_Check | |
7027 | (Compile_Time_Constraint_Error | |
7028 | (Wnode, "value out of range of}?", T_Typ)); | |
7029 | ||
7030 | else | |
7031 | Evolve_Or_Else | |
7032 | (Cond, | |
7033 | Discrete_Range_Cond | |
00c403ee | 7034 | (Opnd_Range, Etype (Targ_Index))); |
ee6ba406 | 7035 | end if; |
7036 | end if; | |
7037 | ||
7038 | Next_Index (Opnd_Index); | |
7039 | Next_Index (Targ_Index); | |
7040 | end loop; | |
7041 | end; | |
7042 | end if; | |
7043 | end if; | |
7044 | end if; | |
7045 | ||
7046 | -- Construct the test and insert into the tree | |
7047 | ||
7048 | if Present (Cond) then | |
7049 | if Do_Access then | |
7050 | Cond := Guard_Access (Cond, Loc, Ck_Node); | |
7051 | end if; | |
7052 | ||
f15731c4 | 7053 | Add_Check |
7054 | (Make_Raise_Constraint_Error (Loc, | |
7055 | Condition => Cond, | |
7056 | Reason => CE_Range_Check_Failed)); | |
ee6ba406 | 7057 | end if; |
7058 | ||
7059 | return Ret_Result; | |
ee6ba406 | 7060 | end Selected_Range_Checks; |
7061 | ||
7062 | ------------------------------- | |
7063 | -- Storage_Checks_Suppressed -- | |
7064 | ------------------------------- | |
7065 | ||
7066 | function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7067 | begin | |
9dfe12ae | 7068 | if Present (E) and then Checks_May_Be_Suppressed (E) then |
7069 | return Is_Check_Suppressed (E, Storage_Check); | |
7070 | else | |
7071 | return Scope_Suppress (Storage_Check); | |
7072 | end if; | |
ee6ba406 | 7073 | end Storage_Checks_Suppressed; |
7074 | ||
7075 | --------------------------- | |
7076 | -- Tag_Checks_Suppressed -- | |
7077 | --------------------------- | |
7078 | ||
7079 | function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7080 | begin | |
9dfe12ae | 7081 | if Present (E) then |
7082 | if Kill_Tag_Checks (E) then | |
7083 | return True; | |
7084 | elsif Checks_May_Be_Suppressed (E) then | |
7085 | return Is_Check_Suppressed (E, Tag_Check); | |
7086 | end if; | |
7087 | end if; | |
7088 | ||
7089 | return Scope_Suppress (Tag_Check); | |
ee6ba406 | 7090 | end Tag_Checks_Suppressed; |
7091 | ||
0577b0b1 | 7092 | -------------------------- |
7093 | -- Validity_Check_Range -- | |
7094 | -------------------------- | |
7095 | ||
7096 | procedure Validity_Check_Range (N : Node_Id) is | |
7097 | begin | |
7098 | if Validity_Checks_On and Validity_Check_Operands then | |
7099 | if Nkind (N) = N_Range then | |
7100 | Ensure_Valid (Low_Bound (N)); | |
7101 | Ensure_Valid (High_Bound (N)); | |
7102 | end if; | |
7103 | end if; | |
7104 | end Validity_Check_Range; | |
7105 | ||
7106 | -------------------------------- | |
7107 | -- Validity_Checks_Suppressed -- | |
7108 | -------------------------------- | |
7109 | ||
7110 | function Validity_Checks_Suppressed (E : Entity_Id) return Boolean is | |
7111 | begin | |
7112 | if Present (E) and then Checks_May_Be_Suppressed (E) then | |
7113 | return Is_Check_Suppressed (E, Validity_Check); | |
7114 | else | |
7115 | return Scope_Suppress (Validity_Check); | |
7116 | end if; | |
7117 | end Validity_Checks_Suppressed; | |
7118 | ||
ee6ba406 | 7119 | end Checks; |