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