1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004, Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Atree; use Atree;
28 with Checks; use Checks;
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_Ch3; use Exp_Ch3;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Fixd; use Exp_Fixd;
38 with Exp_Pakd; use Exp_Pakd;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Exp_VFpt; use Exp_VFpt;
42 with Hostparm; use Hostparm;
43 with Inline; use Inline;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
47 with Rtsfind; use Rtsfind;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Ttypes; use Ttypes;
63 with Uintp; use Uintp;
64 with Urealp; use Urealp;
65 with Validsw; use Validsw;
67 package body Exp_Ch4 is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Binary_Op_Validity_Checks (N : Node_Id);
74 pragma Inline (Binary_Op_Validity_Checks);
75 -- Performs validity checks for a binary operator
77 procedure Build_Boolean_Array_Proc_Call
81 -- If an boolean array assignment can be done in place, build call to
82 -- corresponding library procedure.
84 procedure Expand_Allocator_Expression (N : Node_Id);
85 -- Subsidiary to Expand_N_Allocator, for the case when the expression
86 -- is a qualified expression or an aggregate.
88 procedure Expand_Array_Comparison (N : Node_Id);
89 -- This routine handles expansion of the comparison operators (N_Op_Lt,
90 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
91 -- code for these operators is similar, differing only in the details of
92 -- the actual comparison call that is made. Special processing (call a
95 function Expand_Array_Equality
100 Typ : Entity_Id) return Node_Id;
101 -- Expand an array equality into a call to a function implementing this
102 -- equality, and a call to it. Loc is the location for the generated
103 -- nodes. Lhs and Rhs are the array expressions to be compared.
104 -- Bodies is a list on which to attach bodies of local functions that
105 -- are created in the process. It is the responsibility of the
106 -- caller to insert those bodies at the right place. Nod provides
107 -- the Sloc value for the generated code. Normally the types used
108 -- for the generated equality routine are taken from Lhs and Rhs.
109 -- However, in some situations of generated code, the Etype fields
110 -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the
111 -- type to be used for the formal parameters.
113 procedure Expand_Boolean_Operator (N : Node_Id);
114 -- Common expansion processing for Boolean operators (And, Or, Xor)
115 -- for the case of array type arguments.
117 function Expand_Composite_Equality
122 Bodies : List_Id) return Node_Id;
123 -- Local recursive function used to expand equality for nested
124 -- composite types. Used by Expand_Record/Array_Equality, Bodies
125 -- is a list on which to attach bodies of local functions that are
126 -- created in the process. This is the responsability of the caller
127 -- to insert those bodies at the right place. Nod provides the Sloc
128 -- value for generated code. Lhs and Rhs are the left and right sides
129 -- for the comparison, and Typ is the type of the arrays to compare.
131 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id);
132 -- This routine handles expansion of concatenation operations, where
133 -- N is the N_Op_Concat node being expanded and Operands is the list
134 -- of operands (at least two are present). The caller has dealt with
135 -- converting any singleton operands into singleton aggregates.
137 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id);
138 -- Routine to expand concatenation of 2-5 operands (in the list Operands)
139 -- and replace node Cnode with the result of the contatenation. If there
140 -- are two operands, they can be string or character. If there are more
141 -- than two operands, then are always of type string (i.e. the caller has
142 -- already converted character operands to strings in this case).
144 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
145 -- N is either an N_Op_Divide or N_Op_Multiply node whose result is
146 -- universal fixed. We do not have such a type at runtime, so the
147 -- purpose of this routine is to find the real type by looking up
148 -- the tree. We also determine if the operation must be rounded.
150 function Get_Allocator_Final_List
153 PtrT : Entity_Id) return Entity_Id;
154 -- If the designated type is controlled, build final_list expression
155 -- for created object. If context is an access parameter, create a
156 -- local access type to have a usable finalization list.
158 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
159 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
160 -- discriminants if it has a constrained nominal type, unless the object
161 -- is a component of an enclosing Unchecked_Union object that is subject
162 -- to a per-object constraint and the enclosing object lacks inferable
165 -- An expression of an Unchecked_Union type has inferable discriminants
166 -- if it is either a name of an object with inferable discriminants or a
167 -- qualified expression whose subtype mark denotes a constrained subtype.
169 procedure Insert_Dereference_Action (N : Node_Id);
170 -- N is an expression whose type is an access. When the type of the
171 -- associated storage pool is derived from Checked_Pool, generate a
172 -- call to the 'Dereference' primitive operation.
174 function Make_Array_Comparison_Op
176 Nod : Node_Id) return Node_Id;
177 -- Comparisons between arrays are expanded in line. This function
178 -- produces the body of the implementation of (a > b), where a and b
179 -- are one-dimensional arrays of some discrete type. The original
180 -- node is then expanded into the appropriate call to this function.
181 -- Nod provides the Sloc value for the generated code.
183 function Make_Boolean_Array_Op
185 N : Node_Id) return Node_Id;
186 -- Boolean operations on boolean arrays are expanded in line. This
187 -- function produce the body for the node N, which is (a and b),
188 -- (a or b), or (a xor b). It is used only the normal case and not
189 -- the packed case. The type involved, Typ, is the Boolean array type,
190 -- and the logical operations in the body are simple boolean operations.
191 -- Note that Typ is always a constrained type (the caller has ensured
192 -- this by using Convert_To_Actual_Subtype if necessary).
194 procedure Rewrite_Comparison (N : Node_Id);
195 -- N is the node for a compile time comparison. If this outcome of this
196 -- comparison can be determined at compile time, then the node N can be
197 -- rewritten with True or False. If the outcome cannot be determined at
198 -- compile time, the call has no effect.
200 function Tagged_Membership (N : Node_Id) return Node_Id;
201 -- Construct the expression corresponding to the tagged membership test.
202 -- Deals with a second operand being (or not) a class-wide type.
204 function Safe_In_Place_Array_Op
207 Op2 : Node_Id) return Boolean;
208 -- In the context of an assignment, where the right-hand side is a
209 -- boolean operation on arrays, check whether operation can be performed
212 procedure Unary_Op_Validity_Checks (N : Node_Id);
213 pragma Inline (Unary_Op_Validity_Checks);
214 -- Performs validity checks for a unary operator
216 -------------------------------
217 -- Binary_Op_Validity_Checks --
218 -------------------------------
220 procedure Binary_Op_Validity_Checks (N : Node_Id) is
222 if Validity_Checks_On and Validity_Check_Operands then
223 Ensure_Valid (Left_Opnd (N));
224 Ensure_Valid (Right_Opnd (N));
226 end Binary_Op_Validity_Checks;
228 ------------------------------------
229 -- Build_Boolean_Array_Proc_Call --
230 ------------------------------------
232 procedure Build_Boolean_Array_Proc_Call
237 Loc : constant Source_Ptr := Sloc (N);
238 Kind : constant Node_Kind := Nkind (Expression (N));
239 Target : constant Node_Id :=
240 Make_Attribute_Reference (Loc,
242 Attribute_Name => Name_Address);
244 Arg1 : constant Node_Id := Op1;
245 Arg2 : Node_Id := Op2;
247 Proc_Name : Entity_Id;
250 if Kind = N_Op_Not then
251 if Nkind (Op1) in N_Binary_Op then
253 -- Use negated version of the binary operators
255 if Nkind (Op1) = N_Op_And then
256 Proc_Name := RTE (RE_Vector_Nand);
258 elsif Nkind (Op1) = N_Op_Or then
259 Proc_Name := RTE (RE_Vector_Nor);
261 else pragma Assert (Nkind (Op1) = N_Op_Xor);
262 Proc_Name := RTE (RE_Vector_Xor);
266 Make_Procedure_Call_Statement (Loc,
267 Name => New_Occurrence_Of (Proc_Name, Loc),
269 Parameter_Associations => New_List (
271 Make_Attribute_Reference (Loc,
272 Prefix => Left_Opnd (Op1),
273 Attribute_Name => Name_Address),
275 Make_Attribute_Reference (Loc,
276 Prefix => Right_Opnd (Op1),
277 Attribute_Name => Name_Address),
279 Make_Attribute_Reference (Loc,
280 Prefix => Left_Opnd (Op1),
281 Attribute_Name => Name_Length)));
284 Proc_Name := RTE (RE_Vector_Not);
287 Make_Procedure_Call_Statement (Loc,
288 Name => New_Occurrence_Of (Proc_Name, Loc),
289 Parameter_Associations => New_List (
292 Make_Attribute_Reference (Loc,
294 Attribute_Name => Name_Address),
296 Make_Attribute_Reference (Loc,
298 Attribute_Name => Name_Length)));
302 -- We use the following equivalences:
304 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
305 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
306 -- (not X) xor (not Y) = X xor Y
307 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
309 if Nkind (Op1) = N_Op_Not then
310 if Kind = N_Op_And then
311 Proc_Name := RTE (RE_Vector_Nor);
313 elsif Kind = N_Op_Or then
314 Proc_Name := RTE (RE_Vector_Nand);
317 Proc_Name := RTE (RE_Vector_Xor);
321 if Kind = N_Op_And then
322 Proc_Name := RTE (RE_Vector_And);
324 elsif Kind = N_Op_Or then
325 Proc_Name := RTE (RE_Vector_Or);
327 elsif Nkind (Op2) = N_Op_Not then
328 Proc_Name := RTE (RE_Vector_Nxor);
329 Arg2 := Right_Opnd (Op2);
332 Proc_Name := RTE (RE_Vector_Xor);
337 Make_Procedure_Call_Statement (Loc,
338 Name => New_Occurrence_Of (Proc_Name, Loc),
339 Parameter_Associations => New_List (
341 Make_Attribute_Reference (Loc,
343 Attribute_Name => Name_Address),
344 Make_Attribute_Reference (Loc,
346 Attribute_Name => Name_Address),
347 Make_Attribute_Reference (Loc,
349 Attribute_Name => Name_Length)));
352 Rewrite (N, Call_Node);
356 when RE_Not_Available =>
358 end Build_Boolean_Array_Proc_Call;
360 ---------------------------------
361 -- Expand_Allocator_Expression --
362 ---------------------------------
364 procedure Expand_Allocator_Expression (N : Node_Id) is
365 Loc : constant Source_Ptr := Sloc (N);
366 Exp : constant Node_Id := Expression (Expression (N));
367 Indic : constant Node_Id := Subtype_Mark (Expression (N));
368 PtrT : constant Entity_Id := Etype (N);
369 T : constant Entity_Id := Entity (Indic);
374 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
376 Tag_Assign : Node_Id;
380 if Is_Tagged_Type (T) or else Controlled_Type (T) then
382 -- Actions inserted before:
383 -- Temp : constant ptr_T := new T'(Expression);
384 -- <no CW> Temp._tag := T'tag;
385 -- <CTRL> Adjust (Finalizable (Temp.all));
386 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
388 -- We analyze by hand the new internal allocator to avoid
389 -- any recursion and inappropriate call to Initialize
391 if not Aggr_In_Place then
392 Remove_Side_Effects (Exp);
396 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
398 -- For a class wide allocation generate the following code:
400 -- type Equiv_Record is record ... end record;
401 -- implicit subtype CW is <Class_Wide_Subytpe>;
402 -- temp : PtrT := new CW'(CW!(expr));
404 if Is_Class_Wide_Type (T) then
405 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
407 Set_Expression (Expression (N),
408 Unchecked_Convert_To (Entity (Indic), Exp));
410 Analyze_And_Resolve (Expression (N), Entity (Indic));
413 if Aggr_In_Place then
415 Make_Object_Declaration (Loc,
416 Defining_Identifier => Temp,
417 Object_Definition => New_Reference_To (PtrT, Loc),
420 New_Reference_To (Etype (Exp), Loc)));
422 Set_Comes_From_Source
423 (Expression (Tmp_Node), Comes_From_Source (N));
425 Set_No_Initialization (Expression (Tmp_Node));
426 Insert_Action (N, Tmp_Node);
428 if Controlled_Type (T)
429 and then Ekind (PtrT) = E_Anonymous_Access_Type
431 -- Create local finalization list for access parameter
433 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
436 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
438 Node := Relocate_Node (N);
441 Make_Object_Declaration (Loc,
442 Defining_Identifier => Temp,
443 Constant_Present => True,
444 Object_Definition => New_Reference_To (PtrT, Loc),
445 Expression => Node));
448 -- Suppress the tag assignment when Java_VM because JVM tags
449 -- are represented implicitly in objects.
451 if Is_Tagged_Type (T)
452 and then not Is_Class_Wide_Type (T)
456 Make_Assignment_Statement (Loc,
458 Make_Selected_Component (Loc,
459 Prefix => New_Reference_To (Temp, Loc),
461 New_Reference_To (Tag_Component (T), Loc)),
464 Unchecked_Convert_To (RTE (RE_Tag),
465 New_Reference_To (Access_Disp_Table (T), Loc)));
467 -- The previous assignment has to be done in any case
469 Set_Assignment_OK (Name (Tag_Assign));
470 Insert_Action (N, Tag_Assign);
472 elsif Is_Private_Type (T)
473 and then Is_Tagged_Type (Underlying_Type (T))
477 Utyp : constant Entity_Id := Underlying_Type (T);
478 Ref : constant Node_Id :=
479 Unchecked_Convert_To (Utyp,
480 Make_Explicit_Dereference (Loc,
481 New_Reference_To (Temp, Loc)));
485 Make_Assignment_Statement (Loc,
487 Make_Selected_Component (Loc,
490 New_Reference_To (Tag_Component (Utyp), Loc)),
493 Unchecked_Convert_To (RTE (RE_Tag),
495 Access_Disp_Table (Utyp), Loc)));
497 Set_Assignment_OK (Name (Tag_Assign));
498 Insert_Action (N, Tag_Assign);
502 if Controlled_Type (Designated_Type (PtrT))
503 and then Controlled_Type (T)
507 Apool : constant Entity_Id :=
508 Associated_Storage_Pool (PtrT);
511 -- If it is an allocation on the secondary stack
512 -- (i.e. a value returned from a function), the object
513 -- is attached on the caller side as soon as the call
514 -- is completed (see Expand_Ctrl_Function_Call)
516 if Is_RTE (Apool, RE_SS_Pool) then
518 F : constant Entity_Id :=
519 Make_Defining_Identifier (Loc,
520 New_Internal_Name ('F'));
523 Make_Object_Declaration (Loc,
524 Defining_Identifier => F,
525 Object_Definition => New_Reference_To (RTE
526 (RE_Finalizable_Ptr), Loc)));
528 Flist := New_Reference_To (F, Loc);
529 Attach := Make_Integer_Literal (Loc, 1);
532 -- Normal case, not a secondary stack allocation
535 if Controlled_Type (T)
536 and then Ekind (PtrT) = E_Anonymous_Access_Type
538 -- Create local finalization list for access parameter
541 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
543 Flist := Find_Final_List (PtrT);
546 Attach := Make_Integer_Literal (Loc, 2);
549 if not Aggr_In_Place then
554 -- An unchecked conversion is needed in the
555 -- classwide case because the designated type
556 -- can be an ancestor of the subtype mark of
559 Unchecked_Convert_To (T,
560 Make_Explicit_Dereference (Loc,
561 New_Reference_To (Temp, Loc))),
565 With_Attach => Attach));
570 Rewrite (N, New_Reference_To (Temp, Loc));
571 Analyze_And_Resolve (N, PtrT);
573 elsif Aggr_In_Place then
575 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
577 Make_Object_Declaration (Loc,
578 Defining_Identifier => Temp,
579 Object_Definition => New_Reference_To (PtrT, Loc),
580 Expression => Make_Allocator (Loc,
581 New_Reference_To (Etype (Exp), Loc)));
583 Set_Comes_From_Source
584 (Expression (Tmp_Node), Comes_From_Source (N));
586 Set_No_Initialization (Expression (Tmp_Node));
587 Insert_Action (N, Tmp_Node);
588 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
589 Rewrite (N, New_Reference_To (Temp, Loc));
590 Analyze_And_Resolve (N, PtrT);
592 elsif Is_Access_Type (Designated_Type (PtrT))
593 and then Nkind (Exp) = N_Allocator
594 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
596 -- Apply constraint to designated subtype indication
598 Apply_Constraint_Check (Expression (Exp),
599 Designated_Type (Designated_Type (PtrT)),
602 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
604 -- Propagate constraint_error to enclosing allocator
606 Rewrite (Exp, New_Copy (Expression (Exp)));
609 -- First check against the type of the qualified expression
611 -- NOTE: The commented call should be correct, but for
612 -- some reason causes the compiler to bomb (sigsegv) on
613 -- ACVC test c34007g, so for now we just perform the old
614 -- (incorrect) test against the designated subtype with
615 -- no sliding in the else part of the if statement below.
618 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
620 -- A check is also needed in cases where the designated
621 -- subtype is constrained and differs from the subtype
622 -- given in the qualified expression. Note that the check
623 -- on the qualified expression does not allow sliding,
624 -- but this check does (a relaxation from Ada 83).
626 if Is_Constrained (Designated_Type (PtrT))
627 and then not Subtypes_Statically_Match
628 (T, Designated_Type (PtrT))
630 Apply_Constraint_Check
631 (Exp, Designated_Type (PtrT), No_Sliding => False);
633 -- The nonsliding check should really be performed
634 -- (unconditionally) against the subtype of the
635 -- qualified expression, but that causes a problem
636 -- with c34007g (see above), so for now we retain this.
639 Apply_Constraint_Check
640 (Exp, Designated_Type (PtrT), No_Sliding => True);
645 when RE_Not_Available =>
647 end Expand_Allocator_Expression;
649 -----------------------------
650 -- Expand_Array_Comparison --
651 -----------------------------
653 -- Expansion is only required in the case of array types. For the
654 -- unpacked case, an appropriate runtime routine is called. For
655 -- packed cases, and also in some other cases where a runtime
656 -- routine cannot be called, the form of the expansion is:
658 -- [body for greater_nn; boolean_expression]
660 -- The body is built by Make_Array_Comparison_Op, and the form of the
661 -- Boolean expression depends on the operator involved.
663 procedure Expand_Array_Comparison (N : Node_Id) is
664 Loc : constant Source_Ptr := Sloc (N);
665 Op1 : Node_Id := Left_Opnd (N);
666 Op2 : Node_Id := Right_Opnd (N);
667 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
668 Ctyp : constant Entity_Id := Component_Type (Typ1);
672 Func_Name : Entity_Id;
676 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
677 -- True for byte addressable target
679 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
680 -- Returns True if the length of the given operand is known to be
681 -- less than 4. Returns False if this length is known to be four
682 -- or greater or is not known at compile time.
684 ------------------------
685 -- Length_Less_Than_4 --
686 ------------------------
688 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
689 Otyp : constant Entity_Id := Etype (Opnd);
692 if Ekind (Otyp) = E_String_Literal_Subtype then
693 return String_Literal_Length (Otyp) < 4;
697 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
698 Lo : constant Node_Id := Type_Low_Bound (Ityp);
699 Hi : constant Node_Id := Type_High_Bound (Ityp);
704 if Compile_Time_Known_Value (Lo) then
705 Lov := Expr_Value (Lo);
710 if Compile_Time_Known_Value (Hi) then
711 Hiv := Expr_Value (Hi);
716 return Hiv < Lov + 3;
719 end Length_Less_Than_4;
721 -- Start of processing for Expand_Array_Comparison
724 -- Deal first with unpacked case, where we can call a runtime routine
725 -- except that we avoid this for targets for which are not addressable
726 -- by bytes, and for the JVM, since the JVM does not support direct
727 -- addressing of array components.
729 if not Is_Bit_Packed_Array (Typ1)
730 and then Byte_Addressable
733 -- The call we generate is:
735 -- Compare_Array_xn[_Unaligned]
736 -- (left'address, right'address, left'length, right'length) <op> 0
738 -- x = U for unsigned, S for signed
739 -- n = 8,16,32,64 for component size
740 -- Add _Unaligned if length < 4 and component size is 8.
741 -- <op> is the standard comparison operator
743 if Component_Size (Typ1) = 8 then
744 if Length_Less_Than_4 (Op1)
746 Length_Less_Than_4 (Op2)
748 if Is_Unsigned_Type (Ctyp) then
749 Comp := RE_Compare_Array_U8_Unaligned;
751 Comp := RE_Compare_Array_S8_Unaligned;
755 if Is_Unsigned_Type (Ctyp) then
756 Comp := RE_Compare_Array_U8;
758 Comp := RE_Compare_Array_S8;
762 elsif Component_Size (Typ1) = 16 then
763 if Is_Unsigned_Type (Ctyp) then
764 Comp := RE_Compare_Array_U16;
766 Comp := RE_Compare_Array_S16;
769 elsif Component_Size (Typ1) = 32 then
770 if Is_Unsigned_Type (Ctyp) then
771 Comp := RE_Compare_Array_U32;
773 Comp := RE_Compare_Array_S32;
776 else pragma Assert (Component_Size (Typ1) = 64);
777 if Is_Unsigned_Type (Ctyp) then
778 Comp := RE_Compare_Array_U64;
780 Comp := RE_Compare_Array_S64;
784 Remove_Side_Effects (Op1, Name_Req => True);
785 Remove_Side_Effects (Op2, Name_Req => True);
788 Make_Function_Call (Sloc (Op1),
789 Name => New_Occurrence_Of (RTE (Comp), Loc),
791 Parameter_Associations => New_List (
792 Make_Attribute_Reference (Loc,
793 Prefix => Relocate_Node (Op1),
794 Attribute_Name => Name_Address),
796 Make_Attribute_Reference (Loc,
797 Prefix => Relocate_Node (Op2),
798 Attribute_Name => Name_Address),
800 Make_Attribute_Reference (Loc,
801 Prefix => Relocate_Node (Op1),
802 Attribute_Name => Name_Length),
804 Make_Attribute_Reference (Loc,
805 Prefix => Relocate_Node (Op2),
806 Attribute_Name => Name_Length))));
809 Make_Integer_Literal (Sloc (Op2),
812 Analyze_And_Resolve (Op1, Standard_Integer);
813 Analyze_And_Resolve (Op2, Standard_Integer);
817 -- Cases where we cannot make runtime call
819 -- For (a <= b) we convert to not (a > b)
821 if Chars (N) = Name_Op_Le then
827 Right_Opnd => Op2)));
828 Analyze_And_Resolve (N, Standard_Boolean);
831 -- For < the Boolean expression is
832 -- greater__nn (op2, op1)
834 elsif Chars (N) = Name_Op_Lt then
835 Func_Body := Make_Array_Comparison_Op (Typ1, N);
839 Op1 := Right_Opnd (N);
840 Op2 := Left_Opnd (N);
842 -- For (a >= b) we convert to not (a < b)
844 elsif Chars (N) = Name_Op_Ge then
850 Right_Opnd => Op2)));
851 Analyze_And_Resolve (N, Standard_Boolean);
854 -- For > the Boolean expression is
855 -- greater__nn (op1, op2)
858 pragma Assert (Chars (N) = Name_Op_Gt);
859 Func_Body := Make_Array_Comparison_Op (Typ1, N);
862 Func_Name := Defining_Unit_Name (Specification (Func_Body));
864 Make_Function_Call (Loc,
865 Name => New_Reference_To (Func_Name, Loc),
866 Parameter_Associations => New_List (Op1, Op2));
868 Insert_Action (N, Func_Body);
870 Analyze_And_Resolve (N, Standard_Boolean);
873 when RE_Not_Available =>
875 end Expand_Array_Comparison;
877 ---------------------------
878 -- Expand_Array_Equality --
879 ---------------------------
881 -- Expand an equality function for multi-dimensional arrays. Here is
882 -- an example of such a function for Nb_Dimension = 2
884 -- function Enn (A : atyp; B : btyp) return boolean is
886 -- if (A'length (1) = 0 or else A'length (2) = 0)
888 -- (B'length (1) = 0 or else B'length (2) = 0)
890 -- return True; -- RM 4.5.2(22)
893 -- if A'length (1) /= B'length (1)
895 -- A'length (2) /= B'length (2)
897 -- return False; -- RM 4.5.2(23)
901 -- A1 : Index_T1 := A'first (1);
902 -- B1 : Index_T1 := B'first (1);
906 -- A2 : Index_T2 := A'first (2);
907 -- B2 : Index_T2 := B'first (2);
910 -- if A (A1, A2) /= B (B1, B2) then
914 -- exit when A2 = A'last (2);
915 -- A2 := Index_T2'succ (A2);
916 -- B2 := Index_T2'succ (B2);
920 -- exit when A1 = A'last (1);
921 -- A1 := Index_T1'succ (A1);
922 -- B1 := Index_T1'succ (B1);
929 -- Note on the formal types used (atyp and btyp). If either of the
930 -- arrays is of a private type, we use the underlying type, and
931 -- do an unchecked conversion of the actual. If either of the arrays
932 -- has a bound depending on a discriminant, then we use the base type
933 -- since otherwise we have an escaped discriminant in the function.
935 -- If both arrays are constrained and have the same bounds, we can
936 -- generate a loop with an explicit iteration scheme using a 'Range
937 -- attribute over the first array.
939 function Expand_Array_Equality
944 Typ : Entity_Id) return Node_Id
946 Loc : constant Source_Ptr := Sloc (Nod);
947 Decls : constant List_Id := New_List;
948 Index_List1 : constant List_Id := New_List;
949 Index_List2 : constant List_Id := New_List;
953 Func_Name : Entity_Id;
956 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
957 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
961 -- The parameter types to be used for the formals
966 Num : Int) return Node_Id;
967 -- This builds the attribute reference Arr'Nam (Expr)
969 function Component_Equality (Typ : Entity_Id) return Node_Id;
970 -- Create one statement to compare corresponding components,
971 -- designated by a full set of indices.
973 function Get_Arg_Type (N : Node_Id) return Entity_Id;
974 -- Given one of the arguments, computes the appropriate type to
975 -- be used for that argument in the corresponding function formal
977 function Handle_One_Dimension
979 Index : Node_Id) return Node_Id;
980 -- This procedure returns the following code
983 -- Bn : Index_T := B'First (N);
987 -- exit when An = A'Last (N);
988 -- An := Index_T'Succ (An)
989 -- Bn := Index_T'Succ (Bn)
993 -- If both indices are constrained and identical, the procedure
994 -- returns a simpler loop:
996 -- for An in A'Range (N) loop
1000 -- N is the dimension for which we are generating a loop. Index is the
1001 -- N'th index node, whose Etype is Index_Type_n in the above code.
1002 -- The xxx statement is either the loop or declare for the next
1003 -- dimension or if this is the last dimension the comparison
1004 -- of corresponding components of the arrays.
1006 -- The actual way the code works is to return the comparison
1007 -- of corresponding components for the N+1 call. That's neater!
1009 function Test_Empty_Arrays return Node_Id;
1010 -- This function constructs the test for both arrays being empty
1011 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1013 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1015 function Test_Lengths_Correspond return Node_Id;
1016 -- This function constructs the test for arrays having different
1017 -- lengths in at least one index position, in which case resull
1019 -- A'length (1) /= B'length (1)
1021 -- A'length (2) /= B'length (2)
1032 Num : Int) return Node_Id
1036 Make_Attribute_Reference (Loc,
1037 Attribute_Name => Nam,
1038 Prefix => New_Reference_To (Arr, Loc),
1039 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1042 ------------------------
1043 -- Component_Equality --
1044 ------------------------
1046 function Component_Equality (Typ : Entity_Id) return Node_Id is
1051 -- if a(i1...) /= b(j1...) then return false; end if;
1054 Make_Indexed_Component (Loc,
1055 Prefix => Make_Identifier (Loc, Chars (A)),
1056 Expressions => Index_List1);
1059 Make_Indexed_Component (Loc,
1060 Prefix => Make_Identifier (Loc, Chars (B)),
1061 Expressions => Index_List2);
1063 Test := Expand_Composite_Equality
1064 (Nod, Component_Type (Typ), L, R, Decls);
1067 Make_Implicit_If_Statement (Nod,
1068 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1069 Then_Statements => New_List (
1070 Make_Return_Statement (Loc,
1071 Expression => New_Occurrence_Of (Standard_False, Loc))));
1072 end Component_Equality;
1078 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1089 T := Underlying_Type (T);
1091 X := First_Index (T);
1092 while Present (X) loop
1093 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1095 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1108 --------------------------
1109 -- Handle_One_Dimension --
1110 ---------------------------
1112 function Handle_One_Dimension
1114 Index : Node_Id) return Node_Id
1116 Need_Separate_Indexes : constant Boolean :=
1118 or else not Is_Constrained (Ltyp);
1119 -- If the index types are identical, and we are working with
1120 -- constrained types, then we can use the same index for both of
1123 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1124 Chars => New_Internal_Name ('A'));
1127 Index_T : Entity_Id;
1132 if N > Number_Dimensions (Ltyp) then
1133 return Component_Equality (Ltyp);
1136 -- Case where we generate a loop
1138 Index_T := Base_Type (Etype (Index));
1140 if Need_Separate_Indexes then
1142 Make_Defining_Identifier (Loc,
1143 Chars => New_Internal_Name ('B'));
1148 Append (New_Reference_To (An, Loc), Index_List1);
1149 Append (New_Reference_To (Bn, Loc), Index_List2);
1151 Stm_List := New_List (
1152 Handle_One_Dimension (N + 1, Next_Index (Index)));
1154 if Need_Separate_Indexes then
1155 -- Generate guard for loop, followed by increments of indices
1157 Append_To (Stm_List,
1158 Make_Exit_Statement (Loc,
1161 Left_Opnd => New_Reference_To (An, Loc),
1162 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1164 Append_To (Stm_List,
1165 Make_Assignment_Statement (Loc,
1166 Name => New_Reference_To (An, Loc),
1168 Make_Attribute_Reference (Loc,
1169 Prefix => New_Reference_To (Index_T, Loc),
1170 Attribute_Name => Name_Succ,
1171 Expressions => New_List (New_Reference_To (An, Loc)))));
1173 Append_To (Stm_List,
1174 Make_Assignment_Statement (Loc,
1175 Name => New_Reference_To (Bn, Loc),
1177 Make_Attribute_Reference (Loc,
1178 Prefix => New_Reference_To (Index_T, Loc),
1179 Attribute_Name => Name_Succ,
1180 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1183 -- If separate indexes, we need a declare block for An and Bn,
1184 -- and a loop without an iteration scheme.
1186 if Need_Separate_Indexes then
1188 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1191 Make_Block_Statement (Loc,
1192 Declarations => New_List (
1193 Make_Object_Declaration (Loc,
1194 Defining_Identifier => An,
1195 Object_Definition => New_Reference_To (Index_T, Loc),
1196 Expression => Arr_Attr (A, Name_First, N)),
1198 Make_Object_Declaration (Loc,
1199 Defining_Identifier => Bn,
1200 Object_Definition => New_Reference_To (Index_T, Loc),
1201 Expression => Arr_Attr (B, Name_First, N))),
1203 Handled_Statement_Sequence =>
1204 Make_Handled_Sequence_Of_Statements (Loc,
1205 Statements => New_List (Loop_Stm)));
1207 -- If no separate indexes, return loop statement with explicit
1208 -- iteration scheme on its own
1212 Make_Implicit_Loop_Statement (Nod,
1213 Statements => Stm_List,
1215 Make_Iteration_Scheme (Loc,
1216 Loop_Parameter_Specification =>
1217 Make_Loop_Parameter_Specification (Loc,
1218 Defining_Identifier => An,
1219 Discrete_Subtype_Definition =>
1220 Arr_Attr (A, Name_Range, N))));
1223 end Handle_One_Dimension;
1225 -----------------------
1226 -- Test_Empty_Arrays --
1227 -----------------------
1229 function Test_Empty_Arrays return Node_Id is
1239 for J in 1 .. Number_Dimensions (Ltyp) loop
1242 Left_Opnd => Arr_Attr (A, Name_Length, J),
1243 Right_Opnd => Make_Integer_Literal (Loc, 0));
1247 Left_Opnd => Arr_Attr (B, Name_Length, J),
1248 Right_Opnd => Make_Integer_Literal (Loc, 0));
1257 Left_Opnd => Relocate_Node (Alist),
1258 Right_Opnd => Atest);
1262 Left_Opnd => Relocate_Node (Blist),
1263 Right_Opnd => Btest);
1270 Right_Opnd => Blist);
1271 end Test_Empty_Arrays;
1273 -----------------------------
1274 -- Test_Lengths_Correspond --
1275 -----------------------------
1277 function Test_Lengths_Correspond return Node_Id is
1283 for J in 1 .. Number_Dimensions (Ltyp) loop
1286 Left_Opnd => Arr_Attr (A, Name_Length, J),
1287 Right_Opnd => Arr_Attr (B, Name_Length, J));
1294 Left_Opnd => Relocate_Node (Result),
1295 Right_Opnd => Rtest);
1300 end Test_Lengths_Correspond;
1302 -- Start of processing for Expand_Array_Equality
1305 Ltyp := Get_Arg_Type (Lhs);
1306 Rtyp := Get_Arg_Type (Rhs);
1308 -- For now, if the argument types are not the same, go to the
1309 -- base type, since the code assumes that the formals have the
1310 -- same type. This is fixable in future ???
1312 if Ltyp /= Rtyp then
1313 Ltyp := Base_Type (Ltyp);
1314 Rtyp := Base_Type (Rtyp);
1315 pragma Assert (Ltyp = Rtyp);
1318 -- Build list of formals for function
1320 Formals := New_List (
1321 Make_Parameter_Specification (Loc,
1322 Defining_Identifier => A,
1323 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1325 Make_Parameter_Specification (Loc,
1326 Defining_Identifier => B,
1327 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1329 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1331 -- Build statement sequence for function
1334 Make_Subprogram_Body (Loc,
1336 Make_Function_Specification (Loc,
1337 Defining_Unit_Name => Func_Name,
1338 Parameter_Specifications => Formals,
1339 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)),
1341 Declarations => Decls,
1343 Handled_Statement_Sequence =>
1344 Make_Handled_Sequence_Of_Statements (Loc,
1345 Statements => New_List (
1347 Make_Implicit_If_Statement (Nod,
1348 Condition => Test_Empty_Arrays,
1349 Then_Statements => New_List (
1350 Make_Return_Statement (Loc,
1352 New_Occurrence_Of (Standard_True, Loc)))),
1354 Make_Implicit_If_Statement (Nod,
1355 Condition => Test_Lengths_Correspond,
1356 Then_Statements => New_List (
1357 Make_Return_Statement (Loc,
1359 New_Occurrence_Of (Standard_False, Loc)))),
1361 Handle_One_Dimension (1, First_Index (Ltyp)),
1363 Make_Return_Statement (Loc,
1364 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1366 Set_Has_Completion (Func_Name, True);
1367 Set_Is_Inlined (Func_Name);
1369 -- If the array type is distinct from the type of the arguments,
1370 -- it is the full view of a private type. Apply an unchecked
1371 -- conversion to insure that analysis of the call succeeds.
1381 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1383 L := OK_Convert_To (Ltyp, Lhs);
1387 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1389 R := OK_Convert_To (Rtyp, Rhs);
1392 Actuals := New_List (L, R);
1395 Append_To (Bodies, Func_Body);
1398 Make_Function_Call (Loc,
1399 Name => New_Reference_To (Func_Name, Loc),
1400 Parameter_Associations => Actuals);
1401 end Expand_Array_Equality;
1403 -----------------------------
1404 -- Expand_Boolean_Operator --
1405 -----------------------------
1407 -- Note that we first get the actual subtypes of the operands,
1408 -- since we always want to deal with types that have bounds.
1410 procedure Expand_Boolean_Operator (N : Node_Id) is
1411 Typ : constant Entity_Id := Etype (N);
1414 if Is_Bit_Packed_Array (Typ) then
1415 Expand_Packed_Boolean_Operator (N);
1418 -- For the normal non-packed case, the general expansion is
1419 -- to build a function for carrying out the comparison (using
1420 -- Make_Boolean_Array_Op) and then inserting it into the tree.
1421 -- The original operator node is then rewritten as a call to
1425 Loc : constant Source_Ptr := Sloc (N);
1426 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1427 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1428 Func_Body : Node_Id;
1429 Func_Name : Entity_Id;
1432 Convert_To_Actual_Subtype (L);
1433 Convert_To_Actual_Subtype (R);
1434 Ensure_Defined (Etype (L), N);
1435 Ensure_Defined (Etype (R), N);
1436 Apply_Length_Check (R, Etype (L));
1438 if Nkind (Parent (N)) = N_Assignment_Statement
1439 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1441 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1443 elsif Nkind (Parent (N)) = N_Op_Not
1444 and then Nkind (N) = N_Op_And
1446 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1451 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1452 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1453 Insert_Action (N, Func_Body);
1455 -- Now rewrite the expression with a call
1458 Make_Function_Call (Loc,
1459 Name => New_Reference_To (Func_Name, Loc),
1460 Parameter_Associations =>
1462 (L, Make_Type_Conversion
1463 (Loc, New_Reference_To (Etype (L), Loc), R))));
1465 Analyze_And_Resolve (N, Typ);
1469 end Expand_Boolean_Operator;
1471 -------------------------------
1472 -- Expand_Composite_Equality --
1473 -------------------------------
1475 -- This function is only called for comparing internal fields of composite
1476 -- types when these fields are themselves composites. This is a special
1477 -- case because it is not possible to respect normal Ada visibility rules.
1479 function Expand_Composite_Equality
1484 Bodies : List_Id) return Node_Id
1486 Loc : constant Source_Ptr := Sloc (Nod);
1487 Full_Type : Entity_Id;
1492 if Is_Private_Type (Typ) then
1493 Full_Type := Underlying_Type (Typ);
1498 -- Defense against malformed private types with no completion
1499 -- the error will be diagnosed later by check_completion
1501 if No (Full_Type) then
1502 return New_Reference_To (Standard_False, Loc);
1505 Full_Type := Base_Type (Full_Type);
1507 if Is_Array_Type (Full_Type) then
1509 -- If the operand is an elementary type other than a floating-point
1510 -- type, then we can simply use the built-in block bitwise equality,
1511 -- since the predefined equality operators always apply and bitwise
1512 -- equality is fine for all these cases.
1514 if Is_Elementary_Type (Component_Type (Full_Type))
1515 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1517 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1519 -- For composite component types, and floating-point types, use
1520 -- the expansion. This deals with tagged component types (where
1521 -- we use the applicable equality routine) and floating-point,
1522 -- (where we need to worry about negative zeroes), and also the
1523 -- case of any composite type recursively containing such fields.
1526 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1529 elsif Is_Tagged_Type (Full_Type) then
1531 -- Call the primitive operation "=" of this type
1533 if Is_Class_Wide_Type (Full_Type) then
1534 Full_Type := Root_Type (Full_Type);
1537 -- If this is derived from an untagged private type completed
1538 -- with a tagged type, it does not have a full view, so we
1539 -- use the primitive operations of the private type.
1540 -- This check should no longer be necessary when these
1541 -- types receive their full views ???
1543 if Is_Private_Type (Typ)
1544 and then not Is_Tagged_Type (Typ)
1545 and then not Is_Controlled (Typ)
1546 and then Is_Derived_Type (Typ)
1547 and then No (Full_View (Typ))
1549 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1551 Prim := First_Elmt (Primitive_Operations (Full_Type));
1555 Eq_Op := Node (Prim);
1556 exit when Chars (Eq_Op) = Name_Op_Eq
1557 and then Etype (First_Formal (Eq_Op)) =
1558 Etype (Next_Formal (First_Formal (Eq_Op)))
1559 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1561 pragma Assert (Present (Prim));
1564 Eq_Op := Node (Prim);
1567 Make_Function_Call (Loc,
1568 Name => New_Reference_To (Eq_Op, Loc),
1569 Parameter_Associations =>
1571 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1572 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1574 elsif Is_Record_Type (Full_Type) then
1575 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1577 if Present (Eq_Op) then
1578 if Etype (First_Formal (Eq_Op)) /= Full_Type then
1580 -- Inherited equality from parent type. Convert the actuals
1581 -- to match signature of operation.
1584 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
1588 Make_Function_Call (Loc,
1589 Name => New_Reference_To (Eq_Op, Loc),
1590 Parameter_Associations =>
1591 New_List (OK_Convert_To (T, Lhs),
1592 OK_Convert_To (T, Rhs)));
1596 -- Comparison between Unchecked_Union components
1598 if Is_Unchecked_Union (Full_Type) then
1600 Lhs_Type : Node_Id := Full_Type;
1601 Rhs_Type : Node_Id := Full_Type;
1602 Lhs_Discr_Val : Node_Id;
1603 Rhs_Discr_Val : Node_Id;
1608 if Nkind (Lhs) = N_Selected_Component then
1609 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
1614 if Nkind (Rhs) = N_Selected_Component then
1615 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
1618 -- Lhs of the composite equality
1620 if Is_Constrained (Lhs_Type) then
1622 -- Since the enclosing record can never be an
1623 -- Unchecked_Union (this code is executed for records
1624 -- that do not have variants), we may reference its
1627 if Nkind (Lhs) = N_Selected_Component
1628 and then Has_Per_Object_Constraint (
1629 Entity (Selector_Name (Lhs)))
1632 Make_Selected_Component (Loc,
1633 Prefix => Prefix (Lhs),
1636 Get_Discriminant_Value (
1637 First_Discriminant (Lhs_Type),
1639 Stored_Constraint (Lhs_Type))));
1642 Lhs_Discr_Val := New_Copy (
1643 Get_Discriminant_Value (
1644 First_Discriminant (Lhs_Type),
1646 Stored_Constraint (Lhs_Type)));
1650 -- It is not possible to infer the discriminant since
1651 -- the subtype is not constrained.
1654 Make_Raise_Program_Error (Loc,
1655 Reason => PE_Unchecked_Union_Restriction));
1657 -- Prevent Gigi from generating illegal code, change
1658 -- the equality to a standard False.
1660 return New_Occurrence_Of (Standard_False, Loc);
1663 -- Rhs of the composite equality
1665 if Is_Constrained (Rhs_Type) then
1666 if Nkind (Rhs) = N_Selected_Component
1667 and then Has_Per_Object_Constraint (
1668 Entity (Selector_Name (Rhs)))
1671 Make_Selected_Component (Loc,
1672 Prefix => Prefix (Rhs),
1675 Get_Discriminant_Value (
1676 First_Discriminant (Rhs_Type),
1678 Stored_Constraint (Rhs_Type))));
1681 Rhs_Discr_Val := New_Copy (
1682 Get_Discriminant_Value (
1683 First_Discriminant (Rhs_Type),
1685 Stored_Constraint (Rhs_Type)));
1690 Make_Raise_Program_Error (Loc,
1691 Reason => PE_Unchecked_Union_Restriction));
1696 -- Call the TSS equality function with the inferred
1697 -- discriminant values.
1700 Make_Function_Call (Loc,
1701 Name => New_Reference_To (Eq_Op, Loc),
1702 Parameter_Associations => New_List (
1710 -- Shouldn't this be an else, we can't fall through
1711 -- the above IF, right???
1714 Make_Function_Call (Loc,
1715 Name => New_Reference_To (Eq_Op, Loc),
1716 Parameter_Associations => New_List (Lhs, Rhs));
1720 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
1724 -- It can be a simple record or the full view of a scalar private
1726 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1728 end Expand_Composite_Equality;
1730 ------------------------------
1731 -- Expand_Concatenate_Other --
1732 ------------------------------
1734 -- Let n be the number of array operands to be concatenated, Base_Typ
1735 -- their base type, Ind_Typ their index type, and Arr_Typ the original
1736 -- array type to which the concatenantion operator applies, then the
1737 -- following subprogram is constructed:
1739 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
1742 -- if S1'Length /= 0 then
1743 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
1744 -- XXX = Arr_Typ'First otherwise
1745 -- elsif S2'Length /= 0 then
1746 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
1747 -- YYY = Arr_Typ'First otherwise
1749 -- elsif Sn-1'Length /= 0 then
1750 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
1751 -- ZZZ = Arr_Typ'First otherwise
1759 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1760 -- + Ind_Typ'Pos (L));
1761 -- R : Base_Typ (L .. H);
1763 -- if S1'Length /= 0 then
1767 -- L := Ind_Typ'Succ (L);
1768 -- exit when P = S1'Last;
1769 -- P := Ind_Typ'Succ (P);
1773 -- if S2'Length /= 0 then
1774 -- L := Ind_Typ'Succ (L);
1777 -- L := Ind_Typ'Succ (L);
1778 -- exit when P = S2'Last;
1779 -- P := Ind_Typ'Succ (P);
1785 -- if Sn'Length /= 0 then
1789 -- L := Ind_Typ'Succ (L);
1790 -- exit when P = Sn'Last;
1791 -- P := Ind_Typ'Succ (P);
1799 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id) is
1800 Loc : constant Source_Ptr := Sloc (Cnode);
1801 Nb_Opnds : constant Nat := List_Length (Opnds);
1803 Arr_Typ : constant Entity_Id := Etype (Entity (Cnode));
1804 Base_Typ : constant Entity_Id := Base_Type (Etype (Cnode));
1805 Ind_Typ : constant Entity_Id := Etype (First_Index (Base_Typ));
1808 Func_Spec : Node_Id;
1809 Param_Specs : List_Id;
1811 Func_Body : Node_Id;
1812 Func_Decls : List_Id;
1813 Func_Stmts : List_Id;
1818 Elsif_List : List_Id;
1820 Declare_Block : Node_Id;
1821 Declare_Decls : List_Id;
1822 Declare_Stmts : List_Id;
1834 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id;
1835 -- Builds the sequence of statement:
1839 -- L := Ind_Typ'Succ (L);
1840 -- exit when P = Si'Last;
1841 -- P := Ind_Typ'Succ (P);
1844 -- where i is the input parameter I given.
1845 -- If the flag Last is true, the exit statement is emitted before
1846 -- incrementing the lower bound, to prevent the creation out of
1849 function Init_L (I : Nat) return Node_Id;
1850 -- Builds the statement:
1851 -- L := Arr_Typ'First; If Arr_Typ is constrained
1852 -- L := Si'First; otherwise (where I is the input param given)
1854 function H return Node_Id;
1855 -- Builds reference to identifier H
1857 function Ind_Val (E : Node_Id) return Node_Id;
1858 -- Builds expression Ind_Typ'Val (E);
1860 function L return Node_Id;
1861 -- Builds reference to identifier L
1863 function L_Pos return Node_Id;
1864 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
1865 -- expression to avoid universal_integer computations whenever possible,
1866 -- in the expression for the upper bound H.
1868 function L_Succ return Node_Id;
1869 -- Builds expression Ind_Typ'Succ (L)
1871 function One return Node_Id;
1872 -- Builds integer literal one
1874 function P return Node_Id;
1875 -- Builds reference to identifier P
1877 function P_Succ return Node_Id;
1878 -- Builds expression Ind_Typ'Succ (P)
1880 function R return Node_Id;
1881 -- Builds reference to identifier R
1883 function S (I : Nat) return Node_Id;
1884 -- Builds reference to identifier Si, where I is the value given
1886 function S_First (I : Nat) return Node_Id;
1887 -- Builds expression Si'First, where I is the value given
1889 function S_Last (I : Nat) return Node_Id;
1890 -- Builds expression Si'Last, where I is the value given
1892 function S_Length (I : Nat) return Node_Id;
1893 -- Builds expression Si'Length, where I is the value given
1895 function S_Length_Test (I : Nat) return Node_Id;
1896 -- Builds expression Si'Length /= 0, where I is the value given
1902 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is
1903 Stmts : constant List_Id := New_List;
1905 Loop_Stmt : Node_Id;
1907 Exit_Stmt : Node_Id;
1912 -- First construct the initializations
1914 P_Start := Make_Assignment_Statement (Loc,
1916 Expression => S_First (I));
1917 Append_To (Stmts, P_Start);
1919 -- Then build the loop
1921 R_Copy := Make_Assignment_Statement (Loc,
1922 Name => Make_Indexed_Component (Loc,
1924 Expressions => New_List (L)),
1925 Expression => Make_Indexed_Component (Loc,
1927 Expressions => New_List (P)));
1929 L_Inc := Make_Assignment_Statement (Loc,
1931 Expression => L_Succ);
1933 Exit_Stmt := Make_Exit_Statement (Loc,
1934 Condition => Make_Op_Eq (Loc, P, S_Last (I)));
1936 P_Inc := Make_Assignment_Statement (Loc,
1938 Expression => P_Succ);
1942 Make_Implicit_Loop_Statement (Cnode,
1943 Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc));
1946 Make_Implicit_Loop_Statement (Cnode,
1947 Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc));
1950 Append_To (Stmts, Loop_Stmt);
1959 function H return Node_Id is
1961 return Make_Identifier (Loc, Name_uH);
1968 function Ind_Val (E : Node_Id) return Node_Id is
1971 Make_Attribute_Reference (Loc,
1972 Prefix => New_Reference_To (Ind_Typ, Loc),
1973 Attribute_Name => Name_Val,
1974 Expressions => New_List (E));
1981 function Init_L (I : Nat) return Node_Id is
1985 if Is_Constrained (Arr_Typ) then
1986 E := Make_Attribute_Reference (Loc,
1987 Prefix => New_Reference_To (Arr_Typ, Loc),
1988 Attribute_Name => Name_First);
1994 return Make_Assignment_Statement (Loc, Name => L, Expression => E);
2001 function L return Node_Id is
2003 return Make_Identifier (Loc, Name_uL);
2010 function L_Pos return Node_Id is
2011 Target_Type : Entity_Id;
2014 -- If the index type is an enumeration type, the computation
2015 -- can be done in standard integer. Otherwise, choose a large
2016 -- enough integer type.
2018 if Is_Enumeration_Type (Ind_Typ)
2019 or else Root_Type (Ind_Typ) = Standard_Integer
2020 or else Root_Type (Ind_Typ) = Standard_Short_Integer
2021 or else Root_Type (Ind_Typ) = Standard_Short_Short_Integer
2023 Target_Type := Standard_Integer;
2025 Target_Type := Root_Type (Ind_Typ);
2029 Make_Qualified_Expression (Loc,
2030 Subtype_Mark => New_Reference_To (Target_Type, Loc),
2032 Make_Attribute_Reference (Loc,
2033 Prefix => New_Reference_To (Ind_Typ, Loc),
2034 Attribute_Name => Name_Pos,
2035 Expressions => New_List (L)));
2042 function L_Succ return Node_Id is
2045 Make_Attribute_Reference (Loc,
2046 Prefix => New_Reference_To (Ind_Typ, Loc),
2047 Attribute_Name => Name_Succ,
2048 Expressions => New_List (L));
2055 function One return Node_Id is
2057 return Make_Integer_Literal (Loc, 1);
2064 function P return Node_Id is
2066 return Make_Identifier (Loc, Name_uP);
2073 function P_Succ return Node_Id is
2076 Make_Attribute_Reference (Loc,
2077 Prefix => New_Reference_To (Ind_Typ, Loc),
2078 Attribute_Name => Name_Succ,
2079 Expressions => New_List (P));
2086 function R return Node_Id is
2088 return Make_Identifier (Loc, Name_uR);
2095 function S (I : Nat) return Node_Id is
2097 return Make_Identifier (Loc, New_External_Name ('S', I));
2104 function S_First (I : Nat) return Node_Id is
2106 return Make_Attribute_Reference (Loc,
2108 Attribute_Name => Name_First);
2115 function S_Last (I : Nat) return Node_Id is
2117 return Make_Attribute_Reference (Loc,
2119 Attribute_Name => Name_Last);
2126 function S_Length (I : Nat) return Node_Id is
2128 return Make_Attribute_Reference (Loc,
2130 Attribute_Name => Name_Length);
2137 function S_Length_Test (I : Nat) return Node_Id is
2141 Left_Opnd => S_Length (I),
2142 Right_Opnd => Make_Integer_Literal (Loc, 0));
2145 -- Start of processing for Expand_Concatenate_Other
2148 -- Construct the parameter specs and the overall function spec
2150 Param_Specs := New_List;
2151 for I in 1 .. Nb_Opnds loop
2154 Make_Parameter_Specification (Loc,
2155 Defining_Identifier =>
2156 Make_Defining_Identifier (Loc, New_External_Name ('S', I)),
2157 Parameter_Type => New_Reference_To (Base_Typ, Loc)));
2160 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2162 Make_Function_Specification (Loc,
2163 Defining_Unit_Name => Func_Id,
2164 Parameter_Specifications => Param_Specs,
2165 Subtype_Mark => New_Reference_To (Base_Typ, Loc));
2167 -- Construct L's object declaration
2170 Make_Object_Declaration (Loc,
2171 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uL),
2172 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2174 Func_Decls := New_List (L_Decl);
2176 -- Construct the if-then-elsif statements
2178 Elsif_List := New_List;
2179 for I in 2 .. Nb_Opnds - 1 loop
2180 Append_To (Elsif_List, Make_Elsif_Part (Loc,
2181 Condition => S_Length_Test (I),
2182 Then_Statements => New_List (Init_L (I))));
2186 Make_Implicit_If_Statement (Cnode,
2187 Condition => S_Length_Test (1),
2188 Then_Statements => New_List (Init_L (1)),
2189 Elsif_Parts => Elsif_List,
2190 Else_Statements => New_List (Make_Return_Statement (Loc,
2191 Expression => S (Nb_Opnds))));
2193 -- Construct the declaration for H
2196 Make_Object_Declaration (Loc,
2197 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2198 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2200 H_Init := Make_Op_Subtract (Loc, S_Length (1), One);
2201 for I in 2 .. Nb_Opnds loop
2202 H_Init := Make_Op_Add (Loc, H_Init, S_Length (I));
2204 H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos));
2207 Make_Object_Declaration (Loc,
2208 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uH),
2209 Object_Definition => New_Reference_To (Ind_Typ, Loc),
2210 Expression => H_Init);
2212 -- Construct the declaration for R
2214 R_Range := Make_Range (Loc, Low_Bound => L, High_Bound => H);
2216 Make_Index_Or_Discriminant_Constraint (Loc,
2217 Constraints => New_List (R_Range));
2220 Make_Object_Declaration (Loc,
2221 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uR),
2222 Object_Definition =>
2223 Make_Subtype_Indication (Loc,
2224 Subtype_Mark => New_Reference_To (Base_Typ, Loc),
2225 Constraint => R_Constr));
2227 -- Construct the declarations for the declare block
2229 Declare_Decls := New_List (P_Decl, H_Decl, R_Decl);
2231 -- Construct list of statements for the declare block
2233 Declare_Stmts := New_List;
2234 for I in 1 .. Nb_Opnds loop
2235 Append_To (Declare_Stmts,
2236 Make_Implicit_If_Statement (Cnode,
2237 Condition => S_Length_Test (I),
2238 Then_Statements => Copy_Into_R_S (I, I = Nb_Opnds)));
2241 Append_To (Declare_Stmts, Make_Return_Statement (Loc, Expression => R));
2243 -- Construct the declare block
2245 Declare_Block := Make_Block_Statement (Loc,
2246 Declarations => Declare_Decls,
2247 Handled_Statement_Sequence =>
2248 Make_Handled_Sequence_Of_Statements (Loc, Declare_Stmts));
2250 -- Construct the list of function statements
2252 Func_Stmts := New_List (If_Stmt, Declare_Block);
2254 -- Construct the function body
2257 Make_Subprogram_Body (Loc,
2258 Specification => Func_Spec,
2259 Declarations => Func_Decls,
2260 Handled_Statement_Sequence =>
2261 Make_Handled_Sequence_Of_Statements (Loc, Func_Stmts));
2263 -- Insert the newly generated function in the code. This is analyzed
2264 -- with all checks off, since we have completed all the checks.
2266 -- Note that this does *not* fix the array concatenation bug when the
2267 -- low bound is Integer'first sibce that bug comes from the pointer
2268 -- dereferencing an unconstrained array. An there we need a constraint
2269 -- check to make sure the length of the concatenated array is ok. ???
2271 Insert_Action (Cnode, Func_Body, Suppress => All_Checks);
2273 -- Construct list of arguments for the function call
2276 Operand := First (Opnds);
2277 for I in 1 .. Nb_Opnds loop
2278 Append_To (Params, Relocate_Node (Operand));
2282 -- Insert the function call
2286 Make_Function_Call (Loc, New_Reference_To (Func_Id, Loc), Params));
2288 Analyze_And_Resolve (Cnode, Base_Typ);
2289 Set_Is_Inlined (Func_Id);
2290 end Expand_Concatenate_Other;
2292 -------------------------------
2293 -- Expand_Concatenate_String --
2294 -------------------------------
2296 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id) is
2297 Loc : constant Source_Ptr := Sloc (Cnode);
2298 Opnd1 : constant Node_Id := First (Opnds);
2299 Opnd2 : constant Node_Id := Next (Opnd1);
2300 Typ1 : constant Entity_Id := Base_Type (Etype (Opnd1));
2301 Typ2 : constant Entity_Id := Base_Type (Etype (Opnd2));
2304 -- RE_Id value for function to be called
2307 -- In all cases, we build a call to a routine giving the list of
2308 -- arguments as the parameter list to the routine.
2310 case List_Length (Opnds) is
2312 if Typ1 = Standard_Character then
2313 if Typ2 = Standard_Character then
2314 R := RE_Str_Concat_CC;
2317 pragma Assert (Typ2 = Standard_String);
2318 R := RE_Str_Concat_CS;
2321 elsif Typ1 = Standard_String then
2322 if Typ2 = Standard_Character then
2323 R := RE_Str_Concat_SC;
2326 pragma Assert (Typ2 = Standard_String);
2330 -- If we have anything other than Standard_Character or
2331 -- Standard_String, then we must have had a serious error
2332 -- earlier, so we just abandon the attempt at expansion.
2335 pragma Assert (Serious_Errors_Detected > 0);
2340 R := RE_Str_Concat_3;
2343 R := RE_Str_Concat_4;
2346 R := RE_Str_Concat_5;
2350 raise Program_Error;
2353 -- Now generate the appropriate call
2356 Make_Function_Call (Sloc (Cnode),
2357 Name => New_Occurrence_Of (RTE (R), Loc),
2358 Parameter_Associations => Opnds));
2360 Analyze_And_Resolve (Cnode, Standard_String);
2363 when RE_Not_Available =>
2365 end Expand_Concatenate_String;
2367 ------------------------
2368 -- Expand_N_Allocator --
2369 ------------------------
2371 procedure Expand_N_Allocator (N : Node_Id) is
2372 PtrT : constant Entity_Id := Etype (N);
2373 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2375 Loc : constant Source_Ptr := Sloc (N);
2380 -- RM E.2.3(22). We enforce that the expected type of an allocator
2381 -- shall not be a remote access-to-class-wide-limited-private type
2383 -- Why is this being done at expansion time, seems clearly wrong ???
2385 Validate_Remote_Access_To_Class_Wide_Type (N);
2387 -- Set the Storage Pool
2389 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
2391 if Present (Storage_Pool (N)) then
2392 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
2394 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
2397 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
2398 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
2401 Set_Procedure_To_Call (N,
2402 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
2406 -- Under certain circumstances we can replace an allocator by an
2407 -- access to statically allocated storage. The conditions, as noted
2408 -- in AARM 3.10 (10c) are as follows:
2410 -- Size and initial value is known at compile time
2411 -- Access type is access-to-constant
2413 -- The allocator is not part of a constraint on a record component,
2414 -- because in that case the inserted actions are delayed until the
2415 -- record declaration is fully analyzed, which is too late for the
2416 -- analysis of the rewritten allocator.
2418 if Is_Access_Constant (PtrT)
2419 and then Nkind (Expression (N)) = N_Qualified_Expression
2420 and then Compile_Time_Known_Value (Expression (Expression (N)))
2421 and then Size_Known_At_Compile_Time (Etype (Expression
2423 and then not Is_Record_Type (Current_Scope)
2425 -- Here we can do the optimization. For the allocator
2429 -- We insert an object declaration
2431 -- Tnn : aliased x := y;
2433 -- and replace the allocator by Tnn'Unrestricted_Access.
2434 -- Tnn is marked as requiring static allocation.
2437 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2439 Desig := Subtype_Mark (Expression (N));
2441 -- If context is constrained, use constrained subtype directly,
2442 -- so that the constant is not labelled as having a nomimally
2443 -- unconstrained subtype.
2445 if Entity (Desig) = Base_Type (Dtyp) then
2446 Desig := New_Occurrence_Of (Dtyp, Loc);
2450 Make_Object_Declaration (Loc,
2451 Defining_Identifier => Temp,
2452 Aliased_Present => True,
2453 Constant_Present => Is_Access_Constant (PtrT),
2454 Object_Definition => Desig,
2455 Expression => Expression (Expression (N))));
2458 Make_Attribute_Reference (Loc,
2459 Prefix => New_Occurrence_Of (Temp, Loc),
2460 Attribute_Name => Name_Unrestricted_Access));
2462 Analyze_And_Resolve (N, PtrT);
2464 -- We set the variable as statically allocated, since we don't
2465 -- want it going on the stack of the current procedure!
2467 Set_Is_Statically_Allocated (Temp);
2471 -- Handle case of qualified expression (other than optimization above)
2473 if Nkind (Expression (N)) = N_Qualified_Expression then
2474 Expand_Allocator_Expression (N);
2476 -- If the allocator is for a type which requires initialization, and
2477 -- there is no initial value (i.e. operand is a subtype indication
2478 -- rather than a qualifed expression), then we must generate a call
2479 -- to the initialization routine. This is done using an expression
2482 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
2484 -- Here ptr_T is the pointer type for the allocator, and T is the
2485 -- subtype of the allocator. A special case arises if the designated
2486 -- type of the access type is a task or contains tasks. In this case
2487 -- the call to Init (Temp.all ...) is replaced by code that ensures
2488 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
2489 -- for details). In addition, if the type T is a task T, then the
2490 -- first argument to Init must be converted to the task record type.
2494 T : constant Entity_Id := Entity (Expression (N));
2502 Temp_Decl : Node_Id;
2503 Temp_Type : Entity_Id;
2504 Attach_Level : Uint;
2507 if No_Initialization (N) then
2510 -- Case of no initialization procedure present
2512 elsif not Has_Non_Null_Base_Init_Proc (T) then
2514 -- Case of simple initialization required
2516 if Needs_Simple_Initialization (T) then
2517 Rewrite (Expression (N),
2518 Make_Qualified_Expression (Loc,
2519 Subtype_Mark => New_Occurrence_Of (T, Loc),
2520 Expression => Get_Simple_Init_Val (T, Loc)));
2522 Analyze_And_Resolve (Expression (Expression (N)), T);
2523 Analyze_And_Resolve (Expression (N), T);
2524 Set_Paren_Count (Expression (Expression (N)), 1);
2525 Expand_N_Allocator (N);
2527 -- No initialization required
2533 -- Case of initialization procedure present, must be called
2536 Init := Base_Init_Proc (T);
2539 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
2541 -- Construct argument list for the initialization routine call
2542 -- The CPP constructor needs the address directly
2544 if Is_CPP_Class (T) then
2545 Arg1 := New_Reference_To (Temp, Loc);
2550 Make_Explicit_Dereference (Loc,
2551 Prefix => New_Reference_To (Temp, Loc));
2552 Set_Assignment_OK (Arg1);
2555 -- The initialization procedure expects a specific type.
2556 -- if the context is access to class wide, indicate that
2557 -- the object being allocated has the right specific type.
2559 if Is_Class_Wide_Type (Dtyp) then
2560 Arg1 := Unchecked_Convert_To (T, Arg1);
2564 -- If designated type is a concurrent type or if it is a
2565 -- private type whose definition is a concurrent type,
2566 -- the first argument in the Init routine has to be
2567 -- unchecked conversion to the corresponding record type.
2568 -- If the designated type is a derived type, we also
2569 -- convert the argument to its root type.
2571 if Is_Concurrent_Type (T) then
2573 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
2575 elsif Is_Private_Type (T)
2576 and then Present (Full_View (T))
2577 and then Is_Concurrent_Type (Full_View (T))
2580 Unchecked_Convert_To
2581 (Corresponding_Record_Type (Full_View (T)), Arg1);
2583 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
2586 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
2589 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
2590 Set_Etype (Arg1, Ftyp);
2594 Args := New_List (Arg1);
2596 -- For the task case, pass the Master_Id of the access type
2597 -- as the value of the _Master parameter, and _Chain as the
2598 -- value of the _Chain parameter (_Chain will be defined as
2599 -- part of the generated code for the allocator).
2601 if Has_Task (T) then
2602 if No (Master_Id (Base_Type (PtrT))) then
2604 -- The designated type was an incomplete type, and
2605 -- the access type did not get expanded. Salvage
2608 Expand_N_Full_Type_Declaration
2609 (Parent (Base_Type (PtrT)));
2612 -- If the context of the allocator is a declaration or
2613 -- an assignment, we can generate a meaningful image for
2614 -- it, even though subsequent assignments might remove
2615 -- the connection between task and entity. We build this
2616 -- image when the left-hand side is a simple variable,
2617 -- a simple indexed assignment or a simple selected
2620 if Nkind (Parent (N)) = N_Assignment_Statement then
2622 Nam : constant Node_Id := Name (Parent (N));
2625 if Is_Entity_Name (Nam) then
2627 Build_Task_Image_Decls (
2630 (Entity (Nam), Sloc (Nam)), T);
2632 elsif (Nkind (Nam) = N_Indexed_Component
2633 or else Nkind (Nam) = N_Selected_Component)
2634 and then Is_Entity_Name (Prefix (Nam))
2637 Build_Task_Image_Decls
2638 (Loc, Nam, Etype (Prefix (Nam)));
2640 Decls := Build_Task_Image_Decls (Loc, T, T);
2644 elsif Nkind (Parent (N)) = N_Object_Declaration then
2646 Build_Task_Image_Decls (
2647 Loc, Defining_Identifier (Parent (N)), T);
2650 Decls := Build_Task_Image_Decls (Loc, T, T);
2655 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
2656 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2658 Decl := Last (Decls);
2660 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
2662 -- Has_Task is false, Decls not used
2668 -- Add discriminants if discriminated type
2670 if Has_Discriminants (T) then
2671 Discr := First_Elmt (Discriminant_Constraint (T));
2673 while Present (Discr) loop
2674 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2678 elsif Is_Private_Type (T)
2679 and then Present (Full_View (T))
2680 and then Has_Discriminants (Full_View (T))
2683 First_Elmt (Discriminant_Constraint (Full_View (T)));
2685 while Present (Discr) loop
2686 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2691 -- We set the allocator as analyzed so that when we analyze the
2692 -- expression actions node, we do not get an unwanted recursive
2693 -- expansion of the allocator expression.
2695 Set_Analyzed (N, True);
2696 Node := Relocate_Node (N);
2698 -- Here is the transformation:
2700 -- output: Temp : constant ptr_T := new T;
2701 -- Init (Temp.all, ...);
2702 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
2703 -- <CTRL> Initialize (Finalizable (Temp.all));
2705 -- Here ptr_T is the pointer type for the allocator, and T
2706 -- is the subtype of the allocator.
2709 Make_Object_Declaration (Loc,
2710 Defining_Identifier => Temp,
2711 Constant_Present => True,
2712 Object_Definition => New_Reference_To (Temp_Type, Loc),
2713 Expression => Node);
2715 Set_Assignment_OK (Temp_Decl);
2717 if Is_CPP_Class (T) then
2718 Set_Aliased_Present (Temp_Decl);
2721 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
2723 -- If the designated type is task type or contains tasks,
2724 -- Create block to activate created tasks, and insert
2725 -- declaration for Task_Image variable ahead of call.
2727 if Has_Task (T) then
2729 L : constant List_Id := New_List;
2733 Build_Task_Allocate_Block (L, Node, Args);
2736 Insert_List_Before (First (Declarations (Blk)), Decls);
2737 Insert_Actions (N, L);
2742 Make_Procedure_Call_Statement (Loc,
2743 Name => New_Reference_To (Init, Loc),
2744 Parameter_Associations => Args));
2747 if Controlled_Type (T) then
2748 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
2749 if Ekind (PtrT) = E_Anonymous_Access_Type then
2750 Attach_Level := Uint_1;
2752 Attach_Level := Uint_2;
2756 Ref => New_Copy_Tree (Arg1),
2759 With_Attach => Make_Integer_Literal (Loc,
2763 if Is_CPP_Class (T) then
2765 Make_Attribute_Reference (Loc,
2766 Prefix => New_Reference_To (Temp, Loc),
2767 Attribute_Name => Name_Unchecked_Access));
2769 Rewrite (N, New_Reference_To (Temp, Loc));
2772 Analyze_And_Resolve (N, PtrT);
2778 when RE_Not_Available =>
2780 end Expand_N_Allocator;
2782 -----------------------
2783 -- Expand_N_And_Then --
2784 -----------------------
2786 -- Expand into conditional expression if Actions present, and also
2787 -- deal with optimizing case of arguments being True or False.
2789 procedure Expand_N_And_Then (N : Node_Id) is
2790 Loc : constant Source_Ptr := Sloc (N);
2791 Typ : constant Entity_Id := Etype (N);
2792 Left : constant Node_Id := Left_Opnd (N);
2793 Right : constant Node_Id := Right_Opnd (N);
2797 -- Deal with non-standard booleans
2799 if Is_Boolean_Type (Typ) then
2800 Adjust_Condition (Left);
2801 Adjust_Condition (Right);
2802 Set_Etype (N, Standard_Boolean);
2805 -- Check for cases of left argument is True or False
2807 if Nkind (Left) = N_Identifier then
2809 -- If left argument is True, change (True and then Right) to Right.
2810 -- Any actions associated with Right will be executed unconditionally
2811 -- and can thus be inserted into the tree unconditionally.
2813 if Entity (Left) = Standard_True then
2814 if Present (Actions (N)) then
2815 Insert_Actions (N, Actions (N));
2819 Adjust_Result_Type (N, Typ);
2822 -- If left argument is False, change (False and then Right) to
2823 -- False. In this case we can forget the actions associated with
2824 -- Right, since they will never be executed.
2826 elsif Entity (Left) = Standard_False then
2827 Kill_Dead_Code (Right);
2828 Kill_Dead_Code (Actions (N));
2829 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2830 Adjust_Result_Type (N, Typ);
2835 -- If Actions are present, we expand
2837 -- left and then right
2841 -- if left then right else false end
2843 -- with the actions becoming the Then_Actions of the conditional
2844 -- expression. This conditional expression is then further expanded
2845 -- (and will eventually disappear)
2847 if Present (Actions (N)) then
2848 Actlist := Actions (N);
2850 Make_Conditional_Expression (Loc,
2851 Expressions => New_List (
2854 New_Occurrence_Of (Standard_False, Loc))));
2856 Set_Then_Actions (N, Actlist);
2857 Analyze_And_Resolve (N, Standard_Boolean);
2858 Adjust_Result_Type (N, Typ);
2862 -- No actions present, check for cases of right argument True/False
2864 if Nkind (Right) = N_Identifier then
2866 -- Change (Left and then True) to Left. Note that we know there
2867 -- are no actions associated with the True operand, since we
2868 -- just checked for this case above.
2870 if Entity (Right) = Standard_True then
2873 -- Change (Left and then False) to False, making sure to preserve
2874 -- any side effects associated with the Left operand.
2876 elsif Entity (Right) = Standard_False then
2877 Remove_Side_Effects (Left);
2879 (N, New_Occurrence_Of (Standard_False, Loc));
2883 Adjust_Result_Type (N, Typ);
2884 end Expand_N_And_Then;
2886 -------------------------------------
2887 -- Expand_N_Conditional_Expression --
2888 -------------------------------------
2890 -- Expand into expression actions if then/else actions present
2892 procedure Expand_N_Conditional_Expression (N : Node_Id) is
2893 Loc : constant Source_Ptr := Sloc (N);
2894 Cond : constant Node_Id := First (Expressions (N));
2895 Thenx : constant Node_Id := Next (Cond);
2896 Elsex : constant Node_Id := Next (Thenx);
2897 Typ : constant Entity_Id := Etype (N);
2902 -- If either then or else actions are present, then given:
2904 -- if cond then then-expr else else-expr end
2906 -- we insert the following sequence of actions (using Insert_Actions):
2911 -- Cnn := then-expr;
2917 -- and replace the conditional expression by a reference to Cnn
2919 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
2920 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2923 Make_Implicit_If_Statement (N,
2924 Condition => Relocate_Node (Cond),
2926 Then_Statements => New_List (
2927 Make_Assignment_Statement (Sloc (Thenx),
2928 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
2929 Expression => Relocate_Node (Thenx))),
2931 Else_Statements => New_List (
2932 Make_Assignment_Statement (Sloc (Elsex),
2933 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
2934 Expression => Relocate_Node (Elsex))));
2936 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
2937 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
2939 if Present (Then_Actions (N)) then
2941 (First (Then_Statements (New_If)), Then_Actions (N));
2944 if Present (Else_Actions (N)) then
2946 (First (Else_Statements (New_If)), Else_Actions (N));
2949 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
2952 Make_Object_Declaration (Loc,
2953 Defining_Identifier => Cnn,
2954 Object_Definition => New_Occurrence_Of (Typ, Loc)));
2956 Insert_Action (N, New_If);
2957 Analyze_And_Resolve (N, Typ);
2959 end Expand_N_Conditional_Expression;
2961 -----------------------------------
2962 -- Expand_N_Explicit_Dereference --
2963 -----------------------------------
2965 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
2967 -- The only processing required is an insertion of an explicit
2968 -- dereference call for the checked storage pool case.
2970 Insert_Dereference_Action (Prefix (N));
2971 end Expand_N_Explicit_Dereference;
2977 procedure Expand_N_In (N : Node_Id) is
2978 Loc : constant Source_Ptr := Sloc (N);
2979 Rtyp : constant Entity_Id := Etype (N);
2980 Lop : constant Node_Id := Left_Opnd (N);
2981 Rop : constant Node_Id := Right_Opnd (N);
2982 Static : constant Boolean := Is_OK_Static_Expression (N);
2985 -- If we have an explicit range, do a bit of optimization based
2986 -- on range analysis (we may be able to kill one or both checks).
2988 if Nkind (Rop) = N_Range then
2990 Lcheck : constant Compare_Result :=
2991 Compile_Time_Compare (Lop, Low_Bound (Rop));
2992 Ucheck : constant Compare_Result :=
2993 Compile_Time_Compare (Lop, High_Bound (Rop));
2996 -- If either check is known to fail, replace result
2997 -- by False, since the other check does not matter.
2998 -- Preserve the static flag for legality checks, because
2999 -- we are constant-folding beyond RM 4.9.
3001 if Lcheck = LT or else Ucheck = GT then
3003 New_Reference_To (Standard_False, Loc));
3004 Analyze_And_Resolve (N, Rtyp);
3005 Set_Is_Static_Expression (N, Static);
3008 -- If both checks are known to succeed, replace result
3009 -- by True, since we know we are in range.
3011 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3013 New_Reference_To (Standard_True, Loc));
3014 Analyze_And_Resolve (N, Rtyp);
3015 Set_Is_Static_Expression (N, Static);
3018 -- If lower bound check succeeds and upper bound check is
3019 -- not known to succeed or fail, then replace the range check
3020 -- with a comparison against the upper bound.
3022 elsif Lcheck in Compare_GE then
3026 Right_Opnd => High_Bound (Rop)));
3027 Analyze_And_Resolve (N, Rtyp);
3030 -- If upper bound check succeeds and lower bound check is
3031 -- not known to succeed or fail, then replace the range check
3032 -- with a comparison against the lower bound.
3034 elsif Ucheck in Compare_LE then
3038 Right_Opnd => Low_Bound (Rop)));
3039 Analyze_And_Resolve (N, Rtyp);
3044 -- For all other cases of an explicit range, nothing to be done
3048 -- Here right operand is a subtype mark
3052 Typ : Entity_Id := Etype (Rop);
3053 Is_Acc : constant Boolean := Is_Access_Type (Typ);
3054 Obj : Node_Id := Lop;
3055 Cond : Node_Id := Empty;
3058 Remove_Side_Effects (Obj);
3060 -- For tagged type, do tagged membership operation
3062 if Is_Tagged_Type (Typ) then
3064 -- No expansion will be performed when Java_VM, as the
3065 -- JVM back end will handle the membership tests directly
3066 -- (tags are not explicitly represented in Java objects,
3067 -- so the normal tagged membership expansion is not what
3071 Rewrite (N, Tagged_Membership (N));
3072 Analyze_And_Resolve (N, Rtyp);
3077 -- If type is scalar type, rewrite as x in t'first .. t'last
3078 -- This reason we do this is that the bounds may have the wrong
3079 -- type if they come from the original type definition.
3081 elsif Is_Scalar_Type (Typ) then
3085 Make_Attribute_Reference (Loc,
3086 Attribute_Name => Name_First,
3087 Prefix => New_Reference_To (Typ, Loc)),
3090 Make_Attribute_Reference (Loc,
3091 Attribute_Name => Name_Last,
3092 Prefix => New_Reference_To (Typ, Loc))));
3093 Analyze_And_Resolve (N, Rtyp);
3096 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3097 -- a membership test if the subtype mark denotes a constrained
3098 -- Unchecked_Union subtype and the expression lacks inferable
3101 elsif Is_Unchecked_Union (Base_Type (Typ))
3102 and then Is_Constrained (Typ)
3103 and then not Has_Inferable_Discriminants (Lop)
3106 Make_Raise_Program_Error (Loc,
3107 Reason => PE_Unchecked_Union_Restriction));
3109 -- Prevent Gigi from generating incorrect code by rewriting
3110 -- the test as a standard False.
3113 New_Occurrence_Of (Standard_False, Loc));
3118 -- Here we have a non-scalar type
3121 Typ := Designated_Type (Typ);
3124 if not Is_Constrained (Typ) then
3126 New_Reference_To (Standard_True, Loc));
3127 Analyze_And_Resolve (N, Rtyp);
3129 -- For the constrained array case, we have to check the
3130 -- subscripts for an exact match if the lengths are
3131 -- non-zero (the lengths must match in any case).
3133 elsif Is_Array_Type (Typ) then
3135 Check_Subscripts : declare
3136 function Construct_Attribute_Reference
3139 Dim : Nat) return Node_Id;
3140 -- Build attribute reference E'Nam(Dim)
3142 -----------------------------------
3143 -- Construct_Attribute_Reference --
3144 -----------------------------------
3146 function Construct_Attribute_Reference
3149 Dim : Nat) return Node_Id
3153 Make_Attribute_Reference (Loc,
3155 Attribute_Name => Nam,
3156 Expressions => New_List (
3157 Make_Integer_Literal (Loc, Dim)));
3158 end Construct_Attribute_Reference;
3160 -- Start processing for Check_Subscripts
3163 for J in 1 .. Number_Dimensions (Typ) loop
3164 Evolve_And_Then (Cond,
3167 Construct_Attribute_Reference
3168 (Duplicate_Subexpr_No_Checks (Obj),
3171 Construct_Attribute_Reference
3172 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
3174 Evolve_And_Then (Cond,
3177 Construct_Attribute_Reference
3178 (Duplicate_Subexpr_No_Checks (Obj),
3181 Construct_Attribute_Reference
3182 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
3191 Right_Opnd => Make_Null (Loc)),
3192 Right_Opnd => Cond);
3196 Analyze_And_Resolve (N, Rtyp);
3197 end Check_Subscripts;
3199 -- These are the cases where constraint checks may be
3200 -- required, e.g. records with possible discriminants
3203 -- Expand the test into a series of discriminant comparisons.
3204 -- The expression that is built is the negation of the one
3205 -- that is used for checking discriminant constraints.
3207 Obj := Relocate_Node (Left_Opnd (N));
3209 if Has_Discriminants (Typ) then
3210 Cond := Make_Op_Not (Loc,
3211 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
3214 Cond := Make_Or_Else (Loc,
3218 Right_Opnd => Make_Null (Loc)),
3219 Right_Opnd => Cond);
3223 Cond := New_Occurrence_Of (Standard_True, Loc);
3227 Analyze_And_Resolve (N, Rtyp);
3233 --------------------------------
3234 -- Expand_N_Indexed_Component --
3235 --------------------------------
3237 procedure Expand_N_Indexed_Component (N : Node_Id) is
3238 Loc : constant Source_Ptr := Sloc (N);
3239 Typ : constant Entity_Id := Etype (N);
3240 P : constant Node_Id := Prefix (N);
3241 T : constant Entity_Id := Etype (P);
3244 -- A special optimization, if we have an indexed component that
3245 -- is selecting from a slice, then we can eliminate the slice,
3246 -- since, for example, x (i .. j)(k) is identical to x(k). The
3247 -- only difference is the range check required by the slice. The
3248 -- range check for the slice itself has already been generated.
3249 -- The range check for the subscripting operation is ensured
3250 -- by converting the subject to the subtype of the slice.
3252 -- This optimization not only generates better code, avoiding
3253 -- slice messing especially in the packed case, but more importantly
3254 -- bypasses some problems in handling this peculiar case, for
3255 -- example, the issue of dealing specially with object renamings.
3257 if Nkind (P) = N_Slice then
3259 Make_Indexed_Component (Loc,
3260 Prefix => Prefix (P),
3261 Expressions => New_List (
3263 (Etype (First_Index (Etype (P))),
3264 First (Expressions (N))))));
3265 Analyze_And_Resolve (N, Typ);
3269 -- If the prefix is an access type, then we unconditionally rewrite
3270 -- if as an explicit deference. This simplifies processing for several
3271 -- cases, including packed array cases and certain cases in which
3272 -- checks must be generated. We used to try to do this only when it
3273 -- was necessary, but it cleans up the code to do it all the time.
3275 if Is_Access_Type (T) then
3276 Insert_Explicit_Dereference (P);
3277 Analyze_And_Resolve (P, Designated_Type (T));
3280 -- Generate index and validity checks
3282 Generate_Index_Checks (N);
3284 if Validity_Checks_On and then Validity_Check_Subscripts then
3285 Apply_Subscript_Validity_Checks (N);
3288 -- All done for the non-packed case
3290 if not Is_Packed (Etype (Prefix (N))) then
3294 -- For packed arrays that are not bit-packed (i.e. the case of an array
3295 -- with one or more index types with a non-coniguous enumeration type),
3296 -- we can always use the normal packed element get circuit.
3298 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
3299 Expand_Packed_Element_Reference (N);
3303 -- For a reference to a component of a bit packed array, we have to
3304 -- convert it to a reference to the corresponding Packed_Array_Type.
3305 -- We only want to do this for simple references, and not for:
3307 -- Left side of assignment, or prefix of left side of assignment,
3308 -- or prefix of the prefix, to handle packed arrays of packed arrays,
3309 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
3311 -- Renaming objects in renaming associations
3312 -- This case is handled when a use of the renamed variable occurs
3314 -- Actual parameters for a procedure call
3315 -- This case is handled in Exp_Ch6.Expand_Actuals
3317 -- The second expression in a 'Read attribute reference
3319 -- The prefix of an address or size attribute reference
3321 -- The following circuit detects these exceptions
3324 Child : Node_Id := N;
3325 Parnt : Node_Id := Parent (N);
3329 if Nkind (Parnt) = N_Unchecked_Expression then
3332 elsif Nkind (Parnt) = N_Object_Renaming_Declaration
3333 or else Nkind (Parnt) = N_Procedure_Call_Statement
3334 or else (Nkind (Parnt) = N_Parameter_Association
3336 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
3340 elsif Nkind (Parnt) = N_Attribute_Reference
3341 and then (Attribute_Name (Parnt) = Name_Address
3343 Attribute_Name (Parnt) = Name_Size)
3344 and then Prefix (Parnt) = Child
3348 elsif Nkind (Parnt) = N_Assignment_Statement
3349 and then Name (Parnt) = Child
3353 -- If the expression is an index of an indexed component,
3354 -- it must be expanded regardless of context.
3356 elsif Nkind (Parnt) = N_Indexed_Component
3357 and then Child /= Prefix (Parnt)
3359 Expand_Packed_Element_Reference (N);
3362 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
3363 and then Name (Parent (Parnt)) = Parnt
3367 elsif Nkind (Parnt) = N_Attribute_Reference
3368 and then Attribute_Name (Parnt) = Name_Read
3369 and then Next (First (Expressions (Parnt))) = Child
3373 elsif (Nkind (Parnt) = N_Indexed_Component
3374 or else Nkind (Parnt) = N_Selected_Component)
3375 and then Prefix (Parnt) = Child
3380 Expand_Packed_Element_Reference (N);
3384 -- Keep looking up tree for unchecked expression, or if we are
3385 -- the prefix of a possible assignment left side.
3388 Parnt := Parent (Child);
3392 end Expand_N_Indexed_Component;
3394 ---------------------
3395 -- Expand_N_Not_In --
3396 ---------------------
3398 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
3399 -- can be done. This avoids needing to duplicate this expansion code.
3401 procedure Expand_N_Not_In (N : Node_Id) is
3402 Loc : constant Source_Ptr := Sloc (N);
3403 Typ : constant Entity_Id := Etype (N);
3410 Left_Opnd => Left_Opnd (N),
3411 Right_Opnd => Right_Opnd (N))));
3412 Analyze_And_Resolve (N, Typ);
3413 end Expand_N_Not_In;
3419 -- The only replacement required is for the case of a null of type
3420 -- that is an access to protected subprogram. We represent such
3421 -- access values as a record, and so we must replace the occurrence
3422 -- of null by the equivalent record (with a null address and a null
3423 -- pointer in it), so that the backend creates the proper value.
3425 procedure Expand_N_Null (N : Node_Id) is
3426 Loc : constant Source_Ptr := Sloc (N);
3427 Typ : constant Entity_Id := Etype (N);
3431 if Ekind (Typ) = E_Access_Protected_Subprogram_Type then
3433 Make_Aggregate (Loc,
3434 Expressions => New_List (
3435 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
3439 Analyze_And_Resolve (N, Equivalent_Type (Typ));
3441 -- For subsequent semantic analysis, the node must retain its
3442 -- type. Gigi in any case replaces this type by the corresponding
3443 -- record type before processing the node.
3449 when RE_Not_Available =>
3453 ---------------------
3454 -- Expand_N_Op_Abs --
3455 ---------------------
3457 procedure Expand_N_Op_Abs (N : Node_Id) is
3458 Loc : constant Source_Ptr := Sloc (N);
3459 Expr : constant Node_Id := Right_Opnd (N);
3462 Unary_Op_Validity_Checks (N);
3464 -- Deal with software overflow checking
3466 if not Backend_Overflow_Checks_On_Target
3467 and then Is_Signed_Integer_Type (Etype (N))
3468 and then Do_Overflow_Check (N)
3470 -- The only case to worry about is when the argument is
3471 -- equal to the largest negative number, so what we do is
3472 -- to insert the check:
3474 -- [constraint_error when Expr = typ'Base'First]
3476 -- with the usual Duplicate_Subexpr use coding for expr
3479 Make_Raise_Constraint_Error (Loc,
3482 Left_Opnd => Duplicate_Subexpr (Expr),
3484 Make_Attribute_Reference (Loc,
3486 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
3487 Attribute_Name => Name_First)),
3488 Reason => CE_Overflow_Check_Failed));
3491 -- Vax floating-point types case
3493 if Vax_Float (Etype (N)) then
3494 Expand_Vax_Arith (N);
3496 end Expand_N_Op_Abs;
3498 ---------------------
3499 -- Expand_N_Op_Add --
3500 ---------------------
3502 procedure Expand_N_Op_Add (N : Node_Id) is
3503 Typ : constant Entity_Id := Etype (N);
3506 Binary_Op_Validity_Checks (N);
3508 -- N + 0 = 0 + N = N for integer types
3510 if Is_Integer_Type (Typ) then
3511 if Compile_Time_Known_Value (Right_Opnd (N))
3512 and then Expr_Value (Right_Opnd (N)) = Uint_0
3514 Rewrite (N, Left_Opnd (N));
3517 elsif Compile_Time_Known_Value (Left_Opnd (N))
3518 and then Expr_Value (Left_Opnd (N)) = Uint_0
3520 Rewrite (N, Right_Opnd (N));
3525 -- Arithmetic overflow checks for signed integer/fixed point types
3527 if Is_Signed_Integer_Type (Typ)
3528 or else Is_Fixed_Point_Type (Typ)
3530 Apply_Arithmetic_Overflow_Check (N);
3533 -- Vax floating-point types case
3535 elsif Vax_Float (Typ) then
3536 Expand_Vax_Arith (N);
3538 end Expand_N_Op_Add;
3540 ---------------------
3541 -- Expand_N_Op_And --
3542 ---------------------
3544 procedure Expand_N_Op_And (N : Node_Id) is
3545 Typ : constant Entity_Id := Etype (N);
3548 Binary_Op_Validity_Checks (N);
3550 if Is_Array_Type (Etype (N)) then
3551 Expand_Boolean_Operator (N);
3553 elsif Is_Boolean_Type (Etype (N)) then
3554 Adjust_Condition (Left_Opnd (N));
3555 Adjust_Condition (Right_Opnd (N));
3556 Set_Etype (N, Standard_Boolean);
3557 Adjust_Result_Type (N, Typ);
3559 end Expand_N_Op_And;
3561 ------------------------
3562 -- Expand_N_Op_Concat --
3563 ------------------------
3565 Max_Available_String_Operands : Int := -1;
3566 -- This is initialized the first time this routine is called. It records
3567 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
3568 -- available in the run-time:
3571 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
3572 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
3573 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
3574 -- 5 All routines including RE_Str_Concat_5 available
3576 Char_Concat_Available : Boolean;
3577 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
3578 -- all three are available, False if any one of these is unavailable.
3580 procedure Expand_N_Op_Concat (N : Node_Id) is
3582 -- List of operands to be concatenated
3585 -- Single operand for concatenation
3588 -- Node which is to be replaced by the result of concatenating
3589 -- the nodes in the list Opnds.
3592 -- Array type of concatenation result type
3595 -- Component type of concatenation represented by Cnode
3598 -- Initialize global variables showing run-time status
3600 if Max_Available_String_Operands < 1 then
3601 if not RTE_Available (RE_Str_Concat) then
3602 Max_Available_String_Operands := 0;
3603 elsif not RTE_Available (RE_Str_Concat_3) then
3604 Max_Available_String_Operands := 2;
3605 elsif not RTE_Available (RE_Str_Concat_4) then
3606 Max_Available_String_Operands := 3;
3607 elsif not RTE_Available (RE_Str_Concat_5) then
3608 Max_Available_String_Operands := 4;
3610 Max_Available_String_Operands := 5;
3613 Char_Concat_Available :=
3614 RTE_Available (RE_Str_Concat_CC)
3616 RTE_Available (RE_Str_Concat_CS)
3618 RTE_Available (RE_Str_Concat_SC);
3621 -- Ensure validity of both operands
3623 Binary_Op_Validity_Checks (N);
3625 -- If we are the left operand of a concatenation higher up the
3626 -- tree, then do nothing for now, since we want to deal with a
3627 -- series of concatenations as a unit.
3629 if Nkind (Parent (N)) = N_Op_Concat
3630 and then N = Left_Opnd (Parent (N))
3635 -- We get here with a concatenation whose left operand may be a
3636 -- concatenation itself with a consistent type. We need to process
3637 -- these concatenation operands from left to right, which means
3638 -- from the deepest node in the tree to the highest node.
3641 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
3642 Cnode := Left_Opnd (Cnode);
3645 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
3646 -- nodes above, so now we process bottom up, doing the operations. We
3647 -- gather a string that is as long as possible up to five operands
3649 -- The outer loop runs more than once if there are more than five
3650 -- concatenations of type Standard.String, the most we handle for
3651 -- this case, or if more than one concatenation type is involved.
3654 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
3655 Set_Parent (Opnds, N);
3657 -- The inner loop gathers concatenation operands. We gather any
3658 -- number of these in the non-string case, or if no concatenation
3659 -- routines are available for string (since in that case we will
3660 -- treat string like any other non-string case). Otherwise we only
3661 -- gather as many operands as can be handled by the available
3662 -- procedures in the run-time library (normally 5, but may be
3663 -- less for the configurable run-time case).
3665 Inner : while Cnode /= N
3666 and then (Base_Type (Etype (Cnode)) /= Standard_String
3668 Max_Available_String_Operands = 0
3670 List_Length (Opnds) <
3671 Max_Available_String_Operands)
3672 and then Base_Type (Etype (Cnode)) =
3673 Base_Type (Etype (Parent (Cnode)))
3675 Cnode := Parent (Cnode);
3676 Append (Right_Opnd (Cnode), Opnds);
3679 -- Here we process the collected operands. First we convert
3680 -- singleton operands to singleton aggregates. This is skipped
3681 -- however for the case of two operands of type String, since
3682 -- we have special routines for these cases.
3684 Atyp := Base_Type (Etype (Cnode));
3685 Ctyp := Base_Type (Component_Type (Etype (Cnode)));
3687 if (List_Length (Opnds) > 2 or else Atyp /= Standard_String)
3688 or else not Char_Concat_Available
3690 Opnd := First (Opnds);
3692 if Base_Type (Etype (Opnd)) = Ctyp then
3694 Make_Aggregate (Sloc (Cnode),
3695 Expressions => New_List (Relocate_Node (Opnd))));
3696 Analyze_And_Resolve (Opnd, Atyp);
3700 exit when No (Opnd);
3704 -- Now call appropriate continuation routine
3706 if Atyp = Standard_String
3707 and then Max_Available_String_Operands > 0
3709 Expand_Concatenate_String (Cnode, Opnds);
3711 Expand_Concatenate_Other (Cnode, Opnds);
3714 exit Outer when Cnode = N;
3715 Cnode := Parent (Cnode);
3717 end Expand_N_Op_Concat;
3719 ------------------------
3720 -- Expand_N_Op_Divide --
3721 ------------------------
3723 procedure Expand_N_Op_Divide (N : Node_Id) is
3724 Loc : constant Source_Ptr := Sloc (N);
3725 Ltyp : constant Entity_Id := Etype (Left_Opnd (N));
3726 Rtyp : constant Entity_Id := Etype (Right_Opnd (N));
3727 Typ : Entity_Id := Etype (N);
3730 Binary_Op_Validity_Checks (N);
3732 -- Vax_Float is a special case
3734 if Vax_Float (Typ) then
3735 Expand_Vax_Arith (N);
3739 -- N / 1 = N for integer types
3741 if Is_Integer_Type (Typ)
3742 and then Compile_Time_Known_Value (Right_Opnd (N))
3743 and then Expr_Value (Right_Opnd (N)) = Uint_1
3745 Rewrite (N, Left_Opnd (N));
3749 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
3750 -- Is_Power_Of_2_For_Shift is set means that we know that our left
3751 -- operand is an unsigned integer, as required for this to work.
3753 if Nkind (Right_Opnd (N)) = N_Op_Expon
3754 and then Is_Power_Of_2_For_Shift (Right_Opnd (N))
3756 -- We cannot do this transformation in configurable run time mode if we
3757 -- have 64-bit -- integers and long shifts are not available.
3761 or else Support_Long_Shifts_On_Target)
3764 Make_Op_Shift_Right (Loc,
3765 Left_Opnd => Left_Opnd (N),
3767 Convert_To (Standard_Natural, Right_Opnd (Right_Opnd (N)))));
3768 Analyze_And_Resolve (N, Typ);
3772 -- Do required fixup of universal fixed operation
3774 if Typ = Universal_Fixed then
3775 Fixup_Universal_Fixed_Operation (N);
3779 -- Divisions with fixed-point results
3781 if Is_Fixed_Point_Type (Typ) then
3783 -- No special processing if Treat_Fixed_As_Integer is set,
3784 -- since from a semantic point of view such operations are
3785 -- simply integer operations and will be treated that way.
3787 if not Treat_Fixed_As_Integer (N) then
3788 if Is_Integer_Type (Rtyp) then
3789 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
3791 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
3795 -- Other cases of division of fixed-point operands. Again we
3796 -- exclude the case where Treat_Fixed_As_Integer is set.
3798 elsif (Is_Fixed_Point_Type (Ltyp) or else
3799 Is_Fixed_Point_Type (Rtyp))
3800 and then not Treat_Fixed_As_Integer (N)
3802 if Is_Integer_Type (Typ) then
3803 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
3805 pragma Assert (Is_Floating_Point_Type (Typ));
3806 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
3809 -- Mixed-mode operations can appear in a non-static universal
3810 -- context, in which case the integer argument must be converted
3813 elsif Typ = Universal_Real
3814 and then Is_Integer_Type (Rtyp)
3816 Rewrite (Right_Opnd (N),
3817 Convert_To (Universal_Real, Relocate_Node (Right_Opnd (N))));
3819 Analyze_And_Resolve (Right_Opnd (N), Universal_Real);
3821 elsif Typ = Universal_Real
3822 and then Is_Integer_Type (Ltyp)
3824 Rewrite (Left_Opnd (N),
3825 Convert_To (Universal_Real, Relocate_Node (Left_Opnd (N))));
3827 Analyze_And_Resolve (Left_Opnd (N), Universal_Real);
3829 -- Non-fixed point cases, do zero divide and overflow checks
3831 elsif Is_Integer_Type (Typ) then
3832 Apply_Divide_Check (N);
3834 -- Check for 64-bit division available
3836 if Esize (Ltyp) > 32
3837 and then not Support_64_Bit_Divides_On_Target
3839 Error_Msg_CRT ("64-bit division", N);
3842 end Expand_N_Op_Divide;
3844 --------------------
3845 -- Expand_N_Op_Eq --
3846 --------------------
3848 procedure Expand_N_Op_Eq (N : Node_Id) is
3849 Loc : constant Source_Ptr := Sloc (N);
3850 Typ : constant Entity_Id := Etype (N);
3851 Lhs : constant Node_Id := Left_Opnd (N);
3852 Rhs : constant Node_Id := Right_Opnd (N);
3853 Bodies : constant List_Id := New_List;
3854 A_Typ : constant Entity_Id := Etype (Lhs);
3856 Typl : Entity_Id := A_Typ;
3857 Op_Name : Entity_Id;
3860 procedure Build_Equality_Call (Eq : Entity_Id);
3861 -- If a constructed equality exists for the type or for its parent,
3862 -- build and analyze call, adding conversions if the operation is
3865 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
3866 -- Determines whether a type has a subcompoment of an unconstrained
3867 -- Unchecked_Union subtype. Typ is a record type.
3869 -------------------------
3870 -- Build_Equality_Call --
3871 -------------------------
3873 procedure Build_Equality_Call (Eq : Entity_Id) is
3874 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
3875 L_Exp : Node_Id := Relocate_Node (Lhs);
3876 R_Exp : Node_Id := Relocate_Node (Rhs);
3879 if Base_Type (Op_Type) /= Base_Type (A_Typ)
3880 and then not Is_Class_Wide_Type (A_Typ)
3882 L_Exp := OK_Convert_To (Op_Type, L_Exp);
3883 R_Exp := OK_Convert_To (Op_Type, R_Exp);
3886 -- If we have an Unchecked_Union, we need to add the inferred
3887 -- discriminant values as actuals in the function call. At this
3888 -- point, the expansion has determined that both operands have
3889 -- inferable discriminants.
3891 if Is_Unchecked_Union (Op_Type) then
3893 Lhs_Type : constant Node_Id := Etype (L_Exp);
3894 Rhs_Type : constant Node_Id := Etype (R_Exp);
3895 Lhs_Discr_Val : Node_Id;
3896 Rhs_Discr_Val : Node_Id;
3899 -- Per-object constrained selected components require special
3900 -- attention. If the enclosing scope of the component is an
3901 -- Unchecked_Union, we can not reference its discriminants
3902 -- directly. This is why we use the two extra parameters of
3903 -- the equality function of the enclosing Unchecked_Union.
3905 -- type UU_Type (Discr : Integer := 0) is
3908 -- pragma Unchecked_Union (UU_Type);
3910 -- 1. Unchecked_Union enclosing record:
3912 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
3914 -- Comp : UU_Type (Discr);
3916 -- end Enclosing_UU_Type;
3917 -- pragma Unchecked_Union (Enclosing_UU_Type);
3919 -- Obj1 : Enclosing_UU_Type;
3920 -- Obj2 : Enclosing_UU_Type (1);
3922 -- [. . .] Obj1 = Obj2 [. . .]
3926 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
3928 -- A and B are the formal parameters of the equality function
3929 -- of Enclosing_UU_Type. The function always has two extra
3930 -- formals to capture the inferred discriminant values.
3932 -- 2. Non-Unchecked_Union enclosing record:
3935 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
3938 -- Comp : UU_Type (Discr);
3940 -- end Enclosing_Non_UU_Type;
3942 -- Obj1 : Enclosing_Non_UU_Type;
3943 -- Obj2 : Enclosing_Non_UU_Type (1);
3945 -- . . . Obj1 = Obj2 . . .
3949 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
3950 -- obj1.discr, obj2.discr)) then
3952 -- In this case we can directly reference the discriminants of
3953 -- the enclosing record.
3957 if Nkind (Lhs) = N_Selected_Component
3958 and then Has_Per_Object_Constraint
3959 (Entity (Selector_Name (Lhs)))
3961 -- Enclosing record is an Unchecked_Union, use formal A
3963 if Is_Unchecked_Union (Scope
3964 (Entity (Selector_Name (Lhs))))
3967 Make_Identifier (Loc,
3970 -- Enclosing record is of a non-Unchecked_Union type, it is
3971 -- possible to reference the discriminant.
3975 Make_Selected_Component (Loc,
3976 Prefix => Prefix (Lhs),
3979 (Get_Discriminant_Value
3980 (First_Discriminant (Lhs_Type),
3982 Stored_Constraint (Lhs_Type))));
3985 -- Comment needed here ???
3988 -- Infer the discriminant value
3992 (Get_Discriminant_Value
3993 (First_Discriminant (Lhs_Type),
3995 Stored_Constraint (Lhs_Type)));
4000 if Nkind (Rhs) = N_Selected_Component
4001 and then Has_Per_Object_Constraint
4002 (Entity (Selector_Name (Rhs)))
4004 if Is_Unchecked_Union
4005 (Scope (Entity (Selector_Name (Rhs))))
4008 Make_Identifier (Loc,
4013 Make_Selected_Component (Loc,
4014 Prefix => Prefix (Rhs),
4016 New_Copy (Get_Discriminant_Value (
4017 First_Discriminant (Rhs_Type),
4019 Stored_Constraint (Rhs_Type))));
4024 New_Copy (Get_Discriminant_Value (
4025 First_Discriminant (Rhs_Type),
4027 Stored_Constraint (Rhs_Type)));
4032 Make_Function_Call (Loc,
4033 Name => New_Reference_To (Eq, Loc),
4034 Parameter_Associations => New_List (
4041 -- Normal case, not an unchecked union
4045 Make_Function_Call (Loc,
4046 Name => New_Reference_To (Eq, Loc),
4047 Parameter_Associations => New_List (L_Exp, R_Exp)));
4050 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4051 end Build_Equality_Call;
4053 ------------------------------------
4054 -- Has_Unconstrained_UU_Component --
4055 ------------------------------------
4057 function Has_Unconstrained_UU_Component
4058 (Typ : Node_Id) return Boolean
4060 Tdef : constant Node_Id :=
4061 Type_Definition (Declaration_Node (Typ));
4065 function Component_Is_Unconstrained_UU
4066 (Comp : Node_Id) return Boolean;
4067 -- Determines whether the subtype of the component is an
4068 -- unconstrained Unchecked_Union.
4070 function Variant_Is_Unconstrained_UU
4071 (Variant : Node_Id) return Boolean;
4072 -- Determines whether a component of the variant has an unconstrained
4073 -- Unchecked_Union subtype.
4075 -----------------------------------
4076 -- Component_Is_Unconstrained_UU --
4077 -----------------------------------
4079 function Component_Is_Unconstrained_UU
4080 (Comp : Node_Id) return Boolean
4083 if Nkind (Comp) /= N_Component_Declaration then
4088 Sindic : constant Node_Id :=
4089 Subtype_Indication (Component_Definition (Comp));
4092 -- Unconstrained nominal type. In the case of a constraint
4093 -- present, the node kind would have been N_Subtype_Indication.
4095 if Nkind (Sindic) = N_Identifier then
4096 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
4101 end Component_Is_Unconstrained_UU;
4103 ---------------------------------
4104 -- Variant_Is_Unconstrained_UU --
4105 ---------------------------------
4107 function Variant_Is_Unconstrained_UU
4108 (Variant : Node_Id) return Boolean
4110 Clist : constant Node_Id := Component_List (Variant);
4113 if Is_Empty_List (Component_Items (Clist)) then
4118 Comp : Node_Id := First (Component_Items (Clist));
4121 while Present (Comp) loop
4123 -- One component is sufficent
4125 if Component_Is_Unconstrained_UU (Comp) then
4133 -- None of the components withing the variant were of
4134 -- unconstrained Unchecked_Union type.
4137 end Variant_Is_Unconstrained_UU;
4139 -- Start of processing for Has_Unconstrained_UU_Component
4142 if Null_Present (Tdef) then
4146 Clist := Component_List (Tdef);
4147 Vpart := Variant_Part (Clist);
4149 -- Inspect available components
4151 if Present (Component_Items (Clist)) then
4153 Comp : Node_Id := First (Component_Items (Clist));
4156 while Present (Comp) loop
4158 -- One component is sufficent
4160 if Component_Is_Unconstrained_UU (Comp) then
4169 -- Inspect available components withing variants
4171 if Present (Vpart) then
4173 Variant : Node_Id := First (Variants (Vpart));
4176 while Present (Variant) loop
4178 -- One component within a variant is sufficent
4180 if Variant_Is_Unconstrained_UU (Variant) then
4189 -- Neither the available components, nor the components inside the
4190 -- variant parts were of an unconstrained Unchecked_Union subtype.
4193 end Has_Unconstrained_UU_Component;
4195 -- Start of processing for Expand_N_Op_Eq
4198 Binary_Op_Validity_Checks (N);
4200 if Ekind (Typl) = E_Private_Type then
4201 Typl := Underlying_Type (Typl);
4203 elsif Ekind (Typl) = E_Private_Subtype then
4204 Typl := Underlying_Type (Base_Type (Typl));
4207 -- It may happen in error situations that the underlying type is not
4208 -- set. The error will be detected later, here we just defend the
4215 Typl := Base_Type (Typl);
4219 if Vax_Float (Typl) then
4220 Expand_Vax_Comparison (N);
4223 -- Boolean types (requiring handling of non-standard case)
4225 elsif Is_Boolean_Type (Typl) then
4226 Adjust_Condition (Left_Opnd (N));
4227 Adjust_Condition (Right_Opnd (N));
4228 Set_Etype (N, Standard_Boolean);
4229 Adjust_Result_Type (N, Typ);
4233 elsif Is_Array_Type (Typl) then
4235 -- If we are doing full validity checking, then expand out array
4236 -- comparisons to make sure that we check the array elements.
4238 if Validity_Check_Operands then
4240 Save_Force_Validity_Checks : constant Boolean :=
4241 Force_Validity_Checks;
4243 Force_Validity_Checks := True;
4245 Expand_Array_Equality
4247 Relocate_Node (Lhs),
4248 Relocate_Node (Rhs),
4251 Insert_Actions (N, Bodies);
4252 Analyze_And_Resolve (N, Standard_Boolean);
4253 Force_Validity_Checks := Save_Force_Validity_Checks;
4258 elsif Is_Bit_Packed_Array (Typl) then
4259 Expand_Packed_Eq (N);
4261 -- Where the component type is elementary we can use a block bit
4262 -- comparison (if supported on the target) exception in the case
4263 -- of floating-point (negative zero issues require element by
4264 -- element comparison), and atomic types (where we must be sure
4265 -- to load elements independently).
4267 elsif Is_Elementary_Type (Component_Type (Typl))
4268 and then not Is_Floating_Point_Type (Component_Type (Typl))
4269 and then not Is_Atomic (Component_Type (Typl))
4270 and then Support_Composite_Compare_On_Target
4274 -- For composite and floating-point cases, expand equality loop
4275 -- to make sure of using proper comparisons for tagged types,
4276 -- and correctly handling the floating-point case.
4280 Expand_Array_Equality
4282 Relocate_Node (Lhs),
4283 Relocate_Node (Rhs),
4286 Insert_Actions (N, Bodies, Suppress => All_Checks);
4287 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4292 elsif Is_Record_Type (Typl) then
4294 -- For tagged types, use the primitive "="
4296 if Is_Tagged_Type (Typl) then
4298 -- If this is derived from an untagged private type completed
4299 -- with a tagged type, it does not have a full view, so we
4300 -- use the primitive operations of the private type.
4301 -- This check should no longer be necessary when these
4302 -- types receive their full views ???
4304 if Is_Private_Type (A_Typ)
4305 and then not Is_Tagged_Type (A_Typ)
4306 and then Is_Derived_Type (A_Typ)
4307 and then No (Full_View (A_Typ))
4309 -- Search for equality operation, checking that the
4310 -- operands have the same type. Note that we must find
4311 -- a matching entry, or something is very wrong!
4313 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
4315 while Present (Prim) loop
4316 exit when Chars (Node (Prim)) = Name_Op_Eq
4317 and then Etype (First_Formal (Node (Prim))) =
4318 Etype (Next_Formal (First_Formal (Node (Prim))))
4320 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4325 pragma Assert (Present (Prim));
4326 Op_Name := Node (Prim);
4328 -- Find the type's predefined equality or an overriding
4329 -- user-defined equality. The reason for not simply calling
4330 -- Find_Prim_Op here is that there may be a user-defined
4331 -- overloaded equality op that precedes the equality that
4332 -- we want, so we have to explicitly search (e.g., there
4333 -- could be an equality with two different parameter types).
4336 if Is_Class_Wide_Type (Typl) then
4337 Typl := Root_Type (Typl);
4340 Prim := First_Elmt (Primitive_Operations (Typl));
4341 while Present (Prim) loop
4342 exit when Chars (Node (Prim)) = Name_Op_Eq
4343 and then Etype (First_Formal (Node (Prim))) =
4344 Etype (Next_Formal (First_Formal (Node (Prim))))
4346 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4351 pragma Assert (Present (Prim));
4352 Op_Name := Node (Prim);
4355 Build_Equality_Call (Op_Name);
4357 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
4358 -- predefined equality operator for a type which has a subcomponent
4359 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
4361 elsif Has_Unconstrained_UU_Component (Typl) then
4363 Make_Raise_Program_Error (Loc,
4364 Reason => PE_Unchecked_Union_Restriction));
4366 -- Prevent Gigi from generating incorrect code by rewriting the
4367 -- equality as a standard False.
4370 New_Occurrence_Of (Standard_False, Loc));
4372 elsif Is_Unchecked_Union (Typl) then
4374 -- If we can infer the discriminants of the operands, we make a
4375 -- call to the TSS equality function.
4377 if Has_Inferable_Discriminants (Lhs)
4379 Has_Inferable_Discriminants (Rhs)
4382 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4385 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4386 -- the predefined equality operator for an Unchecked_Union type
4387 -- if either of the operands lack inferable discriminants.
4390 Make_Raise_Program_Error (Loc,
4391 Reason => PE_Unchecked_Union_Restriction));
4393 -- Prevent Gigi from generating incorrect code by rewriting
4394 -- the equality as a standard False.
4397 New_Occurrence_Of (Standard_False, Loc));
4401 -- If a type support function is present (for complex cases), use it
4403 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
4405 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4407 -- Otherwise expand the component by component equality. Note that
4408 -- we never use block-bit coparisons for records, because of the
4409 -- problems with gaps. The backend will often be able to recombine
4410 -- the separate comparisons that we generate here.
4413 Remove_Side_Effects (Lhs);
4414 Remove_Side_Effects (Rhs);
4416 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
4418 Insert_Actions (N, Bodies, Suppress => All_Checks);
4419 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4423 -- If we still have an equality comparison (i.e. it was not rewritten
4424 -- in some way), then we can test if result is needed at compile time).
4426 if Nkind (N) = N_Op_Eq then
4427 Rewrite_Comparison (N);
4431 -----------------------
4432 -- Expand_N_Op_Expon --
4433 -----------------------
4435 procedure Expand_N_Op_Expon (N : Node_Id) is
4436 Loc : constant Source_Ptr := Sloc (N);
4437 Typ : constant Entity_Id := Etype (N);
4438 Rtyp : constant Entity_Id := Root_Type (Typ);
4439 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
4440 Bastyp : constant Node_Id := Etype (Base);
4441 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
4442 Exptyp : constant Entity_Id := Etype (Exp);
4443 Ovflo : constant Boolean := Do_Overflow_Check (N);
4452 Binary_Op_Validity_Checks (N);
4454 -- If either operand is of a private type, then we have the use of
4455 -- an intrinsic operator, and we get rid of the privateness, by using
4456 -- root types of underlying types for the actual operation. Otherwise
4457 -- the private types will cause trouble if we expand multiplications
4458 -- or shifts etc. We also do this transformation if the result type
4459 -- is different from the base type.
4461 if Is_Private_Type (Etype (Base))
4463 Is_Private_Type (Typ)
4465 Is_Private_Type (Exptyp)
4467 Rtyp /= Root_Type (Bastyp)
4470 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
4471 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
4475 Unchecked_Convert_To (Typ,
4477 Left_Opnd => Unchecked_Convert_To (Bt, Base),
4478 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
4479 Analyze_And_Resolve (N, Typ);
4484 -- Test for case of known right argument
4486 if Compile_Time_Known_Value (Exp) then
4487 Expv := Expr_Value (Exp);
4489 -- We only fold small non-negative exponents. You might think we
4490 -- could fold small negative exponents for the real case, but we
4491 -- can't because we are required to raise Constraint_Error for
4492 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
4493 -- See ACVC test C4A012B.
4495 if Expv >= 0 and then Expv <= 4 then
4497 -- X ** 0 = 1 (or 1.0)
4500 if Ekind (Typ) in Integer_Kind then
4501 Xnode := Make_Integer_Literal (Loc, Intval => 1);
4503 Xnode := Make_Real_Literal (Loc, Ureal_1);
4515 Make_Op_Multiply (Loc,
4516 Left_Opnd => Duplicate_Subexpr (Base),
4517 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4519 -- X ** 3 = X * X * X
4523 Make_Op_Multiply (Loc,
4525 Make_Op_Multiply (Loc,
4526 Left_Opnd => Duplicate_Subexpr (Base),
4527 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
4528 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4531 -- En : constant base'type := base * base;
4537 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
4539 Insert_Actions (N, New_List (
4540 Make_Object_Declaration (Loc,
4541 Defining_Identifier => Temp,
4542 Constant_Present => True,
4543 Object_Definition => New_Reference_To (Typ, Loc),
4545 Make_Op_Multiply (Loc,
4546 Left_Opnd => Duplicate_Subexpr (Base),
4547 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
4550 Make_Op_Multiply (Loc,
4551 Left_Opnd => New_Reference_To (Temp, Loc),
4552 Right_Opnd => New_Reference_To (Temp, Loc));
4556 Analyze_And_Resolve (N, Typ);
4561 -- Case of (2 ** expression) appearing as an argument of an integer
4562 -- multiplication, or as the right argument of a division of a non-
4563 -- negative integer. In such cases we leave the node untouched, setting
4564 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
4565 -- of the higher level node converts it into a shift.
4567 if Nkind (Base) = N_Integer_Literal
4568 and then Intval (Base) = 2
4569 and then Is_Integer_Type (Root_Type (Exptyp))
4570 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
4571 and then Is_Unsigned_Type (Exptyp)
4573 and then Nkind (Parent (N)) in N_Binary_Op
4576 P : constant Node_Id := Parent (N);
4577 L : constant Node_Id := Left_Opnd (P);
4578 R : constant Node_Id := Right_Opnd (P);
4581 if (Nkind (P) = N_Op_Multiply
4583 ((Is_Integer_Type (Etype (L)) and then R = N)
4585 (Is_Integer_Type (Etype (R)) and then L = N))
4586 and then not Do_Overflow_Check (P))
4589 (Nkind (P) = N_Op_Divide
4590 and then Is_Integer_Type (Etype (L))
4591 and then Is_Unsigned_Type (Etype (L))
4593 and then not Do_Overflow_Check (P))
4595 Set_Is_Power_Of_2_For_Shift (N);
4601 -- Fall through if exponentiation must be done using a runtime routine
4603 -- First deal with modular case
4605 if Is_Modular_Integer_Type (Rtyp) then
4607 -- Non-binary case, we call the special exponentiation routine for
4608 -- the non-binary case, converting the argument to Long_Long_Integer
4609 -- and passing the modulus value. Then the result is converted back
4610 -- to the base type.
4612 if Non_Binary_Modulus (Rtyp) then
4615 Make_Function_Call (Loc,
4616 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
4617 Parameter_Associations => New_List (
4618 Convert_To (Standard_Integer, Base),
4619 Make_Integer_Literal (Loc, Modulus (Rtyp)),
4622 -- Binary case, in this case, we call one of two routines, either
4623 -- the unsigned integer case, or the unsigned long long integer
4624 -- case, with a final "and" operation to do the required mod.
4627 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
4628 Ent := RTE (RE_Exp_Unsigned);
4630 Ent := RTE (RE_Exp_Long_Long_Unsigned);
4637 Make_Function_Call (Loc,
4638 Name => New_Reference_To (Ent, Loc),
4639 Parameter_Associations => New_List (
4640 Convert_To (Etype (First_Formal (Ent)), Base),
4643 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
4647 -- Common exit point for modular type case
4649 Analyze_And_Resolve (N, Typ);
4652 -- Signed integer cases, done using either Integer or Long_Long_Integer.
4653 -- It is not worth having routines for Short_[Short_]Integer, since for
4654 -- most machines it would not help, and it would generate more code that
4655 -- might need certification in the HI-E case.
4657 -- In the integer cases, we have two routines, one for when overflow
4658 -- checks are required, and one when they are not required, since
4659 -- there is a real gain in ommitting checks on many machines.
4661 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
4662 or else (Rtyp = Base_Type (Standard_Long_Integer)
4664 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
4665 or else (Rtyp = Universal_Integer)
4667 Etyp := Standard_Long_Long_Integer;
4670 Rent := RE_Exp_Long_Long_Integer;
4672 Rent := RE_Exn_Long_Long_Integer;
4675 elsif Is_Signed_Integer_Type (Rtyp) then
4676 Etyp := Standard_Integer;
4679 Rent := RE_Exp_Integer;
4681 Rent := RE_Exn_Integer;
4684 -- Floating-point cases, always done using Long_Long_Float. We do not
4685 -- need separate routines for the overflow case here, since in the case
4686 -- of floating-point, we generate infinities anyway as a rule (either
4687 -- that or we automatically trap overflow), and if there is an infinity
4688 -- generated and a range check is required, the check will fail anyway.
4691 pragma Assert (Is_Floating_Point_Type (Rtyp));
4692 Etyp := Standard_Long_Long_Float;
4693 Rent := RE_Exn_Long_Long_Float;
4696 -- Common processing for integer cases and floating-point cases.
4697 -- If we are in the right type, we can call runtime routine directly
4700 and then Rtyp /= Universal_Integer
4701 and then Rtyp /= Universal_Real
4704 Make_Function_Call (Loc,
4705 Name => New_Reference_To (RTE (Rent), Loc),
4706 Parameter_Associations => New_List (Base, Exp)));
4708 -- Otherwise we have to introduce conversions (conversions are also
4709 -- required in the universal cases, since the runtime routine is
4710 -- typed using one of the standard types.
4715 Make_Function_Call (Loc,
4716 Name => New_Reference_To (RTE (Rent), Loc),
4717 Parameter_Associations => New_List (
4718 Convert_To (Etyp, Base),
4722 Analyze_And_Resolve (N, Typ);
4726 when RE_Not_Available =>
4728 end Expand_N_Op_Expon;
4730 --------------------
4731 -- Expand_N_Op_Ge --
4732 --------------------
4734 procedure Expand_N_Op_Ge (N : Node_Id) is
4735 Typ : constant Entity_Id := Etype (N);
4736 Op1 : constant Node_Id := Left_Opnd (N);
4737 Op2 : constant Node_Id := Right_Opnd (N);
4738 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4741 Binary_Op_Validity_Checks (N);
4743 if Vax_Float (Typ1) then
4744 Expand_Vax_Comparison (N);
4747 elsif Is_Array_Type (Typ1) then
4748 Expand_Array_Comparison (N);
4752 if Is_Boolean_Type (Typ1) then
4753 Adjust_Condition (Op1);
4754 Adjust_Condition (Op2);
4755 Set_Etype (N, Standard_Boolean);
4756 Adjust_Result_Type (N, Typ);
4759 Rewrite_Comparison (N);
4762 --------------------
4763 -- Expand_N_Op_Gt --
4764 --------------------
4766 procedure Expand_N_Op_Gt (N : Node_Id) is
4767 Typ : constant Entity_Id := Etype (N);
4768 Op1 : constant Node_Id := Left_Opnd (N);
4769 Op2 : constant Node_Id := Right_Opnd (N);
4770 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4773 Binary_Op_Validity_Checks (N);
4775 if Vax_Float (Typ1) then
4776 Expand_Vax_Comparison (N);
4779 elsif Is_Array_Type (Typ1) then
4780 Expand_Array_Comparison (N);
4784 if Is_Boolean_Type (Typ1) then
4785 Adjust_Condition (Op1);
4786 Adjust_Condition (Op2);
4787 Set_Etype (N, Standard_Boolean);
4788 Adjust_Result_Type (N, Typ);
4791 Rewrite_Comparison (N);
4794 --------------------
4795 -- Expand_N_Op_Le --
4796 --------------------
4798 procedure Expand_N_Op_Le (N : Node_Id) is
4799 Typ : constant Entity_Id := Etype (N);
4800 Op1 : constant Node_Id := Left_Opnd (N);
4801 Op2 : constant Node_Id := Right_Opnd (N);
4802 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4805 Binary_Op_Validity_Checks (N);
4807 if Vax_Float (Typ1) then
4808 Expand_Vax_Comparison (N);
4811 elsif Is_Array_Type (Typ1) then
4812 Expand_Array_Comparison (N);
4816 if Is_Boolean_Type (Typ1) then
4817 Adjust_Condition (Op1);
4818 Adjust_Condition (Op2);
4819 Set_Etype (N, Standard_Boolean);
4820 Adjust_Result_Type (N, Typ);
4823 Rewrite_Comparison (N);
4826 --------------------
4827 -- Expand_N_Op_Lt --
4828 --------------------
4830 procedure Expand_N_Op_Lt (N : Node_Id) is
4831 Typ : constant Entity_Id := Etype (N);
4832 Op1 : constant Node_Id := Left_Opnd (N);
4833 Op2 : constant Node_Id := Right_Opnd (N);
4834 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4837 Binary_Op_Validity_Checks (N);
4839 if Vax_Float (Typ1) then
4840 Expand_Vax_Comparison (N);
4843 elsif Is_Array_Type (Typ1) then
4844 Expand_Array_Comparison (N);
4848 if Is_Boolean_Type (Typ1) then
4849 Adjust_Condition (Op1);
4850 Adjust_Condition (Op2);
4851 Set_Etype (N, Standard_Boolean);
4852 Adjust_Result_Type (N, Typ);
4855 Rewrite_Comparison (N);
4858 -----------------------
4859 -- Expand_N_Op_Minus --
4860 -----------------------
4862 procedure Expand_N_Op_Minus (N : Node_Id) is
4863 Loc : constant Source_Ptr := Sloc (N);
4864 Typ : constant Entity_Id := Etype (N);
4867 Unary_Op_Validity_Checks (N);
4869 if not Backend_Overflow_Checks_On_Target
4870 and then Is_Signed_Integer_Type (Etype (N))
4871 and then Do_Overflow_Check (N)
4873 -- Software overflow checking expands -expr into (0 - expr)
4876 Make_Op_Subtract (Loc,
4877 Left_Opnd => Make_Integer_Literal (Loc, 0),
4878 Right_Opnd => Right_Opnd (N)));
4880 Analyze_And_Resolve (N, Typ);
4882 -- Vax floating-point types case
4884 elsif Vax_Float (Etype (N)) then
4885 Expand_Vax_Arith (N);
4887 end Expand_N_Op_Minus;
4889 ---------------------
4890 -- Expand_N_Op_Mod --
4891 ---------------------
4893 procedure Expand_N_Op_Mod (N : Node_Id) is
4894 Loc : constant Source_Ptr := Sloc (N);
4895 Typ : constant Entity_Id := Etype (N);
4896 Left : constant Node_Id := Left_Opnd (N);
4897 Right : constant Node_Id := Right_Opnd (N);
4898 DOC : constant Boolean := Do_Overflow_Check (N);
4899 DDC : constant Boolean := Do_Division_Check (N);
4910 Binary_Op_Validity_Checks (N);
4912 Determine_Range (Right, ROK, Rlo, Rhi);
4913 Determine_Range (Left, LOK, Llo, Lhi);
4915 -- Convert mod to rem if operands are known non-negative. We do this
4916 -- since it is quite likely that this will improve the quality of code,
4917 -- (the operation now corresponds to the hardware remainder), and it
4918 -- does not seem likely that it could be harmful.
4920 if LOK and then Llo >= 0
4922 ROK and then Rlo >= 0
4925 Make_Op_Rem (Sloc (N),
4926 Left_Opnd => Left_Opnd (N),
4927 Right_Opnd => Right_Opnd (N)));
4929 -- Instead of reanalyzing the node we do the analysis manually.
4930 -- This avoids anomalies when the replacement is done in an
4931 -- instance and is epsilon more efficient.
4933 Set_Entity (N, Standard_Entity (S_Op_Rem));
4935 Set_Do_Overflow_Check (N, DOC);
4936 Set_Do_Division_Check (N, DDC);
4937 Expand_N_Op_Rem (N);
4940 -- Otherwise, normal mod processing
4943 if Is_Integer_Type (Etype (N)) then
4944 Apply_Divide_Check (N);
4947 -- Apply optimization x mod 1 = 0. We don't really need that with
4948 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
4949 -- certainly harmless.
4951 if Is_Integer_Type (Etype (N))
4952 and then Compile_Time_Known_Value (Right)
4953 and then Expr_Value (Right) = Uint_1
4955 Rewrite (N, Make_Integer_Literal (Loc, 0));
4956 Analyze_And_Resolve (N, Typ);
4960 -- Deal with annoying case of largest negative number remainder
4961 -- minus one. Gigi does not handle this case correctly, because
4962 -- it generates a divide instruction which may trap in this case.
4964 -- In fact the check is quite easy, if the right operand is -1,
4965 -- then the mod value is always 0, and we can just ignore the
4966 -- left operand completely in this case.
4968 -- The operand type may be private (e.g. in the expansion of an
4969 -- an intrinsic operation) so we must use the underlying type to
4970 -- get the bounds, and convert the literals explicitly.
4974 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
4976 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
4978 ((not LOK) or else (Llo = LLB))
4981 Make_Conditional_Expression (Loc,
4982 Expressions => New_List (
4984 Left_Opnd => Duplicate_Subexpr (Right),
4986 Unchecked_Convert_To (Typ,
4987 Make_Integer_Literal (Loc, -1))),
4988 Unchecked_Convert_To (Typ,
4989 Make_Integer_Literal (Loc, Uint_0)),
4990 Relocate_Node (N))));
4992 Set_Analyzed (Next (Next (First (Expressions (N)))));
4993 Analyze_And_Resolve (N, Typ);
4996 end Expand_N_Op_Mod;
4998 --------------------------
4999 -- Expand_N_Op_Multiply --
5000 --------------------------
5002 procedure Expand_N_Op_Multiply (N : Node_Id) is
5003 Loc : constant Source_Ptr := Sloc (N);
5004 Lop : constant Node_Id := Left_Opnd (N);
5005 Rop : constant Node_Id := Right_Opnd (N);
5007 Lp2 : constant Boolean :=
5008 Nkind (Lop) = N_Op_Expon
5009 and then Is_Power_Of_2_For_Shift (Lop);
5011 Rp2 : constant Boolean :=
5012 Nkind (Rop) = N_Op_Expon
5013 and then Is_Power_Of_2_For_Shift (Rop);
5015 Ltyp : constant Entity_Id := Etype (Lop);
5016 Rtyp : constant Entity_Id := Etype (Rop);
5017 Typ : Entity_Id := Etype (N);
5020 Binary_Op_Validity_Checks (N);
5022 -- Special optimizations for integer types
5024 if Is_Integer_Type (Typ) then
5026 -- N * 0 = 0 * N = 0 for integer types
5028 if (Compile_Time_Known_Value (Rop)
5029 and then Expr_Value (Rop) = Uint_0)
5031 (Compile_Time_Known_Value (Lop)
5032 and then Expr_Value (Lop) = Uint_0)
5034 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5035 Analyze_And_Resolve (N, Typ);
5039 -- N * 1 = 1 * N = N for integer types
5041 -- This optimisation is not done if we are going to
5042 -- rewrite the product 1 * 2 ** N to a shift.
5044 if Compile_Time_Known_Value (Rop)
5045 and then Expr_Value (Rop) = Uint_1
5051 elsif Compile_Time_Known_Value (Lop)
5052 and then Expr_Value (Lop) = Uint_1
5060 -- Deal with VAX float case
5062 if Vax_Float (Typ) then
5063 Expand_Vax_Arith (N);
5067 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5068 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5069 -- operand is an integer, as required for this to work.
5074 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5078 Left_Opnd => Make_Integer_Literal (Loc, 2),
5081 Left_Opnd => Right_Opnd (Lop),
5082 Right_Opnd => Right_Opnd (Rop))));
5083 Analyze_And_Resolve (N, Typ);
5088 Make_Op_Shift_Left (Loc,
5091 Convert_To (Standard_Natural, Right_Opnd (Rop))));
5092 Analyze_And_Resolve (N, Typ);
5096 -- Same processing for the operands the other way round
5100 Make_Op_Shift_Left (Loc,
5103 Convert_To (Standard_Natural, Right_Opnd (Lop))));
5104 Analyze_And_Resolve (N, Typ);
5108 -- Do required fixup of universal fixed operation
5110 if Typ = Universal_Fixed then
5111 Fixup_Universal_Fixed_Operation (N);
5115 -- Multiplications with fixed-point results
5117 if Is_Fixed_Point_Type (Typ) then
5119 -- No special processing if Treat_Fixed_As_Integer is set,
5120 -- since from a semantic point of view such operations are
5121 -- simply integer operations and will be treated that way.
5123 if not Treat_Fixed_As_Integer (N) then
5125 -- Case of fixed * integer => fixed
5127 if Is_Integer_Type (Rtyp) then
5128 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
5130 -- Case of integer * fixed => fixed
5132 elsif Is_Integer_Type (Ltyp) then
5133 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
5135 -- Case of fixed * fixed => fixed
5138 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
5142 -- Other cases of multiplication of fixed-point operands. Again
5143 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
5145 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
5146 and then not Treat_Fixed_As_Integer (N)
5148 if Is_Integer_Type (Typ) then
5149 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
5151 pragma Assert (Is_Floating_Point_Type (Typ));
5152 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
5155 -- Mixed-mode operations can appear in a non-static universal
5156 -- context, in which case the integer argument must be converted
5159 elsif Typ = Universal_Real
5160 and then Is_Integer_Type (Rtyp)
5162 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
5164 Analyze_And_Resolve (Rop, Universal_Real);
5166 elsif Typ = Universal_Real
5167 and then Is_Integer_Type (Ltyp)
5169 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
5171 Analyze_And_Resolve (Lop, Universal_Real);
5173 -- Non-fixed point cases, check software overflow checking required
5175 elsif Is_Signed_Integer_Type (Etype (N)) then
5176 Apply_Arithmetic_Overflow_Check (N);
5178 end Expand_N_Op_Multiply;
5180 --------------------
5181 -- Expand_N_Op_Ne --
5182 --------------------
5184 -- Rewrite node as the negation of an equality operation, and reanalyze.
5185 -- The equality to be used is defined in the same scope and has the same
5186 -- signature. It must be set explicitly because in an instance it may not
5187 -- have the same visibility as in the generic unit.
5189 procedure Expand_N_Op_Ne (N : Node_Id) is
5190 Loc : constant Source_Ptr := Sloc (N);
5192 Ne : constant Entity_Id := Entity (N);
5195 Binary_Op_Validity_Checks (N);
5201 Left_Opnd => Left_Opnd (N),
5202 Right_Opnd => Right_Opnd (N)));
5203 Set_Paren_Count (Right_Opnd (Neg), 1);
5205 if Scope (Ne) /= Standard_Standard then
5206 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
5209 -- For navigation purposes, the inequality is treated as an implicit
5210 -- reference to the corresponding equality. Preserve the Comes_From_
5211 -- source flag so that the proper Xref entry is generated.
5213 Preserve_Comes_From_Source (Neg, N);
5214 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
5216 Analyze_And_Resolve (N, Standard_Boolean);
5219 ---------------------
5220 -- Expand_N_Op_Not --
5221 ---------------------
5223 -- If the argument is other than a Boolean array type, there is no
5224 -- special expansion required.
5226 -- For the packed case, we call the special routine in Exp_Pakd, except
5227 -- that if the component size is greater than one, we use the standard
5228 -- routine generating a gruesome loop (it is so peculiar to have packed
5229 -- arrays with non-standard Boolean representations anyway, so it does
5230 -- not matter that we do not handle this case efficiently).
5232 -- For the unpacked case (and for the special packed case where we have
5233 -- non standard Booleans, as discussed above), we generate and insert
5234 -- into the tree the following function definition:
5236 -- function Nnnn (A : arr) is
5239 -- for J in a'range loop
5240 -- B (J) := not A (J);
5245 -- Here arr is the actual subtype of the parameter (and hence always
5246 -- constrained). Then we replace the not with a call to this function.
5248 procedure Expand_N_Op_Not (N : Node_Id) is
5249 Loc : constant Source_Ptr := Sloc (N);
5250 Typ : constant Entity_Id := Etype (N);
5259 Func_Name : Entity_Id;
5260 Loop_Statement : Node_Id;
5263 Unary_Op_Validity_Checks (N);
5265 -- For boolean operand, deal with non-standard booleans
5267 if Is_Boolean_Type (Typ) then
5268 Adjust_Condition (Right_Opnd (N));
5269 Set_Etype (N, Standard_Boolean);
5270 Adjust_Result_Type (N, Typ);
5274 -- Only array types need any other processing
5276 if not Is_Array_Type (Typ) then
5280 -- Case of array operand. If bit packed, handle it in Exp_Pakd
5282 if Is_Bit_Packed_Array (Typ) and then Component_Size (Typ) = 1 then
5283 Expand_Packed_Not (N);
5287 -- Case of array operand which is not bit-packed. If the context is
5288 -- a safe assignment, call in-place operation, If context is a larger
5289 -- boolean expression in the context of a safe assignment, expansion is
5290 -- done by enclosing operation.
5292 Opnd := Relocate_Node (Right_Opnd (N));
5293 Convert_To_Actual_Subtype (Opnd);
5294 Arr := Etype (Opnd);
5295 Ensure_Defined (Arr, N);
5297 if Nkind (Parent (N)) = N_Assignment_Statement then
5298 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
5299 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5302 -- Special case the negation of a binary operation
5304 elsif (Nkind (Opnd) = N_Op_And
5305 or else Nkind (Opnd) = N_Op_Or
5306 or else Nkind (Opnd) = N_Op_Xor)
5307 and then Safe_In_Place_Array_Op
5308 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
5310 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5314 elsif Nkind (Parent (N)) in N_Binary_Op
5315 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
5318 Op1 : constant Node_Id := Left_Opnd (Parent (N));
5319 Op2 : constant Node_Id := Right_Opnd (Parent (N));
5320 Lhs : constant Node_Id := Name (Parent (Parent (N)));
5323 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
5325 and then Nkind (Op2) = N_Op_Not
5327 -- (not A) op (not B) can be reduced to a single call
5332 and then Nkind (Parent (N)) = N_Op_Xor
5334 -- A xor (not B) can also be special-cased
5342 A := Make_Defining_Identifier (Loc, Name_uA);
5343 B := Make_Defining_Identifier (Loc, Name_uB);
5344 J := Make_Defining_Identifier (Loc, Name_uJ);
5347 Make_Indexed_Component (Loc,
5348 Prefix => New_Reference_To (A, Loc),
5349 Expressions => New_List (New_Reference_To (J, Loc)));
5352 Make_Indexed_Component (Loc,
5353 Prefix => New_Reference_To (B, Loc),
5354 Expressions => New_List (New_Reference_To (J, Loc)));
5357 Make_Implicit_Loop_Statement (N,
5358 Identifier => Empty,
5361 Make_Iteration_Scheme (Loc,
5362 Loop_Parameter_Specification =>
5363 Make_Loop_Parameter_Specification (Loc,
5364 Defining_Identifier => J,
5365 Discrete_Subtype_Definition =>
5366 Make_Attribute_Reference (Loc,
5367 Prefix => Make_Identifier (Loc, Chars (A)),
5368 Attribute_Name => Name_Range))),
5370 Statements => New_List (
5371 Make_Assignment_Statement (Loc,
5373 Expression => Make_Op_Not (Loc, A_J))));
5375 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
5376 Set_Is_Inlined (Func_Name);
5379 Make_Subprogram_Body (Loc,
5381 Make_Function_Specification (Loc,
5382 Defining_Unit_Name => Func_Name,
5383 Parameter_Specifications => New_List (
5384 Make_Parameter_Specification (Loc,
5385 Defining_Identifier => A,
5386 Parameter_Type => New_Reference_To (Typ, Loc))),
5387 Subtype_Mark => New_Reference_To (Typ, Loc)),
5389 Declarations => New_List (
5390 Make_Object_Declaration (Loc,
5391 Defining_Identifier => B,
5392 Object_Definition => New_Reference_To (Arr, Loc))),
5394 Handled_Statement_Sequence =>
5395 Make_Handled_Sequence_Of_Statements (Loc,
5396 Statements => New_List (
5398 Make_Return_Statement (Loc,
5400 Make_Identifier (Loc, Chars (B)))))));
5403 Make_Function_Call (Loc,
5404 Name => New_Reference_To (Func_Name, Loc),
5405 Parameter_Associations => New_List (Opnd)));
5407 Analyze_And_Resolve (N, Typ);
5408 end Expand_N_Op_Not;
5410 --------------------
5411 -- Expand_N_Op_Or --
5412 --------------------
5414 procedure Expand_N_Op_Or (N : Node_Id) is
5415 Typ : constant Entity_Id := Etype (N);
5418 Binary_Op_Validity_Checks (N);
5420 if Is_Array_Type (Etype (N)) then
5421 Expand_Boolean_Operator (N);
5423 elsif Is_Boolean_Type (Etype (N)) then
5424 Adjust_Condition (Left_Opnd (N));
5425 Adjust_Condition (Right_Opnd (N));
5426 Set_Etype (N, Standard_Boolean);
5427 Adjust_Result_Type (N, Typ);
5431 ----------------------
5432 -- Expand_N_Op_Plus --
5433 ----------------------
5435 procedure Expand_N_Op_Plus (N : Node_Id) is
5437 Unary_Op_Validity_Checks (N);
5438 end Expand_N_Op_Plus;
5440 ---------------------
5441 -- Expand_N_Op_Rem --
5442 ---------------------
5444 procedure Expand_N_Op_Rem (N : Node_Id) is
5445 Loc : constant Source_Ptr := Sloc (N);
5446 Typ : constant Entity_Id := Etype (N);
5448 Left : constant Node_Id := Left_Opnd (N);
5449 Right : constant Node_Id := Right_Opnd (N);
5460 Binary_Op_Validity_Checks (N);
5462 if Is_Integer_Type (Etype (N)) then
5463 Apply_Divide_Check (N);
5466 -- Apply optimization x rem 1 = 0. We don't really need that with
5467 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5468 -- certainly harmless.
5470 if Is_Integer_Type (Etype (N))
5471 and then Compile_Time_Known_Value (Right)
5472 and then Expr_Value (Right) = Uint_1
5474 Rewrite (N, Make_Integer_Literal (Loc, 0));
5475 Analyze_And_Resolve (N, Typ);
5479 -- Deal with annoying case of largest negative number remainder
5480 -- minus one. Gigi does not handle this case correctly, because
5481 -- it generates a divide instruction which may trap in this case.
5483 -- In fact the check is quite easy, if the right operand is -1,
5484 -- then the remainder is always 0, and we can just ignore the
5485 -- left operand completely in this case.
5487 Determine_Range (Right, ROK, Rlo, Rhi);
5488 Determine_Range (Left, LOK, Llo, Lhi);
5490 -- The operand type may be private (e.g. in the expansion of an
5491 -- an intrinsic operation) so we must use the underlying type to
5492 -- get the bounds, and convert the literals explicitly.
5496 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5498 -- Now perform the test, generating code only if needed
5500 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5502 ((not LOK) or else (Llo = LLB))
5505 Make_Conditional_Expression (Loc,
5506 Expressions => New_List (
5508 Left_Opnd => Duplicate_Subexpr (Right),
5510 Unchecked_Convert_To (Typ,
5511 Make_Integer_Literal (Loc, -1))),
5513 Unchecked_Convert_To (Typ,
5514 Make_Integer_Literal (Loc, Uint_0)),
5516 Relocate_Node (N))));
5518 Set_Analyzed (Next (Next (First (Expressions (N)))));
5519 Analyze_And_Resolve (N, Typ);
5521 end Expand_N_Op_Rem;
5523 -----------------------------
5524 -- Expand_N_Op_Rotate_Left --
5525 -----------------------------
5527 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
5529 Binary_Op_Validity_Checks (N);
5530 end Expand_N_Op_Rotate_Left;
5532 ------------------------------
5533 -- Expand_N_Op_Rotate_Right --
5534 ------------------------------
5536 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
5538 Binary_Op_Validity_Checks (N);
5539 end Expand_N_Op_Rotate_Right;
5541 ----------------------------
5542 -- Expand_N_Op_Shift_Left --
5543 ----------------------------
5545 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
5547 Binary_Op_Validity_Checks (N);
5548 end Expand_N_Op_Shift_Left;
5550 -----------------------------
5551 -- Expand_N_Op_Shift_Right --
5552 -----------------------------
5554 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
5556 Binary_Op_Validity_Checks (N);
5557 end Expand_N_Op_Shift_Right;
5559 ----------------------------------------
5560 -- Expand_N_Op_Shift_Right_Arithmetic --
5561 ----------------------------------------
5563 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
5565 Binary_Op_Validity_Checks (N);
5566 end Expand_N_Op_Shift_Right_Arithmetic;
5568 --------------------------
5569 -- Expand_N_Op_Subtract --
5570 --------------------------
5572 procedure Expand_N_Op_Subtract (N : Node_Id) is
5573 Typ : constant Entity_Id := Etype (N);
5576 Binary_Op_Validity_Checks (N);
5578 -- N - 0 = N for integer types
5580 if Is_Integer_Type (Typ)
5581 and then Compile_Time_Known_Value (Right_Opnd (N))
5582 and then Expr_Value (Right_Opnd (N)) = 0
5584 Rewrite (N, Left_Opnd (N));
5588 -- Arithemtic overflow checks for signed integer/fixed point types
5590 if Is_Signed_Integer_Type (Typ)
5591 or else Is_Fixed_Point_Type (Typ)
5593 Apply_Arithmetic_Overflow_Check (N);
5595 -- Vax floating-point types case
5597 elsif Vax_Float (Typ) then
5598 Expand_Vax_Arith (N);
5600 end Expand_N_Op_Subtract;
5602 ---------------------
5603 -- Expand_N_Op_Xor --
5604 ---------------------
5606 procedure Expand_N_Op_Xor (N : Node_Id) is
5607 Typ : constant Entity_Id := Etype (N);
5610 Binary_Op_Validity_Checks (N);
5612 if Is_Array_Type (Etype (N)) then
5613 Expand_Boolean_Operator (N);
5615 elsif Is_Boolean_Type (Etype (N)) then
5616 Adjust_Condition (Left_Opnd (N));
5617 Adjust_Condition (Right_Opnd (N));
5618 Set_Etype (N, Standard_Boolean);
5619 Adjust_Result_Type (N, Typ);
5621 end Expand_N_Op_Xor;
5623 ----------------------
5624 -- Expand_N_Or_Else --
5625 ----------------------
5627 -- Expand into conditional expression if Actions present, and also
5628 -- deal with optimizing case of arguments being True or False.
5630 procedure Expand_N_Or_Else (N : Node_Id) is
5631 Loc : constant Source_Ptr := Sloc (N);
5632 Typ : constant Entity_Id := Etype (N);
5633 Left : constant Node_Id := Left_Opnd (N);
5634 Right : constant Node_Id := Right_Opnd (N);
5638 -- Deal with non-standard booleans
5640 if Is_Boolean_Type (Typ) then
5641 Adjust_Condition (Left);
5642 Adjust_Condition (Right);
5643 Set_Etype (N, Standard_Boolean);
5646 -- Check for cases of left argument is True or False
5648 if Nkind (Left) = N_Identifier then
5650 -- If left argument is False, change (False or else Right) to Right.
5651 -- Any actions associated with Right will be executed unconditionally
5652 -- and can thus be inserted into the tree unconditionally.
5654 if Entity (Left) = Standard_False then
5655 if Present (Actions (N)) then
5656 Insert_Actions (N, Actions (N));
5660 Adjust_Result_Type (N, Typ);
5663 -- If left argument is True, change (True and then Right) to
5664 -- True. In this case we can forget the actions associated with
5665 -- Right, since they will never be executed.
5667 elsif Entity (Left) = Standard_True then
5668 Kill_Dead_Code (Right);
5669 Kill_Dead_Code (Actions (N));
5670 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5671 Adjust_Result_Type (N, Typ);
5676 -- If Actions are present, we expand
5678 -- left or else right
5682 -- if left then True else right end
5684 -- with the actions becoming the Else_Actions of the conditional
5685 -- expression. This conditional expression is then further expanded
5686 -- (and will eventually disappear)
5688 if Present (Actions (N)) then
5689 Actlist := Actions (N);
5691 Make_Conditional_Expression (Loc,
5692 Expressions => New_List (
5694 New_Occurrence_Of (Standard_True, Loc),
5697 Set_Else_Actions (N, Actlist);
5698 Analyze_And_Resolve (N, Standard_Boolean);
5699 Adjust_Result_Type (N, Typ);
5703 -- No actions present, check for cases of right argument True/False
5705 if Nkind (Right) = N_Identifier then
5707 -- Change (Left or else False) to Left. Note that we know there
5708 -- are no actions associated with the True operand, since we
5709 -- just checked for this case above.
5711 if Entity (Right) = Standard_False then
5714 -- Change (Left or else True) to True, making sure to preserve
5715 -- any side effects associated with the Left operand.
5717 elsif Entity (Right) = Standard_True then
5718 Remove_Side_Effects (Left);
5720 (N, New_Occurrence_Of (Standard_True, Loc));
5724 Adjust_Result_Type (N, Typ);
5725 end Expand_N_Or_Else;
5727 -----------------------------------
5728 -- Expand_N_Qualified_Expression --
5729 -----------------------------------
5731 procedure Expand_N_Qualified_Expression (N : Node_Id) is
5732 Operand : constant Node_Id := Expression (N);
5733 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
5736 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
5737 end Expand_N_Qualified_Expression;
5739 ---------------------------------
5740 -- Expand_N_Selected_Component --
5741 ---------------------------------
5743 -- If the selector is a discriminant of a concurrent object, rewrite the
5744 -- prefix to denote the corresponding record type.
5746 procedure Expand_N_Selected_Component (N : Node_Id) is
5747 Loc : constant Source_Ptr := Sloc (N);
5748 Par : constant Node_Id := Parent (N);
5749 P : constant Node_Id := Prefix (N);
5750 Ptyp : Entity_Id := Underlying_Type (Etype (P));
5755 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
5756 -- Gigi needs a temporary for prefixes that depend on a discriminant,
5757 -- unless the context of an assignment can provide size information.
5758 -- Don't we have a general routine that does this???
5760 -----------------------
5761 -- In_Left_Hand_Side --
5762 -----------------------
5764 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
5766 return (Nkind (Parent (Comp)) = N_Assignment_Statement
5767 and then Comp = Name (Parent (Comp)))
5768 or else (Present (Parent (Comp))
5769 and then Nkind (Parent (Comp)) in N_Subexpr
5770 and then In_Left_Hand_Side (Parent (Comp)));
5771 end In_Left_Hand_Side;
5773 -- Start of processing for Expand_N_Selected_Component
5776 -- Insert explicit dereference if required
5778 if Is_Access_Type (Ptyp) then
5779 Insert_Explicit_Dereference (P);
5780 Analyze_And_Resolve (P, Designated_Type (Ptyp));
5782 if Ekind (Etype (P)) = E_Private_Subtype
5783 and then Is_For_Access_Subtype (Etype (P))
5785 Set_Etype (P, Base_Type (Etype (P)));
5791 -- Deal with discriminant check required
5793 if Do_Discriminant_Check (N) then
5795 -- Present the discrminant checking function to the backend,
5796 -- so that it can inline the call to the function.
5799 (Discriminant_Checking_Func
5800 (Original_Record_Component (Entity (Selector_Name (N)))));
5802 -- Now reset the flag and generate the call
5804 Set_Do_Discriminant_Check (N, False);
5805 Generate_Discriminant_Check (N);
5808 -- Gigi cannot handle unchecked conversions that are the prefix of a
5809 -- selected component with discriminants. This must be checked during
5810 -- expansion, because during analysis the type of the selector is not
5811 -- known at the point the prefix is analyzed. If the conversion is the
5812 -- target of an assignment, then we cannot force the evaluation.
5814 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
5815 and then Has_Discriminants (Etype (N))
5816 and then not In_Left_Hand_Side (N)
5818 Force_Evaluation (Prefix (N));
5821 -- Remaining processing applies only if selector is a discriminant
5823 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
5825 -- If the selector is a discriminant of a constrained record type,
5826 -- we may be able to rewrite the expression with the actual value
5827 -- of the discriminant, a useful optimization in some cases.
5829 if Is_Record_Type (Ptyp)
5830 and then Has_Discriminants (Ptyp)
5831 and then Is_Constrained (Ptyp)
5833 -- Do this optimization for discrete types only, and not for
5834 -- access types (access discriminants get us into trouble!)
5836 if not Is_Discrete_Type (Etype (N)) then
5839 -- Don't do this on the left hand of an assignment statement.
5840 -- Normally one would think that references like this would
5841 -- not occur, but they do in generated code, and mean that
5842 -- we really do want to assign the discriminant!
5844 elsif Nkind (Par) = N_Assignment_Statement
5845 and then Name (Par) = N
5849 -- Don't do this optimization for the prefix of an attribute
5850 -- or the operand of an object renaming declaration since these
5851 -- are contexts where we do not want the value anyway.
5853 elsif (Nkind (Par) = N_Attribute_Reference
5854 and then Prefix (Par) = N)
5855 or else Is_Renamed_Object (N)
5859 -- Don't do this optimization if we are within the code for a
5860 -- discriminant check, since the whole point of such a check may
5861 -- be to verify the condition on which the code below depends!
5863 elsif Is_In_Discriminant_Check (N) then
5866 -- Green light to see if we can do the optimization. There is
5867 -- still one condition that inhibits the optimization below
5868 -- but now is the time to check the particular discriminant.
5871 -- Loop through discriminants to find the matching
5872 -- discriminant constraint to see if we can copy it.
5874 Disc := First_Discriminant (Ptyp);
5875 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
5876 Discr_Loop : while Present (Dcon) loop
5878 -- Check if this is the matching discriminant
5880 if Disc = Entity (Selector_Name (N)) then
5882 -- Here we have the matching discriminant. Check for
5883 -- the case of a discriminant of a component that is
5884 -- constrained by an outer discriminant, which cannot
5885 -- be optimized away.
5888 Denotes_Discriminant
5889 (Node (Dcon), Check_Protected => True)
5893 -- In the context of a case statement, the expression
5894 -- may have the base type of the discriminant, and we
5895 -- need to preserve the constraint to avoid spurious
5896 -- errors on missing cases.
5898 elsif Nkind (Parent (N)) = N_Case_Statement
5899 and then Etype (Node (Dcon)) /= Etype (Disc)
5902 Make_Qualified_Expression (Loc,
5904 New_Occurrence_Of (Etype (Disc), Loc),
5906 New_Copy_Tree (Node (Dcon))));
5907 Analyze_And_Resolve (N, Etype (Disc));
5909 -- In case that comes out as a static expression,
5910 -- reset it (a selected component is never static).
5912 Set_Is_Static_Expression (N, False);
5915 -- Otherwise we can just copy the constraint, but the
5916 -- result is certainly not static! In some cases the
5917 -- discriminant constraint has been analyzed in the
5918 -- context of the original subtype indication, but for
5919 -- itypes the constraint might not have been analyzed
5920 -- yet, and this must be done now.
5923 Rewrite (N, New_Copy_Tree (Node (Dcon)));
5924 Analyze_And_Resolve (N);
5925 Set_Is_Static_Expression (N, False);
5931 Next_Discriminant (Disc);
5932 end loop Discr_Loop;
5934 -- Note: the above loop should always find a matching
5935 -- discriminant, but if it does not, we just missed an
5936 -- optimization due to some glitch (perhaps a previous
5937 -- error), so ignore.
5942 -- The only remaining processing is in the case of a discriminant of
5943 -- a concurrent object, where we rewrite the prefix to denote the
5944 -- corresponding record type. If the type is derived and has renamed
5945 -- discriminants, use corresponding discriminant, which is the one
5946 -- that appears in the corresponding record.
5948 if not Is_Concurrent_Type (Ptyp) then
5952 Disc := Entity (Selector_Name (N));
5954 if Is_Derived_Type (Ptyp)
5955 and then Present (Corresponding_Discriminant (Disc))
5957 Disc := Corresponding_Discriminant (Disc);
5961 Make_Selected_Component (Loc,
5963 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
5965 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
5970 end Expand_N_Selected_Component;
5972 --------------------
5973 -- Expand_N_Slice --
5974 --------------------
5976 procedure Expand_N_Slice (N : Node_Id) is
5977 Loc : constant Source_Ptr := Sloc (N);
5978 Typ : constant Entity_Id := Etype (N);
5979 Pfx : constant Node_Id := Prefix (N);
5980 Ptp : Entity_Id := Etype (Pfx);
5982 function Is_Procedure_Actual (N : Node_Id) return Boolean;
5983 -- Check whether the argument is an actual for a procedure call,
5984 -- in which case the expansion of a bit-packed slice is deferred
5985 -- until the call itself is expanded. The reason this is required
5986 -- is that we might have an IN OUT or OUT parameter, and the copy out
5987 -- is essential, and that copy out would be missed if we created a
5988 -- temporary here in Expand_N_Slice. Note that we don't bother
5989 -- to test specifically for an IN OUT or OUT mode parameter, since it
5990 -- is a bit tricky to do, and it is harmless to defer expansion
5991 -- in the IN case, since the call processing will still generate the
5992 -- appropriate copy in operation, which will take care of the slice.
5994 procedure Make_Temporary;
5995 -- Create a named variable for the value of the slice, in
5996 -- cases where the back-end cannot handle it properly, e.g.
5997 -- when packed types or unaligned slices are involved.
5999 -------------------------
6000 -- Is_Procedure_Actual --
6001 -------------------------
6003 function Is_Procedure_Actual (N : Node_Id) return Boolean is
6004 Par : Node_Id := Parent (N);
6008 -- If our parent is a procedure call we can return
6010 if Nkind (Par) = N_Procedure_Call_Statement then
6013 -- If our parent is a type conversion, keep climbing the
6014 -- tree, since a type conversion can be a procedure actual.
6015 -- Also keep climbing if parameter association or a qualified
6016 -- expression, since these are additional cases that do can
6017 -- appear on procedure actuals.
6019 elsif Nkind (Par) = N_Type_Conversion
6020 or else Nkind (Par) = N_Parameter_Association
6021 or else Nkind (Par) = N_Qualified_Expression
6023 Par := Parent (Par);
6025 -- Any other case is not what we are looking for
6031 end Is_Procedure_Actual;
6033 --------------------
6034 -- Make_Temporary --
6035 --------------------
6037 procedure Make_Temporary is
6039 Ent : constant Entity_Id :=
6040 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
6043 Make_Object_Declaration (Loc,
6044 Defining_Identifier => Ent,
6045 Object_Definition => New_Occurrence_Of (Typ, Loc));
6047 Set_No_Initialization (Decl);
6049 Insert_Actions (N, New_List (
6051 Make_Assignment_Statement (Loc,
6052 Name => New_Occurrence_Of (Ent, Loc),
6053 Expression => Relocate_Node (N))));
6055 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6056 Analyze_And_Resolve (N, Typ);
6059 -- Start of processing for Expand_N_Slice
6062 -- Special handling for access types
6064 if Is_Access_Type (Ptp) then
6066 Ptp := Designated_Type (Ptp);
6069 Make_Explicit_Dereference (Sloc (N),
6070 Prefix => Relocate_Node (Pfx)));
6072 Analyze_And_Resolve (Pfx, Ptp);
6075 -- Range checks are potentially also needed for cases involving
6076 -- a slice indexed by a subtype indication, but Do_Range_Check
6077 -- can currently only be set for expressions ???
6079 if not Index_Checks_Suppressed (Ptp)
6080 and then (not Is_Entity_Name (Pfx)
6081 or else not Index_Checks_Suppressed (Entity (Pfx)))
6082 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
6084 Enable_Range_Check (Discrete_Range (N));
6087 -- The remaining case to be handled is packed slices. We can leave
6088 -- packed slices as they are in the following situations:
6090 -- 1. Right or left side of an assignment (we can handle this
6091 -- situation correctly in the assignment statement expansion).
6093 -- 2. Prefix of indexed component (the slide is optimized away
6094 -- in this case, see the start of Expand_N_Slice.
6096 -- 3. Object renaming declaration, since we want the name of
6097 -- the slice, not the value.
6099 -- 4. Argument to procedure call, since copy-in/copy-out handling
6100 -- may be required, and this is handled in the expansion of
6103 -- 5. Prefix of an address attribute (this is an error which
6104 -- is caught elsewhere, and the expansion would intefere
6105 -- with generating the error message).
6107 if not Is_Packed (Typ) then
6109 -- Apply transformation for actuals of a function call,
6110 -- where Expand_Actuals is not used.
6112 if Nkind (Parent (N)) = N_Function_Call
6113 and then Is_Possibly_Unaligned_Slice (N)
6118 elsif Nkind (Parent (N)) = N_Assignment_Statement
6119 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
6120 and then Parent (N) = Name (Parent (Parent (N))))
6124 elsif Nkind (Parent (N)) = N_Indexed_Component
6125 or else Is_Renamed_Object (N)
6126 or else Is_Procedure_Actual (N)
6130 elsif Nkind (Parent (N)) = N_Attribute_Reference
6131 and then Attribute_Name (Parent (N)) = Name_Address
6140 ------------------------------
6141 -- Expand_N_Type_Conversion --
6142 ------------------------------
6144 procedure Expand_N_Type_Conversion (N : Node_Id) is
6145 Loc : constant Source_Ptr := Sloc (N);
6146 Operand : constant Node_Id := Expression (N);
6147 Target_Type : constant Entity_Id := Etype (N);
6148 Operand_Type : Entity_Id := Etype (Operand);
6150 procedure Handle_Changed_Representation;
6151 -- This is called in the case of record and array type conversions
6152 -- to see if there is a change of representation to be handled.
6153 -- Change of representation is actually handled at the assignment
6154 -- statement level, and what this procedure does is rewrite node N
6155 -- conversion as an assignment to temporary. If there is no change
6156 -- of representation, then the conversion node is unchanged.
6158 procedure Real_Range_Check;
6159 -- Handles generation of range check for real target value
6161 -----------------------------------
6162 -- Handle_Changed_Representation --
6163 -----------------------------------
6165 procedure Handle_Changed_Representation is
6174 -- Nothing to do if no change of representation
6176 if Same_Representation (Operand_Type, Target_Type) then
6179 -- The real change of representation work is done by the assignment
6180 -- statement processing. So if this type conversion is appearing as
6181 -- the expression of an assignment statement, nothing needs to be
6182 -- done to the conversion.
6184 elsif Nkind (Parent (N)) = N_Assignment_Statement then
6187 -- Otherwise we need to generate a temporary variable, and do the
6188 -- change of representation assignment into that temporary variable.
6189 -- The conversion is then replaced by a reference to this variable.
6194 -- If type is unconstrained we have to add a constraint,
6195 -- copied from the actual value of the left hand side.
6197 if not Is_Constrained (Target_Type) then
6198 if Has_Discriminants (Operand_Type) then
6199 Disc := First_Discriminant (Operand_Type);
6201 if Disc /= First_Stored_Discriminant (Operand_Type) then
6202 Disc := First_Stored_Discriminant (Operand_Type);
6206 while Present (Disc) loop
6208 Make_Selected_Component (Loc,
6209 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
6211 Make_Identifier (Loc, Chars (Disc))));
6212 Next_Discriminant (Disc);
6215 elsif Is_Array_Type (Operand_Type) then
6216 N_Ix := First_Index (Target_Type);
6219 for J in 1 .. Number_Dimensions (Operand_Type) loop
6221 -- We convert the bounds explicitly. We use an unchecked
6222 -- conversion because bounds checks are done elsewhere.
6227 Unchecked_Convert_To (Etype (N_Ix),
6228 Make_Attribute_Reference (Loc,
6230 Duplicate_Subexpr_No_Checks
6231 (Operand, Name_Req => True),
6232 Attribute_Name => Name_First,
6233 Expressions => New_List (
6234 Make_Integer_Literal (Loc, J)))),
6237 Unchecked_Convert_To (Etype (N_Ix),
6238 Make_Attribute_Reference (Loc,
6240 Duplicate_Subexpr_No_Checks
6241 (Operand, Name_Req => True),
6242 Attribute_Name => Name_Last,
6243 Expressions => New_List (
6244 Make_Integer_Literal (Loc, J))))));
6251 Odef := New_Occurrence_Of (Target_Type, Loc);
6253 if Present (Cons) then
6255 Make_Subtype_Indication (Loc,
6256 Subtype_Mark => Odef,
6258 Make_Index_Or_Discriminant_Constraint (Loc,
6259 Constraints => Cons));
6262 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
6264 Make_Object_Declaration (Loc,
6265 Defining_Identifier => Temp,
6266 Object_Definition => Odef);
6268 Set_No_Initialization (Decl, True);
6270 -- Insert required actions. It is essential to suppress checks
6271 -- since we have suppressed default initialization, which means
6272 -- that the variable we create may have no discriminants.
6277 Make_Assignment_Statement (Loc,
6278 Name => New_Occurrence_Of (Temp, Loc),
6279 Expression => Relocate_Node (N))),
6280 Suppress => All_Checks);
6282 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6285 end Handle_Changed_Representation;
6287 ----------------------
6288 -- Real_Range_Check --
6289 ----------------------
6291 -- Case of conversions to floating-point or fixed-point. If range
6292 -- checks are enabled and the target type has a range constraint,
6299 -- Tnn : typ'Base := typ'Base (x);
6300 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6303 -- This is necessary when there is a conversion of integer to float
6304 -- or to fixed-point to ensure that the correct checks are made. It
6305 -- is not necessary for float to float where it is enough to simply
6306 -- set the Do_Range_Check flag.
6308 procedure Real_Range_Check is
6309 Btyp : constant Entity_Id := Base_Type (Target_Type);
6310 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
6311 Hi : constant Node_Id := Type_High_Bound (Target_Type);
6312 Xtyp : constant Entity_Id := Etype (Operand);
6317 -- Nothing to do if conversion was rewritten
6319 if Nkind (N) /= N_Type_Conversion then
6323 -- Nothing to do if range checks suppressed, or target has the
6324 -- same range as the base type (or is the base type).
6326 if Range_Checks_Suppressed (Target_Type)
6327 or else (Lo = Type_Low_Bound (Btyp)
6329 Hi = Type_High_Bound (Btyp))
6334 -- Nothing to do if expression is an entity on which checks
6335 -- have been suppressed.
6337 if Is_Entity_Name (Operand)
6338 and then Range_Checks_Suppressed (Entity (Operand))
6343 -- Nothing to do if bounds are all static and we can tell that
6344 -- the expression is within the bounds of the target. Note that
6345 -- if the operand is of an unconstrained floating-point type,
6346 -- then we do not trust it to be in range (might be infinite)
6349 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
6350 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
6353 if (not Is_Floating_Point_Type (Xtyp)
6354 or else Is_Constrained (Xtyp))
6355 and then Compile_Time_Known_Value (S_Lo)
6356 and then Compile_Time_Known_Value (S_Hi)
6357 and then Compile_Time_Known_Value (Hi)
6358 and then Compile_Time_Known_Value (Lo)
6361 D_Lov : constant Ureal := Expr_Value_R (Lo);
6362 D_Hiv : constant Ureal := Expr_Value_R (Hi);
6367 if Is_Real_Type (Xtyp) then
6368 S_Lov := Expr_Value_R (S_Lo);
6369 S_Hiv := Expr_Value_R (S_Hi);
6371 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
6372 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
6376 and then S_Lov >= D_Lov
6377 and then S_Hiv <= D_Hiv
6379 Set_Do_Range_Check (Operand, False);
6386 -- For float to float conversions, we are done
6388 if Is_Floating_Point_Type (Xtyp)
6390 Is_Floating_Point_Type (Btyp)
6395 -- Otherwise rewrite the conversion as described above
6397 Conv := Relocate_Node (N);
6399 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
6400 Set_Etype (Conv, Btyp);
6402 -- Enable overflow except in the case of integer to float
6403 -- conversions, where it is never required, since we can
6404 -- never have overflow in this case.
6406 if not Is_Integer_Type (Etype (Operand)) then
6407 Enable_Overflow_Check (Conv);
6411 Make_Defining_Identifier (Loc,
6412 Chars => New_Internal_Name ('T'));
6414 Insert_Actions (N, New_List (
6415 Make_Object_Declaration (Loc,
6416 Defining_Identifier => Tnn,
6417 Object_Definition => New_Occurrence_Of (Btyp, Loc),
6418 Expression => Conv),
6420 Make_Raise_Constraint_Error (Loc,
6425 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6427 Make_Attribute_Reference (Loc,
6428 Attribute_Name => Name_First,
6430 New_Occurrence_Of (Target_Type, Loc))),
6434 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6436 Make_Attribute_Reference (Loc,
6437 Attribute_Name => Name_Last,
6439 New_Occurrence_Of (Target_Type, Loc)))),
6440 Reason => CE_Range_Check_Failed)));
6442 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
6443 Analyze_And_Resolve (N, Btyp);
6444 end Real_Range_Check;
6446 -- Start of processing for Expand_N_Type_Conversion
6449 -- Nothing at all to do if conversion is to the identical type
6450 -- so remove the conversion completely, it is useless.
6452 if Operand_Type = Target_Type then
6453 Rewrite (N, Relocate_Node (Operand));
6457 -- Deal with Vax floating-point cases
6459 if Vax_Float (Operand_Type) or else Vax_Float (Target_Type) then
6460 Expand_Vax_Conversion (N);
6464 -- Nothing to do if this is the second argument of read. This
6465 -- is a "backwards" conversion that will be handled by the
6466 -- specialized code in attribute processing.
6468 if Nkind (Parent (N)) = N_Attribute_Reference
6469 and then Attribute_Name (Parent (N)) = Name_Read
6470 and then Next (First (Expressions (Parent (N)))) = N
6475 -- Here if we may need to expand conversion
6477 -- Special case of converting from non-standard boolean type
6479 if Is_Boolean_Type (Operand_Type)
6480 and then (Nonzero_Is_True (Operand_Type))
6482 Adjust_Condition (Operand);
6483 Set_Etype (Operand, Standard_Boolean);
6484 Operand_Type := Standard_Boolean;
6487 -- Case of converting to an access type
6489 if Is_Access_Type (Target_Type) then
6491 -- Apply an accessibility check if the operand is an
6492 -- access parameter. Note that other checks may still
6493 -- need to be applied below (such as tagged type checks).
6495 if Is_Entity_Name (Operand)
6496 and then Ekind (Entity (Operand)) in Formal_Kind
6497 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
6499 Apply_Accessibility_Check (Operand, Target_Type);
6501 -- If the level of the operand type is statically deeper
6502 -- then the level of the target type, then force Program_Error.
6503 -- Note that this can only occur for cases where the attribute
6504 -- is within the body of an instantiation (otherwise the
6505 -- conversion will already have been rejected as illegal).
6506 -- Note: warnings are issued by the analyzer for the instance
6509 elsif In_Instance_Body
6510 and then Type_Access_Level (Operand_Type) >
6511 Type_Access_Level (Target_Type)
6514 Make_Raise_Program_Error (Sloc (N),
6515 Reason => PE_Accessibility_Check_Failed));
6516 Set_Etype (N, Target_Type);
6518 -- When the operand is a selected access discriminant
6519 -- the check needs to be made against the level of the
6520 -- object denoted by the prefix of the selected name.
6521 -- Force Program_Error for this case as well (this
6522 -- accessibility violation can only happen if within
6523 -- the body of an instantiation).
6525 elsif In_Instance_Body
6526 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
6527 and then Nkind (Operand) = N_Selected_Component
6528 and then Object_Access_Level (Operand) >
6529 Type_Access_Level (Target_Type)
6532 Make_Raise_Program_Error (Sloc (N),
6533 Reason => PE_Accessibility_Check_Failed));
6534 Set_Etype (N, Target_Type);
6538 -- Case of conversions of tagged types and access to tagged types
6540 -- When needed, that is to say when the expression is class-wide,
6541 -- Add runtime a tag check for (strict) downward conversion by using
6542 -- the membership test, generating:
6544 -- [constraint_error when Operand not in Target_Type'Class]
6546 -- or in the access type case
6548 -- [constraint_error
6549 -- when Operand /= null
6550 -- and then Operand.all not in
6551 -- Designated_Type (Target_Type)'Class]
6553 if (Is_Access_Type (Target_Type)
6554 and then Is_Tagged_Type (Designated_Type (Target_Type)))
6555 or else Is_Tagged_Type (Target_Type)
6557 -- Do not do any expansion in the access type case if the
6558 -- parent is a renaming, since this is an error situation
6559 -- which will be caught by Sem_Ch8, and the expansion can
6560 -- intefere with this error check.
6562 if Is_Access_Type (Target_Type)
6563 and then Is_Renamed_Object (N)
6568 -- Oherwise, proceed with processing tagged conversion
6571 Actual_Operand_Type : Entity_Id;
6572 Actual_Target_Type : Entity_Id;
6577 if Is_Access_Type (Target_Type) then
6578 Actual_Operand_Type := Designated_Type (Operand_Type);
6579 Actual_Target_Type := Designated_Type (Target_Type);
6582 Actual_Operand_Type := Operand_Type;
6583 Actual_Target_Type := Target_Type;
6586 if Is_Class_Wide_Type (Actual_Operand_Type)
6587 and then Root_Type (Actual_Operand_Type) /= Actual_Target_Type
6588 and then Is_Ancestor
6589 (Root_Type (Actual_Operand_Type),
6591 and then not Tag_Checks_Suppressed (Actual_Target_Type)
6593 -- The conversion is valid for any descendant of the
6596 Actual_Target_Type := Class_Wide_Type (Actual_Target_Type);
6598 if Is_Access_Type (Target_Type) then
6603 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6604 Right_Opnd => Make_Null (Loc)),
6609 Make_Explicit_Dereference (Loc,
6611 Duplicate_Subexpr_No_Checks (Operand)),
6613 New_Reference_To (Actual_Target_Type, Loc)));
6618 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6620 New_Reference_To (Actual_Target_Type, Loc));
6624 Make_Raise_Constraint_Error (Loc,
6626 Reason => CE_Tag_Check_Failed));
6632 Make_Unchecked_Type_Conversion (Loc,
6633 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
6634 Expression => Relocate_Node (Expression (N)));
6636 Analyze_And_Resolve (N, Target_Type);
6641 -- Case of other access type conversions
6643 elsif Is_Access_Type (Target_Type) then
6644 Apply_Constraint_Check (Operand, Target_Type);
6646 -- Case of conversions from a fixed-point type
6648 -- These conversions require special expansion and processing, found
6649 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
6650 -- set, since from a semantic point of view, these are simple integer
6651 -- conversions, which do not need further processing.
6653 elsif Is_Fixed_Point_Type (Operand_Type)
6654 and then not Conversion_OK (N)
6656 -- We should never see universal fixed at this case, since the
6657 -- expansion of the constituent divide or multiply should have
6658 -- eliminated the explicit mention of universal fixed.
6660 pragma Assert (Operand_Type /= Universal_Fixed);
6662 -- Check for special case of the conversion to universal real
6663 -- that occurs as a result of the use of a round attribute.
6664 -- In this case, the real type for the conversion is taken
6665 -- from the target type of the Round attribute and the
6666 -- result must be marked as rounded.
6668 if Target_Type = Universal_Real
6669 and then Nkind (Parent (N)) = N_Attribute_Reference
6670 and then Attribute_Name (Parent (N)) = Name_Round
6672 Set_Rounded_Result (N);
6673 Set_Etype (N, Etype (Parent (N)));
6676 -- Otherwise do correct fixed-conversion, but skip these if the
6677 -- Conversion_OK flag is set, because from a semantic point of
6678 -- view these are simple integer conversions needing no further
6679 -- processing (the backend will simply treat them as integers)
6681 if not Conversion_OK (N) then
6682 if Is_Fixed_Point_Type (Etype (N)) then
6683 Expand_Convert_Fixed_To_Fixed (N);
6686 elsif Is_Integer_Type (Etype (N)) then
6687 Expand_Convert_Fixed_To_Integer (N);
6690 pragma Assert (Is_Floating_Point_Type (Etype (N)));
6691 Expand_Convert_Fixed_To_Float (N);
6696 -- Case of conversions to a fixed-point type
6698 -- These conversions require special expansion and processing, found
6699 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
6700 -- is set, since from a semantic point of view, these are simple
6701 -- integer conversions, which do not need further processing.
6703 elsif Is_Fixed_Point_Type (Target_Type)
6704 and then not Conversion_OK (N)
6706 if Is_Integer_Type (Operand_Type) then
6707 Expand_Convert_Integer_To_Fixed (N);
6710 pragma Assert (Is_Floating_Point_Type (Operand_Type));
6711 Expand_Convert_Float_To_Fixed (N);
6715 -- Case of float-to-integer conversions
6717 -- We also handle float-to-fixed conversions with Conversion_OK set
6718 -- since semantically the fixed-point target is treated as though it
6719 -- were an integer in such cases.
6721 elsif Is_Floating_Point_Type (Operand_Type)
6723 (Is_Integer_Type (Target_Type)
6725 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
6727 -- Special processing required if the conversion is the expression
6728 -- of a Truncation attribute reference. In this case we replace:
6730 -- ityp (ftyp'Truncation (x))
6736 -- with the Float_Truncate flag set. This is clearly more efficient
6738 if Nkind (Operand) = N_Attribute_Reference
6739 and then Attribute_Name (Operand) = Name_Truncation
6742 Relocate_Node (First (Expressions (Operand))));
6743 Set_Float_Truncate (N, True);
6746 -- One more check here, gcc is still not able to do conversions of
6747 -- this type with proper overflow checking, and so gigi is doing an
6748 -- approximation of what is required by doing floating-point compares
6749 -- with the end-point. But that can lose precision in some cases, and
6750 -- give a wrong result. Converting the operand to Long_Long_Float is
6751 -- helpful, but still does not catch all cases with 64-bit integers
6752 -- on targets with only 64-bit floats ???
6754 if Do_Range_Check (Operand) then
6756 Make_Type_Conversion (Loc,
6758 New_Occurrence_Of (Standard_Long_Long_Float, Loc),
6760 Relocate_Node (Operand)));
6762 Set_Etype (Operand, Standard_Long_Long_Float);
6763 Enable_Range_Check (Operand);
6764 Set_Do_Range_Check (Expression (Operand), False);
6767 -- Case of array conversions
6769 -- Expansion of array conversions, add required length/range checks
6770 -- but only do this if there is no change of representation. For
6771 -- handling of this case, see Handle_Changed_Representation.
6773 elsif Is_Array_Type (Target_Type) then
6775 if Is_Constrained (Target_Type) then
6776 Apply_Length_Check (Operand, Target_Type);
6778 Apply_Range_Check (Operand, Target_Type);
6781 Handle_Changed_Representation;
6783 -- Case of conversions of discriminated types
6785 -- Add required discriminant checks if target is constrained. Again
6786 -- this change is skipped if we have a change of representation.
6788 elsif Has_Discriminants (Target_Type)
6789 and then Is_Constrained (Target_Type)
6791 Apply_Discriminant_Check (Operand, Target_Type);
6792 Handle_Changed_Representation;
6794 -- Case of all other record conversions. The only processing required
6795 -- is to check for a change of representation requiring the special
6796 -- assignment processing.
6798 elsif Is_Record_Type (Target_Type) then
6800 -- Ada 2005 (AI-216): Program_Error is raised when converting from
6801 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
6802 -- Union type if the operand lacks inferable discriminants.
6804 if Is_Derived_Type (Operand_Type)
6805 and then Is_Unchecked_Union (Base_Type (Operand_Type))
6806 and then not Is_Constrained (Target_Type)
6807 and then not Is_Unchecked_Union (Base_Type (Target_Type))
6808 and then not Has_Inferable_Discriminants (Operand)
6810 -- To prevent Gigi from generating illegal code, we make a
6811 -- Program_Error node, but we give it the target type of the
6815 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
6816 Reason => PE_Unchecked_Union_Restriction);
6819 Set_Etype (PE, Target_Type);
6824 Handle_Changed_Representation;
6827 -- Case of conversions of enumeration types
6829 elsif Is_Enumeration_Type (Target_Type) then
6831 -- Special processing is required if there is a change of
6832 -- representation (from enumeration representation clauses)
6834 if not Same_Representation (Target_Type, Operand_Type) then
6836 -- Convert: x(y) to x'val (ytyp'val (y))
6839 Make_Attribute_Reference (Loc,
6840 Prefix => New_Occurrence_Of (Target_Type, Loc),
6841 Attribute_Name => Name_Val,
6842 Expressions => New_List (
6843 Make_Attribute_Reference (Loc,
6844 Prefix => New_Occurrence_Of (Operand_Type, Loc),
6845 Attribute_Name => Name_Pos,
6846 Expressions => New_List (Operand)))));
6848 Analyze_And_Resolve (N, Target_Type);
6851 -- Case of conversions to floating-point
6853 elsif Is_Floating_Point_Type (Target_Type) then
6856 -- The remaining cases require no front end processing
6862 -- At this stage, either the conversion node has been transformed
6863 -- into some other equivalent expression, or left as a conversion
6864 -- that can be handled by Gigi. The conversions that Gigi can handle
6865 -- are the following:
6867 -- Conversions with no change of representation or type
6869 -- Numeric conversions involving integer values, floating-point
6870 -- values, and fixed-point values. Fixed-point values are allowed
6871 -- only if Conversion_OK is set, i.e. if the fixed-point values
6872 -- are to be treated as integers.
6874 -- No other conversions should be passed to Gigi
6876 -- Check: are these rules stated in sinfo??? if so, why restate here???
6878 -- The only remaining step is to generate a range check if we still
6879 -- have a type conversion at this stage and Do_Range_Check is set.
6880 -- For now we do this only for conversions of discrete types.
6882 if Nkind (N) = N_Type_Conversion
6883 and then Is_Discrete_Type (Etype (N))
6886 Expr : constant Node_Id := Expression (N);
6891 if Do_Range_Check (Expr)
6892 and then Is_Discrete_Type (Etype (Expr))
6894 Set_Do_Range_Check (Expr, False);
6896 -- Before we do a range check, we have to deal with treating
6897 -- a fixed-point operand as an integer. The way we do this
6898 -- is simply to do an unchecked conversion to an appropriate
6899 -- integer type large enough to hold the result.
6901 -- This code is not active yet, because we are only dealing
6902 -- with discrete types so far ???
6904 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
6905 and then Treat_Fixed_As_Integer (Expr)
6907 Ftyp := Base_Type (Etype (Expr));
6909 if Esize (Ftyp) >= Esize (Standard_Integer) then
6910 Ityp := Standard_Long_Long_Integer;
6912 Ityp := Standard_Integer;
6915 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
6918 -- Reset overflow flag, since the range check will include
6919 -- dealing with possible overflow, and generate the check
6920 -- If Address is either source or target type, suppress
6921 -- range check to avoid typing anomalies when it is a visible
6924 Set_Do_Overflow_Check (N, False);
6925 if not Is_Descendent_Of_Address (Etype (Expr))
6926 and then not Is_Descendent_Of_Address (Target_Type)
6928 Generate_Range_Check
6929 (Expr, Target_Type, CE_Range_Check_Failed);
6934 end Expand_N_Type_Conversion;
6936 -----------------------------------
6937 -- Expand_N_Unchecked_Expression --
6938 -----------------------------------
6940 -- Remove the unchecked expression node from the tree. It's job was simply
6941 -- to make sure that its constituent expression was handled with checks
6942 -- off, and now that that is done, we can remove it from the tree, and
6943 -- indeed must, since gigi does not expect to see these nodes.
6945 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
6946 Exp : constant Node_Id := Expression (N);
6949 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
6951 end Expand_N_Unchecked_Expression;
6953 ----------------------------------------
6954 -- Expand_N_Unchecked_Type_Conversion --
6955 ----------------------------------------
6957 -- If this cannot be handled by Gigi and we haven't already made
6958 -- a temporary for it, do it now.
6960 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
6961 Target_Type : constant Entity_Id := Etype (N);
6962 Operand : constant Node_Id := Expression (N);
6963 Operand_Type : constant Entity_Id := Etype (Operand);
6966 -- If we have a conversion of a compile time known value to a target
6967 -- type and the value is in range of the target type, then we can simply
6968 -- replace the construct by an integer literal of the correct type. We
6969 -- only apply this to integer types being converted. Possibly it may
6970 -- apply in other cases, but it is too much trouble to worry about.
6972 -- Note that we do not do this transformation if the Kill_Range_Check
6973 -- flag is set, since then the value may be outside the expected range.
6974 -- This happens in the Normalize_Scalars case.
6976 if Is_Integer_Type (Target_Type)
6977 and then Is_Integer_Type (Operand_Type)
6978 and then Compile_Time_Known_Value (Operand)
6979 and then not Kill_Range_Check (N)
6982 Val : constant Uint := Expr_Value (Operand);
6985 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
6987 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
6989 Val >= Expr_Value (Type_Low_Bound (Target_Type))
6991 Val <= Expr_Value (Type_High_Bound (Target_Type))
6993 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
6995 -- If Address is the target type, just set the type
6996 -- to avoid a spurious type error on the literal when
6997 -- Address is a visible integer type.
6999 if Is_Descendent_Of_Address (Target_Type) then
7000 Set_Etype (N, Target_Type);
7002 Analyze_And_Resolve (N, Target_Type);
7010 -- Nothing to do if conversion is safe
7012 if Safe_Unchecked_Type_Conversion (N) then
7016 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7017 -- flag indicates ??? -- more comments needed here)
7019 if Assignment_OK (N) then
7022 Force_Evaluation (N);
7024 end Expand_N_Unchecked_Type_Conversion;
7026 ----------------------------
7027 -- Expand_Record_Equality --
7028 ----------------------------
7030 -- For non-variant records, Equality is expanded when needed into:
7032 -- and then Lhs.Discr1 = Rhs.Discr1
7034 -- and then Lhs.Discrn = Rhs.Discrn
7035 -- and then Lhs.Cmp1 = Rhs.Cmp1
7037 -- and then Lhs.Cmpn = Rhs.Cmpn
7039 -- The expression is folded by the back-end for adjacent fields. This
7040 -- function is called for tagged record in only one occasion: for imple-
7041 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
7042 -- otherwise the primitive "=" is used directly.
7044 function Expand_Record_Equality
7049 Bodies : List_Id) return Node_Id
7051 Loc : constant Source_Ptr := Sloc (Nod);
7056 First_Time : Boolean := True;
7058 function Suitable_Element (C : Entity_Id) return Entity_Id;
7059 -- Return the first field to compare beginning with C, skipping the
7060 -- inherited components.
7062 ----------------------
7063 -- Suitable_Element --
7064 ----------------------
7066 function Suitable_Element (C : Entity_Id) return Entity_Id is
7071 elsif Ekind (C) /= E_Discriminant
7072 and then Ekind (C) /= E_Component
7074 return Suitable_Element (Next_Entity (C));
7076 elsif Is_Tagged_Type (Typ)
7077 and then C /= Original_Record_Component (C)
7079 return Suitable_Element (Next_Entity (C));
7081 elsif Chars (C) = Name_uController
7082 or else Chars (C) = Name_uTag
7084 return Suitable_Element (Next_Entity (C));
7089 end Suitable_Element;
7091 -- Start of processing for Expand_Record_Equality
7094 -- Generates the following code: (assuming that Typ has one Discr and
7095 -- component C2 is also a record)
7098 -- and then Lhs.Discr1 = Rhs.Discr1
7099 -- and then Lhs.C1 = Rhs.C1
7100 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
7102 -- and then Lhs.Cmpn = Rhs.Cmpn
7104 Result := New_Reference_To (Standard_True, Loc);
7105 C := Suitable_Element (First_Entity (Typ));
7107 while Present (C) loop
7114 First_Time := False;
7118 New_Lhs := New_Copy_Tree (Lhs);
7119 New_Rhs := New_Copy_Tree (Rhs);
7124 Left_Opnd => Result,
7126 Expand_Composite_Equality (Nod, Etype (C),
7128 Make_Selected_Component (Loc,
7130 Selector_Name => New_Reference_To (C, Loc)),
7132 Make_Selected_Component (Loc,
7134 Selector_Name => New_Reference_To (C, Loc)),
7138 C := Suitable_Element (Next_Entity (C));
7142 end Expand_Record_Equality;
7144 -------------------------------------
7145 -- Fixup_Universal_Fixed_Operation --
7146 -------------------------------------
7148 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
7149 Conv : constant Node_Id := Parent (N);
7152 -- We must have a type conversion immediately above us
7154 pragma Assert (Nkind (Conv) = N_Type_Conversion);
7156 -- Normally the type conversion gives our target type. The exception
7157 -- occurs in the case of the Round attribute, where the conversion
7158 -- will be to universal real, and our real type comes from the Round
7159 -- attribute (as well as an indication that we must round the result)
7161 if Nkind (Parent (Conv)) = N_Attribute_Reference
7162 and then Attribute_Name (Parent (Conv)) = Name_Round
7164 Set_Etype (N, Etype (Parent (Conv)));
7165 Set_Rounded_Result (N);
7167 -- Normal case where type comes from conversion above us
7170 Set_Etype (N, Etype (Conv));
7172 end Fixup_Universal_Fixed_Operation;
7174 ------------------------------
7175 -- Get_Allocator_Final_List --
7176 ------------------------------
7178 function Get_Allocator_Final_List
7181 PtrT : Entity_Id) return Entity_Id
7183 Loc : constant Source_Ptr := Sloc (N);
7185 Owner : Entity_Id := PtrT;
7186 -- The entity whose finalisation list must be used to attach the
7187 -- allocated object.
7190 if Ekind (PtrT) = E_Anonymous_Access_Type then
7191 if Nkind (Associated_Node_For_Itype (PtrT))
7192 in N_Subprogram_Specification
7194 -- If the context is an access parameter, we need to create
7195 -- a non-anonymous access type in order to have a usable
7196 -- final list, because there is otherwise no pool to which
7197 -- the allocated object can belong. We create both the type
7198 -- and the finalization chain here, because freezing an
7199 -- internal type does not create such a chain. The Final_Chain
7200 -- that is thus created is shared by the access parameter.
7202 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
7204 Make_Full_Type_Declaration (Loc,
7205 Defining_Identifier => Owner,
7207 Make_Access_To_Object_Definition (Loc,
7208 Subtype_Indication =>
7209 New_Occurrence_Of (T, Loc))));
7211 Build_Final_List (N, Owner);
7212 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
7215 -- Case of an access discriminant, or (Ada 2005) of
7216 -- an anonymous access component: find the final list
7217 -- associated with the scope of the type.
7219 Owner := Scope (PtrT);
7223 return Find_Final_List (Owner);
7224 end Get_Allocator_Final_List;
7226 ---------------------------------
7227 -- Has_Inferable_Discriminants --
7228 ---------------------------------
7230 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
7232 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
7233 -- Determines whether the left-most prefix of a selected component is a
7234 -- formal parameter in a subprogram. Assumes N is a selected component.
7236 --------------------------------
7237 -- Prefix_Is_Formal_Parameter --
7238 --------------------------------
7240 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
7241 Sel_Comp : Node_Id := N;
7244 -- Move to the left-most prefix by climbing up the tree
7246 while Present (Parent (Sel_Comp))
7247 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
7249 Sel_Comp := Parent (Sel_Comp);
7252 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
7253 end Prefix_Is_Formal_Parameter;
7255 -- Start of processing for Has_Inferable_Discriminants
7258 -- For identifiers and indexed components, it is sufficent to have a
7259 -- constrained Unchecked_Union nominal subtype.
7261 if Nkind (N) = N_Identifier
7263 Nkind (N) = N_Indexed_Component
7265 return Is_Unchecked_Union (Base_Type (Etype (N)))
7267 Is_Constrained (Etype (N));
7269 -- For selected components, the subtype of the selector must be a
7270 -- constrained Unchecked_Union. If the component is subject to a
7271 -- per-object constraint, then the enclosing object must have inferable
7274 elsif Nkind (N) = N_Selected_Component then
7275 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
7277 -- A small hack. If we have a per-object constrained selected
7278 -- component of a formal parameter, return True since we do not
7279 -- know the actual parameter association yet.
7281 if Prefix_Is_Formal_Parameter (N) then
7285 -- Otherwise, check the enclosing object and the selector
7287 return Has_Inferable_Discriminants (Prefix (N))
7289 Has_Inferable_Discriminants (Selector_Name (N));
7292 -- The call to Has_Inferable_Discriminants will determine whether
7293 -- the selector has a constrained Unchecked_Union nominal type.
7295 return Has_Inferable_Discriminants (Selector_Name (N));
7297 -- A qualified expression has inferable discriminants if its subtype
7298 -- mark is a constrained Unchecked_Union subtype.
7300 elsif Nkind (N) = N_Qualified_Expression then
7301 return Is_Unchecked_Union (Subtype_Mark (N))
7303 Is_Constrained (Subtype_Mark (N));
7308 end Has_Inferable_Discriminants;
7310 -------------------------------
7311 -- Insert_Dereference_Action --
7312 -------------------------------
7314 procedure Insert_Dereference_Action (N : Node_Id) is
7315 Loc : constant Source_Ptr := Sloc (N);
7316 Typ : constant Entity_Id := Etype (N);
7317 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
7318 Pnod : constant Node_Id := Parent (N);
7320 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
7321 -- Return true if type of P is derived from Checked_Pool;
7323 -----------------------------
7324 -- Is_Checked_Storage_Pool --
7325 -----------------------------
7327 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
7336 while T /= Etype (T) loop
7337 if Is_RTE (T, RE_Checked_Pool) then
7345 end Is_Checked_Storage_Pool;
7347 -- Start of processing for Insert_Dereference_Action
7350 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
7352 if not (Is_Checked_Storage_Pool (Pool)
7353 and then Comes_From_Source (Original_Node (Pnod)))
7359 Make_Procedure_Call_Statement (Loc,
7360 Name => New_Reference_To (
7361 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
7363 Parameter_Associations => New_List (
7367 New_Reference_To (Pool, Loc),
7369 -- Storage_Address. We use the attribute Pool_Address,
7370 -- which uses the pointer itself to find the address of
7371 -- the object, and which handles unconstrained arrays
7372 -- properly by computing the address of the template.
7373 -- i.e. the correct address of the corresponding allocation.
7375 Make_Attribute_Reference (Loc,
7376 Prefix => Duplicate_Subexpr_Move_Checks (N),
7377 Attribute_Name => Name_Pool_Address),
7379 -- Size_In_Storage_Elements
7381 Make_Op_Divide (Loc,
7383 Make_Attribute_Reference (Loc,
7385 Make_Explicit_Dereference (Loc,
7386 Duplicate_Subexpr_Move_Checks (N)),
7387 Attribute_Name => Name_Size),
7389 Make_Integer_Literal (Loc, System_Storage_Unit)),
7393 Make_Attribute_Reference (Loc,
7395 Make_Explicit_Dereference (Loc,
7396 Duplicate_Subexpr_Move_Checks (N)),
7397 Attribute_Name => Name_Alignment))));
7400 when RE_Not_Available =>
7402 end Insert_Dereference_Action;
7404 ------------------------------
7405 -- Make_Array_Comparison_Op --
7406 ------------------------------
7408 -- This is a hand-coded expansion of the following generic function:
7411 -- type elem is (<>);
7412 -- type index is (<>);
7413 -- type a is array (index range <>) of elem;
7415 -- function Gnnn (X : a; Y: a) return boolean is
7416 -- J : index := Y'first;
7419 -- if X'length = 0 then
7422 -- elsif Y'length = 0 then
7426 -- for I in X'range loop
7427 -- if X (I) = Y (J) then
7428 -- if J = Y'last then
7431 -- J := index'succ (J);
7435 -- return X (I) > Y (J);
7439 -- return X'length > Y'length;
7443 -- Note that since we are essentially doing this expansion by hand, we
7444 -- do not need to generate an actual or formal generic part, just the
7445 -- instantiated function itself.
7447 function Make_Array_Comparison_Op
7449 Nod : Node_Id) return Node_Id
7451 Loc : constant Source_Ptr := Sloc (Nod);
7453 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
7454 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
7455 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
7456 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7458 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
7460 Loop_Statement : Node_Id;
7461 Loop_Body : Node_Id;
7464 Final_Expr : Node_Id;
7465 Func_Body : Node_Id;
7466 Func_Name : Entity_Id;
7472 -- if J = Y'last then
7475 -- J := index'succ (J);
7479 Make_Implicit_If_Statement (Nod,
7482 Left_Opnd => New_Reference_To (J, Loc),
7484 Make_Attribute_Reference (Loc,
7485 Prefix => New_Reference_To (Y, Loc),
7486 Attribute_Name => Name_Last)),
7488 Then_Statements => New_List (
7489 Make_Exit_Statement (Loc)),
7493 Make_Assignment_Statement (Loc,
7494 Name => New_Reference_To (J, Loc),
7496 Make_Attribute_Reference (Loc,
7497 Prefix => New_Reference_To (Index, Loc),
7498 Attribute_Name => Name_Succ,
7499 Expressions => New_List (New_Reference_To (J, Loc))))));
7501 -- if X (I) = Y (J) then
7504 -- return X (I) > Y (J);
7508 Make_Implicit_If_Statement (Nod,
7512 Make_Indexed_Component (Loc,
7513 Prefix => New_Reference_To (X, Loc),
7514 Expressions => New_List (New_Reference_To (I, Loc))),
7517 Make_Indexed_Component (Loc,
7518 Prefix => New_Reference_To (Y, Loc),
7519 Expressions => New_List (New_Reference_To (J, Loc)))),
7521 Then_Statements => New_List (Inner_If),
7523 Else_Statements => New_List (
7524 Make_Return_Statement (Loc,
7528 Make_Indexed_Component (Loc,
7529 Prefix => New_Reference_To (X, Loc),
7530 Expressions => New_List (New_Reference_To (I, Loc))),
7533 Make_Indexed_Component (Loc,
7534 Prefix => New_Reference_To (Y, Loc),
7535 Expressions => New_List (
7536 New_Reference_To (J, Loc)))))));
7538 -- for I in X'range loop
7543 Make_Implicit_Loop_Statement (Nod,
7544 Identifier => Empty,
7547 Make_Iteration_Scheme (Loc,
7548 Loop_Parameter_Specification =>
7549 Make_Loop_Parameter_Specification (Loc,
7550 Defining_Identifier => I,
7551 Discrete_Subtype_Definition =>
7552 Make_Attribute_Reference (Loc,
7553 Prefix => New_Reference_To (X, Loc),
7554 Attribute_Name => Name_Range))),
7556 Statements => New_List (Loop_Body));
7558 -- if X'length = 0 then
7560 -- elsif Y'length = 0 then
7563 -- for ... loop ... end loop;
7564 -- return X'length > Y'length;
7568 Make_Attribute_Reference (Loc,
7569 Prefix => New_Reference_To (X, Loc),
7570 Attribute_Name => Name_Length);
7573 Make_Attribute_Reference (Loc,
7574 Prefix => New_Reference_To (Y, Loc),
7575 Attribute_Name => Name_Length);
7579 Left_Opnd => Length1,
7580 Right_Opnd => Length2);
7583 Make_Implicit_If_Statement (Nod,
7587 Make_Attribute_Reference (Loc,
7588 Prefix => New_Reference_To (X, Loc),
7589 Attribute_Name => Name_Length),
7591 Make_Integer_Literal (Loc, 0)),
7595 Make_Return_Statement (Loc,
7596 Expression => New_Reference_To (Standard_False, Loc))),
7598 Elsif_Parts => New_List (
7599 Make_Elsif_Part (Loc,
7603 Make_Attribute_Reference (Loc,
7604 Prefix => New_Reference_To (Y, Loc),
7605 Attribute_Name => Name_Length),
7607 Make_Integer_Literal (Loc, 0)),
7611 Make_Return_Statement (Loc,
7612 Expression => New_Reference_To (Standard_True, Loc))))),
7614 Else_Statements => New_List (
7616 Make_Return_Statement (Loc,
7617 Expression => Final_Expr)));
7621 Formals := New_List (
7622 Make_Parameter_Specification (Loc,
7623 Defining_Identifier => X,
7624 Parameter_Type => New_Reference_To (Typ, Loc)),
7626 Make_Parameter_Specification (Loc,
7627 Defining_Identifier => Y,
7628 Parameter_Type => New_Reference_To (Typ, Loc)));
7630 -- function Gnnn (...) return boolean is
7631 -- J : index := Y'first;
7636 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
7639 Make_Subprogram_Body (Loc,
7641 Make_Function_Specification (Loc,
7642 Defining_Unit_Name => Func_Name,
7643 Parameter_Specifications => Formals,
7644 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)),
7646 Declarations => New_List (
7647 Make_Object_Declaration (Loc,
7648 Defining_Identifier => J,
7649 Object_Definition => New_Reference_To (Index, Loc),
7651 Make_Attribute_Reference (Loc,
7652 Prefix => New_Reference_To (Y, Loc),
7653 Attribute_Name => Name_First))),
7655 Handled_Statement_Sequence =>
7656 Make_Handled_Sequence_Of_Statements (Loc,
7657 Statements => New_List (If_Stat)));
7661 end Make_Array_Comparison_Op;
7663 ---------------------------
7664 -- Make_Boolean_Array_Op --
7665 ---------------------------
7667 -- For logical operations on boolean arrays, expand in line the
7668 -- following, replacing 'and' with 'or' or 'xor' where needed:
7670 -- function Annn (A : typ; B: typ) return typ is
7673 -- for J in A'range loop
7674 -- C (J) := A (J) op B (J);
7679 -- Here typ is the boolean array type
7681 function Make_Boolean_Array_Op
7683 N : Node_Id) return Node_Id
7685 Loc : constant Source_Ptr := Sloc (N);
7687 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7688 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
7689 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
7690 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7698 Func_Name : Entity_Id;
7699 Func_Body : Node_Id;
7700 Loop_Statement : Node_Id;
7704 Make_Indexed_Component (Loc,
7705 Prefix => New_Reference_To (A, Loc),
7706 Expressions => New_List (New_Reference_To (J, Loc)));
7709 Make_Indexed_Component (Loc,
7710 Prefix => New_Reference_To (B, Loc),
7711 Expressions => New_List (New_Reference_To (J, Loc)));
7714 Make_Indexed_Component (Loc,
7715 Prefix => New_Reference_To (C, Loc),
7716 Expressions => New_List (New_Reference_To (J, Loc)));
7718 if Nkind (N) = N_Op_And then
7724 elsif Nkind (N) = N_Op_Or then
7738 Make_Implicit_Loop_Statement (N,
7739 Identifier => Empty,
7742 Make_Iteration_Scheme (Loc,
7743 Loop_Parameter_Specification =>
7744 Make_Loop_Parameter_Specification (Loc,
7745 Defining_Identifier => J,
7746 Discrete_Subtype_Definition =>
7747 Make_Attribute_Reference (Loc,
7748 Prefix => New_Reference_To (A, Loc),
7749 Attribute_Name => Name_Range))),
7751 Statements => New_List (
7752 Make_Assignment_Statement (Loc,
7754 Expression => Op)));
7756 Formals := New_List (
7757 Make_Parameter_Specification (Loc,
7758 Defining_Identifier => A,
7759 Parameter_Type => New_Reference_To (Typ, Loc)),
7761 Make_Parameter_Specification (Loc,
7762 Defining_Identifier => B,
7763 Parameter_Type => New_Reference_To (Typ, Loc)));
7766 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
7767 Set_Is_Inlined (Func_Name);
7770 Make_Subprogram_Body (Loc,
7772 Make_Function_Specification (Loc,
7773 Defining_Unit_Name => Func_Name,
7774 Parameter_Specifications => Formals,
7775 Subtype_Mark => New_Reference_To (Typ, Loc)),
7777 Declarations => New_List (
7778 Make_Object_Declaration (Loc,
7779 Defining_Identifier => C,
7780 Object_Definition => New_Reference_To (Typ, Loc))),
7782 Handled_Statement_Sequence =>
7783 Make_Handled_Sequence_Of_Statements (Loc,
7784 Statements => New_List (
7786 Make_Return_Statement (Loc,
7787 Expression => New_Reference_To (C, Loc)))));
7790 end Make_Boolean_Array_Op;
7792 ------------------------
7793 -- Rewrite_Comparison --
7794 ------------------------
7796 procedure Rewrite_Comparison (N : Node_Id) is
7797 Typ : constant Entity_Id := Etype (N);
7798 Op1 : constant Node_Id := Left_Opnd (N);
7799 Op2 : constant Node_Id := Right_Opnd (N);
7801 Res : constant Compare_Result := Compile_Time_Compare (Op1, Op2);
7802 -- Res indicates if compare outcome can be determined at compile time
7804 True_Result : Boolean;
7805 False_Result : Boolean;
7808 case N_Op_Compare (Nkind (N)) is
7810 True_Result := Res = EQ;
7811 False_Result := Res = LT or else Res = GT or else Res = NE;
7814 True_Result := Res in Compare_GE;
7815 False_Result := Res = LT;
7818 True_Result := Res = GT;
7819 False_Result := Res in Compare_LE;
7822 True_Result := Res = LT;
7823 False_Result := Res in Compare_GE;
7826 True_Result := Res in Compare_LE;
7827 False_Result := Res = GT;
7830 True_Result := Res = NE;
7831 False_Result := Res = LT or else Res = GT or else Res = EQ;
7836 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))));
7837 Analyze_And_Resolve (N, Typ);
7838 Warn_On_Known_Condition (N);
7840 elsif False_Result then
7842 Convert_To (Typ, New_Occurrence_Of (Standard_False, Sloc (N))));
7843 Analyze_And_Resolve (N, Typ);
7844 Warn_On_Known_Condition (N);
7846 end Rewrite_Comparison;
7848 ----------------------------
7849 -- Safe_In_Place_Array_Op --
7850 ----------------------------
7852 function Safe_In_Place_Array_Op
7855 Op2 : Node_Id) return Boolean
7859 function Is_Safe_Operand (Op : Node_Id) return Boolean;
7860 -- Operand is safe if it cannot overlap part of the target of the
7861 -- operation. If the operand and the target are identical, the operand
7862 -- is safe. The operand can be empty in the case of negation.
7864 function Is_Unaliased (N : Node_Id) return Boolean;
7865 -- Check that N is a stand-alone entity
7871 function Is_Unaliased (N : Node_Id) return Boolean is
7875 and then No (Address_Clause (Entity (N)))
7876 and then No (Renamed_Object (Entity (N)));
7879 ---------------------
7880 -- Is_Safe_Operand --
7881 ---------------------
7883 function Is_Safe_Operand (Op : Node_Id) return Boolean is
7888 elsif Is_Entity_Name (Op) then
7889 return Is_Unaliased (Op);
7891 elsif Nkind (Op) = N_Indexed_Component
7892 or else Nkind (Op) = N_Selected_Component
7894 return Is_Unaliased (Prefix (Op));
7896 elsif Nkind (Op) = N_Slice then
7898 Is_Unaliased (Prefix (Op))
7899 and then Entity (Prefix (Op)) /= Target;
7901 elsif Nkind (Op) = N_Op_Not then
7902 return Is_Safe_Operand (Right_Opnd (Op));
7907 end Is_Safe_Operand;
7909 -- Start of processing for Is_Safe_In_Place_Array_Op
7912 -- We skip this processing if the component size is not the
7913 -- same as a system storage unit (since at least for NOT
7914 -- this would cause problems).
7916 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
7919 -- Cannot do in place stuff on Java_VM since cannot pass addresses
7924 -- Cannot do in place stuff if non-standard Boolean representation
7926 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
7929 elsif not Is_Unaliased (Lhs) then
7932 Target := Entity (Lhs);
7935 Is_Safe_Operand (Op1)
7936 and then Is_Safe_Operand (Op2);
7938 end Safe_In_Place_Array_Op;
7940 -----------------------
7941 -- Tagged_Membership --
7942 -----------------------
7944 -- There are two different cases to consider depending on whether
7945 -- the right operand is a class-wide type or not. If not we just
7946 -- compare the actual tag of the left expr to the target type tag:
7948 -- Left_Expr.Tag = Right_Type'Tag;
7950 -- If it is a class-wide type we use the RT function CW_Membership which
7951 -- is usually implemented by looking in the ancestor tables contained in
7952 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
7954 function Tagged_Membership (N : Node_Id) return Node_Id is
7955 Left : constant Node_Id := Left_Opnd (N);
7956 Right : constant Node_Id := Right_Opnd (N);
7957 Loc : constant Source_Ptr := Sloc (N);
7959 Left_Type : Entity_Id;
7960 Right_Type : Entity_Id;
7964 Left_Type := Etype (Left);
7965 Right_Type := Etype (Right);
7967 if Is_Class_Wide_Type (Left_Type) then
7968 Left_Type := Root_Type (Left_Type);
7972 Make_Selected_Component (Loc,
7973 Prefix => Relocate_Node (Left),
7974 Selector_Name => New_Reference_To (Tag_Component (Left_Type), Loc));
7976 if Is_Class_Wide_Type (Right_Type) then
7978 Make_DT_Access_Action (Left_Type,
7979 Action => CW_Membership,
7983 Access_Disp_Table (Root_Type (Right_Type)), Loc)));
7987 Left_Opnd => Obj_Tag,
7989 New_Reference_To (Access_Disp_Table (Right_Type), Loc));
7992 end Tagged_Membership;
7994 ------------------------------
7995 -- Unary_Op_Validity_Checks --
7996 ------------------------------
7998 procedure Unary_Op_Validity_Checks (N : Node_Id) is
8000 if Validity_Checks_On and Validity_Check_Operands then
8001 Ensure_Valid (Right_Opnd (N));
8003 end Unary_Op_Validity_Checks;