Debian Codegraph @

gcc/ada/exp_ch4.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- E X P _ C H 4 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2025, Free Software Foundation, Inc. --
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- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
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 --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
25
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Einfo.Entities; use Einfo.Entities;
33 with Einfo.Utils; use Einfo.Utils;
34 with Elists; use Elists;
35 with Errout; use Errout;
36 with Exp_Aggr; use Exp_Aggr;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch6; use Exp_Ch6;
39 with Exp_Ch7; use Exp_Ch7;
40 with Exp_Ch9; use Exp_Ch9;
41 with Exp_Disp; use Exp_Disp;
42 with Exp_Fixd; use Exp_Fixd;
43 with Exp_Intr; use Exp_Intr;
44 with Exp_Pakd; use Exp_Pakd;
45 with Exp_Tss; use Exp_Tss;
46 with Exp_Util; use Exp_Util;
47 with Freeze; use Freeze;
48 with Inline; use Inline;
49 with Lib; use Lib;
50 with Mutably_Tagged; use Mutably_Tagged;
51 with Namet; use Namet;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Par_SCO; use Par_SCO;
56 with Restrict; use Restrict;
57 with Rident; use Rident;
58 with Rtsfind; use Rtsfind;
59 with Sem; use Sem;
60 with Sem_Aux; use Sem_Aux;
61 with Sem_Cat; use Sem_Cat;
62 with Sem_Ch3; use Sem_Ch3;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Eval; use Sem_Eval;
65 with Sem_Res; use Sem_Res;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Sinfo; use Sinfo;
70 with Sinfo.Nodes; use Sinfo.Nodes;
71 with Sinfo.Utils; use Sinfo.Utils;
72 with Snames; use Snames;
73 with Stand; use Stand;
74 with SCIL_LL; use SCIL_LL;
75 with Targparm; use Targparm;
76 with Tbuild; use Tbuild;
77 with Ttypes; use Ttypes;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Validsw; use Validsw;
81 with Warnsw; use Warnsw;
82
83 package body Exp_Ch4 is
84
85 Too_Large_Length_For_Array : constant Unat := Uint_256;
86 -- Threshold from which we do not try to create static array temporaries in
87 -- order to eliminate dynamic stack allocations.
88
89 -----------------------
90 -- Local Subprograms --
91 -----------------------
92
93 procedure Binary_Op_Validity_Checks (N : Node_Id);
94 pragma Inline (Binary_Op_Validity_Checks);
95 -- Performs validity checks for a binary operator
96
97 procedure Build_Boolean_Array_Proc_Call
98 (N : Node_Id;
99 Op1 : Node_Id;
100 Op2 : Node_Id);
101 -- If a boolean array assignment can be done in place, build call to
102 -- corresponding library procedure.
103
104 procedure Displace_Allocator_Pointer (N : Node_Id);
105 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
106 -- Expand_Allocator_Expression. Allocating class-wide interface objects
107 -- this routine displaces the pointer to the allocated object to reference
108 -- the component referencing the corresponding secondary dispatch table.
109
110 procedure Expand_Allocator_Expression (N : Node_Id);
111 -- Subsidiary to Expand_N_Allocator, for the case when the expression
112 -- is a qualified expression.
113
114 procedure Expand_Array_Comparison (N : Node_Id);
115 -- This routine handles expansion of the comparison operators (N_Op_Lt,
116 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
117 -- code for these operators is similar, differing only in the details of
118 -- the actual comparison call that is made. Special processing (call a
119 -- run-time routine)
120
121 function Expand_Array_Equality
122 (Nod : Node_Id;
123 Lhs : Node_Id;
124 Rhs : Node_Id;
125 Bodies : List_Id;
126 Typ : Entity_Id) return Node_Id;
127 -- Expand an array equality into a call to a function implementing this
128 -- equality, and a call to it. Loc is the location for the generated nodes.
129 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
130 -- on which to attach bodies of local functions that are created in the
131 -- process. It is the responsibility of the caller to insert those bodies
132 -- at the right place. Nod provides the Sloc value for the generated code.
133 -- Normally the types used for the generated equality routine are taken
134 -- from Lhs and Rhs. However, in some situations of generated code, the
135 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
136 -- the type to be used for the formal parameters.
137
138 procedure Expand_Boolean_Operator (N : Node_Id);
139 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
140 -- case of array type arguments.
141
142 procedure Expand_Nonbinary_Modular_Op (N : Node_Id);
143 -- When generating C code or if restriction No_Implicit_Conditionals is in
144 -- effect, convert most nonbinary modular arithmetic operations into code
145 -- that relies on the expansion of an explicit Mod operator. No expansion
146 -- is performed if N is not a nonbinary modular operation.
147
148 procedure Expand_Short_Circuit_Operator (N : Node_Id);
149 -- Common expansion processing for short-circuit boolean operators
150
151 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
152 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
153 -- where we allow comparison of "out of range" values.
154
155 function Expand_Composite_Equality
156 (Outer_Type : Entity_Id;
157 Nod : Node_Id;
158 Comp_Type : Entity_Id;
159 Lhs : Node_Id;
160 Rhs : Node_Id) return Node_Id;
161 -- Local recursive function used to expand equality for nested composite
162 -- types. Used by Expand_Record/Array_Equality. Nod provides the Sloc value
163 -- for generated code. Lhs and Rhs are the left and right sides for the
164 -- comparison, and Comp_Typ is the type of the objects to compare.
165 -- Outer_Type is the composite type containing a component of type
166 -- Comp_Type -- used for printing messages.
167
168 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
169 -- Routine to expand concatenation of a sequence of two or more operands
170 -- (in the list Operands) and replace node Cnode with the result of the
171 -- concatenation. The operands can be of any appropriate type, and can
172 -- include both arrays and singleton elements.
173
174 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
175 -- N is an N_In membership test mode, with the overflow check mode set to
176 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
177 -- integer type. This is a case where top level processing is required to
178 -- handle overflow checks in subtrees.
179
180 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
181 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
182 -- fixed. We do not have such a type at runtime, so the purpose of this
183 -- routine is to find the real type by looking up the tree. We also
184 -- determine if the operation must be rounded.
185
186 procedure Get_First_Index_Bounds (T : Entity_Id; Lo, Hi : out Uint);
187 -- T is an array whose index bounds are all known at compile time. Return
188 -- the value of the low and high bounds of the first index of T.
189
190 function Get_Size_For_Range (Lo, Hi : Uint) return Uint;
191 -- Return the size of a small signed integer type covering Lo .. Hi, the
192 -- main goal being to return a size lower than that of standard types.
193
194 procedure Insert_Conditional_Object_Declaration
195 (Obj_Id : Entity_Id;
196 Typ : Entity_Id;
197 Expr : Node_Id;
198 Const : Boolean);
199 -- Expr is the dependent expression of a conditional expression. Insert in
200 -- the actions of Expr the declaration of Obj_Id with type Typ and Expr as
201 -- initialization expression. Const is True when Obj_Id is a constant.
202
203 procedure Insert_Dereference_Action (N : Node_Id);
204 -- N is an expression whose type is an access. When the type of the
205 -- associated storage pool is derived from Checked_Pool, generate a
206 -- call to the 'Dereference' primitive operation.
207
208 function Make_Array_Comparison_Op
209 (Typ : Entity_Id;
210 Nod : Node_Id) return Node_Id;
211 -- Comparisons between arrays are expanded in line. This function produces
212 -- the body of the implementation of (a > b), where a and b are one-
213 -- dimensional arrays of some discrete type. The original node is then
214 -- expanded into the appropriate call to this function. Nod provides the
215 -- Sloc value for the generated code.
216
217 function Make_Boolean_Array_Op
218 (Typ : Entity_Id;
219 N : Node_Id) return Node_Id;
220 -- Boolean operations on boolean arrays are expanded in line. This function
221 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
222 -- b). It is used only the normal case and not the packed case. The type
223 -- involved, Typ, is the Boolean array type, and the logical operations in
224 -- the body are simple boolean operations. Note that Typ is always a
225 -- constrained type (the caller has ensured this by using
226 -- Convert_To_Actual_Subtype if necessary).
227
228 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
229 -- For signed arithmetic operations when the current overflow mode is
230 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
231 -- as the first thing we do. We then return. We count on the recursive
232 -- apparatus for overflow checks to call us back with an equivalent
233 -- operation that is in CHECKED mode, avoiding a recursive entry into this
234 -- routine, and that is when we will proceed with the expansion of the
235 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
236 -- these optimizations without first making this check, since there may be
237 -- operands further down the tree that are relying on the recursive calls
238 -- triggered by the top level nodes to properly process overflow checking
239 -- and remaining expansion on these nodes. Note that this call back may be
240 -- skipped if the operation is done in Bignum mode but that's fine, since
241 -- the Bignum call takes care of everything.
242
243 procedure Narrow_Large_Operation (N : Node_Id);
244 -- Try to compute the result of a large operation in a narrower type than
245 -- its nominal type. This is mainly aimed at getting rid of operations done
246 -- in Universal_Integer that can be generated for attributes.
247
248 procedure Optimize_Length_Comparison (N : Node_Id);
249 -- Given an expression, if it is of the form X'Length op N (or the other
250 -- way round), where N is known at compile time to be 0 or 1, or something
251 -- else where the value is known to be nonnegative and in the 32-bit range,
252 -- and X is a simple entity, and op is a comparison operator, optimizes it
253 -- into a comparison of X'First and X'Last.
254
255 procedure Process_Transients_In_Expression
256 (Expr : Node_Id;
257 Stmts : List_Id);
258 -- Subsidiary routine to the expansion of expression_with_actions, if and
259 -- case expressions. Inspect and process actions list Stmts of expression
260 -- Expr for transient objects. If such objects are found, the routine will
261 -- generate code to finalize them when the enclosing context is elaborated
262 -- or evaluated.
263
264 -- This specific processing is required for these expressions because the
265 -- management of transient objects for expressions implemented in Exp_Ch7
266 -- cannot deal with nested lists of actions whose effects may outlive the
267 -- lists and affect the result of the parent expressions. In these cases,
268 -- the lifetime of temporaries created in these lists must be extended to
269 -- match that of the enclosing context of the parent expressions and, in
270 -- particular, their finalization must be deferred to this context.
271
272 procedure Rewrite_Comparison (N : Node_Id);
273 -- If N is the node for a comparison whose outcome can be determined at
274 -- compile time, then the node N can be rewritten with True or False. If
275 -- the outcome cannot be determined at compile time, the call has no
276 -- effect. If N is a type conversion, then this processing is applied to
277 -- its expression. If N is neither comparison nor a type conversion, the
278 -- call has no effect.
279
280 procedure Tagged_Membership
281 (N : Node_Id;
282 SCIL_Node : out Node_Id;
283 Result : out Node_Id);
284 -- Construct the expression corresponding to the tagged membership test.
285 -- Deals with a second operand being (or not) a class-wide type.
286
287 function Safe_In_Place_Array_Op
288 (Lhs : Node_Id;
289 Op1 : Node_Id;
290 Op2 : Node_Id) return Boolean;
291 -- In the context of an assignment, where the right-hand side is a boolean
292 -- operation on arrays, check whether operation can be performed in place.
293
294 procedure Unary_Op_Validity_Checks (N : Node_Id);
295 pragma Inline (Unary_Op_Validity_Checks);
296 -- Performs validity checks for a unary operator
297
298 -------------------------------
299 -- Binary_Op_Validity_Checks --
300 -------------------------------
301
302 procedure Binary_Op_Validity_Checks (N : Node_Id) is
303 begin
304 if Validity_Checks_On and Validity_Check_Operands then
305 Ensure_Valid (Left_Opnd (N));
306 Ensure_Valid (Right_Opnd (N));
307 end if;
308 end Binary_Op_Validity_Checks;
309
310 ------------------------------------
311 -- Build_Boolean_Array_Proc_Call --
312 ------------------------------------
313
314 procedure Build_Boolean_Array_Proc_Call
315 (N : Node_Id;
316 Op1 : Node_Id;
317 Op2 : Node_Id)
318 is
319 Loc : constant Source_Ptr := Sloc (N);
320 Kind : constant Node_Kind := Nkind (Expression (N));
321 Target : constant Node_Id :=
322 Make_Attribute_Reference (Loc,
323 Prefix => Name (N),
324 Attribute_Name => Name_Address);
325
326 Arg1 : Node_Id := Op1;
327 Arg2 : Node_Id := Op2;
328 Call_Node : Node_Id;
329 Proc_Name : Entity_Id;
330
331 begin
332 if Kind = N_Op_Not then
333 if Nkind (Op1) in N_Binary_Op then
334
335 -- Use negated version of the binary operators
336
337 if Nkind (Op1) = N_Op_And then
338 Proc_Name := RTE (RE_Vector_Nand);
339
340 elsif Nkind (Op1) = N_Op_Or then
341 Proc_Name := RTE (RE_Vector_Nor);
342
343 else pragma Assert (Nkind (Op1) = N_Op_Xor);
344 Proc_Name := RTE (RE_Vector_Xor);
345 end if;
346
347 Call_Node :=
348 Make_Procedure_Call_Statement (Loc,
349 Name => New_Occurrence_Of (Proc_Name, Loc),
350
351 Parameter_Associations => New_List (
352 Target,
353 Make_Attribute_Reference (Loc,
354 Prefix => Left_Opnd (Op1),
355 Attribute_Name => Name_Address),
356
357 Make_Attribute_Reference (Loc,
358 Prefix => Right_Opnd (Op1),
359 Attribute_Name => Name_Address),
360
361 Make_Attribute_Reference (Loc,
362 Prefix => Left_Opnd (Op1),
363 Attribute_Name => Name_Length)));
364
365 else
366 Proc_Name := RTE (RE_Vector_Not);
367
368 Call_Node :=
369 Make_Procedure_Call_Statement (Loc,
370 Name => New_Occurrence_Of (Proc_Name, Loc),
371 Parameter_Associations => New_List (
372 Target,
373
374 Make_Attribute_Reference (Loc,
375 Prefix => Op1,
376 Attribute_Name => Name_Address),
377
378 Make_Attribute_Reference (Loc,
379 Prefix => Op1,
380 Attribute_Name => Name_Length)));
381 end if;
382
383 else
384 -- We use the following equivalences:
385
386 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
387 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
388 -- (not X) xor (not Y) = X xor Y
389 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
390
391 if Nkind (Op1) = N_Op_Not then
392 Arg1 := Right_Opnd (Op1);
393 Arg2 := Right_Opnd (Op2);
394
395 if Kind = N_Op_And then
396 Proc_Name := RTE (RE_Vector_Nor);
397 elsif Kind = N_Op_Or then
398 Proc_Name := RTE (RE_Vector_Nand);
399 else
400 Proc_Name := RTE (RE_Vector_Xor);
401 end if;
402
403 else
404 if Kind = N_Op_And then
405 Proc_Name := RTE (RE_Vector_And);
406 elsif Kind = N_Op_Or then
407 Proc_Name := RTE (RE_Vector_Or);
408 elsif Nkind (Op2) = N_Op_Not then
409 Proc_Name := RTE (RE_Vector_Nxor);
410 Arg2 := Right_Opnd (Op2);
411 else
412 Proc_Name := RTE (RE_Vector_Xor);
413 end if;
414 end if;
415
416 Call_Node :=
417 Make_Procedure_Call_Statement (Loc,
418 Name => New_Occurrence_Of (Proc_Name, Loc),
419 Parameter_Associations => New_List (
420 Target,
421 Make_Attribute_Reference (Loc,
422 Prefix => Arg1,
423 Attribute_Name => Name_Address),
424 Make_Attribute_Reference (Loc,
425 Prefix => Arg2,
426 Attribute_Name => Name_Address),
427 Make_Attribute_Reference (Loc,
428 Prefix => Arg1,
429 Attribute_Name => Name_Length)));
430 end if;
431
432 Rewrite (N, Call_Node);
433 Analyze (N);
434
435 exception
436 when RE_Not_Available =>
437 return;
438 end Build_Boolean_Array_Proc_Call;
439
440 ---------------------------------
441 -- Build_Cleanup_For_Allocator --
442 ---------------------------------
443
444 function Build_Cleanup_For_Allocator
445 (Loc : Source_Ptr;
446 Obj_Id : Entity_Id;
447 Pool : Entity_Id;
448 Actions : List_Id) return Node_Id
449 is
450 Free_Stmt : constant Node_Id :=
451 Make_Free_Statement (Loc, New_Occurrence_Of (Obj_Id, Loc));
452
453 begin
454 Set_For_Allocator (Free_Stmt);
455 Set_Storage_Pool (Free_Stmt, Pool);
456
457 return
458 Make_Block_Statement (Loc,
459 Handled_Statement_Sequence =>
460 Make_Handled_Sequence_Of_Statements (Loc,
461 Statements => Actions,
462 Exception_Handlers => New_List (
463 Make_Exception_Handler (Loc,
464 Exception_Choices => New_List (
465 Make_Others_Choice (Loc)),
466 Statements => New_List (
467 Free_Stmt,
468 Make_Raise_Statement (Loc))))));
469 end Build_Cleanup_For_Allocator;
470
471 -----------------------
472 -- Build_Eq_Call --
473 -----------------------
474
475 function Build_Eq_Call
476 (Typ : Entity_Id;
477 Loc : Source_Ptr;
478 Lhs : Node_Id;
479 Rhs : Node_Id) return Node_Id
480 is
481 Eq : constant Entity_Id := Get_User_Defined_Equality (Typ);
482
483 begin
484 if Present (Eq) then
485 if Is_Abstract_Subprogram (Eq) then
486 return Result : constant Node_Id :=
487 Make_Raise_Program_Error (Loc, Reason => PE_Explicit_Raise)
488 do
489 Set_Etype (Result, Etype (Eq));
490 end return;
491
492 else
493 return
494 Make_Function_Call (Loc,
495 Name => New_Occurrence_Of (Eq, Loc),
496 Parameter_Associations => New_List (Lhs, Rhs));
497 end if;
498 end if;
499
500 -- If not found, predefined operation will be used
501
502 return Empty;
503 end Build_Eq_Call;
504
505 --------------------------------
506 -- Displace_Allocator_Pointer --
507 --------------------------------
508
509 procedure Displace_Allocator_Pointer (N : Node_Id) is
510 Loc : constant Source_Ptr := Sloc (N);
511 Orig_Node : constant Node_Id := Original_Node (N);
512 Dtyp : Entity_Id;
513 Etyp : Entity_Id;
514 PtrT : Entity_Id;
515
516 begin
517 -- Do nothing in case of VM targets: the virtual machine will handle
518 -- interfaces directly.
519
520 if not Tagged_Type_Expansion then
521 return;
522 end if;
523
524 pragma Assert (Nkind (N) = N_Identifier
525 and then Nkind (Orig_Node) = N_Allocator);
526
527 PtrT := Etype (Orig_Node);
528 Dtyp := Available_View (Designated_Type (PtrT));
529 Etyp := Etype (Expression (Orig_Node));
530
531 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
532
533 -- If the type of the allocator expression is not an interface type
534 -- we can generate code to reference the record component containing
535 -- the pointer to the secondary dispatch table.
536
537 if not Is_Interface (Etyp) then
538 declare
539 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
540
541 begin
542 -- 1) Get access to the allocated object
543
544 Rewrite (N,
545 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
546 Set_Etype (N, Etyp);
547 Set_Analyzed (N);
548
549 -- 2) Add the conversion to displace the pointer to reference
550 -- the secondary dispatch table.
551
552 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
553 Analyze_And_Resolve (N, Dtyp);
554
555 -- 3) The 'access to the secondary dispatch table will be used
556 -- as the value returned by the allocator.
557
558 Rewrite (N,
559 Make_Attribute_Reference (Loc,
560 Prefix => Relocate_Node (N),
561 Attribute_Name => Name_Access));
562 Set_Etype (N, Saved_Typ);
563 Set_Analyzed (N);
564 end;
565
566 -- If the type of the allocator expression is an interface type we
567 -- generate a run-time call to displace "this" to reference the
568 -- component containing the pointer to the secondary dispatch table
569 -- or else raise Constraint_Error if the actual object does not
570 -- implement the target interface. This case corresponds to the
571 -- following example:
572
573 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
574 -- begin
575 -- return new Iface_2'Class'(Obj);
576 -- end Op;
577
578 else
579 Rewrite (N,
580 Unchecked_Convert_To (PtrT,
581 Make_Function_Call (Loc,
582 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
583 Parameter_Associations => New_List (
584 Unchecked_Convert_To (RTE (RE_Address),
585 Relocate_Node (N)),
586
587 New_Occurrence_Of
588 (Elists.Node
589 (First_Elmt
590 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
591 Loc)))));
592 Analyze_And_Resolve (N, PtrT);
593 end if;
594 end if;
595 end Displace_Allocator_Pointer;
596
597 ---------------------------------
598 -- Expand_Allocator_Expression --
599 ---------------------------------
600
601 procedure Expand_Allocator_Expression (N : Node_Id) is
602 Loc : constant Source_Ptr := Sloc (N);
603 Exp : constant Node_Id := Unqualify (Expression (N));
604 Indic : constant Node_Id := Subtype_Mark (Expression (N));
605 T : constant Entity_Id := Entity (Indic);
606 PtrT : constant Entity_Id := Etype (N);
607 DesigT : constant Entity_Id := Designated_Type (PtrT);
608 Pool : constant Node_Id := Storage_Pool (N);
609 Special_Return : constant Boolean := For_Special_Return_Object (N);
610 Special_Pool : constant Boolean :=
611 Present (Pool)
612 and then
613 (Is_RTE (Pool, RE_RS_Pool) or else Is_RTE (Pool, RE_SS_Pool));
614 Static_Match : constant Boolean :=
615 not Is_Constrained (DesigT)
616 or else Subtypes_Statically_Match (T, DesigT);
617
618 procedure Build_Aggregate_In_Place (Temp : Entity_Id; Typ : Entity_Id);
619 -- If Exp is an aggregate to build in place, build the declaration of
620 -- object Temp with Typ and initialization expression an uninitialized
621 -- allocator for Etype (Exp), then perform in-place aggregate assignment
622 -- of Exp into the newly allocated memory.
623
624 procedure Build_Explicit_Assignment (Temp : Entity_Id; Typ : Entity_Id);
625 -- Build the declaration of object Temp with Typ and initialization
626 -- expression an uninitialized allocator for Etype (Exp), then perform
627 -- assignment of Exp into the newly allocated memory.
628
629 procedure Build_Simple_Allocation (Temp : Entity_Id; Typ : Entity_Id);
630 -- Build the declaration of object Temp with Typ and initialization
631 -- expression the allocator N.
632
633 function Needs_Cleanup return Boolean is
634 (not Special_Pool
635 and then Is_Definite_Subtype (T)
636 and then Nkind (Exp) = N_Function_Call
637 and then not (Is_Entity_Name (Name (Exp))
638 and then No_Raise (Entity (Name (Exp))))
639 and then not Restriction_Active (No_Exception_Propagation)
640 and then RTE_Available (RE_Free)
641 and then not Debug_Flag_QQ);
642 -- Return True if a cleanup needs to be built to deallocate the memory
643 -- when the evaluation of the expression raises an exception. This can
644 -- be done only if deallocation is available, but not for special pools
645 -- since such pools do not support deallocation. Moreover, this is not
646 -- needed for an indefinite allocation because the expression will be
647 -- evaluated first, in order to size the allocation. For now, we only
648 -- return True for a call to a function that may raise an exception.
649
650 ------------------------------
651 -- Build_Aggregate_In_Place --
652 ------------------------------
653
654 procedure Build_Aggregate_In_Place (Temp : Entity_Id; Typ : Entity_Id) is
655 Temp_Decl : constant Node_Id :=
656 Make_Object_Declaration (Loc,
657 Defining_Identifier => Temp,
658 Constant_Present => True,
659 Object_Definition => New_Occurrence_Of (Typ, Loc),
660 Expression =>
661 Make_Allocator (Loc,
662 Subpool_Handle_Name =>
663 Relocate_Node (Subpool_Handle_Name (N)),
664 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
665
666 begin
667 -- Prevent default initialization of the allocator
668
669 Set_No_Initialization (Expression (Temp_Decl));
670
671 -- Copy the Comes_From_Source flag onto the allocator since logically
672 -- this allocator is a replacement of the original allocator. This is
673 -- for proper handling of restriction No_Implicit_Heap_Allocations.
674
675 Preserve_Comes_From_Source (Expression (Temp_Decl), N);
676
677 -- Insert the declaration and generate the in-place assignment
678
679 Insert_Action (N, Temp_Decl);
680 Convert_Aggr_In_Allocator (N, Temp);
681 end Build_Aggregate_In_Place;
682
683 -------------------------------
684 -- Build_Explicit_Assignment --
685 -------------------------------
686
687 procedure Build_Explicit_Assignment (Temp : Entity_Id; Typ : Entity_Id)
688 is
689 Assign : constant Node_Id :=
690 Make_Assignment_Statement (Loc,
691 Name =>
692 Make_Explicit_Dereference (Loc,
693 New_Occurrence_Of (Temp, Loc)),
694 Expression => Relocate_Node (Exp));
695
696 Temp_Decl : constant Node_Id :=
697 Make_Object_Declaration (Loc,
698 Defining_Identifier => Temp,
699 Constant_Present => True,
700 Object_Definition => New_Occurrence_Of (Typ, Loc),
701 Expression =>
702 Make_Allocator (Loc,
703 Subpool_Handle_Name =>
704 Relocate_Node (Subpool_Handle_Name (N)),
705 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
706
707 begin
708 -- Prevent default initialization of the allocator
709
710 Set_No_Initialization (Expression (Temp_Decl));
711
712 -- Copy the Comes_From_Source flag onto the allocator since logically
713 -- this allocator is a replacement of the original allocator. This is
714 -- for proper handling of restriction No_Implicit_Heap_Allocations.
715
716 Preserve_Comes_From_Source (Expression (Temp_Decl), N);
717
718 -- Insert the declaration
719
720 Insert_Action (N, Temp_Decl);
721
722 -- Arrange for the expression to be analyzed again and expanded
723
724 if Is_Delayed_Conditional_Expression (Expression (Assign)) then
725 Unanalyze_Delayed_Conditional_Expression (Expression (Assign));
726 end if;
727
728 Set_Assignment_OK (Name (Assign));
729
730 -- If the initialization expression is a function call, we do not
731 -- adjust after the assignment but, in either case, we do not
732 -- finalize before since the target is newly allocated memory.
733
734 if Nkind (Exp) = N_Function_Call then
735 Set_No_Ctrl_Actions (Assign);
736 else
737 Set_No_Finalize_Actions (Assign);
738 end if;
739
740 -- Build a cleanup if the assignment may raise an exception
741
742 if Needs_Cleanup then
743 Insert_Action (N,
744 Build_Cleanup_For_Allocator (Loc,
745 Temp, Pool, New_List (Assign)),
746 Suppress => All_Checks);
747 else
748 Insert_Action (N, Assign, Suppress => All_Checks);
749 end if;
750 end Build_Explicit_Assignment;
751
752 -----------------------------
753 -- Build_Simple_Allocation --
754 -----------------------------
755
756 procedure Build_Simple_Allocation (Temp : Entity_Id; Typ : Entity_Id) is
757 New_N : constant Node_Id := Relocate_Node (N);
758 Temp_Decl : constant Node_Id :=
759 Make_Object_Declaration (Loc,
760 Defining_Identifier => Temp,
761 Constant_Present => True,
762 Object_Definition => New_Occurrence_Of (Typ, Loc),
763 Expression => New_N);
764
765 begin
766 -- Avoid recursion in the mechanism
767
768 Set_Analyzed (New_N);
769
770 Insert_Action (N, Temp_Decl);
771 end Build_Simple_Allocation;
772
773 -- Local variables
774
775 Aggr_In_Place : Boolean;
776 Delayed_Cond_Expr : Boolean;
777
778 TagT : Entity_Id := Empty;
779 -- Type used as source for tag assignment
780
781 TagR : Node_Id := Empty;
782 -- Target reference for tag assignment
783
784 Temp : Entity_Id;
785 -- Temporary used to hold the result of the allocator
786
787 -- Start of processing for Expand_Allocator_Expression
788
789 begin
790 -- Handle call to C++ constructor
791
792 if Is_CPP_Constructor_Call (Exp) then
793 Make_CPP_Constructor_Call_In_Allocator
794 (Allocator => N,
795 Function_Call => Exp);
796 return;
797 end if;
798
799 -- If we have:
800 -- type A is access T1;
801 -- X : A := new T2'(...);
802 -- T1 and T2 can be different subtypes, and we might need to check
803 -- both constraints. First check against the type of the qualified
804 -- expression.
805
806 -- Note that we delay applying predicate checks, because this may
807 -- cause the creation of a temporary, which is illegal for limited
808 -- types and just inefficient in the other cases.
809
810 Apply_Constraint_Check (Exp, T, No_Sliding => True);
811
812 if Do_Range_Check (Exp) then
813 Generate_Range_Check (Exp, T, CE_Range_Check_Failed);
814 end if;
815
816 -- A check is also needed in cases where the designated subtype is
817 -- constrained and differs from the subtype given in the qualified
818 -- expression. Note that the check on the qualified expression does
819 -- not allow sliding, but this check does (a relaxation from Ada 83).
820
821 if not Static_Match then
822 Apply_Constraint_Check (Exp, DesigT, No_Sliding => False);
823
824 if Do_Range_Check (Exp) then
825 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
826 end if;
827 end if;
828
829 -- Propagate Constraint_Error and return
830
831 if Nkind (Exp) = N_Raise_Constraint_Error then
832 Rewrite (N, New_Copy (Exp));
833 Set_Etype (N, PtrT);
834 return;
835 end if;
836
837 -- Check that any anonymous access discriminants are suitable
838 -- for use in an allocator.
839
840 -- Note: This check is performed here instead of during analysis
841 -- so that we can check against the fully resolved Etype of Exp.
842
843 if Is_Entity_Name (Exp)
844 and then Has_Anonymous_Access_Discriminant (Etype (Exp))
845 and then Static_Accessibility_Level (Exp, Object_Decl_Level)
846 > Static_Accessibility_Level (N, Object_Decl_Level)
847 then
848 -- A dynamic check and a warning are generated when we are within
849 -- an instance.
850
851 if In_Instance then
852 Insert_Action (N,
853 Make_Raise_Program_Error (Loc,
854 Reason => PE_Accessibility_Check_Failed));
855
856 Error_Msg_Warn := SPARK_Mode /= On;
857 Error_Msg_N ("anonymous access discriminant is too deep for use"
858 & " in allocator<<", N);
859 Error_Msg_N ("\Program_Error [<<", N);
860
861 -- Otherwise, make the error static
862
863 else
864 Error_Msg_N ("anonymous access discriminant is too deep for use"
865 & " in allocator", N);
866 end if;
867 end if;
868
869 Aggr_In_Place := Is_Delayed_Aggregate (Exp);
870 Delayed_Cond_Expr := Is_Delayed_Conditional_Expression (Exp);
871
872 -- An allocator with a container aggregate, resp. a 2-pass aggregate,
873 -- as qualified expression must be rewritten into the form expected by
874 -- Expand_Container_Aggregate, resp. Two_Pass_Aggregate_Expansion.
875
876 if Nkind (Exp) = N_Aggregate
877 and then (Has_Aspect (T, Aspect_Aggregate)
878 or else Is_Two_Pass_Aggregate (Exp))
879 then
880 Temp := Make_Temporary (Loc, 'P', N);
881 Set_Analyzed (Exp, False);
882 Insert_Action (N,
883 Make_Object_Declaration (Loc,
884 Defining_Identifier => Temp,
885 Object_Definition => New_Occurrence_Of (PtrT, Loc),
886 Expression => Relocate_Node (N)));
887
888 -- Case of tagged type or type requiring finalization
889
890 elsif Is_Tagged_Type (T) or else Needs_Finalization (T) then
891
892 -- Ada 2005 (AI-318-02): If the initialization expression is a call
893 -- to a build-in-place function, then access to the allocated object
894 -- must be passed to the function.
895
896 if Is_Build_In_Place_Function_Call (Exp) then
897 Make_Build_In_Place_Call_In_Allocator (N, Exp);
898 Apply_Accessibility_Check_For_Allocator
899 (N, Exp, N, Built_In_Place => True);
900 return;
901
902 -- Ada 2005 (AI-318-02): Specialization of the previous case for
903 -- expressions containing a build-in-place function call whose
904 -- returned object covers interface types, and Expr has calls to
905 -- Ada.Tags.Displace to displace the pointer to the returned build-
906 -- in-place object to reference the secondary dispatch table of a
907 -- covered interface type.
908
909 elsif Present (Unqual_BIP_Iface_Function_Call (Exp)) then
910 Make_Build_In_Place_Iface_Call_In_Allocator (N, Exp);
911 Apply_Accessibility_Check_For_Allocator
912 (N, Exp, N, Built_In_Place => True);
913 return;
914 end if;
915
916 -- For a class wide allocation generate the following code:
917
918 -- type Equiv_Record is record ... end record;
919 -- implicit subtype CW is <Class_Wide_Subytpe>;
920 -- Temp : PtrT := new CW'(CW!(expr));
921
922 if Is_Class_Wide_Type (T) then
923 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
924
925 -- Ada 2005 (AI-251): If the expression is a class-wide interface
926 -- object we generate code to move up "this" to reference the
927 -- base of the object before allocating the new object.
928
929 -- Note that Exp'Address is recursively expanded into a call
930 -- to Base_Address (Exp.Tag)
931
932 if Is_Class_Wide_Type (Etype (Exp))
933 and then Is_Interface (Etype (Exp))
934 and then Tagged_Type_Expansion
935 then
936 Set_Expression
937 (Expression (N),
938 Unchecked_Convert_To (Entity (Indic),
939 Make_Explicit_Dereference (Loc,
940 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
941 Make_Attribute_Reference (Loc,
942 Prefix => Exp,
943 Attribute_Name => Name_Address)))));
944 else
945 Set_Expression
946 (Expression (N),
947 Unchecked_Convert_To (Entity (Indic), Exp));
948 end if;
949
950 Analyze_And_Resolve (Expression (N), Entity (Indic));
951 end if;
952
953 -- If the designated type is class-wide, then the alignment and the
954 -- controlled nature of the expression are computed dynamically by
955 -- the code generated by Build_Allocate_Deallocate_Proc, which will
956 -- thus need to remove side effects from Exp first. But the below
957 -- test on Exp needs to have its final form to decide whether or not
958 -- to generate an Adjust call, so we preventively remove them here.
959
960 if Is_Class_Wide_Type (DesigT)
961 and then Nkind (Exp) = N_Function_Call
962 and then not Special_Pool
963 then
964 Remove_Side_Effects (Exp);
965 end if;
966
967 -- Actions inserted before:
968 -- Temp : constant PtrT := new T'(Expression);
969 -- Temp._tag = T'tag; -- when not class-wide
970 -- [Deep_]Adjust (Temp.all);
971
972 -- We analyze by hand the new internal allocator to avoid any
973 -- recursion and inappropriate call to Initialize.
974
975 Temp := Make_Temporary (Loc, 'P', N);
976
977 -- Processing for allocators returning non-interface types
978
979 if not Is_Interface (DesigT) then
980 if Aggr_In_Place then
981 Build_Aggregate_In_Place (Temp, PtrT);
982
983 elsif Delayed_Cond_Expr or else Needs_Cleanup then
984 Build_Explicit_Assignment (Temp, PtrT);
985
986 else
987 Build_Simple_Allocation (Temp, PtrT);
988 end if;
989
990 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
991 -- interface type. In this case we use the type of the qualified
992 -- expression to allocate the object.
993
994 else
995 declare
996 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T', N);
997 New_Temp : constant Entity_Id := Make_Temporary (Loc, 'P', N);
998
999 begin
1000 Insert_Action (N,
1001 Make_Full_Type_Declaration (Loc,
1002 Defining_Identifier => Def_Id,
1003 Type_Definition =>
1004 Make_Access_To_Object_Definition (Loc,
1005 All_Present => True,
1006 Constant_Present =>
1007 Is_Access_Constant (PtrT),
1008 Subtype_Indication =>
1009 New_Occurrence_Of (Etype (Exp), Loc))));
1010
1011 -- Inherit the allocation-related attributes from the original
1012 -- access type.
1013
1014 Set_Finalization_Collection
1015 (Def_Id, Finalization_Collection (PtrT));
1016
1017 Set_Associated_Storage_Pool
1018 (Def_Id, Associated_Storage_Pool (PtrT));
1019
1020 -- Declare the object using the previous type declaration
1021
1022 if Aggr_In_Place then
1023 Build_Aggregate_In_Place (New_Temp, Def_Id);
1024
1025 elsif Delayed_Cond_Expr or else Needs_Cleanup then
1026 Build_Explicit_Assignment (New_Temp, Def_Id);
1027
1028 else
1029 Build_Simple_Allocation (New_Temp, Def_Id);
1030 end if;
1031
1032 -- Generate an additional object containing the address of the
1033 -- returned object. The type of this second object declaration
1034 -- is the correct type required for the common processing that
1035 -- is still performed by this subprogram. The displacement of
1036 -- this pointer to reference the component associated with the
1037 -- interface type will be done at the end of common processing.
1038
1039 Insert_Action (N,
1040 Make_Object_Declaration (Loc,
1041 Defining_Identifier => Temp,
1042 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1043 Expression =>
1044 Unchecked_Convert_To (PtrT,
1045 New_Occurrence_Of (New_Temp, Loc))));
1046 end;
1047 end if;
1048
1049 -- Generate the tag assignment
1050
1051 -- Suppress the tag assignment for VM targets because VM tags are
1052 -- represented implicitly in objects.
1053
1054 if not Tagged_Type_Expansion then
1055 null;
1056
1057 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1058 -- interface objects because in this case the tag does not change.
1059
1060 elsif Is_Interface (DesigT) then
1061 pragma Assert (Is_Class_Wide_Type (DesigT));
1062 null;
1063
1064 -- Likewise if the allocator is made for a special return object
1065
1066 elsif Special_Return then
1067 null;
1068
1069 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1070 TagT := T;
1071 TagR :=
1072 Make_Explicit_Dereference (Loc,
1073 Prefix => New_Occurrence_Of (Temp, Loc));
1074
1075 elsif Is_Private_Type (T)
1076 and then Is_Tagged_Type (Underlying_Type (T))
1077 then
1078 TagT := Underlying_Type (T);
1079 TagR :=
1080 Unchecked_Convert_To (Underlying_Type (T),
1081 Make_Explicit_Dereference (Loc,
1082 Prefix => New_Occurrence_Of (Temp, Loc)));
1083 end if;
1084
1085 if Present (TagT) then
1086 Insert_Action (N,
1087 Make_Tag_Assignment_From_Type
1088 (Loc, TagR, Underlying_Type (TagT)));
1089 end if;
1090
1091 -- Generate an Adjust call if the object will be moved. In Ada 2005,
1092 -- the object may be inherently limited, in which case there is no
1093 -- Adjust procedure, and the object is built in place. In Ada 95, the
1094 -- object can be limited but not inherently limited if this allocator
1095 -- came from a return statement (we're allocating the result on the
1096 -- secondary stack); in that case, the object will be moved, so we do
1097 -- want to Adjust. But the call is always skipped if the allocator is
1098 -- made for a special return object because it's generated elsewhere.
1099
1100 -- Needs_Finalization (DesigT) may differ from Needs_Finalization (T)
1101 -- if one of the two types is class-wide, and the other is not.
1102
1103 if Needs_Finalization (DesigT)
1104 and then Needs_Finalization (T)
1105 and then not Is_Inherently_Limited_Type (T)
1106 and then not Aggr_In_Place
1107 and then not Delayed_Cond_Expr
1108 and then Nkind (Exp) /= N_Function_Call
1109 and then not Special_Return
1110 then
1111 declare
1112 Adj_Call : constant Node_Id :=
1113 Make_Adjust_Call
1114 (Obj_Ref =>
1115 Unchecked_Convert_To (T,
1116 Make_Explicit_Dereference (Loc,
1117 Prefix => New_Occurrence_Of (Temp, Loc))),
1118 Typ => T);
1119 -- An unchecked conversion is needed in the CW case because
1120 -- the designated type can be an ancestor of the subtype mark
1121 -- of the allocator.
1122
1123 begin
1124 if Present (Adj_Call) then
1125 Insert_Action (N, Adj_Call);
1126 end if;
1127 end;
1128 end if;
1129
1130 -- This needs to done before generating the accessibility check below
1131 -- because the check comes with cleanup code that invokes Free on the
1132 -- temporary and, therefore, expects the object to be attached to its
1133 -- finalization collection if it is controlled.
1134
1135 Build_Allocate_Deallocate_Proc (Declaration_Node (Temp), Mark => N);
1136
1137 -- Note: the accessibility check must be inserted after the call to
1138 -- [Deep_]Adjust to ensure proper completion of the assignment.
1139
1140 Apply_Accessibility_Check_For_Allocator (N, Exp, Temp);
1141
1142 -- Case of aggregate built in place
1143
1144 elsif Aggr_In_Place then
1145 Temp := Make_Temporary (Loc, 'P', N);
1146 Build_Aggregate_In_Place (Temp, PtrT);
1147 Build_Allocate_Deallocate_Proc (Declaration_Node (Temp), Mark => N);
1148
1149 -- If the initialization expression is a conditional expression whose
1150 -- expansion has been delayed, assign it explicitly to the allocator,
1151 -- but only after analyzing it again and expanding it.
1152
1153 elsif Delayed_Cond_Expr then
1154 Temp := Make_Temporary (Loc, 'P', N);
1155 Build_Explicit_Assignment (Temp, PtrT);
1156 Build_Allocate_Deallocate_Proc (Declaration_Node (Temp), Mark => N);
1157
1158 -- Default case
1159
1160 else
1161 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1162 -- to a build-in-place function, then access to the allocated object
1163 -- must be passed to the function.
1164
1165 if Is_Build_In_Place_Function_Call (Exp) then
1166 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1167 return;
1168 end if;
1169
1170 if Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1171 Install_Null_Excluding_Check (Exp);
1172 end if;
1173
1174 if Is_Access_Type (DesigT)
1175 and then Nkind (Exp) = N_Allocator
1176 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1177 then
1178 -- Apply constraint to designated subtype indication
1179
1180 Apply_Constraint_Check
1181 (Expression (Exp), Designated_Type (DesigT), No_Sliding => True);
1182
1183 -- Propagate Constraint_Error to enclosing allocator
1184
1185 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1186 Rewrite (Exp, New_Copy (Expression (Exp)));
1187 end if;
1188 end if;
1189
1190 -- For an access-to-unconstrained-packed-array type, build an
1191 -- expression with a constrained subtype in order for the code
1192 -- generator to compute the proper size for the allocator.
1193
1194 if Is_Packed_Array (T) and then not Is_Constrained (T) then
1195 declare
1196 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1197 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1198 begin
1199 Insert_Action (Exp,
1200 Make_Subtype_Declaration (Loc,
1201 Defining_Identifier => ConstrT,
1202 Subtype_Indication =>
1203 Make_Subtype_From_Expr (Internal_Exp, T)));
1204 Freeze_Itype (ConstrT, Exp);
1205 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1206 end;
1207 end if;
1208
1209 -- ??? If the allocator is present inside a record type, then the
1210 -- actions are attached to the current scope, to be inserted and
1211 -- analyzed on exit from the scope, so we cannot do any rewriting.
1212
1213 if Is_Record_Type (Current_Scope)
1214 and then not Is_Frozen (Current_Scope)
1215 then
1216 Build_Allocate_Deallocate_Proc (N);
1217 return;
1218 end if;
1219
1220 Temp := Make_Temporary (Loc, 'P', N);
1221 if Needs_Cleanup then
1222 Build_Explicit_Assignment (Temp, PtrT);
1223 else
1224 Build_Simple_Allocation (Temp, PtrT);
1225 end if;
1226 Build_Allocate_Deallocate_Proc (Declaration_Node (Temp), Mark => N);
1227 end if;
1228
1229 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1230 Preserve_Comes_From_Source (N, Original_Node (N));
1231 Analyze_And_Resolve (N, PtrT);
1232
1233 Apply_Predicate_Check (N, T, Deref => True);
1234 if not Static_Match then
1235 Apply_Predicate_Check (N, DesigT, Deref => True);
1236 end if;
1237
1238 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1239 -- component containing the secondary dispatch table of the interface
1240 -- type.
1241
1242 if Is_Interface (DesigT) then
1243 Displace_Allocator_Pointer (N);
1244 end if;
1245
1246 exception
1247 when RE_Not_Available =>
1248 return;
1249 end Expand_Allocator_Expression;
1250
1251 -----------------------------
1252 -- Expand_Array_Comparison --
1253 -----------------------------
1254
1255 -- Expansion is only required in the case of array types. For the unpacked
1256 -- case, an appropriate runtime routine is called. For packed cases, and
1257 -- also in some other cases where a runtime routine cannot be called, the
1258 -- form of the expansion is:
1259
1260 -- [body for greater_nn; boolean_expression]
1261
1262 -- The body is built by Make_Array_Comparison_Op, and the form of the
1263 -- Boolean expression depends on the operator involved.
1264
1265 procedure Expand_Array_Comparison (N : Node_Id) is
1266 Loc : constant Source_Ptr := Sloc (N);
1267 Op1 : Node_Id := Left_Opnd (N);
1268 Op2 : Node_Id := Right_Opnd (N);
1269 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1270 Ctyp : constant Entity_Id := Component_Type (Typ1);
1271
1272 Expr : Node_Id;
1273 Func_Body : Node_Id;
1274 Func_Name : Entity_Id;
1275
1276 Comp : RE_Id;
1277
1278 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1279 -- Returns True if the length of the given operand is known to be less
1280 -- than 4. Returns False if this length is known to be four or greater
1281 -- or is not known at compile time.
1282
1283 ------------------------
1284 -- Length_Less_Than_4 --
1285 ------------------------
1286
1287 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1288 Otyp : constant Entity_Id := Etype (Opnd);
1289
1290 begin
1291 if Ekind (Otyp) = E_String_Literal_Subtype then
1292 return String_Literal_Length (Otyp) < 4;
1293
1294 elsif Compile_Time_Known_Bounds (Otyp) then
1295 declare
1296 Lo, Hi : Uint;
1297
1298 begin
1299 Get_First_Index_Bounds (Otyp, Lo, Hi);
1300 return Hi < Lo + 3;
1301 end;
1302
1303 else
1304 return False;
1305 end if;
1306 end Length_Less_Than_4;
1307
1308 -- Start of processing for Expand_Array_Comparison
1309
1310 begin
1311 -- Deal first with unpacked case, where we can call a runtime routine,
1312 -- except if the component type is a byte (unsigned) where we can use
1313 -- a byte-wise comparison if supported on the target (this is disabled
1314 -- for now in Unnest_Subprogram_Mode for LLVM).
1315
1316 if not Is_Bit_Packed_Array (Typ1) then
1317 -- The call we generate is:
1318
1319 -- Compare_Array_xn[_Unaligned]
1320 -- (left'address, right'address, left'length, right'length) <op> 0
1321
1322 -- x = U for unsigned, S for signed
1323 -- n = 8,16,32,64,128 for component size
1324 -- Add _Unaligned if length < 4 and component size is 8.
1325 -- <op> is the standard comparison operator
1326
1327 if Component_Size (Typ1) = 8 then
1328 if Is_Unsigned_Type (Ctyp)
1329 and then not Is_Possibly_Unaligned_Object (Op1)
1330 and then not Is_Possibly_Unaligned_Slice (Op1)
1331 and then not Is_Possibly_Unaligned_Object (Op2)
1332 and then not Is_Possibly_Unaligned_Slice (Op2)
1333 and then Support_Composite_Compare_On_Target
1334 and then not Unnest_Subprogram_Mode
1335 then
1336 return;
1337
1338 elsif Length_Less_Than_4 (Op1)
1339 or else Length_Less_Than_4 (Op2)
1340 then
1341 if Is_Unsigned_Type (Ctyp) then
1342 Comp := RE_Compare_Array_U8_Unaligned;
1343 else
1344 Comp := RE_Compare_Array_S8_Unaligned;
1345 end if;
1346
1347 else
1348 if Is_Unsigned_Type (Ctyp) then
1349 Comp := RE_Compare_Array_U8;
1350 else
1351 Comp := RE_Compare_Array_S8;
1352 end if;
1353 end if;
1354
1355 elsif Component_Size (Typ1) = 16 then
1356 if Is_Unsigned_Type (Ctyp) then
1357 Comp := RE_Compare_Array_U16;
1358 else
1359 Comp := RE_Compare_Array_S16;
1360 end if;
1361
1362 elsif Component_Size (Typ1) = 32 then
1363 if Is_Unsigned_Type (Ctyp) then
1364 Comp := RE_Compare_Array_U32;
1365 else
1366 Comp := RE_Compare_Array_S32;
1367 end if;
1368
1369 elsif Component_Size (Typ1) = 64 then
1370 if Is_Unsigned_Type (Ctyp) then
1371 Comp := RE_Compare_Array_U64;
1372 else
1373 Comp := RE_Compare_Array_S64;
1374 end if;
1375
1376 else pragma Assert (Component_Size (Typ1) = 128);
1377 if Is_Unsigned_Type (Ctyp) then
1378 Comp := RE_Compare_Array_U128;
1379 else
1380 Comp := RE_Compare_Array_S128;
1381 end if;
1382 end if;
1383
1384 -- Expand to a call only if the runtime function is available,
1385 -- otherwise fall back to inline code.
1386
1387 if RTE_Available (Comp) then
1388 Remove_Side_Effects (Op1, Name_Req => True);
1389 Remove_Side_Effects (Op2, Name_Req => True);
1390
1391 declare
1392 Comp_Call : constant Node_Id :=
1393 Make_Function_Call (Loc,
1394 Name => New_Occurrence_Of (RTE (Comp), Loc),
1395
1396 Parameter_Associations => New_List (
1397 Make_Attribute_Reference (Loc,
1398 Prefix => Relocate_Node (Op1),
1399 Attribute_Name => Name_Address),
1400
1401 Make_Attribute_Reference (Loc,
1402 Prefix => Relocate_Node (Op2),
1403 Attribute_Name => Name_Address),
1404
1405 Make_Attribute_Reference (Loc,
1406 Prefix => Relocate_Node (Op1),
1407 Attribute_Name => Name_Length),
1408
1409 Make_Attribute_Reference (Loc,
1410 Prefix => Relocate_Node (Op2),
1411 Attribute_Name => Name_Length)));
1412
1413 Zero : constant Node_Id :=
1414 Make_Integer_Literal (Loc, Intval => Uint_0);
1415
1416 Comp_Op : Node_Id;
1417
1418 begin
1419 case Nkind (N) is
1420 when N_Op_Lt =>
1421 Comp_Op := Make_Op_Lt (Loc, Comp_Call, Zero);
1422 when N_Op_Le =>
1423 Comp_Op := Make_Op_Le (Loc, Comp_Call, Zero);
1424 when N_Op_Gt =>
1425 Comp_Op := Make_Op_Gt (Loc, Comp_Call, Zero);
1426 when N_Op_Ge =>
1427 Comp_Op := Make_Op_Ge (Loc, Comp_Call, Zero);
1428 when others =>
1429 raise Program_Error;
1430 end case;
1431
1432 Rewrite (N, Comp_Op);
1433 end;
1434
1435 Analyze_And_Resolve (N, Standard_Boolean);
1436 return;
1437 end if;
1438 end if;
1439
1440 -- Cases where we cannot make runtime call
1441
1442 -- For (a <= b) we convert to not (a > b)
1443
1444 if Chars (N) = Name_Op_Le then
1445 Rewrite (N,
1446 Make_Op_Not (Loc,
1447 Right_Opnd =>
1448 Make_Op_Gt (Loc,
1449 Left_Opnd => Op1,
1450 Right_Opnd => Op2)));
1451 Analyze_And_Resolve (N, Standard_Boolean);
1452 return;
1453
1454 -- For < the Boolean expression is
1455 -- greater__nn (op2, op1)
1456
1457 elsif Chars (N) = Name_Op_Lt then
1458 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1459
1460 -- Switch operands
1461
1462 Op1 := Right_Opnd (N);
1463 Op2 := Left_Opnd (N);
1464
1465 -- For (a >= b) we convert to not (a < b)
1466
1467 elsif Chars (N) = Name_Op_Ge then
1468 Rewrite (N,
1469 Make_Op_Not (Loc,
1470 Right_Opnd =>
1471 Make_Op_Lt (Loc,
1472 Left_Opnd => Op1,
1473 Right_Opnd => Op2)));
1474 Analyze_And_Resolve (N, Standard_Boolean);
1475 return;
1476
1477 -- For > the Boolean expression is
1478 -- greater__nn (op1, op2)
1479
1480 else
1481 pragma Assert (Chars (N) = Name_Op_Gt);
1482 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1483 end if;
1484
1485 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1486 Expr :=
1487 Make_Function_Call (Loc,
1488 Name => New_Occurrence_Of (Func_Name, Loc),
1489 Parameter_Associations => New_List (Op1, Op2));
1490
1491 Insert_Action (N, Func_Body);
1492 Rewrite (N, Expr);
1493 Analyze_And_Resolve (N, Standard_Boolean);
1494 end Expand_Array_Comparison;
1495
1496 ---------------------------
1497 -- Expand_Array_Equality --
1498 ---------------------------
1499
1500 -- Expand an equality function for multi-dimensional arrays. Here is an
1501 -- example of such a function for Nb_Dimension = 2
1502
1503 -- function Enn (A : atyp; B : btyp) return boolean is
1504 -- begin
1505 -- if (A'length (1) = 0 or else A'length (2) = 0)
1506 -- and then
1507 -- (B'length (1) = 0 or else B'length (2) = 0)
1508 -- then
1509 -- return true; -- RM 4.5.2(22)
1510 -- end if;
1511
1512 -- if A'length (1) /= B'length (1)
1513 -- or else
1514 -- A'length (2) /= B'length (2)
1515 -- then
1516 -- return false; -- RM 4.5.2(23)
1517 -- end if;
1518
1519 -- declare
1520 -- A1 : Index_T1 := A'first (1);
1521 -- B1 : Index_T1 := B'first (1);
1522 -- begin
1523 -- loop
1524 -- declare
1525 -- A2 : Index_T2 := A'first (2);
1526 -- B2 : Index_T2 := B'first (2);
1527 -- begin
1528 -- loop
1529 -- if A (A1, A2) /= B (B1, B2) then
1530 -- return False;
1531 -- end if;
1532
1533 -- exit when A2 = A'last (2);
1534 -- A2 := Index_T2'succ (A2);
1535 -- B2 := Index_T2'succ (B2);
1536 -- end loop;
1537 -- end;
1538
1539 -- exit when A1 = A'last (1);
1540 -- A1 := Index_T1'succ (A1);
1541 -- B1 := Index_T1'succ (B1);
1542 -- end loop;
1543 -- end;
1544
1545 -- return true;
1546 -- end Enn;
1547
1548 -- Note on the formal types used (atyp and btyp). If either of the arrays
1549 -- is of a private type, we use the underlying type, and do an unchecked
1550 -- conversion of the actual. If either of the arrays has a bound depending
1551 -- on a discriminant, then we use the base type since otherwise we have an
1552 -- escaped discriminant in the function.
1553
1554 -- If both arrays are constrained and have the same bounds, we can generate
1555 -- a loop with an explicit iteration scheme using a 'Range attribute over
1556 -- the first array.
1557
1558 function Expand_Array_Equality
1559 (Nod : Node_Id;
1560 Lhs : Node_Id;
1561 Rhs : Node_Id;
1562 Bodies : List_Id;
1563 Typ : Entity_Id) return Node_Id
1564 is
1565 Loc : constant Source_Ptr := Sloc (Nod);
1566 Decls : constant List_Id := New_List;
1567 Index_List1 : constant List_Id := New_List;
1568 Index_List2 : constant List_Id := New_List;
1569
1570 First_Idx : Node_Id;
1571 Formals : List_Id;
1572 Func_Name : Entity_Id;
1573 Func_Body : Node_Id;
1574
1575 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1576 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1577
1578 Ltyp : Entity_Id;
1579 Rtyp : Entity_Id;
1580 -- The parameter types to be used for the formals
1581
1582 New_Lhs : Node_Id;
1583 New_Rhs : Node_Id;
1584 -- The LHS and RHS converted to the parameter types
1585
1586 function Arr_Attr
1587 (Arr : Entity_Id;
1588 Nam : Name_Id;
1589 Dim : Pos) return Node_Id;
1590 -- This builds the attribute reference Arr'Nam (Dim)
1591
1592 function Component_Equality (Typ : Entity_Id) return Node_Id;
1593 -- Create one statement to compare corresponding components, designated
1594 -- by a full set of indexes.
1595
1596 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1597 -- Given one of the arguments, computes the appropriate type to be used
1598 -- for that argument in the corresponding function formal
1599
1600 function Handle_One_Dimension
1601 (N : Pos;
1602 Index : Node_Id) return Node_Id;
1603 -- This procedure returns the following code
1604 --
1605 -- declare
1606 -- An : Index_T := A'First (N);
1607 -- Bn : Index_T := B'First (N);
1608 -- begin
1609 -- loop
1610 -- xxx
1611 -- exit when An = A'Last (N);
1612 -- An := Index_T'Succ (An)
1613 -- Bn := Index_T'Succ (Bn)
1614 -- end loop;
1615 -- end;
1616 --
1617 -- If both indexes are constrained and identical, the procedure
1618 -- returns a simpler loop:
1619 --
1620 -- for An in A'Range (N) loop
1621 -- xxx
1622 -- end loop
1623 --
1624 -- N is the dimension for which we are generating a loop. Index is the
1625 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1626 -- xxx statement is either the loop or declare for the next dimension
1627 -- or if this is the last dimension the comparison of corresponding
1628 -- components of the arrays.
1629 --
1630 -- The actual way the code works is to return the comparison of
1631 -- corresponding components for the N+1 call. That's neater.
1632
1633 function Test_Empty_Arrays return Node_Id;
1634 -- This function constructs the test for both arrays being empty
1635 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1636 -- and then
1637 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1638
1639 function Test_Lengths_Correspond return Node_Id;
1640 -- This function constructs the test for arrays having different lengths
1641 -- in at least one index position, in which case the resulting code is:
1642
1643 -- A'length (1) /= B'length (1)
1644 -- or else
1645 -- A'length (2) /= B'length (2)
1646 -- or else
1647 -- ...
1648
1649 --------------
1650 -- Arr_Attr --
1651 --------------
1652
1653 function Arr_Attr
1654 (Arr : Entity_Id;
1655 Nam : Name_Id;
1656 Dim : Pos) return Node_Id
1657 is
1658 begin
1659 return
1660 Make_Attribute_Reference (Loc,
1661 Attribute_Name => Nam,
1662 Prefix => New_Occurrence_Of (Arr, Loc),
1663 Expressions => New_List (Make_Integer_Literal (Loc, Dim)));
1664 end Arr_Attr;
1665
1666 ------------------------
1667 -- Component_Equality --
1668 ------------------------
1669
1670 function Component_Equality (Typ : Entity_Id) return Node_Id is
1671 Test : Node_Id;
1672 L, R : Node_Id;
1673
1674 begin
1675 -- if a(i1...) /= b(j1...) then return false; end if;
1676
1677 L :=
1678 Make_Indexed_Component (Loc,
1679 Prefix => Make_Identifier (Loc, Chars (A)),
1680 Expressions => Index_List1);
1681
1682 R :=
1683 Make_Indexed_Component (Loc,
1684 Prefix => Make_Identifier (Loc, Chars (B)),
1685 Expressions => Index_List2);
1686
1687 Test := Expand_Composite_Equality
1688 (Outer_Type => Typ, Nod => Nod, Comp_Type => Component_Type (Typ),
1689 Lhs => L, Rhs => R);
1690
1691 -- This is necessary to give the warning about Program_Error being
1692 -- raised when some (sub)component is an unchecked_union.
1693
1694 Preserve_Comes_From_Source (Test, Nod);
1695
1696 return
1697 Make_Implicit_If_Statement (Nod,
1698 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1699 Then_Statements => New_List (
1700 Make_Simple_Return_Statement (Loc,
1701 Expression => New_Occurrence_Of (Standard_False, Loc))));
1702 end Component_Equality;
1703
1704 ------------------
1705 -- Get_Arg_Type --
1706 ------------------
1707
1708 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1709 T : Entity_Id;
1710 X : Node_Id;
1711
1712 begin
1713 T := Etype (N);
1714
1715 if No (T) then
1716 return Typ;
1717
1718 else
1719 T := Underlying_Type (T);
1720
1721 X := First_Index (T);
1722 while Present (X) loop
1723 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1724 or else
1725 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1726 then
1727 T := Base_Type (T);
1728 exit;
1729 end if;
1730
1731 Next_Index (X);
1732 end loop;
1733
1734 return T;
1735 end if;
1736 end Get_Arg_Type;
1737
1738 --------------------------
1739 -- Handle_One_Dimension --
1740 ---------------------------
1741
1742 function Handle_One_Dimension
1743 (N : Pos;
1744 Index : Node_Id) return Node_Id
1745 is
1746 Need_Separate_Indexes : constant Boolean :=
1747 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1748 -- If the index types are identical, and we are working with
1749 -- constrained types, then we can use the same index for both
1750 -- of the arrays.
1751
1752 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1753
1754 Bn : Entity_Id;
1755 Index_T : Entity_Id;
1756 Stm_List : List_Id;
1757 Loop_Stm : Node_Id;
1758
1759 begin
1760 if N > Number_Dimensions (Ltyp) then
1761 return Component_Equality (Ltyp);
1762 end if;
1763
1764 -- Case where we generate a loop
1765
1766 Index_T := Base_Type (Etype (Index));
1767
1768 if Need_Separate_Indexes then
1769 Bn := Make_Temporary (Loc, 'B');
1770 else
1771 Bn := An;
1772 end if;
1773
1774 Append (New_Occurrence_Of (An, Loc), Index_List1);
1775 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1776
1777 Stm_List := New_List (
1778 Handle_One_Dimension (N + 1, Next_Index (Index)));
1779
1780 if Need_Separate_Indexes then
1781
1782 -- Generate guard for loop, followed by increments of indexes
1783
1784 Append_To (Stm_List,
1785 Make_Exit_Statement (Loc,
1786 Condition =>
1787 Make_Op_Eq (Loc,
1788 Left_Opnd => New_Occurrence_Of (An, Loc),
1789 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1790
1791 Append_To (Stm_List,
1792 Make_Assignment_Statement (Loc,
1793 Name => New_Occurrence_Of (An, Loc),
1794 Expression =>
1795 Make_Attribute_Reference (Loc,
1796 Prefix => New_Occurrence_Of (Index_T, Loc),
1797 Attribute_Name => Name_Succ,
1798 Expressions => New_List (
1799 New_Occurrence_Of (An, Loc)))));
1800
1801 Append_To (Stm_List,
1802 Make_Assignment_Statement (Loc,
1803 Name => New_Occurrence_Of (Bn, Loc),
1804 Expression =>
1805 Make_Attribute_Reference (Loc,
1806 Prefix => New_Occurrence_Of (Index_T, Loc),
1807 Attribute_Name => Name_Succ,
1808 Expressions => New_List (
1809 New_Occurrence_Of (Bn, Loc)))));
1810 end if;
1811
1812 -- If separate indexes, we need a declare block for An and Bn, and a
1813 -- loop without an iteration scheme.
1814
1815 if Need_Separate_Indexes then
1816 Loop_Stm :=
1817 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1818
1819 return
1820 Make_Block_Statement (Loc,
1821 Declarations => New_List (
1822 Make_Object_Declaration (Loc,
1823 Defining_Identifier => An,
1824 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1825 Expression => Arr_Attr (A, Name_First, N)),
1826
1827 Make_Object_Declaration (Loc,
1828 Defining_Identifier => Bn,
1829 Object_Definition => New_Occurrence_Of (Index_T, Loc),
1830 Expression => Arr_Attr (B, Name_First, N))),
1831
1832 Handled_Statement_Sequence =>
1833 Make_Handled_Sequence_Of_Statements (Loc,
1834 Statements => New_List (Loop_Stm)));
1835
1836 -- If no separate indexes, return loop statement with explicit
1837 -- iteration scheme on its own.
1838
1839 else
1840 Loop_Stm :=
1841 Make_Implicit_Loop_Statement (Nod,
1842 Statements => Stm_List,
1843 Iteration_Scheme =>
1844 Make_Iteration_Scheme (Loc,
1845 Loop_Parameter_Specification =>
1846 Make_Loop_Parameter_Specification (Loc,
1847 Defining_Identifier => An,
1848 Discrete_Subtype_Definition =>
1849 Arr_Attr (A, Name_Range, N))));
1850 return Loop_Stm;
1851 end if;
1852 end Handle_One_Dimension;
1853
1854 -----------------------
1855 -- Test_Empty_Arrays --
1856 -----------------------
1857
1858 function Test_Empty_Arrays return Node_Id is
1859 Alist : Node_Id := Empty;
1860 Blist : Node_Id := Empty;
1861
1862 begin
1863 for J in 1 .. Number_Dimensions (Ltyp) loop
1864 Evolve_Or_Else (Alist,
1865 Make_Op_Eq (Loc,
1866 Left_Opnd => Arr_Attr (A, Name_Length, J),
1867 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)));
1868
1869 Evolve_Or_Else (Blist,
1870 Make_Op_Eq (Loc,
1871 Left_Opnd => Arr_Attr (B, Name_Length, J),
1872 Right_Opnd => Make_Integer_Literal (Loc, Uint_0)));
1873 end loop;
1874
1875 return
1876 Make_And_Then (Loc,
1877 Left_Opnd => Alist,
1878 Right_Opnd => Blist);
1879 end Test_Empty_Arrays;
1880
1881 -----------------------------
1882 -- Test_Lengths_Correspond --
1883 -----------------------------
1884
1885 function Test_Lengths_Correspond return Node_Id is
1886 Result : Node_Id := Empty;
1887
1888 begin
1889 for J in 1 .. Number_Dimensions (Ltyp) loop
1890 Evolve_Or_Else (Result,
1891 Make_Op_Ne (Loc,
1892 Left_Opnd => Arr_Attr (A, Name_Length, J),
1893 Right_Opnd => Arr_Attr (B, Name_Length, J)));
1894 end loop;
1895
1896 return Result;
1897 end Test_Lengths_Correspond;
1898
1899 -- Start of processing for Expand_Array_Equality
1900
1901 begin
1902 Ltyp := Get_Arg_Type (Lhs);
1903 Rtyp := Get_Arg_Type (Rhs);
1904
1905 -- For now, if the argument types are not the same, go to the base type,
1906 -- since the code assumes that the formals have the same type. This is
1907 -- fixable in future ???
1908
1909 if Ltyp /= Rtyp then
1910 Ltyp := Base_Type (Ltyp);
1911 Rtyp := Base_Type (Rtyp);
1912 end if;
1913
1914 -- If the array type is distinct from the type of the arguments, it
1915 -- is the full view of a private type. Apply an unchecked conversion
1916 -- to ensure that analysis of the code below succeeds.
1917
1918 if No (Etype (Lhs))
1919 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1920 then
1921 New_Lhs := OK_Convert_To (Ltyp, Lhs);
1922 else
1923 New_Lhs := Lhs;
1924 end if;
1925
1926 if No (Etype (Rhs))
1927 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1928 then
1929 New_Rhs := OK_Convert_To (Rtyp, Rhs);
1930 else
1931 New_Rhs := Rhs;
1932 end if;
1933
1934 pragma Assert (Ltyp = Rtyp);
1935 First_Idx := First_Index (Ltyp);
1936
1937 -- If optimization is enabled and the array boils down to a couple of
1938 -- consecutive elements, generate a simple conjunction of comparisons
1939 -- which should be easier to optimize by the code generator.
1940
1941 if Optimization_Level > 0
1942 and then Is_Constrained (Ltyp)
1943 and then Number_Dimensions (Ltyp) = 1
1944 and then Compile_Time_Known_Bounds (Ltyp)
1945 and then Expr_Value (Type_High_Bound (Etype (First_Idx))) =
1946 Expr_Value (Type_Low_Bound (Etype (First_Idx))) + 1
1947 then
1948 declare
1949 Ctyp : constant Entity_Id := Component_Type (Ltyp);
1950 Low_B : constant Node_Id :=
1951 Type_Low_Bound (Etype (First_Idx));
1952 High_B : constant Node_Id :=
1953 Type_High_Bound (Etype (First_Idx));
1954 L, R : Node_Id;
1955 TestL, TestH : Node_Id;
1956
1957 begin
1958 L :=
1959 Make_Indexed_Component (Loc,
1960 Prefix => New_Copy_Tree (New_Lhs),
1961 Expressions => New_List (New_Copy_Tree (Low_B)));
1962
1963 R :=
1964 Make_Indexed_Component (Loc,
1965 Prefix => New_Copy_Tree (New_Rhs),
1966 Expressions => New_List (New_Copy_Tree (Low_B)));
1967
1968 TestL := Expand_Composite_Equality
1969 (Outer_Type => Ltyp, Nod => Nod, Comp_Type => Ctyp,
1970 Lhs => L, Rhs => R);
1971
1972 L :=
1973 Make_Indexed_Component (Loc,
1974 Prefix => New_Lhs,
1975 Expressions => New_List (New_Copy_Tree (High_B)));
1976
1977 R :=
1978 Make_Indexed_Component (Loc,
1979 Prefix => New_Rhs,
1980 Expressions => New_List (New_Copy_Tree (High_B)));
1981
1982 TestH := Expand_Composite_Equality
1983 (Outer_Type => Ltyp, Nod => Nod, Comp_Type => Ctyp,
1984 Lhs => L, Rhs => R);
1985
1986 return
1987 Make_And_Then (Loc, Left_Opnd => TestL, Right_Opnd => TestH);
1988 end;
1989 end if;
1990
1991 -- Build list of formals for function
1992
1993 Formals := New_List (
1994 Make_Parameter_Specification (Loc,
1995 Defining_Identifier => A,
1996 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
1997
1998 Make_Parameter_Specification (Loc,
1999 Defining_Identifier => B,
2000 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2001
2002 Func_Name := Make_Temporary (Loc, 'E');
2003
2004 -- Build statement sequence for function
2005
2006 Func_Body :=
2007 Make_Subprogram_Body (Loc,
2008 Specification =>
2009 Make_Function_Specification (Loc,
2010 Defining_Unit_Name => Func_Name,
2011 Parameter_Specifications => Formals,
2012 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2013
2014 Declarations => Decls,
2015
2016 Handled_Statement_Sequence =>
2017 Make_Handled_Sequence_Of_Statements (Loc,
2018 Statements => New_List (
2019
2020 Make_Implicit_If_Statement (Nod,
2021 Condition => Test_Empty_Arrays,
2022 Then_Statements => New_List (
2023 Make_Simple_Return_Statement (Loc,
2024 Expression =>
2025 New_Occurrence_Of (Standard_True, Loc)))),
2026
2027 Make_Implicit_If_Statement (Nod,
2028 Condition => Test_Lengths_Correspond,
2029 Then_Statements => New_List (
2030 Make_Simple_Return_Statement (Loc,
2031 Expression => New_Occurrence_Of (Standard_False, Loc)))),
2032
2033 Handle_One_Dimension (1, First_Idx),
2034
2035 Make_Simple_Return_Statement (Loc,
2036 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2037
2038 Set_Has_Completion (Func_Name, True);
2039 Set_Is_Inlined (Func_Name);
2040
2041 Append_To (Bodies, Func_Body);
2042
2043 return
2044 Make_Function_Call (Loc,
2045 Name => New_Occurrence_Of (Func_Name, Loc),
2046 Parameter_Associations => New_List (New_Lhs, New_Rhs));
2047 end Expand_Array_Equality;
2048
2049 -----------------------------
2050 -- Expand_Boolean_Operator --
2051 -----------------------------
2052
2053 -- Note that we first get the actual subtypes of the operands, since we
2054 -- always want to deal with types that have bounds.
2055
2056 procedure Expand_Boolean_Operator (N : Node_Id) is
2057 Typ : constant Entity_Id := Etype (N);
2058
2059 begin
2060 -- Special case of bit packed array where both operands are known to be
2061 -- properly aligned. In this case we use an efficient run time routine
2062 -- to carry out the operation (see System.Bit_Ops).
2063
2064 if Is_Bit_Packed_Array (Typ)
2065 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2066 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2067 then
2068 Expand_Packed_Boolean_Operator (N);
2069 return;
2070 end if;
2071
2072 -- For the normal non-packed case, the general expansion is to build
2073 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2074 -- and then inserting it into the tree. The original operator node is
2075 -- then rewritten as a call to this function. We also use this in the
2076 -- packed case if either operand is a possibly unaligned object.
2077
2078 declare
2079 Loc : constant Source_Ptr := Sloc (N);
2080 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2081 R : Node_Id := Relocate_Node (Right_Opnd (N));
2082 Func_Body : Node_Id;
2083 Func_Name : Entity_Id;
2084
2085 begin
2086 Convert_To_Actual_Subtype (L);
2087 Convert_To_Actual_Subtype (R);
2088 Ensure_Defined (Etype (L), N);
2089 Ensure_Defined (Etype (R), N);
2090 Apply_Length_Check (R, Etype (L));
2091
2092 if Nkind (N) = N_Op_Xor then
2093 R := Duplicate_Subexpr (R);
2094 Silly_Boolean_Array_Xor_Test (N, R, Etype (L));
2095 end if;
2096
2097 if Nkind (Parent (N)) = N_Assignment_Statement
2098 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2099 then
2100 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2101
2102 elsif Nkind (Parent (N)) = N_Op_Not
2103 and then Nkind (N) = N_Op_And
2104 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
2105 and then Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2106 then
2107 return;
2108 else
2109 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2110 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2111 Insert_Action (N, Func_Body);
2112 Rewrite (N,
2113 Make_Function_Call (Loc,
2114 Name => New_Occurrence_Of (Func_Name, Loc),
2115 Parameter_Associations =>
2116 New_List (
2117 L,
2118 Make_Type_Conversion
2119 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2120 Analyze_And_Resolve (N, Typ);
2121 end if;
2122 end;
2123 end Expand_Boolean_Operator;
2124
2125 ------------------------------------------------
2126 -- Expand_Compare_Minimize_Eliminate_Overflow --
2127 ------------------------------------------------
2128
2129 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2130 Loc : constant Source_Ptr := Sloc (N);
2131
2132 Result_Type : constant Entity_Id := Etype (N);
2133 -- Capture result type (could be a derived boolean type)
2134
2135 Llo, Lhi : Uint;
2136 Rlo, Rhi : Uint;
2137
2138 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2139 -- Entity for Long_Long_Integer'Base
2140
2141 procedure Set_True;
2142 procedure Set_False;
2143 -- These procedures rewrite N with an occurrence of Standard_True or
2144 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2145
2146 ---------------
2147 -- Set_False --
2148 ---------------
2149
2150 procedure Set_False is
2151 begin
2152 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2153 Warn_On_Known_Condition (N);
2154 end Set_False;
2155
2156 --------------
2157 -- Set_True --
2158 --------------
2159
2160 procedure Set_True is
2161 begin
2162 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2163 Warn_On_Known_Condition (N);
2164 end Set_True;
2165
2166 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2167
2168 begin
2169 -- OK, this is the case we are interested in. First step is to process
2170 -- our operands using the Minimize_Eliminate circuitry which applies
2171 -- this processing to the two operand subtrees.
2172
2173 Minimize_Eliminate_Overflows
2174 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2175 Minimize_Eliminate_Overflows
2176 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2177
2178 -- See if the range information decides the result of the comparison.
2179 -- We can only do this if we in fact have full range information (which
2180 -- won't be the case if either operand is bignum at this stage).
2181
2182 if Present (Llo) and then Present (Rlo) then
2183 case N_Op_Compare (Nkind (N)) is
2184 when N_Op_Eq =>
2185 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2186 Set_True;
2187 elsif Llo > Rhi or else Lhi < Rlo then
2188 Set_False;
2189 end if;
2190
2191 when N_Op_Ge =>
2192 if Llo >= Rhi then
2193 Set_True;
2194 elsif Lhi < Rlo then
2195 Set_False;
2196 end if;
2197
2198 when N_Op_Gt =>
2199 if Llo > Rhi then
2200 Set_True;
2201 elsif Lhi <= Rlo then
2202 Set_False;
2203 end if;
2204
2205 when N_Op_Le =>
2206 if Llo > Rhi then
2207 Set_False;
2208 elsif Lhi <= Rlo then
2209 Set_True;
2210 end if;
2211
2212 when N_Op_Lt =>
2213 if Llo >= Rhi then
2214 Set_False;
2215 elsif Lhi < Rlo then
2216 Set_True;
2217 end if;
2218
2219 when N_Op_Ne =>
2220 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2221 Set_False;
2222 elsif Llo > Rhi or else Lhi < Rlo then
2223 Set_True;
2224 end if;
2225 end case;
2226
2227 -- All done if we did the rewrite
2228
2229 if Nkind (N) not in N_Op_Compare then
2230 return;
2231 end if;
2232 end if;
2233
2234 -- Otherwise, time to do the comparison
2235
2236 declare
2237 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2238 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2239
2240 begin
2241 -- If the two operands have the same signed integer type we are
2242 -- all set, nothing more to do. This is the case where either
2243 -- both operands were unchanged, or we rewrote both of them to
2244 -- be Long_Long_Integer.
2245
2246 -- Note: Entity for the comparison may be wrong, but it's not worth
2247 -- the effort to change it, since the back end does not use it.
2248
2249 if Is_Signed_Integer_Type (Ltype)
2250 and then Base_Type (Ltype) = Base_Type (Rtype)
2251 then
2252 return;
2253
2254 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2255
2256 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2257 declare
2258 Left : Node_Id := Left_Opnd (N);
2259 Right : Node_Id := Right_Opnd (N);
2260 -- Bignum references for left and right operands
2261
2262 begin
2263 if not Is_RTE (Ltype, RE_Bignum) then
2264 Left := Convert_To_Bignum (Left);
2265 elsif not Is_RTE (Rtype, RE_Bignum) then
2266 Right := Convert_To_Bignum (Right);
2267 end if;
2268
2269 -- We rewrite our node with:
2270
2271 -- do
2272 -- Bnn : Result_Type;
2273 -- declare
2274 -- M : Mark_Id := SS_Mark;
2275 -- begin
2276 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2277 -- SS_Release (M);
2278 -- end;
2279 -- in
2280 -- Bnn
2281 -- end
2282
2283 declare
2284 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2285 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2286 Ent : RE_Id;
2287
2288 begin
2289 case N_Op_Compare (Nkind (N)) is
2290 when N_Op_Eq => Ent := RE_Big_EQ;
2291 when N_Op_Ge => Ent := RE_Big_GE;
2292 when N_Op_Gt => Ent := RE_Big_GT;
2293 when N_Op_Le => Ent := RE_Big_LE;
2294 when N_Op_Lt => Ent := RE_Big_LT;
2295 when N_Op_Ne => Ent := RE_Big_NE;
2296 end case;
2297
2298 -- Insert assignment to Bnn into the bignum block
2299
2300 Insert_Before
2301 (First (Statements (Handled_Statement_Sequence (Blk))),
2302 Make_Assignment_Statement (Loc,
2303 Name => New_Occurrence_Of (Bnn, Loc),
2304 Expression =>
2305 Make_Function_Call (Loc,
2306 Name =>
2307 New_Occurrence_Of (RTE (Ent), Loc),
2308 Parameter_Associations => New_List (Left, Right))));
2309
2310 -- Now do the rewrite with expression actions
2311
2312 Rewrite (N,
2313 Make_Expression_With_Actions (Loc,
2314 Actions => New_List (
2315 Make_Object_Declaration (Loc,
2316 Defining_Identifier => Bnn,
2317 Object_Definition =>
2318 New_Occurrence_Of (Result_Type, Loc)),
2319 Blk),
2320 Expression => New_Occurrence_Of (Bnn, Loc)));
2321 Analyze_And_Resolve (N, Result_Type);
2322 end;
2323 end;
2324
2325 -- No bignums involved, but types are different, so we must have
2326 -- rewritten one of the operands as a Long_Long_Integer but not
2327 -- the other one.
2328
2329 -- If left operand is Long_Long_Integer, convert right operand
2330 -- and we are done (with a comparison of two Long_Long_Integers).
2331
2332 elsif Ltype = LLIB then
2333 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2334 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2335 return;
2336
2337 -- If right operand is Long_Long_Integer, convert left operand
2338 -- and we are done (with a comparison of two Long_Long_Integers).
2339
2340 -- This is the only remaining possibility
2341
2342 else pragma Assert (Rtype = LLIB);
2343 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2344 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2345 return;
2346 end if;
2347 end;
2348 end Expand_Compare_Minimize_Eliminate_Overflow;
2349
2350 -------------------------------
2351 -- Expand_Composite_Equality --
2352 -------------------------------
2353
2354 -- This function is only called for comparing internal fields of composite
2355 -- types when these fields are themselves composites. This is a special
2356 -- case because it is not possible to respect normal Ada visibility rules.
2357
2358 function Expand_Composite_Equality
2359 (Outer_Type : Entity_Id;
2360 Nod : Node_Id;
2361 Comp_Type : Entity_Id;
2362 Lhs : Node_Id;
2363 Rhs : Node_Id) return Node_Id
2364 is
2365 Loc : constant Source_Ptr := Sloc (Nod);
2366 Full_Type : Entity_Id;
2367 Eq_Op : Entity_Id;
2368
2369 begin
2370 if Is_Private_Type (Comp_Type) then
2371 Full_Type := Underlying_Type (Comp_Type);
2372 else
2373 Full_Type := Comp_Type;
2374 end if;
2375
2376 -- If the private type has no completion the context may be the
2377 -- expansion of a composite equality for a composite type with some
2378 -- still incomplete components. The expression will not be analyzed
2379 -- until the enclosing type is completed, at which point this will be
2380 -- properly expanded, unless there is a bona fide completion error.
2381
2382 if No (Full_Type) then
2383 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2384 end if;
2385
2386 Full_Type := Base_Type (Full_Type);
2387
2388 -- When the base type itself is private, use the full view to expand
2389 -- the composite equality.
2390
2391 if Is_Private_Type (Full_Type) then
2392 Full_Type := Underlying_Type (Full_Type);
2393 end if;
2394
2395 -- Case of tagged record types
2396
2397 if Is_Tagged_Type (Full_Type) then
2398 Eq_Op := Find_Primitive_Eq (Comp_Type);
2399 pragma Assert (Present (Eq_Op));
2400
2401 return
2402 Make_Function_Call (Loc,
2403 Name => New_Occurrence_Of (Eq_Op, Loc),
2404 Parameter_Associations =>
2405 New_List
2406 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2407 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2408
2409 -- Case of untagged record types
2410
2411 elsif Is_Record_Type (Full_Type) then
2412 -- Equality composes in Ada 2012 for untagged record types. It also
2413 -- composes for bounded strings, because they are part of the
2414 -- predefined environment (see 4.5.2(32.1/1)). We could make it
2415 -- compose for bounded strings by making them tagged, or by making
2416 -- sure all subcomponents are set to the same value, even when not
2417 -- used. Instead, we have this special case in the compiler, because
2418 -- it's more efficient.
2419
2420 if Ada_Version >= Ada_2012 or else Is_Bounded_String (Comp_Type) then
2421 declare
2422 Eq_Call : constant Node_Id :=
2423 Build_Eq_Call (Comp_Type, Loc, Lhs, Rhs);
2424
2425 begin
2426 if Present (Eq_Call) then
2427 return Eq_Call;
2428 end if;
2429 end;
2430 end if;
2431
2432 -- Check whether a TSS has been created for the type
2433
2434 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2435
2436 if Present (Eq_Op) then
2437 declare
2438 Op_Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
2439
2440 L_Exp, R_Exp : Node_Id;
2441
2442 begin
2443 -- Adjust operands if necessary to comparison type
2444
2445 if Base_Type (Full_Type) /= Base_Type (Op_Typ) then
2446 L_Exp := OK_Convert_To (Op_Typ, Lhs);
2447 R_Exp := OK_Convert_To (Op_Typ, Rhs);
2448
2449 else
2450 L_Exp := Relocate_Node (Lhs);
2451 R_Exp := Relocate_Node (Rhs);
2452 end if;
2453
2454 return
2455 Make_Function_Call (Loc,
2456 Name => New_Occurrence_Of (Eq_Op, Loc),
2457 Parameter_Associations => New_List (L_Exp, R_Exp));
2458 end;
2459
2460 else
2461 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs);
2462 end if;
2463
2464 -- Case of non-record types (always use predefined equality)
2465
2466 else
2467 -- Print a warning if there is a user-defined "=", because it can be
2468 -- surprising that the predefined "=" takes precedence over it.
2469
2470 -- Suppress the warning if the "user-defined" one is in the
2471 -- predefined library, because those are defined to compose
2472 -- properly by RM-4.5.2(32.1/1). Intrinsics also compose.
2473
2474 declare
2475 Op : constant Entity_Id := Find_Primitive_Eq (Comp_Type);
2476 begin
2477 if Warn_On_Ignored_Equality
2478 and then Present (Op)
2479 and then not In_Predefined_Unit (Base_Type (Comp_Type))
2480 and then not Is_Intrinsic_Subprogram (Op)
2481 then
2482 pragma Assert
2483 (Is_First_Subtype (Outer_Type)
2484 or else Is_Generic_Actual_Type (Outer_Type));
2485 Error_Msg_Node_2 := Comp_Type;
2486 Error_Msg_N
2487 ("?_q?""="" for type & uses predefined ""="" for }",
2488 Outer_Type);
2489 Error_Msg_Sloc := Sloc (Op);
2490 Error_Msg_N ("\?_q?""="" # is ignored here", Outer_Type);
2491 end if;
2492 end;
2493
2494 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2495 end if;
2496 end Expand_Composite_Equality;
2497
2498 ------------------------
2499 -- Expand_Concatenate --
2500 ------------------------
2501
2502 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2503 Loc : constant Source_Ptr := Sloc (Cnode);
2504
2505 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2506 -- Result type of concatenation
2507
2508 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2509 -- Component type. Elements of this component type can appear as one
2510 -- of the operands of concatenation as well as arrays.
2511
2512 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2513 -- Index subtype
2514
2515 Ityp : constant Entity_Id := Base_Type (Istyp);
2516 -- Index type. This is the base type of the index subtype, and is used
2517 -- for all computed bounds (which may be out of range of Istyp in the
2518 -- case of null ranges).
2519
2520 Artyp : Entity_Id;
2521 -- This is the type we use to do arithmetic to compute the bounds and
2522 -- lengths of operands. The choice of this type is a little subtle and
2523 -- is discussed in a separate section at the start of the body code.
2524
2525 Result_May_Be_Null : Boolean := True;
2526 -- Reset to False if at least one operand is encountered which is known
2527 -- at compile time to be non-null. Used for handling the special case
2528 -- of setting the high bound to the last operand high bound for a null
2529 -- result, thus ensuring a proper high bound in the superflat case.
2530
2531 N : constant Nat := List_Length (Opnds);
2532 -- Number of concatenation operands including possibly null operands
2533
2534 NN : Nat := 0;
2535 -- Number of operands excluding any known to be null, except that the
2536 -- last operand is always retained, in case it provides the bounds for
2537 -- a null result.
2538
2539 Opnd : Node_Id := Empty;
2540 -- Current operand being processed in the loop through operands. After
2541 -- this loop is complete, always contains the last operand (which is not
2542 -- the same as Operands (NN), since null operands are skipped).
2543
2544 -- Arrays describing the operands, only the first NN entries of each
2545 -- array are set (NN < N when we exclude known null operands).
2546
2547 Is_Fixed_Length : array (1 .. N) of Boolean;
2548 -- True if length of corresponding operand known at compile time
2549
2550 Operands : array (1 .. N) of Node_Id;
2551 -- Set to the corresponding entry in the Opnds list (but note that null
2552 -- operands are excluded, so not all entries in the list are stored).
2553
2554 Fixed_Length : array (1 .. N) of Unat;
2555 -- Set to length of operand. Entries in this array are set only if the
2556 -- corresponding entry in Is_Fixed_Length is True.
2557
2558 Max_Length : array (1 .. N) of Unat;
2559 -- Set to the maximum length of operand, or Too_Large_Length_For_Array
2560 -- if it is not known. Entries in this array are set only if the
2561 -- corresponding entry in Is_Fixed_Length is False;
2562
2563 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2564 -- Set to lower bound of operand. Either an integer literal in the case
2565 -- where the bound is known at compile time, else actual lower bound.
2566 -- The operand low bound is of type Ityp.
2567
2568 Var_Length : array (1 .. N) of Entity_Id;
2569 -- Set to an entity of type Natural that contains the length of an
2570 -- operand whose length is not known at compile time. Entries in this
2571 -- array are set only if the corresponding entry in Is_Fixed_Length
2572 -- is False. The entity is of type Artyp.
2573
2574 Aggr_Length : array (0 .. N) of Node_Id;
2575 -- The J'th entry is an expression node that represents the total length
2576 -- of operands 1 through J. It is either an integer literal node, or a
2577 -- reference to a constant entity with the right value, so it is fine
2578 -- to just do a Copy_Node to get an appropriate copy. The extra zeroth
2579 -- entry always is set to zero. The length is of type Artyp.
2580
2581 Max_Aggr_Length : Unat := Too_Large_Length_For_Array;
2582 -- Set to the maximum total length, or Too_Large_Length_For_Array at
2583 -- least if it is not known.
2584
2585 Low_Bound : Node_Id := Empty;
2586 -- A tree node representing the low bound of the result (of type Ityp).
2587 -- This is either an integer literal node, or an identifier reference to
2588 -- a constant entity initialized to the appropriate value.
2589
2590 High_Bound : Node_Id := Empty;
2591 -- A tree node representing the high bound of the result (of type Ityp)
2592
2593 Last_Opnd_Low_Bound : Node_Id := Empty;
2594 -- A tree node representing the low bound of the last operand. This
2595 -- need only be set if the result could be null. It is used for the
2596 -- special case of setting the right low bound for a null result.
2597 -- This is of type Ityp.
2598
2599 Last_Opnd_High_Bound : Node_Id := Empty;
2600 -- A tree node representing the high bound of the last operand. This
2601 -- need only be set if the result could be null. It is used for the
2602 -- special case of setting the right high bound for a null result.
2603 -- This is of type Ityp.
2604
2605 Result : Node_Id := Empty;
2606 -- Result of the concatenation (of type Ityp)
2607
2608 Actions : constant List_Id := New_List;
2609 -- Collect actions to be inserted
2610
2611 Known_Non_Null_Operand_Seen : Boolean;
2612 -- Set True during generation of the assignments of operands into
2613 -- result once an operand known to be non-null has been seen.
2614
2615 function Library_Level_Target return Boolean;
2616 -- Return True if the concatenation is within the expression of the
2617 -- declaration of a library-level object.
2618
2619 function Make_Artyp_Literal (Val : Uint) return Node_Id;
2620 -- This function makes an N_Integer_Literal node that is returned in
2621 -- analyzed form with the type set to Artyp. Importantly this literal
2622 -- is not flagged as static, so that if we do computations with it that
2623 -- result in statically detected out of range conditions, we will not
2624 -- generate error messages but instead warning messages.
2625
2626 function To_Artyp (X : Node_Id) return Node_Id;
2627 -- Given a node of type Ityp, returns the corresponding value of type
2628 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2629 -- For enum types, the Pos of the value is returned.
2630
2631 function To_Ityp (X : Node_Id) return Node_Id;
2632 -- The inverse function (uses Val in the case of enumeration types)
2633
2634 --------------------------
2635 -- Library_Level_Target --
2636 --------------------------
2637
2638 function Library_Level_Target return Boolean is
2639 P : Node_Id := Parent (Cnode);
2640
2641 begin
2642 while Present (P) loop
2643 if Nkind (P) = N_Object_Declaration then
2644 return Is_Library_Level_Entity (Defining_Identifier (P));
2645
2646 -- Prevent the search from going too far
2647
2648 elsif Is_Body_Or_Package_Declaration (P) then
2649 return False;
2650 end if;
2651
2652 P := Parent (P);
2653 end loop;
2654
2655 return False;
2656 end Library_Level_Target;
2657
2658 ------------------------
2659 -- Make_Artyp_Literal --
2660 ------------------------
2661
2662 function Make_Artyp_Literal (Val : Uint) return Node_Id is
2663 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2664 begin
2665 Set_Etype (Result, Artyp);
2666 Set_Analyzed (Result, True);
2667 Set_Is_Static_Expression (Result, False);
2668 return Result;
2669 end Make_Artyp_Literal;
2670
2671 --------------
2672 -- To_Artyp --
2673 --------------
2674
2675 function To_Artyp (X : Node_Id) return Node_Id is
2676 begin
2677 if Ityp = Base_Type (Artyp) then
2678 return X;
2679
2680 elsif Is_Enumeration_Type (Ityp) then
2681 return
2682 Make_Attribute_Reference (Loc,
2683 Prefix => New_Occurrence_Of (Ityp, Loc),
2684 Attribute_Name => Name_Pos,
2685 Expressions => New_List (X));
2686
2687 else
2688 return Convert_To (Artyp, X);
2689 end if;
2690 end To_Artyp;
2691
2692 -------------
2693 -- To_Ityp --
2694 -------------
2695
2696 function To_Ityp (X : Node_Id) return Node_Id is
2697 begin
2698 if Is_Enumeration_Type (Ityp) then
2699 return
2700 Make_Attribute_Reference (Loc,
2701 Prefix => New_Occurrence_Of (Ityp, Loc),
2702 Attribute_Name => Name_Val,
2703 Expressions => New_List (X));
2704
2705 -- Case where we will do a type conversion
2706
2707 else
2708 if Ityp = Base_Type (Artyp) then
2709 return X;
2710 else
2711 return Convert_To (Ityp, X);
2712 end if;
2713 end if;
2714 end To_Ityp;
2715
2716 -- Local variables
2717
2718 Opnd_Typ : Entity_Id;
2719 Slice_Rng : Node_Id;
2720 Subtyp_Ind : Node_Id;
2721 Subtyp_Rng : Node_Id;
2722 Ent : Entity_Id;
2723 Len : Unat;
2724 J : Nat;
2725 Clen : Node_Id;
2726 Set : Boolean;
2727
2728 -- Start of processing for Expand_Concatenate
2729
2730 begin
2731 -- Choose an appropriate computational type
2732
2733 -- We will be doing calculations of lengths and bounds in this routine
2734 -- and computing one from the other in some cases, e.g. getting the high
2735 -- bound by adding the length-1 to the low bound.
2736
2737 -- We can't just use the index type, or even its base type for this
2738 -- purpose for two reasons. First it might be an enumeration type which
2739 -- is not suitable for computations of any kind, and second it may
2740 -- simply not have enough range. For example if the index type is
2741 -- -128..+127 then lengths can be up to 256, which is out of range of
2742 -- the type.
2743
2744 -- For enumeration types, we can simply use Standard_Integer, this is
2745 -- sufficient since the actual number of enumeration literals cannot
2746 -- possibly exceed the range of integer (remember we will be doing the
2747 -- arithmetic with POS values, not representation values).
2748
2749 if Is_Enumeration_Type (Ityp) then
2750 Artyp := Standard_Integer;
2751
2752 -- For modular types, we use a 32-bit modular type for types whose size
2753 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
2754 -- identity type, and for larger unsigned types we use a 64-bit type.
2755
2756 elsif Is_Modular_Integer_Type (Ityp) then
2757 if RM_Size (Ityp) < Standard_Integer_Size then
2758 Artyp := Standard_Unsigned;
2759 elsif RM_Size (Ityp) = Standard_Integer_Size then
2760 Artyp := Ityp;
2761 else
2762 Artyp := Standard_Long_Long_Unsigned;
2763 end if;
2764
2765 -- Similar treatment for signed types
2766
2767 else
2768 if RM_Size (Ityp) < Standard_Integer_Size then
2769 Artyp := Standard_Integer;
2770 elsif RM_Size (Ityp) = Standard_Integer_Size then
2771 Artyp := Ityp;
2772 else
2773 Artyp := Standard_Long_Long_Integer;
2774 end if;
2775 end if;
2776
2777 -- Supply dummy entry at start of length array
2778
2779 Aggr_Length (0) := Make_Artyp_Literal (Uint_0);
2780
2781 -- Go through operands setting up the above arrays
2782
2783 J := 1;
2784 while J <= N loop
2785 Opnd := Remove_Head (Opnds);
2786 Opnd_Typ := Etype (Opnd);
2787
2788 -- The parent got messed up when we put the operands in a list,
2789 -- so now put back the proper parent for the saved operand, that
2790 -- is to say the concatenation node, to make sure that each operand
2791 -- is seen as a subexpression, e.g. if actions must be inserted.
2792
2793 Set_Parent (Opnd, Cnode);
2794
2795 -- Set will be True when we have setup one entry in the array
2796
2797 Set := False;
2798
2799 -- Singleton element (or character literal) case
2800
2801 if Base_Type (Opnd_Typ) = Ctyp then
2802 NN := NN + 1;
2803 Operands (NN) := Opnd;
2804 Is_Fixed_Length (NN) := True;
2805 Fixed_Length (NN) := Uint_1;
2806 Result_May_Be_Null := False;
2807
2808 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
2809 -- since we know that the result cannot be null).
2810
2811 Opnd_Low_Bound (NN) :=
2812 Make_Attribute_Reference (Loc,
2813 Prefix => New_Occurrence_Of (Istyp, Loc),
2814 Attribute_Name => Name_First);
2815
2816 Set := True;
2817
2818 -- String literal case (can only occur for strings of course)
2819
2820 elsif Nkind (Opnd) = N_String_Literal then
2821 Len := String_Literal_Length (Opnd_Typ);
2822
2823 if Len > 0 then
2824 Result_May_Be_Null := False;
2825 end if;
2826
2827 -- Capture last operand low and high bound if result could be null
2828
2829 if J = N and then Result_May_Be_Null then
2830 Last_Opnd_Low_Bound :=
2831 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2832
2833 Last_Opnd_High_Bound :=
2834 Make_Op_Subtract (Loc,
2835 Left_Opnd =>
2836 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
2837 Right_Opnd => Make_Integer_Literal (Loc, 1));
2838 end if;
2839
2840 -- Skip null string literal
2841
2842 if J < N and then Len = 0 then
2843 goto Continue;
2844 end if;
2845
2846 NN := NN + 1;
2847 Operands (NN) := Opnd;
2848 Is_Fixed_Length (NN) := True;
2849
2850 -- Set length and bounds
2851
2852 Fixed_Length (NN) := Len;
2853
2854 Opnd_Low_Bound (NN) :=
2855 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
2856
2857 Set := True;
2858
2859 -- All other cases
2860
2861 else
2862 -- Check constrained case with known bounds
2863
2864 if Is_Constrained (Opnd_Typ)
2865 and then Compile_Time_Known_Bounds (Opnd_Typ)
2866 then
2867 declare
2868 Lo, Hi : Uint;
2869
2870 begin
2871 -- Fixed length constrained array type with known at compile
2872 -- time bounds is last case of fixed length operand.
2873
2874 Get_First_Index_Bounds (Opnd_Typ, Lo, Hi);
2875 Len := UI_Max (Hi - Lo + 1, Uint_0);
2876
2877 if Len > 0 then
2878 Result_May_Be_Null := False;
2879 end if;
2880
2881 -- Capture last operand bounds if result could be null
2882
2883 if J = N and then Result_May_Be_Null then
2884 Last_Opnd_Low_Bound :=
2885 To_Ityp (Make_Integer_Literal (Loc, Lo));
2886
2887 Last_Opnd_High_Bound :=
2888 To_Ityp (Make_Integer_Literal (Loc, Hi));
2889 end if;
2890
2891 -- Exclude null length case unless last operand
2892
2893 if J < N and then Len = 0 then
2894 goto Continue;
2895 end if;
2896
2897 NN := NN + 1;
2898 Operands (NN) := Opnd;
2899 Is_Fixed_Length (NN) := True;
2900 Fixed_Length (NN) := Len;
2901
2902 Opnd_Low_Bound (NN) :=
2903 To_Ityp (Make_Integer_Literal (Loc, Lo));
2904 Set := True;
2905 end;
2906 end if;
2907
2908 -- All cases where the length is not known at compile time, or the
2909 -- special case of an operand which is known to be null but has a
2910 -- lower bound other than 1 or is other than a string type.
2911
2912 if not Set then
2913 NN := NN + 1;
2914
2915 -- Set low bound of operand and check first the constrained
2916 -- case with known bound
2917
2918 if Is_Constrained (Opnd_Typ) then
2919 declare
2920 Low_Bound : constant Node_Id :=
2921 Type_Low_Bound
2922 (Underlying_Type (Etype (First_Index (Opnd_Typ))));
2923
2924 begin
2925 if Compile_Time_Known_Value (Low_Bound) then
2926 Opnd_Low_Bound (NN) := New_Copy_Tree (Low_Bound);
2927 Set := True;
2928 end if;
2929 end;
2930 end if;
2931
2932 -- Otherwise fall back to the general expression
2933
2934 if not Set then
2935 Opnd_Low_Bound (NN) :=
2936 Make_Attribute_Reference (Loc,
2937 Prefix =>
2938 Duplicate_Subexpr (Opnd, Name_Req => True),
2939 Attribute_Name => Name_First);
2940 end if;
2941
2942 -- Capture last operand bounds if result could be null
2943
2944 if J = N and Result_May_Be_Null then
2945 Last_Opnd_Low_Bound :=
2946 Convert_To (Ityp,
2947 Make_Attribute_Reference (Loc,
2948 Prefix =>
2949 Duplicate_Subexpr (Opnd, Name_Req => True),
2950 Attribute_Name => Name_First));
2951
2952 Last_Opnd_High_Bound :=
2953 Convert_To (Ityp,
2954 Make_Attribute_Reference (Loc,
2955 Prefix =>
2956 Duplicate_Subexpr (Opnd, Name_Req => True),
2957 Attribute_Name => Name_Last));
2958 end if;
2959
2960 -- Capture length of operand in entity
2961
2962 Operands (NN) := Opnd;
2963 Is_Fixed_Length (NN) := False;
2964
2965 Var_Length (NN) := Make_Temporary (Loc, 'L');
2966
2967 -- If the operand is a slice, try to compute an upper bound for
2968 -- its length.
2969
2970 if Nkind (Opnd) = N_Slice
2971 and then Is_Constrained (Etype (Prefix (Opnd)))
2972 and then Compile_Time_Known_Bounds (Etype (Prefix (Opnd)))
2973 then
2974 declare
2975 Lo, Hi : Uint;
2976
2977 begin
2978 Get_First_Index_Bounds (Etype (Prefix (Opnd)), Lo, Hi);
2979 Max_Length (NN) := UI_Max (Hi - Lo + 1, Uint_0);
2980 end;
2981
2982 else
2983 Max_Length (NN) := Too_Large_Length_For_Array;
2984 end if;
2985
2986 Append_To (Actions,
2987 Make_Object_Declaration (Loc,
2988 Defining_Identifier => Var_Length (NN),
2989 Constant_Present => True,
2990 Object_Definition => New_Occurrence_Of (Artyp, Loc),
2991 Expression =>
2992 Make_Attribute_Reference (Loc,
2993 Prefix =>
2994 Duplicate_Subexpr (Opnd, Name_Req => True),
2995 Attribute_Name => Name_Length)));
2996 end if;
2997 end if;
2998
2999 -- Set next entry in aggregate length array
3000
3001 -- For first entry, make either integer literal for fixed length
3002 -- or a reference to the saved length for variable length.
3003
3004 if NN = 1 then
3005 if Is_Fixed_Length (1) then
3006 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3007 Max_Aggr_Length := Fixed_Length (1);
3008 else
3009 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3010 Max_Aggr_Length := Max_Length (1);
3011 end if;
3012
3013 -- If entry is fixed length and only fixed lengths so far, make
3014 -- appropriate new integer literal adding new length.
3015
3016 elsif Is_Fixed_Length (NN)
3017 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3018 then
3019 Aggr_Length (NN) :=
3020 Make_Integer_Literal (Loc,
3021 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3022 Max_Aggr_Length := Intval (Aggr_Length (NN));
3023
3024 -- All other cases, construct an addition node for the length and
3025 -- create an entity initialized to this length.
3026
3027 else
3028 Ent := Make_Temporary (Loc, 'L');
3029
3030 if Is_Fixed_Length (NN) then
3031 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3032 Max_Aggr_Length := Max_Aggr_Length + Fixed_Length (NN);
3033
3034 else
3035 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3036 Max_Aggr_Length := Max_Aggr_Length + Max_Length (NN);
3037 end if;
3038
3039 Append_To (Actions,
3040 Make_Object_Declaration (Loc,
3041 Defining_Identifier => Ent,
3042 Constant_Present => True,
3043 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3044 Expression =>
3045 Make_Op_Add (Loc,
3046 Left_Opnd => New_Copy_Tree (Aggr_Length (NN - 1)),
3047 Right_Opnd => Clen)));
3048
3049 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3050 end if;
3051
3052 <<Continue>>
3053 J := J + 1;
3054 end loop;
3055
3056 -- If we have only skipped null operands, return the last operand
3057
3058 if NN = 0 then
3059 Result := Opnd;
3060 goto Done;
3061 end if;
3062
3063 -- If we have only one non-null operand, return it and we are done.
3064 -- There is one case in which this cannot be done, and that is when
3065 -- the sole operand is of the element type, in which case it must be
3066 -- converted to an array, and the easiest way of doing that is to go
3067 -- through the normal general circuit.
3068
3069 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3070 Result := Operands (1);
3071 goto Done;
3072 end if;
3073
3074 -- Cases where we have a real concatenation
3075
3076 -- Next step is to find the low bound for the result array that we
3077 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3078
3079 -- If the ultimate ancestor of the index subtype is a constrained array
3080 -- definition, then the lower bound is that of the index subtype as
3081 -- specified by (RM 4.5.3(6)).
3082
3083 -- The right test here is to go to the root type, and then the ultimate
3084 -- ancestor is the first subtype of this root type.
3085
3086 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3087 Low_Bound :=
3088 Make_Attribute_Reference (Loc,
3089 Prefix =>
3090 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3091 Attribute_Name => Name_First);
3092
3093 -- If the first operand in the list has known length we know that
3094 -- the lower bound of the result is the lower bound of this operand.
3095
3096 elsif Is_Fixed_Length (1) then
3097 Low_Bound := Opnd_Low_Bound (1);
3098
3099 -- OK, we don't know the lower bound, we have to build a horrible
3100 -- if expression node of the form
3101
3102 -- if Cond1'Length /= 0 then
3103 -- Opnd1 low bound
3104 -- else
3105 -- if Opnd2'Length /= 0 then
3106 -- Opnd2 low bound
3107 -- else
3108 -- ...
3109
3110 -- The nesting ends either when we hit an operand whose length is known
3111 -- at compile time, or on reaching the last operand, whose low bound we
3112 -- take unconditionally whether or not it is null. It's easiest to do
3113 -- this with a recursive procedure:
3114
3115 -- We fold the common case where all the low bounds are the same
3116
3117 else
3118 declare
3119 function Get_Known_Bound (J : Nat) return Node_Id;
3120 -- Returns the lower bound determined by operands J .. NN
3121
3122 ---------------------
3123 -- Get_Known_Bound --
3124 ---------------------
3125
3126 function Get_Known_Bound (J : Nat) return Node_Id is
3127 begin
3128 if Is_Fixed_Length (J) or else J = NN then
3129 return New_Copy_Tree (Opnd_Low_Bound (J));
3130
3131 else
3132 declare
3133 Known_Bound : constant Node_Id := Get_Known_Bound (J + 1);
3134 Comparison : constant Compare_Result :=
3135 Compile_Time_Compare
3136 (Opnd_Low_Bound (J),
3137 Known_Bound,
3138 Assume_Valid => True);
3139
3140 begin
3141 if Comparison = EQ then
3142 return Known_Bound;
3143
3144 else
3145 return
3146 Make_If_Expression (Loc,
3147 Expressions => New_List (
3148
3149 Make_Op_Ne (Loc,
3150 Left_Opnd =>
3151 New_Occurrence_Of (Var_Length (J), Loc),
3152 Right_Opnd =>
3153 Make_Integer_Literal (Loc, 0)),
3154
3155 New_Copy_Tree (Opnd_Low_Bound (J)),
3156 Known_Bound));
3157 end if;
3158 end;
3159 end if;
3160 end Get_Known_Bound;
3161
3162 Known_Bound : constant Node_Id := Get_Known_Bound (1);
3163
3164 begin
3165 if Nkind (Known_Bound) /= N_If_Expression then
3166 Low_Bound := Known_Bound;
3167
3168 else
3169 Ent := Make_Temporary (Loc, 'L');
3170
3171 Append_To (Actions,
3172 Make_Object_Declaration (Loc,
3173 Defining_Identifier => Ent,
3174 Constant_Present => True,
3175 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3176 Expression => Known_Bound));
3177
3178 Low_Bound := New_Occurrence_Of (Ent, Loc);
3179 end if;
3180 end;
3181 end if;
3182
3183 pragma Assert (Present (Low_Bound));
3184
3185 -- Now we can compute the high bound as Low_Bound + Length - 1
3186
3187 if Compile_Time_Known_Value (Low_Bound)
3188 and then Nkind (Aggr_Length (NN)) = N_Integer_Literal
3189 then
3190 High_Bound :=
3191 To_Ityp
3192 (Make_Artyp_Literal
3193 (Expr_Value (Low_Bound) + Intval (Aggr_Length (NN)) - 1));
3194
3195 else
3196 High_Bound :=
3197 To_Ityp
3198 (Make_Op_Add (Loc,
3199 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3200 Right_Opnd =>
3201 Make_Op_Subtract (Loc,
3202 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3203 Right_Opnd => Make_Artyp_Literal (Uint_1))));
3204
3205 -- Note that calculation of the high bound may cause overflow in some
3206 -- very weird cases, so in the general case we need an overflow check
3207 -- on the high bound. We can avoid this for the common case of string
3208 -- types and other types whose index is Positive, since we chose a
3209 -- wider range for the arithmetic type. If checks are suppressed, we
3210 -- do not set the flag so superfluous warnings may be omitted.
3211
3212 if Istyp /= Standard_Positive
3213 and then not Overflow_Checks_Suppressed (Istyp)
3214 then
3215 Activate_Overflow_Check (High_Bound);
3216 end if;
3217 end if;
3218
3219 -- Handle the exceptional case where the result is null, in which case
3220 -- case the bounds come from the last operand (so that we get the proper
3221 -- bounds if the last operand is superflat).
3222
3223 if Result_May_Be_Null then
3224 Low_Bound :=
3225 Make_If_Expression (Loc,
3226 Expressions => New_List (
3227 Make_Op_Eq (Loc,
3228 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3229 Right_Opnd => Make_Artyp_Literal (Uint_0)),
3230 Last_Opnd_Low_Bound,
3231 Low_Bound));
3232
3233 High_Bound :=
3234 Make_If_Expression (Loc,
3235 Expressions => New_List (
3236 Make_Op_Eq (Loc,
3237 Left_Opnd => New_Copy_Tree (Aggr_Length (NN)),
3238 Right_Opnd => Make_Artyp_Literal (Uint_0)),
3239 Last_Opnd_High_Bound,
3240 High_Bound));
3241 end if;
3242
3243 -- Here is where we insert the saved up actions
3244
3245 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3246
3247 -- If the low bound is known at compile time and not the high bound, but
3248 -- we have computed a sensible upper bound for the length, then adjust
3249 -- the high bound for the subtype of the array. This will change it into
3250 -- a static subtype and thus help the code generator.
3251
3252 if Compile_Time_Known_Value (Low_Bound)
3253 and then not Compile_Time_Known_Value (High_Bound)
3254 and then Max_Aggr_Length < Too_Large_Length_For_Array
3255 then
3256 declare
3257 Known_High_Bound : constant Node_Id :=
3258 To_Ityp
3259 (Make_Artyp_Literal
3260 (Expr_Value (Low_Bound) + Max_Aggr_Length - 1));
3261
3262 begin
3263 if not Is_Out_Of_Range (Known_High_Bound, Ityp) then
3264 Slice_Rng := Make_Range (Loc, Low_Bound, High_Bound);
3265 High_Bound := Known_High_Bound;
3266
3267 else
3268 Slice_Rng := Empty;
3269 end if;
3270 end;
3271
3272 else
3273 Slice_Rng := Empty;
3274 end if;
3275
3276 Subtyp_Rng := Make_Range (Loc, Low_Bound, High_Bound);
3277
3278 -- If the result cannot be null then the range cannot be superflat
3279
3280 Set_Cannot_Be_Superflat (Subtyp_Rng, not Result_May_Be_Null);
3281
3282 -- Now we construct an array object with appropriate bounds. We mark
3283 -- the target as internal to prevent useless initialization when
3284 -- Initialize_Scalars is enabled. Also since this is the actual result
3285 -- entity, we make sure we have debug information for the result.
3286
3287 Subtyp_Ind :=
3288 Make_Subtype_Indication (Loc,
3289 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3290 Constraint =>
3291 Make_Index_Or_Discriminant_Constraint (Loc,
3292 Constraints => New_List (Subtyp_Rng)));
3293
3294 Ent := Make_Temporary (Loc, 'S');
3295 Set_Is_Internal (Ent);
3296 Set_Debug_Info_Needed (Ent);
3297
3298 -- If we are concatenating strings and the current scope already uses
3299 -- the secondary stack, allocate the result also on the secondary stack
3300 -- to avoid putting too much pressure on the primary stack.
3301
3302 -- Don't do this if -gnatd.h is set, as this will break the wrapping of
3303 -- Cnode in an Expression_With_Actions, see Expand_N_Op_Concat.
3304
3305 if Atyp = Standard_String
3306 and then Uses_Sec_Stack (Current_Scope)
3307 and then RTE_Available (RE_SS_Pool)
3308 and then not Debug_Flag_Dot_H
3309 then
3310 -- Generate:
3311 -- subtype Axx is String (<low-bound> .. <high-bound>)
3312 -- type Ayy is access Axx;
3313 -- Rxx : Ayy := new <Axx> [storage_pool = ss_pool];
3314 -- Sxx : Axx renames Rxx.all;
3315
3316 declare
3317 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
3318 Acc_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
3319
3320 Alloc : Node_Id;
3321 Temp : Entity_Id;
3322
3323 begin
3324 Insert_Action (Cnode,
3325 Make_Subtype_Declaration (Loc,
3326 Defining_Identifier => ConstrT,
3327 Subtype_Indication => Subtyp_Ind),
3328 Suppress => All_Checks);
3329
3330 Freeze_Itype (ConstrT, Cnode);
3331
3332 Insert_Action (Cnode,
3333 Make_Full_Type_Declaration (Loc,
3334 Defining_Identifier => Acc_Typ,
3335 Type_Definition =>
3336 Make_Access_To_Object_Definition (Loc,
3337 Subtype_Indication => New_Occurrence_Of (ConstrT, Loc))),
3338 Suppress => All_Checks);
3339
3340 Mutate_Ekind (Acc_Typ, E_Access_Type);
3341 Set_Associated_Storage_Pool (Acc_Typ, RTE (RE_SS_Pool));
3342
3343 Alloc :=
3344 Make_Allocator (Loc,
3345 Expression => New_Occurrence_Of (ConstrT, Loc));
3346
3347 -- This is currently done only for type String, which normally
3348 -- doesn't have default initialization, but we need to set the
3349 -- No_Initialization flag in case of either Initialize_Scalars
3350 -- or Normalize_Scalars.
3351
3352 Set_No_Initialization (Alloc);
3353
3354 Temp := Make_Temporary (Loc, 'R', Alloc);
3355 Insert_Action (Cnode,
3356 Make_Object_Declaration (Loc,
3357 Defining_Identifier => Temp,
3358 Object_Definition => New_Occurrence_Of (Acc_Typ, Loc),
3359 Expression => Alloc),
3360 Suppress => All_Checks);
3361
3362 Insert_Action (Cnode,
3363 Make_Object_Renaming_Declaration (Loc,
3364 Defining_Identifier => Ent,
3365 Subtype_Mark => New_Occurrence_Of (ConstrT, Loc),
3366 Name =>
3367 Make_Explicit_Dereference (Loc,
3368 Prefix => New_Occurrence_Of (Temp, Loc))),
3369 Suppress => All_Checks);
3370 end;
3371
3372 else
3373 -- If the bound is statically known to be out of range, we do not
3374 -- want to abort, we want a warning and a runtime constraint error.
3375 -- Note that we have arranged that the result will not be treated
3376 -- as a static constant, so we won't get an illegality during this
3377 -- insertion. We also enable checks (in particular range checks) in
3378 -- case the bounds of Subtyp_Ind are out of range.
3379
3380 Insert_Action (Cnode,
3381 Make_Object_Declaration (Loc,
3382 Defining_Identifier => Ent,
3383 Object_Definition => Subtyp_Ind));
3384 end if;
3385
3386 -- If the result of the concatenation appears as the initializing
3387 -- expression of an object declaration, we can just rename the
3388 -- result, rather than copying it.
3389
3390 Set_OK_To_Rename (Ent);
3391
3392 -- Catch the static out of range case now
3393
3394 if Raises_Constraint_Error (High_Bound)
3395 or else Is_Out_Of_Range (High_Bound, Ityp)
3396 then
3397 -- Kill warning generated for the declaration of the static out of
3398 -- range high bound, and instead generate a Constraint_Error with
3399 -- an appropriate specific message.
3400
3401 if Nkind (High_Bound) = N_Integer_Literal then
3402 Kill_Dead_Code (High_Bound);
3403 Rewrite (High_Bound, New_Copy_Tree (Low_Bound));
3404
3405 else
3406 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3407 end if;
3408
3409 Apply_Compile_Time_Constraint_Error
3410 (N => Cnode,
3411 Msg => "concatenation result upper bound out of range??",
3412 Reason => CE_Range_Check_Failed);
3413
3414 return;
3415 end if;
3416
3417 -- Now we will generate the assignments to do the actual concatenation
3418
3419 -- There is one case in which we will not do this, namely when all the
3420 -- following conditions are met:
3421
3422 -- The result type is Standard.String
3423
3424 -- There are nine or fewer retained (non-null) operands
3425
3426 -- The optimization level is -O0 or the debug flag gnatd.C is set,
3427 -- and the debug flag gnatd.c is not set.
3428
3429 -- The corresponding System.Concat_n.Str_Concat_n routine is
3430 -- available in the run time.
3431
3432 -- If all these conditions are met then we generate a call to the
3433 -- relevant concatenation routine. The purpose of this is to avoid
3434 -- undesirable code bloat at -O0.
3435
3436 -- If the concatenation is within the declaration of a library-level
3437 -- object, we call the built-in concatenation routines to prevent code
3438 -- bloat, regardless of the optimization level. This is space efficient
3439 -- and prevents linking problems when units are compiled with different
3440 -- optimization levels.
3441
3442 if Atyp = Standard_String
3443 and then NN in 2 .. 9
3444 and then (((Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3445 and then not Debug_Flag_Dot_C)
3446 or else Library_Level_Target)
3447 then
3448 declare
3449 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3450 (RE_Str_Concat_2,
3451 RE_Str_Concat_3,
3452 RE_Str_Concat_4,
3453 RE_Str_Concat_5,
3454 RE_Str_Concat_6,
3455 RE_Str_Concat_7,
3456 RE_Str_Concat_8,
3457 RE_Str_Concat_9);
3458
3459 begin
3460 if RTE_Available (RR (NN)) then
3461 declare
3462 Opnds : constant List_Id :=
3463 New_List (New_Occurrence_Of (Ent, Loc));
3464
3465 begin
3466 for J in 1 .. NN loop
3467 if Is_List_Member (Operands (J)) then
3468 Remove (Operands (J));
3469 end if;
3470
3471 if Base_Type (Etype (Operands (J))) = Ctyp then
3472 Append_To (Opnds,
3473 Make_Aggregate (Loc,
3474 Component_Associations => New_List (
3475 Make_Component_Association (Loc,
3476 Choices => New_List (
3477 Make_Integer_Literal (Loc, 1)),
3478 Expression => Operands (J)))));
3479
3480 else
3481 Append_To (Opnds, Operands (J));
3482 end if;
3483 end loop;
3484
3485 Insert_Action (Cnode,
3486 Make_Procedure_Call_Statement (Loc,
3487 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3488 Parameter_Associations => Opnds));
3489
3490 -- No assignments left to do below
3491
3492 NN := 0;
3493 end;
3494 end if;
3495 end;
3496 end if;
3497
3498 -- Not special case so generate the assignments
3499
3500 Known_Non_Null_Operand_Seen := False;
3501
3502 for J in 1 .. NN loop
3503 declare
3504 Lo : constant Node_Id :=
3505 Make_Op_Add (Loc,
3506 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3507 Right_Opnd => Aggr_Length (J - 1));
3508
3509 Hi : constant Node_Id :=
3510 Make_Op_Add (Loc,
3511 Left_Opnd => To_Artyp (New_Copy_Tree (Low_Bound)),
3512 Right_Opnd =>
3513 Make_Op_Subtract (Loc,
3514 Left_Opnd => Aggr_Length (J),
3515 Right_Opnd => Make_Artyp_Literal (Uint_1)));
3516
3517 begin
3518 -- Singleton case, simple assignment
3519
3520 if Base_Type (Etype (Operands (J))) = Ctyp then
3521 Known_Non_Null_Operand_Seen := True;
3522 Insert_Action (Cnode,
3523 Make_Assignment_Statement (Loc,
3524 Name =>
3525 Make_Indexed_Component (Loc,
3526 Prefix => New_Occurrence_Of (Ent, Loc),
3527 Expressions => New_List (To_Ityp (Lo))),
3528 Expression => Operands (J)),
3529 Suppress => All_Checks);
3530
3531 -- Array case, slice assignment, skipped when argument is fixed
3532 -- length and known to be null.
3533
3534 elsif not Is_Fixed_Length (J) or else Fixed_Length (J) > 0 then
3535 declare
3536 Assign : Node_Id :=
3537 Make_Assignment_Statement (Loc,
3538 Name =>
3539 Make_Slice (Loc,
3540 Prefix =>
3541 New_Occurrence_Of (Ent, Loc),
3542 Discrete_Range =>
3543 Make_Range (Loc,
3544 Low_Bound => To_Ityp (Lo),
3545 High_Bound => To_Ityp (Hi))),
3546 Expression => Operands (J));
3547 begin
3548 if Is_Fixed_Length (J) then
3549 Known_Non_Null_Operand_Seen := True;
3550
3551 elsif not Known_Non_Null_Operand_Seen then
3552
3553 -- Here if operand length is not statically known and no
3554 -- operand known to be non-null has been processed yet.
3555 -- If operand length is 0, we do not need to perform the
3556 -- assignment, and we must avoid the evaluation of the
3557 -- high bound of the slice, since it may underflow if the
3558 -- low bound is Ityp'First.
3559
3560 Assign :=
3561 Make_Implicit_If_Statement (Cnode,
3562 Condition =>
3563 Make_Op_Ne (Loc,
3564 Left_Opnd =>
3565 New_Occurrence_Of (Var_Length (J), Loc),
3566 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3567 Then_Statements => New_List (Assign));
3568 end if;
3569
3570 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3571 end;
3572 end if;
3573 end;
3574 end loop;
3575
3576 -- Finally we build the result, which is either a direct reference to
3577 -- the array object or a slice of it.
3578
3579 Result := New_Occurrence_Of (Ent, Loc);
3580
3581 if Present (Slice_Rng) then
3582 Result := Make_Slice (Loc, Result, Slice_Rng);
3583 end if;
3584
3585 <<Done>>
3586 pragma Assert (Present (Result));
3587 Rewrite (Cnode, Result);
3588 Analyze_And_Resolve (Cnode, Atyp);
3589 end Expand_Concatenate;
3590
3591 ---------------------------------------------------
3592 -- Expand_Membership_Minimize_Eliminate_Overflow --
3593 ---------------------------------------------------
3594
3595 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3596 pragma Assert (Nkind (N) = N_In);
3597 -- Despite the name, this routine applies only to N_In, not to
3598 -- N_Not_In. The latter is always rewritten as not (X in Y).
3599
3600 Result_Type : constant Entity_Id := Etype (N);
3601 -- Capture result type, may be a derived boolean type
3602
3603 Loc : constant Source_Ptr := Sloc (N);
3604 Lop : constant Node_Id := Left_Opnd (N);
3605 Rop : constant Node_Id := Right_Opnd (N);
3606
3607 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3608 -- is thus tempting to capture these values, but due to the rewrites
3609 -- that occur as a result of overflow checking, these values change
3610 -- as we go along, and it is safe just to always use Etype explicitly.
3611
3612 Restype : constant Entity_Id := Etype (N);
3613 -- Save result type
3614
3615 Lo, Hi : Uint;
3616 -- Bounds in Minimize calls, not used currently
3617
3618 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3619 -- Entity for Long_Long_Integer'Base
3620
3621 begin
3622 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3623
3624 -- If right operand is a subtype name, and the subtype name has no
3625 -- predicate, then we can just replace the right operand with an
3626 -- explicit range T'First .. T'Last, and use the explicit range code.
3627
3628 if Nkind (Rop) /= N_Range
3629 and then No (Predicate_Function (Etype (Rop)))
3630 then
3631 declare
3632 Rtyp : constant Entity_Id := Etype (Rop);
3633 begin
3634 Rewrite (Rop,
3635 Make_Range (Loc,
3636 Low_Bound =>
3637 Make_Attribute_Reference (Loc,
3638 Attribute_Name => Name_First,
3639 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3640 High_Bound =>
3641 Make_Attribute_Reference (Loc,
3642 Attribute_Name => Name_Last,
3643 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3644 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3645 end;
3646 end if;
3647
3648 -- Here for the explicit range case. Note that the bounds of the range
3649 -- have not been processed for minimized or eliminated checks.
3650
3651 if Nkind (Rop) = N_Range then
3652 Minimize_Eliminate_Overflows
3653 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3654 Minimize_Eliminate_Overflows
3655 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3656
3657 -- We have A in B .. C, treated as A >= B and then A <= C
3658
3659 -- Bignum case
3660
3661 if Is_RTE (Etype (Lop), RE_Bignum)
3662 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3663 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3664 then
3665 declare
3666 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3667 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3668 L : constant Entity_Id :=
3669 Make_Defining_Identifier (Loc, Name_uL);
3670 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3671 Lbound : constant Node_Id :=
3672 Convert_To_Bignum (Low_Bound (Rop));
3673 Hbound : constant Node_Id :=
3674 Convert_To_Bignum (High_Bound (Rop));
3675
3676 -- Now we rewrite the membership test node to look like
3677
3678 -- do
3679 -- Bnn : Result_Type;
3680 -- declare
3681 -- M : Mark_Id := SS_Mark;
3682 -- L : Bignum := Lopnd;
3683 -- begin
3684 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3685 -- SS_Release (M);
3686 -- end;
3687 -- in
3688 -- Bnn
3689 -- end
3690
3691 begin
3692 -- Insert declaration of L into declarations of bignum block
3693
3694 Insert_After
3695 (Last (Declarations (Blk)),
3696 Make_Object_Declaration (Loc,
3697 Defining_Identifier => L,
3698 Object_Definition =>
3699 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3700 Expression => Lopnd));
3701
3702 -- Insert assignment to Bnn into expressions of bignum block
3703
3704 Insert_Before
3705 (First (Statements (Handled_Statement_Sequence (Blk))),
3706 Make_Assignment_Statement (Loc,
3707 Name => New_Occurrence_Of (Bnn, Loc),
3708 Expression =>
3709 Make_And_Then (Loc,
3710 Left_Opnd =>
3711 Make_Function_Call (Loc,
3712 Name =>
3713 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3714 Parameter_Associations => New_List (
3715 New_Occurrence_Of (L, Loc),
3716 Lbound)),
3717
3718 Right_Opnd =>
3719 Make_Function_Call (Loc,
3720 Name =>
3721 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3722 Parameter_Associations => New_List (
3723 New_Occurrence_Of (L, Loc),
3724 Hbound)))));
3725
3726 -- Now rewrite the node
3727
3728 Rewrite (N,
3729 Make_Expression_With_Actions (Loc,
3730 Actions => New_List (
3731 Make_Object_Declaration (Loc,
3732 Defining_Identifier => Bnn,
3733 Object_Definition =>
3734 New_Occurrence_Of (Result_Type, Loc)),
3735 Blk),
3736 Expression => New_Occurrence_Of (Bnn, Loc)));
3737 Analyze_And_Resolve (N, Result_Type);
3738 return;
3739 end;
3740
3741 -- Here if no bignums around
3742
3743 else
3744 -- Case where types are all the same
3745
3746 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3747 and then
3748 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3749 then
3750 null;
3751
3752 -- If types are not all the same, it means that we have rewritten
3753 -- at least one of them to be of type Long_Long_Integer, and we
3754 -- will convert the other operands to Long_Long_Integer.
3755
3756 else
3757 Convert_To_And_Rewrite (LLIB, Lop);
3758 Set_Analyzed (Lop, False);
3759 Analyze_And_Resolve (Lop, LLIB);
3760
3761 -- For the right operand, avoid unnecessary recursion into
3762 -- this routine, we know that overflow is not possible.
3763
3764 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3765 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3766 Set_Analyzed (Rop, False);
3767 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3768 end if;
3769
3770 -- Now the three operands are of the same signed integer type,
3771 -- so we can use the normal expansion routine for membership,
3772 -- setting the flag to prevent recursion into this procedure.
3773
3774 Set_No_Minimize_Eliminate (N);
3775 Expand_N_In (N);
3776 end if;
3777
3778 -- Right operand is a subtype name and the subtype has a predicate. We
3779 -- have to make sure the predicate is checked, and for that we need to
3780 -- use the standard N_In circuitry with appropriate types.
3781
3782 else
3783 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3784
3785 -- If types are "right", just call Expand_N_In preventing recursion
3786
3787 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3788 Set_No_Minimize_Eliminate (N);
3789 Expand_N_In (N);
3790
3791 -- Bignum case
3792
3793 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3794
3795 -- For X in T, we want to rewrite our node as
3796
3797 -- do
3798 -- Bnn : Result_Type;
3799
3800 -- declare
3801 -- M : Mark_Id := SS_Mark;
3802 -- Lnn : Long_Long_Integer'Base
3803 -- Nnn : Bignum;
3804
3805 -- begin
3806 -- Nnn := X;
3807
3808 -- if not Bignum_In_LLI_Range (Nnn) then
3809 -- Bnn := False;
3810 -- else
3811 -- Lnn := From_Bignum (Nnn);
3812 -- Bnn :=
3813 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3814 -- and then T'Base (Lnn) in T;
3815 -- end if;
3816
3817 -- SS_Release (M);
3818 -- end
3819 -- in
3820 -- Bnn
3821 -- end
3822
3823 -- A bit gruesome, but there doesn't seem to be a simpler way
3824
3825 declare
3826 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3827 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3828 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3829 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3830 T : constant Entity_Id := Etype (Rop);
3831 TB : constant Entity_Id := Base_Type (T);
3832 Nin : Node_Id;
3833
3834 begin
3835 -- Mark the last membership operation to prevent recursion
3836
3837 Nin :=
3838 Make_In (Loc,
3839 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
3840 Right_Opnd => New_Occurrence_Of (T, Loc));
3841 Set_No_Minimize_Eliminate (Nin);
3842
3843 -- Now decorate the block
3844
3845 Insert_After
3846 (Last (Declarations (Blk)),
3847 Make_Object_Declaration (Loc,
3848 Defining_Identifier => Lnn,
3849 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3850
3851 Insert_After
3852 (Last (Declarations (Blk)),
3853 Make_Object_Declaration (Loc,
3854 Defining_Identifier => Nnn,
3855 Object_Definition =>
3856 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3857
3858 Insert_List_Before
3859 (First (Statements (Handled_Statement_Sequence (Blk))),
3860 New_List (
3861 Make_Assignment_Statement (Loc,
3862 Name => New_Occurrence_Of (Nnn, Loc),
3863 Expression => Relocate_Node (Lop)),
3864
3865 Make_Implicit_If_Statement (N,
3866 Condition =>
3867 Make_Op_Not (Loc,
3868 Right_Opnd =>
3869 Make_Function_Call (Loc,
3870 Name =>
3871 New_Occurrence_Of
3872 (RTE (RE_Bignum_In_LLI_Range), Loc),
3873 Parameter_Associations => New_List (
3874 New_Occurrence_Of (Nnn, Loc)))),
3875
3876 Then_Statements => New_List (
3877 Make_Assignment_Statement (Loc,
3878 Name => New_Occurrence_Of (Bnn, Loc),
3879 Expression =>
3880 New_Occurrence_Of (Standard_False, Loc))),
3881
3882 Else_Statements => New_List (
3883 Make_Assignment_Statement (Loc,
3884 Name => New_Occurrence_Of (Lnn, Loc),
3885 Expression =>
3886 Make_Function_Call (Loc,
3887 Name =>
3888 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
3889 Parameter_Associations => New_List (
3890 New_Occurrence_Of (Nnn, Loc)))),
3891
3892 Make_Assignment_Statement (Loc,
3893 Name => New_Occurrence_Of (Bnn, Loc),
3894 Expression =>
3895 Make_And_Then (Loc,
3896 Left_Opnd =>
3897 Make_In (Loc,
3898 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
3899 Right_Opnd =>
3900 Make_Range (Loc,
3901 Low_Bound =>
3902 Convert_To (LLIB,
3903 Make_Attribute_Reference (Loc,
3904 Attribute_Name => Name_First,
3905 Prefix =>
3906 New_Occurrence_Of (TB, Loc))),
3907
3908 High_Bound =>
3909 Convert_To (LLIB,
3910 Make_Attribute_Reference (Loc,
3911 Attribute_Name => Name_Last,
3912 Prefix =>
3913 New_Occurrence_Of (TB, Loc))))),
3914
3915 Right_Opnd => Nin))))));
3916
3917 -- Now we can do the rewrite
3918
3919 Rewrite (N,
3920 Make_Expression_With_Actions (Loc,
3921 Actions => New_List (
3922 Make_Object_Declaration (Loc,
3923 Defining_Identifier => Bnn,
3924 Object_Definition =>
3925 New_Occurrence_Of (Result_Type, Loc)),
3926 Blk),
3927 Expression => New_Occurrence_Of (Bnn, Loc)));
3928 Analyze_And_Resolve (N, Result_Type);
3929 return;
3930 end;
3931
3932 -- Not bignum case, but types don't match (this means we rewrote the
3933 -- left operand to be Long_Long_Integer).
3934
3935 else
3936 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
3937
3938 -- We rewrite the membership test as (where T is the type with
3939 -- the predicate, i.e. the type of the right operand)
3940
3941 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3942 -- and then T'Base (Lop) in T
3943
3944 declare
3945 T : constant Entity_Id := Etype (Rop);
3946 TB : constant Entity_Id := Base_Type (T);
3947 Nin : Node_Id;
3948
3949 begin
3950 -- The last membership test is marked to prevent recursion
3951
3952 Nin :=
3953 Make_In (Loc,
3954 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
3955 Right_Opnd => New_Occurrence_Of (T, Loc));
3956 Set_No_Minimize_Eliminate (Nin);
3957
3958 -- Now do the rewrite
3959
3960 Rewrite (N,
3961 Make_And_Then (Loc,
3962 Left_Opnd =>
3963 Make_In (Loc,
3964 Left_Opnd => Lop,
3965 Right_Opnd =>
3966 Make_Range (Loc,
3967 Low_Bound =>
3968 Convert_To (LLIB,
3969 Make_Attribute_Reference (Loc,
3970 Attribute_Name => Name_First,
3971 Prefix =>
3972 New_Occurrence_Of (TB, Loc))),
3973 High_Bound =>
3974 Convert_To (LLIB,
3975 Make_Attribute_Reference (Loc,
3976 Attribute_Name => Name_Last,
3977 Prefix =>
3978 New_Occurrence_Of (TB, Loc))))),
3979 Right_Opnd => Nin));
3980 Set_Analyzed (N, False);
3981 Analyze_And_Resolve (N, Restype);
3982 end;
3983 end if;
3984 end if;
3985 end Expand_Membership_Minimize_Eliminate_Overflow;
3986
3987 ---------------------------------
3988 -- Expand_Nonbinary_Modular_Op --
3989 ---------------------------------
3990
3991 procedure Expand_Nonbinary_Modular_Op (N : Node_Id) is
3992 Loc : constant Source_Ptr := Sloc (N);
3993 Typ : constant Entity_Id := Etype (N);
3994
3995 procedure Expand_Modular_Addition;
3996 -- Expand the modular addition, handling the special case of adding a
3997 -- constant.
3998
3999 procedure Expand_Modular_Op;
4000 -- Compute the general rule: (lhs OP rhs) mod Modulus
4001
4002 procedure Expand_Modular_Subtraction;
4003 -- Expand the modular addition, handling the special case of subtracting
4004 -- a constant.
4005
4006 -----------------------------
4007 -- Expand_Modular_Addition --
4008 -----------------------------
4009
4010 procedure Expand_Modular_Addition is
4011 begin
4012 -- If this is not the addition of a constant or else restriction
4013 -- No_Implicit_Conditionals is in effect, then compute it using
4014 -- the general rule: (lhs + rhs) mod Modulus.
4015
4016 if Nkind (Right_Opnd (N)) /= N_Integer_Literal
4017 or else Restriction_Active (No_Implicit_Conditionals)
4018 then
4019 Expand_Modular_Op;
4020
4021 -- If this is an addition of a constant, convert it to a subtraction
4022 -- plus a conditional expression since we can compute it faster than
4023 -- computing the modulus.
4024
4025 -- modMinusRhs = Modulus - rhs
4026 -- if lhs < modMinusRhs then lhs + rhs
4027 -- else lhs - modMinusRhs
4028
4029 else
4030 declare
4031 Mod_Minus_Right : constant Uint :=
4032 Modulus (Typ) - Intval (Right_Opnd (N));
4033
4034 Cond_Expr : Node_Id;
4035 Then_Expr : Node_Id;
4036 Else_Expr : Node_Id;
4037
4038 begin
4039 -- To prevent spurious visibility issues, convert all
4040 -- operands to Standard.Unsigned.
4041
4042 Cond_Expr :=
4043 Make_Op_Lt (Loc,
4044 Left_Opnd =>
4045 Unchecked_Convert_To (Standard_Unsigned,
4046 New_Copy_Tree (Left_Opnd (N))),
4047 Right_Opnd =>
4048 Make_Integer_Literal (Loc, Mod_Minus_Right));
4049
4050 Then_Expr :=
4051 Make_Op_Add (Loc,
4052 Left_Opnd =>
4053 Unchecked_Convert_To (Standard_Unsigned,
4054 New_Copy_Tree (Left_Opnd (N))),
4055 Right_Opnd =>
4056 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4057
4058 Else_Expr :=
4059 Make_Op_Subtract (Loc,
4060 Left_Opnd =>
4061 Unchecked_Convert_To (Standard_Unsigned,
4062 New_Copy_Tree (Left_Opnd (N))),
4063 Right_Opnd =>
4064 Make_Integer_Literal (Loc, Mod_Minus_Right));
4065
4066 Rewrite (N,
4067 Unchecked_Convert_To (Typ,
4068 Make_If_Expression (Loc,
4069 Expressions =>
4070 New_List (Cond_Expr, Then_Expr, Else_Expr))));
4071 end;
4072 end if;
4073 end Expand_Modular_Addition;
4074
4075 -----------------------
4076 -- Expand_Modular_Op --
4077 -----------------------
4078
4079 procedure Expand_Modular_Op is
4080 -- We will convert to another type (not a nonbinary-modulus modular
4081 -- type), evaluate the op in that representation, reduce the result,
4082 -- and convert back to the original type. This means that the
4083 -- back end does not have to deal with nonbinary-modulus ops.
4084
4085 Mod_Expr : Node_Id;
4086 Op_Expr : Node_Id;
4087 Target_Type : Entity_Id;
4088
4089 begin
4090 -- Select a target type that is large enough to avoid spurious
4091 -- intermediate overflow on pre-reduction computation (for
4092 -- correctness) but is no larger than is needed (for performance).
4093
4094 declare
4095 Required_Size : Uint := RM_Size (Etype (N));
4096
4097 begin
4098 case Nkind (N) is
4099 when N_Op_Add | N_Op_Subtract =>
4100 -- For example, if modulus is 255 then RM_Size will be 8
4101 -- and the range of possible values (before reduction) will
4102 -- be 0 .. 508; that range requires 9 bits.
4103 Required_Size := Required_Size + 1;
4104
4105 when N_Op_Multiply =>
4106 -- For example, if modulus is 255 then RM_Size will be 8
4107 -- and the range of possible values (before reduction) will
4108 -- be 0 .. 64,516; that range requires 16 bits.
4109 Required_Size := Required_Size * 2;
4110
4111 when others =>
4112 null;
4113 end case;
4114
4115 Target_Type := Small_Integer_Type_For (Required_Size, Uns => True);
4116 pragma Assert (Present (Target_Type));
4117 end;
4118
4119 Op_Expr := New_Op_Node (Nkind (N), Loc);
4120 Set_Left_Opnd (Op_Expr,
4121 Unchecked_Convert_To (Target_Type, New_Copy_Tree (Left_Opnd (N))));
4122 Set_Right_Opnd (Op_Expr,
4123 Unchecked_Convert_To (Target_Type, New_Copy_Tree (Right_Opnd (N))));
4124
4125 -- ??? Why do this stuff for some ops and not others?
4126 if Nkind (N) not in N_Op_And | N_Op_Or | N_Op_Xor then
4127
4128 -- Link this node to the tree to analyze it
4129
4130 -- If the parent node is an expression with actions we link it to
4131 -- N since otherwise Force_Evaluation cannot identify if this node
4132 -- comes from the Expression and rejects generating the temporary.
4133
4134 if Nkind (Parent (N)) = N_Expression_With_Actions then
4135 Set_Parent (Op_Expr, N);
4136
4137 -- Common case
4138
4139 else
4140 Set_Parent (Op_Expr, Parent (N));
4141 end if;
4142
4143 Analyze (Op_Expr);
4144
4145 -- Force generating a temporary because in the expansion of this
4146 -- expression we may generate code that performs this computation
4147 -- several times.
4148
4149 Force_Evaluation (Op_Expr, Mode => Strict);
4150 end if;
4151
4152 -- Unconditionally add the modulus to the result for a subtraction,
4153 -- this gets rid of all its peculiarities by cancelling out the
4154 -- addition of the binary modulus in the case where the subtraction
4155 -- wraps around in Target_Type.
4156
4157 if Nkind (N) = N_Op_Subtract then
4158 Op_Expr :=
4159 Make_Op_Add (Loc,
4160 Left_Opnd => Op_Expr,
4161 Right_Opnd => Make_Integer_Literal (Loc, Modulus (Typ)));
4162 end if;
4163
4164 Mod_Expr :=
4165 Make_Op_Mod (Loc,
4166 Left_Opnd => Op_Expr,
4167 Right_Opnd => Make_Integer_Literal (Loc, Modulus (Typ)));
4168
4169 Rewrite (N, Unchecked_Convert_To (Typ, Mod_Expr));
4170 end Expand_Modular_Op;
4171
4172 --------------------------------
4173 -- Expand_Modular_Subtraction --
4174 --------------------------------
4175
4176 procedure Expand_Modular_Subtraction is
4177 begin
4178 -- If this is not the addition of a constant or else restriction
4179 -- No_Implicit_Conditionals is in effect, then compute it using
4180 -- the general rule: (lhs - rhs) mod Modulus.
4181
4182 if Nkind (Right_Opnd (N)) /= N_Integer_Literal
4183 or else Restriction_Active (No_Implicit_Conditionals)
4184 then
4185 Expand_Modular_Op;
4186
4187 -- If this is an addition of a constant, convert it to a subtraction
4188 -- plus a conditional expression since we can compute it faster than
4189 -- computing the modulus.
4190
4191 -- modMinusRhs = Modulus - rhs
4192 -- if lhs < rhs then lhs + modMinusRhs
4193 -- else lhs - rhs
4194
4195 else
4196 declare
4197 Mod_Minus_Right : constant Uint :=
4198 Modulus (Typ) - Intval (Right_Opnd (N));
4199
4200 Cond_Expr : Node_Id;
4201 Then_Expr : Node_Id;
4202 Else_Expr : Node_Id;
4203
4204 begin
4205 Cond_Expr :=
4206 Make_Op_Lt (Loc,
4207 Left_Opnd =>
4208 Unchecked_Convert_To (Standard_Unsigned,
4209 New_Copy_Tree (Left_Opnd (N))),
4210 Right_Opnd =>
4211 Make_Integer_Literal (Loc, Intval (Right_Opnd (N))));
4212
4213 Then_Expr :=
4214 Make_Op_Add (Loc,
4215 Left_Opnd =>
4216 Unchecked_Convert_To (Standard_Unsigned,
4217 New_Copy_Tree (Left_Opnd (N))),
4218 Right_Opnd =>
4219 Make_Integer_Literal (Loc, Mod_Minus_Right));
4220
4221 Else_Expr :=
4222 Make_Op_Subtract (Loc,
4223 Left_Opnd =>
4224 Unchecked_Convert_To (Standard_Unsigned,
4225 New_Copy_Tree (Left_Opnd (N))),
4226 Right_Opnd =>
4227 Unchecked_Convert_To (Standard_Unsigned,
4228 New_Copy_Tree (Right_Opnd (N))));
4229
4230 Rewrite (N,
4231 Unchecked_Convert_To (Typ,
4232 Make_If_Expression (Loc,
4233 Expressions =>
4234 New_List (Cond_Expr, Then_Expr, Else_Expr))));
4235 end;
4236 end if;
4237 end Expand_Modular_Subtraction;
4238
4239 -- Start of processing for Expand_Nonbinary_Modular_Op
4240
4241 begin
4242 -- No action needed if we have a binary modular operand
4243
4244 if not Non_Binary_Modulus (Typ) then
4245 return;
4246 end if;
4247
4248 case Nkind (N) is
4249 when N_Op_Add =>
4250 -- No action needed if front-end expansion is not required and
4251 -- restriction No_Implicit_Conditionals is not in effect.
4252
4253 if not Expand_Nonbinary_Modular_Ops
4254 and then not Restriction_Active (No_Implicit_Conditionals)
4255 then
4256 return;
4257 end if;
4258
4259 Expand_Modular_Addition;
4260
4261 when N_Op_Subtract =>
4262 -- No action needed if front-end expansion is not required and
4263 -- restriction No_Implicit_Conditionals is not in effect.
4264
4265 if not Expand_Nonbinary_Modular_Ops
4266 and then not Restriction_Active (No_Implicit_Conditionals)
4267 then
4268 return;
4269 end if;
4270
4271 Expand_Modular_Subtraction;
4272
4273 when N_Op_Minus =>
4274 -- No action needed if front-end expansion is not required and
4275 -- restriction No_Implicit_Conditionals is not in effect.
4276
4277 if not Expand_Nonbinary_Modular_Ops
4278 and then not Restriction_Active (No_Implicit_Conditionals)
4279 then
4280 return;
4281 end if;
4282
4283 -- Expand -expr into (0 - expr)
4284
4285 Rewrite (N,
4286 Make_Op_Subtract (Loc,
4287 Left_Opnd => Make_Integer_Literal (Loc, 0),
4288 Right_Opnd => Right_Opnd (N)));
4289 Analyze_And_Resolve (N, Typ);
4290
4291 when others =>
4292 -- No action needed only if front-end expansion is not required
4293 -- because we assume that logical and multiplicative operations
4294 -- do not involve implicit conditionals.
4295
4296 if not Expand_Nonbinary_Modular_Ops then
4297 return;
4298 end if;
4299
4300 Expand_Modular_Op;
4301 end case;
4302
4303 Analyze_And_Resolve (N, Typ);
4304 end Expand_Nonbinary_Modular_Op;
4305
4306 ------------------------
4307 -- Expand_N_Allocator --
4308 ------------------------
4309
4310 procedure Expand_N_Allocator (N : Node_Id) is
4311 Loc : constant Source_Ptr := Sloc (N);
4312 PtrT : constant Entity_Id := Etype (N);
4313 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4314 Etyp : constant Entity_Id := Etype (Expression (N));
4315
4316 procedure Rewrite_Coextension (N : Node_Id);
4317 -- Static coextensions have the same lifetime as the entity they
4318 -- constrain. Such occurrences can be rewritten as aliased objects
4319 -- and their unrestricted access used instead of the coextension.
4320
4321 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4322 -- Given a constrained array type E, returns a node representing the
4323 -- code to compute a close approximation of the size in storage elements
4324 -- for the given type; for indexes that are modular types we compute
4325 -- 'Last - First (instead of 'Length) because for large arrays computing
4326 -- 'Last -'First + 1 causes overflow. This is done without using the
4327 -- attribute 'Size_In_Storage_Elements (which malfunctions for large
4328 -- sizes ???).
4329
4330 -------------------------
4331 -- Rewrite_Coextension --
4332 -------------------------
4333
4334 procedure Rewrite_Coextension (N : Node_Id) is
4335 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4336 Temp_Decl : Node_Id;
4337
4338 begin
4339 -- Generate:
4340 -- Cnn : aliased Etyp;
4341
4342 Temp_Decl :=
4343 Make_Object_Declaration (Loc,
4344 Defining_Identifier => Temp_Id,
4345 Aliased_Present => True,
4346 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4347
4348 if Nkind (Expression (N)) = N_Qualified_Expression then
4349 Set_Expression (Temp_Decl, Expression (Expression (N)));
4350 end if;
4351
4352 Insert_Action (N, Temp_Decl);
4353 Rewrite (N,
4354 Make_Attribute_Reference (Loc,
4355 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4356 Attribute_Name => Name_Unrestricted_Access));
4357
4358 Analyze_And_Resolve (N, PtrT);
4359 end Rewrite_Coextension;
4360
4361 ------------------------------
4362 -- Size_In_Storage_Elements --
4363 ------------------------------
4364
4365 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4366 Idx : Node_Id := First_Index (E);
4367 Len : Node_Id := Empty;
4368 Res : Node_Id := Empty;
4369
4370 begin
4371 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4372 -- However, the reason for the existence of this function is to
4373 -- construct a test for sizes too large, which means near the 32-bit
4374 -- limit on a 32-bit machine, and precisely the trouble is that we
4375 -- get overflows when sizes are greater than 2**31.
4376
4377 -- So what we end up doing for array types is to use the expression:
4378
4379 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4380
4381 -- which avoids this problem. All this is a bit bogus, but it does
4382 -- mean we catch common cases of trying to allocate arrays that are
4383 -- too large, and which in the absence of a check results in
4384 -- undetected chaos ???
4385
4386 for J in 1 .. Number_Dimensions (E) loop
4387
4388 if not Is_Modular_Integer_Type (Etype (Idx)) then
4389 Len :=
4390 Make_Attribute_Reference (Loc,
4391 Prefix => New_Occurrence_Of (E, Loc),
4392 Attribute_Name => Name_Length,
4393 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4394
4395 -- For indexes that are modular types we cannot generate code to
4396 -- compute 'Length since for large arrays 'Last -'First + 1 causes
4397 -- overflow; therefore we compute 'Last - 'First (which is not the
4398 -- exact number of components but it is valid for the purpose of
4399 -- this runtime check on 32-bit targets).
4400
4401 else
4402 declare
4403 Len_Minus_1_Expr : Node_Id;
4404 Test_Gt : Node_Id;
4405
4406 begin
4407 Test_Gt :=
4408 Make_Op_Gt (Loc,
4409 Make_Attribute_Reference (Loc,
4410 Prefix => New_Occurrence_Of (E, Loc),
4411 Attribute_Name => Name_Last,
4412 Expressions =>
4413 New_List (Make_Integer_Literal (Loc, J))),
4414 Make_Attribute_Reference (Loc,
4415 Prefix => New_Occurrence_Of (E, Loc),
4416 Attribute_Name => Name_First,
4417 Expressions =>
4418 New_List (Make_Integer_Literal (Loc, J))));
4419
4420 Len_Minus_1_Expr :=
4421 Convert_To (Standard_Unsigned,
4422 Make_Op_Subtract (Loc,
4423 Make_Attribute_Reference (Loc,
4424 Prefix => New_Occurrence_Of (E, Loc),
4425 Attribute_Name => Name_Last,
4426 Expressions =>
4427 New_List (Make_Integer_Literal (Loc, J))),
4428 Make_Attribute_Reference (Loc,
4429 Prefix => New_Occurrence_Of (E, Loc),
4430 Attribute_Name => Name_First,
4431 Expressions =>
4432 New_List (Make_Integer_Literal (Loc, J)))));
4433
4434 -- Handle superflat arrays, i.e. arrays with such bounds as
4435 -- 4 .. 2, to ensure that the result is correct.
4436
4437 -- Generate:
4438 -- (if X'Last > X'First then X'Last - X'First else 0)
4439
4440 Len :=
4441 Make_If_Expression (Loc,
4442 Expressions => New_List (
4443 Test_Gt,
4444 Len_Minus_1_Expr,
4445 Make_Integer_Literal (Loc, Uint_0)));
4446 end;
4447 end if;
4448
4449 if J = 1 then
4450 Res := Len;
4451
4452 else
4453 pragma Assert (Present (Res));
4454 Res :=
4455 Make_Op_Multiply (Loc,
4456 Left_Opnd => Res,
4457 Right_Opnd => Len);
4458 end if;
4459
4460 Next_Index (Idx);
4461 end loop;
4462
4463 return
4464 Make_Op_Multiply (Loc,
4465 Left_Opnd => Len,
4466 Right_Opnd =>
4467 Make_Attribute_Reference (Loc,
4468 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4469 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4470 end Size_In_Storage_Elements;
4471
4472 -- Local variables
4473
4474 Desig : Entity_Id;
4475 Init_Expr : Node_Id;
4476 Init_Stmts : List_Id;
4477 Pool : Entity_Id;
4478 Rel_Typ : Entity_Id;
4479 Target_Ref : Node_Id;
4480 Temp : Entity_Id;
4481 Temp_Decl : Node_Id;
4482
4483 -- Start of processing for Expand_N_Allocator
4484
4485 begin
4486 -- Warn on the presence of an allocator of an anonymous access type when
4487 -- enabled, except when it's an object declaration at library level.
4488
4489 if Warn_On_Anonymous_Allocators
4490 and then Ekind (PtrT) = E_Anonymous_Access_Type
4491 and then not (Is_Library_Level_Entity (PtrT)
4492 and then Nkind (Associated_Node_For_Itype (PtrT)) =
4493 N_Object_Declaration)
4494 then
4495 Error_Msg_N ("?_a?use of an anonymous access type allocator", N);
4496 end if;
4497
4498 -- RM E.2.2(17). We enforce that the expected type of an allocator
4499 -- shall not be a remote access-to-class-wide-limited-private type.
4500 -- We probably shouldn't be doing this legality check during expansion,
4501 -- but this is only an issue for Annex E users, and is unlikely to be a
4502 -- problem in practice.
4503
4504 Validate_Remote_Access_To_Class_Wide_Type (N);
4505
4506 -- Processing for anonymous access-to-controlled types. These access
4507 -- types receive a special finalization collection which appears in the
4508 -- declarations of the enclosing semantic unit. This expansion is done
4509 -- now to ensure that any additional types generated by this routine or
4510 -- Expand_Allocator_Expression inherit the proper type attributes.
4511
4512 if (Ekind (PtrT) = E_Anonymous_Access_Type
4513 or else (Is_Itype (PtrT)
4514 and then No (Finalization_Collection (PtrT))))
4515 and then Needs_Finalization (Dtyp)
4516 then
4517 -- Detect the allocation of an anonymous controlled object where the
4518 -- type of the context is named. For example:
4519
4520 -- procedure Proc (Ptr : Named_Access_Typ);
4521 -- Proc (new Designated_Typ);
4522
4523 -- Regardless of the anonymous-to-named access type conversion, the
4524 -- lifetime of the object must be associated with the named access
4525 -- type. Use the finalization-related attributes of this type.
4526
4527 if Nkind (Parent (N)) in N_Type_Conversion
4528 | N_Unchecked_Type_Conversion
4529 and then Ekind (Etype (Parent (N))) in E_Access_Subtype
4530 | E_Access_Type
4531 | E_General_Access_Type
4532 then
4533 Rel_Typ := Etype (Parent (N));
4534 else
4535 Rel_Typ := Empty;
4536 end if;
4537
4538 -- Anonymous access-to-controlled types allocate on the global pool.
4539 -- Note that this is a "root type only" attribute.
4540
4541 if No (Associated_Storage_Pool (PtrT)) then
4542 if Present (Rel_Typ) then
4543 Set_Associated_Storage_Pool
4544 (Root_Type (PtrT), Associated_Storage_Pool (Rel_Typ));
4545 else
4546 Set_Associated_Storage_Pool
4547 (Root_Type (PtrT), RTE (RE_Global_Pool_Object));
4548 end if;
4549 end if;
4550
4551 -- The finalization collection must be inserted and analyzed as part
4552 -- of the current semantic unit. Note that the collection is updated
4553 -- when analysis changes current units. Note that this is a root type
4554 -- attribute.
4555
4556 if Present (Rel_Typ) then
4557 Set_Finalization_Collection
4558 (Root_Type (PtrT), Finalization_Collection (Rel_Typ));
4559 else
4560 Build_Anonymous_Collection (Root_Type (PtrT));
4561 end if;
4562 end if;
4563
4564 -- Set the storage pool and find the appropriate version of Allocate to
4565 -- call. Do not overwrite the storage pool if it is already set, which
4566 -- can occur for BIP function returns (see Expand_N_Object_Declaration).
4567
4568 if No (Storage_Pool (N)) then
4569 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4570
4571 if Present (Pool) then
4572 Set_Storage_Pool (N, Pool);
4573
4574 if Is_RTE (Pool, RE_RS_Pool) then
4575 Set_Procedure_To_Call (N, RTE (RE_RS_Allocate));
4576
4577 elsif Is_RTE (Pool, RE_SS_Pool) then
4578 Check_Restriction (No_Secondary_Stack, N);
4579 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4580
4581 -- In the case of an allocator for a simple storage pool, locate
4582 -- and save a reference to the pool type's Allocate routine.
4583
4584 elsif Present (Get_Rep_Pragma
4585 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4586 then
4587 declare
4588 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4589 Alloc_Op : Entity_Id;
4590 begin
4591 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4592 while Present (Alloc_Op) loop
4593 if Scope (Alloc_Op) = Scope (Pool_Type)
4594 and then Present (First_Formal (Alloc_Op))
4595 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4596 then
4597 Set_Procedure_To_Call (N, Alloc_Op);
4598 exit;
4599 else
4600 Alloc_Op := Homonym (Alloc_Op);
4601 end if;
4602 end loop;
4603 end;
4604
4605 elsif Is_Class_Wide_Type (Etype (Pool)) then
4606 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4607
4608 else
4609 Set_Procedure_To_Call (N,
4610 Find_Storage_Op (Etype (Pool), Name_Allocate));
4611 end if;
4612 end if;
4613 end if;
4614
4615 -- Under certain circumstances we can replace an allocator by an access
4616 -- to statically allocated storage. The conditions, as noted in AARM
4617 -- 3.10 (10c) are as follows:
4618
4619 -- Size and initial value is known at compile time
4620 -- Access type is access-to-constant
4621
4622 -- The allocator is not part of a constraint on a record component,
4623 -- because in that case the inserted actions are delayed until the
4624 -- record declaration is fully analyzed, which is too late for the
4625 -- analysis of the rewritten allocator.
4626
4627 if Is_Access_Constant (PtrT)
4628 and then Nkind (Expression (N)) = N_Qualified_Expression
4629 and then Compile_Time_Known_Value (Expression (Expression (N)))
4630 and then Size_Known_At_Compile_Time
4631 (Etype (Expression (Expression (N))))
4632 and then not Is_Record_Type (Current_Scope)
4633 then
4634 -- Here we can do the optimization. For the allocator
4635
4636 -- new x'(y)
4637
4638 -- We insert an object declaration
4639
4640 -- Tnn : aliased x := y;
4641
4642 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4643 -- marked as requiring static allocation.
4644
4645 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4646 Desig := Subtype_Mark (Expression (N));
4647
4648 -- If context is constrained, use constrained subtype directly,
4649 -- so that the constant is not labelled as having a nominally
4650 -- unconstrained subtype.
4651
4652 if Entity (Desig) = Base_Type (Dtyp) then
4653 Desig := New_Occurrence_Of (Dtyp, Loc);
4654 end if;
4655
4656 Insert_Action (N,
4657 Make_Object_Declaration (Loc,
4658 Defining_Identifier => Temp,
4659 Aliased_Present => True,
4660 Constant_Present => Is_Access_Constant (PtrT),
4661 Object_Definition => Desig,
4662 Expression => Expression (Expression (N))));
4663
4664 Rewrite (N,
4665 Make_Attribute_Reference (Loc,
4666 Prefix => New_Occurrence_Of (Temp, Loc),
4667 Attribute_Name => Name_Unrestricted_Access));
4668
4669 Analyze_And_Resolve (N, PtrT);
4670
4671 -- We set the variable as statically allocated, since we don't want
4672 -- it going on the stack of the current procedure.
4673
4674 Set_Is_Statically_Allocated (Temp);
4675 return;
4676 end if;
4677
4678 -- Same if the allocator is an access discriminant for a local object:
4679 -- instead of an allocator we create a local value and constrain the
4680 -- enclosing object with the corresponding access attribute.
4681
4682 if Is_Static_Coextension (N) then
4683 Rewrite_Coextension (N);
4684 return;
4685 end if;
4686
4687 -- Check for size too large, we do this because the back end misses
4688 -- proper checks here and can generate rubbish allocation calls when
4689 -- we are near the limit. We only do this for the 32-bit address case
4690 -- since that is from a practical point of view where we see a problem.
4691
4692 if System_Address_Size = 32
4693 and then not Storage_Checks_Suppressed (PtrT)
4694 and then not Storage_Checks_Suppressed (Dtyp)
4695 and then not Storage_Checks_Suppressed (Etyp)
4696 then
4697 -- The check we want to generate should look like
4698
4699 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4700 -- raise Storage_Error;
4701 -- end if;
4702
4703 -- where 3.5 gigabytes is a constant large enough to accommodate any
4704 -- reasonable request for. But we can't do it this way because at
4705 -- least at the moment we don't compute this attribute right, and
4706 -- can silently give wrong results when the result gets large. Since
4707 -- this is all about large results, that's bad, so instead we only
4708 -- apply the check for constrained arrays, and manually compute the
4709 -- value of the attribute ???
4710
4711 -- The check on No_Initialization is used here to prevent generating
4712 -- this runtime check twice when the allocator is locally replaced by
4713 -- the expander with another one.
4714
4715 if Is_Array_Type (Etyp) and then not No_Initialization (N) then
4716 declare
4717 Cond : Node_Id;
4718 Ins_Nod : Node_Id := N;
4719 Siz_Typ : Entity_Id := Etyp;
4720 Expr : Node_Id;
4721
4722 begin
4723 -- For unconstrained array types initialized with a qualified
4724 -- expression we use its type to perform this check
4725
4726 if not Is_Constrained (Etyp)
4727 and then not No_Initialization (N)
4728 and then Nkind (Expression (N)) = N_Qualified_Expression
4729 then
4730 Expr := Expression (Expression (N));
4731 Siz_Typ := Etype (Expression (Expression (N)));
4732
4733 -- If the qualified expression has been moved to an internal
4734 -- temporary (to remove side effects) then we must insert
4735 -- the runtime check before its declaration to ensure that
4736 -- the check is performed before the execution of the code
4737 -- computing the qualified expression.
4738
4739 if Nkind (Expr) = N_Identifier
4740 and then Is_Internal_Name (Chars (Expr))
4741 and then
4742 Nkind (Parent (Entity (Expr))) = N_Object_Declaration
4743 then
4744 Ins_Nod := Parent (Entity (Expr));
4745 else
4746 Ins_Nod := Expr;
4747 end if;
4748 end if;
4749
4750 if Is_Constrained (Siz_Typ)
4751 and then Ekind (Siz_Typ) /= E_String_Literal_Subtype
4752 then
4753 -- The largest object is 3.5 gigabytes
4754
4755 Cond :=
4756 Make_Op_Gt (Loc,
4757 Left_Opnd => Size_In_Storage_Elements (Siz_Typ),
4758 Right_Opnd => Make_Integer_Literal (Loc,
4759 Uint_7 * (Uint_2 ** 29)));
4760 Insert_Action (Ins_Nod,
4761 Make_Raise_Storage_Error (Loc,
4762 Condition => Cond,
4763 Reason => SE_Object_Too_Large));
4764
4765 if Entity (Cond) = Standard_True then
4766 Error_Msg_N
4767 ("object too large: Storage_Error will be raised at "
4768 & "run time??", N);
4769 end if;
4770 end if;
4771 end;
4772 end if;
4773 end if;
4774
4775 -- If no storage pool has been specified, or the storage pool
4776 -- is System.Pool_Global.Global_Pool_Object, and the restriction
4777 -- No_Standard_Allocators_After_Elaboration is present, then generate
4778 -- a call to Elaboration_Allocators.Check_Standard_Allocator.
4779
4780 if Nkind (N) = N_Allocator
4781 and then (No (Storage_Pool (N))
4782 or else Is_RTE (Storage_Pool (N), RE_Global_Pool_Object))
4783 and then Restriction_Active (No_Standard_Allocators_After_Elaboration)
4784 then
4785 Insert_Action (N,
4786 Make_Procedure_Call_Statement (Loc,
4787 Name =>
4788 New_Occurrence_Of (RTE (RE_Check_Standard_Allocator), Loc)));
4789 end if;
4790
4791 -- Handle case of qualified expression (other than optimization above)
4792
4793 if Nkind (Expression (N)) = N_Qualified_Expression then
4794 Expand_Allocator_Expression (N);
4795
4796 -- If no initialization is necessary, just create a custom Allocate if
4797 -- the context requires it; that is the case only for allocators built
4798 -- for the special return objects because, in other cases, the custom
4799 -- Allocate will be created later during the expansion of the original
4800 -- allocator without the No_Initialization flag.
4801
4802 elsif No_Initialization (N) then
4803 if For_Special_Return_Object (N) then
4804 Build_Allocate_Deallocate_Proc (Parent (N));
4805 end if;
4806
4807 -- If the allocator is for a type which requires initialization, and
4808 -- there is no initial value (i.e. operand is a subtype indication
4809 -- rather than a qualified expression), then we must generate a call to
4810 -- the initialization routine:
4811
4812 -- Temp : constant PtrT := new T;
4813 -- Init (Temp.all,...);
4814 -- ... := Temp.all;
4815
4816 -- A special case arises if T is a task type or contains tasks. In this
4817 -- case the call to Init (Temp.all ...) is replaced by code that ensures
4818 -- that tasks get activated (see Build_Task_Allocate_Block for details).
4819
4820 else
4821 -- Apply constraint checks against designated subtype (RM 4.8(10/2)).
4822 -- Discriminant checks will be generated by the expansion below.
4823
4824 if Is_Array_Type (Dtyp) then
4825 Apply_Constraint_Check (Expression (N), Dtyp, No_Sliding => True);
4826
4827 if Nkind (Expression (N)) = N_Raise_Constraint_Error then
4828 Rewrite (N, New_Copy (Expression (N)));
4829 Set_Etype (N, PtrT);
4830 return;
4831 end if;
4832 end if;
4833
4834 -- First try a simple initialization; if it succeeds, then we just
4835 -- assign the value to the allocated memory.
4836
4837 Init_Expr := Build_Default_Simple_Initialization (N, Etyp, Empty);
4838
4839 if Present (Init_Expr) then
4840 declare
4841 Deref : Node_Id;
4842 Stmt : Node_Id;
4843
4844 begin
4845 -- We set the allocator as analyzed so that when we analyze
4846 -- the expression node, we do not get an unwanted recursive
4847 -- expansion of the allocator expression.
4848
4849 Set_Analyzed (N);
4850
4851 Temp := Make_Temporary (Loc, 'P');
4852
4853 -- Generate:
4854 -- Temp : constant PtrT := new ...;
4855
4856 Temp_Decl :=
4857 Make_Object_Declaration (Loc,
4858 Defining_Identifier => Temp,
4859 Constant_Present => True,
4860 Object_Definition => New_Occurrence_Of (PtrT, Loc),
4861 Expression => Relocate_Node (N));
4862
4863 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4864
4865 -- Generate:
4866 -- Temp.all := ...
4867
4868 Deref :=
4869 Make_Explicit_Dereference (Loc,
4870 New_Occurrence_Of (Temp, Loc));
4871
4872 if Is_Incomplete_Or_Private_Type (Designated_Type (PtrT)) then
4873 Deref := Unchecked_Convert_To (Etype (Init_Expr), Deref);
4874 end if;
4875
4876 Stmt :=
4877 Make_Assignment_Statement (Loc,
4878 Name => Deref,
4879 Expression => Init_Expr);
4880 Set_Assignment_OK (Name (Stmt));
4881
4882 Insert_Action (N, Stmt, Suppress => All_Checks);
4883 Build_Allocate_Deallocate_Proc (Temp_Decl);
4884 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4885 Analyze_And_Resolve (N, PtrT);
4886
4887 Apply_Predicate_Check (N, Dtyp, Deref => True);
4888 end;
4889
4890 -- Or else build the fully-fledged initialization if need be
4891
4892 else
4893 -- For the task case, pass the Master_Id of the access type as
4894 -- the value of the _Master parameter, and _Chain as the value
4895 -- of the _Chain parameter (_Chain will be defined as part of
4896 -- the generated code for the allocator).
4897
4898 -- In Ada 2005, the context may be a function that returns an
4899 -- anonymous access type. In that case the Master_Id has been
4900 -- created when expanding the function declaration.
4901
4902 if Has_Task (Etyp) then
4903 if No (Master_Id (Base_Type (PtrT))) then
4904 -- The designated type was an incomplete type, and the
4905 -- access type did not get expanded. Salvage it now.
4906
4907 if Present (Declaration_Node (Base_Type (PtrT))) then
4908 Expand_N_Full_Type_Declaration
4909 (Declaration_Node (Base_Type (PtrT)));
4910
4911 -- When the allocator has a subtype indication then a
4912 -- constraint is present and an itype has been added by
4913 -- Analyze_Allocator as the subtype of this allocator.
4914
4915 -- If an allocator with constraints is called in the
4916 -- return statement of a function returning a general
4917 -- access type, then propagate to the itype the master
4918 -- of the general access type (since it is the master
4919 -- associated with the returned object).
4920
4921 elsif Is_Itype (PtrT)
4922 and then Ekind (Current_Scope) = E_Function
4923 and then
4924 Ekind (Etype (Current_Scope)) = E_General_Access_Type
4925 and then In_Return_Value (N)
4926 then
4927 Set_Master_Id (PtrT, Master_Id (Etype (Current_Scope)));
4928
4929 -- The only other possibility is an itype. For this
4930 -- case, the master must exist in the context. This is
4931 -- the case when the allocator initializes an access
4932 -- component in an init-proc.
4933
4934 else
4935 pragma Assert (Is_Itype (PtrT));
4936 Build_Master_Renaming (PtrT, N);
4937 end if;
4938 end if;
4939
4940 -- If the context of the allocator is a declaration or an
4941 -- assignment, we can generate a meaningful image for the
4942 -- task even though subsequent assignments might remove the
4943 -- connection between task and entity. We build this image
4944 -- when the left-hand side is a simple variable, a simple
4945 -- indexed assignment or a simple selected component.
4946
4947 if Nkind (Parent (N)) = N_Object_Declaration then
4948 Target_Ref := Defining_Identifier (Parent (N));
4949
4950 elsif Nkind (Parent (N)) = N_Assignment_Statement then
4951 declare
4952 Nam : constant Node_Id := Name (Parent (N));
4953
4954 begin
4955 if Is_Entity_Name (Nam) then
4956 Target_Ref := Nam;
4957
4958 elsif Nkind (Nam) in N_Indexed_Component
4959 | N_Selected_Component
4960 and then Is_Entity_Name (Prefix (Nam))
4961 then
4962 Target_Ref := Nam;
4963
4964 else
4965 Target_Ref := PtrT;
4966 end if;
4967 end;
4968
4969 -- Otherwise we just pass the access type
4970
4971 else
4972 Target_Ref := PtrT;
4973 end if;
4974
4975 -- Nothing to pass in the non-task case
4976
4977 else
4978 Target_Ref := Empty;
4979 end if;
4980
4981 Temp := Make_Temporary (Loc, 'P');
4982
4983 if Is_Mutably_Tagged_Type (Dtyp) then
4984 Init_Stmts :=
4985 Build_Default_Initialization (N, Etype (Etyp), Temp,
4986 For_CW => False,
4987 Target_Ref => Target_Ref);
4988 else
4989 Init_Stmts :=
4990 Build_Default_Initialization (N, Etyp, Temp,
4991 For_CW => Is_Class_Wide_Type (Dtyp),
4992 Target_Ref => Target_Ref);
4993 end if;
4994
4995 if Present (Init_Stmts) then
4996 -- We set the allocator as analyzed so that when we analyze
4997 -- the expression node, we do not get an unwanted recursive
4998 -- expansion of the allocator expression.
4999
5000 Set_Analyzed (N);
5001
5002 Temp_Decl :=
5003 Make_Object_Declaration (Loc,
5004 Defining_Identifier => Temp,
5005 Constant_Present => True,
5006 Object_Definition => New_Occurrence_Of (PtrT, Loc),
5007 Expression => Relocate_Node (N));
5008
5009 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
5010
5011 -- If the designated type is a task type or contains tasks,
5012 -- create a specific block to activate the created tasks.
5013
5014 if Has_Task (Etyp) then
5015 declare
5016 Actions : constant List_Id := New_List;
5017
5018 begin
5019 Build_Task_Allocate_Block
5020 (Actions, Relocate_Node (N), Init_Stmts);
5021 Insert_Actions (N, Actions, Suppress => All_Checks);
5022 end;
5023
5024 else
5025 Insert_Actions (N, Init_Stmts, Suppress => All_Checks);
5026 end if;
5027
5028 Build_Allocate_Deallocate_Proc (Temp_Decl);
5029 Rewrite (N, New_Occurrence_Of (Temp, Loc));
5030 Analyze_And_Resolve (N, PtrT);
5031
5032 Apply_Predicate_Check (N, Dtyp, Deref => True);
5033
5034 -- When designated type has Default_Initial_Condition aspects,
5035 -- make a call to the type's DIC procedure to perform the
5036 -- checks. Theoretically this might also be needed for cases
5037 -- where the type doesn't have an init proc, but those should
5038 -- be very uncommon, and for now we only support the init proc
5039 -- case. ???
5040
5041 if Has_DIC (Dtyp)
5042 and then Present (DIC_Procedure (Dtyp))
5043 and then not Has_Null_Body (DIC_Procedure (Dtyp))
5044 then
5045 Insert_Action (N,
5046 Build_DIC_Call (Loc,
5047 Make_Explicit_Dereference (Loc,
5048 Prefix => New_Occurrence_Of (Temp, Loc)),
5049 Dtyp));
5050 end if;
5051
5052 -- Ada 2005 (AI-251): Displace the pointer to reference the
5053 -- record component containing the secondary dispatch table
5054 -- of the interface type.
5055
5056 if Is_Interface (Dtyp) then
5057 Displace_Allocator_Pointer (N);
5058 end if;
5059
5060 -- No initialization required
5061
5062 else
5063 Build_Allocate_Deallocate_Proc (N);
5064 end if;
5065 end if;
5066 end if;
5067
5068 exception
5069 when RE_Not_Available =>
5070 return;
5071 end Expand_N_Allocator;
5072
5073 -----------------------
5074 -- Expand_N_And_Then --
5075 -----------------------
5076
5077 procedure Expand_N_And_Then (N : Node_Id)
5078 renames Expand_Short_Circuit_Operator;
5079
5080 ------------------------------
5081 -- Expand_N_Case_Expression --
5082 ------------------------------
5083
5084 procedure Expand_N_Case_Expression (N : Node_Id) is
5085 Loc : constant Source_Ptr := Sloc (N);
5086 Par : constant Node_Id := Parent (N);
5087 Scop : constant Entity_Id := Current_Scope;
5088 Typ : constant Entity_Id := Etype (N);
5089
5090 function Is_Copy_Type (Typ : Entity_Id) return Boolean;
5091 -- Return True if we can copy objects of this type when expanding a case
5092 -- expression.
5093
5094 function Is_Optimizable_Declaration (N : Node_Id) return Boolean;
5095 -- Return True if N is an object declaration that can be optimized
5096
5097 ------------------
5098 -- Is_Copy_Type --
5099 ------------------
5100
5101 function Is_Copy_Type (Typ : Entity_Id) return Boolean is
5102 begin
5103 return Is_Elementary_Type (Underlying_Type (Typ));
5104 end Is_Copy_Type;
5105
5106 --------------------------------
5107 -- Is_Optimizable_Declaration --
5108 --------------------------------
5109
5110 function Is_Optimizable_Declaration (N : Node_Id) return Boolean is
5111 begin
5112 return Nkind (N) = N_Object_Declaration
5113 and then not (Is_Entity_Name (Object_Definition (N))
5114 and then Is_Class_Wide_Type
5115 (Entity (Object_Definition (N))))
5116 and then not Is_Return_Object (Defining_Identifier (N))
5117 and then not Is_Copy_Type (Typ);
5118 end Is_Optimizable_Declaration;
5119
5120 -- Local variables
5121
5122 Acts : List_Id;
5123 Alt : Node_Id;
5124 Case_Stmt : Node_Id;
5125 Decl : Node_Id;
5126 New_N : Node_Id;
5127 Par_Obj : Node_Id;
5128 Target : Entity_Id := Empty;
5129 Target_Typ : Entity_Id;
5130
5131 Optimize_Assignment_Stmt : Boolean := False;
5132 -- Small optimization: when the case expression appears in the context
5133 -- of a safe assignment statement, expand into
5134
5135 -- case X is
5136 -- when A =>
5137 -- lhs := AX;
5138 -- when B =>
5139 -- lhs := BX;
5140 -- ...
5141 -- end case;
5142
5143 -- This makes the expansion much more efficient in the context of an
5144 -- aggregate converted into assignments.
5145
5146 Optimize_Return_Stmt : Boolean := False;
5147 -- Small optimization: when the case expression appears in the context
5148 -- of a simple return statement, expand into
5149
5150 -- case X is
5151 -- when A =>
5152 -- return AX;
5153 -- when B =>
5154 -- return BX;
5155 -- ...
5156 -- end case;
5157
5158 -- This makes the expansion much easier when expressions are calls to
5159 -- build-in-place functions.
5160
5161 Optimize_Object_Decl : Boolean := False;
5162 -- Small optimization: when the case expression appears in the context
5163 -- of an object declaration of a type not Is_Copy_Type, expand into
5164
5165 -- case X is
5166 -- when A =>
5167 -- then-obj : typ := then_expr;
5168 -- target := then-obj'Unrestricted_Access;
5169 -- when B =>
5170 -- else-obj : typ := else-expr;
5171 -- target := else-obj'Unrestricted_Access;
5172 -- ...
5173 -- end case
5174 --
5175 -- obj : typ renames target.all;
5176
5177 -- This makes the expansion much easier when expressions are calls to
5178 -- build-in-place functions.
5179
5180 -- Start of processing for Expand_N_Case_Expression
5181
5182 begin
5183 -- If the expression is in the context of a simple return statement,
5184 -- possibly through intermediate conditional expressions, we delay
5185 -- expansion until the (immediate) parent is rewritten as a return
5186 -- statement (or is already the return statement). Likewise if it is
5187 -- in the context of an object declaration that can be optimized.
5188
5189 if not Expansion_Delayed (N) then
5190 declare
5191 Uncond_Par : constant Node_Id := Unconditional_Parent (N);
5192 begin
5193 if Nkind (Uncond_Par) = N_Simple_Return_Statement
5194 or else Is_Optimizable_Declaration (Uncond_Par)
5195 then
5196 Delay_Conditional_Expressions_Between (N, Uncond_Par);
5197 end if;
5198 end;
5199 end if;
5200
5201 -- If the expansion of the expression has been delayed, we wait for the
5202 -- rewriting of its parent as an assignment statement, or as as return
5203 -- statement or as an object declaration; when that's done, we optimize
5204 -- the assignment, return or declaration (the purpose of the delaying).
5205
5206 if Expansion_Delayed (N) then
5207 if Nkind (Par) = N_Assignment_Statement then
5208 Optimize_Assignment_Stmt := True;
5209
5210 elsif Nkind (Par) = N_Simple_Return_Statement then
5211 Optimize_Return_Stmt := True;
5212
5213 elsif Is_Optimizable_Declaration (Par) then
5214 Optimize_Object_Decl := True;
5215
5216 else
5217 return;
5218 end if;
5219
5220 Set_Expansion_Delayed (N, False);
5221 end if;
5222
5223 -- Check for MINIMIZED/ELIMINATED overflow mode
5224
5225 if Minimized_Eliminated_Overflow_Check (N) then
5226 Apply_Arithmetic_Overflow_Check (N);
5227 return;
5228 end if;
5229
5230 -- If the case expression is a predicate specification, do not expand
5231 -- because it will need to be recognized and converted to the canonical
5232 -- predicate form later if it it happens to be static.
5233
5234 if Ekind (Scop) in E_Function | E_Procedure
5235 and then Is_Predicate_Function (Scop)
5236 and then Is_Entity_Name (Expression (N))
5237 and then Entity (Expression (N)) = First_Entity (Scop)
5238 and then (Is_Scalar_Type (Etype (Expression (N)))
5239 or else Is_String_Type (Etype (Expression (N))))
5240 and then not Has_Dynamic_Predicate_Aspect (Etype (Expression (N)))
5241 then
5242 return;
5243 end if;
5244
5245 -- When the type of the case expression is elementary, expand
5246
5247 -- (case X is when A => AX, when B => BX ...)
5248
5249 -- into
5250
5251 -- do
5252 -- Target : Typ;
5253 -- case X is
5254 -- when A =>
5255 -- <<actions>>
5256 -- Target := AX;
5257 -- when B =>
5258 -- <<actions>>
5259 -- Target := BX;
5260 -- ...
5261 -- end case;
5262 -- in Target end;
5263
5264 -- In all other cases expand into
5265
5266 -- type Ptr_Typ is not null access all Typ;
5267 -- Target : Ptr_Typ;
5268 -- case X is
5269 -- when A =>
5270 -- <<actions>>
5271 -- Target := AX'Unrestricted_Access;
5272 -- when B =>
5273 -- <<actions>>
5274 -- Target := BX'Unrestricted_Access;
5275 -- ...
5276 -- end case;
5277
5278 -- and replace the case expression by a reference to Target.all.
5279
5280 -- This approach avoids extra copies of potentially large objects. It
5281 -- also allows handling of values of limited or unconstrained types.
5282
5283 Case_Stmt :=
5284 Make_Case_Statement (Loc,
5285 Expression => Expression (N),
5286 Alternatives => New_List);
5287
5288 -- Preserve the original context for which the case statement is being
5289 -- generated. This is needed by the finalization machinery to prevent
5290 -- the premature finalization of controlled objects found within the
5291 -- case statement.
5292
5293 Set_From_Conditional_Expression (Case_Stmt);
5294 Acts := New_List;
5295
5296 -- No need for Target_Typ in the case of statements
5297
5298 if Optimize_Assignment_Stmt or else Optimize_Return_Stmt then
5299 Target_Typ := Empty;
5300
5301 -- Scalar/Copy case
5302
5303 elsif Is_Copy_Type (Typ) then
5304 Target_Typ := Typ;
5305
5306 -- Otherwise create an access type to handle the general case using
5307 -- 'Unrestricted_Access.
5308
5309 -- Generate:
5310 -- type Target_Typ is not null access all [constant] Typ;
5311
5312 else
5313 Target_Typ := Make_Temporary (Loc, 'P');
5314
5315 Append_To (Acts,
5316 Make_Full_Type_Declaration (Loc,
5317 Defining_Identifier => Target_Typ,
5318 Type_Definition =>
5319 Make_Access_To_Object_Definition (Loc,
5320 All_Present => True,
5321 Null_Exclusion_Present => True,
5322 Subtype_Indication => New_Occurrence_Of (Typ, Loc),
5323 Constant_Present =>
5324 Optimize_Object_Decl and then Constant_Present (Par))));
5325 end if;
5326
5327 -- Create the declaration of the target which captures the value of the
5328 -- expression.
5329
5330 -- Generate:
5331 -- Target : [Ptr_]Typ;
5332
5333 if Optimize_Assignment_Stmt then
5334 Remove_Side_Effects (Name (Par), Name_Req => True);
5335
5336 elsif not Optimize_Return_Stmt then
5337 Target := Make_Temporary (Loc, 'T');
5338
5339 Decl :=
5340 Make_Object_Declaration (Loc,
5341 Defining_Identifier => Target,
5342 Object_Definition => New_Occurrence_Of (Target_Typ, Loc));
5343 Set_No_Initialization (Decl);
5344
5345 Append_To (Acts, Decl);
5346 end if;
5347
5348 -- Process the alternatives
5349
5350 Alt := First (Alternatives (N));
5351 while Present (Alt) loop
5352 -- When the alternative's expression involves controlled function
5353 -- calls, generated temporaries are chained on the corresponding
5354 -- list of actions. These temporaries need to be finalized after
5355 -- the case expression is evaluated.
5356
5357 Process_Transients_In_Expression (N, Actions (Alt));
5358
5359 declare
5360 Alt_Loc : constant Source_Ptr := Sloc (Expression (Alt));
5361
5362 Alt_Expr : Node_Id := Relocate_Node (Expression (Alt));
5363 LHS : Node_Id;
5364 Obj : Node_Id;
5365 Stmts : List_Id;
5366
5367 begin
5368 -- Generate:
5369 -- lhs := AX;
5370
5371 if Optimize_Assignment_Stmt then
5372 -- We directly copy the parent node to preserve its flags
5373
5374 Stmts := New_List (New_Copy (Par));
5375 Set_Sloc (First (Stmts), Alt_Loc);
5376 Set_Name (First (Stmts), New_Copy_Tree (Name (Par)));
5377 Set_Expression (First (Stmts), Alt_Expr);
5378
5379 -- If the expression is itself a conditional expression whose
5380 -- expansion has been delayed, analyze it again and expand it.
5381
5382 if Is_Delayed_Conditional_Expression (Alt_Expr) then
5383 Unanalyze_Delayed_Conditional_Expression (Alt_Expr);
5384 end if;
5385
5386 -- Generate:
5387 -- return AX;
5388
5389 elsif Optimize_Return_Stmt then
5390 Stmts := New_List (
5391 Make_Simple_Return_Statement (Alt_Loc,
5392 Expression => Alt_Expr));
5393
5394 -- If the expression is itself a conditional expression whose
5395 -- expansion has been delayed, analyze it again and expand it.
5396
5397 if Is_Delayed_Conditional_Expression (Alt_Expr) then
5398 Unanalyze_Delayed_Conditional_Expression (Alt_Expr);
5399 end if;
5400
5401 -- Generate:
5402 -- Obj : [constant] Typ := AX;
5403 -- Target := Obj'Unrestricted_Access;
5404
5405 elsif Optimize_Object_Decl then
5406 Obj := Make_Temporary (Loc, 'C', Alt_Expr);
5407
5408 Insert_Conditional_Object_Declaration
5409 (Obj, Typ, Alt_Expr, Const => Constant_Present (Par));
5410
5411 Stmts := New_List (
5412 Make_Assignment_Statement (Alt_Loc,
5413 Name => New_Occurrence_Of (Target, Loc),
5414 Expression =>
5415 Make_Attribute_Reference (Alt_Loc,
5416 Prefix => New_Occurrence_Of (Obj, Alt_Loc),
5417 Attribute_Name => Name_Unrestricted_Access)));
5418
5419 -- Take the unrestricted access of the expression value for non-
5420 -- scalar types. This approach avoids big copies and covers the
5421 -- limited and unconstrained cases.
5422
5423 -- Generate:
5424 -- Target := AX'Unrestricted_Access;
5425
5426 else
5427 if not Is_Copy_Type (Typ) then
5428 -- It's possible that a call to Apply_Length_Check in
5429 -- Resolve_Case_Expression rewrote the dependent expression
5430 -- into a N_Raise_Constraint_Error. If that's the case, we
5431 -- don't create a reference to Unrestricted_Access, but we
5432 -- update the type of the N_Raise_Constraint_Error node.
5433
5434 if Nkind (Alt_Expr) in N_Raise_Constraint_Error then
5435 Set_Etype (Alt_Expr, Target_Typ);
5436 else
5437 Alt_Expr :=
5438 Make_Attribute_Reference (Alt_Loc,
5439 Prefix => Alt_Expr,
5440 Attribute_Name => Name_Unrestricted_Access);
5441 end if;
5442 end if;
5443
5444 LHS := New_Occurrence_Of (Target, Loc);
5445 Set_Assignment_OK (LHS);
5446
5447 Stmts := New_List (
5448 Make_Assignment_Statement (Alt_Loc,
5449 Name => LHS,
5450 Expression => Alt_Expr));
5451 end if;
5452
5453 -- Propagate declarations inserted in the node by Insert_Actions
5454 -- (for example, temporaries generated to remove side effects).
5455 -- These actions must remain attached to the alternative, given
5456 -- that they are generated by the corresponding expression.
5457
5458 if Present (Actions (Alt)) then
5459 Prepend_List (Actions (Alt), Stmts);
5460 end if;
5461
5462 Append_To
5463 (Alternatives (Case_Stmt),
5464 Make_Case_Statement_Alternative (Sloc (Alt),
5465 Discrete_Choices => Discrete_Choices (Alt),
5466 Statements => Stmts));
5467 end;
5468
5469 Next (Alt);
5470 end loop;
5471
5472 -- Rewrite the parent statement as a case statement
5473
5474 if Optimize_Assignment_Stmt or else Optimize_Return_Stmt then
5475 Rewrite (Par, Case_Stmt);
5476 Analyze (Par);
5477
5478 elsif Optimize_Object_Decl then
5479 Append_To (Acts, Case_Stmt);
5480 Insert_Actions (Par, Acts);
5481
5482 New_N :=
5483 Make_Explicit_Dereference (Loc,
5484 Prefix => New_Occurrence_Of (Target, Loc));
5485
5486 -- The renaming is not analyzed so complete the decoration of the
5487 -- object and set the type of the name directly.
5488
5489 Par_Obj := Defining_Identifier (Par);
5490 if Constant_Present (Par) then
5491 Mutate_Ekind (Par_Obj, E_Constant);
5492 Set_Is_True_Constant (Par_Obj);
5493 else
5494 Mutate_Ekind (Par_Obj, E_Variable);
5495 end if;
5496
5497 Set_Etype (New_N, Etype (Par_Obj));
5498
5499 Rewrite_Object_Declaration_As_Renaming (Par, New_N);
5500
5501 -- Otherwise rewrite the case expression itself
5502
5503 else
5504 Append_To (Acts, Case_Stmt);
5505
5506 if Is_Copy_Type (Typ) then
5507 New_N :=
5508 Make_Expression_With_Actions (Loc,
5509 Expression => New_Occurrence_Of (Target, Loc),
5510 Actions => Acts);
5511
5512 else
5513 Insert_Actions (N, Acts);
5514 New_N :=
5515 Make_Explicit_Dereference (Loc,
5516 Prefix => New_Occurrence_Of (Target, Loc));
5517 end if;
5518
5519 Rewrite (N, New_N);
5520 Analyze_And_Resolve (N, Typ);
5521 end if;
5522 end Expand_N_Case_Expression;
5523
5524 -----------------------------------
5525 -- Expand_N_Explicit_Dereference --
5526 -----------------------------------
5527
5528 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5529 begin
5530 -- Insert explicit dereference call for the checked storage pool case
5531
5532 Insert_Dereference_Action (Prefix (N));
5533
5534 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5535 -- we set the atomic sync flag.
5536
5537 if Is_Atomic (Etype (N))
5538 and then not Atomic_Synchronization_Disabled (Etype (N))
5539 then
5540 Activate_Atomic_Synchronization (N);
5541 end if;
5542 end Expand_N_Explicit_Dereference;
5543
5544 --------------------------------------
5545 -- Expand_N_Expression_With_Actions --
5546 --------------------------------------
5547
5548 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5549 Acts : constant List_Id := Actions (N);
5550
5551 procedure Force_Boolean_Evaluation (Expr : Node_Id);
5552 -- Force the evaluation of Boolean expression Expr
5553
5554 ------------------------------
5555 -- Force_Boolean_Evaluation --
5556 ------------------------------
5557
5558 procedure Force_Boolean_Evaluation (Expr : Node_Id) is
5559 Loc : constant Source_Ptr := Sloc (N);
5560 Flag_Decl : Node_Id;
5561 Flag_Id : Entity_Id;
5562
5563 begin
5564 -- Relocate the expression to the actions list by capturing its value
5565 -- in a Boolean flag. Generate:
5566 -- Flag : constant Boolean := Expr;
5567
5568 Flag_Id := Make_Temporary (Loc, 'F');
5569
5570 Flag_Decl :=
5571 Make_Object_Declaration (Loc,
5572 Defining_Identifier => Flag_Id,
5573 Constant_Present => True,
5574 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
5575 Expression => Relocate_Node (Expr));
5576
5577 Append (Flag_Decl, Acts);
5578 Analyze (Flag_Decl);
5579
5580 -- Replace the expression with a reference to the flag
5581
5582 Rewrite (Expression (N), New_Occurrence_Of (Flag_Id, Loc));
5583 Analyze (Expression (N));
5584 end Force_Boolean_Evaluation;
5585
5586 -- Start of processing for Expand_N_Expression_With_Actions
5587
5588 begin
5589 -- Do not evaluate the expression when it denotes an entity because the
5590 -- expression_with_actions node will be replaced by the reference.
5591
5592 if Is_Entity_Name (Expression (N)) then
5593 null;
5594
5595 -- Do not evaluate the expression when there are no actions because the
5596 -- expression_with_actions node will be replaced by the expression.
5597
5598 elsif Is_Empty_List (Acts) then
5599 null;
5600
5601 -- Force the evaluation of the expression by capturing its value in a
5602 -- temporary. This ensures that aliases of transient objects do not leak
5603 -- to the expression of the expression_with_actions node:
5604
5605 -- do
5606 -- Trans_Id : Ctrl_Typ := ...;
5607 -- Alias : ... := Trans_Id;
5608 -- in ... Alias ... end;
5609
5610 -- In the example above, Trans_Id cannot be finalized at the end of the
5611 -- actions list because this may affect the alias and the final value of
5612 -- the expression_with_actions. Forcing the evaluation encapsulates the
5613 -- reference to the Alias within the actions list:
5614
5615 -- do
5616 -- Trans_Id : Ctrl_Typ := ...;
5617 -- Alias : ... := Trans_Id;
5618 -- Val : constant Boolean := ... Alias ...;
5619 -- <finalize Trans_Id>
5620 -- in Val end;
5621
5622 -- Once this transformation is performed, it is safe to finalize the
5623 -- transient object at the end of the actions list.
5624
5625 -- Note that Force_Evaluation does not remove side effects in operators
5626 -- because it assumes that all operands are evaluated and side effect
5627 -- free. This is not the case when an operand depends implicitly on the
5628 -- transient object through the use of access types.
5629
5630 elsif Is_Boolean_Type (Etype (Expression (N))) then
5631 Force_Boolean_Evaluation (Expression (N));
5632
5633 -- The expression of an expression_with_actions node may not necessarily
5634 -- be Boolean when the node appears in an if expression. In this case do
5635 -- the usual forced evaluation to encapsulate potential aliasing.
5636
5637 else
5638 -- A check is also needed since the subtype of the EWA node and the
5639 -- subtype of the expression may differ (for example, the EWA node
5640 -- may have a null-excluding access subtype).
5641
5642 Apply_Constraint_Check (Expression (N), Etype (N));
5643 Force_Evaluation (Expression (N));
5644 end if;
5645
5646 -- Process transient objects found within the actions of the EWA node
5647
5648 Process_Transients_In_Expression (N, Acts);
5649
5650 -- Deal with case where there are no actions. In this case we simply
5651 -- rewrite the node with its expression since we don't need the actions
5652 -- and the specification of this node does not allow a null action list.
5653
5654 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5655 -- the expanded tree and relying on being able to retrieve the original
5656 -- tree in cases like this. This raises a whole lot of issues of whether
5657 -- we have problems elsewhere, which will be addressed in the future???
5658
5659 if Is_Empty_List (Acts) then
5660 Rewrite (N, Relocate_Node (Expression (N)));
5661 end if;
5662 end Expand_N_Expression_With_Actions;
5663
5664 ----------------------------
5665 -- Expand_N_If_Expression --
5666 ----------------------------
5667
5668 -- Deal with limited types and condition actions
5669
5670 procedure Expand_N_If_Expression (N : Node_Id) is
5671 Cond : constant Node_Id := First (Expressions (N));
5672 Loc : constant Source_Ptr := Sloc (N);
5673 Thenx : constant Node_Id := Next (Cond);
5674 Elsex : constant Node_Id := Next (Thenx);
5675 Par : constant Node_Id := Parent (N);
5676 Typ : constant Entity_Id := Etype (N);
5677
5678 Force_Expand : constant Boolean := Is_Anonymous_Access_Actual (N);
5679 -- Determine if we are dealing with a special case of a conditional
5680 -- expression used as an actual for an anonymous access type which
5681 -- forces us to transform the if expression into an expression with
5682 -- actions in order to create a temporary to capture the level of the
5683 -- expression in each branch.
5684
5685 function Is_Copy_Type (Typ : Entity_Id) return Boolean;
5686 -- Return True if we can copy objects of this type when expanding an if
5687 -- expression.
5688
5689 function Is_Optimizable_Declaration (N : Node_Id) return Boolean;
5690 -- Return True if N is an object declaration that can be optimized
5691
5692 function OK_For_Single_Subtype (T1, T2 : Entity_Id) return Boolean;
5693 -- Return true if it is acceptable to use a single subtype for two
5694 -- dependent expressions of subtype T1 and T2 respectively, which are
5695 -- unidimensional arrays whose index bounds are known at compile time.
5696
5697 ------------------
5698 -- Is_Copy_Type --
5699 ------------------
5700
5701 function Is_Copy_Type (Typ : Entity_Id) return Boolean is
5702 Utyp : constant Entity_Id := Underlying_Type (Typ);
5703
5704 begin
5705 return Is_Definite_Subtype (Utyp)
5706 and then not Is_By_Reference_Type (Utyp);
5707 end Is_Copy_Type;
5708
5709 --------------------------------
5710 -- Is_Optimizable_Declaration --
5711 --------------------------------
5712
5713 function Is_Optimizable_Declaration (N : Node_Id) return Boolean is
5714 begin
5715 return Nkind (N) = N_Object_Declaration
5716 and then not (Is_Entity_Name (Object_Definition (N))
5717 and then Is_Class_Wide_Type
5718 (Entity (Object_Definition (N))))
5719 and then not Is_Return_Object (Defining_Identifier (N))
5720 and then not Is_Copy_Type (Typ);
5721 end Is_Optimizable_Declaration;
5722
5723 ---------------------------
5724 -- OK_For_Single_Subtype --
5725 ---------------------------
5726
5727 function OK_For_Single_Subtype (T1, T2 : Entity_Id) return Boolean is
5728 Lo1, Hi1 : Uint;
5729 Lo2, Hi2 : Uint;
5730
5731 begin
5732 Get_First_Index_Bounds (T1, Lo1, Hi1);
5733 Get_First_Index_Bounds (T2, Lo2, Hi2);
5734
5735 -- Return true if the length of the covering subtype is not too large
5736
5737 return
5738 UI_Max (Hi1, Hi2) - UI_Min (Lo1, Lo2) < Too_Large_Length_For_Array;
5739 end OK_For_Single_Subtype;
5740
5741 -- Local variables
5742
5743 Actions : List_Id;
5744 Decl : Node_Id;
5745 Expr : Node_Id;
5746 If_Stmt : Node_Id;
5747 New_Else : Node_Id;
5748 New_N : Node_Id;
5749 New_Then : Node_Id;
5750
5751 Optimize_Assignment_Stmt : Boolean := False;
5752 -- Small optimization: when the if expression appears in the context of
5753 -- a safe assignment statement, expand into
5754
5755 -- if cond then
5756 -- lhs := then-expr;
5757 -- else
5758 -- lhs := else-expr;
5759 -- end if;
5760
5761 -- This makes the expansion much more efficient in the context of an
5762 -- aggregate converted into assignments.
5763
5764 Optimize_Return_Stmt : Boolean := False;
5765 -- Small optimization: when the if expression appears in the context of
5766 -- a simple return statement, expand into
5767
5768 -- if cond then
5769 -- return then-expr;
5770 -- else
5771 -- return else-expr;
5772 -- end if;
5773
5774 -- This makes the expansion much easier when expressions are calls to
5775 -- build-in-place functions.
5776
5777 Optimize_Object_Decl : Boolean := False;
5778 -- Small optimization: when the if expression appears in the context of
5779 -- an object declaration of a type not Is_Copy_Type, expand into
5780
5781 -- if cond then
5782 -- then-obj : typ := then_expr;
5783 -- target := then-obj'Unrestricted_Access;
5784 -- else
5785 -- else-obj : typ := else-expr;
5786 -- target := else-obj'Unrestricted_Access;
5787 -- end if;
5788 --
5789 -- obj : typ renames target.all;
5790
5791 -- This makes the expansion much easier when expressions are calls to
5792 -- build-in-place functions.
5793
5794 -- Start of processing for Expand_N_If_Expression
5795
5796 begin
5797 -- If the expression is in the context of a simple return statement,
5798 -- possibly through intermediate conditional expressions, we delay
5799 -- expansion until the (immediate) parent is rewritten as a return
5800 -- statement (or is already the return statement). Likewise if it is
5801 -- in the context of an object declaration that can be optimized.
5802 -- Note that this deals with the case of the elsif part of the if
5803 -- expression, if it exists.
5804
5805 if not Expansion_Delayed (N) then
5806 declare
5807 Uncond_Par : constant Node_Id := Unconditional_Parent (N);
5808 begin
5809 if Nkind (Uncond_Par) = N_Simple_Return_Statement
5810 or else Is_Optimizable_Declaration (Uncond_Par)
5811 then
5812 Delay_Conditional_Expressions_Between (N, Uncond_Par);
5813 end if;
5814 end;
5815 end if;
5816
5817 -- If the expansion of the expression has been delayed, we wait for the
5818 -- rewriting of its parent as an assignment statement, or as as return
5819 -- statement or as an object declaration; when that's done, we optimize
5820 -- the assignment, return or declaration (the purpose of the delaying).
5821
5822 if Expansion_Delayed (N) then
5823 if Nkind (Par) = N_Assignment_Statement then
5824 Optimize_Assignment_Stmt := True;
5825
5826 elsif Nkind (Par) = N_Simple_Return_Statement then
5827 Optimize_Return_Stmt := True;
5828
5829 elsif Is_Optimizable_Declaration (Par) then
5830 Optimize_Object_Decl := True;
5831
5832 else
5833 return;
5834 end if;
5835
5836 Set_Expansion_Delayed (N, False);
5837 end if;
5838
5839 -- Deal with non-standard booleans
5840
5841 Adjust_Condition (Cond);
5842
5843 -- Check for MINIMIZED/ELIMINATED overflow mode.
5844 -- Apply_Arithmetic_Overflow_Check will not deal with Then/Else_Actions
5845 -- so skip this step if any actions are present.
5846
5847 if Minimized_Eliminated_Overflow_Check (N)
5848 and then No (Then_Actions (N))
5849 and then No (Else_Actions (N))
5850 then
5851 Apply_Arithmetic_Overflow_Check (N);
5852 return;
5853 end if;
5854
5855 -- Fold at compile time if condition known. We have already folded
5856 -- static if expressions, but it is possible to fold any case in which
5857 -- the condition is known at compile time, even though the result is
5858 -- non-static.
5859
5860 -- Note that we don't do the fold of such cases in Sem_Elab because
5861 -- it can cause infinite loops with the expander adding a conditional
5862 -- expression, and Sem_Elab circuitry removing it repeatedly.
5863
5864 if Compile_Time_Known_Value (Cond) then
5865 if Is_True (Expr_Value (Cond)) then
5866 Expr := Relocate_Node (Thenx);
5867 Actions := Then_Actions (N);
5868 else
5869 Expr := Relocate_Node (Elsex);
5870 Actions := Else_Actions (N);
5871 end if;
5872
5873 -- When the "then" or "else" expressions involve controlled function
5874 -- calls, generated temporaries are chained on the corresponding list
5875 -- of actions. These temporaries need to be finalized after the if
5876 -- expression is evaluated.
5877
5878 Process_Transients_In_Expression (N, Actions);
5879
5880 -- If the expression is itself a conditional expression whose
5881 -- expansion has been delayed, analyze it again and expand it.
5882
5883 if Is_Delayed_Conditional_Expression (Expr) then
5884 Unanalyze_Delayed_Conditional_Expression (Expr);
5885 end if;
5886
5887 Insert_Actions (N, Actions);
5888 Rewrite (N, Expr);
5889 Analyze_And_Resolve (N, Typ);
5890
5891 -- Note that the result is never static (legitimate cases of
5892 -- static if expressions were folded in Sem_Eval).
5893
5894 Set_Is_Static_Expression (N, False);
5895 return;
5896
5897 -- Build an if statement assigning each dependent expression to the
5898 -- target separately. The (main) use case is aggregate elaboration.
5899
5900 elsif Optimize_Assignment_Stmt then
5901 Remove_Side_Effects (Name (Par), Name_Req => True);
5902
5903 -- When the "then" or "else" expressions involve controlled function
5904 -- calls, generated temporaries are chained on the corresponding list
5905 -- of actions. These temporaries need to be finalized after the if
5906 -- expression is evaluated.
5907
5908 Process_Transients_In_Expression (N, Then_Actions (N));
5909 Process_Transients_In_Expression (N, Else_Actions (N));
5910
5911 -- We directly copy the parent node to preserve its flags
5912
5913 New_Then := New_Copy (Par);
5914 Set_Sloc (New_Then, Sloc (Thenx));
5915 Set_Name (New_Then, New_Copy_Tree (Name (Par)));
5916 Set_Expression (New_Then, Relocate_Node (Thenx));
5917
5918 -- If the expression is itself a conditional expression whose
5919 -- expansion has been delayed, analyze it again and expand it.
5920
5921 if Is_Delayed_Conditional_Expression (Expression (New_Then)) then
5922 Unanalyze_Delayed_Conditional_Expression (Expression (New_Then));
5923 end if;
5924
5925 New_Else := New_Copy (Par);
5926 Set_Sloc (New_Else, Sloc (Elsex));
5927 Set_Name (New_Else, New_Copy_Tree (Name (Par)));
5928 Set_Expression (New_Else, Relocate_Node (Elsex));
5929
5930 if Is_Delayed_Conditional_Expression (Expression (New_Else)) then
5931 Unanalyze_Delayed_Conditional_Expression (Expression (New_Else));
5932 end if;
5933
5934 If_Stmt :=
5935 Make_Implicit_If_Statement (N,
5936 Condition => Relocate_Node (Cond),
5937 Then_Statements => New_List (New_Then),
5938 Else_Statements => New_List (New_Else));
5939 Decl := Empty;
5940 New_N := Empty;
5941
5942 -- Preserve the original context for which the if statement is
5943 -- being generated. This is needed by the finalization machinery
5944 -- to prevent the premature finalization of controlled objects
5945 -- found within the if statement.
5946
5947 Set_From_Conditional_Expression (If_Stmt);
5948
5949 -- Build an if statement returning each dependent expression from the
5950 -- function separately. The main use case is expression function.
5951
5952 elsif Optimize_Return_Stmt then
5953 -- When the "then" or "else" expressions involve controlled function
5954 -- calls, generated temporaries are chained on the corresponding list
5955 -- of actions. These temporaries need to be finalized after the if
5956 -- expression is evaluated.
5957
5958 Process_Transients_In_Expression (N, Then_Actions (N));
5959 Process_Transients_In_Expression (N, Else_Actions (N));
5960
5961 New_Then := Relocate_Node (Thenx);
5962
5963 -- If the expression is itself a conditional expression whose
5964 -- expansion has been delayed, analyze it again and expand it.
5965
5966 if Is_Delayed_Conditional_Expression (New_Then) then
5967 Unanalyze_Delayed_Conditional_Expression (New_Then);
5968 end if;
5969
5970 New_Else := Relocate_Node (Elsex);
5971
5972 -- If the expression is itself a conditional expression whose
5973 -- expansion has been delayed, analyze it again and expand it.
5974
5975 if Is_Delayed_Conditional_Expression (New_Else) then
5976 Unanalyze_Delayed_Conditional_Expression (New_Else);
5977 end if;
5978
5979 If_Stmt :=
5980 Make_Implicit_If_Statement (N,
5981 Condition => Relocate_Node (Cond),
5982 Then_Statements => New_List (
5983 Make_Simple_Return_Statement (Sloc (New_Then),
5984 Expression => New_Then)),
5985 Else_Statements => New_List (
5986 Make_Simple_Return_Statement (Sloc (New_Else),
5987 Expression => New_Else)));
5988 Decl := Empty;
5989 New_N := Empty;
5990
5991 -- Preserve the original context for which the if statement is
5992 -- being generated. This is needed by the finalization machinery
5993 -- to prevent the premature finalization of controlled objects
5994 -- found within the if statement.
5995
5996 Set_From_Conditional_Expression (If_Stmt);
5997
5998 elsif Optimize_Object_Decl then
5999 -- When the "then" or "else" expressions involve controlled function
6000 -- calls, generated temporaries are chained on the corresponding list
6001 -- of actions. These temporaries need to be finalized after the if
6002 -- expression is evaluated.
6003
6004 Process_Transients_In_Expression (N, Then_Actions (N));
6005 Process_Transients_In_Expression (N, Else_Actions (N));
6006
6007 declare
6008 Par_Obj : constant Entity_Id := Defining_Identifier (Par);
6009 Then_Obj : constant Entity_Id := Make_Temporary (Loc, 'C', Thenx);
6010 Else_Obj : constant Entity_Id := Make_Temporary (Loc, 'C', Elsex);
6011 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6012 Target : constant Entity_Id := Make_Temporary (Loc, 'C', N);
6013
6014 begin
6015 Insert_Conditional_Object_Declaration
6016 (Then_Obj, Typ, Thenx, Const => Constant_Present (Par));
6017 Insert_Conditional_Object_Declaration
6018 (Else_Obj, Typ, Elsex, Const => Constant_Present (Par));
6019
6020 -- Generate:
6021 -- type Ptr_Typ is not null access all [constant] Typ;
6022
6023 Insert_Action (Par,
6024 Make_Full_Type_Declaration (Loc,
6025 Defining_Identifier => Ptr_Typ,
6026 Type_Definition =>
6027 Make_Access_To_Object_Definition (Loc,
6028 All_Present => True,
6029 Null_Exclusion_Present => True,
6030 Subtype_Indication => New_Occurrence_Of (Typ, Loc),
6031 Constant_Present => Constant_Present (Par))));
6032
6033 -- Generate:
6034 -- Target : Ptr_Typ;
6035
6036 Decl :=
6037 Make_Object_Declaration (Loc,
6038 Defining_Identifier => Target,
6039 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
6040 Set_No_Initialization (Decl);
6041 Insert_Action (Par, Decl);
6042
6043 -- Generate:
6044 -- if Cond then
6045 -- Target := <Then_Obj>'Unrestricted_Access;
6046 -- else
6047 -- Target := <Else_Obj>'Unrestricted_Access;
6048 -- end if;
6049
6050 If_Stmt :=
6051 Make_Implicit_If_Statement (N,
6052 Condition => Relocate_Node (Cond),
6053 Then_Statements => New_List (
6054 Make_Assignment_Statement (Sloc (Thenx),
6055 Name => New_Occurrence_Of (Target, Sloc (Thenx)),
6056 Expression =>
6057 Make_Attribute_Reference (Loc,
6058 Prefix => New_Occurrence_Of (Then_Obj, Loc),
6059 Attribute_Name => Name_Unrestricted_Access))),
6060
6061 Else_Statements => New_List (
6062 Make_Assignment_Statement (Sloc (Elsex),
6063 Name => New_Occurrence_Of (Target, Sloc (Elsex)),
6064 Expression =>
6065 Make_Attribute_Reference (Loc,
6066 Prefix => New_Occurrence_Of (Else_Obj, Loc),
6067 Attribute_Name => Name_Unrestricted_Access))));
6068
6069 -- Preserve the original context for which the if statement is
6070 -- being generated. This is needed by the finalization machinery
6071 -- to prevent the premature finalization of controlled objects
6072 -- found within the if statement.
6073
6074 Set_From_Conditional_Expression (If_Stmt);
6075
6076 New_N :=
6077 Make_Explicit_Dereference (Loc,
6078 Prefix => New_Occurrence_Of (Target, Loc));
6079
6080 -- The renaming is not analyzed so complete the decoration of the
6081 -- object and set the type of the name directly.
6082
6083 if Constant_Present (Par) then
6084 Mutate_Ekind (Par_Obj, E_Constant);
6085 Set_Is_True_Constant (Par_Obj);
6086 else
6087 Mutate_Ekind (Par_Obj, E_Variable);
6088 end if;
6089
6090 Set_Etype (New_N, Etype (Par_Obj));
6091 end;
6092
6093 -- If the result is a unidimensional unconstrained array but the two
6094 -- dependent expressions have constrained subtypes with known bounds,
6095 -- then we expand as follows:
6096
6097 -- subtype Txx is Typ (<static low-bound> .. <static high-bound>);
6098 -- Cnn : Txx;
6099 -- if cond then
6100 -- <<then actions>>
6101 -- Cnn (<then low-bound .. then high-bound>) := then-expr;
6102 -- else
6103 -- <<else actions>>
6104 -- Cnn (<else low bound .. else high-bound>) := else-expr;
6105 -- end if;
6106
6107 -- and replace the if expression by a slice of Cnn, provided that Txx
6108 -- is not too large. This will create a static temporary instead of the
6109 -- dynamic one of the next case and thus help the code generator.
6110
6111 -- Note that we need to deal with the case where the else expression is
6112 -- itself such a slice, in order to catch if expressions with more than
6113 -- two dependent expressions in the source code.
6114
6115 -- Also note that this creates variables on branches without an explicit
6116 -- scope, causing troubles with e.g. the LLVM IR, so disable this
6117 -- optimization when Unnest_Subprogram_Mode (enabled for LLVM).
6118
6119 elsif Is_Array_Type (Typ)
6120 and then Number_Dimensions (Typ) = 1
6121 and then not Is_Constrained (Typ)
6122 and then not Is_By_Reference_Type (Typ)
6123 and then Is_Constrained (Etype (Thenx))
6124 and then Compile_Time_Known_Bounds (Etype (Thenx))
6125 and then
6126 ((Is_Constrained (Etype (Elsex))
6127 and then Compile_Time_Known_Bounds (Etype (Elsex))
6128 and then OK_For_Single_Subtype (Etype (Thenx), Etype (Elsex)))
6129 or else
6130 (Nkind (Elsex) = N_Slice
6131 and then Is_Constrained (Etype (Prefix (Elsex)))
6132 and then Compile_Time_Known_Bounds (Etype (Prefix (Elsex)))
6133 and then
6134 OK_For_Single_Subtype (Etype (Thenx), Etype (Prefix (Elsex)))))
6135 and then not Unnest_Subprogram_Mode
6136 then
6137 -- When the "then" or "else" expressions involve controlled function
6138 -- calls, generated temporaries are chained on the corresponding list
6139 -- of actions. These temporaries need to be finalized after the if
6140 -- expression is evaluated.
6141
6142 Process_Transients_In_Expression (N, Then_Actions (N));
6143 Process_Transients_In_Expression (N, Else_Actions (N));
6144
6145 declare
6146 Ityp : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
6147
6148 function Build_New_Bound
6149 (Then_Bnd : Uint;
6150 Else_Bnd : Uint;
6151 Slice_Bnd : Node_Id) return Node_Id;
6152 -- Build a new bound from the bounds of the if expression
6153
6154 function To_Ityp (V : Uint) return Node_Id;
6155 -- Convert V to an index value in Ityp
6156
6157 ---------------------
6158 -- Build_New_Bound --
6159 ---------------------
6160
6161 function Build_New_Bound
6162 (Then_Bnd : Uint;
6163 Else_Bnd : Uint;
6164 Slice_Bnd : Node_Id) return Node_Id is
6165
6166 begin
6167 -- We need to use the special processing for slices only if
6168 -- they do not have compile-time known bounds; if they do, they
6169 -- can be treated like any other expressions.
6170
6171 if Nkind (Elsex) = N_Slice
6172 and then not Compile_Time_Known_Bounds (Etype (Elsex))
6173 then
6174 if Compile_Time_Known_Value (Slice_Bnd)
6175 and then Expr_Value (Slice_Bnd) = Then_Bnd
6176 then
6177 return To_Ityp (Then_Bnd);
6178
6179 else
6180 return Make_If_Expression (Loc,
6181 Expressions => New_List (
6182 Duplicate_Subexpr (Cond),
6183 To_Ityp (Then_Bnd),
6184 New_Copy_Tree (Slice_Bnd)));
6185 end if;
6186
6187 elsif Then_Bnd = Else_Bnd then
6188 return To_Ityp (Then_Bnd);
6189
6190 else
6191 return Make_If_Expression (Loc,
6192 Expressions => New_List (
6193 Duplicate_Subexpr (Cond),
6194 To_Ityp (Then_Bnd),
6195 To_Ityp (Else_Bnd)));
6196 end if;
6197 end Build_New_Bound;
6198
6199 -------------
6200 -- To_Ityp --
6201 -------------
6202
6203 function To_Ityp (V : Uint) return Node_Id is
6204 Result : constant Node_Id := Make_Integer_Literal (Loc, V);
6205
6206 begin
6207 if Is_Enumeration_Type (Ityp) then
6208 return
6209 Make_Attribute_Reference (Loc,
6210 Prefix => New_Occurrence_Of (Ityp, Loc),
6211 Attribute_Name => Name_Val,
6212 Expressions => New_List (Result));
6213 else
6214 return Result;
6215 end if;
6216 end To_Ityp;
6217
6218 Ent : Node_Id;
6219 Slice_Lo, Slice_Hi : Node_Id;
6220 Subtyp_Ind : Node_Id;
6221 Else_Lo, Else_Hi : Uint;
6222 Min_Lo, Max_Hi : Uint;
6223 Then_Lo, Then_Hi : Uint;
6224 Then_List, Else_List : List_Id;
6225
6226 begin
6227 Get_First_Index_Bounds (Etype (Thenx), Then_Lo, Then_Hi);
6228
6229 -- See the rationale in Build_New_Bound
6230
6231 if Nkind (Elsex) = N_Slice
6232 and then not Compile_Time_Known_Bounds (Etype (Elsex))
6233 then
6234 Slice_Lo := Low_Bound (Discrete_Range (Elsex));
6235 Slice_Hi := High_Bound (Discrete_Range (Elsex));
6236 Get_First_Index_Bounds
6237 (Etype (Prefix (Elsex)), Else_Lo, Else_Hi);
6238
6239 else
6240 Slice_Lo := Empty;
6241 Slice_Hi := Empty;
6242 Get_First_Index_Bounds (Etype (Elsex), Else_Lo, Else_Hi);
6243 end if;
6244
6245 Min_Lo := UI_Min (Then_Lo, Else_Lo);
6246 Max_Hi := UI_Max (Then_Hi, Else_Hi);
6247
6248 -- Now we construct an array object with appropriate bounds and
6249 -- mark it as internal to prevent useless initialization when
6250 -- Initialize_Scalars is enabled. Also since this is the actual
6251 -- result entity, we make sure we have debug information for it.
6252
6253 Subtyp_Ind :=
6254 Make_Subtype_Indication (Loc,
6255 Subtype_Mark => New_Occurrence_Of (Typ, Loc),
6256 Constraint =>
6257 Make_Index_Or_Discriminant_Constraint (Loc,
6258 Constraints => New_List (
6259 Make_Range (Loc,
6260 Low_Bound => To_Ityp (Min_Lo),
6261 High_Bound => To_Ityp (Max_Hi)))));
6262
6263 Ent := Make_Temporary (Loc, 'C');
6264 Set_Is_Internal (Ent);
6265 Set_Debug_Info_Needed (Ent);
6266
6267 Decl :=
6268 Make_Object_Declaration (Loc,
6269 Defining_Identifier => Ent,
6270 Object_Definition => Subtyp_Ind);
6271
6272 -- If the result of the expression appears as the initializing
6273 -- expression of an object declaration, we can just rename the
6274 -- result, rather than copying it.
6275
6276 Mutate_Ekind (Ent, E_Variable);
6277 Set_OK_To_Rename (Ent);
6278
6279 Then_List := New_List (
6280 Make_Assignment_Statement (Loc,
6281 Name =>
6282 Make_Slice (Loc,
6283 Prefix => New_Occurrence_Of (Ent, Loc),
6284 Discrete_Range =>
6285 Make_Range (Loc,
6286 Low_Bound => To_Ityp (Then_Lo),
6287 High_Bound => To_Ityp (Then_Hi))),
6288 Expression => Relocate_Node (Thenx)));
6289
6290 Set_Suppress_Assignment_Checks (Last (Then_List));
6291
6292 -- See the rationale in Build_New_Bound
6293
6294 if Nkind (Elsex) = N_Slice
6295 and then not Compile_Time_Known_Bounds (Etype (Elsex))
6296 then
6297 Else_List := New_List (
6298 Make_Assignment_Statement (Loc,
6299 Name =>
6300 Make_Slice (Loc,
6301 Prefix => New_Occurrence_Of (Ent, Loc),
6302 Discrete_Range =>
6303 Make_Range (Loc,
6304 Low_Bound => New_Copy_Tree (Slice_Lo),
6305 High_Bound => New_Copy_Tree (Slice_Hi))),
6306 Expression => Relocate_Node (Elsex)));
6307
6308 else
6309 Else_List := New_List (
6310 Make_Assignment_Statement (Loc,
6311 Name =>
6312 Make_Slice (Loc,
6313 Prefix => New_Occurrence_Of (Ent, Loc),
6314 Discrete_Range =>
6315 Make_Range (Loc,
6316 Low_Bound => To_Ityp (Else_Lo),
6317 High_Bound => To_Ityp (Else_Hi))),
6318 Expression => Relocate_Node (Elsex)));
6319 end if;
6320
6321 Set_Suppress_Assignment_Checks (Last (Else_List));
6322
6323 If_Stmt :=
6324 Make_Implicit_If_Statement (N,
6325 Condition => Duplicate_Subexpr (Cond),
6326 Then_Statements => Then_List,
6327 Else_Statements => Else_List);
6328
6329 New_N :=
6330 Make_Slice (Loc,
6331 Prefix => New_Occurrence_Of (Ent, Loc),
6332 Discrete_Range => Make_Range (Loc,
6333 Low_Bound => Build_New_Bound (Then_Lo, Else_Lo, Slice_Lo),
6334 High_Bound => Build_New_Bound (Then_Hi, Else_Hi, Slice_Hi)));
6335 end;
6336
6337 -- If the type cannot be copied, then we expand as follows to avoid the
6338 -- possibility of improper copying.
6339
6340 -- type Ptr_Typ is not null access all Typ;
6341 -- Target : Ptr;
6342 -- if cond then
6343 -- <<then actions>>
6344 -- Target := then-expr'Unrestricted_Access;
6345 -- else
6346 -- <<else actions>>
6347 -- Target := else-expr'Unrestricted_Access;
6348 -- end if;
6349
6350 -- and replace the if expression by a reference to Target.all.
6351
6352 elsif not Is_Copy_Type (Typ) then
6353 -- When the "then" or "else" expressions involve controlled function
6354 -- calls, generated temporaries are chained on the corresponding list
6355 -- of actions. These temporaries need to be finalized after the if
6356 -- expression is evaluated.
6357
6358 Process_Transients_In_Expression (N, Then_Actions (N));
6359 Process_Transients_In_Expression (N, Else_Actions (N));
6360
6361 declare
6362 Ptr_Typ : constant Entity_Id := Make_Temporary (Loc, 'A');
6363 Target : constant Entity_Id := Make_Temporary (Loc, 'C', N);
6364
6365 begin
6366 -- Generate:
6367 -- type Ptr_Typ is not null access all Typ;
6368
6369 Insert_Action (N,
6370 Make_Full_Type_Declaration (Loc,
6371 Defining_Identifier => Ptr_Typ,
6372 Type_Definition =>
6373 Make_Access_To_Object_Definition (Loc,
6374 All_Present => True,
6375 Null_Exclusion_Present => True,
6376 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
6377
6378 -- Generate:
6379 -- Target : Ptr_Typ;
6380
6381 Decl :=
6382 Make_Object_Declaration (Loc,
6383 Defining_Identifier => Target,
6384 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
6385 Set_No_Initialization (Decl);
6386
6387 -- Generate:
6388 -- if Cond then
6389 -- Target := <Thenx>'Unrestricted_Access;
6390 -- else
6391 -- Target := <Elsex>'Unrestricted_Access;
6392 -- end if;
6393
6394 If_Stmt :=
6395 Make_Implicit_If_Statement (N,
6396 Condition => Relocate_Node (Cond),
6397 Then_Statements => New_List (
6398 Make_Assignment_Statement (Sloc (Thenx),
6399 Name => New_Occurrence_Of (Target, Sloc (Thenx)),
6400 Expression =>
6401 Make_Attribute_Reference (Loc,
6402 Prefix => Relocate_Node (Thenx),
6403 Attribute_Name => Name_Unrestricted_Access))),
6404
6405 Else_Statements => New_List (
6406 Make_Assignment_Statement (Sloc (Elsex),
6407 Name => New_Occurrence_Of (Target, Sloc (Elsex)),
6408 Expression =>
6409 Make_Attribute_Reference (Loc,
6410 Prefix => Relocate_Node (Elsex),
6411 Attribute_Name => Name_Unrestricted_Access))));
6412
6413 -- Preserve the original context for which the if statement is
6414 -- being generated. This is needed by the finalization machinery
6415 -- to prevent the premature finalization of controlled objects
6416 -- found within the if statement.
6417
6418 Set_From_Conditional_Expression (If_Stmt);
6419
6420 New_N :=
6421 Make_Explicit_Dereference (Loc,
6422 Prefix => New_Occurrence_Of (Target, Loc));
6423 end;
6424
6425 -- For other types, we only need to expand if there are other actions
6426 -- associated with either branch or we need to force expansion to deal
6427 -- with if expressions used as an actual of an anonymous access type.
6428
6429 elsif Present (Then_Actions (N))
6430 or else Present (Else_Actions (N))
6431 or else Force_Expand
6432 then
6433 -- We now wrap the actions into the appropriate expression
6434
6435 -- We do not need to call Process_Transients_In_Expression on
6436 -- the list of actions in this case, because the expansion of
6437 -- Expression_With_Actions will do it.
6438
6439 if Present (Then_Actions (N)) then
6440 Rewrite (Thenx,
6441 Make_Expression_With_Actions (Sloc (Thenx),
6442 Actions => Then_Actions (N),
6443 Expression => Relocate_Node (Thenx)));
6444
6445 Set_Then_Actions (N, No_List);
6446 Analyze_And_Resolve (Thenx, Typ);
6447 end if;
6448
6449 if Present (Else_Actions (N)) then
6450 Rewrite (Elsex,
6451 Make_Expression_With_Actions (Sloc (Elsex),
6452 Actions => Else_Actions (N),
6453 Expression => Relocate_Node (Elsex)));
6454
6455 Set_Else_Actions (N, No_List);
6456 Analyze_And_Resolve (Elsex, Typ);
6457 end if;
6458
6459 -- We must force expansion into an expression with actions when
6460 -- an if expression gets used directly as an actual for an
6461 -- anonymous access type.
6462
6463 if Force_Expand then
6464 declare
6465 Cnn : constant Entity_Id := Make_Temporary (Loc, 'C');
6466 Acts : List_Id;
6467 begin
6468 Acts := New_List;
6469
6470 -- Generate:
6471 -- Cnn : Ann;
6472
6473 Decl :=
6474 Make_Object_Declaration (Loc,
6475 Defining_Identifier => Cnn,
6476 Object_Definition => New_Occurrence_Of (Typ, Loc));
6477 Append_To (Acts, Decl);
6478
6479 Set_No_Initialization (Decl);
6480
6481 -- Generate:
6482 -- if Cond then
6483 -- Cnn := <Thenx>;
6484 -- else
6485 -- Cnn := <Elsex>;
6486 -- end if;
6487
6488 If_Stmt :=
6489 Make_Implicit_If_Statement (N,
6490 Condition => Relocate_Node (Cond),
6491 Then_Statements => New_List (
6492 Make_Assignment_Statement (Sloc (Thenx),
6493 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
6494 Expression => Relocate_Node (Thenx))),
6495
6496 Else_Statements => New_List (
6497 Make_Assignment_Statement (Sloc (Elsex),
6498 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
6499 Expression => Relocate_Node (Elsex))));
6500 Append_To (Acts, If_Stmt);
6501
6502 -- Generate:
6503 -- do
6504 -- ...
6505 -- in Cnn end;
6506
6507 Rewrite (N,
6508 Make_Expression_With_Actions (Loc,
6509 Expression => New_Occurrence_Of (Cnn, Loc),
6510 Actions => Acts));
6511 Analyze_And_Resolve (N, Typ);
6512 end;
6513 end if;
6514
6515 return;
6516
6517 -- For the sake of GNATcoverage, generate an intermediate temporary in
6518 -- the case where the if expression is a condition in an outer decision,
6519 -- in order to make sure that no branch is shared between the decisions.
6520
6521 elsif Opt.Suppress_Control_Flow_Optimizations
6522 and then Nkind (Original_Node (Par)) in N_Case_Expression
6523 | N_Case_Statement
6524 | N_If_Expression
6525 | N_If_Statement
6526 | N_Goto_When_Statement
6527 | N_Loop_Statement
6528 | N_Return_When_Statement
6529 | N_Short_Circuit
6530 then
6531 declare
6532 Cnn : constant Entity_Id := Make_Temporary (Loc, 'C');
6533 Acts : List_Id;
6534
6535 begin
6536 -- Generate:
6537 -- do
6538 -- Cnn : constant Typ := N;
6539 -- in Cnn end
6540
6541 Acts := New_List (
6542 Make_Object_Declaration (Loc,
6543 Defining_Identifier => Cnn,
6544 Constant_Present => True,
6545 Object_Definition => New_Occurrence_Of (Typ, Loc),
6546 Expression => Relocate_Node (N)));
6547
6548 Rewrite (N,
6549 Make_Expression_With_Actions (Loc,
6550 Expression => New_Occurrence_Of (Cnn, Loc),
6551 Actions => Acts));
6552
6553 Analyze_And_Resolve (N, Typ);
6554 return;
6555 end;
6556
6557 -- If no actions then no expansion needed, gigi will handle it using the
6558 -- same approach as a C conditional expression.
6559
6560 else
6561 return;
6562 end if;
6563
6564 -- Fall through here for either the limited expansion, or the case of
6565 -- inserting actions for nonlimited types. In both these cases, we must
6566 -- move the SLOC of the parent If statement to the newly created one and
6567 -- change it to the SLOC of the expression which, after expansion, will
6568 -- correspond to what is being evaluated.
6569
6570 if Present (Par) and then Nkind (Par) = N_If_Statement then
6571 Set_Sloc (If_Stmt, Sloc (Par));
6572 Set_Sloc (Par, Loc);
6573 end if;
6574
6575 -- Move Then_Actions and Else_Actions, if any, to the new if statement
6576
6577 if Present (Then_Actions (N)) then
6578 Prepend_List (Then_Actions (N), Then_Statements (If_Stmt));
6579 end if;
6580
6581 if Present (Else_Actions (N)) then
6582 Prepend_List (Else_Actions (N), Else_Statements (If_Stmt));
6583 end if;
6584
6585 -- Rewrite the parent statement as an if statement
6586
6587 if Optimize_Assignment_Stmt or else Optimize_Return_Stmt then
6588 Rewrite (Par, If_Stmt);
6589 Analyze (Par);
6590
6591 elsif Optimize_Object_Decl then
6592 Insert_Action (Par, If_Stmt);
6593 Rewrite_Object_Declaration_As_Renaming (Par, New_N);
6594
6595 -- Otherwise rewrite the if expression itself
6596
6597 else
6598 Insert_Action (N, Decl);
6599 Insert_Action (N, If_Stmt);
6600 Rewrite (N, New_N);
6601 Analyze_And_Resolve (N, Typ);
6602 end if;
6603 end Expand_N_If_Expression;
6604
6605 -----------------
6606 -- Expand_N_In --
6607 -----------------
6608
6609 procedure Expand_N_In (N : Node_Id) is
6610 Loc : constant Source_Ptr := Sloc (N);
6611 Restyp : constant Entity_Id := Etype (N);
6612 Lop : constant Node_Id := Left_Opnd (N);
6613 Rop : constant Node_Id := Right_Opnd (N);
6614 Static : constant Boolean := Is_OK_Static_Expression (N);
6615
6616 procedure Substitute_Valid_Test;
6617 -- Replaces node N by Lop'Valid. This is done when we have an explicit
6618 -- test for the left operand being in range of its subtype.
6619
6620 ---------------------------
6621 -- Substitute_Valid_Test --
6622 ---------------------------
6623
6624 procedure Substitute_Valid_Test is
6625 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean;
6626 -- Determine whether arbitrary node Nod denotes a source object that
6627 -- may safely act as prefix of attribute 'Valid.
6628
6629 ----------------------------
6630 -- Is_OK_Object_Reference --
6631 ----------------------------
6632
6633 function Is_OK_Object_Reference (Nod : Node_Id) return Boolean is
6634 Obj_Ref : constant Node_Id := Original_Node (Nod);
6635 -- The original operand
6636
6637 begin
6638 -- The object reference must be a source construct, otherwise the
6639 -- codefix suggestion may refer to nonexistent code from a user
6640 -- perspective.
6641
6642 return Comes_From_Source (Obj_Ref)
6643 and then Is_Object_Reference (Unqual_Conv (Obj_Ref));
6644 end Is_OK_Object_Reference;
6645
6646 -- Start of processing for Substitute_Valid_Test
6647
6648 begin
6649 Rewrite (N,
6650 Make_Attribute_Reference (Loc,
6651 Prefix => Relocate_Node (Lop),
6652 Attribute_Name => Name_Valid));
6653
6654 Analyze_And_Resolve (N, Restyp);
6655
6656 -- Emit a warning when the left-hand operand of the membership test
6657 -- is a source object, otherwise the use of attribute 'Valid would be
6658 -- illegal. The warning is not given when overflow checking is either
6659 -- MINIMIZED or ELIMINATED, as the danger of optimization has been
6660 -- eliminated above.
6661
6662 if Is_OK_Object_Reference (Lop)
6663 and then Overflow_Check_Mode not in Minimized_Or_Eliminated
6664 then
6665 Error_Msg_N
6666 ("??explicit membership test may be optimized away", N);
6667 Error_Msg_N -- CODEFIX
6668 ("\??use ''Valid attribute instead", N);
6669 end if;
6670 end Substitute_Valid_Test;
6671
6672 -- Local variables
6673
6674 Ltyp : Entity_Id;
6675 Rtyp : Entity_Id;
6676
6677 -- Start of processing for Expand_N_In
6678
6679 begin
6680 -- If set membership case, expand with separate procedure
6681
6682 if Present (Alternatives (N)) then
6683 Expand_Set_Membership (N);
6684 return;
6685 end if;
6686
6687 -- Not set membership, proceed with expansion
6688
6689 Ltyp := Etype (Left_Opnd (N));
6690 Rtyp := Etype (Right_Opnd (N));
6691
6692 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
6693 -- type, then expand with a separate procedure. Note the use of the
6694 -- flag No_Minimize_Eliminate to prevent infinite recursion.
6695
6696 if Minimized_Eliminated_Overflow_Check (Left_Opnd (N))
6697 and then not No_Minimize_Eliminate (N)
6698 then
6699 Expand_Membership_Minimize_Eliminate_Overflow (N);
6700 return;
6701 end if;
6702
6703 -- Check case of explicit test for an expression in range of its
6704 -- subtype. This is suspicious usage and we replace it with a 'Valid
6705 -- test and give a warning for scalar types.
6706
6707 if Is_Scalar_Type (Ltyp)
6708
6709 -- Only relevant for source comparisons
6710
6711 and then Comes_From_Source (N)
6712
6713 -- In floating-point this is a standard way to check for finite values
6714 -- and using 'Valid would typically be a pessimization.
6715
6716 and then not Is_Floating_Point_Type (Ltyp)
6717
6718 -- Don't give the message unless right operand is a type entity and
6719 -- the type of the left operand matches this type. Note that this
6720 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
6721 -- checks have changed the type of the left operand.
6722
6723 and then Is_Entity_Name (Rop)
6724 and then Ltyp = Entity (Rop)
6725
6726 -- Skip this for predicated types, where such expressions are a
6727 -- reasonable way of testing if something meets the predicate.
6728
6729 and then No (Predicate_Function (Ltyp))
6730 then
6731 Substitute_Valid_Test;
6732 return;
6733 end if;
6734
6735 -- Do validity check on operands
6736
6737 if Validity_Checks_On and Validity_Check_Operands then
6738 Ensure_Valid (Left_Opnd (N));
6739 Validity_Check_Range (Right_Opnd (N));
6740 end if;
6741
6742 -- Case of explicit range
6743
6744 if Nkind (Rop) = N_Range then
6745 declare
6746 Lo : constant Node_Id := Low_Bound (Rop);
6747 Hi : constant Node_Id := High_Bound (Rop);
6748
6749 Lo_Orig : constant Node_Id := Original_Node (Lo);
6750 Hi_Orig : constant Node_Id := Original_Node (Hi);
6751 Rop_Orig : constant Node_Id := Original_Node (Rop);
6752
6753 Comes_From_Simple_Range_In_Source : constant Boolean :=
6754 Comes_From_Source (N)
6755 and then not
6756 (Is_Entity_Name (Rop_Orig)
6757 and then Is_Type (Entity (Rop_Orig))
6758 and then Present (Predicate_Function (Entity (Rop_Orig))));
6759 -- This is true for a membership test present in the source with a
6760 -- range or mark for a subtype that is not predicated. As already
6761 -- explained a few lines above, we do not want to give warnings on
6762 -- a test with a mark for a subtype that is predicated.
6763
6764 Warn : constant Boolean :=
6765 Constant_Condition_Warnings
6766 and then Comes_From_Simple_Range_In_Source
6767 and then not In_Instance;
6768 -- This must be true for any of the optimization warnings, we
6769 -- clearly want to give them only for source with the flag on. We
6770 -- also skip these warnings in an instance since it may be the
6771 -- case that different instantiations have different ranges.
6772
6773 Lcheck : Compare_Result;
6774 Ucheck : Compare_Result;
6775
6776 begin
6777 -- If test is explicit x'First .. x'Last, replace by 'Valid test
6778
6779 if Is_Scalar_Type (Ltyp)
6780
6781 -- Only relevant for source comparisons
6782
6783 and then Comes_From_Simple_Range_In_Source
6784
6785 -- And left operand is X'First where X matches left operand
6786 -- type (this eliminates cases of type mismatch, including
6787 -- the cases where ELIMINATED/MINIMIZED mode has changed the
6788 -- type of the left operand.
6789
6790 and then Nkind (Lo_Orig) = N_Attribute_Reference
6791 and then Attribute_Name (Lo_Orig) = Name_First
6792 and then Is_Entity_Name (Prefix (Lo_Orig))
6793 and then Entity (Prefix (Lo_Orig)) = Ltyp
6794
6795 -- Same tests for right operand
6796
6797 and then Nkind (Hi_Orig) = N_Attribute_Reference
6798 and then Attribute_Name (Hi_Orig) = Name_Last
6799 and then Is_Entity_Name (Prefix (Hi_Orig))
6800 and then Entity (Prefix (Hi_Orig)) = Ltyp
6801 then
6802 Substitute_Valid_Test;
6803 goto Leave;
6804 end if;
6805
6806 -- If bounds of type are known at compile time, and the end points
6807 -- are known at compile time and identical, this is another case
6808 -- for substituting a valid test. We only do this for discrete
6809 -- types, since it won't arise in practice for float types.
6810
6811 if Comes_From_Simple_Range_In_Source
6812 and then Is_Discrete_Type (Ltyp)
6813 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
6814 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
6815 and then Compile_Time_Known_Value (Lo)
6816 and then Compile_Time_Known_Value (Hi)
6817 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
6818 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
6819
6820 -- Kill warnings in instances, since they may be cases where we
6821 -- have a test in the generic that makes sense with some types
6822 -- and not with other types.
6823
6824 -- Similarly, do not rewrite membership as a 'Valid test if
6825 -- within the predicate function for the type.
6826
6827 -- Finally, if the original bounds are type conversions, even
6828 -- if they have been folded into constants, there are different
6829 -- types involved and 'Valid is not appropriate.
6830
6831 then
6832 if In_Instance
6833 or else (Ekind (Current_Scope) = E_Function
6834 and then Is_Predicate_Function (Current_Scope))
6835 then
6836 null;
6837
6838 elsif Nkind (Lo_Orig) = N_Type_Conversion
6839 or else Nkind (Hi_Orig) = N_Type_Conversion
6840 then
6841 null;
6842
6843 else
6844 Substitute_Valid_Test;
6845 goto Leave;
6846 end if;
6847 end if;
6848
6849 -- If we have an explicit range, do a bit of optimization based on
6850 -- range analysis (we may be able to kill one or both checks).
6851
6852 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
6853 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
6854
6855 -- If either check is known to fail, replace result by False since
6856 -- the other check does not matter. Preserve the static flag for
6857 -- legality checks, because we are constant-folding beyond RM 4.9.
6858
6859 if Lcheck = LT or else Ucheck = GT then
6860 if Warn then
6861 Error_Msg_N ("?c?range test optimized away", N);
6862 Error_Msg_N ("\?c?value is known to be out of range", N);
6863 end if;
6864
6865 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
6866 Analyze_And_Resolve (N, Restyp);
6867 Set_Is_Static_Expression (N, Static);
6868 goto Leave;
6869
6870 -- If both checks are known to succeed, replace result by True,
6871 -- since we know we are in range.
6872
6873 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6874 if Warn then
6875 Error_Msg_N ("?c?range test optimized away", N);
6876 Error_Msg_N ("\?c?value is known to be in range", N);
6877 end if;
6878
6879 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
6880 Analyze_And_Resolve (N, Restyp);
6881 Set_Is_Static_Expression (N, Static);
6882 goto Leave;
6883
6884 -- If lower bound check succeeds and upper bound check is not
6885 -- known to succeed or fail, then replace the range check with
6886 -- a comparison against the upper bound.
6887
6888 elsif Lcheck in Compare_GE then
6889 Rewrite (N,
6890 Make_Op_Le (Loc,
6891 Left_Opnd => Lop,
6892 Right_Opnd => High_Bound (Rop)));
6893 Analyze_And_Resolve (N, Restyp);
6894 goto Leave;
6895
6896 -- Inverse of previous case.
6897
6898 elsif Ucheck in Compare_LE then
6899 Rewrite (N,
6900 Make_Op_Ge (Loc,
6901 Left_Opnd => Lop,
6902 Right_Opnd => Low_Bound (Rop)));
6903 Analyze_And_Resolve (N, Restyp);
6904 goto Leave;
6905 end if;
6906
6907 -- We couldn't optimize away the range check, but there is one
6908 -- more issue. If we are checking constant conditionals, then we
6909 -- see if we can determine the outcome assuming everything is
6910 -- valid, and if so give an appropriate warning.
6911
6912 if Warn and then not Assume_No_Invalid_Values then
6913 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
6914 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
6915
6916 -- Result is out of range for valid value
6917
6918 if Lcheck = LT or else Ucheck = GT then
6919 Error_Msg_N
6920 ("?c?value can only be in range if it is invalid", N);
6921
6922 -- Result is in range for valid value
6923
6924 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
6925 Error_Msg_N
6926 ("?c?value can only be out of range if it is invalid", N);
6927 end if;
6928 end if;
6929 end;
6930
6931 -- Try to narrow the operation
6932
6933 if Ltyp = Universal_Integer and then Nkind (N) = N_In then
6934 Narrow_Large_Operation (N);
6935 end if;
6936
6937 -- For all other cases of an explicit range, nothing to be done
6938
6939 goto Leave;
6940
6941 -- Here right operand is a subtype mark
6942
6943 else
6944 declare
6945 Typ : Entity_Id := Etype (Rop);
6946 Is_Acc : constant Boolean := Is_Access_Type (Typ);
6947 Check_Null_Exclusion : Boolean;
6948 Cond : Node_Id := Empty;
6949 New_N : Node_Id;
6950 Obj : Node_Id := Lop;
6951 SCIL_Node : Node_Id;
6952
6953 begin
6954 Remove_Side_Effects (Obj);
6955
6956 -- For tagged type, do tagged membership operation
6957
6958 if Is_Tagged_Type (Typ) then
6959
6960 -- No expansion will be performed for VM targets, as the VM
6961 -- back ends will handle the membership tests directly.
6962
6963 if Tagged_Type_Expansion then
6964 Tagged_Membership (N, SCIL_Node, New_N);
6965 Rewrite (N, New_N);
6966 Analyze_And_Resolve (N, Restyp, Suppress => All_Checks);
6967
6968 -- Update decoration of relocated node referenced by the
6969 -- SCIL node.
6970
6971 if Generate_SCIL and then Present (SCIL_Node) then
6972 Set_SCIL_Node (N, SCIL_Node);
6973 end if;
6974 end if;
6975
6976 goto Leave;
6977
6978 -- If type is scalar type, rewrite as x in t'First .. t'Last.
6979 -- The reason we do this is that the bounds may have the wrong
6980 -- type if they come from the original type definition. Also this
6981 -- way we get all the processing above for an explicit range.
6982
6983 -- Don't do this for predicated types, since in this case we want
6984 -- to generate the predicate check at the end of the function.
6985
6986 elsif Is_Scalar_Type (Typ) then
6987 if No (Predicate_Function (Typ)) then
6988 Rewrite (Rop,
6989 Make_Range (Loc,
6990 Low_Bound =>
6991 Make_Attribute_Reference (Loc,
6992 Attribute_Name => Name_First,
6993 Prefix => New_Occurrence_Of (Typ, Loc)),
6994
6995 High_Bound =>
6996 Make_Attribute_Reference (Loc,
6997 Attribute_Name => Name_Last,
6998 Prefix => New_Occurrence_Of (Typ, Loc))));
6999
7000 Analyze_And_Resolve (N, Restyp);
7001 end if;
7002
7003 goto Leave;
7004
7005 -- Ada 2005 (AI95-0216 amended by AI12-0162): Program_Error is
7006 -- raised when evaluating an individual membership test if the
7007 -- subtype mark denotes a constrained Unchecked_Union subtype
7008 -- and the expression lacks inferable discriminants.
7009
7010 elsif Is_Unchecked_Union (Base_Type (Typ))
7011 and then Is_Constrained (Typ)
7012 and then not Has_Inferable_Discriminants (Lop)
7013 then
7014 Rewrite (N,
7015 Make_Expression_With_Actions (Loc,
7016 Actions =>
7017 New_List (Make_Raise_Program_Error (Loc,
7018 Reason => PE_Unchecked_Union_Restriction)),
7019 Expression =>
7020 New_Occurrence_Of (Standard_False, Loc)));
7021 Analyze_And_Resolve (N, Restyp);
7022
7023 goto Leave;
7024 end if;
7025
7026 -- Here we have a non-scalar type
7027
7028 if Is_Acc then
7029
7030 -- If the null exclusion checks are not compatible, need to
7031 -- perform further checks. In other words, we cannot have
7032 -- Ltyp including null or Lop being null, and Typ excluding
7033 -- null. All other cases are OK.
7034
7035 Check_Null_Exclusion :=
7036 Can_Never_Be_Null (Typ)
7037 and then (not Can_Never_Be_Null (Ltyp)
7038 or else Nkind (Lop) = N_Null);
7039 Typ := Designated_Type (Typ);
7040 end if;
7041
7042 if not Is_Constrained (Typ) then
7043 Cond := New_Occurrence_Of (Standard_True, Loc);
7044
7045 -- For the constrained array case, we have to check the subscripts
7046 -- for an exact match if the lengths are non-zero (the lengths
7047 -- must match in any case).
7048
7049 elsif Is_Array_Type (Typ) then
7050 Check_Subscripts : declare
7051 function Build_Attribute_Reference
7052 (E : Node_Id;
7053 Nam : Name_Id;
7054 Dim : Nat) return Node_Id;
7055 -- Build attribute reference E'Nam (Dim)
7056
7057 -------------------------------
7058 -- Build_Attribute_Reference --
7059 -------------------------------
7060
7061 function Build_Attribute_Reference
7062 (E : Node_Id;
7063 Nam : Name_Id;
7064 Dim : Nat) return Node_Id
7065 is
7066 begin
7067 return
7068 Make_Attribute_Reference (Loc,
7069 Prefix => E,
7070 Attribute_Name => Nam,
7071 Expressions => New_List (
7072 Make_Integer_Literal (Loc, Dim)));
7073 end Build_Attribute_Reference;
7074
7075 -- Start of processing for Check_Subscripts
7076
7077 begin
7078 for J in 1 .. Number_Dimensions (Typ) loop
7079 Evolve_And_Then (Cond,
7080 Make_Op_Eq (Loc,
7081 Left_Opnd =>
7082 Build_Attribute_Reference
7083 (Duplicate_Subexpr_No_Checks (Obj),
7084 Name_First, J),
7085 Right_Opnd =>
7086 Build_Attribute_Reference
7087 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
7088
7089 Evolve_And_Then (Cond,
7090 Make_Op_Eq (Loc,
7091 Left_Opnd =>
7092 Build_Attribute_Reference
7093 (Duplicate_Subexpr_No_Checks (Obj),
7094 Name_Last, J),
7095 Right_Opnd =>
7096 Build_Attribute_Reference
7097 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
7098 end loop;
7099 end Check_Subscripts;
7100
7101 -- These are the cases where constraint checks may be required,
7102 -- e.g. records with possible discriminants
7103
7104 else
7105 -- Expand the test into a series of discriminant comparisons.
7106 -- The expression that is built is the negation of the one that
7107 -- is used for checking discriminant constraints.
7108
7109 Obj := Relocate_Node (Left_Opnd (N));
7110
7111 if Has_Discriminants (Typ) then
7112 Cond := Make_Op_Not (Loc,
7113 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
7114 else
7115 Cond := New_Occurrence_Of (Standard_True, Loc);
7116 end if;
7117 end if;
7118
7119 if Is_Acc then
7120 if Check_Null_Exclusion then
7121 Cond := Make_And_Then (Loc,
7122 Left_Opnd =>
7123 Make_Op_Ne (Loc,
7124 Left_Opnd => Obj,
7125 Right_Opnd => Make_Null (Loc)),
7126 Right_Opnd => Cond);
7127 else
7128 Cond := Make_Or_Else (Loc,
7129 Left_Opnd =>
7130 Make_Op_Eq (Loc,
7131 Left_Opnd => Obj,
7132 Right_Opnd => Make_Null (Loc)),
7133 Right_Opnd => Cond);
7134 end if;
7135 end if;
7136
7137 Rewrite (N, Cond);
7138 Analyze_And_Resolve (N, Restyp);
7139
7140 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
7141 -- expression of an anonymous access type. This can involve an
7142 -- accessibility test and a tagged type membership test in the
7143 -- case of tagged designated types.
7144
7145 if Ada_Version >= Ada_2012
7146 and then Is_Acc
7147 and then Ekind (Ltyp) = E_Anonymous_Access_Type
7148 then
7149 declare
7150 Expr_Entity : Entity_Id := Empty;
7151 New_N : Node_Id;
7152 Param_Level : Node_Id;
7153 Type_Level : Node_Id;
7154
7155 begin
7156 if Is_Entity_Name (Lop) then
7157 Expr_Entity := Param_Entity (Lop);
7158
7159 if No (Expr_Entity) then
7160 Expr_Entity := Entity (Lop);
7161 end if;
7162 end if;
7163
7164 -- When restriction No_Dynamic_Accessibility_Checks is in
7165 -- effect, expand the membership test to a static value
7166 -- since we cannot rely on dynamic levels.
7167
7168 if No_Dynamic_Accessibility_Checks_Enabled (Lop) then
7169 if Static_Accessibility_Level
7170 (Lop, Object_Decl_Level)
7171 > Type_Access_Level (Rtyp)
7172 then
7173 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
7174 else
7175 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
7176 end if;
7177 Analyze_And_Resolve (N, Restyp);
7178
7179 -- If a conversion of the anonymous access value to the
7180 -- tested type would be illegal, then the result is False.
7181
7182 elsif not Valid_Conversion
7183 (Lop, Rtyp, Lop, Report_Errs => False)
7184 then
7185 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
7186 Analyze_And_Resolve (N, Restyp);
7187
7188 -- Apply an accessibility check if the access object has an
7189 -- associated access level and when the level of the type is
7190 -- less deep than the level of the access parameter. This
7191 -- can only occur for access parameters and stand-alone
7192 -- objects of an anonymous access type.
7193
7194 else
7195 Param_Level := Accessibility_Level
7196 (Expr_Entity, Dynamic_Level);
7197
7198 Type_Level :=
7199 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
7200
7201 -- Return True only if the accessibility level of the
7202 -- expression entity is not deeper than the level of
7203 -- the tested access type.
7204
7205 Rewrite (N,
7206 Make_And_Then (Loc,
7207 Left_Opnd => Relocate_Node (N),
7208 Right_Opnd => Make_Op_Le (Loc,
7209 Left_Opnd => Param_Level,
7210 Right_Opnd => Type_Level)));
7211
7212 Analyze_And_Resolve (N);
7213
7214 -- If the designated type is tagged, do tagged membership
7215 -- operation.
7216
7217 if Is_Tagged_Type (Typ) then
7218
7219 -- No expansion will be performed for VM targets, as
7220 -- the VM back ends will handle the membership tests
7221 -- directly.
7222
7223 if Tagged_Type_Expansion then
7224
7225 -- Note that we have to pass Original_Node, because
7226 -- the membership test might already have been
7227 -- rewritten by earlier parts of membership test.
7228
7229 Tagged_Membership
7230 (Original_Node (N), SCIL_Node, New_N);
7231
7232 -- Update decoration of relocated node referenced
7233 -- by the SCIL node.
7234
7235 if Generate_SCIL and then Present (SCIL_Node) then
7236 Set_SCIL_Node (New_N, SCIL_Node);
7237 end if;
7238
7239 Rewrite (N,
7240 Make_And_Then (Loc,
7241 Left_Opnd => Relocate_Node (N),
7242 Right_Opnd => New_N));
7243
7244 Analyze_And_Resolve (N, Restyp);
7245 end if;
7246 end if;
7247 end if;
7248 end;
7249 end if;
7250 end;
7251 end if;
7252
7253 -- At this point, we have done the processing required for the basic
7254 -- membership test, but not yet dealt with the predicate.
7255
7256 <<Leave>>
7257
7258 -- If a predicate is present, then we do the predicate test, but we
7259 -- most certainly want to omit this if we are within the predicate
7260 -- function itself, since otherwise we have an infinite recursion.
7261 -- The check should also not be emitted when testing against a range
7262 -- (the check is only done when the right operand is a subtype; see
7263 -- RM12-4.5.2 (28.1/3-30/3)).
7264
7265 Predicate_Check : declare
7266 function In_Range_Check return Boolean;
7267 -- Within an expanded range check that may raise Constraint_Error do
7268 -- not generate a predicate check as well. It is redundant because
7269 -- the context will add an explicit predicate check, and it will
7270 -- raise the wrong exception if it fails.
7271
7272 --------------------
7273 -- In_Range_Check --
7274 --------------------
7275
7276 function In_Range_Check return Boolean is
7277 P : Node_Id;
7278 begin
7279 P := Parent (N);
7280 while Present (P) loop
7281 if Nkind (P) = N_Raise_Constraint_Error then
7282 return True;
7283
7284 elsif Nkind (P) in N_Statement_Other_Than_Procedure_Call
7285 or else Nkind (P) = N_Procedure_Call_Statement
7286 or else Nkind (P) in N_Declaration
7287 then
7288 return False;
7289 end if;
7290
7291 P := Parent (P);
7292 end loop;
7293
7294 return False;
7295 end In_Range_Check;
7296
7297 -- Local variables
7298
7299 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
7300 R_Op : Node_Id;
7301
7302 -- Start of processing for Predicate_Check
7303
7304 begin
7305 if Present (PFunc)
7306 and then Current_Scope /= PFunc
7307 and then Nkind (Rop) /= N_Range
7308 then
7309 -- First apply the transformation that was skipped above
7310
7311 if Is_Scalar_Type (Rtyp) then
7312 Rewrite (Rop,
7313 Make_Range (Loc,
7314 Low_Bound =>
7315 Make_Attribute_Reference (Loc,
7316 Attribute_Name => Name_First,
7317 Prefix => New_Occurrence_Of (Rtyp, Loc)),
7318
7319 High_Bound =>
7320 Make_Attribute_Reference (Loc,
7321 Attribute_Name => Name_Last,
7322 Prefix => New_Occurrence_Of (Rtyp, Loc))));
7323
7324 Analyze_And_Resolve (N, Restyp);
7325 end if;
7326
7327 if not In_Range_Check then
7328 -- Indicate via Static_Mem parameter that this predicate
7329 -- evaluation is for a membership test.
7330 R_Op := Make_Predicate_Call (Rtyp, Lop, Static_Mem => True);
7331 else
7332 R_Op := New_Occurrence_Of (Standard_True, Loc);
7333 end if;
7334
7335 Rewrite (N,
7336 Make_And_Then (Loc,
7337 Left_Opnd => Relocate_Node (N),
7338 Right_Opnd => R_Op));
7339
7340 -- Analyze new expression, mark left operand as analyzed to
7341 -- avoid infinite recursion adding predicate calls. Similarly,
7342 -- suppress further range checks on the call.
7343
7344 Set_Analyzed (Left_Opnd (N));
7345 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7346 end if;
7347 end Predicate_Check;
7348 end Expand_N_In;
7349
7350 --------------------------------
7351 -- Expand_N_Indexed_Component --
7352 --------------------------------
7353
7354 procedure Expand_N_Indexed_Component (N : Node_Id) is
7355
7356 Wild_Reads_May_Have_Bad_Side_Effects : Boolean
7357 renames Validity_Check_Subscripts;
7358 -- This Boolean needs to be True if reading from a bad address can
7359 -- have a bad side effect (e.g., a segmentation fault that is not
7360 -- transformed into a Storage_Error exception, or interactions with
7361 -- memory-mapped I/O) that needs to be prevented. This refers to the
7362 -- act of reading itself, not to any damage that might be caused later
7363 -- by making use of whatever value was read. We assume here that
7364 -- Validity_Check_Subscripts meets this requirement, but introduce
7365 -- this declaration in order to document this assumption.
7366
7367 function Is_Renamed_Variable_Name (N : Node_Id) return Boolean;
7368 -- Returns True if the given name occurs as part of the renaming
7369 -- of a variable. In this case, the indexing operation should be
7370 -- treated as a write, rather than a read, with respect to validity
7371 -- checking. This is because the renamed variable can later be
7372 -- written to.
7373
7374 function Type_Requires_Subscript_Validity_Checks_For_Reads
7375 (Typ : Entity_Id) return Boolean;
7376 -- If Wild_Reads_May_Have_Bad_Side_Effects is False and we are indexing
7377 -- into an array of characters in order to read an element, it is ok
7378 -- if an invalid index value goes undetected. But if it is an array of
7379 -- pointers or an array of tasks, the consequences of such a read are
7380 -- potentially more severe and so we want to detect an invalid index
7381 -- value. This function captures that distinction; this is intended to
7382 -- be consistent with the "but does not by itself lead to erroneous
7383 -- ... execution" rule of RM 13.9.1(11).
7384
7385 ------------------------------
7386 -- Is_Renamed_Variable_Name --
7387 ------------------------------
7388
7389 function Is_Renamed_Variable_Name (N : Node_Id) return Boolean is
7390 Rover : Node_Id := N;
7391 begin
7392 if Is_Variable (N) then
7393 loop
7394 declare
7395 Rover_Parent : constant Node_Id := Parent (Rover);
7396 begin
7397 case Nkind (Rover_Parent) is
7398 when N_Object_Renaming_Declaration =>
7399 return Rover = Name (Rover_Parent);
7400
7401 when N_Indexed_Component
7402 | N_Slice
7403 | N_Selected_Component
7404 =>
7405 exit when Rover /= Prefix (Rover_Parent);
7406 Rover := Rover_Parent;
7407
7408 -- No need to check for qualified expressions or type
7409 -- conversions here, mostly because of the Is_Variable
7410 -- test. It is possible to have a view conversion for
7411 -- which Is_Variable yields True and which occurs as
7412 -- part of an object renaming, but only if the type is
7413 -- tagged; in that case this function will not be called.
7414
7415 when others =>
7416 exit;
7417 end case;
7418 end;
7419 end loop;
7420 end if;
7421 return False;
7422 end Is_Renamed_Variable_Name;
7423
7424 -------------------------------------------------------
7425 -- Type_Requires_Subscript_Validity_Checks_For_Reads --
7426 -------------------------------------------------------
7427
7428 function Type_Requires_Subscript_Validity_Checks_For_Reads
7429 (Typ : Entity_Id) return Boolean
7430 is
7431 -- a shorter name for recursive calls
7432 function Needs_Check (Typ : Entity_Id) return Boolean renames
7433 Type_Requires_Subscript_Validity_Checks_For_Reads;
7434 begin
7435 if Is_Access_Type (Typ)
7436 or else Is_Tagged_Type (Typ)
7437 or else Is_Concurrent_Type (Typ)
7438 or else (Is_Array_Type (Typ)
7439 and then Needs_Check (Component_Type (Typ)))
7440 or else (Is_Scalar_Type (Typ)
7441 and then Has_Aspect (Typ, Aspect_Default_Value))
7442 then
7443 return True;
7444 end if;
7445
7446 if Is_Record_Type (Typ) then
7447 declare
7448 Comp : Entity_Id := First_Component_Or_Discriminant (Typ);
7449 begin
7450 while Present (Comp) loop
7451 if Needs_Check (Etype (Comp)) then
7452 return True;
7453 end if;
7454
7455 Next_Component_Or_Discriminant (Comp);
7456 end loop;
7457 end;
7458 end if;
7459
7460 return False;
7461 end Type_Requires_Subscript_Validity_Checks_For_Reads;
7462
7463 -- Local constants
7464
7465 Loc : constant Source_Ptr := Sloc (N);
7466 Typ : constant Entity_Id := Etype (N);
7467 P : constant Node_Id := Prefix (N);
7468 T : constant Entity_Id := Etype (P);
7469
7470 -- Start of processing for Expand_N_Indexed_Component
7471
7472 begin
7473 -- A special optimization, if we have an indexed component that is
7474 -- selecting from a slice, then we can eliminate the slice, since, for
7475 -- example, x (i .. j)(k) is identical to x(k). The only difference is
7476 -- the range check required by the slice. The range check for the slice
7477 -- itself has already been generated. The range check for the
7478 -- subscripting operation is ensured by converting the subject to
7479 -- the subtype of the slice.
7480
7481 -- This optimization not only generates better code, avoiding slice
7482 -- messing especially in the packed case, but more importantly bypasses
7483 -- some problems in handling this peculiar case, for example, the issue
7484 -- of dealing specially with object renamings.
7485
7486 if Nkind (P) = N_Slice
7487
7488 -- This optimization is disabled for CodePeer because it can transform
7489 -- an index-check constraint_error into a range-check constraint_error
7490 -- and CodePeer cares about that distinction.
7491
7492 and then not CodePeer_Mode
7493 then
7494 Rewrite (N,
7495 Make_Indexed_Component (Loc,
7496 Prefix => Prefix (P),
7497 Expressions => New_List (
7498 Convert_To
7499 (Etype (First_Index (Etype (P))),
7500 First (Expressions (N))))));
7501 Analyze_And_Resolve (N, Typ);
7502 return;
7503 end if;
7504
7505 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
7506 -- function, then additional actuals must be passed.
7507
7508 if Is_Build_In_Place_Function_Call (P) then
7509 Make_Build_In_Place_Call_In_Anonymous_Context (P);
7510
7511 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
7512 -- containing build-in-place function calls whose returned object covers
7513 -- interface types.
7514
7515 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
7516 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
7517 end if;
7518
7519 -- Generate index and validity checks
7520
7521 declare
7522 Dims_Checked : Dimension_Set (Dimensions =>
7523 (if Is_Array_Type (T)
7524 then Number_Dimensions (T)
7525 else 1));
7526 -- Dims_Checked is used to avoid generating two checks (one in
7527 -- Generate_Index_Checks, one in Apply_Subscript_Validity_Checks)
7528 -- for the same index value in cases where the index check eliminates
7529 -- the need for the validity check. The Is_Array_Type test avoids
7530 -- cascading errors.
7531
7532 begin
7533 Generate_Index_Checks (N, Checks_Generated => Dims_Checked);
7534
7535 if Validity_Checks_On
7536 and then (Validity_Check_Subscripts
7537 or else Wild_Reads_May_Have_Bad_Side_Effects
7538 or else Type_Requires_Subscript_Validity_Checks_For_Reads
7539 (Typ)
7540 or else Is_Renamed_Variable_Name (N))
7541 then
7542 if Validity_Check_Subscripts then
7543 -- If we index into an array with an uninitialized variable
7544 -- and we generate an index check that passes at run time,
7545 -- passing that check does not ensure that the variable is
7546 -- valid (although it does in the common case where the
7547 -- object's subtype matches the index subtype).
7548 -- Consider an uninitialized variable with subtype 1 .. 10
7549 -- used to index into an array with bounds 1 .. 20 when the
7550 -- value of the uninitialized variable happens to be 15.
7551 -- The index check will succeed but the variable is invalid.
7552 -- If Validity_Check_Subscripts is True then we need to
7553 -- ensure validity, so we adjust Dims_Checked accordingly.
7554 Dims_Checked.Elements := (others => False);
7555
7556 elsif Is_Array_Type (T) then
7557 -- We are only adding extra validity checks here to
7558 -- deal with uninitialized variables (but this includes
7559 -- assigning one uninitialized variable to another). Other
7560 -- ways of producing invalid objects imply erroneousness, so
7561 -- the compiler can do whatever it wants for those cases.
7562 -- If an index type has the Default_Value aspect specified,
7563 -- then we don't have to worry about the possibility of an
7564 -- uninitialized variable, so no need for these extra
7565 -- validity checks.
7566
7567 declare
7568 Idx : Node_Id := First_Index (T);
7569 begin
7570 for No_Check_Needed of Dims_Checked.Elements loop
7571 No_Check_Needed := No_Check_Needed
7572 or else Has_Aspect (Etype (Idx), Aspect_Default_Value);
7573 Next_Index (Idx);
7574 end loop;
7575 end;
7576 end if;
7577
7578 Apply_Subscript_Validity_Checks
7579 (N, No_Check_Needed => Dims_Checked);
7580 end if;
7581 end;
7582
7583 -- If selecting from an array with atomic components, and atomic sync
7584 -- is not suppressed for this array type, set atomic sync flag.
7585
7586 if (Has_Atomic_Components (T)
7587 and then not Atomic_Synchronization_Disabled (T))
7588 or else (Is_Atomic (Typ)
7589 and then not Atomic_Synchronization_Disabled (Typ))
7590 or else (Is_Entity_Name (P)
7591 and then Has_Atomic_Components (Entity (P))
7592 and then not Atomic_Synchronization_Disabled (Entity (P)))
7593 then
7594 Activate_Atomic_Synchronization (N);
7595 end if;
7596
7597 -- All done if the prefix is not a packed array implemented specially
7598
7599 if not (Is_Packed (Etype (Prefix (N)))
7600 and then Present (Packed_Array_Impl_Type (Etype (Prefix (N)))))
7601 then
7602 return;
7603 end if;
7604
7605 -- For packed arrays that are not bit-packed (i.e. the case of an array
7606 -- with one or more index types with a non-contiguous enumeration type),
7607 -- we can always use the normal packed element get circuit.
7608
7609 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
7610 Expand_Packed_Element_Reference (N);
7611 return;
7612 end if;
7613
7614 -- For a reference to a component of a bit packed array, we convert it
7615 -- to a reference to the corresponding Packed_Array_Impl_Type. We only
7616 -- want to do this for simple references, and not for:
7617
7618 -- Left side of assignment, or prefix of left side of assignment, or
7619 -- prefix of the prefix, to handle packed arrays of packed arrays,
7620 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
7621
7622 -- Renaming objects in renaming associations
7623 -- This case is handled when a use of the renamed variable occurs
7624
7625 -- Actual parameters for a subprogram call
7626 -- This case is handled in Exp_Ch6.Expand_Actuals
7627
7628 -- The second expression in a 'Read attribute reference
7629
7630 -- The prefix of an address or bit or size attribute reference
7631
7632 -- The following circuit detects these exceptions. Note that we need to
7633 -- deal with implicit dereferences when climbing up the parent chain,
7634 -- with the additional difficulty that the type of parents may have yet
7635 -- to be resolved since prefixes are usually resolved first.
7636
7637 declare
7638 Child : Node_Id := N;
7639 Parnt : Node_Id := Parent (N);
7640
7641 begin
7642 loop
7643 if Nkind (Parnt) = N_Object_Renaming_Declaration then
7644 return;
7645
7646 elsif Nkind (Parnt) in N_Subprogram_Call
7647 or else (Nkind (Parnt) = N_Parameter_Association
7648 and then Nkind (Parent (Parnt)) in N_Subprogram_Call)
7649 then
7650 return;
7651
7652 elsif Nkind (Parnt) = N_Attribute_Reference
7653 and then Attribute_Name (Parnt) in Name_Address
7654 | Name_Bit
7655 | Name_Size
7656 and then Prefix (Parnt) = Child
7657 then
7658 return;
7659
7660 elsif Nkind (Parnt) = N_Assignment_Statement
7661 and then Name (Parnt) = Child
7662 then
7663 return;
7664
7665 -- If the expression is an index of an indexed component, it must
7666 -- be expanded regardless of context.
7667
7668 elsif Nkind (Parnt) = N_Indexed_Component
7669 and then Child /= Prefix (Parnt)
7670 then
7671 Expand_Packed_Element_Reference (N);
7672 return;
7673
7674 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
7675 and then Name (Parent (Parnt)) = Parnt
7676 then
7677 return;
7678
7679 elsif Nkind (Parnt) = N_Attribute_Reference
7680 and then Attribute_Name (Parnt) = Name_Read
7681 and then Next (First (Expressions (Parnt))) = Child
7682 then
7683 return;
7684
7685 elsif Nkind (Parnt) = N_Indexed_Component
7686 and then Prefix (Parnt) = Child
7687 then
7688 null;
7689
7690 elsif Nkind (Parnt) = N_Selected_Component
7691 and then Prefix (Parnt) = Child
7692 and then not (Present (Etype (Selector_Name (Parnt)))
7693 and then
7694 Is_Access_Type (Etype (Selector_Name (Parnt))))
7695 then
7696 null;
7697
7698 -- If the parent is a dereference, either implicit or explicit,
7699 -- then the packed reference needs to be expanded.
7700
7701 else
7702 Expand_Packed_Element_Reference (N);
7703 return;
7704 end if;
7705
7706 -- Keep looking up tree for unchecked expression, or if we are the
7707 -- prefix of a possible assignment left side.
7708
7709 Child := Parnt;
7710 Parnt := Parent (Child);
7711 end loop;
7712 end;
7713 end Expand_N_Indexed_Component;
7714
7715 ---------------------
7716 -- Expand_N_Not_In --
7717 ---------------------
7718
7719 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
7720 -- can be done. This avoids needing to duplicate this expansion code.
7721
7722 procedure Expand_N_Not_In (N : Node_Id) is
7723 Loc : constant Source_Ptr := Sloc (N);
7724 Typ : constant Entity_Id := Etype (N);
7725 Cfs : constant Boolean := Comes_From_Source (N);
7726
7727 begin
7728 Rewrite (N,
7729 Make_Op_Not (Loc,
7730 Right_Opnd =>
7731 Make_In (Loc,
7732 Left_Opnd => Left_Opnd (N),
7733 Right_Opnd => Right_Opnd (N),
7734 Alternatives => Alternatives (N))));
7735
7736 -- We want this to appear as coming from source if original does (see
7737 -- transformations in Expand_N_In).
7738
7739 Set_Comes_From_Source (N, Cfs);
7740 Set_Comes_From_Source (Right_Opnd (N), Cfs);
7741
7742 -- Now analyze transformed node
7743
7744 Analyze_And_Resolve (N, Typ);
7745 end Expand_N_Not_In;
7746
7747 -------------------
7748 -- Expand_N_Null --
7749 -------------------
7750
7751 -- The only replacement required is for the case of a null of a type that
7752 -- is an access to protected subprogram, or a subtype thereof. We represent
7753 -- such access values as a record, and so we must replace the occurrence of
7754 -- null by the equivalent record (with a null address and a null pointer in
7755 -- it), so that the back end creates the proper value.
7756
7757 procedure Expand_N_Null (N : Node_Id) is
7758 Loc : constant Source_Ptr := Sloc (N);
7759 Typ : constant Entity_Id := Base_Type (Etype (N));
7760 Agg : Node_Id;
7761
7762 begin
7763 if Is_Access_Protected_Subprogram_Type (Typ) then
7764 Agg :=
7765 Make_Aggregate (Loc,
7766 Expressions => New_List (
7767 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
7768 Make_Null (Loc)));
7769
7770 Rewrite (N, Agg);
7771 Analyze_And_Resolve (N, Equivalent_Type (Typ));
7772
7773 -- For subsequent semantic analysis, the node must retain its type.
7774 -- Gigi in any case replaces this type by the corresponding record
7775 -- type before processing the node.
7776
7777 Set_Etype (N, Typ);
7778 end if;
7779
7780 exception
7781 when RE_Not_Available =>
7782 return;
7783 end Expand_N_Null;
7784
7785 ---------------------
7786 -- Expand_N_Op_Abs --
7787 ---------------------
7788
7789 procedure Expand_N_Op_Abs (N : Node_Id) is
7790 Loc : constant Source_Ptr := Sloc (N);
7791 Expr : constant Node_Id := Right_Opnd (N);
7792 Typ : constant Entity_Id := Etype (N);
7793
7794 begin
7795 Unary_Op_Validity_Checks (N);
7796
7797 -- Check for MINIMIZED/ELIMINATED overflow mode
7798
7799 if Minimized_Eliminated_Overflow_Check (N) then
7800 Apply_Arithmetic_Overflow_Check (N);
7801 return;
7802 end if;
7803
7804 -- Try to narrow the operation
7805
7806 if Typ = Universal_Integer then
7807 Narrow_Large_Operation (N);
7808
7809 if Nkind (N) /= N_Op_Abs then
7810 return;
7811 end if;
7812 end if;
7813
7814 -- Deal with software overflow checking
7815
7816 if Is_Signed_Integer_Type (Typ)
7817 and then Do_Overflow_Check (N)
7818 then
7819 -- The only case to worry about is when the argument is equal to the
7820 -- largest negative number, so what we do is to insert the check:
7821
7822 -- [constraint_error when Expr = typ'Base'First]
7823
7824 -- with the usual Duplicate_Subexpr use coding for expr
7825
7826 Insert_Action (N,
7827 Make_Raise_Constraint_Error (Loc,
7828 Condition =>
7829 Make_Op_Eq (Loc,
7830 Left_Opnd => Duplicate_Subexpr (Expr),
7831 Right_Opnd =>
7832 Make_Attribute_Reference (Loc,
7833 Prefix =>
7834 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
7835 Attribute_Name => Name_First)),
7836 Reason => CE_Overflow_Check_Failed));
7837
7838 Set_Do_Overflow_Check (N, False);
7839 end if;
7840 end Expand_N_Op_Abs;
7841
7842 ---------------------
7843 -- Expand_N_Op_Add --
7844 ---------------------
7845
7846 procedure Expand_N_Op_Add (N : Node_Id) is
7847 Typ : constant Entity_Id := Etype (N);
7848
7849 begin
7850 Binary_Op_Validity_Checks (N);
7851
7852 -- Check for MINIMIZED/ELIMINATED overflow mode
7853
7854 if Minimized_Eliminated_Overflow_Check (N) then
7855 Apply_Arithmetic_Overflow_Check (N);
7856 return;
7857 end if;
7858
7859 -- N + 0 = 0 + N = N for integer types
7860
7861 if Is_Integer_Type (Typ) then
7862 if Compile_Time_Known_Value (Right_Opnd (N))
7863 and then Expr_Value (Right_Opnd (N)) = Uint_0
7864 then
7865 Rewrite (N, Left_Opnd (N));
7866 return;
7867
7868 elsif Compile_Time_Known_Value (Left_Opnd (N))
7869 and then Expr_Value (Left_Opnd (N)) = Uint_0
7870 then
7871 Rewrite (N, Right_Opnd (N));
7872 return;
7873 end if;
7874 end if;
7875
7876 -- Try to narrow the operation
7877
7878 if Typ = Universal_Integer then
7879 Narrow_Large_Operation (N);
7880
7881 if Nkind (N) /= N_Op_Add then
7882 return;
7883 end if;
7884 end if;
7885
7886 -- Arithmetic overflow checks for signed integer/fixed point types
7887
7888 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
7889 Apply_Arithmetic_Overflow_Check (N);
7890 return;
7891 end if;
7892
7893 -- Overflow checks for floating-point if -gnateF mode active
7894
7895 Check_Float_Op_Overflow (N);
7896
7897 Expand_Nonbinary_Modular_Op (N);
7898 end Expand_N_Op_Add;
7899
7900 ---------------------
7901 -- Expand_N_Op_And --
7902 ---------------------
7903
7904 procedure Expand_N_Op_And (N : Node_Id) is
7905 Typ : constant Entity_Id := Etype (N);
7906
7907 begin
7908 Binary_Op_Validity_Checks (N);
7909
7910 if Is_Array_Type (Etype (N)) then
7911 Expand_Boolean_Operator (N);
7912
7913 elsif Is_Boolean_Type (Etype (N)) then
7914 Adjust_Condition (Left_Opnd (N));
7915 Adjust_Condition (Right_Opnd (N));
7916 Set_Etype (N, Standard_Boolean);
7917 Adjust_Result_Type (N, Typ);
7918
7919 elsif Is_Intrinsic_Subprogram (Entity (N)) then
7920 Expand_Intrinsic_Call (N, Entity (N));
7921 end if;
7922
7923 Expand_Nonbinary_Modular_Op (N);
7924 end Expand_N_Op_And;
7925
7926 ------------------------
7927 -- Expand_N_Op_Concat --
7928 ------------------------
7929
7930 procedure Expand_N_Op_Concat (N : Node_Id) is
7931 Opnds : List_Id;
7932 -- List of operands to be concatenated
7933
7934 Cnode : Node_Id;
7935 -- Node which is to be replaced by the result of concatenating the nodes
7936 -- in the list Opnds.
7937
7938 begin
7939 -- Ensure validity of both operands
7940
7941 Binary_Op_Validity_Checks (N);
7942
7943 -- If we are the left operand of a concatenation higher up the tree,
7944 -- then do nothing for now, since we want to deal with a series of
7945 -- concatenations as a unit.
7946
7947 if Nkind (Parent (N)) = N_Op_Concat
7948 and then N = Left_Opnd (Parent (N))
7949 then
7950 return;
7951 end if;
7952
7953 -- We get here with a concatenation whose left operand may be a
7954 -- concatenation itself with a consistent type. We need to process
7955 -- these concatenation operands from left to right, which means
7956 -- from the deepest node in the tree to the highest node.
7957
7958 Cnode := N;
7959 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
7960 Cnode := Left_Opnd (Cnode);
7961 end loop;
7962
7963 -- Now Cnode is the deepest concatenation, and its parents are the
7964 -- concatenation nodes above, so now we process bottom up, doing the
7965 -- operands.
7966
7967 -- The outer loop runs more than once if more than one concatenation
7968 -- type is involved.
7969
7970 Outer : loop
7971 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
7972 Set_Parent (Opnds, N);
7973
7974 -- The inner loop gathers concatenation operands
7975
7976 Inner : while Cnode /= N
7977 and then Base_Type (Etype (Cnode)) =
7978 Base_Type (Etype (Parent (Cnode)))
7979 loop
7980 Cnode := Parent (Cnode);
7981 Append (Right_Opnd (Cnode), Opnds);
7982 end loop Inner;
7983
7984 -- Note: The following code is a temporary workaround for N731-034
7985 -- and N829-028 and will be kept until the general issue of internal
7986 -- symbol serialization is addressed. The workaround is kept under a
7987 -- debug switch to avoid permiating into the general case.
7988
7989 -- Wrap the node to concatenate into an expression actions node to
7990 -- keep it nicely packaged. This is useful in the case of an assert
7991 -- pragma with a concatenation where we want to be able to delete
7992 -- the concatenation and all its expansion stuff.
7993
7994 if Debug_Flag_Dot_H then
7995 declare
7996 Cnod : constant Node_Id := New_Copy_Tree (Cnode);
7997 Typ : constant Entity_Id := Base_Type (Etype (Cnode));
7998
7999 begin
8000 -- Note: use Rewrite rather than Replace here, so that for
8001 -- example Why_Not_Static can find the original concatenation
8002 -- node OK!
8003
8004 Rewrite (Cnode,
8005 Make_Expression_With_Actions (Sloc (Cnode),
8006 Actions => New_List (Make_Null_Statement (Sloc (Cnode))),
8007 Expression => Cnod));
8008
8009 Expand_Concatenate (Cnod, Opnds);
8010 Analyze_And_Resolve (Cnode, Typ);
8011 end;
8012
8013 -- Default case
8014
8015 else
8016 Expand_Concatenate (Cnode, Opnds);
8017 end if;
8018
8019 exit Outer when Cnode = N;
8020 Cnode := Parent (Cnode);
8021 end loop Outer;
8022 end Expand_N_Op_Concat;
8023
8024 ------------------------
8025 -- Expand_N_Op_Divide --
8026 ------------------------
8027
8028 procedure Expand_N_Op_Divide (N : Node_Id) is
8029 Loc : constant Source_Ptr := Sloc (N);
8030 Lopnd : constant Node_Id := Left_Opnd (N);
8031 Ropnd : constant Node_Id := Right_Opnd (N);
8032 Ltyp : constant Entity_Id := Etype (Lopnd);
8033 Rtyp : constant Entity_Id := Etype (Ropnd);
8034 Typ : Entity_Id := Etype (N);
8035 Rknow : constant Boolean := Is_Integer_Type (Typ)
8036 and then
8037 Compile_Time_Known_Value (Ropnd);
8038 Rval : Uint;
8039
8040 begin
8041 Binary_Op_Validity_Checks (N);
8042
8043 -- Check for MINIMIZED/ELIMINATED overflow mode
8044
8045 if Minimized_Eliminated_Overflow_Check (N) then
8046 Apply_Arithmetic_Overflow_Check (N);
8047 return;
8048 end if;
8049
8050 -- Otherwise proceed with expansion of division
8051
8052 if Rknow then
8053 Rval := Expr_Value (Ropnd);
8054 end if;
8055
8056 -- N / 1 = N for integer types
8057
8058 if Rknow and then Rval = Uint_1 then
8059 Rewrite (N, Lopnd);
8060 return;
8061 end if;
8062
8063 -- Try to narrow the operation
8064
8065 if Typ = Universal_Integer then
8066 Narrow_Large_Operation (N);
8067
8068 if Nkind (N) /= N_Op_Divide then
8069 return;
8070 end if;
8071 end if;
8072
8073 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
8074 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8075 -- operand is an unsigned integer, as required for this to work.
8076
8077 if Nkind (Ropnd) = N_Op_Expon
8078 and then Is_Power_Of_2_For_Shift (Ropnd)
8079
8080 -- We cannot do this transformation in configurable run time mode if we
8081 -- have 64-bit integers and long shifts are not available.
8082
8083 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
8084 then
8085 Rewrite (N,
8086 Make_Op_Shift_Right (Loc,
8087 Left_Opnd => Lopnd,
8088 Right_Opnd =>
8089 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
8090 Analyze_And_Resolve (N, Typ);
8091 return;
8092 end if;
8093
8094 -- Do required fixup of universal fixed operation
8095
8096 if Typ = Universal_Fixed then
8097 Fixup_Universal_Fixed_Operation (N);
8098 Typ := Etype (N);
8099 end if;
8100
8101 -- Divisions with fixed-point results
8102
8103 if Is_Fixed_Point_Type (Typ) then
8104
8105 if Is_Integer_Type (Rtyp) then
8106 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
8107 else
8108 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
8109 end if;
8110
8111 -- Deal with divide-by-zero check if back end cannot handle them
8112 -- and the flag is set indicating that we need such a check. Note
8113 -- that we don't need to bother here with the case of mixed-mode
8114 -- (Right operand an integer type), since these will be rewritten
8115 -- with conversions to a divide with a fixed-point right operand.
8116
8117 if Nkind (N) = N_Op_Divide
8118 and then Do_Division_Check (N)
8119 and then not Backend_Divide_Checks_On_Target
8120 and then not Is_Integer_Type (Rtyp)
8121 then
8122 Set_Do_Division_Check (N, False);
8123 Insert_Action (N,
8124 Make_Raise_Constraint_Error (Loc,
8125 Condition =>
8126 Make_Op_Eq (Loc,
8127 Left_Opnd => Duplicate_Subexpr_Move_Checks (Ropnd),
8128 Right_Opnd => Make_Real_Literal (Loc, Ureal_0)),
8129 Reason => CE_Divide_By_Zero));
8130 end if;
8131
8132 -- Other cases of division of fixed-point operands
8133
8134 elsif Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp) then
8135 if Is_Integer_Type (Typ) then
8136 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
8137 else
8138 pragma Assert (Is_Floating_Point_Type (Typ));
8139 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
8140 end if;
8141
8142 -- Mixed-mode operations can appear in a non-static universal context,
8143 -- in which case the integer argument must be converted explicitly.
8144
8145 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8146 Rewrite (Ropnd,
8147 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
8148
8149 Analyze_And_Resolve (Ropnd, Universal_Real);
8150
8151 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8152 Rewrite (Lopnd,
8153 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
8154
8155 Analyze_And_Resolve (Lopnd, Universal_Real);
8156
8157 -- Non-fixed point cases, do integer zero divide and overflow checks
8158
8159 elsif Is_Integer_Type (Typ) then
8160 Apply_Divide_Checks (N);
8161 end if;
8162
8163 -- Overflow checks for floating-point if -gnateF mode active
8164
8165 Check_Float_Op_Overflow (N);
8166
8167 Expand_Nonbinary_Modular_Op (N);
8168 end Expand_N_Op_Divide;
8169
8170 --------------------
8171 -- Expand_N_Op_Eq --
8172 --------------------
8173
8174 procedure Expand_N_Op_Eq (N : Node_Id) is
8175 Loc : constant Source_Ptr := Sloc (N);
8176 Typ : constant Entity_Id := Etype (N);
8177 Lhs : constant Node_Id := Left_Opnd (N);
8178 Rhs : constant Node_Id := Right_Opnd (N);
8179 Bodies : constant List_Id := New_List;
8180 A_Typ : constant Entity_Id := Etype (Lhs);
8181
8182 procedure Build_Equality_Call (Eq : Entity_Id);
8183 -- If a constructed equality exists for the type or for its parent,
8184 -- build and analyze call, adding conversions if the operation is
8185 -- inherited.
8186
8187 function Find_Equality (Prims : Elist_Id) return Entity_Id;
8188 -- Find a primitive equality function within primitive operation list
8189 -- Prims.
8190
8191 function Has_Unconstrained_UU_Component (Typ : Entity_Id) return Boolean;
8192 -- Determines whether a type has a subcomponent of an unconstrained
8193 -- Unchecked_Union subtype. Typ is a record type.
8194
8195 -------------------------
8196 -- Build_Equality_Call --
8197 -------------------------
8198
8199 procedure Build_Equality_Call (Eq : Entity_Id) is
8200 Op_Typ : constant Entity_Id := Etype (First_Formal (Eq));
8201
8202 L_Exp, R_Exp : Node_Id;
8203
8204 begin
8205 -- Adjust operands if necessary to comparison type
8206
8207 if Base_Type (A_Typ) /= Base_Type (Op_Typ)
8208 and then not Is_Class_Wide_Type (A_Typ)
8209 then
8210 L_Exp := OK_Convert_To (Op_Typ, Lhs);
8211 R_Exp := OK_Convert_To (Op_Typ, Rhs);
8212
8213 else
8214 L_Exp := Relocate_Node (Lhs);
8215 R_Exp := Relocate_Node (Rhs);
8216 end if;
8217
8218 Rewrite (N,
8219 Make_Function_Call (Loc,
8220 Name => New_Occurrence_Of (Eq, Loc),
8221 Parameter_Associations => New_List (L_Exp, R_Exp)));
8222
8223 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
8224 end Build_Equality_Call;
8225
8226 -------------------
8227 -- Find_Equality --
8228 -------------------
8229
8230 function Find_Equality (Prims : Elist_Id) return Entity_Id is
8231 function Find_Aliased_Equality (Prim : Entity_Id) return Entity_Id;
8232 -- Find an equality in a possible alias chain starting from primitive
8233 -- operation Prim.
8234
8235 ---------------------------
8236 -- Find_Aliased_Equality --
8237 ---------------------------
8238
8239 function Find_Aliased_Equality (Prim : Entity_Id) return Entity_Id is
8240 Candid : Entity_Id;
8241
8242 begin
8243 -- Inspect each candidate in the alias chain, checking whether it
8244 -- denotes an equality.
8245
8246 Candid := Prim;
8247 while Present (Candid) loop
8248 if Is_User_Defined_Equality (Candid) then
8249 return Candid;
8250 end if;
8251
8252 Candid := Alias (Candid);
8253 end loop;
8254
8255 return Empty;
8256 end Find_Aliased_Equality;
8257
8258 -- Local variables
8259
8260 Eq_Prim : Entity_Id;
8261 Prim_Elmt : Elmt_Id;
8262
8263 -- Start of processing for Find_Equality
8264
8265 begin
8266 -- Assume that the tagged type lacks an equality
8267
8268 Eq_Prim := Empty;
8269
8270 -- Inspect the list of primitives looking for a suitable equality
8271 -- within a possible chain of aliases.
8272
8273 Prim_Elmt := First_Elmt (Prims);
8274 while Present (Prim_Elmt) and then No (Eq_Prim) loop
8275 Eq_Prim := Find_Aliased_Equality (Node (Prim_Elmt));
8276
8277 Next_Elmt (Prim_Elmt);
8278 end loop;
8279
8280 -- A tagged type should always have an equality
8281
8282 pragma Assert (Present (Eq_Prim));
8283
8284 return Eq_Prim;
8285 end Find_Equality;
8286
8287 ------------------------------------
8288 -- Has_Unconstrained_UU_Component --
8289 ------------------------------------
8290
8291 function Has_Unconstrained_UU_Component
8292 (Typ : Entity_Id) return Boolean
8293 is
8294 function Unconstrained_UU_In_Component_Declaration
8295 (N : Node_Id) return Boolean;
8296
8297 function Unconstrained_UU_In_Component_Items
8298 (L : List_Id) return Boolean;
8299
8300 function Unconstrained_UU_In_Component_List
8301 (N : Node_Id) return Boolean;
8302
8303 function Unconstrained_UU_In_Variant_Part
8304 (N : Node_Id) return Boolean;
8305 -- A family of routines that determine whether a particular construct
8306 -- of a record type definition contains a subcomponent of an
8307 -- unchecked union type whose nominal subtype is unconstrained.
8308 --
8309 -- Individual routines correspond to the production rules of the Ada
8310 -- grammar, as described in the Ada RM (P).
8311
8312 -----------------------------------------------
8313 -- Unconstrained_UU_In_Component_Declaration --
8314 -----------------------------------------------
8315
8316 function Unconstrained_UU_In_Component_Declaration
8317 (N : Node_Id) return Boolean
8318 is
8319 pragma Assert (Nkind (N) = N_Component_Declaration);
8320
8321 Sindic : constant Node_Id :=
8322 Subtype_Indication (Component_Definition (N));
8323 begin
8324 -- If the component declaration includes a subtype indication
8325 -- it is not an unchecked_union. Otherwise verify that it carries
8326 -- the Unchecked_Union flag and is either a record or a private
8327 -- type. A Record_Subtype declared elsewhere does not qualify,
8328 -- even if its parent type carries the flag.
8329
8330 return Nkind (Sindic) in N_Expanded_Name | N_Identifier
8331 and then Is_Unchecked_Union (Base_Type (Etype (Sindic)))
8332 and then Ekind (Entity (Sindic)) in
8333 E_Private_Type | E_Record_Type;
8334 end Unconstrained_UU_In_Component_Declaration;
8335
8336 -----------------------------------------
8337 -- Unconstrained_UU_In_Component_Items --
8338 -----------------------------------------
8339
8340 function Unconstrained_UU_In_Component_Items
8341 (L : List_Id) return Boolean
8342 is
8343 N : Node_Id := First (L);
8344 begin
8345 while Present (N) loop
8346 if Nkind (N) = N_Component_Declaration
8347 and then Unconstrained_UU_In_Component_Declaration (N)
8348 then
8349 return True;
8350 end if;
8351
8352 Next (N);
8353 end loop;
8354
8355 return False;
8356 end Unconstrained_UU_In_Component_Items;
8357
8358 ----------------------------------------
8359 -- Unconstrained_UU_In_Component_List --
8360 ----------------------------------------
8361
8362 function Unconstrained_UU_In_Component_List
8363 (N : Node_Id) return Boolean
8364 is
8365 pragma Assert (Nkind (N) = N_Component_List);
8366
8367 Optional_Variant_Part : Node_Id;
8368 begin
8369 if Unconstrained_UU_In_Component_Items (Component_Items (N)) then
8370 return True;
8371 end if;
8372
8373 Optional_Variant_Part := Variant_Part (N);
8374
8375 return
8376 Present (Optional_Variant_Part)
8377 and then
8378 Unconstrained_UU_In_Variant_Part (Optional_Variant_Part);
8379 end Unconstrained_UU_In_Component_List;
8380
8381 --------------------------------------
8382 -- Unconstrained_UU_In_Variant_Part --
8383 --------------------------------------
8384
8385 function Unconstrained_UU_In_Variant_Part
8386 (N : Node_Id) return Boolean
8387 is
8388 pragma Assert (Nkind (N) = N_Variant_Part);
8389
8390 Variant : Node_Id := First (Variants (N));
8391 begin
8392 loop
8393 if Unconstrained_UU_In_Component_List (Component_List (Variant))
8394 then
8395 return True;
8396 end if;
8397
8398 Next (Variant);
8399 exit when No (Variant);
8400 end loop;
8401
8402 return False;
8403 end Unconstrained_UU_In_Variant_Part;
8404
8405 Typ_Def : constant Node_Id :=
8406 Type_Definition (Declaration_Node (Base_Type (Typ)));
8407
8408 Optional_Component_List : constant Node_Id :=
8409 Component_List (Typ_Def);
8410
8411 -- Start of processing for Has_Unconstrained_UU_Component
8412
8413 begin
8414 return Present (Optional_Component_List)
8415 and then
8416 Unconstrained_UU_In_Component_List (Optional_Component_List);
8417 end Has_Unconstrained_UU_Component;
8418
8419 -- Local variables
8420
8421 Typl : Entity_Id;
8422
8423 -- Start of processing for Expand_N_Op_Eq
8424
8425 begin
8426 Binary_Op_Validity_Checks (N);
8427
8428 -- Deal with private types
8429
8430 Typl := Underlying_Type (A_Typ);
8431
8432 -- It may happen in error situations that the underlying type is not
8433 -- set. The error will be detected later, here we just defend the
8434 -- expander code.
8435
8436 if No (Typl) then
8437 return;
8438 end if;
8439
8440 -- Now get the implementation base type (note that plain Base_Type here
8441 -- might lead us back to the private type, which is not what we want!)
8442
8443 Typl := Implementation_Base_Type (Typl);
8444
8445 -- Equality between variant records results in a call to a routine
8446 -- that has conditional tests of the discriminant value(s), and hence
8447 -- violates the No_Implicit_Conditionals restriction.
8448
8449 if Has_Variant_Part (Typl) then
8450 declare
8451 Msg : Boolean;
8452
8453 begin
8454 Check_Restriction (Msg, No_Implicit_Conditionals, N);
8455
8456 if Msg then
8457 Error_Msg_N
8458 ("\comparison of variant records tests discriminants", N);
8459 return;
8460 end if;
8461 end;
8462 end if;
8463
8464 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
8465 -- means we no longer have a comparison operation, we are all done.
8466
8467 if Minimized_Eliminated_Overflow_Check (Left_Opnd (N)) then
8468 Expand_Compare_Minimize_Eliminate_Overflow (N);
8469 end if;
8470
8471 if Nkind (N) /= N_Op_Eq then
8472 return;
8473 end if;
8474
8475 -- Boolean types (requiring handling of non-standard case)
8476
8477 if Is_Boolean_Type (Typl) then
8478 Adjust_Condition (Left_Opnd (N));
8479 Adjust_Condition (Right_Opnd (N));
8480 Set_Etype (N, Standard_Boolean);
8481 Adjust_Result_Type (N, Typ);
8482
8483 -- Array types
8484
8485 elsif Is_Array_Type (Typl) then
8486
8487 -- If we are doing full validity checking, and it is possible for the
8488 -- array elements to be invalid then expand out array comparisons to
8489 -- make sure that we check the array elements.
8490
8491 if Validity_Check_Operands
8492 and then not Is_Known_Valid (Component_Type (Typl))
8493 then
8494 declare
8495 Save_Force_Validity_Checks : constant Boolean :=
8496 Force_Validity_Checks;
8497 begin
8498 Force_Validity_Checks := True;
8499 Rewrite (N,
8500 Expand_Array_Equality
8501 (N,
8502 Relocate_Node (Lhs),
8503 Relocate_Node (Rhs),
8504 Bodies,
8505 Typl));
8506 Insert_Actions (N, Bodies);
8507 Analyze_And_Resolve (N, Standard_Boolean);
8508 Force_Validity_Checks := Save_Force_Validity_Checks;
8509 end;
8510
8511 -- Packed case where both operands are known aligned
8512
8513 elsif Is_Bit_Packed_Array (Typl)
8514 and then not Is_Possibly_Unaligned_Object (Lhs)
8515 and then not Is_Possibly_Unaligned_Object (Rhs)
8516 then
8517 Expand_Packed_Eq (N);
8518
8519 -- When the component type is elementary, we can use a byte-wise
8520 -- comparison if supported on the target, except in the cases
8521 -- of floating-point (negative zero issues require element by
8522 -- element comparison), and full access types (where we must be sure
8523 -- to load elements independently) and possibly unaligned arrays.
8524
8525 elsif Is_Elementary_Type (Component_Type (Typl))
8526 and then not Is_Floating_Point_Type (Component_Type (Typl))
8527 and then not Is_Full_Access (Component_Type (Typl))
8528 and then not Is_Possibly_Unaligned_Object (Lhs)
8529 and then not Is_Possibly_Unaligned_Slice (Lhs)
8530 and then not Is_Possibly_Unaligned_Object (Rhs)
8531 and then not Is_Possibly_Unaligned_Slice (Rhs)
8532 and then Support_Composite_Compare_On_Target
8533 then
8534 null;
8535
8536 -- For composite and floating-point cases, expand equality loop to
8537 -- make sure of using proper comparisons for tagged types, and
8538 -- correctly handling the floating-point case.
8539
8540 else
8541 Rewrite (N,
8542 Expand_Array_Equality
8543 (N,
8544 Relocate_Node (Lhs),
8545 Relocate_Node (Rhs),
8546 Bodies,
8547 Typl));
8548 Insert_Actions (N, Bodies, Suppress => All_Checks);
8549 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
8550 end if;
8551
8552 -- Record Types
8553
8554 elsif Is_Record_Type (Typl) then
8555
8556 -- For tagged types, use the primitive "="
8557
8558 if Is_Tagged_Type (Typl) then
8559
8560 -- No need to do anything else compiling under restriction
8561 -- No_Dispatching_Calls. During the semantic analysis we
8562 -- already notified such violation.
8563
8564 if Restriction_Active (No_Dispatching_Calls) then
8565 return;
8566 end if;
8567
8568 -- If this is an untagged private type completed with a derivation
8569 -- of an untagged private type whose full view is a tagged type,
8570 -- we use the primitive operations of the private type (since it
8571 -- does not have a full view, and also because its equality
8572 -- primitive may have been overridden in its untagged full view).
8573
8574 if Inherits_From_Tagged_Full_View (A_Typ) then
8575 Build_Equality_Call
8576 (Find_Equality (Collect_Primitive_Operations (A_Typ)));
8577
8578 -- Find the type's predefined equality or an overriding
8579 -- user-defined equality. The reason for not simply calling
8580 -- Find_Prim_Op here is that there may be a user-defined
8581 -- overloaded equality op that precedes the equality that we
8582 -- want, so we have to explicitly search (e.g., there could be
8583 -- an equality with two different parameter types).
8584
8585 else
8586 if Is_Class_Wide_Type (Typl) then
8587 Typl := Find_Specific_Type (Typl);
8588 end if;
8589
8590 Build_Equality_Call
8591 (Find_Equality (Primitive_Operations (Typl)));
8592 end if;
8593
8594 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
8595 -- predefined equality operator for a type which has a subcomponent
8596 -- of an unchecked union type whose nominal subtype is unconstrained.
8597
8598 elsif Has_Unconstrained_UU_Component (Typl) then
8599 Insert_Action (N,
8600 Make_Raise_Program_Error (Loc,
8601 Reason => PE_Unchecked_Union_Restriction));
8602
8603 Rewrite (N,
8604 New_Occurrence_Of (Standard_False, Loc));
8605
8606 -- If a type support function is present, e.g. if there is a variant
8607 -- part, including an unchecked union type, use it.
8608
8609 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
8610 Build_Equality_Call
8611 (TSS (Root_Type (Typl), TSS_Composite_Equality));
8612
8613 -- When comparing two Bounded_Strings, use the primitive equality of
8614 -- the root Super_String type.
8615
8616 elsif Is_Bounded_String (Typl) then
8617 Build_Equality_Call
8618 (Find_Equality
8619 (Collect_Primitive_Operations (Root_Type (Typl))));
8620
8621 -- Otherwise expand the component by component equality. Note that
8622 -- we never use block-bit comparisons for records, because of the
8623 -- problems with gaps. The back end will often be able to recombine
8624 -- the separate comparisons that we generate here.
8625
8626 else
8627 Remove_Side_Effects (Lhs);
8628 Remove_Side_Effects (Rhs);
8629 Rewrite (N, Expand_Record_Equality (N, Typl, Lhs, Rhs));
8630
8631 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
8632 end if;
8633
8634 -- If unnesting, handle elementary types whose Equivalent_Types are
8635 -- records because there may be padding or undefined fields.
8636
8637 elsif Unnest_Subprogram_Mode
8638 and then Ekind (Typl) in E_Class_Wide_Type
8639 | E_Class_Wide_Subtype
8640 | E_Access_Subprogram_Type
8641 | E_Access_Protected_Subprogram_Type
8642 | E_Anonymous_Access_Protected_Subprogram_Type
8643 | E_Exception_Type
8644 and then Present (Equivalent_Type (Typl))
8645 and then Is_Record_Type (Equivalent_Type (Typl))
8646 then
8647 Typl := Equivalent_Type (Typl);
8648 Remove_Side_Effects (Lhs);
8649 Remove_Side_Effects (Rhs);
8650 Rewrite (N,
8651 Expand_Record_Equality (N, Typl,
8652 Unchecked_Convert_To (Typl, Lhs),
8653 Unchecked_Convert_To (Typl, Rhs)));
8654
8655 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
8656 end if;
8657
8658 -- Test if result is known at compile time
8659
8660 Rewrite_Comparison (N);
8661
8662 -- Try to narrow the operation
8663
8664 if Typl = Universal_Integer and then Nkind (N) = N_Op_Eq then
8665 Narrow_Large_Operation (N);
8666 end if;
8667
8668 -- Special optimization of length comparison
8669
8670 Optimize_Length_Comparison (N);
8671
8672 -- One more special case: if we have a comparison of X'Result = expr
8673 -- in floating-point, then if not already there, change expr to be
8674 -- f'Machine (expr) to eliminate surprise from extra precision.
8675
8676 if Is_Floating_Point_Type (Typl)
8677 and then Is_Attribute_Result (Original_Node (Lhs))
8678 then
8679 -- Stick in the Typ'Machine call if not already there
8680
8681 if Nkind (Rhs) /= N_Attribute_Reference
8682 or else Attribute_Name (Rhs) /= Name_Machine
8683 then
8684 Rewrite (Rhs,
8685 Make_Attribute_Reference (Loc,
8686 Prefix => New_Occurrence_Of (Typl, Loc),
8687 Attribute_Name => Name_Machine,
8688 Expressions => New_List (Relocate_Node (Rhs))));
8689 Analyze_And_Resolve (Rhs, Typl);
8690 end if;
8691 end if;
8692 end Expand_N_Op_Eq;
8693
8694 -----------------------
8695 -- Expand_N_Op_Expon --
8696 -----------------------
8697
8698 procedure Expand_N_Op_Expon (N : Node_Id) is
8699 Loc : constant Source_Ptr := Sloc (N);
8700 Ovflo : constant Boolean := Do_Overflow_Check (N);
8701 Typ : constant Entity_Id := Etype (N);
8702 Rtyp : constant Entity_Id := Root_Type (Typ);
8703
8704 Bastyp : Entity_Id;
8705
8706 function Wrap_MA (Exp : Node_Id) return Node_Id;
8707 -- Given an expression Exp, if the root type is Float or Long_Float,
8708 -- then wrap the expression in a call of Bastyp'Machine, to stop any
8709 -- extra precision. This is done to ensure that X**A = X**B when A is
8710 -- a static constant and B is a variable with the same value. For any
8711 -- other type, the node Exp is returned unchanged.
8712
8713 -------------
8714 -- Wrap_MA --
8715 -------------
8716
8717 function Wrap_MA (Exp : Node_Id) return Node_Id is
8718 Loc : constant Source_Ptr := Sloc (Exp);
8719
8720 begin
8721 if Rtyp = Standard_Float or else Rtyp = Standard_Long_Float then
8722 return
8723 Make_Attribute_Reference (Loc,
8724 Attribute_Name => Name_Machine,
8725 Prefix => New_Occurrence_Of (Bastyp, Loc),
8726 Expressions => New_List (Relocate_Node (Exp)));
8727 else
8728 return Exp;
8729 end if;
8730 end Wrap_MA;
8731
8732 -- Local variables
8733
8734 Base : Node_Id;
8735 Ent : Entity_Id;
8736 Etyp : Entity_Id;
8737 Exp : Node_Id;
8738 Exptyp : Entity_Id;
8739 Expv : Uint;
8740 Rent : RE_Id;
8741 Temp : Node_Id;
8742 Xnode : Node_Id;
8743
8744 -- Start of processing for Expand_N_Op_Expon
8745
8746 begin
8747 Binary_Op_Validity_Checks (N);
8748
8749 -- CodePeer wants to see the unexpanded N_Op_Expon node
8750
8751 if CodePeer_Mode then
8752 return;
8753 end if;
8754
8755 -- Relocation of left and right operands must be done after performing
8756 -- the validity checks since the generation of validation checks may
8757 -- remove side effects.
8758
8759 Base := Relocate_Node (Left_Opnd (N));
8760 Bastyp := Etype (Base);
8761 Exp := Relocate_Node (Right_Opnd (N));
8762 Exptyp := Etype (Exp);
8763
8764 -- If either operand is of a private type, then we have the use of an
8765 -- intrinsic operator, and we get rid of the privateness, by using root
8766 -- types of underlying types for the actual operation. Otherwise the
8767 -- private types will cause trouble if we expand multiplications or
8768 -- shifts etc. We also do this transformation if the result type is
8769 -- different from the base type.
8770
8771 if Is_Private_Type (Etype (Base))
8772 or else Is_Private_Type (Typ)
8773 or else Is_Private_Type (Exptyp)
8774 or else Rtyp /= Root_Type (Bastyp)
8775 then
8776 declare
8777 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
8778 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
8779 begin
8780 Rewrite (N,
8781 Unchecked_Convert_To (Typ,
8782 Make_Op_Expon (Loc,
8783 Left_Opnd => Unchecked_Convert_To (Bt, Base),
8784 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
8785 Analyze_And_Resolve (N, Typ);
8786 return;
8787 end;
8788 end if;
8789
8790 -- Check for MINIMIZED/ELIMINATED overflow mode
8791
8792 if Minimized_Eliminated_Overflow_Check (N) then
8793 Apply_Arithmetic_Overflow_Check (N);
8794 return;
8795 end if;
8796
8797 -- Test for case of known right argument where we can replace the
8798 -- exponentiation by an equivalent expression using multiplication.
8799
8800 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
8801 -- configurable run-time mode, we may not have the exponentiation
8802 -- routine available, and we don't want the legality of the program
8803 -- to depend on how clever the compiler is in knowing values.
8804
8805 if CRT_Safe_Compile_Time_Known_Value (Exp) then
8806 Expv := Expr_Value (Exp);
8807
8808 -- We only fold small non-negative exponents. You might think we
8809 -- could fold small negative exponents for the real case, but we
8810 -- can't because we are required to raise Constraint_Error for
8811 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
8812 -- See ACVC test C4A012B, and it is not worth generating the test.
8813
8814 -- For small negative exponents, we return the reciprocal of
8815 -- the folding of the exponentiation for the opposite (positive)
8816 -- exponent, as required by RM 4.5.6(11/3).
8817
8818 if abs Expv <= 4 then
8819
8820 -- X ** 0 = 1 (or 1.0)
8821
8822 if Expv = 0 then
8823
8824 -- Call Remove_Side_Effects to ensure that any side effects
8825 -- in the ignored left operand (in particular function calls
8826 -- to user defined functions) are properly executed.
8827
8828 Remove_Side_Effects (Base);
8829
8830 if Ekind (Typ) in Integer_Kind then
8831 Xnode := Make_Integer_Literal (Loc, Intval => 1);
8832 else
8833 Xnode := Make_Real_Literal (Loc, Ureal_1);
8834 end if;
8835
8836 -- X ** 1 = X
8837
8838 elsif Expv = 1 then
8839 Xnode := Base;
8840
8841 -- X ** 2 = X * X
8842
8843 elsif Expv = 2 then
8844 Xnode :=
8845 Wrap_MA (
8846 Make_Op_Multiply (Loc,
8847 Left_Opnd => Duplicate_Subexpr (Base),
8848 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8849
8850 -- X ** 3 = X * X * X
8851
8852 elsif Expv = 3 then
8853 Xnode :=
8854 Wrap_MA (
8855 Make_Op_Multiply (Loc,
8856 Left_Opnd =>
8857 Make_Op_Multiply (Loc,
8858 Left_Opnd => Duplicate_Subexpr (Base),
8859 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
8860 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)));
8861
8862 -- X ** 4 ->
8863
8864 -- do
8865 -- En : constant base'type := base * base;
8866 -- in
8867 -- En * En
8868
8869 elsif Expv = 4 then
8870 Temp := Make_Temporary (Loc, 'E', Base);
8871
8872 Xnode :=
8873 Make_Expression_With_Actions (Loc,
8874 Actions => New_List (
8875 Make_Object_Declaration (Loc,
8876 Defining_Identifier => Temp,
8877 Constant_Present => True,
8878 Object_Definition => New_Occurrence_Of (Typ, Loc),
8879 Expression =>
8880 Wrap_MA (
8881 Make_Op_Multiply (Loc,
8882 Left_Opnd =>
8883 Duplicate_Subexpr (Base),
8884 Right_Opnd =>
8885 Duplicate_Subexpr_No_Checks (Base))))),
8886
8887 Expression =>
8888 Wrap_MA (
8889 Make_Op_Multiply (Loc,
8890 Left_Opnd => New_Occurrence_Of (Temp, Loc),
8891 Right_Opnd => New_Occurrence_Of (Temp, Loc))));
8892
8893 -- X ** N = 1.0 / X ** (-N)
8894 -- N in -4 .. -1
8895
8896 else
8897 pragma Assert
8898 (Expv = -1 or Expv = -2 or Expv = -3 or Expv = -4);
8899
8900 Xnode :=
8901 Make_Op_Divide (Loc,
8902 Left_Opnd =>
8903 Make_Float_Literal (Loc,
8904 Radix => Uint_1,
8905 Significand => Uint_1,
8906 Exponent => Uint_0),
8907 Right_Opnd =>
8908 Make_Op_Expon (Loc,
8909 Left_Opnd => Duplicate_Subexpr (Base),
8910 Right_Opnd =>
8911 Make_Integer_Literal (Loc,
8912 Intval => -Expv)));
8913 end if;
8914
8915 Rewrite (N, Xnode);
8916 Analyze_And_Resolve (N, Typ);
8917 return;
8918 end if;
8919 end if;
8920
8921 -- Optimize 2 ** expression to shift where possible
8922
8923 -- Note: we used to check that Exptyp was an unsigned type. But that is
8924 -- an unnecessary check, since if Exp is negative, we have a run-time
8925 -- error that is either caught (so we get the right result) or we have
8926 -- suppressed the check, in which case the code is erroneous anyway.
8927
8928 if Is_Integer_Type (Rtyp)
8929
8930 -- The base value must be "safe compile-time known", and exactly 2
8931
8932 and then Nkind (Base) = N_Integer_Literal
8933 and then CRT_Safe_Compile_Time_Known_Value (Base)
8934 and then Expr_Value (Base) = Uint_2
8935
8936 -- This transformation is not applicable for a modular type with a
8937 -- nonbinary modulus because shifting makes no sense in that case.
8938
8939 and then not Non_Binary_Modulus (Typ)
8940 then
8941 -- Handle the cases where our parent is a division or multiplication
8942 -- specially. In these cases we can convert to using a shift at the
8943 -- parent level if we are not doing overflow checking, since it is
8944 -- too tricky to combine the overflow check at the parent level.
8945
8946 if not Ovflo
8947 and then Nkind (Parent (N)) in N_Op_Divide | N_Op_Multiply
8948 then
8949 declare
8950 P : constant Node_Id := Parent (N);
8951 L : constant Node_Id := Left_Opnd (P);
8952 R : constant Node_Id := Right_Opnd (P);
8953
8954 begin
8955 if (Nkind (P) = N_Op_Multiply
8956 and then
8957 ((Is_Integer_Type (Etype (L)) and then R = N)
8958 or else
8959 (Is_Integer_Type (Etype (R)) and then L = N))
8960 and then not Do_Overflow_Check (P))
8961
8962 or else
8963 (Nkind (P) = N_Op_Divide
8964 and then Is_Integer_Type (Etype (L))
8965 and then Is_Unsigned_Type (Etype (L))
8966 and then R = N
8967 and then not Do_Overflow_Check (P))
8968 then
8969 Set_Is_Power_Of_2_For_Shift (N);
8970 return;
8971 end if;
8972 end;
8973
8974 -- Here we have 2 ** N on its own, so we can convert this into a
8975 -- shift.
8976
8977 else
8978 -- Op_Shift_Left (generated below) has modular-shift semantics;
8979 -- therefore we might need to generate an overflow check here
8980 -- if the type is signed.
8981
8982 if Is_Signed_Integer_Type (Typ) and then Ovflo then
8983 declare
8984 OK : Boolean;
8985 Lo : Uint;
8986 Hi : Uint;
8987
8988 MaxS : constant Uint := Esize (Rtyp) - 2;
8989 -- Maximum shift count with no overflow
8990 begin
8991 Determine_Range (Exp, OK, Lo, Hi, Assume_Valid => True);
8992
8993 if not OK or else Hi > MaxS then
8994 Insert_Action (N,
8995 Make_Raise_Constraint_Error (Loc,
8996 Condition =>
8997 Make_Op_Gt (Loc,
8998 Left_Opnd => Duplicate_Subexpr (Exp),
8999 Right_Opnd => Make_Integer_Literal (Loc, MaxS)),
9000 Reason => CE_Overflow_Check_Failed));
9001 end if;
9002 end;
9003 end if;
9004
9005 -- Generate Shift_Left (1, Exp)
9006
9007 Rewrite (N,
9008 Make_Op_Shift_Left (Loc,
9009 Left_Opnd => Make_Integer_Literal (Loc, Uint_1),
9010 Right_Opnd => Exp));
9011
9012 Analyze_And_Resolve (N, Typ);
9013 return;
9014 end if;
9015 end if;
9016
9017 -- Fall through if exponentiation must be done using a runtime routine
9018
9019 -- First deal with modular case
9020
9021 if Is_Modular_Integer_Type (Rtyp) then
9022
9023 -- Nonbinary modular case, we call the special exponentiation
9024 -- routine for the nonbinary case, converting the argument to
9025 -- Long_Long_Integer and passing the modulus value. Then the
9026 -- result is converted back to the base type.
9027
9028 if Non_Binary_Modulus (Rtyp) then
9029 Rewrite (N,
9030 Convert_To (Typ,
9031 Make_Function_Call (Loc,
9032 Name =>
9033 New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
9034 Parameter_Associations => New_List (
9035 Convert_To (RTE (RE_Unsigned), Base),
9036 Make_Integer_Literal (Loc, Modulus (Rtyp)),
9037 Exp))));
9038
9039 -- Binary modular case, in this case, we call one of three routines,
9040 -- either the unsigned integer case, or the unsigned long long
9041 -- integer case, or the unsigned long long long integer case, with a
9042 -- final "and" operation to do the required mod.
9043
9044 else
9045 if Esize (Rtyp) <= Standard_Integer_Size then
9046 Ent := RTE (RE_Exp_Unsigned);
9047 elsif Esize (Rtyp) <= Standard_Long_Long_Integer_Size then
9048 Ent := RTE (RE_Exp_Long_Long_Unsigned);
9049 else
9050 Ent := RTE (RE_Exp_Long_Long_Long_Unsigned);
9051 end if;
9052
9053 Rewrite (N,
9054 Convert_To (Typ,
9055 Make_Op_And (Loc,
9056 Left_Opnd =>
9057 Make_Function_Call (Loc,
9058 Name => New_Occurrence_Of (Ent, Loc),
9059 Parameter_Associations => New_List (
9060 Convert_To (Etype (First_Formal (Ent)), Base),
9061 Exp)),
9062 Right_Opnd =>
9063 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
9064
9065 end if;
9066
9067 -- Common exit point for modular type case
9068
9069 Analyze_And_Resolve (N, Typ);
9070 return;
9071
9072 -- Signed integer cases, using either Integer, Long_Long_Integer or
9073 -- Long_Long_Long_Integer. It is not worth also having routines for
9074 -- Short_[Short_]Integer, since for most machines it would not help,
9075 -- and it would generate more code that might need certification when
9076 -- a certified run time is required.
9077
9078 -- In the integer cases, we have two routines, one for when overflow
9079 -- checks are required, and one when they are not required, since there
9080 -- is a real gain in omitting checks on many machines.
9081
9082 elsif Is_Signed_Integer_Type (Rtyp) then
9083 if Esize (Rtyp) <= Standard_Integer_Size then
9084 Etyp := Standard_Integer;
9085
9086 if Ovflo then
9087 Rent := RE_Exp_Integer;
9088 else
9089 Rent := RE_Exn_Integer;
9090 end if;
9091
9092 elsif Esize (Rtyp) <= Standard_Long_Long_Integer_Size then
9093 Etyp := Standard_Long_Long_Integer;
9094
9095 if Ovflo then
9096 Rent := RE_Exp_Long_Long_Integer;
9097 else
9098 Rent := RE_Exn_Long_Long_Integer;
9099 end if;
9100
9101 else
9102 Etyp := Standard_Long_Long_Long_Integer;
9103
9104 if Ovflo then
9105 Rent := RE_Exp_Long_Long_Long_Integer;
9106 else
9107 Rent := RE_Exn_Long_Long_Long_Integer;
9108 end if;
9109 end if;
9110
9111 -- Floating-point cases. We do not need separate routines for the
9112 -- overflow case here, since in the case of floating-point, we generate
9113 -- infinities anyway as a rule (either that or we automatically trap
9114 -- overflow), and if there is an infinity generated and a range check
9115 -- is required, the check will fail anyway.
9116
9117 else
9118 pragma Assert (Is_Floating_Point_Type (Rtyp));
9119
9120 -- Short_Float and Float are the same type for GNAT
9121
9122 if Rtyp = Standard_Short_Float or else Rtyp = Standard_Float then
9123 Etyp := Standard_Float;
9124 Rent := RE_Exn_Float;
9125
9126 elsif Rtyp = Standard_Long_Float then
9127 Etyp := Standard_Long_Float;
9128 Rent := RE_Exn_Long_Float;
9129
9130 else
9131 Etyp := Standard_Long_Long_Float;
9132 Rent := RE_Exn_Long_Long_Float;
9133 end if;
9134 end if;
9135
9136 -- Common processing for integer cases and floating-point cases.
9137 -- If we are in the right type, we can call runtime routine directly
9138
9139 if Typ = Etyp
9140 and then not Is_Universal_Numeric_Type (Rtyp)
9141 then
9142 Rewrite (N,
9143 Wrap_MA (
9144 Make_Function_Call (Loc,
9145 Name => New_Occurrence_Of (RTE (Rent), Loc),
9146 Parameter_Associations => New_List (Base, Exp))));
9147
9148 -- Otherwise we have to introduce conversions (conversions are also
9149 -- required in the universal cases, since the runtime routine is
9150 -- typed using one of the standard types).
9151
9152 else
9153 Rewrite (N,
9154 Convert_To (Typ,
9155 Make_Function_Call (Loc,
9156 Name => New_Occurrence_Of (RTE (Rent), Loc),
9157 Parameter_Associations => New_List (
9158 Convert_To (Etyp, Base),
9159 Exp))));
9160 end if;
9161
9162 Analyze_And_Resolve (N, Typ);
9163 return;
9164
9165 exception
9166 when RE_Not_Available =>
9167 return;
9168 end Expand_N_Op_Expon;
9169
9170 --------------------
9171 -- Expand_N_Op_Ge --
9172 --------------------
9173
9174 procedure Expand_N_Op_Ge (N : Node_Id) is
9175 Typ : constant Entity_Id := Etype (N);
9176 Op1 : constant Node_Id := Left_Opnd (N);
9177 Op2 : constant Node_Id := Right_Opnd (N);
9178 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
9179
9180 begin
9181 Binary_Op_Validity_Checks (N);
9182
9183 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9184 -- means we no longer have a comparison operation, we are all done.
9185
9186 if Minimized_Eliminated_Overflow_Check (Op1) then
9187 Expand_Compare_Minimize_Eliminate_Overflow (N);
9188 end if;
9189
9190 if Nkind (N) /= N_Op_Ge then
9191 return;
9192 end if;
9193
9194 -- Array type case
9195
9196 if Is_Array_Type (Typ1) then
9197 Expand_Array_Comparison (N);
9198 return;
9199 end if;
9200
9201 -- Deal with boolean operands
9202
9203 if Is_Boolean_Type (Typ1) then
9204 Adjust_Condition (Op1);
9205 Adjust_Condition (Op2);
9206 Set_Etype (N, Standard_Boolean);
9207 Adjust_Result_Type (N, Typ);
9208 end if;
9209
9210 Rewrite_Comparison (N);
9211
9212 -- Try to narrow the operation
9213
9214 if Typ1 = Universal_Integer and then Nkind (N) = N_Op_Ge then
9215 Narrow_Large_Operation (N);
9216 end if;
9217
9218 Optimize_Length_Comparison (N);
9219 end Expand_N_Op_Ge;
9220
9221 --------------------
9222 -- Expand_N_Op_Gt --
9223 --------------------
9224
9225 procedure Expand_N_Op_Gt (N : Node_Id) is
9226 Typ : constant Entity_Id := Etype (N);
9227 Op1 : constant Node_Id := Left_Opnd (N);
9228 Op2 : constant Node_Id := Right_Opnd (N);
9229 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
9230
9231 begin
9232 Binary_Op_Validity_Checks (N);
9233
9234 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9235 -- means we no longer have a comparison operation, we are all done.
9236
9237 if Minimized_Eliminated_Overflow_Check (Op1) then
9238 Expand_Compare_Minimize_Eliminate_Overflow (N);
9239 end if;
9240
9241 if Nkind (N) /= N_Op_Gt then
9242 return;
9243 end if;
9244
9245 -- Deal with array type operands
9246
9247 if Is_Array_Type (Typ1) then
9248 Expand_Array_Comparison (N);
9249 return;
9250 end if;
9251
9252 -- Deal with boolean type operands
9253
9254 if Is_Boolean_Type (Typ1) then
9255 Adjust_Condition (Op1);
9256 Adjust_Condition (Op2);
9257 Set_Etype (N, Standard_Boolean);
9258 Adjust_Result_Type (N, Typ);
9259 end if;
9260
9261 Rewrite_Comparison (N);
9262
9263 -- Try to narrow the operation
9264
9265 if Typ1 = Universal_Integer and then Nkind (N) = N_Op_Gt then
9266 Narrow_Large_Operation (N);
9267 end if;
9268
9269 Optimize_Length_Comparison (N);
9270 end Expand_N_Op_Gt;
9271
9272 --------------------
9273 -- Expand_N_Op_Le --
9274 --------------------
9275
9276 procedure Expand_N_Op_Le (N : Node_Id) is
9277 Typ : constant Entity_Id := Etype (N);
9278 Op1 : constant Node_Id := Left_Opnd (N);
9279 Op2 : constant Node_Id := Right_Opnd (N);
9280 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
9281
9282 begin
9283 Binary_Op_Validity_Checks (N);
9284
9285 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9286 -- means we no longer have a comparison operation, we are all done.
9287
9288 if Minimized_Eliminated_Overflow_Check (Op1) then
9289 Expand_Compare_Minimize_Eliminate_Overflow (N);
9290 end if;
9291
9292 if Nkind (N) /= N_Op_Le then
9293 return;
9294 end if;
9295
9296 -- Deal with array type operands
9297
9298 if Is_Array_Type (Typ1) then
9299 Expand_Array_Comparison (N);
9300 return;
9301 end if;
9302
9303 -- Deal with Boolean type operands
9304
9305 if Is_Boolean_Type (Typ1) then
9306 Adjust_Condition (Op1);
9307 Adjust_Condition (Op2);
9308 Set_Etype (N, Standard_Boolean);
9309 Adjust_Result_Type (N, Typ);
9310 end if;
9311
9312 Rewrite_Comparison (N);
9313
9314 -- Try to narrow the operation
9315
9316 if Typ1 = Universal_Integer and then Nkind (N) = N_Op_Le then
9317 Narrow_Large_Operation (N);
9318 end if;
9319
9320 Optimize_Length_Comparison (N);
9321 end Expand_N_Op_Le;
9322
9323 --------------------
9324 -- Expand_N_Op_Lt --
9325 --------------------
9326
9327 procedure Expand_N_Op_Lt (N : Node_Id) is
9328 Typ : constant Entity_Id := Etype (N);
9329 Op1 : constant Node_Id := Left_Opnd (N);
9330 Op2 : constant Node_Id := Right_Opnd (N);
9331 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
9332
9333 begin
9334 Binary_Op_Validity_Checks (N);
9335
9336 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
9337 -- means we no longer have a comparison operation, we are all done.
9338
9339 if Minimized_Eliminated_Overflow_Check (Op1) then
9340 Expand_Compare_Minimize_Eliminate_Overflow (N);
9341 end if;
9342
9343 if Nkind (N) /= N_Op_Lt then
9344 return;
9345 end if;
9346
9347 -- Deal with array type operands
9348
9349 if Is_Array_Type (Typ1) then
9350 Expand_Array_Comparison (N);
9351 return;
9352 end if;
9353
9354 -- Deal with Boolean type operands
9355
9356 if Is_Boolean_Type (Typ1) then
9357 Adjust_Condition (Op1);
9358 Adjust_Condition (Op2);
9359 Set_Etype (N, Standard_Boolean);
9360 Adjust_Result_Type (N, Typ);
9361 end if;
9362
9363 Rewrite_Comparison (N);
9364
9365 -- Try to narrow the operation
9366
9367 if Typ1 = Universal_Integer and then Nkind (N) = N_Op_Lt then
9368 Narrow_Large_Operation (N);
9369 end if;
9370
9371 Optimize_Length_Comparison (N);
9372 end Expand_N_Op_Lt;
9373
9374 -----------------------
9375 -- Expand_N_Op_Minus --
9376 -----------------------
9377
9378 procedure Expand_N_Op_Minus (N : Node_Id) is
9379 Loc : constant Source_Ptr := Sloc (N);
9380 Typ : constant Entity_Id := Etype (N);
9381
9382 begin
9383 Unary_Op_Validity_Checks (N);
9384
9385 -- Check for MINIMIZED/ELIMINATED overflow mode
9386
9387 if Minimized_Eliminated_Overflow_Check (N) then
9388 Apply_Arithmetic_Overflow_Check (N);
9389 return;
9390 end if;
9391
9392 -- Try to narrow the operation
9393
9394 if Typ = Universal_Integer then
9395 Narrow_Large_Operation (N);
9396
9397 if Nkind (N) /= N_Op_Minus then
9398 return;
9399 end if;
9400 end if;
9401
9402 if not Backend_Overflow_Checks_On_Target
9403 and then Is_Signed_Integer_Type (Typ)
9404 and then Do_Overflow_Check (N)
9405 then
9406 -- Software overflow checking expands -expr into (0 - expr)
9407
9408 Rewrite (N,
9409 Make_Op_Subtract (Loc,
9410 Left_Opnd => Make_Integer_Literal (Loc, 0),
9411 Right_Opnd => Right_Opnd (N)));
9412
9413 Analyze_And_Resolve (N, Typ);
9414 end if;
9415
9416 Expand_Nonbinary_Modular_Op (N);
9417 end Expand_N_Op_Minus;
9418
9419 ---------------------
9420 -- Expand_N_Op_Mod --
9421 ---------------------
9422
9423 procedure Expand_N_Op_Mod (N : Node_Id) is
9424 Loc : constant Source_Ptr := Sloc (N);
9425 Typ : constant Entity_Id := Etype (N);
9426 DDC : constant Boolean := Do_Division_Check (N);
9427
9428 Is_Stoele_Mod : constant Boolean :=
9429 Is_RTE (Typ, RE_Address)
9430 and then Nkind (Right_Opnd (N)) = N_Unchecked_Type_Conversion
9431 and then
9432 Is_RTE (Etype (Expression (Right_Opnd (N))), RE_Storage_Offset);
9433 -- True if this is the special mod operator of System.Storage_Elements
9434
9435 Left : Node_Id;
9436 Right : Node_Id;
9437
9438 LLB : Uint;
9439 Llo : Uint;
9440 Lhi : Uint;
9441 LOK : Boolean;
9442 Rlo : Uint;
9443 Rhi : Uint;
9444 ROK : Boolean;
9445
9446 pragma Warnings (Off, Lhi);
9447
9448 begin
9449 Binary_Op_Validity_Checks (N);
9450
9451 -- Check for MINIMIZED/ELIMINATED overflow mode
9452
9453 if Minimized_Eliminated_Overflow_Check (N) then
9454 Apply_Arithmetic_Overflow_Check (N);
9455 return;
9456 end if;
9457
9458 -- Try to narrow the operation
9459
9460 if Typ = Universal_Integer then
9461 Narrow_Large_Operation (N);
9462
9463 if Nkind (N) /= N_Op_Mod then
9464 return;
9465 end if;
9466 end if;
9467
9468 -- For the special mod operator of System.Storage_Elements, the checks
9469 -- are subsumed into the handling of the negative case below.
9470
9471 if Is_Integer_Type (Typ) and then not Is_Stoele_Mod then
9472 Apply_Divide_Checks (N);
9473
9474 -- All done if we don't have a MOD any more, which can happen as a
9475 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
9476
9477 if Nkind (N) /= N_Op_Mod then
9478 return;
9479 end if;
9480 end if;
9481
9482 -- Proceed with expansion of mod operator
9483
9484 Left := Left_Opnd (N);
9485 Right := Right_Opnd (N);
9486
9487 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
9488 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
9489
9490 -- Convert mod to rem if operands are both known to be non-negative, or
9491 -- both known to be non-positive (these are the cases in which rem and
9492 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
9493 -- likely that this will improve the quality of code, (the operation now
9494 -- corresponds to the hardware remainder), and it does not seem likely
9495 -- that it could be harmful.
9496
9497 if (LOK and ROK)
9498 and then ((Llo >= 0 and then Rlo >= 0)
9499 or else
9500 (Lhi <= 0 and then Rhi <= 0))
9501 and then not Is_Stoele_Mod
9502 then
9503 Rewrite (N,
9504 Make_Op_Rem (Sloc (N),
9505 Left_Opnd => Left_Opnd (N),
9506 Right_Opnd => Right_Opnd (N)));
9507
9508 -- Instead of reanalyzing the node we do the analysis manually. This
9509 -- avoids anomalies when the replacement is done in an instance and
9510 -- is epsilon more efficient.
9511
9512 pragma Assert (Entity (N) = Standard_Op_Rem);
9513 Set_Etype (N, Typ);
9514 Set_Do_Division_Check (N, DDC);
9515 Expand_N_Op_Rem (N);
9516 Set_Analyzed (N);
9517 return;
9518
9519 -- Otherwise, normal mod processing
9520
9521 else
9522 -- Apply optimization x mod 1 = 0. We don't really need that with
9523 -- gcc, but it is useful with other back ends and is certainly
9524 -- harmless.
9525
9526 if Is_Integer_Type (Etype (N))
9527 and then Compile_Time_Known_Value (Right)
9528 and then Expr_Value (Right) = Uint_1
9529 then
9530 -- Call Remove_Side_Effects to ensure that any side effects in
9531 -- the ignored left operand (in particular function calls to
9532 -- user defined functions) are properly executed.
9533
9534 Remove_Side_Effects (Left);
9535
9536 Rewrite (N, Make_Integer_Literal (Loc, 0));
9537 Analyze_And_Resolve (N, Typ);
9538 return;
9539 end if;
9540
9541 -- The negative case makes no sense since it is a case of a mod where
9542 -- the left argument is unsigned and the right argument is signed. In
9543 -- accordance with the (spirit of the) permission of RM 13.7.1(16),
9544 -- we raise CE, and also include the zero case here. Yes, the RM says
9545 -- PE, but this really is so obviously more like a constraint error.
9546
9547 if Is_Stoele_Mod and then (not ROK or else Rlo <= 0) then
9548 Insert_Action (N,
9549 Make_Raise_Constraint_Error (Loc,
9550 Condition =>
9551 Make_Op_Le (Loc,
9552 Left_Opnd =>
9553 Duplicate_Subexpr_No_Checks (Expression (Right)),
9554 Right_Opnd => Make_Integer_Literal (Loc, 0)),
9555 Reason => CE_Overflow_Check_Failed));
9556 return;
9557 end if;
9558
9559 -- Deal with annoying case of largest negative number mod minus one.
9560 -- Gigi may not handle this case correctly, because on some targets,
9561 -- the mod value is computed using a divide instruction which gives
9562 -- an overflow trap for this case.
9563
9564 -- It would be a bit more efficient to figure out which targets
9565 -- this is really needed for, but in practice it is reasonable
9566 -- to do the following special check in all cases, since it means
9567 -- we get a clearer message, and also the overhead is minimal given
9568 -- that division is expensive in any case.
9569
9570 -- In fact the check is quite easy, if the right operand is -1, then
9571 -- the mod value is always 0, and we can just ignore the left operand
9572 -- completely in this case.
9573
9574 -- This only applies if we still have a mod operator. Skip if we
9575 -- have already rewritten this (e.g. in the case of eliminated
9576 -- overflow checks which have driven us into bignum mode).
9577
9578 if Nkind (N) = N_Op_Mod then
9579
9580 -- The operand type may be private (e.g. in the expansion of an
9581 -- intrinsic operation) so we must use the underlying type to get
9582 -- the bounds, and convert the literals explicitly.
9583
9584 LLB :=
9585 Expr_Value
9586 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
9587
9588 if (not ROK or else (Rlo <= (-1) and then (-1) <= Rhi))
9589 and then (not LOK or else Llo = LLB)
9590 and then not CodePeer_Mode
9591 then
9592 Rewrite (N,
9593 Make_If_Expression (Loc,
9594 Expressions => New_List (
9595 Make_Op_Eq (Loc,
9596 Left_Opnd => Duplicate_Subexpr (Right),
9597 Right_Opnd =>
9598 Unchecked_Convert_To (Typ,
9599 Make_Integer_Literal (Loc, -1))),
9600 Unchecked_Convert_To (Typ,
9601 Make_Integer_Literal (Loc, Uint_0)),
9602 Relocate_Node (N))));
9603
9604 Set_Analyzed (Next (Next (First (Expressions (N)))));
9605 Analyze_And_Resolve (N, Typ);
9606 end if;
9607 end if;
9608 end if;
9609 end Expand_N_Op_Mod;
9610
9611 --------------------------
9612 -- Expand_N_Op_Multiply --
9613 --------------------------
9614
9615 procedure Expand_N_Op_Multiply (N : Node_Id) is
9616 Loc : constant Source_Ptr := Sloc (N);
9617 Lop : constant Node_Id := Left_Opnd (N);
9618 Rop : constant Node_Id := Right_Opnd (N);
9619
9620 Lp2 : constant Boolean :=
9621 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
9622 Rp2 : constant Boolean :=
9623 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
9624
9625 Ltyp : constant Entity_Id := Etype (Lop);
9626 Rtyp : constant Entity_Id := Etype (Rop);
9627 Typ : Entity_Id := Etype (N);
9628
9629 begin
9630 Binary_Op_Validity_Checks (N);
9631
9632 -- Check for MINIMIZED/ELIMINATED overflow mode
9633
9634 if Minimized_Eliminated_Overflow_Check (N) then
9635 Apply_Arithmetic_Overflow_Check (N);
9636 return;
9637 end if;
9638
9639 -- Special optimizations for integer types
9640
9641 if Is_Integer_Type (Typ) then
9642
9643 -- N * 0 = 0 for integer types
9644
9645 if Compile_Time_Known_Value (Rop)
9646 and then Expr_Value (Rop) = Uint_0
9647 then
9648 -- Call Remove_Side_Effects to ensure that any side effects in
9649 -- the ignored left operand (in particular function calls to
9650 -- user defined functions) are properly executed.
9651
9652 Remove_Side_Effects (Lop);
9653
9654 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9655 Analyze_And_Resolve (N, Typ);
9656 return;
9657 end if;
9658
9659 -- Similar handling for 0 * N = 0
9660
9661 if Compile_Time_Known_Value (Lop)
9662 and then Expr_Value (Lop) = Uint_0
9663 then
9664 Remove_Side_Effects (Rop);
9665 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
9666 Analyze_And_Resolve (N, Typ);
9667 return;
9668 end if;
9669
9670 -- N * 1 = 1 * N = N for integer types
9671
9672 -- This optimisation is not done if we are going to
9673 -- rewrite the product 1 * 2 ** N to a shift.
9674
9675 if Compile_Time_Known_Value (Rop)
9676 and then Expr_Value (Rop) = Uint_1
9677 and then not Lp2
9678 then
9679 Rewrite (N, Lop);
9680 return;
9681
9682 elsif Compile_Time_Known_Value (Lop)
9683 and then Expr_Value (Lop) = Uint_1
9684 and then not Rp2
9685 then
9686 Rewrite (N, Rop);
9687 return;
9688 end if;
9689 end if;
9690
9691 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
9692 -- Is_Power_Of_2_For_Shift is set means that we know that our left
9693 -- operand is an integer, as required for this to work.
9694
9695 if Rp2 then
9696 if Lp2 then
9697
9698 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
9699
9700 Rewrite (N,
9701 Make_Op_Expon (Loc,
9702 Left_Opnd => Make_Integer_Literal (Loc, 2),
9703 Right_Opnd =>
9704 Make_Op_Add (Loc,
9705 Left_Opnd => Right_Opnd (Lop),
9706 Right_Opnd => Right_Opnd (Rop))));
9707 Analyze_And_Resolve (N, Typ);
9708 return;
9709
9710 else
9711 -- If the result is modular, perform the reduction of the result
9712 -- appropriately.
9713
9714 if Is_Modular_Integer_Type (Typ)
9715 and then not Non_Binary_Modulus (Typ)
9716 then
9717 Rewrite (N,
9718 Make_Op_And (Loc,
9719 Left_Opnd =>
9720 Make_Op_Shift_Left (Loc,
9721 Left_Opnd => Lop,
9722 Right_Opnd =>
9723 Convert_To (Standard_Natural, Right_Opnd (Rop))),
9724 Right_Opnd =>
9725 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9726
9727 else
9728 Rewrite (N,
9729 Make_Op_Shift_Left (Loc,
9730 Left_Opnd => Lop,
9731 Right_Opnd =>
9732 Convert_To (Standard_Natural, Right_Opnd (Rop))));
9733 end if;
9734
9735 Analyze_And_Resolve (N, Typ);
9736 return;
9737 end if;
9738
9739 -- Same processing for the operands the other way round
9740
9741 elsif Lp2 then
9742 if Is_Modular_Integer_Type (Typ)
9743 and then not Non_Binary_Modulus (Typ)
9744 then
9745 Rewrite (N,
9746 Make_Op_And (Loc,
9747 Left_Opnd =>
9748 Make_Op_Shift_Left (Loc,
9749 Left_Opnd => Rop,
9750 Right_Opnd =>
9751 Convert_To (Standard_Natural, Right_Opnd (Lop))),
9752 Right_Opnd =>
9753 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
9754
9755 else
9756 Rewrite (N,
9757 Make_Op_Shift_Left (Loc,
9758 Left_Opnd => Rop,
9759 Right_Opnd =>
9760 Convert_To (Standard_Natural, Right_Opnd (Lop))));
9761 end if;
9762
9763 Analyze_And_Resolve (N, Typ);
9764 return;
9765 end if;
9766
9767 -- Try to narrow the operation
9768
9769 if Typ = Universal_Integer then
9770 Narrow_Large_Operation (N);
9771
9772 if Nkind (N) /= N_Op_Multiply then
9773 return;
9774 end if;
9775 end if;
9776
9777 -- Do required fixup of universal fixed operation
9778
9779 if Typ = Universal_Fixed then
9780 Fixup_Universal_Fixed_Operation (N);
9781 Typ := Etype (N);
9782 end if;
9783
9784 -- Multiplications with fixed-point results
9785
9786 if Is_Fixed_Point_Type (Typ) then
9787
9788 -- Case of fixed * integer => fixed
9789
9790 if Is_Integer_Type (Rtyp) then
9791 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
9792
9793 -- Case of integer * fixed => fixed
9794
9795 elsif Is_Integer_Type (Ltyp) then
9796 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
9797
9798 -- Case of fixed * fixed => fixed
9799
9800 else
9801 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
9802 end if;
9803
9804 -- Other cases of multiplication of fixed-point operands
9805
9806 elsif Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp) then
9807 if Is_Integer_Type (Typ) then
9808 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
9809 else
9810 pragma Assert (Is_Floating_Point_Type (Typ));
9811 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
9812 end if;
9813
9814 -- Mixed-mode operations can appear in a non-static universal context,
9815 -- in which case the integer argument must be converted explicitly.
9816
9817 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
9818 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
9819 Analyze_And_Resolve (Rop, Universal_Real);
9820
9821 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
9822 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
9823 Analyze_And_Resolve (Lop, Universal_Real);
9824
9825 -- Non-fixed point cases, check software overflow checking required
9826
9827 elsif Is_Signed_Integer_Type (Etype (N)) then
9828 Apply_Arithmetic_Overflow_Check (N);
9829 end if;
9830
9831 -- Overflow checks for floating-point if -gnateF mode active
9832
9833 Check_Float_Op_Overflow (N);
9834
9835 Expand_Nonbinary_Modular_Op (N);
9836 end Expand_N_Op_Multiply;
9837
9838 --------------------
9839 -- Expand_N_Op_Ne --
9840 --------------------
9841
9842 procedure Expand_N_Op_Ne (N : Node_Id) is
9843 Typ : constant Entity_Id := Etype (Left_Opnd (N));
9844
9845 begin
9846 -- Case of elementary type with standard operator. But if unnesting,
9847 -- handle elementary types whose Equivalent_Types are records because
9848 -- there may be padding or undefined fields.
9849
9850 if Is_Elementary_Type (Typ)
9851 and then Sloc (Entity (N)) = Standard_Location
9852 and then not (Ekind (Typ) in E_Class_Wide_Type
9853 | E_Class_Wide_Subtype
9854 | E_Access_Subprogram_Type
9855 | E_Access_Protected_Subprogram_Type
9856 | E_Anonymous_Access_Protected_Subprogram_Type
9857 | E_Exception_Type
9858 and then Present (Equivalent_Type (Typ))
9859 and then Is_Record_Type (Equivalent_Type (Typ)))
9860 then
9861 Binary_Op_Validity_Checks (N);
9862
9863 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
9864 -- means we no longer have a /= operation, we are all done.
9865
9866 if Minimized_Eliminated_Overflow_Check (Left_Opnd (N)) then
9867 Expand_Compare_Minimize_Eliminate_Overflow (N);
9868 end if;
9869
9870 if Nkind (N) /= N_Op_Ne then
9871 return;
9872 end if;
9873
9874 -- Boolean types (requiring handling of non-standard case)
9875
9876 if Is_Boolean_Type (Typ) then
9877 Adjust_Condition (Left_Opnd (N));
9878 Adjust_Condition (Right_Opnd (N));
9879 Set_Etype (N, Standard_Boolean);
9880 Adjust_Result_Type (N, Typ);
9881 end if;
9882
9883 Rewrite_Comparison (N);
9884
9885 -- Try to narrow the operation
9886
9887 if Typ = Universal_Integer and then Nkind (N) = N_Op_Ne then
9888 Narrow_Large_Operation (N);
9889 end if;
9890
9891 -- For all cases other than elementary types, we rewrite node as the
9892 -- negation of an equality operation, and reanalyze. The equality to be
9893 -- used is defined in the same scope and has the same signature. This
9894 -- signature must be set explicitly since in an instance it may not have
9895 -- the same visibility as in the generic unit. This avoids duplicating
9896 -- or factoring the complex code for record/array equality tests etc.
9897
9898 -- This case is also used for the minimal expansion performed in
9899 -- GNATprove mode.
9900
9901 else
9902 declare
9903 Loc : constant Source_Ptr := Sloc (N);
9904 Neg : Node_Id;
9905 Ne : constant Entity_Id := Entity (N);
9906
9907 begin
9908 Binary_Op_Validity_Checks (N);
9909
9910 Neg :=
9911 Make_Op_Not (Loc,
9912 Right_Opnd =>
9913 Make_Op_Eq (Loc,
9914 Left_Opnd => Left_Opnd (N),
9915 Right_Opnd => Right_Opnd (N)));
9916
9917 if Scope (Ne) /= Standard_Standard then
9918 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
9919 end if;
9920
9921 -- For navigation purposes, we want to treat the inequality as an
9922 -- implicit reference to the corresponding equality. Preserve the
9923 -- Comes_From_ source flag to generate proper Xref entries.
9924
9925 Preserve_Comes_From_Source (Neg, N);
9926 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
9927 Rewrite (N, Neg);
9928 Analyze_And_Resolve (N, Standard_Boolean);
9929 end;
9930 end if;
9931
9932 -- No need for optimization in GNATprove mode, where we would rather see
9933 -- the original source expression.
9934
9935 if not GNATprove_Mode then
9936 Optimize_Length_Comparison (N);
9937 end if;
9938 end Expand_N_Op_Ne;
9939
9940 ---------------------
9941 -- Expand_N_Op_Not --
9942 ---------------------
9943
9944 -- If the argument is other than a Boolean array type, there is no special
9945 -- expansion required, except for dealing with validity checks, and non-
9946 -- standard boolean representations.
9947
9948 -- For the packed array case, we call the special routine in Exp_Pakd,
9949 -- except that if the component size is greater than one, we use the
9950 -- standard routine generating a gruesome loop (it is so peculiar to have
9951 -- packed arrays with non-standard Boolean representations anyway, so it
9952 -- does not matter that we do not handle this case efficiently).
9953
9954 -- For the unpacked array case (and for the special packed case where we
9955 -- have non standard Booleans, as discussed above), we generate and insert
9956 -- into the tree the following function definition:
9957
9958 -- function Nnnn (A : arr) is
9959 -- B : arr;
9960 -- begin
9961 -- for J in a'range loop
9962 -- B (J) := not A (J);
9963 -- end loop;
9964 -- return B;
9965 -- end Nnnn;
9966
9967 -- Here arr is the actual subtype of the parameter (and hence always
9968 -- constrained). Then we replace the not with a call to this subprogram.
9969
9970 procedure Expand_N_Op_Not (N : Node_Id) is
9971 Loc : constant Source_Ptr := Sloc (N);
9972 Typ : constant Entity_Id := Etype (Right_Opnd (N));
9973 Opnd : Node_Id;
9974 Arr : Entity_Id;
9975 A : Entity_Id;
9976 B : Entity_Id;
9977 J : Entity_Id;
9978 A_J : Node_Id;
9979 B_J : Node_Id;
9980
9981 Func_Name : Entity_Id;
9982 Loop_Statement : Node_Id;
9983
9984 begin
9985 Unary_Op_Validity_Checks (N);
9986
9987 -- For boolean operand, deal with non-standard booleans
9988
9989 if Is_Boolean_Type (Typ) then
9990 Adjust_Condition (Right_Opnd (N));
9991 Set_Etype (N, Standard_Boolean);
9992 Adjust_Result_Type (N, Typ);
9993 return;
9994 end if;
9995
9996 -- Only array types need any other processing
9997
9998 if not Is_Array_Type (Typ) then
9999 return;
10000 end if;
10001
10002 -- Case of array operand. If bit packed with a component size of 1,
10003 -- handle it in Exp_Pakd if the operand is known to be aligned.
10004
10005 if Is_Bit_Packed_Array (Typ)
10006 and then Component_Size (Typ) = 1
10007 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
10008 then
10009 Expand_Packed_Not (N);
10010 return;
10011 end if;
10012
10013 -- Case of array operand which is not bit-packed. If the context is
10014 -- a safe assignment, call in-place operation, If context is a larger
10015 -- boolean expression in the context of a safe assignment, expansion is
10016 -- done by enclosing operation.
10017
10018 Opnd := Relocate_Node (Right_Opnd (N));
10019 Convert_To_Actual_Subtype (Opnd);
10020 Arr := Etype (Opnd);
10021 Ensure_Defined (Arr, N);
10022 Silly_Boolean_Array_Not_Test (N, Arr);
10023
10024 if Nkind (Parent (N)) = N_Assignment_Statement then
10025 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
10026 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
10027 return;
10028
10029 -- Special case the negation of a binary operation
10030
10031 elsif Nkind (Opnd) in N_Op_And | N_Op_Or | N_Op_Xor
10032 and then Safe_In_Place_Array_Op
10033 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
10034 then
10035 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
10036 return;
10037 end if;
10038
10039 elsif Nkind (Parent (N)) in N_Binary_Op
10040 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
10041 then
10042 declare
10043 Op1 : constant Node_Id := Left_Opnd (Parent (N));
10044 Op2 : constant Node_Id := Right_Opnd (Parent (N));
10045 Lhs : constant Node_Id := Name (Parent (Parent (N)));
10046
10047 begin
10048 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
10049
10050 -- (not A) op (not B) can be reduced to a single call
10051
10052 if N = Op1 and then Nkind (Op2) = N_Op_Not then
10053 return;
10054
10055 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
10056 return;
10057
10058 -- A xor (not B) can also be special-cased
10059
10060 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
10061 return;
10062 end if;
10063 end if;
10064 end;
10065 end if;
10066
10067 A := Make_Defining_Identifier (Loc, Name_uA);
10068 B := Make_Defining_Identifier (Loc, Name_uB);
10069 J := Make_Defining_Identifier (Loc, Name_uJ);
10070
10071 A_J :=
10072 Make_Indexed_Component (Loc,
10073 Prefix => New_Occurrence_Of (A, Loc),
10074 Expressions => New_List (New_Occurrence_Of (J, Loc)));
10075
10076 B_J :=
10077 Make_Indexed_Component (Loc,
10078 Prefix => New_Occurrence_Of (B, Loc),
10079 Expressions => New_List (New_Occurrence_Of (J, Loc)));
10080
10081 Loop_Statement :=
10082 Make_Implicit_Loop_Statement (N,
10083 Identifier => Empty,
10084
10085 Iteration_Scheme =>
10086 Make_Iteration_Scheme (Loc,
10087 Loop_Parameter_Specification =>
10088 Make_Loop_Parameter_Specification (Loc,
10089 Defining_Identifier => J,
10090 Discrete_Subtype_Definition =>
10091 Make_Attribute_Reference (Loc,
10092 Prefix => Make_Identifier (Loc, Chars (A)),
10093 Attribute_Name => Name_Range))),
10094
10095 Statements => New_List (
10096 Make_Assignment_Statement (Loc,
10097 Name => B_J,
10098 Expression => Make_Op_Not (Loc, A_J))));
10099
10100 Func_Name := Make_Temporary (Loc, 'N');
10101 Set_Is_Inlined (Func_Name);
10102
10103 Insert_Action (N,
10104 Make_Subprogram_Body (Loc,
10105 Specification =>
10106 Make_Function_Specification (Loc,
10107 Defining_Unit_Name => Func_Name,
10108 Parameter_Specifications => New_List (
10109 Make_Parameter_Specification (Loc,
10110 Defining_Identifier => A,
10111 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
10112 Result_Definition => New_Occurrence_Of (Typ, Loc)),
10113
10114 Declarations => New_List (
10115 Make_Object_Declaration (Loc,
10116 Defining_Identifier => B,
10117 Object_Definition => New_Occurrence_Of (Arr, Loc))),
10118
10119 Handled_Statement_Sequence =>
10120 Make_Handled_Sequence_Of_Statements (Loc,
10121 Statements => New_List (
10122 Loop_Statement,
10123 Make_Simple_Return_Statement (Loc,
10124 Expression => Make_Identifier (Loc, Chars (B)))))));
10125
10126 Rewrite (N,
10127 Make_Function_Call (Loc,
10128 Name => New_Occurrence_Of (Func_Name, Loc),
10129 Parameter_Associations => New_List (Opnd)));
10130
10131 Analyze_And_Resolve (N, Typ);
10132 end Expand_N_Op_Not;
10133
10134 --------------------
10135 -- Expand_N_Op_Or --
10136 --------------------
10137
10138 procedure Expand_N_Op_Or (N : Node_Id) is
10139 Typ : constant Entity_Id := Etype (N);
10140
10141 begin
10142 Binary_Op_Validity_Checks (N);
10143
10144 if Is_Array_Type (Etype (N)) then
10145 Expand_Boolean_Operator (N);
10146
10147 elsif Is_Boolean_Type (Etype (N)) then
10148 Adjust_Condition (Left_Opnd (N));
10149 Adjust_Condition (Right_Opnd (N));
10150 Set_Etype (N, Standard_Boolean);
10151 Adjust_Result_Type (N, Typ);
10152
10153 elsif Is_Intrinsic_Subprogram (Entity (N)) then
10154 Expand_Intrinsic_Call (N, Entity (N));
10155 end if;
10156
10157 Expand_Nonbinary_Modular_Op (N);
10158 end Expand_N_Op_Or;
10159
10160 ----------------------
10161 -- Expand_N_Op_Plus --
10162 ----------------------
10163
10164 procedure Expand_N_Op_Plus (N : Node_Id) is
10165 Typ : constant Entity_Id := Etype (N);
10166
10167 begin
10168 Unary_Op_Validity_Checks (N);
10169
10170 -- Check for MINIMIZED/ELIMINATED overflow mode
10171
10172 if Minimized_Eliminated_Overflow_Check (N) then
10173 Apply_Arithmetic_Overflow_Check (N);
10174 return;
10175 end if;
10176
10177 -- Try to narrow the operation
10178
10179 if Typ = Universal_Integer then
10180 Narrow_Large_Operation (N);
10181 end if;
10182 end Expand_N_Op_Plus;
10183
10184 ---------------------
10185 -- Expand_N_Op_Rem --
10186 ---------------------
10187
10188 procedure Expand_N_Op_Rem (N : Node_Id) is
10189 Loc : constant Source_Ptr := Sloc (N);
10190 Typ : constant Entity_Id := Etype (N);
10191
10192 Left : Node_Id;
10193 Right : Node_Id;
10194
10195 Lo : Uint;
10196 Hi : Uint;
10197 OK : Boolean;
10198
10199 Lneg : Boolean;
10200 Rneg : Boolean;
10201 -- Set if corresponding operand can be negative
10202
10203 begin
10204 Binary_Op_Validity_Checks (N);
10205
10206 -- Check for MINIMIZED/ELIMINATED overflow mode
10207
10208 if Minimized_Eliminated_Overflow_Check (N) then
10209 Apply_Arithmetic_Overflow_Check (N);
10210 return;
10211 end if;
10212
10213 -- Try to narrow the operation
10214
10215 if Typ = Universal_Integer then
10216 Narrow_Large_Operation (N);
10217
10218 if Nkind (N) /= N_Op_Rem then
10219 return;
10220 end if;
10221 end if;
10222
10223 if Is_Integer_Type (Etype (N)) then
10224 Apply_Divide_Checks (N);
10225
10226 -- All done if we don't have a REM any more, which can happen as a
10227 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
10228
10229 if Nkind (N) /= N_Op_Rem then
10230 return;
10231 end if;
10232 end if;
10233
10234 -- Proceed with expansion of REM
10235
10236 Left := Left_Opnd (N);
10237 Right := Right_Opnd (N);
10238
10239 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
10240 -- but it is useful with other back ends, and is certainly harmless.
10241
10242 if Is_Integer_Type (Etype (N))
10243 and then Compile_Time_Known_Value (Right)
10244 and then Expr_Value (Right) = Uint_1
10245 then
10246 -- Call Remove_Side_Effects to ensure that any side effects in the
10247 -- ignored left operand (in particular function calls to user defined
10248 -- functions) are properly executed.
10249
10250 Remove_Side_Effects (Left);
10251
10252 Rewrite (N, Make_Integer_Literal (Loc, 0));
10253 Analyze_And_Resolve (N, Typ);
10254 return;
10255 end if;
10256
10257 -- Deal with annoying case of largest negative number remainder minus
10258 -- one. Gigi may not handle this case correctly, because on some
10259 -- targets, the mod value is computed using a divide instruction
10260 -- which gives an overflow trap for this case.
10261
10262 -- It would be a bit more efficient to figure out which targets this
10263 -- is really needed for, but in practice it is reasonable to do the
10264 -- following special check in all cases, since it means we get a clearer
10265 -- message, and also the overhead is minimal given that division is
10266 -- expensive in any case.
10267
10268 -- In fact the check is quite easy, if the right operand is -1, then
10269 -- the remainder is always 0, and we can just ignore the left operand
10270 -- completely in this case.
10271
10272 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
10273 Lneg := not OK or else Lo < 0;
10274
10275 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
10276 Rneg := not OK or else Lo < 0;
10277
10278 -- We won't mess with trying to find out if the left operand can really
10279 -- be the largest negative number (that's a pain in the case of private
10280 -- types and this is really marginal). We will just assume that we need
10281 -- the test if the left operand can be negative at all.
10282
10283 if (Lneg and Rneg)
10284 and then not CodePeer_Mode
10285 then
10286 Rewrite (N,
10287 Make_If_Expression (Loc,
10288 Expressions => New_List (
10289 Make_Op_Eq (Loc,
10290 Left_Opnd => Duplicate_Subexpr (Right),
10291 Right_Opnd =>
10292 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
10293
10294 Unchecked_Convert_To (Typ,
10295 Make_Integer_Literal (Loc, Uint_0)),
10296
10297 Relocate_Node (N))));
10298
10299 Set_Analyzed (Next (Next (First (Expressions (N)))));
10300 Analyze_And_Resolve (N, Typ);
10301 end if;
10302 end Expand_N_Op_Rem;
10303
10304 -----------------------------
10305 -- Expand_N_Op_Rotate_Left --
10306 -----------------------------
10307
10308 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
10309 begin
10310 Binary_Op_Validity_Checks (N);
10311 end Expand_N_Op_Rotate_Left;
10312
10313 ------------------------------
10314 -- Expand_N_Op_Rotate_Right --
10315 ------------------------------
10316
10317 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
10318 begin
10319 Binary_Op_Validity_Checks (N);
10320 end Expand_N_Op_Rotate_Right;
10321
10322 ----------------------------
10323 -- Expand_N_Op_Shift_Left --
10324 ----------------------------
10325
10326 -- Note: nothing in this routine depends on left as opposed to right shifts
10327 -- so we share the routine for expanding shift right operations.
10328
10329 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
10330 begin
10331 Binary_Op_Validity_Checks (N);
10332 end Expand_N_Op_Shift_Left;
10333
10334 -----------------------------
10335 -- Expand_N_Op_Shift_Right --
10336 -----------------------------
10337
10338 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
10339 begin
10340 -- Share shift left circuit
10341
10342 Expand_N_Op_Shift_Left (N);
10343 end Expand_N_Op_Shift_Right;
10344
10345 ----------------------------------------
10346 -- Expand_N_Op_Shift_Right_Arithmetic --
10347 ----------------------------------------
10348
10349 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
10350 begin
10351 Binary_Op_Validity_Checks (N);
10352 end Expand_N_Op_Shift_Right_Arithmetic;
10353
10354 --------------------------
10355 -- Expand_N_Op_Subtract --
10356 --------------------------
10357
10358 procedure Expand_N_Op_Subtract (N : Node_Id) is
10359 Typ : constant Entity_Id := Etype (N);
10360
10361 begin
10362 Binary_Op_Validity_Checks (N);
10363
10364 -- Check for MINIMIZED/ELIMINATED overflow mode
10365
10366 if Minimized_Eliminated_Overflow_Check (N) then
10367 Apply_Arithmetic_Overflow_Check (N);
10368 return;
10369 end if;
10370
10371 -- Try to narrow the operation
10372
10373 if Typ = Universal_Integer then
10374 Narrow_Large_Operation (N);
10375
10376 if Nkind (N) /= N_Op_Subtract then
10377 return;
10378 end if;
10379 end if;
10380
10381 -- N - 0 = N for integer types
10382
10383 if Is_Integer_Type (Typ)
10384 and then Compile_Time_Known_Value (Right_Opnd (N))
10385 and then Expr_Value (Right_Opnd (N)) = Uint_0
10386 then
10387 Rewrite (N, Left_Opnd (N));
10388 return;
10389 end if;
10390
10391 -- Arithmetic overflow checks for signed integer/fixed point types
10392
10393 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
10394 Apply_Arithmetic_Overflow_Check (N);
10395 end if;
10396
10397 -- Overflow checks for floating-point if -gnateF mode active
10398
10399 Check_Float_Op_Overflow (N);
10400
10401 Expand_Nonbinary_Modular_Op (N);
10402 end Expand_N_Op_Subtract;
10403
10404 ---------------------
10405 -- Expand_N_Op_Xor --
10406 ---------------------
10407
10408 procedure Expand_N_Op_Xor (N : Node_Id) is
10409 Typ : constant Entity_Id := Etype (N);
10410
10411 begin
10412 Binary_Op_Validity_Checks (N);
10413
10414 if Is_Array_Type (Etype (N)) then
10415 Expand_Boolean_Operator (N);
10416
10417 elsif Is_Boolean_Type (Etype (N)) then
10418 Adjust_Condition (Left_Opnd (N));
10419 Adjust_Condition (Right_Opnd (N));
10420 Set_Etype (N, Standard_Boolean);
10421 Adjust_Result_Type (N, Typ);
10422
10423 elsif Is_Intrinsic_Subprogram (Entity (N)) then
10424 Expand_Intrinsic_Call (N, Entity (N));
10425 end if;
10426
10427 Expand_Nonbinary_Modular_Op (N);
10428 end Expand_N_Op_Xor;
10429
10430 ----------------------
10431 -- Expand_N_Or_Else --
10432 ----------------------
10433
10434 procedure Expand_N_Or_Else (N : Node_Id)
10435 renames Expand_Short_Circuit_Operator;
10436
10437 -----------------------------------
10438 -- Expand_N_Qualified_Expression --
10439 -----------------------------------
10440
10441 procedure Expand_N_Qualified_Expression (N : Node_Id) is
10442 Loc : constant Source_Ptr := Sloc (N);
10443 Operand : constant Node_Id := Expression (N);
10444 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
10445
10446 begin
10447 -- Nothing to do if the operand is an identical qualified expression
10448
10449 if Nkind (Operand) = N_Qualified_Expression
10450 and then Entity (Subtype_Mark (Operand)) = Target_Type
10451 then
10452 Rewrite (N, Relocate_Node (Operand));
10453 return;
10454
10455 -- An allocator expects a qualified expression in all cases
10456
10457 elsif Nkind (Parent (N)) = N_Allocator then
10458 null;
10459
10460 -- Distribute the qualified expression into the dependent expressions
10461 -- of a delayed conditional expression. The goal is to enable further
10462 -- optimizations, for example within a return statement, by exposing
10463 -- the conditional expression.
10464
10465 elsif Nkind (Operand) = N_Case_Expression
10466 and then Expansion_Delayed (Operand)
10467 then
10468 declare
10469 New_Alts : constant List_Id := New_List;
10470 New_Case : constant Node_Id :=
10471 Make_Case_Expression (Loc,
10472 Expression => Relocate_Node (Expression (Operand)),
10473 Alternatives => New_Alts);
10474
10475 Alt : Node_Id;
10476 New_Alt : Node_Id;
10477
10478 begin
10479 Alt := First (Alternatives (Operand));
10480 while Present (Alt) loop
10481 New_Alt :=
10482 Make_Case_Expression_Alternative (Sloc (Alt),
10483 Discrete_Choices => Discrete_Choices (Alt),
10484 Expression =>
10485 Make_Qualified_Expression (Loc,
10486 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10487 Expression => Relocate_Node (Expression (Alt))));
10488 Append_To (New_Alts, New_Alt);
10489 Set_Actions (New_Alt, Actions (Alt));
10490
10491 Next (Alt);
10492 end loop;
10493
10494 Rewrite (N, New_Case);
10495 Analyze_And_Resolve (N);
10496 return;
10497 end;
10498
10499 elsif Nkind (Operand) = N_If_Expression
10500 and then Expansion_Delayed (Operand)
10501 then
10502 declare
10503 Cond : constant Node_Id := First (Expressions (Operand));
10504 Thenx : constant Node_Id := Next (Cond);
10505 Elsex : constant Node_Id := Next (Thenx);
10506 New_If : constant Node_Id :=
10507 Make_If_Expression (Loc,
10508 Expressions => New_List (
10509 Relocate_Node (Cond),
10510 Make_Qualified_Expression (Loc,
10511 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10512 Expression => Relocate_Node (Thenx)),
10513 Make_Qualified_Expression (Loc,
10514 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10515 Expression => Relocate_Node (Elsex))));
10516
10517 begin
10518 Set_Then_Actions (New_If, Then_Actions (Operand));
10519 Set_Else_Actions (New_If, Else_Actions (Operand));
10520
10521 Rewrite (N, New_If);
10522 Analyze_And_Resolve (N);
10523 return;
10524 end;
10525 end if;
10526
10527 -- Do validity check if validity checking operands
10528
10529 if Validity_Checks_On and Validity_Check_Operands then
10530 Ensure_Valid (Operand);
10531 end if;
10532
10533 Freeze_Before (Operand, Target_Type);
10534
10535 -- Apply possible constraint check
10536
10537 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
10538
10539 -- Apply possible predicate check but, for a delayed aggregate, the
10540 -- check is effectively delayed until after the aggregate is expanded
10541 -- into a series of assignments. Likewise for a conditional expression
10542 -- whose expansion has been delayed.
10543
10544 if not Is_Delayed_Aggregate (Operand)
10545 and then not Is_Delayed_Conditional_Expression (Operand)
10546 then
10547 Apply_Predicate_Check (Operand, Target_Type);
10548 end if;
10549
10550 if Do_Range_Check (Operand) then
10551 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
10552 end if;
10553 end Expand_N_Qualified_Expression;
10554
10555 ------------------------------------
10556 -- Expand_N_Quantified_Expression --
10557 ------------------------------------
10558
10559 -- We expand:
10560
10561 -- for all X in range => Cond
10562
10563 -- into:
10564
10565 -- T := True;
10566 -- for X in range loop
10567 -- if not Cond then
10568 -- T := False;
10569 -- exit;
10570 -- end if;
10571 -- end loop;
10572
10573 -- Similarly, an existentially quantified expression:
10574
10575 -- for some X in range => Cond
10576
10577 -- becomes:
10578
10579 -- T := False;
10580 -- for X in range loop
10581 -- if Cond then
10582 -- T := True;
10583 -- exit;
10584 -- end if;
10585 -- end loop;
10586
10587 -- In both cases, the iteration may be over a container in which case it is
10588 -- given by an iterator specification, not a loop parameter specification.
10589
10590 procedure Expand_N_Quantified_Expression (N : Node_Id) is
10591 Actions : constant List_Id := New_List;
10592 For_All : constant Boolean := All_Present (N);
10593 Iter_Spec : constant Node_Id := Iterator_Specification (N);
10594 Loc : constant Source_Ptr := Sloc (N);
10595 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
10596 Cond : Node_Id;
10597 Flag : Entity_Id;
10598 Scheme : Node_Id;
10599 Stmts : List_Id;
10600 Var : Entity_Id;
10601
10602 begin
10603 -- Ensure that the bound variable as well as the type of Name of the
10604 -- Iter_Spec if present are properly frozen. We must do this before
10605 -- expansion because the expression is about to be converted into a
10606 -- loop, and resulting freeze nodes may end up in the wrong place in the
10607 -- tree.
10608
10609 if Present (Iter_Spec) then
10610 Var := Defining_Identifier (Iter_Spec);
10611 else
10612 Var := Defining_Identifier (Loop_Spec);
10613 end if;
10614
10615 declare
10616 P : Node_Id := Parent (N);
10617 begin
10618 while Nkind (P) in N_Subexpr loop
10619 P := Parent (P);
10620 end loop;
10621
10622 if Present (Iter_Spec) then
10623 Freeze_Before (P, Etype (Name (Iter_Spec)));
10624 end if;
10625
10626 Freeze_Before (P, Etype (Var));
10627 end;
10628
10629 -- Create the declaration of the flag which tracks the status of the
10630 -- quantified expression. Generate:
10631
10632 -- Flag : Boolean := (True | False);
10633
10634 Flag := Make_Temporary (Loc, 'T', N);
10635
10636 Append_To (Actions,
10637 Make_Object_Declaration (Loc,
10638 Defining_Identifier => Flag,
10639 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
10640 Expression =>
10641 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
10642
10643 -- Construct the circuitry which tracks the status of the quantified
10644 -- expression. Generate:
10645
10646 -- if [not] Cond then
10647 -- Flag := (False | True);
10648 -- exit;
10649 -- end if;
10650
10651 Cond := Relocate_Node (Condition (N));
10652
10653 if For_All then
10654 Cond := Make_Op_Not (Loc, Cond);
10655 end if;
10656
10657 Stmts := New_List (
10658 Make_Implicit_If_Statement (N,
10659 Condition => Cond,
10660 Then_Statements => New_List (
10661 Make_Assignment_Statement (Loc,
10662 Name => New_Occurrence_Of (Flag, Loc),
10663 Expression =>
10664 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
10665 Make_Exit_Statement (Loc))));
10666
10667 -- Build the loop equivalent of the quantified expression
10668
10669 if Present (Iter_Spec) then
10670 Scheme :=
10671 Make_Iteration_Scheme (Loc,
10672 Iterator_Specification => Iter_Spec);
10673 else
10674 Scheme :=
10675 Make_Iteration_Scheme (Loc,
10676 Loop_Parameter_Specification => Loop_Spec);
10677 end if;
10678
10679 Append_To (Actions,
10680 Make_Loop_Statement (Loc,
10681 Iteration_Scheme => Scheme,
10682 Statements => Stmts,
10683 End_Label => Empty));
10684
10685 -- Transform the quantified expression
10686
10687 Rewrite (N,
10688 Make_Expression_With_Actions (Loc,
10689 Expression => New_Occurrence_Of (Flag, Loc),
10690 Actions => Actions));
10691 Analyze_And_Resolve (N, Standard_Boolean);
10692 end Expand_N_Quantified_Expression;
10693
10694 ---------------------------------
10695 -- Expand_N_Selected_Component --
10696 ---------------------------------
10697
10698 procedure Expand_N_Selected_Component (N : Node_Id) is
10699 Loc : constant Source_Ptr := Sloc (N);
10700 Par : constant Node_Id := Parent (N);
10701 P : constant Node_Id := Prefix (N);
10702 S : constant Node_Id := Selector_Name (N);
10703 Ptyp : constant Entity_Id := Underlying_Type (Etype (P));
10704 Disc : Entity_Id;
10705 New_N : Node_Id;
10706 Dcon : Elmt_Id;
10707 Dval : Node_Id;
10708
10709 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
10710 -- Gigi needs a temporary for prefixes that depend on a discriminant,
10711 -- unless the context of an assignment can provide size information.
10712 -- Don't we have a general routine that does this???
10713
10714 function Is_Subtype_Declaration return Boolean;
10715 -- The replacement of a discriminant reference by its value is required
10716 -- if this is part of the initialization of an temporary generated by a
10717 -- change of representation. This shows up as the construction of a
10718 -- discriminant constraint for a subtype declared at the same point as
10719 -- the entity in the prefix of the selected component. We recognize this
10720 -- case when the context of the reference is:
10721 -- subtype ST is T(Obj.D);
10722 -- where the entity for Obj comes from source, and ST has the same sloc.
10723
10724 -----------------------
10725 -- In_Left_Hand_Side --
10726 -----------------------
10727
10728 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
10729 begin
10730 return (Nkind (Parent (Comp)) = N_Assignment_Statement
10731 and then Comp = Name (Parent (Comp)))
10732 or else (Present (Parent (Comp))
10733 and then Nkind (Parent (Comp)) in N_Subexpr
10734 and then In_Left_Hand_Side (Parent (Comp)));
10735 end In_Left_Hand_Side;
10736
10737 -----------------------------
10738 -- Is_Subtype_Declaration --
10739 -----------------------------
10740
10741 function Is_Subtype_Declaration return Boolean is
10742 Par : constant Node_Id := Parent (N);
10743 begin
10744 return
10745 Nkind (Par) = N_Index_Or_Discriminant_Constraint
10746 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
10747 and then Comes_From_Source (Entity (Prefix (N)))
10748 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
10749 end Is_Subtype_Declaration;
10750
10751 -- Start of processing for Expand_N_Selected_Component
10752
10753 begin
10754 -- Deal with discriminant check required
10755
10756 if Do_Discriminant_Check (N) then
10757 if Present (Discriminant_Checking_Func
10758 (Original_Record_Component (Entity (S))))
10759 then
10760 -- Present the discriminant checking function to the backend, so
10761 -- that it can inline the call to the function.
10762
10763 Add_Inlined_Body
10764 (Discriminant_Checking_Func
10765 (Original_Record_Component (Entity (S))),
10766 N);
10767
10768 -- Now reset the flag and generate the call
10769
10770 Set_Do_Discriminant_Check (N, False);
10771 Generate_Discriminant_Check (N);
10772
10773 -- In the case of Unchecked_Union, no discriminant checking is
10774 -- actually performed.
10775
10776 else
10777 if not Is_Unchecked_Union
10778 (Implementation_Base_Type (Etype (Prefix (N))))
10779 and then not Is_Predefined_Unit (Get_Source_Unit (N))
10780 then
10781 Error_Msg_N
10782 ("sorry - unable to generate discriminant check for" &
10783 " reference to variant component &",
10784 Selector_Name (N));
10785 end if;
10786
10787 Set_Do_Discriminant_Check (N, False);
10788 end if;
10789 end if;
10790
10791 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
10792 -- function, then additional actuals must be passed.
10793
10794 if Is_Build_In_Place_Function_Call (P) then
10795 Make_Build_In_Place_Call_In_Anonymous_Context (P);
10796
10797 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
10798 -- containing build-in-place function calls whose returned object covers
10799 -- interface types.
10800
10801 elsif Present (Unqual_BIP_Iface_Function_Call (P)) then
10802 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (P);
10803 end if;
10804
10805 -- Gigi cannot handle unchecked conversions that are the prefix of a
10806 -- selected component with discriminants. This must be checked during
10807 -- expansion, because during analysis the type of the selector is not
10808 -- known at the point the prefix is analyzed. If the conversion is the
10809 -- target of an assignment, then we cannot force the evaluation.
10810
10811 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
10812 and then Has_Discriminants (Etype (N))
10813 and then not In_Left_Hand_Side (N)
10814 then
10815 Force_Evaluation (Prefix (N));
10816 end if;
10817
10818 -- Remaining processing applies only if selector is a discriminant
10819
10820 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
10821
10822 -- If the selector is a discriminant of a constrained record type,
10823 -- we may be able to rewrite the expression with the actual value
10824 -- of the discriminant, a useful optimization in some cases.
10825
10826 if Is_Record_Type (Ptyp)
10827 and then Has_Discriminants (Ptyp)
10828 and then Is_Constrained (Ptyp)
10829 then
10830 -- Do this optimization for discrete types only, and not for
10831 -- access types (access discriminants get us into trouble).
10832
10833 if not Is_Discrete_Type (Etype (N)) then
10834 null;
10835
10836 -- Don't do this on the left-hand side of an assignment statement.
10837 -- Normally one would think that references like this would not
10838 -- occur, but they do in generated code, and mean that we really
10839 -- do want to assign the discriminant.
10840
10841 elsif Nkind (Par) = N_Assignment_Statement
10842 and then Name (Par) = N
10843 then
10844 null;
10845
10846 -- Don't do this optimization for the prefix of an attribute or
10847 -- the name of an object renaming declaration since these are
10848 -- contexts where we do not want the value anyway.
10849
10850 elsif (Nkind (Par) = N_Attribute_Reference
10851 and then Prefix (Par) = N)
10852 or else Is_Renamed_Object (N)
10853 then
10854 null;
10855
10856 -- Don't do this optimization if we are within the code for a
10857 -- discriminant check, since the whole point of such a check may
10858 -- be to verify the condition on which the code below depends.
10859
10860 elsif Is_In_Discriminant_Check (N) then
10861 null;
10862
10863 -- Green light to see if we can do the optimization. There is
10864 -- still one condition that inhibits the optimization below but
10865 -- now is the time to check the particular discriminant.
10866
10867 else
10868 -- Loop through discriminants to find the matching discriminant
10869 -- constraint to see if we can copy it.
10870
10871 Disc := First_Discriminant (Ptyp);
10872 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
10873 Discr_Loop : while Present (Dcon) loop
10874 Dval := Node (Dcon);
10875
10876 -- Check if this is the matching discriminant and if the
10877 -- discriminant value is simple enough to make sense to
10878 -- copy. We don't want to copy complex expressions, and
10879 -- indeed to do so can cause trouble (before we put in
10880 -- this guard, a discriminant expression containing an
10881 -- AND THEN was copied, causing problems for coverage
10882 -- analysis tools).
10883
10884 -- However, if the reference is part of the initialization
10885 -- code generated for an object declaration, we must use
10886 -- the discriminant value from the subtype constraint,
10887 -- because the selected component may be a reference to the
10888 -- object being initialized, whose discriminant is not yet
10889 -- set. This only happens in complex cases involving changes
10890 -- of representation.
10891
10892 if Disc = Entity (Selector_Name (N))
10893 and then (Is_Entity_Name (Dval)
10894 or else Compile_Time_Known_Value (Dval)
10895 or else Is_Subtype_Declaration)
10896 then
10897 -- Here we have the matching discriminant. Check for
10898 -- the case of a discriminant of a component that is
10899 -- constrained by an outer discriminant, which cannot
10900 -- be optimized away.
10901
10902 if Denotes_Discriminant (Dval, Check_Concurrent => True)
10903 then
10904 exit Discr_Loop;
10905
10906 -- Do not retrieve value if constraint is not static. It
10907 -- is generally not useful, and the constraint may be a
10908 -- rewritten outer discriminant in which case it is in
10909 -- fact incorrect.
10910
10911 elsif Is_Entity_Name (Dval)
10912 and then
10913 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
10914 and then Present (Expression (Parent (Entity (Dval))))
10915 and then not
10916 Is_OK_Static_Expression
10917 (Expression (Parent (Entity (Dval))))
10918 then
10919 exit Discr_Loop;
10920
10921 -- In the context of a case statement, the expression may
10922 -- have the base type of the discriminant, and we need to
10923 -- preserve the constraint to avoid spurious errors on
10924 -- missing cases.
10925
10926 elsif Nkind (Parent (N)) = N_Case_Statement
10927 and then Etype (Dval) /= Etype (Disc)
10928 then
10929 Rewrite (N,
10930 Make_Qualified_Expression (Loc,
10931 Subtype_Mark =>
10932 New_Occurrence_Of (Etype (Disc), Loc),
10933 Expression =>
10934 New_Copy_Tree (Dval)));
10935 Analyze_And_Resolve (N, Etype (Disc));
10936
10937 -- In case that comes out as a static expression,
10938 -- reset it (a selected component is never static).
10939
10940 Set_Is_Static_Expression (N, False);
10941 return;
10942
10943 -- Otherwise we can just copy the constraint, but the
10944 -- result is certainly not static. In some cases the
10945 -- discriminant constraint has been analyzed in the
10946 -- context of the original subtype indication, but for
10947 -- itypes the constraint might not have been analyzed
10948 -- yet, and this must be done now.
10949
10950 else
10951 Rewrite (N, New_Copy_Tree (Dval));
10952 Analyze_And_Resolve (N);
10953 Set_Is_Static_Expression (N, False);
10954 return;
10955 end if;
10956 end if;
10957
10958 Next_Elmt (Dcon);
10959 Next_Discriminant (Disc);
10960 end loop Discr_Loop;
10961
10962 -- Note: the above loop should always find a matching
10963 -- discriminant, but if it does not, we just missed an
10964 -- optimization due to some glitch (perhaps a previous
10965 -- error), so ignore.
10966
10967 end if;
10968 end if;
10969
10970 -- The only remaining processing is in the case of a discriminant of
10971 -- a concurrent object, where we rewrite the prefix to denote the
10972 -- corresponding record type. If the type is derived and has renamed
10973 -- discriminants, use corresponding discriminant, which is the one
10974 -- that appears in the corresponding record.
10975
10976 if not Is_Concurrent_Type (Ptyp) then
10977 return;
10978 end if;
10979
10980 Disc := Entity (Selector_Name (N));
10981
10982 if Is_Derived_Type (Ptyp)
10983 and then Present (Corresponding_Discriminant (Disc))
10984 then
10985 Disc := Corresponding_Discriminant (Disc);
10986 end if;
10987
10988 New_N :=
10989 Make_Selected_Component (Loc,
10990 Prefix =>
10991 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
10992 New_Copy_Tree (P)),
10993 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
10994
10995 Rewrite (N, New_N);
10996 Analyze (N);
10997 end if;
10998
10999 -- Set Atomic_Sync_Required if necessary for atomic component
11000
11001 if Nkind (N) = N_Selected_Component then
11002 declare
11003 E : constant Entity_Id := Entity (Selector_Name (N));
11004 Set : Boolean;
11005
11006 begin
11007 -- If component is atomic, but type is not, setting depends on
11008 -- disable/enable state for the component.
11009
11010 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
11011 Set := not Atomic_Synchronization_Disabled (E);
11012
11013 -- If component is not atomic, but its type is atomic, setting
11014 -- depends on disable/enable state for the type.
11015
11016 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
11017 Set := not Atomic_Synchronization_Disabled (Etype (E));
11018
11019 -- If both component and type are atomic, we disable if either
11020 -- component or its type have sync disabled.
11021
11022 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
11023 Set := not Atomic_Synchronization_Disabled (E)
11024 and then
11025 not Atomic_Synchronization_Disabled (Etype (E));
11026
11027 else
11028 Set := False;
11029 end if;
11030
11031 -- Set flag if required
11032
11033 if Set then
11034 Activate_Atomic_Synchronization (N);
11035 end if;
11036 end;
11037 end if;
11038 end Expand_N_Selected_Component;
11039
11040 --------------------
11041 -- Expand_N_Slice --
11042 --------------------
11043
11044 procedure Expand_N_Slice (N : Node_Id) is
11045 Loc : constant Source_Ptr := Sloc (N);
11046 Typ : constant Entity_Id := Etype (N);
11047
11048 function Is_Procedure_Actual (N : Node_Id) return Boolean;
11049 -- Check whether the argument is an actual for a procedure call, in
11050 -- which case the expansion of a bit-packed slice is deferred until the
11051 -- call itself is expanded. The reason this is required is that we might
11052 -- have an IN OUT or OUT parameter, and the copy out is essential, and
11053 -- that copy out would be missed if we created a temporary here in
11054 -- Expand_N_Slice. Note that we don't bother to test specifically for an
11055 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
11056 -- is harmless to defer expansion in the IN case, since the call
11057 -- processing will still generate the appropriate copy in operation,
11058 -- which will take care of the slice.
11059
11060 procedure Make_Temporary_For_Slice;
11061 -- Create a named variable for the value of the slice, in cases where
11062 -- the back end cannot handle it properly, e.g. when packed types or
11063 -- unaligned slices are involved.
11064
11065 -------------------------
11066 -- Is_Procedure_Actual --
11067 -------------------------
11068
11069 function Is_Procedure_Actual (N : Node_Id) return Boolean is
11070 Par : Node_Id := Parent (N);
11071
11072 begin
11073 loop
11074 -- If our parent is a procedure call we can return
11075
11076 if Nkind (Par) = N_Procedure_Call_Statement then
11077 return True;
11078
11079 -- If our parent is a type conversion, keep climbing the tree,
11080 -- since a type conversion can be a procedure actual. Also keep
11081 -- climbing if parameter association or a qualified expression,
11082 -- since these are additional cases that do can appear on
11083 -- procedure actuals.
11084
11085 elsif Nkind (Par) in N_Type_Conversion
11086 | N_Parameter_Association
11087 | N_Qualified_Expression
11088 then
11089 Par := Parent (Par);
11090
11091 -- Any other case is not what we are looking for
11092
11093 else
11094 return False;
11095 end if;
11096 end loop;
11097 end Is_Procedure_Actual;
11098
11099 ------------------------------
11100 -- Make_Temporary_For_Slice --
11101 ------------------------------
11102
11103 procedure Make_Temporary_For_Slice is
11104 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
11105 Decl : Node_Id;
11106
11107 begin
11108 Decl :=
11109 Make_Object_Declaration (Loc,
11110 Defining_Identifier => Ent,
11111 Object_Definition => New_Occurrence_Of (Typ, Loc));
11112
11113 Set_No_Initialization (Decl);
11114
11115 Insert_Actions (N, New_List (
11116 Decl,
11117 Make_Assignment_Statement (Loc,
11118 Name => New_Occurrence_Of (Ent, Loc),
11119 Expression => Relocate_Node (N))));
11120
11121 Rewrite (N, New_Occurrence_Of (Ent, Loc));
11122 Analyze_And_Resolve (N, Typ);
11123 end Make_Temporary_For_Slice;
11124
11125 -- Local variables
11126
11127 Pref : constant Node_Id := Prefix (N);
11128
11129 -- Start of processing for Expand_N_Slice
11130
11131 begin
11132 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
11133 -- function, then additional actuals must be passed.
11134
11135 if Is_Build_In_Place_Function_Call (Pref) then
11136 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
11137
11138 -- Ada 2005 (AI-318-02): Specialization of the previous case for prefix
11139 -- containing build-in-place function calls whose returned object covers
11140 -- interface types.
11141
11142 elsif Present (Unqual_BIP_Iface_Function_Call (Pref)) then
11143 Make_Build_In_Place_Iface_Call_In_Anonymous_Context (Pref);
11144 end if;
11145
11146 -- The remaining case to be handled is packed slices. We can leave
11147 -- packed slices as they are in the following situations:
11148
11149 -- 1. Right or left side of an assignment (we can handle this
11150 -- situation correctly in the assignment statement expansion).
11151
11152 -- 2. Prefix of indexed component (the slide is optimized away in this
11153 -- case, see the start of Expand_N_Indexed_Component.)
11154
11155 -- 3. Object renaming declaration, since we want the name of the
11156 -- slice, not the value.
11157
11158 -- 4. Argument to procedure call, since copy-in/copy-out handling may
11159 -- be required, and this is handled in the expansion of call
11160 -- itself.
11161
11162 -- 5. Prefix of an address attribute (this is an error which is caught
11163 -- elsewhere, and the expansion would interfere with generating the
11164 -- error message) or of a size attribute (because 'Size may change
11165 -- when applied to the temporary instead of the slice directly).
11166
11167 if not Is_Packed (Typ) then
11168
11169 -- Apply transformation for actuals of a function call, where
11170 -- Expand_Actuals is not used.
11171
11172 if Nkind (Parent (N)) = N_Function_Call
11173 and then Is_Possibly_Unaligned_Slice (N)
11174 then
11175 Make_Temporary_For_Slice;
11176 end if;
11177
11178 elsif Nkind (Parent (N)) = N_Assignment_Statement
11179 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
11180 and then Parent (N) = Name (Parent (Parent (N))))
11181 then
11182 return;
11183
11184 elsif Nkind (Parent (N)) = N_Indexed_Component
11185 or else Is_Renamed_Object (N)
11186 or else Is_Procedure_Actual (N)
11187 then
11188 return;
11189
11190 elsif Nkind (Parent (N)) = N_Attribute_Reference
11191 and then (Attribute_Name (Parent (N)) = Name_Address
11192 or else Attribute_Name (Parent (N)) = Name_Size)
11193 then
11194 return;
11195
11196 else
11197 Make_Temporary_For_Slice;
11198 end if;
11199 end Expand_N_Slice;
11200
11201 ------------------------------
11202 -- Expand_N_Type_Conversion --
11203 ------------------------------
11204
11205 procedure Expand_N_Type_Conversion (N : Node_Id) is
11206 Loc : constant Source_Ptr := Sloc (N);
11207 Operand : constant Node_Id := Expression (N);
11208 Operand_Acc : Node_Id := Operand;
11209 Target_Type : Entity_Id := Etype (N);
11210 Operand_Type : Entity_Id := Etype (Operand);
11211
11212 procedure Discrete_Range_Check;
11213 -- Handles generation of range check for discrete target value
11214
11215 procedure Handle_Changed_Representation;
11216 -- This is called in the case of record and array type conversions to
11217 -- see if there is a change of representation to be handled. Change of
11218 -- representation is actually handled at the assignment statement level,
11219 -- and what this procedure does is rewrite node N conversion as an
11220 -- assignment to temporary. If there is no change of representation,
11221 -- then the conversion node is unchanged.
11222
11223 procedure Raise_Accessibility_Error;
11224 -- Called when we know that an accessibility check will fail. Rewrites
11225 -- node N to an appropriate raise statement and outputs warning msgs.
11226 -- The Etype of the raise node is set to Target_Type. Note that in this
11227 -- case the rest of the processing should be skipped (i.e. the call to
11228 -- this procedure will be followed by "goto Done").
11229
11230 procedure Real_Range_Check;
11231 -- Handles generation of range check for real target value
11232
11233 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
11234 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
11235 -- evaluates to True.
11236
11237 function Statically_Deeper_Relation_Applies (Targ_Typ : Entity_Id)
11238 return Boolean;
11239 -- Given a target type for a conversion, determine whether the
11240 -- statically deeper accessibility rules apply to it.
11241
11242 --------------------------
11243 -- Discrete_Range_Check --
11244 --------------------------
11245
11246 -- Case of conversions to a discrete type. We let Generate_Range_Check
11247 -- do the heavy lifting, after converting a fixed-point operand to an
11248 -- appropriate integer type.
11249
11250 procedure Discrete_Range_Check is
11251 Expr : Node_Id;
11252 Ityp : Entity_Id;
11253
11254 -- Start of processing for Discrete_Range_Check
11255
11256 begin
11257 -- Nothing more to do if conversion was rewritten
11258
11259 if Nkind (N) /= N_Type_Conversion then
11260 return;
11261 end if;
11262
11263 Expr := Expression (N);
11264
11265 -- Clear the Do_Range_Check flag on Expr
11266
11267 Set_Do_Range_Check (Expr, False);
11268
11269 -- Nothing to do if range checks suppressed
11270
11271 if Range_Checks_Suppressed (Target_Type) then
11272 return;
11273 end if;
11274
11275 -- Nothing to do if expression is an entity on which checks have been
11276 -- suppressed.
11277
11278 if Is_Entity_Name (Expr)
11279 and then Range_Checks_Suppressed (Entity (Expr))
11280 then
11281 return;
11282 end if;
11283
11284 -- Before we do a range check, we have to deal with treating
11285 -- a fixed-point operand as an integer. The way we do this
11286 -- is simply to do an unchecked conversion to an appropriate
11287 -- integer type with the smallest size, so that we can suppress
11288 -- trivial checks.
11289
11290 if Is_Fixed_Point_Type (Etype (Expr)) then
11291 Ityp := Small_Integer_Type_For
11292 (Esize (Base_Type (Etype (Expr))), Uns => False);
11293 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
11294 end if;
11295
11296 -- Reset overflow flag, since the range check will include
11297 -- dealing with possible overflow, and generate the check.
11298
11299 Set_Do_Overflow_Check (N, False);
11300
11301 Generate_Range_Check (Expr, Target_Type, CE_Range_Check_Failed);
11302 end Discrete_Range_Check;
11303
11304 -----------------------------------
11305 -- Handle_Changed_Representation --
11306 -----------------------------------
11307
11308 procedure Handle_Changed_Representation is
11309 Cons : List_Id;
11310 Decl : Node_Id;
11311 N_Ix : Node_Id;
11312 Odef : Node_Id;
11313 Stmt : Node_Id;
11314 Temp : Entity_Id;
11315
11316 begin
11317 -- Nothing else to do if no change of representation
11318
11319 if Has_Compatible_Representation (Target_Type, Operand_Type) then
11320 return;
11321
11322 -- The real change of representation work is done by the assignment
11323 -- statement processing. So if this type conversion is appearing as
11324 -- the expression of an assignment statement, nothing needs to be
11325 -- done to the conversion.
11326
11327 elsif Nkind (Parent (N)) = N_Assignment_Statement then
11328 return;
11329
11330 -- Otherwise we need to generate a temporary variable, and do the
11331 -- change of representation assignment into that temporary variable.
11332 -- The conversion is then replaced by a reference to this variable.
11333
11334 else
11335 Cons := No_List;
11336
11337 -- If type is unconstrained we have to add a constraint, copied
11338 -- from the actual value of the left-hand side.
11339
11340 if not Is_Constrained (Target_Type) then
11341 if Has_Discriminants (Operand_Type) then
11342
11343 -- A change of representation can only apply to untagged
11344 -- types. We need to build the constraint that applies to
11345 -- the target type, using the constraints of the operand.
11346 -- The analysis is complicated if there are both inherited
11347 -- discriminants and constrained discriminants.
11348 -- We iterate over the discriminants of the target, and
11349 -- find the discriminant of the same name:
11350
11351 -- a) If there is a corresponding discriminant in the object
11352 -- then the value is a selected component of the operand.
11353
11354 -- b) Otherwise the value of a constrained discriminant is
11355 -- found in the stored constraint of the operand.
11356
11357 declare
11358 Stored : constant Elist_Id :=
11359 Stored_Constraint (Operand_Type);
11360 -- Stored constraints of the operand. If present, they
11361 -- correspond to the discriminants of the parent type.
11362
11363 Disc_O : Entity_Id;
11364 -- Discriminant of the operand type. Its value in the
11365 -- object is captured in a selected component.
11366
11367 Disc_T : Entity_Id;
11368 -- Discriminant of the target type
11369
11370 Elmt : Elmt_Id;
11371
11372 begin
11373 Disc_O := First_Discriminant (Operand_Type);
11374 Disc_T := First_Discriminant (Target_Type);
11375 Elmt := (if Present (Stored)
11376 then First_Elmt (Stored)
11377 else No_Elmt);
11378
11379 Cons := New_List;
11380 while Present (Disc_T) loop
11381 if Present (Disc_O)
11382 and then Chars (Disc_T) = Chars (Disc_O)
11383 then
11384 Append_To (Cons,
11385 Make_Selected_Component (Loc,
11386 Prefix =>
11387 Duplicate_Subexpr_Move_Checks (Operand),
11388 Selector_Name =>
11389 Make_Identifier (Loc, Chars (Disc_O))));
11390 Next_Discriminant (Disc_O);
11391
11392 elsif Present (Elmt) then
11393 Append_To (Cons, New_Copy_Tree (Node (Elmt)));
11394 end if;
11395
11396 if Present (Elmt) then
11397 Next_Elmt (Elmt);
11398 end if;
11399
11400 Next_Discriminant (Disc_T);
11401 end loop;
11402 end;
11403
11404 elsif Is_Array_Type (Operand_Type) then
11405 N_Ix := First_Index (Target_Type);
11406 Cons := New_List;
11407
11408 for J in 1 .. Number_Dimensions (Operand_Type) loop
11409
11410 -- We convert the bounds explicitly. We use an unchecked
11411 -- conversion because bounds checks are done elsewhere.
11412
11413 Append_To (Cons,
11414 Make_Range (Loc,
11415 Low_Bound =>
11416 Unchecked_Convert_To (Etype (N_Ix),
11417 Make_Attribute_Reference (Loc,
11418 Prefix =>
11419 Duplicate_Subexpr_No_Checks
11420 (Operand, Name_Req => True),
11421 Attribute_Name => Name_First,
11422 Expressions => New_List (
11423 Make_Integer_Literal (Loc, J)))),
11424
11425 High_Bound =>
11426 Unchecked_Convert_To (Etype (N_Ix),
11427 Make_Attribute_Reference (Loc,
11428 Prefix =>
11429 Duplicate_Subexpr_No_Checks
11430 (Operand, Name_Req => True),
11431 Attribute_Name => Name_Last,
11432 Expressions => New_List (
11433 Make_Integer_Literal (Loc, J))))));
11434
11435 Next_Index (N_Ix);
11436 end loop;
11437 end if;
11438 end if;
11439
11440 Odef := New_Occurrence_Of (Target_Type, Loc);
11441
11442 if Present (Cons) then
11443 Odef :=
11444 Make_Subtype_Indication (Loc,
11445 Subtype_Mark => Odef,
11446 Constraint =>
11447 Make_Index_Or_Discriminant_Constraint (Loc,
11448 Constraints => Cons));
11449 end if;
11450
11451 Temp := Make_Temporary (Loc, 'C');
11452 Decl :=
11453 Make_Object_Declaration (Loc,
11454 Defining_Identifier => Temp,
11455 Object_Definition => Odef);
11456
11457 -- The temporary need not be initialized
11458
11459 Set_No_Initialization (Decl);
11460
11461 Stmt :=
11462 Make_Assignment_Statement (Loc,
11463 Name => New_Occurrence_Of (Temp, Loc),
11464 Expression => Relocate_Node (N));
11465
11466 -- And, therefore, cannot be finalized
11467
11468 Set_No_Finalize_Actions (Stmt);
11469
11470 -- Insert required actions. It is essential to suppress checks
11471 -- since we have suppressed default initialization, which means
11472 -- that the variable we create may have no discriminants.
11473
11474 Insert_Actions (N, New_List (Decl, Stmt), Suppress => All_Checks);
11475
11476 Rewrite (N, New_Occurrence_Of (Temp, Loc));
11477 return;
11478 end if;
11479 end Handle_Changed_Representation;
11480
11481 -------------------------------
11482 -- Raise_Accessibility_Error --
11483 -------------------------------
11484
11485 procedure Raise_Accessibility_Error is
11486 begin
11487 Error_Msg_Warn := SPARK_Mode /= On;
11488 Rewrite (N,
11489 Make_Raise_Program_Error (Sloc (N),
11490 Reason => PE_Accessibility_Check_Failed));
11491 Set_Etype (N, Target_Type);
11492
11493 Error_Msg_N ("accessibility check failure<<", N);
11494 Error_Msg_N ("\Program_Error [<<", N);
11495 end Raise_Accessibility_Error;
11496
11497 ----------------------
11498 -- Real_Range_Check --
11499 ----------------------
11500
11501 -- Case of conversions to floating-point or fixed-point. If range checks
11502 -- are enabled and the target type has a range constraint, we convert:
11503
11504 -- typ (x)
11505
11506 -- to
11507
11508 -- Tnn : typ'Base := typ'Base (x);
11509 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
11510 -- typ (Tnn)
11511
11512 -- This is necessary when there is a conversion of integer to float or
11513 -- to fixed-point to ensure that the correct checks are made. It is not
11514 -- necessary for the float-to-float case where it is enough to just set
11515 -- the Do_Range_Check flag on the expression.
11516
11517 procedure Real_Range_Check is
11518 Btyp : constant Entity_Id := Base_Type (Target_Type);
11519 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
11520 Hi : constant Node_Id := Type_High_Bound (Target_Type);
11521
11522 Conv : Node_Id;
11523 Hi_Arg : Node_Id;
11524 Hi_Val : Node_Id;
11525 Lo_Arg : Node_Id;
11526 Lo_Val : Node_Id;
11527 Expr : Entity_Id;
11528 Tnn : Entity_Id;
11529
11530 begin
11531 -- Nothing more to do if conversion was rewritten
11532
11533 if Nkind (N) /= N_Type_Conversion then
11534 return;
11535 end if;
11536
11537 Expr := Expression (N);
11538
11539 -- Clear the Do_Range_Check flag on Expr
11540
11541 Set_Do_Range_Check (Expr, False);
11542
11543 -- Nothing to do if range checks suppressed, or target has the same
11544 -- range as the base type (or is the base type).
11545
11546 if Range_Checks_Suppressed (Target_Type)
11547 or else (Lo = Type_Low_Bound (Btyp)
11548 and then
11549 Hi = Type_High_Bound (Btyp))
11550 then
11551 return;
11552 end if;
11553
11554 -- Nothing to do if expression is an entity on which checks have been
11555 -- suppressed.
11556
11557 if Is_Entity_Name (Expr)
11558 and then Range_Checks_Suppressed (Entity (Expr))
11559 then
11560 return;
11561 end if;
11562
11563 -- Nothing to do if expression was rewritten into a float-to-float
11564 -- conversion, since this kind of conversion is handled elsewhere.
11565
11566 if Is_Floating_Point_Type (Etype (Expr))
11567 and then Is_Floating_Point_Type (Target_Type)
11568 then
11569 return;
11570 end if;
11571
11572 -- Nothing to do if bounds are all static and we can tell that the
11573 -- expression is within the bounds of the target. Note that if the
11574 -- operand is of an unconstrained floating-point type, then we do
11575 -- not trust it to be in range (might be infinite)
11576
11577 declare
11578 S_Lo : constant Node_Id := Type_Low_Bound (Etype (Expr));
11579 S_Hi : constant Node_Id := Type_High_Bound (Etype (Expr));
11580
11581 begin
11582 if (not Is_Floating_Point_Type (Etype (Expr))
11583 or else Is_Constrained (Etype (Expr)))
11584 and then Compile_Time_Known_Value (S_Lo)
11585 and then Compile_Time_Known_Value (S_Hi)
11586 and then Compile_Time_Known_Value (Hi)
11587 and then Compile_Time_Known_Value (Lo)
11588 then
11589 declare
11590 D_Lov : constant Ureal := Expr_Value_R (Lo);
11591 D_Hiv : constant Ureal := Expr_Value_R (Hi);
11592 S_Lov : Ureal;
11593 S_Hiv : Ureal;
11594
11595 begin
11596 if Is_Real_Type (Etype (Expr)) then
11597 S_Lov := Expr_Value_R (S_Lo);
11598 S_Hiv := Expr_Value_R (S_Hi);
11599 else
11600 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
11601 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
11602 end if;
11603
11604 if D_Hiv > D_Lov
11605 and then S_Lov >= D_Lov
11606 and then S_Hiv <= D_Hiv
11607 then
11608 return;
11609 end if;
11610 end;
11611 end if;
11612 end;
11613
11614 -- Otherwise rewrite the conversion as described above
11615
11616 Conv := Convert_To (Btyp, Expr);
11617
11618 -- If a conversion is necessary, then copy the specific flags from
11619 -- the original one and also move the Do_Overflow_Check flag since
11620 -- this new conversion is to the base type.
11621
11622 if Nkind (Conv) = N_Type_Conversion then
11623 Set_Conversion_OK (Conv, Conversion_OK (N));
11624 Set_Float_Truncate (Conv, Float_Truncate (N));
11625 Set_Rounded_Result (Conv, Rounded_Result (N));
11626
11627 if Do_Overflow_Check (N) then
11628 Set_Do_Overflow_Check (Conv);
11629 Set_Do_Overflow_Check (N, False);
11630 end if;
11631 end if;
11632
11633 Tnn := Make_Temporary (Loc, 'T', Conv);
11634
11635 -- For a conversion from Float to Fixed where the bounds of the
11636 -- fixed-point type are static, we can obtain a more accurate
11637 -- fixed-point value by converting the result of the floating-
11638 -- point expression to an appropriate integer type, and then
11639 -- performing an unchecked conversion to the target fixed-point
11640 -- type. The range check can then use the corresponding integer
11641 -- value of the bounds instead of requiring further conversions.
11642 -- This preserves the identity:
11643
11644 -- Fix_Val = Fixed_Type (Float_Type (Fix_Val))
11645
11646 -- which used to fail when Fix_Val was a bound of the type and
11647 -- the 'Small was not a representable number.
11648 -- This transformation requires an integer type large enough to
11649 -- accommodate a fixed-point value.
11650
11651 if Is_Ordinary_Fixed_Point_Type (Target_Type)
11652 and then Is_Floating_Point_Type (Etype (Expr))
11653 and then RM_Size (Btyp) <= System_Max_Integer_Size
11654 and then Nkind (Lo) = N_Real_Literal
11655 and then Nkind (Hi) = N_Real_Literal
11656 then
11657 declare
11658 Expr_Id : constant Entity_Id := Make_Temporary (Loc, 'T', Conv);
11659 Int_Typ : constant Entity_Id :=
11660 Small_Integer_Type_For (RM_Size (Btyp), Uns => False);
11661 Trunc : constant Boolean := Float_Truncate (Conv);
11662
11663 begin
11664 Conv := Convert_To (Int_Typ, Expression (Conv));
11665 Set_Float_Truncate (Conv, Trunc);
11666
11667 -- Generate a temporary with the integer value. Required in the
11668 -- CCG compiler to ensure that run-time checks reference this
11669 -- integer expression (instead of the resulting fixed-point
11670 -- value because fixed-point values are handled by means of
11671 -- unsigned integer types).
11672
11673 Insert_Action (N,
11674 Make_Object_Declaration (Loc,
11675 Defining_Identifier => Expr_Id,
11676 Object_Definition => New_Occurrence_Of (Int_Typ, Loc),
11677 Constant_Present => True,
11678 Expression => Conv));
11679
11680 -- Create integer objects for range checking of result.
11681
11682 Lo_Arg :=
11683 Unchecked_Convert_To
11684 (Int_Typ, New_Occurrence_Of (Expr_Id, Loc));
11685
11686 Lo_Val :=
11687 Make_Integer_Literal (Loc, Corresponding_Integer_Value (Lo));
11688
11689 Hi_Arg :=
11690 Unchecked_Convert_To
11691 (Int_Typ, New_Occurrence_Of (Expr_Id, Loc));
11692
11693 Hi_Val :=
11694 Make_Integer_Literal (Loc, Corresponding_Integer_Value (Hi));
11695
11696 -- Rewrite conversion as an integer conversion of the
11697 -- original floating-point expression, followed by an
11698 -- unchecked conversion to the target fixed-point type.
11699
11700 Conv :=
11701 Unchecked_Convert_To
11702 (Target_Type, New_Occurrence_Of (Expr_Id, Loc));
11703 end;
11704
11705 -- All other conversions
11706
11707 else
11708 Lo_Arg := New_Occurrence_Of (Tnn, Loc);
11709 Lo_Val :=
11710 Make_Attribute_Reference (Loc,
11711 Prefix => New_Occurrence_Of (Target_Type, Loc),
11712 Attribute_Name => Name_First);
11713
11714 Hi_Arg := New_Occurrence_Of (Tnn, Loc);
11715 Hi_Val :=
11716 Make_Attribute_Reference (Loc,
11717 Prefix => New_Occurrence_Of (Target_Type, Loc),
11718 Attribute_Name => Name_Last);
11719 end if;
11720
11721 -- Build code for range checking. Note that checks are suppressed
11722 -- here since we don't want a recursive range check popping up.
11723
11724 Insert_Actions (N, New_List (
11725 Make_Object_Declaration (Loc,
11726 Defining_Identifier => Tnn,
11727 Object_Definition => New_Occurrence_Of (Btyp, Loc),
11728 Constant_Present => True,
11729 Expression => Conv),
11730
11731 Make_Raise_Constraint_Error (Loc,
11732 Condition =>
11733 Make_Or_Else (Loc,
11734 Left_Opnd =>
11735 Make_Op_Lt (Loc,
11736 Left_Opnd => Lo_Arg,
11737 Right_Opnd => Lo_Val),
11738
11739 Right_Opnd =>
11740 Make_Op_Gt (Loc,
11741 Left_Opnd => Hi_Arg,
11742 Right_Opnd => Hi_Val)),
11743 Reason => CE_Range_Check_Failed)),
11744 Suppress => All_Checks);
11745
11746 Rewrite (Expr, New_Occurrence_Of (Tnn, Loc));
11747 end Real_Range_Check;
11748
11749 -----------------------------
11750 -- Has_Extra_Accessibility --
11751 -----------------------------
11752
11753 -- Returns true for a formal of an anonymous access type or for an Ada
11754 -- 2012-style stand-alone object of an anonymous access type.
11755
11756 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
11757 begin
11758 if Is_Formal (Id) or else Ekind (Id) in E_Constant | E_Variable then
11759 return Present (Effective_Extra_Accessibility (Id));
11760 else
11761 return False;
11762 end if;
11763 end Has_Extra_Accessibility;
11764
11765 ----------------------------------------
11766 -- Statically_Deeper_Relation_Applies --
11767 ----------------------------------------
11768
11769 function Statically_Deeper_Relation_Applies (Targ_Typ : Entity_Id)
11770 return Boolean
11771 is
11772 begin
11773 -- The case where the target type is an anonymous access type is
11774 -- ignored since they have different semantics and get covered by
11775 -- various runtime checks depending on context.
11776
11777 -- Note, the current implementation of this predicate is incomplete
11778 -- and doesn't fully reflect the rules given in RM 3.10.2 (19) and
11779 -- (19.1) ???
11780
11781 return Ekind (Targ_Typ) /= E_Anonymous_Access_Type;
11782 end Statically_Deeper_Relation_Applies;
11783
11784 -- Start of processing for Expand_N_Type_Conversion
11785
11786 begin
11787 -- First remove check marks put by the semantic analysis on the type
11788 -- conversion between array types. We need these checks, and they will
11789 -- be generated by this expansion routine, but we do not depend on these
11790 -- flags being set, and since we do intend to expand the checks in the
11791 -- front end, we don't want them on the tree passed to the back end.
11792
11793 if Is_Array_Type (Target_Type) then
11794 if Is_Constrained (Target_Type) then
11795 Set_Do_Length_Check (N, False);
11796 else
11797 Set_Do_Range_Check (Operand, False);
11798 end if;
11799 end if;
11800
11801 -- Nothing at all to do if conversion is to the identical type so remove
11802 -- the conversion completely, it is useless, except that it may carry
11803 -- an Assignment_OK attribute, which must be propagated to the operand
11804 -- and the Do_Range_Check flag on the operand must be cleared, if any.
11805
11806 if Operand_Type = Target_Type then
11807 if Assignment_OK (N) then
11808 Set_Assignment_OK (Operand);
11809 end if;
11810
11811 Set_Do_Range_Check (Operand, False);
11812
11813 Rewrite (N, Relocate_Node (Operand));
11814
11815 goto Done;
11816 end if;
11817
11818 -- Nothing to do if this is the second argument of read. This is a
11819 -- "backwards" conversion that will be handled by the specialized code
11820 -- in attribute processing.
11821
11822 if Nkind (Parent (N)) = N_Attribute_Reference
11823 and then Attribute_Name (Parent (N)) = Name_Read
11824 and then Next (First (Expressions (Parent (N)))) = N
11825 then
11826 goto Done;
11827 end if;
11828
11829 -- Check for case of converting to a type that has an invariant
11830 -- associated with it. This requires an invariant check. We insert
11831 -- a call:
11832
11833 -- invariant_check (typ (expr))
11834
11835 -- in the code, after removing side effects from the expression.
11836 -- This is clearer than replacing the conversion into an expression
11837 -- with actions, because the context may impose additional actions
11838 -- (tag checks, membership tests, etc.) that conflict with this
11839 -- rewriting (used previously).
11840
11841 -- Note: the Comes_From_Source check, and then the resetting of this
11842 -- flag prevents what would otherwise be an infinite recursion.
11843
11844 if Has_Invariants (Target_Type)
11845 and then Present (Invariant_Procedure (Target_Type))
11846 and then Comes_From_Source (N)
11847 then
11848 Set_Comes_From_Source (N, False);
11849 Remove_Side_Effects (N);
11850 Insert_Action (N, Make_Invariant_Call (Duplicate_Subexpr (N)));
11851 goto Done;
11852
11853 -- AI12-0042: For a view conversion to a class-wide type occurring
11854 -- within the immediate scope of T, from a specific type that is
11855 -- a descendant of T (including T itself), an invariant check is
11856 -- performed on the part of the object that is of type T. (We don't
11857 -- need to explicitly check for the operand type being a descendant,
11858 -- just that it's a specific type, because the conversion would be
11859 -- illegal if it's specific and not a descendant -- downward conversion
11860 -- is not allowed).
11861
11862 elsif Is_Class_Wide_Type (Target_Type)
11863 and then not Is_Class_Wide_Type (Etype (Expression (N)))
11864 and then Present (Invariant_Procedure (Root_Type (Target_Type)))
11865 and then Comes_From_Source (N)
11866 and then Within_Scope (Find_Enclosing_Scope (N), Scope (Target_Type))
11867 then
11868 Remove_Side_Effects (N);
11869
11870 -- Perform the invariant check on a conversion to the class-wide
11871 -- type's root type.
11872
11873 declare
11874 Root_Conv : constant Node_Id :=
11875 Make_Type_Conversion (Loc,
11876 Subtype_Mark =>
11877 New_Occurrence_Of (Root_Type (Target_Type), Loc),
11878 Expression => Duplicate_Subexpr (Expression (N)));
11879 begin
11880 Set_Etype (Root_Conv, Root_Type (Target_Type));
11881
11882 Insert_Action (N, Make_Invariant_Call (Root_Conv));
11883 goto Done;
11884 end;
11885 end if;
11886
11887 -- Here if we may need to expand conversion
11888
11889 -- If the operand of the type conversion is an arithmetic operation on
11890 -- signed integers, and the based type of the signed integer type in
11891 -- question is smaller than Standard.Integer, we promote both of the
11892 -- operands to type Integer.
11893
11894 -- For example, if we have
11895
11896 -- target-type (opnd1 + opnd2)
11897
11898 -- and opnd1 and opnd2 are of type short integer, then we rewrite
11899 -- this as:
11900
11901 -- target-type (integer(opnd1) + integer(opnd2))
11902
11903 -- We do this because we are always allowed to compute in a larger type
11904 -- if we do the right thing with the result, and in this case we are
11905 -- going to do a conversion which will do an appropriate check to make
11906 -- sure that things are in range of the target type in any case. This
11907 -- avoids some unnecessary intermediate overflows.
11908
11909 -- We might consider a similar transformation in the case where the
11910 -- target is a real type or a 64-bit integer type, and the operand
11911 -- is an arithmetic operation using a 32-bit integer type. However,
11912 -- we do not bother with this case, because it could cause significant
11913 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
11914 -- much cheaper, but we don't want different behavior on 32-bit and
11915 -- 64-bit machines. Note that the exclusion of the 64-bit case also
11916 -- handles the configurable run-time cases where 64-bit arithmetic
11917 -- may simply be unavailable.
11918
11919 -- Note: this circuit is partially redundant with respect to the circuit
11920 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
11921 -- the processing here. Also we still need the Checks circuit, since we
11922 -- have to be sure not to generate junk overflow checks in the first
11923 -- place, since it would be tricky to remove them here.
11924
11925 if Integer_Promotion_Possible (N) then
11926
11927 -- All conditions met, go ahead with transformation
11928
11929 declare
11930 Opnd : Node_Id;
11931 L, R : Node_Id;
11932
11933 begin
11934 Opnd := New_Op_Node (Nkind (Operand), Loc);
11935
11936 R := Convert_To (Standard_Integer, Right_Opnd (Operand));
11937 Set_Right_Opnd (Opnd, R);
11938
11939 if Nkind (Operand) in N_Binary_Op then
11940 L := Convert_To (Standard_Integer, Left_Opnd (Operand));
11941 Set_Left_Opnd (Opnd, L);
11942 end if;
11943
11944 Rewrite (N,
11945 Make_Type_Conversion (Loc,
11946 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
11947 Expression => Opnd));
11948
11949 Analyze_And_Resolve (N, Target_Type);
11950 goto Done;
11951 end;
11952 end if;
11953
11954 -- If the conversion is from Universal_Integer and requires an overflow
11955 -- check, try to do an intermediate conversion to a narrower type first
11956 -- without overflow check, in order to avoid doing the overflow check
11957 -- in Universal_Integer, which can be a very large type.
11958
11959 if Operand_Type = Universal_Integer and then Do_Overflow_Check (N) then
11960 declare
11961 Lo, Hi, Siz : Uint;
11962 OK : Boolean;
11963 Typ : Entity_Id;
11964
11965 begin
11966 Determine_Range (Operand, OK, Lo, Hi, Assume_Valid => True);
11967
11968 if OK then
11969 Siz := Get_Size_For_Range (Lo, Hi);
11970
11971 -- We use the base type instead of the first subtype because
11972 -- overflow checks are done in the base type, so this avoids
11973 -- the need for useless conversions.
11974
11975 if Siz < System_Max_Integer_Size then
11976 Typ := Etype (Integer_Type_For (Siz, Uns => False));
11977
11978 Convert_To_And_Rewrite (Typ, Operand);
11979 Analyze_And_Resolve
11980 (Operand, Typ, Suppress => Overflow_Check);
11981
11982 Analyze_And_Resolve (N, Target_Type);
11983 goto Done;
11984 end if;
11985 end if;
11986 end;
11987 end if;
11988
11989 -- Do validity check if validity checking operands
11990
11991 if Validity_Checks_On and Validity_Check_Operands then
11992 Ensure_Valid (Operand);
11993 end if;
11994
11995 -- Special case of converting from non-standard boolean type
11996
11997 if Is_Boolean_Type (Operand_Type)
11998 and then Nonzero_Is_True (Operand_Type)
11999 then
12000 Adjust_Condition (Operand);
12001 Set_Etype (Operand, Standard_Boolean);
12002 Operand_Type := Standard_Boolean;
12003 end if;
12004
12005 -- Case of converting to an access type
12006
12007 if Is_Access_Type (Target_Type) then
12008 -- In terms of accessibility rules, an anonymous access discriminant
12009 -- is not considered separate from its parent object.
12010
12011 if Nkind (Operand) = N_Selected_Component
12012 and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant
12013 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
12014 then
12015 Operand_Acc := Original_Node (Prefix (Operand));
12016 end if;
12017
12018 -- If this type conversion was internally generated by the front end
12019 -- to displace the pointer to the object to reference an interface
12020 -- type and the original node was an Unrestricted_Access attribute,
12021 -- then skip applying accessibility checks (because, according to the
12022 -- GNAT Reference Manual, this attribute is similar to 'Access except
12023 -- that all accessibility and aliased view checks are omitted).
12024
12025 if not Comes_From_Source (N)
12026 and then Is_Interface (Designated_Type (Target_Type))
12027 and then Nkind (Original_Node (N)) = N_Attribute_Reference
12028 and then Attribute_Name (Original_Node (N)) =
12029 Name_Unrestricted_Access
12030 then
12031 null;
12032
12033 -- Apply an accessibility check when the conversion operand is an
12034 -- access parameter (or a renaming thereof), unless conversion was
12035 -- expanded from an Unchecked_ or Unrestricted_Access attribute,
12036 -- or for the actual of a class-wide interface parameter. Note that
12037 -- other checks may still need to be applied below (such as tagged
12038 -- type checks).
12039
12040 elsif Is_Entity_Name (Operand_Acc)
12041 and then Has_Extra_Accessibility (Entity (Operand_Acc))
12042 and then Ekind (Etype (Operand_Acc)) = E_Anonymous_Access_Type
12043 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
12044 or else Attribute_Name (Original_Node (N)) = Name_Access)
12045 and then not No_Dynamic_Accessibility_Checks_Enabled (N)
12046 then
12047 if not Comes_From_Source (N)
12048 and then Nkind (Parent (N)) in N_Function_Call
12049 | N_Parameter_Association
12050 | N_Procedure_Call_Statement
12051 and then Is_Interface (Designated_Type (Target_Type))
12052 and then Is_Class_Wide_Type (Designated_Type (Target_Type))
12053 then
12054 null;
12055
12056 else
12057 Apply_Accessibility_Check
12058 (Operand, Target_Type, Insert_Node => Operand);
12059 end if;
12060
12061 -- If the level of the operand type is statically deeper than the
12062 -- level of the target type, then force Program_Error. Note that this
12063 -- can only occur for cases where the attribute is within the body of
12064 -- an instantiation, otherwise the conversion will already have been
12065 -- rejected as illegal.
12066
12067 -- Note: warnings are issued by the analyzer for the instance cases,
12068 -- and, since we are late in expansion, a check is performed to
12069 -- verify that neither the target type nor the operand type are
12070 -- internally generated - as this can lead to spurious errors when,
12071 -- for example, the operand type is a result of BIP expansion.
12072
12073 elsif In_Instance_Body
12074 and then Statically_Deeper_Relation_Applies (Target_Type)
12075 and then not Is_Internal (Target_Type)
12076 and then not Is_Internal (Operand_Type)
12077 and then
12078 Type_Access_Level (Operand_Type) > Type_Access_Level (Target_Type)
12079 then
12080 Raise_Accessibility_Error;
12081 goto Done;
12082
12083 -- When the operand is a selected access discriminant the check needs
12084 -- to be made against the level of the object denoted by the prefix
12085 -- of the selected name. Force Program_Error for this case as well
12086 -- (this accessibility violation can only happen if within the body
12087 -- of an instantiation).
12088
12089 elsif In_Instance_Body
12090 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
12091 and then Nkind (Operand) = N_Selected_Component
12092 and then Ekind (Entity (Selector_Name (Operand))) = E_Discriminant
12093 and then Static_Accessibility_Level (Operand, Zero_On_Dynamic_Level)
12094 > Type_Access_Level (Target_Type)
12095 then
12096 Raise_Accessibility_Error;
12097 goto Done;
12098 end if;
12099 end if;
12100
12101 -- Generate a tag check for view conversions of mutably tagged objects,
12102 -- which are special in nature and require selecting the tag component
12103 -- from the class-wide equivalent type.
12104
12105 -- Possibly this could be combined with the logic below for better code
12106 -- reuse ???
12107
12108 if Is_View_Conversion (N)
12109 and then Is_Variable (Operand)
12110 and then Is_Class_Wide_Equivalent_Type (Etype (Operand))
12111 then
12112 -- Generate:
12113 -- [Constraint_Error when Operand.Tag /= Root_Type]
12114
12115 Insert_Action (N,
12116 Make_Raise_Constraint_Error (Loc,
12117 Condition =>
12118 Make_Op_Ne (Loc,
12119 Left_Opnd =>
12120 Make_Selected_Component (Loc,
12121 Prefix => Duplicate_Subexpr_No_Checks (Operand),
12122 Selector_Name => Make_Identifier (Loc, Name_uTag)),
12123 Right_Opnd =>
12124 Make_Attribute_Reference (Loc,
12125 Prefix => New_Occurrence_Of (Target_Type, Loc),
12126 Attribute_Name => Name_Tag)),
12127 Reason => CE_Tag_Check_Failed));
12128
12129 -- Case of conversions of tagged types and access to tagged types
12130
12131 -- When needed, that is to say when the expression is class-wide, Add
12132 -- runtime a tag check for (strict) downward conversion by using the
12133 -- membership test, generating:
12134
12135 -- [constraint_error when Operand not in Target_Type'Class]
12136
12137 -- or in the access type case
12138
12139 -- [constraint_error
12140 -- when Operand /= null
12141 -- and then Operand.all not in
12142 -- Designated_Type (Target_Type)'Class]
12143
12144 elsif (Is_Access_Type (Target_Type)
12145 and then Is_Tagged_Type (Designated_Type (Target_Type)))
12146 or else Is_Tagged_Type (Target_Type)
12147 then
12148 -- Do not do any expansion in the access type case if the parent is a
12149 -- renaming, since this is an error situation which will be caught by
12150 -- Sem_Ch8, and the expansion can interfere with this error check.
12151
12152 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
12153 goto Done;
12154 end if;
12155
12156 -- Otherwise, proceed with processing tagged conversion
12157
12158 Tagged_Conversion : declare
12159 Actual_Op_Typ : Entity_Id;
12160 Actual_Targ_Typ : Entity_Id;
12161 Root_Op_Typ : Entity_Id;
12162
12163 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
12164 -- Create a membership check to test whether Operand is a member
12165 -- of Targ_Typ. If the original Target_Type is an access, include
12166 -- a test for null value. The check is inserted at N.
12167
12168 --------------------
12169 -- Make_Tag_Check --
12170 --------------------
12171
12172 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
12173 Cond : Node_Id;
12174
12175 begin
12176 -- Generate:
12177 -- [Constraint_Error
12178 -- when Operand /= null
12179 -- and then Operand.all not in Targ_Typ]
12180
12181 if Is_Access_Type (Target_Type) then
12182 Cond :=
12183 Make_And_Then (Loc,
12184 Left_Opnd =>
12185 Make_Op_Ne (Loc,
12186 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
12187 Right_Opnd => Make_Null (Loc)),
12188
12189 Right_Opnd =>
12190 Make_Not_In (Loc,
12191 Left_Opnd =>
12192 Make_Explicit_Dereference (Loc,
12193 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
12194 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
12195
12196 -- Generate:
12197 -- [Constraint_Error when Operand not in Targ_Typ]
12198
12199 else
12200 Cond :=
12201 Make_Not_In (Loc,
12202 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
12203 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
12204 end if;
12205
12206 Insert_Action (N,
12207 Make_Raise_Constraint_Error (Loc,
12208 Condition => Cond,
12209 Reason => CE_Tag_Check_Failed),
12210 Suppress => All_Checks);
12211 end Make_Tag_Check;
12212
12213 -- Start of processing for Tagged_Conversion
12214
12215 begin
12216 -- Handle entities from the limited view
12217
12218 if Is_Access_Type (Operand_Type) then
12219 Actual_Op_Typ :=
12220 Available_View (Designated_Type (Operand_Type));
12221 else
12222 Actual_Op_Typ := Operand_Type;
12223 end if;
12224
12225 if Is_Access_Type (Target_Type) then
12226 Actual_Targ_Typ :=
12227 Available_View (Designated_Type (Target_Type));
12228 else
12229 Actual_Targ_Typ := Target_Type;
12230 end if;
12231
12232 Root_Op_Typ := Root_Type (Actual_Op_Typ);
12233
12234 -- Ada 2005 (AI-251): Handle interface type conversion
12235
12236 if Is_Interface (Actual_Op_Typ)
12237 or else
12238 Is_Interface (Actual_Targ_Typ)
12239 then
12240 Expand_Interface_Conversion (N);
12241 goto Done;
12242 end if;
12243
12244 -- Create a runtime tag check for a downward CW type conversion
12245
12246 if Is_Class_Wide_Type (Actual_Op_Typ)
12247 and then Actual_Op_Typ /= Actual_Targ_Typ
12248 and then Root_Op_Typ /= Actual_Targ_Typ
12249 and then Is_Ancestor
12250 (Root_Op_Typ, Actual_Targ_Typ, Use_Full_View => True)
12251 and then not Tag_Checks_Suppressed (Actual_Targ_Typ)
12252 then
12253 declare
12254 Conv : Node_Id;
12255 begin
12256 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
12257 Conv := Unchecked_Convert_To (Target_Type, Expression (N));
12258 Rewrite (N, Conv);
12259 Analyze_And_Resolve (N, Target_Type);
12260 end;
12261 end if;
12262 end Tagged_Conversion;
12263
12264 -- Case of other access type conversions
12265
12266 elsif Is_Access_Type (Target_Type) then
12267 Apply_Constraint_Check (Operand, Target_Type);
12268
12269 -- Case of conversions from a fixed-point type
12270
12271 -- These conversions require special expansion and processing, found in
12272 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
12273 -- since from a semantic point of view, these are simple integer
12274 -- conversions, which do not need further processing except for the
12275 -- generation of range checks, which is performed at the end of this
12276 -- procedure.
12277
12278 elsif Is_Fixed_Point_Type (Operand_Type)
12279 and then not Conversion_OK (N)
12280 then
12281 -- We should never see universal fixed at this case, since the
12282 -- expansion of the constituent divide or multiply should have
12283 -- eliminated the explicit mention of universal fixed.
12284
12285 pragma Assert (Operand_Type /= Universal_Fixed);
12286
12287 -- Check for special case of the conversion to universal real that
12288 -- occurs as a result of the use of a round attribute. In this case,
12289 -- the real type for the conversion is taken from the target type of
12290 -- the Round attribute and the result must be marked as rounded.
12291
12292 if Target_Type = Universal_Real
12293 and then Nkind (Parent (N)) = N_Attribute_Reference
12294 and then Attribute_Name (Parent (N)) = Name_Round
12295 then
12296 Set_Etype (N, Etype (Parent (N)));
12297 Target_Type := Etype (N);
12298 Set_Rounded_Result (N);
12299 end if;
12300
12301 if Is_Fixed_Point_Type (Target_Type) then
12302 Expand_Convert_Fixed_To_Fixed (N);
12303 elsif Is_Integer_Type (Target_Type) then
12304 Expand_Convert_Fixed_To_Integer (N);
12305 else
12306 pragma Assert (Is_Floating_Point_Type (Target_Type));
12307 Expand_Convert_Fixed_To_Float (N);
12308 end if;
12309
12310 -- Case of conversions to a fixed-point type
12311
12312 -- These conversions require special expansion and processing, found in
12313 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
12314 -- since from a semantic point of view, these are simple integer
12315 -- conversions, which do not need further processing.
12316
12317 elsif Is_Fixed_Point_Type (Target_Type)
12318 and then not Conversion_OK (N)
12319 then
12320 if Is_Integer_Type (Operand_Type) then
12321 Expand_Convert_Integer_To_Fixed (N);
12322 else
12323 pragma Assert (Is_Floating_Point_Type (Operand_Type));
12324 Expand_Convert_Float_To_Fixed (N);
12325 end if;
12326
12327 -- Case of array conversions
12328
12329 -- Expansion of array conversions, add required length/range checks but
12330 -- only do this if there is no change of representation. For handling of
12331 -- this case, see Handle_Changed_Representation.
12332
12333 elsif Is_Array_Type (Target_Type) then
12334 if Is_Constrained (Target_Type) then
12335 Apply_Length_Check (Operand, Target_Type);
12336 else
12337 -- If the object has an unconstrained array subtype with fixed
12338 -- lower bound, then sliding to that bound may be needed.
12339
12340 if Is_Fixed_Lower_Bound_Array_Subtype (Target_Type) then
12341 Expand_Sliding_Conversion (Operand, Target_Type);
12342 end if;
12343
12344 Apply_Range_Check (Operand, Target_Type);
12345 end if;
12346
12347 Handle_Changed_Representation;
12348
12349 -- Case of conversions of discriminated types
12350
12351 -- Add required discriminant checks if target is constrained. Again this
12352 -- change is skipped if we have a change of representation.
12353
12354 elsif Has_Discriminants (Target_Type)
12355 and then Is_Constrained (Target_Type)
12356 then
12357 Apply_Discriminant_Check (Operand, Target_Type);
12358 Handle_Changed_Representation;
12359
12360 -- Case of all other record conversions. The only processing required
12361 -- is to check for a change of representation requiring the special
12362 -- assignment processing.
12363
12364 elsif Is_Record_Type (Target_Type) then
12365
12366 -- Ada 2005 (AI-216): Program_Error is raised when converting from
12367 -- a derived Unchecked_Union type to an unconstrained type that is
12368 -- not Unchecked_Union if the operand lacks inferable discriminants.
12369
12370 if Is_Derived_Type (Operand_Type)
12371 and then Is_Unchecked_Union (Base_Type (Operand_Type))
12372 and then not Is_Constrained (Target_Type)
12373 and then not Is_Unchecked_Union (Base_Type (Target_Type))
12374 and then not Has_Inferable_Discriminants (Operand)
12375 then
12376 -- To prevent Gigi from generating illegal code, we generate a
12377 -- Program_Error node, but we give it the target type of the
12378 -- conversion (is this requirement documented somewhere ???)
12379
12380 declare
12381 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
12382 Reason => PE_Unchecked_Union_Restriction);
12383
12384 begin
12385 Set_Etype (PE, Target_Type);
12386 Rewrite (N, PE);
12387
12388 end;
12389 else
12390 Handle_Changed_Representation;
12391 end if;
12392
12393 -- Case of conversions of enumeration types
12394
12395 elsif Is_Enumeration_Type (Target_Type) then
12396
12397 -- Special processing is required if there is a change of
12398 -- representation (from enumeration representation clauses).
12399
12400 if not Has_Compatible_Representation (Target_Type, Operand_Type)
12401 and then not Conversion_OK (N)
12402 then
12403 if Optimization_Level > 0
12404 and then Is_Boolean_Type (Target_Type)
12405 then
12406 -- Convert x(y) to (if y then x'(True) else x'(False)).
12407 -- Use literals, instead of indexing x'val, to enable
12408 -- further optimizations in the middle-end.
12409
12410 Rewrite (N,
12411 Make_If_Expression (Loc,
12412 Expressions => New_List (
12413 Operand,
12414 Convert_To (Target_Type,
12415 New_Occurrence_Of (Standard_True, Loc)),
12416 Convert_To (Target_Type,
12417 New_Occurrence_Of (Standard_False, Loc)))));
12418
12419 else
12420 -- Convert: x(y) to x'val (ytyp'pos (y))
12421
12422 Rewrite (N,
12423 Make_Attribute_Reference (Loc,
12424 Prefix => New_Occurrence_Of (Target_Type, Loc),
12425 Attribute_Name => Name_Val,
12426 Expressions => New_List (
12427 Make_Attribute_Reference (Loc,
12428 Prefix => New_Occurrence_Of (Operand_Type, Loc),
12429 Attribute_Name => Name_Pos,
12430 Expressions => New_List (Operand)))));
12431 end if;
12432
12433 Analyze_And_Resolve (N, Target_Type);
12434 end if;
12435 end if;
12436
12437 -- At this stage, either the conversion node has been transformed into
12438 -- some other equivalent expression, or left as a conversion that can be
12439 -- handled by Gigi.
12440
12441 -- The only remaining step is to generate a range check if we still have
12442 -- a type conversion at this stage and Do_Range_Check is set. Note that
12443 -- we need to deal with at most 8 out of the 9 possible cases of numeric
12444 -- conversions here, because the float-to-integer case is entirely dealt
12445 -- with by Apply_Float_Conversion_Check.
12446
12447 if Nkind (N) = N_Type_Conversion
12448 and then Do_Range_Check (Expression (N))
12449 then
12450 -- Float-to-float conversions
12451
12452 if Is_Floating_Point_Type (Target_Type)
12453 and then Is_Floating_Point_Type (Etype (Expression (N)))
12454 then
12455 -- Reset overflow flag, since the range check will include
12456 -- dealing with possible overflow, and generate the check.
12457
12458 Set_Do_Overflow_Check (N, False);
12459
12460 Generate_Range_Check
12461 (Expression (N), Target_Type, CE_Range_Check_Failed);
12462
12463 -- Discrete-to-discrete conversions or fixed-point-to-discrete
12464 -- conversions when Conversion_OK is set.
12465
12466 elsif Is_Discrete_Type (Target_Type)
12467 and then (Is_Discrete_Type (Etype (Expression (N)))
12468 or else (Is_Fixed_Point_Type (Etype (Expression (N)))
12469 and then Conversion_OK (N)))
12470 then
12471 -- If Address is either a source type or target type,
12472 -- suppress range check to avoid typing anomalies when
12473 -- it is a visible integer type.
12474
12475 if Is_Descendant_Of_Address (Etype (Expression (N)))
12476 or else Is_Descendant_Of_Address (Target_Type)
12477 then
12478 Set_Do_Range_Check (Expression (N), False);
12479 else
12480 Discrete_Range_Check;
12481 end if;
12482
12483 -- Conversions to floating- or fixed-point when Conversion_OK is set
12484
12485 elsif Is_Floating_Point_Type (Target_Type)
12486 or else (Is_Fixed_Point_Type (Target_Type)
12487 and then Conversion_OK (N))
12488 then
12489 Real_Range_Check;
12490 end if;
12491
12492 pragma Assert (not Do_Range_Check (Expression (N)));
12493 end if;
12494
12495 -- Here at end of processing
12496
12497 <<Done>>
12498 -- Apply predicate check if required. Note that we can't just call
12499 -- Apply_Predicate_Check here, because the type looks right after
12500 -- the conversion and it would omit the check. The Comes_From_Source
12501 -- guard is necessary to prevent infinite recursions when we generate
12502 -- internal conversions for the purpose of checking predicates.
12503
12504 -- A view conversion of a tagged object is an object and can appear
12505 -- in an assignment context, in which case no predicate check applies
12506 -- to the now-dead value.
12507
12508 if Nkind (Parent (N)) = N_Assignment_Statement
12509 and then N = Name (Parent (N))
12510 then
12511 null;
12512
12513 elsif Predicate_Enabled (Target_Type)
12514 and then Target_Type /= Operand_Type
12515 and then Comes_From_Source (N)
12516 then
12517 declare
12518 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
12519
12520 begin
12521 -- Avoid infinite recursion on the subsequent expansion of the
12522 -- copy of the original type conversion. When needed, a range
12523 -- check has already been applied to the expression.
12524
12525 Set_Comes_From_Source (New_Expr, False);
12526 Insert_Action (N,
12527 Make_Predicate_Check (Target_Type, New_Expr),
12528 Suppress => Range_Check);
12529 end;
12530 end if;
12531 end Expand_N_Type_Conversion;
12532
12533 ----------------------------------------
12534 -- Expand_N_Unchecked_Type_Conversion --
12535 ----------------------------------------
12536
12537 -- If this cannot be handled by Gigi and we haven't already made a
12538 -- temporary for it, do it now.
12539
12540 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
12541 Target_Type : constant Entity_Id := Etype (N);
12542 Operand : constant Node_Id := Expression (N);
12543 Operand_Type : constant Entity_Id := Etype (Operand);
12544
12545 begin
12546 -- Nothing at all to do if conversion is to the identical type so remove
12547 -- the conversion completely, it is useless, except that it may carry
12548 -- an Assignment_OK indication which must be propagated to the operand.
12549
12550 if Operand_Type = Target_Type then
12551 if Assignment_OK (N) then
12552 Set_Assignment_OK (Operand);
12553 end if;
12554
12555 Rewrite (N, Operand);
12556 return;
12557 end if;
12558
12559 -- Nothing to do if conversion is safe
12560
12561 if Safe_Unchecked_Type_Conversion (N) then
12562 return;
12563 end if;
12564
12565 if Assignment_OK (N) then
12566 null;
12567 else
12568 Force_Evaluation (N);
12569 end if;
12570 end Expand_N_Unchecked_Type_Conversion;
12571
12572 ----------------------------
12573 -- Expand_Record_Equality --
12574 ----------------------------
12575
12576 -- For non-variant records, Equality is expanded when needed into:
12577
12578 -- and then Lhs.Discr1 = Rhs.Discr1
12579 -- and then ...
12580 -- and then Lhs.Discrn = Rhs.Discrn
12581 -- and then Lhs.Cmp1 = Rhs.Cmp1
12582 -- and then ...
12583 -- and then Lhs.Cmpn = Rhs.Cmpn
12584
12585 -- The expression is folded by the back end for adjacent fields. This
12586 -- function is called for tagged record in only one occasion: for imple-
12587 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
12588 -- otherwise the primitive "=" is used directly.
12589
12590 function Expand_Record_Equality
12591 (Nod : Node_Id;
12592 Typ : Entity_Id;
12593 Lhs : Node_Id;
12594 Rhs : Node_Id) return Node_Id
12595 is
12596 Loc : constant Source_Ptr := Sloc (Nod);
12597
12598 Result : Node_Id;
12599 C : Entity_Id;
12600
12601 First_Time : Boolean := True;
12602
12603 function Element_To_Compare (C : Entity_Id) return Entity_Id;
12604 -- Return the next discriminant or component to compare, starting with
12605 -- C, skipping inherited components.
12606
12607 ------------------------
12608 -- Element_To_Compare --
12609 ------------------------
12610
12611 function Element_To_Compare (C : Entity_Id) return Entity_Id is
12612 Comp : Entity_Id := C;
12613
12614 begin
12615 while Present (Comp) loop
12616 -- Skip inherited components
12617
12618 -- Note: for a tagged type, we always generate the "=" primitive
12619 -- for the base type (not on the first subtype), so the test for
12620 -- Comp /= Original_Record_Component (Comp) is True for inherited
12621 -- components only.
12622
12623 if (Is_Tagged_Type (Typ)
12624 and then Comp /= Original_Record_Component (Comp))
12625
12626 -- Skip _Tag
12627
12628 or else Chars (Comp) = Name_uTag
12629
12630 -- Skip interface elements (secondary tags???)
12631
12632 or else Is_Interface (Etype (Comp))
12633 then
12634 Next_Component_Or_Discriminant (Comp);
12635 else
12636 return Comp;
12637 end if;
12638 end loop;
12639
12640 return Empty;
12641 end Element_To_Compare;
12642
12643 -- Start of processing for Expand_Record_Equality
12644
12645 begin
12646 -- Generates the following code: (assuming that Typ has one Discr and
12647 -- component C2 is also a record)
12648
12649 -- Lhs.Discr1 = Rhs.Discr1
12650 -- and then Lhs.C1 = Rhs.C1
12651 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
12652 -- and then ...
12653 -- and then Lhs.Cmpn = Rhs.Cmpn
12654
12655 Result := New_Occurrence_Of (Standard_True, Loc);
12656 C := Element_To_Compare (First_Component_Or_Discriminant (Typ));
12657 while Present (C) loop
12658 declare
12659 New_Lhs : Node_Id;
12660 New_Rhs : Node_Id;
12661 Check : Node_Id;
12662 Lhs_Sel : Node_Id;
12663 Rhs_Sel : Node_Id;
12664 C_Typ : Entity_Id := Etype (C);
12665
12666 begin
12667 if First_Time then
12668 New_Lhs := Lhs;
12669 New_Rhs := Rhs;
12670 else
12671 New_Lhs := New_Copy_Tree (Lhs);
12672 New_Rhs := New_Copy_Tree (Rhs);
12673 end if;
12674
12675 Lhs_Sel :=
12676 Make_Selected_Component (Loc,
12677 Prefix => New_Lhs,
12678 Selector_Name => New_Occurrence_Of (C, Loc));
12679 Rhs_Sel :=
12680 Make_Selected_Component (Loc,
12681 Prefix => New_Rhs,
12682 Selector_Name => New_Occurrence_Of (C, Loc));
12683
12684 -- Generate mutably tagged conversions in case we encounter a
12685 -- special class-wide equivalent type.
12686
12687 if Is_Mutably_Tagged_CW_Equivalent_Type (Etype (C)) then
12688 C_Typ := Corresponding_Mutably_Tagged_Type (Etype (C));
12689 Make_Mutably_Tagged_Conversion (Lhs_Sel, C_Typ);
12690 Make_Mutably_Tagged_Conversion (Rhs_Sel, C_Typ);
12691 end if;
12692
12693 Check :=
12694 Expand_Composite_Equality
12695 (Outer_Type => Typ,
12696 Nod => Nod,
12697 Comp_Type => C_Typ,
12698 Lhs => Lhs_Sel,
12699 Rhs => Rhs_Sel);
12700
12701 -- If some (sub)component is an unchecked_union, the whole
12702 -- operation will raise program error.
12703
12704 if Nkind (Check) = N_Raise_Program_Error then
12705 Result := Check;
12706 Set_Etype (Result, Standard_Boolean);
12707 exit;
12708 else
12709 if First_Time then
12710 Result := Check;
12711
12712 -- Generate logical "and" for CodePeer to simplify the
12713 -- generated code and analysis.
12714
12715 elsif CodePeer_Mode then
12716 Result :=
12717 Make_Op_And (Loc,
12718 Left_Opnd => Result,
12719 Right_Opnd => Check);
12720
12721 else
12722 Result :=
12723 Make_And_Then (Loc,
12724 Left_Opnd => Result,
12725 Right_Opnd => Check);
12726 end if;
12727 end if;
12728 end;
12729
12730 First_Time := False;
12731 C := Element_To_Compare (Next_Component_Or_Discriminant (C));
12732 end loop;
12733
12734 return Result;
12735 end Expand_Record_Equality;
12736
12737 ---------------------------
12738 -- Expand_Set_Membership --
12739 ---------------------------
12740
12741 procedure Expand_Set_Membership (N : Node_Id) is
12742 Lop : constant Node_Id := Left_Opnd (N);
12743
12744 function Make_Cond (Alt : Node_Id) return Node_Id;
12745 -- If the alternative is a subtype mark, create a simple membership
12746 -- test. Otherwise create an equality test for it.
12747
12748 ---------------
12749 -- Make_Cond --
12750 ---------------
12751
12752 function Make_Cond (Alt : Node_Id) return Node_Id is
12753 Cond : Node_Id;
12754 L : constant Node_Id := New_Copy_Tree (Lop);
12755 R : constant Node_Id := Relocate_Node (Alt);
12756
12757 begin
12758 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
12759 or else Nkind (Alt) = N_Range
12760 then
12761 Cond := Make_In (Sloc (Alt), Left_Opnd => L, Right_Opnd => R);
12762
12763 else
12764 Cond := Make_Op_Eq (Sloc (Alt), Left_Opnd => L, Right_Opnd => R);
12765 Resolve_Membership_Equality (Cond, Etype (Alt));
12766 end if;
12767
12768 return Cond;
12769 end Make_Cond;
12770
12771 -- Local variables
12772
12773 Alt : Node_Id;
12774 Res : Node_Id := Empty;
12775
12776 -- Start of processing for Expand_Set_Membership
12777
12778 begin
12779 Remove_Side_Effects (Lop);
12780
12781 -- We use left associativity as in the equivalent boolean case. This
12782 -- kind of canonicalization helps the optimizer of the code generator.
12783
12784 Alt := First (Alternatives (N));
12785 while Present (Alt) loop
12786 Evolve_Or_Else (Res, Make_Cond (Alt));
12787 Next (Alt);
12788 end loop;
12789
12790 Rewrite (N, Res);
12791 Analyze_And_Resolve (N, Standard_Boolean);
12792 end Expand_Set_Membership;
12793
12794 -----------------------------------
12795 -- Expand_Short_Circuit_Operator --
12796 -----------------------------------
12797
12798 -- Deal with special expansion if actions are present for the right operand
12799 -- and deal with optimizing case of arguments being True or False. We also
12800 -- deal with the special case of non-standard boolean values.
12801
12802 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
12803 Loc : constant Source_Ptr := Sloc (N);
12804 Typ : constant Entity_Id := Etype (N);
12805 Left : constant Node_Id := Left_Opnd (N);
12806 Right : constant Node_Id := Right_Opnd (N);
12807 LocR : constant Source_Ptr := Sloc (Right);
12808 Actlist : List_Id;
12809
12810 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
12811 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
12812 -- If Left = Shortcut_Value then Right need not be evaluated
12813
12814 function Useful (Actions : List_Id) return Boolean;
12815 -- Return True if Actions contains useful nodes to process
12816
12817 ------------
12818 -- Useful --
12819 ------------
12820
12821 function Useful (Actions : List_Id) return Boolean is
12822 Action : Node_Id;
12823 begin
12824 Action := First (Actions);
12825
12826 -- For now "useful" means not N_Variable_Reference_Marker. Consider
12827 -- stripping other nodes in the future.
12828
12829 while Present (Action) loop
12830 if Nkind (Action) /= N_Variable_Reference_Marker then
12831 return True;
12832 end if;
12833
12834 Next (Action);
12835 end loop;
12836
12837 return False;
12838 end Useful;
12839
12840 -- Start of processing for Expand_Short_Circuit_Operator
12841
12842 begin
12843 -- Deal with non-standard booleans
12844
12845 if Is_Boolean_Type (Typ) then
12846 Adjust_Condition (Left);
12847 Adjust_Condition (Right);
12848 Set_Etype (N, Standard_Boolean);
12849 end if;
12850
12851 -- Check for cases where left argument is known to be True or False
12852
12853 if Compile_Time_Known_Value (Left) then
12854
12855 -- Mark SCO for left condition as compile time known
12856
12857 if Generate_SCO and then Comes_From_Source (Left) then
12858 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
12859 end if;
12860
12861 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
12862 -- Any actions associated with Right will be executed unconditionally
12863 -- and can thus be inserted into the tree unconditionally.
12864
12865 if Expr_Value_E (Left) /= Shortcut_Ent then
12866 if Present (Actions (N)) then
12867 Insert_Actions (N, Actions (N));
12868 end if;
12869
12870 Rewrite (N, Right);
12871
12872 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
12873 -- In this case we can forget the actions associated with Right,
12874 -- since they will never be executed.
12875
12876 else
12877 Kill_Dead_Code (Right);
12878 Kill_Dead_Code (Actions (N));
12879 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12880 end if;
12881
12882 Adjust_Result_Type (N, Typ);
12883 return;
12884 end if;
12885
12886 -- If Actions are present for the right operand, we have to do some
12887 -- special processing. We can't just let these actions filter back into
12888 -- code preceding the short circuit (which is what would have happened
12889 -- if we had not trapped them in the short-circuit form), since they
12890 -- must only be executed if the right operand of the short circuit is
12891 -- executed and not otherwise.
12892
12893 if Useful (Actions (N)) then
12894 Actlist := Actions (N);
12895
12896 -- Use an Expression_With_Actions node for the right operand of the
12897 -- short-circuit form. Note that this solves traceability problems
12898 -- for coverage analysis at the object level.
12899
12900 Rewrite (Right,
12901 Make_Expression_With_Actions (LocR,
12902 Expression => Relocate_Node (Right),
12903 Actions => Actlist));
12904
12905 Set_Actions (N, No_List);
12906 Analyze_And_Resolve (Right, Standard_Boolean);
12907 Adjust_Result_Type (N, Typ);
12908 return;
12909 end if;
12910
12911 -- No actions present, check for cases of right argument True/False
12912
12913 if Compile_Time_Known_Value (Right) then
12914
12915 -- Mark SCO for left condition as compile time known
12916
12917 if Generate_SCO and then Comes_From_Source (Right) then
12918 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
12919 end if;
12920
12921 -- Change (Left and then True), (Left or else False) to Left. Note
12922 -- that we know there are no actions associated with the right
12923 -- operand, since we just checked for this case above.
12924
12925 if Expr_Value_E (Right) /= Shortcut_Ent then
12926 Rewrite (N, Left);
12927
12928 -- Change (Left and then False), (Left or else True) to Right,
12929 -- making sure to preserve any side effects associated with the Left
12930 -- operand.
12931
12932 else
12933 Remove_Side_Effects (Left);
12934 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
12935 end if;
12936 end if;
12937
12938 Adjust_Result_Type (N, Typ);
12939 end Expand_Short_Circuit_Operator;
12940
12941 -------------------------------------
12942 -- Expand_Unchecked_Union_Equality --
12943 -------------------------------------
12944
12945 procedure Expand_Unchecked_Union_Equality (N : Node_Id) is
12946 Loc : constant Source_Ptr := Sloc (N);
12947 Eq : constant Entity_Id := Entity (Name (N));
12948 Lhs : constant Node_Id := First_Actual (N);
12949 Rhs : constant Node_Id := Next_Actual (Lhs);
12950
12951 function Get_Discr_Values (Op : Node_Id; Lhs : Boolean) return Elist_Id;
12952 -- Return the list of inferred discriminant values for Op
12953
12954 ----------------------
12955 -- Get_Discr_Values --
12956 ----------------------
12957
12958 function Get_Discr_Values (Op : Node_Id; Lhs : Boolean) return Elist_Id
12959 is
12960 Typ : constant Entity_Id := Etype (Op);
12961 Values : constant Elist_Id := New_Elmt_List;
12962
12963 function Get_Extra_Formal (Nam : Name_Id) return Entity_Id;
12964 -- Return the extra formal Nam from the current scope, which must be
12965 -- an equality function for an unchecked union type.
12966
12967 ----------------------
12968 -- Get_Extra_Formal --
12969 ----------------------
12970
12971 function Get_Extra_Formal (Nam : Name_Id) return Entity_Id is
12972 Func : constant Entity_Id := Current_Scope;
12973
12974 Formal : Entity_Id;
12975
12976 begin
12977 pragma Assert (Ekind (Func) = E_Function);
12978
12979 Formal := Extra_Formals (Func);
12980 while Present (Formal) loop
12981 if Chars (Formal) = Nam then
12982 return Formal;
12983 end if;
12984
12985 Formal := Extra_Formal (Formal);
12986 end loop;
12987
12988 -- An extra formal of the proper name must be found
12989
12990 raise Program_Error;
12991 end Get_Extra_Formal;
12992
12993 -- Local variables
12994
12995 Discr : Entity_Id;
12996
12997 -- Start of processing for Get_Discr_Values
12998
12999 begin
13000 -- Per-object constrained selected components require special
13001 -- attention. If the enclosing scope of the component is an
13002 -- Unchecked_Union, we cannot reference its discriminants
13003 -- directly. This is why we use the extra parameters of the
13004 -- equality function of the enclosing Unchecked_Union.
13005
13006 -- type UU_Type (Discr : Integer := 0) is
13007 -- . . .
13008 -- end record;
13009 -- pragma Unchecked_Union (UU_Type);
13010
13011 -- 1. Unchecked_Union enclosing record:
13012
13013 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
13014 -- . . .
13015 -- Comp : UU_Type (Discr);
13016 -- . . .
13017 -- end Enclosing_UU_Type;
13018 -- pragma Unchecked_Union (Enclosing_UU_Type);
13019
13020 -- Obj1 : Enclosing_UU_Type;
13021 -- Obj2 : Enclosing_UU_Type (1);
13022
13023 -- [. . .] Obj1 = Obj2 [. . .]
13024
13025 -- Generated code:
13026
13027 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
13028
13029 -- A and B are the formal parameters of the equality function
13030 -- of Enclosing_UU_Type. The function always has two extra
13031 -- formals to capture the inferred discriminant values for
13032 -- each discriminant of the type.
13033
13034 -- 2. Non-Unchecked_Union enclosing record:
13035
13036 -- type
13037 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
13038 -- is record
13039 -- . . .
13040 -- Comp : UU_Type (Discr);
13041 -- . . .
13042 -- end Enclosing_Non_UU_Type;
13043
13044 -- Obj1 : Enclosing_Non_UU_Type;
13045 -- Obj2 : Enclosing_Non_UU_Type (1);
13046
13047 -- ... Obj1 = Obj2 ...
13048
13049 -- Generated code:
13050
13051 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
13052 -- obj1.discr, obj2.discr)) then
13053
13054 -- In this case we can directly reference the discriminants of
13055 -- the enclosing record.
13056
13057 if Nkind (Op) = N_Selected_Component
13058 and then Has_Per_Object_Constraint (Entity (Selector_Name (Op)))
13059 then
13060 -- If enclosing record is an Unchecked_Union, use formals
13061 -- corresponding to each discriminant. The name of the
13062 -- formal is that of the discriminant, with added suffix,
13063 -- see Exp_Ch3.Build_Variant_Record_Equality for details.
13064
13065 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Op)))) then
13066 Discr :=
13067 First_Discriminant
13068 (Scope (Entity (Selector_Name (Op))));
13069 while Present (Discr) loop
13070 Append_Elmt
13071 (New_Occurrence_Of
13072 (Get_Extra_Formal
13073 (New_External_Name
13074 (Chars (Discr), (if Lhs then 'A' else 'B'))), Loc),
13075 To => Values);
13076 Next_Discriminant (Discr);
13077 end loop;
13078
13079 -- If enclosing record is of a non-Unchecked_Union type, it
13080 -- is possible to reference its discriminants directly.
13081
13082 else
13083 Discr := First_Discriminant (Typ);
13084 while Present (Discr) loop
13085 Append_Elmt
13086 (Make_Selected_Component (Loc,
13087 Prefix => Prefix (Op),
13088 Selector_Name =>
13089 New_Copy
13090 (Get_Discriminant_Value (Discr,
13091 Typ,
13092 Stored_Constraint (Typ)))),
13093 To => Values);
13094 Next_Discriminant (Discr);
13095 end loop;
13096 end if;
13097
13098 -- Otherwise operand is on object with a constrained type.
13099 -- Infer the discriminant values from the constraint.
13100
13101 else
13102 Discr := First_Discriminant (Typ);
13103 while Present (Discr) loop
13104 Append_Elmt
13105 (New_Copy
13106 (Get_Discriminant_Value (Discr,
13107 Typ,
13108 Stored_Constraint (Typ))),
13109 To => Values);
13110 Next_Discriminant (Discr);
13111 end loop;
13112 end if;
13113
13114 return Values;
13115 end Get_Discr_Values;
13116
13117 -- Start of processing for Expand_Unchecked_Union_Equality
13118
13119 begin
13120 -- Guard against repeated invocation on the same node
13121
13122 if Present (Next_Actual (Rhs)) then
13123 return;
13124 end if;
13125
13126 -- If we can infer the discriminants of the operands, make a call to Eq
13127
13128 if Has_Inferable_Discriminants (Lhs)
13129 and then
13130 Has_Inferable_Discriminants (Rhs)
13131 then
13132 declare
13133 Lhs_Values : constant Elist_Id := Get_Discr_Values (Lhs, True);
13134 Rhs_Values : constant Elist_Id := Get_Discr_Values (Rhs, False);
13135
13136 Formal : Entity_Id;
13137 L_Elmt : Elmt_Id;
13138 R_Elmt : Elmt_Id;
13139
13140 begin
13141 -- Add the inferred discriminant values as extra actuals
13142
13143 Formal := Extra_Formals (Eq);
13144 L_Elmt := First_Elmt (Lhs_Values);
13145 R_Elmt := First_Elmt (Rhs_Values);
13146
13147 while Present (L_Elmt) loop
13148 Analyze_And_Resolve (Node (L_Elmt), Etype (Formal));
13149 Add_Extra_Actual_To_Call (N, Formal, Node (L_Elmt));
13150
13151 Formal := Extra_Formal (Formal);
13152
13153 Analyze_And_Resolve (Node (R_Elmt), Etype (Formal));
13154 Add_Extra_Actual_To_Call (N, Formal, Node (R_Elmt));
13155
13156 Formal := Extra_Formal (Formal);
13157 Next_Elmt (L_Elmt);
13158 Next_Elmt (R_Elmt);
13159 end loop;
13160 end;
13161
13162 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
13163 -- the predefined equality operator for an Unchecked_Union type
13164 -- if either of the operands lack inferable discriminants.
13165
13166 else
13167 Insert_Action (N,
13168 Make_Raise_Program_Error (Loc,
13169 Reason => PE_Unchecked_Union_Restriction));
13170
13171 -- Give a warning on source equalities only, otherwise the message
13172 -- may appear out of place due to internal use. It is unconditional
13173 -- because it is required by the language.
13174
13175 if Comes_From_Source (Original_Node (N)) then
13176 Error_Msg_N
13177 ("Unchecked_Union discriminants cannot be determined??", N);
13178 Error_Msg_N
13179 ("\Program_Error will be raised for equality operation??", N);
13180 end if;
13181
13182 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
13183 end if;
13184 end Expand_Unchecked_Union_Equality;
13185
13186 ------------------------------------
13187 -- Fixup_Universal_Fixed_Operation --
13188 -------------------------------------
13189
13190 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
13191 Conv : constant Node_Id := Parent (N);
13192
13193 begin
13194 -- We must have a type conversion immediately above us
13195
13196 pragma Assert (Nkind (Conv) = N_Type_Conversion);
13197
13198 -- Normally the type conversion gives our target type. The exception
13199 -- occurs in the case of the Round attribute, where the conversion
13200 -- will be to universal real, and our real type comes from the Round
13201 -- attribute (as well as an indication that we must round the result)
13202
13203 if Etype (Conv) = Universal_Real
13204 and then Nkind (Parent (Conv)) = N_Attribute_Reference
13205 and then Attribute_Name (Parent (Conv)) = Name_Round
13206 then
13207 Set_Etype (N, Base_Type (Etype (Parent (Conv))));
13208 Set_Rounded_Result (N);
13209
13210 -- Normal case where type comes from conversion above us
13211
13212 else
13213 Set_Etype (N, Base_Type (Etype (Conv)));
13214 end if;
13215 end Fixup_Universal_Fixed_Operation;
13216
13217 ----------------------------
13218 -- Get_First_Index_Bounds --
13219 ----------------------------
13220
13221 procedure Get_First_Index_Bounds (T : Entity_Id; Lo, Hi : out Uint) is
13222 Typ : Entity_Id;
13223
13224 begin
13225 pragma Assert (Is_Array_Type (T));
13226
13227 -- This follows Sem_Eval.Compile_Time_Known_Bounds
13228
13229 if Ekind (T) = E_String_Literal_Subtype then
13230 Lo := Expr_Value (String_Literal_Low_Bound (T));
13231 Hi := Lo + String_Literal_Length (T) - 1;
13232
13233 else
13234 Typ := Underlying_Type (Etype (First_Index (T)));
13235
13236 Lo := Expr_Value (Type_Low_Bound (Typ));
13237 Hi := Expr_Value (Type_High_Bound (Typ));
13238 end if;
13239 end Get_First_Index_Bounds;
13240
13241 ------------------------
13242 -- Get_Size_For_Range --
13243 ------------------------
13244
13245 function Get_Size_For_Range (Lo, Hi : Uint) return Uint is
13246
13247 function Is_OK_For_Range (Siz : Uint) return Boolean;
13248 -- Return True if a signed integer with given size can cover Lo .. Hi
13249
13250 --------------------------
13251 -- Is_OK_For_Range --
13252 --------------------------
13253
13254 function Is_OK_For_Range (Siz : Uint) return Boolean is
13255 B : constant Uint := Uint_2 ** (Siz - 1);
13256
13257 begin
13258 -- Test B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
13259
13260 return Lo >= -B and then Hi >= -B and then Lo < B and then Hi < B;
13261 end Is_OK_For_Range;
13262
13263 begin
13264 -- This is (almost always) the size of Integer
13265
13266 if Is_OK_For_Range (Uint_32) then
13267 return Uint_32;
13268
13269 -- Check 63
13270
13271 elsif Is_OK_For_Range (Uint_63) then
13272 return Uint_63;
13273
13274 -- This is (almost always) the size of Long_Long_Integer
13275
13276 elsif Is_OK_For_Range (Uint_64) then
13277 return Uint_64;
13278
13279 -- Check 127
13280
13281 elsif Is_OK_For_Range (Uint_127) then
13282 return Uint_127;
13283
13284 else
13285 return Uint_128;
13286 end if;
13287 end Get_Size_For_Range;
13288
13289 -------------------------------------------
13290 -- Insert_Conditional_Object_Declaration --
13291 -------------------------------------------
13292
13293 procedure Insert_Conditional_Object_Declaration
13294 (Obj_Id : Entity_Id;
13295 Typ : Entity_Id;
13296 Expr : Node_Id;
13297 Const : Boolean)
13298 is
13299 Loc : constant Source_Ptr := Sloc (Expr);
13300 Obj_Decl : constant Node_Id :=
13301 Make_Object_Declaration (Loc,
13302 Defining_Identifier => Obj_Id,
13303 Aliased_Present => True,
13304 Constant_Present => Const,
13305 Object_Definition => New_Occurrence_Of (Typ, Loc),
13306 Expression => Relocate_Node (Expr));
13307 -- We make the object unconditionally aliased to avoid dangling bound
13308 -- issues when its nominal subtype is an unconstrained array type.
13309
13310 Master_Node_Decl : Node_Id;
13311 Master_Node_Id : Entity_Id;
13312
13313 begin
13314 -- If the expression is itself a conditional expression whose
13315 -- expansion has been delayed, analyze it again and expand it.
13316
13317 if Is_Delayed_Conditional_Expression (Expression (Obj_Decl)) then
13318 Unanalyze_Delayed_Conditional_Expression (Expression (Obj_Decl));
13319 end if;
13320
13321 Insert_Action (Expr, Obj_Decl);
13322
13323 -- The object can never be local to an elaboration routine at library
13324 -- level since we will take 'Unrestricted_Access of it. Beware that
13325 -- Is_Library_Level_Entity always returns False when called from within
13326 -- a transient scope, but the associated block will not be materialized
13327 -- when the transient scope is finally closed in the case of an object
13328 -- declaration (see Exp.Ch7.Wrap_Transient_Declaration).
13329
13330 if Scope (Obj_Id) = Current_Scope and then Scope_Is_Transient then
13331 Set_Is_Statically_Allocated
13332 (Obj_Id, Is_Library_Level_Entity (Scope (Obj_Id)));
13333 else
13334 Set_Is_Statically_Allocated
13335 (Obj_Id, Is_Library_Level_Entity (Obj_Id));
13336 end if;
13337
13338 -- If the object needs finalization, we need to insert its Master_Node
13339 -- manually because 1) the machinery in Exp_Ch7 will not pick it since
13340 -- it will be declared in the arm of a conditional statement and 2) we
13341 -- cannot invoke Process_Transients_In_Expression on it since it is not
13342 -- a transient object (it has the lifetime of the original object).
13343
13344 if Needs_Finalization (Base_Type (Etype (Obj_Id))) then
13345 Master_Node_Id := Make_Temporary (Loc, 'N');
13346 Master_Node_Decl :=
13347 Make_Master_Node_Declaration (Loc, Master_Node_Id, Obj_Id);
13348
13349 -- The master is the innermost enclosing non-transient construct
13350
13351 Insert_Action (Find_Hook_Context (Expr), Master_Node_Decl);
13352
13353 -- Propagate the relaxed finalization semantics
13354
13355 Set_Is_Independent
13356 (Master_Node_Id,
13357 Has_Relaxed_Finalization (Base_Type (Etype (Obj_Id))));
13358
13359 -- Generate the attachment of the object to the Master_Node
13360
13361 Attach_Object_To_Master_Node (Obj_Decl, Master_Node_Id);
13362
13363 -- Mark the transient object to avoid double finalization
13364
13365 Set_Is_Finalized_Transient (Obj_Id);
13366 end if;
13367 end Insert_Conditional_Object_Declaration;
13368
13369 -------------------------------
13370 -- Insert_Dereference_Action --
13371 -------------------------------
13372
13373 procedure Insert_Dereference_Action (N : Node_Id) is
13374 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
13375 -- Return true if type of P is derived from Checked_Pool;
13376
13377 -----------------------------
13378 -- Is_Checked_Storage_Pool --
13379 -----------------------------
13380
13381 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
13382 T : Entity_Id;
13383
13384 begin
13385 if No (P) then
13386 return False;
13387 end if;
13388
13389 T := Etype (P);
13390 while T /= Etype (T) loop
13391 if Is_RTE (T, RE_Checked_Pool) then
13392 return True;
13393 else
13394 T := Etype (T);
13395 end if;
13396 end loop;
13397
13398 return False;
13399 end Is_Checked_Storage_Pool;
13400
13401 -- Local variables
13402
13403 Context : constant Node_Id := Parent (N);
13404 Ptr_Typ : constant Entity_Id := Etype (N);
13405 Desig_Typ : constant Entity_Id :=
13406 Available_View (Designated_Type (Ptr_Typ));
13407 Loc : constant Source_Ptr := Sloc (N);
13408 Pool : constant Entity_Id := Associated_Storage_Pool (Ptr_Typ);
13409
13410 Addr : Entity_Id;
13411 Alig : Entity_Id;
13412 Deref : Node_Id;
13413 Size : Entity_Id;
13414 Size_Bits : Node_Id;
13415 Stmt : Node_Id;
13416
13417 -- Start of processing for Insert_Dereference_Action
13418
13419 begin
13420 pragma Assert (Nkind (Context) = N_Explicit_Dereference);
13421
13422 -- Do not re-expand a dereference which has already been processed by
13423 -- this routine.
13424
13425 if Has_Dereference_Action (Context) then
13426 return;
13427
13428 -- Do not perform this type of expansion for internally-generated
13429 -- dereferences.
13430
13431 elsif not Comes_From_Source (Original_Node (Context)) then
13432 return;
13433
13434 -- A dereference action is only applicable to objects which have been
13435 -- allocated on a checked pool.
13436
13437 elsif not Is_Checked_Storage_Pool (Pool) then
13438 return;
13439 end if;
13440
13441 -- Extract the address of the dereferenced object. Generate:
13442
13443 -- Addr : System.Address := <N>'Pool_Address;
13444
13445 Addr := Make_Temporary (Loc, 'P');
13446
13447 Insert_Action (N,
13448 Make_Object_Declaration (Loc,
13449 Defining_Identifier => Addr,
13450 Object_Definition =>
13451 New_Occurrence_Of (RTE (RE_Address), Loc),
13452 Expression =>
13453 Make_Attribute_Reference (Loc,
13454 Prefix => Duplicate_Subexpr_Move_Checks (N),
13455 Attribute_Name => Name_Pool_Address)));
13456
13457 -- Calculate the size of the dereferenced object. Generate:
13458
13459 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
13460
13461 Deref :=
13462 Make_Explicit_Dereference (Loc,
13463 Prefix => Duplicate_Subexpr_Move_Checks (N));
13464 Set_Has_Dereference_Action (Deref);
13465
13466 Size_Bits :=
13467 Make_Attribute_Reference (Loc,
13468 Prefix => Deref,
13469 Attribute_Name => Name_Size);
13470
13471 -- Special case of an unconstrained array: need to add descriptor size
13472
13473 if Is_Array_Type (Desig_Typ)
13474 and then not Is_Constrained (First_Subtype (Desig_Typ))
13475 then
13476 Size_Bits :=
13477 Make_Op_Add (Loc,
13478 Left_Opnd =>
13479 Make_Attribute_Reference (Loc,
13480 Prefix =>
13481 New_Occurrence_Of (First_Subtype (Desig_Typ), Loc),
13482 Attribute_Name => Name_Descriptor_Size),
13483 Right_Opnd => Size_Bits);
13484 end if;
13485
13486 Size := Make_Temporary (Loc, 'S');
13487 Insert_Action (N,
13488 Make_Object_Declaration (Loc,
13489 Defining_Identifier => Size,
13490 Object_Definition =>
13491 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
13492 Expression =>
13493 Make_Op_Divide (Loc,
13494 Left_Opnd => Size_Bits,
13495 Right_Opnd => Make_Integer_Literal (Loc, System_Storage_Unit))));
13496
13497 -- Calculate the alignment of the dereferenced object. Generate:
13498 -- Alig : constant Storage_Count := <N>.all'Alignment;
13499
13500 Deref :=
13501 Make_Explicit_Dereference (Loc,
13502 Prefix => Duplicate_Subexpr_Move_Checks (N));
13503 Set_Has_Dereference_Action (Deref);
13504
13505 Alig := Make_Temporary (Loc, 'A');
13506 Insert_Action (N,
13507 Make_Object_Declaration (Loc,
13508 Defining_Identifier => Alig,
13509 Object_Definition =>
13510 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
13511 Expression =>
13512 Make_Attribute_Reference (Loc,
13513 Prefix => Deref,
13514 Attribute_Name => Name_Alignment)));
13515
13516 -- A dereference of a controlled object requires special processing. The
13517 -- finalization machinery requests additional space from the underlying
13518 -- pool to allocate and hide two pointers. As a result, a checked pool
13519 -- may mark the wrong memory as valid. Since checked pools do not have
13520 -- knowledge of hidden pointers, we have to bring the two pointers back
13521 -- in view in order to restore the original state of the object.
13522
13523 -- The address manipulation is not performed for access types that are
13524 -- subject to pragma No_Heap_Finalization because the two pointers do
13525 -- not exist in the first place. Likewise for designated types that are
13526 -- subject to relaxed finalization.
13527
13528 if No_Heap_Finalization (Ptr_Typ) then
13529 null;
13530
13531 elsif Needs_Finalization (Desig_Typ)
13532 and then not Has_Relaxed_Finalization (Desig_Typ)
13533 then
13534 -- Adjust the address and size of the dereferenced object. Generate:
13535 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
13536
13537 Stmt :=
13538 Make_Procedure_Call_Statement (Loc,
13539 Name =>
13540 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
13541 Parameter_Associations => New_List (
13542 New_Occurrence_Of (Addr, Loc),
13543 New_Occurrence_Of (Size, Loc),
13544 New_Occurrence_Of (Alig, Loc)));
13545
13546 -- Class-wide types complicate things because we cannot determine
13547 -- statically whether the actual object is truly controlled. We must
13548 -- generate a runtime check to detect this property. Generate:
13549 --
13550 -- if Needs_Finalization (<N>.all'Tag) then
13551 -- <Stmt>;
13552 -- end if;
13553
13554 if Is_Class_Wide_Type (Desig_Typ) then
13555 Deref :=
13556 Make_Explicit_Dereference (Loc,
13557 Prefix => Duplicate_Subexpr_Move_Checks (N));
13558 Set_Has_Dereference_Action (Deref);
13559
13560 Stmt :=
13561 Make_Implicit_If_Statement (N,
13562 Condition =>
13563 Make_Function_Call (Loc,
13564 Name =>
13565 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
13566 Parameter_Associations => New_List (
13567 Make_Attribute_Reference (Loc,
13568 Prefix => Deref,
13569 Attribute_Name => Name_Tag))),
13570 Then_Statements => New_List (Stmt));
13571 end if;
13572
13573 Insert_Action (N, Stmt);
13574 end if;
13575
13576 -- Generate:
13577 -- Dereference (Pool, Addr, Size, Alig);
13578
13579 Insert_Action (N,
13580 Make_Procedure_Call_Statement (Loc,
13581 Name =>
13582 New_Occurrence_Of
13583 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
13584 Parameter_Associations => New_List (
13585 New_Occurrence_Of (Pool, Loc),
13586 New_Occurrence_Of (Addr, Loc),
13587 New_Occurrence_Of (Size, Loc),
13588 New_Occurrence_Of (Alig, Loc))));
13589
13590 -- Mark the explicit dereference as processed to avoid potential
13591 -- infinite expansion.
13592
13593 Set_Has_Dereference_Action (Context);
13594
13595 exception
13596 when RE_Not_Available =>
13597 return;
13598 end Insert_Dereference_Action;
13599
13600 --------------------------------
13601 -- Integer_Promotion_Possible --
13602 --------------------------------
13603
13604 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
13605 Operand : constant Node_Id := Expression (N);
13606 Operand_Type : constant Entity_Id := Etype (Operand);
13607 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
13608
13609 begin
13610 pragma Assert (Nkind (N) = N_Type_Conversion);
13611
13612 return
13613
13614 -- We only do the transformation for source constructs. We assume
13615 -- that the expander knows what it is doing when it generates code.
13616
13617 Comes_From_Source (N)
13618
13619 -- If the operand type is Short_Integer or Short_Short_Integer,
13620 -- then we will promote to Integer, which is available on all
13621 -- targets, and is sufficient to ensure no intermediate overflow.
13622 -- Furthermore it is likely to be as efficient or more efficient
13623 -- than using the smaller type for the computation so we do this
13624 -- unconditionally.
13625
13626 and then
13627 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
13628 or else
13629 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
13630
13631 -- Test for interesting operation, which includes addition,
13632 -- division, exponentiation, multiplication, subtraction, absolute
13633 -- value and unary negation. Unary "+" is omitted since it is a
13634 -- no-op and thus can't overflow.
13635
13636 and then Nkind (Operand) in
13637 N_Op_Abs | N_Op_Add | N_Op_Divide | N_Op_Expon |
13638 N_Op_Minus | N_Op_Multiply | N_Op_Subtract;
13639 end Integer_Promotion_Possible;
13640
13641 ------------------------------
13642 -- Make_Array_Comparison_Op --
13643 ------------------------------
13644
13645 -- This is a hand-coded expansion of the following generic function:
13646
13647 -- generic
13648 -- type elem is (<>);
13649 -- type index is (<>);
13650 -- type a is array (index range <>) of elem;
13651
13652 -- function Gnnn (X : a; Y: a) return boolean is
13653 -- J : index := Y'first;
13654
13655 -- begin
13656 -- if X'length = 0 then
13657 -- return false;
13658
13659 -- elsif Y'length = 0 then
13660 -- return true;
13661
13662 -- else
13663 -- for I in X'range loop
13664 -- if X (I) = Y (J) then
13665 -- if J = Y'last then
13666 -- exit;
13667 -- else
13668 -- J := index'succ (J);
13669 -- end if;
13670
13671 -- else
13672 -- return X (I) > Y (J);
13673 -- end if;
13674 -- end loop;
13675
13676 -- return X'length > Y'length;
13677 -- end if;
13678 -- end Gnnn;
13679
13680 -- Note that since we are essentially doing this expansion by hand, we
13681 -- do not need to generate an actual or formal generic part, just the
13682 -- instantiated function itself.
13683
13684 function Make_Array_Comparison_Op
13685 (Typ : Entity_Id;
13686 Nod : Node_Id) return Node_Id
13687 is
13688 Loc : constant Source_Ptr := Sloc (Nod);
13689
13690 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
13691 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
13692 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
13693 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
13694
13695 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
13696
13697 Loop_Statement : Node_Id;
13698 Loop_Body : Node_Id;
13699 If_Stat : Node_Id;
13700 Inner_If : Node_Id;
13701 Final_Expr : Node_Id;
13702 Func_Body : Node_Id;
13703 Func_Name : Entity_Id;
13704 Formals : List_Id;
13705 Length1 : Node_Id;
13706 Length2 : Node_Id;
13707
13708 begin
13709 -- if J = Y'last then
13710 -- exit;
13711 -- else
13712 -- J := index'succ (J);
13713 -- end if;
13714
13715 Inner_If :=
13716 Make_Implicit_If_Statement (Nod,
13717 Condition =>
13718 Make_Op_Eq (Loc,
13719 Left_Opnd => New_Occurrence_Of (J, Loc),
13720 Right_Opnd =>
13721 Make_Attribute_Reference (Loc,
13722 Prefix => New_Occurrence_Of (Y, Loc),
13723 Attribute_Name => Name_Last)),
13724
13725 Then_Statements => New_List (
13726 Make_Exit_Statement (Loc)),
13727
13728 Else_Statements =>
13729 New_List (
13730 Make_Assignment_Statement (Loc,
13731 Name => New_Occurrence_Of (J, Loc),
13732 Expression =>
13733 Make_Attribute_Reference (Loc,
13734 Prefix => New_Occurrence_Of (Index, Loc),
13735 Attribute_Name => Name_Succ,
13736 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
13737
13738 -- if X (I) = Y (J) then
13739 -- if ... end if;
13740 -- else
13741 -- return X (I) > Y (J);
13742 -- end if;
13743
13744 Loop_Body :=
13745 Make_Implicit_If_Statement (Nod,
13746 Condition =>
13747 Make_Op_Eq (Loc,
13748 Left_Opnd =>
13749 Make_Indexed_Component (Loc,
13750 Prefix => New_Occurrence_Of (X, Loc),
13751 Expressions => New_List (New_Occurrence_Of (I, Loc))),
13752
13753 Right_Opnd =>
13754 Make_Indexed_Component (Loc,
13755 Prefix => New_Occurrence_Of (Y, Loc),
13756 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
13757
13758 Then_Statements => New_List (Inner_If),
13759
13760 Else_Statements => New_List (
13761 Make_Simple_Return_Statement (Loc,
13762 Expression =>
13763 Make_Op_Gt (Loc,
13764 Left_Opnd =>
13765 Make_Indexed_Component (Loc,
13766 Prefix => New_Occurrence_Of (X, Loc),
13767 Expressions => New_List (New_Occurrence_Of (I, Loc))),
13768
13769 Right_Opnd =>
13770 Make_Indexed_Component (Loc,
13771 Prefix => New_Occurrence_Of (Y, Loc),
13772 Expressions => New_List (
13773 New_Occurrence_Of (J, Loc)))))));
13774
13775 -- for I in X'range loop
13776 -- if ... end if;
13777 -- end loop;
13778
13779 Loop_Statement :=
13780 Make_Implicit_Loop_Statement (Nod,
13781 Identifier => Empty,
13782
13783 Iteration_Scheme =>
13784 Make_Iteration_Scheme (Loc,
13785 Loop_Parameter_Specification =>
13786 Make_Loop_Parameter_Specification (Loc,
13787 Defining_Identifier => I,
13788 Discrete_Subtype_Definition =>
13789 Make_Attribute_Reference (Loc,
13790 Prefix => New_Occurrence_Of (X, Loc),
13791 Attribute_Name => Name_Range))),
13792
13793 Statements => New_List (Loop_Body));
13794
13795 -- if X'length = 0 then
13796 -- return false;
13797 -- elsif Y'length = 0 then
13798 -- return true;
13799 -- else
13800 -- for ... loop ... end loop;
13801 -- return X'length > Y'length;
13802 -- end if;
13803
13804 Length1 :=
13805 Make_Attribute_Reference (Loc,
13806 Prefix => New_Occurrence_Of (X, Loc),
13807 Attribute_Name => Name_Length);
13808
13809 Length2 :=
13810 Make_Attribute_Reference (Loc,
13811 Prefix => New_Occurrence_Of (Y, Loc),
13812 Attribute_Name => Name_Length);
13813
13814 Final_Expr :=
13815 Make_Op_Gt (Loc,
13816 Left_Opnd => Length1,
13817 Right_Opnd => Length2);
13818
13819 If_Stat :=
13820 Make_Implicit_If_Statement (Nod,
13821 Condition =>
13822 Make_Op_Eq (Loc,
13823 Left_Opnd =>
13824 Make_Attribute_Reference (Loc,
13825 Prefix => New_Occurrence_Of (X, Loc),
13826 Attribute_Name => Name_Length),
13827 Right_Opnd =>
13828 Make_Integer_Literal (Loc, 0)),
13829
13830 Then_Statements =>
13831 New_List (
13832 Make_Simple_Return_Statement (Loc,
13833 Expression => New_Occurrence_Of (Standard_False, Loc))),
13834
13835 Elsif_Parts => New_List (
13836 Make_Elsif_Part (Loc,
13837 Condition =>
13838 Make_Op_Eq (Loc,
13839 Left_Opnd =>
13840 Make_Attribute_Reference (Loc,
13841 Prefix => New_Occurrence_Of (Y, Loc),
13842 Attribute_Name => Name_Length),
13843 Right_Opnd =>
13844 Make_Integer_Literal (Loc, 0)),
13845
13846 Then_Statements =>
13847 New_List (
13848 Make_Simple_Return_Statement (Loc,
13849 Expression => New_Occurrence_Of (Standard_True, Loc))))),
13850
13851 Else_Statements => New_List (
13852 Loop_Statement,
13853 Make_Simple_Return_Statement (Loc,
13854 Expression => Final_Expr)));
13855
13856 -- (X : a; Y: a)
13857
13858 Formals := New_List (
13859 Make_Parameter_Specification (Loc,
13860 Defining_Identifier => X,
13861 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
13862
13863 Make_Parameter_Specification (Loc,
13864 Defining_Identifier => Y,
13865 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
13866
13867 -- function Gnnn (...) return boolean is
13868 -- J : index := Y'first;
13869 -- begin
13870 -- if ... end if;
13871 -- end Gnnn;
13872
13873 Func_Name := Make_Temporary (Loc, 'G');
13874
13875 Func_Body :=
13876 Make_Subprogram_Body (Loc,
13877 Specification =>
13878 Make_Function_Specification (Loc,
13879 Defining_Unit_Name => Func_Name,
13880 Parameter_Specifications => Formals,
13881 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
13882
13883 Declarations => New_List (
13884 Make_Object_Declaration (Loc,
13885 Defining_Identifier => J,
13886 Object_Definition => New_Occurrence_Of (Index, Loc),
13887 Expression =>
13888 Make_Attribute_Reference (Loc,
13889 Prefix => New_Occurrence_Of (Y, Loc),
13890 Attribute_Name => Name_First))),
13891
13892 Handled_Statement_Sequence =>
13893 Make_Handled_Sequence_Of_Statements (Loc,
13894 Statements => New_List (If_Stat)));
13895
13896 return Func_Body;
13897 end Make_Array_Comparison_Op;
13898
13899 ---------------------------
13900 -- Make_Boolean_Array_Op --
13901 ---------------------------
13902
13903 -- For logical operations on boolean arrays, expand in line the following,
13904 -- replacing 'and' with 'or' or 'xor' where needed:
13905
13906 -- function Annn (A : typ; B: typ) return typ is
13907 -- C : typ;
13908 -- begin
13909 -- for J in A'range loop
13910 -- C (J) := A (J) op B (J);
13911 -- end loop;
13912 -- return C;
13913 -- end Annn;
13914
13915 -- Here typ is the boolean array type
13916
13917 function Make_Boolean_Array_Op
13918 (Typ : Entity_Id;
13919 N : Node_Id) return Node_Id
13920 is
13921 Loc : constant Source_Ptr := Sloc (N);
13922
13923 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
13924 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
13925 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
13926
13927 C : Entity_Id;
13928
13929 A_J : Node_Id;
13930 B_J : Node_Id;
13931 C_J : Node_Id;
13932 Op : Node_Id;
13933
13934 Formals : List_Id;
13935 Func_Name : Entity_Id;
13936 Func_Body : Node_Id;
13937 Loop_Statement : Node_Id;
13938
13939 begin
13940 C := Make_Defining_Identifier (Loc, Name_uC);
13941
13942 A_J :=
13943 Make_Indexed_Component (Loc,
13944 Prefix => New_Occurrence_Of (A, Loc),
13945 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13946
13947 B_J :=
13948 Make_Indexed_Component (Loc,
13949 Prefix => New_Occurrence_Of (B, Loc),
13950 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13951
13952 C_J :=
13953 Make_Indexed_Component (Loc,
13954 Prefix => New_Occurrence_Of (C, Loc),
13955 Expressions => New_List (New_Occurrence_Of (J, Loc)));
13956
13957 if Nkind (N) = N_Op_And then
13958 Op :=
13959 Make_Op_And (Loc,
13960 Left_Opnd => A_J,
13961 Right_Opnd => B_J);
13962
13963 elsif Nkind (N) = N_Op_Or then
13964 Op :=
13965 Make_Op_Or (Loc,
13966 Left_Opnd => A_J,
13967 Right_Opnd => B_J);
13968
13969 else
13970 Op :=
13971 Make_Op_Xor (Loc,
13972 Left_Opnd => A_J,
13973 Right_Opnd => B_J);
13974 end if;
13975
13976 Loop_Statement :=
13977 Make_Implicit_Loop_Statement (N,
13978 Identifier => Empty,
13979
13980 Iteration_Scheme =>
13981 Make_Iteration_Scheme (Loc,
13982 Loop_Parameter_Specification =>
13983 Make_Loop_Parameter_Specification (Loc,
13984 Defining_Identifier => J,
13985 Discrete_Subtype_Definition =>
13986 Make_Attribute_Reference (Loc,
13987 Prefix => New_Occurrence_Of (A, Loc),
13988 Attribute_Name => Name_Range))),
13989
13990 Statements => New_List (
13991 Make_Assignment_Statement (Loc,
13992 Name => C_J,
13993 Expression => Op)));
13994
13995 Formals := New_List (
13996 Make_Parameter_Specification (Loc,
13997 Defining_Identifier => A,
13998 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
13999
14000 Make_Parameter_Specification (Loc,
14001 Defining_Identifier => B,
14002 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
14003
14004 Func_Name := Make_Temporary (Loc, 'A');
14005 Set_Is_Inlined (Func_Name);
14006
14007 Func_Body :=
14008 Make_Subprogram_Body (Loc,
14009 Specification =>
14010 Make_Function_Specification (Loc,
14011 Defining_Unit_Name => Func_Name,
14012 Parameter_Specifications => Formals,
14013 Result_Definition => New_Occurrence_Of (Typ, Loc)),
14014
14015 Declarations => New_List (
14016 Make_Object_Declaration (Loc,
14017 Defining_Identifier => C,
14018 Object_Definition => New_Occurrence_Of (Typ, Loc))),
14019
14020 Handled_Statement_Sequence =>
14021 Make_Handled_Sequence_Of_Statements (Loc,
14022 Statements => New_List (
14023 Loop_Statement,
14024 Make_Simple_Return_Statement (Loc,
14025 Expression => New_Occurrence_Of (C, Loc)))));
14026
14027 return Func_Body;
14028 end Make_Boolean_Array_Op;
14029
14030 -----------------------------------------
14031 -- Minimized_Eliminated_Overflow_Check --
14032 -----------------------------------------
14033
14034 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
14035 begin
14036 -- The MINIMIZED mode operates in Long_Long_Integer so we cannot use it
14037 -- if the type of the expression is already larger.
14038
14039 return
14040 Is_Signed_Integer_Type (Etype (N))
14041 and then Overflow_Check_Mode in Minimized_Or_Eliminated
14042 and then not (Overflow_Check_Mode = Minimized
14043 and then
14044 Esize (Etype (N)) > Standard_Long_Long_Integer_Size);
14045 end Minimized_Eliminated_Overflow_Check;
14046
14047 ----------------------------
14048 -- Narrow_Large_Operation --
14049 ----------------------------
14050
14051 procedure Narrow_Large_Operation (N : Node_Id) is
14052 Kind : constant Node_Kind := Nkind (N);
14053 Otyp : constant Entity_Id := Etype (N);
14054 In_Rng : constant Boolean := Kind = N_In;
14055 Binary : constant Boolean := Kind in N_Binary_Op or else In_Rng;
14056 Compar : constant Boolean := Kind in N_Op_Compare or else In_Rng;
14057 R : constant Node_Id := Right_Opnd (N);
14058 Typ : constant Entity_Id := Etype (R);
14059 Tsiz : constant Uint := RM_Size (Typ);
14060
14061 -- Local variables
14062
14063 L : Node_Id;
14064 Llo, Lhi : Uint;
14065 Rlo, Rhi : Uint;
14066 Lsiz, Rsiz : Uint;
14067 Nlo, Nhi : Uint;
14068 Nsiz : Uint;
14069 Ntyp : Entity_Id;
14070 Nop : Node_Id;
14071 OK : Boolean;
14072
14073 -- Start of processing for Narrow_Large_Operation
14074
14075 begin
14076 -- First, determine the range of the left operand, if any
14077
14078 if Binary then
14079 L := Left_Opnd (N);
14080 Determine_Range (L, OK, Llo, Lhi, Assume_Valid => True);
14081 if not OK then
14082 return;
14083 end if;
14084
14085 else
14086 L := Empty;
14087 Llo := Uint_0;
14088 Lhi := Uint_0;
14089 end if;
14090
14091 -- Second, determine the range of the right operand, which can itself
14092 -- be a range, in which case we take the lower bound of the low bound
14093 -- and the upper bound of the high bound.
14094
14095 if In_Rng then
14096 declare
14097 Zlo, Zhi : Uint;
14098
14099 begin
14100 Determine_Range
14101 (Low_Bound (R), OK, Rlo, Zhi, Assume_Valid => True);
14102 if not OK then
14103 return;
14104 end if;
14105
14106 Determine_Range
14107 (High_Bound (R), OK, Zlo, Rhi, Assume_Valid => True);
14108 if not OK then
14109 return;
14110 end if;
14111 end;
14112
14113 else
14114 Determine_Range (R, OK, Rlo, Rhi, Assume_Valid => True);
14115 if not OK then
14116 return;
14117 end if;
14118 end if;
14119
14120 -- Then compute a size suitable for each range
14121
14122 if Binary then
14123 Lsiz := Get_Size_For_Range (Llo, Lhi);
14124 else
14125 Lsiz := Uint_0;
14126 end if;
14127
14128 Rsiz := Get_Size_For_Range (Rlo, Rhi);
14129
14130 -- Now compute the size of the narrower type
14131
14132 if Compar then
14133 -- The type must be able to accommodate the operands
14134
14135 Nsiz := UI_Max (Lsiz, Rsiz);
14136
14137 else
14138 -- The type must be able to accommodate the operand(s) and result.
14139
14140 -- Note that Determine_Range typically does not report the bounds of
14141 -- the value as being larger than those of the base type, which means
14142 -- that it does not report overflow (see also Enable_Overflow_Check).
14143
14144 Determine_Range (N, OK, Nlo, Nhi, Assume_Valid => True);
14145 if not OK then
14146 return;
14147 end if;
14148
14149 -- Therefore, if Nsiz is not lower than the size of the original type
14150 -- here, we cannot be sure that the operation does not overflow.
14151
14152 Nsiz := Get_Size_For_Range (Nlo, Nhi);
14153 Nsiz := UI_Max (Nsiz, Lsiz);
14154 Nsiz := UI_Max (Nsiz, Rsiz);
14155 end if;
14156
14157 -- If the size is not lower than the size of the original type, then
14158 -- there is no point in changing the type, except in the case where
14159 -- we can remove a conversion to the original type from an operand.
14160
14161 if Nsiz >= Tsiz
14162 and then not (Binary
14163 and then Nkind (L) = N_Type_Conversion
14164 and then Entity (Subtype_Mark (L)) = Typ)
14165 and then not (Nkind (R) = N_Type_Conversion
14166 and then Entity (Subtype_Mark (R)) = Typ)
14167 then
14168 return;
14169 end if;
14170
14171 -- Now pick the narrower type according to the size. We use the base
14172 -- type instead of the first subtype because operations are done in
14173 -- the base type, so this avoids the need for useless conversions.
14174
14175 if Nsiz <= System_Max_Integer_Size then
14176 Ntyp := Etype (Integer_Type_For (Nsiz, Uns => False));
14177 else
14178 return;
14179 end if;
14180
14181 -- Finally, rewrite the operation in the narrower type, but make sure
14182 -- not to perform name resolution for the operator again.
14183
14184 Nop := New_Op_Node (Kind, Sloc (N));
14185 if Nkind (N) in N_Has_Entity then
14186 Set_Entity (Nop, Entity (N));
14187 end if;
14188
14189 if Binary then
14190 Set_Left_Opnd (Nop, Convert_To (Ntyp, L));
14191 end if;
14192
14193 if In_Rng then
14194 Set_Right_Opnd (Nop,
14195 Make_Range (Sloc (N),
14196 Convert_To (Ntyp, Low_Bound (R)),
14197 Convert_To (Ntyp, High_Bound (R))));
14198 else
14199 Set_Right_Opnd (Nop, Convert_To (Ntyp, R));
14200 end if;
14201
14202 Rewrite (N, Nop);
14203
14204 if Compar then
14205 -- Analyze it with the comparison type and checks suppressed since
14206 -- the conversions of the operands cannot overflow.
14207
14208 Analyze_And_Resolve (N, Otyp, Suppress => Overflow_Check);
14209
14210 else
14211 -- Analyze it with the narrower type and checks suppressed, but only
14212 -- when we are sure that the operation does not overflow, see above.
14213
14214 if Nsiz < Tsiz then
14215 Analyze_And_Resolve (N, Ntyp, Suppress => Overflow_Check);
14216 else
14217 Analyze_And_Resolve (N, Ntyp);
14218 end if;
14219
14220 -- Put back a conversion to the original type
14221
14222 Convert_To_And_Rewrite (Typ, N);
14223 end if;
14224 end Narrow_Large_Operation;
14225
14226 --------------------------------
14227 -- Optimize_Length_Comparison --
14228 --------------------------------
14229
14230 procedure Optimize_Length_Comparison (N : Node_Id) is
14231 Loc : constant Source_Ptr := Sloc (N);
14232 Typ : constant Entity_Id := Etype (N);
14233 Result : Node_Id;
14234
14235 Left : Node_Id;
14236 Right : Node_Id;
14237 -- First and Last attribute reference nodes, which end up as left and
14238 -- right operands of the optimized result.
14239
14240 Is_Zero : Boolean;
14241 -- True for comparison operand of zero
14242
14243 Maybe_Superflat : Boolean;
14244 -- True if we may be in the dynamic superflat case, i.e. Is_Zero is set
14245 -- to false but the comparison operand can be zero at run time. In this
14246 -- case, we normally cannot do anything because the canonical formula of
14247 -- the length is not valid, but there is one exception: when the operand
14248 -- is itself the length of an array with the same bounds as the array on
14249 -- the LHS, we can entirely optimize away the comparison.
14250
14251 Comp : Node_Id;
14252 -- Comparison operand, set only if Is_Zero is false
14253
14254 Ent : array (Pos range 1 .. 2) of Entity_Id := (Empty, Empty);
14255 -- Entities whose length is being compared
14256
14257 Index : array (Pos range 1 .. 2) of Node_Id := (Empty, Empty);
14258 -- Integer_Literal nodes for length attribute expressions, or Empty
14259 -- if there is no such expression present.
14260
14261 Op : Node_Kind := Nkind (N);
14262 -- Kind of comparison operator, gets flipped if operands backwards
14263
14264 function Convert_To_Long_Long_Integer (N : Node_Id) return Node_Id;
14265 -- Given a discrete expression, returns a Long_Long_Integer typed
14266 -- expression representing the underlying value of the expression.
14267 -- This is done with an unchecked conversion to Long_Long_Integer.
14268 -- We use unchecked conversion to handle the enumeration type case.
14269
14270 function Is_Entity_Length (N : Node_Id; Num : Pos) return Boolean;
14271 -- Tests if N is a length attribute applied to a simple entity. If so,
14272 -- returns True, and sets Ent to the entity, and Index to the integer
14273 -- literal provided as an attribute expression, or to Empty if none.
14274 -- Num is the index designating the relevant slot in Ent and Index.
14275 -- Also returns True if the expression is a generated type conversion
14276 -- whose expression is of the desired form. This latter case arises
14277 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
14278 -- to check for being in range, which is not needed in this context.
14279 -- Returns False if neither condition holds.
14280
14281 function Is_Optimizable (N : Node_Id) return Boolean;
14282 -- Tests N to see if it is an optimizable comparison value (defined as
14283 -- constant zero or one, or something else where the value is known to
14284 -- be nonnegative and in the 32-bit range and where the corresponding
14285 -- Length value is also known to be 32 bits). If result is true, sets
14286 -- Is_Zero, Maybe_Superflat and Comp accordingly.
14287
14288 procedure Rewrite_For_Equal_Lengths;
14289 -- Rewrite the comparison of two equal lengths into either True or False
14290
14291 ----------------------------------
14292 -- Convert_To_Long_Long_Integer --
14293 ----------------------------------
14294
14295 function Convert_To_Long_Long_Integer (N : Node_Id) return Node_Id is
14296 begin
14297 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
14298 end Convert_To_Long_Long_Integer;
14299
14300 ----------------------
14301 -- Is_Entity_Length --
14302 ----------------------
14303
14304 function Is_Entity_Length (N : Node_Id; Num : Pos) return Boolean is
14305 begin
14306 if Nkind (N) = N_Attribute_Reference
14307 and then Attribute_Name (N) = Name_Length
14308 and then Is_Entity_Name (Prefix (N))
14309 then
14310 Ent (Num) := Entity (Prefix (N));
14311
14312 if Present (Expressions (N)) then
14313 Index (Num) := First (Expressions (N));
14314 else
14315 Index (Num) := Empty;
14316 end if;
14317
14318 return True;
14319
14320 elsif Nkind (N) = N_Type_Conversion
14321 and then not Comes_From_Source (N)
14322 then
14323 return Is_Entity_Length (Expression (N), Num);
14324
14325 else
14326 return False;
14327 end if;
14328 end Is_Entity_Length;
14329
14330 --------------------
14331 -- Is_Optimizable --
14332 --------------------
14333
14334 function Is_Optimizable (N : Node_Id) return Boolean is
14335 Val : Uint;
14336 OK : Boolean;
14337 Lo : Uint;
14338 Hi : Uint;
14339 Indx : Node_Id;
14340 Dbl : Boolean;
14341 Ityp : Entity_Id;
14342
14343 begin
14344 if Compile_Time_Known_Value (N) then
14345 Val := Expr_Value (N);
14346
14347 if Val = Uint_0 then
14348 Is_Zero := True;
14349 Maybe_Superflat := False;
14350 Comp := Empty;
14351 return True;
14352
14353 elsif Val = Uint_1 then
14354 Is_Zero := False;
14355 Maybe_Superflat := False;
14356 Comp := Empty;
14357 return True;
14358 end if;
14359 end if;
14360
14361 -- Here we have to make sure of being within a 32-bit range (take the
14362 -- full unsigned range so the length of 32-bit arrays is accepted).
14363
14364 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
14365
14366 if not OK
14367 or else Lo < Uint_0
14368 or else Hi > Uint_2 ** 32
14369 then
14370 return False;
14371 end if;
14372
14373 Maybe_Superflat := (Lo = Uint_0);
14374
14375 -- Tests if N is also a length attribute applied to a simple entity
14376
14377 Dbl := Is_Entity_Length (N, 2);
14378
14379 -- We can deal with the superflat case only if N is also a length
14380
14381 if Maybe_Superflat and then not Dbl then
14382 return False;
14383 end if;
14384
14385 -- Comparison value was within range, so now we must check the index
14386 -- value to make sure it is also within 32 bits.
14387
14388 for K in Pos range 1 .. 2 loop
14389 Indx := First_Index (Etype (Ent (K)));
14390
14391 if Present (Index (K)) then
14392 for J in 2 .. UI_To_Int (Intval (Index (K))) loop
14393 Next_Index (Indx);
14394 end loop;
14395 end if;
14396
14397 Ityp := Etype (Indx);
14398
14399 if Esize (Ityp) > 32 then
14400 return False;
14401 end if;
14402
14403 exit when not Dbl;
14404 end loop;
14405
14406 Is_Zero := False;
14407 Comp := N;
14408 return True;
14409 end Is_Optimizable;
14410
14411 -------------------------------
14412 -- Rewrite_For_Equal_Lengths --
14413 -------------------------------
14414
14415 procedure Rewrite_For_Equal_Lengths is
14416 begin
14417 case Op is
14418 when N_Op_Eq
14419 | N_Op_Ge
14420 | N_Op_Le
14421 =>
14422 Rewrite (N,
14423 Convert_To (Typ,
14424 New_Occurrence_Of (Standard_True, Sloc (N))));
14425
14426 when N_Op_Ne
14427 | N_Op_Gt
14428 | N_Op_Lt
14429 =>
14430 Rewrite (N,
14431 Convert_To (Typ,
14432 New_Occurrence_Of (Standard_False, Sloc (N))));
14433
14434 when others =>
14435 raise Program_Error;
14436 end case;
14437
14438 Analyze_And_Resolve (N, Typ);
14439 end Rewrite_For_Equal_Lengths;
14440
14441 -- Start of processing for Optimize_Length_Comparison
14442
14443 begin
14444 -- Nothing to do if not a comparison
14445
14446 if Op not in N_Op_Compare then
14447 return;
14448 end if;
14449
14450 -- Nothing to do if special -gnatd.P debug flag set.
14451
14452 if Debug_Flag_Dot_PP then
14453 return;
14454 end if;
14455
14456 -- Ent'Length op 0/1
14457
14458 if Is_Entity_Length (Left_Opnd (N), 1)
14459 and then Is_Optimizable (Right_Opnd (N))
14460 then
14461 null;
14462
14463 -- 0/1 op Ent'Length
14464
14465 elsif Is_Entity_Length (Right_Opnd (N), 1)
14466 and then Is_Optimizable (Left_Opnd (N))
14467 then
14468 -- Flip comparison to opposite sense
14469
14470 case Op is
14471 when N_Op_Lt => Op := N_Op_Gt;
14472 when N_Op_Le => Op := N_Op_Ge;
14473 when N_Op_Gt => Op := N_Op_Lt;
14474 when N_Op_Ge => Op := N_Op_Le;
14475 when others => null;
14476 end case;
14477
14478 -- Else optimization not possible
14479
14480 else
14481 return;
14482 end if;
14483
14484 -- Fall through if we will do the optimization
14485
14486 -- Cases to handle:
14487
14488 -- X'Length = 0 => X'First > X'Last
14489 -- X'Length = 1 => X'First = X'Last
14490 -- X'Length = n => X'First + (n - 1) = X'Last
14491
14492 -- X'Length /= 0 => X'First <= X'Last
14493 -- X'Length /= 1 => X'First /= X'Last
14494 -- X'Length /= n => X'First + (n - 1) /= X'Last
14495
14496 -- X'Length >= 0 => always true, warn
14497 -- X'Length >= 1 => X'First <= X'Last
14498 -- X'Length >= n => X'First + (n - 1) <= X'Last
14499
14500 -- X'Length > 0 => X'First <= X'Last
14501 -- X'Length > 1 => X'First < X'Last
14502 -- X'Length > n => X'First + (n - 1) < X'Last
14503
14504 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
14505 -- X'Length <= 1 => X'First >= X'Last
14506 -- X'Length <= n => X'First + (n - 1) >= X'Last
14507
14508 -- X'Length < 0 => always false (warn)
14509 -- X'Length < 1 => X'First > X'Last
14510 -- X'Length < n => X'First + (n - 1) > X'Last
14511
14512 -- Note: for the cases of n (not constant 0,1), we require that the
14513 -- corresponding index type be integer or shorter (i.e. not 64-bit),
14514 -- and the same for the comparison value. Then we do the comparison
14515 -- using 64-bit arithmetic (actually long long integer), so that we
14516 -- cannot have overflow intefering with the result.
14517
14518 -- First deal with warning cases
14519
14520 if Is_Zero then
14521 case Op is
14522
14523 -- X'Length >= 0
14524
14525 when N_Op_Ge =>
14526 Rewrite (N,
14527 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
14528 Analyze_And_Resolve (N, Typ);
14529 Warn_On_Known_Condition (N);
14530 return;
14531
14532 -- X'Length < 0
14533
14534 when N_Op_Lt =>
14535 Rewrite (N,
14536 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
14537 Analyze_And_Resolve (N, Typ);
14538 Warn_On_Known_Condition (N);
14539 return;
14540
14541 when N_Op_Le =>
14542 if Constant_Condition_Warnings
14543 and then Comes_From_Source (Original_Node (N))
14544 then
14545 Error_Msg_N ("could replace by ""'=""?c?", N);
14546 end if;
14547
14548 Op := N_Op_Eq;
14549
14550 when others =>
14551 null;
14552 end case;
14553 end if;
14554
14555 -- Build the First reference we will use
14556
14557 Left :=
14558 Make_Attribute_Reference (Loc,
14559 Prefix => New_Occurrence_Of (Ent (1), Loc),
14560 Attribute_Name => Name_First);
14561
14562 if Present (Index (1)) then
14563 Set_Expressions (Left, New_List (New_Copy (Index (1))));
14564 end if;
14565
14566 -- Build the Last reference we will use
14567
14568 Right :=
14569 Make_Attribute_Reference (Loc,
14570 Prefix => New_Occurrence_Of (Ent (1), Loc),
14571 Attribute_Name => Name_Last);
14572
14573 if Present (Index (1)) then
14574 Set_Expressions (Right, New_List (New_Copy (Index (1))));
14575 end if;
14576
14577 -- If general value case, then do the addition of (n - 1), and
14578 -- also add the needed conversions to type Long_Long_Integer.
14579
14580 -- If n = Y'Length, we rewrite X'First + (n - 1) op X'Last into:
14581
14582 -- Y'Last + (X'First - Y'First) op X'Last
14583
14584 -- in the hope that X'First - Y'First can be computed statically.
14585
14586 if Present (Comp) then
14587 if Present (Ent (2)) then
14588 declare
14589 Y_First : constant Node_Id :=
14590 Make_Attribute_Reference (Loc,
14591 Prefix => New_Occurrence_Of (Ent (2), Loc),
14592 Attribute_Name => Name_First);
14593 Y_Last : constant Node_Id :=
14594 Make_Attribute_Reference (Loc,
14595 Prefix => New_Occurrence_Of (Ent (2), Loc),
14596 Attribute_Name => Name_Last);
14597 R : Compare_Result;
14598
14599 begin
14600 if Present (Index (2)) then
14601 Set_Expressions (Y_First, New_List (New_Copy (Index (2))));
14602 Set_Expressions (Y_Last, New_List (New_Copy (Index (2))));
14603 end if;
14604
14605 Analyze (Left);
14606 Analyze (Y_First);
14607
14608 -- If X'First = Y'First, simplify the above formula into a
14609 -- direct comparison of Y'Last and X'Last.
14610
14611 R := Compile_Time_Compare (Left, Y_First, Assume_Valid => True);
14612
14613 if R = EQ then
14614 Analyze (Right);
14615 Analyze (Y_Last);
14616
14617 R := Compile_Time_Compare
14618 (Right, Y_Last, Assume_Valid => True);
14619
14620 -- If the pairs of attributes are equal, we are done
14621
14622 if R = EQ then
14623 Rewrite_For_Equal_Lengths;
14624 return;
14625 end if;
14626
14627 -- If the base types are different, convert both operands to
14628 -- Long_Long_Integer, else compare them directly.
14629
14630 if Base_Type (Etype (Right)) /= Base_Type (Etype (Y_Last))
14631 then
14632 Left := Convert_To_Long_Long_Integer (Y_Last);
14633 else
14634 Left := Y_Last;
14635 Comp := Empty;
14636 end if;
14637
14638 -- Otherwise, use the above formula as-is
14639
14640 else
14641 Left :=
14642 Make_Op_Add (Loc,
14643 Left_Opnd =>
14644 Convert_To_Long_Long_Integer (Y_Last),
14645 Right_Opnd =>
14646 Make_Op_Subtract (Loc,
14647 Left_Opnd =>
14648 Convert_To_Long_Long_Integer (Left),
14649 Right_Opnd =>
14650 Convert_To_Long_Long_Integer (Y_First)));
14651 end if;
14652 end;
14653
14654 -- General value case
14655
14656 else
14657 Left :=
14658 Make_Op_Add (Loc,
14659 Left_Opnd => Convert_To_Long_Long_Integer (Left),
14660 Right_Opnd =>
14661 Make_Op_Subtract (Loc,
14662 Left_Opnd => Convert_To_Long_Long_Integer (Comp),
14663 Right_Opnd => Make_Integer_Literal (Loc, 1)));
14664 end if;
14665 end if;
14666
14667 -- We cannot do anything in the superflat case past this point
14668
14669 if Maybe_Superflat then
14670 return;
14671 end if;
14672
14673 -- If general operand, convert Last reference to Long_Long_Integer
14674
14675 if Present (Comp) then
14676 Right := Convert_To_Long_Long_Integer (Right);
14677 end if;
14678
14679 -- Check for cases to optimize
14680
14681 -- X'Length = 0 => X'First > X'Last
14682 -- X'Length < 1 => X'First > X'Last
14683 -- X'Length < n => X'First + (n - 1) > X'Last
14684
14685 if (Is_Zero and then Op = N_Op_Eq)
14686 or else (not Is_Zero and then Op = N_Op_Lt)
14687 then
14688 Result :=
14689 Make_Op_Gt (Loc,
14690 Left_Opnd => Left,
14691 Right_Opnd => Right);
14692
14693 -- X'Length = 1 => X'First = X'Last
14694 -- X'Length = n => X'First + (n - 1) = X'Last
14695
14696 elsif not Is_Zero and then Op = N_Op_Eq then
14697 Result :=
14698 Make_Op_Eq (Loc,
14699 Left_Opnd => Left,
14700 Right_Opnd => Right);
14701
14702 -- X'Length /= 0 => X'First <= X'Last
14703 -- X'Length > 0 => X'First <= X'Last
14704
14705 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
14706 Result :=
14707 Make_Op_Le (Loc,
14708 Left_Opnd => Left,
14709 Right_Opnd => Right);
14710
14711 -- X'Length /= 1 => X'First /= X'Last
14712 -- X'Length /= n => X'First + (n - 1) /= X'Last
14713
14714 elsif not Is_Zero and then Op = N_Op_Ne then
14715 Result :=
14716 Make_Op_Ne (Loc,
14717 Left_Opnd => Left,
14718 Right_Opnd => Right);
14719
14720 -- X'Length >= 1 => X'First <= X'Last
14721 -- X'Length >= n => X'First + (n - 1) <= X'Last
14722
14723 elsif not Is_Zero and then Op = N_Op_Ge then
14724 Result :=
14725 Make_Op_Le (Loc,
14726 Left_Opnd => Left,
14727 Right_Opnd => Right);
14728
14729 -- X'Length > 1 => X'First < X'Last
14730 -- X'Length > n => X'First + (n = 1) < X'Last
14731
14732 elsif not Is_Zero and then Op = N_Op_Gt then
14733 Result :=
14734 Make_Op_Lt (Loc,
14735 Left_Opnd => Left,
14736 Right_Opnd => Right);
14737
14738 -- X'Length <= 1 => X'First >= X'Last
14739 -- X'Length <= n => X'First + (n - 1) >= X'Last
14740
14741 elsif not Is_Zero and then Op = N_Op_Le then
14742 Result :=
14743 Make_Op_Ge (Loc,
14744 Left_Opnd => Left,
14745 Right_Opnd => Right);
14746
14747 -- Should not happen at this stage
14748
14749 else
14750 raise Program_Error;
14751 end if;
14752
14753 -- Rewrite and finish up (we can suppress overflow checks, see above)
14754
14755 Rewrite (N, Result);
14756 Analyze_And_Resolve (N, Typ, Suppress => Overflow_Check);
14757 end Optimize_Length_Comparison;
14758
14759 --------------------------------------
14760 -- Process_Transients_In_Expression --
14761 --------------------------------------
14762
14763 procedure Process_Transients_In_Expression
14764 (Expr : Node_Id;
14765 Stmts : List_Id)
14766 is
14767 procedure Process_Transient_In_Expression (Obj_Decl : Node_Id);
14768 -- Process the object whose declaration Obj_Decl is present in Stmts
14769
14770 -------------------------------------
14771 -- Process_Transient_In_Expression --
14772 -------------------------------------
14773
14774 procedure Process_Transient_In_Expression (Obj_Decl : Node_Id) is
14775 Loc : constant Source_Ptr := Sloc (Obj_Decl);
14776 Obj_Id : constant Entity_Id := Defining_Identifier (Obj_Decl);
14777
14778 Hook_Context : constant Node_Id := Find_Hook_Context (Expr);
14779 -- The node after which to insert deferred finalization actions. This
14780 -- is usually the innermost enclosing non-transient construct.
14781
14782 Fin_Context : Node_Id;
14783 -- The node after which to insert the finalization actions
14784
14785 Master_Node_Decl : Node_Id;
14786 Master_Node_Id : Entity_Id;
14787 -- Declaration and entity of the Master_Node respectively
14788
14789 begin
14790 -- When the context is a Boolean evaluation, all three nodes capture
14791 -- the result of their computation in a local temporary:
14792
14793 -- do
14794 -- Trans_Id : Ctrl_Typ := ...;
14795 -- Result : constant Boolean := ... Trans_Id ...;
14796 -- <finalize Trans_Id>
14797 -- in Result end;
14798
14799 -- As a result, the finalization of any transient objects can take
14800 -- place just after the result is captured, except for the case of
14801 -- conditional expressions in a simple return statement because the
14802 -- return statement will be distributed into dependent expressions
14803 -- (see the special handling of simple return statements below).
14804
14805 -- ??? could this be extended to elementary types?
14806
14807 if Is_Boolean_Type (Etype (Expr))
14808 and then
14809 (Nkind (Expr) = N_Expression_With_Actions
14810 or else Nkind (Parent (Expr)) /= N_Simple_Return_Statement)
14811 then
14812 Fin_Context := Last (Stmts);
14813
14814 -- Otherwise the immediate context may not be safe enough to carry
14815 -- out transient object finalization due to aliasing and nesting of
14816 -- constructs. Insert calls to [Deep_]Finalize after the innermost
14817 -- enclosing non-transient construct.
14818
14819 else
14820 Fin_Context := Hook_Context;
14821 end if;
14822
14823 -- Create the declaration of the Master_Node for the object and
14824 -- insert it before the context. It will later be picked up by
14825 -- the general finalization mechanism (see Build_Finalizer).
14826
14827 Master_Node_Id := Make_Temporary (Loc, 'N');
14828 Master_Node_Decl :=
14829 Make_Master_Node_Declaration (Loc, Master_Node_Id, Obj_Id);
14830 Insert_Action (Hook_Context, Master_Node_Decl);
14831
14832 -- Generate the attachment of the object to the Master_Node
14833
14834 Attach_Object_To_Master_Node (Obj_Decl, Master_Node_Id);
14835
14836 -- When the node is part of a return statement, there is no need
14837 -- to insert a finalization call, as the general finalization
14838 -- mechanism (see Build_Finalizer) would take care of the master
14839 -- on subprogram exit. Note that it would also be impossible to
14840 -- insert the finalization call after the return statement as
14841 -- this will render it unreachable.
14842
14843 if Nkind (Fin_Context) = N_Simple_Return_Statement
14844 or else Nkind (Parent (Expr)) = N_Simple_Return_Statement
14845 then
14846 null;
14847
14848 -- Finalize the object after the context has been evaluated
14849
14850 -- Note that the node returned by Find_Hook_Context above may be an
14851 -- operator, which is not a list member. We must locate the proper
14852 -- node in the tree after which to insert the finalization call.
14853
14854 else
14855 while not Is_List_Member (Fin_Context) loop
14856 Fin_Context := Parent (Fin_Context);
14857 end loop;
14858
14859 pragma Assert (Present (Fin_Context));
14860
14861 Insert_Action_After (Fin_Context,
14862 Make_Finalize_Call_For_Node (Loc, Master_Node_Id));
14863 end if;
14864
14865 -- Mark the transient object to avoid double finalization
14866
14867 Set_Is_Finalized_Transient (Obj_Id);
14868 end Process_Transient_In_Expression;
14869
14870 -- Local variables
14871
14872 Decl : Node_Id;
14873
14874 -- Start of processing for Process_Transients_In_Expression
14875
14876 begin
14877 pragma Assert (Nkind (Expr) in N_Case_Expression
14878 | N_Expression_With_Actions
14879 | N_If_Expression);
14880
14881 Decl := First (Stmts);
14882 while Present (Decl) loop
14883 if Nkind (Decl) = N_Object_Declaration
14884 and then Is_Finalizable_Transient (Decl, Expr)
14885 then
14886 Process_Transient_In_Expression (Decl);
14887 end if;
14888
14889 Next (Decl);
14890 end loop;
14891 end Process_Transients_In_Expression;
14892
14893 ------------------------
14894 -- Rewrite_Comparison --
14895 ------------------------
14896
14897 procedure Rewrite_Comparison (N : Node_Id) is
14898 Typ : constant Entity_Id := Etype (N);
14899
14900 False_Result : Boolean;
14901 True_Result : Boolean;
14902
14903 begin
14904 if Nkind (N) = N_Type_Conversion then
14905 Rewrite_Comparison (Expression (N));
14906 return;
14907
14908 elsif Nkind (N) not in N_Op_Compare then
14909 return;
14910 end if;
14911
14912 -- If both operands are static, then the comparison has been already
14913 -- folded in evaluation.
14914
14915 pragma Assert
14916 (not Is_Static_Expression (Left_Opnd (N))
14917 or else
14918 not Is_Static_Expression (Right_Opnd (N)));
14919
14920 -- Determine the potential outcome of the comparison assuming that the
14921 -- operands are valid and emit a warning when the comparison evaluates
14922 -- to True or False only in the presence of invalid values.
14923
14924 Warn_On_Constant_Valid_Condition (N);
14925
14926 -- Determine the potential outcome of the comparison assuming that the
14927 -- operands are not valid.
14928
14929 Test_Comparison
14930 (Op => N,
14931 Assume_Valid => False,
14932 True_Result => True_Result,
14933 False_Result => False_Result);
14934
14935 -- The outcome is a decisive False or True, rewrite the operator into a
14936 -- non-static literal.
14937
14938 if False_Result or True_Result then
14939 Rewrite (N,
14940 Convert_To (Typ,
14941 New_Occurrence_Of (Boolean_Literals (True_Result), Sloc (N))));
14942
14943 Analyze_And_Resolve (N, Typ);
14944 Set_Is_Static_Expression (N, False);
14945 Warn_On_Known_Condition (N);
14946 end if;
14947 end Rewrite_Comparison;
14948
14949 ----------------------------
14950 -- Safe_In_Place_Array_Op --
14951 ----------------------------
14952
14953 function Safe_In_Place_Array_Op
14954 (Lhs : Node_Id;
14955 Op1 : Node_Id;
14956 Op2 : Node_Id) return Boolean
14957 is
14958 Target : Entity_Id;
14959
14960 function Is_Safe_Operand (Op : Node_Id) return Boolean;
14961 -- Operand is safe if it cannot overlap part of the target of the
14962 -- operation. If the operand and the target are identical, the operand
14963 -- is safe. The operand can be empty in the case of negation.
14964
14965 function Is_Unaliased (N : Node_Id) return Boolean;
14966 -- Check that N is a stand-alone entity
14967
14968 ------------------
14969 -- Is_Unaliased --
14970 ------------------
14971
14972 function Is_Unaliased (N : Node_Id) return Boolean is
14973 begin
14974 return
14975 Is_Entity_Name (N)
14976 and then No (Address_Clause (Entity (N)))
14977 and then No (Renamed_Object (Entity (N)));
14978 end Is_Unaliased;
14979
14980 ---------------------
14981 -- Is_Safe_Operand --
14982 ---------------------
14983
14984 function Is_Safe_Operand (Op : Node_Id) return Boolean is
14985 begin
14986 if No (Op) then
14987 return True;
14988
14989 elsif Is_Entity_Name (Op) then
14990 return Is_Unaliased (Op);
14991
14992 elsif Nkind (Op) in N_Indexed_Component | N_Selected_Component then
14993 return Is_Unaliased (Prefix (Op));
14994
14995 elsif Nkind (Op) = N_Slice then
14996 return
14997 Is_Unaliased (Prefix (Op))
14998 and then Entity (Prefix (Op)) /= Target;
14999
15000 elsif Nkind (Op) = N_Op_Not then
15001 return Is_Safe_Operand (Right_Opnd (Op));
15002
15003 else
15004 return False;
15005 end if;
15006 end Is_Safe_Operand;
15007
15008 -- Start of processing for Safe_In_Place_Array_Op
15009
15010 begin
15011 -- Skip this processing if the component size is different from system
15012 -- storage unit (since at least for NOT this would cause problems).
15013
15014 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
15015 return False;
15016
15017 -- Cannot do in place stuff if non-standard Boolean representation
15018
15019 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
15020 return False;
15021
15022 elsif not Is_Unaliased (Lhs) then
15023 return False;
15024
15025 else
15026 Target := Entity (Lhs);
15027 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
15028 end if;
15029 end Safe_In_Place_Array_Op;
15030
15031 -----------------------
15032 -- Tagged_Membership --
15033 -----------------------
15034
15035 -- There are two different cases to consider depending on whether the right
15036 -- operand is a class-wide type or not. If not we just compare the actual
15037 -- tag of the left expr to the target type tag:
15038 --
15039 -- Left_Expr.Tag = Right_Type'Tag;
15040 --
15041 -- If it is a class-wide type we use the RT function CW_Membership which is
15042 -- usually implemented by looking in the ancestor tables contained in the
15043 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
15044
15045 -- In both cases if Left_Expr is an access type, we first check whether it
15046 -- is null.
15047
15048 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
15049 -- function IW_Membership which is usually implemented by looking in the
15050 -- table of abstract interface types plus the ancestor table contained in
15051 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
15052
15053 procedure Tagged_Membership
15054 (N : Node_Id;
15055 SCIL_Node : out Node_Id;
15056 Result : out Node_Id)
15057 is
15058 Left : constant Node_Id := Left_Opnd (N);
15059 Right : constant Node_Id := Right_Opnd (N);
15060 Loc : constant Source_Ptr := Sloc (N);
15061
15062 -- Handle entities from the limited view
15063
15064 Orig_Right_Type : constant Entity_Id :=
15065 Base_Type (Available_View (Etype (Right)));
15066
15067 Full_R_Typ : Entity_Id;
15068 Left_Type : Entity_Id := Base_Type (Available_View (Etype (Left)));
15069 Right_Type : Entity_Id := Orig_Right_Type;
15070 Obj_Tag : Node_Id;
15071
15072 begin
15073 SCIL_Node := Empty;
15074
15075 -- We have to examine the corresponding record type when dealing with
15076 -- protected types instead of the original, unexpanded, type.
15077
15078 if Ekind (Right_Type) = E_Protected_Type then
15079 Right_Type := Corresponding_Record_Type (Right_Type);
15080 end if;
15081
15082 if Ekind (Left_Type) = E_Protected_Type then
15083 Left_Type := Corresponding_Record_Type (Left_Type);
15084 end if;
15085
15086 -- In the case where the type is an access type, the test is applied
15087 -- using the designated types (needed in Ada 2012 for implicit anonymous
15088 -- access conversions, for AI05-0149).
15089
15090 if Is_Access_Type (Right_Type) then
15091 Left_Type := Designated_Type (Left_Type);
15092 Right_Type := Designated_Type (Right_Type);
15093 end if;
15094
15095 if Is_Class_Wide_Type (Left_Type) then
15096 Left_Type := Root_Type (Left_Type);
15097 end if;
15098
15099 if Is_Class_Wide_Type (Right_Type) then
15100 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
15101 else
15102 Full_R_Typ := Underlying_Type (Right_Type);
15103 end if;
15104
15105 Obj_Tag :=
15106 Make_Selected_Component (Loc,
15107 Prefix => Relocate_Node (Left),
15108 Selector_Name =>
15109 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
15110
15111 if Is_Class_Wide_Type (Right_Type) then
15112
15113 -- No need to issue a run-time check if we statically know that the
15114 -- result of this membership test is always true. For example,
15115 -- considering the following declarations:
15116
15117 -- type Iface is interface;
15118 -- type T is tagged null record;
15119 -- type DT is new T and Iface with null record;
15120
15121 -- Obj1 : T;
15122 -- Obj2 : DT;
15123
15124 -- These membership tests are always true:
15125
15126 -- Obj1 in T'Class
15127 -- Obj2 in T'Class;
15128 -- Obj2 in Iface'Class;
15129
15130 -- We do not need to handle cases where the membership is illegal.
15131 -- For example:
15132
15133 -- Obj1 in DT'Class; -- Compile time error
15134 -- Obj1 in Iface'Class; -- Compile time error
15135
15136 if not Is_Interface (Left_Type)
15137 and then not Is_Class_Wide_Type (Left_Type)
15138 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
15139 Use_Full_View => True)
15140 or else (Is_Interface (Etype (Right_Type))
15141 and then Interface_Present_In_Ancestor
15142 (Typ => Left_Type,
15143 Iface => Etype (Right_Type))))
15144 then
15145 Result := New_Occurrence_Of (Standard_True, Loc);
15146 return;
15147 end if;
15148
15149 -- Ada 2005 (AI-251): Class-wide applied to interfaces
15150
15151 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
15152
15153 -- Support to: "Iface_CW_Typ in Typ'Class"
15154
15155 or else Is_Interface (Left_Type)
15156 then
15157 -- Issue error if IW_Membership operation not available in a
15158 -- configurable run-time setting.
15159
15160 if not RTE_Available (RE_IW_Membership) then
15161 Error_Msg_CRT
15162 ("dynamic membership test on interface types", N);
15163 Result := Empty;
15164 return;
15165 end if;
15166
15167 Result :=
15168 Make_Function_Call (Loc,
15169 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
15170 Parameter_Associations => New_List (
15171 Make_Attribute_Reference (Loc,
15172 Prefix => Obj_Tag,
15173 Attribute_Name => Name_Address),
15174 New_Occurrence_Of (
15175 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
15176 Loc)));
15177
15178 -- Ada 95: Normal case
15179
15180 else
15181 -- Issue error if CW_Membership operation not available in a
15182 -- configurable run-time setting.
15183
15184 if not RTE_Available (RE_CW_Membership) then
15185 Error_Msg_CRT
15186 ("dynamic membership test on tagged types", N);
15187 Result := Empty;
15188 return;
15189 end if;
15190
15191 Result :=
15192 Make_Function_Call (Loc,
15193 Name => New_Occurrence_Of (RTE (RE_CW_Membership), Loc),
15194 Parameter_Associations => New_List (
15195 Obj_Tag,
15196 New_Occurrence_Of (
15197 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
15198 Loc)));
15199
15200 -- Generate the SCIL node for this class-wide membership test.
15201
15202 if Generate_SCIL then
15203 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
15204 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
15205 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
15206 end if;
15207 end if;
15208
15209 -- Right_Type is not a class-wide type
15210
15211 else
15212 -- No need to check the tag of the object if Right_Typ is abstract
15213
15214 if Is_Abstract_Type (Right_Type) then
15215 Result := New_Occurrence_Of (Standard_False, Loc);
15216
15217 else
15218 Result :=
15219 Make_Op_Eq (Loc,
15220 Left_Opnd => Obj_Tag,
15221 Right_Opnd =>
15222 New_Occurrence_Of
15223 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
15224 end if;
15225 end if;
15226
15227 -- if Left is an access object then generate test of the form:
15228 -- * if Right_Type excludes null: Left /= null and then ...
15229 -- * if Right_Type includes null: Left = null or else ...
15230
15231 if Is_Access_Type (Orig_Right_Type) then
15232 if Can_Never_Be_Null (Orig_Right_Type) then
15233 Result := Make_And_Then (Loc,
15234 Left_Opnd =>
15235 Make_Op_Ne (Loc,
15236 Left_Opnd => Left,
15237 Right_Opnd => Make_Null (Loc)),
15238 Right_Opnd => Result);
15239
15240 else
15241 Result := Make_Or_Else (Loc,
15242 Left_Opnd =>
15243 Make_Op_Eq (Loc,
15244 Left_Opnd => Left,
15245 Right_Opnd => Make_Null (Loc)),
15246 Right_Opnd => Result);
15247 end if;
15248 end if;
15249 end Tagged_Membership;
15250
15251 ------------------------------
15252 -- Unary_Op_Validity_Checks --
15253 ------------------------------
15254
15255 procedure Unary_Op_Validity_Checks (N : Node_Id) is
15256 begin
15257 if Validity_Checks_On and Validity_Check_Operands then
15258 Ensure_Valid (Right_Opnd (N));
15259 end if;
15260 end Unary_Op_Validity_Checks;
15261
15262 end Exp_Ch4;