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