1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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_Fixd; use Exp_Fixd;
37 with Exp_Pakd; use Exp_Pakd;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Exp_VFpt; use Exp_VFpt;
41 with Hostparm; use Hostparm;
42 with Inline; use Inline;
43 with Nlists; use Nlists;
44 with Nmake; use Nmake;
46 with Rtsfind; use Rtsfind;
48 with Sem_Cat; use Sem_Cat;
49 with Sem_Ch3; use Sem_Ch3;
50 with Sem_Ch13; use Sem_Ch13;
51 with Sem_Eval; use Sem_Eval;
52 with Sem_Res; use Sem_Res;
53 with Sem_Type; use Sem_Type;
54 with Sem_Util; use Sem_Util;
55 with Sem_Warn; use Sem_Warn;
56 with Sinfo; use Sinfo;
57 with Snames; use Snames;
58 with Stand; use Stand;
59 with Targparm; use Targparm;
60 with Tbuild; use Tbuild;
61 with Ttypes; use Ttypes;
62 with Uintp; use Uintp;
63 with Urealp; use Urealp;
64 with Validsw; use Validsw;
66 package body Exp_Ch4 is
68 -----------------------
69 -- Local Subprograms --
70 -----------------------
72 procedure Binary_Op_Validity_Checks (N : Node_Id);
73 pragma Inline (Binary_Op_Validity_Checks);
74 -- Performs validity checks for a binary operator
76 procedure Build_Boolean_Array_Proc_Call
80 -- If an boolean array assignment can be done in place, build call to
81 -- corresponding library procedure.
83 procedure Expand_Allocator_Expression (N : Node_Id);
84 -- Subsidiary to Expand_N_Allocator, for the case when the expression
85 -- is a qualified expression or an aggregate.
87 procedure Expand_Array_Comparison (N : Node_Id);
88 -- This routine handles expansion of the comparison operators (N_Op_Lt,
89 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
90 -- code for these operators is similar, differing only in the details of
91 -- the actual comparison call that is made. Special processing (call a
94 function Expand_Array_Equality
99 Typ : Entity_Id) return Node_Id;
100 -- Expand an array equality into a call to a function implementing this
101 -- equality, and a call to it. Loc is the location for the generated
102 -- nodes. Lhs and Rhs are the array expressions to be compared.
103 -- Bodies is a list on which to attach bodies of local functions that
104 -- are created in the process. It is the responsibility of the
105 -- caller to insert those bodies at the right place. Nod provides
106 -- the Sloc value for the generated code. Normally the types used
107 -- for the generated equality routine are taken from Lhs and Rhs.
108 -- However, in some situations of generated code, the Etype fields
109 -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the
110 -- type to be used for the formal parameters.
112 procedure Expand_Boolean_Operator (N : Node_Id);
113 -- Common expansion processing for Boolean operators (And, Or, Xor)
114 -- for the case of array type arguments.
116 function Expand_Composite_Equality
121 Bodies : List_Id) return Node_Id;
122 -- Local recursive function used to expand equality for nested
123 -- composite types. Used by Expand_Record/Array_Equality, Bodies
124 -- is a list on which to attach bodies of local functions that are
125 -- created in the process. This is the responsability of the caller
126 -- to insert those bodies at the right place. Nod provides the Sloc
127 -- value for generated code. Lhs and Rhs are the left and right sides
128 -- for the comparison, and Typ is the type of the arrays to compare.
130 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id);
131 -- This routine handles expansion of concatenation operations, where
132 -- N is the N_Op_Concat node being expanded and Operands is the list
133 -- of operands (at least two are present). The caller has dealt with
134 -- converting any singleton operands into singleton aggregates.
136 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id);
137 -- Routine to expand concatenation of 2-5 operands (in the list Operands)
138 -- and replace node Cnode with the result of the contatenation. If there
139 -- are two operands, they can be string or character. If there are more
140 -- than two operands, then are always of type string (i.e. the caller has
141 -- already converted character operands to strings in this case).
143 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
144 -- N is either an N_Op_Divide or N_Op_Multiply node whose result is
145 -- universal fixed. We do not have such a type at runtime, so the
146 -- purpose of this routine is to find the real type by looking up
147 -- the tree. We also determine if the operation must be rounded.
149 function Get_Allocator_Final_List
152 PtrT : Entity_Id) return Entity_Id;
153 -- If the designated type is controlled, build final_list expression
154 -- for created object. If context is an access parameter, create a
155 -- local access type to have a usable finalization list.
157 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
158 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
159 -- discriminants if it has a constrained nominal type, unless the object
160 -- is a component of an enclosing Unchecked_Union object that is subject
161 -- to a per-object constraint and the enclosing object lacks inferable
164 -- An expression of an Unchecked_Union type has inferable discriminants
165 -- if it is either a name of an object with inferable discriminants or a
166 -- qualified expression whose subtype mark denotes a constrained subtype.
168 procedure Insert_Dereference_Action (N : Node_Id);
169 -- N is an expression whose type is an access. When the type of the
170 -- associated storage pool is derived from Checked_Pool, generate a
171 -- call to the 'Dereference' primitive operation.
173 function Make_Array_Comparison_Op
175 Nod : Node_Id) return Node_Id;
176 -- Comparisons between arrays are expanded in line. This function
177 -- produces the body of the implementation of (a > b), where a and b
178 -- are one-dimensional arrays of some discrete type. The original
179 -- node is then expanded into the appropriate call to this function.
180 -- Nod provides the Sloc value for the generated code.
182 function Make_Boolean_Array_Op
184 N : Node_Id) return Node_Id;
185 -- Boolean operations on boolean arrays are expanded in line. This
186 -- function produce the body for the node N, which is (a and b),
187 -- (a or b), or (a xor b). It is used only the normal case and not
188 -- the packed case. The type involved, Typ, is the Boolean array type,
189 -- and the logical operations in the body are simple boolean operations.
190 -- Note that Typ is always a constrained type (the caller has ensured
191 -- this by using Convert_To_Actual_Subtype if necessary).
193 procedure Rewrite_Comparison (N : Node_Id);
194 -- N is the node for a compile time comparison. If this outcome of this
195 -- comparison can be determined at compile time, then the node N can be
196 -- rewritten with True or False. If the outcome cannot be determined at
197 -- compile time, the call has no effect.
199 function Tagged_Membership (N : Node_Id) return Node_Id;
200 -- Construct the expression corresponding to the tagged membership test.
201 -- Deals with a second operand being (or not) a class-wide type.
203 function Safe_In_Place_Array_Op
206 Op2 : Node_Id) return Boolean;
207 -- In the context of an assignment, where the right-hand side is a
208 -- boolean operation on arrays, check whether operation can be performed
211 procedure Unary_Op_Validity_Checks (N : Node_Id);
212 pragma Inline (Unary_Op_Validity_Checks);
213 -- Performs validity checks for a unary operator
215 -------------------------------
216 -- Binary_Op_Validity_Checks --
217 -------------------------------
219 procedure Binary_Op_Validity_Checks (N : Node_Id) is
221 if Validity_Checks_On and Validity_Check_Operands then
222 Ensure_Valid (Left_Opnd (N));
223 Ensure_Valid (Right_Opnd (N));
225 end Binary_Op_Validity_Checks;
227 ------------------------------------
228 -- Build_Boolean_Array_Proc_Call --
229 ------------------------------------
231 procedure Build_Boolean_Array_Proc_Call
236 Loc : constant Source_Ptr := Sloc (N);
237 Kind : constant Node_Kind := Nkind (Expression (N));
238 Target : constant Node_Id :=
239 Make_Attribute_Reference (Loc,
241 Attribute_Name => Name_Address);
243 Arg1 : constant Node_Id := Op1;
244 Arg2 : Node_Id := Op2;
246 Proc_Name : Entity_Id;
249 if Kind = N_Op_Not then
250 if Nkind (Op1) in N_Binary_Op then
252 -- Use negated version of the binary operators
254 if Nkind (Op1) = N_Op_And then
255 Proc_Name := RTE (RE_Vector_Nand);
257 elsif Nkind (Op1) = N_Op_Or then
258 Proc_Name := RTE (RE_Vector_Nor);
260 else pragma Assert (Nkind (Op1) = N_Op_Xor);
261 Proc_Name := RTE (RE_Vector_Xor);
265 Make_Procedure_Call_Statement (Loc,
266 Name => New_Occurrence_Of (Proc_Name, Loc),
268 Parameter_Associations => New_List (
270 Make_Attribute_Reference (Loc,
271 Prefix => Left_Opnd (Op1),
272 Attribute_Name => Name_Address),
274 Make_Attribute_Reference (Loc,
275 Prefix => Right_Opnd (Op1),
276 Attribute_Name => Name_Address),
278 Make_Attribute_Reference (Loc,
279 Prefix => Left_Opnd (Op1),
280 Attribute_Name => Name_Length)));
283 Proc_Name := RTE (RE_Vector_Not);
286 Make_Procedure_Call_Statement (Loc,
287 Name => New_Occurrence_Of (Proc_Name, Loc),
288 Parameter_Associations => New_List (
291 Make_Attribute_Reference (Loc,
293 Attribute_Name => Name_Address),
295 Make_Attribute_Reference (Loc,
297 Attribute_Name => Name_Length)));
301 -- We use the following equivalences:
303 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
304 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
305 -- (not X) xor (not Y) = X xor Y
306 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
308 if Nkind (Op1) = N_Op_Not then
309 if Kind = N_Op_And then
310 Proc_Name := RTE (RE_Vector_Nor);
312 elsif Kind = N_Op_Or then
313 Proc_Name := RTE (RE_Vector_Nand);
316 Proc_Name := RTE (RE_Vector_Xor);
320 if Kind = N_Op_And then
321 Proc_Name := RTE (RE_Vector_And);
323 elsif Kind = N_Op_Or then
324 Proc_Name := RTE (RE_Vector_Or);
326 elsif Nkind (Op2) = N_Op_Not then
327 Proc_Name := RTE (RE_Vector_Nxor);
328 Arg2 := Right_Opnd (Op2);
331 Proc_Name := RTE (RE_Vector_Xor);
336 Make_Procedure_Call_Statement (Loc,
337 Name => New_Occurrence_Of (Proc_Name, Loc),
338 Parameter_Associations => New_List (
340 Make_Attribute_Reference (Loc,
342 Attribute_Name => Name_Address),
343 Make_Attribute_Reference (Loc,
345 Attribute_Name => Name_Address),
346 Make_Attribute_Reference (Loc,
348 Attribute_Name => Name_Length)));
351 Rewrite (N, Call_Node);
355 when RE_Not_Available =>
357 end Build_Boolean_Array_Proc_Call;
359 ---------------------------------
360 -- Expand_Allocator_Expression --
361 ---------------------------------
363 procedure Expand_Allocator_Expression (N : Node_Id) is
364 Loc : constant Source_Ptr := Sloc (N);
365 Exp : constant Node_Id := Expression (Expression (N));
366 Indic : constant Node_Id := Subtype_Mark (Expression (N));
367 PtrT : constant Entity_Id := Etype (N);
368 T : constant Entity_Id := Entity (Indic);
373 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
375 Tag_Assign : Node_Id;
379 if Is_Tagged_Type (T) or else Controlled_Type (T) then
381 -- Actions inserted before:
382 -- Temp : constant ptr_T := new T'(Expression);
383 -- <no CW> Temp._tag := T'tag;
384 -- <CTRL> Adjust (Finalizable (Temp.all));
385 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
387 -- We analyze by hand the new internal allocator to avoid
388 -- any recursion and inappropriate call to Initialize
390 if not Aggr_In_Place then
391 Remove_Side_Effects (Exp);
395 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
397 -- For a class wide allocation generate the following code:
399 -- type Equiv_Record is record ... end record;
400 -- implicit subtype CW is <Class_Wide_Subytpe>;
401 -- temp : PtrT := new CW'(CW!(expr));
403 if Is_Class_Wide_Type (T) then
404 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
406 Set_Expression (Expression (N),
407 Unchecked_Convert_To (Entity (Indic), Exp));
409 Analyze_And_Resolve (Expression (N), Entity (Indic));
412 if Aggr_In_Place then
414 Make_Object_Declaration (Loc,
415 Defining_Identifier => Temp,
416 Object_Definition => New_Reference_To (PtrT, Loc),
419 New_Reference_To (Etype (Exp), Loc)));
421 Set_Comes_From_Source
422 (Expression (Tmp_Node), Comes_From_Source (N));
424 Set_No_Initialization (Expression (Tmp_Node));
425 Insert_Action (N, Tmp_Node);
427 if Controlled_Type (T)
428 and then Ekind (PtrT) = E_Anonymous_Access_Type
430 -- Create local finalization list for access parameter
432 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
435 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
437 Node := Relocate_Node (N);
440 Make_Object_Declaration (Loc,
441 Defining_Identifier => Temp,
442 Constant_Present => True,
443 Object_Definition => New_Reference_To (PtrT, Loc),
444 Expression => Node));
447 -- Ada 2005 (AI-344):
448 -- For an allocator with a class-wide designated type, generate an
449 -- accessibility check to verify that the level of the type of the
450 -- created object is not deeper than the level of the access type.
451 -- If the type of the qualified expression is class-wide, then
452 -- always generate the check. Otherwise, only generate the check
453 -- if the level of the qualified expression type is statically deeper
454 -- than the access type. Although the static accessibility will
455 -- generally have been performed as a legality check, it won't have
456 -- been done in cases where the allocator appears in a generic body,
457 -- so the run-time check is needed in general. (Not yet doing the
458 -- optimization to suppress the check for the static level case.???)
460 if Ada_Version >= Ada_05
461 and then Is_Class_Wide_Type (Designated_Type (PtrT))
464 Make_Raise_Program_Error (Loc,
468 Make_Function_Call (Loc,
470 New_Reference_To (RTE (RE_Get_Access_Level), Loc),
471 Parameter_Associations =>
472 New_List (Make_Attribute_Reference (Loc,
474 New_Reference_To (Temp, Loc),
478 Make_Integer_Literal (Loc, Type_Access_Level (PtrT))),
479 Reason => PE_Accessibility_Check_Failed));
482 -- Suppress the tag assignment when Java_VM because JVM tags
483 -- are represented implicitly in objects.
485 if Is_Tagged_Type (T)
486 and then not Is_Class_Wide_Type (T)
490 Make_Assignment_Statement (Loc,
492 Make_Selected_Component (Loc,
493 Prefix => New_Reference_To (Temp, Loc),
495 New_Reference_To (First_Tag_Component (T), Loc)),
498 Unchecked_Convert_To (RTE (RE_Tag),
500 (Elists.Node (First_Elmt (Access_Disp_Table (T))),
503 -- The previous assignment has to be done in any case
505 Set_Assignment_OK (Name (Tag_Assign));
506 Insert_Action (N, Tag_Assign);
508 elsif Is_Private_Type (T)
509 and then Is_Tagged_Type (Underlying_Type (T))
513 Utyp : constant Entity_Id := Underlying_Type (T);
514 Ref : constant Node_Id :=
515 Unchecked_Convert_To (Utyp,
516 Make_Explicit_Dereference (Loc,
517 New_Reference_To (Temp, Loc)));
521 Make_Assignment_Statement (Loc,
523 Make_Selected_Component (Loc,
526 New_Reference_To (First_Tag_Component (Utyp), Loc)),
529 Unchecked_Convert_To (RTE (RE_Tag),
531 Elists.Node (First_Elmt (Access_Disp_Table (Utyp))),
534 Set_Assignment_OK (Name (Tag_Assign));
535 Insert_Action (N, Tag_Assign);
539 if Controlled_Type (Designated_Type (PtrT))
540 and then Controlled_Type (T)
544 Apool : constant Entity_Id :=
545 Associated_Storage_Pool (PtrT);
548 -- If it is an allocation on the secondary stack
549 -- (i.e. a value returned from a function), the object
550 -- is attached on the caller side as soon as the call
551 -- is completed (see Expand_Ctrl_Function_Call)
553 if Is_RTE (Apool, RE_SS_Pool) then
555 F : constant Entity_Id :=
556 Make_Defining_Identifier (Loc,
557 New_Internal_Name ('F'));
560 Make_Object_Declaration (Loc,
561 Defining_Identifier => F,
562 Object_Definition => New_Reference_To (RTE
563 (RE_Finalizable_Ptr), Loc)));
565 Flist := New_Reference_To (F, Loc);
566 Attach := Make_Integer_Literal (Loc, 1);
569 -- Normal case, not a secondary stack allocation
572 if Controlled_Type (T)
573 and then Ekind (PtrT) = E_Anonymous_Access_Type
575 -- Create local finalization list for access parameter
578 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
580 Flist := Find_Final_List (PtrT);
583 Attach := Make_Integer_Literal (Loc, 2);
586 if not Aggr_In_Place then
591 -- An unchecked conversion is needed in the
592 -- classwide case because the designated type
593 -- can be an ancestor of the subtype mark of
596 Unchecked_Convert_To (T,
597 Make_Explicit_Dereference (Loc,
598 New_Reference_To (Temp, Loc))),
602 With_Attach => Attach));
607 Rewrite (N, New_Reference_To (Temp, Loc));
608 Analyze_And_Resolve (N, PtrT);
610 elsif Aggr_In_Place then
612 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
614 Make_Object_Declaration (Loc,
615 Defining_Identifier => Temp,
616 Object_Definition => New_Reference_To (PtrT, Loc),
617 Expression => Make_Allocator (Loc,
618 New_Reference_To (Etype (Exp), Loc)));
620 Set_Comes_From_Source
621 (Expression (Tmp_Node), Comes_From_Source (N));
623 Set_No_Initialization (Expression (Tmp_Node));
624 Insert_Action (N, Tmp_Node);
625 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
626 Rewrite (N, New_Reference_To (Temp, Loc));
627 Analyze_And_Resolve (N, PtrT);
629 elsif Is_Access_Type (Designated_Type (PtrT))
630 and then Nkind (Exp) = N_Allocator
631 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
633 -- Apply constraint to designated subtype indication
635 Apply_Constraint_Check (Expression (Exp),
636 Designated_Type (Designated_Type (PtrT)),
639 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
641 -- Propagate constraint_error to enclosing allocator
643 Rewrite (Exp, New_Copy (Expression (Exp)));
646 -- First check against the type of the qualified expression
648 -- NOTE: The commented call should be correct, but for
649 -- some reason causes the compiler to bomb (sigsegv) on
650 -- ACVC test c34007g, so for now we just perform the old
651 -- (incorrect) test against the designated subtype with
652 -- no sliding in the else part of the if statement below.
655 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
657 -- A check is also needed in cases where the designated
658 -- subtype is constrained and differs from the subtype
659 -- given in the qualified expression. Note that the check
660 -- on the qualified expression does not allow sliding,
661 -- but this check does (a relaxation from Ada 83).
663 if Is_Constrained (Designated_Type (PtrT))
664 and then not Subtypes_Statically_Match
665 (T, Designated_Type (PtrT))
667 Apply_Constraint_Check
668 (Exp, Designated_Type (PtrT), No_Sliding => False);
670 -- The nonsliding check should really be performed
671 -- (unconditionally) against the subtype of the
672 -- qualified expression, but that causes a problem
673 -- with c34007g (see above), so for now we retain this.
676 Apply_Constraint_Check
677 (Exp, Designated_Type (PtrT), No_Sliding => True);
682 when RE_Not_Available =>
684 end Expand_Allocator_Expression;
686 -----------------------------
687 -- Expand_Array_Comparison --
688 -----------------------------
690 -- Expansion is only required in the case of array types. For the
691 -- unpacked case, an appropriate runtime routine is called. For
692 -- packed cases, and also in some other cases where a runtime
693 -- routine cannot be called, the form of the expansion is:
695 -- [body for greater_nn; boolean_expression]
697 -- The body is built by Make_Array_Comparison_Op, and the form of the
698 -- Boolean expression depends on the operator involved.
700 procedure Expand_Array_Comparison (N : Node_Id) is
701 Loc : constant Source_Ptr := Sloc (N);
702 Op1 : Node_Id := Left_Opnd (N);
703 Op2 : Node_Id := Right_Opnd (N);
704 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
705 Ctyp : constant Entity_Id := Component_Type (Typ1);
709 Func_Name : Entity_Id;
713 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
714 -- True for byte addressable target
716 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
717 -- Returns True if the length of the given operand is known to be
718 -- less than 4. Returns False if this length is known to be four
719 -- or greater or is not known at compile time.
721 ------------------------
722 -- Length_Less_Than_4 --
723 ------------------------
725 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
726 Otyp : constant Entity_Id := Etype (Opnd);
729 if Ekind (Otyp) = E_String_Literal_Subtype then
730 return String_Literal_Length (Otyp) < 4;
734 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
735 Lo : constant Node_Id := Type_Low_Bound (Ityp);
736 Hi : constant Node_Id := Type_High_Bound (Ityp);
741 if Compile_Time_Known_Value (Lo) then
742 Lov := Expr_Value (Lo);
747 if Compile_Time_Known_Value (Hi) then
748 Hiv := Expr_Value (Hi);
753 return Hiv < Lov + 3;
756 end Length_Less_Than_4;
758 -- Start of processing for Expand_Array_Comparison
761 -- Deal first with unpacked case, where we can call a runtime routine
762 -- except that we avoid this for targets for which are not addressable
763 -- by bytes, and for the JVM, since the JVM does not support direct
764 -- addressing of array components.
766 if not Is_Bit_Packed_Array (Typ1)
767 and then Byte_Addressable
770 -- The call we generate is:
772 -- Compare_Array_xn[_Unaligned]
773 -- (left'address, right'address, left'length, right'length) <op> 0
775 -- x = U for unsigned, S for signed
776 -- n = 8,16,32,64 for component size
777 -- Add _Unaligned if length < 4 and component size is 8.
778 -- <op> is the standard comparison operator
780 if Component_Size (Typ1) = 8 then
781 if Length_Less_Than_4 (Op1)
783 Length_Less_Than_4 (Op2)
785 if Is_Unsigned_Type (Ctyp) then
786 Comp := RE_Compare_Array_U8_Unaligned;
788 Comp := RE_Compare_Array_S8_Unaligned;
792 if Is_Unsigned_Type (Ctyp) then
793 Comp := RE_Compare_Array_U8;
795 Comp := RE_Compare_Array_S8;
799 elsif Component_Size (Typ1) = 16 then
800 if Is_Unsigned_Type (Ctyp) then
801 Comp := RE_Compare_Array_U16;
803 Comp := RE_Compare_Array_S16;
806 elsif Component_Size (Typ1) = 32 then
807 if Is_Unsigned_Type (Ctyp) then
808 Comp := RE_Compare_Array_U32;
810 Comp := RE_Compare_Array_S32;
813 else pragma Assert (Component_Size (Typ1) = 64);
814 if Is_Unsigned_Type (Ctyp) then
815 Comp := RE_Compare_Array_U64;
817 Comp := RE_Compare_Array_S64;
821 Remove_Side_Effects (Op1, Name_Req => True);
822 Remove_Side_Effects (Op2, Name_Req => True);
825 Make_Function_Call (Sloc (Op1),
826 Name => New_Occurrence_Of (RTE (Comp), Loc),
828 Parameter_Associations => New_List (
829 Make_Attribute_Reference (Loc,
830 Prefix => Relocate_Node (Op1),
831 Attribute_Name => Name_Address),
833 Make_Attribute_Reference (Loc,
834 Prefix => Relocate_Node (Op2),
835 Attribute_Name => Name_Address),
837 Make_Attribute_Reference (Loc,
838 Prefix => Relocate_Node (Op1),
839 Attribute_Name => Name_Length),
841 Make_Attribute_Reference (Loc,
842 Prefix => Relocate_Node (Op2),
843 Attribute_Name => Name_Length))));
846 Make_Integer_Literal (Sloc (Op2),
849 Analyze_And_Resolve (Op1, Standard_Integer);
850 Analyze_And_Resolve (Op2, Standard_Integer);
854 -- Cases where we cannot make runtime call
856 -- For (a <= b) we convert to not (a > b)
858 if Chars (N) = Name_Op_Le then
864 Right_Opnd => Op2)));
865 Analyze_And_Resolve (N, Standard_Boolean);
868 -- For < the Boolean expression is
869 -- greater__nn (op2, op1)
871 elsif Chars (N) = Name_Op_Lt then
872 Func_Body := Make_Array_Comparison_Op (Typ1, N);
876 Op1 := Right_Opnd (N);
877 Op2 := Left_Opnd (N);
879 -- For (a >= b) we convert to not (a < b)
881 elsif Chars (N) = Name_Op_Ge then
887 Right_Opnd => Op2)));
888 Analyze_And_Resolve (N, Standard_Boolean);
891 -- For > the Boolean expression is
892 -- greater__nn (op1, op2)
895 pragma Assert (Chars (N) = Name_Op_Gt);
896 Func_Body := Make_Array_Comparison_Op (Typ1, N);
899 Func_Name := Defining_Unit_Name (Specification (Func_Body));
901 Make_Function_Call (Loc,
902 Name => New_Reference_To (Func_Name, Loc),
903 Parameter_Associations => New_List (Op1, Op2));
905 Insert_Action (N, Func_Body);
907 Analyze_And_Resolve (N, Standard_Boolean);
910 when RE_Not_Available =>
912 end Expand_Array_Comparison;
914 ---------------------------
915 -- Expand_Array_Equality --
916 ---------------------------
918 -- Expand an equality function for multi-dimensional arrays. Here is
919 -- an example of such a function for Nb_Dimension = 2
921 -- function Enn (A : atyp; B : btyp) return boolean is
923 -- if (A'length (1) = 0 or else A'length (2) = 0)
925 -- (B'length (1) = 0 or else B'length (2) = 0)
927 -- return True; -- RM 4.5.2(22)
930 -- if A'length (1) /= B'length (1)
932 -- A'length (2) /= B'length (2)
934 -- return False; -- RM 4.5.2(23)
938 -- A1 : Index_T1 := A'first (1);
939 -- B1 : Index_T1 := B'first (1);
943 -- A2 : Index_T2 := A'first (2);
944 -- B2 : Index_T2 := B'first (2);
947 -- if A (A1, A2) /= B (B1, B2) then
951 -- exit when A2 = A'last (2);
952 -- A2 := Index_T2'succ (A2);
953 -- B2 := Index_T2'succ (B2);
957 -- exit when A1 = A'last (1);
958 -- A1 := Index_T1'succ (A1);
959 -- B1 := Index_T1'succ (B1);
966 -- Note on the formal types used (atyp and btyp). If either of the
967 -- arrays is of a private type, we use the underlying type, and
968 -- do an unchecked conversion of the actual. If either of the arrays
969 -- has a bound depending on a discriminant, then we use the base type
970 -- since otherwise we have an escaped discriminant in the function.
972 -- If both arrays are constrained and have the same bounds, we can
973 -- generate a loop with an explicit iteration scheme using a 'Range
974 -- attribute over the first array.
976 function Expand_Array_Equality
981 Typ : Entity_Id) return Node_Id
983 Loc : constant Source_Ptr := Sloc (Nod);
984 Decls : constant List_Id := New_List;
985 Index_List1 : constant List_Id := New_List;
986 Index_List2 : constant List_Id := New_List;
990 Func_Name : Entity_Id;
993 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
994 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
998 -- The parameter types to be used for the formals
1003 Num : Int) return Node_Id;
1004 -- This builds the attribute reference Arr'Nam (Expr)
1006 function Component_Equality (Typ : Entity_Id) return Node_Id;
1007 -- Create one statement to compare corresponding components,
1008 -- designated by a full set of indices.
1010 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1011 -- Given one of the arguments, computes the appropriate type to
1012 -- be used for that argument in the corresponding function formal
1014 function Handle_One_Dimension
1016 Index : Node_Id) return Node_Id;
1017 -- This procedure returns the following code
1020 -- Bn : Index_T := B'First (N);
1024 -- exit when An = A'Last (N);
1025 -- An := Index_T'Succ (An)
1026 -- Bn := Index_T'Succ (Bn)
1030 -- If both indices are constrained and identical, the procedure
1031 -- returns a simpler loop:
1033 -- for An in A'Range (N) loop
1037 -- N is the dimension for which we are generating a loop. Index is the
1038 -- N'th index node, whose Etype is Index_Type_n in the above code.
1039 -- The xxx statement is either the loop or declare for the next
1040 -- dimension or if this is the last dimension the comparison
1041 -- of corresponding components of the arrays.
1043 -- The actual way the code works is to return the comparison
1044 -- of corresponding components for the N+1 call. That's neater!
1046 function Test_Empty_Arrays return Node_Id;
1047 -- This function constructs the test for both arrays being empty
1048 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1050 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1052 function Test_Lengths_Correspond return Node_Id;
1053 -- This function constructs the test for arrays having different
1054 -- lengths in at least one index position, in which case resull
1056 -- A'length (1) /= B'length (1)
1058 -- A'length (2) /= B'length (2)
1069 Num : Int) return Node_Id
1073 Make_Attribute_Reference (Loc,
1074 Attribute_Name => Nam,
1075 Prefix => New_Reference_To (Arr, Loc),
1076 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1079 ------------------------
1080 -- Component_Equality --
1081 ------------------------
1083 function Component_Equality (Typ : Entity_Id) return Node_Id is
1088 -- if a(i1...) /= b(j1...) then return false; end if;
1091 Make_Indexed_Component (Loc,
1092 Prefix => Make_Identifier (Loc, Chars (A)),
1093 Expressions => Index_List1);
1096 Make_Indexed_Component (Loc,
1097 Prefix => Make_Identifier (Loc, Chars (B)),
1098 Expressions => Index_List2);
1100 Test := Expand_Composite_Equality
1101 (Nod, Component_Type (Typ), L, R, Decls);
1103 -- If some (sub)component is an unchecked_union, the whole operation
1104 -- will raise program error.
1106 if Nkind (Test) = N_Raise_Program_Error then
1108 -- This node is going to be inserted at a location where a
1109 -- statement is expected: clear its Etype so analysis will
1110 -- set it to the expected Standard_Void_Type.
1112 Set_Etype (Test, Empty);
1117 Make_Implicit_If_Statement (Nod,
1118 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1119 Then_Statements => New_List (
1120 Make_Return_Statement (Loc,
1121 Expression => New_Occurrence_Of (Standard_False, Loc))));
1123 end Component_Equality;
1129 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1140 T := Underlying_Type (T);
1142 X := First_Index (T);
1143 while Present (X) loop
1144 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1146 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1159 --------------------------
1160 -- Handle_One_Dimension --
1161 ---------------------------
1163 function Handle_One_Dimension
1165 Index : Node_Id) return Node_Id
1167 Need_Separate_Indexes : constant Boolean :=
1169 or else not Is_Constrained (Ltyp);
1170 -- If the index types are identical, and we are working with
1171 -- constrained types, then we can use the same index for both of
1174 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1175 Chars => New_Internal_Name ('A'));
1178 Index_T : Entity_Id;
1183 if N > Number_Dimensions (Ltyp) then
1184 return Component_Equality (Ltyp);
1187 -- Case where we generate a loop
1189 Index_T := Base_Type (Etype (Index));
1191 if Need_Separate_Indexes then
1193 Make_Defining_Identifier (Loc,
1194 Chars => New_Internal_Name ('B'));
1199 Append (New_Reference_To (An, Loc), Index_List1);
1200 Append (New_Reference_To (Bn, Loc), Index_List2);
1202 Stm_List := New_List (
1203 Handle_One_Dimension (N + 1, Next_Index (Index)));
1205 if Need_Separate_Indexes then
1207 -- Generate guard for loop, followed by increments of indices
1209 Append_To (Stm_List,
1210 Make_Exit_Statement (Loc,
1213 Left_Opnd => New_Reference_To (An, Loc),
1214 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1216 Append_To (Stm_List,
1217 Make_Assignment_Statement (Loc,
1218 Name => New_Reference_To (An, Loc),
1220 Make_Attribute_Reference (Loc,
1221 Prefix => New_Reference_To (Index_T, Loc),
1222 Attribute_Name => Name_Succ,
1223 Expressions => New_List (New_Reference_To (An, Loc)))));
1225 Append_To (Stm_List,
1226 Make_Assignment_Statement (Loc,
1227 Name => New_Reference_To (Bn, Loc),
1229 Make_Attribute_Reference (Loc,
1230 Prefix => New_Reference_To (Index_T, Loc),
1231 Attribute_Name => Name_Succ,
1232 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1235 -- If separate indexes, we need a declare block for An and Bn, and a
1236 -- loop without an iteration scheme.
1238 if Need_Separate_Indexes then
1240 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1243 Make_Block_Statement (Loc,
1244 Declarations => New_List (
1245 Make_Object_Declaration (Loc,
1246 Defining_Identifier => An,
1247 Object_Definition => New_Reference_To (Index_T, Loc),
1248 Expression => Arr_Attr (A, Name_First, N)),
1250 Make_Object_Declaration (Loc,
1251 Defining_Identifier => Bn,
1252 Object_Definition => New_Reference_To (Index_T, Loc),
1253 Expression => Arr_Attr (B, Name_First, N))),
1255 Handled_Statement_Sequence =>
1256 Make_Handled_Sequence_Of_Statements (Loc,
1257 Statements => New_List (Loop_Stm)));
1259 -- If no separate indexes, return loop statement with explicit
1260 -- iteration scheme on its own
1264 Make_Implicit_Loop_Statement (Nod,
1265 Statements => Stm_List,
1267 Make_Iteration_Scheme (Loc,
1268 Loop_Parameter_Specification =>
1269 Make_Loop_Parameter_Specification (Loc,
1270 Defining_Identifier => An,
1271 Discrete_Subtype_Definition =>
1272 Arr_Attr (A, Name_Range, N))));
1275 end Handle_One_Dimension;
1277 -----------------------
1278 -- Test_Empty_Arrays --
1279 -----------------------
1281 function Test_Empty_Arrays return Node_Id is
1291 for J in 1 .. Number_Dimensions (Ltyp) loop
1294 Left_Opnd => Arr_Attr (A, Name_Length, J),
1295 Right_Opnd => Make_Integer_Literal (Loc, 0));
1299 Left_Opnd => Arr_Attr (B, Name_Length, J),
1300 Right_Opnd => Make_Integer_Literal (Loc, 0));
1309 Left_Opnd => Relocate_Node (Alist),
1310 Right_Opnd => Atest);
1314 Left_Opnd => Relocate_Node (Blist),
1315 Right_Opnd => Btest);
1322 Right_Opnd => Blist);
1323 end Test_Empty_Arrays;
1325 -----------------------------
1326 -- Test_Lengths_Correspond --
1327 -----------------------------
1329 function Test_Lengths_Correspond return Node_Id is
1335 for J in 1 .. Number_Dimensions (Ltyp) loop
1338 Left_Opnd => Arr_Attr (A, Name_Length, J),
1339 Right_Opnd => Arr_Attr (B, Name_Length, J));
1346 Left_Opnd => Relocate_Node (Result),
1347 Right_Opnd => Rtest);
1352 end Test_Lengths_Correspond;
1354 -- Start of processing for Expand_Array_Equality
1357 Ltyp := Get_Arg_Type (Lhs);
1358 Rtyp := Get_Arg_Type (Rhs);
1360 -- For now, if the argument types are not the same, go to the
1361 -- base type, since the code assumes that the formals have the
1362 -- same type. This is fixable in future ???
1364 if Ltyp /= Rtyp then
1365 Ltyp := Base_Type (Ltyp);
1366 Rtyp := Base_Type (Rtyp);
1367 pragma Assert (Ltyp = Rtyp);
1370 -- Build list of formals for function
1372 Formals := New_List (
1373 Make_Parameter_Specification (Loc,
1374 Defining_Identifier => A,
1375 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1377 Make_Parameter_Specification (Loc,
1378 Defining_Identifier => B,
1379 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1381 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1383 -- Build statement sequence for function
1386 Make_Subprogram_Body (Loc,
1388 Make_Function_Specification (Loc,
1389 Defining_Unit_Name => Func_Name,
1390 Parameter_Specifications => Formals,
1391 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)),
1393 Declarations => Decls,
1395 Handled_Statement_Sequence =>
1396 Make_Handled_Sequence_Of_Statements (Loc,
1397 Statements => New_List (
1399 Make_Implicit_If_Statement (Nod,
1400 Condition => Test_Empty_Arrays,
1401 Then_Statements => New_List (
1402 Make_Return_Statement (Loc,
1404 New_Occurrence_Of (Standard_True, Loc)))),
1406 Make_Implicit_If_Statement (Nod,
1407 Condition => Test_Lengths_Correspond,
1408 Then_Statements => New_List (
1409 Make_Return_Statement (Loc,
1411 New_Occurrence_Of (Standard_False, Loc)))),
1413 Handle_One_Dimension (1, First_Index (Ltyp)),
1415 Make_Return_Statement (Loc,
1416 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1418 Set_Has_Completion (Func_Name, True);
1419 Set_Is_Inlined (Func_Name);
1421 -- If the array type is distinct from the type of the arguments,
1422 -- it is the full view of a private type. Apply an unchecked
1423 -- conversion to insure that analysis of the call succeeds.
1433 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1435 L := OK_Convert_To (Ltyp, Lhs);
1439 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1441 R := OK_Convert_To (Rtyp, Rhs);
1444 Actuals := New_List (L, R);
1447 Append_To (Bodies, Func_Body);
1450 Make_Function_Call (Loc,
1451 Name => New_Reference_To (Func_Name, Loc),
1452 Parameter_Associations => Actuals);
1453 end Expand_Array_Equality;
1455 -----------------------------
1456 -- Expand_Boolean_Operator --
1457 -----------------------------
1459 -- Note that we first get the actual subtypes of the operands,
1460 -- since we always want to deal with types that have bounds.
1462 procedure Expand_Boolean_Operator (N : Node_Id) is
1463 Typ : constant Entity_Id := Etype (N);
1466 -- Special case of bit packed array where both operands are known
1467 -- to be properly aligned. In this case we use an efficient run time
1468 -- routine to carry out the operation (see System.Bit_Ops).
1470 if Is_Bit_Packed_Array (Typ)
1471 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1472 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1474 Expand_Packed_Boolean_Operator (N);
1478 -- For the normal non-packed case, the general expansion is to build
1479 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1480 -- and then inserting it into the tree. The original operator node is
1481 -- then rewritten as a call to this function. We also use this in the
1482 -- packed case if either operand is a possibly unaligned object.
1485 Loc : constant Source_Ptr := Sloc (N);
1486 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1487 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1488 Func_Body : Node_Id;
1489 Func_Name : Entity_Id;
1492 Convert_To_Actual_Subtype (L);
1493 Convert_To_Actual_Subtype (R);
1494 Ensure_Defined (Etype (L), N);
1495 Ensure_Defined (Etype (R), N);
1496 Apply_Length_Check (R, Etype (L));
1498 if Nkind (Parent (N)) = N_Assignment_Statement
1499 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1501 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1503 elsif Nkind (Parent (N)) = N_Op_Not
1504 and then Nkind (N) = N_Op_And
1506 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1511 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1512 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1513 Insert_Action (N, Func_Body);
1515 -- Now rewrite the expression with a call
1518 Make_Function_Call (Loc,
1519 Name => New_Reference_To (Func_Name, Loc),
1520 Parameter_Associations =>
1523 Make_Type_Conversion
1524 (Loc, New_Reference_To (Etype (L), Loc), R))));
1526 Analyze_And_Resolve (N, Typ);
1529 end Expand_Boolean_Operator;
1531 -------------------------------
1532 -- Expand_Composite_Equality --
1533 -------------------------------
1535 -- This function is only called for comparing internal fields of composite
1536 -- types when these fields are themselves composites. This is a special
1537 -- case because it is not possible to respect normal Ada visibility rules.
1539 function Expand_Composite_Equality
1544 Bodies : List_Id) return Node_Id
1546 Loc : constant Source_Ptr := Sloc (Nod);
1547 Full_Type : Entity_Id;
1552 if Is_Private_Type (Typ) then
1553 Full_Type := Underlying_Type (Typ);
1558 -- Defense against malformed private types with no completion
1559 -- the error will be diagnosed later by check_completion
1561 if No (Full_Type) then
1562 return New_Reference_To (Standard_False, Loc);
1565 Full_Type := Base_Type (Full_Type);
1567 if Is_Array_Type (Full_Type) then
1569 -- If the operand is an elementary type other than a floating-point
1570 -- type, then we can simply use the built-in block bitwise equality,
1571 -- since the predefined equality operators always apply and bitwise
1572 -- equality is fine for all these cases.
1574 if Is_Elementary_Type (Component_Type (Full_Type))
1575 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1577 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1579 -- For composite component types, and floating-point types, use
1580 -- the expansion. This deals with tagged component types (where
1581 -- we use the applicable equality routine) and floating-point,
1582 -- (where we need to worry about negative zeroes), and also the
1583 -- case of any composite type recursively containing such fields.
1586 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1589 elsif Is_Tagged_Type (Full_Type) then
1591 -- Call the primitive operation "=" of this type
1593 if Is_Class_Wide_Type (Full_Type) then
1594 Full_Type := Root_Type (Full_Type);
1597 -- If this is derived from an untagged private type completed
1598 -- with a tagged type, it does not have a full view, so we
1599 -- use the primitive operations of the private type.
1600 -- This check should no longer be necessary when these
1601 -- types receive their full views ???
1603 if Is_Private_Type (Typ)
1604 and then not Is_Tagged_Type (Typ)
1605 and then not Is_Controlled (Typ)
1606 and then Is_Derived_Type (Typ)
1607 and then No (Full_View (Typ))
1609 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1611 Prim := First_Elmt (Primitive_Operations (Full_Type));
1615 Eq_Op := Node (Prim);
1616 exit when Chars (Eq_Op) = Name_Op_Eq
1617 and then Etype (First_Formal (Eq_Op)) =
1618 Etype (Next_Formal (First_Formal (Eq_Op)))
1619 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1621 pragma Assert (Present (Prim));
1624 Eq_Op := Node (Prim);
1627 Make_Function_Call (Loc,
1628 Name => New_Reference_To (Eq_Op, Loc),
1629 Parameter_Associations =>
1631 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1632 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1634 elsif Is_Record_Type (Full_Type) then
1635 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1637 if Present (Eq_Op) then
1638 if Etype (First_Formal (Eq_Op)) /= Full_Type then
1640 -- Inherited equality from parent type. Convert the actuals
1641 -- to match signature of operation.
1644 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
1648 Make_Function_Call (Loc,
1649 Name => New_Reference_To (Eq_Op, Loc),
1650 Parameter_Associations =>
1651 New_List (OK_Convert_To (T, Lhs),
1652 OK_Convert_To (T, Rhs)));
1656 -- Comparison between Unchecked_Union components
1658 if Is_Unchecked_Union (Full_Type) then
1660 Lhs_Type : Node_Id := Full_Type;
1661 Rhs_Type : Node_Id := Full_Type;
1662 Lhs_Discr_Val : Node_Id;
1663 Rhs_Discr_Val : Node_Id;
1668 if Nkind (Lhs) = N_Selected_Component then
1669 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
1674 if Nkind (Rhs) = N_Selected_Component then
1675 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
1678 -- Lhs of the composite equality
1680 if Is_Constrained (Lhs_Type) then
1682 -- Since the enclosing record can never be an
1683 -- Unchecked_Union (this code is executed for records
1684 -- that do not have variants), we may reference its
1687 if Nkind (Lhs) = N_Selected_Component
1688 and then Has_Per_Object_Constraint (
1689 Entity (Selector_Name (Lhs)))
1692 Make_Selected_Component (Loc,
1693 Prefix => Prefix (Lhs),
1696 Get_Discriminant_Value (
1697 First_Discriminant (Lhs_Type),
1699 Stored_Constraint (Lhs_Type))));
1702 Lhs_Discr_Val := New_Copy (
1703 Get_Discriminant_Value (
1704 First_Discriminant (Lhs_Type),
1706 Stored_Constraint (Lhs_Type)));
1710 -- It is not possible to infer the discriminant since
1711 -- the subtype is not constrained.
1714 Make_Raise_Program_Error (Loc,
1715 Reason => PE_Unchecked_Union_Restriction);
1718 -- Rhs of the composite equality
1720 if Is_Constrained (Rhs_Type) then
1721 if Nkind (Rhs) = N_Selected_Component
1722 and then Has_Per_Object_Constraint (
1723 Entity (Selector_Name (Rhs)))
1726 Make_Selected_Component (Loc,
1727 Prefix => Prefix (Rhs),
1730 Get_Discriminant_Value (
1731 First_Discriminant (Rhs_Type),
1733 Stored_Constraint (Rhs_Type))));
1736 Rhs_Discr_Val := New_Copy (
1737 Get_Discriminant_Value (
1738 First_Discriminant (Rhs_Type),
1740 Stored_Constraint (Rhs_Type)));
1745 Make_Raise_Program_Error (Loc,
1746 Reason => PE_Unchecked_Union_Restriction);
1749 -- Call the TSS equality function with the inferred
1750 -- discriminant values.
1753 Make_Function_Call (Loc,
1754 Name => New_Reference_To (Eq_Op, Loc),
1755 Parameter_Associations => New_List (
1763 -- Shouldn't this be an else, we can't fall through
1764 -- the above IF, right???
1767 Make_Function_Call (Loc,
1768 Name => New_Reference_To (Eq_Op, Loc),
1769 Parameter_Associations => New_List (Lhs, Rhs));
1773 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
1777 -- It can be a simple record or the full view of a scalar private
1779 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1781 end Expand_Composite_Equality;
1783 ------------------------------
1784 -- Expand_Concatenate_Other --
1785 ------------------------------
1787 -- Let n be the number of array operands to be concatenated, Base_Typ
1788 -- their base type, Ind_Typ their index type, and Arr_Typ the original
1789 -- array type to which the concatenantion operator applies, then the
1790 -- following subprogram is constructed:
1792 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
1795 -- if S1'Length /= 0 then
1796 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
1797 -- XXX = Arr_Typ'First otherwise
1798 -- elsif S2'Length /= 0 then
1799 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
1800 -- YYY = Arr_Typ'First otherwise
1802 -- elsif Sn-1'Length /= 0 then
1803 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
1804 -- ZZZ = Arr_Typ'First otherwise
1812 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1813 -- + Ind_Typ'Pos (L));
1814 -- R : Base_Typ (L .. H);
1816 -- if S1'Length /= 0 then
1820 -- L := Ind_Typ'Succ (L);
1821 -- exit when P = S1'Last;
1822 -- P := Ind_Typ'Succ (P);
1826 -- if S2'Length /= 0 then
1827 -- L := Ind_Typ'Succ (L);
1830 -- L := Ind_Typ'Succ (L);
1831 -- exit when P = S2'Last;
1832 -- P := Ind_Typ'Succ (P);
1838 -- if Sn'Length /= 0 then
1842 -- L := Ind_Typ'Succ (L);
1843 -- exit when P = Sn'Last;
1844 -- P := Ind_Typ'Succ (P);
1852 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id) is
1853 Loc : constant Source_Ptr := Sloc (Cnode);
1854 Nb_Opnds : constant Nat := List_Length (Opnds);
1856 Arr_Typ : constant Entity_Id := Etype (Entity (Cnode));
1857 Base_Typ : constant Entity_Id := Base_Type (Etype (Cnode));
1858 Ind_Typ : constant Entity_Id := Etype (First_Index (Base_Typ));
1861 Func_Spec : Node_Id;
1862 Param_Specs : List_Id;
1864 Func_Body : Node_Id;
1865 Func_Decls : List_Id;
1866 Func_Stmts : List_Id;
1871 Elsif_List : List_Id;
1873 Declare_Block : Node_Id;
1874 Declare_Decls : List_Id;
1875 Declare_Stmts : List_Id;
1887 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id;
1888 -- Builds the sequence of statement:
1892 -- L := Ind_Typ'Succ (L);
1893 -- exit when P = Si'Last;
1894 -- P := Ind_Typ'Succ (P);
1897 -- where i is the input parameter I given.
1898 -- If the flag Last is true, the exit statement is emitted before
1899 -- incrementing the lower bound, to prevent the creation out of
1902 function Init_L (I : Nat) return Node_Id;
1903 -- Builds the statement:
1904 -- L := Arr_Typ'First; If Arr_Typ is constrained
1905 -- L := Si'First; otherwise (where I is the input param given)
1907 function H return Node_Id;
1908 -- Builds reference to identifier H
1910 function Ind_Val (E : Node_Id) return Node_Id;
1911 -- Builds expression Ind_Typ'Val (E);
1913 function L return Node_Id;
1914 -- Builds reference to identifier L
1916 function L_Pos return Node_Id;
1917 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
1918 -- expression to avoid universal_integer computations whenever possible,
1919 -- in the expression for the upper bound H.
1921 function L_Succ return Node_Id;
1922 -- Builds expression Ind_Typ'Succ (L)
1924 function One return Node_Id;
1925 -- Builds integer literal one
1927 function P return Node_Id;
1928 -- Builds reference to identifier P
1930 function P_Succ return Node_Id;
1931 -- Builds expression Ind_Typ'Succ (P)
1933 function R return Node_Id;
1934 -- Builds reference to identifier R
1936 function S (I : Nat) return Node_Id;
1937 -- Builds reference to identifier Si, where I is the value given
1939 function S_First (I : Nat) return Node_Id;
1940 -- Builds expression Si'First, where I is the value given
1942 function S_Last (I : Nat) return Node_Id;
1943 -- Builds expression Si'Last, where I is the value given
1945 function S_Length (I : Nat) return Node_Id;
1946 -- Builds expression Si'Length, where I is the value given
1948 function S_Length_Test (I : Nat) return Node_Id;
1949 -- Builds expression Si'Length /= 0, where I is the value given
1955 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is
1956 Stmts : constant List_Id := New_List;
1958 Loop_Stmt : Node_Id;
1960 Exit_Stmt : Node_Id;
1965 -- First construct the initializations
1967 P_Start := Make_Assignment_Statement (Loc,
1969 Expression => S_First (I));
1970 Append_To (Stmts, P_Start);
1972 -- Then build the loop
1974 R_Copy := Make_Assignment_Statement (Loc,
1975 Name => Make_Indexed_Component (Loc,
1977 Expressions => New_List (L)),
1978 Expression => Make_Indexed_Component (Loc,
1980 Expressions => New_List (P)));
1982 L_Inc := Make_Assignment_Statement (Loc,
1984 Expression => L_Succ);
1986 Exit_Stmt := Make_Exit_Statement (Loc,
1987 Condition => Make_Op_Eq (Loc, P, S_Last (I)));
1989 P_Inc := Make_Assignment_Statement (Loc,
1991 Expression => P_Succ);
1995 Make_Implicit_Loop_Statement (Cnode,
1996 Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc));
1999 Make_Implicit_Loop_Statement (Cnode,
2000 Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc));
2003 Append_To (Stmts, Loop_Stmt);
2012 function H return Node_Id is
2014 return Make_Identifier (Loc, Name_uH);
2021 function Ind_Val (E : Node_Id) return Node_Id is
2024 Make_Attribute_Reference (Loc,
2025 Prefix => New_Reference_To (Ind_Typ, Loc),
2026 Attribute_Name => Name_Val,
2027 Expressions => New_List (E));
2034 function Init_L (I : Nat) return Node_Id is
2038 if Is_Constrained (Arr_Typ) then
2039 E := Make_Attribute_Reference (Loc,
2040 Prefix => New_Reference_To (Arr_Typ, Loc),
2041 Attribute_Name => Name_First);
2047 return Make_Assignment_Statement (Loc, Name => L, Expression => E);
2054 function L return Node_Id is
2056 return Make_Identifier (Loc, Name_uL);
2063 function L_Pos return Node_Id is
2064 Target_Type : Entity_Id;
2067 -- If the index type is an enumeration type, the computation
2068 -- can be done in standard integer. Otherwise, choose a large
2069 -- enough integer type.
2071 if Is_Enumeration_Type (Ind_Typ)
2072 or else Root_Type (Ind_Typ) = Standard_Integer
2073 or else Root_Type (Ind_Typ) = Standard_Short_Integer
2074 or else Root_Type (Ind_Typ) = Standard_Short_Short_Integer
2076 Target_Type := Standard_Integer;
2078 Target_Type := Root_Type (Ind_Typ);
2082 Make_Qualified_Expression (Loc,
2083 Subtype_Mark => New_Reference_To (Target_Type, Loc),
2085 Make_Attribute_Reference (Loc,
2086 Prefix => New_Reference_To (Ind_Typ, Loc),
2087 Attribute_Name => Name_Pos,
2088 Expressions => New_List (L)));
2095 function L_Succ return Node_Id is
2098 Make_Attribute_Reference (Loc,
2099 Prefix => New_Reference_To (Ind_Typ, Loc),
2100 Attribute_Name => Name_Succ,
2101 Expressions => New_List (L));
2108 function One return Node_Id is
2110 return Make_Integer_Literal (Loc, 1);
2117 function P return Node_Id is
2119 return Make_Identifier (Loc, Name_uP);
2126 function P_Succ return Node_Id is
2129 Make_Attribute_Reference (Loc,
2130 Prefix => New_Reference_To (Ind_Typ, Loc),
2131 Attribute_Name => Name_Succ,
2132 Expressions => New_List (P));
2139 function R return Node_Id is
2141 return Make_Identifier (Loc, Name_uR);
2148 function S (I : Nat) return Node_Id is
2150 return Make_Identifier (Loc, New_External_Name ('S', I));
2157 function S_First (I : Nat) return Node_Id is
2159 return Make_Attribute_Reference (Loc,
2161 Attribute_Name => Name_First);
2168 function S_Last (I : Nat) return Node_Id is
2170 return Make_Attribute_Reference (Loc,
2172 Attribute_Name => Name_Last);
2179 function S_Length (I : Nat) return Node_Id is
2181 return Make_Attribute_Reference (Loc,
2183 Attribute_Name => Name_Length);
2190 function S_Length_Test (I : Nat) return Node_Id is
2194 Left_Opnd => S_Length (I),
2195 Right_Opnd => Make_Integer_Literal (Loc, 0));
2198 -- Start of processing for Expand_Concatenate_Other
2201 -- Construct the parameter specs and the overall function spec
2203 Param_Specs := New_List;
2204 for I in 1 .. Nb_Opnds loop
2207 Make_Parameter_Specification (Loc,
2208 Defining_Identifier =>
2209 Make_Defining_Identifier (Loc, New_External_Name ('S', I)),
2210 Parameter_Type => New_Reference_To (Base_Typ, Loc)));
2213 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2215 Make_Function_Specification (Loc,
2216 Defining_Unit_Name => Func_Id,
2217 Parameter_Specifications => Param_Specs,
2218 Subtype_Mark => New_Reference_To (Base_Typ, Loc));
2220 -- Construct L's object declaration
2223 Make_Object_Declaration (Loc,
2224 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uL),
2225 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2227 Func_Decls := New_List (L_Decl);
2229 -- Construct the if-then-elsif statements
2231 Elsif_List := New_List;
2232 for I in 2 .. Nb_Opnds - 1 loop
2233 Append_To (Elsif_List, Make_Elsif_Part (Loc,
2234 Condition => S_Length_Test (I),
2235 Then_Statements => New_List (Init_L (I))));
2239 Make_Implicit_If_Statement (Cnode,
2240 Condition => S_Length_Test (1),
2241 Then_Statements => New_List (Init_L (1)),
2242 Elsif_Parts => Elsif_List,
2243 Else_Statements => New_List (Make_Return_Statement (Loc,
2244 Expression => S (Nb_Opnds))));
2246 -- Construct the declaration for H
2249 Make_Object_Declaration (Loc,
2250 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2251 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2253 H_Init := Make_Op_Subtract (Loc, S_Length (1), One);
2254 for I in 2 .. Nb_Opnds loop
2255 H_Init := Make_Op_Add (Loc, H_Init, S_Length (I));
2257 H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos));
2260 Make_Object_Declaration (Loc,
2261 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uH),
2262 Object_Definition => New_Reference_To (Ind_Typ, Loc),
2263 Expression => H_Init);
2265 -- Construct the declaration for R
2267 R_Range := Make_Range (Loc, Low_Bound => L, High_Bound => H);
2269 Make_Index_Or_Discriminant_Constraint (Loc,
2270 Constraints => New_List (R_Range));
2273 Make_Object_Declaration (Loc,
2274 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uR),
2275 Object_Definition =>
2276 Make_Subtype_Indication (Loc,
2277 Subtype_Mark => New_Reference_To (Base_Typ, Loc),
2278 Constraint => R_Constr));
2280 -- Construct the declarations for the declare block
2282 Declare_Decls := New_List (P_Decl, H_Decl, R_Decl);
2284 -- Construct list of statements for the declare block
2286 Declare_Stmts := New_List;
2287 for I in 1 .. Nb_Opnds loop
2288 Append_To (Declare_Stmts,
2289 Make_Implicit_If_Statement (Cnode,
2290 Condition => S_Length_Test (I),
2291 Then_Statements => Copy_Into_R_S (I, I = Nb_Opnds)));
2294 Append_To (Declare_Stmts, Make_Return_Statement (Loc, Expression => R));
2296 -- Construct the declare block
2298 Declare_Block := Make_Block_Statement (Loc,
2299 Declarations => Declare_Decls,
2300 Handled_Statement_Sequence =>
2301 Make_Handled_Sequence_Of_Statements (Loc, Declare_Stmts));
2303 -- Construct the list of function statements
2305 Func_Stmts := New_List (If_Stmt, Declare_Block);
2307 -- Construct the function body
2310 Make_Subprogram_Body (Loc,
2311 Specification => Func_Spec,
2312 Declarations => Func_Decls,
2313 Handled_Statement_Sequence =>
2314 Make_Handled_Sequence_Of_Statements (Loc, Func_Stmts));
2316 -- Insert the newly generated function in the code. This is analyzed
2317 -- with all checks off, since we have completed all the checks.
2319 -- Note that this does *not* fix the array concatenation bug when the
2320 -- low bound is Integer'first sibce that bug comes from the pointer
2321 -- dereferencing an unconstrained array. An there we need a constraint
2322 -- check to make sure the length of the concatenated array is ok. ???
2324 Insert_Action (Cnode, Func_Body, Suppress => All_Checks);
2326 -- Construct list of arguments for the function call
2329 Operand := First (Opnds);
2330 for I in 1 .. Nb_Opnds loop
2331 Append_To (Params, Relocate_Node (Operand));
2335 -- Insert the function call
2339 Make_Function_Call (Loc, New_Reference_To (Func_Id, Loc), Params));
2341 Analyze_And_Resolve (Cnode, Base_Typ);
2342 Set_Is_Inlined (Func_Id);
2343 end Expand_Concatenate_Other;
2345 -------------------------------
2346 -- Expand_Concatenate_String --
2347 -------------------------------
2349 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id) is
2350 Loc : constant Source_Ptr := Sloc (Cnode);
2351 Opnd1 : constant Node_Id := First (Opnds);
2352 Opnd2 : constant Node_Id := Next (Opnd1);
2353 Typ1 : constant Entity_Id := Base_Type (Etype (Opnd1));
2354 Typ2 : constant Entity_Id := Base_Type (Etype (Opnd2));
2357 -- RE_Id value for function to be called
2360 -- In all cases, we build a call to a routine giving the list of
2361 -- arguments as the parameter list to the routine.
2363 case List_Length (Opnds) is
2365 if Typ1 = Standard_Character then
2366 if Typ2 = Standard_Character then
2367 R := RE_Str_Concat_CC;
2370 pragma Assert (Typ2 = Standard_String);
2371 R := RE_Str_Concat_CS;
2374 elsif Typ1 = Standard_String then
2375 if Typ2 = Standard_Character then
2376 R := RE_Str_Concat_SC;
2379 pragma Assert (Typ2 = Standard_String);
2383 -- If we have anything other than Standard_Character or
2384 -- Standard_String, then we must have had a serious error
2385 -- earlier, so we just abandon the attempt at expansion.
2388 pragma Assert (Serious_Errors_Detected > 0);
2393 R := RE_Str_Concat_3;
2396 R := RE_Str_Concat_4;
2399 R := RE_Str_Concat_5;
2403 raise Program_Error;
2406 -- Now generate the appropriate call
2409 Make_Function_Call (Sloc (Cnode),
2410 Name => New_Occurrence_Of (RTE (R), Loc),
2411 Parameter_Associations => Opnds));
2413 Analyze_And_Resolve (Cnode, Standard_String);
2416 when RE_Not_Available =>
2418 end Expand_Concatenate_String;
2420 ------------------------
2421 -- Expand_N_Allocator --
2422 ------------------------
2424 procedure Expand_N_Allocator (N : Node_Id) is
2425 PtrT : constant Entity_Id := Etype (N);
2426 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2428 Loc : constant Source_Ptr := Sloc (N);
2433 -- RM E.2.3(22). We enforce that the expected type of an allocator
2434 -- shall not be a remote access-to-class-wide-limited-private type
2436 -- Why is this being done at expansion time, seems clearly wrong ???
2438 Validate_Remote_Access_To_Class_Wide_Type (N);
2440 -- Set the Storage Pool
2442 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
2444 if Present (Storage_Pool (N)) then
2445 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
2447 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
2450 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
2451 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
2454 Set_Procedure_To_Call (N,
2455 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
2459 -- Under certain circumstances we can replace an allocator by an
2460 -- access to statically allocated storage. The conditions, as noted
2461 -- in AARM 3.10 (10c) are as follows:
2463 -- Size and initial value is known at compile time
2464 -- Access type is access-to-constant
2466 -- The allocator is not part of a constraint on a record component,
2467 -- because in that case the inserted actions are delayed until the
2468 -- record declaration is fully analyzed, which is too late for the
2469 -- analysis of the rewritten allocator.
2471 if Is_Access_Constant (PtrT)
2472 and then Nkind (Expression (N)) = N_Qualified_Expression
2473 and then Compile_Time_Known_Value (Expression (Expression (N)))
2474 and then Size_Known_At_Compile_Time (Etype (Expression
2476 and then not Is_Record_Type (Current_Scope)
2478 -- Here we can do the optimization. For the allocator
2482 -- We insert an object declaration
2484 -- Tnn : aliased x := y;
2486 -- and replace the allocator by Tnn'Unrestricted_Access.
2487 -- Tnn is marked as requiring static allocation.
2490 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2492 Desig := Subtype_Mark (Expression (N));
2494 -- If context is constrained, use constrained subtype directly,
2495 -- so that the constant is not labelled as having a nomimally
2496 -- unconstrained subtype.
2498 if Entity (Desig) = Base_Type (Dtyp) then
2499 Desig := New_Occurrence_Of (Dtyp, Loc);
2503 Make_Object_Declaration (Loc,
2504 Defining_Identifier => Temp,
2505 Aliased_Present => True,
2506 Constant_Present => Is_Access_Constant (PtrT),
2507 Object_Definition => Desig,
2508 Expression => Expression (Expression (N))));
2511 Make_Attribute_Reference (Loc,
2512 Prefix => New_Occurrence_Of (Temp, Loc),
2513 Attribute_Name => Name_Unrestricted_Access));
2515 Analyze_And_Resolve (N, PtrT);
2517 -- We set the variable as statically allocated, since we don't
2518 -- want it going on the stack of the current procedure!
2520 Set_Is_Statically_Allocated (Temp);
2524 -- Handle case of qualified expression (other than optimization above)
2526 if Nkind (Expression (N)) = N_Qualified_Expression then
2527 Expand_Allocator_Expression (N);
2529 -- If the allocator is for a type which requires initialization, and
2530 -- there is no initial value (i.e. operand is a subtype indication
2531 -- rather than a qualifed expression), then we must generate a call
2532 -- to the initialization routine. This is done using an expression
2535 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
2537 -- Here ptr_T is the pointer type for the allocator, and T is the
2538 -- subtype of the allocator. A special case arises if the designated
2539 -- type of the access type is a task or contains tasks. In this case
2540 -- the call to Init (Temp.all ...) is replaced by code that ensures
2541 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
2542 -- for details). In addition, if the type T is a task T, then the
2543 -- first argument to Init must be converted to the task record type.
2547 T : constant Entity_Id := Entity (Expression (N));
2555 Temp_Decl : Node_Id;
2556 Temp_Type : Entity_Id;
2557 Attach_Level : Uint;
2560 if No_Initialization (N) then
2563 -- Case of no initialization procedure present
2565 elsif not Has_Non_Null_Base_Init_Proc (T) then
2567 -- Case of simple initialization required
2569 if Needs_Simple_Initialization (T) then
2570 Rewrite (Expression (N),
2571 Make_Qualified_Expression (Loc,
2572 Subtype_Mark => New_Occurrence_Of (T, Loc),
2573 Expression => Get_Simple_Init_Val (T, Loc)));
2575 Analyze_And_Resolve (Expression (Expression (N)), T);
2576 Analyze_And_Resolve (Expression (N), T);
2577 Set_Paren_Count (Expression (Expression (N)), 1);
2578 Expand_N_Allocator (N);
2580 -- No initialization required
2586 -- Case of initialization procedure present, must be called
2589 Init := Base_Init_Proc (T);
2592 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
2594 -- Construct argument list for the initialization routine call
2595 -- The CPP constructor needs the address directly
2597 if Is_CPP_Class (T) then
2598 Arg1 := New_Reference_To (Temp, Loc);
2603 Make_Explicit_Dereference (Loc,
2604 Prefix => New_Reference_To (Temp, Loc));
2605 Set_Assignment_OK (Arg1);
2608 -- The initialization procedure expects a specific type.
2609 -- if the context is access to class wide, indicate that
2610 -- the object being allocated has the right specific type.
2612 if Is_Class_Wide_Type (Dtyp) then
2613 Arg1 := Unchecked_Convert_To (T, Arg1);
2617 -- If designated type is a concurrent type or if it is a
2618 -- private type whose definition is a concurrent type,
2619 -- the first argument in the Init routine has to be
2620 -- unchecked conversion to the corresponding record type.
2621 -- If the designated type is a derived type, we also
2622 -- convert the argument to its root type.
2624 if Is_Concurrent_Type (T) then
2626 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
2628 elsif Is_Private_Type (T)
2629 and then Present (Full_View (T))
2630 and then Is_Concurrent_Type (Full_View (T))
2633 Unchecked_Convert_To
2634 (Corresponding_Record_Type (Full_View (T)), Arg1);
2636 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
2639 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
2642 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
2643 Set_Etype (Arg1, Ftyp);
2647 Args := New_List (Arg1);
2649 -- For the task case, pass the Master_Id of the access type
2650 -- as the value of the _Master parameter, and _Chain as the
2651 -- value of the _Chain parameter (_Chain will be defined as
2652 -- part of the generated code for the allocator).
2654 if Has_Task (T) then
2655 if No (Master_Id (Base_Type (PtrT))) then
2657 -- The designated type was an incomplete type, and
2658 -- the access type did not get expanded. Salvage
2661 Expand_N_Full_Type_Declaration
2662 (Parent (Base_Type (PtrT)));
2665 -- If the context of the allocator is a declaration or
2666 -- an assignment, we can generate a meaningful image for
2667 -- it, even though subsequent assignments might remove
2668 -- the connection between task and entity. We build this
2669 -- image when the left-hand side is a simple variable,
2670 -- a simple indexed assignment or a simple selected
2673 if Nkind (Parent (N)) = N_Assignment_Statement then
2675 Nam : constant Node_Id := Name (Parent (N));
2678 if Is_Entity_Name (Nam) then
2680 Build_Task_Image_Decls (
2683 (Entity (Nam), Sloc (Nam)), T);
2685 elsif (Nkind (Nam) = N_Indexed_Component
2686 or else Nkind (Nam) = N_Selected_Component)
2687 and then Is_Entity_Name (Prefix (Nam))
2690 Build_Task_Image_Decls
2691 (Loc, Nam, Etype (Prefix (Nam)));
2693 Decls := Build_Task_Image_Decls (Loc, T, T);
2697 elsif Nkind (Parent (N)) = N_Object_Declaration then
2699 Build_Task_Image_Decls (
2700 Loc, Defining_Identifier (Parent (N)), T);
2703 Decls := Build_Task_Image_Decls (Loc, T, T);
2708 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
2709 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2711 Decl := Last (Decls);
2713 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
2715 -- Has_Task is false, Decls not used
2721 -- Add discriminants if discriminated type
2723 if Has_Discriminants (T) then
2724 Discr := First_Elmt (Discriminant_Constraint (T));
2726 while Present (Discr) loop
2727 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2731 elsif Is_Private_Type (T)
2732 and then Present (Full_View (T))
2733 and then Has_Discriminants (Full_View (T))
2736 First_Elmt (Discriminant_Constraint (Full_View (T)));
2738 while Present (Discr) loop
2739 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2744 -- We set the allocator as analyzed so that when we analyze the
2745 -- expression actions node, we do not get an unwanted recursive
2746 -- expansion of the allocator expression.
2748 Set_Analyzed (N, True);
2749 Node := Relocate_Node (N);
2751 -- Here is the transformation:
2753 -- output: Temp : constant ptr_T := new T;
2754 -- Init (Temp.all, ...);
2755 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
2756 -- <CTRL> Initialize (Finalizable (Temp.all));
2758 -- Here ptr_T is the pointer type for the allocator, and T
2759 -- is the subtype of the allocator.
2762 Make_Object_Declaration (Loc,
2763 Defining_Identifier => Temp,
2764 Constant_Present => True,
2765 Object_Definition => New_Reference_To (Temp_Type, Loc),
2766 Expression => Node);
2768 Set_Assignment_OK (Temp_Decl);
2770 if Is_CPP_Class (T) then
2771 Set_Aliased_Present (Temp_Decl);
2774 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
2776 -- If the designated type is task type or contains tasks,
2777 -- Create block to activate created tasks, and insert
2778 -- declaration for Task_Image variable ahead of call.
2780 if Has_Task (T) then
2782 L : constant List_Id := New_List;
2786 Build_Task_Allocate_Block (L, Node, Args);
2789 Insert_List_Before (First (Declarations (Blk)), Decls);
2790 Insert_Actions (N, L);
2795 Make_Procedure_Call_Statement (Loc,
2796 Name => New_Reference_To (Init, Loc),
2797 Parameter_Associations => Args));
2800 if Controlled_Type (T) then
2801 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
2802 if Ekind (PtrT) = E_Anonymous_Access_Type then
2803 Attach_Level := Uint_1;
2805 Attach_Level := Uint_2;
2809 Ref => New_Copy_Tree (Arg1),
2812 With_Attach => Make_Integer_Literal (Loc,
2816 if Is_CPP_Class (T) then
2818 Make_Attribute_Reference (Loc,
2819 Prefix => New_Reference_To (Temp, Loc),
2820 Attribute_Name => Name_Unchecked_Access));
2822 Rewrite (N, New_Reference_To (Temp, Loc));
2825 Analyze_And_Resolve (N, PtrT);
2831 when RE_Not_Available =>
2833 end Expand_N_Allocator;
2835 -----------------------
2836 -- Expand_N_And_Then --
2837 -----------------------
2839 -- Expand into conditional expression if Actions present, and also
2840 -- deal with optimizing case of arguments being True or False.
2842 procedure Expand_N_And_Then (N : Node_Id) is
2843 Loc : constant Source_Ptr := Sloc (N);
2844 Typ : constant Entity_Id := Etype (N);
2845 Left : constant Node_Id := Left_Opnd (N);
2846 Right : constant Node_Id := Right_Opnd (N);
2850 -- Deal with non-standard booleans
2852 if Is_Boolean_Type (Typ) then
2853 Adjust_Condition (Left);
2854 Adjust_Condition (Right);
2855 Set_Etype (N, Standard_Boolean);
2858 -- Check for cases of left argument is True or False
2860 if Nkind (Left) = N_Identifier then
2862 -- If left argument is True, change (True and then Right) to Right.
2863 -- Any actions associated with Right will be executed unconditionally
2864 -- and can thus be inserted into the tree unconditionally.
2866 if Entity (Left) = Standard_True then
2867 if Present (Actions (N)) then
2868 Insert_Actions (N, Actions (N));
2872 Adjust_Result_Type (N, Typ);
2875 -- If left argument is False, change (False and then Right) to
2876 -- False. In this case we can forget the actions associated with
2877 -- Right, since they will never be executed.
2879 elsif Entity (Left) = Standard_False then
2880 Kill_Dead_Code (Right);
2881 Kill_Dead_Code (Actions (N));
2882 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2883 Adjust_Result_Type (N, Typ);
2888 -- If Actions are present, we expand
2890 -- left and then right
2894 -- if left then right else false end
2896 -- with the actions becoming the Then_Actions of the conditional
2897 -- expression. This conditional expression is then further expanded
2898 -- (and will eventually disappear)
2900 if Present (Actions (N)) then
2901 Actlist := Actions (N);
2903 Make_Conditional_Expression (Loc,
2904 Expressions => New_List (
2907 New_Occurrence_Of (Standard_False, Loc))));
2909 Set_Then_Actions (N, Actlist);
2910 Analyze_And_Resolve (N, Standard_Boolean);
2911 Adjust_Result_Type (N, Typ);
2915 -- No actions present, check for cases of right argument True/False
2917 if Nkind (Right) = N_Identifier then
2919 -- Change (Left and then True) to Left. Note that we know there
2920 -- are no actions associated with the True operand, since we
2921 -- just checked for this case above.
2923 if Entity (Right) = Standard_True then
2926 -- Change (Left and then False) to False, making sure to preserve
2927 -- any side effects associated with the Left operand.
2929 elsif Entity (Right) = Standard_False then
2930 Remove_Side_Effects (Left);
2932 (N, New_Occurrence_Of (Standard_False, Loc));
2936 Adjust_Result_Type (N, Typ);
2937 end Expand_N_And_Then;
2939 -------------------------------------
2940 -- Expand_N_Conditional_Expression --
2941 -------------------------------------
2943 -- Expand into expression actions if then/else actions present
2945 procedure Expand_N_Conditional_Expression (N : Node_Id) is
2946 Loc : constant Source_Ptr := Sloc (N);
2947 Cond : constant Node_Id := First (Expressions (N));
2948 Thenx : constant Node_Id := Next (Cond);
2949 Elsex : constant Node_Id := Next (Thenx);
2950 Typ : constant Entity_Id := Etype (N);
2955 -- If either then or else actions are present, then given:
2957 -- if cond then then-expr else else-expr end
2959 -- we insert the following sequence of actions (using Insert_Actions):
2964 -- Cnn := then-expr;
2970 -- and replace the conditional expression by a reference to Cnn
2972 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
2973 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2976 Make_Implicit_If_Statement (N,
2977 Condition => Relocate_Node (Cond),
2979 Then_Statements => New_List (
2980 Make_Assignment_Statement (Sloc (Thenx),
2981 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
2982 Expression => Relocate_Node (Thenx))),
2984 Else_Statements => New_List (
2985 Make_Assignment_Statement (Sloc (Elsex),
2986 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
2987 Expression => Relocate_Node (Elsex))));
2989 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
2990 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
2992 if Present (Then_Actions (N)) then
2994 (First (Then_Statements (New_If)), Then_Actions (N));
2997 if Present (Else_Actions (N)) then
2999 (First (Else_Statements (New_If)), Else_Actions (N));
3002 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
3005 Make_Object_Declaration (Loc,
3006 Defining_Identifier => Cnn,
3007 Object_Definition => New_Occurrence_Of (Typ, Loc)));
3009 Insert_Action (N, New_If);
3010 Analyze_And_Resolve (N, Typ);
3012 end Expand_N_Conditional_Expression;
3014 -----------------------------------
3015 -- Expand_N_Explicit_Dereference --
3016 -----------------------------------
3018 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
3020 -- The only processing required is an insertion of an explicit
3021 -- dereference call for the checked storage pool case.
3023 Insert_Dereference_Action (Prefix (N));
3024 end Expand_N_Explicit_Dereference;
3030 procedure Expand_N_In (N : Node_Id) is
3031 Loc : constant Source_Ptr := Sloc (N);
3032 Rtyp : constant Entity_Id := Etype (N);
3033 Lop : constant Node_Id := Left_Opnd (N);
3034 Rop : constant Node_Id := Right_Opnd (N);
3035 Static : constant Boolean := Is_OK_Static_Expression (N);
3038 -- If we have an explicit range, do a bit of optimization based
3039 -- on range analysis (we may be able to kill one or both checks).
3041 if Nkind (Rop) = N_Range then
3043 Lcheck : constant Compare_Result :=
3044 Compile_Time_Compare (Lop, Low_Bound (Rop));
3045 Ucheck : constant Compare_Result :=
3046 Compile_Time_Compare (Lop, High_Bound (Rop));
3049 -- If either check is known to fail, replace result
3050 -- by False, since the other check does not matter.
3051 -- Preserve the static flag for legality checks, because
3052 -- we are constant-folding beyond RM 4.9.
3054 if Lcheck = LT or else Ucheck = GT then
3056 New_Reference_To (Standard_False, Loc));
3057 Analyze_And_Resolve (N, Rtyp);
3058 Set_Is_Static_Expression (N, Static);
3061 -- If both checks are known to succeed, replace result
3062 -- by True, since we know we are in range.
3064 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3066 New_Reference_To (Standard_True, Loc));
3067 Analyze_And_Resolve (N, Rtyp);
3068 Set_Is_Static_Expression (N, Static);
3071 -- If lower bound check succeeds and upper bound check is
3072 -- not known to succeed or fail, then replace the range check
3073 -- with a comparison against the upper bound.
3075 elsif Lcheck in Compare_GE then
3079 Right_Opnd => High_Bound (Rop)));
3080 Analyze_And_Resolve (N, Rtyp);
3083 -- If upper bound check succeeds and lower bound check is
3084 -- not known to succeed or fail, then replace the range check
3085 -- with a comparison against the lower bound.
3087 elsif Ucheck in Compare_LE then
3091 Right_Opnd => Low_Bound (Rop)));
3092 Analyze_And_Resolve (N, Rtyp);
3097 -- For all other cases of an explicit range, nothing to be done
3101 -- Here right operand is a subtype mark
3105 Typ : Entity_Id := Etype (Rop);
3106 Is_Acc : constant Boolean := Is_Access_Type (Typ);
3107 Obj : Node_Id := Lop;
3108 Cond : Node_Id := Empty;
3111 Remove_Side_Effects (Obj);
3113 -- For tagged type, do tagged membership operation
3115 if Is_Tagged_Type (Typ) then
3117 -- No expansion will be performed when Java_VM, as the
3118 -- JVM back end will handle the membership tests directly
3119 -- (tags are not explicitly represented in Java objects,
3120 -- so the normal tagged membership expansion is not what
3124 Rewrite (N, Tagged_Membership (N));
3125 Analyze_And_Resolve (N, Rtyp);
3130 -- If type is scalar type, rewrite as x in t'first .. t'last
3131 -- This reason we do this is that the bounds may have the wrong
3132 -- type if they come from the original type definition.
3134 elsif Is_Scalar_Type (Typ) then
3138 Make_Attribute_Reference (Loc,
3139 Attribute_Name => Name_First,
3140 Prefix => New_Reference_To (Typ, Loc)),
3143 Make_Attribute_Reference (Loc,
3144 Attribute_Name => Name_Last,
3145 Prefix => New_Reference_To (Typ, Loc))));
3146 Analyze_And_Resolve (N, Rtyp);
3149 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3150 -- a membership test if the subtype mark denotes a constrained
3151 -- Unchecked_Union subtype and the expression lacks inferable
3154 elsif Is_Unchecked_Union (Base_Type (Typ))
3155 and then Is_Constrained (Typ)
3156 and then not Has_Inferable_Discriminants (Lop)
3159 Make_Raise_Program_Error (Loc,
3160 Reason => PE_Unchecked_Union_Restriction));
3162 -- Prevent Gigi from generating incorrect code by rewriting
3163 -- the test as a standard False.
3166 New_Occurrence_Of (Standard_False, Loc));
3171 -- Here we have a non-scalar type
3174 Typ := Designated_Type (Typ);
3177 if not Is_Constrained (Typ) then
3179 New_Reference_To (Standard_True, Loc));
3180 Analyze_And_Resolve (N, Rtyp);
3182 -- For the constrained array case, we have to check the
3183 -- subscripts for an exact match if the lengths are
3184 -- non-zero (the lengths must match in any case).
3186 elsif Is_Array_Type (Typ) then
3188 Check_Subscripts : declare
3189 function Construct_Attribute_Reference
3192 Dim : Nat) return Node_Id;
3193 -- Build attribute reference E'Nam(Dim)
3195 -----------------------------------
3196 -- Construct_Attribute_Reference --
3197 -----------------------------------
3199 function Construct_Attribute_Reference
3202 Dim : Nat) return Node_Id
3206 Make_Attribute_Reference (Loc,
3208 Attribute_Name => Nam,
3209 Expressions => New_List (
3210 Make_Integer_Literal (Loc, Dim)));
3211 end Construct_Attribute_Reference;
3213 -- Start processing for Check_Subscripts
3216 for J in 1 .. Number_Dimensions (Typ) loop
3217 Evolve_And_Then (Cond,
3220 Construct_Attribute_Reference
3221 (Duplicate_Subexpr_No_Checks (Obj),
3224 Construct_Attribute_Reference
3225 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
3227 Evolve_And_Then (Cond,
3230 Construct_Attribute_Reference
3231 (Duplicate_Subexpr_No_Checks (Obj),
3234 Construct_Attribute_Reference
3235 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
3244 Right_Opnd => Make_Null (Loc)),
3245 Right_Opnd => Cond);
3249 Analyze_And_Resolve (N, Rtyp);
3250 end Check_Subscripts;
3252 -- These are the cases where constraint checks may be
3253 -- required, e.g. records with possible discriminants
3256 -- Expand the test into a series of discriminant comparisons.
3257 -- The expression that is built is the negation of the one
3258 -- that is used for checking discriminant constraints.
3260 Obj := Relocate_Node (Left_Opnd (N));
3262 if Has_Discriminants (Typ) then
3263 Cond := Make_Op_Not (Loc,
3264 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
3267 Cond := Make_Or_Else (Loc,
3271 Right_Opnd => Make_Null (Loc)),
3272 Right_Opnd => Cond);
3276 Cond := New_Occurrence_Of (Standard_True, Loc);
3280 Analyze_And_Resolve (N, Rtyp);
3286 --------------------------------
3287 -- Expand_N_Indexed_Component --
3288 --------------------------------
3290 procedure Expand_N_Indexed_Component (N : Node_Id) is
3291 Loc : constant Source_Ptr := Sloc (N);
3292 Typ : constant Entity_Id := Etype (N);
3293 P : constant Node_Id := Prefix (N);
3294 T : constant Entity_Id := Etype (P);
3297 -- A special optimization, if we have an indexed component that
3298 -- is selecting from a slice, then we can eliminate the slice,
3299 -- since, for example, x (i .. j)(k) is identical to x(k). The
3300 -- only difference is the range check required by the slice. The
3301 -- range check for the slice itself has already been generated.
3302 -- The range check for the subscripting operation is ensured
3303 -- by converting the subject to the subtype of the slice.
3305 -- This optimization not only generates better code, avoiding
3306 -- slice messing especially in the packed case, but more importantly
3307 -- bypasses some problems in handling this peculiar case, for
3308 -- example, the issue of dealing specially with object renamings.
3310 if Nkind (P) = N_Slice then
3312 Make_Indexed_Component (Loc,
3313 Prefix => Prefix (P),
3314 Expressions => New_List (
3316 (Etype (First_Index (Etype (P))),
3317 First (Expressions (N))))));
3318 Analyze_And_Resolve (N, Typ);
3322 -- If the prefix is an access type, then we unconditionally rewrite
3323 -- if as an explicit deference. This simplifies processing for several
3324 -- cases, including packed array cases and certain cases in which
3325 -- checks must be generated. We used to try to do this only when it
3326 -- was necessary, but it cleans up the code to do it all the time.
3328 if Is_Access_Type (T) then
3329 Insert_Explicit_Dereference (P);
3330 Analyze_And_Resolve (P, Designated_Type (T));
3333 -- Generate index and validity checks
3335 Generate_Index_Checks (N);
3337 if Validity_Checks_On and then Validity_Check_Subscripts then
3338 Apply_Subscript_Validity_Checks (N);
3341 -- All done for the non-packed case
3343 if not Is_Packed (Etype (Prefix (N))) then
3347 -- For packed arrays that are not bit-packed (i.e. the case of an array
3348 -- with one or more index types with a non-coniguous enumeration type),
3349 -- we can always use the normal packed element get circuit.
3351 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
3352 Expand_Packed_Element_Reference (N);
3356 -- For a reference to a component of a bit packed array, we have to
3357 -- convert it to a reference to the corresponding Packed_Array_Type.
3358 -- We only want to do this for simple references, and not for:
3360 -- Left side of assignment, or prefix of left side of assignment,
3361 -- or prefix of the prefix, to handle packed arrays of packed arrays,
3362 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
3364 -- Renaming objects in renaming associations
3365 -- This case is handled when a use of the renamed variable occurs
3367 -- Actual parameters for a procedure call
3368 -- This case is handled in Exp_Ch6.Expand_Actuals
3370 -- The second expression in a 'Read attribute reference
3372 -- The prefix of an address or size attribute reference
3374 -- The following circuit detects these exceptions
3377 Child : Node_Id := N;
3378 Parnt : Node_Id := Parent (N);
3382 if Nkind (Parnt) = N_Unchecked_Expression then
3385 elsif Nkind (Parnt) = N_Object_Renaming_Declaration
3386 or else Nkind (Parnt) = N_Procedure_Call_Statement
3387 or else (Nkind (Parnt) = N_Parameter_Association
3389 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
3393 elsif Nkind (Parnt) = N_Attribute_Reference
3394 and then (Attribute_Name (Parnt) = Name_Address
3396 Attribute_Name (Parnt) = Name_Size)
3397 and then Prefix (Parnt) = Child
3401 elsif Nkind (Parnt) = N_Assignment_Statement
3402 and then Name (Parnt) = Child
3406 -- If the expression is an index of an indexed component,
3407 -- it must be expanded regardless of context.
3409 elsif Nkind (Parnt) = N_Indexed_Component
3410 and then Child /= Prefix (Parnt)
3412 Expand_Packed_Element_Reference (N);
3415 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
3416 and then Name (Parent (Parnt)) = Parnt
3420 elsif Nkind (Parnt) = N_Attribute_Reference
3421 and then Attribute_Name (Parnt) = Name_Read
3422 and then Next (First (Expressions (Parnt))) = Child
3426 elsif (Nkind (Parnt) = N_Indexed_Component
3427 or else Nkind (Parnt) = N_Selected_Component)
3428 and then Prefix (Parnt) = Child
3433 Expand_Packed_Element_Reference (N);
3437 -- Keep looking up tree for unchecked expression, or if we are
3438 -- the prefix of a possible assignment left side.
3441 Parnt := Parent (Child);
3445 end Expand_N_Indexed_Component;
3447 ---------------------
3448 -- Expand_N_Not_In --
3449 ---------------------
3451 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
3452 -- can be done. This avoids needing to duplicate this expansion code.
3454 procedure Expand_N_Not_In (N : Node_Id) is
3455 Loc : constant Source_Ptr := Sloc (N);
3456 Typ : constant Entity_Id := Etype (N);
3463 Left_Opnd => Left_Opnd (N),
3464 Right_Opnd => Right_Opnd (N))));
3465 Analyze_And_Resolve (N, Typ);
3466 end Expand_N_Not_In;
3472 -- The only replacement required is for the case of a null of type
3473 -- that is an access to protected subprogram. We represent such
3474 -- access values as a record, and so we must replace the occurrence
3475 -- of null by the equivalent record (with a null address and a null
3476 -- pointer in it), so that the backend creates the proper value.
3478 procedure Expand_N_Null (N : Node_Id) is
3479 Loc : constant Source_Ptr := Sloc (N);
3480 Typ : constant Entity_Id := Etype (N);
3484 if Ekind (Typ) = E_Access_Protected_Subprogram_Type then
3486 Make_Aggregate (Loc,
3487 Expressions => New_List (
3488 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
3492 Analyze_And_Resolve (N, Equivalent_Type (Typ));
3494 -- For subsequent semantic analysis, the node must retain its
3495 -- type. Gigi in any case replaces this type by the corresponding
3496 -- record type before processing the node.
3502 when RE_Not_Available =>
3506 ---------------------
3507 -- Expand_N_Op_Abs --
3508 ---------------------
3510 procedure Expand_N_Op_Abs (N : Node_Id) is
3511 Loc : constant Source_Ptr := Sloc (N);
3512 Expr : constant Node_Id := Right_Opnd (N);
3515 Unary_Op_Validity_Checks (N);
3517 -- Deal with software overflow checking
3519 if not Backend_Overflow_Checks_On_Target
3520 and then Is_Signed_Integer_Type (Etype (N))
3521 and then Do_Overflow_Check (N)
3523 -- The only case to worry about is when the argument is
3524 -- equal to the largest negative number, so what we do is
3525 -- to insert the check:
3527 -- [constraint_error when Expr = typ'Base'First]
3529 -- with the usual Duplicate_Subexpr use coding for expr
3532 Make_Raise_Constraint_Error (Loc,
3535 Left_Opnd => Duplicate_Subexpr (Expr),
3537 Make_Attribute_Reference (Loc,
3539 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
3540 Attribute_Name => Name_First)),
3541 Reason => CE_Overflow_Check_Failed));
3544 -- Vax floating-point types case
3546 if Vax_Float (Etype (N)) then
3547 Expand_Vax_Arith (N);
3549 end Expand_N_Op_Abs;
3551 ---------------------
3552 -- Expand_N_Op_Add --
3553 ---------------------
3555 procedure Expand_N_Op_Add (N : Node_Id) is
3556 Typ : constant Entity_Id := Etype (N);
3559 Binary_Op_Validity_Checks (N);
3561 -- N + 0 = 0 + N = N for integer types
3563 if Is_Integer_Type (Typ) then
3564 if Compile_Time_Known_Value (Right_Opnd (N))
3565 and then Expr_Value (Right_Opnd (N)) = Uint_0
3567 Rewrite (N, Left_Opnd (N));
3570 elsif Compile_Time_Known_Value (Left_Opnd (N))
3571 and then Expr_Value (Left_Opnd (N)) = Uint_0
3573 Rewrite (N, Right_Opnd (N));
3578 -- Arithmetic overflow checks for signed integer/fixed point types
3580 if Is_Signed_Integer_Type (Typ)
3581 or else Is_Fixed_Point_Type (Typ)
3583 Apply_Arithmetic_Overflow_Check (N);
3586 -- Vax floating-point types case
3588 elsif Vax_Float (Typ) then
3589 Expand_Vax_Arith (N);
3591 end Expand_N_Op_Add;
3593 ---------------------
3594 -- Expand_N_Op_And --
3595 ---------------------
3597 procedure Expand_N_Op_And (N : Node_Id) is
3598 Typ : constant Entity_Id := Etype (N);
3601 Binary_Op_Validity_Checks (N);
3603 if Is_Array_Type (Etype (N)) then
3604 Expand_Boolean_Operator (N);
3606 elsif Is_Boolean_Type (Etype (N)) then
3607 Adjust_Condition (Left_Opnd (N));
3608 Adjust_Condition (Right_Opnd (N));
3609 Set_Etype (N, Standard_Boolean);
3610 Adjust_Result_Type (N, Typ);
3612 end Expand_N_Op_And;
3614 ------------------------
3615 -- Expand_N_Op_Concat --
3616 ------------------------
3618 Max_Available_String_Operands : Int := -1;
3619 -- This is initialized the first time this routine is called. It records
3620 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
3621 -- available in the run-time:
3624 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
3625 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
3626 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
3627 -- 5 All routines including RE_Str_Concat_5 available
3629 Char_Concat_Available : Boolean;
3630 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
3631 -- all three are available, False if any one of these is unavailable.
3633 procedure Expand_N_Op_Concat (N : Node_Id) is
3635 -- List of operands to be concatenated
3638 -- Single operand for concatenation
3641 -- Node which is to be replaced by the result of concatenating
3642 -- the nodes in the list Opnds.
3645 -- Array type of concatenation result type
3648 -- Component type of concatenation represented by Cnode
3651 -- Initialize global variables showing run-time status
3653 if Max_Available_String_Operands < 1 then
3654 if not RTE_Available (RE_Str_Concat) then
3655 Max_Available_String_Operands := 0;
3656 elsif not RTE_Available (RE_Str_Concat_3) then
3657 Max_Available_String_Operands := 2;
3658 elsif not RTE_Available (RE_Str_Concat_4) then
3659 Max_Available_String_Operands := 3;
3660 elsif not RTE_Available (RE_Str_Concat_5) then
3661 Max_Available_String_Operands := 4;
3663 Max_Available_String_Operands := 5;
3666 Char_Concat_Available :=
3667 RTE_Available (RE_Str_Concat_CC)
3669 RTE_Available (RE_Str_Concat_CS)
3671 RTE_Available (RE_Str_Concat_SC);
3674 -- Ensure validity of both operands
3676 Binary_Op_Validity_Checks (N);
3678 -- If we are the left operand of a concatenation higher up the
3679 -- tree, then do nothing for now, since we want to deal with a
3680 -- series of concatenations as a unit.
3682 if Nkind (Parent (N)) = N_Op_Concat
3683 and then N = Left_Opnd (Parent (N))
3688 -- We get here with a concatenation whose left operand may be a
3689 -- concatenation itself with a consistent type. We need to process
3690 -- these concatenation operands from left to right, which means
3691 -- from the deepest node in the tree to the highest node.
3694 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
3695 Cnode := Left_Opnd (Cnode);
3698 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
3699 -- nodes above, so now we process bottom up, doing the operations. We
3700 -- gather a string that is as long as possible up to five operands
3702 -- The outer loop runs more than once if there are more than five
3703 -- concatenations of type Standard.String, the most we handle for
3704 -- this case, or if more than one concatenation type is involved.
3707 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
3708 Set_Parent (Opnds, N);
3710 -- The inner loop gathers concatenation operands. We gather any
3711 -- number of these in the non-string case, or if no concatenation
3712 -- routines are available for string (since in that case we will
3713 -- treat string like any other non-string case). Otherwise we only
3714 -- gather as many operands as can be handled by the available
3715 -- procedures in the run-time library (normally 5, but may be
3716 -- less for the configurable run-time case).
3718 Inner : while Cnode /= N
3719 and then (Base_Type (Etype (Cnode)) /= Standard_String
3721 Max_Available_String_Operands = 0
3723 List_Length (Opnds) <
3724 Max_Available_String_Operands)
3725 and then Base_Type (Etype (Cnode)) =
3726 Base_Type (Etype (Parent (Cnode)))
3728 Cnode := Parent (Cnode);
3729 Append (Right_Opnd (Cnode), Opnds);
3732 -- Here we process the collected operands. First we convert
3733 -- singleton operands to singleton aggregates. This is skipped
3734 -- however for the case of two operands of type String, since
3735 -- we have special routines for these cases.
3737 Atyp := Base_Type (Etype (Cnode));
3738 Ctyp := Base_Type (Component_Type (Etype (Cnode)));
3740 if (List_Length (Opnds) > 2 or else Atyp /= Standard_String)
3741 or else not Char_Concat_Available
3743 Opnd := First (Opnds);
3745 if Base_Type (Etype (Opnd)) = Ctyp then
3747 Make_Aggregate (Sloc (Cnode),
3748 Expressions => New_List (Relocate_Node (Opnd))));
3749 Analyze_And_Resolve (Opnd, Atyp);
3753 exit when No (Opnd);
3757 -- Now call appropriate continuation routine
3759 if Atyp = Standard_String
3760 and then Max_Available_String_Operands > 0
3762 Expand_Concatenate_String (Cnode, Opnds);
3764 Expand_Concatenate_Other (Cnode, Opnds);
3767 exit Outer when Cnode = N;
3768 Cnode := Parent (Cnode);
3770 end Expand_N_Op_Concat;
3772 ------------------------
3773 -- Expand_N_Op_Divide --
3774 ------------------------
3776 procedure Expand_N_Op_Divide (N : Node_Id) is
3777 Loc : constant Source_Ptr := Sloc (N);
3778 Ltyp : constant Entity_Id := Etype (Left_Opnd (N));
3779 Rtyp : constant Entity_Id := Etype (Right_Opnd (N));
3780 Typ : Entity_Id := Etype (N);
3783 Binary_Op_Validity_Checks (N);
3785 -- Vax_Float is a special case
3787 if Vax_Float (Typ) then
3788 Expand_Vax_Arith (N);
3792 -- N / 1 = N for integer types
3794 if Is_Integer_Type (Typ)
3795 and then Compile_Time_Known_Value (Right_Opnd (N))
3796 and then Expr_Value (Right_Opnd (N)) = Uint_1
3798 Rewrite (N, Left_Opnd (N));
3802 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
3803 -- Is_Power_Of_2_For_Shift is set means that we know that our left
3804 -- operand is an unsigned integer, as required for this to work.
3806 if Nkind (Right_Opnd (N)) = N_Op_Expon
3807 and then Is_Power_Of_2_For_Shift (Right_Opnd (N))
3809 -- We cannot do this transformation in configurable run time mode if we
3810 -- have 64-bit -- integers and long shifts are not available.
3814 or else Support_Long_Shifts_On_Target)
3817 Make_Op_Shift_Right (Loc,
3818 Left_Opnd => Left_Opnd (N),
3820 Convert_To (Standard_Natural, Right_Opnd (Right_Opnd (N)))));
3821 Analyze_And_Resolve (N, Typ);
3825 -- Do required fixup of universal fixed operation
3827 if Typ = Universal_Fixed then
3828 Fixup_Universal_Fixed_Operation (N);
3832 -- Divisions with fixed-point results
3834 if Is_Fixed_Point_Type (Typ) then
3836 -- No special processing if Treat_Fixed_As_Integer is set,
3837 -- since from a semantic point of view such operations are
3838 -- simply integer operations and will be treated that way.
3840 if not Treat_Fixed_As_Integer (N) then
3841 if Is_Integer_Type (Rtyp) then
3842 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
3844 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
3848 -- Other cases of division of fixed-point operands. Again we
3849 -- exclude the case where Treat_Fixed_As_Integer is set.
3851 elsif (Is_Fixed_Point_Type (Ltyp) or else
3852 Is_Fixed_Point_Type (Rtyp))
3853 and then not Treat_Fixed_As_Integer (N)
3855 if Is_Integer_Type (Typ) then
3856 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
3858 pragma Assert (Is_Floating_Point_Type (Typ));
3859 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
3862 -- Mixed-mode operations can appear in a non-static universal
3863 -- context, in which case the integer argument must be converted
3866 elsif Typ = Universal_Real
3867 and then Is_Integer_Type (Rtyp)
3869 Rewrite (Right_Opnd (N),
3870 Convert_To (Universal_Real, Relocate_Node (Right_Opnd (N))));
3872 Analyze_And_Resolve (Right_Opnd (N), Universal_Real);
3874 elsif Typ = Universal_Real
3875 and then Is_Integer_Type (Ltyp)
3877 Rewrite (Left_Opnd (N),
3878 Convert_To (Universal_Real, Relocate_Node (Left_Opnd (N))));
3880 Analyze_And_Resolve (Left_Opnd (N), Universal_Real);
3882 -- Non-fixed point cases, do zero divide and overflow checks
3884 elsif Is_Integer_Type (Typ) then
3885 Apply_Divide_Check (N);
3887 -- Check for 64-bit division available
3889 if Esize (Ltyp) > 32
3890 and then not Support_64_Bit_Divides_On_Target
3892 Error_Msg_CRT ("64-bit division", N);
3895 end Expand_N_Op_Divide;
3897 --------------------
3898 -- Expand_N_Op_Eq --
3899 --------------------
3901 procedure Expand_N_Op_Eq (N : Node_Id) is
3902 Loc : constant Source_Ptr := Sloc (N);
3903 Typ : constant Entity_Id := Etype (N);
3904 Lhs : constant Node_Id := Left_Opnd (N);
3905 Rhs : constant Node_Id := Right_Opnd (N);
3906 Bodies : constant List_Id := New_List;
3907 A_Typ : constant Entity_Id := Etype (Lhs);
3909 Typl : Entity_Id := A_Typ;
3910 Op_Name : Entity_Id;
3913 procedure Build_Equality_Call (Eq : Entity_Id);
3914 -- If a constructed equality exists for the type or for its parent,
3915 -- build and analyze call, adding conversions if the operation is
3918 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
3919 -- Determines whether a type has a subcompoment of an unconstrained
3920 -- Unchecked_Union subtype. Typ is a record type.
3922 -------------------------
3923 -- Build_Equality_Call --
3924 -------------------------
3926 procedure Build_Equality_Call (Eq : Entity_Id) is
3927 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
3928 L_Exp : Node_Id := Relocate_Node (Lhs);
3929 R_Exp : Node_Id := Relocate_Node (Rhs);
3932 if Base_Type (Op_Type) /= Base_Type (A_Typ)
3933 and then not Is_Class_Wide_Type (A_Typ)
3935 L_Exp := OK_Convert_To (Op_Type, L_Exp);
3936 R_Exp := OK_Convert_To (Op_Type, R_Exp);
3939 -- If we have an Unchecked_Union, we need to add the inferred
3940 -- discriminant values as actuals in the function call. At this
3941 -- point, the expansion has determined that both operands have
3942 -- inferable discriminants.
3944 if Is_Unchecked_Union (Op_Type) then
3946 Lhs_Type : constant Node_Id := Etype (L_Exp);
3947 Rhs_Type : constant Node_Id := Etype (R_Exp);
3948 Lhs_Discr_Val : Node_Id;
3949 Rhs_Discr_Val : Node_Id;
3952 -- Per-object constrained selected components require special
3953 -- attention. If the enclosing scope of the component is an
3954 -- Unchecked_Union, we can not reference its discriminants
3955 -- directly. This is why we use the two extra parameters of
3956 -- the equality function of the enclosing Unchecked_Union.
3958 -- type UU_Type (Discr : Integer := 0) is
3961 -- pragma Unchecked_Union (UU_Type);
3963 -- 1. Unchecked_Union enclosing record:
3965 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
3967 -- Comp : UU_Type (Discr);
3969 -- end Enclosing_UU_Type;
3970 -- pragma Unchecked_Union (Enclosing_UU_Type);
3972 -- Obj1 : Enclosing_UU_Type;
3973 -- Obj2 : Enclosing_UU_Type (1);
3975 -- [. . .] Obj1 = Obj2 [. . .]
3979 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
3981 -- A and B are the formal parameters of the equality function
3982 -- of Enclosing_UU_Type. The function always has two extra
3983 -- formals to capture the inferred discriminant values.
3985 -- 2. Non-Unchecked_Union enclosing record:
3988 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
3991 -- Comp : UU_Type (Discr);
3993 -- end Enclosing_Non_UU_Type;
3995 -- Obj1 : Enclosing_Non_UU_Type;
3996 -- Obj2 : Enclosing_Non_UU_Type (1);
3998 -- . . . Obj1 = Obj2 . . .
4002 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
4003 -- obj1.discr, obj2.discr)) then
4005 -- In this case we can directly reference the discriminants of
4006 -- the enclosing record.
4010 if Nkind (Lhs) = N_Selected_Component
4011 and then Has_Per_Object_Constraint
4012 (Entity (Selector_Name (Lhs)))
4014 -- Enclosing record is an Unchecked_Union, use formal A
4016 if Is_Unchecked_Union (Scope
4017 (Entity (Selector_Name (Lhs))))
4020 Make_Identifier (Loc,
4023 -- Enclosing record is of a non-Unchecked_Union type, it is
4024 -- possible to reference the discriminant.
4028 Make_Selected_Component (Loc,
4029 Prefix => Prefix (Lhs),
4032 (Get_Discriminant_Value
4033 (First_Discriminant (Lhs_Type),
4035 Stored_Constraint (Lhs_Type))));
4038 -- Comment needed here ???
4041 -- Infer the discriminant value
4045 (Get_Discriminant_Value
4046 (First_Discriminant (Lhs_Type),
4048 Stored_Constraint (Lhs_Type)));
4053 if Nkind (Rhs) = N_Selected_Component
4054 and then Has_Per_Object_Constraint
4055 (Entity (Selector_Name (Rhs)))
4057 if Is_Unchecked_Union
4058 (Scope (Entity (Selector_Name (Rhs))))
4061 Make_Identifier (Loc,
4066 Make_Selected_Component (Loc,
4067 Prefix => Prefix (Rhs),
4069 New_Copy (Get_Discriminant_Value (
4070 First_Discriminant (Rhs_Type),
4072 Stored_Constraint (Rhs_Type))));
4077 New_Copy (Get_Discriminant_Value (
4078 First_Discriminant (Rhs_Type),
4080 Stored_Constraint (Rhs_Type)));
4085 Make_Function_Call (Loc,
4086 Name => New_Reference_To (Eq, Loc),
4087 Parameter_Associations => New_List (
4094 -- Normal case, not an unchecked union
4098 Make_Function_Call (Loc,
4099 Name => New_Reference_To (Eq, Loc),
4100 Parameter_Associations => New_List (L_Exp, R_Exp)));
4103 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4104 end Build_Equality_Call;
4106 ------------------------------------
4107 -- Has_Unconstrained_UU_Component --
4108 ------------------------------------
4110 function Has_Unconstrained_UU_Component
4111 (Typ : Node_Id) return Boolean
4113 Tdef : constant Node_Id :=
4114 Type_Definition (Declaration_Node (Base_Type (Typ)));
4118 function Component_Is_Unconstrained_UU
4119 (Comp : Node_Id) return Boolean;
4120 -- Determines whether the subtype of the component is an
4121 -- unconstrained Unchecked_Union.
4123 function Variant_Is_Unconstrained_UU
4124 (Variant : Node_Id) return Boolean;
4125 -- Determines whether a component of the variant has an unconstrained
4126 -- Unchecked_Union subtype.
4128 -----------------------------------
4129 -- Component_Is_Unconstrained_UU --
4130 -----------------------------------
4132 function Component_Is_Unconstrained_UU
4133 (Comp : Node_Id) return Boolean
4136 if Nkind (Comp) /= N_Component_Declaration then
4141 Sindic : constant Node_Id :=
4142 Subtype_Indication (Component_Definition (Comp));
4145 -- Unconstrained nominal type. In the case of a constraint
4146 -- present, the node kind would have been N_Subtype_Indication.
4148 if Nkind (Sindic) = N_Identifier then
4149 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
4154 end Component_Is_Unconstrained_UU;
4156 ---------------------------------
4157 -- Variant_Is_Unconstrained_UU --
4158 ---------------------------------
4160 function Variant_Is_Unconstrained_UU
4161 (Variant : Node_Id) return Boolean
4163 Clist : constant Node_Id := Component_List (Variant);
4166 if Is_Empty_List (Component_Items (Clist)) then
4171 Comp : Node_Id := First (Component_Items (Clist));
4174 while Present (Comp) loop
4176 -- One component is sufficent
4178 if Component_Is_Unconstrained_UU (Comp) then
4186 -- None of the components withing the variant were of
4187 -- unconstrained Unchecked_Union type.
4190 end Variant_Is_Unconstrained_UU;
4192 -- Start of processing for Has_Unconstrained_UU_Component
4195 if Null_Present (Tdef) then
4199 Clist := Component_List (Tdef);
4200 Vpart := Variant_Part (Clist);
4202 -- Inspect available components
4204 if Present (Component_Items (Clist)) then
4206 Comp : Node_Id := First (Component_Items (Clist));
4209 while Present (Comp) loop
4211 -- One component is sufficent
4213 if Component_Is_Unconstrained_UU (Comp) then
4222 -- Inspect available components withing variants
4224 if Present (Vpart) then
4226 Variant : Node_Id := First (Variants (Vpart));
4229 while Present (Variant) loop
4231 -- One component within a variant is sufficent
4233 if Variant_Is_Unconstrained_UU (Variant) then
4242 -- Neither the available components, nor the components inside the
4243 -- variant parts were of an unconstrained Unchecked_Union subtype.
4246 end Has_Unconstrained_UU_Component;
4248 -- Start of processing for Expand_N_Op_Eq
4251 Binary_Op_Validity_Checks (N);
4253 if Ekind (Typl) = E_Private_Type then
4254 Typl := Underlying_Type (Typl);
4256 elsif Ekind (Typl) = E_Private_Subtype then
4257 Typl := Underlying_Type (Base_Type (Typl));
4260 -- It may happen in error situations that the underlying type is not
4261 -- set. The error will be detected later, here we just defend the
4268 Typl := Base_Type (Typl);
4272 if Vax_Float (Typl) then
4273 Expand_Vax_Comparison (N);
4276 -- Boolean types (requiring handling of non-standard case)
4278 elsif Is_Boolean_Type (Typl) then
4279 Adjust_Condition (Left_Opnd (N));
4280 Adjust_Condition (Right_Opnd (N));
4281 Set_Etype (N, Standard_Boolean);
4282 Adjust_Result_Type (N, Typ);
4286 elsif Is_Array_Type (Typl) then
4288 -- If we are doing full validity checking, then expand out array
4289 -- comparisons to make sure that we check the array elements.
4291 if Validity_Check_Operands then
4293 Save_Force_Validity_Checks : constant Boolean :=
4294 Force_Validity_Checks;
4296 Force_Validity_Checks := True;
4298 Expand_Array_Equality
4300 Relocate_Node (Lhs),
4301 Relocate_Node (Rhs),
4304 Insert_Actions (N, Bodies);
4305 Analyze_And_Resolve (N, Standard_Boolean);
4306 Force_Validity_Checks := Save_Force_Validity_Checks;
4309 -- Packed case where both operands are known aligned
4311 elsif Is_Bit_Packed_Array (Typl)
4312 and then not Is_Possibly_Unaligned_Object (Lhs)
4313 and then not Is_Possibly_Unaligned_Object (Rhs)
4315 Expand_Packed_Eq (N);
4317 -- Where the component type is elementary we can use a block bit
4318 -- comparison (if supported on the target) exception in the case
4319 -- of floating-point (negative zero issues require element by
4320 -- element comparison), and atomic types (where we must be sure
4321 -- to load elements independently) and possibly unaligned arrays.
4323 elsif Is_Elementary_Type (Component_Type (Typl))
4324 and then not Is_Floating_Point_Type (Component_Type (Typl))
4325 and then not Is_Atomic (Component_Type (Typl))
4326 and then not Is_Possibly_Unaligned_Object (Lhs)
4327 and then not Is_Possibly_Unaligned_Object (Rhs)
4328 and then Support_Composite_Compare_On_Target
4332 -- For composite and floating-point cases, expand equality loop
4333 -- to make sure of using proper comparisons for tagged types,
4334 -- and correctly handling the floating-point case.
4338 Expand_Array_Equality
4340 Relocate_Node (Lhs),
4341 Relocate_Node (Rhs),
4344 Insert_Actions (N, Bodies, Suppress => All_Checks);
4345 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4350 elsif Is_Record_Type (Typl) then
4352 -- For tagged types, use the primitive "="
4354 if Is_Tagged_Type (Typl) then
4356 -- If this is derived from an untagged private type completed
4357 -- with a tagged type, it does not have a full view, so we
4358 -- use the primitive operations of the private type.
4359 -- This check should no longer be necessary when these
4360 -- types receive their full views ???
4362 if Is_Private_Type (A_Typ)
4363 and then not Is_Tagged_Type (A_Typ)
4364 and then Is_Derived_Type (A_Typ)
4365 and then No (Full_View (A_Typ))
4367 -- Search for equality operation, checking that the
4368 -- operands have the same type. Note that we must find
4369 -- a matching entry, or something is very wrong!
4371 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
4373 while Present (Prim) loop
4374 exit when Chars (Node (Prim)) = Name_Op_Eq
4375 and then Etype (First_Formal (Node (Prim))) =
4376 Etype (Next_Formal (First_Formal (Node (Prim))))
4378 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4383 pragma Assert (Present (Prim));
4384 Op_Name := Node (Prim);
4386 -- Find the type's predefined equality or an overriding
4387 -- user-defined equality. The reason for not simply calling
4388 -- Find_Prim_Op here is that there may be a user-defined
4389 -- overloaded equality op that precedes the equality that
4390 -- we want, so we have to explicitly search (e.g., there
4391 -- could be an equality with two different parameter types).
4394 if Is_Class_Wide_Type (Typl) then
4395 Typl := Root_Type (Typl);
4398 Prim := First_Elmt (Primitive_Operations (Typl));
4399 while Present (Prim) loop
4400 exit when Chars (Node (Prim)) = Name_Op_Eq
4401 and then Etype (First_Formal (Node (Prim))) =
4402 Etype (Next_Formal (First_Formal (Node (Prim))))
4404 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4409 pragma Assert (Present (Prim));
4410 Op_Name := Node (Prim);
4413 Build_Equality_Call (Op_Name);
4415 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
4416 -- predefined equality operator for a type which has a subcomponent
4417 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
4419 elsif Has_Unconstrained_UU_Component (Typl) then
4421 Make_Raise_Program_Error (Loc,
4422 Reason => PE_Unchecked_Union_Restriction));
4424 -- Prevent Gigi from generating incorrect code by rewriting the
4425 -- equality as a standard False.
4428 New_Occurrence_Of (Standard_False, Loc));
4430 elsif Is_Unchecked_Union (Typl) then
4432 -- If we can infer the discriminants of the operands, we make a
4433 -- call to the TSS equality function.
4435 if Has_Inferable_Discriminants (Lhs)
4437 Has_Inferable_Discriminants (Rhs)
4440 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4443 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4444 -- the predefined equality operator for an Unchecked_Union type
4445 -- if either of the operands lack inferable discriminants.
4448 Make_Raise_Program_Error (Loc,
4449 Reason => PE_Unchecked_Union_Restriction));
4451 -- Prevent Gigi from generating incorrect code by rewriting
4452 -- the equality as a standard False.
4455 New_Occurrence_Of (Standard_False, Loc));
4459 -- If a type support function is present (for complex cases), use it
4461 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
4463 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4465 -- Otherwise expand the component by component equality. Note that
4466 -- we never use block-bit coparisons for records, because of the
4467 -- problems with gaps. The backend will often be able to recombine
4468 -- the separate comparisons that we generate here.
4471 Remove_Side_Effects (Lhs);
4472 Remove_Side_Effects (Rhs);
4474 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
4476 Insert_Actions (N, Bodies, Suppress => All_Checks);
4477 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4481 -- If we still have an equality comparison (i.e. it was not rewritten
4482 -- in some way), then we can test if result is needed at compile time).
4484 if Nkind (N) = N_Op_Eq then
4485 Rewrite_Comparison (N);
4489 -----------------------
4490 -- Expand_N_Op_Expon --
4491 -----------------------
4493 procedure Expand_N_Op_Expon (N : Node_Id) is
4494 Loc : constant Source_Ptr := Sloc (N);
4495 Typ : constant Entity_Id := Etype (N);
4496 Rtyp : constant Entity_Id := Root_Type (Typ);
4497 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
4498 Bastyp : constant Node_Id := Etype (Base);
4499 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
4500 Exptyp : constant Entity_Id := Etype (Exp);
4501 Ovflo : constant Boolean := Do_Overflow_Check (N);
4510 Binary_Op_Validity_Checks (N);
4512 -- If either operand is of a private type, then we have the use of
4513 -- an intrinsic operator, and we get rid of the privateness, by using
4514 -- root types of underlying types for the actual operation. Otherwise
4515 -- the private types will cause trouble if we expand multiplications
4516 -- or shifts etc. We also do this transformation if the result type
4517 -- is different from the base type.
4519 if Is_Private_Type (Etype (Base))
4521 Is_Private_Type (Typ)
4523 Is_Private_Type (Exptyp)
4525 Rtyp /= Root_Type (Bastyp)
4528 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
4529 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
4533 Unchecked_Convert_To (Typ,
4535 Left_Opnd => Unchecked_Convert_To (Bt, Base),
4536 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
4537 Analyze_And_Resolve (N, Typ);
4542 -- Test for case of known right argument
4544 if Compile_Time_Known_Value (Exp) then
4545 Expv := Expr_Value (Exp);
4547 -- We only fold small non-negative exponents. You might think we
4548 -- could fold small negative exponents for the real case, but we
4549 -- can't because we are required to raise Constraint_Error for
4550 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
4551 -- See ACVC test C4A012B.
4553 if Expv >= 0 and then Expv <= 4 then
4555 -- X ** 0 = 1 (or 1.0)
4558 if Ekind (Typ) in Integer_Kind then
4559 Xnode := Make_Integer_Literal (Loc, Intval => 1);
4561 Xnode := Make_Real_Literal (Loc, Ureal_1);
4573 Make_Op_Multiply (Loc,
4574 Left_Opnd => Duplicate_Subexpr (Base),
4575 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4577 -- X ** 3 = X * X * X
4581 Make_Op_Multiply (Loc,
4583 Make_Op_Multiply (Loc,
4584 Left_Opnd => Duplicate_Subexpr (Base),
4585 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
4586 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4589 -- En : constant base'type := base * base;
4595 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
4597 Insert_Actions (N, New_List (
4598 Make_Object_Declaration (Loc,
4599 Defining_Identifier => Temp,
4600 Constant_Present => True,
4601 Object_Definition => New_Reference_To (Typ, Loc),
4603 Make_Op_Multiply (Loc,
4604 Left_Opnd => Duplicate_Subexpr (Base),
4605 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
4608 Make_Op_Multiply (Loc,
4609 Left_Opnd => New_Reference_To (Temp, Loc),
4610 Right_Opnd => New_Reference_To (Temp, Loc));
4614 Analyze_And_Resolve (N, Typ);
4619 -- Case of (2 ** expression) appearing as an argument of an integer
4620 -- multiplication, or as the right argument of a division of a non-
4621 -- negative integer. In such cases we leave the node untouched, setting
4622 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
4623 -- of the higher level node converts it into a shift.
4625 if Nkind (Base) = N_Integer_Literal
4626 and then Intval (Base) = 2
4627 and then Is_Integer_Type (Root_Type (Exptyp))
4628 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
4629 and then Is_Unsigned_Type (Exptyp)
4631 and then Nkind (Parent (N)) in N_Binary_Op
4634 P : constant Node_Id := Parent (N);
4635 L : constant Node_Id := Left_Opnd (P);
4636 R : constant Node_Id := Right_Opnd (P);
4639 if (Nkind (P) = N_Op_Multiply
4641 ((Is_Integer_Type (Etype (L)) and then R = N)
4643 (Is_Integer_Type (Etype (R)) and then L = N))
4644 and then not Do_Overflow_Check (P))
4647 (Nkind (P) = N_Op_Divide
4648 and then Is_Integer_Type (Etype (L))
4649 and then Is_Unsigned_Type (Etype (L))
4651 and then not Do_Overflow_Check (P))
4653 Set_Is_Power_Of_2_For_Shift (N);
4659 -- Fall through if exponentiation must be done using a runtime routine
4661 -- First deal with modular case
4663 if Is_Modular_Integer_Type (Rtyp) then
4665 -- Non-binary case, we call the special exponentiation routine for
4666 -- the non-binary case, converting the argument to Long_Long_Integer
4667 -- and passing the modulus value. Then the result is converted back
4668 -- to the base type.
4670 if Non_Binary_Modulus (Rtyp) then
4673 Make_Function_Call (Loc,
4674 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
4675 Parameter_Associations => New_List (
4676 Convert_To (Standard_Integer, Base),
4677 Make_Integer_Literal (Loc, Modulus (Rtyp)),
4680 -- Binary case, in this case, we call one of two routines, either
4681 -- the unsigned integer case, or the unsigned long long integer
4682 -- case, with a final "and" operation to do the required mod.
4685 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
4686 Ent := RTE (RE_Exp_Unsigned);
4688 Ent := RTE (RE_Exp_Long_Long_Unsigned);
4695 Make_Function_Call (Loc,
4696 Name => New_Reference_To (Ent, Loc),
4697 Parameter_Associations => New_List (
4698 Convert_To (Etype (First_Formal (Ent)), Base),
4701 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
4705 -- Common exit point for modular type case
4707 Analyze_And_Resolve (N, Typ);
4710 -- Signed integer cases, done using either Integer or Long_Long_Integer.
4711 -- It is not worth having routines for Short_[Short_]Integer, since for
4712 -- most machines it would not help, and it would generate more code that
4713 -- might need certification in the HI-E case.
4715 -- In the integer cases, we have two routines, one for when overflow
4716 -- checks are required, and one when they are not required, since
4717 -- there is a real gain in ommitting checks on many machines.
4719 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
4720 or else (Rtyp = Base_Type (Standard_Long_Integer)
4722 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
4723 or else (Rtyp = Universal_Integer)
4725 Etyp := Standard_Long_Long_Integer;
4728 Rent := RE_Exp_Long_Long_Integer;
4730 Rent := RE_Exn_Long_Long_Integer;
4733 elsif Is_Signed_Integer_Type (Rtyp) then
4734 Etyp := Standard_Integer;
4737 Rent := RE_Exp_Integer;
4739 Rent := RE_Exn_Integer;
4742 -- Floating-point cases, always done using Long_Long_Float. We do not
4743 -- need separate routines for the overflow case here, since in the case
4744 -- of floating-point, we generate infinities anyway as a rule (either
4745 -- that or we automatically trap overflow), and if there is an infinity
4746 -- generated and a range check is required, the check will fail anyway.
4749 pragma Assert (Is_Floating_Point_Type (Rtyp));
4750 Etyp := Standard_Long_Long_Float;
4751 Rent := RE_Exn_Long_Long_Float;
4754 -- Common processing for integer cases and floating-point cases.
4755 -- If we are in the right type, we can call runtime routine directly
4758 and then Rtyp /= Universal_Integer
4759 and then Rtyp /= Universal_Real
4762 Make_Function_Call (Loc,
4763 Name => New_Reference_To (RTE (Rent), Loc),
4764 Parameter_Associations => New_List (Base, Exp)));
4766 -- Otherwise we have to introduce conversions (conversions are also
4767 -- required in the universal cases, since the runtime routine is
4768 -- typed using one of the standard types.
4773 Make_Function_Call (Loc,
4774 Name => New_Reference_To (RTE (Rent), Loc),
4775 Parameter_Associations => New_List (
4776 Convert_To (Etyp, Base),
4780 Analyze_And_Resolve (N, Typ);
4784 when RE_Not_Available =>
4786 end Expand_N_Op_Expon;
4788 --------------------
4789 -- Expand_N_Op_Ge --
4790 --------------------
4792 procedure Expand_N_Op_Ge (N : Node_Id) is
4793 Typ : constant Entity_Id := Etype (N);
4794 Op1 : constant Node_Id := Left_Opnd (N);
4795 Op2 : constant Node_Id := Right_Opnd (N);
4796 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4799 Binary_Op_Validity_Checks (N);
4801 if Vax_Float (Typ1) then
4802 Expand_Vax_Comparison (N);
4805 elsif Is_Array_Type (Typ1) then
4806 Expand_Array_Comparison (N);
4810 if Is_Boolean_Type (Typ1) then
4811 Adjust_Condition (Op1);
4812 Adjust_Condition (Op2);
4813 Set_Etype (N, Standard_Boolean);
4814 Adjust_Result_Type (N, Typ);
4817 Rewrite_Comparison (N);
4820 --------------------
4821 -- Expand_N_Op_Gt --
4822 --------------------
4824 procedure Expand_N_Op_Gt (N : Node_Id) is
4825 Typ : constant Entity_Id := Etype (N);
4826 Op1 : constant Node_Id := Left_Opnd (N);
4827 Op2 : constant Node_Id := Right_Opnd (N);
4828 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4831 Binary_Op_Validity_Checks (N);
4833 if Vax_Float (Typ1) then
4834 Expand_Vax_Comparison (N);
4837 elsif Is_Array_Type (Typ1) then
4838 Expand_Array_Comparison (N);
4842 if Is_Boolean_Type (Typ1) then
4843 Adjust_Condition (Op1);
4844 Adjust_Condition (Op2);
4845 Set_Etype (N, Standard_Boolean);
4846 Adjust_Result_Type (N, Typ);
4849 Rewrite_Comparison (N);
4852 --------------------
4853 -- Expand_N_Op_Le --
4854 --------------------
4856 procedure Expand_N_Op_Le (N : Node_Id) is
4857 Typ : constant Entity_Id := Etype (N);
4858 Op1 : constant Node_Id := Left_Opnd (N);
4859 Op2 : constant Node_Id := Right_Opnd (N);
4860 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4863 Binary_Op_Validity_Checks (N);
4865 if Vax_Float (Typ1) then
4866 Expand_Vax_Comparison (N);
4869 elsif Is_Array_Type (Typ1) then
4870 Expand_Array_Comparison (N);
4874 if Is_Boolean_Type (Typ1) then
4875 Adjust_Condition (Op1);
4876 Adjust_Condition (Op2);
4877 Set_Etype (N, Standard_Boolean);
4878 Adjust_Result_Type (N, Typ);
4881 Rewrite_Comparison (N);
4884 --------------------
4885 -- Expand_N_Op_Lt --
4886 --------------------
4888 procedure Expand_N_Op_Lt (N : Node_Id) is
4889 Typ : constant Entity_Id := Etype (N);
4890 Op1 : constant Node_Id := Left_Opnd (N);
4891 Op2 : constant Node_Id := Right_Opnd (N);
4892 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4895 Binary_Op_Validity_Checks (N);
4897 if Vax_Float (Typ1) then
4898 Expand_Vax_Comparison (N);
4901 elsif Is_Array_Type (Typ1) then
4902 Expand_Array_Comparison (N);
4906 if Is_Boolean_Type (Typ1) then
4907 Adjust_Condition (Op1);
4908 Adjust_Condition (Op2);
4909 Set_Etype (N, Standard_Boolean);
4910 Adjust_Result_Type (N, Typ);
4913 Rewrite_Comparison (N);
4916 -----------------------
4917 -- Expand_N_Op_Minus --
4918 -----------------------
4920 procedure Expand_N_Op_Minus (N : Node_Id) is
4921 Loc : constant Source_Ptr := Sloc (N);
4922 Typ : constant Entity_Id := Etype (N);
4925 Unary_Op_Validity_Checks (N);
4927 if not Backend_Overflow_Checks_On_Target
4928 and then Is_Signed_Integer_Type (Etype (N))
4929 and then Do_Overflow_Check (N)
4931 -- Software overflow checking expands -expr into (0 - expr)
4934 Make_Op_Subtract (Loc,
4935 Left_Opnd => Make_Integer_Literal (Loc, 0),
4936 Right_Opnd => Right_Opnd (N)));
4938 Analyze_And_Resolve (N, Typ);
4940 -- Vax floating-point types case
4942 elsif Vax_Float (Etype (N)) then
4943 Expand_Vax_Arith (N);
4945 end Expand_N_Op_Minus;
4947 ---------------------
4948 -- Expand_N_Op_Mod --
4949 ---------------------
4951 procedure Expand_N_Op_Mod (N : Node_Id) is
4952 Loc : constant Source_Ptr := Sloc (N);
4953 Typ : constant Entity_Id := Etype (N);
4954 Left : constant Node_Id := Left_Opnd (N);
4955 Right : constant Node_Id := Right_Opnd (N);
4956 DOC : constant Boolean := Do_Overflow_Check (N);
4957 DDC : constant Boolean := Do_Division_Check (N);
4968 Binary_Op_Validity_Checks (N);
4970 Determine_Range (Right, ROK, Rlo, Rhi);
4971 Determine_Range (Left, LOK, Llo, Lhi);
4973 -- Convert mod to rem if operands are known non-negative. We do this
4974 -- since it is quite likely that this will improve the quality of code,
4975 -- (the operation now corresponds to the hardware remainder), and it
4976 -- does not seem likely that it could be harmful.
4978 if LOK and then Llo >= 0
4980 ROK and then Rlo >= 0
4983 Make_Op_Rem (Sloc (N),
4984 Left_Opnd => Left_Opnd (N),
4985 Right_Opnd => Right_Opnd (N)));
4987 -- Instead of reanalyzing the node we do the analysis manually.
4988 -- This avoids anomalies when the replacement is done in an
4989 -- instance and is epsilon more efficient.
4991 Set_Entity (N, Standard_Entity (S_Op_Rem));
4993 Set_Do_Overflow_Check (N, DOC);
4994 Set_Do_Division_Check (N, DDC);
4995 Expand_N_Op_Rem (N);
4998 -- Otherwise, normal mod processing
5001 if Is_Integer_Type (Etype (N)) then
5002 Apply_Divide_Check (N);
5005 -- Apply optimization x mod 1 = 0. We don't really need that with
5006 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5007 -- certainly harmless.
5009 if Is_Integer_Type (Etype (N))
5010 and then Compile_Time_Known_Value (Right)
5011 and then Expr_Value (Right) = Uint_1
5013 Rewrite (N, Make_Integer_Literal (Loc, 0));
5014 Analyze_And_Resolve (N, Typ);
5018 -- Deal with annoying case of largest negative number remainder
5019 -- minus one. Gigi does not handle this case correctly, because
5020 -- it generates a divide instruction which may trap in this case.
5022 -- In fact the check is quite easy, if the right operand is -1,
5023 -- then the mod value is always 0, and we can just ignore the
5024 -- left operand completely in this case.
5026 -- The operand type may be private (e.g. in the expansion of an
5027 -- an intrinsic operation) so we must use the underlying type to
5028 -- get the bounds, and convert the literals explicitly.
5032 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5034 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5036 ((not LOK) or else (Llo = LLB))
5039 Make_Conditional_Expression (Loc,
5040 Expressions => New_List (
5042 Left_Opnd => Duplicate_Subexpr (Right),
5044 Unchecked_Convert_To (Typ,
5045 Make_Integer_Literal (Loc, -1))),
5046 Unchecked_Convert_To (Typ,
5047 Make_Integer_Literal (Loc, Uint_0)),
5048 Relocate_Node (N))));
5050 Set_Analyzed (Next (Next (First (Expressions (N)))));
5051 Analyze_And_Resolve (N, Typ);
5054 end Expand_N_Op_Mod;
5056 --------------------------
5057 -- Expand_N_Op_Multiply --
5058 --------------------------
5060 procedure Expand_N_Op_Multiply (N : Node_Id) is
5061 Loc : constant Source_Ptr := Sloc (N);
5062 Lop : constant Node_Id := Left_Opnd (N);
5063 Rop : constant Node_Id := Right_Opnd (N);
5065 Lp2 : constant Boolean :=
5066 Nkind (Lop) = N_Op_Expon
5067 and then Is_Power_Of_2_For_Shift (Lop);
5069 Rp2 : constant Boolean :=
5070 Nkind (Rop) = N_Op_Expon
5071 and then Is_Power_Of_2_For_Shift (Rop);
5073 Ltyp : constant Entity_Id := Etype (Lop);
5074 Rtyp : constant Entity_Id := Etype (Rop);
5075 Typ : Entity_Id := Etype (N);
5078 Binary_Op_Validity_Checks (N);
5080 -- Special optimizations for integer types
5082 if Is_Integer_Type (Typ) then
5084 -- N * 0 = 0 * N = 0 for integer types
5086 if (Compile_Time_Known_Value (Rop)
5087 and then Expr_Value (Rop) = Uint_0)
5089 (Compile_Time_Known_Value (Lop)
5090 and then Expr_Value (Lop) = Uint_0)
5092 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5093 Analyze_And_Resolve (N, Typ);
5097 -- N * 1 = 1 * N = N for integer types
5099 -- This optimisation is not done if we are going to
5100 -- rewrite the product 1 * 2 ** N to a shift.
5102 if Compile_Time_Known_Value (Rop)
5103 and then Expr_Value (Rop) = Uint_1
5109 elsif Compile_Time_Known_Value (Lop)
5110 and then Expr_Value (Lop) = Uint_1
5118 -- Deal with VAX float case
5120 if Vax_Float (Typ) then
5121 Expand_Vax_Arith (N);
5125 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5126 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5127 -- operand is an integer, as required for this to work.
5132 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5136 Left_Opnd => Make_Integer_Literal (Loc, 2),
5139 Left_Opnd => Right_Opnd (Lop),
5140 Right_Opnd => Right_Opnd (Rop))));
5141 Analyze_And_Resolve (N, Typ);
5146 Make_Op_Shift_Left (Loc,
5149 Convert_To (Standard_Natural, Right_Opnd (Rop))));
5150 Analyze_And_Resolve (N, Typ);
5154 -- Same processing for the operands the other way round
5158 Make_Op_Shift_Left (Loc,
5161 Convert_To (Standard_Natural, Right_Opnd (Lop))));
5162 Analyze_And_Resolve (N, Typ);
5166 -- Do required fixup of universal fixed operation
5168 if Typ = Universal_Fixed then
5169 Fixup_Universal_Fixed_Operation (N);
5173 -- Multiplications with fixed-point results
5175 if Is_Fixed_Point_Type (Typ) then
5177 -- No special processing if Treat_Fixed_As_Integer is set,
5178 -- since from a semantic point of view such operations are
5179 -- simply integer operations and will be treated that way.
5181 if not Treat_Fixed_As_Integer (N) then
5183 -- Case of fixed * integer => fixed
5185 if Is_Integer_Type (Rtyp) then
5186 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
5188 -- Case of integer * fixed => fixed
5190 elsif Is_Integer_Type (Ltyp) then
5191 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
5193 -- Case of fixed * fixed => fixed
5196 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
5200 -- Other cases of multiplication of fixed-point operands. Again
5201 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
5203 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
5204 and then not Treat_Fixed_As_Integer (N)
5206 if Is_Integer_Type (Typ) then
5207 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
5209 pragma Assert (Is_Floating_Point_Type (Typ));
5210 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
5213 -- Mixed-mode operations can appear in a non-static universal
5214 -- context, in which case the integer argument must be converted
5217 elsif Typ = Universal_Real
5218 and then Is_Integer_Type (Rtyp)
5220 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
5222 Analyze_And_Resolve (Rop, Universal_Real);
5224 elsif Typ = Universal_Real
5225 and then Is_Integer_Type (Ltyp)
5227 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
5229 Analyze_And_Resolve (Lop, Universal_Real);
5231 -- Non-fixed point cases, check software overflow checking required
5233 elsif Is_Signed_Integer_Type (Etype (N)) then
5234 Apply_Arithmetic_Overflow_Check (N);
5236 end Expand_N_Op_Multiply;
5238 --------------------
5239 -- Expand_N_Op_Ne --
5240 --------------------
5242 -- Rewrite node as the negation of an equality operation, and reanalyze.
5243 -- The equality to be used is defined in the same scope and has the same
5244 -- signature. It must be set explicitly because in an instance it may not
5245 -- have the same visibility as in the generic unit.
5247 procedure Expand_N_Op_Ne (N : Node_Id) is
5248 Loc : constant Source_Ptr := Sloc (N);
5250 Ne : constant Entity_Id := Entity (N);
5253 Binary_Op_Validity_Checks (N);
5259 Left_Opnd => Left_Opnd (N),
5260 Right_Opnd => Right_Opnd (N)));
5261 Set_Paren_Count (Right_Opnd (Neg), 1);
5263 if Scope (Ne) /= Standard_Standard then
5264 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
5267 -- For navigation purposes, the inequality is treated as an implicit
5268 -- reference to the corresponding equality. Preserve the Comes_From_
5269 -- source flag so that the proper Xref entry is generated.
5271 Preserve_Comes_From_Source (Neg, N);
5272 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
5274 Analyze_And_Resolve (N, Standard_Boolean);
5277 ---------------------
5278 -- Expand_N_Op_Not --
5279 ---------------------
5281 -- If the argument is other than a Boolean array type, there is no
5282 -- special expansion required.
5284 -- For the packed case, we call the special routine in Exp_Pakd, except
5285 -- that if the component size is greater than one, we use the standard
5286 -- routine generating a gruesome loop (it is so peculiar to have packed
5287 -- arrays with non-standard Boolean representations anyway, so it does
5288 -- not matter that we do not handle this case efficiently).
5290 -- For the unpacked case (and for the special packed case where we have
5291 -- non standard Booleans, as discussed above), we generate and insert
5292 -- into the tree the following function definition:
5294 -- function Nnnn (A : arr) is
5297 -- for J in a'range loop
5298 -- B (J) := not A (J);
5303 -- Here arr is the actual subtype of the parameter (and hence always
5304 -- constrained). Then we replace the not with a call to this function.
5306 procedure Expand_N_Op_Not (N : Node_Id) is
5307 Loc : constant Source_Ptr := Sloc (N);
5308 Typ : constant Entity_Id := Etype (N);
5317 Func_Name : Entity_Id;
5318 Loop_Statement : Node_Id;
5321 Unary_Op_Validity_Checks (N);
5323 -- For boolean operand, deal with non-standard booleans
5325 if Is_Boolean_Type (Typ) then
5326 Adjust_Condition (Right_Opnd (N));
5327 Set_Etype (N, Standard_Boolean);
5328 Adjust_Result_Type (N, Typ);
5332 -- Only array types need any other processing
5334 if not Is_Array_Type (Typ) then
5338 -- Case of array operand. If bit packed with a component size of 1,
5339 -- handle it in Exp_Pakd if the operand is known to be aligned.
5341 if Is_Bit_Packed_Array (Typ)
5342 and then Component_Size (Typ) = 1
5343 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
5345 Expand_Packed_Not (N);
5349 -- Case of array operand which is not bit-packed. If the context is
5350 -- a safe assignment, call in-place operation, If context is a larger
5351 -- boolean expression in the context of a safe assignment, expansion is
5352 -- done by enclosing operation.
5354 Opnd := Relocate_Node (Right_Opnd (N));
5355 Convert_To_Actual_Subtype (Opnd);
5356 Arr := Etype (Opnd);
5357 Ensure_Defined (Arr, N);
5359 if Nkind (Parent (N)) = N_Assignment_Statement then
5360 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
5361 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5364 -- Special case the negation of a binary operation
5366 elsif (Nkind (Opnd) = N_Op_And
5367 or else Nkind (Opnd) = N_Op_Or
5368 or else Nkind (Opnd) = N_Op_Xor)
5369 and then Safe_In_Place_Array_Op
5370 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
5372 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5376 elsif Nkind (Parent (N)) in N_Binary_Op
5377 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
5380 Op1 : constant Node_Id := Left_Opnd (Parent (N));
5381 Op2 : constant Node_Id := Right_Opnd (Parent (N));
5382 Lhs : constant Node_Id := Name (Parent (Parent (N)));
5385 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
5387 and then Nkind (Op2) = N_Op_Not
5389 -- (not A) op (not B) can be reduced to a single call
5394 and then Nkind (Parent (N)) = N_Op_Xor
5396 -- A xor (not B) can also be special-cased
5404 A := Make_Defining_Identifier (Loc, Name_uA);
5405 B := Make_Defining_Identifier (Loc, Name_uB);
5406 J := Make_Defining_Identifier (Loc, Name_uJ);
5409 Make_Indexed_Component (Loc,
5410 Prefix => New_Reference_To (A, Loc),
5411 Expressions => New_List (New_Reference_To (J, Loc)));
5414 Make_Indexed_Component (Loc,
5415 Prefix => New_Reference_To (B, Loc),
5416 Expressions => New_List (New_Reference_To (J, Loc)));
5419 Make_Implicit_Loop_Statement (N,
5420 Identifier => Empty,
5423 Make_Iteration_Scheme (Loc,
5424 Loop_Parameter_Specification =>
5425 Make_Loop_Parameter_Specification (Loc,
5426 Defining_Identifier => J,
5427 Discrete_Subtype_Definition =>
5428 Make_Attribute_Reference (Loc,
5429 Prefix => Make_Identifier (Loc, Chars (A)),
5430 Attribute_Name => Name_Range))),
5432 Statements => New_List (
5433 Make_Assignment_Statement (Loc,
5435 Expression => Make_Op_Not (Loc, A_J))));
5437 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
5438 Set_Is_Inlined (Func_Name);
5441 Make_Subprogram_Body (Loc,
5443 Make_Function_Specification (Loc,
5444 Defining_Unit_Name => Func_Name,
5445 Parameter_Specifications => New_List (
5446 Make_Parameter_Specification (Loc,
5447 Defining_Identifier => A,
5448 Parameter_Type => New_Reference_To (Typ, Loc))),
5449 Subtype_Mark => New_Reference_To (Typ, Loc)),
5451 Declarations => New_List (
5452 Make_Object_Declaration (Loc,
5453 Defining_Identifier => B,
5454 Object_Definition => New_Reference_To (Arr, Loc))),
5456 Handled_Statement_Sequence =>
5457 Make_Handled_Sequence_Of_Statements (Loc,
5458 Statements => New_List (
5460 Make_Return_Statement (Loc,
5462 Make_Identifier (Loc, Chars (B)))))));
5465 Make_Function_Call (Loc,
5466 Name => New_Reference_To (Func_Name, Loc),
5467 Parameter_Associations => New_List (Opnd)));
5469 Analyze_And_Resolve (N, Typ);
5470 end Expand_N_Op_Not;
5472 --------------------
5473 -- Expand_N_Op_Or --
5474 --------------------
5476 procedure Expand_N_Op_Or (N : Node_Id) is
5477 Typ : constant Entity_Id := Etype (N);
5480 Binary_Op_Validity_Checks (N);
5482 if Is_Array_Type (Etype (N)) then
5483 Expand_Boolean_Operator (N);
5485 elsif Is_Boolean_Type (Etype (N)) then
5486 Adjust_Condition (Left_Opnd (N));
5487 Adjust_Condition (Right_Opnd (N));
5488 Set_Etype (N, Standard_Boolean);
5489 Adjust_Result_Type (N, Typ);
5493 ----------------------
5494 -- Expand_N_Op_Plus --
5495 ----------------------
5497 procedure Expand_N_Op_Plus (N : Node_Id) is
5499 Unary_Op_Validity_Checks (N);
5500 end Expand_N_Op_Plus;
5502 ---------------------
5503 -- Expand_N_Op_Rem --
5504 ---------------------
5506 procedure Expand_N_Op_Rem (N : Node_Id) is
5507 Loc : constant Source_Ptr := Sloc (N);
5508 Typ : constant Entity_Id := Etype (N);
5510 Left : constant Node_Id := Left_Opnd (N);
5511 Right : constant Node_Id := Right_Opnd (N);
5522 Binary_Op_Validity_Checks (N);
5524 if Is_Integer_Type (Etype (N)) then
5525 Apply_Divide_Check (N);
5528 -- Apply optimization x rem 1 = 0. We don't really need that with
5529 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5530 -- certainly harmless.
5532 if Is_Integer_Type (Etype (N))
5533 and then Compile_Time_Known_Value (Right)
5534 and then Expr_Value (Right) = Uint_1
5536 Rewrite (N, Make_Integer_Literal (Loc, 0));
5537 Analyze_And_Resolve (N, Typ);
5541 -- Deal with annoying case of largest negative number remainder
5542 -- minus one. Gigi does not handle this case correctly, because
5543 -- it generates a divide instruction which may trap in this case.
5545 -- In fact the check is quite easy, if the right operand is -1,
5546 -- then the remainder is always 0, and we can just ignore the
5547 -- left operand completely in this case.
5549 Determine_Range (Right, ROK, Rlo, Rhi);
5550 Determine_Range (Left, LOK, Llo, Lhi);
5552 -- The operand type may be private (e.g. in the expansion of an
5553 -- an intrinsic operation) so we must use the underlying type to
5554 -- get the bounds, and convert the literals explicitly.
5558 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5560 -- Now perform the test, generating code only if needed
5562 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5564 ((not LOK) or else (Llo = LLB))
5567 Make_Conditional_Expression (Loc,
5568 Expressions => New_List (
5570 Left_Opnd => Duplicate_Subexpr (Right),
5572 Unchecked_Convert_To (Typ,
5573 Make_Integer_Literal (Loc, -1))),
5575 Unchecked_Convert_To (Typ,
5576 Make_Integer_Literal (Loc, Uint_0)),
5578 Relocate_Node (N))));
5580 Set_Analyzed (Next (Next (First (Expressions (N)))));
5581 Analyze_And_Resolve (N, Typ);
5583 end Expand_N_Op_Rem;
5585 -----------------------------
5586 -- Expand_N_Op_Rotate_Left --
5587 -----------------------------
5589 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
5591 Binary_Op_Validity_Checks (N);
5592 end Expand_N_Op_Rotate_Left;
5594 ------------------------------
5595 -- Expand_N_Op_Rotate_Right --
5596 ------------------------------
5598 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
5600 Binary_Op_Validity_Checks (N);
5601 end Expand_N_Op_Rotate_Right;
5603 ----------------------------
5604 -- Expand_N_Op_Shift_Left --
5605 ----------------------------
5607 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
5609 Binary_Op_Validity_Checks (N);
5610 end Expand_N_Op_Shift_Left;
5612 -----------------------------
5613 -- Expand_N_Op_Shift_Right --
5614 -----------------------------
5616 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
5618 Binary_Op_Validity_Checks (N);
5619 end Expand_N_Op_Shift_Right;
5621 ----------------------------------------
5622 -- Expand_N_Op_Shift_Right_Arithmetic --
5623 ----------------------------------------
5625 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
5627 Binary_Op_Validity_Checks (N);
5628 end Expand_N_Op_Shift_Right_Arithmetic;
5630 --------------------------
5631 -- Expand_N_Op_Subtract --
5632 --------------------------
5634 procedure Expand_N_Op_Subtract (N : Node_Id) is
5635 Typ : constant Entity_Id := Etype (N);
5638 Binary_Op_Validity_Checks (N);
5640 -- N - 0 = N for integer types
5642 if Is_Integer_Type (Typ)
5643 and then Compile_Time_Known_Value (Right_Opnd (N))
5644 and then Expr_Value (Right_Opnd (N)) = 0
5646 Rewrite (N, Left_Opnd (N));
5650 -- Arithemtic overflow checks for signed integer/fixed point types
5652 if Is_Signed_Integer_Type (Typ)
5653 or else Is_Fixed_Point_Type (Typ)
5655 Apply_Arithmetic_Overflow_Check (N);
5657 -- Vax floating-point types case
5659 elsif Vax_Float (Typ) then
5660 Expand_Vax_Arith (N);
5662 end Expand_N_Op_Subtract;
5664 ---------------------
5665 -- Expand_N_Op_Xor --
5666 ---------------------
5668 procedure Expand_N_Op_Xor (N : Node_Id) is
5669 Typ : constant Entity_Id := Etype (N);
5672 Binary_Op_Validity_Checks (N);
5674 if Is_Array_Type (Etype (N)) then
5675 Expand_Boolean_Operator (N);
5677 elsif Is_Boolean_Type (Etype (N)) then
5678 Adjust_Condition (Left_Opnd (N));
5679 Adjust_Condition (Right_Opnd (N));
5680 Set_Etype (N, Standard_Boolean);
5681 Adjust_Result_Type (N, Typ);
5683 end Expand_N_Op_Xor;
5685 ----------------------
5686 -- Expand_N_Or_Else --
5687 ----------------------
5689 -- Expand into conditional expression if Actions present, and also
5690 -- deal with optimizing case of arguments being True or False.
5692 procedure Expand_N_Or_Else (N : Node_Id) is
5693 Loc : constant Source_Ptr := Sloc (N);
5694 Typ : constant Entity_Id := Etype (N);
5695 Left : constant Node_Id := Left_Opnd (N);
5696 Right : constant Node_Id := Right_Opnd (N);
5700 -- Deal with non-standard booleans
5702 if Is_Boolean_Type (Typ) then
5703 Adjust_Condition (Left);
5704 Adjust_Condition (Right);
5705 Set_Etype (N, Standard_Boolean);
5708 -- Check for cases of left argument is True or False
5710 if Nkind (Left) = N_Identifier then
5712 -- If left argument is False, change (False or else Right) to Right.
5713 -- Any actions associated with Right will be executed unconditionally
5714 -- and can thus be inserted into the tree unconditionally.
5716 if Entity (Left) = Standard_False then
5717 if Present (Actions (N)) then
5718 Insert_Actions (N, Actions (N));
5722 Adjust_Result_Type (N, Typ);
5725 -- If left argument is True, change (True and then Right) to
5726 -- True. In this case we can forget the actions associated with
5727 -- Right, since they will never be executed.
5729 elsif Entity (Left) = Standard_True then
5730 Kill_Dead_Code (Right);
5731 Kill_Dead_Code (Actions (N));
5732 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5733 Adjust_Result_Type (N, Typ);
5738 -- If Actions are present, we expand
5740 -- left or else right
5744 -- if left then True else right end
5746 -- with the actions becoming the Else_Actions of the conditional
5747 -- expression. This conditional expression is then further expanded
5748 -- (and will eventually disappear)
5750 if Present (Actions (N)) then
5751 Actlist := Actions (N);
5753 Make_Conditional_Expression (Loc,
5754 Expressions => New_List (
5756 New_Occurrence_Of (Standard_True, Loc),
5759 Set_Else_Actions (N, Actlist);
5760 Analyze_And_Resolve (N, Standard_Boolean);
5761 Adjust_Result_Type (N, Typ);
5765 -- No actions present, check for cases of right argument True/False
5767 if Nkind (Right) = N_Identifier then
5769 -- Change (Left or else False) to Left. Note that we know there
5770 -- are no actions associated with the True operand, since we
5771 -- just checked for this case above.
5773 if Entity (Right) = Standard_False then
5776 -- Change (Left or else True) to True, making sure to preserve
5777 -- any side effects associated with the Left operand.
5779 elsif Entity (Right) = Standard_True then
5780 Remove_Side_Effects (Left);
5782 (N, New_Occurrence_Of (Standard_True, Loc));
5786 Adjust_Result_Type (N, Typ);
5787 end Expand_N_Or_Else;
5789 -----------------------------------
5790 -- Expand_N_Qualified_Expression --
5791 -----------------------------------
5793 procedure Expand_N_Qualified_Expression (N : Node_Id) is
5794 Operand : constant Node_Id := Expression (N);
5795 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
5798 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
5799 end Expand_N_Qualified_Expression;
5801 ---------------------------------
5802 -- Expand_N_Selected_Component --
5803 ---------------------------------
5805 -- If the selector is a discriminant of a concurrent object, rewrite the
5806 -- prefix to denote the corresponding record type.
5808 procedure Expand_N_Selected_Component (N : Node_Id) is
5809 Loc : constant Source_Ptr := Sloc (N);
5810 Par : constant Node_Id := Parent (N);
5811 P : constant Node_Id := Prefix (N);
5812 Ptyp : Entity_Id := Underlying_Type (Etype (P));
5817 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
5818 -- Gigi needs a temporary for prefixes that depend on a discriminant,
5819 -- unless the context of an assignment can provide size information.
5820 -- Don't we have a general routine that does this???
5822 -----------------------
5823 -- In_Left_Hand_Side --
5824 -----------------------
5826 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
5828 return (Nkind (Parent (Comp)) = N_Assignment_Statement
5829 and then Comp = Name (Parent (Comp)))
5830 or else (Present (Parent (Comp))
5831 and then Nkind (Parent (Comp)) in N_Subexpr
5832 and then In_Left_Hand_Side (Parent (Comp)));
5833 end In_Left_Hand_Side;
5835 -- Start of processing for Expand_N_Selected_Component
5838 -- Insert explicit dereference if required
5840 if Is_Access_Type (Ptyp) then
5841 Insert_Explicit_Dereference (P);
5842 Analyze_And_Resolve (P, Designated_Type (Ptyp));
5844 if Ekind (Etype (P)) = E_Private_Subtype
5845 and then Is_For_Access_Subtype (Etype (P))
5847 Set_Etype (P, Base_Type (Etype (P)));
5853 -- Deal with discriminant check required
5855 if Do_Discriminant_Check (N) then
5857 -- Present the discrminant checking function to the backend,
5858 -- so that it can inline the call to the function.
5861 (Discriminant_Checking_Func
5862 (Original_Record_Component (Entity (Selector_Name (N)))));
5864 -- Now reset the flag and generate the call
5866 Set_Do_Discriminant_Check (N, False);
5867 Generate_Discriminant_Check (N);
5870 -- Gigi cannot handle unchecked conversions that are the prefix of a
5871 -- selected component with discriminants. This must be checked during
5872 -- expansion, because during analysis the type of the selector is not
5873 -- known at the point the prefix is analyzed. If the conversion is the
5874 -- target of an assignment, then we cannot force the evaluation.
5876 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
5877 and then Has_Discriminants (Etype (N))
5878 and then not In_Left_Hand_Side (N)
5880 Force_Evaluation (Prefix (N));
5883 -- Remaining processing applies only if selector is a discriminant
5885 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
5887 -- If the selector is a discriminant of a constrained record type,
5888 -- we may be able to rewrite the expression with the actual value
5889 -- of the discriminant, a useful optimization in some cases.
5891 if Is_Record_Type (Ptyp)
5892 and then Has_Discriminants (Ptyp)
5893 and then Is_Constrained (Ptyp)
5895 -- Do this optimization for discrete types only, and not for
5896 -- access types (access discriminants get us into trouble!)
5898 if not Is_Discrete_Type (Etype (N)) then
5901 -- Don't do this on the left hand of an assignment statement.
5902 -- Normally one would think that references like this would
5903 -- not occur, but they do in generated code, and mean that
5904 -- we really do want to assign the discriminant!
5906 elsif Nkind (Par) = N_Assignment_Statement
5907 and then Name (Par) = N
5911 -- Don't do this optimization for the prefix of an attribute
5912 -- or the operand of an object renaming declaration since these
5913 -- are contexts where we do not want the value anyway.
5915 elsif (Nkind (Par) = N_Attribute_Reference
5916 and then Prefix (Par) = N)
5917 or else Is_Renamed_Object (N)
5921 -- Don't do this optimization if we are within the code for a
5922 -- discriminant check, since the whole point of such a check may
5923 -- be to verify the condition on which the code below depends!
5925 elsif Is_In_Discriminant_Check (N) then
5928 -- Green light to see if we can do the optimization. There is
5929 -- still one condition that inhibits the optimization below
5930 -- but now is the time to check the particular discriminant.
5933 -- Loop through discriminants to find the matching
5934 -- discriminant constraint to see if we can copy it.
5936 Disc := First_Discriminant (Ptyp);
5937 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
5938 Discr_Loop : while Present (Dcon) loop
5940 -- Check if this is the matching discriminant
5942 if Disc = Entity (Selector_Name (N)) then
5944 -- Here we have the matching discriminant. Check for
5945 -- the case of a discriminant of a component that is
5946 -- constrained by an outer discriminant, which cannot
5947 -- be optimized away.
5950 Denotes_Discriminant
5951 (Node (Dcon), Check_Protected => True)
5955 -- In the context of a case statement, the expression
5956 -- may have the base type of the discriminant, and we
5957 -- need to preserve the constraint to avoid spurious
5958 -- errors on missing cases.
5960 elsif Nkind (Parent (N)) = N_Case_Statement
5961 and then Etype (Node (Dcon)) /= Etype (Disc)
5964 Make_Qualified_Expression (Loc,
5966 New_Occurrence_Of (Etype (Disc), Loc),
5968 New_Copy_Tree (Node (Dcon))));
5969 Analyze_And_Resolve (N, Etype (Disc));
5971 -- In case that comes out as a static expression,
5972 -- reset it (a selected component is never static).
5974 Set_Is_Static_Expression (N, False);
5977 -- Otherwise we can just copy the constraint, but the
5978 -- result is certainly not static! In some cases the
5979 -- discriminant constraint has been analyzed in the
5980 -- context of the original subtype indication, but for
5981 -- itypes the constraint might not have been analyzed
5982 -- yet, and this must be done now.
5985 Rewrite (N, New_Copy_Tree (Node (Dcon)));
5986 Analyze_And_Resolve (N);
5987 Set_Is_Static_Expression (N, False);
5993 Next_Discriminant (Disc);
5994 end loop Discr_Loop;
5996 -- Note: the above loop should always find a matching
5997 -- discriminant, but if it does not, we just missed an
5998 -- optimization due to some glitch (perhaps a previous
5999 -- error), so ignore.
6004 -- The only remaining processing is in the case of a discriminant of
6005 -- a concurrent object, where we rewrite the prefix to denote the
6006 -- corresponding record type. If the type is derived and has renamed
6007 -- discriminants, use corresponding discriminant, which is the one
6008 -- that appears in the corresponding record.
6010 if not Is_Concurrent_Type (Ptyp) then
6014 Disc := Entity (Selector_Name (N));
6016 if Is_Derived_Type (Ptyp)
6017 and then Present (Corresponding_Discriminant (Disc))
6019 Disc := Corresponding_Discriminant (Disc);
6023 Make_Selected_Component (Loc,
6025 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
6027 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
6032 end Expand_N_Selected_Component;
6034 --------------------
6035 -- Expand_N_Slice --
6036 --------------------
6038 procedure Expand_N_Slice (N : Node_Id) is
6039 Loc : constant Source_Ptr := Sloc (N);
6040 Typ : constant Entity_Id := Etype (N);
6041 Pfx : constant Node_Id := Prefix (N);
6042 Ptp : Entity_Id := Etype (Pfx);
6044 function Is_Procedure_Actual (N : Node_Id) return Boolean;
6045 -- Check whether the argument is an actual for a procedure call,
6046 -- in which case the expansion of a bit-packed slice is deferred
6047 -- until the call itself is expanded. The reason this is required
6048 -- is that we might have an IN OUT or OUT parameter, and the copy out
6049 -- is essential, and that copy out would be missed if we created a
6050 -- temporary here in Expand_N_Slice. Note that we don't bother
6051 -- to test specifically for an IN OUT or OUT mode parameter, since it
6052 -- is a bit tricky to do, and it is harmless to defer expansion
6053 -- in the IN case, since the call processing will still generate the
6054 -- appropriate copy in operation, which will take care of the slice.
6056 procedure Make_Temporary;
6057 -- Create a named variable for the value of the slice, in
6058 -- cases where the back-end cannot handle it properly, e.g.
6059 -- when packed types or unaligned slices are involved.
6061 -------------------------
6062 -- Is_Procedure_Actual --
6063 -------------------------
6065 function Is_Procedure_Actual (N : Node_Id) return Boolean is
6066 Par : Node_Id := Parent (N);
6070 -- If our parent is a procedure call we can return
6072 if Nkind (Par) = N_Procedure_Call_Statement then
6075 -- If our parent is a type conversion, keep climbing the
6076 -- tree, since a type conversion can be a procedure actual.
6077 -- Also keep climbing if parameter association or a qualified
6078 -- expression, since these are additional cases that do can
6079 -- appear on procedure actuals.
6081 elsif Nkind (Par) = N_Type_Conversion
6082 or else Nkind (Par) = N_Parameter_Association
6083 or else Nkind (Par) = N_Qualified_Expression
6085 Par := Parent (Par);
6087 -- Any other case is not what we are looking for
6093 end Is_Procedure_Actual;
6095 --------------------
6096 -- Make_Temporary --
6097 --------------------
6099 procedure Make_Temporary is
6101 Ent : constant Entity_Id :=
6102 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
6105 Make_Object_Declaration (Loc,
6106 Defining_Identifier => Ent,
6107 Object_Definition => New_Occurrence_Of (Typ, Loc));
6109 Set_No_Initialization (Decl);
6111 Insert_Actions (N, New_List (
6113 Make_Assignment_Statement (Loc,
6114 Name => New_Occurrence_Of (Ent, Loc),
6115 Expression => Relocate_Node (N))));
6117 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6118 Analyze_And_Resolve (N, Typ);
6121 -- Start of processing for Expand_N_Slice
6124 -- Special handling for access types
6126 if Is_Access_Type (Ptp) then
6128 Ptp := Designated_Type (Ptp);
6131 Make_Explicit_Dereference (Sloc (N),
6132 Prefix => Relocate_Node (Pfx)));
6134 Analyze_And_Resolve (Pfx, Ptp);
6137 -- Range checks are potentially also needed for cases involving
6138 -- a slice indexed by a subtype indication, but Do_Range_Check
6139 -- can currently only be set for expressions ???
6141 if not Index_Checks_Suppressed (Ptp)
6142 and then (not Is_Entity_Name (Pfx)
6143 or else not Index_Checks_Suppressed (Entity (Pfx)))
6144 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
6146 Enable_Range_Check (Discrete_Range (N));
6149 -- The remaining case to be handled is packed slices. We can leave
6150 -- packed slices as they are in the following situations:
6152 -- 1. Right or left side of an assignment (we can handle this
6153 -- situation correctly in the assignment statement expansion).
6155 -- 2. Prefix of indexed component (the slide is optimized away
6156 -- in this case, see the start of Expand_N_Slice.
6158 -- 3. Object renaming declaration, since we want the name of
6159 -- the slice, not the value.
6161 -- 4. Argument to procedure call, since copy-in/copy-out handling
6162 -- may be required, and this is handled in the expansion of
6165 -- 5. Prefix of an address attribute (this is an error which
6166 -- is caught elsewhere, and the expansion would intefere
6167 -- with generating the error message).
6169 if not Is_Packed (Typ) then
6171 -- Apply transformation for actuals of a function call,
6172 -- where Expand_Actuals is not used.
6174 if Nkind (Parent (N)) = N_Function_Call
6175 and then Is_Possibly_Unaligned_Slice (N)
6180 elsif Nkind (Parent (N)) = N_Assignment_Statement
6181 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
6182 and then Parent (N) = Name (Parent (Parent (N))))
6186 elsif Nkind (Parent (N)) = N_Indexed_Component
6187 or else Is_Renamed_Object (N)
6188 or else Is_Procedure_Actual (N)
6192 elsif Nkind (Parent (N)) = N_Attribute_Reference
6193 and then Attribute_Name (Parent (N)) = Name_Address
6202 ------------------------------
6203 -- Expand_N_Type_Conversion --
6204 ------------------------------
6206 procedure Expand_N_Type_Conversion (N : Node_Id) is
6207 Loc : constant Source_Ptr := Sloc (N);
6208 Operand : constant Node_Id := Expression (N);
6209 Target_Type : constant Entity_Id := Etype (N);
6210 Operand_Type : Entity_Id := Etype (Operand);
6212 procedure Handle_Changed_Representation;
6213 -- This is called in the case of record and array type conversions
6214 -- to see if there is a change of representation to be handled.
6215 -- Change of representation is actually handled at the assignment
6216 -- statement level, and what this procedure does is rewrite node N
6217 -- conversion as an assignment to temporary. If there is no change
6218 -- of representation, then the conversion node is unchanged.
6220 procedure Real_Range_Check;
6221 -- Handles generation of range check for real target value
6223 -----------------------------------
6224 -- Handle_Changed_Representation --
6225 -----------------------------------
6227 procedure Handle_Changed_Representation is
6236 -- Nothing to do if no change of representation
6238 if Same_Representation (Operand_Type, Target_Type) then
6241 -- The real change of representation work is done by the assignment
6242 -- statement processing. So if this type conversion is appearing as
6243 -- the expression of an assignment statement, nothing needs to be
6244 -- done to the conversion.
6246 elsif Nkind (Parent (N)) = N_Assignment_Statement then
6249 -- Otherwise we need to generate a temporary variable, and do the
6250 -- change of representation assignment into that temporary variable.
6251 -- The conversion is then replaced by a reference to this variable.
6256 -- If type is unconstrained we have to add a constraint,
6257 -- copied from the actual value of the left hand side.
6259 if not Is_Constrained (Target_Type) then
6260 if Has_Discriminants (Operand_Type) then
6261 Disc := First_Discriminant (Operand_Type);
6263 if Disc /= First_Stored_Discriminant (Operand_Type) then
6264 Disc := First_Stored_Discriminant (Operand_Type);
6268 while Present (Disc) loop
6270 Make_Selected_Component (Loc,
6271 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
6273 Make_Identifier (Loc, Chars (Disc))));
6274 Next_Discriminant (Disc);
6277 elsif Is_Array_Type (Operand_Type) then
6278 N_Ix := First_Index (Target_Type);
6281 for J in 1 .. Number_Dimensions (Operand_Type) loop
6283 -- We convert the bounds explicitly. We use an unchecked
6284 -- conversion because bounds checks are done elsewhere.
6289 Unchecked_Convert_To (Etype (N_Ix),
6290 Make_Attribute_Reference (Loc,
6292 Duplicate_Subexpr_No_Checks
6293 (Operand, Name_Req => True),
6294 Attribute_Name => Name_First,
6295 Expressions => New_List (
6296 Make_Integer_Literal (Loc, J)))),
6299 Unchecked_Convert_To (Etype (N_Ix),
6300 Make_Attribute_Reference (Loc,
6302 Duplicate_Subexpr_No_Checks
6303 (Operand, Name_Req => True),
6304 Attribute_Name => Name_Last,
6305 Expressions => New_List (
6306 Make_Integer_Literal (Loc, J))))));
6313 Odef := New_Occurrence_Of (Target_Type, Loc);
6315 if Present (Cons) then
6317 Make_Subtype_Indication (Loc,
6318 Subtype_Mark => Odef,
6320 Make_Index_Or_Discriminant_Constraint (Loc,
6321 Constraints => Cons));
6324 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
6326 Make_Object_Declaration (Loc,
6327 Defining_Identifier => Temp,
6328 Object_Definition => Odef);
6330 Set_No_Initialization (Decl, True);
6332 -- Insert required actions. It is essential to suppress checks
6333 -- since we have suppressed default initialization, which means
6334 -- that the variable we create may have no discriminants.
6339 Make_Assignment_Statement (Loc,
6340 Name => New_Occurrence_Of (Temp, Loc),
6341 Expression => Relocate_Node (N))),
6342 Suppress => All_Checks);
6344 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6347 end Handle_Changed_Representation;
6349 ----------------------
6350 -- Real_Range_Check --
6351 ----------------------
6353 -- Case of conversions to floating-point or fixed-point. If range
6354 -- checks are enabled and the target type has a range constraint,
6361 -- Tnn : typ'Base := typ'Base (x);
6362 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6365 -- This is necessary when there is a conversion of integer to float
6366 -- or to fixed-point to ensure that the correct checks are made. It
6367 -- is not necessary for float to float where it is enough to simply
6368 -- set the Do_Range_Check flag.
6370 procedure Real_Range_Check is
6371 Btyp : constant Entity_Id := Base_Type (Target_Type);
6372 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
6373 Hi : constant Node_Id := Type_High_Bound (Target_Type);
6374 Xtyp : constant Entity_Id := Etype (Operand);
6379 -- Nothing to do if conversion was rewritten
6381 if Nkind (N) /= N_Type_Conversion then
6385 -- Nothing to do if range checks suppressed, or target has the
6386 -- same range as the base type (or is the base type).
6388 if Range_Checks_Suppressed (Target_Type)
6389 or else (Lo = Type_Low_Bound (Btyp)
6391 Hi = Type_High_Bound (Btyp))
6396 -- Nothing to do if expression is an entity on which checks
6397 -- have been suppressed.
6399 if Is_Entity_Name (Operand)
6400 and then Range_Checks_Suppressed (Entity (Operand))
6405 -- Nothing to do if bounds are all static and we can tell that
6406 -- the expression is within the bounds of the target. Note that
6407 -- if the operand is of an unconstrained floating-point type,
6408 -- then we do not trust it to be in range (might be infinite)
6411 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
6412 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
6415 if (not Is_Floating_Point_Type (Xtyp)
6416 or else Is_Constrained (Xtyp))
6417 and then Compile_Time_Known_Value (S_Lo)
6418 and then Compile_Time_Known_Value (S_Hi)
6419 and then Compile_Time_Known_Value (Hi)
6420 and then Compile_Time_Known_Value (Lo)
6423 D_Lov : constant Ureal := Expr_Value_R (Lo);
6424 D_Hiv : constant Ureal := Expr_Value_R (Hi);
6429 if Is_Real_Type (Xtyp) then
6430 S_Lov := Expr_Value_R (S_Lo);
6431 S_Hiv := Expr_Value_R (S_Hi);
6433 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
6434 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
6438 and then S_Lov >= D_Lov
6439 and then S_Hiv <= D_Hiv
6441 Set_Do_Range_Check (Operand, False);
6448 -- For float to float conversions, we are done
6450 if Is_Floating_Point_Type (Xtyp)
6452 Is_Floating_Point_Type (Btyp)
6457 -- Otherwise rewrite the conversion as described above
6459 Conv := Relocate_Node (N);
6461 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
6462 Set_Etype (Conv, Btyp);
6464 -- Enable overflow except in the case of integer to float
6465 -- conversions, where it is never required, since we can
6466 -- never have overflow in this case.
6468 if not Is_Integer_Type (Etype (Operand)) then
6469 Enable_Overflow_Check (Conv);
6473 Make_Defining_Identifier (Loc,
6474 Chars => New_Internal_Name ('T'));
6476 Insert_Actions (N, New_List (
6477 Make_Object_Declaration (Loc,
6478 Defining_Identifier => Tnn,
6479 Object_Definition => New_Occurrence_Of (Btyp, Loc),
6480 Expression => Conv),
6482 Make_Raise_Constraint_Error (Loc,
6487 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6489 Make_Attribute_Reference (Loc,
6490 Attribute_Name => Name_First,
6492 New_Occurrence_Of (Target_Type, Loc))),
6496 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6498 Make_Attribute_Reference (Loc,
6499 Attribute_Name => Name_Last,
6501 New_Occurrence_Of (Target_Type, Loc)))),
6502 Reason => CE_Range_Check_Failed)));
6504 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
6505 Analyze_And_Resolve (N, Btyp);
6506 end Real_Range_Check;
6508 -- Start of processing for Expand_N_Type_Conversion
6511 -- Nothing at all to do if conversion is to the identical type
6512 -- so remove the conversion completely, it is useless.
6514 if Operand_Type = Target_Type then
6515 Rewrite (N, Relocate_Node (Operand));
6519 -- Deal with Vax floating-point cases
6521 if Vax_Float (Operand_Type) or else Vax_Float (Target_Type) then
6522 Expand_Vax_Conversion (N);
6526 -- Nothing to do if this is the second argument of read. This
6527 -- is a "backwards" conversion that will be handled by the
6528 -- specialized code in attribute processing.
6530 if Nkind (Parent (N)) = N_Attribute_Reference
6531 and then Attribute_Name (Parent (N)) = Name_Read
6532 and then Next (First (Expressions (Parent (N)))) = N
6537 -- Here if we may need to expand conversion
6539 -- Special case of converting from non-standard boolean type
6541 if Is_Boolean_Type (Operand_Type)
6542 and then (Nonzero_Is_True (Operand_Type))
6544 Adjust_Condition (Operand);
6545 Set_Etype (Operand, Standard_Boolean);
6546 Operand_Type := Standard_Boolean;
6549 -- Case of converting to an access type
6551 if Is_Access_Type (Target_Type) then
6553 -- Apply an accessibility check if the operand is an
6554 -- access parameter. Note that other checks may still
6555 -- need to be applied below (such as tagged type checks).
6557 if Is_Entity_Name (Operand)
6558 and then Ekind (Entity (Operand)) in Formal_Kind
6559 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
6561 Apply_Accessibility_Check (Operand, Target_Type);
6563 -- If the level of the operand type is statically deeper
6564 -- then the level of the target type, then force Program_Error.
6565 -- Note that this can only occur for cases where the attribute
6566 -- is within the body of an instantiation (otherwise the
6567 -- conversion will already have been rejected as illegal).
6568 -- Note: warnings are issued by the analyzer for the instance
6571 elsif In_Instance_Body
6572 and then Type_Access_Level (Operand_Type) >
6573 Type_Access_Level (Target_Type)
6576 Make_Raise_Program_Error (Sloc (N),
6577 Reason => PE_Accessibility_Check_Failed));
6578 Set_Etype (N, Target_Type);
6580 -- When the operand is a selected access discriminant
6581 -- the check needs to be made against the level of the
6582 -- object denoted by the prefix of the selected name.
6583 -- Force Program_Error for this case as well (this
6584 -- accessibility violation can only happen if within
6585 -- the body of an instantiation).
6587 elsif In_Instance_Body
6588 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
6589 and then Nkind (Operand) = N_Selected_Component
6590 and then Object_Access_Level (Operand) >
6591 Type_Access_Level (Target_Type)
6594 Make_Raise_Program_Error (Sloc (N),
6595 Reason => PE_Accessibility_Check_Failed));
6596 Set_Etype (N, Target_Type);
6600 -- Case of conversions of tagged types and access to tagged types
6602 -- When needed, that is to say when the expression is class-wide,
6603 -- Add runtime a tag check for (strict) downward conversion by using
6604 -- the membership test, generating:
6606 -- [constraint_error when Operand not in Target_Type'Class]
6608 -- or in the access type case
6610 -- [constraint_error
6611 -- when Operand /= null
6612 -- and then Operand.all not in
6613 -- Designated_Type (Target_Type)'Class]
6615 if (Is_Access_Type (Target_Type)
6616 and then Is_Tagged_Type (Designated_Type (Target_Type)))
6617 or else Is_Tagged_Type (Target_Type)
6619 -- Do not do any expansion in the access type case if the
6620 -- parent is a renaming, since this is an error situation
6621 -- which will be caught by Sem_Ch8, and the expansion can
6622 -- intefere with this error check.
6624 if Is_Access_Type (Target_Type)
6625 and then Is_Renamed_Object (N)
6630 -- Oherwise, proceed with processing tagged conversion
6633 Actual_Operand_Type : Entity_Id;
6634 Actual_Target_Type : Entity_Id;
6639 if Is_Access_Type (Target_Type) then
6640 Actual_Operand_Type := Designated_Type (Operand_Type);
6641 Actual_Target_Type := Designated_Type (Target_Type);
6644 Actual_Operand_Type := Operand_Type;
6645 Actual_Target_Type := Target_Type;
6648 if Is_Class_Wide_Type (Actual_Operand_Type)
6649 and then Root_Type (Actual_Operand_Type) /= Actual_Target_Type
6650 and then Is_Ancestor
6651 (Root_Type (Actual_Operand_Type),
6653 and then not Tag_Checks_Suppressed (Actual_Target_Type)
6655 -- The conversion is valid for any descendant of the
6658 Actual_Target_Type := Class_Wide_Type (Actual_Target_Type);
6660 if Is_Access_Type (Target_Type) then
6665 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6666 Right_Opnd => Make_Null (Loc)),
6671 Make_Explicit_Dereference (Loc,
6673 Duplicate_Subexpr_No_Checks (Operand)),
6675 New_Reference_To (Actual_Target_Type, Loc)));
6680 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6682 New_Reference_To (Actual_Target_Type, Loc));
6686 Make_Raise_Constraint_Error (Loc,
6688 Reason => CE_Tag_Check_Failed));
6694 Make_Unchecked_Type_Conversion (Loc,
6695 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
6696 Expression => Relocate_Node (Expression (N)));
6698 Analyze_And_Resolve (N, Target_Type);
6703 -- Case of other access type conversions
6705 elsif Is_Access_Type (Target_Type) then
6706 Apply_Constraint_Check (Operand, Target_Type);
6708 -- Case of conversions from a fixed-point type
6710 -- These conversions require special expansion and processing, found
6711 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
6712 -- set, since from a semantic point of view, these are simple integer
6713 -- conversions, which do not need further processing.
6715 elsif Is_Fixed_Point_Type (Operand_Type)
6716 and then not Conversion_OK (N)
6718 -- We should never see universal fixed at this case, since the
6719 -- expansion of the constituent divide or multiply should have
6720 -- eliminated the explicit mention of universal fixed.
6722 pragma Assert (Operand_Type /= Universal_Fixed);
6724 -- Check for special case of the conversion to universal real
6725 -- that occurs as a result of the use of a round attribute.
6726 -- In this case, the real type for the conversion is taken
6727 -- from the target type of the Round attribute and the
6728 -- result must be marked as rounded.
6730 if Target_Type = Universal_Real
6731 and then Nkind (Parent (N)) = N_Attribute_Reference
6732 and then Attribute_Name (Parent (N)) = Name_Round
6734 Set_Rounded_Result (N);
6735 Set_Etype (N, Etype (Parent (N)));
6738 -- Otherwise do correct fixed-conversion, but skip these if the
6739 -- Conversion_OK flag is set, because from a semantic point of
6740 -- view these are simple integer conversions needing no further
6741 -- processing (the backend will simply treat them as integers)
6743 if not Conversion_OK (N) then
6744 if Is_Fixed_Point_Type (Etype (N)) then
6745 Expand_Convert_Fixed_To_Fixed (N);
6748 elsif Is_Integer_Type (Etype (N)) then
6749 Expand_Convert_Fixed_To_Integer (N);
6752 pragma Assert (Is_Floating_Point_Type (Etype (N)));
6753 Expand_Convert_Fixed_To_Float (N);
6758 -- Case of conversions to a fixed-point type
6760 -- These conversions require special expansion and processing, found
6761 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
6762 -- is set, since from a semantic point of view, these are simple
6763 -- integer conversions, which do not need further processing.
6765 elsif Is_Fixed_Point_Type (Target_Type)
6766 and then not Conversion_OK (N)
6768 if Is_Integer_Type (Operand_Type) then
6769 Expand_Convert_Integer_To_Fixed (N);
6772 pragma Assert (Is_Floating_Point_Type (Operand_Type));
6773 Expand_Convert_Float_To_Fixed (N);
6777 -- Case of float-to-integer conversions
6779 -- We also handle float-to-fixed conversions with Conversion_OK set
6780 -- since semantically the fixed-point target is treated as though it
6781 -- were an integer in such cases.
6783 elsif Is_Floating_Point_Type (Operand_Type)
6785 (Is_Integer_Type (Target_Type)
6787 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
6789 -- Special processing required if the conversion is the expression
6790 -- of a Truncation attribute reference. In this case we replace:
6792 -- ityp (ftyp'Truncation (x))
6798 -- with the Float_Truncate flag set. This is clearly more efficient
6800 if Nkind (Operand) = N_Attribute_Reference
6801 and then Attribute_Name (Operand) = Name_Truncation
6804 Relocate_Node (First (Expressions (Operand))));
6805 Set_Float_Truncate (N, True);
6808 -- One more check here, gcc is still not able to do conversions of
6809 -- this type with proper overflow checking, and so gigi is doing an
6810 -- approximation of what is required by doing floating-point compares
6811 -- with the end-point. But that can lose precision in some cases, and
6812 -- give a wrong result. Converting the operand to Long_Long_Float is
6813 -- helpful, but still does not catch all cases with 64-bit integers
6814 -- on targets with only 64-bit floats ???
6816 if Do_Range_Check (Operand) then
6818 Make_Type_Conversion (Loc,
6820 New_Occurrence_Of (Standard_Long_Long_Float, Loc),
6822 Relocate_Node (Operand)));
6824 Set_Etype (Operand, Standard_Long_Long_Float);
6825 Enable_Range_Check (Operand);
6826 Set_Do_Range_Check (Expression (Operand), False);
6829 -- Case of array conversions
6831 -- Expansion of array conversions, add required length/range checks
6832 -- but only do this if there is no change of representation. For
6833 -- handling of this case, see Handle_Changed_Representation.
6835 elsif Is_Array_Type (Target_Type) then
6837 if Is_Constrained (Target_Type) then
6838 Apply_Length_Check (Operand, Target_Type);
6840 Apply_Range_Check (Operand, Target_Type);
6843 Handle_Changed_Representation;
6845 -- Case of conversions of discriminated types
6847 -- Add required discriminant checks if target is constrained. Again
6848 -- this change is skipped if we have a change of representation.
6850 elsif Has_Discriminants (Target_Type)
6851 and then Is_Constrained (Target_Type)
6853 Apply_Discriminant_Check (Operand, Target_Type);
6854 Handle_Changed_Representation;
6856 -- Case of all other record conversions. The only processing required
6857 -- is to check for a change of representation requiring the special
6858 -- assignment processing.
6860 elsif Is_Record_Type (Target_Type) then
6862 -- Ada 2005 (AI-216): Program_Error is raised when converting from
6863 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
6864 -- Union type if the operand lacks inferable discriminants.
6866 if Is_Derived_Type (Operand_Type)
6867 and then Is_Unchecked_Union (Base_Type (Operand_Type))
6868 and then not Is_Constrained (Target_Type)
6869 and then not Is_Unchecked_Union (Base_Type (Target_Type))
6870 and then not Has_Inferable_Discriminants (Operand)
6872 -- To prevent Gigi from generating illegal code, we make a
6873 -- Program_Error node, but we give it the target type of the
6877 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
6878 Reason => PE_Unchecked_Union_Restriction);
6881 Set_Etype (PE, Target_Type);
6886 Handle_Changed_Representation;
6889 -- Case of conversions of enumeration types
6891 elsif Is_Enumeration_Type (Target_Type) then
6893 -- Special processing is required if there is a change of
6894 -- representation (from enumeration representation clauses)
6896 if not Same_Representation (Target_Type, Operand_Type) then
6898 -- Convert: x(y) to x'val (ytyp'val (y))
6901 Make_Attribute_Reference (Loc,
6902 Prefix => New_Occurrence_Of (Target_Type, Loc),
6903 Attribute_Name => Name_Val,
6904 Expressions => New_List (
6905 Make_Attribute_Reference (Loc,
6906 Prefix => New_Occurrence_Of (Operand_Type, Loc),
6907 Attribute_Name => Name_Pos,
6908 Expressions => New_List (Operand)))));
6910 Analyze_And_Resolve (N, Target_Type);
6913 -- Case of conversions to floating-point
6915 elsif Is_Floating_Point_Type (Target_Type) then
6918 -- The remaining cases require no front end processing
6924 -- At this stage, either the conversion node has been transformed
6925 -- into some other equivalent expression, or left as a conversion
6926 -- that can be handled by Gigi. The conversions that Gigi can handle
6927 -- are the following:
6929 -- Conversions with no change of representation or type
6931 -- Numeric conversions involving integer values, floating-point
6932 -- values, and fixed-point values. Fixed-point values are allowed
6933 -- only if Conversion_OK is set, i.e. if the fixed-point values
6934 -- are to be treated as integers.
6936 -- No other conversions should be passed to Gigi
6938 -- Check: are these rules stated in sinfo??? if so, why restate here???
6940 -- The only remaining step is to generate a range check if we still
6941 -- have a type conversion at this stage and Do_Range_Check is set.
6942 -- For now we do this only for conversions of discrete types.
6944 if Nkind (N) = N_Type_Conversion
6945 and then Is_Discrete_Type (Etype (N))
6948 Expr : constant Node_Id := Expression (N);
6953 if Do_Range_Check (Expr)
6954 and then Is_Discrete_Type (Etype (Expr))
6956 Set_Do_Range_Check (Expr, False);
6958 -- Before we do a range check, we have to deal with treating
6959 -- a fixed-point operand as an integer. The way we do this
6960 -- is simply to do an unchecked conversion to an appropriate
6961 -- integer type large enough to hold the result.
6963 -- This code is not active yet, because we are only dealing
6964 -- with discrete types so far ???
6966 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
6967 and then Treat_Fixed_As_Integer (Expr)
6969 Ftyp := Base_Type (Etype (Expr));
6971 if Esize (Ftyp) >= Esize (Standard_Integer) then
6972 Ityp := Standard_Long_Long_Integer;
6974 Ityp := Standard_Integer;
6977 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
6980 -- Reset overflow flag, since the range check will include
6981 -- dealing with possible overflow, and generate the check
6982 -- If Address is either source or target type, suppress
6983 -- range check to avoid typing anomalies when it is a visible
6986 Set_Do_Overflow_Check (N, False);
6987 if not Is_Descendent_Of_Address (Etype (Expr))
6988 and then not Is_Descendent_Of_Address (Target_Type)
6990 Generate_Range_Check
6991 (Expr, Target_Type, CE_Range_Check_Failed);
6996 end Expand_N_Type_Conversion;
6998 -----------------------------------
6999 -- Expand_N_Unchecked_Expression --
7000 -----------------------------------
7002 -- Remove the unchecked expression node from the tree. It's job was simply
7003 -- to make sure that its constituent expression was handled with checks
7004 -- off, and now that that is done, we can remove it from the tree, and
7005 -- indeed must, since gigi does not expect to see these nodes.
7007 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
7008 Exp : constant Node_Id := Expression (N);
7011 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
7013 end Expand_N_Unchecked_Expression;
7015 ----------------------------------------
7016 -- Expand_N_Unchecked_Type_Conversion --
7017 ----------------------------------------
7019 -- If this cannot be handled by Gigi and we haven't already made
7020 -- a temporary for it, do it now.
7022 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
7023 Target_Type : constant Entity_Id := Etype (N);
7024 Operand : constant Node_Id := Expression (N);
7025 Operand_Type : constant Entity_Id := Etype (Operand);
7028 -- If we have a conversion of a compile time known value to a target
7029 -- type and the value is in range of the target type, then we can simply
7030 -- replace the construct by an integer literal of the correct type. We
7031 -- only apply this to integer types being converted. Possibly it may
7032 -- apply in other cases, but it is too much trouble to worry about.
7034 -- Note that we do not do this transformation if the Kill_Range_Check
7035 -- flag is set, since then the value may be outside the expected range.
7036 -- This happens in the Normalize_Scalars case.
7038 if Is_Integer_Type (Target_Type)
7039 and then Is_Integer_Type (Operand_Type)
7040 and then Compile_Time_Known_Value (Operand)
7041 and then not Kill_Range_Check (N)
7044 Val : constant Uint := Expr_Value (Operand);
7047 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
7049 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
7051 Val >= Expr_Value (Type_Low_Bound (Target_Type))
7053 Val <= Expr_Value (Type_High_Bound (Target_Type))
7055 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
7057 -- If Address is the target type, just set the type
7058 -- to avoid a spurious type error on the literal when
7059 -- Address is a visible integer type.
7061 if Is_Descendent_Of_Address (Target_Type) then
7062 Set_Etype (N, Target_Type);
7064 Analyze_And_Resolve (N, Target_Type);
7072 -- Nothing to do if conversion is safe
7074 if Safe_Unchecked_Type_Conversion (N) then
7078 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7079 -- flag indicates ??? -- more comments needed here)
7081 if Assignment_OK (N) then
7084 Force_Evaluation (N);
7086 end Expand_N_Unchecked_Type_Conversion;
7088 ----------------------------
7089 -- Expand_Record_Equality --
7090 ----------------------------
7092 -- For non-variant records, Equality is expanded when needed into:
7094 -- and then Lhs.Discr1 = Rhs.Discr1
7096 -- and then Lhs.Discrn = Rhs.Discrn
7097 -- and then Lhs.Cmp1 = Rhs.Cmp1
7099 -- and then Lhs.Cmpn = Rhs.Cmpn
7101 -- The expression is folded by the back-end for adjacent fields. This
7102 -- function is called for tagged record in only one occasion: for imple-
7103 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
7104 -- otherwise the primitive "=" is used directly.
7106 function Expand_Record_Equality
7111 Bodies : List_Id) return Node_Id
7113 Loc : constant Source_Ptr := Sloc (Nod);
7118 First_Time : Boolean := True;
7120 function Suitable_Element (C : Entity_Id) return Entity_Id;
7121 -- Return the first field to compare beginning with C, skipping the
7122 -- inherited components.
7124 ----------------------
7125 -- Suitable_Element --
7126 ----------------------
7128 function Suitable_Element (C : Entity_Id) return Entity_Id is
7133 elsif Ekind (C) /= E_Discriminant
7134 and then Ekind (C) /= E_Component
7136 return Suitable_Element (Next_Entity (C));
7138 elsif Is_Tagged_Type (Typ)
7139 and then C /= Original_Record_Component (C)
7141 return Suitable_Element (Next_Entity (C));
7143 elsif Chars (C) = Name_uController
7144 or else Chars (C) = Name_uTag
7146 return Suitable_Element (Next_Entity (C));
7151 end Suitable_Element;
7153 -- Start of processing for Expand_Record_Equality
7156 -- Generates the following code: (assuming that Typ has one Discr and
7157 -- component C2 is also a record)
7160 -- and then Lhs.Discr1 = Rhs.Discr1
7161 -- and then Lhs.C1 = Rhs.C1
7162 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
7164 -- and then Lhs.Cmpn = Rhs.Cmpn
7166 Result := New_Reference_To (Standard_True, Loc);
7167 C := Suitable_Element (First_Entity (Typ));
7169 while Present (C) loop
7177 First_Time := False;
7181 New_Lhs := New_Copy_Tree (Lhs);
7182 New_Rhs := New_Copy_Tree (Rhs);
7186 Expand_Composite_Equality (Nod, Etype (C),
7188 Make_Selected_Component (Loc,
7190 Selector_Name => New_Reference_To (C, Loc)),
7192 Make_Selected_Component (Loc,
7194 Selector_Name => New_Reference_To (C, Loc)),
7197 -- If some (sub)component is an unchecked_union, the whole
7198 -- operation will raise program error.
7200 if Nkind (Check) = N_Raise_Program_Error then
7202 Set_Etype (Result, Standard_Boolean);
7207 Left_Opnd => Result,
7208 Right_Opnd => Check);
7212 C := Suitable_Element (Next_Entity (C));
7216 end Expand_Record_Equality;
7218 -------------------------------------
7219 -- Fixup_Universal_Fixed_Operation --
7220 -------------------------------------
7222 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
7223 Conv : constant Node_Id := Parent (N);
7226 -- We must have a type conversion immediately above us
7228 pragma Assert (Nkind (Conv) = N_Type_Conversion);
7230 -- Normally the type conversion gives our target type. The exception
7231 -- occurs in the case of the Round attribute, where the conversion
7232 -- will be to universal real, and our real type comes from the Round
7233 -- attribute (as well as an indication that we must round the result)
7235 if Nkind (Parent (Conv)) = N_Attribute_Reference
7236 and then Attribute_Name (Parent (Conv)) = Name_Round
7238 Set_Etype (N, Etype (Parent (Conv)));
7239 Set_Rounded_Result (N);
7241 -- Normal case where type comes from conversion above us
7244 Set_Etype (N, Etype (Conv));
7246 end Fixup_Universal_Fixed_Operation;
7248 ------------------------------
7249 -- Get_Allocator_Final_List --
7250 ------------------------------
7252 function Get_Allocator_Final_List
7255 PtrT : Entity_Id) return Entity_Id
7257 Loc : constant Source_Ptr := Sloc (N);
7259 Owner : Entity_Id := PtrT;
7260 -- The entity whose finalisation list must be used to attach the
7261 -- allocated object.
7264 if Ekind (PtrT) = E_Anonymous_Access_Type then
7265 if Nkind (Associated_Node_For_Itype (PtrT))
7266 in N_Subprogram_Specification
7268 -- If the context is an access parameter, we need to create
7269 -- a non-anonymous access type in order to have a usable
7270 -- final list, because there is otherwise no pool to which
7271 -- the allocated object can belong. We create both the type
7272 -- and the finalization chain here, because freezing an
7273 -- internal type does not create such a chain. The Final_Chain
7274 -- that is thus created is shared by the access parameter.
7276 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
7278 Make_Full_Type_Declaration (Loc,
7279 Defining_Identifier => Owner,
7281 Make_Access_To_Object_Definition (Loc,
7282 Subtype_Indication =>
7283 New_Occurrence_Of (T, Loc))));
7285 Build_Final_List (N, Owner);
7286 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
7289 -- Case of an access discriminant, or (Ada 2005) of
7290 -- an anonymous access component: find the final list
7291 -- associated with the scope of the type.
7293 Owner := Scope (PtrT);
7297 return Find_Final_List (Owner);
7298 end Get_Allocator_Final_List;
7300 ---------------------------------
7301 -- Has_Inferable_Discriminants --
7302 ---------------------------------
7304 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
7306 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
7307 -- Determines whether the left-most prefix of a selected component is a
7308 -- formal parameter in a subprogram. Assumes N is a selected component.
7310 --------------------------------
7311 -- Prefix_Is_Formal_Parameter --
7312 --------------------------------
7314 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
7315 Sel_Comp : Node_Id := N;
7318 -- Move to the left-most prefix by climbing up the tree
7320 while Present (Parent (Sel_Comp))
7321 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
7323 Sel_Comp := Parent (Sel_Comp);
7326 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
7327 end Prefix_Is_Formal_Parameter;
7329 -- Start of processing for Has_Inferable_Discriminants
7332 -- For identifiers and indexed components, it is sufficent to have a
7333 -- constrained Unchecked_Union nominal subtype.
7335 if Nkind (N) = N_Identifier
7337 Nkind (N) = N_Indexed_Component
7339 return Is_Unchecked_Union (Base_Type (Etype (N)))
7341 Is_Constrained (Etype (N));
7343 -- For selected components, the subtype of the selector must be a
7344 -- constrained Unchecked_Union. If the component is subject to a
7345 -- per-object constraint, then the enclosing object must have inferable
7348 elsif Nkind (N) = N_Selected_Component then
7349 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
7351 -- A small hack. If we have a per-object constrained selected
7352 -- component of a formal parameter, return True since we do not
7353 -- know the actual parameter association yet.
7355 if Prefix_Is_Formal_Parameter (N) then
7359 -- Otherwise, check the enclosing object and the selector
7361 return Has_Inferable_Discriminants (Prefix (N))
7363 Has_Inferable_Discriminants (Selector_Name (N));
7366 -- The call to Has_Inferable_Discriminants will determine whether
7367 -- the selector has a constrained Unchecked_Union nominal type.
7369 return Has_Inferable_Discriminants (Selector_Name (N));
7371 -- A qualified expression has inferable discriminants if its subtype
7372 -- mark is a constrained Unchecked_Union subtype.
7374 elsif Nkind (N) = N_Qualified_Expression then
7375 return Is_Unchecked_Union (Subtype_Mark (N))
7377 Is_Constrained (Subtype_Mark (N));
7382 end Has_Inferable_Discriminants;
7384 -------------------------------
7385 -- Insert_Dereference_Action --
7386 -------------------------------
7388 procedure Insert_Dereference_Action (N : Node_Id) is
7389 Loc : constant Source_Ptr := Sloc (N);
7390 Typ : constant Entity_Id := Etype (N);
7391 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
7392 Pnod : constant Node_Id := Parent (N);
7394 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
7395 -- Return true if type of P is derived from Checked_Pool;
7397 -----------------------------
7398 -- Is_Checked_Storage_Pool --
7399 -----------------------------
7401 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
7410 while T /= Etype (T) loop
7411 if Is_RTE (T, RE_Checked_Pool) then
7419 end Is_Checked_Storage_Pool;
7421 -- Start of processing for Insert_Dereference_Action
7424 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
7426 if not (Is_Checked_Storage_Pool (Pool)
7427 and then Comes_From_Source (Original_Node (Pnod)))
7433 Make_Procedure_Call_Statement (Loc,
7434 Name => New_Reference_To (
7435 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
7437 Parameter_Associations => New_List (
7441 New_Reference_To (Pool, Loc),
7443 -- Storage_Address. We use the attribute Pool_Address,
7444 -- which uses the pointer itself to find the address of
7445 -- the object, and which handles unconstrained arrays
7446 -- properly by computing the address of the template.
7447 -- i.e. the correct address of the corresponding allocation.
7449 Make_Attribute_Reference (Loc,
7450 Prefix => Duplicate_Subexpr_Move_Checks (N),
7451 Attribute_Name => Name_Pool_Address),
7453 -- Size_In_Storage_Elements
7455 Make_Op_Divide (Loc,
7457 Make_Attribute_Reference (Loc,
7459 Make_Explicit_Dereference (Loc,
7460 Duplicate_Subexpr_Move_Checks (N)),
7461 Attribute_Name => Name_Size),
7463 Make_Integer_Literal (Loc, System_Storage_Unit)),
7467 Make_Attribute_Reference (Loc,
7469 Make_Explicit_Dereference (Loc,
7470 Duplicate_Subexpr_Move_Checks (N)),
7471 Attribute_Name => Name_Alignment))));
7474 when RE_Not_Available =>
7476 end Insert_Dereference_Action;
7478 ------------------------------
7479 -- Make_Array_Comparison_Op --
7480 ------------------------------
7482 -- This is a hand-coded expansion of the following generic function:
7485 -- type elem is (<>);
7486 -- type index is (<>);
7487 -- type a is array (index range <>) of elem;
7489 -- function Gnnn (X : a; Y: a) return boolean is
7490 -- J : index := Y'first;
7493 -- if X'length = 0 then
7496 -- elsif Y'length = 0 then
7500 -- for I in X'range loop
7501 -- if X (I) = Y (J) then
7502 -- if J = Y'last then
7505 -- J := index'succ (J);
7509 -- return X (I) > Y (J);
7513 -- return X'length > Y'length;
7517 -- Note that since we are essentially doing this expansion by hand, we
7518 -- do not need to generate an actual or formal generic part, just the
7519 -- instantiated function itself.
7521 function Make_Array_Comparison_Op
7523 Nod : Node_Id) return Node_Id
7525 Loc : constant Source_Ptr := Sloc (Nod);
7527 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
7528 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
7529 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
7530 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7532 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
7534 Loop_Statement : Node_Id;
7535 Loop_Body : Node_Id;
7538 Final_Expr : Node_Id;
7539 Func_Body : Node_Id;
7540 Func_Name : Entity_Id;
7546 -- if J = Y'last then
7549 -- J := index'succ (J);
7553 Make_Implicit_If_Statement (Nod,
7556 Left_Opnd => New_Reference_To (J, Loc),
7558 Make_Attribute_Reference (Loc,
7559 Prefix => New_Reference_To (Y, Loc),
7560 Attribute_Name => Name_Last)),
7562 Then_Statements => New_List (
7563 Make_Exit_Statement (Loc)),
7567 Make_Assignment_Statement (Loc,
7568 Name => New_Reference_To (J, Loc),
7570 Make_Attribute_Reference (Loc,
7571 Prefix => New_Reference_To (Index, Loc),
7572 Attribute_Name => Name_Succ,
7573 Expressions => New_List (New_Reference_To (J, Loc))))));
7575 -- if X (I) = Y (J) then
7578 -- return X (I) > Y (J);
7582 Make_Implicit_If_Statement (Nod,
7586 Make_Indexed_Component (Loc,
7587 Prefix => New_Reference_To (X, Loc),
7588 Expressions => New_List (New_Reference_To (I, Loc))),
7591 Make_Indexed_Component (Loc,
7592 Prefix => New_Reference_To (Y, Loc),
7593 Expressions => New_List (New_Reference_To (J, Loc)))),
7595 Then_Statements => New_List (Inner_If),
7597 Else_Statements => New_List (
7598 Make_Return_Statement (Loc,
7602 Make_Indexed_Component (Loc,
7603 Prefix => New_Reference_To (X, Loc),
7604 Expressions => New_List (New_Reference_To (I, Loc))),
7607 Make_Indexed_Component (Loc,
7608 Prefix => New_Reference_To (Y, Loc),
7609 Expressions => New_List (
7610 New_Reference_To (J, Loc)))))));
7612 -- for I in X'range loop
7617 Make_Implicit_Loop_Statement (Nod,
7618 Identifier => Empty,
7621 Make_Iteration_Scheme (Loc,
7622 Loop_Parameter_Specification =>
7623 Make_Loop_Parameter_Specification (Loc,
7624 Defining_Identifier => I,
7625 Discrete_Subtype_Definition =>
7626 Make_Attribute_Reference (Loc,
7627 Prefix => New_Reference_To (X, Loc),
7628 Attribute_Name => Name_Range))),
7630 Statements => New_List (Loop_Body));
7632 -- if X'length = 0 then
7634 -- elsif Y'length = 0 then
7637 -- for ... loop ... end loop;
7638 -- return X'length > Y'length;
7642 Make_Attribute_Reference (Loc,
7643 Prefix => New_Reference_To (X, Loc),
7644 Attribute_Name => Name_Length);
7647 Make_Attribute_Reference (Loc,
7648 Prefix => New_Reference_To (Y, Loc),
7649 Attribute_Name => Name_Length);
7653 Left_Opnd => Length1,
7654 Right_Opnd => Length2);
7657 Make_Implicit_If_Statement (Nod,
7661 Make_Attribute_Reference (Loc,
7662 Prefix => New_Reference_To (X, Loc),
7663 Attribute_Name => Name_Length),
7665 Make_Integer_Literal (Loc, 0)),
7669 Make_Return_Statement (Loc,
7670 Expression => New_Reference_To (Standard_False, Loc))),
7672 Elsif_Parts => New_List (
7673 Make_Elsif_Part (Loc,
7677 Make_Attribute_Reference (Loc,
7678 Prefix => New_Reference_To (Y, Loc),
7679 Attribute_Name => Name_Length),
7681 Make_Integer_Literal (Loc, 0)),
7685 Make_Return_Statement (Loc,
7686 Expression => New_Reference_To (Standard_True, Loc))))),
7688 Else_Statements => New_List (
7690 Make_Return_Statement (Loc,
7691 Expression => Final_Expr)));
7695 Formals := New_List (
7696 Make_Parameter_Specification (Loc,
7697 Defining_Identifier => X,
7698 Parameter_Type => New_Reference_To (Typ, Loc)),
7700 Make_Parameter_Specification (Loc,
7701 Defining_Identifier => Y,
7702 Parameter_Type => New_Reference_To (Typ, Loc)));
7704 -- function Gnnn (...) return boolean is
7705 -- J : index := Y'first;
7710 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
7713 Make_Subprogram_Body (Loc,
7715 Make_Function_Specification (Loc,
7716 Defining_Unit_Name => Func_Name,
7717 Parameter_Specifications => Formals,
7718 Subtype_Mark => New_Reference_To (Standard_Boolean, Loc)),
7720 Declarations => New_List (
7721 Make_Object_Declaration (Loc,
7722 Defining_Identifier => J,
7723 Object_Definition => New_Reference_To (Index, Loc),
7725 Make_Attribute_Reference (Loc,
7726 Prefix => New_Reference_To (Y, Loc),
7727 Attribute_Name => Name_First))),
7729 Handled_Statement_Sequence =>
7730 Make_Handled_Sequence_Of_Statements (Loc,
7731 Statements => New_List (If_Stat)));
7735 end Make_Array_Comparison_Op;
7737 ---------------------------
7738 -- Make_Boolean_Array_Op --
7739 ---------------------------
7741 -- For logical operations on boolean arrays, expand in line the
7742 -- following, replacing 'and' with 'or' or 'xor' where needed:
7744 -- function Annn (A : typ; B: typ) return typ is
7747 -- for J in A'range loop
7748 -- C (J) := A (J) op B (J);
7753 -- Here typ is the boolean array type
7755 function Make_Boolean_Array_Op
7757 N : Node_Id) return Node_Id
7759 Loc : constant Source_Ptr := Sloc (N);
7761 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7762 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
7763 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
7764 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7772 Func_Name : Entity_Id;
7773 Func_Body : Node_Id;
7774 Loop_Statement : Node_Id;
7778 Make_Indexed_Component (Loc,
7779 Prefix => New_Reference_To (A, Loc),
7780 Expressions => New_List (New_Reference_To (J, Loc)));
7783 Make_Indexed_Component (Loc,
7784 Prefix => New_Reference_To (B, Loc),
7785 Expressions => New_List (New_Reference_To (J, Loc)));
7788 Make_Indexed_Component (Loc,
7789 Prefix => New_Reference_To (C, Loc),
7790 Expressions => New_List (New_Reference_To (J, Loc)));
7792 if Nkind (N) = N_Op_And then
7798 elsif Nkind (N) = N_Op_Or then
7812 Make_Implicit_Loop_Statement (N,
7813 Identifier => Empty,
7816 Make_Iteration_Scheme (Loc,
7817 Loop_Parameter_Specification =>
7818 Make_Loop_Parameter_Specification (Loc,
7819 Defining_Identifier => J,
7820 Discrete_Subtype_Definition =>
7821 Make_Attribute_Reference (Loc,
7822 Prefix => New_Reference_To (A, Loc),
7823 Attribute_Name => Name_Range))),
7825 Statements => New_List (
7826 Make_Assignment_Statement (Loc,
7828 Expression => Op)));
7830 Formals := New_List (
7831 Make_Parameter_Specification (Loc,
7832 Defining_Identifier => A,
7833 Parameter_Type => New_Reference_To (Typ, Loc)),
7835 Make_Parameter_Specification (Loc,
7836 Defining_Identifier => B,
7837 Parameter_Type => New_Reference_To (Typ, Loc)));
7840 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
7841 Set_Is_Inlined (Func_Name);
7844 Make_Subprogram_Body (Loc,
7846 Make_Function_Specification (Loc,
7847 Defining_Unit_Name => Func_Name,
7848 Parameter_Specifications => Formals,
7849 Subtype_Mark => New_Reference_To (Typ, Loc)),
7851 Declarations => New_List (
7852 Make_Object_Declaration (Loc,
7853 Defining_Identifier => C,
7854 Object_Definition => New_Reference_To (Typ, Loc))),
7856 Handled_Statement_Sequence =>
7857 Make_Handled_Sequence_Of_Statements (Loc,
7858 Statements => New_List (
7860 Make_Return_Statement (Loc,
7861 Expression => New_Reference_To (C, Loc)))));
7864 end Make_Boolean_Array_Op;
7866 ------------------------
7867 -- Rewrite_Comparison --
7868 ------------------------
7870 procedure Rewrite_Comparison (N : Node_Id) is
7871 Typ : constant Entity_Id := Etype (N);
7872 Op1 : constant Node_Id := Left_Opnd (N);
7873 Op2 : constant Node_Id := Right_Opnd (N);
7875 Res : constant Compare_Result := Compile_Time_Compare (Op1, Op2);
7876 -- Res indicates if compare outcome can be determined at compile time
7878 True_Result : Boolean;
7879 False_Result : Boolean;
7882 case N_Op_Compare (Nkind (N)) is
7884 True_Result := Res = EQ;
7885 False_Result := Res = LT or else Res = GT or else Res = NE;
7888 True_Result := Res in Compare_GE;
7889 False_Result := Res = LT;
7892 True_Result := Res = GT;
7893 False_Result := Res in Compare_LE;
7896 True_Result := Res = LT;
7897 False_Result := Res in Compare_GE;
7900 True_Result := Res in Compare_LE;
7901 False_Result := Res = GT;
7904 True_Result := Res = NE;
7905 False_Result := Res = LT or else Res = GT or else Res = EQ;
7910 Convert_To (Typ, New_Occurrence_Of (Standard_True, Sloc (N))));
7911 Analyze_And_Resolve (N, Typ);
7912 Warn_On_Known_Condition (N);
7914 elsif False_Result then
7916 Convert_To (Typ, New_Occurrence_Of (Standard_False, Sloc (N))));
7917 Analyze_And_Resolve (N, Typ);
7918 Warn_On_Known_Condition (N);
7920 end Rewrite_Comparison;
7922 ----------------------------
7923 -- Safe_In_Place_Array_Op --
7924 ----------------------------
7926 function Safe_In_Place_Array_Op
7929 Op2 : Node_Id) return Boolean
7933 function Is_Safe_Operand (Op : Node_Id) return Boolean;
7934 -- Operand is safe if it cannot overlap part of the target of the
7935 -- operation. If the operand and the target are identical, the operand
7936 -- is safe. The operand can be empty in the case of negation.
7938 function Is_Unaliased (N : Node_Id) return Boolean;
7939 -- Check that N is a stand-alone entity
7945 function Is_Unaliased (N : Node_Id) return Boolean is
7949 and then No (Address_Clause (Entity (N)))
7950 and then No (Renamed_Object (Entity (N)));
7953 ---------------------
7954 -- Is_Safe_Operand --
7955 ---------------------
7957 function Is_Safe_Operand (Op : Node_Id) return Boolean is
7962 elsif Is_Entity_Name (Op) then
7963 return Is_Unaliased (Op);
7965 elsif Nkind (Op) = N_Indexed_Component
7966 or else Nkind (Op) = N_Selected_Component
7968 return Is_Unaliased (Prefix (Op));
7970 elsif Nkind (Op) = N_Slice then
7972 Is_Unaliased (Prefix (Op))
7973 and then Entity (Prefix (Op)) /= Target;
7975 elsif Nkind (Op) = N_Op_Not then
7976 return Is_Safe_Operand (Right_Opnd (Op));
7981 end Is_Safe_Operand;
7983 -- Start of processing for Is_Safe_In_Place_Array_Op
7986 -- We skip this processing if the component size is not the
7987 -- same as a system storage unit (since at least for NOT
7988 -- this would cause problems).
7990 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
7993 -- Cannot do in place stuff on Java_VM since cannot pass addresses
7998 -- Cannot do in place stuff if non-standard Boolean representation
8000 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
8003 elsif not Is_Unaliased (Lhs) then
8006 Target := Entity (Lhs);
8009 Is_Safe_Operand (Op1)
8010 and then Is_Safe_Operand (Op2);
8012 end Safe_In_Place_Array_Op;
8014 -----------------------
8015 -- Tagged_Membership --
8016 -----------------------
8018 -- There are two different cases to consider depending on whether
8019 -- the right operand is a class-wide type or not. If not we just
8020 -- compare the actual tag of the left expr to the target type tag:
8022 -- Left_Expr.Tag = Right_Type'Tag;
8024 -- If it is a class-wide type we use the RT function CW_Membership which
8025 -- is usually implemented by looking in the ancestor tables contained in
8026 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
8028 function Tagged_Membership (N : Node_Id) return Node_Id is
8029 Left : constant Node_Id := Left_Opnd (N);
8030 Right : constant Node_Id := Right_Opnd (N);
8031 Loc : constant Source_Ptr := Sloc (N);
8033 Left_Type : Entity_Id;
8034 Right_Type : Entity_Id;
8038 Left_Type := Etype (Left);
8039 Right_Type := Etype (Right);
8041 if Is_Class_Wide_Type (Left_Type) then
8042 Left_Type := Root_Type (Left_Type);
8046 Make_Selected_Component (Loc,
8047 Prefix => Relocate_Node (Left),
8049 New_Reference_To (First_Tag_Component (Left_Type), Loc));
8051 if Is_Class_Wide_Type (Right_Type) then
8053 -- Ada 2005 (AI-251): Class-wide applied to interfaces
8055 if Is_Interface (Etype (Class_Wide_Type (Right_Type))) then
8057 Make_Function_Call (Loc,
8058 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
8059 Parameter_Associations => New_List (
8060 Make_Attribute_Reference (Loc,
8062 Attribute_Name => Name_Address),
8065 (Access_Disp_Table (Root_Type (Right_Type)))),
8068 -- Ada 95: Normal case
8072 Make_Function_Call (Loc,
8073 Name => New_Occurrence_Of (RTE (RE_CW_Membership), Loc),
8074 Parameter_Associations => New_List (
8078 (Access_Disp_Table (Root_Type (Right_Type)))),
8085 Left_Opnd => Obj_Tag,
8088 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
8091 end Tagged_Membership;
8093 ------------------------------
8094 -- Unary_Op_Validity_Checks --
8095 ------------------------------
8097 procedure Unary_Op_Validity_Checks (N : Node_Id) is
8099 if Validity_Checks_On and Validity_Check_Operands then
8100 Ensure_Valid (Right_Opnd (N));
8102 end Unary_Op_Validity_Checks;