[multiple changes]
[gcc.git] / gcc / ada / exp_ch4.adb
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-2012, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Fixd; use Exp_Fixd;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Exp_VFpt; use Exp_VFpt;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
48 with Lib; use Lib;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
52 with Opt; use Opt;
53 with Par_SCO; use Par_SCO;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with SCIL_LL; use SCIL_LL;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Ttypes; use Ttypes;
75 with Uintp; use Uintp;
76 with Urealp; use Urealp;
77 with Validsw; use Validsw;
78
79 package body Exp_Ch4 is
80
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
84
85 procedure Binary_Op_Validity_Checks (N : Node_Id);
86 pragma Inline (Binary_Op_Validity_Checks);
87 -- Performs validity checks for a binary operator
88
89 procedure Build_Boolean_Array_Proc_Call
90 (N : Node_Id;
91 Op1 : Node_Id;
92 Op2 : Node_Id);
93 -- If a boolean array assignment can be done in place, build call to
94 -- corresponding library procedure.
95
96 function Current_Anonymous_Master return Entity_Id;
97 -- Return the entity of the heterogeneous finalization master belonging to
98 -- the current unit (either function, package or procedure). This master
99 -- services all anonymous access-to-controlled types. If the current unit
100 -- does not have such master, create one.
101
102 procedure Displace_Allocator_Pointer (N : Node_Id);
103 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
104 -- Expand_Allocator_Expression. Allocating class-wide interface objects
105 -- this routine displaces the pointer to the allocated object to reference
106 -- the component referencing the corresponding secondary dispatch table.
107
108 procedure Expand_Allocator_Expression (N : Node_Id);
109 -- Subsidiary to Expand_N_Allocator, for the case when the expression
110 -- is a qualified expression or an aggregate.
111
112 procedure Expand_Array_Comparison (N : Node_Id);
113 -- This routine handles expansion of the comparison operators (N_Op_Lt,
114 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
115 -- code for these operators is similar, differing only in the details of
116 -- the actual comparison call that is made. Special processing (call a
117 -- run-time routine)
118
119 function Expand_Array_Equality
120 (Nod : Node_Id;
121 Lhs : Node_Id;
122 Rhs : Node_Id;
123 Bodies : List_Id;
124 Typ : Entity_Id) return Node_Id;
125 -- Expand an array equality into a call to a function implementing this
126 -- equality, and a call to it. Loc is the location for the generated nodes.
127 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
128 -- on which to attach bodies of local functions that are created in the
129 -- process. It is the responsibility of the caller to insert those bodies
130 -- at the right place. Nod provides the Sloc value for the generated code.
131 -- Normally the types used for the generated equality routine are taken
132 -- from Lhs and Rhs. However, in some situations of generated code, the
133 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
134 -- the type to be used for the formal parameters.
135
136 procedure Expand_Boolean_Operator (N : Node_Id);
137 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
138 -- case of array type arguments.
139
140 procedure Expand_Short_Circuit_Operator (N : Node_Id);
141 -- Common expansion processing for short-circuit boolean operators
142
143 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
144 -- Deal with comparison in Minimize/Eliminate overflow mode. This is where
145 -- we allow comparison of "out of range" values.
146
147 function Expand_Composite_Equality
148 (Nod : Node_Id;
149 Typ : Entity_Id;
150 Lhs : Node_Id;
151 Rhs : Node_Id;
152 Bodies : List_Id) return Node_Id;
153 -- Local recursive function used to expand equality for nested composite
154 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
155 -- to attach bodies of local functions that are created in the process.
156 -- It is the responsibility of the caller to insert those bodies at the
157 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
158 -- are the left and right sides for the comparison, and Typ is the type of
159 -- the objects to compare.
160
161 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
162 -- Routine to expand concatenation of a sequence of two or more operands
163 -- (in the list Operands) and replace node Cnode with the result of the
164 -- concatenation. The operands can be of any appropriate type, and can
165 -- include both arrays and singleton elements.
166
167 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
168 -- N is an N_In membership test mode, with the overflow check mode
169 -- set to Minimized or Eliminated, and the type of the left operand
170 -- is a signed integer type. This is a case where top level processing
171 -- is required to handle overflow checks in subtrees.
172
173 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
174 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
175 -- fixed. We do not have such a type at runtime, so the purpose of this
176 -- routine is to find the real type by looking up the tree. We also
177 -- determine if the operation must be rounded.
178
179 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
180 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
181 -- discriminants if it has a constrained nominal type, unless the object
182 -- is a component of an enclosing Unchecked_Union object that is subject
183 -- to a per-object constraint and the enclosing object lacks inferable
184 -- discriminants.
185 --
186 -- An expression of an Unchecked_Union type has inferable discriminants
187 -- if it is either a name of an object with inferable discriminants or a
188 -- qualified expression whose subtype mark denotes a constrained subtype.
189
190 procedure Insert_Dereference_Action (N : Node_Id);
191 -- N is an expression whose type is an access. When the type of the
192 -- associated storage pool is derived from Checked_Pool, generate a
193 -- call to the 'Dereference' primitive operation.
194
195 function Make_Array_Comparison_Op
196 (Typ : Entity_Id;
197 Nod : Node_Id) return Node_Id;
198 -- Comparisons between arrays are expanded in line. This function produces
199 -- the body of the implementation of (a > b), where a and b are one-
200 -- dimensional arrays of some discrete type. The original node is then
201 -- expanded into the appropriate call to this function. Nod provides the
202 -- Sloc value for the generated code.
203
204 function Make_Boolean_Array_Op
205 (Typ : Entity_Id;
206 N : Node_Id) return Node_Id;
207 -- Boolean operations on boolean arrays are expanded in line. This function
208 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
209 -- b). It is used only the normal case and not the packed case. The type
210 -- involved, Typ, is the Boolean array type, and the logical operations in
211 -- the body are simple boolean operations. Note that Typ is always a
212 -- constrained type (the caller has ensured this by using
213 -- Convert_To_Actual_Subtype if necessary).
214
215 procedure Optimize_Length_Comparison (N : Node_Id);
216 -- Given an expression, if it is of the form X'Length op N (or the other
217 -- way round), where N is known at compile time to be 0 or 1, and X is a
218 -- simple entity, and op is a comparison operator, optimizes it into a
219 -- comparison of First and Last.
220
221 procedure Rewrite_Comparison (N : Node_Id);
222 -- If N is the node for a comparison whose outcome can be determined at
223 -- compile time, then the node N can be rewritten with True or False. If
224 -- the outcome cannot be determined at compile time, the call has no
225 -- effect. If N is a type conversion, then this processing is applied to
226 -- its expression. If N is neither comparison nor a type conversion, the
227 -- call has no effect.
228
229 procedure Tagged_Membership
230 (N : Node_Id;
231 SCIL_Node : out Node_Id;
232 Result : out Node_Id);
233 -- Construct the expression corresponding to the tagged membership test.
234 -- Deals with a second operand being (or not) a class-wide type.
235
236 function Safe_In_Place_Array_Op
237 (Lhs : Node_Id;
238 Op1 : Node_Id;
239 Op2 : Node_Id) return Boolean;
240 -- In the context of an assignment, where the right-hand side is a boolean
241 -- operation on arrays, check whether operation can be performed in place.
242
243 procedure Unary_Op_Validity_Checks (N : Node_Id);
244 pragma Inline (Unary_Op_Validity_Checks);
245 -- Performs validity checks for a unary operator
246
247 -------------------------------
248 -- Binary_Op_Validity_Checks --
249 -------------------------------
250
251 procedure Binary_Op_Validity_Checks (N : Node_Id) is
252 begin
253 if Validity_Checks_On and Validity_Check_Operands then
254 Ensure_Valid (Left_Opnd (N));
255 Ensure_Valid (Right_Opnd (N));
256 end if;
257 end Binary_Op_Validity_Checks;
258
259 ------------------------------------
260 -- Build_Boolean_Array_Proc_Call --
261 ------------------------------------
262
263 procedure Build_Boolean_Array_Proc_Call
264 (N : Node_Id;
265 Op1 : Node_Id;
266 Op2 : Node_Id)
267 is
268 Loc : constant Source_Ptr := Sloc (N);
269 Kind : constant Node_Kind := Nkind (Expression (N));
270 Target : constant Node_Id :=
271 Make_Attribute_Reference (Loc,
272 Prefix => Name (N),
273 Attribute_Name => Name_Address);
274
275 Arg1 : Node_Id := Op1;
276 Arg2 : Node_Id := Op2;
277 Call_Node : Node_Id;
278 Proc_Name : Entity_Id;
279
280 begin
281 if Kind = N_Op_Not then
282 if Nkind (Op1) in N_Binary_Op then
283
284 -- Use negated version of the binary operators
285
286 if Nkind (Op1) = N_Op_And then
287 Proc_Name := RTE (RE_Vector_Nand);
288
289 elsif Nkind (Op1) = N_Op_Or then
290 Proc_Name := RTE (RE_Vector_Nor);
291
292 else pragma Assert (Nkind (Op1) = N_Op_Xor);
293 Proc_Name := RTE (RE_Vector_Xor);
294 end if;
295
296 Call_Node :=
297 Make_Procedure_Call_Statement (Loc,
298 Name => New_Occurrence_Of (Proc_Name, Loc),
299
300 Parameter_Associations => New_List (
301 Target,
302 Make_Attribute_Reference (Loc,
303 Prefix => Left_Opnd (Op1),
304 Attribute_Name => Name_Address),
305
306 Make_Attribute_Reference (Loc,
307 Prefix => Right_Opnd (Op1),
308 Attribute_Name => Name_Address),
309
310 Make_Attribute_Reference (Loc,
311 Prefix => Left_Opnd (Op1),
312 Attribute_Name => Name_Length)));
313
314 else
315 Proc_Name := RTE (RE_Vector_Not);
316
317 Call_Node :=
318 Make_Procedure_Call_Statement (Loc,
319 Name => New_Occurrence_Of (Proc_Name, Loc),
320 Parameter_Associations => New_List (
321 Target,
322
323 Make_Attribute_Reference (Loc,
324 Prefix => Op1,
325 Attribute_Name => Name_Address),
326
327 Make_Attribute_Reference (Loc,
328 Prefix => Op1,
329 Attribute_Name => Name_Length)));
330 end if;
331
332 else
333 -- We use the following equivalences:
334
335 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
336 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
337 -- (not X) xor (not Y) = X xor Y
338 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
339
340 if Nkind (Op1) = N_Op_Not then
341 Arg1 := Right_Opnd (Op1);
342 Arg2 := Right_Opnd (Op2);
343 if Kind = N_Op_And then
344 Proc_Name := RTE (RE_Vector_Nor);
345 elsif Kind = N_Op_Or then
346 Proc_Name := RTE (RE_Vector_Nand);
347 else
348 Proc_Name := RTE (RE_Vector_Xor);
349 end if;
350
351 else
352 if Kind = N_Op_And then
353 Proc_Name := RTE (RE_Vector_And);
354 elsif Kind = N_Op_Or then
355 Proc_Name := RTE (RE_Vector_Or);
356 elsif Nkind (Op2) = N_Op_Not then
357 Proc_Name := RTE (RE_Vector_Nxor);
358 Arg2 := Right_Opnd (Op2);
359 else
360 Proc_Name := RTE (RE_Vector_Xor);
361 end if;
362 end if;
363
364 Call_Node :=
365 Make_Procedure_Call_Statement (Loc,
366 Name => New_Occurrence_Of (Proc_Name, Loc),
367 Parameter_Associations => New_List (
368 Target,
369 Make_Attribute_Reference (Loc,
370 Prefix => Arg1,
371 Attribute_Name => Name_Address),
372 Make_Attribute_Reference (Loc,
373 Prefix => Arg2,
374 Attribute_Name => Name_Address),
375 Make_Attribute_Reference (Loc,
376 Prefix => Arg1,
377 Attribute_Name => Name_Length)));
378 end if;
379
380 Rewrite (N, Call_Node);
381 Analyze (N);
382
383 exception
384 when RE_Not_Available =>
385 return;
386 end Build_Boolean_Array_Proc_Call;
387
388 ------------------------------
389 -- Current_Anonymous_Master --
390 ------------------------------
391
392 function Current_Anonymous_Master return Entity_Id is
393 Decls : List_Id;
394 Loc : Source_Ptr;
395 Subp_Body : Node_Id;
396 Unit_Decl : Node_Id;
397 Unit_Id : Entity_Id;
398
399 begin
400 Unit_Id := Cunit_Entity (Current_Sem_Unit);
401
402 -- Find the entity of the current unit
403
404 if Ekind (Unit_Id) = E_Subprogram_Body then
405
406 -- When processing subprogram bodies, the proper scope is always that
407 -- of the spec.
408
409 Subp_Body := Unit_Id;
410 while Present (Subp_Body)
411 and then Nkind (Subp_Body) /= N_Subprogram_Body
412 loop
413 Subp_Body := Parent (Subp_Body);
414 end loop;
415
416 Unit_Id := Corresponding_Spec (Subp_Body);
417 end if;
418
419 Loc := Sloc (Unit_Id);
420 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
421
422 -- Find the declarations list of the current unit
423
424 if Nkind (Unit_Decl) = N_Package_Declaration then
425 Unit_Decl := Specification (Unit_Decl);
426 Decls := Visible_Declarations (Unit_Decl);
427
428 if No (Decls) then
429 Decls := New_List (Make_Null_Statement (Loc));
430 Set_Visible_Declarations (Unit_Decl, Decls);
431
432 elsif Is_Empty_List (Decls) then
433 Append_To (Decls, Make_Null_Statement (Loc));
434 end if;
435
436 else
437 Decls := Declarations (Unit_Decl);
438
439 if No (Decls) then
440 Decls := New_List (Make_Null_Statement (Loc));
441 Set_Declarations (Unit_Decl, Decls);
442
443 elsif Is_Empty_List (Decls) then
444 Append_To (Decls, Make_Null_Statement (Loc));
445 end if;
446 end if;
447
448 -- The current unit has an existing anonymous master, traverse its
449 -- declarations and locate the entity.
450
451 if Has_Anonymous_Master (Unit_Id) then
452 declare
453 Decl : Node_Id;
454 Fin_Mas_Id : Entity_Id;
455
456 begin
457 Decl := First (Decls);
458 while Present (Decl) loop
459
460 -- Look for the first variable in the declarations whole type
461 -- is Finalization_Master.
462
463 if Nkind (Decl) = N_Object_Declaration then
464 Fin_Mas_Id := Defining_Identifier (Decl);
465
466 if Ekind (Fin_Mas_Id) = E_Variable
467 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
468 then
469 return Fin_Mas_Id;
470 end if;
471 end if;
472
473 Next (Decl);
474 end loop;
475
476 -- The master was not found even though the unit was labeled as
477 -- having one.
478
479 raise Program_Error;
480 end;
481
482 -- Create a new anonymous master
483
484 else
485 declare
486 First_Decl : constant Node_Id := First (Decls);
487 Action : Node_Id;
488 Fin_Mas_Id : Entity_Id;
489
490 begin
491 -- Since the master and its associated initialization is inserted
492 -- at top level, use the scope of the unit when analyzing.
493
494 Push_Scope (Unit_Id);
495
496 -- Create the finalization master
497
498 Fin_Mas_Id :=
499 Make_Defining_Identifier (Loc,
500 Chars => New_External_Name (Chars (Unit_Id), "AM"));
501
502 -- Generate:
503 -- <Fin_Mas_Id> : Finalization_Master;
504
505 Action :=
506 Make_Object_Declaration (Loc,
507 Defining_Identifier => Fin_Mas_Id,
508 Object_Definition =>
509 New_Reference_To (RTE (RE_Finalization_Master), Loc));
510
511 Insert_Before_And_Analyze (First_Decl, Action);
512
513 -- Mark the unit to prevent the generation of multiple masters
514
515 Set_Has_Anonymous_Master (Unit_Id);
516
517 -- Do not set the base pool and mode of operation on .NET/JVM
518 -- since those targets do not support pools and all VM masters
519 -- are heterogeneous by default.
520
521 if VM_Target = No_VM then
522
523 -- Generate:
524 -- Set_Base_Pool
525 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
526
527 Action :=
528 Make_Procedure_Call_Statement (Loc,
529 Name =>
530 New_Reference_To (RTE (RE_Set_Base_Pool), Loc),
531
532 Parameter_Associations => New_List (
533 New_Reference_To (Fin_Mas_Id, Loc),
534 Make_Attribute_Reference (Loc,
535 Prefix =>
536 New_Reference_To (RTE (RE_Global_Pool_Object), Loc),
537 Attribute_Name => Name_Unrestricted_Access)));
538
539 Insert_Before_And_Analyze (First_Decl, Action);
540
541 -- Generate:
542 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
543
544 Action :=
545 Make_Procedure_Call_Statement (Loc,
546 Name =>
547 New_Reference_To (RTE (RE_Set_Is_Heterogeneous), Loc),
548 Parameter_Associations => New_List (
549 New_Reference_To (Fin_Mas_Id, Loc)));
550
551 Insert_Before_And_Analyze (First_Decl, Action);
552 end if;
553
554 -- Restore the original state of the scope stack
555
556 Pop_Scope;
557
558 return Fin_Mas_Id;
559 end;
560 end if;
561 end Current_Anonymous_Master;
562
563 --------------------------------
564 -- Displace_Allocator_Pointer --
565 --------------------------------
566
567 procedure Displace_Allocator_Pointer (N : Node_Id) is
568 Loc : constant Source_Ptr := Sloc (N);
569 Orig_Node : constant Node_Id := Original_Node (N);
570 Dtyp : Entity_Id;
571 Etyp : Entity_Id;
572 PtrT : Entity_Id;
573
574 begin
575 -- Do nothing in case of VM targets: the virtual machine will handle
576 -- interfaces directly.
577
578 if not Tagged_Type_Expansion then
579 return;
580 end if;
581
582 pragma Assert (Nkind (N) = N_Identifier
583 and then Nkind (Orig_Node) = N_Allocator);
584
585 PtrT := Etype (Orig_Node);
586 Dtyp := Available_View (Designated_Type (PtrT));
587 Etyp := Etype (Expression (Orig_Node));
588
589 if Is_Class_Wide_Type (Dtyp)
590 and then Is_Interface (Dtyp)
591 then
592 -- If the type of the allocator expression is not an interface type
593 -- we can generate code to reference the record component containing
594 -- the pointer to the secondary dispatch table.
595
596 if not Is_Interface (Etyp) then
597 declare
598 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
599
600 begin
601 -- 1) Get access to the allocated object
602
603 Rewrite (N,
604 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
605 Set_Etype (N, Etyp);
606 Set_Analyzed (N);
607
608 -- 2) Add the conversion to displace the pointer to reference
609 -- the secondary dispatch table.
610
611 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
612 Analyze_And_Resolve (N, Dtyp);
613
614 -- 3) The 'access to the secondary dispatch table will be used
615 -- as the value returned by the allocator.
616
617 Rewrite (N,
618 Make_Attribute_Reference (Loc,
619 Prefix => Relocate_Node (N),
620 Attribute_Name => Name_Access));
621 Set_Etype (N, Saved_Typ);
622 Set_Analyzed (N);
623 end;
624
625 -- If the type of the allocator expression is an interface type we
626 -- generate a run-time call to displace "this" to reference the
627 -- component containing the pointer to the secondary dispatch table
628 -- or else raise Constraint_Error if the actual object does not
629 -- implement the target interface. This case corresponds with the
630 -- following example:
631
632 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
633 -- begin
634 -- return new Iface_2'Class'(Obj);
635 -- end Op;
636
637 else
638 Rewrite (N,
639 Unchecked_Convert_To (PtrT,
640 Make_Function_Call (Loc,
641 Name => New_Reference_To (RTE (RE_Displace), Loc),
642 Parameter_Associations => New_List (
643 Unchecked_Convert_To (RTE (RE_Address),
644 Relocate_Node (N)),
645
646 New_Occurrence_Of
647 (Elists.Node
648 (First_Elmt
649 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
650 Loc)))));
651 Analyze_And_Resolve (N, PtrT);
652 end if;
653 end if;
654 end Displace_Allocator_Pointer;
655
656 ---------------------------------
657 -- Expand_Allocator_Expression --
658 ---------------------------------
659
660 procedure Expand_Allocator_Expression (N : Node_Id) is
661 Loc : constant Source_Ptr := Sloc (N);
662 Exp : constant Node_Id := Expression (Expression (N));
663 PtrT : constant Entity_Id := Etype (N);
664 DesigT : constant Entity_Id := Designated_Type (PtrT);
665
666 procedure Apply_Accessibility_Check
667 (Ref : Node_Id;
668 Built_In_Place : Boolean := False);
669 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
670 -- type, generate an accessibility check to verify that the level of the
671 -- type of the created object is not deeper than the level of the access
672 -- type. If the type of the qualified expression is class-wide, then
673 -- always generate the check (except in the case where it is known to be
674 -- unnecessary, see comment below). Otherwise, only generate the check
675 -- if the level of the qualified expression type is statically deeper
676 -- than the access type.
677 --
678 -- Although the static accessibility will generally have been performed
679 -- as a legality check, it won't have been done in cases where the
680 -- allocator appears in generic body, so a run-time check is needed in
681 -- general. One special case is when the access type is declared in the
682 -- same scope as the class-wide allocator, in which case the check can
683 -- never fail, so it need not be generated.
684 --
685 -- As an open issue, there seem to be cases where the static level
686 -- associated with the class-wide object's underlying type is not
687 -- sufficient to perform the proper accessibility check, such as for
688 -- allocators in nested subprograms or accept statements initialized by
689 -- class-wide formals when the actual originates outside at a deeper
690 -- static level. The nested subprogram case might require passing
691 -- accessibility levels along with class-wide parameters, and the task
692 -- case seems to be an actual gap in the language rules that needs to
693 -- be fixed by the ARG. ???
694
695 -------------------------------
696 -- Apply_Accessibility_Check --
697 -------------------------------
698
699 procedure Apply_Accessibility_Check
700 (Ref : Node_Id;
701 Built_In_Place : Boolean := False)
702 is
703 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
704 Cond : Node_Id;
705 Free_Stmt : Node_Id;
706 Obj_Ref : Node_Id;
707 Stmts : List_Id;
708
709 begin
710 if Ada_Version >= Ada_2005
711 and then Is_Class_Wide_Type (DesigT)
712 and then not Scope_Suppress.Suppress (Accessibility_Check)
713 and then
714 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
715 or else
716 (Is_Class_Wide_Type (Etype (Exp))
717 and then Scope (PtrT) /= Current_Scope))
718 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
719 then
720 -- If the allocator was built in place, Ref is already a reference
721 -- to the access object initialized to the result of the allocator
722 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
723 -- Remove_Side_Effects for cases where the build-in-place call may
724 -- still be the prefix of the reference (to avoid generating
725 -- duplicate calls). Otherwise, it is the entity associated with
726 -- the object containing the address of the allocated object.
727
728 if Built_In_Place then
729 Remove_Side_Effects (Ref);
730 Obj_Ref := New_Copy (Ref);
731 else
732 Obj_Ref := New_Reference_To (Ref, Loc);
733 end if;
734
735 -- Step 1: Create the object clean up code
736
737 Stmts := New_List;
738
739 -- Create an explicit free statement to clean up the allocated
740 -- object in case the accessibility check fails. Generate:
741
742 -- Free (Obj_Ref);
743
744 Free_Stmt := Make_Free_Statement (Loc, New_Copy (Obj_Ref));
745 Set_Storage_Pool (Free_Stmt, Pool_Id);
746
747 Append_To (Stmts, Free_Stmt);
748
749 -- Finalize the object (if applicable), but wrap the call inside
750 -- a block to ensure that the object would still be deallocated in
751 -- case the finalization fails. Generate:
752
753 -- begin
754 -- [Deep_]Finalize (Obj_Ref.all);
755 -- exception
756 -- when others =>
757 -- Free (Obj_Ref);
758 -- raise;
759 -- end;
760
761 if Needs_Finalization (DesigT) then
762 Prepend_To (Stmts,
763 Make_Block_Statement (Loc,
764 Handled_Statement_Sequence =>
765 Make_Handled_Sequence_Of_Statements (Loc,
766 Statements => New_List (
767 Make_Final_Call (
768 Obj_Ref =>
769 Make_Explicit_Dereference (Loc,
770 Prefix => New_Copy (Obj_Ref)),
771 Typ => DesigT)),
772
773 Exception_Handlers => New_List (
774 Make_Exception_Handler (Loc,
775 Exception_Choices => New_List (
776 Make_Others_Choice (Loc)),
777 Statements => New_List (
778 New_Copy_Tree (Free_Stmt),
779 Make_Raise_Statement (Loc)))))));
780 end if;
781
782 -- Signal the accessibility failure through a Program_Error
783
784 Append_To (Stmts,
785 Make_Raise_Program_Error (Loc,
786 Condition => New_Reference_To (Standard_True, Loc),
787 Reason => PE_Accessibility_Check_Failed));
788
789 -- Step 2: Create the accessibility comparison
790
791 -- Generate:
792 -- Ref'Tag
793
794 Obj_Ref :=
795 Make_Attribute_Reference (Loc,
796 Prefix => Obj_Ref,
797 Attribute_Name => Name_Tag);
798
799 -- For tagged types, determine the accessibility level by looking
800 -- at the type specific data of the dispatch table. Generate:
801
802 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
803
804 if Tagged_Type_Expansion then
805 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
806
807 -- Use a runtime call to determine the accessibility level when
808 -- compiling on virtual machine targets. Generate:
809
810 -- Get_Access_Level (Ref'Tag)
811
812 else
813 Cond :=
814 Make_Function_Call (Loc,
815 Name =>
816 New_Reference_To (RTE (RE_Get_Access_Level), Loc),
817 Parameter_Associations => New_List (Obj_Ref));
818 end if;
819
820 Cond :=
821 Make_Op_Gt (Loc,
822 Left_Opnd => Cond,
823 Right_Opnd =>
824 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
825
826 -- Due to the complexity and side effects of the check, utilize an
827 -- if statement instead of the regular Program_Error circuitry.
828
829 Insert_Action (N,
830 Make_If_Statement (Loc,
831 Condition => Cond,
832 Then_Statements => Stmts));
833 end if;
834 end Apply_Accessibility_Check;
835
836 -- Local variables
837
838 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
839 Indic : constant Node_Id := Subtype_Mark (Expression (N));
840 T : constant Entity_Id := Entity (Indic);
841 Node : Node_Id;
842 Tag_Assign : Node_Id;
843 Temp : Entity_Id;
844 Temp_Decl : Node_Id;
845
846 TagT : Entity_Id := Empty;
847 -- Type used as source for tag assignment
848
849 TagR : Node_Id := Empty;
850 -- Target reference for tag assignment
851
852 -- Start of processing for Expand_Allocator_Expression
853
854 begin
855 -- In the case of an Ada 2012 allocator whose initial value comes from a
856 -- function call, pass "the accessibility level determined by the point
857 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
858 -- Expand_Call but it couldn't be done there (because the Etype of the
859 -- allocator wasn't set then) so we generate the parameter here. See
860 -- the Boolean variable Defer in (a block within) Expand_Call.
861
862 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
863 declare
864 Subp : Entity_Id;
865
866 begin
867 if Nkind (Name (Exp)) = N_Explicit_Dereference then
868 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
869 else
870 Subp := Entity (Name (Exp));
871 end if;
872
873 Subp := Ultimate_Alias (Subp);
874
875 if Present (Extra_Accessibility_Of_Result (Subp)) then
876 Add_Extra_Actual_To_Call
877 (Subprogram_Call => Exp,
878 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
879 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
880 end if;
881 end;
882 end if;
883
884 -- Case of tagged type or type requiring finalization
885
886 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
887 if Is_CPP_Constructor_Call (Exp) then
888
889 -- Generate:
890 -- Pnnn : constant ptr_T := new (T);
891 -- Init (Pnnn.all,...);
892
893 -- Allocate the object without an expression
894
895 Node := Relocate_Node (N);
896 Set_Expression (Node, New_Reference_To (Etype (Exp), Loc));
897
898 -- Avoid its expansion to avoid generating a call to the default
899 -- C++ constructor.
900
901 Set_Analyzed (Node);
902
903 Temp := Make_Temporary (Loc, 'P', N);
904
905 Temp_Decl :=
906 Make_Object_Declaration (Loc,
907 Defining_Identifier => Temp,
908 Constant_Present => True,
909 Object_Definition => New_Reference_To (PtrT, Loc),
910 Expression => Node);
911 Insert_Action (N, Temp_Decl);
912
913 Apply_Accessibility_Check (Temp);
914
915 -- Locate the enclosing list and insert the C++ constructor call
916
917 declare
918 P : Node_Id;
919
920 begin
921 P := Parent (Node);
922 while not Is_List_Member (P) loop
923 P := Parent (P);
924 end loop;
925
926 Insert_List_After_And_Analyze (P,
927 Build_Initialization_Call (Loc,
928 Id_Ref =>
929 Make_Explicit_Dereference (Loc,
930 Prefix => New_Reference_To (Temp, Loc)),
931 Typ => Etype (Exp),
932 Constructor_Ref => Exp));
933 end;
934
935 Rewrite (N, New_Reference_To (Temp, Loc));
936 Analyze_And_Resolve (N, PtrT);
937 return;
938 end if;
939
940 -- Ada 2005 (AI-318-02): If the initialization expression is a call
941 -- to a build-in-place function, then access to the allocated object
942 -- must be passed to the function. Currently we limit such functions
943 -- to those with constrained limited result subtypes, but eventually
944 -- we plan to expand the allowed forms of functions that are treated
945 -- as build-in-place.
946
947 if Ada_Version >= Ada_2005
948 and then Is_Build_In_Place_Function_Call (Exp)
949 then
950 Make_Build_In_Place_Call_In_Allocator (N, Exp);
951 Apply_Accessibility_Check (N, Built_In_Place => True);
952 return;
953 end if;
954
955 -- Actions inserted before:
956 -- Temp : constant ptr_T := new T'(Expression);
957 -- Temp._tag = T'tag; -- when not class-wide
958 -- [Deep_]Adjust (Temp.all);
959
960 -- We analyze by hand the new internal allocator to avoid any
961 -- recursion and inappropriate call to Initialize
962
963 -- We don't want to remove side effects when the expression must be
964 -- built in place. In the case of a build-in-place function call,
965 -- that could lead to a duplication of the call, which was already
966 -- substituted for the allocator.
967
968 if not Aggr_In_Place then
969 Remove_Side_Effects (Exp);
970 end if;
971
972 Temp := Make_Temporary (Loc, 'P', N);
973
974 -- For a class wide allocation generate the following code:
975
976 -- type Equiv_Record is record ... end record;
977 -- implicit subtype CW is <Class_Wide_Subytpe>;
978 -- temp : PtrT := new CW'(CW!(expr));
979
980 if Is_Class_Wide_Type (T) then
981 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
982
983 -- Ada 2005 (AI-251): If the expression is a class-wide interface
984 -- object we generate code to move up "this" to reference the
985 -- base of the object before allocating the new object.
986
987 -- Note that Exp'Address is recursively expanded into a call
988 -- to Base_Address (Exp.Tag)
989
990 if Is_Class_Wide_Type (Etype (Exp))
991 and then Is_Interface (Etype (Exp))
992 and then Tagged_Type_Expansion
993 then
994 Set_Expression
995 (Expression (N),
996 Unchecked_Convert_To (Entity (Indic),
997 Make_Explicit_Dereference (Loc,
998 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
999 Make_Attribute_Reference (Loc,
1000 Prefix => Exp,
1001 Attribute_Name => Name_Address)))));
1002 else
1003 Set_Expression
1004 (Expression (N),
1005 Unchecked_Convert_To (Entity (Indic), Exp));
1006 end if;
1007
1008 Analyze_And_Resolve (Expression (N), Entity (Indic));
1009 end if;
1010
1011 -- Processing for allocators returning non-interface types
1012
1013 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1014 if Aggr_In_Place then
1015 Temp_Decl :=
1016 Make_Object_Declaration (Loc,
1017 Defining_Identifier => Temp,
1018 Object_Definition => New_Reference_To (PtrT, Loc),
1019 Expression =>
1020 Make_Allocator (Loc,
1021 Expression =>
1022 New_Reference_To (Etype (Exp), Loc)));
1023
1024 -- Copy the Comes_From_Source flag for the allocator we just
1025 -- built, since logically this allocator is a replacement of
1026 -- the original allocator node. This is for proper handling of
1027 -- restriction No_Implicit_Heap_Allocations.
1028
1029 Set_Comes_From_Source
1030 (Expression (Temp_Decl), Comes_From_Source (N));
1031
1032 Set_No_Initialization (Expression (Temp_Decl));
1033 Insert_Action (N, Temp_Decl);
1034
1035 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1036 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1037
1038 -- Attach the object to the associated finalization master.
1039 -- This is done manually on .NET/JVM since those compilers do
1040 -- no support pools and can't benefit from internally generated
1041 -- Allocate / Deallocate procedures.
1042
1043 if VM_Target /= No_VM
1044 and then Is_Controlled (DesigT)
1045 and then Present (Finalization_Master (PtrT))
1046 then
1047 Insert_Action (N,
1048 Make_Attach_Call (
1049 Obj_Ref =>
1050 New_Reference_To (Temp, Loc),
1051 Ptr_Typ => PtrT));
1052 end if;
1053
1054 else
1055 Node := Relocate_Node (N);
1056 Set_Analyzed (Node);
1057
1058 Temp_Decl :=
1059 Make_Object_Declaration (Loc,
1060 Defining_Identifier => Temp,
1061 Constant_Present => True,
1062 Object_Definition => New_Reference_To (PtrT, Loc),
1063 Expression => Node);
1064
1065 Insert_Action (N, Temp_Decl);
1066 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1067
1068 -- Attach the object to the associated finalization master.
1069 -- This is done manually on .NET/JVM since those compilers do
1070 -- no support pools and can't benefit from internally generated
1071 -- Allocate / Deallocate procedures.
1072
1073 if VM_Target /= No_VM
1074 and then Is_Controlled (DesigT)
1075 and then Present (Finalization_Master (PtrT))
1076 then
1077 Insert_Action (N,
1078 Make_Attach_Call (
1079 Obj_Ref =>
1080 New_Reference_To (Temp, Loc),
1081 Ptr_Typ => PtrT));
1082 end if;
1083 end if;
1084
1085 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1086 -- interface type. In this case we use the type of the qualified
1087 -- expression to allocate the object.
1088
1089 else
1090 declare
1091 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1092 New_Decl : Node_Id;
1093
1094 begin
1095 New_Decl :=
1096 Make_Full_Type_Declaration (Loc,
1097 Defining_Identifier => Def_Id,
1098 Type_Definition =>
1099 Make_Access_To_Object_Definition (Loc,
1100 All_Present => True,
1101 Null_Exclusion_Present => False,
1102 Constant_Present =>
1103 Is_Access_Constant (Etype (N)),
1104 Subtype_Indication =>
1105 New_Reference_To (Etype (Exp), Loc)));
1106
1107 Insert_Action (N, New_Decl);
1108
1109 -- Inherit the allocation-related attributes from the original
1110 -- access type.
1111
1112 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
1113
1114 Set_Associated_Storage_Pool (Def_Id,
1115 Associated_Storage_Pool (PtrT));
1116
1117 -- Declare the object using the previous type declaration
1118
1119 if Aggr_In_Place then
1120 Temp_Decl :=
1121 Make_Object_Declaration (Loc,
1122 Defining_Identifier => Temp,
1123 Object_Definition => New_Reference_To (Def_Id, Loc),
1124 Expression =>
1125 Make_Allocator (Loc,
1126 New_Reference_To (Etype (Exp), Loc)));
1127
1128 -- Copy the Comes_From_Source flag for the allocator we just
1129 -- built, since logically this allocator is a replacement of
1130 -- the original allocator node. This is for proper handling
1131 -- of restriction No_Implicit_Heap_Allocations.
1132
1133 Set_Comes_From_Source
1134 (Expression (Temp_Decl), Comes_From_Source (N));
1135
1136 Set_No_Initialization (Expression (Temp_Decl));
1137 Insert_Action (N, Temp_Decl);
1138
1139 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1140 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1141
1142 else
1143 Node := Relocate_Node (N);
1144 Set_Analyzed (Node);
1145
1146 Temp_Decl :=
1147 Make_Object_Declaration (Loc,
1148 Defining_Identifier => Temp,
1149 Constant_Present => True,
1150 Object_Definition => New_Reference_To (Def_Id, Loc),
1151 Expression => Node);
1152
1153 Insert_Action (N, Temp_Decl);
1154 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1155 end if;
1156
1157 -- Generate an additional object containing the address of the
1158 -- returned object. The type of this second object declaration
1159 -- is the correct type required for the common processing that
1160 -- is still performed by this subprogram. The displacement of
1161 -- this pointer to reference the component associated with the
1162 -- interface type will be done at the end of common processing.
1163
1164 New_Decl :=
1165 Make_Object_Declaration (Loc,
1166 Defining_Identifier => Make_Temporary (Loc, 'P'),
1167 Object_Definition => New_Reference_To (PtrT, Loc),
1168 Expression =>
1169 Unchecked_Convert_To (PtrT,
1170 New_Reference_To (Temp, Loc)));
1171
1172 Insert_Action (N, New_Decl);
1173
1174 Temp_Decl := New_Decl;
1175 Temp := Defining_Identifier (New_Decl);
1176 end;
1177 end if;
1178
1179 Apply_Accessibility_Check (Temp);
1180
1181 -- Generate the tag assignment
1182
1183 -- Suppress the tag assignment when VM_Target because VM tags are
1184 -- represented implicitly in objects.
1185
1186 if not Tagged_Type_Expansion then
1187 null;
1188
1189 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1190 -- interface objects because in this case the tag does not change.
1191
1192 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1193 pragma Assert (Is_Class_Wide_Type
1194 (Directly_Designated_Type (Etype (N))));
1195 null;
1196
1197 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1198 TagT := T;
1199 TagR := New_Reference_To (Temp, Loc);
1200
1201 elsif Is_Private_Type (T)
1202 and then Is_Tagged_Type (Underlying_Type (T))
1203 then
1204 TagT := Underlying_Type (T);
1205 TagR :=
1206 Unchecked_Convert_To (Underlying_Type (T),
1207 Make_Explicit_Dereference (Loc,
1208 Prefix => New_Reference_To (Temp, Loc)));
1209 end if;
1210
1211 if Present (TagT) then
1212 declare
1213 Full_T : constant Entity_Id := Underlying_Type (TagT);
1214 begin
1215 Tag_Assign :=
1216 Make_Assignment_Statement (Loc,
1217 Name =>
1218 Make_Selected_Component (Loc,
1219 Prefix => TagR,
1220 Selector_Name =>
1221 New_Reference_To (First_Tag_Component (Full_T), Loc)),
1222 Expression =>
1223 Unchecked_Convert_To (RTE (RE_Tag),
1224 New_Reference_To
1225 (Elists.Node
1226 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1227 end;
1228
1229 -- The previous assignment has to be done in any case
1230
1231 Set_Assignment_OK (Name (Tag_Assign));
1232 Insert_Action (N, Tag_Assign);
1233 end if;
1234
1235 if Needs_Finalization (DesigT)
1236 and then Needs_Finalization (T)
1237 then
1238 -- Generate an Adjust call if the object will be moved. In Ada
1239 -- 2005, the object may be inherently limited, in which case
1240 -- there is no Adjust procedure, and the object is built in
1241 -- place. In Ada 95, the object can be limited but not
1242 -- inherently limited if this allocator came from a return
1243 -- statement (we're allocating the result on the secondary
1244 -- stack). In that case, the object will be moved, so we _do_
1245 -- want to Adjust.
1246
1247 if not Aggr_In_Place
1248 and then not Is_Immutably_Limited_Type (T)
1249 then
1250 Insert_Action (N,
1251 Make_Adjust_Call (
1252 Obj_Ref =>
1253
1254 -- An unchecked conversion is needed in the classwide
1255 -- case because the designated type can be an ancestor
1256 -- of the subtype mark of the allocator.
1257
1258 Unchecked_Convert_To (T,
1259 Make_Explicit_Dereference (Loc,
1260 Prefix => New_Reference_To (Temp, Loc))),
1261 Typ => T));
1262 end if;
1263
1264 -- Generate:
1265 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1266
1267 -- Do not generate this call in the following cases:
1268
1269 -- * .NET/JVM - these targets do not support address arithmetic
1270 -- and unchecked conversion, key elements of Finalize_Address.
1271
1272 -- * Alfa mode - the call is useless and results in unwanted
1273 -- expansion.
1274
1275 -- * CodePeer mode - TSS primitive Finalize_Address is not
1276 -- created in this mode.
1277
1278 if VM_Target = No_VM
1279 and then not Alfa_Mode
1280 and then not CodePeer_Mode
1281 and then Present (Finalization_Master (PtrT))
1282 and then Present (Temp_Decl)
1283 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1284 then
1285 Insert_Action (N,
1286 Make_Set_Finalize_Address_Call
1287 (Loc => Loc,
1288 Typ => T,
1289 Ptr_Typ => PtrT));
1290 end if;
1291 end if;
1292
1293 Rewrite (N, New_Reference_To (Temp, Loc));
1294 Analyze_And_Resolve (N, PtrT);
1295
1296 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1297 -- component containing the secondary dispatch table of the interface
1298 -- type.
1299
1300 if Is_Interface (Directly_Designated_Type (PtrT)) then
1301 Displace_Allocator_Pointer (N);
1302 end if;
1303
1304 elsif Aggr_In_Place then
1305 Temp := Make_Temporary (Loc, 'P', N);
1306 Temp_Decl :=
1307 Make_Object_Declaration (Loc,
1308 Defining_Identifier => Temp,
1309 Object_Definition => New_Reference_To (PtrT, Loc),
1310 Expression =>
1311 Make_Allocator (Loc,
1312 Expression => New_Reference_To (Etype (Exp), Loc)));
1313
1314 -- Copy the Comes_From_Source flag for the allocator we just built,
1315 -- since logically this allocator is a replacement of the original
1316 -- allocator node. This is for proper handling of restriction
1317 -- No_Implicit_Heap_Allocations.
1318
1319 Set_Comes_From_Source
1320 (Expression (Temp_Decl), Comes_From_Source (N));
1321
1322 Set_No_Initialization (Expression (Temp_Decl));
1323 Insert_Action (N, Temp_Decl);
1324
1325 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1326 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1327
1328 -- Attach the object to the associated finalization master. Thisis
1329 -- done manually on .NET/JVM since those compilers do no support
1330 -- pools and cannot benefit from internally generated Allocate and
1331 -- Deallocate procedures.
1332
1333 if VM_Target /= No_VM
1334 and then Is_Controlled (DesigT)
1335 and then Present (Finalization_Master (PtrT))
1336 then
1337 Insert_Action (N,
1338 Make_Attach_Call
1339 (Obj_Ref => New_Reference_To (Temp, Loc),
1340 Ptr_Typ => PtrT));
1341 end if;
1342
1343 Rewrite (N, New_Reference_To (Temp, Loc));
1344 Analyze_And_Resolve (N, PtrT);
1345
1346 elsif Is_Access_Type (T)
1347 and then Can_Never_Be_Null (T)
1348 then
1349 Install_Null_Excluding_Check (Exp);
1350
1351 elsif Is_Access_Type (DesigT)
1352 and then Nkind (Exp) = N_Allocator
1353 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1354 then
1355 -- Apply constraint to designated subtype indication
1356
1357 Apply_Constraint_Check (Expression (Exp),
1358 Designated_Type (DesigT),
1359 No_Sliding => True);
1360
1361 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1362
1363 -- Propagate constraint_error to enclosing allocator
1364
1365 Rewrite (Exp, New_Copy (Expression (Exp)));
1366 end if;
1367
1368 else
1369 Build_Allocate_Deallocate_Proc (N, True);
1370
1371 -- If we have:
1372 -- type A is access T1;
1373 -- X : A := new T2'(...);
1374 -- T1 and T2 can be different subtypes, and we might need to check
1375 -- both constraints. First check against the type of the qualified
1376 -- expression.
1377
1378 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1379
1380 if Do_Range_Check (Exp) then
1381 Set_Do_Range_Check (Exp, False);
1382 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1383 end if;
1384
1385 -- A check is also needed in cases where the designated subtype is
1386 -- constrained and differs from the subtype given in the qualified
1387 -- expression. Note that the check on the qualified expression does
1388 -- not allow sliding, but this check does (a relaxation from Ada 83).
1389
1390 if Is_Constrained (DesigT)
1391 and then not Subtypes_Statically_Match (T, DesigT)
1392 then
1393 Apply_Constraint_Check
1394 (Exp, DesigT, No_Sliding => False);
1395
1396 if Do_Range_Check (Exp) then
1397 Set_Do_Range_Check (Exp, False);
1398 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1399 end if;
1400 end if;
1401
1402 -- For an access to unconstrained packed array, GIGI needs to see an
1403 -- expression with a constrained subtype in order to compute the
1404 -- proper size for the allocator.
1405
1406 if Is_Array_Type (T)
1407 and then not Is_Constrained (T)
1408 and then Is_Packed (T)
1409 then
1410 declare
1411 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1412 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1413 begin
1414 Insert_Action (Exp,
1415 Make_Subtype_Declaration (Loc,
1416 Defining_Identifier => ConstrT,
1417 Subtype_Indication =>
1418 Make_Subtype_From_Expr (Internal_Exp, T)));
1419 Freeze_Itype (ConstrT, Exp);
1420 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1421 end;
1422 end if;
1423
1424 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1425 -- to a build-in-place function, then access to the allocated object
1426 -- must be passed to the function. Currently we limit such functions
1427 -- to those with constrained limited result subtypes, but eventually
1428 -- we plan to expand the allowed forms of functions that are treated
1429 -- as build-in-place.
1430
1431 if Ada_Version >= Ada_2005
1432 and then Is_Build_In_Place_Function_Call (Exp)
1433 then
1434 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1435 end if;
1436 end if;
1437
1438 exception
1439 when RE_Not_Available =>
1440 return;
1441 end Expand_Allocator_Expression;
1442
1443 -----------------------------
1444 -- Expand_Array_Comparison --
1445 -----------------------------
1446
1447 -- Expansion is only required in the case of array types. For the unpacked
1448 -- case, an appropriate runtime routine is called. For packed cases, and
1449 -- also in some other cases where a runtime routine cannot be called, the
1450 -- form of the expansion is:
1451
1452 -- [body for greater_nn; boolean_expression]
1453
1454 -- The body is built by Make_Array_Comparison_Op, and the form of the
1455 -- Boolean expression depends on the operator involved.
1456
1457 procedure Expand_Array_Comparison (N : Node_Id) is
1458 Loc : constant Source_Ptr := Sloc (N);
1459 Op1 : Node_Id := Left_Opnd (N);
1460 Op2 : Node_Id := Right_Opnd (N);
1461 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1462 Ctyp : constant Entity_Id := Component_Type (Typ1);
1463
1464 Expr : Node_Id;
1465 Func_Body : Node_Id;
1466 Func_Name : Entity_Id;
1467
1468 Comp : RE_Id;
1469
1470 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1471 -- True for byte addressable target
1472
1473 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1474 -- Returns True if the length of the given operand is known to be less
1475 -- than 4. Returns False if this length is known to be four or greater
1476 -- or is not known at compile time.
1477
1478 ------------------------
1479 -- Length_Less_Than_4 --
1480 ------------------------
1481
1482 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1483 Otyp : constant Entity_Id := Etype (Opnd);
1484
1485 begin
1486 if Ekind (Otyp) = E_String_Literal_Subtype then
1487 return String_Literal_Length (Otyp) < 4;
1488
1489 else
1490 declare
1491 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1492 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1493 Hi : constant Node_Id := Type_High_Bound (Ityp);
1494 Lov : Uint;
1495 Hiv : Uint;
1496
1497 begin
1498 if Compile_Time_Known_Value (Lo) then
1499 Lov := Expr_Value (Lo);
1500 else
1501 return False;
1502 end if;
1503
1504 if Compile_Time_Known_Value (Hi) then
1505 Hiv := Expr_Value (Hi);
1506 else
1507 return False;
1508 end if;
1509
1510 return Hiv < Lov + 3;
1511 end;
1512 end if;
1513 end Length_Less_Than_4;
1514
1515 -- Start of processing for Expand_Array_Comparison
1516
1517 begin
1518 -- Deal first with unpacked case, where we can call a runtime routine
1519 -- except that we avoid this for targets for which are not addressable
1520 -- by bytes, and for the JVM/CIL, since they do not support direct
1521 -- addressing of array components.
1522
1523 if not Is_Bit_Packed_Array (Typ1)
1524 and then Byte_Addressable
1525 and then VM_Target = No_VM
1526 then
1527 -- The call we generate is:
1528
1529 -- Compare_Array_xn[_Unaligned]
1530 -- (left'address, right'address, left'length, right'length) <op> 0
1531
1532 -- x = U for unsigned, S for signed
1533 -- n = 8,16,32,64 for component size
1534 -- Add _Unaligned if length < 4 and component size is 8.
1535 -- <op> is the standard comparison operator
1536
1537 if Component_Size (Typ1) = 8 then
1538 if Length_Less_Than_4 (Op1)
1539 or else
1540 Length_Less_Than_4 (Op2)
1541 then
1542 if Is_Unsigned_Type (Ctyp) then
1543 Comp := RE_Compare_Array_U8_Unaligned;
1544 else
1545 Comp := RE_Compare_Array_S8_Unaligned;
1546 end if;
1547
1548 else
1549 if Is_Unsigned_Type (Ctyp) then
1550 Comp := RE_Compare_Array_U8;
1551 else
1552 Comp := RE_Compare_Array_S8;
1553 end if;
1554 end if;
1555
1556 elsif Component_Size (Typ1) = 16 then
1557 if Is_Unsigned_Type (Ctyp) then
1558 Comp := RE_Compare_Array_U16;
1559 else
1560 Comp := RE_Compare_Array_S16;
1561 end if;
1562
1563 elsif Component_Size (Typ1) = 32 then
1564 if Is_Unsigned_Type (Ctyp) then
1565 Comp := RE_Compare_Array_U32;
1566 else
1567 Comp := RE_Compare_Array_S32;
1568 end if;
1569
1570 else pragma Assert (Component_Size (Typ1) = 64);
1571 if Is_Unsigned_Type (Ctyp) then
1572 Comp := RE_Compare_Array_U64;
1573 else
1574 Comp := RE_Compare_Array_S64;
1575 end if;
1576 end if;
1577
1578 Remove_Side_Effects (Op1, Name_Req => True);
1579 Remove_Side_Effects (Op2, Name_Req => True);
1580
1581 Rewrite (Op1,
1582 Make_Function_Call (Sloc (Op1),
1583 Name => New_Occurrence_Of (RTE (Comp), Loc),
1584
1585 Parameter_Associations => New_List (
1586 Make_Attribute_Reference (Loc,
1587 Prefix => Relocate_Node (Op1),
1588 Attribute_Name => Name_Address),
1589
1590 Make_Attribute_Reference (Loc,
1591 Prefix => Relocate_Node (Op2),
1592 Attribute_Name => Name_Address),
1593
1594 Make_Attribute_Reference (Loc,
1595 Prefix => Relocate_Node (Op1),
1596 Attribute_Name => Name_Length),
1597
1598 Make_Attribute_Reference (Loc,
1599 Prefix => Relocate_Node (Op2),
1600 Attribute_Name => Name_Length))));
1601
1602 Rewrite (Op2,
1603 Make_Integer_Literal (Sloc (Op2),
1604 Intval => Uint_0));
1605
1606 Analyze_And_Resolve (Op1, Standard_Integer);
1607 Analyze_And_Resolve (Op2, Standard_Integer);
1608 return;
1609 end if;
1610
1611 -- Cases where we cannot make runtime call
1612
1613 -- For (a <= b) we convert to not (a > b)
1614
1615 if Chars (N) = Name_Op_Le then
1616 Rewrite (N,
1617 Make_Op_Not (Loc,
1618 Right_Opnd =>
1619 Make_Op_Gt (Loc,
1620 Left_Opnd => Op1,
1621 Right_Opnd => Op2)));
1622 Analyze_And_Resolve (N, Standard_Boolean);
1623 return;
1624
1625 -- For < the Boolean expression is
1626 -- greater__nn (op2, op1)
1627
1628 elsif Chars (N) = Name_Op_Lt then
1629 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1630
1631 -- Switch operands
1632
1633 Op1 := Right_Opnd (N);
1634 Op2 := Left_Opnd (N);
1635
1636 -- For (a >= b) we convert to not (a < b)
1637
1638 elsif Chars (N) = Name_Op_Ge then
1639 Rewrite (N,
1640 Make_Op_Not (Loc,
1641 Right_Opnd =>
1642 Make_Op_Lt (Loc,
1643 Left_Opnd => Op1,
1644 Right_Opnd => Op2)));
1645 Analyze_And_Resolve (N, Standard_Boolean);
1646 return;
1647
1648 -- For > the Boolean expression is
1649 -- greater__nn (op1, op2)
1650
1651 else
1652 pragma Assert (Chars (N) = Name_Op_Gt);
1653 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1654 end if;
1655
1656 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1657 Expr :=
1658 Make_Function_Call (Loc,
1659 Name => New_Reference_To (Func_Name, Loc),
1660 Parameter_Associations => New_List (Op1, Op2));
1661
1662 Insert_Action (N, Func_Body);
1663 Rewrite (N, Expr);
1664 Analyze_And_Resolve (N, Standard_Boolean);
1665
1666 exception
1667 when RE_Not_Available =>
1668 return;
1669 end Expand_Array_Comparison;
1670
1671 ---------------------------
1672 -- Expand_Array_Equality --
1673 ---------------------------
1674
1675 -- Expand an equality function for multi-dimensional arrays. Here is an
1676 -- example of such a function for Nb_Dimension = 2
1677
1678 -- function Enn (A : atyp; B : btyp) return boolean is
1679 -- begin
1680 -- if (A'length (1) = 0 or else A'length (2) = 0)
1681 -- and then
1682 -- (B'length (1) = 0 or else B'length (2) = 0)
1683 -- then
1684 -- return True; -- RM 4.5.2(22)
1685 -- end if;
1686
1687 -- if A'length (1) /= B'length (1)
1688 -- or else
1689 -- A'length (2) /= B'length (2)
1690 -- then
1691 -- return False; -- RM 4.5.2(23)
1692 -- end if;
1693
1694 -- declare
1695 -- A1 : Index_T1 := A'first (1);
1696 -- B1 : Index_T1 := B'first (1);
1697 -- begin
1698 -- loop
1699 -- declare
1700 -- A2 : Index_T2 := A'first (2);
1701 -- B2 : Index_T2 := B'first (2);
1702 -- begin
1703 -- loop
1704 -- if A (A1, A2) /= B (B1, B2) then
1705 -- return False;
1706 -- end if;
1707
1708 -- exit when A2 = A'last (2);
1709 -- A2 := Index_T2'succ (A2);
1710 -- B2 := Index_T2'succ (B2);
1711 -- end loop;
1712 -- end;
1713
1714 -- exit when A1 = A'last (1);
1715 -- A1 := Index_T1'succ (A1);
1716 -- B1 := Index_T1'succ (B1);
1717 -- end loop;
1718 -- end;
1719
1720 -- return true;
1721 -- end Enn;
1722
1723 -- Note on the formal types used (atyp and btyp). If either of the arrays
1724 -- is of a private type, we use the underlying type, and do an unchecked
1725 -- conversion of the actual. If either of the arrays has a bound depending
1726 -- on a discriminant, then we use the base type since otherwise we have an
1727 -- escaped discriminant in the function.
1728
1729 -- If both arrays are constrained and have the same bounds, we can generate
1730 -- a loop with an explicit iteration scheme using a 'Range attribute over
1731 -- the first array.
1732
1733 function Expand_Array_Equality
1734 (Nod : Node_Id;
1735 Lhs : Node_Id;
1736 Rhs : Node_Id;
1737 Bodies : List_Id;
1738 Typ : Entity_Id) return Node_Id
1739 is
1740 Loc : constant Source_Ptr := Sloc (Nod);
1741 Decls : constant List_Id := New_List;
1742 Index_List1 : constant List_Id := New_List;
1743 Index_List2 : constant List_Id := New_List;
1744
1745 Actuals : List_Id;
1746 Formals : List_Id;
1747 Func_Name : Entity_Id;
1748 Func_Body : Node_Id;
1749
1750 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1751 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1752
1753 Ltyp : Entity_Id;
1754 Rtyp : Entity_Id;
1755 -- The parameter types to be used for the formals
1756
1757 function Arr_Attr
1758 (Arr : Entity_Id;
1759 Nam : Name_Id;
1760 Num : Int) return Node_Id;
1761 -- This builds the attribute reference Arr'Nam (Expr)
1762
1763 function Component_Equality (Typ : Entity_Id) return Node_Id;
1764 -- Create one statement to compare corresponding components, designated
1765 -- by a full set of indexes.
1766
1767 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1768 -- Given one of the arguments, computes the appropriate type to be used
1769 -- for that argument in the corresponding function formal
1770
1771 function Handle_One_Dimension
1772 (N : Int;
1773 Index : Node_Id) return Node_Id;
1774 -- This procedure returns the following code
1775 --
1776 -- declare
1777 -- Bn : Index_T := B'First (N);
1778 -- begin
1779 -- loop
1780 -- xxx
1781 -- exit when An = A'Last (N);
1782 -- An := Index_T'Succ (An)
1783 -- Bn := Index_T'Succ (Bn)
1784 -- end loop;
1785 -- end;
1786 --
1787 -- If both indexes are constrained and identical, the procedure
1788 -- returns a simpler loop:
1789 --
1790 -- for An in A'Range (N) loop
1791 -- xxx
1792 -- end loop
1793 --
1794 -- N is the dimension for which we are generating a loop. Index is the
1795 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1796 -- xxx statement is either the loop or declare for the next dimension
1797 -- or if this is the last dimension the comparison of corresponding
1798 -- components of the arrays.
1799 --
1800 -- The actual way the code works is to return the comparison of
1801 -- corresponding components for the N+1 call. That's neater!
1802
1803 function Test_Empty_Arrays return Node_Id;
1804 -- This function constructs the test for both arrays being empty
1805 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1806 -- and then
1807 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1808
1809 function Test_Lengths_Correspond return Node_Id;
1810 -- This function constructs the test for arrays having different lengths
1811 -- in at least one index position, in which case the resulting code is:
1812
1813 -- A'length (1) /= B'length (1)
1814 -- or else
1815 -- A'length (2) /= B'length (2)
1816 -- or else
1817 -- ...
1818
1819 --------------
1820 -- Arr_Attr --
1821 --------------
1822
1823 function Arr_Attr
1824 (Arr : Entity_Id;
1825 Nam : Name_Id;
1826 Num : Int) return Node_Id
1827 is
1828 begin
1829 return
1830 Make_Attribute_Reference (Loc,
1831 Attribute_Name => Nam,
1832 Prefix => New_Reference_To (Arr, Loc),
1833 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1834 end Arr_Attr;
1835
1836 ------------------------
1837 -- Component_Equality --
1838 ------------------------
1839
1840 function Component_Equality (Typ : Entity_Id) return Node_Id is
1841 Test : Node_Id;
1842 L, R : Node_Id;
1843
1844 begin
1845 -- if a(i1...) /= b(j1...) then return false; end if;
1846
1847 L :=
1848 Make_Indexed_Component (Loc,
1849 Prefix => Make_Identifier (Loc, Chars (A)),
1850 Expressions => Index_List1);
1851
1852 R :=
1853 Make_Indexed_Component (Loc,
1854 Prefix => Make_Identifier (Loc, Chars (B)),
1855 Expressions => Index_List2);
1856
1857 Test := Expand_Composite_Equality
1858 (Nod, Component_Type (Typ), L, R, Decls);
1859
1860 -- If some (sub)component is an unchecked_union, the whole operation
1861 -- will raise program error.
1862
1863 if Nkind (Test) = N_Raise_Program_Error then
1864
1865 -- This node is going to be inserted at a location where a
1866 -- statement is expected: clear its Etype so analysis will set
1867 -- it to the expected Standard_Void_Type.
1868
1869 Set_Etype (Test, Empty);
1870 return Test;
1871
1872 else
1873 return
1874 Make_Implicit_If_Statement (Nod,
1875 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1876 Then_Statements => New_List (
1877 Make_Simple_Return_Statement (Loc,
1878 Expression => New_Occurrence_Of (Standard_False, Loc))));
1879 end if;
1880 end Component_Equality;
1881
1882 ------------------
1883 -- Get_Arg_Type --
1884 ------------------
1885
1886 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1887 T : Entity_Id;
1888 X : Node_Id;
1889
1890 begin
1891 T := Etype (N);
1892
1893 if No (T) then
1894 return Typ;
1895
1896 else
1897 T := Underlying_Type (T);
1898
1899 X := First_Index (T);
1900 while Present (X) loop
1901 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1902 or else
1903 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1904 then
1905 T := Base_Type (T);
1906 exit;
1907 end if;
1908
1909 Next_Index (X);
1910 end loop;
1911
1912 return T;
1913 end if;
1914 end Get_Arg_Type;
1915
1916 --------------------------
1917 -- Handle_One_Dimension --
1918 ---------------------------
1919
1920 function Handle_One_Dimension
1921 (N : Int;
1922 Index : Node_Id) return Node_Id
1923 is
1924 Need_Separate_Indexes : constant Boolean :=
1925 Ltyp /= Rtyp
1926 or else not Is_Constrained (Ltyp);
1927 -- If the index types are identical, and we are working with
1928 -- constrained types, then we can use the same index for both
1929 -- of the arrays.
1930
1931 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1932
1933 Bn : Entity_Id;
1934 Index_T : Entity_Id;
1935 Stm_List : List_Id;
1936 Loop_Stm : Node_Id;
1937
1938 begin
1939 if N > Number_Dimensions (Ltyp) then
1940 return Component_Equality (Ltyp);
1941 end if;
1942
1943 -- Case where we generate a loop
1944
1945 Index_T := Base_Type (Etype (Index));
1946
1947 if Need_Separate_Indexes then
1948 Bn := Make_Temporary (Loc, 'B');
1949 else
1950 Bn := An;
1951 end if;
1952
1953 Append (New_Reference_To (An, Loc), Index_List1);
1954 Append (New_Reference_To (Bn, Loc), Index_List2);
1955
1956 Stm_List := New_List (
1957 Handle_One_Dimension (N + 1, Next_Index (Index)));
1958
1959 if Need_Separate_Indexes then
1960
1961 -- Generate guard for loop, followed by increments of indexes
1962
1963 Append_To (Stm_List,
1964 Make_Exit_Statement (Loc,
1965 Condition =>
1966 Make_Op_Eq (Loc,
1967 Left_Opnd => New_Reference_To (An, Loc),
1968 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1969
1970 Append_To (Stm_List,
1971 Make_Assignment_Statement (Loc,
1972 Name => New_Reference_To (An, Loc),
1973 Expression =>
1974 Make_Attribute_Reference (Loc,
1975 Prefix => New_Reference_To (Index_T, Loc),
1976 Attribute_Name => Name_Succ,
1977 Expressions => New_List (New_Reference_To (An, Loc)))));
1978
1979 Append_To (Stm_List,
1980 Make_Assignment_Statement (Loc,
1981 Name => New_Reference_To (Bn, Loc),
1982 Expression =>
1983 Make_Attribute_Reference (Loc,
1984 Prefix => New_Reference_To (Index_T, Loc),
1985 Attribute_Name => Name_Succ,
1986 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1987 end if;
1988
1989 -- If separate indexes, we need a declare block for An and Bn, and a
1990 -- loop without an iteration scheme.
1991
1992 if Need_Separate_Indexes then
1993 Loop_Stm :=
1994 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1995
1996 return
1997 Make_Block_Statement (Loc,
1998 Declarations => New_List (
1999 Make_Object_Declaration (Loc,
2000 Defining_Identifier => An,
2001 Object_Definition => New_Reference_To (Index_T, Loc),
2002 Expression => Arr_Attr (A, Name_First, N)),
2003
2004 Make_Object_Declaration (Loc,
2005 Defining_Identifier => Bn,
2006 Object_Definition => New_Reference_To (Index_T, Loc),
2007 Expression => Arr_Attr (B, Name_First, N))),
2008
2009 Handled_Statement_Sequence =>
2010 Make_Handled_Sequence_Of_Statements (Loc,
2011 Statements => New_List (Loop_Stm)));
2012
2013 -- If no separate indexes, return loop statement with explicit
2014 -- iteration scheme on its own
2015
2016 else
2017 Loop_Stm :=
2018 Make_Implicit_Loop_Statement (Nod,
2019 Statements => Stm_List,
2020 Iteration_Scheme =>
2021 Make_Iteration_Scheme (Loc,
2022 Loop_Parameter_Specification =>
2023 Make_Loop_Parameter_Specification (Loc,
2024 Defining_Identifier => An,
2025 Discrete_Subtype_Definition =>
2026 Arr_Attr (A, Name_Range, N))));
2027 return Loop_Stm;
2028 end if;
2029 end Handle_One_Dimension;
2030
2031 -----------------------
2032 -- Test_Empty_Arrays --
2033 -----------------------
2034
2035 function Test_Empty_Arrays return Node_Id is
2036 Alist : Node_Id;
2037 Blist : Node_Id;
2038
2039 Atest : Node_Id;
2040 Btest : Node_Id;
2041
2042 begin
2043 Alist := Empty;
2044 Blist := Empty;
2045 for J in 1 .. Number_Dimensions (Ltyp) loop
2046 Atest :=
2047 Make_Op_Eq (Loc,
2048 Left_Opnd => Arr_Attr (A, Name_Length, J),
2049 Right_Opnd => Make_Integer_Literal (Loc, 0));
2050
2051 Btest :=
2052 Make_Op_Eq (Loc,
2053 Left_Opnd => Arr_Attr (B, Name_Length, J),
2054 Right_Opnd => Make_Integer_Literal (Loc, 0));
2055
2056 if No (Alist) then
2057 Alist := Atest;
2058 Blist := Btest;
2059
2060 else
2061 Alist :=
2062 Make_Or_Else (Loc,
2063 Left_Opnd => Relocate_Node (Alist),
2064 Right_Opnd => Atest);
2065
2066 Blist :=
2067 Make_Or_Else (Loc,
2068 Left_Opnd => Relocate_Node (Blist),
2069 Right_Opnd => Btest);
2070 end if;
2071 end loop;
2072
2073 return
2074 Make_And_Then (Loc,
2075 Left_Opnd => Alist,
2076 Right_Opnd => Blist);
2077 end Test_Empty_Arrays;
2078
2079 -----------------------------
2080 -- Test_Lengths_Correspond --
2081 -----------------------------
2082
2083 function Test_Lengths_Correspond return Node_Id is
2084 Result : Node_Id;
2085 Rtest : Node_Id;
2086
2087 begin
2088 Result := Empty;
2089 for J in 1 .. Number_Dimensions (Ltyp) loop
2090 Rtest :=
2091 Make_Op_Ne (Loc,
2092 Left_Opnd => Arr_Attr (A, Name_Length, J),
2093 Right_Opnd => Arr_Attr (B, Name_Length, J));
2094
2095 if No (Result) then
2096 Result := Rtest;
2097 else
2098 Result :=
2099 Make_Or_Else (Loc,
2100 Left_Opnd => Relocate_Node (Result),
2101 Right_Opnd => Rtest);
2102 end if;
2103 end loop;
2104
2105 return Result;
2106 end Test_Lengths_Correspond;
2107
2108 -- Start of processing for Expand_Array_Equality
2109
2110 begin
2111 Ltyp := Get_Arg_Type (Lhs);
2112 Rtyp := Get_Arg_Type (Rhs);
2113
2114 -- For now, if the argument types are not the same, go to the base type,
2115 -- since the code assumes that the formals have the same type. This is
2116 -- fixable in future ???
2117
2118 if Ltyp /= Rtyp then
2119 Ltyp := Base_Type (Ltyp);
2120 Rtyp := Base_Type (Rtyp);
2121 pragma Assert (Ltyp = Rtyp);
2122 end if;
2123
2124 -- Build list of formals for function
2125
2126 Formals := New_List (
2127 Make_Parameter_Specification (Loc,
2128 Defining_Identifier => A,
2129 Parameter_Type => New_Reference_To (Ltyp, Loc)),
2130
2131 Make_Parameter_Specification (Loc,
2132 Defining_Identifier => B,
2133 Parameter_Type => New_Reference_To (Rtyp, Loc)));
2134
2135 Func_Name := Make_Temporary (Loc, 'E');
2136
2137 -- Build statement sequence for function
2138
2139 Func_Body :=
2140 Make_Subprogram_Body (Loc,
2141 Specification =>
2142 Make_Function_Specification (Loc,
2143 Defining_Unit_Name => Func_Name,
2144 Parameter_Specifications => Formals,
2145 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
2146
2147 Declarations => Decls,
2148
2149 Handled_Statement_Sequence =>
2150 Make_Handled_Sequence_Of_Statements (Loc,
2151 Statements => New_List (
2152
2153 Make_Implicit_If_Statement (Nod,
2154 Condition => Test_Empty_Arrays,
2155 Then_Statements => New_List (
2156 Make_Simple_Return_Statement (Loc,
2157 Expression =>
2158 New_Occurrence_Of (Standard_True, Loc)))),
2159
2160 Make_Implicit_If_Statement (Nod,
2161 Condition => Test_Lengths_Correspond,
2162 Then_Statements => New_List (
2163 Make_Simple_Return_Statement (Loc,
2164 Expression =>
2165 New_Occurrence_Of (Standard_False, Loc)))),
2166
2167 Handle_One_Dimension (1, First_Index (Ltyp)),
2168
2169 Make_Simple_Return_Statement (Loc,
2170 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2171
2172 Set_Has_Completion (Func_Name, True);
2173 Set_Is_Inlined (Func_Name);
2174
2175 -- If the array type is distinct from the type of the arguments, it
2176 -- is the full view of a private type. Apply an unchecked conversion
2177 -- to insure that analysis of the call succeeds.
2178
2179 declare
2180 L, R : Node_Id;
2181
2182 begin
2183 L := Lhs;
2184 R := Rhs;
2185
2186 if No (Etype (Lhs))
2187 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2188 then
2189 L := OK_Convert_To (Ltyp, Lhs);
2190 end if;
2191
2192 if No (Etype (Rhs))
2193 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2194 then
2195 R := OK_Convert_To (Rtyp, Rhs);
2196 end if;
2197
2198 Actuals := New_List (L, R);
2199 end;
2200
2201 Append_To (Bodies, Func_Body);
2202
2203 return
2204 Make_Function_Call (Loc,
2205 Name => New_Reference_To (Func_Name, Loc),
2206 Parameter_Associations => Actuals);
2207 end Expand_Array_Equality;
2208
2209 -----------------------------
2210 -- Expand_Boolean_Operator --
2211 -----------------------------
2212
2213 -- Note that we first get the actual subtypes of the operands, since we
2214 -- always want to deal with types that have bounds.
2215
2216 procedure Expand_Boolean_Operator (N : Node_Id) is
2217 Typ : constant Entity_Id := Etype (N);
2218
2219 begin
2220 -- Special case of bit packed array where both operands are known to be
2221 -- properly aligned. In this case we use an efficient run time routine
2222 -- to carry out the operation (see System.Bit_Ops).
2223
2224 if Is_Bit_Packed_Array (Typ)
2225 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2226 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2227 then
2228 Expand_Packed_Boolean_Operator (N);
2229 return;
2230 end if;
2231
2232 -- For the normal non-packed case, the general expansion is to build
2233 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2234 -- and then inserting it into the tree. The original operator node is
2235 -- then rewritten as a call to this function. We also use this in the
2236 -- packed case if either operand is a possibly unaligned object.
2237
2238 declare
2239 Loc : constant Source_Ptr := Sloc (N);
2240 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2241 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2242 Func_Body : Node_Id;
2243 Func_Name : Entity_Id;
2244
2245 begin
2246 Convert_To_Actual_Subtype (L);
2247 Convert_To_Actual_Subtype (R);
2248 Ensure_Defined (Etype (L), N);
2249 Ensure_Defined (Etype (R), N);
2250 Apply_Length_Check (R, Etype (L));
2251
2252 if Nkind (N) = N_Op_Xor then
2253 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2254 end if;
2255
2256 if Nkind (Parent (N)) = N_Assignment_Statement
2257 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2258 then
2259 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2260
2261 elsif Nkind (Parent (N)) = N_Op_Not
2262 and then Nkind (N) = N_Op_And
2263 and then
2264 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2265 then
2266 return;
2267 else
2268
2269 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2270 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2271 Insert_Action (N, Func_Body);
2272
2273 -- Now rewrite the expression with a call
2274
2275 Rewrite (N,
2276 Make_Function_Call (Loc,
2277 Name => New_Reference_To (Func_Name, Loc),
2278 Parameter_Associations =>
2279 New_List (
2280 L,
2281 Make_Type_Conversion
2282 (Loc, New_Reference_To (Etype (L), Loc), R))));
2283
2284 Analyze_And_Resolve (N, Typ);
2285 end if;
2286 end;
2287 end Expand_Boolean_Operator;
2288
2289 ------------------------------------------------
2290 -- Expand_Compare_Minimize_Eliminate_Overflow --
2291 ------------------------------------------------
2292
2293 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2294 Loc : constant Source_Ptr := Sloc (N);
2295
2296 Llo, Lhi : Uint;
2297 Rlo, Rhi : Uint;
2298
2299 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2300 -- Entity for Long_Long_Integer'Base
2301
2302 Check : constant Overflow_Check_Type := Overflow_Check_Mode (Empty);
2303 -- Current checking mode
2304
2305 procedure Set_True;
2306 procedure Set_False;
2307 -- These procedures rewrite N with an occurrence of Standard_True or
2308 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2309
2310 ---------------
2311 -- Set_False --
2312 ---------------
2313
2314 procedure Set_False is
2315 begin
2316 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2317 Warn_On_Known_Condition (N);
2318 end Set_False;
2319
2320 --------------
2321 -- Set_True --
2322 --------------
2323
2324 procedure Set_True is
2325 begin
2326 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2327 Warn_On_Known_Condition (N);
2328 end Set_True;
2329
2330 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2331
2332 begin
2333 -- Nothing to do unless we have a comparison operator with operands
2334 -- that are signed integer types, and we are operating in either
2335 -- MINIMIZED or ELIMINATED overflow checking mode.
2336
2337 if Nkind (N) not in N_Op_Compare
2338 or else Check not in Minimized_Or_Eliminated
2339 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2340 then
2341 return;
2342 end if;
2343
2344 -- OK, this is the case we are interested in. First step is to process
2345 -- our operands using the Minimize_Eliminate circuitry which applies
2346 -- this processing to the two operand subtrees.
2347
2348 Minimize_Eliminate_Overflow_Checks
2349 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2350 Minimize_Eliminate_Overflow_Checks
2351 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2352
2353 -- See if the range information decides the result of the comparison
2354
2355 case N_Op_Compare (Nkind (N)) is
2356 when N_Op_Eq =>
2357 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2358 Set_True;
2359 elsif Llo > Rhi or else Rlo > Lhi then
2360 Set_False;
2361 end if;
2362
2363 when N_Op_Ge =>
2364 if Llo >= Rhi then
2365 Set_True;
2366 elsif Lhi < Rlo then
2367 Set_False;
2368 end if;
2369
2370 when N_Op_Gt =>
2371 if Llo > Rhi then
2372 Set_True;
2373 elsif Lhi <= Rlo then
2374 Set_False;
2375 end if;
2376
2377 when N_Op_Le =>
2378 if Llo > Rhi then
2379 Set_False;
2380 elsif Lhi <= Rlo then
2381 Set_True;
2382 end if;
2383
2384 when N_Op_Lt =>
2385 if Llo >= Rhi then
2386 Set_True;
2387 elsif Lhi < Rlo then
2388 Set_False;
2389 end if;
2390
2391 when N_Op_Ne =>
2392 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2393 Set_True;
2394 elsif Llo > Rhi or else Rlo > Lhi then
2395 Set_False;
2396 end if;
2397 end case;
2398
2399 -- All done if we did the rewrite
2400
2401 if Nkind (N) not in N_Op_Compare then
2402 return;
2403 end if;
2404
2405 -- Otherwise, time to do the comparison
2406
2407 declare
2408 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2409 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2410
2411 begin
2412 -- If the two operands have the same signed integer type we are
2413 -- all set, nothing more to do. This is the case where either
2414 -- both operands were unchanged, or we rewrote both of them to
2415 -- be Long_Long_Integer.
2416
2417 -- Note: Entity for the comparison may be wrong, but it's not worth
2418 -- the effort to change it, since the back end does not use it.
2419
2420 if Is_Signed_Integer_Type (Ltype)
2421 and then Base_Type (Ltype) = Base_Type (Rtype)
2422 then
2423 return;
2424
2425 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2426
2427 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2428 declare
2429 Left : Node_Id := Left_Opnd (N);
2430 Right : Node_Id := Right_Opnd (N);
2431 -- Bignum references for left and right operands
2432
2433 begin
2434 if not Is_RTE (Ltype, RE_Bignum) then
2435 Left := Convert_To_Bignum (Left);
2436 elsif not Is_RTE (Rtype, RE_Bignum) then
2437 Right := Convert_To_Bignum (Right);
2438 end if;
2439
2440 -- We need a sequence that looks like
2441
2442 -- Bnn : Boolean;
2443
2444 -- declare
2445 -- M : Mark_Id := SS_Mark;
2446 -- begin
2447 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2448 -- SS_Release (M);
2449 -- end;
2450
2451 -- This block is inserted (using Insert_Actions), and then the
2452 -- node is replaced with a reference to Bnn.
2453
2454 declare
2455 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2456 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2457 Ent : RE_Id;
2458
2459 begin
2460 case N_Op_Compare (Nkind (N)) is
2461 when N_Op_Eq => Ent := RE_Big_EQ;
2462 when N_Op_Ge => Ent := RE_Big_GE;
2463 when N_Op_Gt => Ent := RE_Big_GT;
2464 when N_Op_Le => Ent := RE_Big_LE;
2465 when N_Op_Lt => Ent := RE_Big_LT;
2466 when N_Op_Ne => Ent := RE_Big_NE;
2467 end case;
2468
2469 -- Insert assignment to Bnn
2470
2471 Insert_Before
2472 (First (Statements (Handled_Statement_Sequence (Blk))),
2473 Make_Assignment_Statement (Loc,
2474 Name => New_Occurrence_Of (Bnn, Loc),
2475 Expression =>
2476 Make_Function_Call (Loc,
2477 Name =>
2478 New_Occurrence_Of (RTE (Ent), Loc),
2479 Parameter_Associations => New_List (Left, Right))));
2480
2481 -- Insert actions (declaration of Bnn and block)
2482
2483 Insert_Actions (N, New_List (
2484 Make_Object_Declaration (Loc,
2485 Defining_Identifier => Bnn,
2486 Object_Definition =>
2487 New_Occurrence_Of (Standard_Boolean, Loc)),
2488 Blk));
2489
2490 -- Rewrite node with reference to Bnn
2491
2492 Rewrite (N, New_Occurrence_Of (Bnn, Loc));
2493 Analyze_And_Resolve (N);
2494 end;
2495 end;
2496
2497 -- No bignums involved, but types are different, so we must have
2498 -- rewritten one of the operands as a Long_Long_Integer but not
2499 -- the other one.
2500
2501 -- If left operand is Long_Long_Integer, convert right operand
2502 -- and we are done (with a comparison of two Long_Long_Integers).
2503
2504 elsif Ltype = LLIB then
2505 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2506 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2507 return;
2508
2509 -- If right operand is Long_Long_Integer, convert left operand
2510 -- and we are done (with a comparison of two Long_Long_Integers).
2511
2512 -- This is the only remaining possibility
2513
2514 else pragma Assert (Rtype = LLIB);
2515 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2516 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2517 return;
2518 end if;
2519 end;
2520 end Expand_Compare_Minimize_Eliminate_Overflow;
2521
2522 -------------------------------
2523 -- Expand_Composite_Equality --
2524 -------------------------------
2525
2526 -- This function is only called for comparing internal fields of composite
2527 -- types when these fields are themselves composites. This is a special
2528 -- case because it is not possible to respect normal Ada visibility rules.
2529
2530 function Expand_Composite_Equality
2531 (Nod : Node_Id;
2532 Typ : Entity_Id;
2533 Lhs : Node_Id;
2534 Rhs : Node_Id;
2535 Bodies : List_Id) return Node_Id
2536 is
2537 Loc : constant Source_Ptr := Sloc (Nod);
2538 Full_Type : Entity_Id;
2539 Prim : Elmt_Id;
2540 Eq_Op : Entity_Id;
2541
2542 function Find_Primitive_Eq return Node_Id;
2543 -- AI05-0123: Locate primitive equality for type if it exists, and
2544 -- build the corresponding call. If operation is abstract, replace
2545 -- call with an explicit raise. Return Empty if there is no primitive.
2546
2547 -----------------------
2548 -- Find_Primitive_Eq --
2549 -----------------------
2550
2551 function Find_Primitive_Eq return Node_Id is
2552 Prim_E : Elmt_Id;
2553 Prim : Node_Id;
2554
2555 begin
2556 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2557 while Present (Prim_E) loop
2558 Prim := Node (Prim_E);
2559
2560 -- Locate primitive equality with the right signature
2561
2562 if Chars (Prim) = Name_Op_Eq
2563 and then Etype (First_Formal (Prim)) =
2564 Etype (Next_Formal (First_Formal (Prim)))
2565 and then Etype (Prim) = Standard_Boolean
2566 then
2567 if Is_Abstract_Subprogram (Prim) then
2568 return
2569 Make_Raise_Program_Error (Loc,
2570 Reason => PE_Explicit_Raise);
2571
2572 else
2573 return
2574 Make_Function_Call (Loc,
2575 Name => New_Reference_To (Prim, Loc),
2576 Parameter_Associations => New_List (Lhs, Rhs));
2577 end if;
2578 end if;
2579
2580 Next_Elmt (Prim_E);
2581 end loop;
2582
2583 -- If not found, predefined operation will be used
2584
2585 return Empty;
2586 end Find_Primitive_Eq;
2587
2588 -- Start of processing for Expand_Composite_Equality
2589
2590 begin
2591 if Is_Private_Type (Typ) then
2592 Full_Type := Underlying_Type (Typ);
2593 else
2594 Full_Type := Typ;
2595 end if;
2596
2597 -- Defense against malformed private types with no completion the error
2598 -- will be diagnosed later by check_completion
2599
2600 if No (Full_Type) then
2601 return New_Reference_To (Standard_False, Loc);
2602 end if;
2603
2604 Full_Type := Base_Type (Full_Type);
2605
2606 if Is_Array_Type (Full_Type) then
2607
2608 -- If the operand is an elementary type other than a floating-point
2609 -- type, then we can simply use the built-in block bitwise equality,
2610 -- since the predefined equality operators always apply and bitwise
2611 -- equality is fine for all these cases.
2612
2613 if Is_Elementary_Type (Component_Type (Full_Type))
2614 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2615 then
2616 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2617
2618 -- For composite component types, and floating-point types, use the
2619 -- expansion. This deals with tagged component types (where we use
2620 -- the applicable equality routine) and floating-point, (where we
2621 -- need to worry about negative zeroes), and also the case of any
2622 -- composite type recursively containing such fields.
2623
2624 else
2625 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2626 end if;
2627
2628 elsif Is_Tagged_Type (Full_Type) then
2629
2630 -- Call the primitive operation "=" of this type
2631
2632 if Is_Class_Wide_Type (Full_Type) then
2633 Full_Type := Root_Type (Full_Type);
2634 end if;
2635
2636 -- If this is derived from an untagged private type completed with a
2637 -- tagged type, it does not have a full view, so we use the primitive
2638 -- operations of the private type. This check should no longer be
2639 -- necessary when these types receive their full views ???
2640
2641 if Is_Private_Type (Typ)
2642 and then not Is_Tagged_Type (Typ)
2643 and then not Is_Controlled (Typ)
2644 and then Is_Derived_Type (Typ)
2645 and then No (Full_View (Typ))
2646 then
2647 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2648 else
2649 Prim := First_Elmt (Primitive_Operations (Full_Type));
2650 end if;
2651
2652 loop
2653 Eq_Op := Node (Prim);
2654 exit when Chars (Eq_Op) = Name_Op_Eq
2655 and then Etype (First_Formal (Eq_Op)) =
2656 Etype (Next_Formal (First_Formal (Eq_Op)))
2657 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2658 Next_Elmt (Prim);
2659 pragma Assert (Present (Prim));
2660 end loop;
2661
2662 Eq_Op := Node (Prim);
2663
2664 return
2665 Make_Function_Call (Loc,
2666 Name => New_Reference_To (Eq_Op, Loc),
2667 Parameter_Associations =>
2668 New_List
2669 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2670 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2671
2672 elsif Is_Record_Type (Full_Type) then
2673 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2674
2675 if Present (Eq_Op) then
2676 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2677
2678 -- Inherited equality from parent type. Convert the actuals to
2679 -- match signature of operation.
2680
2681 declare
2682 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2683
2684 begin
2685 return
2686 Make_Function_Call (Loc,
2687 Name => New_Reference_To (Eq_Op, Loc),
2688 Parameter_Associations => New_List (
2689 OK_Convert_To (T, Lhs),
2690 OK_Convert_To (T, Rhs)));
2691 end;
2692
2693 else
2694 -- Comparison between Unchecked_Union components
2695
2696 if Is_Unchecked_Union (Full_Type) then
2697 declare
2698 Lhs_Type : Node_Id := Full_Type;
2699 Rhs_Type : Node_Id := Full_Type;
2700 Lhs_Discr_Val : Node_Id;
2701 Rhs_Discr_Val : Node_Id;
2702
2703 begin
2704 -- Lhs subtype
2705
2706 if Nkind (Lhs) = N_Selected_Component then
2707 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2708 end if;
2709
2710 -- Rhs subtype
2711
2712 if Nkind (Rhs) = N_Selected_Component then
2713 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2714 end if;
2715
2716 -- Lhs of the composite equality
2717
2718 if Is_Constrained (Lhs_Type) then
2719
2720 -- Since the enclosing record type can never be an
2721 -- Unchecked_Union (this code is executed for records
2722 -- that do not have variants), we may reference its
2723 -- discriminant(s).
2724
2725 if Nkind (Lhs) = N_Selected_Component
2726 and then Has_Per_Object_Constraint (
2727 Entity (Selector_Name (Lhs)))
2728 then
2729 Lhs_Discr_Val :=
2730 Make_Selected_Component (Loc,
2731 Prefix => Prefix (Lhs),
2732 Selector_Name =>
2733 New_Copy
2734 (Get_Discriminant_Value
2735 (First_Discriminant (Lhs_Type),
2736 Lhs_Type,
2737 Stored_Constraint (Lhs_Type))));
2738
2739 else
2740 Lhs_Discr_Val :=
2741 New_Copy
2742 (Get_Discriminant_Value
2743 (First_Discriminant (Lhs_Type),
2744 Lhs_Type,
2745 Stored_Constraint (Lhs_Type)));
2746
2747 end if;
2748 else
2749 -- It is not possible to infer the discriminant since
2750 -- the subtype is not constrained.
2751
2752 return
2753 Make_Raise_Program_Error (Loc,
2754 Reason => PE_Unchecked_Union_Restriction);
2755 end if;
2756
2757 -- Rhs of the composite equality
2758
2759 if Is_Constrained (Rhs_Type) then
2760 if Nkind (Rhs) = N_Selected_Component
2761 and then Has_Per_Object_Constraint
2762 (Entity (Selector_Name (Rhs)))
2763 then
2764 Rhs_Discr_Val :=
2765 Make_Selected_Component (Loc,
2766 Prefix => Prefix (Rhs),
2767 Selector_Name =>
2768 New_Copy
2769 (Get_Discriminant_Value
2770 (First_Discriminant (Rhs_Type),
2771 Rhs_Type,
2772 Stored_Constraint (Rhs_Type))));
2773
2774 else
2775 Rhs_Discr_Val :=
2776 New_Copy
2777 (Get_Discriminant_Value
2778 (First_Discriminant (Rhs_Type),
2779 Rhs_Type,
2780 Stored_Constraint (Rhs_Type)));
2781
2782 end if;
2783 else
2784 return
2785 Make_Raise_Program_Error (Loc,
2786 Reason => PE_Unchecked_Union_Restriction);
2787 end if;
2788
2789 -- Call the TSS equality function with the inferred
2790 -- discriminant values.
2791
2792 return
2793 Make_Function_Call (Loc,
2794 Name => New_Reference_To (Eq_Op, Loc),
2795 Parameter_Associations => New_List (
2796 Lhs,
2797 Rhs,
2798 Lhs_Discr_Val,
2799 Rhs_Discr_Val));
2800 end;
2801
2802 else
2803 return
2804 Make_Function_Call (Loc,
2805 Name => New_Reference_To (Eq_Op, Loc),
2806 Parameter_Associations => New_List (Lhs, Rhs));
2807 end if;
2808 end if;
2809
2810 -- Equality composes in Ada 2012 for untagged record types. It also
2811 -- composes for bounded strings, because they are part of the
2812 -- predefined environment. We could make it compose for bounded
2813 -- strings by making them tagged, or by making sure all subcomponents
2814 -- are set to the same value, even when not used. Instead, we have
2815 -- this special case in the compiler, because it's more efficient.
2816
2817 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2818
2819 -- if no TSS has been created for the type, check whether there is
2820 -- a primitive equality declared for it.
2821
2822 declare
2823 Op : constant Node_Id := Find_Primitive_Eq;
2824
2825 begin
2826 -- Use user-defined primitive if it exists, otherwise use
2827 -- predefined equality.
2828
2829 if Present (Op) then
2830 return Op;
2831 else
2832 return Make_Op_Eq (Loc, Lhs, Rhs);
2833 end if;
2834 end;
2835
2836 else
2837 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2838 end if;
2839
2840 else
2841 -- If not array or record type, it is predefined equality.
2842
2843 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2844 end if;
2845 end Expand_Composite_Equality;
2846
2847 ------------------------
2848 -- Expand_Concatenate --
2849 ------------------------
2850
2851 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2852 Loc : constant Source_Ptr := Sloc (Cnode);
2853
2854 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2855 -- Result type of concatenation
2856
2857 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2858 -- Component type. Elements of this component type can appear as one
2859 -- of the operands of concatenation as well as arrays.
2860
2861 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2862 -- Index subtype
2863
2864 Ityp : constant Entity_Id := Base_Type (Istyp);
2865 -- Index type. This is the base type of the index subtype, and is used
2866 -- for all computed bounds (which may be out of range of Istyp in the
2867 -- case of null ranges).
2868
2869 Artyp : Entity_Id;
2870 -- This is the type we use to do arithmetic to compute the bounds and
2871 -- lengths of operands. The choice of this type is a little subtle and
2872 -- is discussed in a separate section at the start of the body code.
2873
2874 Concatenation_Error : exception;
2875 -- Raised if concatenation is sure to raise a CE
2876
2877 Result_May_Be_Null : Boolean := True;
2878 -- Reset to False if at least one operand is encountered which is known
2879 -- at compile time to be non-null. Used for handling the special case
2880 -- of setting the high bound to the last operand high bound for a null
2881 -- result, thus ensuring a proper high bound in the super-flat case.
2882
2883 N : constant Nat := List_Length (Opnds);
2884 -- Number of concatenation operands including possibly null operands
2885
2886 NN : Nat := 0;
2887 -- Number of operands excluding any known to be null, except that the
2888 -- last operand is always retained, in case it provides the bounds for
2889 -- a null result.
2890
2891 Opnd : Node_Id;
2892 -- Current operand being processed in the loop through operands. After
2893 -- this loop is complete, always contains the last operand (which is not
2894 -- the same as Operands (NN), since null operands are skipped).
2895
2896 -- Arrays describing the operands, only the first NN entries of each
2897 -- array are set (NN < N when we exclude known null operands).
2898
2899 Is_Fixed_Length : array (1 .. N) of Boolean;
2900 -- True if length of corresponding operand known at compile time
2901
2902 Operands : array (1 .. N) of Node_Id;
2903 -- Set to the corresponding entry in the Opnds list (but note that null
2904 -- operands are excluded, so not all entries in the list are stored).
2905
2906 Fixed_Length : array (1 .. N) of Uint;
2907 -- Set to length of operand. Entries in this array are set only if the
2908 -- corresponding entry in Is_Fixed_Length is True.
2909
2910 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2911 -- Set to lower bound of operand. Either an integer literal in the case
2912 -- where the bound is known at compile time, else actual lower bound.
2913 -- The operand low bound is of type Ityp.
2914
2915 Var_Length : array (1 .. N) of Entity_Id;
2916 -- Set to an entity of type Natural that contains the length of an
2917 -- operand whose length is not known at compile time. Entries in this
2918 -- array are set only if the corresponding entry in Is_Fixed_Length
2919 -- is False. The entity is of type Artyp.
2920
2921 Aggr_Length : array (0 .. N) of Node_Id;
2922 -- The J'th entry in an expression node that represents the total length
2923 -- of operands 1 through J. It is either an integer literal node, or a
2924 -- reference to a constant entity with the right value, so it is fine
2925 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2926 -- entry always is set to zero. The length is of type Artyp.
2927
2928 Low_Bound : Node_Id;
2929 -- A tree node representing the low bound of the result (of type Ityp).
2930 -- This is either an integer literal node, or an identifier reference to
2931 -- a constant entity initialized to the appropriate value.
2932
2933 Last_Opnd_Low_Bound : Node_Id;
2934 -- A tree node representing the low bound of the last operand. This
2935 -- need only be set if the result could be null. It is used for the
2936 -- special case of setting the right low bound for a null result.
2937 -- This is of type Ityp.
2938
2939 Last_Opnd_High_Bound : Node_Id;
2940 -- A tree node representing the high bound of the last operand. This
2941 -- need only be set if the result could be null. It is used for the
2942 -- special case of setting the right high bound for a null result.
2943 -- This is of type Ityp.
2944
2945 High_Bound : Node_Id;
2946 -- A tree node representing the high bound of the result (of type Ityp)
2947
2948 Result : Node_Id;
2949 -- Result of the concatenation (of type Ityp)
2950
2951 Actions : constant List_Id := New_List;
2952 -- Collect actions to be inserted
2953
2954 Known_Non_Null_Operand_Seen : Boolean;
2955 -- Set True during generation of the assignments of operands into
2956 -- result once an operand known to be non-null has been seen.
2957
2958 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2959 -- This function makes an N_Integer_Literal node that is returned in
2960 -- analyzed form with the type set to Artyp. Importantly this literal
2961 -- is not flagged as static, so that if we do computations with it that
2962 -- result in statically detected out of range conditions, we will not
2963 -- generate error messages but instead warning messages.
2964
2965 function To_Artyp (X : Node_Id) return Node_Id;
2966 -- Given a node of type Ityp, returns the corresponding value of type
2967 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2968 -- For enum types, the Pos of the value is returned.
2969
2970 function To_Ityp (X : Node_Id) return Node_Id;
2971 -- The inverse function (uses Val in the case of enumeration types)
2972
2973 ------------------------
2974 -- Make_Artyp_Literal --
2975 ------------------------
2976
2977 function Make_Artyp_Literal (Val : Nat) return Node_Id is
2978 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
2979 begin
2980 Set_Etype (Result, Artyp);
2981 Set_Analyzed (Result, True);
2982 Set_Is_Static_Expression (Result, False);
2983 return Result;
2984 end Make_Artyp_Literal;
2985
2986 --------------
2987 -- To_Artyp --
2988 --------------
2989
2990 function To_Artyp (X : Node_Id) return Node_Id is
2991 begin
2992 if Ityp = Base_Type (Artyp) then
2993 return X;
2994
2995 elsif Is_Enumeration_Type (Ityp) then
2996 return
2997 Make_Attribute_Reference (Loc,
2998 Prefix => New_Occurrence_Of (Ityp, Loc),
2999 Attribute_Name => Name_Pos,
3000 Expressions => New_List (X));
3001
3002 else
3003 return Convert_To (Artyp, X);
3004 end if;
3005 end To_Artyp;
3006
3007 -------------
3008 -- To_Ityp --
3009 -------------
3010
3011 function To_Ityp (X : Node_Id) return Node_Id is
3012 begin
3013 if Is_Enumeration_Type (Ityp) then
3014 return
3015 Make_Attribute_Reference (Loc,
3016 Prefix => New_Occurrence_Of (Ityp, Loc),
3017 Attribute_Name => Name_Val,
3018 Expressions => New_List (X));
3019
3020 -- Case where we will do a type conversion
3021
3022 else
3023 if Ityp = Base_Type (Artyp) then
3024 return X;
3025 else
3026 return Convert_To (Ityp, X);
3027 end if;
3028 end if;
3029 end To_Ityp;
3030
3031 -- Local Declarations
3032
3033 Opnd_Typ : Entity_Id;
3034 Ent : Entity_Id;
3035 Len : Uint;
3036 J : Nat;
3037 Clen : Node_Id;
3038 Set : Boolean;
3039
3040 -- Start of processing for Expand_Concatenate
3041
3042 begin
3043 -- Choose an appropriate computational type
3044
3045 -- We will be doing calculations of lengths and bounds in this routine
3046 -- and computing one from the other in some cases, e.g. getting the high
3047 -- bound by adding the length-1 to the low bound.
3048
3049 -- We can't just use the index type, or even its base type for this
3050 -- purpose for two reasons. First it might be an enumeration type which
3051 -- is not suitable for computations of any kind, and second it may
3052 -- simply not have enough range. For example if the index type is
3053 -- -128..+127 then lengths can be up to 256, which is out of range of
3054 -- the type.
3055
3056 -- For enumeration types, we can simply use Standard_Integer, this is
3057 -- sufficient since the actual number of enumeration literals cannot
3058 -- possibly exceed the range of integer (remember we will be doing the
3059 -- arithmetic with POS values, not representation values).
3060
3061 if Is_Enumeration_Type (Ityp) then
3062 Artyp := Standard_Integer;
3063
3064 -- If index type is Positive, we use the standard unsigned type, to give
3065 -- more room on the top of the range, obviating the need for an overflow
3066 -- check when creating the upper bound. This is needed to avoid junk
3067 -- overflow checks in the common case of String types.
3068
3069 -- ??? Disabled for now
3070
3071 -- elsif Istyp = Standard_Positive then
3072 -- Artyp := Standard_Unsigned;
3073
3074 -- For modular types, we use a 32-bit modular type for types whose size
3075 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3076 -- identity type, and for larger unsigned types we use 64-bits.
3077
3078 elsif Is_Modular_Integer_Type (Ityp) then
3079 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3080 Artyp := Standard_Unsigned;
3081 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3082 Artyp := Ityp;
3083 else
3084 Artyp := RTE (RE_Long_Long_Unsigned);
3085 end if;
3086
3087 -- Similar treatment for signed types
3088
3089 else
3090 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3091 Artyp := Standard_Integer;
3092 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3093 Artyp := Ityp;
3094 else
3095 Artyp := Standard_Long_Long_Integer;
3096 end if;
3097 end if;
3098
3099 -- Supply dummy entry at start of length array
3100
3101 Aggr_Length (0) := Make_Artyp_Literal (0);
3102
3103 -- Go through operands setting up the above arrays
3104
3105 J := 1;
3106 while J <= N loop
3107 Opnd := Remove_Head (Opnds);
3108 Opnd_Typ := Etype (Opnd);
3109
3110 -- The parent got messed up when we put the operands in a list,
3111 -- so now put back the proper parent for the saved operand, that
3112 -- is to say the concatenation node, to make sure that each operand
3113 -- is seen as a subexpression, e.g. if actions must be inserted.
3114
3115 Set_Parent (Opnd, Cnode);
3116
3117 -- Set will be True when we have setup one entry in the array
3118
3119 Set := False;
3120
3121 -- Singleton element (or character literal) case
3122
3123 if Base_Type (Opnd_Typ) = Ctyp then
3124 NN := NN + 1;
3125 Operands (NN) := Opnd;
3126 Is_Fixed_Length (NN) := True;
3127 Fixed_Length (NN) := Uint_1;
3128 Result_May_Be_Null := False;
3129
3130 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3131 -- since we know that the result cannot be null).
3132
3133 Opnd_Low_Bound (NN) :=
3134 Make_Attribute_Reference (Loc,
3135 Prefix => New_Reference_To (Istyp, Loc),
3136 Attribute_Name => Name_First);
3137
3138 Set := True;
3139
3140 -- String literal case (can only occur for strings of course)
3141
3142 elsif Nkind (Opnd) = N_String_Literal then
3143 Len := String_Literal_Length (Opnd_Typ);
3144
3145 if Len /= 0 then
3146 Result_May_Be_Null := False;
3147 end if;
3148
3149 -- Capture last operand low and high bound if result could be null
3150
3151 if J = N and then Result_May_Be_Null then
3152 Last_Opnd_Low_Bound :=
3153 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3154
3155 Last_Opnd_High_Bound :=
3156 Make_Op_Subtract (Loc,
3157 Left_Opnd =>
3158 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3159 Right_Opnd => Make_Integer_Literal (Loc, 1));
3160 end if;
3161
3162 -- Skip null string literal
3163
3164 if J < N and then Len = 0 then
3165 goto Continue;
3166 end if;
3167
3168 NN := NN + 1;
3169 Operands (NN) := Opnd;
3170 Is_Fixed_Length (NN) := True;
3171
3172 -- Set length and bounds
3173
3174 Fixed_Length (NN) := Len;
3175
3176 Opnd_Low_Bound (NN) :=
3177 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3178
3179 Set := True;
3180
3181 -- All other cases
3182
3183 else
3184 -- Check constrained case with known bounds
3185
3186 if Is_Constrained (Opnd_Typ) then
3187 declare
3188 Index : constant Node_Id := First_Index (Opnd_Typ);
3189 Indx_Typ : constant Entity_Id := Etype (Index);
3190 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3191 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3192
3193 begin
3194 -- Fixed length constrained array type with known at compile
3195 -- time bounds is last case of fixed length operand.
3196
3197 if Compile_Time_Known_Value (Lo)
3198 and then
3199 Compile_Time_Known_Value (Hi)
3200 then
3201 declare
3202 Loval : constant Uint := Expr_Value (Lo);
3203 Hival : constant Uint := Expr_Value (Hi);
3204 Len : constant Uint :=
3205 UI_Max (Hival - Loval + 1, Uint_0);
3206
3207 begin
3208 if Len > 0 then
3209 Result_May_Be_Null := False;
3210 end if;
3211
3212 -- Capture last operand bounds if result could be null
3213
3214 if J = N and then Result_May_Be_Null then
3215 Last_Opnd_Low_Bound :=
3216 Convert_To (Ityp,
3217 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3218
3219 Last_Opnd_High_Bound :=
3220 Convert_To (Ityp,
3221 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3222 end if;
3223
3224 -- Exclude null length case unless last operand
3225
3226 if J < N and then Len = 0 then
3227 goto Continue;
3228 end if;
3229
3230 NN := NN + 1;
3231 Operands (NN) := Opnd;
3232 Is_Fixed_Length (NN) := True;
3233 Fixed_Length (NN) := Len;
3234
3235 Opnd_Low_Bound (NN) :=
3236 To_Ityp
3237 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3238 Set := True;
3239 end;
3240 end if;
3241 end;
3242 end if;
3243
3244 -- All cases where the length is not known at compile time, or the
3245 -- special case of an operand which is known to be null but has a
3246 -- lower bound other than 1 or is other than a string type.
3247
3248 if not Set then
3249 NN := NN + 1;
3250
3251 -- Capture operand bounds
3252
3253 Opnd_Low_Bound (NN) :=
3254 Make_Attribute_Reference (Loc,
3255 Prefix =>
3256 Duplicate_Subexpr (Opnd, Name_Req => True),
3257 Attribute_Name => Name_First);
3258
3259 -- Capture last operand bounds if result could be null
3260
3261 if J = N and Result_May_Be_Null then
3262 Last_Opnd_Low_Bound :=
3263 Convert_To (Ityp,
3264 Make_Attribute_Reference (Loc,
3265 Prefix =>
3266 Duplicate_Subexpr (Opnd, Name_Req => True),
3267 Attribute_Name => Name_First));
3268
3269 Last_Opnd_High_Bound :=
3270 Convert_To (Ityp,
3271 Make_Attribute_Reference (Loc,
3272 Prefix =>
3273 Duplicate_Subexpr (Opnd, Name_Req => True),
3274 Attribute_Name => Name_Last));
3275 end if;
3276
3277 -- Capture length of operand in entity
3278
3279 Operands (NN) := Opnd;
3280 Is_Fixed_Length (NN) := False;
3281
3282 Var_Length (NN) := Make_Temporary (Loc, 'L');
3283
3284 Append_To (Actions,
3285 Make_Object_Declaration (Loc,
3286 Defining_Identifier => Var_Length (NN),
3287 Constant_Present => True,
3288 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3289 Expression =>
3290 Make_Attribute_Reference (Loc,
3291 Prefix =>
3292 Duplicate_Subexpr (Opnd, Name_Req => True),
3293 Attribute_Name => Name_Length)));
3294 end if;
3295 end if;
3296
3297 -- Set next entry in aggregate length array
3298
3299 -- For first entry, make either integer literal for fixed length
3300 -- or a reference to the saved length for variable length.
3301
3302 if NN = 1 then
3303 if Is_Fixed_Length (1) then
3304 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3305 else
3306 Aggr_Length (1) := New_Reference_To (Var_Length (1), Loc);
3307 end if;
3308
3309 -- If entry is fixed length and only fixed lengths so far, make
3310 -- appropriate new integer literal adding new length.
3311
3312 elsif Is_Fixed_Length (NN)
3313 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3314 then
3315 Aggr_Length (NN) :=
3316 Make_Integer_Literal (Loc,
3317 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3318
3319 -- All other cases, construct an addition node for the length and
3320 -- create an entity initialized to this length.
3321
3322 else
3323 Ent := Make_Temporary (Loc, 'L');
3324
3325 if Is_Fixed_Length (NN) then
3326 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3327 else
3328 Clen := New_Reference_To (Var_Length (NN), Loc);
3329 end if;
3330
3331 Append_To (Actions,
3332 Make_Object_Declaration (Loc,
3333 Defining_Identifier => Ent,
3334 Constant_Present => True,
3335 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3336 Expression =>
3337 Make_Op_Add (Loc,
3338 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3339 Right_Opnd => Clen)));
3340
3341 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3342 end if;
3343
3344 <<Continue>>
3345 J := J + 1;
3346 end loop;
3347
3348 -- If we have only skipped null operands, return the last operand
3349
3350 if NN = 0 then
3351 Result := Opnd;
3352 goto Done;
3353 end if;
3354
3355 -- If we have only one non-null operand, return it and we are done.
3356 -- There is one case in which this cannot be done, and that is when
3357 -- the sole operand is of the element type, in which case it must be
3358 -- converted to an array, and the easiest way of doing that is to go
3359 -- through the normal general circuit.
3360
3361 if NN = 1
3362 and then Base_Type (Etype (Operands (1))) /= Ctyp
3363 then
3364 Result := Operands (1);
3365 goto Done;
3366 end if;
3367
3368 -- Cases where we have a real concatenation
3369
3370 -- Next step is to find the low bound for the result array that we
3371 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3372
3373 -- If the ultimate ancestor of the index subtype is a constrained array
3374 -- definition, then the lower bound is that of the index subtype as
3375 -- specified by (RM 4.5.3(6)).
3376
3377 -- The right test here is to go to the root type, and then the ultimate
3378 -- ancestor is the first subtype of this root type.
3379
3380 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3381 Low_Bound :=
3382 Make_Attribute_Reference (Loc,
3383 Prefix =>
3384 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3385 Attribute_Name => Name_First);
3386
3387 -- If the first operand in the list has known length we know that
3388 -- the lower bound of the result is the lower bound of this operand.
3389
3390 elsif Is_Fixed_Length (1) then
3391 Low_Bound := Opnd_Low_Bound (1);
3392
3393 -- OK, we don't know the lower bound, we have to build a horrible
3394 -- conditional expression node of the form
3395
3396 -- if Cond1'Length /= 0 then
3397 -- Opnd1 low bound
3398 -- else
3399 -- if Opnd2'Length /= 0 then
3400 -- Opnd2 low bound
3401 -- else
3402 -- ...
3403
3404 -- The nesting ends either when we hit an operand whose length is known
3405 -- at compile time, or on reaching the last operand, whose low bound we
3406 -- take unconditionally whether or not it is null. It's easiest to do
3407 -- this with a recursive procedure:
3408
3409 else
3410 declare
3411 function Get_Known_Bound (J : Nat) return Node_Id;
3412 -- Returns the lower bound determined by operands J .. NN
3413
3414 ---------------------
3415 -- Get_Known_Bound --
3416 ---------------------
3417
3418 function Get_Known_Bound (J : Nat) return Node_Id is
3419 begin
3420 if Is_Fixed_Length (J) or else J = NN then
3421 return New_Copy (Opnd_Low_Bound (J));
3422
3423 else
3424 return
3425 Make_Conditional_Expression (Loc,
3426 Expressions => New_List (
3427
3428 Make_Op_Ne (Loc,
3429 Left_Opnd => New_Reference_To (Var_Length (J), Loc),
3430 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3431
3432 New_Copy (Opnd_Low_Bound (J)),
3433 Get_Known_Bound (J + 1)));
3434 end if;
3435 end Get_Known_Bound;
3436
3437 begin
3438 Ent := Make_Temporary (Loc, 'L');
3439
3440 Append_To (Actions,
3441 Make_Object_Declaration (Loc,
3442 Defining_Identifier => Ent,
3443 Constant_Present => True,
3444 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3445 Expression => Get_Known_Bound (1)));
3446
3447 Low_Bound := New_Reference_To (Ent, Loc);
3448 end;
3449 end if;
3450
3451 -- Now we can safely compute the upper bound, normally
3452 -- Low_Bound + Length - 1.
3453
3454 High_Bound :=
3455 To_Ityp (
3456 Make_Op_Add (Loc,
3457 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3458 Right_Opnd =>
3459 Make_Op_Subtract (Loc,
3460 Left_Opnd => New_Copy (Aggr_Length (NN)),
3461 Right_Opnd => Make_Artyp_Literal (1))));
3462
3463 -- Note that calculation of the high bound may cause overflow in some
3464 -- very weird cases, so in the general case we need an overflow check on
3465 -- the high bound. We can avoid this for the common case of string types
3466 -- and other types whose index is Positive, since we chose a wider range
3467 -- for the arithmetic type.
3468
3469 if Istyp /= Standard_Positive then
3470 Activate_Overflow_Check (High_Bound);
3471 end if;
3472
3473 -- Handle the exceptional case where the result is null, in which case
3474 -- case the bounds come from the last operand (so that we get the proper
3475 -- bounds if the last operand is super-flat).
3476
3477 if Result_May_Be_Null then
3478 Low_Bound :=
3479 Make_Conditional_Expression (Loc,
3480 Expressions => New_List (
3481 Make_Op_Eq (Loc,
3482 Left_Opnd => New_Copy (Aggr_Length (NN)),
3483 Right_Opnd => Make_Artyp_Literal (0)),
3484 Last_Opnd_Low_Bound,
3485 Low_Bound));
3486
3487 High_Bound :=
3488 Make_Conditional_Expression (Loc,
3489 Expressions => New_List (
3490 Make_Op_Eq (Loc,
3491 Left_Opnd => New_Copy (Aggr_Length (NN)),
3492 Right_Opnd => Make_Artyp_Literal (0)),
3493 Last_Opnd_High_Bound,
3494 High_Bound));
3495 end if;
3496
3497 -- Here is where we insert the saved up actions
3498
3499 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3500
3501 -- Now we construct an array object with appropriate bounds. We mark
3502 -- the target as internal to prevent useless initialization when
3503 -- Initialize_Scalars is enabled. Also since this is the actual result
3504 -- entity, we make sure we have debug information for the result.
3505
3506 Ent := Make_Temporary (Loc, 'S');
3507 Set_Is_Internal (Ent);
3508 Set_Needs_Debug_Info (Ent);
3509
3510 -- If the bound is statically known to be out of range, we do not want
3511 -- to abort, we want a warning and a runtime constraint error. Note that
3512 -- we have arranged that the result will not be treated as a static
3513 -- constant, so we won't get an illegality during this insertion.
3514
3515 Insert_Action (Cnode,
3516 Make_Object_Declaration (Loc,
3517 Defining_Identifier => Ent,
3518 Object_Definition =>
3519 Make_Subtype_Indication (Loc,
3520 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3521 Constraint =>
3522 Make_Index_Or_Discriminant_Constraint (Loc,
3523 Constraints => New_List (
3524 Make_Range (Loc,
3525 Low_Bound => Low_Bound,
3526 High_Bound => High_Bound))))),
3527 Suppress => All_Checks);
3528
3529 -- If the result of the concatenation appears as the initializing
3530 -- expression of an object declaration, we can just rename the
3531 -- result, rather than copying it.
3532
3533 Set_OK_To_Rename (Ent);
3534
3535 -- Catch the static out of range case now
3536
3537 if Raises_Constraint_Error (High_Bound) then
3538 raise Concatenation_Error;
3539 end if;
3540
3541 -- Now we will generate the assignments to do the actual concatenation
3542
3543 -- There is one case in which we will not do this, namely when all the
3544 -- following conditions are met:
3545
3546 -- The result type is Standard.String
3547
3548 -- There are nine or fewer retained (non-null) operands
3549
3550 -- The optimization level is -O0
3551
3552 -- The corresponding System.Concat_n.Str_Concat_n routine is
3553 -- available in the run time.
3554
3555 -- The debug flag gnatd.c is not set
3556
3557 -- If all these conditions are met then we generate a call to the
3558 -- relevant concatenation routine. The purpose of this is to avoid
3559 -- undesirable code bloat at -O0.
3560
3561 if Atyp = Standard_String
3562 and then NN in 2 .. 9
3563 and then (Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3564 and then not Debug_Flag_Dot_C
3565 then
3566 declare
3567 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3568 (RE_Str_Concat_2,
3569 RE_Str_Concat_3,
3570 RE_Str_Concat_4,
3571 RE_Str_Concat_5,
3572 RE_Str_Concat_6,
3573 RE_Str_Concat_7,
3574 RE_Str_Concat_8,
3575 RE_Str_Concat_9);
3576
3577 begin
3578 if RTE_Available (RR (NN)) then
3579 declare
3580 Opnds : constant List_Id :=
3581 New_List (New_Occurrence_Of (Ent, Loc));
3582
3583 begin
3584 for J in 1 .. NN loop
3585 if Is_List_Member (Operands (J)) then
3586 Remove (Operands (J));
3587 end if;
3588
3589 if Base_Type (Etype (Operands (J))) = Ctyp then
3590 Append_To (Opnds,
3591 Make_Aggregate (Loc,
3592 Component_Associations => New_List (
3593 Make_Component_Association (Loc,
3594 Choices => New_List (
3595 Make_Integer_Literal (Loc, 1)),
3596 Expression => Operands (J)))));
3597
3598 else
3599 Append_To (Opnds, Operands (J));
3600 end if;
3601 end loop;
3602
3603 Insert_Action (Cnode,
3604 Make_Procedure_Call_Statement (Loc,
3605 Name => New_Reference_To (RTE (RR (NN)), Loc),
3606 Parameter_Associations => Opnds));
3607
3608 Result := New_Reference_To (Ent, Loc);
3609 goto Done;
3610 end;
3611 end if;
3612 end;
3613 end if;
3614
3615 -- Not special case so generate the assignments
3616
3617 Known_Non_Null_Operand_Seen := False;
3618
3619 for J in 1 .. NN loop
3620 declare
3621 Lo : constant Node_Id :=
3622 Make_Op_Add (Loc,
3623 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3624 Right_Opnd => Aggr_Length (J - 1));
3625
3626 Hi : constant Node_Id :=
3627 Make_Op_Add (Loc,
3628 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3629 Right_Opnd =>
3630 Make_Op_Subtract (Loc,
3631 Left_Opnd => Aggr_Length (J),
3632 Right_Opnd => Make_Artyp_Literal (1)));
3633
3634 begin
3635 -- Singleton case, simple assignment
3636
3637 if Base_Type (Etype (Operands (J))) = Ctyp then
3638 Known_Non_Null_Operand_Seen := True;
3639 Insert_Action (Cnode,
3640 Make_Assignment_Statement (Loc,
3641 Name =>
3642 Make_Indexed_Component (Loc,
3643 Prefix => New_Occurrence_Of (Ent, Loc),
3644 Expressions => New_List (To_Ityp (Lo))),
3645 Expression => Operands (J)),
3646 Suppress => All_Checks);
3647
3648 -- Array case, slice assignment, skipped when argument is fixed
3649 -- length and known to be null.
3650
3651 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3652 declare
3653 Assign : Node_Id :=
3654 Make_Assignment_Statement (Loc,
3655 Name =>
3656 Make_Slice (Loc,
3657 Prefix =>
3658 New_Occurrence_Of (Ent, Loc),
3659 Discrete_Range =>
3660 Make_Range (Loc,
3661 Low_Bound => To_Ityp (Lo),
3662 High_Bound => To_Ityp (Hi))),
3663 Expression => Operands (J));
3664 begin
3665 if Is_Fixed_Length (J) then
3666 Known_Non_Null_Operand_Seen := True;
3667
3668 elsif not Known_Non_Null_Operand_Seen then
3669
3670 -- Here if operand length is not statically known and no
3671 -- operand known to be non-null has been processed yet.
3672 -- If operand length is 0, we do not need to perform the
3673 -- assignment, and we must avoid the evaluation of the
3674 -- high bound of the slice, since it may underflow if the
3675 -- low bound is Ityp'First.
3676
3677 Assign :=
3678 Make_Implicit_If_Statement (Cnode,
3679 Condition =>
3680 Make_Op_Ne (Loc,
3681 Left_Opnd =>
3682 New_Occurrence_Of (Var_Length (J), Loc),
3683 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3684 Then_Statements => New_List (Assign));
3685 end if;
3686
3687 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3688 end;
3689 end if;
3690 end;
3691 end loop;
3692
3693 -- Finally we build the result, which is a reference to the array object
3694
3695 Result := New_Reference_To (Ent, Loc);
3696
3697 <<Done>>
3698 Rewrite (Cnode, Result);
3699 Analyze_And_Resolve (Cnode, Atyp);
3700
3701 exception
3702 when Concatenation_Error =>
3703
3704 -- Kill warning generated for the declaration of the static out of
3705 -- range high bound, and instead generate a Constraint_Error with
3706 -- an appropriate specific message.
3707
3708 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3709 Apply_Compile_Time_Constraint_Error
3710 (N => Cnode,
3711 Msg => "concatenation result upper bound out of range?",
3712 Reason => CE_Range_Check_Failed);
3713 end Expand_Concatenate;
3714
3715 ---------------------------------------------------
3716 -- Expand_Membership_Minimize_Eliminate_Overflow --
3717 ---------------------------------------------------
3718
3719 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3720 pragma Assert (Nkind (N) = N_In);
3721 -- Despite the name, this routine applies only to N_In, not to
3722 -- N_Not_In. The latter is always rewritten as not (X in Y).
3723
3724 Loc : constant Source_Ptr := Sloc (N);
3725 Lop : constant Node_Id := Left_Opnd (N);
3726 Rop : constant Node_Id := Right_Opnd (N);
3727 Ltype : constant Entity_Id := Etype (Lop);
3728 Rtype : constant Entity_Id := Etype (Rop);
3729
3730 Restype : constant Entity_Id := Etype (N);
3731 -- Save result type
3732
3733 Lo, Hi : Uint;
3734 -- Bounds in Minimize calls, not used yet ???
3735
3736 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3737 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3738
3739 begin
3740 Minimize_Eliminate_Overflow_Checks (Lop, Lo, Hi, Top_Level => False);
3741
3742 -- If right operand is a subtype name, and the subtype name has no
3743 -- predicate, then we can just replace the right operand with an
3744 -- explicit range T'First .. T'Last, and use the explicit range code.
3745
3746 if Nkind (Rop) /= N_Range and then No (Predicate_Function (Rtype)) then
3747 Rewrite (Rop,
3748 Make_Range (Loc,
3749 Low_Bound =>
3750 Make_Attribute_Reference (Loc,
3751 Attribute_Name => Name_First,
3752 Prefix => New_Reference_To (Rtype, Loc)),
3753
3754 High_Bound =>
3755 Make_Attribute_Reference (Loc,
3756 Attribute_Name => Name_Last,
3757 Prefix => New_Reference_To (Rtype, Loc))));
3758 Analyze_And_Resolve (Rop, Rtype, Suppress => All_Checks);
3759 end if;
3760
3761 -- Here for the explicit range case. Note that the bounds of the range
3762 -- have not been processed for minimized or eliminated checks.
3763
3764 if Nkind (Rop) = N_Range then
3765 Minimize_Eliminate_Overflow_Checks
3766 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3767 Minimize_Eliminate_Overflow_Checks
3768 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3769
3770 -- We have A in B .. C, treated as A >= B and then A <= C
3771
3772 -- Bignum case
3773
3774 if Is_RTE (Ltype, RE_Bignum)
3775 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3776 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3777 then
3778 declare
3779 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3780 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3781 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3782 Lbound : constant Node_Id :=
3783 Convert_To_Bignum (Low_Bound (Rop));
3784 Hbound : constant Node_Id :=
3785 Convert_To_Bignum (High_Bound (Rop));
3786
3787 -- Now we insert code that looks like
3788
3789 -- Bnn : Boolean;
3790
3791 -- declare
3792 -- M : Mark_Id := SS_Mark;
3793 -- L : Bignum := Lopnd;
3794 -- begin
3795 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3796 -- SS_Release (M);
3797 -- end;
3798
3799 -- and rewrite the membership test as a reference to Bnn
3800
3801 begin
3802 Insert_After
3803 (Last (Declarations (Blk)),
3804 Make_Object_Declaration (Loc,
3805 Defining_Identifier => Bnn,
3806 Object_Definition =>
3807 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3808 Expression => Lopnd));
3809
3810 Insert_Before
3811 (First (Statements (Handled_Statement_Sequence (Blk))),
3812 Make_Assignment_Statement (Loc,
3813 Name => New_Occurrence_Of (Bnn, Loc),
3814 Expression =>
3815 Make_And_Then (Loc,
3816 Left_Opnd =>
3817 Make_Function_Call (Loc,
3818 Name =>
3819 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3820 Parameter_Associations => New_List (Lbound)),
3821 Right_Opnd =>
3822 Make_Function_Call (Loc,
3823 Name =>
3824 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3825 Parameter_Associations => New_List (Hbound)))));
3826
3827 Insert_Actions (N, New_List (
3828 Make_Object_Declaration (Loc,
3829 Defining_Identifier => Bnn,
3830 Object_Definition =>
3831 New_Occurrence_Of (Standard_Boolean, Loc)),
3832 Blk));
3833
3834 Rewrite (N, New_Occurrence_Of (Bnn, Loc));
3835 Analyze_And_Resolve (N);
3836 return;
3837 end;
3838
3839 -- Here if no bignums around
3840
3841 else
3842 -- Case where types are all the same
3843
3844 if Ltype = Etype (Low_Bound (Rop))
3845 and then
3846 Ltype = Etype (High_Bound (Rop))
3847 then
3848 null;
3849
3850 -- If types are not all the same, it means that we have rewritten
3851 -- at least one of them to be of type Long_Long_Integer, and we
3852 -- will convert the other operands to Long_Long_Integer.
3853
3854 else
3855 Convert_To_And_Rewrite (LLIB, Lop);
3856 Analyze_And_Resolve (Lop, LLIB, Suppress => All_Checks);
3857
3858 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3859 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3860 Set_Analyzed (Rop, False);
3861 Analyze_And_Resolve (Rop, LLIB, Suppress => All_Checks);
3862 end if;
3863
3864 -- Now the three operands are of the same signed integer type,
3865 -- so we can use the normal expansion routine for membership.
3866
3867 Set_No_Minimize_Eliminate (N);
3868 Expand_N_In (N);
3869 end if;
3870
3871 -- Right operand is a subtype name and the subtype has a predicate. We
3872 -- have to make sure predicate is checked, and for that we need to use
3873 -- the standard N_In circuitry with appropriate types.
3874
3875 else
3876 pragma Assert (Present (Predicate_Function (Rtype)));
3877
3878 -- If types are "right", just call Expand_N_In preventing recursion
3879
3880 if Base_Type (Ltype) = Base_Type (Rtype) then
3881 Set_No_Minimize_Eliminate (N);
3882 Expand_N_In (N);
3883
3884 -- Bignum case
3885
3886 elsif Is_RTE (Ltype, RE_Bignum) then
3887
3888 -- For X in T, we want to insert code that looks like
3889
3890 -- Bnn : Boolean;
3891
3892 -- declare
3893 -- M : Mark_Id := SS_Mark;
3894 -- Lnn : Long_Long_Integer'Base
3895 -- Nnn : Bignum;
3896
3897 -- begin
3898 -- Nnn := X;
3899
3900 -- if not Bignum_In_LLI_Range (Nnn) then
3901 -- Bnn := False;
3902 -- else
3903 -- Lnn := From_Bignum (Nnn);
3904 -- Bnn := Lnn in T'Base and then T'Base (Lnn) in T;
3905 -- end if;
3906 --
3907 -- SS_Release (M);
3908 -- end;
3909
3910 -- And then rewrite the original membership as a reference to Bnn.
3911 -- A bit gruesome, but here goes.
3912
3913 declare
3914 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3915 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3916 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3917 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3918 Nin : Node_Id;
3919
3920 begin
3921 -- The last membership test is marked to prevent recursion
3922
3923 Nin :=
3924 Make_In (Loc,
3925 Left_Opnd =>
3926 Convert_To (Base_Type (Rtype),
3927 New_Occurrence_Of (Lnn, Loc)),
3928 Right_Opnd => New_Occurrence_Of (Rtype, Loc));
3929 Set_No_Minimize_Eliminate (Nin);
3930
3931 -- Now decorate the block
3932
3933 Insert_After
3934 (Last (Declarations (Blk)),
3935 Make_Object_Declaration (Loc,
3936 Defining_Identifier => Lnn,
3937 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
3938
3939 Insert_After
3940 (Last (Declarations (Blk)),
3941 Make_Object_Declaration (Loc,
3942 Defining_Identifier => Nnn,
3943 Object_Definition =>
3944 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
3945
3946 Insert_List_Before
3947 (First (Statements (Handled_Statement_Sequence (Blk))),
3948 New_List (
3949 Make_Assignment_Statement (Loc,
3950 Name => New_Occurrence_Of (Nnn, Loc),
3951 Expression => Relocate_Node (Lop)),
3952
3953 Make_If_Statement (Loc,
3954 Condition =>
3955 Make_Function_Call (Loc,
3956 Name =>
3957 New_Occurrence_Of
3958 (RTE (RE_Bignum_In_LLI_Range), Loc),
3959 Parameter_Associations => New_List (
3960 New_Occurrence_Of (Nnn, Loc))),
3961
3962 Then_Statements => New_List (
3963 Make_Assignment_Statement (Loc,
3964 Name => New_Occurrence_Of (Bnn, Loc),
3965 Expression =>
3966 New_Occurrence_Of (Standard_False, Loc))),
3967
3968 Else_Statements => New_List (
3969 Make_Assignment_Statement (Loc,
3970 Name => New_Occurrence_Of (Lnn, Loc),
3971 Expression =>
3972 Make_Function_Call (Loc,
3973 Name =>
3974 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
3975 Parameter_Associations => New_List (
3976 New_Occurrence_Of (Nnn, Loc)))),
3977
3978 Make_Assignment_Statement (Loc,
3979 Name => New_Occurrence_Of (Bnn, Loc),
3980 Expression =>
3981 Make_And_Then (Loc,
3982 Left_Opnd =>
3983 Make_In (Loc,
3984 Left_Opnd =>
3985 New_Occurrence_Of (Lnn, Loc),
3986 Right_Opnd =>
3987 New_Occurrence_Of
3988 (Base_Type (Rtype), Loc)),
3989 Right_Opnd => Nin))))));
3990
3991 Insert_Actions (N, New_List (
3992 Make_Object_Declaration (Loc,
3993 Defining_Identifier => Bnn,
3994 Object_Definition =>
3995 New_Occurrence_Of (Standard_Boolean, Loc)),
3996 Blk));
3997
3998 Rewrite (N, New_Occurrence_Of (Bnn, Loc));
3999 Analyze_And_Resolve (N);
4000 return;
4001 end;
4002
4003 -- Not bignum case, but types don't match (this means we rewrote the
4004 -- left operand to be Long_Long_Integer.
4005
4006 else
4007 pragma Assert (Base_Type (Ltype) = LLIB);
4008
4009 -- We rewrite the membership test as
4010
4011 -- Lop in T'Base and then T'Base (Lop) in T
4012
4013 declare
4014 Nin : Node_Id;
4015
4016 begin
4017 -- The last membership test is marked to prevent recursion
4018
4019 Nin :=
4020 Make_In (Loc,
4021 Left_Opnd =>
4022 Convert_To (Base_Type (Rtype), Duplicate_Subexpr (Lop)),
4023 Right_Opnd => New_Occurrence_Of (Rtype, Loc));
4024 Set_No_Minimize_Eliminate (Nin);
4025
4026 -- Now do the rewrite
4027
4028 Rewrite (N,
4029 Make_And_Then (Loc,
4030 Left_Opnd =>
4031 Make_In (Loc,
4032 Left_Opnd => Lop,
4033 Right_Opnd =>
4034 New_Occurrence_Of (Base_Type (Ltype), Loc)),
4035 Right_Opnd => Nin));
4036
4037 Analyze_And_Resolve (N, Restype, Suppress => All_Checks);
4038 end;
4039 end if;
4040 end if;
4041 end Expand_Membership_Minimize_Eliminate_Overflow;
4042
4043 ------------------------
4044 -- Expand_N_Allocator --
4045 ------------------------
4046
4047 procedure Expand_N_Allocator (N : Node_Id) is
4048 PtrT : constant Entity_Id := Etype (N);
4049 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4050 Etyp : constant Entity_Id := Etype (Expression (N));
4051 Loc : constant Source_Ptr := Sloc (N);
4052 Desig : Entity_Id;
4053 Nod : Node_Id;
4054 Pool : Entity_Id;
4055 Temp : Entity_Id;
4056
4057 procedure Rewrite_Coextension (N : Node_Id);
4058 -- Static coextensions have the same lifetime as the entity they
4059 -- constrain. Such occurrences can be rewritten as aliased objects
4060 -- and their unrestricted access used instead of the coextension.
4061
4062 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4063 -- Given a constrained array type E, returns a node representing the
4064 -- code to compute the size in storage elements for the given type.
4065 -- This is done without using the attribute (which malfunctions for
4066 -- large sizes ???)
4067
4068 -------------------------
4069 -- Rewrite_Coextension --
4070 -------------------------
4071
4072 procedure Rewrite_Coextension (N : Node_Id) is
4073 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4074 Temp_Decl : Node_Id;
4075
4076 begin
4077 -- Generate:
4078 -- Cnn : aliased Etyp;
4079
4080 Temp_Decl :=
4081 Make_Object_Declaration (Loc,
4082 Defining_Identifier => Temp_Id,
4083 Aliased_Present => True,
4084 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4085
4086 if Nkind (Expression (N)) = N_Qualified_Expression then
4087 Set_Expression (Temp_Decl, Expression (Expression (N)));
4088 end if;
4089
4090 Insert_Action (N, Temp_Decl);
4091 Rewrite (N,
4092 Make_Attribute_Reference (Loc,
4093 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4094 Attribute_Name => Name_Unrestricted_Access));
4095
4096 Analyze_And_Resolve (N, PtrT);
4097 end Rewrite_Coextension;
4098
4099 ------------------------------
4100 -- Size_In_Storage_Elements --
4101 ------------------------------
4102
4103 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4104 begin
4105 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4106 -- However, the reason for the existence of this function is
4107 -- to construct a test for sizes too large, which means near the
4108 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4109 -- is that we get overflows when sizes are greater than 2**31.
4110
4111 -- So what we end up doing for array types is to use the expression:
4112
4113 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4114
4115 -- which avoids this problem. All this is a bit bogus, but it does
4116 -- mean we catch common cases of trying to allocate arrays that
4117 -- are too large, and which in the absence of a check results in
4118 -- undetected chaos ???
4119
4120 declare
4121 Len : Node_Id;
4122 Res : Node_Id;
4123
4124 begin
4125 for J in 1 .. Number_Dimensions (E) loop
4126 Len :=
4127 Make_Attribute_Reference (Loc,
4128 Prefix => New_Occurrence_Of (E, Loc),
4129 Attribute_Name => Name_Length,
4130 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4131
4132 if J = 1 then
4133 Res := Len;
4134
4135 else
4136 Res :=
4137 Make_Op_Multiply (Loc,
4138 Left_Opnd => Res,
4139 Right_Opnd => Len);
4140 end if;
4141 end loop;
4142
4143 return
4144 Make_Op_Multiply (Loc,
4145 Left_Opnd => Len,
4146 Right_Opnd =>
4147 Make_Attribute_Reference (Loc,
4148 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4149 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4150 end;
4151 end Size_In_Storage_Elements;
4152
4153 -- Start of processing for Expand_N_Allocator
4154
4155 begin
4156 -- RM E.2.3(22). We enforce that the expected type of an allocator
4157 -- shall not be a remote access-to-class-wide-limited-private type
4158
4159 -- Why is this being done at expansion time, seems clearly wrong ???
4160
4161 Validate_Remote_Access_To_Class_Wide_Type (N);
4162
4163 -- Processing for anonymous access-to-controlled types. These access
4164 -- types receive a special finalization master which appears in the
4165 -- declarations of the enclosing semantic unit. This expansion is done
4166 -- now to ensure that any additional types generated by this routine or
4167 -- Expand_Allocator_Expression inherit the proper type attributes.
4168
4169 if (Ekind (PtrT) = E_Anonymous_Access_Type
4170 or else
4171 (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4172 and then Needs_Finalization (Dtyp)
4173 then
4174 -- Anonymous access-to-controlled types allocate on the global pool.
4175 -- Do not set this attribute on .NET/JVM since those targets do not
4176 -- support pools.
4177
4178 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4179 Set_Associated_Storage_Pool
4180 (PtrT, Get_Global_Pool_For_Access_Type (PtrT));
4181 end if;
4182
4183 -- The finalization master must be inserted and analyzed as part of
4184 -- the current semantic unit. This form of expansion is not carried
4185 -- out in Alfa mode because it is useless. Note that the master is
4186 -- updated when analysis changes current units.
4187
4188 if not Alfa_Mode then
4189 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4190 end if;
4191 end if;
4192
4193 -- Set the storage pool and find the appropriate version of Allocate to
4194 -- call. Do not overwrite the storage pool if it is already set, which
4195 -- can happen for build-in-place function returns (see
4196 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4197
4198 if No (Storage_Pool (N)) then
4199 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4200
4201 if Present (Pool) then
4202 Set_Storage_Pool (N, Pool);
4203
4204 if Is_RTE (Pool, RE_SS_Pool) then
4205 if VM_Target = No_VM then
4206 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4207 end if;
4208
4209 -- In the case of an allocator for a simple storage pool, locate
4210 -- and save a reference to the pool type's Allocate routine.
4211
4212 elsif Present (Get_Rep_Pragma
4213 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4214 then
4215 declare
4216 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4217 Alloc_Op : Entity_Id;
4218 begin
4219 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4220 while Present (Alloc_Op) loop
4221 if Scope (Alloc_Op) = Scope (Pool_Type)
4222 and then Present (First_Formal (Alloc_Op))
4223 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4224 then
4225 Set_Procedure_To_Call (N, Alloc_Op);
4226 exit;
4227 else
4228 Alloc_Op := Homonym (Alloc_Op);
4229 end if;
4230 end loop;
4231 end;
4232
4233 elsif Is_Class_Wide_Type (Etype (Pool)) then
4234 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4235
4236 else
4237 Set_Procedure_To_Call (N,
4238 Find_Prim_Op (Etype (Pool), Name_Allocate));
4239 end if;
4240 end if;
4241 end if;
4242
4243 -- Under certain circumstances we can replace an allocator by an access
4244 -- to statically allocated storage. The conditions, as noted in AARM
4245 -- 3.10 (10c) are as follows:
4246
4247 -- Size and initial value is known at compile time
4248 -- Access type is access-to-constant
4249
4250 -- The allocator is not part of a constraint on a record component,
4251 -- because in that case the inserted actions are delayed until the
4252 -- record declaration is fully analyzed, which is too late for the
4253 -- analysis of the rewritten allocator.
4254
4255 if Is_Access_Constant (PtrT)
4256 and then Nkind (Expression (N)) = N_Qualified_Expression
4257 and then Compile_Time_Known_Value (Expression (Expression (N)))
4258 and then Size_Known_At_Compile_Time
4259 (Etype (Expression (Expression (N))))
4260 and then not Is_Record_Type (Current_Scope)
4261 then
4262 -- Here we can do the optimization. For the allocator
4263
4264 -- new x'(y)
4265
4266 -- We insert an object declaration
4267
4268 -- Tnn : aliased x := y;
4269
4270 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4271 -- marked as requiring static allocation.
4272
4273 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4274 Desig := Subtype_Mark (Expression (N));
4275
4276 -- If context is constrained, use constrained subtype directly,
4277 -- so that the constant is not labelled as having a nominally
4278 -- unconstrained subtype.
4279
4280 if Entity (Desig) = Base_Type (Dtyp) then
4281 Desig := New_Occurrence_Of (Dtyp, Loc);
4282 end if;
4283
4284 Insert_Action (N,
4285 Make_Object_Declaration (Loc,
4286 Defining_Identifier => Temp,
4287 Aliased_Present => True,
4288 Constant_Present => Is_Access_Constant (PtrT),
4289 Object_Definition => Desig,
4290 Expression => Expression (Expression (N))));
4291
4292 Rewrite (N,
4293 Make_Attribute_Reference (Loc,
4294 Prefix => New_Occurrence_Of (Temp, Loc),
4295 Attribute_Name => Name_Unrestricted_Access));
4296
4297 Analyze_And_Resolve (N, PtrT);
4298
4299 -- We set the variable as statically allocated, since we don't want
4300 -- it going on the stack of the current procedure!
4301
4302 Set_Is_Statically_Allocated (Temp);
4303 return;
4304 end if;
4305
4306 -- Same if the allocator is an access discriminant for a local object:
4307 -- instead of an allocator we create a local value and constrain the
4308 -- enclosing object with the corresponding access attribute.
4309
4310 if Is_Static_Coextension (N) then
4311 Rewrite_Coextension (N);
4312 return;
4313 end if;
4314
4315 -- Check for size too large, we do this because the back end misses
4316 -- proper checks here and can generate rubbish allocation calls when
4317 -- we are near the limit. We only do this for the 32-bit address case
4318 -- since that is from a practical point of view where we see a problem.
4319
4320 if System_Address_Size = 32
4321 and then not Storage_Checks_Suppressed (PtrT)
4322 and then not Storage_Checks_Suppressed (Dtyp)
4323 and then not Storage_Checks_Suppressed (Etyp)
4324 then
4325 -- The check we want to generate should look like
4326
4327 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4328 -- raise Storage_Error;
4329 -- end if;
4330
4331 -- where 3.5 gigabytes is a constant large enough to accommodate any
4332 -- reasonable request for. But we can't do it this way because at
4333 -- least at the moment we don't compute this attribute right, and
4334 -- can silently give wrong results when the result gets large. Since
4335 -- this is all about large results, that's bad, so instead we only
4336 -- apply the check for constrained arrays, and manually compute the
4337 -- value of the attribute ???
4338
4339 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4340 Insert_Action (N,
4341 Make_Raise_Storage_Error (Loc,
4342 Condition =>
4343 Make_Op_Gt (Loc,
4344 Left_Opnd => Size_In_Storage_Elements (Etyp),
4345 Right_Opnd =>
4346 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4347 Reason => SE_Object_Too_Large));
4348 end if;
4349 end if;
4350
4351 -- Handle case of qualified expression (other than optimization above)
4352 -- First apply constraint checks, because the bounds or discriminants
4353 -- in the aggregate might not match the subtype mark in the allocator.
4354
4355 if Nkind (Expression (N)) = N_Qualified_Expression then
4356 Apply_Constraint_Check
4357 (Expression (Expression (N)), Etype (Expression (N)));
4358
4359 Expand_Allocator_Expression (N);
4360 return;
4361 end if;
4362
4363 -- If the allocator is for a type which requires initialization, and
4364 -- there is no initial value (i.e. operand is a subtype indication
4365 -- rather than a qualified expression), then we must generate a call to
4366 -- the initialization routine using an expressions action node:
4367
4368 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4369
4370 -- Here ptr_T is the pointer type for the allocator, and T is the
4371 -- subtype of the allocator. A special case arises if the designated
4372 -- type of the access type is a task or contains tasks. In this case
4373 -- the call to Init (Temp.all ...) is replaced by code that ensures
4374 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4375 -- for details). In addition, if the type T is a task T, then the
4376 -- first argument to Init must be converted to the task record type.
4377
4378 declare
4379 T : constant Entity_Id := Entity (Expression (N));
4380 Args : List_Id;
4381 Decls : List_Id;
4382 Decl : Node_Id;
4383 Discr : Elmt_Id;
4384 Init : Entity_Id;
4385 Init_Arg1 : Node_Id;
4386 Temp_Decl : Node_Id;
4387 Temp_Type : Entity_Id;
4388
4389 begin
4390 if No_Initialization (N) then
4391
4392 -- Even though this might be a simple allocation, create a custom
4393 -- Allocate if the context requires it. Since .NET/JVM compilers
4394 -- do not support pools, this step is skipped.
4395
4396 if VM_Target = No_VM
4397 and then Present (Finalization_Master (PtrT))
4398 then
4399 Build_Allocate_Deallocate_Proc
4400 (N => N,
4401 Is_Allocate => True);
4402 end if;
4403
4404 -- Case of no initialization procedure present
4405
4406 elsif not Has_Non_Null_Base_Init_Proc (T) then
4407
4408 -- Case of simple initialization required
4409
4410 if Needs_Simple_Initialization (T) then
4411 Check_Restriction (No_Default_Initialization, N);
4412 Rewrite (Expression (N),
4413 Make_Qualified_Expression (Loc,
4414 Subtype_Mark => New_Occurrence_Of (T, Loc),
4415 Expression => Get_Simple_Init_Val (T, N)));
4416
4417 Analyze_And_Resolve (Expression (Expression (N)), T);
4418 Analyze_And_Resolve (Expression (N), T);
4419 Set_Paren_Count (Expression (Expression (N)), 1);
4420 Expand_N_Allocator (N);
4421
4422 -- No initialization required
4423
4424 else
4425 null;
4426 end if;
4427
4428 -- Case of initialization procedure present, must be called
4429
4430 else
4431 Check_Restriction (No_Default_Initialization, N);
4432
4433 if not Restriction_Active (No_Default_Initialization) then
4434 Init := Base_Init_Proc (T);
4435 Nod := N;
4436 Temp := Make_Temporary (Loc, 'P');
4437
4438 -- Construct argument list for the initialization routine call
4439
4440 Init_Arg1 :=
4441 Make_Explicit_Dereference (Loc,
4442 Prefix =>
4443 New_Reference_To (Temp, Loc));
4444
4445 Set_Assignment_OK (Init_Arg1);
4446 Temp_Type := PtrT;
4447
4448 -- The initialization procedure expects a specific type. if the
4449 -- context is access to class wide, indicate that the object
4450 -- being allocated has the right specific type.
4451
4452 if Is_Class_Wide_Type (Dtyp) then
4453 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4454 end if;
4455
4456 -- If designated type is a concurrent type or if it is private
4457 -- type whose definition is a concurrent type, the first
4458 -- argument in the Init routine has to be unchecked conversion
4459 -- to the corresponding record type. If the designated type is
4460 -- a derived type, also convert the argument to its root type.
4461
4462 if Is_Concurrent_Type (T) then
4463 Init_Arg1 :=
4464 Unchecked_Convert_To (
4465 Corresponding_Record_Type (T), Init_Arg1);
4466
4467 elsif Is_Private_Type (T)
4468 and then Present (Full_View (T))
4469 and then Is_Concurrent_Type (Full_View (T))
4470 then
4471 Init_Arg1 :=
4472 Unchecked_Convert_To
4473 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4474
4475 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4476 declare
4477 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4478
4479 begin
4480 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4481 Set_Etype (Init_Arg1, Ftyp);
4482 end;
4483 end if;
4484
4485 Args := New_List (Init_Arg1);
4486
4487 -- For the task case, pass the Master_Id of the access type as
4488 -- the value of the _Master parameter, and _Chain as the value
4489 -- of the _Chain parameter (_Chain will be defined as part of
4490 -- the generated code for the allocator).
4491
4492 -- In Ada 2005, the context may be a function that returns an
4493 -- anonymous access type. In that case the Master_Id has been
4494 -- created when expanding the function declaration.
4495
4496 if Has_Task (T) then
4497 if No (Master_Id (Base_Type (PtrT))) then
4498
4499 -- The designated type was an incomplete type, and the
4500 -- access type did not get expanded. Salvage it now.
4501
4502 if not Restriction_Active (No_Task_Hierarchy) then
4503 pragma Assert (Present (Parent (Base_Type (PtrT))));
4504 Expand_N_Full_Type_Declaration
4505 (Parent (Base_Type (PtrT)));
4506 end if;
4507 end if;
4508
4509 -- If the context of the allocator is a declaration or an
4510 -- assignment, we can generate a meaningful image for it,
4511 -- even though subsequent assignments might remove the
4512 -- connection between task and entity. We build this image
4513 -- when the left-hand side is a simple variable, a simple
4514 -- indexed assignment or a simple selected component.
4515
4516 if Nkind (Parent (N)) = N_Assignment_Statement then
4517 declare
4518 Nam : constant Node_Id := Name (Parent (N));
4519
4520 begin
4521 if Is_Entity_Name (Nam) then
4522 Decls :=
4523 Build_Task_Image_Decls
4524 (Loc,
4525 New_Occurrence_Of
4526 (Entity (Nam), Sloc (Nam)), T);
4527
4528 elsif Nkind_In (Nam, N_Indexed_Component,
4529 N_Selected_Component)
4530 and then Is_Entity_Name (Prefix (Nam))
4531 then
4532 Decls :=
4533 Build_Task_Image_Decls
4534 (Loc, Nam, Etype (Prefix (Nam)));
4535 else
4536 Decls := Build_Task_Image_Decls (Loc, T, T);
4537 end if;
4538 end;
4539
4540 elsif Nkind (Parent (N)) = N_Object_Declaration then
4541 Decls :=
4542 Build_Task_Image_Decls
4543 (Loc, Defining_Identifier (Parent (N)), T);
4544
4545 else
4546 Decls := Build_Task_Image_Decls (Loc, T, T);
4547 end if;
4548
4549 if Restriction_Active (No_Task_Hierarchy) then
4550 Append_To (Args,
4551 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4552 else
4553 Append_To (Args,
4554 New_Reference_To
4555 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4556 end if;
4557
4558 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4559
4560 Decl := Last (Decls);
4561 Append_To (Args,
4562 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4563
4564 -- Has_Task is false, Decls not used
4565
4566 else
4567 Decls := No_List;
4568 end if;
4569
4570 -- Add discriminants if discriminated type
4571
4572 declare
4573 Dis : Boolean := False;
4574 Typ : Entity_Id;
4575
4576 begin
4577 if Has_Discriminants (T) then
4578 Dis := True;
4579 Typ := T;
4580
4581 elsif Is_Private_Type (T)
4582 and then Present (Full_View (T))
4583 and then Has_Discriminants (Full_View (T))
4584 then
4585 Dis := True;
4586 Typ := Full_View (T);
4587 end if;
4588
4589 if Dis then
4590
4591 -- If the allocated object will be constrained by the
4592 -- default values for discriminants, then build a subtype
4593 -- with those defaults, and change the allocated subtype
4594 -- to that. Note that this happens in fewer cases in Ada
4595 -- 2005 (AI-363).
4596
4597 if not Is_Constrained (Typ)
4598 and then Present (Discriminant_Default_Value
4599 (First_Discriminant (Typ)))
4600 and then (Ada_Version < Ada_2005
4601 or else not
4602 Effectively_Has_Constrained_Partial_View
4603 (Typ => Typ,
4604 Scop => Current_Scope))
4605 then
4606 Typ := Build_Default_Subtype (Typ, N);
4607 Set_Expression (N, New_Reference_To (Typ, Loc));
4608 end if;
4609
4610 Discr := First_Elmt (Discriminant_Constraint (Typ));
4611 while Present (Discr) loop
4612 Nod := Node (Discr);
4613 Append (New_Copy_Tree (Node (Discr)), Args);
4614
4615 -- AI-416: when the discriminant constraint is an
4616 -- anonymous access type make sure an accessibility
4617 -- check is inserted if necessary (3.10.2(22.q/2))
4618
4619 if Ada_Version >= Ada_2005
4620 and then
4621 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4622 then
4623 Apply_Accessibility_Check
4624 (Nod, Typ, Insert_Node => Nod);
4625 end if;
4626
4627 Next_Elmt (Discr);
4628 end loop;
4629 end if;
4630 end;
4631
4632 -- We set the allocator as analyzed so that when we analyze
4633 -- the conditional expression node, we do not get an unwanted
4634 -- recursive expansion of the allocator expression.
4635
4636 Set_Analyzed (N, True);
4637 Nod := Relocate_Node (N);
4638
4639 -- Here is the transformation:
4640 -- input: new Ctrl_Typ
4641 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4642 -- Ctrl_TypIP (Temp.all, ...);
4643 -- [Deep_]Initialize (Temp.all);
4644
4645 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4646 -- is the subtype of the allocator.
4647
4648 Temp_Decl :=
4649 Make_Object_Declaration (Loc,
4650 Defining_Identifier => Temp,
4651 Constant_Present => True,
4652 Object_Definition => New_Reference_To (Temp_Type, Loc),
4653 Expression => Nod);
4654
4655 Set_Assignment_OK (Temp_Decl);
4656 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4657
4658 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4659
4660 -- If the designated type is a task type or contains tasks,
4661 -- create block to activate created tasks, and insert
4662 -- declaration for Task_Image variable ahead of call.
4663
4664 if Has_Task (T) then
4665 declare
4666 L : constant List_Id := New_List;
4667 Blk : Node_Id;
4668 begin
4669 Build_Task_Allocate_Block (L, Nod, Args);
4670 Blk := Last (L);
4671 Insert_List_Before (First (Declarations (Blk)), Decls);
4672 Insert_Actions (N, L);
4673 end;
4674
4675 else
4676 Insert_Action (N,
4677 Make_Procedure_Call_Statement (Loc,
4678 Name => New_Reference_To (Init, Loc),
4679 Parameter_Associations => Args));
4680 end if;
4681
4682 if Needs_Finalization (T) then
4683
4684 -- Generate:
4685 -- [Deep_]Initialize (Init_Arg1);
4686
4687 Insert_Action (N,
4688 Make_Init_Call
4689 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4690 Typ => T));
4691
4692 if Present (Finalization_Master (PtrT)) then
4693
4694 -- Special processing for .NET/JVM, the allocated object
4695 -- is attached to the finalization master. Generate:
4696
4697 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4698
4699 -- Types derived from [Limited_]Controlled are the only
4700 -- ones considered since they have fields Prev and Next.
4701
4702 if VM_Target /= No_VM then
4703 if Is_Controlled (T) then
4704 Insert_Action (N,
4705 Make_Attach_Call
4706 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4707 Ptr_Typ => PtrT));
4708 end if;
4709
4710 -- Default case, generate:
4711
4712 -- Set_Finalize_Address
4713 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4714
4715 -- Do not generate this call in the following cases:
4716 --
4717 -- * Alfa mode - the call is useless and results in
4718 -- unwanted expansion.
4719 --
4720 -- * CodePeer mode - TSS primitive Finalize_Address is
4721 -- not created in this mode.
4722
4723 elsif not Alfa_Mode
4724 and then not CodePeer_Mode
4725 then
4726 Insert_Action (N,
4727 Make_Set_Finalize_Address_Call
4728 (Loc => Loc,
4729 Typ => T,
4730 Ptr_Typ => PtrT));
4731 end if;
4732 end if;
4733 end if;
4734
4735 Rewrite (N, New_Reference_To (Temp, Loc));
4736 Analyze_And_Resolve (N, PtrT);
4737 end if;
4738 end if;
4739 end;
4740
4741 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4742 -- object that has been rewritten as a reference, we displace "this"
4743 -- to reference properly its secondary dispatch table.
4744
4745 if Nkind (N) = N_Identifier
4746 and then Is_Interface (Dtyp)
4747 then
4748 Displace_Allocator_Pointer (N);
4749 end if;
4750
4751 exception
4752 when RE_Not_Available =>
4753 return;
4754 end Expand_N_Allocator;
4755
4756 -----------------------
4757 -- Expand_N_And_Then --
4758 -----------------------
4759
4760 procedure Expand_N_And_Then (N : Node_Id)
4761 renames Expand_Short_Circuit_Operator;
4762
4763 ------------------------------
4764 -- Expand_N_Case_Expression --
4765 ------------------------------
4766
4767 procedure Expand_N_Case_Expression (N : Node_Id) is
4768 Loc : constant Source_Ptr := Sloc (N);
4769 Typ : constant Entity_Id := Etype (N);
4770 Cstmt : Node_Id;
4771 Tnn : Entity_Id;
4772 Pnn : Entity_Id;
4773 Actions : List_Id;
4774 Ttyp : Entity_Id;
4775 Alt : Node_Id;
4776 Fexp : Node_Id;
4777
4778 begin
4779 -- If Do_Overflow_Check is set, it means we are in MINIMIZED/ELIMINATED
4780 -- mode, and all we do is to call Apply_Arithmetic_Overflow_Check to
4781 -- ensure proper overflow handling for the dependent expressions. The
4782 -- checks circuitry will rewrite the case expression in this case with
4783 -- Do_Overflow_Checks off. so that when that rewritten node arrives back
4784 -- here, then we will do the full expansion.
4785
4786 if Do_Overflow_Check (N) then
4787 Apply_Arithmetic_Overflow_Check (N);
4788 return;
4789 end if;
4790
4791 -- We expand
4792
4793 -- case X is when A => AX, when B => BX ...
4794
4795 -- to
4796
4797 -- do
4798 -- Tnn : typ;
4799 -- case X is
4800 -- when A =>
4801 -- Tnn := AX;
4802 -- when B =>
4803 -- Tnn := BX;
4804 -- ...
4805 -- end case;
4806 -- in Tnn end;
4807
4808 -- However, this expansion is wrong for limited types, and also
4809 -- wrong for unconstrained types (since the bounds may not be the
4810 -- same in all branches). Furthermore it involves an extra copy
4811 -- for large objects. So we take care of this by using the following
4812 -- modified expansion for non-elementary types:
4813
4814 -- do
4815 -- type Pnn is access all typ;
4816 -- Tnn : Pnn;
4817 -- case X is
4818 -- when A =>
4819 -- T := AX'Unrestricted_Access;
4820 -- when B =>
4821 -- T := BX'Unrestricted_Access;
4822 -- ...
4823 -- end case;
4824 -- in Tnn.all end;
4825
4826 Cstmt :=
4827 Make_Case_Statement (Loc,
4828 Expression => Expression (N),
4829 Alternatives => New_List);
4830
4831 Actions := New_List;
4832
4833 -- Scalar case
4834
4835 if Is_Elementary_Type (Typ) then
4836 Ttyp := Typ;
4837
4838 else
4839 Pnn := Make_Temporary (Loc, 'P');
4840 Append_To (Actions,
4841 Make_Full_Type_Declaration (Loc,
4842 Defining_Identifier => Pnn,
4843 Type_Definition =>
4844 Make_Access_To_Object_Definition (Loc,
4845 All_Present => True,
4846 Subtype_Indication =>
4847 New_Reference_To (Typ, Loc))));
4848 Ttyp := Pnn;
4849 end if;
4850
4851 Tnn := Make_Temporary (Loc, 'T');
4852 Append_To (Actions,
4853 Make_Object_Declaration (Loc,
4854 Defining_Identifier => Tnn,
4855 Object_Definition => New_Occurrence_Of (Ttyp, Loc)));
4856
4857 -- Now process the alternatives
4858
4859 Alt := First (Alternatives (N));
4860 while Present (Alt) loop
4861 declare
4862 Aexp : Node_Id := Expression (Alt);
4863 Aloc : constant Source_Ptr := Sloc (Aexp);
4864 Stats : List_Id;
4865
4866 begin
4867 -- As described above, take Unrestricted_Access for case of non-
4868 -- scalar types, to avoid big copies, and special cases.
4869
4870 if not Is_Elementary_Type (Typ) then
4871 Aexp :=
4872 Make_Attribute_Reference (Aloc,
4873 Prefix => Relocate_Node (Aexp),
4874 Attribute_Name => Name_Unrestricted_Access);
4875 end if;
4876
4877 Stats := New_List (
4878 Make_Assignment_Statement (Aloc,
4879 Name => New_Occurrence_Of (Tnn, Loc),
4880 Expression => Aexp));
4881
4882 -- Propagate declarations inserted in the node by Insert_Actions
4883 -- (for example, temporaries generated to remove side effects).
4884 -- These actions must remain attached to the alternative, given
4885 -- that they are generated by the corresponding expression.
4886
4887 if Present (Sinfo.Actions (Alt)) then
4888 Prepend_List (Sinfo.Actions (Alt), Stats);
4889 end if;
4890
4891 Append_To
4892 (Alternatives (Cstmt),
4893 Make_Case_Statement_Alternative (Sloc (Alt),
4894 Discrete_Choices => Discrete_Choices (Alt),
4895 Statements => Stats));
4896 end;
4897
4898 Next (Alt);
4899 end loop;
4900
4901 Append_To (Actions, Cstmt);
4902
4903 -- Construct and return final expression with actions
4904
4905 if Is_Elementary_Type (Typ) then
4906 Fexp := New_Occurrence_Of (Tnn, Loc);
4907 else
4908 Fexp :=
4909 Make_Explicit_Dereference (Loc,
4910 Prefix => New_Occurrence_Of (Tnn, Loc));
4911 end if;
4912
4913 Rewrite (N,
4914 Make_Expression_With_Actions (Loc,
4915 Expression => Fexp,
4916 Actions => Actions));
4917
4918 Analyze_And_Resolve (N, Typ);
4919 end Expand_N_Case_Expression;
4920
4921 -------------------------------------
4922 -- Expand_N_Conditional_Expression --
4923 -------------------------------------
4924
4925 -- Deal with limited types and condition actions
4926
4927 procedure Expand_N_Conditional_Expression (N : Node_Id) is
4928 function Create_Alternative
4929 (Loc : Source_Ptr;
4930 Temp_Id : Entity_Id;
4931 Flag_Id : Entity_Id;
4932 Expr : Node_Id) return List_Id;
4933 -- Build the statements of a "then" or "else" conditional expression
4934 -- alternative. Temp_Id is the conditional expression result, Flag_Id
4935 -- is a finalization flag created to service expression Expr.
4936
4937 function Is_Controlled_Function_Call (Expr : Node_Id) return Boolean;
4938 -- Determine if expression Expr is a rewritten controlled function call
4939
4940 ------------------------
4941 -- Create_Alternative --
4942 ------------------------
4943
4944 function Create_Alternative
4945 (Loc : Source_Ptr;
4946 Temp_Id : Entity_Id;
4947 Flag_Id : Entity_Id;
4948 Expr : Node_Id) return List_Id
4949 is
4950 Result : constant List_Id := New_List;
4951
4952 begin
4953 -- Generate:
4954 -- Fnn := True;
4955
4956 if Present (Flag_Id)
4957 and then not Is_Controlled_Function_Call (Expr)
4958 then
4959 Append_To (Result,
4960 Make_Assignment_Statement (Loc,
4961 Name => New_Reference_To (Flag_Id, Loc),
4962 Expression => New_Reference_To (Standard_True, Loc)));
4963 end if;
4964
4965 -- Generate:
4966 -- Cnn := <expr>'Unrestricted_Access;
4967
4968 Append_To (Result,
4969 Make_Assignment_Statement (Loc,
4970 Name => New_Reference_To (Temp_Id, Loc),
4971 Expression =>
4972 Make_Attribute_Reference (Loc,
4973 Prefix => Relocate_Node (Expr),
4974 Attribute_Name => Name_Unrestricted_Access)));
4975
4976 return Result;
4977 end Create_Alternative;
4978
4979 ---------------------------------
4980 -- Is_Controlled_Function_Call --
4981 ---------------------------------
4982
4983 function Is_Controlled_Function_Call (Expr : Node_Id) return Boolean is
4984 begin
4985 return
4986 Nkind (Original_Node (Expr)) = N_Function_Call
4987 and then Needs_Finalization (Etype (Expr));
4988 end Is_Controlled_Function_Call;
4989
4990 -- Local variables
4991
4992 Loc : constant Source_Ptr := Sloc (N);
4993 Cond : constant Node_Id := First (Expressions (N));
4994 Thenx : constant Node_Id := Next (Cond);
4995 Elsex : constant Node_Id := Next (Thenx);
4996 Typ : constant Entity_Id := Etype (N);
4997
4998 Actions : List_Id;
4999 Cnn : Entity_Id;
5000 Decl : Node_Id;
5001 Expr : Node_Id;
5002 New_If : Node_Id;
5003 New_N : Node_Id;
5004
5005 begin
5006 -- Fold at compile time if condition known. We have already folded
5007 -- static conditional expressions, but it is possible to fold any
5008 -- case in which the condition is known at compile time, even though
5009 -- the result is non-static.
5010
5011 -- Note that we don't do the fold of such cases in Sem_Elab because
5012 -- it can cause infinite loops with the expander adding a conditional
5013 -- expression, and Sem_Elab circuitry removing it repeatedly.
5014
5015 if Compile_Time_Known_Value (Cond) then
5016 if Is_True (Expr_Value (Cond)) then
5017 Expr := Thenx;
5018 Actions := Then_Actions (N);
5019 else
5020 Expr := Elsex;
5021 Actions := Else_Actions (N);
5022 end if;
5023
5024 Remove (Expr);
5025
5026 if Present (Actions) then
5027
5028 -- If we are not allowed to use Expression_With_Actions, just skip
5029 -- the optimization, it is not critical for correctness.
5030
5031 if not Use_Expression_With_Actions then
5032 goto Skip_Optimization;
5033 end if;
5034
5035 Rewrite (N,
5036 Make_Expression_With_Actions (Loc,
5037 Expression => Relocate_Node (Expr),
5038 Actions => Actions));
5039 Analyze_And_Resolve (N, Typ);
5040
5041 else
5042 Rewrite (N, Relocate_Node (Expr));
5043 end if;
5044
5045 -- Note that the result is never static (legitimate cases of static
5046 -- conditional expressions were folded in Sem_Eval).
5047
5048 Set_Is_Static_Expression (N, False);
5049 return;
5050 end if;
5051
5052 <<Skip_Optimization>>
5053
5054 -- If the type is limited or unconstrained, we expand as follows to
5055 -- avoid any possibility of improper copies.
5056
5057 -- Note: it may be possible to avoid this special processing if the
5058 -- back end uses its own mechanisms for handling by-reference types ???
5059
5060 -- type Ptr is access all Typ;
5061 -- Cnn : Ptr;
5062 -- if cond then
5063 -- <<then actions>>
5064 -- Cnn := then-expr'Unrestricted_Access;
5065 -- else
5066 -- <<else actions>>
5067 -- Cnn := else-expr'Unrestricted_Access;
5068 -- end if;
5069
5070 -- and replace the conditional expression by a reference to Cnn.all.
5071
5072 -- This special case can be skipped if the back end handles limited
5073 -- types properly and ensures that no incorrect copies are made.
5074
5075 if Is_By_Reference_Type (Typ)
5076 and then not Back_End_Handles_Limited_Types
5077 then
5078 declare
5079 Flag_Id : Entity_Id;
5080 Ptr_Typ : Entity_Id;
5081
5082 begin
5083 Flag_Id := Empty;
5084
5085 -- At least one of the conditional expression alternatives uses a
5086 -- controlled function to provide the result. Create a status flag
5087 -- to signal the finalization machinery that Cnn needs special
5088 -- handling.
5089
5090 if Is_Controlled_Function_Call (Thenx)
5091 or else
5092 Is_Controlled_Function_Call (Elsex)
5093 then
5094 Flag_Id := Make_Temporary (Loc, 'F');
5095
5096 Insert_Action (N,
5097 Make_Object_Declaration (Loc,
5098 Defining_Identifier => Flag_Id,
5099 Object_Definition =>
5100 New_Reference_To (Standard_Boolean, Loc),
5101 Expression =>
5102 New_Reference_To (Standard_False, Loc)));
5103 end if;
5104
5105 -- Generate:
5106 -- type Ann is access all Typ;
5107
5108 Ptr_Typ := Make_Temporary (Loc, 'A');
5109
5110 Insert_Action (N,
5111 Make_Full_Type_Declaration (Loc,
5112 Defining_Identifier => Ptr_Typ,
5113 Type_Definition =>
5114 Make_Access_To_Object_Definition (Loc,
5115 All_Present => True,
5116 Subtype_Indication => New_Reference_To (Typ, Loc))));
5117
5118 -- Generate:
5119 -- Cnn : Ann;
5120
5121 Cnn := Make_Temporary (Loc, 'C', N);
5122 Set_Ekind (Cnn, E_Variable);
5123 Set_Status_Flag_Or_Transient_Decl (Cnn, Flag_Id);
5124
5125 Decl :=
5126 Make_Object_Declaration (Loc,
5127 Defining_Identifier => Cnn,
5128 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5129
5130 New_If :=
5131 Make_Implicit_If_Statement (N,
5132 Condition => Relocate_Node (Cond),
5133 Then_Statements =>
5134 Create_Alternative (Sloc (Thenx), Cnn, Flag_Id, Thenx),
5135 Else_Statements =>
5136 Create_Alternative (Sloc (Elsex), Cnn, Flag_Id, Elsex));
5137
5138 New_N :=
5139 Make_Explicit_Dereference (Loc,
5140 Prefix => New_Occurrence_Of (Cnn, Loc));
5141 end;
5142
5143 -- For other types, we only need to expand if there are other actions
5144 -- associated with either branch.
5145
5146 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5147
5148 -- We have two approaches to handling this. If we are allowed to use
5149 -- N_Expression_With_Actions, then we can just wrap the actions into
5150 -- the appropriate expression.
5151
5152 if Use_Expression_With_Actions then
5153 if Present (Then_Actions (N)) then
5154 Rewrite (Thenx,
5155 Make_Expression_With_Actions (Sloc (Thenx),
5156 Actions => Then_Actions (N),
5157 Expression => Relocate_Node (Thenx)));
5158 Set_Then_Actions (N, No_List);
5159 Analyze_And_Resolve (Thenx, Typ);
5160 end if;
5161
5162 if Present (Else_Actions (N)) then
5163 Rewrite (Elsex,
5164 Make_Expression_With_Actions (Sloc (Elsex),
5165 Actions => Else_Actions (N),
5166 Expression => Relocate_Node (Elsex)));
5167 Set_Else_Actions (N, No_List);
5168 Analyze_And_Resolve (Elsex, Typ);
5169 end if;
5170
5171 return;
5172
5173 -- if we can't use N_Expression_With_Actions nodes, then we insert
5174 -- the following sequence of actions (using Insert_Actions):
5175
5176 -- Cnn : typ;
5177 -- if cond then
5178 -- <<then actions>>
5179 -- Cnn := then-expr;
5180 -- else
5181 -- <<else actions>>
5182 -- Cnn := else-expr
5183 -- end if;
5184
5185 -- and replace the conditional expression by a reference to Cnn
5186
5187 else
5188 Cnn := Make_Temporary (Loc, 'C', N);
5189
5190 Decl :=
5191 Make_Object_Declaration (Loc,
5192 Defining_Identifier => Cnn,
5193 Object_Definition => New_Occurrence_Of (Typ, Loc));
5194
5195 New_If :=
5196 Make_Implicit_If_Statement (N,
5197 Condition => Relocate_Node (Cond),
5198
5199 Then_Statements => New_List (
5200 Make_Assignment_Statement (Sloc (Thenx),
5201 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5202 Expression => Relocate_Node (Thenx))),
5203
5204 Else_Statements => New_List (
5205 Make_Assignment_Statement (Sloc (Elsex),
5206 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5207 Expression => Relocate_Node (Elsex))));
5208
5209 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
5210 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
5211
5212 New_N := New_Occurrence_Of (Cnn, Loc);
5213 end if;
5214
5215 -- If no actions then no expansion needed, gigi will handle it using
5216 -- the same approach as a C conditional expression.
5217
5218 else
5219 -- If Do_Overflow_Check is set it means we have a signed intger type
5220 -- in MINIMIZED or ELIMINATED mode, so we apply an overflow check to
5221 -- the if expression (to make sure that overflow checking is properly
5222 -- handled for dependent expressions).
5223
5224 if Do_Overflow_Check (N) then
5225 Apply_Arithmetic_Overflow_Check (N);
5226 end if;
5227
5228 return;
5229 end if;
5230
5231 -- Fall through here for either the limited expansion, or the case of
5232 -- inserting actions for non-limited types. In both these cases, we must
5233 -- move the SLOC of the parent If statement to the newly created one and
5234 -- change it to the SLOC of the expression which, after expansion, will
5235 -- correspond to what is being evaluated.
5236
5237 if Present (Parent (N))
5238 and then Nkind (Parent (N)) = N_If_Statement
5239 then
5240 Set_Sloc (New_If, Sloc (Parent (N)));
5241 Set_Sloc (Parent (N), Loc);
5242 end if;
5243
5244 -- Make sure Then_Actions and Else_Actions are appropriately moved
5245 -- to the new if statement.
5246
5247 if Present (Then_Actions (N)) then
5248 Insert_List_Before
5249 (First (Then_Statements (New_If)), Then_Actions (N));
5250 end if;
5251
5252 if Present (Else_Actions (N)) then
5253 Insert_List_Before
5254 (First (Else_Statements (New_If)), Else_Actions (N));
5255 end if;
5256
5257 Insert_Action (N, Decl);
5258 Insert_Action (N, New_If);
5259 Rewrite (N, New_N);
5260 Analyze_And_Resolve (N, Typ);
5261 end Expand_N_Conditional_Expression;
5262
5263 -----------------------------------
5264 -- Expand_N_Explicit_Dereference --
5265 -----------------------------------
5266
5267 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5268 begin
5269 -- Insert explicit dereference call for the checked storage pool case
5270
5271 Insert_Dereference_Action (Prefix (N));
5272
5273 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5274 -- we set the atomic sync flag.
5275
5276 if Is_Atomic (Etype (N))
5277 and then not Atomic_Synchronization_Disabled (Etype (N))
5278 then
5279 Activate_Atomic_Synchronization (N);
5280 end if;
5281 end Expand_N_Explicit_Dereference;
5282
5283 --------------------------------------
5284 -- Expand_N_Expression_With_Actions --
5285 --------------------------------------
5286
5287 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5288
5289 procedure Process_Transient_Object (Decl : Node_Id);
5290 -- Given the declaration of a controlled transient declared inside the
5291 -- Actions list of an Expression_With_Actions, generate all necessary
5292 -- types and hooks in order to properly finalize the transient. This
5293 -- mechanism works in conjunction with Build_Finalizer.
5294
5295 ------------------------------
5296 -- Process_Transient_Object --
5297 ------------------------------
5298
5299 procedure Process_Transient_Object (Decl : Node_Id) is
5300
5301 function Find_Insertion_Node return Node_Id;
5302 -- Complex conditions in if statements may be converted into nested
5303 -- EWAs. In this case, any generated code must be inserted before the
5304 -- if statement to ensure proper visibility of the hook objects. This
5305 -- routine returns the top most short circuit operator or the parent
5306 -- of the EWA if no nesting was detected.
5307
5308 -------------------------
5309 -- Find_Insertion_Node --
5310 -------------------------
5311
5312 function Find_Insertion_Node return Node_Id is
5313 Par : Node_Id;
5314
5315 begin
5316 -- Climb up the branches of a complex condition
5317
5318 Par := N;
5319 while Nkind_In (Parent (Par), N_And_Then, N_Op_Not, N_Or_Else) loop
5320 Par := Parent (Par);
5321 end loop;
5322
5323 return Par;
5324 end Find_Insertion_Node;
5325
5326 -- Local variables
5327
5328 Ins_Node : constant Node_Id := Find_Insertion_Node;
5329 Loc : constant Source_Ptr := Sloc (Decl);
5330 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
5331 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
5332 Desig_Typ : Entity_Id;
5333 Expr : Node_Id;
5334 Ptr_Decl : Node_Id;
5335 Ptr_Id : Entity_Id;
5336 Temp_Decl : Node_Id;
5337 Temp_Id : Node_Id;
5338
5339 -- Start of processing for Process_Transient_Object
5340
5341 begin
5342 -- Step 1: Create the access type which provides a reference to the
5343 -- transient object.
5344
5345 if Is_Access_Type (Obj_Typ) then
5346 Desig_Typ := Directly_Designated_Type (Obj_Typ);
5347 else
5348 Desig_Typ := Obj_Typ;
5349 end if;
5350
5351 -- Generate:
5352 -- Ann : access [all] <Desig_Typ>;
5353
5354 Ptr_Id := Make_Temporary (Loc, 'A');
5355
5356 Ptr_Decl :=
5357 Make_Full_Type_Declaration (Loc,
5358 Defining_Identifier => Ptr_Id,
5359 Type_Definition =>
5360 Make_Access_To_Object_Definition (Loc,
5361 All_Present =>
5362 Ekind (Obj_Typ) = E_General_Access_Type,
5363 Subtype_Indication => New_Reference_To (Desig_Typ, Loc)));
5364
5365 Insert_Action (Ins_Node, Ptr_Decl);
5366 Analyze (Ptr_Decl);
5367
5368 -- Step 2: Create a temporary which acts as a hook to the transient
5369 -- object. Generate:
5370
5371 -- Temp : Ptr_Id := null;
5372
5373 Temp_Id := Make_Temporary (Loc, 'T');
5374
5375 Temp_Decl :=
5376 Make_Object_Declaration (Loc,
5377 Defining_Identifier => Temp_Id,
5378 Object_Definition => New_Reference_To (Ptr_Id, Loc));
5379
5380 Insert_Action (Ins_Node, Temp_Decl);
5381 Analyze (Temp_Decl);
5382
5383 -- Mark this temporary as created for the purposes of exporting the
5384 -- transient declaration out of the Actions list. This signals the
5385 -- machinery in Build_Finalizer to recognize this special case.
5386
5387 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
5388
5389 -- Step 3: Hook the transient object to the temporary
5390
5391 if Is_Access_Type (Obj_Typ) then
5392 Expr := Convert_To (Ptr_Id, New_Reference_To (Obj_Id, Loc));
5393 else
5394 Expr :=
5395 Make_Attribute_Reference (Loc,
5396 Prefix => New_Reference_To (Obj_Id, Loc),
5397 Attribute_Name => Name_Unrestricted_Access);
5398 end if;
5399
5400 -- Generate:
5401 -- Temp := Ptr_Id (Obj_Id);
5402 -- <or>
5403 -- Temp := Obj_Id'Unrestricted_Access;
5404
5405 Insert_After_And_Analyze (Decl,
5406 Make_Assignment_Statement (Loc,
5407 Name => New_Reference_To (Temp_Id, Loc),
5408 Expression => Expr));
5409 end Process_Transient_Object;
5410
5411 -- Local variables
5412
5413 Decl : Node_Id;
5414
5415 -- Start of processing for Expand_N_Expression_With_Actions
5416
5417 begin
5418 Decl := First (Actions (N));
5419 while Present (Decl) loop
5420 if Nkind (Decl) = N_Object_Declaration
5421 and then Is_Finalizable_Transient (Decl, N)
5422 then
5423 Process_Transient_Object (Decl);
5424 end if;
5425
5426 Next (Decl);
5427 end loop;
5428 end Expand_N_Expression_With_Actions;
5429
5430 -----------------
5431 -- Expand_N_In --
5432 -----------------
5433
5434 procedure Expand_N_In (N : Node_Id) is
5435 Loc : constant Source_Ptr := Sloc (N);
5436 Restyp : constant Entity_Id := Etype (N);
5437 Lop : constant Node_Id := Left_Opnd (N);
5438 Rop : constant Node_Id := Right_Opnd (N);
5439 Static : constant Boolean := Is_OK_Static_Expression (N);
5440
5441 Ltyp : Entity_Id;
5442 Rtyp : Entity_Id;
5443
5444 procedure Substitute_Valid_Check;
5445 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5446 -- test for the left operand being in range of its subtype.
5447
5448 ----------------------------
5449 -- Substitute_Valid_Check --
5450 ----------------------------
5451
5452 procedure Substitute_Valid_Check is
5453 begin
5454 Rewrite (N,
5455 Make_Attribute_Reference (Loc,
5456 Prefix => Relocate_Node (Lop),
5457 Attribute_Name => Name_Valid));
5458
5459 Analyze_And_Resolve (N, Restyp);
5460
5461 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5462 -- in which case, this usage makes sense, and in any case, we have
5463 -- actually eliminated the danger of optimization above.
5464
5465 if Overflow_Check_Mode (Restyp) not in Minimized_Or_Eliminated then
5466 Error_Msg_N ("?explicit membership test may be optimized away", N);
5467 Error_Msg_N -- CODEFIX
5468 ("\?use ''Valid attribute instead", N);
5469 end if;
5470
5471 return;
5472 end Substitute_Valid_Check;
5473
5474 -- Start of processing for Expand_N_In
5475
5476 begin
5477 -- If set membership case, expand with separate procedure
5478
5479 if Present (Alternatives (N)) then
5480 Expand_Set_Membership (N);
5481 return;
5482 end if;
5483
5484 -- Not set membership, proceed with expansion
5485
5486 Ltyp := Etype (Left_Opnd (N));
5487 Rtyp := Etype (Right_Opnd (N));
5488
5489 -- If Minimize/Eliminate overflow mode and type is a signed integer
5490 -- type, then expand with a separate procedure. Note the use of the
5491 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5492
5493 if Overflow_Check_Mode (Empty) in Minimized_Or_Eliminated
5494 and then Is_Signed_Integer_Type (Ltyp)
5495 and then not No_Minimize_Eliminate (N)
5496 then
5497 Expand_Membership_Minimize_Eliminate_Overflow (N);
5498 return;
5499 end if;
5500
5501 -- Check case of explicit test for an expression in range of its
5502 -- subtype. This is suspicious usage and we replace it with a 'Valid
5503 -- test and give a warning. For floating point types however, this is a
5504 -- standard way to check for finite numbers, and using 'Valid would
5505 -- typically be a pessimization. Also skip this test for predicated
5506 -- types, since it is perfectly reasonable to check if a value meets
5507 -- its predicate.
5508
5509 if Is_Scalar_Type (Ltyp)
5510 and then not Is_Floating_Point_Type (Ltyp)
5511 and then Nkind (Rop) in N_Has_Entity
5512 and then Ltyp = Entity (Rop)
5513 and then Comes_From_Source (N)
5514 and then VM_Target = No_VM
5515 and then not (Is_Discrete_Type (Ltyp)
5516 and then Present (Predicate_Function (Ltyp)))
5517 then
5518 Substitute_Valid_Check;
5519 return;
5520 end if;
5521
5522 -- Do validity check on operands
5523
5524 if Validity_Checks_On and Validity_Check_Operands then
5525 Ensure_Valid (Left_Opnd (N));
5526 Validity_Check_Range (Right_Opnd (N));
5527 end if;
5528
5529 -- Case of explicit range
5530
5531 if Nkind (Rop) = N_Range then
5532 declare
5533 Lo : constant Node_Id := Low_Bound (Rop);
5534 Hi : constant Node_Id := High_Bound (Rop);
5535
5536 Lo_Orig : constant Node_Id := Original_Node (Lo);
5537 Hi_Orig : constant Node_Id := Original_Node (Hi);
5538
5539 Lcheck : Compare_Result;
5540 Ucheck : Compare_Result;
5541
5542 Warn1 : constant Boolean :=
5543 Constant_Condition_Warnings
5544 and then Comes_From_Source (N)
5545 and then not In_Instance;
5546 -- This must be true for any of the optimization warnings, we
5547 -- clearly want to give them only for source with the flag on. We
5548 -- also skip these warnings in an instance since it may be the
5549 -- case that different instantiations have different ranges.
5550
5551 Warn2 : constant Boolean :=
5552 Warn1
5553 and then Nkind (Original_Node (Rop)) = N_Range
5554 and then Is_Integer_Type (Etype (Lo));
5555 -- For the case where only one bound warning is elided, we also
5556 -- insist on an explicit range and an integer type. The reason is
5557 -- that the use of enumeration ranges including an end point is
5558 -- common, as is the use of a subtype name, one of whose bounds is
5559 -- the same as the type of the expression.
5560
5561 begin
5562 -- If test is explicit x'First .. x'Last, replace by valid check
5563
5564 -- Could use some individual comments for this complex test ???
5565
5566 if Is_Scalar_Type (Ltyp)
5567 and then Nkind (Lo_Orig) = N_Attribute_Reference
5568 and then Attribute_Name (Lo_Orig) = Name_First
5569 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5570 and then Entity (Prefix (Lo_Orig)) = Ltyp
5571 and then Nkind (Hi_Orig) = N_Attribute_Reference
5572 and then Attribute_Name (Hi_Orig) = Name_Last
5573 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5574 and then Entity (Prefix (Hi_Orig)) = Ltyp
5575 and then Comes_From_Source (N)
5576 and then VM_Target = No_VM
5577 then
5578 Substitute_Valid_Check;
5579 goto Leave;
5580 end if;
5581
5582 -- If bounds of type are known at compile time, and the end points
5583 -- are known at compile time and identical, this is another case
5584 -- for substituting a valid test. We only do this for discrete
5585 -- types, since it won't arise in practice for float types.
5586
5587 if Comes_From_Source (N)
5588 and then Is_Discrete_Type (Ltyp)
5589 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5590 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5591 and then Compile_Time_Known_Value (Lo)
5592 and then Compile_Time_Known_Value (Hi)
5593 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5594 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5595
5596 -- Kill warnings in instances, since they may be cases where we
5597 -- have a test in the generic that makes sense with some types
5598 -- and not with other types.
5599
5600 and then not In_Instance
5601 then
5602 Substitute_Valid_Check;
5603 goto Leave;
5604 end if;
5605
5606 -- If we have an explicit range, do a bit of optimization based on
5607 -- range analysis (we may be able to kill one or both checks).
5608
5609 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5610 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5611
5612 -- If either check is known to fail, replace result by False since
5613 -- the other check does not matter. Preserve the static flag for
5614 -- legality checks, because we are constant-folding beyond RM 4.9.
5615
5616 if Lcheck = LT or else Ucheck = GT then
5617 if Warn1 then
5618 Error_Msg_N ("?range test optimized away", N);
5619 Error_Msg_N ("\?value is known to be out of range", N);
5620 end if;
5621
5622 Rewrite (N, New_Reference_To (Standard_False, Loc));
5623 Analyze_And_Resolve (N, Restyp);
5624 Set_Is_Static_Expression (N, Static);
5625 goto Leave;
5626
5627 -- If both checks are known to succeed, replace result by True,
5628 -- since we know we are in range.
5629
5630 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5631 if Warn1 then
5632 Error_Msg_N ("?range test optimized away", N);
5633 Error_Msg_N ("\?value is known to be in range", N);
5634 end if;
5635
5636 Rewrite (N, New_Reference_To (Standard_True, Loc));
5637 Analyze_And_Resolve (N, Restyp);
5638 Set_Is_Static_Expression (N, Static);
5639 goto Leave;
5640
5641 -- If lower bound check succeeds and upper bound check is not
5642 -- known to succeed or fail, then replace the range check with
5643 -- a comparison against the upper bound.
5644
5645 elsif Lcheck in Compare_GE then
5646 if Warn2 and then not In_Instance then
5647 Error_Msg_N ("?lower bound test optimized away", Lo);
5648 Error_Msg_N ("\?value is known to be in range", Lo);
5649 end if;
5650
5651 Rewrite (N,
5652 Make_Op_Le (Loc,
5653 Left_Opnd => Lop,
5654 Right_Opnd => High_Bound (Rop)));
5655 Analyze_And_Resolve (N, Restyp);
5656 goto Leave;
5657
5658 -- If upper bound check succeeds and lower bound check is not
5659 -- known to succeed or fail, then replace the range check with
5660 -- a comparison against the lower bound.
5661
5662 elsif Ucheck in Compare_LE then
5663 if Warn2 and then not In_Instance then
5664 Error_Msg_N ("?upper bound test optimized away", Hi);
5665 Error_Msg_N ("\?value is known to be in range", Hi);
5666 end if;
5667
5668 Rewrite (N,
5669 Make_Op_Ge (Loc,
5670 Left_Opnd => Lop,
5671 Right_Opnd => Low_Bound (Rop)));
5672 Analyze_And_Resolve (N, Restyp);
5673 goto Leave;
5674 end if;
5675
5676 -- We couldn't optimize away the range check, but there is one
5677 -- more issue. If we are checking constant conditionals, then we
5678 -- see if we can determine the outcome assuming everything is
5679 -- valid, and if so give an appropriate warning.
5680
5681 if Warn1 and then not Assume_No_Invalid_Values then
5682 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5683 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5684
5685 -- Result is out of range for valid value
5686
5687 if Lcheck = LT or else Ucheck = GT then
5688 Error_Msg_N
5689 ("?value can only be in range if it is invalid", N);
5690
5691 -- Result is in range for valid value
5692
5693 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5694 Error_Msg_N
5695 ("?value can only be out of range if it is invalid", N);
5696
5697 -- Lower bound check succeeds if value is valid
5698
5699 elsif Warn2 and then Lcheck in Compare_GE then
5700 Error_Msg_N
5701 ("?lower bound check only fails if it is invalid", Lo);
5702
5703 -- Upper bound check succeeds if value is valid
5704
5705 elsif Warn2 and then Ucheck in Compare_LE then
5706 Error_Msg_N
5707 ("?upper bound check only fails for invalid values", Hi);
5708 end if;
5709 end if;
5710 end;
5711
5712 -- For all other cases of an explicit range, nothing to be done
5713
5714 goto Leave;
5715
5716 -- Here right operand is a subtype mark
5717
5718 else
5719 declare
5720 Typ : Entity_Id := Etype (Rop);
5721 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5722 Cond : Node_Id := Empty;
5723 New_N : Node_Id;
5724 Obj : Node_Id := Lop;
5725 SCIL_Node : Node_Id;
5726
5727 begin
5728 Remove_Side_Effects (Obj);
5729
5730 -- For tagged type, do tagged membership operation
5731
5732 if Is_Tagged_Type (Typ) then
5733
5734 -- No expansion will be performed when VM_Target, as the VM
5735 -- back-ends will handle the membership tests directly (tags
5736 -- are not explicitly represented in Java objects, so the
5737 -- normal tagged membership expansion is not what we want).
5738
5739 if Tagged_Type_Expansion then
5740 Tagged_Membership (N, SCIL_Node, New_N);
5741 Rewrite (N, New_N);
5742 Analyze_And_Resolve (N, Restyp);
5743
5744 -- Update decoration of relocated node referenced by the
5745 -- SCIL node.
5746
5747 if Generate_SCIL and then Present (SCIL_Node) then
5748 Set_SCIL_Node (N, SCIL_Node);
5749 end if;
5750 end if;
5751
5752 goto Leave;
5753
5754 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5755 -- This reason we do this is that the bounds may have the wrong
5756 -- type if they come from the original type definition. Also this
5757 -- way we get all the processing above for an explicit range.
5758
5759 -- Don't do this for predicated types, since in this case we
5760 -- want to check the predicate!
5761
5762 elsif Is_Scalar_Type (Typ) then
5763 if No (Predicate_Function (Typ)) then
5764 Rewrite (Rop,
5765 Make_Range (Loc,
5766 Low_Bound =>
5767 Make_Attribute_Reference (Loc,
5768 Attribute_Name => Name_First,
5769 Prefix => New_Reference_To (Typ, Loc)),
5770
5771 High_Bound =>
5772 Make_Attribute_Reference (Loc,
5773 Attribute_Name => Name_Last,
5774 Prefix => New_Reference_To (Typ, Loc))));
5775 Analyze_And_Resolve (N, Restyp);
5776 end if;
5777
5778 goto Leave;
5779
5780 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5781 -- a membership test if the subtype mark denotes a constrained
5782 -- Unchecked_Union subtype and the expression lacks inferable
5783 -- discriminants.
5784
5785 elsif Is_Unchecked_Union (Base_Type (Typ))
5786 and then Is_Constrained (Typ)
5787 and then not Has_Inferable_Discriminants (Lop)
5788 then
5789 Insert_Action (N,
5790 Make_Raise_Program_Error (Loc,
5791 Reason => PE_Unchecked_Union_Restriction));
5792
5793 -- Prevent Gigi from generating incorrect code by rewriting the
5794 -- test as False. What is this undocumented thing about ???
5795
5796 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5797 goto Leave;
5798 end if;
5799
5800 -- Here we have a non-scalar type
5801
5802 if Is_Acc then
5803 Typ := Designated_Type (Typ);
5804 end if;
5805
5806 if not Is_Constrained (Typ) then
5807 Rewrite (N, New_Reference_To (Standard_True, Loc));
5808 Analyze_And_Resolve (N, Restyp);
5809
5810 -- For the constrained array case, we have to check the subscripts
5811 -- for an exact match if the lengths are non-zero (the lengths
5812 -- must match in any case).
5813
5814 elsif Is_Array_Type (Typ) then
5815 Check_Subscripts : declare
5816 function Build_Attribute_Reference
5817 (E : Node_Id;
5818 Nam : Name_Id;
5819 Dim : Nat) return Node_Id;
5820 -- Build attribute reference E'Nam (Dim)
5821
5822 -------------------------------
5823 -- Build_Attribute_Reference --
5824 -------------------------------
5825
5826 function Build_Attribute_Reference
5827 (E : Node_Id;
5828 Nam : Name_Id;
5829 Dim : Nat) return Node_Id
5830 is
5831 begin
5832 return
5833 Make_Attribute_Reference (Loc,
5834 Prefix => E,
5835 Attribute_Name => Nam,
5836 Expressions => New_List (
5837 Make_Integer_Literal (Loc, Dim)));
5838 end Build_Attribute_Reference;
5839
5840 -- Start of processing for Check_Subscripts
5841
5842 begin
5843 for J in 1 .. Number_Dimensions (Typ) loop
5844 Evolve_And_Then (Cond,
5845 Make_Op_Eq (Loc,
5846 Left_Opnd =>
5847 Build_Attribute_Reference
5848 (Duplicate_Subexpr_No_Checks (Obj),
5849 Name_First, J),
5850 Right_Opnd =>
5851 Build_Attribute_Reference
5852 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5853
5854 Evolve_And_Then (Cond,
5855 Make_Op_Eq (Loc,
5856 Left_Opnd =>
5857 Build_Attribute_Reference
5858 (Duplicate_Subexpr_No_Checks (Obj),
5859 Name_Last, J),
5860 Right_Opnd =>
5861 Build_Attribute_Reference
5862 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5863 end loop;
5864
5865 if Is_Acc then
5866 Cond :=
5867 Make_Or_Else (Loc,
5868 Left_Opnd =>
5869 Make_Op_Eq (Loc,
5870 Left_Opnd => Obj,
5871 Right_Opnd => Make_Null (Loc)),
5872 Right_Opnd => Cond);
5873 end if;
5874
5875 Rewrite (N, Cond);
5876 Analyze_And_Resolve (N, Restyp);
5877 end Check_Subscripts;
5878
5879 -- These are the cases where constraint checks may be required,
5880 -- e.g. records with possible discriminants
5881
5882 else
5883 -- Expand the test into a series of discriminant comparisons.
5884 -- The expression that is built is the negation of the one that
5885 -- is used for checking discriminant constraints.
5886
5887 Obj := Relocate_Node (Left_Opnd (N));
5888
5889 if Has_Discriminants (Typ) then
5890 Cond := Make_Op_Not (Loc,
5891 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5892
5893 if Is_Acc then
5894 Cond := Make_Or_Else (Loc,
5895 Left_Opnd =>
5896 Make_Op_Eq (Loc,
5897 Left_Opnd => Obj,
5898 Right_Opnd => Make_Null (Loc)),
5899 Right_Opnd => Cond);
5900 end if;
5901
5902 else
5903 Cond := New_Occurrence_Of (Standard_True, Loc);
5904 end if;
5905
5906 Rewrite (N, Cond);
5907 Analyze_And_Resolve (N, Restyp);
5908 end if;
5909
5910 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5911 -- expression of an anonymous access type. This can involve an
5912 -- accessibility test and a tagged type membership test in the
5913 -- case of tagged designated types.
5914
5915 if Ada_Version >= Ada_2012
5916 and then Is_Acc
5917 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5918 then
5919 declare
5920 Expr_Entity : Entity_Id := Empty;
5921 New_N : Node_Id;
5922 Param_Level : Node_Id;
5923 Type_Level : Node_Id;
5924
5925 begin
5926 if Is_Entity_Name (Lop) then
5927 Expr_Entity := Param_Entity (Lop);
5928
5929 if not Present (Expr_Entity) then
5930 Expr_Entity := Entity (Lop);
5931 end if;
5932 end if;
5933
5934 -- If a conversion of the anonymous access value to the
5935 -- tested type would be illegal, then the result is False.
5936
5937 if not Valid_Conversion
5938 (Lop, Rtyp, Lop, Report_Errs => False)
5939 then
5940 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5941 Analyze_And_Resolve (N, Restyp);
5942
5943 -- Apply an accessibility check if the access object has an
5944 -- associated access level and when the level of the type is
5945 -- less deep than the level of the access parameter. This
5946 -- only occur for access parameters and stand-alone objects
5947 -- of an anonymous access type.
5948
5949 else
5950 if Present (Expr_Entity)
5951 and then
5952 Present
5953 (Effective_Extra_Accessibility (Expr_Entity))
5954 and then UI_Gt (Object_Access_Level (Lop),
5955 Type_Access_Level (Rtyp))
5956 then
5957 Param_Level :=
5958 New_Occurrence_Of
5959 (Effective_Extra_Accessibility (Expr_Entity), Loc);
5960
5961 Type_Level :=
5962 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
5963
5964 -- Return True only if the accessibility level of the
5965 -- expression entity is not deeper than the level of
5966 -- the tested access type.
5967
5968 Rewrite (N,
5969 Make_And_Then (Loc,
5970 Left_Opnd => Relocate_Node (N),
5971 Right_Opnd => Make_Op_Le (Loc,
5972 Left_Opnd => Param_Level,
5973 Right_Opnd => Type_Level)));
5974
5975 Analyze_And_Resolve (N);
5976 end if;
5977
5978 -- If the designated type is tagged, do tagged membership
5979 -- operation.
5980
5981 -- *** NOTE: we have to check not null before doing the
5982 -- tagged membership test (but maybe that can be done
5983 -- inside Tagged_Membership?).
5984
5985 if Is_Tagged_Type (Typ) then
5986 Rewrite (N,
5987 Make_And_Then (Loc,
5988 Left_Opnd => Relocate_Node (N),
5989 Right_Opnd =>
5990 Make_Op_Ne (Loc,
5991 Left_Opnd => Obj,
5992 Right_Opnd => Make_Null (Loc))));
5993
5994 -- No expansion will be performed when VM_Target, as
5995 -- the VM back-ends will handle the membership tests
5996 -- directly (tags are not explicitly represented in
5997 -- Java objects, so the normal tagged membership
5998 -- expansion is not what we want).
5999
6000 if Tagged_Type_Expansion then
6001
6002 -- Note that we have to pass Original_Node, because
6003 -- the membership test might already have been
6004 -- rewritten by earlier parts of membership test.
6005
6006 Tagged_Membership
6007 (Original_Node (N), SCIL_Node, New_N);
6008
6009 -- Update decoration of relocated node referenced
6010 -- by the SCIL node.
6011
6012 if Generate_SCIL and then Present (SCIL_Node) then
6013 Set_SCIL_Node (New_N, SCIL_Node);
6014 end if;
6015
6016 Rewrite (N,
6017 Make_And_Then (Loc,
6018 Left_Opnd => Relocate_Node (N),
6019 Right_Opnd => New_N));
6020
6021 Analyze_And_Resolve (N, Restyp);
6022 end if;
6023 end if;
6024 end if;
6025 end;
6026 end if;
6027 end;
6028 end if;
6029
6030 -- At this point, we have done the processing required for the basic
6031 -- membership test, but not yet dealt with the predicate.
6032
6033 <<Leave>>
6034
6035 -- If a predicate is present, then we do the predicate test, but we
6036 -- most certainly want to omit this if we are within the predicate
6037 -- function itself, since otherwise we have an infinite recursion!
6038
6039 declare
6040 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6041
6042 begin
6043 if Present (PFunc)
6044 and then Current_Scope /= PFunc
6045 then
6046 Rewrite (N,
6047 Make_And_Then (Loc,
6048 Left_Opnd => Relocate_Node (N),
6049 Right_Opnd => Make_Predicate_Call (Rtyp, Lop)));
6050
6051 -- Analyze new expression, mark left operand as analyzed to
6052 -- avoid infinite recursion adding predicate calls. Similarly,
6053 -- suppress further range checks on the call.
6054
6055 Set_Analyzed (Left_Opnd (N));
6056 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6057
6058 -- All done, skip attempt at compile time determination of result
6059
6060 return;
6061 end if;
6062 end;
6063 end Expand_N_In;
6064
6065 --------------------------------
6066 -- Expand_N_Indexed_Component --
6067 --------------------------------
6068
6069 procedure Expand_N_Indexed_Component (N : Node_Id) is
6070 Loc : constant Source_Ptr := Sloc (N);
6071 Typ : constant Entity_Id := Etype (N);
6072 P : constant Node_Id := Prefix (N);
6073 T : constant Entity_Id := Etype (P);
6074 Atp : Entity_Id;
6075
6076 begin
6077 -- A special optimization, if we have an indexed component that is
6078 -- selecting from a slice, then we can eliminate the slice, since, for
6079 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6080 -- the range check required by the slice. The range check for the slice
6081 -- itself has already been generated. The range check for the
6082 -- subscripting operation is ensured by converting the subject to
6083 -- the subtype of the slice.
6084
6085 -- This optimization not only generates better code, avoiding slice
6086 -- messing especially in the packed case, but more importantly bypasses
6087 -- some problems in handling this peculiar case, for example, the issue
6088 -- of dealing specially with object renamings.
6089
6090 if Nkind (P) = N_Slice then
6091 Rewrite (N,
6092 Make_Indexed_Component (Loc,
6093 Prefix => Prefix (P),
6094 Expressions => New_List (
6095 Convert_To
6096 (Etype (First_Index (Etype (P))),
6097 First (Expressions (N))))));
6098 Analyze_And_Resolve (N, Typ);
6099 return;
6100 end if;
6101
6102 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6103 -- function, then additional actuals must be passed.
6104
6105 if Ada_Version >= Ada_2005
6106 and then Is_Build_In_Place_Function_Call (P)
6107 then
6108 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6109 end if;
6110
6111 -- If the prefix is an access type, then we unconditionally rewrite if
6112 -- as an explicit dereference. This simplifies processing for several
6113 -- cases, including packed array cases and certain cases in which checks
6114 -- must be generated. We used to try to do this only when it was
6115 -- necessary, but it cleans up the code to do it all the time.
6116
6117 if Is_Access_Type (T) then
6118 Insert_Explicit_Dereference (P);
6119 Analyze_And_Resolve (P, Designated_Type (T));
6120 Atp := Designated_Type (T);
6121 else
6122 Atp := T;
6123 end if;
6124
6125 -- Generate index and validity checks
6126
6127 Generate_Index_Checks (N);
6128
6129 if Validity_Checks_On and then Validity_Check_Subscripts then
6130 Apply_Subscript_Validity_Checks (N);
6131 end if;
6132
6133 -- If selecting from an array with atomic components, and atomic sync
6134 -- is not suppressed for this array type, set atomic sync flag.
6135
6136 if (Has_Atomic_Components (Atp)
6137 and then not Atomic_Synchronization_Disabled (Atp))
6138 or else (Is_Atomic (Typ)
6139 and then not Atomic_Synchronization_Disabled (Typ))
6140 then
6141 Activate_Atomic_Synchronization (N);
6142 end if;
6143
6144 -- All done for the non-packed case
6145
6146 if not Is_Packed (Etype (Prefix (N))) then
6147 return;
6148 end if;
6149
6150 -- For packed arrays that are not bit-packed (i.e. the case of an array
6151 -- with one or more index types with a non-contiguous enumeration type),
6152 -- we can always use the normal packed element get circuit.
6153
6154 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6155 Expand_Packed_Element_Reference (N);
6156 return;
6157 end if;
6158
6159 -- For a reference to a component of a bit packed array, we have to
6160 -- convert it to a reference to the corresponding Packed_Array_Type.
6161 -- We only want to do this for simple references, and not for:
6162
6163 -- Left side of assignment, or prefix of left side of assignment, or
6164 -- prefix of the prefix, to handle packed arrays of packed arrays,
6165 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6166
6167 -- Renaming objects in renaming associations
6168 -- This case is handled when a use of the renamed variable occurs
6169
6170 -- Actual parameters for a procedure call
6171 -- This case is handled in Exp_Ch6.Expand_Actuals
6172
6173 -- The second expression in a 'Read attribute reference
6174
6175 -- The prefix of an address or bit or size attribute reference
6176
6177 -- The following circuit detects these exceptions
6178
6179 declare
6180 Child : Node_Id := N;
6181 Parnt : Node_Id := Parent (N);
6182
6183 begin
6184 loop
6185 if Nkind (Parnt) = N_Unchecked_Expression then
6186 null;
6187
6188 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6189 N_Procedure_Call_Statement)
6190 or else (Nkind (Parnt) = N_Parameter_Association
6191 and then
6192 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6193 then
6194 return;
6195
6196 elsif Nkind (Parnt) = N_Attribute_Reference
6197 and then (Attribute_Name (Parnt) = Name_Address
6198 or else
6199 Attribute_Name (Parnt) = Name_Bit
6200 or else
6201 Attribute_Name (Parnt) = Name_Size)
6202 and then Prefix (Parnt) = Child
6203 then
6204 return;
6205
6206 elsif Nkind (Parnt) = N_Assignment_Statement
6207 and then Name (Parnt) = Child
6208 then
6209 return;
6210
6211 -- If the expression is an index of an indexed component, it must
6212 -- be expanded regardless of context.
6213
6214 elsif Nkind (Parnt) = N_Indexed_Component
6215 and then Child /= Prefix (Parnt)
6216 then
6217 Expand_Packed_Element_Reference (N);
6218 return;
6219
6220 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6221 and then Name (Parent (Parnt)) = Parnt
6222 then
6223 return;
6224
6225 elsif Nkind (Parnt) = N_Attribute_Reference
6226 and then Attribute_Name (Parnt) = Name_Read
6227 and then Next (First (Expressions (Parnt))) = Child
6228 then
6229 return;
6230
6231 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6232 and then Prefix (Parnt) = Child
6233 then
6234 null;
6235
6236 else
6237 Expand_Packed_Element_Reference (N);
6238 return;
6239 end if;
6240
6241 -- Keep looking up tree for unchecked expression, or if we are the
6242 -- prefix of a possible assignment left side.
6243
6244 Child := Parnt;
6245 Parnt := Parent (Child);
6246 end loop;
6247 end;
6248 end Expand_N_Indexed_Component;
6249
6250 ---------------------
6251 -- Expand_N_Not_In --
6252 ---------------------
6253
6254 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6255 -- can be done. This avoids needing to duplicate this expansion code.
6256
6257 procedure Expand_N_Not_In (N : Node_Id) is
6258 Loc : constant Source_Ptr := Sloc (N);
6259 Typ : constant Entity_Id := Etype (N);
6260 Cfs : constant Boolean := Comes_From_Source (N);
6261
6262 begin
6263 Rewrite (N,
6264 Make_Op_Not (Loc,
6265 Right_Opnd =>
6266 Make_In (Loc,
6267 Left_Opnd => Left_Opnd (N),
6268 Right_Opnd => Right_Opnd (N))));
6269
6270 -- If this is a set membership, preserve list of alternatives
6271
6272 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6273
6274 -- We want this to appear as coming from source if original does (see
6275 -- transformations in Expand_N_In).
6276
6277 Set_Comes_From_Source (N, Cfs);
6278 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6279
6280 -- Now analyze transformed node
6281
6282 Analyze_And_Resolve (N, Typ);
6283 end Expand_N_Not_In;
6284
6285 -------------------
6286 -- Expand_N_Null --
6287 -------------------
6288
6289 -- The only replacement required is for the case of a null of a type that
6290 -- is an access to protected subprogram, or a subtype thereof. We represent
6291 -- such access values as a record, and so we must replace the occurrence of
6292 -- null by the equivalent record (with a null address and a null pointer in
6293 -- it), so that the backend creates the proper value.
6294
6295 procedure Expand_N_Null (N : Node_Id) is
6296 Loc : constant Source_Ptr := Sloc (N);
6297 Typ : constant Entity_Id := Base_Type (Etype (N));
6298 Agg : Node_Id;
6299
6300 begin
6301 if Is_Access_Protected_Subprogram_Type (Typ) then
6302 Agg :=
6303 Make_Aggregate (Loc,
6304 Expressions => New_List (
6305 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6306 Make_Null (Loc)));
6307
6308 Rewrite (N, Agg);
6309 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6310
6311 -- For subsequent semantic analysis, the node must retain its type.
6312 -- Gigi in any case replaces this type by the corresponding record
6313 -- type before processing the node.
6314
6315 Set_Etype (N, Typ);
6316 end if;
6317
6318 exception
6319 when RE_Not_Available =>
6320 return;
6321 end Expand_N_Null;
6322
6323 ---------------------
6324 -- Expand_N_Op_Abs --
6325 ---------------------
6326
6327 procedure Expand_N_Op_Abs (N : Node_Id) is
6328 Loc : constant Source_Ptr := Sloc (N);
6329 Expr : constant Node_Id := Right_Opnd (N);
6330
6331 begin
6332 Unary_Op_Validity_Checks (N);
6333
6334 -- Deal with software overflow checking
6335
6336 if not Backend_Overflow_Checks_On_Target
6337 and then Is_Signed_Integer_Type (Etype (N))
6338 and then Do_Overflow_Check (N)
6339 then
6340 -- The only case to worry about is when the argument is equal to the
6341 -- largest negative number, so what we do is to insert the check:
6342
6343 -- [constraint_error when Expr = typ'Base'First]
6344
6345 -- with the usual Duplicate_Subexpr use coding for expr
6346
6347 Insert_Action (N,
6348 Make_Raise_Constraint_Error (Loc,
6349 Condition =>
6350 Make_Op_Eq (Loc,
6351 Left_Opnd => Duplicate_Subexpr (Expr),
6352 Right_Opnd =>
6353 Make_Attribute_Reference (Loc,
6354 Prefix =>
6355 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6356 Attribute_Name => Name_First)),
6357 Reason => CE_Overflow_Check_Failed));
6358 end if;
6359
6360 -- Vax floating-point types case
6361
6362 if Vax_Float (Etype (N)) then
6363 Expand_Vax_Arith (N);
6364 end if;
6365 end Expand_N_Op_Abs;
6366
6367 ---------------------
6368 -- Expand_N_Op_Add --
6369 ---------------------
6370
6371 procedure Expand_N_Op_Add (N : Node_Id) is
6372 Typ : constant Entity_Id := Etype (N);
6373
6374 begin
6375 Binary_Op_Validity_Checks (N);
6376
6377 -- N + 0 = 0 + N = N for integer types
6378
6379 if Is_Integer_Type (Typ) then
6380 if Compile_Time_Known_Value (Right_Opnd (N))
6381 and then Expr_Value (Right_Opnd (N)) = Uint_0
6382 then
6383 Rewrite (N, Left_Opnd (N));
6384 return;
6385
6386 elsif Compile_Time_Known_Value (Left_Opnd (N))
6387 and then Expr_Value (Left_Opnd (N)) = Uint_0
6388 then
6389 Rewrite (N, Right_Opnd (N));
6390 return;
6391 end if;
6392 end if;
6393
6394 -- Arithmetic overflow checks for signed integer/fixed point types
6395
6396 if Is_Signed_Integer_Type (Typ)
6397 or else Is_Fixed_Point_Type (Typ)
6398 then
6399 Apply_Arithmetic_Overflow_Check (N);
6400 return;
6401
6402 -- Vax floating-point types case
6403
6404 elsif Vax_Float (Typ) then
6405 Expand_Vax_Arith (N);
6406 end if;
6407 end Expand_N_Op_Add;
6408
6409 ---------------------
6410 -- Expand_N_Op_And --
6411 ---------------------
6412
6413 procedure Expand_N_Op_And (N : Node_Id) is
6414 Typ : constant Entity_Id := Etype (N);
6415
6416 begin
6417 Binary_Op_Validity_Checks (N);
6418
6419 if Is_Array_Type (Etype (N)) then
6420 Expand_Boolean_Operator (N);
6421
6422 elsif Is_Boolean_Type (Etype (N)) then
6423 Adjust_Condition (Left_Opnd (N));
6424 Adjust_Condition (Right_Opnd (N));
6425 Set_Etype (N, Standard_Boolean);
6426 Adjust_Result_Type (N, Typ);
6427
6428 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6429 Expand_Intrinsic_Call (N, Entity (N));
6430
6431 end if;
6432 end Expand_N_Op_And;
6433
6434 ------------------------
6435 -- Expand_N_Op_Concat --
6436 ------------------------
6437
6438 procedure Expand_N_Op_Concat (N : Node_Id) is
6439 Opnds : List_Id;
6440 -- List of operands to be concatenated
6441
6442 Cnode : Node_Id;
6443 -- Node which is to be replaced by the result of concatenating the nodes
6444 -- in the list Opnds.
6445
6446 begin
6447 -- Ensure validity of both operands
6448
6449 Binary_Op_Validity_Checks (N);
6450
6451 -- If we are the left operand of a concatenation higher up the tree,
6452 -- then do nothing for now, since we want to deal with a series of
6453 -- concatenations as a unit.
6454
6455 if Nkind (Parent (N)) = N_Op_Concat
6456 and then N = Left_Opnd (Parent (N))
6457 then
6458 return;
6459 end if;
6460
6461 -- We get here with a concatenation whose left operand may be a
6462 -- concatenation itself with a consistent type. We need to process
6463 -- these concatenation operands from left to right, which means
6464 -- from the deepest node in the tree to the highest node.
6465
6466 Cnode := N;
6467 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6468 Cnode := Left_Opnd (Cnode);
6469 end loop;
6470
6471 -- Now Cnode is the deepest concatenation, and its parents are the
6472 -- concatenation nodes above, so now we process bottom up, doing the
6473 -- operations. We gather a string that is as long as possible up to five
6474 -- operands.
6475
6476 -- The outer loop runs more than once if more than one concatenation
6477 -- type is involved.
6478
6479 Outer : loop
6480 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6481 Set_Parent (Opnds, N);
6482
6483 -- The inner loop gathers concatenation operands
6484
6485 Inner : while Cnode /= N
6486 and then Base_Type (Etype (Cnode)) =
6487 Base_Type (Etype (Parent (Cnode)))
6488 loop
6489 Cnode := Parent (Cnode);
6490 Append (Right_Opnd (Cnode), Opnds);
6491 end loop Inner;
6492
6493 Expand_Concatenate (Cnode, Opnds);
6494
6495 exit Outer when Cnode = N;
6496 Cnode := Parent (Cnode);
6497 end loop Outer;
6498 end Expand_N_Op_Concat;
6499
6500 ------------------------
6501 -- Expand_N_Op_Divide --
6502 ------------------------
6503
6504 procedure Expand_N_Op_Divide (N : Node_Id) is
6505 Loc : constant Source_Ptr := Sloc (N);
6506 Lopnd : constant Node_Id := Left_Opnd (N);
6507 Ropnd : constant Node_Id := Right_Opnd (N);
6508 Ltyp : constant Entity_Id := Etype (Lopnd);
6509 Rtyp : constant Entity_Id := Etype (Ropnd);
6510 Typ : Entity_Id := Etype (N);
6511 Rknow : constant Boolean := Is_Integer_Type (Typ)
6512 and then
6513 Compile_Time_Known_Value (Ropnd);
6514 Rval : Uint;
6515
6516 begin
6517 Binary_Op_Validity_Checks (N);
6518
6519 if Rknow then
6520 Rval := Expr_Value (Ropnd);
6521 end if;
6522
6523 -- N / 1 = N for integer types
6524
6525 if Rknow and then Rval = Uint_1 then
6526 Rewrite (N, Lopnd);
6527 return;
6528 end if;
6529
6530 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6531 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6532 -- operand is an unsigned integer, as required for this to work.
6533
6534 if Nkind (Ropnd) = N_Op_Expon
6535 and then Is_Power_Of_2_For_Shift (Ropnd)
6536
6537 -- We cannot do this transformation in configurable run time mode if we
6538 -- have 64-bit integers and long shifts are not available.
6539
6540 and then
6541 (Esize (Ltyp) <= 32
6542 or else Support_Long_Shifts_On_Target)
6543 then
6544 Rewrite (N,
6545 Make_Op_Shift_Right (Loc,
6546 Left_Opnd => Lopnd,
6547 Right_Opnd =>
6548 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6549 Analyze_And_Resolve (N, Typ);
6550 return;
6551 end if;
6552
6553 -- Do required fixup of universal fixed operation
6554
6555 if Typ = Universal_Fixed then
6556 Fixup_Universal_Fixed_Operation (N);
6557 Typ := Etype (N);
6558 end if;
6559
6560 -- Divisions with fixed-point results
6561
6562 if Is_Fixed_Point_Type (Typ) then
6563
6564 -- No special processing if Treat_Fixed_As_Integer is set, since
6565 -- from a semantic point of view such operations are simply integer
6566 -- operations and will be treated that way.
6567
6568 if not Treat_Fixed_As_Integer (N) then
6569 if Is_Integer_Type (Rtyp) then
6570 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6571 else
6572 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6573 end if;
6574 end if;
6575
6576 -- Other cases of division of fixed-point operands. Again we exclude the
6577 -- case where Treat_Fixed_As_Integer is set.
6578
6579 elsif (Is_Fixed_Point_Type (Ltyp) or else
6580 Is_Fixed_Point_Type (Rtyp))
6581 and then not Treat_Fixed_As_Integer (N)
6582 then
6583 if Is_Integer_Type (Typ) then
6584 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6585 else
6586 pragma Assert (Is_Floating_Point_Type (Typ));
6587 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6588 end if;
6589
6590 -- Mixed-mode operations can appear in a non-static universal context,
6591 -- in which case the integer argument must be converted explicitly.
6592
6593 elsif Typ = Universal_Real
6594 and then Is_Integer_Type (Rtyp)
6595 then
6596 Rewrite (Ropnd,
6597 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6598
6599 Analyze_And_Resolve (Ropnd, Universal_Real);
6600
6601 elsif Typ = Universal_Real
6602 and then Is_Integer_Type (Ltyp)
6603 then
6604 Rewrite (Lopnd,
6605 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6606
6607 Analyze_And_Resolve (Lopnd, Universal_Real);
6608
6609 -- Non-fixed point cases, do integer zero divide and overflow checks
6610
6611 elsif Is_Integer_Type (Typ) then
6612 Apply_Divide_Checks (N);
6613
6614 -- Deal with Vax_Float
6615
6616 elsif Vax_Float (Typ) then
6617 Expand_Vax_Arith (N);
6618 return;
6619 end if;
6620 end Expand_N_Op_Divide;
6621
6622 --------------------
6623 -- Expand_N_Op_Eq --
6624 --------------------
6625
6626 procedure Expand_N_Op_Eq (N : Node_Id) is
6627 Loc : constant Source_Ptr := Sloc (N);
6628 Typ : constant Entity_Id := Etype (N);
6629 Lhs : constant Node_Id := Left_Opnd (N);
6630 Rhs : constant Node_Id := Right_Opnd (N);
6631 Bodies : constant List_Id := New_List;
6632 A_Typ : constant Entity_Id := Etype (Lhs);
6633
6634 Typl : Entity_Id := A_Typ;
6635 Op_Name : Entity_Id;
6636 Prim : Elmt_Id;
6637
6638 procedure Build_Equality_Call (Eq : Entity_Id);
6639 -- If a constructed equality exists for the type or for its parent,
6640 -- build and analyze call, adding conversions if the operation is
6641 -- inherited.
6642
6643 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6644 -- Determines whether a type has a subcomponent of an unconstrained
6645 -- Unchecked_Union subtype. Typ is a record type.
6646
6647 -------------------------
6648 -- Build_Equality_Call --
6649 -------------------------
6650
6651 procedure Build_Equality_Call (Eq : Entity_Id) is
6652 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6653 L_Exp : Node_Id := Relocate_Node (Lhs);
6654 R_Exp : Node_Id := Relocate_Node (Rhs);
6655
6656 begin
6657 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6658 and then not Is_Class_Wide_Type (A_Typ)
6659 then
6660 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6661 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6662 end if;
6663
6664 -- If we have an Unchecked_Union, we need to add the inferred
6665 -- discriminant values as actuals in the function call. At this
6666 -- point, the expansion has determined that both operands have
6667 -- inferable discriminants.
6668
6669 if Is_Unchecked_Union (Op_Type) then
6670 declare
6671 Lhs_Type : constant Node_Id := Etype (L_Exp);
6672 Rhs_Type : constant Node_Id := Etype (R_Exp);
6673 Lhs_Discr_Val : Node_Id;
6674 Rhs_Discr_Val : Node_Id;
6675
6676 begin
6677 -- Per-object constrained selected components require special
6678 -- attention. If the enclosing scope of the component is an
6679 -- Unchecked_Union, we cannot reference its discriminants
6680 -- directly. This is why we use the two extra parameters of
6681 -- the equality function of the enclosing Unchecked_Union.
6682
6683 -- type UU_Type (Discr : Integer := 0) is
6684 -- . . .
6685 -- end record;
6686 -- pragma Unchecked_Union (UU_Type);
6687
6688 -- 1. Unchecked_Union enclosing record:
6689
6690 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6691 -- . . .
6692 -- Comp : UU_Type (Discr);
6693 -- . . .
6694 -- end Enclosing_UU_Type;
6695 -- pragma Unchecked_Union (Enclosing_UU_Type);
6696
6697 -- Obj1 : Enclosing_UU_Type;
6698 -- Obj2 : Enclosing_UU_Type (1);
6699
6700 -- [. . .] Obj1 = Obj2 [. . .]
6701
6702 -- Generated code:
6703
6704 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6705
6706 -- A and B are the formal parameters of the equality function
6707 -- of Enclosing_UU_Type. The function always has two extra
6708 -- formals to capture the inferred discriminant values.
6709
6710 -- 2. Non-Unchecked_Union enclosing record:
6711
6712 -- type
6713 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6714 -- is record
6715 -- . . .
6716 -- Comp : UU_Type (Discr);
6717 -- . . .
6718 -- end Enclosing_Non_UU_Type;
6719
6720 -- Obj1 : Enclosing_Non_UU_Type;
6721 -- Obj2 : Enclosing_Non_UU_Type (1);
6722
6723 -- ... Obj1 = Obj2 ...
6724
6725 -- Generated code:
6726
6727 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6728 -- obj1.discr, obj2.discr)) then
6729
6730 -- In this case we can directly reference the discriminants of
6731 -- the enclosing record.
6732
6733 -- Lhs of equality
6734
6735 if Nkind (Lhs) = N_Selected_Component
6736 and then Has_Per_Object_Constraint
6737 (Entity (Selector_Name (Lhs)))
6738 then
6739 -- Enclosing record is an Unchecked_Union, use formal A
6740
6741 if Is_Unchecked_Union
6742 (Scope (Entity (Selector_Name (Lhs))))
6743 then
6744 Lhs_Discr_Val := Make_Identifier (Loc, Name_A);
6745
6746 -- Enclosing record is of a non-Unchecked_Union type, it is
6747 -- possible to reference the discriminant.
6748
6749 else
6750 Lhs_Discr_Val :=
6751 Make_Selected_Component (Loc,
6752 Prefix => Prefix (Lhs),
6753 Selector_Name =>
6754 New_Copy
6755 (Get_Discriminant_Value
6756 (First_Discriminant (Lhs_Type),
6757 Lhs_Type,
6758 Stored_Constraint (Lhs_Type))));
6759 end if;
6760
6761 -- Comment needed here ???
6762
6763 else
6764 -- Infer the discriminant value
6765
6766 Lhs_Discr_Val :=
6767 New_Copy
6768 (Get_Discriminant_Value
6769 (First_Discriminant (Lhs_Type),
6770 Lhs_Type,
6771 Stored_Constraint (Lhs_Type)));
6772 end if;
6773
6774 -- Rhs of equality
6775
6776 if Nkind (Rhs) = N_Selected_Component
6777 and then Has_Per_Object_Constraint
6778 (Entity (Selector_Name (Rhs)))
6779 then
6780 if Is_Unchecked_Union
6781 (Scope (Entity (Selector_Name (Rhs))))
6782 then
6783 Rhs_Discr_Val := Make_Identifier (Loc, Name_B);
6784
6785 else
6786 Rhs_Discr_Val :=
6787 Make_Selected_Component (Loc,
6788 Prefix => Prefix (Rhs),
6789 Selector_Name =>
6790 New_Copy (Get_Discriminant_Value (
6791 First_Discriminant (Rhs_Type),
6792 Rhs_Type,
6793 Stored_Constraint (Rhs_Type))));
6794
6795 end if;
6796 else
6797 Rhs_Discr_Val :=
6798 New_Copy (Get_Discriminant_Value (
6799 First_Discriminant (Rhs_Type),
6800 Rhs_Type,
6801 Stored_Constraint (Rhs_Type)));
6802
6803 end if;
6804
6805 Rewrite (N,
6806 Make_Function_Call (Loc,
6807 Name => New_Reference_To (Eq, Loc),
6808 Parameter_Associations => New_List (
6809 L_Exp,
6810 R_Exp,
6811 Lhs_Discr_Val,
6812 Rhs_Discr_Val)));
6813 end;
6814
6815 -- Normal case, not an unchecked union
6816
6817 else
6818 Rewrite (N,
6819 Make_Function_Call (Loc,
6820 Name => New_Reference_To (Eq, Loc),
6821 Parameter_Associations => New_List (L_Exp, R_Exp)));
6822 end if;
6823
6824 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6825 end Build_Equality_Call;
6826
6827 ------------------------------------
6828 -- Has_Unconstrained_UU_Component --
6829 ------------------------------------
6830
6831 function Has_Unconstrained_UU_Component
6832 (Typ : Node_Id) return Boolean
6833 is
6834 Tdef : constant Node_Id :=
6835 Type_Definition (Declaration_Node (Base_Type (Typ)));
6836 Clist : Node_Id;
6837 Vpart : Node_Id;
6838
6839 function Component_Is_Unconstrained_UU
6840 (Comp : Node_Id) return Boolean;
6841 -- Determines whether the subtype of the component is an
6842 -- unconstrained Unchecked_Union.
6843
6844 function Variant_Is_Unconstrained_UU
6845 (Variant : Node_Id) return Boolean;
6846 -- Determines whether a component of the variant has an unconstrained
6847 -- Unchecked_Union subtype.
6848
6849 -----------------------------------
6850 -- Component_Is_Unconstrained_UU --
6851 -----------------------------------
6852
6853 function Component_Is_Unconstrained_UU
6854 (Comp : Node_Id) return Boolean
6855 is
6856 begin
6857 if Nkind (Comp) /= N_Component_Declaration then
6858 return False;
6859 end if;
6860
6861 declare
6862 Sindic : constant Node_Id :=
6863 Subtype_Indication (Component_Definition (Comp));
6864
6865 begin
6866 -- Unconstrained nominal type. In the case of a constraint
6867 -- present, the node kind would have been N_Subtype_Indication.
6868
6869 if Nkind (Sindic) = N_Identifier then
6870 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6871 end if;
6872
6873 return False;
6874 end;
6875 end Component_Is_Unconstrained_UU;
6876
6877 ---------------------------------
6878 -- Variant_Is_Unconstrained_UU --
6879 ---------------------------------
6880
6881 function Variant_Is_Unconstrained_UU
6882 (Variant : Node_Id) return Boolean
6883 is
6884 Clist : constant Node_Id := Component_List (Variant);
6885
6886 begin
6887 if Is_Empty_List (Component_Items (Clist)) then
6888 return False;
6889 end if;
6890
6891 -- We only need to test one component
6892
6893 declare
6894 Comp : Node_Id := First (Component_Items (Clist));
6895
6896 begin
6897 while Present (Comp) loop
6898 if Component_Is_Unconstrained_UU (Comp) then
6899 return True;
6900 end if;
6901
6902 Next (Comp);
6903 end loop;
6904 end;
6905
6906 -- None of the components withing the variant were of
6907 -- unconstrained Unchecked_Union type.
6908
6909 return False;
6910 end Variant_Is_Unconstrained_UU;
6911
6912 -- Start of processing for Has_Unconstrained_UU_Component
6913
6914 begin
6915 if Null_Present (Tdef) then
6916 return False;
6917 end if;
6918
6919 Clist := Component_List (Tdef);
6920 Vpart := Variant_Part (Clist);
6921
6922 -- Inspect available components
6923
6924 if Present (Component_Items (Clist)) then
6925 declare
6926 Comp : Node_Id := First (Component_Items (Clist));
6927
6928 begin
6929 while Present (Comp) loop
6930
6931 -- One component is sufficient
6932
6933 if Component_Is_Unconstrained_UU (Comp) then
6934 return True;
6935 end if;
6936
6937 Next (Comp);
6938 end loop;
6939 end;
6940 end if;
6941
6942 -- Inspect available components withing variants
6943
6944 if Present (Vpart) then
6945 declare
6946 Variant : Node_Id := First (Variants (Vpart));
6947
6948 begin
6949 while Present (Variant) loop
6950
6951 -- One component within a variant is sufficient
6952
6953 if Variant_Is_Unconstrained_UU (Variant) then
6954 return True;
6955 end if;
6956
6957 Next (Variant);
6958 end loop;
6959 end;
6960 end if;
6961
6962 -- Neither the available components, nor the components inside the
6963 -- variant parts were of an unconstrained Unchecked_Union subtype.
6964
6965 return False;
6966 end Has_Unconstrained_UU_Component;
6967
6968 -- Start of processing for Expand_N_Op_Eq
6969
6970 begin
6971 Binary_Op_Validity_Checks (N);
6972
6973 -- Deal with private types
6974
6975 if Ekind (Typl) = E_Private_Type then
6976 Typl := Underlying_Type (Typl);
6977 elsif Ekind (Typl) = E_Private_Subtype then
6978 Typl := Underlying_Type (Base_Type (Typl));
6979 else
6980 null;
6981 end if;
6982
6983 -- It may happen in error situations that the underlying type is not
6984 -- set. The error will be detected later, here we just defend the
6985 -- expander code.
6986
6987 if No (Typl) then
6988 return;
6989 end if;
6990
6991 Typl := Base_Type (Typl);
6992
6993 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
6994 -- results in not having a comparison operation any more, we are done.
6995
6996 Expand_Compare_Minimize_Eliminate_Overflow (N);
6997
6998 if Nkind (N) /= N_Op_Eq then
6999 return;
7000 end if;
7001
7002 -- Boolean types (requiring handling of non-standard case)
7003
7004 if Is_Boolean_Type (Typl) then
7005 Adjust_Condition (Left_Opnd (N));
7006 Adjust_Condition (Right_Opnd (N));
7007 Set_Etype (N, Standard_Boolean);
7008 Adjust_Result_Type (N, Typ);
7009
7010 -- Array types
7011
7012 elsif Is_Array_Type (Typl) then
7013
7014 -- If we are doing full validity checking, and it is possible for the
7015 -- array elements to be invalid then expand out array comparisons to
7016 -- make sure that we check the array elements.
7017
7018 if Validity_Check_Operands
7019 and then not Is_Known_Valid (Component_Type (Typl))
7020 then
7021 declare
7022 Save_Force_Validity_Checks : constant Boolean :=
7023 Force_Validity_Checks;
7024 begin
7025 Force_Validity_Checks := True;
7026 Rewrite (N,
7027 Expand_Array_Equality
7028 (N,
7029 Relocate_Node (Lhs),
7030 Relocate_Node (Rhs),
7031 Bodies,
7032 Typl));
7033 Insert_Actions (N, Bodies);
7034 Analyze_And_Resolve (N, Standard_Boolean);
7035 Force_Validity_Checks := Save_Force_Validity_Checks;
7036 end;
7037
7038 -- Packed case where both operands are known aligned
7039
7040 elsif Is_Bit_Packed_Array (Typl)
7041 and then not Is_Possibly_Unaligned_Object (Lhs)
7042 and then not Is_Possibly_Unaligned_Object (Rhs)
7043 then
7044 Expand_Packed_Eq (N);
7045
7046 -- Where the component type is elementary we can use a block bit
7047 -- comparison (if supported on the target) exception in the case
7048 -- of floating-point (negative zero issues require element by
7049 -- element comparison), and atomic types (where we must be sure
7050 -- to load elements independently) and possibly unaligned arrays.
7051
7052 elsif Is_Elementary_Type (Component_Type (Typl))
7053 and then not Is_Floating_Point_Type (Component_Type (Typl))
7054 and then not Is_Atomic (Component_Type (Typl))
7055 and then not Is_Possibly_Unaligned_Object (Lhs)
7056 and then not Is_Possibly_Unaligned_Object (Rhs)
7057 and then Support_Composite_Compare_On_Target
7058 then
7059 null;
7060
7061 -- For composite and floating-point cases, expand equality loop to
7062 -- make sure of using proper comparisons for tagged types, and
7063 -- correctly handling the floating-point case.
7064
7065 else
7066 Rewrite (N,
7067 Expand_Array_Equality
7068 (N,
7069 Relocate_Node (Lhs),
7070 Relocate_Node (Rhs),
7071 Bodies,
7072 Typl));
7073 Insert_Actions (N, Bodies, Suppress => All_Checks);
7074 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7075 end if;
7076
7077 -- Record Types
7078
7079 elsif Is_Record_Type (Typl) then
7080
7081 -- For tagged types, use the primitive "="
7082
7083 if Is_Tagged_Type (Typl) then
7084
7085 -- No need to do anything else compiling under restriction
7086 -- No_Dispatching_Calls. During the semantic analysis we
7087 -- already notified such violation.
7088
7089 if Restriction_Active (No_Dispatching_Calls) then
7090 return;
7091 end if;
7092
7093 -- If this is derived from an untagged private type completed with
7094 -- a tagged type, it does not have a full view, so we use the
7095 -- primitive operations of the private type. This check should no
7096 -- longer be necessary when these types get their full views???
7097
7098 if Is_Private_Type (A_Typ)
7099 and then not Is_Tagged_Type (A_Typ)
7100 and then Is_Derived_Type (A_Typ)
7101 and then No (Full_View (A_Typ))
7102 then
7103 -- Search for equality operation, checking that the operands
7104 -- have the same type. Note that we must find a matching entry,
7105 -- or something is very wrong!
7106
7107 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7108
7109 while Present (Prim) loop
7110 exit when Chars (Node (Prim)) = Name_Op_Eq
7111 and then Etype (First_Formal (Node (Prim))) =
7112 Etype (Next_Formal (First_Formal (Node (Prim))))
7113 and then
7114 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7115
7116 Next_Elmt (Prim);
7117 end loop;
7118
7119 pragma Assert (Present (Prim));
7120 Op_Name := Node (Prim);
7121
7122 -- Find the type's predefined equality or an overriding
7123 -- user- defined equality. The reason for not simply calling
7124 -- Find_Prim_Op here is that there may be a user-defined
7125 -- overloaded equality op that precedes the equality that we want,
7126 -- so we have to explicitly search (e.g., there could be an
7127 -- equality with two different parameter types).
7128
7129 else
7130 if Is_Class_Wide_Type (Typl) then
7131 Typl := Root_Type (Typl);
7132 end if;
7133
7134 Prim := First_Elmt (Primitive_Operations (Typl));
7135 while Present (Prim) loop
7136 exit when Chars (Node (Prim)) = Name_Op_Eq
7137 and then Etype (First_Formal (Node (Prim))) =
7138 Etype (Next_Formal (First_Formal (Node (Prim))))
7139 and then
7140 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7141
7142 Next_Elmt (Prim);
7143 end loop;
7144
7145 pragma Assert (Present (Prim));
7146 Op_Name := Node (Prim);
7147 end if;
7148
7149 Build_Equality_Call (Op_Name);
7150
7151 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7152 -- predefined equality operator for a type which has a subcomponent
7153 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7154
7155 elsif Has_Unconstrained_UU_Component (Typl) then
7156 Insert_Action (N,
7157 Make_Raise_Program_Error (Loc,
7158 Reason => PE_Unchecked_Union_Restriction));
7159
7160 -- Prevent Gigi from generating incorrect code by rewriting the
7161 -- equality as a standard False. (is this documented somewhere???)
7162
7163 Rewrite (N,
7164 New_Occurrence_Of (Standard_False, Loc));
7165
7166 elsif Is_Unchecked_Union (Typl) then
7167
7168 -- If we can infer the discriminants of the operands, we make a
7169 -- call to the TSS equality function.
7170
7171 if Has_Inferable_Discriminants (Lhs)
7172 and then
7173 Has_Inferable_Discriminants (Rhs)
7174 then
7175 Build_Equality_Call
7176 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7177
7178 else
7179 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7180 -- the predefined equality operator for an Unchecked_Union type
7181 -- if either of the operands lack inferable discriminants.
7182
7183 Insert_Action (N,
7184 Make_Raise_Program_Error (Loc,
7185 Reason => PE_Unchecked_Union_Restriction));
7186
7187 -- Prevent Gigi from generating incorrect code by rewriting
7188 -- the equality as a standard False (documented where???).
7189
7190 Rewrite (N,
7191 New_Occurrence_Of (Standard_False, Loc));
7192
7193 end if;
7194
7195 -- If a type support function is present (for complex cases), use it
7196
7197 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7198 Build_Equality_Call
7199 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7200
7201 -- Otherwise expand the component by component equality. Note that
7202 -- we never use block-bit comparisons for records, because of the
7203 -- problems with gaps. The backend will often be able to recombine
7204 -- the separate comparisons that we generate here.
7205
7206 else
7207 Remove_Side_Effects (Lhs);
7208 Remove_Side_Effects (Rhs);
7209 Rewrite (N,
7210 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7211
7212 Insert_Actions (N, Bodies, Suppress => All_Checks);
7213 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7214 end if;
7215 end if;
7216
7217 -- Test if result is known at compile time
7218
7219 Rewrite_Comparison (N);
7220
7221 -- If we still have comparison for Vax_Float, process it
7222
7223 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
7224 Expand_Vax_Comparison (N);
7225 return;
7226 end if;
7227
7228 Optimize_Length_Comparison (N);
7229 end Expand_N_Op_Eq;
7230
7231 -----------------------
7232 -- Expand_N_Op_Expon --
7233 -----------------------
7234
7235 procedure Expand_N_Op_Expon (N : Node_Id) is
7236 Loc : constant Source_Ptr := Sloc (N);
7237 Typ : constant Entity_Id := Etype (N);
7238 Rtyp : constant Entity_Id := Root_Type (Typ);
7239 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7240 Bastyp : constant Node_Id := Etype (Base);
7241 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7242 Exptyp : constant Entity_Id := Etype (Exp);
7243 Ovflo : constant Boolean := Do_Overflow_Check (N);
7244 Expv : Uint;
7245 Temp : Node_Id;
7246 Rent : RE_Id;
7247 Ent : Entity_Id;
7248 Etyp : Entity_Id;
7249 Xnode : Node_Id;
7250
7251 begin
7252 Binary_Op_Validity_Checks (N);
7253
7254 -- CodePeer and GNATprove want to see the unexpanded N_Op_Expon node
7255
7256 if CodePeer_Mode or Alfa_Mode then
7257 return;
7258 end if;
7259
7260 -- If either operand is of a private type, then we have the use of an
7261 -- intrinsic operator, and we get rid of the privateness, by using root
7262 -- types of underlying types for the actual operation. Otherwise the
7263 -- private types will cause trouble if we expand multiplications or
7264 -- shifts etc. We also do this transformation if the result type is
7265 -- different from the base type.
7266
7267 if Is_Private_Type (Etype (Base))
7268 or else Is_Private_Type (Typ)
7269 or else Is_Private_Type (Exptyp)
7270 or else Rtyp /= Root_Type (Bastyp)
7271 then
7272 declare
7273 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7274 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7275
7276 begin
7277 Rewrite (N,
7278 Unchecked_Convert_To (Typ,
7279 Make_Op_Expon (Loc,
7280 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7281 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7282 Analyze_And_Resolve (N, Typ);
7283 return;
7284 end;
7285 end if;
7286
7287 -- Normally we complete expansion of exponentiation (e.g. converting
7288 -- to multplications) right here, but there is one exception to this.
7289 -- If we have a signed integer type and the overflow checking mode
7290 -- is MINIMIZED or ELIMINATED and overflow checking is activated, then
7291 -- we don't yet want to expand, since that will intefere with handling
7292 -- of extended precision intermediate value. In this situation we just
7293 -- apply the arithmetic overflow check, and then the overflow check
7294 -- circuit will re-expand the exponentiation node in CHECKED mode.
7295
7296 if Is_Signed_Integer_Type (Rtyp)
7297 and then Overflow_Check_Mode (Typ) in Minimized_Or_Eliminated
7298 and then Do_Overflow_Check (N)
7299 then
7300 Apply_Arithmetic_Overflow_Check (N);
7301 return;
7302 end if;
7303
7304 -- Test for case of known right argument where we can replace the
7305 -- exponentiation by an equivalent expression using multiplication.
7306
7307 if Compile_Time_Known_Value (Exp) then
7308 Expv := Expr_Value (Exp);
7309
7310 -- We only fold small non-negative exponents. You might think we
7311 -- could fold small negative exponents for the real case, but we
7312 -- can't because we are required to raise Constraint_Error for
7313 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7314 -- See ACVC test C4A012B.
7315
7316 if Expv >= 0 and then Expv <= 4 then
7317
7318 -- X ** 0 = 1 (or 1.0)
7319
7320 if Expv = 0 then
7321
7322 -- Call Remove_Side_Effects to ensure that any side effects
7323 -- in the ignored left operand (in particular function calls
7324 -- to user defined functions) are properly executed.
7325
7326 Remove_Side_Effects (Base);
7327
7328 if Ekind (Typ) in Integer_Kind then
7329 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7330 else
7331 Xnode := Make_Real_Literal (Loc, Ureal_1);
7332 end if;
7333
7334 -- X ** 1 = X
7335
7336 elsif Expv = 1 then
7337 Xnode := Base;
7338
7339 -- X ** 2 = X * X
7340
7341 elsif Expv = 2 then
7342 Xnode :=
7343 Make_Op_Multiply (Loc,
7344 Left_Opnd => Duplicate_Subexpr (Base),
7345 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7346
7347 -- X ** 3 = X * X * X
7348
7349 elsif Expv = 3 then
7350 Xnode :=
7351 Make_Op_Multiply (Loc,
7352 Left_Opnd =>
7353 Make_Op_Multiply (Loc,
7354 Left_Opnd => Duplicate_Subexpr (Base),
7355 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7356 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7357
7358 -- X ** 4 ->
7359
7360 -- do
7361 -- En : constant base'type := base * base;
7362 -- in
7363 -- En * En
7364
7365 else
7366 pragma Assert (Expv = 4);
7367 Temp := Make_Temporary (Loc, 'E', Base);
7368
7369 Xnode :=
7370 Make_Expression_With_Actions (Loc,
7371 Actions => New_List (
7372 Make_Object_Declaration (Loc,
7373 Defining_Identifier => Temp,
7374 Constant_Present => True,
7375 Object_Definition => New_Reference_To (Typ, Loc),
7376 Expression =>
7377 Make_Op_Multiply (Loc,
7378 Left_Opnd =>
7379 Duplicate_Subexpr (Base),
7380 Right_Opnd =>
7381 Duplicate_Subexpr_No_Checks (Base)))),
7382
7383 Expression =>
7384 Make_Op_Multiply (Loc,
7385 Left_Opnd => New_Reference_To (Temp, Loc),
7386 Right_Opnd => New_Reference_To (Temp, Loc)));
7387 end if;
7388
7389 Rewrite (N, Xnode);
7390 Analyze_And_Resolve (N, Typ);
7391 return;
7392 end if;
7393 end if;
7394
7395 -- Case of (2 ** expression) appearing as an argument of an integer
7396 -- multiplication, or as the right argument of a division of a non-
7397 -- negative integer. In such cases we leave the node untouched, setting
7398 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7399 -- of the higher level node converts it into a shift.
7400
7401 -- Another case is 2 ** N in any other context. We simply convert
7402 -- this to 1 * 2 ** N, and then the above transformation applies.
7403
7404 -- Note: this transformation is not applicable for a modular type with
7405 -- a non-binary modulus in the multiplication case, since we get a wrong
7406 -- result if the shift causes an overflow before the modular reduction.
7407
7408 if Nkind (Base) = N_Integer_Literal
7409 and then Intval (Base) = 2
7410 and then Is_Integer_Type (Root_Type (Exptyp))
7411 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7412 and then Is_Unsigned_Type (Exptyp)
7413 and then not Ovflo
7414 then
7415 -- First the multiply and divide cases
7416
7417 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7418 declare
7419 P : constant Node_Id := Parent (N);
7420 L : constant Node_Id := Left_Opnd (P);
7421 R : constant Node_Id := Right_Opnd (P);
7422
7423 begin
7424 if (Nkind (P) = N_Op_Multiply
7425 and then not Non_Binary_Modulus (Typ)
7426 and then
7427 ((Is_Integer_Type (Etype (L)) and then R = N)
7428 or else
7429 (Is_Integer_Type (Etype (R)) and then L = N))
7430 and then not Do_Overflow_Check (P))
7431 or else
7432 (Nkind (P) = N_Op_Divide
7433 and then Is_Integer_Type (Etype (L))
7434 and then Is_Unsigned_Type (Etype (L))
7435 and then R = N
7436 and then not Do_Overflow_Check (P))
7437 then
7438 Set_Is_Power_Of_2_For_Shift (N);
7439 return;
7440 end if;
7441 end;
7442
7443 -- Now the other cases
7444
7445 elsif not Non_Binary_Modulus (Typ) then
7446 Rewrite (N,
7447 Make_Op_Multiply (Loc,
7448 Left_Opnd => Make_Integer_Literal (Loc, 1),
7449 Right_Opnd => Relocate_Node (N)));
7450 Analyze_And_Resolve (N, Typ);
7451 return;
7452 end if;
7453 end if;
7454
7455 -- Fall through if exponentiation must be done using a runtime routine
7456
7457 -- First deal with modular case
7458
7459 if Is_Modular_Integer_Type (Rtyp) then
7460
7461 -- Non-binary case, we call the special exponentiation routine for
7462 -- the non-binary case, converting the argument to Long_Long_Integer
7463 -- and passing the modulus value. Then the result is converted back
7464 -- to the base type.
7465
7466 if Non_Binary_Modulus (Rtyp) then
7467 Rewrite (N,
7468 Convert_To (Typ,
7469 Make_Function_Call (Loc,
7470 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
7471 Parameter_Associations => New_List (
7472 Convert_To (Standard_Integer, Base),
7473 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7474 Exp))));
7475
7476 -- Binary case, in this case, we call one of two routines, either the
7477 -- unsigned integer case, or the unsigned long long integer case,
7478 -- with a final "and" operation to do the required mod.
7479
7480 else
7481 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7482 Ent := RTE (RE_Exp_Unsigned);
7483 else
7484 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7485 end if;
7486
7487 Rewrite (N,
7488 Convert_To (Typ,
7489 Make_Op_And (Loc,
7490 Left_Opnd =>
7491 Make_Function_Call (Loc,
7492 Name => New_Reference_To (Ent, Loc),
7493 Parameter_Associations => New_List (
7494 Convert_To (Etype (First_Formal (Ent)), Base),
7495 Exp)),
7496 Right_Opnd =>
7497 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7498
7499 end if;
7500
7501 -- Common exit point for modular type case
7502
7503 Analyze_And_Resolve (N, Typ);
7504 return;
7505
7506 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7507 -- It is not worth having routines for Short_[Short_]Integer, since for
7508 -- most machines it would not help, and it would generate more code that
7509 -- might need certification when a certified run time is required.
7510
7511 -- In the integer cases, we have two routines, one for when overflow
7512 -- checks are required, and one when they are not required, since there
7513 -- is a real gain in omitting checks on many machines.
7514
7515 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7516 or else (Rtyp = Base_Type (Standard_Long_Integer)
7517 and then
7518 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7519 or else (Rtyp = Universal_Integer)
7520 then
7521 Etyp := Standard_Long_Long_Integer;
7522
7523 if Ovflo then
7524 Rent := RE_Exp_Long_Long_Integer;
7525 else
7526 Rent := RE_Exn_Long_Long_Integer;
7527 end if;
7528
7529 elsif Is_Signed_Integer_Type (Rtyp) then
7530 Etyp := Standard_Integer;
7531
7532 if Ovflo then
7533 Rent := RE_Exp_Integer;
7534 else
7535 Rent := RE_Exn_Integer;
7536 end if;
7537
7538 -- Floating-point cases, always done using Long_Long_Float. We do not
7539 -- need separate routines for the overflow case here, since in the case
7540 -- of floating-point, we generate infinities anyway as a rule (either
7541 -- that or we automatically trap overflow), and if there is an infinity
7542 -- generated and a range check is required, the check will fail anyway.
7543
7544 else
7545 pragma Assert (Is_Floating_Point_Type (Rtyp));
7546 Etyp := Standard_Long_Long_Float;
7547 Rent := RE_Exn_Long_Long_Float;
7548 end if;
7549
7550 -- Common processing for integer cases and floating-point cases.
7551 -- If we are in the right type, we can call runtime routine directly
7552
7553 if Typ = Etyp
7554 and then Rtyp /= Universal_Integer
7555 and then Rtyp /= Universal_Real
7556 then
7557 Rewrite (N,
7558 Make_Function_Call (Loc,
7559 Name => New_Reference_To (RTE (Rent), Loc),
7560 Parameter_Associations => New_List (Base, Exp)));
7561
7562 -- Otherwise we have to introduce conversions (conversions are also
7563 -- required in the universal cases, since the runtime routine is
7564 -- typed using one of the standard types).
7565
7566 else
7567 Rewrite (N,
7568 Convert_To (Typ,
7569 Make_Function_Call (Loc,
7570 Name => New_Reference_To (RTE (Rent), Loc),
7571 Parameter_Associations => New_List (
7572 Convert_To (Etyp, Base),
7573 Exp))));
7574 end if;
7575
7576 Analyze_And_Resolve (N, Typ);
7577 return;
7578
7579 exception
7580 when RE_Not_Available =>
7581 return;
7582 end Expand_N_Op_Expon;
7583
7584 --------------------
7585 -- Expand_N_Op_Ge --
7586 --------------------
7587
7588 procedure Expand_N_Op_Ge (N : Node_Id) is
7589 Typ : constant Entity_Id := Etype (N);
7590 Op1 : constant Node_Id := Left_Opnd (N);
7591 Op2 : constant Node_Id := Right_Opnd (N);
7592 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7593
7594 begin
7595 Binary_Op_Validity_Checks (N);
7596
7597 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7598 -- results in not having a comparison operation any more, we are done.
7599
7600 Expand_Compare_Minimize_Eliminate_Overflow (N);
7601
7602 if Nkind (N) /= N_Op_Ge then
7603 return;
7604 end if;
7605
7606 -- Array type case
7607
7608 if Is_Array_Type (Typ1) then
7609 Expand_Array_Comparison (N);
7610 return;
7611 end if;
7612
7613 -- Deal with boolean operands
7614
7615 if Is_Boolean_Type (Typ1) then
7616 Adjust_Condition (Op1);
7617 Adjust_Condition (Op2);
7618 Set_Etype (N, Standard_Boolean);
7619 Adjust_Result_Type (N, Typ);
7620 end if;
7621
7622 Rewrite_Comparison (N);
7623
7624 -- If we still have comparison, and Vax_Float type, process it
7625
7626 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7627 Expand_Vax_Comparison (N);
7628 return;
7629 end if;
7630
7631 Optimize_Length_Comparison (N);
7632 end Expand_N_Op_Ge;
7633
7634 --------------------
7635 -- Expand_N_Op_Gt --
7636 --------------------
7637
7638 procedure Expand_N_Op_Gt (N : Node_Id) is
7639 Typ : constant Entity_Id := Etype (N);
7640 Op1 : constant Node_Id := Left_Opnd (N);
7641 Op2 : constant Node_Id := Right_Opnd (N);
7642 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7643
7644 begin
7645 Binary_Op_Validity_Checks (N);
7646
7647 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7648 -- results in not having a comparison operation any more, we are done.
7649
7650 Expand_Compare_Minimize_Eliminate_Overflow (N);
7651
7652 if Nkind (N) /= N_Op_Gt then
7653 return;
7654 end if;
7655
7656 -- Deal with array type operands
7657
7658 if Is_Array_Type (Typ1) then
7659 Expand_Array_Comparison (N);
7660 return;
7661 end if;
7662
7663 -- Deal with boolean type operands
7664
7665 if Is_Boolean_Type (Typ1) then
7666 Adjust_Condition (Op1);
7667 Adjust_Condition (Op2);
7668 Set_Etype (N, Standard_Boolean);
7669 Adjust_Result_Type (N, Typ);
7670 end if;
7671
7672 Rewrite_Comparison (N);
7673
7674 -- If we still have comparison, and Vax_Float type, process it
7675
7676 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7677 Expand_Vax_Comparison (N);
7678 return;
7679 end if;
7680
7681 Optimize_Length_Comparison (N);
7682 end Expand_N_Op_Gt;
7683
7684 --------------------
7685 -- Expand_N_Op_Le --
7686 --------------------
7687
7688 procedure Expand_N_Op_Le (N : Node_Id) is
7689 Typ : constant Entity_Id := Etype (N);
7690 Op1 : constant Node_Id := Left_Opnd (N);
7691 Op2 : constant Node_Id := Right_Opnd (N);
7692 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7693
7694 begin
7695 Binary_Op_Validity_Checks (N);
7696
7697 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7698 -- results in not having a comparison operation any more, we are done.
7699
7700 Expand_Compare_Minimize_Eliminate_Overflow (N);
7701
7702 if Nkind (N) /= N_Op_Le then
7703 return;
7704 end if;
7705
7706 -- Deal with array type operands
7707
7708 if Is_Array_Type (Typ1) then
7709 Expand_Array_Comparison (N);
7710 return;
7711 end if;
7712
7713 -- Deal with Boolean type operands
7714
7715 if Is_Boolean_Type (Typ1) then
7716 Adjust_Condition (Op1);
7717 Adjust_Condition (Op2);
7718 Set_Etype (N, Standard_Boolean);
7719 Adjust_Result_Type (N, Typ);
7720 end if;
7721
7722 Rewrite_Comparison (N);
7723
7724 -- If we still have comparison, and Vax_Float type, process it
7725
7726 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7727 Expand_Vax_Comparison (N);
7728 return;
7729 end if;
7730
7731 Optimize_Length_Comparison (N);
7732 end Expand_N_Op_Le;
7733
7734 --------------------
7735 -- Expand_N_Op_Lt --
7736 --------------------
7737
7738 procedure Expand_N_Op_Lt (N : Node_Id) is
7739 Typ : constant Entity_Id := Etype (N);
7740 Op1 : constant Node_Id := Left_Opnd (N);
7741 Op2 : constant Node_Id := Right_Opnd (N);
7742 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7743
7744 begin
7745 Binary_Op_Validity_Checks (N);
7746
7747 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7748 -- results in not having a comparison operation any more, we are done.
7749
7750 Expand_Compare_Minimize_Eliminate_Overflow (N);
7751
7752 if Nkind (N) /= N_Op_Lt then
7753 return;
7754 end if;
7755
7756 -- Deal with array type operands
7757
7758 if Is_Array_Type (Typ1) then
7759 Expand_Array_Comparison (N);
7760 return;
7761 end if;
7762
7763 -- Deal with Boolean type operands
7764
7765 if Is_Boolean_Type (Typ1) then
7766 Adjust_Condition (Op1);
7767 Adjust_Condition (Op2);
7768 Set_Etype (N, Standard_Boolean);
7769 Adjust_Result_Type (N, Typ);
7770 end if;
7771
7772 Rewrite_Comparison (N);
7773
7774 -- If we still have comparison, and Vax_Float type, process it
7775
7776 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7777 Expand_Vax_Comparison (N);
7778 return;
7779 end if;
7780
7781 Optimize_Length_Comparison (N);
7782 end Expand_N_Op_Lt;
7783
7784 -----------------------
7785 -- Expand_N_Op_Minus --
7786 -----------------------
7787
7788 procedure Expand_N_Op_Minus (N : Node_Id) is
7789 Loc : constant Source_Ptr := Sloc (N);
7790 Typ : constant Entity_Id := Etype (N);
7791
7792 begin
7793 Unary_Op_Validity_Checks (N);
7794
7795 if not Backend_Overflow_Checks_On_Target
7796 and then Is_Signed_Integer_Type (Etype (N))
7797 and then Do_Overflow_Check (N)
7798 then
7799 -- Software overflow checking expands -expr into (0 - expr)
7800
7801 Rewrite (N,
7802 Make_Op_Subtract (Loc,
7803 Left_Opnd => Make_Integer_Literal (Loc, 0),
7804 Right_Opnd => Right_Opnd (N)));
7805
7806 Analyze_And_Resolve (N, Typ);
7807
7808 -- Vax floating-point types case
7809
7810 elsif Vax_Float (Etype (N)) then
7811 Expand_Vax_Arith (N);
7812 end if;
7813 end Expand_N_Op_Minus;
7814
7815 ---------------------
7816 -- Expand_N_Op_Mod --
7817 ---------------------
7818
7819 procedure Expand_N_Op_Mod (N : Node_Id) is
7820 Loc : constant Source_Ptr := Sloc (N);
7821 Typ : constant Entity_Id := Etype (N);
7822 Left : constant Node_Id := Left_Opnd (N);
7823 Right : constant Node_Id := Right_Opnd (N);
7824 DOC : constant Boolean := Do_Overflow_Check (N);
7825 DDC : constant Boolean := Do_Division_Check (N);
7826
7827 LLB : Uint;
7828 Llo : Uint;
7829 Lhi : Uint;
7830 LOK : Boolean;
7831 Rlo : Uint;
7832 Rhi : Uint;
7833 ROK : Boolean;
7834
7835 pragma Warnings (Off, Lhi);
7836
7837 begin
7838 Binary_Op_Validity_Checks (N);
7839
7840 if Is_Integer_Type (Etype (N)) then
7841 Apply_Divide_Checks (N);
7842 end if;
7843
7844 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
7845 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
7846
7847 -- Convert mod to rem if operands are known non-negative. We do this
7848 -- since it is quite likely that this will improve the quality of code,
7849 -- (the operation now corresponds to the hardware remainder), and it
7850 -- does not seem likely that it could be harmful.
7851
7852 if LOK and then Llo >= 0
7853 and then
7854 ROK and then Rlo >= 0
7855 then
7856 Rewrite (N,
7857 Make_Op_Rem (Sloc (N),
7858 Left_Opnd => Left_Opnd (N),
7859 Right_Opnd => Right_Opnd (N)));
7860
7861 -- Instead of reanalyzing the node we do the analysis manually. This
7862 -- avoids anomalies when the replacement is done in an instance and
7863 -- is epsilon more efficient.
7864
7865 Set_Entity (N, Standard_Entity (S_Op_Rem));
7866 Set_Etype (N, Typ);
7867 Set_Do_Overflow_Check (N, DOC);
7868 Set_Do_Division_Check (N, DDC);
7869 Expand_N_Op_Rem (N);
7870 Set_Analyzed (N);
7871
7872 -- Otherwise, normal mod processing
7873
7874 else
7875 -- Apply optimization x mod 1 = 0. We don't really need that with
7876 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
7877 -- certainly harmless.
7878
7879 if Is_Integer_Type (Etype (N))
7880 and then Compile_Time_Known_Value (Right)
7881 and then Expr_Value (Right) = Uint_1
7882 then
7883 -- Call Remove_Side_Effects to ensure that any side effects in
7884 -- the ignored left operand (in particular function calls to
7885 -- user defined functions) are properly executed.
7886
7887 Remove_Side_Effects (Left);
7888
7889 Rewrite (N, Make_Integer_Literal (Loc, 0));
7890 Analyze_And_Resolve (N, Typ);
7891 return;
7892 end if;
7893
7894 -- Deal with annoying case of largest negative number remainder
7895 -- minus one. Gigi does not handle this case correctly, because
7896 -- it generates a divide instruction which may trap in this case.
7897
7898 -- In fact the check is quite easy, if the right operand is -1, then
7899 -- the mod value is always 0, and we can just ignore the left operand
7900 -- completely in this case.
7901
7902 -- This only applies if we still have a mod operator. Skip if we
7903 -- have already rewritten this (e.g. in the case of eliminated
7904 -- overflow checks which have driven us into bignum mode).
7905
7906 if Nkind (N) = N_Op_Mod then
7907
7908 -- The operand type may be private (e.g. in the expansion of an
7909 -- intrinsic operation) so we must use the underlying type to get
7910 -- the bounds, and convert the literals explicitly.
7911
7912 LLB :=
7913 Expr_Value
7914 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
7915
7916 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
7917 and then
7918 ((not LOK) or else (Llo = LLB))
7919 then
7920 Rewrite (N,
7921 Make_Conditional_Expression (Loc,
7922 Expressions => New_List (
7923 Make_Op_Eq (Loc,
7924 Left_Opnd => Duplicate_Subexpr (Right),
7925 Right_Opnd =>
7926 Unchecked_Convert_To (Typ,
7927 Make_Integer_Literal (Loc, -1))),
7928 Unchecked_Convert_To (Typ,
7929 Make_Integer_Literal (Loc, Uint_0)),
7930 Relocate_Node (N))));
7931
7932 Set_Analyzed (Next (Next (First (Expressions (N)))));
7933 Analyze_And_Resolve (N, Typ);
7934 end if;
7935 end if;
7936 end if;
7937 end Expand_N_Op_Mod;
7938
7939 --------------------------
7940 -- Expand_N_Op_Multiply --
7941 --------------------------
7942
7943 procedure Expand_N_Op_Multiply (N : Node_Id) is
7944 Loc : constant Source_Ptr := Sloc (N);
7945 Lop : constant Node_Id := Left_Opnd (N);
7946 Rop : constant Node_Id := Right_Opnd (N);
7947
7948 Lp2 : constant Boolean :=
7949 Nkind (Lop) = N_Op_Expon
7950 and then Is_Power_Of_2_For_Shift (Lop);
7951
7952 Rp2 : constant Boolean :=
7953 Nkind (Rop) = N_Op_Expon
7954 and then Is_Power_Of_2_For_Shift (Rop);
7955
7956 Ltyp : constant Entity_Id := Etype (Lop);
7957 Rtyp : constant Entity_Id := Etype (Rop);
7958 Typ : Entity_Id := Etype (N);
7959
7960 begin
7961 Binary_Op_Validity_Checks (N);
7962
7963 -- Special optimizations for integer types
7964
7965 if Is_Integer_Type (Typ) then
7966
7967 -- N * 0 = 0 for integer types
7968
7969 if Compile_Time_Known_Value (Rop)
7970 and then Expr_Value (Rop) = Uint_0
7971 then
7972 -- Call Remove_Side_Effects to ensure that any side effects in
7973 -- the ignored left operand (in particular function calls to
7974 -- user defined functions) are properly executed.
7975
7976 Remove_Side_Effects (Lop);
7977
7978 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
7979 Analyze_And_Resolve (N, Typ);
7980 return;
7981 end if;
7982
7983 -- Similar handling for 0 * N = 0
7984
7985 if Compile_Time_Known_Value (Lop)
7986 and then Expr_Value (Lop) = Uint_0
7987 then
7988 Remove_Side_Effects (Rop);
7989 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
7990 Analyze_And_Resolve (N, Typ);
7991 return;
7992 end if;
7993
7994 -- N * 1 = 1 * N = N for integer types
7995
7996 -- This optimisation is not done if we are going to
7997 -- rewrite the product 1 * 2 ** N to a shift.
7998
7999 if Compile_Time_Known_Value (Rop)
8000 and then Expr_Value (Rop) = Uint_1
8001 and then not Lp2
8002 then
8003 Rewrite (N, Lop);
8004 return;
8005
8006 elsif Compile_Time_Known_Value (Lop)
8007 and then Expr_Value (Lop) = Uint_1
8008 and then not Rp2
8009 then
8010 Rewrite (N, Rop);
8011 return;
8012 end if;
8013 end if;
8014
8015 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8016 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8017 -- operand is an integer, as required for this to work.
8018
8019 if Rp2 then
8020 if Lp2 then
8021
8022 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8023
8024 Rewrite (N,
8025 Make_Op_Expon (Loc,
8026 Left_Opnd => Make_Integer_Literal (Loc, 2),
8027 Right_Opnd =>
8028 Make_Op_Add (Loc,
8029 Left_Opnd => Right_Opnd (Lop),
8030 Right_Opnd => Right_Opnd (Rop))));
8031 Analyze_And_Resolve (N, Typ);
8032 return;
8033
8034 else
8035 Rewrite (N,
8036 Make_Op_Shift_Left (Loc,
8037 Left_Opnd => Lop,
8038 Right_Opnd =>
8039 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8040 Analyze_And_Resolve (N, Typ);
8041 return;
8042 end if;
8043
8044 -- Same processing for the operands the other way round
8045
8046 elsif Lp2 then
8047 Rewrite (N,
8048 Make_Op_Shift_Left (Loc,
8049 Left_Opnd => Rop,
8050 Right_Opnd =>
8051 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8052 Analyze_And_Resolve (N, Typ);
8053 return;
8054 end if;
8055
8056 -- Do required fixup of universal fixed operation
8057
8058 if Typ = Universal_Fixed then
8059 Fixup_Universal_Fixed_Operation (N);
8060 Typ := Etype (N);
8061 end if;
8062
8063 -- Multiplications with fixed-point results
8064
8065 if Is_Fixed_Point_Type (Typ) then
8066
8067 -- No special processing if Treat_Fixed_As_Integer is set, since from
8068 -- a semantic point of view such operations are simply integer
8069 -- operations and will be treated that way.
8070
8071 if not Treat_Fixed_As_Integer (N) then
8072
8073 -- Case of fixed * integer => fixed
8074
8075 if Is_Integer_Type (Rtyp) then
8076 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8077
8078 -- Case of integer * fixed => fixed
8079
8080 elsif Is_Integer_Type (Ltyp) then
8081 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8082
8083 -- Case of fixed * fixed => fixed
8084
8085 else
8086 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8087 end if;
8088 end if;
8089
8090 -- Other cases of multiplication of fixed-point operands. Again we
8091 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8092
8093 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8094 and then not Treat_Fixed_As_Integer (N)
8095 then
8096 if Is_Integer_Type (Typ) then
8097 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8098 else
8099 pragma Assert (Is_Floating_Point_Type (Typ));
8100 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8101 end if;
8102
8103 -- Mixed-mode operations can appear in a non-static universal context,
8104 -- in which case the integer argument must be converted explicitly.
8105
8106 elsif Typ = Universal_Real
8107 and then Is_Integer_Type (Rtyp)
8108 then
8109 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8110
8111 Analyze_And_Resolve (Rop, Universal_Real);
8112
8113 elsif Typ = Universal_Real
8114 and then Is_Integer_Type (Ltyp)
8115 then
8116 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8117
8118 Analyze_And_Resolve (Lop, Universal_Real);
8119
8120 -- Non-fixed point cases, check software overflow checking required
8121
8122 elsif Is_Signed_Integer_Type (Etype (N)) then
8123 Apply_Arithmetic_Overflow_Check (N);
8124
8125 -- Deal with VAX float case
8126
8127 elsif Vax_Float (Typ) then
8128 Expand_Vax_Arith (N);
8129 return;
8130 end if;
8131 end Expand_N_Op_Multiply;
8132
8133 --------------------
8134 -- Expand_N_Op_Ne --
8135 --------------------
8136
8137 procedure Expand_N_Op_Ne (N : Node_Id) is
8138 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8139
8140 begin
8141 -- Case of elementary type with standard operator
8142
8143 if Is_Elementary_Type (Typ)
8144 and then Sloc (Entity (N)) = Standard_Location
8145 then
8146 Binary_Op_Validity_Checks (N);
8147
8148 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8149 -- that results in not having a /= opertion any more, we are done.
8150
8151 Expand_Compare_Minimize_Eliminate_Overflow (N);
8152
8153 if Nkind (N) /= N_Op_Ne then
8154 return;
8155 end if;
8156
8157 -- Boolean types (requiring handling of non-standard case)
8158
8159 if Is_Boolean_Type (Typ) then
8160 Adjust_Condition (Left_Opnd (N));
8161 Adjust_Condition (Right_Opnd (N));
8162 Set_Etype (N, Standard_Boolean);
8163 Adjust_Result_Type (N, Typ);
8164 end if;
8165
8166 Rewrite_Comparison (N);
8167
8168 -- If we still have comparison for Vax_Float, process it
8169
8170 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
8171 Expand_Vax_Comparison (N);
8172 return;
8173 end if;
8174
8175 -- For all cases other than elementary types, we rewrite node as the
8176 -- negation of an equality operation, and reanalyze. The equality to be
8177 -- used is defined in the same scope and has the same signature. This
8178 -- signature must be set explicitly since in an instance it may not have
8179 -- the same visibility as in the generic unit. This avoids duplicating
8180 -- or factoring the complex code for record/array equality tests etc.
8181
8182 else
8183 declare
8184 Loc : constant Source_Ptr := Sloc (N);
8185 Neg : Node_Id;
8186 Ne : constant Entity_Id := Entity (N);
8187
8188 begin
8189 Binary_Op_Validity_Checks (N);
8190
8191 Neg :=
8192 Make_Op_Not (Loc,
8193 Right_Opnd =>
8194 Make_Op_Eq (Loc,
8195 Left_Opnd => Left_Opnd (N),
8196 Right_Opnd => Right_Opnd (N)));
8197 Set_Paren_Count (Right_Opnd (Neg), 1);
8198
8199 if Scope (Ne) /= Standard_Standard then
8200 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8201 end if;
8202
8203 -- For navigation purposes, we want to treat the inequality as an
8204 -- implicit reference to the corresponding equality. Preserve the
8205 -- Comes_From_ source flag to generate proper Xref entries.
8206
8207 Preserve_Comes_From_Source (Neg, N);
8208 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8209 Rewrite (N, Neg);
8210 Analyze_And_Resolve (N, Standard_Boolean);
8211 end;
8212 end if;
8213
8214 Optimize_Length_Comparison (N);
8215 end Expand_N_Op_Ne;
8216
8217 ---------------------
8218 -- Expand_N_Op_Not --
8219 ---------------------
8220
8221 -- If the argument is other than a Boolean array type, there is no special
8222 -- expansion required, except for VMS operations on signed integers.
8223
8224 -- For the packed case, we call the special routine in Exp_Pakd, except
8225 -- that if the component size is greater than one, we use the standard
8226 -- routine generating a gruesome loop (it is so peculiar to have packed
8227 -- arrays with non-standard Boolean representations anyway, so it does not
8228 -- matter that we do not handle this case efficiently).
8229
8230 -- For the unpacked case (and for the special packed case where we have non
8231 -- standard Booleans, as discussed above), we generate and insert into the
8232 -- tree the following function definition:
8233
8234 -- function Nnnn (A : arr) is
8235 -- B : arr;
8236 -- begin
8237 -- for J in a'range loop
8238 -- B (J) := not A (J);
8239 -- end loop;
8240 -- return B;
8241 -- end Nnnn;
8242
8243 -- Here arr is the actual subtype of the parameter (and hence always
8244 -- constrained). Then we replace the not with a call to this function.
8245
8246 procedure Expand_N_Op_Not (N : Node_Id) is
8247 Loc : constant Source_Ptr := Sloc (N);
8248 Typ : constant Entity_Id := Etype (N);
8249 Opnd : Node_Id;
8250 Arr : Entity_Id;
8251 A : Entity_Id;
8252 B : Entity_Id;
8253 J : Entity_Id;
8254 A_J : Node_Id;
8255 B_J : Node_Id;
8256
8257 Func_Name : Entity_Id;
8258 Loop_Statement : Node_Id;
8259
8260 begin
8261 Unary_Op_Validity_Checks (N);
8262
8263 -- For boolean operand, deal with non-standard booleans
8264
8265 if Is_Boolean_Type (Typ) then
8266 Adjust_Condition (Right_Opnd (N));
8267 Set_Etype (N, Standard_Boolean);
8268 Adjust_Result_Type (N, Typ);
8269 return;
8270 end if;
8271
8272 -- For the VMS "not" on signed integer types, use conversion to and from
8273 -- a predefined modular type.
8274
8275 if Is_VMS_Operator (Entity (N)) then
8276 declare
8277 Rtyp : Entity_Id;
8278 Utyp : Entity_Id;
8279
8280 begin
8281 -- If this is a derived type, retrieve original VMS type so that
8282 -- the proper sized type is used for intermediate values.
8283
8284 if Is_Derived_Type (Typ) then
8285 Rtyp := First_Subtype (Etype (Typ));
8286 else
8287 Rtyp := Typ;
8288 end if;
8289
8290 -- The proper unsigned type must have a size compatible with the
8291 -- operand, to prevent misalignment.
8292
8293 if RM_Size (Rtyp) <= 8 then
8294 Utyp := RTE (RE_Unsigned_8);
8295
8296 elsif RM_Size (Rtyp) <= 16 then
8297 Utyp := RTE (RE_Unsigned_16);
8298
8299 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
8300 Utyp := RTE (RE_Unsigned_32);
8301
8302 else
8303 Utyp := RTE (RE_Long_Long_Unsigned);
8304 end if;
8305
8306 Rewrite (N,
8307 Unchecked_Convert_To (Typ,
8308 Make_Op_Not (Loc,
8309 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
8310 Analyze_And_Resolve (N, Typ);
8311 return;
8312 end;
8313 end if;
8314
8315 -- Only array types need any other processing
8316
8317 if not Is_Array_Type (Typ) then
8318 return;
8319 end if;
8320
8321 -- Case of array operand. If bit packed with a component size of 1,
8322 -- handle it in Exp_Pakd if the operand is known to be aligned.
8323
8324 if Is_Bit_Packed_Array (Typ)
8325 and then Component_Size (Typ) = 1
8326 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8327 then
8328 Expand_Packed_Not (N);
8329 return;
8330 end if;
8331
8332 -- Case of array operand which is not bit-packed. If the context is
8333 -- a safe assignment, call in-place operation, If context is a larger
8334 -- boolean expression in the context of a safe assignment, expansion is
8335 -- done by enclosing operation.
8336
8337 Opnd := Relocate_Node (Right_Opnd (N));
8338 Convert_To_Actual_Subtype (Opnd);
8339 Arr := Etype (Opnd);
8340 Ensure_Defined (Arr, N);
8341 Silly_Boolean_Array_Not_Test (N, Arr);
8342
8343 if Nkind (Parent (N)) = N_Assignment_Statement then
8344 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8345 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8346 return;
8347
8348 -- Special case the negation of a binary operation
8349
8350 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8351 and then Safe_In_Place_Array_Op
8352 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8353 then
8354 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8355 return;
8356 end if;
8357
8358 elsif Nkind (Parent (N)) in N_Binary_Op
8359 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8360 then
8361 declare
8362 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8363 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8364 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8365
8366 begin
8367 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8368
8369 -- (not A) op (not B) can be reduced to a single call
8370
8371 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8372 return;
8373
8374 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8375 return;
8376
8377 -- A xor (not B) can also be special-cased
8378
8379 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8380 return;
8381 end if;
8382 end if;
8383 end;
8384 end if;
8385
8386 A := Make_Defining_Identifier (Loc, Name_uA);
8387 B := Make_Defining_Identifier (Loc, Name_uB);
8388 J := Make_Defining_Identifier (Loc, Name_uJ);
8389
8390 A_J :=
8391 Make_Indexed_Component (Loc,
8392 Prefix => New_Reference_To (A, Loc),
8393 Expressions => New_List (New_Reference_To (J, Loc)));
8394
8395 B_J :=
8396 Make_Indexed_Component (Loc,
8397 Prefix => New_Reference_To (B, Loc),
8398 Expressions => New_List (New_Reference_To (J, Loc)));
8399
8400 Loop_Statement :=
8401 Make_Implicit_Loop_Statement (N,
8402 Identifier => Empty,
8403
8404 Iteration_Scheme =>
8405 Make_Iteration_Scheme (Loc,
8406 Loop_Parameter_Specification =>
8407 Make_Loop_Parameter_Specification (Loc,
8408 Defining_Identifier => J,
8409 Discrete_Subtype_Definition =>
8410 Make_Attribute_Reference (Loc,
8411 Prefix => Make_Identifier (Loc, Chars (A)),
8412 Attribute_Name => Name_Range))),
8413
8414 Statements => New_List (
8415 Make_Assignment_Statement (Loc,
8416 Name => B_J,
8417 Expression => Make_Op_Not (Loc, A_J))));
8418
8419 Func_Name := Make_Temporary (Loc, 'N');
8420 Set_Is_Inlined (Func_Name);
8421
8422 Insert_Action (N,
8423 Make_Subprogram_Body (Loc,
8424 Specification =>
8425 Make_Function_Specification (Loc,
8426 Defining_Unit_Name => Func_Name,
8427 Parameter_Specifications => New_List (
8428 Make_Parameter_Specification (Loc,
8429 Defining_Identifier => A,
8430 Parameter_Type => New_Reference_To (Typ, Loc))),
8431 Result_Definition => New_Reference_To (Typ, Loc)),
8432
8433 Declarations => New_List (
8434 Make_Object_Declaration (Loc,
8435 Defining_Identifier => B,
8436 Object_Definition => New_Reference_To (Arr, Loc))),
8437
8438 Handled_Statement_Sequence =>
8439 Make_Handled_Sequence_Of_Statements (Loc,
8440 Statements => New_List (
8441 Loop_Statement,
8442 Make_Simple_Return_Statement (Loc,
8443 Expression => Make_Identifier (Loc, Chars (B)))))));
8444
8445 Rewrite (N,
8446 Make_Function_Call (Loc,
8447 Name => New_Reference_To (Func_Name, Loc),
8448 Parameter_Associations => New_List (Opnd)));
8449
8450 Analyze_And_Resolve (N, Typ);
8451 end Expand_N_Op_Not;
8452
8453 --------------------
8454 -- Expand_N_Op_Or --
8455 --------------------
8456
8457 procedure Expand_N_Op_Or (N : Node_Id) is
8458 Typ : constant Entity_Id := Etype (N);
8459
8460 begin
8461 Binary_Op_Validity_Checks (N);
8462
8463 if Is_Array_Type (Etype (N)) then
8464 Expand_Boolean_Operator (N);
8465
8466 elsif Is_Boolean_Type (Etype (N)) then
8467 Adjust_Condition (Left_Opnd (N));
8468 Adjust_Condition (Right_Opnd (N));
8469 Set_Etype (N, Standard_Boolean);
8470 Adjust_Result_Type (N, Typ);
8471
8472 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8473 Expand_Intrinsic_Call (N, Entity (N));
8474
8475 end if;
8476 end Expand_N_Op_Or;
8477
8478 ----------------------
8479 -- Expand_N_Op_Plus --
8480 ----------------------
8481
8482 procedure Expand_N_Op_Plus (N : Node_Id) is
8483 begin
8484 Unary_Op_Validity_Checks (N);
8485 end Expand_N_Op_Plus;
8486
8487 ---------------------
8488 -- Expand_N_Op_Rem --
8489 ---------------------
8490
8491 procedure Expand_N_Op_Rem (N : Node_Id) is
8492 Loc : constant Source_Ptr := Sloc (N);
8493 Typ : constant Entity_Id := Etype (N);
8494
8495 Left : constant Node_Id := Left_Opnd (N);
8496 Right : constant Node_Id := Right_Opnd (N);
8497
8498 Lo : Uint;
8499 Hi : Uint;
8500 OK : Boolean;
8501
8502 Lneg : Boolean;
8503 Rneg : Boolean;
8504 -- Set if corresponding operand can be negative
8505
8506 pragma Unreferenced (Hi);
8507
8508 begin
8509 Binary_Op_Validity_Checks (N);
8510
8511 if Is_Integer_Type (Etype (N)) then
8512 Apply_Divide_Checks (N);
8513 end if;
8514
8515 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8516 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8517 -- harmless.
8518
8519 if Is_Integer_Type (Etype (N))
8520 and then Compile_Time_Known_Value (Right)
8521 and then Expr_Value (Right) = Uint_1
8522 then
8523 -- Call Remove_Side_Effects to ensure that any side effects in the
8524 -- ignored left operand (in particular function calls to user defined
8525 -- functions) are properly executed.
8526
8527 Remove_Side_Effects (Left);
8528
8529 Rewrite (N, Make_Integer_Literal (Loc, 0));
8530 Analyze_And_Resolve (N, Typ);
8531 return;
8532 end if;
8533
8534 -- Deal with annoying case of largest negative number remainder minus
8535 -- one. Gigi does not handle this case correctly, because it generates
8536 -- a divide instruction which may trap in this case.
8537
8538 -- In fact the check is quite easy, if the right operand is -1, then
8539 -- the remainder is always 0, and we can just ignore the left operand
8540 -- completely in this case.
8541
8542 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8543 Lneg := (not OK) or else Lo < 0;
8544
8545 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8546 Rneg := (not OK) or else Lo < 0;
8547
8548 -- We won't mess with trying to find out if the left operand can really
8549 -- be the largest negative number (that's a pain in the case of private
8550 -- types and this is really marginal). We will just assume that we need
8551 -- the test if the left operand can be negative at all.
8552
8553 if Lneg and Rneg then
8554 Rewrite (N,
8555 Make_Conditional_Expression (Loc,
8556 Expressions => New_List (
8557 Make_Op_Eq (Loc,
8558 Left_Opnd => Duplicate_Subexpr (Right),
8559 Right_Opnd =>
8560 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
8561
8562 Unchecked_Convert_To (Typ,
8563 Make_Integer_Literal (Loc, Uint_0)),
8564
8565 Relocate_Node (N))));
8566
8567 Set_Analyzed (Next (Next (First (Expressions (N)))));
8568 Analyze_And_Resolve (N, Typ);
8569 end if;
8570 end Expand_N_Op_Rem;
8571
8572 -----------------------------
8573 -- Expand_N_Op_Rotate_Left --
8574 -----------------------------
8575
8576 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8577 begin
8578 Binary_Op_Validity_Checks (N);
8579 end Expand_N_Op_Rotate_Left;
8580
8581 ------------------------------
8582 -- Expand_N_Op_Rotate_Right --
8583 ------------------------------
8584
8585 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8586 begin
8587 Binary_Op_Validity_Checks (N);
8588 end Expand_N_Op_Rotate_Right;
8589
8590 ----------------------------
8591 -- Expand_N_Op_Shift_Left --
8592 ----------------------------
8593
8594 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
8595 begin
8596 Binary_Op_Validity_Checks (N);
8597 end Expand_N_Op_Shift_Left;
8598
8599 -----------------------------
8600 -- Expand_N_Op_Shift_Right --
8601 -----------------------------
8602
8603 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
8604 begin
8605 Binary_Op_Validity_Checks (N);
8606 end Expand_N_Op_Shift_Right;
8607
8608 ----------------------------------------
8609 -- Expand_N_Op_Shift_Right_Arithmetic --
8610 ----------------------------------------
8611
8612 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
8613 begin
8614 Binary_Op_Validity_Checks (N);
8615 end Expand_N_Op_Shift_Right_Arithmetic;
8616
8617 --------------------------
8618 -- Expand_N_Op_Subtract --
8619 --------------------------
8620
8621 procedure Expand_N_Op_Subtract (N : Node_Id) is
8622 Typ : constant Entity_Id := Etype (N);
8623
8624 begin
8625 Binary_Op_Validity_Checks (N);
8626
8627 -- N - 0 = N for integer types
8628
8629 if Is_Integer_Type (Typ)
8630 and then Compile_Time_Known_Value (Right_Opnd (N))
8631 and then Expr_Value (Right_Opnd (N)) = 0
8632 then
8633 Rewrite (N, Left_Opnd (N));
8634 return;
8635 end if;
8636
8637 -- Arithmetic overflow checks for signed integer/fixed point types
8638
8639 if Is_Signed_Integer_Type (Typ)
8640 or else
8641 Is_Fixed_Point_Type (Typ)
8642 then
8643 Apply_Arithmetic_Overflow_Check (N);
8644
8645 -- VAX floating-point types case
8646
8647 elsif Vax_Float (Typ) then
8648 Expand_Vax_Arith (N);
8649 end if;
8650 end Expand_N_Op_Subtract;
8651
8652 ---------------------
8653 -- Expand_N_Op_Xor --
8654 ---------------------
8655
8656 procedure Expand_N_Op_Xor (N : Node_Id) is
8657 Typ : constant Entity_Id := Etype (N);
8658
8659 begin
8660 Binary_Op_Validity_Checks (N);
8661
8662 if Is_Array_Type (Etype (N)) then
8663 Expand_Boolean_Operator (N);
8664
8665 elsif Is_Boolean_Type (Etype (N)) then
8666 Adjust_Condition (Left_Opnd (N));
8667 Adjust_Condition (Right_Opnd (N));
8668 Set_Etype (N, Standard_Boolean);
8669 Adjust_Result_Type (N, Typ);
8670
8671 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8672 Expand_Intrinsic_Call (N, Entity (N));
8673
8674 end if;
8675 end Expand_N_Op_Xor;
8676
8677 ----------------------
8678 -- Expand_N_Or_Else --
8679 ----------------------
8680
8681 procedure Expand_N_Or_Else (N : Node_Id)
8682 renames Expand_Short_Circuit_Operator;
8683
8684 -----------------------------------
8685 -- Expand_N_Qualified_Expression --
8686 -----------------------------------
8687
8688 procedure Expand_N_Qualified_Expression (N : Node_Id) is
8689 Operand : constant Node_Id := Expression (N);
8690 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
8691
8692 begin
8693 -- Do validity check if validity checking operands
8694
8695 if Validity_Checks_On and then Validity_Check_Operands then
8696 Ensure_Valid (Operand);
8697 end if;
8698
8699 -- Apply possible constraint check
8700
8701 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
8702
8703 if Do_Range_Check (Operand) then
8704 Set_Do_Range_Check (Operand, False);
8705 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
8706 end if;
8707 end Expand_N_Qualified_Expression;
8708
8709 ------------------------------------
8710 -- Expand_N_Quantified_Expression --
8711 ------------------------------------
8712
8713 -- We expand:
8714
8715 -- for all X in range => Cond
8716
8717 -- into:
8718
8719 -- T := True;
8720 -- for X in range loop
8721 -- if not Cond then
8722 -- T := False;
8723 -- exit;
8724 -- end if;
8725 -- end loop;
8726
8727 -- Similarly, an existentially quantified expression:
8728
8729 -- for some X in range => Cond
8730
8731 -- becomes:
8732
8733 -- T := False;
8734 -- for X in range loop
8735 -- if Cond then
8736 -- T := True;
8737 -- exit;
8738 -- end if;
8739 -- end loop;
8740
8741 -- In both cases, the iteration may be over a container in which case it is
8742 -- given by an iterator specification, not a loop parameter specification.
8743
8744 procedure Expand_N_Quantified_Expression (N : Node_Id) is
8745 Actions : constant List_Id := New_List;
8746 For_All : constant Boolean := All_Present (N);
8747 Iter_Spec : constant Node_Id := Iterator_Specification (N);
8748 Loc : constant Source_Ptr := Sloc (N);
8749 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
8750 Cond : Node_Id;
8751 Flag : Entity_Id;
8752 Scheme : Node_Id;
8753 Stmts : List_Id;
8754
8755 begin
8756 -- Create the declaration of the flag which tracks the status of the
8757 -- quantified expression. Generate:
8758
8759 -- Flag : Boolean := (True | False);
8760
8761 Flag := Make_Temporary (Loc, 'T', N);
8762
8763 Append_To (Actions,
8764 Make_Object_Declaration (Loc,
8765 Defining_Identifier => Flag,
8766 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
8767 Expression =>
8768 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
8769
8770 -- Construct the circuitry which tracks the status of the quantified
8771 -- expression. Generate:
8772
8773 -- if [not] Cond then
8774 -- Flag := (False | True);
8775 -- exit;
8776 -- end if;
8777
8778 Cond := Relocate_Node (Condition (N));
8779
8780 if For_All then
8781 Cond := Make_Op_Not (Loc, Cond);
8782 end if;
8783
8784 Stmts := New_List (
8785 Make_Implicit_If_Statement (N,
8786 Condition => Cond,
8787 Then_Statements => New_List (
8788 Make_Assignment_Statement (Loc,
8789 Name => New_Occurrence_Of (Flag, Loc),
8790 Expression =>
8791 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
8792 Make_Exit_Statement (Loc))));
8793
8794 -- Build the loop equivalent of the quantified expression
8795
8796 if Present (Iter_Spec) then
8797 Scheme :=
8798 Make_Iteration_Scheme (Loc,
8799 Iterator_Specification => Iter_Spec);
8800 else
8801 Scheme :=
8802 Make_Iteration_Scheme (Loc,
8803 Loop_Parameter_Specification => Loop_Spec);
8804 end if;
8805
8806 Append_To (Actions,
8807 Make_Loop_Statement (Loc,
8808 Iteration_Scheme => Scheme,
8809 Statements => Stmts,
8810 End_Label => Empty));
8811
8812 -- Transform the quantified expression
8813
8814 Rewrite (N,
8815 Make_Expression_With_Actions (Loc,
8816 Expression => New_Occurrence_Of (Flag, Loc),
8817 Actions => Actions));
8818 Analyze_And_Resolve (N, Standard_Boolean);
8819 end Expand_N_Quantified_Expression;
8820
8821 ---------------------------------
8822 -- Expand_N_Selected_Component --
8823 ---------------------------------
8824
8825 procedure Expand_N_Selected_Component (N : Node_Id) is
8826 Loc : constant Source_Ptr := Sloc (N);
8827 Par : constant Node_Id := Parent (N);
8828 P : constant Node_Id := Prefix (N);
8829 Ptyp : Entity_Id := Underlying_Type (Etype (P));
8830 Disc : Entity_Id;
8831 New_N : Node_Id;
8832 Dcon : Elmt_Id;
8833 Dval : Node_Id;
8834
8835 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
8836 -- Gigi needs a temporary for prefixes that depend on a discriminant,
8837 -- unless the context of an assignment can provide size information.
8838 -- Don't we have a general routine that does this???
8839
8840 function Is_Subtype_Declaration return Boolean;
8841 -- The replacement of a discriminant reference by its value is required
8842 -- if this is part of the initialization of an temporary generated by a
8843 -- change of representation. This shows up as the construction of a
8844 -- discriminant constraint for a subtype declared at the same point as
8845 -- the entity in the prefix of the selected component. We recognize this
8846 -- case when the context of the reference is:
8847 -- subtype ST is T(Obj.D);
8848 -- where the entity for Obj comes from source, and ST has the same sloc.
8849
8850 -----------------------
8851 -- In_Left_Hand_Side --
8852 -----------------------
8853
8854 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
8855 begin
8856 return (Nkind (Parent (Comp)) = N_Assignment_Statement
8857 and then Comp = Name (Parent (Comp)))
8858 or else (Present (Parent (Comp))
8859 and then Nkind (Parent (Comp)) in N_Subexpr
8860 and then In_Left_Hand_Side (Parent (Comp)));
8861 end In_Left_Hand_Side;
8862
8863 -----------------------------
8864 -- Is_Subtype_Declaration --
8865 -----------------------------
8866
8867 function Is_Subtype_Declaration return Boolean is
8868 Par : constant Node_Id := Parent (N);
8869 begin
8870 return
8871 Nkind (Par) = N_Index_Or_Discriminant_Constraint
8872 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
8873 and then Comes_From_Source (Entity (Prefix (N)))
8874 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
8875 end Is_Subtype_Declaration;
8876
8877 -- Start of processing for Expand_N_Selected_Component
8878
8879 begin
8880 -- Insert explicit dereference if required
8881
8882 if Is_Access_Type (Ptyp) then
8883
8884 -- First set prefix type to proper access type, in case it currently
8885 -- has a private (non-access) view of this type.
8886
8887 Set_Etype (P, Ptyp);
8888
8889 Insert_Explicit_Dereference (P);
8890 Analyze_And_Resolve (P, Designated_Type (Ptyp));
8891
8892 if Ekind (Etype (P)) = E_Private_Subtype
8893 and then Is_For_Access_Subtype (Etype (P))
8894 then
8895 Set_Etype (P, Base_Type (Etype (P)));
8896 end if;
8897
8898 Ptyp := Etype (P);
8899 end if;
8900
8901 -- Deal with discriminant check required
8902
8903 if Do_Discriminant_Check (N) then
8904
8905 -- Present the discriminant checking function to the backend, so that
8906 -- it can inline the call to the function.
8907
8908 Add_Inlined_Body
8909 (Discriminant_Checking_Func
8910 (Original_Record_Component (Entity (Selector_Name (N)))));
8911
8912 -- Now reset the flag and generate the call
8913
8914 Set_Do_Discriminant_Check (N, False);
8915 Generate_Discriminant_Check (N);
8916 end if;
8917
8918 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
8919 -- function, then additional actuals must be passed.
8920
8921 if Ada_Version >= Ada_2005
8922 and then Is_Build_In_Place_Function_Call (P)
8923 then
8924 Make_Build_In_Place_Call_In_Anonymous_Context (P);
8925 end if;
8926
8927 -- Gigi cannot handle unchecked conversions that are the prefix of a
8928 -- selected component with discriminants. This must be checked during
8929 -- expansion, because during analysis the type of the selector is not
8930 -- known at the point the prefix is analyzed. If the conversion is the
8931 -- target of an assignment, then we cannot force the evaluation.
8932
8933 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
8934 and then Has_Discriminants (Etype (N))
8935 and then not In_Left_Hand_Side (N)
8936 then
8937 Force_Evaluation (Prefix (N));
8938 end if;
8939
8940 -- Remaining processing applies only if selector is a discriminant
8941
8942 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
8943
8944 -- If the selector is a discriminant of a constrained record type,
8945 -- we may be able to rewrite the expression with the actual value
8946 -- of the discriminant, a useful optimization in some cases.
8947
8948 if Is_Record_Type (Ptyp)
8949 and then Has_Discriminants (Ptyp)
8950 and then Is_Constrained (Ptyp)
8951 then
8952 -- Do this optimization for discrete types only, and not for
8953 -- access types (access discriminants get us into trouble!)
8954
8955 if not Is_Discrete_Type (Etype (N)) then
8956 null;
8957
8958 -- Don't do this on the left hand of an assignment statement.
8959 -- Normally one would think that references like this would not
8960 -- occur, but they do in generated code, and mean that we really
8961 -- do want to assign the discriminant!
8962
8963 elsif Nkind (Par) = N_Assignment_Statement
8964 and then Name (Par) = N
8965 then
8966 null;
8967
8968 -- Don't do this optimization for the prefix of an attribute or
8969 -- the name of an object renaming declaration since these are
8970 -- contexts where we do not want the value anyway.
8971
8972 elsif (Nkind (Par) = N_Attribute_Reference
8973 and then Prefix (Par) = N)
8974 or else Is_Renamed_Object (N)
8975 then
8976 null;
8977
8978 -- Don't do this optimization if we are within the code for a
8979 -- discriminant check, since the whole point of such a check may
8980 -- be to verify the condition on which the code below depends!
8981
8982 elsif Is_In_Discriminant_Check (N) then
8983 null;
8984
8985 -- Green light to see if we can do the optimization. There is
8986 -- still one condition that inhibits the optimization below but
8987 -- now is the time to check the particular discriminant.
8988
8989 else
8990 -- Loop through discriminants to find the matching discriminant
8991 -- constraint to see if we can copy it.
8992
8993 Disc := First_Discriminant (Ptyp);
8994 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
8995 Discr_Loop : while Present (Dcon) loop
8996 Dval := Node (Dcon);
8997
8998 -- Check if this is the matching discriminant and if the
8999 -- discriminant value is simple enough to make sense to
9000 -- copy. We don't want to copy complex expressions, and
9001 -- indeed to do so can cause trouble (before we put in
9002 -- this guard, a discriminant expression containing an
9003 -- AND THEN was copied, causing problems for coverage
9004 -- analysis tools).
9005
9006 -- However, if the reference is part of the initialization
9007 -- code generated for an object declaration, we must use
9008 -- the discriminant value from the subtype constraint,
9009 -- because the selected component may be a reference to the
9010 -- object being initialized, whose discriminant is not yet
9011 -- set. This only happens in complex cases involving changes
9012 -- or representation.
9013
9014 if Disc = Entity (Selector_Name (N))
9015 and then (Is_Entity_Name (Dval)
9016 or else Compile_Time_Known_Value (Dval)
9017 or else Is_Subtype_Declaration)
9018 then
9019 -- Here we have the matching discriminant. Check for
9020 -- the case of a discriminant of a component that is
9021 -- constrained by an outer discriminant, which cannot
9022 -- be optimized away.
9023
9024 if Denotes_Discriminant
9025 (Dval, Check_Concurrent => True)
9026 then
9027 exit Discr_Loop;
9028
9029 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9030 and then
9031 Denotes_Discriminant
9032 (Selector_Name (Original_Node (Dval)), True)
9033 then
9034 exit Discr_Loop;
9035
9036 -- Do not retrieve value if constraint is not static. It
9037 -- is generally not useful, and the constraint may be a
9038 -- rewritten outer discriminant in which case it is in
9039 -- fact incorrect.
9040
9041 elsif Is_Entity_Name (Dval)
9042 and then Nkind (Parent (Entity (Dval))) =
9043 N_Object_Declaration
9044 and then Present (Expression (Parent (Entity (Dval))))
9045 and then
9046 not Is_Static_Expression
9047 (Expression (Parent (Entity (Dval))))
9048 then
9049 exit Discr_Loop;
9050
9051 -- In the context of a case statement, the expression may
9052 -- have the base type of the discriminant, and we need to
9053 -- preserve the constraint to avoid spurious errors on
9054 -- missing cases.
9055
9056 elsif Nkind (Parent (N)) = N_Case_Statement
9057 and then Etype (Dval) /= Etype (Disc)
9058 then
9059 Rewrite (N,
9060 Make_Qualified_Expression (Loc,
9061 Subtype_Mark =>
9062 New_Occurrence_Of (Etype (Disc), Loc),
9063 Expression =>
9064 New_Copy_Tree (Dval)));
9065 Analyze_And_Resolve (N, Etype (Disc));
9066
9067 -- In case that comes out as a static expression,
9068 -- reset it (a selected component is never static).
9069
9070 Set_Is_Static_Expression (N, False);
9071 return;
9072
9073 -- Otherwise we can just copy the constraint, but the
9074 -- result is certainly not static! In some cases the
9075 -- discriminant constraint has been analyzed in the
9076 -- context of the original subtype indication, but for
9077 -- itypes the constraint might not have been analyzed
9078 -- yet, and this must be done now.
9079
9080 else
9081 Rewrite (N, New_Copy_Tree (Dval));
9082 Analyze_And_Resolve (N);
9083 Set_Is_Static_Expression (N, False);
9084 return;
9085 end if;
9086 end if;
9087
9088 Next_Elmt (Dcon);
9089 Next_Discriminant (Disc);
9090 end loop Discr_Loop;
9091
9092 -- Note: the above loop should always find a matching
9093 -- discriminant, but if it does not, we just missed an
9094 -- optimization due to some glitch (perhaps a previous
9095 -- error), so ignore.
9096
9097 end if;
9098 end if;
9099
9100 -- The only remaining processing is in the case of a discriminant of
9101 -- a concurrent object, where we rewrite the prefix to denote the
9102 -- corresponding record type. If the type is derived and has renamed
9103 -- discriminants, use corresponding discriminant, which is the one
9104 -- that appears in the corresponding record.
9105
9106 if not Is_Concurrent_Type (Ptyp) then
9107 return;
9108 end if;
9109
9110 Disc := Entity (Selector_Name (N));
9111
9112 if Is_Derived_Type (Ptyp)
9113 and then Present (Corresponding_Discriminant (Disc))
9114 then
9115 Disc := Corresponding_Discriminant (Disc);
9116 end if;
9117
9118 New_N :=
9119 Make_Selected_Component (Loc,
9120 Prefix =>
9121 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9122 New_Copy_Tree (P)),
9123 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9124
9125 Rewrite (N, New_N);
9126 Analyze (N);
9127 end if;
9128
9129 -- Set Atomic_Sync_Required if necessary for atomic component
9130
9131 if Nkind (N) = N_Selected_Component then
9132 declare
9133 E : constant Entity_Id := Entity (Selector_Name (N));
9134 Set : Boolean;
9135
9136 begin
9137 -- If component is atomic, but type is not, setting depends on
9138 -- disable/enable state for the component.
9139
9140 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9141 Set := not Atomic_Synchronization_Disabled (E);
9142
9143 -- If component is not atomic, but its type is atomic, setting
9144 -- depends on disable/enable state for the type.
9145
9146 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9147 Set := not Atomic_Synchronization_Disabled (Etype (E));
9148
9149 -- If both component and type are atomic, we disable if either
9150 -- component or its type have sync disabled.
9151
9152 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9153 Set := (not Atomic_Synchronization_Disabled (E))
9154 and then
9155 (not Atomic_Synchronization_Disabled (Etype (E)));
9156
9157 else
9158 Set := False;
9159 end if;
9160
9161 -- Set flag if required
9162
9163 if Set then
9164 Activate_Atomic_Synchronization (N);
9165 end if;
9166 end;
9167 end if;
9168 end Expand_N_Selected_Component;
9169
9170 --------------------
9171 -- Expand_N_Slice --
9172 --------------------
9173
9174 procedure Expand_N_Slice (N : Node_Id) is
9175 Loc : constant Source_Ptr := Sloc (N);
9176 Typ : constant Entity_Id := Etype (N);
9177 Pfx : constant Node_Id := Prefix (N);
9178 Ptp : Entity_Id := Etype (Pfx);
9179
9180 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9181 -- Check whether the argument is an actual for a procedure call, in
9182 -- which case the expansion of a bit-packed slice is deferred until the
9183 -- call itself is expanded. The reason this is required is that we might
9184 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9185 -- that copy out would be missed if we created a temporary here in
9186 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9187 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9188 -- is harmless to defer expansion in the IN case, since the call
9189 -- processing will still generate the appropriate copy in operation,
9190 -- which will take care of the slice.
9191
9192 procedure Make_Temporary_For_Slice;
9193 -- Create a named variable for the value of the slice, in cases where
9194 -- the back-end cannot handle it properly, e.g. when packed types or
9195 -- unaligned slices are involved.
9196
9197 -------------------------
9198 -- Is_Procedure_Actual --
9199 -------------------------
9200
9201 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9202 Par : Node_Id := Parent (N);
9203
9204 begin
9205 loop
9206 -- If our parent is a procedure call we can return
9207
9208 if Nkind (Par) = N_Procedure_Call_Statement then
9209 return True;
9210
9211 -- If our parent is a type conversion, keep climbing the tree,
9212 -- since a type conversion can be a procedure actual. Also keep
9213 -- climbing if parameter association or a qualified expression,
9214 -- since these are additional cases that do can appear on
9215 -- procedure actuals.
9216
9217 elsif Nkind_In (Par, N_Type_Conversion,
9218 N_Parameter_Association,
9219 N_Qualified_Expression)
9220 then
9221 Par := Parent (Par);
9222
9223 -- Any other case is not what we are looking for
9224
9225 else
9226 return False;
9227 end if;
9228 end loop;
9229 end Is_Procedure_Actual;
9230
9231 ------------------------------
9232 -- Make_Temporary_For_Slice --
9233 ------------------------------
9234
9235 procedure Make_Temporary_For_Slice is
9236 Decl : Node_Id;
9237 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9238
9239 begin
9240 Decl :=
9241 Make_Object_Declaration (Loc,
9242 Defining_Identifier => Ent,
9243 Object_Definition => New_Occurrence_Of (Typ, Loc));
9244
9245 Set_No_Initialization (Decl);
9246
9247 Insert_Actions (N, New_List (
9248 Decl,
9249 Make_Assignment_Statement (Loc,
9250 Name => New_Occurrence_Of (Ent, Loc),
9251 Expression => Relocate_Node (N))));
9252
9253 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9254 Analyze_And_Resolve (N, Typ);
9255 end Make_Temporary_For_Slice;
9256
9257 -- Start of processing for Expand_N_Slice
9258
9259 begin
9260 -- Special handling for access types
9261
9262 if Is_Access_Type (Ptp) then
9263
9264 Ptp := Designated_Type (Ptp);
9265
9266 Rewrite (Pfx,
9267 Make_Explicit_Dereference (Sloc (N),
9268 Prefix => Relocate_Node (Pfx)));
9269
9270 Analyze_And_Resolve (Pfx, Ptp);
9271 end if;
9272
9273 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9274 -- function, then additional actuals must be passed.
9275
9276 if Ada_Version >= Ada_2005
9277 and then Is_Build_In_Place_Function_Call (Pfx)
9278 then
9279 Make_Build_In_Place_Call_In_Anonymous_Context (Pfx);
9280 end if;
9281
9282 -- The remaining case to be handled is packed slices. We can leave
9283 -- packed slices as they are in the following situations:
9284
9285 -- 1. Right or left side of an assignment (we can handle this
9286 -- situation correctly in the assignment statement expansion).
9287
9288 -- 2. Prefix of indexed component (the slide is optimized away in this
9289 -- case, see the start of Expand_N_Slice.)
9290
9291 -- 3. Object renaming declaration, since we want the name of the
9292 -- slice, not the value.
9293
9294 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9295 -- be required, and this is handled in the expansion of call
9296 -- itself.
9297
9298 -- 5. Prefix of an address attribute (this is an error which is caught
9299 -- elsewhere, and the expansion would interfere with generating the
9300 -- error message).
9301
9302 if not Is_Packed (Typ) then
9303
9304 -- Apply transformation for actuals of a function call, where
9305 -- Expand_Actuals is not used.
9306
9307 if Nkind (Parent (N)) = N_Function_Call
9308 and then Is_Possibly_Unaligned_Slice (N)
9309 then
9310 Make_Temporary_For_Slice;
9311 end if;
9312
9313 elsif Nkind (Parent (N)) = N_Assignment_Statement
9314 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
9315 and then Parent (N) = Name (Parent (Parent (N))))
9316 then
9317 return;
9318
9319 elsif Nkind (Parent (N)) = N_Indexed_Component
9320 or else Is_Renamed_Object (N)
9321 or else Is_Procedure_Actual (N)
9322 then
9323 return;
9324
9325 elsif Nkind (Parent (N)) = N_Attribute_Reference
9326 and then Attribute_Name (Parent (N)) = Name_Address
9327 then
9328 return;
9329
9330 else
9331 Make_Temporary_For_Slice;
9332 end if;
9333 end Expand_N_Slice;
9334
9335 ------------------------------
9336 -- Expand_N_Type_Conversion --
9337 ------------------------------
9338
9339 procedure Expand_N_Type_Conversion (N : Node_Id) is
9340 Loc : constant Source_Ptr := Sloc (N);
9341 Operand : constant Node_Id := Expression (N);
9342 Target_Type : constant Entity_Id := Etype (N);
9343 Operand_Type : Entity_Id := Etype (Operand);
9344
9345 procedure Handle_Changed_Representation;
9346 -- This is called in the case of record and array type conversions to
9347 -- see if there is a change of representation to be handled. Change of
9348 -- representation is actually handled at the assignment statement level,
9349 -- and what this procedure does is rewrite node N conversion as an
9350 -- assignment to temporary. If there is no change of representation,
9351 -- then the conversion node is unchanged.
9352
9353 procedure Raise_Accessibility_Error;
9354 -- Called when we know that an accessibility check will fail. Rewrites
9355 -- node N to an appropriate raise statement and outputs warning msgs.
9356 -- The Etype of the raise node is set to Target_Type.
9357
9358 procedure Real_Range_Check;
9359 -- Handles generation of range check for real target value
9360
9361 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9362 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9363 -- evaluates to True.
9364
9365 -----------------------------------
9366 -- Handle_Changed_Representation --
9367 -----------------------------------
9368
9369 procedure Handle_Changed_Representation is
9370 Temp : Entity_Id;
9371 Decl : Node_Id;
9372 Odef : Node_Id;
9373 Disc : Node_Id;
9374 N_Ix : Node_Id;
9375 Cons : List_Id;
9376
9377 begin
9378 -- Nothing else to do if no change of representation
9379
9380 if Same_Representation (Operand_Type, Target_Type) then
9381 return;
9382
9383 -- The real change of representation work is done by the assignment
9384 -- statement processing. So if this type conversion is appearing as
9385 -- the expression of an assignment statement, nothing needs to be
9386 -- done to the conversion.
9387
9388 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9389 return;
9390
9391 -- Otherwise we need to generate a temporary variable, and do the
9392 -- change of representation assignment into that temporary variable.
9393 -- The conversion is then replaced by a reference to this variable.
9394
9395 else
9396 Cons := No_List;
9397
9398 -- If type is unconstrained we have to add a constraint, copied
9399 -- from the actual value of the left hand side.
9400
9401 if not Is_Constrained (Target_Type) then
9402 if Has_Discriminants (Operand_Type) then
9403 Disc := First_Discriminant (Operand_Type);
9404
9405 if Disc /= First_Stored_Discriminant (Operand_Type) then
9406 Disc := First_Stored_Discriminant (Operand_Type);
9407 end if;
9408
9409 Cons := New_List;
9410 while Present (Disc) loop
9411 Append_To (Cons,
9412 Make_Selected_Component (Loc,
9413 Prefix =>
9414 Duplicate_Subexpr_Move_Checks (Operand),
9415 Selector_Name =>
9416 Make_Identifier (Loc, Chars (Disc))));
9417 Next_Discriminant (Disc);
9418 end loop;
9419
9420 elsif Is_Array_Type (Operand_Type) then
9421 N_Ix := First_Index (Target_Type);
9422 Cons := New_List;
9423
9424 for J in 1 .. Number_Dimensions (Operand_Type) loop
9425
9426 -- We convert the bounds explicitly. We use an unchecked
9427 -- conversion because bounds checks are done elsewhere.
9428
9429 Append_To (Cons,
9430 Make_Range (Loc,
9431 Low_Bound =>
9432 Unchecked_Convert_To (Etype (N_Ix),
9433 Make_Attribute_Reference (Loc,
9434 Prefix =>
9435 Duplicate_Subexpr_No_Checks
9436 (Operand, Name_Req => True),
9437 Attribute_Name => Name_First,
9438 Expressions => New_List (
9439 Make_Integer_Literal (Loc, J)))),
9440
9441 High_Bound =>
9442 Unchecked_Convert_To (Etype (N_Ix),
9443 Make_Attribute_Reference (Loc,
9444 Prefix =>
9445 Duplicate_Subexpr_No_Checks
9446 (Operand, Name_Req => True),
9447 Attribute_Name => Name_Last,
9448 Expressions => New_List (
9449 Make_Integer_Literal (Loc, J))))));
9450
9451 Next_Index (N_Ix);
9452 end loop;
9453 end if;
9454 end if;
9455
9456 Odef := New_Occurrence_Of (Target_Type, Loc);
9457
9458 if Present (Cons) then
9459 Odef :=
9460 Make_Subtype_Indication (Loc,
9461 Subtype_Mark => Odef,
9462 Constraint =>
9463 Make_Index_Or_Discriminant_Constraint (Loc,
9464 Constraints => Cons));
9465 end if;
9466
9467 Temp := Make_Temporary (Loc, 'C');
9468 Decl :=
9469 Make_Object_Declaration (Loc,
9470 Defining_Identifier => Temp,
9471 Object_Definition => Odef);
9472
9473 Set_No_Initialization (Decl, True);
9474
9475 -- Insert required actions. It is essential to suppress checks
9476 -- since we have suppressed default initialization, which means
9477 -- that the variable we create may have no discriminants.
9478
9479 Insert_Actions (N,
9480 New_List (
9481 Decl,
9482 Make_Assignment_Statement (Loc,
9483 Name => New_Occurrence_Of (Temp, Loc),
9484 Expression => Relocate_Node (N))),
9485 Suppress => All_Checks);
9486
9487 Rewrite (N, New_Occurrence_Of (Temp, Loc));
9488 return;
9489 end if;
9490 end Handle_Changed_Representation;
9491
9492 -------------------------------
9493 -- Raise_Accessibility_Error --
9494 -------------------------------
9495
9496 procedure Raise_Accessibility_Error is
9497 begin
9498 Rewrite (N,
9499 Make_Raise_Program_Error (Sloc (N),
9500 Reason => PE_Accessibility_Check_Failed));
9501 Set_Etype (N, Target_Type);
9502
9503 Error_Msg_N ("?accessibility check failure", N);
9504 Error_Msg_NE
9505 ("\?& will be raised at run time", N, Standard_Program_Error);
9506 end Raise_Accessibility_Error;
9507
9508 ----------------------
9509 -- Real_Range_Check --
9510 ----------------------
9511
9512 -- Case of conversions to floating-point or fixed-point. If range checks
9513 -- are enabled and the target type has a range constraint, we convert:
9514
9515 -- typ (x)
9516
9517 -- to
9518
9519 -- Tnn : typ'Base := typ'Base (x);
9520 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
9521 -- Tnn
9522
9523 -- This is necessary when there is a conversion of integer to float or
9524 -- to fixed-point to ensure that the correct checks are made. It is not
9525 -- necessary for float to float where it is enough to simply set the
9526 -- Do_Range_Check flag.
9527
9528 procedure Real_Range_Check is
9529 Btyp : constant Entity_Id := Base_Type (Target_Type);
9530 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
9531 Hi : constant Node_Id := Type_High_Bound (Target_Type);
9532 Xtyp : constant Entity_Id := Etype (Operand);
9533 Conv : Node_Id;
9534 Tnn : Entity_Id;
9535
9536 begin
9537 -- Nothing to do if conversion was rewritten
9538
9539 if Nkind (N) /= N_Type_Conversion then
9540 return;
9541 end if;
9542
9543 -- Nothing to do if range checks suppressed, or target has the same
9544 -- range as the base type (or is the base type).
9545
9546 if Range_Checks_Suppressed (Target_Type)
9547 or else (Lo = Type_Low_Bound (Btyp)
9548 and then
9549 Hi = Type_High_Bound (Btyp))
9550 then
9551 return;
9552 end if;
9553
9554 -- Nothing to do if expression is an entity on which checks have been
9555 -- suppressed.
9556
9557 if Is_Entity_Name (Operand)
9558 and then Range_Checks_Suppressed (Entity (Operand))
9559 then
9560 return;
9561 end if;
9562
9563 -- Nothing to do if bounds are all static and we can tell that the
9564 -- expression is within the bounds of the target. Note that if the
9565 -- operand is of an unconstrained floating-point type, then we do
9566 -- not trust it to be in range (might be infinite)
9567
9568 declare
9569 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
9570 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
9571
9572 begin
9573 if (not Is_Floating_Point_Type (Xtyp)
9574 or else Is_Constrained (Xtyp))
9575 and then Compile_Time_Known_Value (S_Lo)
9576 and then Compile_Time_Known_Value (S_Hi)
9577 and then Compile_Time_Known_Value (Hi)
9578 and then Compile_Time_Known_Value (Lo)
9579 then
9580 declare
9581 D_Lov : constant Ureal := Expr_Value_R (Lo);
9582 D_Hiv : constant Ureal := Expr_Value_R (Hi);
9583 S_Lov : Ureal;
9584 S_Hiv : Ureal;
9585
9586 begin
9587 if Is_Real_Type (Xtyp) then
9588 S_Lov := Expr_Value_R (S_Lo);
9589 S_Hiv := Expr_Value_R (S_Hi);
9590 else
9591 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
9592 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
9593 end if;
9594
9595 if D_Hiv > D_Lov
9596 and then S_Lov >= D_Lov
9597 and then S_Hiv <= D_Hiv
9598 then
9599 Set_Do_Range_Check (Operand, False);
9600 return;
9601 end if;
9602 end;
9603 end if;
9604 end;
9605
9606 -- For float to float conversions, we are done
9607
9608 if Is_Floating_Point_Type (Xtyp)
9609 and then
9610 Is_Floating_Point_Type (Btyp)
9611 then
9612 return;
9613 end if;
9614
9615 -- Otherwise rewrite the conversion as described above
9616
9617 Conv := Relocate_Node (N);
9618 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
9619 Set_Etype (Conv, Btyp);
9620
9621 -- Enable overflow except for case of integer to float conversions,
9622 -- where it is never required, since we can never have overflow in
9623 -- this case.
9624
9625 if not Is_Integer_Type (Etype (Operand)) then
9626 Enable_Overflow_Check (Conv);
9627 end if;
9628
9629 Tnn := Make_Temporary (Loc, 'T', Conv);
9630
9631 Insert_Actions (N, New_List (
9632 Make_Object_Declaration (Loc,
9633 Defining_Identifier => Tnn,
9634 Object_Definition => New_Occurrence_Of (Btyp, Loc),
9635 Constant_Present => True,
9636 Expression => Conv),
9637
9638 Make_Raise_Constraint_Error (Loc,
9639 Condition =>
9640 Make_Or_Else (Loc,
9641 Left_Opnd =>
9642 Make_Op_Lt (Loc,
9643 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9644 Right_Opnd =>
9645 Make_Attribute_Reference (Loc,
9646 Attribute_Name => Name_First,
9647 Prefix =>
9648 New_Occurrence_Of (Target_Type, Loc))),
9649
9650 Right_Opnd =>
9651 Make_Op_Gt (Loc,
9652 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
9653 Right_Opnd =>
9654 Make_Attribute_Reference (Loc,
9655 Attribute_Name => Name_Last,
9656 Prefix =>
9657 New_Occurrence_Of (Target_Type, Loc)))),
9658 Reason => CE_Range_Check_Failed)));
9659
9660 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
9661 Analyze_And_Resolve (N, Btyp);
9662 end Real_Range_Check;
9663
9664 -----------------------------
9665 -- Has_Extra_Accessibility --
9666 -----------------------------
9667
9668 -- Returns true for a formal of an anonymous access type or for
9669 -- an Ada 2012-style stand-alone object of an anonymous access type.
9670
9671 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
9672 begin
9673 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
9674 return Present (Effective_Extra_Accessibility (Id));
9675 else
9676 return False;
9677 end if;
9678 end Has_Extra_Accessibility;
9679
9680 -- Start of processing for Expand_N_Type_Conversion
9681
9682 begin
9683 -- Nothing at all to do if conversion is to the identical type so remove
9684 -- the conversion completely, it is useless, except that it may carry
9685 -- an Assignment_OK attribute, which must be propagated to the operand.
9686
9687 if Operand_Type = Target_Type then
9688 if Assignment_OK (N) then
9689 Set_Assignment_OK (Operand);
9690 end if;
9691
9692 Rewrite (N, Relocate_Node (Operand));
9693 goto Done;
9694 end if;
9695
9696 -- Nothing to do if this is the second argument of read. This is a
9697 -- "backwards" conversion that will be handled by the specialized code
9698 -- in attribute processing.
9699
9700 if Nkind (Parent (N)) = N_Attribute_Reference
9701 and then Attribute_Name (Parent (N)) = Name_Read
9702 and then Next (First (Expressions (Parent (N)))) = N
9703 then
9704 goto Done;
9705 end if;
9706
9707 -- Check for case of converting to a type that has an invariant
9708 -- associated with it. This required an invariant check. We convert
9709
9710 -- typ (expr)
9711
9712 -- into
9713
9714 -- do invariant_check (typ (expr)) in typ (expr);
9715
9716 -- using Duplicate_Subexpr to avoid multiple side effects
9717
9718 -- Note: the Comes_From_Source check, and then the resetting of this
9719 -- flag prevents what would otherwise be an infinite recursion.
9720
9721 if Has_Invariants (Target_Type)
9722 and then Present (Invariant_Procedure (Target_Type))
9723 and then Comes_From_Source (N)
9724 then
9725 Set_Comes_From_Source (N, False);
9726 Rewrite (N,
9727 Make_Expression_With_Actions (Loc,
9728 Actions => New_List (
9729 Make_Invariant_Call (Duplicate_Subexpr (N))),
9730 Expression => Duplicate_Subexpr_No_Checks (N)));
9731 Analyze_And_Resolve (N, Target_Type);
9732 goto Done;
9733 end if;
9734
9735 -- Here if we may need to expand conversion
9736
9737 -- If the operand of the type conversion is an arithmetic operation on
9738 -- signed integers, and the based type of the signed integer type in
9739 -- question is smaller than Standard.Integer, we promote both of the
9740 -- operands to type Integer.
9741
9742 -- For example, if we have
9743
9744 -- target-type (opnd1 + opnd2)
9745
9746 -- and opnd1 and opnd2 are of type short integer, then we rewrite
9747 -- this as:
9748
9749 -- target-type (integer(opnd1) + integer(opnd2))
9750
9751 -- We do this because we are always allowed to compute in a larger type
9752 -- if we do the right thing with the result, and in this case we are
9753 -- going to do a conversion which will do an appropriate check to make
9754 -- sure that things are in range of the target type in any case. This
9755 -- avoids some unnecessary intermediate overflows.
9756
9757 -- We might consider a similar transformation in the case where the
9758 -- target is a real type or a 64-bit integer type, and the operand
9759 -- is an arithmetic operation using a 32-bit integer type. However,
9760 -- we do not bother with this case, because it could cause significant
9761 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
9762 -- much cheaper, but we don't want different behavior on 32-bit and
9763 -- 64-bit machines. Note that the exclusion of the 64-bit case also
9764 -- handles the configurable run-time cases where 64-bit arithmetic
9765 -- may simply be unavailable.
9766
9767 -- Note: this circuit is partially redundant with respect to the circuit
9768 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
9769 -- the processing here. Also we still need the Checks circuit, since we
9770 -- have to be sure not to generate junk overflow checks in the first
9771 -- place, since it would be trick to remove them here!
9772
9773 if Integer_Promotion_Possible (N) then
9774
9775 -- All conditions met, go ahead with transformation
9776
9777 declare
9778 Opnd : Node_Id;
9779 L, R : Node_Id;
9780
9781 begin
9782 R :=
9783 Make_Type_Conversion (Loc,
9784 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
9785 Expression => Relocate_Node (Right_Opnd (Operand)));
9786
9787 Opnd := New_Op_Node (Nkind (Operand), Loc);
9788 Set_Right_Opnd (Opnd, R);
9789
9790 if Nkind (Operand) in N_Binary_Op then
9791 L :=
9792 Make_Type_Conversion (Loc,
9793 Subtype_Mark => New_Reference_To (Standard_Integer, Loc),
9794 Expression => Relocate_Node (Left_Opnd (Operand)));
9795
9796 Set_Left_Opnd (Opnd, L);
9797 end if;
9798
9799 Rewrite (N,
9800 Make_Type_Conversion (Loc,
9801 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
9802 Expression => Opnd));
9803
9804 Analyze_And_Resolve (N, Target_Type);
9805 goto Done;
9806 end;
9807 end if;
9808
9809 -- Do validity check if validity checking operands
9810
9811 if Validity_Checks_On
9812 and then Validity_Check_Operands
9813 then
9814 Ensure_Valid (Operand);
9815 end if;
9816
9817 -- Special case of converting from non-standard boolean type
9818
9819 if Is_Boolean_Type (Operand_Type)
9820 and then (Nonzero_Is_True (Operand_Type))
9821 then
9822 Adjust_Condition (Operand);
9823 Set_Etype (Operand, Standard_Boolean);
9824 Operand_Type := Standard_Boolean;
9825 end if;
9826
9827 -- Case of converting to an access type
9828
9829 if Is_Access_Type (Target_Type) then
9830
9831 -- Apply an accessibility check when the conversion operand is an
9832 -- access parameter (or a renaming thereof), unless conversion was
9833 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
9834 -- Note that other checks may still need to be applied below (such
9835 -- as tagged type checks).
9836
9837 if Is_Entity_Name (Operand)
9838 and then Has_Extra_Accessibility (Entity (Operand))
9839 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
9840 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
9841 or else Attribute_Name (Original_Node (N)) = Name_Access)
9842 then
9843 Apply_Accessibility_Check
9844 (Operand, Target_Type, Insert_Node => Operand);
9845
9846 -- If the level of the operand type is statically deeper than the
9847 -- level of the target type, then force Program_Error. Note that this
9848 -- can only occur for cases where the attribute is within the body of
9849 -- an instantiation (otherwise the conversion will already have been
9850 -- rejected as illegal). Note: warnings are issued by the analyzer
9851 -- for the instance cases.
9852
9853 elsif In_Instance_Body
9854 and then Type_Access_Level (Operand_Type) >
9855 Type_Access_Level (Target_Type)
9856 then
9857 Raise_Accessibility_Error;
9858
9859 -- When the operand is a selected access discriminant the check needs
9860 -- to be made against the level of the object denoted by the prefix
9861 -- of the selected name. Force Program_Error for this case as well
9862 -- (this accessibility violation can only happen if within the body
9863 -- of an instantiation).
9864
9865 elsif In_Instance_Body
9866 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
9867 and then Nkind (Operand) = N_Selected_Component
9868 and then Object_Access_Level (Operand) >
9869 Type_Access_Level (Target_Type)
9870 then
9871 Raise_Accessibility_Error;
9872 goto Done;
9873 end if;
9874 end if;
9875
9876 -- Case of conversions of tagged types and access to tagged types
9877
9878 -- When needed, that is to say when the expression is class-wide, Add
9879 -- runtime a tag check for (strict) downward conversion by using the
9880 -- membership test, generating:
9881
9882 -- [constraint_error when Operand not in Target_Type'Class]
9883
9884 -- or in the access type case
9885
9886 -- [constraint_error
9887 -- when Operand /= null
9888 -- and then Operand.all not in
9889 -- Designated_Type (Target_Type)'Class]
9890
9891 if (Is_Access_Type (Target_Type)
9892 and then Is_Tagged_Type (Designated_Type (Target_Type)))
9893 or else Is_Tagged_Type (Target_Type)
9894 then
9895 -- Do not do any expansion in the access type case if the parent is a
9896 -- renaming, since this is an error situation which will be caught by
9897 -- Sem_Ch8, and the expansion can interfere with this error check.
9898
9899 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
9900 goto Done;
9901 end if;
9902
9903 -- Otherwise, proceed with processing tagged conversion
9904
9905 Tagged_Conversion : declare
9906 Actual_Op_Typ : Entity_Id;
9907 Actual_Targ_Typ : Entity_Id;
9908 Make_Conversion : Boolean := False;
9909 Root_Op_Typ : Entity_Id;
9910
9911 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
9912 -- Create a membership check to test whether Operand is a member
9913 -- of Targ_Typ. If the original Target_Type is an access, include
9914 -- a test for null value. The check is inserted at N.
9915
9916 --------------------
9917 -- Make_Tag_Check --
9918 --------------------
9919
9920 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
9921 Cond : Node_Id;
9922
9923 begin
9924 -- Generate:
9925 -- [Constraint_Error
9926 -- when Operand /= null
9927 -- and then Operand.all not in Targ_Typ]
9928
9929 if Is_Access_Type (Target_Type) then
9930 Cond :=
9931 Make_And_Then (Loc,
9932 Left_Opnd =>
9933 Make_Op_Ne (Loc,
9934 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
9935 Right_Opnd => Make_Null (Loc)),
9936
9937 Right_Opnd =>
9938 Make_Not_In (Loc,
9939 Left_Opnd =>
9940 Make_Explicit_Dereference (Loc,
9941 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
9942 Right_Opnd => New_Reference_To (Targ_Typ, Loc)));
9943
9944 -- Generate:
9945 -- [Constraint_Error when Operand not in Targ_Typ]
9946
9947 else
9948 Cond :=
9949 Make_Not_In (Loc,
9950 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
9951 Right_Opnd => New_Reference_To (Targ_Typ, Loc));
9952 end if;
9953
9954 Insert_Action (N,
9955 Make_Raise_Constraint_Error (Loc,
9956 Condition => Cond,
9957 Reason => CE_Tag_Check_Failed));
9958 end Make_Tag_Check;
9959
9960 -- Start of processing for Tagged_Conversion
9961
9962 begin
9963 -- Handle entities from the limited view
9964
9965 if Is_Access_Type (Operand_Type) then
9966 Actual_Op_Typ :=
9967 Available_View (Designated_Type (Operand_Type));
9968 else
9969 Actual_Op_Typ := Operand_Type;
9970 end if;
9971
9972 if Is_Access_Type (Target_Type) then
9973 Actual_Targ_Typ :=
9974 Available_View (Designated_Type (Target_Type));
9975 else
9976 Actual_Targ_Typ := Target_Type;
9977 end if;
9978
9979 Root_Op_Typ := Root_Type (Actual_Op_Typ);
9980
9981 -- Ada 2005 (AI-251): Handle interface type conversion
9982
9983 if Is_Interface (Actual_Op_Typ) then
9984 Expand_Interface_Conversion (N, Is_Static => False);
9985 goto Done;
9986 end if;
9987
9988 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
9989
9990 -- Create a runtime tag check for a downward class-wide type
9991 -- conversion.
9992
9993 if Is_Class_Wide_Type (Actual_Op_Typ)
9994 and then Actual_Op_Typ /= Actual_Targ_Typ
9995 and then Root_Op_Typ /= Actual_Targ_Typ
9996 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
9997 Use_Full_View => True)
9998 then
9999 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10000 Make_Conversion := True;
10001 end if;
10002
10003 -- AI05-0073: If the result subtype of the function is defined
10004 -- by an access_definition designating a specific tagged type
10005 -- T, a check is made that the result value is null or the tag
10006 -- of the object designated by the result value identifies T.
10007 -- Constraint_Error is raised if this check fails.
10008
10009 if Nkind (Parent (N)) = Sinfo.N_Return_Statement then
10010 declare
10011 Func : Entity_Id;
10012 Func_Typ : Entity_Id;
10013
10014 begin
10015 -- Climb scope stack looking for the enclosing function
10016
10017 Func := Current_Scope;
10018 while Present (Func)
10019 and then Ekind (Func) /= E_Function
10020 loop
10021 Func := Scope (Func);
10022 end loop;
10023
10024 -- The function's return subtype must be defined using
10025 -- an access definition.
10026
10027 if Nkind (Result_Definition (Parent (Func))) =
10028 N_Access_Definition
10029 then
10030 Func_Typ := Directly_Designated_Type (Etype (Func));
10031
10032 -- The return subtype denotes a specific tagged type,
10033 -- in other words, a non class-wide type.
10034
10035 if Is_Tagged_Type (Func_Typ)
10036 and then not Is_Class_Wide_Type (Func_Typ)
10037 then
10038 Make_Tag_Check (Actual_Targ_Typ);
10039 Make_Conversion := True;
10040 end if;
10041 end if;
10042 end;
10043 end if;
10044
10045 -- We have generated a tag check for either a class-wide type
10046 -- conversion or for AI05-0073.
10047
10048 if Make_Conversion then
10049 declare
10050 Conv : Node_Id;
10051 begin
10052 Conv :=
10053 Make_Unchecked_Type_Conversion (Loc,
10054 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10055 Expression => Relocate_Node (Expression (N)));
10056 Rewrite (N, Conv);
10057 Analyze_And_Resolve (N, Target_Type);
10058 end;
10059 end if;
10060 end if;
10061 end Tagged_Conversion;
10062
10063 -- Case of other access type conversions
10064
10065 elsif Is_Access_Type (Target_Type) then
10066 Apply_Constraint_Check (Operand, Target_Type);
10067
10068 -- Case of conversions from a fixed-point type
10069
10070 -- These conversions require special expansion and processing, found in
10071 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10072 -- since from a semantic point of view, these are simple integer
10073 -- conversions, which do not need further processing.
10074
10075 elsif Is_Fixed_Point_Type (Operand_Type)
10076 and then not Conversion_OK (N)
10077 then
10078 -- We should never see universal fixed at this case, since the
10079 -- expansion of the constituent divide or multiply should have
10080 -- eliminated the explicit mention of universal fixed.
10081
10082 pragma Assert (Operand_Type /= Universal_Fixed);
10083
10084 -- Check for special case of the conversion to universal real that
10085 -- occurs as a result of the use of a round attribute. In this case,
10086 -- the real type for the conversion is taken from the target type of
10087 -- the Round attribute and the result must be marked as rounded.
10088
10089 if Target_Type = Universal_Real
10090 and then Nkind (Parent (N)) = N_Attribute_Reference
10091 and then Attribute_Name (Parent (N)) = Name_Round
10092 then
10093 Set_Rounded_Result (N);
10094 Set_Etype (N, Etype (Parent (N)));
10095 end if;
10096
10097 -- Otherwise do correct fixed-conversion, but skip these if the
10098 -- Conversion_OK flag is set, because from a semantic point of view
10099 -- these are simple integer conversions needing no further processing
10100 -- (the backend will simply treat them as integers).
10101
10102 if not Conversion_OK (N) then
10103 if Is_Fixed_Point_Type (Etype (N)) then
10104 Expand_Convert_Fixed_To_Fixed (N);
10105 Real_Range_Check;
10106
10107 elsif Is_Integer_Type (Etype (N)) then
10108 Expand_Convert_Fixed_To_Integer (N);
10109
10110 else
10111 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10112 Expand_Convert_Fixed_To_Float (N);
10113 Real_Range_Check;
10114 end if;
10115 end if;
10116
10117 -- Case of conversions to a fixed-point type
10118
10119 -- These conversions require special expansion and processing, found in
10120 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10121 -- since from a semantic point of view, these are simple integer
10122 -- conversions, which do not need further processing.
10123
10124 elsif Is_Fixed_Point_Type (Target_Type)
10125 and then not Conversion_OK (N)
10126 then
10127 if Is_Integer_Type (Operand_Type) then
10128 Expand_Convert_Integer_To_Fixed (N);
10129 Real_Range_Check;
10130 else
10131 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10132 Expand_Convert_Float_To_Fixed (N);
10133 Real_Range_Check;
10134 end if;
10135
10136 -- Case of float-to-integer conversions
10137
10138 -- We also handle float-to-fixed conversions with Conversion_OK set
10139 -- since semantically the fixed-point target is treated as though it
10140 -- were an integer in such cases.
10141
10142 elsif Is_Floating_Point_Type (Operand_Type)
10143 and then
10144 (Is_Integer_Type (Target_Type)
10145 or else
10146 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10147 then
10148 -- One more check here, gcc is still not able to do conversions of
10149 -- this type with proper overflow checking, and so gigi is doing an
10150 -- approximation of what is required by doing floating-point compares
10151 -- with the end-point. But that can lose precision in some cases, and
10152 -- give a wrong result. Converting the operand to Universal_Real is
10153 -- helpful, but still does not catch all cases with 64-bit integers
10154 -- on targets with only 64-bit floats.
10155
10156 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10157 -- Can this code be removed ???
10158
10159 if Do_Range_Check (Operand) then
10160 Rewrite (Operand,
10161 Make_Type_Conversion (Loc,
10162 Subtype_Mark =>
10163 New_Occurrence_Of (Universal_Real, Loc),
10164 Expression =>
10165 Relocate_Node (Operand)));
10166
10167 Set_Etype (Operand, Universal_Real);
10168 Enable_Range_Check (Operand);
10169 Set_Do_Range_Check (Expression (Operand), False);
10170 end if;
10171
10172 -- Case of array conversions
10173
10174 -- Expansion of array conversions, add required length/range checks but
10175 -- only do this if there is no change of representation. For handling of
10176 -- this case, see Handle_Changed_Representation.
10177
10178 elsif Is_Array_Type (Target_Type) then
10179 if Is_Constrained (Target_Type) then
10180 Apply_Length_Check (Operand, Target_Type);
10181 else
10182 Apply_Range_Check (Operand, Target_Type);
10183 end if;
10184
10185 Handle_Changed_Representation;
10186
10187 -- Case of conversions of discriminated types
10188
10189 -- Add required discriminant checks if target is constrained. Again this
10190 -- change is skipped if we have a change of representation.
10191
10192 elsif Has_Discriminants (Target_Type)
10193 and then Is_Constrained (Target_Type)
10194 then
10195 Apply_Discriminant_Check (Operand, Target_Type);
10196 Handle_Changed_Representation;
10197
10198 -- Case of all other record conversions. The only processing required
10199 -- is to check for a change of representation requiring the special
10200 -- assignment processing.
10201
10202 elsif Is_Record_Type (Target_Type) then
10203
10204 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10205 -- a derived Unchecked_Union type to an unconstrained type that is
10206 -- not Unchecked_Union if the operand lacks inferable discriminants.
10207
10208 if Is_Derived_Type (Operand_Type)
10209 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10210 and then not Is_Constrained (Target_Type)
10211 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10212 and then not Has_Inferable_Discriminants (Operand)
10213 then
10214 -- To prevent Gigi from generating illegal code, we generate a
10215 -- Program_Error node, but we give it the target type of the
10216 -- conversion (is this requirement documented somewhere ???)
10217
10218 declare
10219 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10220 Reason => PE_Unchecked_Union_Restriction);
10221
10222 begin
10223 Set_Etype (PE, Target_Type);
10224 Rewrite (N, PE);
10225
10226 end;
10227 else
10228 Handle_Changed_Representation;
10229 end if;
10230
10231 -- Case of conversions of enumeration types
10232
10233 elsif Is_Enumeration_Type (Target_Type) then
10234
10235 -- Special processing is required if there is a change of
10236 -- representation (from enumeration representation clauses).
10237
10238 if not Same_Representation (Target_Type, Operand_Type) then
10239
10240 -- Convert: x(y) to x'val (ytyp'val (y))
10241
10242 Rewrite (N,
10243 Make_Attribute_Reference (Loc,
10244 Prefix => New_Occurrence_Of (Target_Type, Loc),
10245 Attribute_Name => Name_Val,
10246 Expressions => New_List (
10247 Make_Attribute_Reference (Loc,
10248 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10249 Attribute_Name => Name_Pos,
10250 Expressions => New_List (Operand)))));
10251
10252 Analyze_And_Resolve (N, Target_Type);
10253 end if;
10254
10255 -- Case of conversions to floating-point
10256
10257 elsif Is_Floating_Point_Type (Target_Type) then
10258 Real_Range_Check;
10259 end if;
10260
10261 -- At this stage, either the conversion node has been transformed into
10262 -- some other equivalent expression, or left as a conversion that can be
10263 -- handled by Gigi, in the following cases:
10264
10265 -- Conversions with no change of representation or type
10266
10267 -- Numeric conversions involving integer, floating- and fixed-point
10268 -- values. Fixed-point values are allowed only if Conversion_OK is
10269 -- set, i.e. if the fixed-point values are to be treated as integers.
10270
10271 -- No other conversions should be passed to Gigi
10272
10273 -- Check: are these rules stated in sinfo??? if so, why restate here???
10274
10275 -- The only remaining step is to generate a range check if we still have
10276 -- a type conversion at this stage and Do_Range_Check is set. For now we
10277 -- do this only for conversions of discrete types.
10278
10279 if Nkind (N) = N_Type_Conversion
10280 and then Is_Discrete_Type (Etype (N))
10281 then
10282 declare
10283 Expr : constant Node_Id := Expression (N);
10284 Ftyp : Entity_Id;
10285 Ityp : Entity_Id;
10286
10287 begin
10288 if Do_Range_Check (Expr)
10289 and then Is_Discrete_Type (Etype (Expr))
10290 then
10291 Set_Do_Range_Check (Expr, False);
10292
10293 -- Before we do a range check, we have to deal with treating a
10294 -- fixed-point operand as an integer. The way we do this is
10295 -- simply to do an unchecked conversion to an appropriate
10296 -- integer type large enough to hold the result.
10297
10298 -- This code is not active yet, because we are only dealing
10299 -- with discrete types so far ???
10300
10301 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10302 and then Treat_Fixed_As_Integer (Expr)
10303 then
10304 Ftyp := Base_Type (Etype (Expr));
10305
10306 if Esize (Ftyp) >= Esize (Standard_Integer) then
10307 Ityp := Standard_Long_Long_Integer;
10308 else
10309 Ityp := Standard_Integer;
10310 end if;
10311
10312 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10313 end if;
10314
10315 -- Reset overflow flag, since the range check will include
10316 -- dealing with possible overflow, and generate the check. If
10317 -- Address is either a source type or target type, suppress
10318 -- range check to avoid typing anomalies when it is a visible
10319 -- integer type.
10320
10321 Set_Do_Overflow_Check (N, False);
10322 if not Is_Descendent_Of_Address (Etype (Expr))
10323 and then not Is_Descendent_Of_Address (Target_Type)
10324 then
10325 Generate_Range_Check
10326 (Expr, Target_Type, CE_Range_Check_Failed);
10327 end if;
10328 end if;
10329 end;
10330 end if;
10331
10332 -- Final step, if the result is a type conversion involving Vax_Float
10333 -- types, then it is subject for further special processing.
10334
10335 if Nkind (N) = N_Type_Conversion
10336 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
10337 then
10338 Expand_Vax_Conversion (N);
10339 goto Done;
10340 end if;
10341
10342 -- Here at end of processing
10343
10344 <<Done>>
10345 -- Apply predicate check if required. Note that we can't just call
10346 -- Apply_Predicate_Check here, because the type looks right after
10347 -- the conversion and it would omit the check. The Comes_From_Source
10348 -- guard is necessary to prevent infinite recursions when we generate
10349 -- internal conversions for the purpose of checking predicates.
10350
10351 if Present (Predicate_Function (Target_Type))
10352 and then Target_Type /= Operand_Type
10353 and then Comes_From_Source (N)
10354 then
10355 declare
10356 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10357
10358 begin
10359 -- Avoid infinite recursion on the subsequent expansion of
10360 -- of the copy of the original type conversion.
10361
10362 Set_Comes_From_Source (New_Expr, False);
10363 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10364 end;
10365 end if;
10366 end Expand_N_Type_Conversion;
10367
10368 -----------------------------------
10369 -- Expand_N_Unchecked_Expression --
10370 -----------------------------------
10371
10372 -- Remove the unchecked expression node from the tree. Its job was simply
10373 -- to make sure that its constituent expression was handled with checks
10374 -- off, and now that that is done, we can remove it from the tree, and
10375 -- indeed must, since Gigi does not expect to see these nodes.
10376
10377 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10378 Exp : constant Node_Id := Expression (N);
10379 begin
10380 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
10381 Rewrite (N, Exp);
10382 end Expand_N_Unchecked_Expression;
10383
10384 ----------------------------------------
10385 -- Expand_N_Unchecked_Type_Conversion --
10386 ----------------------------------------
10387
10388 -- If this cannot be handled by Gigi and we haven't already made a
10389 -- temporary for it, do it now.
10390
10391 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
10392 Target_Type : constant Entity_Id := Etype (N);
10393 Operand : constant Node_Id := Expression (N);
10394 Operand_Type : constant Entity_Id := Etype (Operand);
10395
10396 begin
10397 -- Nothing at all to do if conversion is to the identical type so remove
10398 -- the conversion completely, it is useless, except that it may carry
10399 -- an Assignment_OK indication which must be propagated to the operand.
10400
10401 if Operand_Type = Target_Type then
10402
10403 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10404
10405 if Assignment_OK (N) then
10406 Set_Assignment_OK (Operand);
10407 end if;
10408
10409 Rewrite (N, Relocate_Node (Operand));
10410 return;
10411 end if;
10412
10413 -- If we have a conversion of a compile time known value to a target
10414 -- type and the value is in range of the target type, then we can simply
10415 -- replace the construct by an integer literal of the correct type. We
10416 -- only apply this to integer types being converted. Possibly it may
10417 -- apply in other cases, but it is too much trouble to worry about.
10418
10419 -- Note that we do not do this transformation if the Kill_Range_Check
10420 -- flag is set, since then the value may be outside the expected range.
10421 -- This happens in the Normalize_Scalars case.
10422
10423 -- We also skip this if either the target or operand type is biased
10424 -- because in this case, the unchecked conversion is supposed to
10425 -- preserve the bit pattern, not the integer value.
10426
10427 if Is_Integer_Type (Target_Type)
10428 and then not Has_Biased_Representation (Target_Type)
10429 and then Is_Integer_Type (Operand_Type)
10430 and then not Has_Biased_Representation (Operand_Type)
10431 and then Compile_Time_Known_Value (Operand)
10432 and then not Kill_Range_Check (N)
10433 then
10434 declare
10435 Val : constant Uint := Expr_Value (Operand);
10436
10437 begin
10438 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
10439 and then
10440 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
10441 and then
10442 Val >= Expr_Value (Type_Low_Bound (Target_Type))
10443 and then
10444 Val <= Expr_Value (Type_High_Bound (Target_Type))
10445 then
10446 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
10447
10448 -- If Address is the target type, just set the type to avoid a
10449 -- spurious type error on the literal when Address is a visible
10450 -- integer type.
10451
10452 if Is_Descendent_Of_Address (Target_Type) then
10453 Set_Etype (N, Target_Type);
10454 else
10455 Analyze_And_Resolve (N, Target_Type);
10456 end if;
10457
10458 return;
10459 end if;
10460 end;
10461 end if;
10462
10463 -- Nothing to do if conversion is safe
10464
10465 if Safe_Unchecked_Type_Conversion (N) then
10466 return;
10467 end if;
10468
10469 -- Otherwise force evaluation unless Assignment_OK flag is set (this
10470 -- flag indicates ??? -- more comments needed here)
10471
10472 if Assignment_OK (N) then
10473 null;
10474 else
10475 Force_Evaluation (N);
10476 end if;
10477 end Expand_N_Unchecked_Type_Conversion;
10478
10479 ----------------------------
10480 -- Expand_Record_Equality --
10481 ----------------------------
10482
10483 -- For non-variant records, Equality is expanded when needed into:
10484
10485 -- and then Lhs.Discr1 = Rhs.Discr1
10486 -- and then ...
10487 -- and then Lhs.Discrn = Rhs.Discrn
10488 -- and then Lhs.Cmp1 = Rhs.Cmp1
10489 -- and then ...
10490 -- and then Lhs.Cmpn = Rhs.Cmpn
10491
10492 -- The expression is folded by the back-end for adjacent fields. This
10493 -- function is called for tagged record in only one occasion: for imple-
10494 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
10495 -- otherwise the primitive "=" is used directly.
10496
10497 function Expand_Record_Equality
10498 (Nod : Node_Id;
10499 Typ : Entity_Id;
10500 Lhs : Node_Id;
10501 Rhs : Node_Id;
10502 Bodies : List_Id) return Node_Id
10503 is
10504 Loc : constant Source_Ptr := Sloc (Nod);
10505
10506 Result : Node_Id;
10507 C : Entity_Id;
10508
10509 First_Time : Boolean := True;
10510
10511 function Suitable_Element (C : Entity_Id) return Entity_Id;
10512 -- Return the first field to compare beginning with C, skipping the
10513 -- inherited components.
10514
10515 ----------------------
10516 -- Suitable_Element --
10517 ----------------------
10518
10519 function Suitable_Element (C : Entity_Id) return Entity_Id is
10520 begin
10521 if No (C) then
10522 return Empty;
10523
10524 elsif Ekind (C) /= E_Discriminant
10525 and then Ekind (C) /= E_Component
10526 then
10527 return Suitable_Element (Next_Entity (C));
10528
10529 elsif Is_Tagged_Type (Typ)
10530 and then C /= Original_Record_Component (C)
10531 then
10532 return Suitable_Element (Next_Entity (C));
10533
10534 elsif Chars (C) = Name_uTag then
10535 return Suitable_Element (Next_Entity (C));
10536
10537 -- The .NET/JVM version of type Root_Controlled contains two fields
10538 -- which should not be considered part of the object. To achieve
10539 -- proper equiality between two controlled objects on .NET/JVM, skip
10540 -- field _parent whenever it is of type Root_Controlled.
10541
10542 elsif Chars (C) = Name_uParent
10543 and then VM_Target /= No_VM
10544 and then Etype (C) = RTE (RE_Root_Controlled)
10545 then
10546 return Suitable_Element (Next_Entity (C));
10547
10548 elsif Is_Interface (Etype (C)) then
10549 return Suitable_Element (Next_Entity (C));
10550
10551 else
10552 return C;
10553 end if;
10554 end Suitable_Element;
10555
10556 -- Start of processing for Expand_Record_Equality
10557
10558 begin
10559 -- Generates the following code: (assuming that Typ has one Discr and
10560 -- component C2 is also a record)
10561
10562 -- True
10563 -- and then Lhs.Discr1 = Rhs.Discr1
10564 -- and then Lhs.C1 = Rhs.C1
10565 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
10566 -- and then ...
10567 -- and then Lhs.Cmpn = Rhs.Cmpn
10568
10569 Result := New_Reference_To (Standard_True, Loc);
10570 C := Suitable_Element (First_Entity (Typ));
10571 while Present (C) loop
10572 declare
10573 New_Lhs : Node_Id;
10574 New_Rhs : Node_Id;
10575 Check : Node_Id;
10576
10577 begin
10578 if First_Time then
10579 First_Time := False;
10580 New_Lhs := Lhs;
10581 New_Rhs := Rhs;
10582 else
10583 New_Lhs := New_Copy_Tree (Lhs);
10584 New_Rhs := New_Copy_Tree (Rhs);
10585 end if;
10586
10587 Check :=
10588 Expand_Composite_Equality (Nod, Etype (C),
10589 Lhs =>
10590 Make_Selected_Component (Loc,
10591 Prefix => New_Lhs,
10592 Selector_Name => New_Reference_To (C, Loc)),
10593 Rhs =>
10594 Make_Selected_Component (Loc,
10595 Prefix => New_Rhs,
10596 Selector_Name => New_Reference_To (C, Loc)),
10597 Bodies => Bodies);
10598
10599 -- If some (sub)component is an unchecked_union, the whole
10600 -- operation will raise program error.
10601
10602 if Nkind (Check) = N_Raise_Program_Error then
10603 Result := Check;
10604 Set_Etype (Result, Standard_Boolean);
10605 exit;
10606 else
10607 Result :=
10608 Make_And_Then (Loc,
10609 Left_Opnd => Result,
10610 Right_Opnd => Check);
10611 end if;
10612 end;
10613
10614 C := Suitable_Element (Next_Entity (C));
10615 end loop;
10616
10617 return Result;
10618 end Expand_Record_Equality;
10619
10620 ---------------------------
10621 -- Expand_Set_Membership --
10622 ---------------------------
10623
10624 procedure Expand_Set_Membership (N : Node_Id) is
10625 Lop : constant Node_Id := Left_Opnd (N);
10626 Alt : Node_Id;
10627 Res : Node_Id;
10628
10629 function Make_Cond (Alt : Node_Id) return Node_Id;
10630 -- If the alternative is a subtype mark, create a simple membership
10631 -- test. Otherwise create an equality test for it.
10632
10633 ---------------
10634 -- Make_Cond --
10635 ---------------
10636
10637 function Make_Cond (Alt : Node_Id) return Node_Id is
10638 Cond : Node_Id;
10639 L : constant Node_Id := New_Copy (Lop);
10640 R : constant Node_Id := Relocate_Node (Alt);
10641
10642 begin
10643 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
10644 or else Nkind (Alt) = N_Range
10645 then
10646 Cond :=
10647 Make_In (Sloc (Alt),
10648 Left_Opnd => L,
10649 Right_Opnd => R);
10650 else
10651 Cond :=
10652 Make_Op_Eq (Sloc (Alt),
10653 Left_Opnd => L,
10654 Right_Opnd => R);
10655 end if;
10656
10657 return Cond;
10658 end Make_Cond;
10659
10660 -- Start of processing for Expand_Set_Membership
10661
10662 begin
10663 Remove_Side_Effects (Lop);
10664
10665 Alt := Last (Alternatives (N));
10666 Res := Make_Cond (Alt);
10667
10668 Prev (Alt);
10669 while Present (Alt) loop
10670 Res :=
10671 Make_Or_Else (Sloc (Alt),
10672 Left_Opnd => Make_Cond (Alt),
10673 Right_Opnd => Res);
10674 Prev (Alt);
10675 end loop;
10676
10677 Rewrite (N, Res);
10678 Analyze_And_Resolve (N, Standard_Boolean);
10679 end Expand_Set_Membership;
10680
10681 -----------------------------------
10682 -- Expand_Short_Circuit_Operator --
10683 -----------------------------------
10684
10685 -- Deal with special expansion if actions are present for the right operand
10686 -- and deal with optimizing case of arguments being True or False. We also
10687 -- deal with the special case of non-standard boolean values.
10688
10689 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
10690 Loc : constant Source_Ptr := Sloc (N);
10691 Typ : constant Entity_Id := Etype (N);
10692 Left : constant Node_Id := Left_Opnd (N);
10693 Right : constant Node_Id := Right_Opnd (N);
10694 LocR : constant Source_Ptr := Sloc (Right);
10695 Actlist : List_Id;
10696
10697 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
10698 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
10699 -- If Left = Shortcut_Value then Right need not be evaluated
10700
10701 function Make_Test_Expr (Opnd : Node_Id) return Node_Id;
10702 -- For Opnd a boolean expression, return a Boolean expression equivalent
10703 -- to Opnd /= Shortcut_Value.
10704
10705 --------------------
10706 -- Make_Test_Expr --
10707 --------------------
10708
10709 function Make_Test_Expr (Opnd : Node_Id) return Node_Id is
10710 begin
10711 if Shortcut_Value then
10712 return Make_Op_Not (Sloc (Opnd), Opnd);
10713 else
10714 return Opnd;
10715 end if;
10716 end Make_Test_Expr;
10717
10718 Op_Var : Entity_Id;
10719 -- Entity for a temporary variable holding the value of the operator,
10720 -- used for expansion in the case where actions are present.
10721
10722 -- Start of processing for Expand_Short_Circuit_Operator
10723
10724 begin
10725 -- Deal with non-standard booleans
10726
10727 if Is_Boolean_Type (Typ) then
10728 Adjust_Condition (Left);
10729 Adjust_Condition (Right);
10730 Set_Etype (N, Standard_Boolean);
10731 end if;
10732
10733 -- Check for cases where left argument is known to be True or False
10734
10735 if Compile_Time_Known_Value (Left) then
10736
10737 -- Mark SCO for left condition as compile time known
10738
10739 if Generate_SCO and then Comes_From_Source (Left) then
10740 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
10741 end if;
10742
10743 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
10744 -- Any actions associated with Right will be executed unconditionally
10745 -- and can thus be inserted into the tree unconditionally.
10746
10747 if Expr_Value_E (Left) /= Shortcut_Ent then
10748 if Present (Actions (N)) then
10749 Insert_Actions (N, Actions (N));
10750 end if;
10751
10752 Rewrite (N, Right);
10753
10754 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
10755 -- In this case we can forget the actions associated with Right,
10756 -- since they will never be executed.
10757
10758 else
10759 Kill_Dead_Code (Right);
10760 Kill_Dead_Code (Actions (N));
10761 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
10762 end if;
10763
10764 Adjust_Result_Type (N, Typ);
10765 return;
10766 end if;
10767
10768 -- If Actions are present for the right operand, we have to do some
10769 -- special processing. We can't just let these actions filter back into
10770 -- code preceding the short circuit (which is what would have happened
10771 -- if we had not trapped them in the short-circuit form), since they
10772 -- must only be executed if the right operand of the short circuit is
10773 -- executed and not otherwise.
10774
10775 -- the temporary variable C.
10776
10777 if Present (Actions (N)) then
10778 Actlist := Actions (N);
10779
10780 -- The old approach is to expand:
10781
10782 -- left AND THEN right
10783
10784 -- into
10785
10786 -- C : Boolean := False;
10787 -- IF left THEN
10788 -- Actions;
10789 -- IF right THEN
10790 -- C := True;
10791 -- END IF;
10792 -- END IF;
10793
10794 -- and finally rewrite the operator into a reference to C. Similarly
10795 -- for left OR ELSE right, with negated values. Note that this
10796 -- rewrite causes some difficulties for coverage analysis because
10797 -- of the introduction of the new variable C, which obscures the
10798 -- structure of the test.
10799
10800 -- We use this "old approach" if use of N_Expression_With_Actions
10801 -- is False (see description in Opt of when this is or is not set).
10802
10803 if not Use_Expression_With_Actions then
10804 Op_Var := Make_Temporary (Loc, 'C', Related_Node => N);
10805
10806 Insert_Action (N,
10807 Make_Object_Declaration (Loc,
10808 Defining_Identifier =>
10809 Op_Var,
10810 Object_Definition =>
10811 New_Occurrence_Of (Standard_Boolean, Loc),
10812 Expression =>
10813 New_Occurrence_Of (Shortcut_Ent, Loc)));
10814
10815 Append_To (Actlist,
10816 Make_Implicit_If_Statement (Right,
10817 Condition => Make_Test_Expr (Right),
10818 Then_Statements => New_List (
10819 Make_Assignment_Statement (LocR,
10820 Name => New_Occurrence_Of (Op_Var, LocR),
10821 Expression =>
10822 New_Occurrence_Of
10823 (Boolean_Literals (not Shortcut_Value), LocR)))));
10824
10825 Insert_Action (N,
10826 Make_Implicit_If_Statement (Left,
10827 Condition => Make_Test_Expr (Left),
10828 Then_Statements => Actlist));
10829
10830 Rewrite (N, New_Occurrence_Of (Op_Var, Loc));
10831 Analyze_And_Resolve (N, Standard_Boolean);
10832
10833 -- The new approach, activated for now by the use of debug flag
10834 -- -gnatd.X is to use the new Expression_With_Actions node for the
10835 -- right operand of the short-circuit form. This should solve the
10836 -- traceability problems for coverage analysis.
10837
10838 else
10839 Rewrite (Right,
10840 Make_Expression_With_Actions (LocR,
10841 Expression => Relocate_Node (Right),
10842 Actions => Actlist));
10843 Set_Actions (N, No_List);
10844 Analyze_And_Resolve (Right, Standard_Boolean);
10845 end if;
10846
10847 Adjust_Result_Type (N, Typ);
10848 return;
10849 end if;
10850
10851 -- No actions present, check for cases of right argument True/False
10852
10853 if Compile_Time_Known_Value (Right) then
10854
10855 -- Mark SCO for left condition as compile time known
10856
10857 if Generate_SCO and then Comes_From_Source (Right) then
10858 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
10859 end if;
10860
10861 -- Change (Left and then True), (Left or else False) to Left.
10862 -- Note that we know there are no actions associated with the right
10863 -- operand, since we just checked for this case above.
10864
10865 if Expr_Value_E (Right) /= Shortcut_Ent then
10866 Rewrite (N, Left);
10867
10868 -- Change (Left and then False), (Left or else True) to Right,
10869 -- making sure to preserve any side effects associated with the Left
10870 -- operand.
10871
10872 else
10873 Remove_Side_Effects (Left);
10874 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
10875 end if;
10876 end if;
10877
10878 Adjust_Result_Type (N, Typ);
10879 end Expand_Short_Circuit_Operator;
10880
10881 -------------------------------------
10882 -- Fixup_Universal_Fixed_Operation --
10883 -------------------------------------
10884
10885 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
10886 Conv : constant Node_Id := Parent (N);
10887
10888 begin
10889 -- We must have a type conversion immediately above us
10890
10891 pragma Assert (Nkind (Conv) = N_Type_Conversion);
10892
10893 -- Normally the type conversion gives our target type. The exception
10894 -- occurs in the case of the Round attribute, where the conversion
10895 -- will be to universal real, and our real type comes from the Round
10896 -- attribute (as well as an indication that we must round the result)
10897
10898 if Nkind (Parent (Conv)) = N_Attribute_Reference
10899 and then Attribute_Name (Parent (Conv)) = Name_Round
10900 then
10901 Set_Etype (N, Etype (Parent (Conv)));
10902 Set_Rounded_Result (N);
10903
10904 -- Normal case where type comes from conversion above us
10905
10906 else
10907 Set_Etype (N, Etype (Conv));
10908 end if;
10909 end Fixup_Universal_Fixed_Operation;
10910
10911 ---------------------------------
10912 -- Has_Inferable_Discriminants --
10913 ---------------------------------
10914
10915 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
10916
10917 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
10918 -- Determines whether the left-most prefix of a selected component is a
10919 -- formal parameter in a subprogram. Assumes N is a selected component.
10920
10921 --------------------------------
10922 -- Prefix_Is_Formal_Parameter --
10923 --------------------------------
10924
10925 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
10926 Sel_Comp : Node_Id;
10927
10928 begin
10929 -- Move to the left-most prefix by climbing up the tree
10930
10931 Sel_Comp := N;
10932 while Present (Parent (Sel_Comp))
10933 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
10934 loop
10935 Sel_Comp := Parent (Sel_Comp);
10936 end loop;
10937
10938 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
10939 end Prefix_Is_Formal_Parameter;
10940
10941 -- Start of processing for Has_Inferable_Discriminants
10942
10943 begin
10944 -- For selected components, the subtype of the selector must be a
10945 -- constrained Unchecked_Union. If the component is subject to a
10946 -- per-object constraint, then the enclosing object must have inferable
10947 -- discriminants.
10948
10949 if Nkind (N) = N_Selected_Component then
10950 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
10951
10952 -- A small hack. If we have a per-object constrained selected
10953 -- component of a formal parameter, return True since we do not
10954 -- know the actual parameter association yet.
10955
10956 if Prefix_Is_Formal_Parameter (N) then
10957 return True;
10958
10959 -- Otherwise, check the enclosing object and the selector
10960
10961 else
10962 return Has_Inferable_Discriminants (Prefix (N))
10963 and then Has_Inferable_Discriminants (Selector_Name (N));
10964 end if;
10965
10966 -- The call to Has_Inferable_Discriminants will determine whether
10967 -- the selector has a constrained Unchecked_Union nominal type.
10968
10969 else
10970 return Has_Inferable_Discriminants (Selector_Name (N));
10971 end if;
10972
10973 -- A qualified expression has inferable discriminants if its subtype
10974 -- mark is a constrained Unchecked_Union subtype.
10975
10976 elsif Nkind (N) = N_Qualified_Expression then
10977 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
10978 and then Is_Constrained (Etype (Subtype_Mark (N)));
10979
10980 -- For all other names, it is sufficient to have a constrained
10981 -- Unchecked_Union nominal subtype.
10982
10983 else
10984 return Is_Unchecked_Union (Base_Type (Etype (N)))
10985 and then Is_Constrained (Etype (N));
10986 end if;
10987 end Has_Inferable_Discriminants;
10988
10989 -------------------------------
10990 -- Insert_Dereference_Action --
10991 -------------------------------
10992
10993 procedure Insert_Dereference_Action (N : Node_Id) is
10994
10995 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
10996 -- Return true if type of P is derived from Checked_Pool;
10997
10998 -----------------------------
10999 -- Is_Checked_Storage_Pool --
11000 -----------------------------
11001
11002 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11003 T : Entity_Id;
11004
11005 begin
11006 if No (P) then
11007 return False;
11008 end if;
11009
11010 T := Etype (P);
11011 while T /= Etype (T) loop
11012 if Is_RTE (T, RE_Checked_Pool) then
11013 return True;
11014 else
11015 T := Etype (T);
11016 end if;
11017 end loop;
11018
11019 return False;
11020 end Is_Checked_Storage_Pool;
11021
11022 -- Local variables
11023
11024 Typ : constant Entity_Id := Etype (N);
11025 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11026 Loc : constant Source_Ptr := Sloc (N);
11027 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11028 Pnod : constant Node_Id := Parent (N);
11029
11030 Addr : Entity_Id;
11031 Alig : Entity_Id;
11032 Deref : Node_Id;
11033 Size : Entity_Id;
11034 Stmt : Node_Id;
11035
11036 -- Start of processing for Insert_Dereference_Action
11037
11038 begin
11039 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11040
11041 -- Do not re-expand a dereference which has already been processed by
11042 -- this routine.
11043
11044 if Has_Dereference_Action (Pnod) then
11045 return;
11046
11047 -- Do not perform this type of expansion for internally-generated
11048 -- dereferences.
11049
11050 elsif not Comes_From_Source (Original_Node (Pnod)) then
11051 return;
11052
11053 -- A dereference action is only applicable to objects which have been
11054 -- allocated on a checked pool.
11055
11056 elsif not Is_Checked_Storage_Pool (Pool) then
11057 return;
11058 end if;
11059
11060 -- Extract the address of the dereferenced object. Generate:
11061
11062 -- Addr : System.Address := <N>'Pool_Address;
11063
11064 Addr := Make_Temporary (Loc, 'P');
11065
11066 Insert_Action (N,
11067 Make_Object_Declaration (Loc,
11068 Defining_Identifier => Addr,
11069 Object_Definition =>
11070 New_Reference_To (RTE (RE_Address), Loc),
11071 Expression =>
11072 Make_Attribute_Reference (Loc,
11073 Prefix => Duplicate_Subexpr_Move_Checks (N),
11074 Attribute_Name => Name_Pool_Address)));
11075
11076 -- Calculate the size of the dereferenced object. Generate:
11077
11078 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11079
11080 Deref :=
11081 Make_Explicit_Dereference (Loc,
11082 Prefix => Duplicate_Subexpr_Move_Checks (N));
11083 Set_Has_Dereference_Action (Deref);
11084
11085 Size := Make_Temporary (Loc, 'S');
11086
11087 Insert_Action (N,
11088 Make_Object_Declaration (Loc,
11089 Defining_Identifier => Size,
11090
11091 Object_Definition =>
11092 New_Reference_To (RTE (RE_Storage_Count), Loc),
11093
11094 Expression =>
11095 Make_Op_Divide (Loc,
11096 Left_Opnd =>
11097 Make_Attribute_Reference (Loc,
11098 Prefix => Deref,
11099 Attribute_Name => Name_Size),
11100 Right_Opnd =>
11101 Make_Integer_Literal (Loc, System_Storage_Unit))));
11102
11103 -- Calculate the alignment of the dereferenced object. Generate:
11104 -- Alig : constant Storage_Count := <N>.all'Alignment;
11105
11106 Deref :=
11107 Make_Explicit_Dereference (Loc,
11108 Prefix => Duplicate_Subexpr_Move_Checks (N));
11109 Set_Has_Dereference_Action (Deref);
11110
11111 Alig := Make_Temporary (Loc, 'A');
11112
11113 Insert_Action (N,
11114 Make_Object_Declaration (Loc,
11115 Defining_Identifier => Alig,
11116 Object_Definition =>
11117 New_Reference_To (RTE (RE_Storage_Count), Loc),
11118 Expression =>
11119 Make_Attribute_Reference (Loc,
11120 Prefix => Deref,
11121 Attribute_Name => Name_Alignment)));
11122
11123 -- A dereference of a controlled object requires special processing. The
11124 -- finalization machinery requests additional space from the underlying
11125 -- pool to allocate and hide two pointers. As a result, a checked pool
11126 -- may mark the wrong memory as valid. Since checked pools do not have
11127 -- knowledge of hidden pointers, we have to bring the two pointers back
11128 -- in view in order to restore the original state of the object.
11129
11130 if Needs_Finalization (Desig) then
11131
11132 -- Adjust the address and size of the dereferenced object. Generate:
11133 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11134
11135 Stmt :=
11136 Make_Procedure_Call_Statement (Loc,
11137 Name =>
11138 New_Reference_To (RTE (RE_Adjust_Controlled_Dereference), Loc),
11139 Parameter_Associations => New_List (
11140 New_Reference_To (Addr, Loc),
11141 New_Reference_To (Size, Loc),
11142 New_Reference_To (Alig, Loc)));
11143
11144 -- Class-wide types complicate things because we cannot determine
11145 -- statically whether the actual object is truly controlled. We must
11146 -- generate a runtime check to detect this property. Generate:
11147 --
11148 -- if Needs_Finalization (<N>.all'Tag) then
11149 -- <Stmt>;
11150 -- end if;
11151
11152 if Is_Class_Wide_Type (Desig) then
11153 Deref :=
11154 Make_Explicit_Dereference (Loc,
11155 Prefix => Duplicate_Subexpr_Move_Checks (N));
11156 Set_Has_Dereference_Action (Deref);
11157
11158 Stmt :=
11159 Make_If_Statement (Loc,
11160 Condition =>
11161 Make_Function_Call (Loc,
11162 Name =>
11163 New_Reference_To (RTE (RE_Needs_Finalization), Loc),
11164 Parameter_Associations => New_List (
11165 Make_Attribute_Reference (Loc,
11166 Prefix => Deref,
11167 Attribute_Name => Name_Tag))),
11168 Then_Statements => New_List (Stmt));
11169 end if;
11170
11171 Insert_Action (N, Stmt);
11172 end if;
11173
11174 -- Generate:
11175 -- Dereference (Pool, Addr, Size, Alig);
11176
11177 Insert_Action (N,
11178 Make_Procedure_Call_Statement (Loc,
11179 Name =>
11180 New_Reference_To
11181 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11182 Parameter_Associations => New_List (
11183 New_Reference_To (Pool, Loc),
11184 New_Reference_To (Addr, Loc),
11185 New_Reference_To (Size, Loc),
11186 New_Reference_To (Alig, Loc))));
11187
11188 -- Mark the explicit dereference as processed to avoid potential
11189 -- infinite expansion.
11190
11191 Set_Has_Dereference_Action (Pnod);
11192
11193 exception
11194 when RE_Not_Available =>
11195 return;
11196 end Insert_Dereference_Action;
11197
11198 --------------------------------
11199 -- Integer_Promotion_Possible --
11200 --------------------------------
11201
11202 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11203 Operand : constant Node_Id := Expression (N);
11204 Operand_Type : constant Entity_Id := Etype (Operand);
11205 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11206
11207 begin
11208 pragma Assert (Nkind (N) = N_Type_Conversion);
11209
11210 return
11211
11212 -- We only do the transformation for source constructs. We assume
11213 -- that the expander knows what it is doing when it generates code.
11214
11215 Comes_From_Source (N)
11216
11217 -- If the operand type is Short_Integer or Short_Short_Integer,
11218 -- then we will promote to Integer, which is available on all
11219 -- targets, and is sufficient to ensure no intermediate overflow.
11220 -- Furthermore it is likely to be as efficient or more efficient
11221 -- than using the smaller type for the computation so we do this
11222 -- unconditionally.
11223
11224 and then
11225 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
11226 or else
11227 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11228
11229 -- Test for interesting operation, which includes addition,
11230 -- division, exponentiation, multiplication, subtraction, absolute
11231 -- value and unary negation. Unary "+" is omitted since it is a
11232 -- no-op and thus can't overflow.
11233
11234 and then Nkind_In (Operand, N_Op_Abs,
11235 N_Op_Add,
11236 N_Op_Divide,
11237 N_Op_Expon,
11238 N_Op_Minus,
11239 N_Op_Multiply,
11240 N_Op_Subtract);
11241 end Integer_Promotion_Possible;
11242
11243 ------------------------------
11244 -- Make_Array_Comparison_Op --
11245 ------------------------------
11246
11247 -- This is a hand-coded expansion of the following generic function:
11248
11249 -- generic
11250 -- type elem is (<>);
11251 -- type index is (<>);
11252 -- type a is array (index range <>) of elem;
11253
11254 -- function Gnnn (X : a; Y: a) return boolean is
11255 -- J : index := Y'first;
11256
11257 -- begin
11258 -- if X'length = 0 then
11259 -- return false;
11260
11261 -- elsif Y'length = 0 then
11262 -- return true;
11263
11264 -- else
11265 -- for I in X'range loop
11266 -- if X (I) = Y (J) then
11267 -- if J = Y'last then
11268 -- exit;
11269 -- else
11270 -- J := index'succ (J);
11271 -- end if;
11272
11273 -- else
11274 -- return X (I) > Y (J);
11275 -- end if;
11276 -- end loop;
11277
11278 -- return X'length > Y'length;
11279 -- end if;
11280 -- end Gnnn;
11281
11282 -- Note that since we are essentially doing this expansion by hand, we
11283 -- do not need to generate an actual or formal generic part, just the
11284 -- instantiated function itself.
11285
11286 function Make_Array_Comparison_Op
11287 (Typ : Entity_Id;
11288 Nod : Node_Id) return Node_Id
11289 is
11290 Loc : constant Source_Ptr := Sloc (Nod);
11291
11292 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11293 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11294 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11295 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11296
11297 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11298
11299 Loop_Statement : Node_Id;
11300 Loop_Body : Node_Id;
11301 If_Stat : Node_Id;
11302 Inner_If : Node_Id;
11303 Final_Expr : Node_Id;
11304 Func_Body : Node_Id;
11305 Func_Name : Entity_Id;
11306 Formals : List_Id;
11307 Length1 : Node_Id;
11308 Length2 : Node_Id;
11309
11310 begin
11311 -- if J = Y'last then
11312 -- exit;
11313 -- else
11314 -- J := index'succ (J);
11315 -- end if;
11316
11317 Inner_If :=
11318 Make_Implicit_If_Statement (Nod,
11319 Condition =>
11320 Make_Op_Eq (Loc,
11321 Left_Opnd => New_Reference_To (J, Loc),
11322 Right_Opnd =>
11323 Make_Attribute_Reference (Loc,
11324 Prefix => New_Reference_To (Y, Loc),
11325 Attribute_Name => Name_Last)),
11326
11327 Then_Statements => New_List (
11328 Make_Exit_Statement (Loc)),
11329
11330 Else_Statements =>
11331 New_List (
11332 Make_Assignment_Statement (Loc,
11333 Name => New_Reference_To (J, Loc),
11334 Expression =>
11335 Make_Attribute_Reference (Loc,
11336 Prefix => New_Reference_To (Index, Loc),
11337 Attribute_Name => Name_Succ,
11338 Expressions => New_List (New_Reference_To (J, Loc))))));
11339
11340 -- if X (I) = Y (J) then
11341 -- if ... end if;
11342 -- else
11343 -- return X (I) > Y (J);
11344 -- end if;
11345
11346 Loop_Body :=
11347 Make_Implicit_If_Statement (Nod,
11348 Condition =>
11349 Make_Op_Eq (Loc,
11350 Left_Opnd =>
11351 Make_Indexed_Component (Loc,
11352 Prefix => New_Reference_To (X, Loc),
11353 Expressions => New_List (New_Reference_To (I, Loc))),
11354
11355 Right_Opnd =>
11356 Make_Indexed_Component (Loc,
11357 Prefix => New_Reference_To (Y, Loc),
11358 Expressions => New_List (New_Reference_To (J, Loc)))),
11359
11360 Then_Statements => New_List (Inner_If),
11361
11362 Else_Statements => New_List (
11363 Make_Simple_Return_Statement (Loc,
11364 Expression =>
11365 Make_Op_Gt (Loc,
11366 Left_Opnd =>
11367 Make_Indexed_Component (Loc,
11368 Prefix => New_Reference_To (X, Loc),
11369 Expressions => New_List (New_Reference_To (I, Loc))),
11370
11371 Right_Opnd =>
11372 Make_Indexed_Component (Loc,
11373 Prefix => New_Reference_To (Y, Loc),
11374 Expressions => New_List (
11375 New_Reference_To (J, Loc)))))));
11376
11377 -- for I in X'range loop
11378 -- if ... end if;
11379 -- end loop;
11380
11381 Loop_Statement :=
11382 Make_Implicit_Loop_Statement (Nod,
11383 Identifier => Empty,
11384
11385 Iteration_Scheme =>
11386 Make_Iteration_Scheme (Loc,
11387 Loop_Parameter_Specification =>
11388 Make_Loop_Parameter_Specification (Loc,
11389 Defining_Identifier => I,
11390 Discrete_Subtype_Definition =>
11391 Make_Attribute_Reference (Loc,
11392 Prefix => New_Reference_To (X, Loc),
11393 Attribute_Name => Name_Range))),
11394
11395 Statements => New_List (Loop_Body));
11396
11397 -- if X'length = 0 then
11398 -- return false;
11399 -- elsif Y'length = 0 then
11400 -- return true;
11401 -- else
11402 -- for ... loop ... end loop;
11403 -- return X'length > Y'length;
11404 -- end if;
11405
11406 Length1 :=
11407 Make_Attribute_Reference (Loc,
11408 Prefix => New_Reference_To (X, Loc),
11409 Attribute_Name => Name_Length);
11410
11411 Length2 :=
11412 Make_Attribute_Reference (Loc,
11413 Prefix => New_Reference_To (Y, Loc),
11414 Attribute_Name => Name_Length);
11415
11416 Final_Expr :=
11417 Make_Op_Gt (Loc,
11418 Left_Opnd => Length1,
11419 Right_Opnd => Length2);
11420
11421 If_Stat :=
11422 Make_Implicit_If_Statement (Nod,
11423 Condition =>
11424 Make_Op_Eq (Loc,
11425 Left_Opnd =>
11426 Make_Attribute_Reference (Loc,
11427 Prefix => New_Reference_To (X, Loc),
11428 Attribute_Name => Name_Length),
11429 Right_Opnd =>
11430 Make_Integer_Literal (Loc, 0)),
11431
11432 Then_Statements =>
11433 New_List (
11434 Make_Simple_Return_Statement (Loc,
11435 Expression => New_Reference_To (Standard_False, Loc))),
11436
11437 Elsif_Parts => New_List (
11438 Make_Elsif_Part (Loc,
11439 Condition =>
11440 Make_Op_Eq (Loc,
11441 Left_Opnd =>
11442 Make_Attribute_Reference (Loc,
11443 Prefix => New_Reference_To (Y, Loc),
11444 Attribute_Name => Name_Length),
11445 Right_Opnd =>
11446 Make_Integer_Literal (Loc, 0)),
11447
11448 Then_Statements =>
11449 New_List (
11450 Make_Simple_Return_Statement (Loc,
11451 Expression => New_Reference_To (Standard_True, Loc))))),
11452
11453 Else_Statements => New_List (
11454 Loop_Statement,
11455 Make_Simple_Return_Statement (Loc,
11456 Expression => Final_Expr)));
11457
11458 -- (X : a; Y: a)
11459
11460 Formals := New_List (
11461 Make_Parameter_Specification (Loc,
11462 Defining_Identifier => X,
11463 Parameter_Type => New_Reference_To (Typ, Loc)),
11464
11465 Make_Parameter_Specification (Loc,
11466 Defining_Identifier => Y,
11467 Parameter_Type => New_Reference_To (Typ, Loc)));
11468
11469 -- function Gnnn (...) return boolean is
11470 -- J : index := Y'first;
11471 -- begin
11472 -- if ... end if;
11473 -- end Gnnn;
11474
11475 Func_Name := Make_Temporary (Loc, 'G');
11476
11477 Func_Body :=
11478 Make_Subprogram_Body (Loc,
11479 Specification =>
11480 Make_Function_Specification (Loc,
11481 Defining_Unit_Name => Func_Name,
11482 Parameter_Specifications => Formals,
11483 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
11484
11485 Declarations => New_List (
11486 Make_Object_Declaration (Loc,
11487 Defining_Identifier => J,
11488 Object_Definition => New_Reference_To (Index, Loc),
11489 Expression =>
11490 Make_Attribute_Reference (Loc,
11491 Prefix => New_Reference_To (Y, Loc),
11492 Attribute_Name => Name_First))),
11493
11494 Handled_Statement_Sequence =>
11495 Make_Handled_Sequence_Of_Statements (Loc,
11496 Statements => New_List (If_Stat)));
11497
11498 return Func_Body;
11499 end Make_Array_Comparison_Op;
11500
11501 ---------------------------
11502 -- Make_Boolean_Array_Op --
11503 ---------------------------
11504
11505 -- For logical operations on boolean arrays, expand in line the following,
11506 -- replacing 'and' with 'or' or 'xor' where needed:
11507
11508 -- function Annn (A : typ; B: typ) return typ is
11509 -- C : typ;
11510 -- begin
11511 -- for J in A'range loop
11512 -- C (J) := A (J) op B (J);
11513 -- end loop;
11514 -- return C;
11515 -- end Annn;
11516
11517 -- Here typ is the boolean array type
11518
11519 function Make_Boolean_Array_Op
11520 (Typ : Entity_Id;
11521 N : Node_Id) return Node_Id
11522 is
11523 Loc : constant Source_Ptr := Sloc (N);
11524
11525 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
11526 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
11527 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
11528 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11529
11530 A_J : Node_Id;
11531 B_J : Node_Id;
11532 C_J : Node_Id;
11533 Op : Node_Id;
11534
11535 Formals : List_Id;
11536 Func_Name : Entity_Id;
11537 Func_Body : Node_Id;
11538 Loop_Statement : Node_Id;
11539
11540 begin
11541 A_J :=
11542 Make_Indexed_Component (Loc,
11543 Prefix => New_Reference_To (A, Loc),
11544 Expressions => New_List (New_Reference_To (J, Loc)));
11545
11546 B_J :=
11547 Make_Indexed_Component (Loc,
11548 Prefix => New_Reference_To (B, Loc),
11549 Expressions => New_List (New_Reference_To (J, Loc)));
11550
11551 C_J :=
11552 Make_Indexed_Component (Loc,
11553 Prefix => New_Reference_To (C, Loc),
11554 Expressions => New_List (New_Reference_To (J, Loc)));
11555
11556 if Nkind (N) = N_Op_And then
11557 Op :=
11558 Make_Op_And (Loc,
11559 Left_Opnd => A_J,
11560 Right_Opnd => B_J);
11561
11562 elsif Nkind (N) = N_Op_Or then
11563 Op :=
11564 Make_Op_Or (Loc,
11565 Left_Opnd => A_J,
11566 Right_Opnd => B_J);
11567
11568 else
11569 Op :=
11570 Make_Op_Xor (Loc,
11571 Left_Opnd => A_J,
11572 Right_Opnd => B_J);
11573 end if;
11574
11575 Loop_Statement :=
11576 Make_Implicit_Loop_Statement (N,
11577 Identifier => Empty,
11578
11579 Iteration_Scheme =>
11580 Make_Iteration_Scheme (Loc,
11581 Loop_Parameter_Specification =>
11582 Make_Loop_Parameter_Specification (Loc,
11583 Defining_Identifier => J,
11584 Discrete_Subtype_Definition =>
11585 Make_Attribute_Reference (Loc,
11586 Prefix => New_Reference_To (A, Loc),
11587 Attribute_Name => Name_Range))),
11588
11589 Statements => New_List (
11590 Make_Assignment_Statement (Loc,
11591 Name => C_J,
11592 Expression => Op)));
11593
11594 Formals := New_List (
11595 Make_Parameter_Specification (Loc,
11596 Defining_Identifier => A,
11597 Parameter_Type => New_Reference_To (Typ, Loc)),
11598
11599 Make_Parameter_Specification (Loc,
11600 Defining_Identifier => B,
11601 Parameter_Type => New_Reference_To (Typ, Loc)));
11602
11603 Func_Name := Make_Temporary (Loc, 'A');
11604 Set_Is_Inlined (Func_Name);
11605
11606 Func_Body :=
11607 Make_Subprogram_Body (Loc,
11608 Specification =>
11609 Make_Function_Specification (Loc,
11610 Defining_Unit_Name => Func_Name,
11611 Parameter_Specifications => Formals,
11612 Result_Definition => New_Reference_To (Typ, Loc)),
11613
11614 Declarations => New_List (
11615 Make_Object_Declaration (Loc,
11616 Defining_Identifier => C,
11617 Object_Definition => New_Reference_To (Typ, Loc))),
11618
11619 Handled_Statement_Sequence =>
11620 Make_Handled_Sequence_Of_Statements (Loc,
11621 Statements => New_List (
11622 Loop_Statement,
11623 Make_Simple_Return_Statement (Loc,
11624 Expression => New_Reference_To (C, Loc)))));
11625
11626 return Func_Body;
11627 end Make_Boolean_Array_Op;
11628
11629 --------------------------------
11630 -- Optimize_Length_Comparison --
11631 --------------------------------
11632
11633 procedure Optimize_Length_Comparison (N : Node_Id) is
11634 Loc : constant Source_Ptr := Sloc (N);
11635 Typ : constant Entity_Id := Etype (N);
11636 Result : Node_Id;
11637
11638 Left : Node_Id;
11639 Right : Node_Id;
11640 -- First and Last attribute reference nodes, which end up as left and
11641 -- right operands of the optimized result.
11642
11643 Is_Zero : Boolean;
11644 -- True for comparison operand of zero
11645
11646 Comp : Node_Id;
11647 -- Comparison operand, set only if Is_Zero is false
11648
11649 Ent : Entity_Id;
11650 -- Entity whose length is being compared
11651
11652 Index : Node_Id;
11653 -- Integer_Literal node for length attribute expression, or Empty
11654 -- if there is no such expression present.
11655
11656 Ityp : Entity_Id;
11657 -- Type of array index to which 'Length is applied
11658
11659 Op : Node_Kind := Nkind (N);
11660 -- Kind of comparison operator, gets flipped if operands backwards
11661
11662 function Is_Optimizable (N : Node_Id) return Boolean;
11663 -- Tests N to see if it is an optimizable comparison value (defined as
11664 -- constant zero or one, or something else where the value is known to
11665 -- be positive and in the range of 32-bits, and where the corresponding
11666 -- Length value is also known to be 32-bits. If result is true, sets
11667 -- Is_Zero, Ityp, and Comp accordingly.
11668
11669 function Is_Entity_Length (N : Node_Id) return Boolean;
11670 -- Tests if N is a length attribute applied to a simple entity. If so,
11671 -- returns True, and sets Ent to the entity, and Index to the integer
11672 -- literal provided as an attribute expression, or to Empty if none.
11673 -- Also returns True if the expression is a generated type conversion
11674 -- whose expression is of the desired form. This latter case arises
11675 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
11676 -- to check for being in range, which is not needed in this context.
11677 -- Returns False if neither condition holds.
11678
11679 function Prepare_64 (N : Node_Id) return Node_Id;
11680 -- Given a discrete expression, returns a Long_Long_Integer typed
11681 -- expression representing the underlying value of the expression.
11682 -- This is done with an unchecked conversion to the result type. We
11683 -- use unchecked conversion to handle the enumeration type case.
11684
11685 ----------------------
11686 -- Is_Entity_Length --
11687 ----------------------
11688
11689 function Is_Entity_Length (N : Node_Id) return Boolean is
11690 begin
11691 if Nkind (N) = N_Attribute_Reference
11692 and then Attribute_Name (N) = Name_Length
11693 and then Is_Entity_Name (Prefix (N))
11694 then
11695 Ent := Entity (Prefix (N));
11696
11697 if Present (Expressions (N)) then
11698 Index := First (Expressions (N));
11699 else
11700 Index := Empty;
11701 end if;
11702
11703 return True;
11704
11705 elsif Nkind (N) = N_Type_Conversion
11706 and then not Comes_From_Source (N)
11707 then
11708 return Is_Entity_Length (Expression (N));
11709
11710 else
11711 return False;
11712 end if;
11713 end Is_Entity_Length;
11714
11715 --------------------
11716 -- Is_Optimizable --
11717 --------------------
11718
11719 function Is_Optimizable (N : Node_Id) return Boolean is
11720 Val : Uint;
11721 OK : Boolean;
11722 Lo : Uint;
11723 Hi : Uint;
11724 Indx : Node_Id;
11725
11726 begin
11727 if Compile_Time_Known_Value (N) then
11728 Val := Expr_Value (N);
11729
11730 if Val = Uint_0 then
11731 Is_Zero := True;
11732 Comp := Empty;
11733 return True;
11734
11735 elsif Val = Uint_1 then
11736 Is_Zero := False;
11737 Comp := Empty;
11738 return True;
11739 end if;
11740 end if;
11741
11742 -- Here we have to make sure of being within 32-bits
11743
11744 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
11745
11746 if not OK
11747 or else Lo < Uint_1
11748 or else Hi > UI_From_Int (Int'Last)
11749 then
11750 return False;
11751 end if;
11752
11753 -- Comparison value was within range, so now we must check the index
11754 -- value to make sure it is also within 32-bits.
11755
11756 Indx := First_Index (Etype (Ent));
11757
11758 if Present (Index) then
11759 for J in 2 .. UI_To_Int (Intval (Index)) loop
11760 Next_Index (Indx);
11761 end loop;
11762 end if;
11763
11764 Ityp := Etype (Indx);
11765
11766 if Esize (Ityp) > 32 then
11767 return False;
11768 end if;
11769
11770 Is_Zero := False;
11771 Comp := N;
11772 return True;
11773 end Is_Optimizable;
11774
11775 ----------------
11776 -- Prepare_64 --
11777 ----------------
11778
11779 function Prepare_64 (N : Node_Id) return Node_Id is
11780 begin
11781 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
11782 end Prepare_64;
11783
11784 -- Start of processing for Optimize_Length_Comparison
11785
11786 begin
11787 -- Nothing to do if not a comparison
11788
11789 if Op not in N_Op_Compare then
11790 return;
11791 end if;
11792
11793 -- Nothing to do if special -gnatd.P debug flag set
11794
11795 if Debug_Flag_Dot_PP then
11796 return;
11797 end if;
11798
11799 -- Ent'Length op 0/1
11800
11801 if Is_Entity_Length (Left_Opnd (N))
11802 and then Is_Optimizable (Right_Opnd (N))
11803 then
11804 null;
11805
11806 -- 0/1 op Ent'Length
11807
11808 elsif Is_Entity_Length (Right_Opnd (N))
11809 and then Is_Optimizable (Left_Opnd (N))
11810 then
11811 -- Flip comparison to opposite sense
11812
11813 case Op is
11814 when N_Op_Lt => Op := N_Op_Gt;
11815 when N_Op_Le => Op := N_Op_Ge;
11816 when N_Op_Gt => Op := N_Op_Lt;
11817 when N_Op_Ge => Op := N_Op_Le;
11818 when others => null;
11819 end case;
11820
11821 -- Else optimization not possible
11822
11823 else
11824 return;
11825 end if;
11826
11827 -- Fall through if we will do the optimization
11828
11829 -- Cases to handle:
11830
11831 -- X'Length = 0 => X'First > X'Last
11832 -- X'Length = 1 => X'First = X'Last
11833 -- X'Length = n => X'First + (n - 1) = X'Last
11834
11835 -- X'Length /= 0 => X'First <= X'Last
11836 -- X'Length /= 1 => X'First /= X'Last
11837 -- X'Length /= n => X'First + (n - 1) /= X'Last
11838
11839 -- X'Length >= 0 => always true, warn
11840 -- X'Length >= 1 => X'First <= X'Last
11841 -- X'Length >= n => X'First + (n - 1) <= X'Last
11842
11843 -- X'Length > 0 => X'First <= X'Last
11844 -- X'Length > 1 => X'First < X'Last
11845 -- X'Length > n => X'First + (n - 1) < X'Last
11846
11847 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
11848 -- X'Length <= 1 => X'First >= X'Last
11849 -- X'Length <= n => X'First + (n - 1) >= X'Last
11850
11851 -- X'Length < 0 => always false (warn)
11852 -- X'Length < 1 => X'First > X'Last
11853 -- X'Length < n => X'First + (n - 1) > X'Last
11854
11855 -- Note: for the cases of n (not constant 0,1), we require that the
11856 -- corresponding index type be integer or shorter (i.e. not 64-bit),
11857 -- and the same for the comparison value. Then we do the comparison
11858 -- using 64-bit arithmetic (actually long long integer), so that we
11859 -- cannot have overflow intefering with the result.
11860
11861 -- First deal with warning cases
11862
11863 if Is_Zero then
11864 case Op is
11865
11866 -- X'Length >= 0
11867
11868 when N_Op_Ge =>
11869 Rewrite (N,
11870 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
11871 Analyze_And_Resolve (N, Typ);
11872 Warn_On_Known_Condition (N);
11873 return;
11874
11875 -- X'Length < 0
11876
11877 when N_Op_Lt =>
11878 Rewrite (N,
11879 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
11880 Analyze_And_Resolve (N, Typ);
11881 Warn_On_Known_Condition (N);
11882 return;
11883
11884 when N_Op_Le =>
11885 if Constant_Condition_Warnings
11886 and then Comes_From_Source (Original_Node (N))
11887 then
11888 Error_Msg_N ("could replace by ""'=""?", N);
11889 end if;
11890
11891 Op := N_Op_Eq;
11892
11893 when others =>
11894 null;
11895 end case;
11896 end if;
11897
11898 -- Build the First reference we will use
11899
11900 Left :=
11901 Make_Attribute_Reference (Loc,
11902 Prefix => New_Occurrence_Of (Ent, Loc),
11903 Attribute_Name => Name_First);
11904
11905 if Present (Index) then
11906 Set_Expressions (Left, New_List (New_Copy (Index)));
11907 end if;
11908
11909 -- If general value case, then do the addition of (n - 1), and
11910 -- also add the needed conversions to type Long_Long_Integer.
11911
11912 if Present (Comp) then
11913 Left :=
11914 Make_Op_Add (Loc,
11915 Left_Opnd => Prepare_64 (Left),
11916 Right_Opnd =>
11917 Make_Op_Subtract (Loc,
11918 Left_Opnd => Prepare_64 (Comp),
11919 Right_Opnd => Make_Integer_Literal (Loc, 1)));
11920 end if;
11921
11922 -- Build the Last reference we will use
11923
11924 Right :=
11925 Make_Attribute_Reference (Loc,
11926 Prefix => New_Occurrence_Of (Ent, Loc),
11927 Attribute_Name => Name_Last);
11928
11929 if Present (Index) then
11930 Set_Expressions (Right, New_List (New_Copy (Index)));
11931 end if;
11932
11933 -- If general operand, convert Last reference to Long_Long_Integer
11934
11935 if Present (Comp) then
11936 Right := Prepare_64 (Right);
11937 end if;
11938
11939 -- Check for cases to optimize
11940
11941 -- X'Length = 0 => X'First > X'Last
11942 -- X'Length < 1 => X'First > X'Last
11943 -- X'Length < n => X'First + (n - 1) > X'Last
11944
11945 if (Is_Zero and then Op = N_Op_Eq)
11946 or else (not Is_Zero and then Op = N_Op_Lt)
11947 then
11948 Result :=
11949 Make_Op_Gt (Loc,
11950 Left_Opnd => Left,
11951 Right_Opnd => Right);
11952
11953 -- X'Length = 1 => X'First = X'Last
11954 -- X'Length = n => X'First + (n - 1) = X'Last
11955
11956 elsif not Is_Zero and then Op = N_Op_Eq then
11957 Result :=
11958 Make_Op_Eq (Loc,
11959 Left_Opnd => Left,
11960 Right_Opnd => Right);
11961
11962 -- X'Length /= 0 => X'First <= X'Last
11963 -- X'Length > 0 => X'First <= X'Last
11964
11965 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
11966 Result :=
11967 Make_Op_Le (Loc,
11968 Left_Opnd => Left,
11969 Right_Opnd => Right);
11970
11971 -- X'Length /= 1 => X'First /= X'Last
11972 -- X'Length /= n => X'First + (n - 1) /= X'Last
11973
11974 elsif not Is_Zero and then Op = N_Op_Ne then
11975 Result :=
11976 Make_Op_Ne (Loc,
11977 Left_Opnd => Left,
11978 Right_Opnd => Right);
11979
11980 -- X'Length >= 1 => X'First <= X'Last
11981 -- X'Length >= n => X'First + (n - 1) <= X'Last
11982
11983 elsif not Is_Zero and then Op = N_Op_Ge then
11984 Result :=
11985 Make_Op_Le (Loc,
11986 Left_Opnd => Left,
11987 Right_Opnd => Right);
11988
11989 -- X'Length > 1 => X'First < X'Last
11990 -- X'Length > n => X'First + (n = 1) < X'Last
11991
11992 elsif not Is_Zero and then Op = N_Op_Gt then
11993 Result :=
11994 Make_Op_Lt (Loc,
11995 Left_Opnd => Left,
11996 Right_Opnd => Right);
11997
11998 -- X'Length <= 1 => X'First >= X'Last
11999 -- X'Length <= n => X'First + (n - 1) >= X'Last
12000
12001 elsif not Is_Zero and then Op = N_Op_Le then
12002 Result :=
12003 Make_Op_Ge (Loc,
12004 Left_Opnd => Left,
12005 Right_Opnd => Right);
12006
12007 -- Should not happen at this stage
12008
12009 else
12010 raise Program_Error;
12011 end if;
12012
12013 -- Rewrite and finish up
12014
12015 Rewrite (N, Result);
12016 Analyze_And_Resolve (N, Typ);
12017 return;
12018 end Optimize_Length_Comparison;
12019
12020 ------------------------
12021 -- Rewrite_Comparison --
12022 ------------------------
12023
12024 procedure Rewrite_Comparison (N : Node_Id) is
12025 Warning_Generated : Boolean := False;
12026 -- Set to True if first pass with Assume_Valid generates a warning in
12027 -- which case we skip the second pass to avoid warning overloaded.
12028
12029 Result : Node_Id;
12030 -- Set to Standard_True or Standard_False
12031
12032 begin
12033 if Nkind (N) = N_Type_Conversion then
12034 Rewrite_Comparison (Expression (N));
12035 return;
12036
12037 elsif Nkind (N) not in N_Op_Compare then
12038 return;
12039 end if;
12040
12041 -- Now start looking at the comparison in detail. We potentially go
12042 -- through this loop twice. The first time, Assume_Valid is set False
12043 -- in the call to Compile_Time_Compare. If this call results in a
12044 -- clear result of always True or Always False, that's decisive and
12045 -- we are done. Otherwise we repeat the processing with Assume_Valid
12046 -- set to True to generate additional warnings. We can skip that step
12047 -- if Constant_Condition_Warnings is False.
12048
12049 for AV in False .. True loop
12050 declare
12051 Typ : constant Entity_Id := Etype (N);
12052 Op1 : constant Node_Id := Left_Opnd (N);
12053 Op2 : constant Node_Id := Right_Opnd (N);
12054
12055 Res : constant Compare_Result :=
12056 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12057 -- Res indicates if compare outcome can be compile time determined
12058
12059 True_Result : Boolean;
12060 False_Result : Boolean;
12061
12062 begin
12063 case N_Op_Compare (Nkind (N)) is
12064 when N_Op_Eq =>
12065 True_Result := Res = EQ;
12066 False_Result := Res = LT or else Res = GT or else Res = NE;
12067
12068 when N_Op_Ge =>
12069 True_Result := Res in Compare_GE;
12070 False_Result := Res = LT;
12071
12072 if Res = LE
12073 and then Constant_Condition_Warnings
12074 and then Comes_From_Source (Original_Node (N))
12075 and then Nkind (Original_Node (N)) = N_Op_Ge
12076 and then not In_Instance
12077 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12078 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12079 then
12080 Error_Msg_N
12081 ("can never be greater than, could replace by ""'=""?", N);
12082 Warning_Generated := True;
12083 end if;
12084
12085 when N_Op_Gt =>
12086 True_Result := Res = GT;
12087 False_Result := Res in Compare_LE;
12088
12089 when N_Op_Lt =>
12090 True_Result := Res = LT;
12091 False_Result := Res in Compare_GE;
12092
12093 when N_Op_Le =>
12094 True_Result := Res in Compare_LE;
12095 False_Result := Res = GT;
12096
12097 if Res = GE
12098 and then Constant_Condition_Warnings
12099 and then Comes_From_Source (Original_Node (N))
12100 and then Nkind (Original_Node (N)) = N_Op_Le
12101 and then not In_Instance
12102 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12103 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12104 then
12105 Error_Msg_N
12106 ("can never be less than, could replace by ""'=""?", N);
12107 Warning_Generated := True;
12108 end if;
12109
12110 when N_Op_Ne =>
12111 True_Result := Res = NE or else Res = GT or else Res = LT;
12112 False_Result := Res = EQ;
12113 end case;
12114
12115 -- If this is the first iteration, then we actually convert the
12116 -- comparison into True or False, if the result is certain.
12117
12118 if AV = False then
12119 if True_Result or False_Result then
12120 Result := Boolean_Literals (True_Result);
12121 Rewrite (N,
12122 Convert_To (Typ,
12123 New_Occurrence_Of (Result, Sloc (N))));
12124 Analyze_And_Resolve (N, Typ);
12125 Warn_On_Known_Condition (N);
12126 return;
12127 end if;
12128
12129 -- If this is the second iteration (AV = True), and the original
12130 -- node comes from source and we are not in an instance, then give
12131 -- a warning if we know result would be True or False. Note: we
12132 -- know Constant_Condition_Warnings is set if we get here.
12133
12134 elsif Comes_From_Source (Original_Node (N))
12135 and then not In_Instance
12136 then
12137 if True_Result then
12138 Error_Msg_N
12139 ("condition can only be False if invalid values present?",
12140 N);
12141 elsif False_Result then
12142 Error_Msg_N
12143 ("condition can only be True if invalid values present?",
12144 N);
12145 end if;
12146 end if;
12147 end;
12148
12149 -- Skip second iteration if not warning on constant conditions or
12150 -- if the first iteration already generated a warning of some kind or
12151 -- if we are in any case assuming all values are valid (so that the
12152 -- first iteration took care of the valid case).
12153
12154 exit when not Constant_Condition_Warnings;
12155 exit when Warning_Generated;
12156 exit when Assume_No_Invalid_Values;
12157 end loop;
12158 end Rewrite_Comparison;
12159
12160 ----------------------------
12161 -- Safe_In_Place_Array_Op --
12162 ----------------------------
12163
12164 function Safe_In_Place_Array_Op
12165 (Lhs : Node_Id;
12166 Op1 : Node_Id;
12167 Op2 : Node_Id) return Boolean
12168 is
12169 Target : Entity_Id;
12170
12171 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12172 -- Operand is safe if it cannot overlap part of the target of the
12173 -- operation. If the operand and the target are identical, the operand
12174 -- is safe. The operand can be empty in the case of negation.
12175
12176 function Is_Unaliased (N : Node_Id) return Boolean;
12177 -- Check that N is a stand-alone entity
12178
12179 ------------------
12180 -- Is_Unaliased --
12181 ------------------
12182
12183 function Is_Unaliased (N : Node_Id) return Boolean is
12184 begin
12185 return
12186 Is_Entity_Name (N)
12187 and then No (Address_Clause (Entity (N)))
12188 and then No (Renamed_Object (Entity (N)));
12189 end Is_Unaliased;
12190
12191 ---------------------
12192 -- Is_Safe_Operand --
12193 ---------------------
12194
12195 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12196 begin
12197 if No (Op) then
12198 return True;
12199
12200 elsif Is_Entity_Name (Op) then
12201 return Is_Unaliased (Op);
12202
12203 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
12204 return Is_Unaliased (Prefix (Op));
12205
12206 elsif Nkind (Op) = N_Slice then
12207 return
12208 Is_Unaliased (Prefix (Op))
12209 and then Entity (Prefix (Op)) /= Target;
12210
12211 elsif Nkind (Op) = N_Op_Not then
12212 return Is_Safe_Operand (Right_Opnd (Op));
12213
12214 else
12215 return False;
12216 end if;
12217 end Is_Safe_Operand;
12218
12219 -- Start of processing for Is_Safe_In_Place_Array_Op
12220
12221 begin
12222 -- Skip this processing if the component size is different from system
12223 -- storage unit (since at least for NOT this would cause problems).
12224
12225 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
12226 return False;
12227
12228 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12229
12230 elsif VM_Target /= No_VM then
12231 return False;
12232
12233 -- Cannot do in place stuff if non-standard Boolean representation
12234
12235 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
12236 return False;
12237
12238 elsif not Is_Unaliased (Lhs) then
12239 return False;
12240
12241 else
12242 Target := Entity (Lhs);
12243 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
12244 end if;
12245 end Safe_In_Place_Array_Op;
12246
12247 -----------------------
12248 -- Tagged_Membership --
12249 -----------------------
12250
12251 -- There are two different cases to consider depending on whether the right
12252 -- operand is a class-wide type or not. If not we just compare the actual
12253 -- tag of the left expr to the target type tag:
12254 --
12255 -- Left_Expr.Tag = Right_Type'Tag;
12256 --
12257 -- If it is a class-wide type we use the RT function CW_Membership which is
12258 -- usually implemented by looking in the ancestor tables contained in the
12259 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
12260
12261 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
12262 -- function IW_Membership which is usually implemented by looking in the
12263 -- table of abstract interface types plus the ancestor table contained in
12264 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
12265
12266 procedure Tagged_Membership
12267 (N : Node_Id;
12268 SCIL_Node : out Node_Id;
12269 Result : out Node_Id)
12270 is
12271 Left : constant Node_Id := Left_Opnd (N);
12272 Right : constant Node_Id := Right_Opnd (N);
12273 Loc : constant Source_Ptr := Sloc (N);
12274
12275 Full_R_Typ : Entity_Id;
12276 Left_Type : Entity_Id;
12277 New_Node : Node_Id;
12278 Right_Type : Entity_Id;
12279 Obj_Tag : Node_Id;
12280
12281 begin
12282 SCIL_Node := Empty;
12283
12284 -- Handle entities from the limited view
12285
12286 Left_Type := Available_View (Etype (Left));
12287 Right_Type := Available_View (Etype (Right));
12288
12289 -- In the case where the type is an access type, the test is applied
12290 -- using the designated types (needed in Ada 2012 for implicit anonymous
12291 -- access conversions, for AI05-0149).
12292
12293 if Is_Access_Type (Right_Type) then
12294 Left_Type := Designated_Type (Left_Type);
12295 Right_Type := Designated_Type (Right_Type);
12296 end if;
12297
12298 if Is_Class_Wide_Type (Left_Type) then
12299 Left_Type := Root_Type (Left_Type);
12300 end if;
12301
12302 if Is_Class_Wide_Type (Right_Type) then
12303 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
12304 else
12305 Full_R_Typ := Underlying_Type (Right_Type);
12306 end if;
12307
12308 Obj_Tag :=
12309 Make_Selected_Component (Loc,
12310 Prefix => Relocate_Node (Left),
12311 Selector_Name =>
12312 New_Reference_To (First_Tag_Component (Left_Type), Loc));
12313
12314 if Is_Class_Wide_Type (Right_Type) then
12315
12316 -- No need to issue a run-time check if we statically know that the
12317 -- result of this membership test is always true. For example,
12318 -- considering the following declarations:
12319
12320 -- type Iface is interface;
12321 -- type T is tagged null record;
12322 -- type DT is new T and Iface with null record;
12323
12324 -- Obj1 : T;
12325 -- Obj2 : DT;
12326
12327 -- These membership tests are always true:
12328
12329 -- Obj1 in T'Class
12330 -- Obj2 in T'Class;
12331 -- Obj2 in Iface'Class;
12332
12333 -- We do not need to handle cases where the membership is illegal.
12334 -- For example:
12335
12336 -- Obj1 in DT'Class; -- Compile time error
12337 -- Obj1 in Iface'Class; -- Compile time error
12338
12339 if not Is_Class_Wide_Type (Left_Type)
12340 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
12341 Use_Full_View => True)
12342 or else (Is_Interface (Etype (Right_Type))
12343 and then Interface_Present_In_Ancestor
12344 (Typ => Left_Type,
12345 Iface => Etype (Right_Type))))
12346 then
12347 Result := New_Reference_To (Standard_True, Loc);
12348 return;
12349 end if;
12350
12351 -- Ada 2005 (AI-251): Class-wide applied to interfaces
12352
12353 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
12354
12355 -- Support to: "Iface_CW_Typ in Typ'Class"
12356
12357 or else Is_Interface (Left_Type)
12358 then
12359 -- Issue error if IW_Membership operation not available in a
12360 -- configurable run time setting.
12361
12362 if not RTE_Available (RE_IW_Membership) then
12363 Error_Msg_CRT
12364 ("dynamic membership test on interface types", N);
12365 Result := Empty;
12366 return;
12367 end if;
12368
12369 Result :=
12370 Make_Function_Call (Loc,
12371 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
12372 Parameter_Associations => New_List (
12373 Make_Attribute_Reference (Loc,
12374 Prefix => Obj_Tag,
12375 Attribute_Name => Name_Address),
12376 New_Reference_To (
12377 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
12378 Loc)));
12379
12380 -- Ada 95: Normal case
12381
12382 else
12383 Build_CW_Membership (Loc,
12384 Obj_Tag_Node => Obj_Tag,
12385 Typ_Tag_Node =>
12386 New_Reference_To (
12387 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
12388 Related_Nod => N,
12389 New_Node => New_Node);
12390
12391 -- Generate the SCIL node for this class-wide membership test.
12392 -- Done here because the previous call to Build_CW_Membership
12393 -- relocates Obj_Tag.
12394
12395 if Generate_SCIL then
12396 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
12397 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
12398 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
12399 end if;
12400
12401 Result := New_Node;
12402 end if;
12403
12404 -- Right_Type is not a class-wide type
12405
12406 else
12407 -- No need to check the tag of the object if Right_Typ is abstract
12408
12409 if Is_Abstract_Type (Right_Type) then
12410 Result := New_Reference_To (Standard_False, Loc);
12411
12412 else
12413 Result :=
12414 Make_Op_Eq (Loc,
12415 Left_Opnd => Obj_Tag,
12416 Right_Opnd =>
12417 New_Reference_To
12418 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
12419 end if;
12420 end if;
12421 end Tagged_Membership;
12422
12423 ------------------------------
12424 -- Unary_Op_Validity_Checks --
12425 ------------------------------
12426
12427 procedure Unary_Op_Validity_Checks (N : Node_Id) is
12428 begin
12429 if Validity_Checks_On and Validity_Check_Operands then
12430 Ensure_Valid (Right_Opnd (N));
12431 end if;
12432 end Unary_Op_Validity_Checks;
12433
12434 end Exp_Ch4;