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