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 Freeze; use Freeze;
42 with Hostparm; use Hostparm;
43 with Inline; use Inline;
44 with Nlists; use Nlists;
45 with Nmake; use Nmake;
47 with Rtsfind; use Rtsfind;
49 with Sem_Cat; use Sem_Cat;
50 with Sem_Ch3; use Sem_Ch3;
51 with Sem_Ch13; use Sem_Ch13;
52 with Sem_Eval; use Sem_Eval;
53 with Sem_Res; use Sem_Res;
54 with Sem_Type; use Sem_Type;
55 with Sem_Util; use Sem_Util;
56 with Sem_Warn; use Sem_Warn;
57 with Sinfo; use Sinfo;
58 with Snames; use Snames;
59 with Stand; use Stand;
60 with Targparm; use Targparm;
61 with Tbuild; use Tbuild;
62 with Ttypes; use Ttypes;
63 with Uintp; use Uintp;
64 with Urealp; use Urealp;
65 with Validsw; use Validsw;
67 package body Exp_Ch4 is
69 -----------------------
70 -- Local Subprograms --
71 -----------------------
73 procedure Binary_Op_Validity_Checks (N : Node_Id);
74 pragma Inline (Binary_Op_Validity_Checks);
75 -- Performs validity checks for a binary operator
77 procedure Build_Boolean_Array_Proc_Call
81 -- If an boolean array assignment can be done in place, build call to
82 -- corresponding library procedure.
84 procedure Expand_Allocator_Expression (N : Node_Id);
85 -- Subsidiary to Expand_N_Allocator, for the case when the expression
86 -- is a qualified expression or an aggregate.
88 procedure Expand_Array_Comparison (N : Node_Id);
89 -- This routine handles expansion of the comparison operators (N_Op_Lt,
90 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
91 -- code for these operators is similar, differing only in the details of
92 -- the actual comparison call that is made. Special processing (call a
95 function Expand_Array_Equality
100 Typ : Entity_Id) return Node_Id;
101 -- Expand an array equality into a call to a function implementing this
102 -- equality, and a call to it. Loc is the location for the generated
103 -- nodes. Lhs and Rhs are the array expressions to be compared.
104 -- Bodies is a list on which to attach bodies of local functions that
105 -- are created in the process. It is the responsibility of the
106 -- caller to insert those bodies at the right place. Nod provides
107 -- the Sloc value for the generated code. Normally the types used
108 -- for the generated equality routine are taken from Lhs and Rhs.
109 -- However, in some situations of generated code, the Etype fields
110 -- of Lhs and Rhs are not set yet. In such cases, Typ supplies the
111 -- type to be used for the formal parameters.
113 procedure Expand_Boolean_Operator (N : Node_Id);
114 -- Common expansion processing for Boolean operators (And, Or, Xor)
115 -- for the case of array type arguments.
117 function Expand_Composite_Equality
122 Bodies : List_Id) return Node_Id;
123 -- Local recursive function used to expand equality for nested
124 -- composite types. Used by Expand_Record/Array_Equality, Bodies
125 -- is a list on which to attach bodies of local functions that are
126 -- created in the process. This is the responsability of the caller
127 -- to insert those bodies at the right place. Nod provides the Sloc
128 -- value for generated code. Lhs and Rhs are the left and right sides
129 -- for the comparison, and Typ is the type of the arrays to compare.
131 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id);
132 -- This routine handles expansion of concatenation operations, where
133 -- N is the N_Op_Concat node being expanded and Operands is the list
134 -- of operands (at least two are present). The caller has dealt with
135 -- converting any singleton operands into singleton aggregates.
137 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id);
138 -- Routine to expand concatenation of 2-5 operands (in the list Operands)
139 -- and replace node Cnode with the result of the contatenation. If there
140 -- are two operands, they can be string or character. If there are more
141 -- than two operands, then are always of type string (i.e. the caller has
142 -- already converted character operands to strings in this case).
144 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
145 -- N is either an N_Op_Divide or N_Op_Multiply node whose result is
146 -- universal fixed. We do not have such a type at runtime, so the
147 -- purpose of this routine is to find the real type by looking up
148 -- the tree. We also determine if the operation must be rounded.
150 function Get_Allocator_Final_List
153 PtrT : Entity_Id) return Entity_Id;
154 -- If the designated type is controlled, build final_list expression
155 -- for created object. If context is an access parameter, create a
156 -- local access type to have a usable finalization list.
158 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
159 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
160 -- discriminants if it has a constrained nominal type, unless the object
161 -- is a component of an enclosing Unchecked_Union object that is subject
162 -- to a per-object constraint and the enclosing object lacks inferable
165 -- An expression of an Unchecked_Union type has inferable discriminants
166 -- if it is either a name of an object with inferable discriminants or a
167 -- qualified expression whose subtype mark denotes a constrained subtype.
169 procedure Insert_Dereference_Action (N : Node_Id);
170 -- N is an expression whose type is an access. When the type of the
171 -- associated storage pool is derived from Checked_Pool, generate a
172 -- call to the 'Dereference' primitive operation.
174 function Make_Array_Comparison_Op
176 Nod : Node_Id) return Node_Id;
177 -- Comparisons between arrays are expanded in line. This function
178 -- produces the body of the implementation of (a > b), where a and b
179 -- are one-dimensional arrays of some discrete type. The original
180 -- node is then expanded into the appropriate call to this function.
181 -- Nod provides the Sloc value for the generated code.
183 function Make_Boolean_Array_Op
185 N : Node_Id) return Node_Id;
186 -- Boolean operations on boolean arrays are expanded in line. This
187 -- function produce the body for the node N, which is (a and b),
188 -- (a or b), or (a xor b). It is used only the normal case and not
189 -- the packed case. The type involved, Typ, is the Boolean array type,
190 -- and the logical operations in the body are simple boolean operations.
191 -- Note that Typ is always a constrained type (the caller has ensured
192 -- this by using Convert_To_Actual_Subtype if necessary).
194 procedure Rewrite_Comparison (N : Node_Id);
195 -- if N is the node for a comparison whose outcome can be determined at
196 -- compile time, then the node N can be rewritten with True or False. If
197 -- the outcome cannot be determined at compile time, the call has no
198 -- effect. If N is a type conversion, then this processing is applied to
199 -- its expression. If N is neither comparison nor a type conversion, the
200 -- call has no effect.
202 function Tagged_Membership (N : Node_Id) return Node_Id;
203 -- Construct the expression corresponding to the tagged membership test.
204 -- Deals with a second operand being (or not) a class-wide type.
206 function Safe_In_Place_Array_Op
209 Op2 : Node_Id) return Boolean;
210 -- In the context of an assignment, where the right-hand side is a
211 -- boolean operation on arrays, check whether operation can be performed
214 procedure Unary_Op_Validity_Checks (N : Node_Id);
215 pragma Inline (Unary_Op_Validity_Checks);
216 -- Performs validity checks for a unary operator
218 -------------------------------
219 -- Binary_Op_Validity_Checks --
220 -------------------------------
222 procedure Binary_Op_Validity_Checks (N : Node_Id) is
224 if Validity_Checks_On and Validity_Check_Operands then
225 Ensure_Valid (Left_Opnd (N));
226 Ensure_Valid (Right_Opnd (N));
228 end Binary_Op_Validity_Checks;
230 ------------------------------------
231 -- Build_Boolean_Array_Proc_Call --
232 ------------------------------------
234 procedure Build_Boolean_Array_Proc_Call
239 Loc : constant Source_Ptr := Sloc (N);
240 Kind : constant Node_Kind := Nkind (Expression (N));
241 Target : constant Node_Id :=
242 Make_Attribute_Reference (Loc,
244 Attribute_Name => Name_Address);
246 Arg1 : constant Node_Id := Op1;
247 Arg2 : Node_Id := Op2;
249 Proc_Name : Entity_Id;
252 if Kind = N_Op_Not then
253 if Nkind (Op1) in N_Binary_Op then
255 -- Use negated version of the binary operators
257 if Nkind (Op1) = N_Op_And then
258 Proc_Name := RTE (RE_Vector_Nand);
260 elsif Nkind (Op1) = N_Op_Or then
261 Proc_Name := RTE (RE_Vector_Nor);
263 else pragma Assert (Nkind (Op1) = N_Op_Xor);
264 Proc_Name := RTE (RE_Vector_Xor);
268 Make_Procedure_Call_Statement (Loc,
269 Name => New_Occurrence_Of (Proc_Name, Loc),
271 Parameter_Associations => New_List (
273 Make_Attribute_Reference (Loc,
274 Prefix => Left_Opnd (Op1),
275 Attribute_Name => Name_Address),
277 Make_Attribute_Reference (Loc,
278 Prefix => Right_Opnd (Op1),
279 Attribute_Name => Name_Address),
281 Make_Attribute_Reference (Loc,
282 Prefix => Left_Opnd (Op1),
283 Attribute_Name => Name_Length)));
286 Proc_Name := RTE (RE_Vector_Not);
289 Make_Procedure_Call_Statement (Loc,
290 Name => New_Occurrence_Of (Proc_Name, Loc),
291 Parameter_Associations => New_List (
294 Make_Attribute_Reference (Loc,
296 Attribute_Name => Name_Address),
298 Make_Attribute_Reference (Loc,
300 Attribute_Name => Name_Length)));
304 -- We use the following equivalences:
306 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
307 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
308 -- (not X) xor (not Y) = X xor Y
309 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
311 if Nkind (Op1) = N_Op_Not then
312 if Kind = N_Op_And then
313 Proc_Name := RTE (RE_Vector_Nor);
315 elsif Kind = N_Op_Or then
316 Proc_Name := RTE (RE_Vector_Nand);
319 Proc_Name := RTE (RE_Vector_Xor);
323 if Kind = N_Op_And then
324 Proc_Name := RTE (RE_Vector_And);
326 elsif Kind = N_Op_Or then
327 Proc_Name := RTE (RE_Vector_Or);
329 elsif Nkind (Op2) = N_Op_Not then
330 Proc_Name := RTE (RE_Vector_Nxor);
331 Arg2 := Right_Opnd (Op2);
334 Proc_Name := RTE (RE_Vector_Xor);
339 Make_Procedure_Call_Statement (Loc,
340 Name => New_Occurrence_Of (Proc_Name, Loc),
341 Parameter_Associations => New_List (
343 Make_Attribute_Reference (Loc,
345 Attribute_Name => Name_Address),
346 Make_Attribute_Reference (Loc,
348 Attribute_Name => Name_Address),
349 Make_Attribute_Reference (Loc,
351 Attribute_Name => Name_Length)));
354 Rewrite (N, Call_Node);
358 when RE_Not_Available =>
360 end Build_Boolean_Array_Proc_Call;
362 ---------------------------------
363 -- Expand_Allocator_Expression --
364 ---------------------------------
366 procedure Expand_Allocator_Expression (N : Node_Id) is
367 Loc : constant Source_Ptr := Sloc (N);
368 Exp : constant Node_Id := Expression (Expression (N));
369 Indic : constant Node_Id := Subtype_Mark (Expression (N));
370 PtrT : constant Entity_Id := Etype (N);
371 DesigT : constant Entity_Id := Designated_Type (PtrT);
372 T : constant Entity_Id := Entity (Indic);
377 TagT : Entity_Id := Empty;
378 -- Type used as source for tag assignment
380 TagR : Node_Id := Empty;
381 -- Target reference for tag assignment
383 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
385 Tag_Assign : Node_Id;
389 if Is_Tagged_Type (T) or else Controlled_Type (T) then
391 -- Actions inserted before:
392 -- Temp : constant ptr_T := new T'(Expression);
393 -- <no CW> Temp._tag := T'tag;
394 -- <CTRL> Adjust (Finalizable (Temp.all));
395 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
397 -- We analyze by hand the new internal allocator to avoid
398 -- any recursion and inappropriate call to Initialize
400 if not Aggr_In_Place then
401 Remove_Side_Effects (Exp);
405 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
407 -- For a class wide allocation generate the following code:
409 -- type Equiv_Record is record ... end record;
410 -- implicit subtype CW is <Class_Wide_Subytpe>;
411 -- temp : PtrT := new CW'(CW!(expr));
413 if Is_Class_Wide_Type (T) then
414 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
416 Set_Expression (Expression (N),
417 Unchecked_Convert_To (Entity (Indic), Exp));
419 Analyze_And_Resolve (Expression (N), Entity (Indic));
422 if Aggr_In_Place then
424 Make_Object_Declaration (Loc,
425 Defining_Identifier => Temp,
426 Object_Definition => New_Reference_To (PtrT, Loc),
429 New_Reference_To (Etype (Exp), Loc)));
431 Set_Comes_From_Source
432 (Expression (Tmp_Node), Comes_From_Source (N));
434 Set_No_Initialization (Expression (Tmp_Node));
435 Insert_Action (N, Tmp_Node);
437 if Controlled_Type (T)
438 and then Ekind (PtrT) = E_Anonymous_Access_Type
440 -- Create local finalization list for access parameter
442 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
445 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
447 Node := Relocate_Node (N);
450 Make_Object_Declaration (Loc,
451 Defining_Identifier => Temp,
452 Constant_Present => True,
453 Object_Definition => New_Reference_To (PtrT, Loc),
454 Expression => Node));
457 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
458 -- type, generate an accessibility check to verify that the level of
459 -- the type of the created object is not deeper than the level of the
460 -- access type. If the type of the qualified expression is class-
461 -- wide, then always generate the check. Otherwise, only generate the
462 -- check if the level of the qualified expression type is statically
463 -- deeper than the access type. Although the static accessibility
464 -- will generally have been performed as a legality check, it won't
465 -- have been done in cases where the allocator appears in generic
466 -- body, so a run-time check is needed in general.
468 if Ada_Version >= Ada_05
469 and then Is_Class_Wide_Type (DesigT)
470 and then not Scope_Suppress (Accessibility_Check)
472 (Is_Class_Wide_Type (Etype (Exp))
474 Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT))
477 Make_Raise_Program_Error (Loc,
481 Make_Function_Call (Loc,
483 New_Reference_To (RTE (RE_Get_Access_Level), Loc),
484 Parameter_Associations =>
485 New_List (Make_Attribute_Reference (Loc,
487 New_Reference_To (Temp, Loc),
491 Make_Integer_Literal (Loc, Type_Access_Level (PtrT))),
492 Reason => PE_Accessibility_Check_Failed));
497 -- Suppress the tag assignment when Java_VM because JVM tags
498 -- are represented implicitly in objects.
502 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
504 TagR := New_Reference_To (Temp, Loc);
506 elsif Is_Private_Type (T)
507 and then Is_Tagged_Type (Underlying_Type (T))
509 TagT := Underlying_Type (T);
510 TagR := Unchecked_Convert_To (Underlying_Type (T),
511 Make_Explicit_Dereference (Loc,
512 New_Reference_To (Temp, Loc)));
516 if Present (TagT) then
518 Make_Assignment_Statement (Loc,
520 Make_Selected_Component (Loc,
523 New_Reference_To (First_Tag_Component (TagT), Loc)),
526 Unchecked_Convert_To (RTE (RE_Tag),
528 (Elists.Node (First_Elmt (Access_Disp_Table (TagT))),
531 -- The previous assignment has to be done in any case
533 Set_Assignment_OK (Name (Tag_Assign));
534 Insert_Action (N, Tag_Assign);
537 if Controlled_Type (DesigT)
538 and then Controlled_Type (T)
542 Apool : constant Entity_Id :=
543 Associated_Storage_Pool (PtrT);
546 -- If it is an allocation on the secondary stack
547 -- (i.e. a value returned from a function), the object
548 -- is attached on the caller side as soon as the call
549 -- is completed (see Expand_Ctrl_Function_Call)
551 if Is_RTE (Apool, RE_SS_Pool) then
553 F : constant Entity_Id :=
554 Make_Defining_Identifier (Loc,
555 New_Internal_Name ('F'));
558 Make_Object_Declaration (Loc,
559 Defining_Identifier => F,
560 Object_Definition => New_Reference_To (RTE
561 (RE_Finalizable_Ptr), Loc)));
563 Flist := New_Reference_To (F, Loc);
564 Attach := Make_Integer_Literal (Loc, 1);
567 -- Normal case, not a secondary stack allocation
570 if Controlled_Type (T)
571 and then Ekind (PtrT) = E_Anonymous_Access_Type
573 -- Create local finalization list for access parameter
576 Get_Allocator_Final_List (N, Base_Type (T), PtrT);
578 Flist := Find_Final_List (PtrT);
581 Attach := Make_Integer_Literal (Loc, 2);
584 if not Aggr_In_Place then
589 -- An unchecked conversion is needed in the
590 -- classwide case because the designated type
591 -- can be an ancestor of the subtype mark of
594 Unchecked_Convert_To (T,
595 Make_Explicit_Dereference (Loc,
596 New_Reference_To (Temp, Loc))),
600 With_Attach => Attach));
605 Rewrite (N, New_Reference_To (Temp, Loc));
606 Analyze_And_Resolve (N, PtrT);
608 elsif Aggr_In_Place then
610 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
612 Make_Object_Declaration (Loc,
613 Defining_Identifier => Temp,
614 Object_Definition => New_Reference_To (PtrT, Loc),
615 Expression => Make_Allocator (Loc,
616 New_Reference_To (Etype (Exp), Loc)));
618 Set_Comes_From_Source
619 (Expression (Tmp_Node), Comes_From_Source (N));
621 Set_No_Initialization (Expression (Tmp_Node));
622 Insert_Action (N, Tmp_Node);
623 Convert_Aggr_In_Allocator (Tmp_Node, Exp);
624 Rewrite (N, New_Reference_To (Temp, Loc));
625 Analyze_And_Resolve (N, PtrT);
627 elsif Is_Access_Type (DesigT)
628 and then Nkind (Exp) = N_Allocator
629 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
631 -- Apply constraint to designated subtype indication
633 Apply_Constraint_Check (Expression (Exp),
634 Designated_Type (DesigT),
637 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
639 -- Propagate constraint_error to enclosing allocator
641 Rewrite (Exp, New_Copy (Expression (Exp)));
644 -- First check against the type of the qualified expression
646 -- NOTE: The commented call should be correct, but for
647 -- some reason causes the compiler to bomb (sigsegv) on
648 -- ACVC test c34007g, so for now we just perform the old
649 -- (incorrect) test against the designated subtype with
650 -- no sliding in the else part of the if statement below.
653 -- Apply_Constraint_Check (Exp, T, No_Sliding => True);
655 -- A check is also needed in cases where the designated
656 -- subtype is constrained and differs from the subtype
657 -- given in the qualified expression. Note that the check
658 -- on the qualified expression does not allow sliding,
659 -- but this check does (a relaxation from Ada 83).
661 if Is_Constrained (DesigT)
662 and then not Subtypes_Statically_Match
665 Apply_Constraint_Check
666 (Exp, DesigT, No_Sliding => False);
668 -- The nonsliding check should really be performed
669 -- (unconditionally) against the subtype of the
670 -- qualified expression, but that causes a problem
671 -- with c34007g (see above), so for now we retain this.
674 Apply_Constraint_Check
675 (Exp, DesigT, No_Sliding => True);
678 -- For an access to unconstrained packed array, GIGI needs
679 -- to see an expression with a constrained subtype in order
680 -- to compute the proper size for the allocator.
683 and then not Is_Constrained (T)
684 and then Is_Packed (T)
687 ConstrT : constant Entity_Id :=
688 Make_Defining_Identifier (Loc,
689 Chars => New_Internal_Name ('A'));
690 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
693 Make_Subtype_Declaration (Loc,
694 Defining_Identifier => ConstrT,
695 Subtype_Indication =>
696 Make_Subtype_From_Expr (Exp, T)));
697 Freeze_Itype (ConstrT, Exp);
698 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
705 when RE_Not_Available =>
707 end Expand_Allocator_Expression;
709 -----------------------------
710 -- Expand_Array_Comparison --
711 -----------------------------
713 -- Expansion is only required in the case of array types. For the
714 -- unpacked case, an appropriate runtime routine is called. For
715 -- packed cases, and also in some other cases where a runtime
716 -- routine cannot be called, the form of the expansion is:
718 -- [body for greater_nn; boolean_expression]
720 -- The body is built by Make_Array_Comparison_Op, and the form of the
721 -- Boolean expression depends on the operator involved.
723 procedure Expand_Array_Comparison (N : Node_Id) is
724 Loc : constant Source_Ptr := Sloc (N);
725 Op1 : Node_Id := Left_Opnd (N);
726 Op2 : Node_Id := Right_Opnd (N);
727 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
728 Ctyp : constant Entity_Id := Component_Type (Typ1);
732 Func_Name : Entity_Id;
736 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
737 -- True for byte addressable target
739 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
740 -- Returns True if the length of the given operand is known to be
741 -- less than 4. Returns False if this length is known to be four
742 -- or greater or is not known at compile time.
744 ------------------------
745 -- Length_Less_Than_4 --
746 ------------------------
748 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
749 Otyp : constant Entity_Id := Etype (Opnd);
752 if Ekind (Otyp) = E_String_Literal_Subtype then
753 return String_Literal_Length (Otyp) < 4;
757 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
758 Lo : constant Node_Id := Type_Low_Bound (Ityp);
759 Hi : constant Node_Id := Type_High_Bound (Ityp);
764 if Compile_Time_Known_Value (Lo) then
765 Lov := Expr_Value (Lo);
770 if Compile_Time_Known_Value (Hi) then
771 Hiv := Expr_Value (Hi);
776 return Hiv < Lov + 3;
779 end Length_Less_Than_4;
781 -- Start of processing for Expand_Array_Comparison
784 -- Deal first with unpacked case, where we can call a runtime routine
785 -- except that we avoid this for targets for which are not addressable
786 -- by bytes, and for the JVM, since the JVM does not support direct
787 -- addressing of array components.
789 if not Is_Bit_Packed_Array (Typ1)
790 and then Byte_Addressable
793 -- The call we generate is:
795 -- Compare_Array_xn[_Unaligned]
796 -- (left'address, right'address, left'length, right'length) <op> 0
798 -- x = U for unsigned, S for signed
799 -- n = 8,16,32,64 for component size
800 -- Add _Unaligned if length < 4 and component size is 8.
801 -- <op> is the standard comparison operator
803 if Component_Size (Typ1) = 8 then
804 if Length_Less_Than_4 (Op1)
806 Length_Less_Than_4 (Op2)
808 if Is_Unsigned_Type (Ctyp) then
809 Comp := RE_Compare_Array_U8_Unaligned;
811 Comp := RE_Compare_Array_S8_Unaligned;
815 if Is_Unsigned_Type (Ctyp) then
816 Comp := RE_Compare_Array_U8;
818 Comp := RE_Compare_Array_S8;
822 elsif Component_Size (Typ1) = 16 then
823 if Is_Unsigned_Type (Ctyp) then
824 Comp := RE_Compare_Array_U16;
826 Comp := RE_Compare_Array_S16;
829 elsif Component_Size (Typ1) = 32 then
830 if Is_Unsigned_Type (Ctyp) then
831 Comp := RE_Compare_Array_U32;
833 Comp := RE_Compare_Array_S32;
836 else pragma Assert (Component_Size (Typ1) = 64);
837 if Is_Unsigned_Type (Ctyp) then
838 Comp := RE_Compare_Array_U64;
840 Comp := RE_Compare_Array_S64;
844 Remove_Side_Effects (Op1, Name_Req => True);
845 Remove_Side_Effects (Op2, Name_Req => True);
848 Make_Function_Call (Sloc (Op1),
849 Name => New_Occurrence_Of (RTE (Comp), Loc),
851 Parameter_Associations => New_List (
852 Make_Attribute_Reference (Loc,
853 Prefix => Relocate_Node (Op1),
854 Attribute_Name => Name_Address),
856 Make_Attribute_Reference (Loc,
857 Prefix => Relocate_Node (Op2),
858 Attribute_Name => Name_Address),
860 Make_Attribute_Reference (Loc,
861 Prefix => Relocate_Node (Op1),
862 Attribute_Name => Name_Length),
864 Make_Attribute_Reference (Loc,
865 Prefix => Relocate_Node (Op2),
866 Attribute_Name => Name_Length))));
869 Make_Integer_Literal (Sloc (Op2),
872 Analyze_And_Resolve (Op1, Standard_Integer);
873 Analyze_And_Resolve (Op2, Standard_Integer);
877 -- Cases where we cannot make runtime call
879 -- For (a <= b) we convert to not (a > b)
881 if Chars (N) = Name_Op_Le then
887 Right_Opnd => Op2)));
888 Analyze_And_Resolve (N, Standard_Boolean);
891 -- For < the Boolean expression is
892 -- greater__nn (op2, op1)
894 elsif Chars (N) = Name_Op_Lt then
895 Func_Body := Make_Array_Comparison_Op (Typ1, N);
899 Op1 := Right_Opnd (N);
900 Op2 := Left_Opnd (N);
902 -- For (a >= b) we convert to not (a < b)
904 elsif Chars (N) = Name_Op_Ge then
910 Right_Opnd => Op2)));
911 Analyze_And_Resolve (N, Standard_Boolean);
914 -- For > the Boolean expression is
915 -- greater__nn (op1, op2)
918 pragma Assert (Chars (N) = Name_Op_Gt);
919 Func_Body := Make_Array_Comparison_Op (Typ1, N);
922 Func_Name := Defining_Unit_Name (Specification (Func_Body));
924 Make_Function_Call (Loc,
925 Name => New_Reference_To (Func_Name, Loc),
926 Parameter_Associations => New_List (Op1, Op2));
928 Insert_Action (N, Func_Body);
930 Analyze_And_Resolve (N, Standard_Boolean);
933 when RE_Not_Available =>
935 end Expand_Array_Comparison;
937 ---------------------------
938 -- Expand_Array_Equality --
939 ---------------------------
941 -- Expand an equality function for multi-dimensional arrays. Here is
942 -- an example of such a function for Nb_Dimension = 2
944 -- function Enn (A : atyp; B : btyp) return boolean is
946 -- if (A'length (1) = 0 or else A'length (2) = 0)
948 -- (B'length (1) = 0 or else B'length (2) = 0)
950 -- return True; -- RM 4.5.2(22)
953 -- if A'length (1) /= B'length (1)
955 -- A'length (2) /= B'length (2)
957 -- return False; -- RM 4.5.2(23)
961 -- A1 : Index_T1 := A'first (1);
962 -- B1 : Index_T1 := B'first (1);
966 -- A2 : Index_T2 := A'first (2);
967 -- B2 : Index_T2 := B'first (2);
970 -- if A (A1, A2) /= B (B1, B2) then
974 -- exit when A2 = A'last (2);
975 -- A2 := Index_T2'succ (A2);
976 -- B2 := Index_T2'succ (B2);
980 -- exit when A1 = A'last (1);
981 -- A1 := Index_T1'succ (A1);
982 -- B1 := Index_T1'succ (B1);
989 -- Note on the formal types used (atyp and btyp). If either of the
990 -- arrays is of a private type, we use the underlying type, and
991 -- do an unchecked conversion of the actual. If either of the arrays
992 -- has a bound depending on a discriminant, then we use the base type
993 -- since otherwise we have an escaped discriminant in the function.
995 -- If both arrays are constrained and have the same bounds, we can
996 -- generate a loop with an explicit iteration scheme using a 'Range
997 -- attribute over the first array.
999 function Expand_Array_Equality
1004 Typ : Entity_Id) return Node_Id
1006 Loc : constant Source_Ptr := Sloc (Nod);
1007 Decls : constant List_Id := New_List;
1008 Index_List1 : constant List_Id := New_List;
1009 Index_List2 : constant List_Id := New_List;
1013 Func_Name : Entity_Id;
1014 Func_Body : Node_Id;
1016 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1017 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1021 -- The parameter types to be used for the formals
1026 Num : Int) return Node_Id;
1027 -- This builds the attribute reference Arr'Nam (Expr)
1029 function Component_Equality (Typ : Entity_Id) return Node_Id;
1030 -- Create one statement to compare corresponding components,
1031 -- designated by a full set of indices.
1033 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1034 -- Given one of the arguments, computes the appropriate type to
1035 -- be used for that argument in the corresponding function formal
1037 function Handle_One_Dimension
1039 Index : Node_Id) return Node_Id;
1040 -- This procedure returns the following code
1043 -- Bn : Index_T := B'First (N);
1047 -- exit when An = A'Last (N);
1048 -- An := Index_T'Succ (An)
1049 -- Bn := Index_T'Succ (Bn)
1053 -- If both indices are constrained and identical, the procedure
1054 -- returns a simpler loop:
1056 -- for An in A'Range (N) loop
1060 -- N is the dimension for which we are generating a loop. Index is the
1061 -- N'th index node, whose Etype is Index_Type_n in the above code.
1062 -- The xxx statement is either the loop or declare for the next
1063 -- dimension or if this is the last dimension the comparison
1064 -- of corresponding components of the arrays.
1066 -- The actual way the code works is to return the comparison
1067 -- of corresponding components for the N+1 call. That's neater!
1069 function Test_Empty_Arrays return Node_Id;
1070 -- This function constructs the test for both arrays being empty
1071 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1073 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1075 function Test_Lengths_Correspond return Node_Id;
1076 -- This function constructs the test for arrays having different
1077 -- lengths in at least one index position, in which case resull
1079 -- A'length (1) /= B'length (1)
1081 -- A'length (2) /= B'length (2)
1092 Num : Int) return Node_Id
1096 Make_Attribute_Reference (Loc,
1097 Attribute_Name => Nam,
1098 Prefix => New_Reference_To (Arr, Loc),
1099 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1102 ------------------------
1103 -- Component_Equality --
1104 ------------------------
1106 function Component_Equality (Typ : Entity_Id) return Node_Id is
1111 -- if a(i1...) /= b(j1...) then return false; end if;
1114 Make_Indexed_Component (Loc,
1115 Prefix => Make_Identifier (Loc, Chars (A)),
1116 Expressions => Index_List1);
1119 Make_Indexed_Component (Loc,
1120 Prefix => Make_Identifier (Loc, Chars (B)),
1121 Expressions => Index_List2);
1123 Test := Expand_Composite_Equality
1124 (Nod, Component_Type (Typ), L, R, Decls);
1126 -- If some (sub)component is an unchecked_union, the whole operation
1127 -- will raise program error.
1129 if Nkind (Test) = N_Raise_Program_Error then
1131 -- This node is going to be inserted at a location where a
1132 -- statement is expected: clear its Etype so analysis will
1133 -- set it to the expected Standard_Void_Type.
1135 Set_Etype (Test, Empty);
1140 Make_Implicit_If_Statement (Nod,
1141 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1142 Then_Statements => New_List (
1143 Make_Return_Statement (Loc,
1144 Expression => New_Occurrence_Of (Standard_False, Loc))));
1146 end Component_Equality;
1152 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1163 T := Underlying_Type (T);
1165 X := First_Index (T);
1166 while Present (X) loop
1167 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1169 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1182 --------------------------
1183 -- Handle_One_Dimension --
1184 ---------------------------
1186 function Handle_One_Dimension
1188 Index : Node_Id) return Node_Id
1190 Need_Separate_Indexes : constant Boolean :=
1192 or else not Is_Constrained (Ltyp);
1193 -- If the index types are identical, and we are working with
1194 -- constrained types, then we can use the same index for both of
1197 An : constant Entity_Id := Make_Defining_Identifier (Loc,
1198 Chars => New_Internal_Name ('A'));
1201 Index_T : Entity_Id;
1206 if N > Number_Dimensions (Ltyp) then
1207 return Component_Equality (Ltyp);
1210 -- Case where we generate a loop
1212 Index_T := Base_Type (Etype (Index));
1214 if Need_Separate_Indexes then
1216 Make_Defining_Identifier (Loc,
1217 Chars => New_Internal_Name ('B'));
1222 Append (New_Reference_To (An, Loc), Index_List1);
1223 Append (New_Reference_To (Bn, Loc), Index_List2);
1225 Stm_List := New_List (
1226 Handle_One_Dimension (N + 1, Next_Index (Index)));
1228 if Need_Separate_Indexes then
1230 -- Generate guard for loop, followed by increments of indices
1232 Append_To (Stm_List,
1233 Make_Exit_Statement (Loc,
1236 Left_Opnd => New_Reference_To (An, Loc),
1237 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1239 Append_To (Stm_List,
1240 Make_Assignment_Statement (Loc,
1241 Name => New_Reference_To (An, Loc),
1243 Make_Attribute_Reference (Loc,
1244 Prefix => New_Reference_To (Index_T, Loc),
1245 Attribute_Name => Name_Succ,
1246 Expressions => New_List (New_Reference_To (An, Loc)))));
1248 Append_To (Stm_List,
1249 Make_Assignment_Statement (Loc,
1250 Name => New_Reference_To (Bn, Loc),
1252 Make_Attribute_Reference (Loc,
1253 Prefix => New_Reference_To (Index_T, Loc),
1254 Attribute_Name => Name_Succ,
1255 Expressions => New_List (New_Reference_To (Bn, Loc)))));
1258 -- If separate indexes, we need a declare block for An and Bn, and a
1259 -- loop without an iteration scheme.
1261 if Need_Separate_Indexes then
1263 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
1266 Make_Block_Statement (Loc,
1267 Declarations => New_List (
1268 Make_Object_Declaration (Loc,
1269 Defining_Identifier => An,
1270 Object_Definition => New_Reference_To (Index_T, Loc),
1271 Expression => Arr_Attr (A, Name_First, N)),
1273 Make_Object_Declaration (Loc,
1274 Defining_Identifier => Bn,
1275 Object_Definition => New_Reference_To (Index_T, Loc),
1276 Expression => Arr_Attr (B, Name_First, N))),
1278 Handled_Statement_Sequence =>
1279 Make_Handled_Sequence_Of_Statements (Loc,
1280 Statements => New_List (Loop_Stm)));
1282 -- If no separate indexes, return loop statement with explicit
1283 -- iteration scheme on its own
1287 Make_Implicit_Loop_Statement (Nod,
1288 Statements => Stm_List,
1290 Make_Iteration_Scheme (Loc,
1291 Loop_Parameter_Specification =>
1292 Make_Loop_Parameter_Specification (Loc,
1293 Defining_Identifier => An,
1294 Discrete_Subtype_Definition =>
1295 Arr_Attr (A, Name_Range, N))));
1298 end Handle_One_Dimension;
1300 -----------------------
1301 -- Test_Empty_Arrays --
1302 -----------------------
1304 function Test_Empty_Arrays return Node_Id is
1314 for J in 1 .. Number_Dimensions (Ltyp) loop
1317 Left_Opnd => Arr_Attr (A, Name_Length, J),
1318 Right_Opnd => Make_Integer_Literal (Loc, 0));
1322 Left_Opnd => Arr_Attr (B, Name_Length, J),
1323 Right_Opnd => Make_Integer_Literal (Loc, 0));
1332 Left_Opnd => Relocate_Node (Alist),
1333 Right_Opnd => Atest);
1337 Left_Opnd => Relocate_Node (Blist),
1338 Right_Opnd => Btest);
1345 Right_Opnd => Blist);
1346 end Test_Empty_Arrays;
1348 -----------------------------
1349 -- Test_Lengths_Correspond --
1350 -----------------------------
1352 function Test_Lengths_Correspond return Node_Id is
1358 for J in 1 .. Number_Dimensions (Ltyp) loop
1361 Left_Opnd => Arr_Attr (A, Name_Length, J),
1362 Right_Opnd => Arr_Attr (B, Name_Length, J));
1369 Left_Opnd => Relocate_Node (Result),
1370 Right_Opnd => Rtest);
1375 end Test_Lengths_Correspond;
1377 -- Start of processing for Expand_Array_Equality
1380 Ltyp := Get_Arg_Type (Lhs);
1381 Rtyp := Get_Arg_Type (Rhs);
1383 -- For now, if the argument types are not the same, go to the
1384 -- base type, since the code assumes that the formals have the
1385 -- same type. This is fixable in future ???
1387 if Ltyp /= Rtyp then
1388 Ltyp := Base_Type (Ltyp);
1389 Rtyp := Base_Type (Rtyp);
1390 pragma Assert (Ltyp = Rtyp);
1393 -- Build list of formals for function
1395 Formals := New_List (
1396 Make_Parameter_Specification (Loc,
1397 Defining_Identifier => A,
1398 Parameter_Type => New_Reference_To (Ltyp, Loc)),
1400 Make_Parameter_Specification (Loc,
1401 Defining_Identifier => B,
1402 Parameter_Type => New_Reference_To (Rtyp, Loc)));
1404 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
1406 -- Build statement sequence for function
1409 Make_Subprogram_Body (Loc,
1411 Make_Function_Specification (Loc,
1412 Defining_Unit_Name => Func_Name,
1413 Parameter_Specifications => Formals,
1414 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
1416 Declarations => Decls,
1418 Handled_Statement_Sequence =>
1419 Make_Handled_Sequence_Of_Statements (Loc,
1420 Statements => New_List (
1422 Make_Implicit_If_Statement (Nod,
1423 Condition => Test_Empty_Arrays,
1424 Then_Statements => New_List (
1425 Make_Return_Statement (Loc,
1427 New_Occurrence_Of (Standard_True, Loc)))),
1429 Make_Implicit_If_Statement (Nod,
1430 Condition => Test_Lengths_Correspond,
1431 Then_Statements => New_List (
1432 Make_Return_Statement (Loc,
1434 New_Occurrence_Of (Standard_False, Loc)))),
1436 Handle_One_Dimension (1, First_Index (Ltyp)),
1438 Make_Return_Statement (Loc,
1439 Expression => New_Occurrence_Of (Standard_True, Loc)))));
1441 Set_Has_Completion (Func_Name, True);
1442 Set_Is_Inlined (Func_Name);
1444 -- If the array type is distinct from the type of the arguments,
1445 -- it is the full view of a private type. Apply an unchecked
1446 -- conversion to insure that analysis of the call succeeds.
1456 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
1458 L := OK_Convert_To (Ltyp, Lhs);
1462 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
1464 R := OK_Convert_To (Rtyp, Rhs);
1467 Actuals := New_List (L, R);
1470 Append_To (Bodies, Func_Body);
1473 Make_Function_Call (Loc,
1474 Name => New_Reference_To (Func_Name, Loc),
1475 Parameter_Associations => Actuals);
1476 end Expand_Array_Equality;
1478 -----------------------------
1479 -- Expand_Boolean_Operator --
1480 -----------------------------
1482 -- Note that we first get the actual subtypes of the operands,
1483 -- since we always want to deal with types that have bounds.
1485 procedure Expand_Boolean_Operator (N : Node_Id) is
1486 Typ : constant Entity_Id := Etype (N);
1489 -- Special case of bit packed array where both operands are known
1490 -- to be properly aligned. In this case we use an efficient run time
1491 -- routine to carry out the operation (see System.Bit_Ops).
1493 if Is_Bit_Packed_Array (Typ)
1494 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
1495 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
1497 Expand_Packed_Boolean_Operator (N);
1501 -- For the normal non-packed case, the general expansion is to build
1502 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
1503 -- and then inserting it into the tree. The original operator node is
1504 -- then rewritten as a call to this function. We also use this in the
1505 -- packed case if either operand is a possibly unaligned object.
1508 Loc : constant Source_Ptr := Sloc (N);
1509 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
1510 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
1511 Func_Body : Node_Id;
1512 Func_Name : Entity_Id;
1515 Convert_To_Actual_Subtype (L);
1516 Convert_To_Actual_Subtype (R);
1517 Ensure_Defined (Etype (L), N);
1518 Ensure_Defined (Etype (R), N);
1519 Apply_Length_Check (R, Etype (L));
1521 if Nkind (Parent (N)) = N_Assignment_Statement
1522 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
1524 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
1526 elsif Nkind (Parent (N)) = N_Op_Not
1527 and then Nkind (N) = N_Op_And
1529 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
1534 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
1535 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1536 Insert_Action (N, Func_Body);
1538 -- Now rewrite the expression with a call
1541 Make_Function_Call (Loc,
1542 Name => New_Reference_To (Func_Name, Loc),
1543 Parameter_Associations =>
1546 Make_Type_Conversion
1547 (Loc, New_Reference_To (Etype (L), Loc), R))));
1549 Analyze_And_Resolve (N, Typ);
1552 end Expand_Boolean_Operator;
1554 -------------------------------
1555 -- Expand_Composite_Equality --
1556 -------------------------------
1558 -- This function is only called for comparing internal fields of composite
1559 -- types when these fields are themselves composites. This is a special
1560 -- case because it is not possible to respect normal Ada visibility rules.
1562 function Expand_Composite_Equality
1567 Bodies : List_Id) return Node_Id
1569 Loc : constant Source_Ptr := Sloc (Nod);
1570 Full_Type : Entity_Id;
1575 if Is_Private_Type (Typ) then
1576 Full_Type := Underlying_Type (Typ);
1581 -- Defense against malformed private types with no completion
1582 -- the error will be diagnosed later by check_completion
1584 if No (Full_Type) then
1585 return New_Reference_To (Standard_False, Loc);
1588 Full_Type := Base_Type (Full_Type);
1590 if Is_Array_Type (Full_Type) then
1592 -- If the operand is an elementary type other than a floating-point
1593 -- type, then we can simply use the built-in block bitwise equality,
1594 -- since the predefined equality operators always apply and bitwise
1595 -- equality is fine for all these cases.
1597 if Is_Elementary_Type (Component_Type (Full_Type))
1598 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
1600 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1602 -- For composite component types, and floating-point types, use
1603 -- the expansion. This deals with tagged component types (where
1604 -- we use the applicable equality routine) and floating-point,
1605 -- (where we need to worry about negative zeroes), and also the
1606 -- case of any composite type recursively containing such fields.
1609 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
1612 elsif Is_Tagged_Type (Full_Type) then
1614 -- Call the primitive operation "=" of this type
1616 if Is_Class_Wide_Type (Full_Type) then
1617 Full_Type := Root_Type (Full_Type);
1620 -- If this is derived from an untagged private type completed
1621 -- with a tagged type, it does not have a full view, so we
1622 -- use the primitive operations of the private type.
1623 -- This check should no longer be necessary when these
1624 -- types receive their full views ???
1626 if Is_Private_Type (Typ)
1627 and then not Is_Tagged_Type (Typ)
1628 and then not Is_Controlled (Typ)
1629 and then Is_Derived_Type (Typ)
1630 and then No (Full_View (Typ))
1632 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
1634 Prim := First_Elmt (Primitive_Operations (Full_Type));
1638 Eq_Op := Node (Prim);
1639 exit when Chars (Eq_Op) = Name_Op_Eq
1640 and then Etype (First_Formal (Eq_Op)) =
1641 Etype (Next_Formal (First_Formal (Eq_Op)))
1642 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
1644 pragma Assert (Present (Prim));
1647 Eq_Op := Node (Prim);
1650 Make_Function_Call (Loc,
1651 Name => New_Reference_To (Eq_Op, Loc),
1652 Parameter_Associations =>
1654 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
1655 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
1657 elsif Is_Record_Type (Full_Type) then
1658 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
1660 if Present (Eq_Op) then
1661 if Etype (First_Formal (Eq_Op)) /= Full_Type then
1663 -- Inherited equality from parent type. Convert the actuals
1664 -- to match signature of operation.
1667 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
1671 Make_Function_Call (Loc,
1672 Name => New_Reference_To (Eq_Op, Loc),
1673 Parameter_Associations =>
1674 New_List (OK_Convert_To (T, Lhs),
1675 OK_Convert_To (T, Rhs)));
1679 -- Comparison between Unchecked_Union components
1681 if Is_Unchecked_Union (Full_Type) then
1683 Lhs_Type : Node_Id := Full_Type;
1684 Rhs_Type : Node_Id := Full_Type;
1685 Lhs_Discr_Val : Node_Id;
1686 Rhs_Discr_Val : Node_Id;
1691 if Nkind (Lhs) = N_Selected_Component then
1692 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
1697 if Nkind (Rhs) = N_Selected_Component then
1698 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
1701 -- Lhs of the composite equality
1703 if Is_Constrained (Lhs_Type) then
1705 -- Since the enclosing record can never be an
1706 -- Unchecked_Union (this code is executed for records
1707 -- that do not have variants), we may reference its
1710 if Nkind (Lhs) = N_Selected_Component
1711 and then Has_Per_Object_Constraint (
1712 Entity (Selector_Name (Lhs)))
1715 Make_Selected_Component (Loc,
1716 Prefix => Prefix (Lhs),
1719 Get_Discriminant_Value (
1720 First_Discriminant (Lhs_Type),
1722 Stored_Constraint (Lhs_Type))));
1725 Lhs_Discr_Val := New_Copy (
1726 Get_Discriminant_Value (
1727 First_Discriminant (Lhs_Type),
1729 Stored_Constraint (Lhs_Type)));
1733 -- It is not possible to infer the discriminant since
1734 -- the subtype is not constrained.
1737 Make_Raise_Program_Error (Loc,
1738 Reason => PE_Unchecked_Union_Restriction);
1741 -- Rhs of the composite equality
1743 if Is_Constrained (Rhs_Type) then
1744 if Nkind (Rhs) = N_Selected_Component
1745 and then Has_Per_Object_Constraint (
1746 Entity (Selector_Name (Rhs)))
1749 Make_Selected_Component (Loc,
1750 Prefix => Prefix (Rhs),
1753 Get_Discriminant_Value (
1754 First_Discriminant (Rhs_Type),
1756 Stored_Constraint (Rhs_Type))));
1759 Rhs_Discr_Val := New_Copy (
1760 Get_Discriminant_Value (
1761 First_Discriminant (Rhs_Type),
1763 Stored_Constraint (Rhs_Type)));
1768 Make_Raise_Program_Error (Loc,
1769 Reason => PE_Unchecked_Union_Restriction);
1772 -- Call the TSS equality function with the inferred
1773 -- discriminant values.
1776 Make_Function_Call (Loc,
1777 Name => New_Reference_To (Eq_Op, Loc),
1778 Parameter_Associations => New_List (
1786 -- Shouldn't this be an else, we can't fall through
1787 -- the above IF, right???
1790 Make_Function_Call (Loc,
1791 Name => New_Reference_To (Eq_Op, Loc),
1792 Parameter_Associations => New_List (Lhs, Rhs));
1796 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
1800 -- It can be a simple record or the full view of a scalar private
1802 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
1804 end Expand_Composite_Equality;
1806 ------------------------------
1807 -- Expand_Concatenate_Other --
1808 ------------------------------
1810 -- Let n be the number of array operands to be concatenated, Base_Typ
1811 -- their base type, Ind_Typ their index type, and Arr_Typ the original
1812 -- array type to which the concatenantion operator applies, then the
1813 -- following subprogram is constructed:
1815 -- [function Cnn (S1 : Base_Typ; ...; Sn : Base_Typ) return Base_Typ is
1818 -- if S1'Length /= 0 then
1819 -- L := XXX; --> XXX = S1'First if Arr_Typ is unconstrained
1820 -- XXX = Arr_Typ'First otherwise
1821 -- elsif S2'Length /= 0 then
1822 -- L := YYY; --> YYY = S2'First if Arr_Typ is unconstrained
1823 -- YYY = Arr_Typ'First otherwise
1825 -- elsif Sn-1'Length /= 0 then
1826 -- L := ZZZ; --> ZZZ = Sn-1'First if Arr_Typ is unconstrained
1827 -- ZZZ = Arr_Typ'First otherwise
1835 -- Ind_Typ'Val ((((S1'Length - 1) + S2'Length) + ... + Sn'Length)
1836 -- + Ind_Typ'Pos (L));
1837 -- R : Base_Typ (L .. H);
1839 -- if S1'Length /= 0 then
1843 -- L := Ind_Typ'Succ (L);
1844 -- exit when P = S1'Last;
1845 -- P := Ind_Typ'Succ (P);
1849 -- if S2'Length /= 0 then
1850 -- L := Ind_Typ'Succ (L);
1853 -- L := Ind_Typ'Succ (L);
1854 -- exit when P = S2'Last;
1855 -- P := Ind_Typ'Succ (P);
1861 -- if Sn'Length /= 0 then
1865 -- L := Ind_Typ'Succ (L);
1866 -- exit when P = Sn'Last;
1867 -- P := Ind_Typ'Succ (P);
1875 procedure Expand_Concatenate_Other (Cnode : Node_Id; Opnds : List_Id) is
1876 Loc : constant Source_Ptr := Sloc (Cnode);
1877 Nb_Opnds : constant Nat := List_Length (Opnds);
1879 Arr_Typ : constant Entity_Id := Etype (Entity (Cnode));
1880 Base_Typ : constant Entity_Id := Base_Type (Etype (Cnode));
1881 Ind_Typ : constant Entity_Id := Etype (First_Index (Base_Typ));
1884 Func_Spec : Node_Id;
1885 Param_Specs : List_Id;
1887 Func_Body : Node_Id;
1888 Func_Decls : List_Id;
1889 Func_Stmts : List_Id;
1894 Elsif_List : List_Id;
1896 Declare_Block : Node_Id;
1897 Declare_Decls : List_Id;
1898 Declare_Stmts : List_Id;
1910 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id;
1911 -- Builds the sequence of statement:
1915 -- L := Ind_Typ'Succ (L);
1916 -- exit when P = Si'Last;
1917 -- P := Ind_Typ'Succ (P);
1920 -- where i is the input parameter I given.
1921 -- If the flag Last is true, the exit statement is emitted before
1922 -- incrementing the lower bound, to prevent the creation out of
1925 function Init_L (I : Nat) return Node_Id;
1926 -- Builds the statement:
1927 -- L := Arr_Typ'First; If Arr_Typ is constrained
1928 -- L := Si'First; otherwise (where I is the input param given)
1930 function H return Node_Id;
1931 -- Builds reference to identifier H
1933 function Ind_Val (E : Node_Id) return Node_Id;
1934 -- Builds expression Ind_Typ'Val (E);
1936 function L return Node_Id;
1937 -- Builds reference to identifier L
1939 function L_Pos return Node_Id;
1940 -- Builds expression Integer_Type'(Ind_Typ'Pos (L)). We qualify the
1941 -- expression to avoid universal_integer computations whenever possible,
1942 -- in the expression for the upper bound H.
1944 function L_Succ return Node_Id;
1945 -- Builds expression Ind_Typ'Succ (L)
1947 function One return Node_Id;
1948 -- Builds integer literal one
1950 function P return Node_Id;
1951 -- Builds reference to identifier P
1953 function P_Succ return Node_Id;
1954 -- Builds expression Ind_Typ'Succ (P)
1956 function R return Node_Id;
1957 -- Builds reference to identifier R
1959 function S (I : Nat) return Node_Id;
1960 -- Builds reference to identifier Si, where I is the value given
1962 function S_First (I : Nat) return Node_Id;
1963 -- Builds expression Si'First, where I is the value given
1965 function S_Last (I : Nat) return Node_Id;
1966 -- Builds expression Si'Last, where I is the value given
1968 function S_Length (I : Nat) return Node_Id;
1969 -- Builds expression Si'Length, where I is the value given
1971 function S_Length_Test (I : Nat) return Node_Id;
1972 -- Builds expression Si'Length /= 0, where I is the value given
1978 function Copy_Into_R_S (I : Nat; Last : Boolean) return List_Id is
1979 Stmts : constant List_Id := New_List;
1981 Loop_Stmt : Node_Id;
1983 Exit_Stmt : Node_Id;
1988 -- First construct the initializations
1990 P_Start := Make_Assignment_Statement (Loc,
1992 Expression => S_First (I));
1993 Append_To (Stmts, P_Start);
1995 -- Then build the loop
1997 R_Copy := Make_Assignment_Statement (Loc,
1998 Name => Make_Indexed_Component (Loc,
2000 Expressions => New_List (L)),
2001 Expression => Make_Indexed_Component (Loc,
2003 Expressions => New_List (P)));
2005 L_Inc := Make_Assignment_Statement (Loc,
2007 Expression => L_Succ);
2009 Exit_Stmt := Make_Exit_Statement (Loc,
2010 Condition => Make_Op_Eq (Loc, P, S_Last (I)));
2012 P_Inc := Make_Assignment_Statement (Loc,
2014 Expression => P_Succ);
2018 Make_Implicit_Loop_Statement (Cnode,
2019 Statements => New_List (R_Copy, Exit_Stmt, L_Inc, P_Inc));
2022 Make_Implicit_Loop_Statement (Cnode,
2023 Statements => New_List (R_Copy, L_Inc, Exit_Stmt, P_Inc));
2026 Append_To (Stmts, Loop_Stmt);
2035 function H return Node_Id is
2037 return Make_Identifier (Loc, Name_uH);
2044 function Ind_Val (E : Node_Id) return Node_Id is
2047 Make_Attribute_Reference (Loc,
2048 Prefix => New_Reference_To (Ind_Typ, Loc),
2049 Attribute_Name => Name_Val,
2050 Expressions => New_List (E));
2057 function Init_L (I : Nat) return Node_Id is
2061 if Is_Constrained (Arr_Typ) then
2062 E := Make_Attribute_Reference (Loc,
2063 Prefix => New_Reference_To (Arr_Typ, Loc),
2064 Attribute_Name => Name_First);
2070 return Make_Assignment_Statement (Loc, Name => L, Expression => E);
2077 function L return Node_Id is
2079 return Make_Identifier (Loc, Name_uL);
2086 function L_Pos return Node_Id is
2087 Target_Type : Entity_Id;
2090 -- If the index type is an enumeration type, the computation
2091 -- can be done in standard integer. Otherwise, choose a large
2092 -- enough integer type.
2094 if Is_Enumeration_Type (Ind_Typ)
2095 or else Root_Type (Ind_Typ) = Standard_Integer
2096 or else Root_Type (Ind_Typ) = Standard_Short_Integer
2097 or else Root_Type (Ind_Typ) = Standard_Short_Short_Integer
2099 Target_Type := Standard_Integer;
2101 Target_Type := Root_Type (Ind_Typ);
2105 Make_Qualified_Expression (Loc,
2106 Subtype_Mark => New_Reference_To (Target_Type, Loc),
2108 Make_Attribute_Reference (Loc,
2109 Prefix => New_Reference_To (Ind_Typ, Loc),
2110 Attribute_Name => Name_Pos,
2111 Expressions => New_List (L)));
2118 function L_Succ return Node_Id is
2121 Make_Attribute_Reference (Loc,
2122 Prefix => New_Reference_To (Ind_Typ, Loc),
2123 Attribute_Name => Name_Succ,
2124 Expressions => New_List (L));
2131 function One return Node_Id is
2133 return Make_Integer_Literal (Loc, 1);
2140 function P return Node_Id is
2142 return Make_Identifier (Loc, Name_uP);
2149 function P_Succ return Node_Id is
2152 Make_Attribute_Reference (Loc,
2153 Prefix => New_Reference_To (Ind_Typ, Loc),
2154 Attribute_Name => Name_Succ,
2155 Expressions => New_List (P));
2162 function R return Node_Id is
2164 return Make_Identifier (Loc, Name_uR);
2171 function S (I : Nat) return Node_Id is
2173 return Make_Identifier (Loc, New_External_Name ('S', I));
2180 function S_First (I : Nat) return Node_Id is
2182 return Make_Attribute_Reference (Loc,
2184 Attribute_Name => Name_First);
2191 function S_Last (I : Nat) return Node_Id is
2193 return Make_Attribute_Reference (Loc,
2195 Attribute_Name => Name_Last);
2202 function S_Length (I : Nat) return Node_Id is
2204 return Make_Attribute_Reference (Loc,
2206 Attribute_Name => Name_Length);
2213 function S_Length_Test (I : Nat) return Node_Id is
2217 Left_Opnd => S_Length (I),
2218 Right_Opnd => Make_Integer_Literal (Loc, 0));
2221 -- Start of processing for Expand_Concatenate_Other
2224 -- Construct the parameter specs and the overall function spec
2226 Param_Specs := New_List;
2227 for I in 1 .. Nb_Opnds loop
2230 Make_Parameter_Specification (Loc,
2231 Defining_Identifier =>
2232 Make_Defining_Identifier (Loc, New_External_Name ('S', I)),
2233 Parameter_Type => New_Reference_To (Base_Typ, Loc)));
2236 Func_Id := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2238 Make_Function_Specification (Loc,
2239 Defining_Unit_Name => Func_Id,
2240 Parameter_Specifications => Param_Specs,
2241 Result_Definition => New_Reference_To (Base_Typ, Loc));
2243 -- Construct L's object declaration
2246 Make_Object_Declaration (Loc,
2247 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uL),
2248 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2250 Func_Decls := New_List (L_Decl);
2252 -- Construct the if-then-elsif statements
2254 Elsif_List := New_List;
2255 for I in 2 .. Nb_Opnds - 1 loop
2256 Append_To (Elsif_List, Make_Elsif_Part (Loc,
2257 Condition => S_Length_Test (I),
2258 Then_Statements => New_List (Init_L (I))));
2262 Make_Implicit_If_Statement (Cnode,
2263 Condition => S_Length_Test (1),
2264 Then_Statements => New_List (Init_L (1)),
2265 Elsif_Parts => Elsif_List,
2266 Else_Statements => New_List (Make_Return_Statement (Loc,
2267 Expression => S (Nb_Opnds))));
2269 -- Construct the declaration for H
2272 Make_Object_Declaration (Loc,
2273 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uP),
2274 Object_Definition => New_Reference_To (Ind_Typ, Loc));
2276 H_Init := Make_Op_Subtract (Loc, S_Length (1), One);
2277 for I in 2 .. Nb_Opnds loop
2278 H_Init := Make_Op_Add (Loc, H_Init, S_Length (I));
2280 H_Init := Ind_Val (Make_Op_Add (Loc, H_Init, L_Pos));
2283 Make_Object_Declaration (Loc,
2284 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uH),
2285 Object_Definition => New_Reference_To (Ind_Typ, Loc),
2286 Expression => H_Init);
2288 -- Construct the declaration for R
2290 R_Range := Make_Range (Loc, Low_Bound => L, High_Bound => H);
2292 Make_Index_Or_Discriminant_Constraint (Loc,
2293 Constraints => New_List (R_Range));
2296 Make_Object_Declaration (Loc,
2297 Defining_Identifier => Make_Defining_Identifier (Loc, Name_uR),
2298 Object_Definition =>
2299 Make_Subtype_Indication (Loc,
2300 Subtype_Mark => New_Reference_To (Base_Typ, Loc),
2301 Constraint => R_Constr));
2303 -- Construct the declarations for the declare block
2305 Declare_Decls := New_List (P_Decl, H_Decl, R_Decl);
2307 -- Construct list of statements for the declare block
2309 Declare_Stmts := New_List;
2310 for I in 1 .. Nb_Opnds loop
2311 Append_To (Declare_Stmts,
2312 Make_Implicit_If_Statement (Cnode,
2313 Condition => S_Length_Test (I),
2314 Then_Statements => Copy_Into_R_S (I, I = Nb_Opnds)));
2317 Append_To (Declare_Stmts, Make_Return_Statement (Loc, Expression => R));
2319 -- Construct the declare block
2321 Declare_Block := Make_Block_Statement (Loc,
2322 Declarations => Declare_Decls,
2323 Handled_Statement_Sequence =>
2324 Make_Handled_Sequence_Of_Statements (Loc, Declare_Stmts));
2326 -- Construct the list of function statements
2328 Func_Stmts := New_List (If_Stmt, Declare_Block);
2330 -- Construct the function body
2333 Make_Subprogram_Body (Loc,
2334 Specification => Func_Spec,
2335 Declarations => Func_Decls,
2336 Handled_Statement_Sequence =>
2337 Make_Handled_Sequence_Of_Statements (Loc, Func_Stmts));
2339 -- Insert the newly generated function in the code. This is analyzed
2340 -- with all checks off, since we have completed all the checks.
2342 -- Note that this does *not* fix the array concatenation bug when the
2343 -- low bound is Integer'first sibce that bug comes from the pointer
2344 -- dereferencing an unconstrained array. An there we need a constraint
2345 -- check to make sure the length of the concatenated array is ok. ???
2347 Insert_Action (Cnode, Func_Body, Suppress => All_Checks);
2349 -- Construct list of arguments for the function call
2352 Operand := First (Opnds);
2353 for I in 1 .. Nb_Opnds loop
2354 Append_To (Params, Relocate_Node (Operand));
2358 -- Insert the function call
2362 Make_Function_Call (Loc, New_Reference_To (Func_Id, Loc), Params));
2364 Analyze_And_Resolve (Cnode, Base_Typ);
2365 Set_Is_Inlined (Func_Id);
2366 end Expand_Concatenate_Other;
2368 -------------------------------
2369 -- Expand_Concatenate_String --
2370 -------------------------------
2372 procedure Expand_Concatenate_String (Cnode : Node_Id; Opnds : List_Id) is
2373 Loc : constant Source_Ptr := Sloc (Cnode);
2374 Opnd1 : constant Node_Id := First (Opnds);
2375 Opnd2 : constant Node_Id := Next (Opnd1);
2376 Typ1 : constant Entity_Id := Base_Type (Etype (Opnd1));
2377 Typ2 : constant Entity_Id := Base_Type (Etype (Opnd2));
2380 -- RE_Id value for function to be called
2383 -- In all cases, we build a call to a routine giving the list of
2384 -- arguments as the parameter list to the routine.
2386 case List_Length (Opnds) is
2388 if Typ1 = Standard_Character then
2389 if Typ2 = Standard_Character then
2390 R := RE_Str_Concat_CC;
2393 pragma Assert (Typ2 = Standard_String);
2394 R := RE_Str_Concat_CS;
2397 elsif Typ1 = Standard_String then
2398 if Typ2 = Standard_Character then
2399 R := RE_Str_Concat_SC;
2402 pragma Assert (Typ2 = Standard_String);
2406 -- If we have anything other than Standard_Character or
2407 -- Standard_String, then we must have had a serious error
2408 -- earlier, so we just abandon the attempt at expansion.
2411 pragma Assert (Serious_Errors_Detected > 0);
2416 R := RE_Str_Concat_3;
2419 R := RE_Str_Concat_4;
2422 R := RE_Str_Concat_5;
2426 raise Program_Error;
2429 -- Now generate the appropriate call
2432 Make_Function_Call (Sloc (Cnode),
2433 Name => New_Occurrence_Of (RTE (R), Loc),
2434 Parameter_Associations => Opnds));
2436 Analyze_And_Resolve (Cnode, Standard_String);
2439 when RE_Not_Available =>
2441 end Expand_Concatenate_String;
2443 ------------------------
2444 -- Expand_N_Allocator --
2445 ------------------------
2447 procedure Expand_N_Allocator (N : Node_Id) is
2448 PtrT : constant Entity_Id := Etype (N);
2449 Dtyp : constant Entity_Id := Designated_Type (PtrT);
2451 Loc : constant Source_Ptr := Sloc (N);
2456 -- RM E.2.3(22). We enforce that the expected type of an allocator
2457 -- shall not be a remote access-to-class-wide-limited-private type
2459 -- Why is this being done at expansion time, seems clearly wrong ???
2461 Validate_Remote_Access_To_Class_Wide_Type (N);
2463 -- Set the Storage Pool
2465 Set_Storage_Pool (N, Associated_Storage_Pool (Root_Type (PtrT)));
2467 if Present (Storage_Pool (N)) then
2468 if Is_RTE (Storage_Pool (N), RE_SS_Pool) then
2470 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
2473 elsif Is_Class_Wide_Type (Etype (Storage_Pool (N))) then
2474 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
2477 Set_Procedure_To_Call (N,
2478 Find_Prim_Op (Etype (Storage_Pool (N)), Name_Allocate));
2482 -- Under certain circumstances we can replace an allocator by an
2483 -- access to statically allocated storage. The conditions, as noted
2484 -- in AARM 3.10 (10c) are as follows:
2486 -- Size and initial value is known at compile time
2487 -- Access type is access-to-constant
2489 -- The allocator is not part of a constraint on a record component,
2490 -- because in that case the inserted actions are delayed until the
2491 -- record declaration is fully analyzed, which is too late for the
2492 -- analysis of the rewritten allocator.
2494 if Is_Access_Constant (PtrT)
2495 and then Nkind (Expression (N)) = N_Qualified_Expression
2496 and then Compile_Time_Known_Value (Expression (Expression (N)))
2497 and then Size_Known_At_Compile_Time (Etype (Expression
2499 and then not Is_Record_Type (Current_Scope)
2501 -- Here we can do the optimization. For the allocator
2505 -- We insert an object declaration
2507 -- Tnn : aliased x := y;
2509 -- and replace the allocator by Tnn'Unrestricted_Access.
2510 -- Tnn is marked as requiring static allocation.
2513 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
2515 Desig := Subtype_Mark (Expression (N));
2517 -- If context is constrained, use constrained subtype directly,
2518 -- so that the constant is not labelled as having a nomimally
2519 -- unconstrained subtype.
2521 if Entity (Desig) = Base_Type (Dtyp) then
2522 Desig := New_Occurrence_Of (Dtyp, Loc);
2526 Make_Object_Declaration (Loc,
2527 Defining_Identifier => Temp,
2528 Aliased_Present => True,
2529 Constant_Present => Is_Access_Constant (PtrT),
2530 Object_Definition => Desig,
2531 Expression => Expression (Expression (N))));
2534 Make_Attribute_Reference (Loc,
2535 Prefix => New_Occurrence_Of (Temp, Loc),
2536 Attribute_Name => Name_Unrestricted_Access));
2538 Analyze_And_Resolve (N, PtrT);
2540 -- We set the variable as statically allocated, since we don't
2541 -- want it going on the stack of the current procedure!
2543 Set_Is_Statically_Allocated (Temp);
2547 -- Handle case of qualified expression (other than optimization above)
2549 if Nkind (Expression (N)) = N_Qualified_Expression then
2550 Expand_Allocator_Expression (N);
2552 -- If the allocator is for a type which requires initialization, and
2553 -- there is no initial value (i.e. operand is a subtype indication
2554 -- rather than a qualifed expression), then we must generate a call
2555 -- to the initialization routine. This is done using an expression
2558 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
2560 -- Here ptr_T is the pointer type for the allocator, and T is the
2561 -- subtype of the allocator. A special case arises if the designated
2562 -- type of the access type is a task or contains tasks. In this case
2563 -- the call to Init (Temp.all ...) is replaced by code that ensures
2564 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
2565 -- for details). In addition, if the type T is a task T, then the
2566 -- first argument to Init must be converted to the task record type.
2570 T : constant Entity_Id := Entity (Expression (N));
2578 Temp_Decl : Node_Id;
2579 Temp_Type : Entity_Id;
2580 Attach_Level : Uint;
2583 if No_Initialization (N) then
2586 -- Case of no initialization procedure present
2588 elsif not Has_Non_Null_Base_Init_Proc (T) then
2590 -- Case of simple initialization required
2592 if Needs_Simple_Initialization (T) then
2593 Rewrite (Expression (N),
2594 Make_Qualified_Expression (Loc,
2595 Subtype_Mark => New_Occurrence_Of (T, Loc),
2596 Expression => Get_Simple_Init_Val (T, Loc)));
2598 Analyze_And_Resolve (Expression (Expression (N)), T);
2599 Analyze_And_Resolve (Expression (N), T);
2600 Set_Paren_Count (Expression (Expression (N)), 1);
2601 Expand_N_Allocator (N);
2603 -- No initialization required
2609 -- Case of initialization procedure present, must be called
2612 Init := Base_Init_Proc (T);
2615 Make_Defining_Identifier (Loc, New_Internal_Name ('P'));
2617 -- Construct argument list for the initialization routine call
2618 -- The CPP constructor needs the address directly
2620 if Is_CPP_Class (T) then
2621 Arg1 := New_Reference_To (Temp, Loc);
2626 Make_Explicit_Dereference (Loc,
2627 Prefix => New_Reference_To (Temp, Loc));
2628 Set_Assignment_OK (Arg1);
2631 -- The initialization procedure expects a specific type.
2632 -- if the context is access to class wide, indicate that
2633 -- the object being allocated has the right specific type.
2635 if Is_Class_Wide_Type (Dtyp) then
2636 Arg1 := Unchecked_Convert_To (T, Arg1);
2640 -- If designated type is a concurrent type or if it is a
2641 -- private type whose definition is a concurrent type,
2642 -- the first argument in the Init routine has to be
2643 -- unchecked conversion to the corresponding record type.
2644 -- If the designated type is a derived type, we also
2645 -- convert the argument to its root type.
2647 if Is_Concurrent_Type (T) then
2649 Unchecked_Convert_To (Corresponding_Record_Type (T), Arg1);
2651 elsif Is_Private_Type (T)
2652 and then Present (Full_View (T))
2653 and then Is_Concurrent_Type (Full_View (T))
2656 Unchecked_Convert_To
2657 (Corresponding_Record_Type (Full_View (T)), Arg1);
2659 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
2662 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
2665 Arg1 := OK_Convert_To (Etype (Ftyp), Arg1);
2666 Set_Etype (Arg1, Ftyp);
2670 Args := New_List (Arg1);
2672 -- For the task case, pass the Master_Id of the access type
2673 -- as the value of the _Master parameter, and _Chain as the
2674 -- value of the _Chain parameter (_Chain will be defined as
2675 -- part of the generated code for the allocator).
2677 if Has_Task (T) then
2678 if No (Master_Id (Base_Type (PtrT))) then
2680 -- The designated type was an incomplete type, and
2681 -- the access type did not get expanded. Salvage
2684 Expand_N_Full_Type_Declaration
2685 (Parent (Base_Type (PtrT)));
2688 -- If the context of the allocator is a declaration or
2689 -- an assignment, we can generate a meaningful image for
2690 -- it, even though subsequent assignments might remove
2691 -- the connection between task and entity. We build this
2692 -- image when the left-hand side is a simple variable,
2693 -- a simple indexed assignment or a simple selected
2696 if Nkind (Parent (N)) = N_Assignment_Statement then
2698 Nam : constant Node_Id := Name (Parent (N));
2701 if Is_Entity_Name (Nam) then
2703 Build_Task_Image_Decls (
2706 (Entity (Nam), Sloc (Nam)), T);
2708 elsif (Nkind (Nam) = N_Indexed_Component
2709 or else Nkind (Nam) = N_Selected_Component)
2710 and then Is_Entity_Name (Prefix (Nam))
2713 Build_Task_Image_Decls
2714 (Loc, Nam, Etype (Prefix (Nam)));
2716 Decls := Build_Task_Image_Decls (Loc, T, T);
2720 elsif Nkind (Parent (N)) = N_Object_Declaration then
2722 Build_Task_Image_Decls (
2723 Loc, Defining_Identifier (Parent (N)), T);
2726 Decls := Build_Task_Image_Decls (Loc, T, T);
2731 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
2732 Append_To (Args, Make_Identifier (Loc, Name_uChain));
2734 Decl := Last (Decls);
2736 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
2738 -- Has_Task is false, Decls not used
2744 -- Add discriminants if discriminated type
2746 if Has_Discriminants (T) then
2747 Discr := First_Elmt (Discriminant_Constraint (T));
2749 while Present (Discr) loop
2750 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2754 elsif Is_Private_Type (T)
2755 and then Present (Full_View (T))
2756 and then Has_Discriminants (Full_View (T))
2759 First_Elmt (Discriminant_Constraint (Full_View (T)));
2761 while Present (Discr) loop
2762 Append (New_Copy_Tree (Elists.Node (Discr)), Args);
2767 -- We set the allocator as analyzed so that when we analyze the
2768 -- expression actions node, we do not get an unwanted recursive
2769 -- expansion of the allocator expression.
2771 Set_Analyzed (N, True);
2772 Node := Relocate_Node (N);
2774 -- Here is the transformation:
2776 -- output: Temp : constant ptr_T := new T;
2777 -- Init (Temp.all, ...);
2778 -- <CTRL> Attach_To_Final_List (Finalizable (Temp.all));
2779 -- <CTRL> Initialize (Finalizable (Temp.all));
2781 -- Here ptr_T is the pointer type for the allocator, and T
2782 -- is the subtype of the allocator.
2785 Make_Object_Declaration (Loc,
2786 Defining_Identifier => Temp,
2787 Constant_Present => True,
2788 Object_Definition => New_Reference_To (Temp_Type, Loc),
2789 Expression => Node);
2791 Set_Assignment_OK (Temp_Decl);
2793 if Is_CPP_Class (T) then
2794 Set_Aliased_Present (Temp_Decl);
2797 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
2799 -- If the designated type is task type or contains tasks,
2800 -- Create block to activate created tasks, and insert
2801 -- declaration for Task_Image variable ahead of call.
2803 if Has_Task (T) then
2805 L : constant List_Id := New_List;
2809 Build_Task_Allocate_Block (L, Node, Args);
2812 Insert_List_Before (First (Declarations (Blk)), Decls);
2813 Insert_Actions (N, L);
2818 Make_Procedure_Call_Statement (Loc,
2819 Name => New_Reference_To (Init, Loc),
2820 Parameter_Associations => Args));
2823 if Controlled_Type (T) then
2824 Flist := Get_Allocator_Final_List (N, Base_Type (T), PtrT);
2825 if Ekind (PtrT) = E_Anonymous_Access_Type then
2826 Attach_Level := Uint_1;
2828 Attach_Level := Uint_2;
2832 Ref => New_Copy_Tree (Arg1),
2835 With_Attach => Make_Integer_Literal (Loc,
2839 if Is_CPP_Class (T) then
2841 Make_Attribute_Reference (Loc,
2842 Prefix => New_Reference_To (Temp, Loc),
2843 Attribute_Name => Name_Unchecked_Access));
2845 Rewrite (N, New_Reference_To (Temp, Loc));
2848 Analyze_And_Resolve (N, PtrT);
2854 when RE_Not_Available =>
2856 end Expand_N_Allocator;
2858 -----------------------
2859 -- Expand_N_And_Then --
2860 -----------------------
2862 -- Expand into conditional expression if Actions present, and also
2863 -- deal with optimizing case of arguments being True or False.
2865 procedure Expand_N_And_Then (N : Node_Id) is
2866 Loc : constant Source_Ptr := Sloc (N);
2867 Typ : constant Entity_Id := Etype (N);
2868 Left : constant Node_Id := Left_Opnd (N);
2869 Right : constant Node_Id := Right_Opnd (N);
2873 -- Deal with non-standard booleans
2875 if Is_Boolean_Type (Typ) then
2876 Adjust_Condition (Left);
2877 Adjust_Condition (Right);
2878 Set_Etype (N, Standard_Boolean);
2881 -- Check for cases of left argument is True or False
2883 if Nkind (Left) = N_Identifier then
2885 -- If left argument is True, change (True and then Right) to Right.
2886 -- Any actions associated with Right will be executed unconditionally
2887 -- and can thus be inserted into the tree unconditionally.
2889 if Entity (Left) = Standard_True then
2890 if Present (Actions (N)) then
2891 Insert_Actions (N, Actions (N));
2895 Adjust_Result_Type (N, Typ);
2898 -- If left argument is False, change (False and then Right) to
2899 -- False. In this case we can forget the actions associated with
2900 -- Right, since they will never be executed.
2902 elsif Entity (Left) = Standard_False then
2903 Kill_Dead_Code (Right);
2904 Kill_Dead_Code (Actions (N));
2905 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2906 Adjust_Result_Type (N, Typ);
2911 -- If Actions are present, we expand
2913 -- left and then right
2917 -- if left then right else false end
2919 -- with the actions becoming the Then_Actions of the conditional
2920 -- expression. This conditional expression is then further expanded
2921 -- (and will eventually disappear)
2923 if Present (Actions (N)) then
2924 Actlist := Actions (N);
2926 Make_Conditional_Expression (Loc,
2927 Expressions => New_List (
2930 New_Occurrence_Of (Standard_False, Loc))));
2932 Set_Then_Actions (N, Actlist);
2933 Analyze_And_Resolve (N, Standard_Boolean);
2934 Adjust_Result_Type (N, Typ);
2938 -- No actions present, check for cases of right argument True/False
2940 if Nkind (Right) = N_Identifier then
2942 -- Change (Left and then True) to Left. Note that we know there
2943 -- are no actions associated with the True operand, since we
2944 -- just checked for this case above.
2946 if Entity (Right) = Standard_True then
2949 -- Change (Left and then False) to False, making sure to preserve
2950 -- any side effects associated with the Left operand.
2952 elsif Entity (Right) = Standard_False then
2953 Remove_Side_Effects (Left);
2955 (N, New_Occurrence_Of (Standard_False, Loc));
2959 Adjust_Result_Type (N, Typ);
2960 end Expand_N_And_Then;
2962 -------------------------------------
2963 -- Expand_N_Conditional_Expression --
2964 -------------------------------------
2966 -- Expand into expression actions if then/else actions present
2968 procedure Expand_N_Conditional_Expression (N : Node_Id) is
2969 Loc : constant Source_Ptr := Sloc (N);
2970 Cond : constant Node_Id := First (Expressions (N));
2971 Thenx : constant Node_Id := Next (Cond);
2972 Elsex : constant Node_Id := Next (Thenx);
2973 Typ : constant Entity_Id := Etype (N);
2978 -- If either then or else actions are present, then given:
2980 -- if cond then then-expr else else-expr end
2982 -- we insert the following sequence of actions (using Insert_Actions):
2987 -- Cnn := then-expr;
2993 -- and replace the conditional expression by a reference to Cnn
2995 if Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
2996 Cnn := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
2999 Make_Implicit_If_Statement (N,
3000 Condition => Relocate_Node (Cond),
3002 Then_Statements => New_List (
3003 Make_Assignment_Statement (Sloc (Thenx),
3004 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
3005 Expression => Relocate_Node (Thenx))),
3007 Else_Statements => New_List (
3008 Make_Assignment_Statement (Sloc (Elsex),
3009 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
3010 Expression => Relocate_Node (Elsex))));
3012 Set_Assignment_OK (Name (First (Then_Statements (New_If))));
3013 Set_Assignment_OK (Name (First (Else_Statements (New_If))));
3015 if Present (Then_Actions (N)) then
3017 (First (Then_Statements (New_If)), Then_Actions (N));
3020 if Present (Else_Actions (N)) then
3022 (First (Else_Statements (New_If)), Else_Actions (N));
3025 Rewrite (N, New_Occurrence_Of (Cnn, Loc));
3028 Make_Object_Declaration (Loc,
3029 Defining_Identifier => Cnn,
3030 Object_Definition => New_Occurrence_Of (Typ, Loc)));
3032 Insert_Action (N, New_If);
3033 Analyze_And_Resolve (N, Typ);
3035 end Expand_N_Conditional_Expression;
3037 -----------------------------------
3038 -- Expand_N_Explicit_Dereference --
3039 -----------------------------------
3041 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
3043 -- The only processing required is an insertion of an explicit
3044 -- dereference call for the checked storage pool case.
3046 Insert_Dereference_Action (Prefix (N));
3047 end Expand_N_Explicit_Dereference;
3053 procedure Expand_N_In (N : Node_Id) is
3054 Loc : constant Source_Ptr := Sloc (N);
3055 Rtyp : constant Entity_Id := Etype (N);
3056 Lop : constant Node_Id := Left_Opnd (N);
3057 Rop : constant Node_Id := Right_Opnd (N);
3058 Static : constant Boolean := Is_OK_Static_Expression (N);
3060 procedure Substitute_Valid_Check;
3061 -- Replaces node N by Lop'Valid. This is done when we have an explicit
3062 -- test for the left operand being in range of its subtype.
3064 ----------------------------
3065 -- Substitute_Valid_Check --
3066 ----------------------------
3068 procedure Substitute_Valid_Check is
3071 Make_Attribute_Reference (Loc,
3072 Prefix => Relocate_Node (Lop),
3073 Attribute_Name => Name_Valid));
3075 Analyze_And_Resolve (N, Rtyp);
3077 Error_Msg_N ("?explicit membership test may be optimized away", N);
3078 Error_Msg_N ("\?use ''Valid attribute instead", N);
3080 end Substitute_Valid_Check;
3082 -- Start of processing for Expand_N_In
3085 -- Check case of explicit test for an expression in range of its
3086 -- subtype. This is suspicious usage and we replace it with a 'Valid
3087 -- test and give a warning.
3089 if Is_Scalar_Type (Etype (Lop))
3090 and then Nkind (Rop) in N_Has_Entity
3091 and then Etype (Lop) = Entity (Rop)
3092 and then Comes_From_Source (N)
3094 Substitute_Valid_Check;
3098 -- Case of explicit range
3100 if Nkind (Rop) = N_Range then
3102 Lo : constant Node_Id := Low_Bound (Rop);
3103 Hi : constant Node_Id := High_Bound (Rop);
3105 Lo_Orig : constant Node_Id := Original_Node (Lo);
3106 Hi_Orig : constant Node_Id := Original_Node (Hi);
3108 Lcheck : constant Compare_Result := Compile_Time_Compare (Lop, Lo);
3109 Ucheck : constant Compare_Result := Compile_Time_Compare (Lop, Hi);
3112 -- If test is explicit x'first .. x'last, replace by valid check
3114 if Is_Scalar_Type (Etype (Lop))
3115 and then Nkind (Lo_Orig) = N_Attribute_Reference
3116 and then Attribute_Name (Lo_Orig) = Name_First
3117 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
3118 and then Entity (Prefix (Lo_Orig)) = Etype (Lop)
3119 and then Nkind (Hi_Orig) = N_Attribute_Reference
3120 and then Attribute_Name (Hi_Orig) = Name_Last
3121 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
3122 and then Entity (Prefix (Hi_Orig)) = Etype (Lop)
3123 and then Comes_From_Source (N)
3125 Substitute_Valid_Check;
3129 -- If we have an explicit range, do a bit of optimization based
3130 -- on range analysis (we may be able to kill one or both checks).
3132 -- If either check is known to fail, replace result by False since
3133 -- the other check does not matter. Preserve the static flag for
3134 -- legality checks, because we are constant-folding beyond RM 4.9.
3136 if Lcheck = LT or else Ucheck = GT then
3138 New_Reference_To (Standard_False, Loc));
3139 Analyze_And_Resolve (N, Rtyp);
3140 Set_Is_Static_Expression (N, Static);
3143 -- If both checks are known to succeed, replace result
3144 -- by True, since we know we are in range.
3146 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
3148 New_Reference_To (Standard_True, Loc));
3149 Analyze_And_Resolve (N, Rtyp);
3150 Set_Is_Static_Expression (N, Static);
3153 -- If lower bound check succeeds and upper bound check is
3154 -- not known to succeed or fail, then replace the range check
3155 -- with a comparison against the upper bound.
3157 elsif Lcheck in Compare_GE then
3161 Right_Opnd => High_Bound (Rop)));
3162 Analyze_And_Resolve (N, Rtyp);
3165 -- If upper bound check succeeds and lower bound check is
3166 -- not known to succeed or fail, then replace the range check
3167 -- with a comparison against the lower bound.
3169 elsif Ucheck in Compare_LE then
3173 Right_Opnd => Low_Bound (Rop)));
3174 Analyze_And_Resolve (N, Rtyp);
3179 -- For all other cases of an explicit range, nothing to be done
3183 -- Here right operand is a subtype mark
3187 Typ : Entity_Id := Etype (Rop);
3188 Is_Acc : constant Boolean := Is_Access_Type (Typ);
3189 Obj : Node_Id := Lop;
3190 Cond : Node_Id := Empty;
3193 Remove_Side_Effects (Obj);
3195 -- For tagged type, do tagged membership operation
3197 if Is_Tagged_Type (Typ) then
3199 -- No expansion will be performed when Java_VM, as the
3200 -- JVM back end will handle the membership tests directly
3201 -- (tags are not explicitly represented in Java objects,
3202 -- so the normal tagged membership expansion is not what
3206 Rewrite (N, Tagged_Membership (N));
3207 Analyze_And_Resolve (N, Rtyp);
3212 -- If type is scalar type, rewrite as x in t'first .. t'last
3213 -- This reason we do this is that the bounds may have the wrong
3214 -- type if they come from the original type definition.
3216 elsif Is_Scalar_Type (Typ) then
3220 Make_Attribute_Reference (Loc,
3221 Attribute_Name => Name_First,
3222 Prefix => New_Reference_To (Typ, Loc)),
3225 Make_Attribute_Reference (Loc,
3226 Attribute_Name => Name_Last,
3227 Prefix => New_Reference_To (Typ, Loc))));
3228 Analyze_And_Resolve (N, Rtyp);
3231 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
3232 -- a membership test if the subtype mark denotes a constrained
3233 -- Unchecked_Union subtype and the expression lacks inferable
3236 elsif Is_Unchecked_Union (Base_Type (Typ))
3237 and then Is_Constrained (Typ)
3238 and then not Has_Inferable_Discriminants (Lop)
3241 Make_Raise_Program_Error (Loc,
3242 Reason => PE_Unchecked_Union_Restriction));
3244 -- Prevent Gigi from generating incorrect code by rewriting
3245 -- the test as a standard False.
3248 New_Occurrence_Of (Standard_False, Loc));
3253 -- Here we have a non-scalar type
3256 Typ := Designated_Type (Typ);
3259 if not Is_Constrained (Typ) then
3261 New_Reference_To (Standard_True, Loc));
3262 Analyze_And_Resolve (N, Rtyp);
3264 -- For the constrained array case, we have to check the
3265 -- subscripts for an exact match if the lengths are
3266 -- non-zero (the lengths must match in any case).
3268 elsif Is_Array_Type (Typ) then
3270 Check_Subscripts : declare
3271 function Construct_Attribute_Reference
3274 Dim : Nat) return Node_Id;
3275 -- Build attribute reference E'Nam(Dim)
3277 -----------------------------------
3278 -- Construct_Attribute_Reference --
3279 -----------------------------------
3281 function Construct_Attribute_Reference
3284 Dim : Nat) return Node_Id
3288 Make_Attribute_Reference (Loc,
3290 Attribute_Name => Nam,
3291 Expressions => New_List (
3292 Make_Integer_Literal (Loc, Dim)));
3293 end Construct_Attribute_Reference;
3295 -- Start processing for Check_Subscripts
3298 for J in 1 .. Number_Dimensions (Typ) loop
3299 Evolve_And_Then (Cond,
3302 Construct_Attribute_Reference
3303 (Duplicate_Subexpr_No_Checks (Obj),
3306 Construct_Attribute_Reference
3307 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
3309 Evolve_And_Then (Cond,
3312 Construct_Attribute_Reference
3313 (Duplicate_Subexpr_No_Checks (Obj),
3316 Construct_Attribute_Reference
3317 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
3326 Right_Opnd => Make_Null (Loc)),
3327 Right_Opnd => Cond);
3331 Analyze_And_Resolve (N, Rtyp);
3332 end Check_Subscripts;
3334 -- These are the cases where constraint checks may be
3335 -- required, e.g. records with possible discriminants
3338 -- Expand the test into a series of discriminant comparisons.
3339 -- The expression that is built is the negation of the one
3340 -- that is used for checking discriminant constraints.
3342 Obj := Relocate_Node (Left_Opnd (N));
3344 if Has_Discriminants (Typ) then
3345 Cond := Make_Op_Not (Loc,
3346 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
3349 Cond := Make_Or_Else (Loc,
3353 Right_Opnd => Make_Null (Loc)),
3354 Right_Opnd => Cond);
3358 Cond := New_Occurrence_Of (Standard_True, Loc);
3362 Analyze_And_Resolve (N, Rtyp);
3368 --------------------------------
3369 -- Expand_N_Indexed_Component --
3370 --------------------------------
3372 procedure Expand_N_Indexed_Component (N : Node_Id) is
3373 Loc : constant Source_Ptr := Sloc (N);
3374 Typ : constant Entity_Id := Etype (N);
3375 P : constant Node_Id := Prefix (N);
3376 T : constant Entity_Id := Etype (P);
3379 -- A special optimization, if we have an indexed component that
3380 -- is selecting from a slice, then we can eliminate the slice,
3381 -- since, for example, x (i .. j)(k) is identical to x(k). The
3382 -- only difference is the range check required by the slice. The
3383 -- range check for the slice itself has already been generated.
3384 -- The range check for the subscripting operation is ensured
3385 -- by converting the subject to the subtype of the slice.
3387 -- This optimization not only generates better code, avoiding
3388 -- slice messing especially in the packed case, but more importantly
3389 -- bypasses some problems in handling this peculiar case, for
3390 -- example, the issue of dealing specially with object renamings.
3392 if Nkind (P) = N_Slice then
3394 Make_Indexed_Component (Loc,
3395 Prefix => Prefix (P),
3396 Expressions => New_List (
3398 (Etype (First_Index (Etype (P))),
3399 First (Expressions (N))))));
3400 Analyze_And_Resolve (N, Typ);
3404 -- If the prefix is an access type, then we unconditionally rewrite
3405 -- if as an explicit deference. This simplifies processing for several
3406 -- cases, including packed array cases and certain cases in which
3407 -- checks must be generated. We used to try to do this only when it
3408 -- was necessary, but it cleans up the code to do it all the time.
3410 if Is_Access_Type (T) then
3411 Insert_Explicit_Dereference (P);
3412 Analyze_And_Resolve (P, Designated_Type (T));
3415 -- Generate index and validity checks
3417 Generate_Index_Checks (N);
3419 if Validity_Checks_On and then Validity_Check_Subscripts then
3420 Apply_Subscript_Validity_Checks (N);
3423 -- All done for the non-packed case
3425 if not Is_Packed (Etype (Prefix (N))) then
3429 -- For packed arrays that are not bit-packed (i.e. the case of an array
3430 -- with one or more index types with a non-coniguous enumeration type),
3431 -- we can always use the normal packed element get circuit.
3433 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
3434 Expand_Packed_Element_Reference (N);
3438 -- For a reference to a component of a bit packed array, we have to
3439 -- convert it to a reference to the corresponding Packed_Array_Type.
3440 -- We only want to do this for simple references, and not for:
3442 -- Left side of assignment, or prefix of left side of assignment,
3443 -- or prefix of the prefix, to handle packed arrays of packed arrays,
3444 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
3446 -- Renaming objects in renaming associations
3447 -- This case is handled when a use of the renamed variable occurs
3449 -- Actual parameters for a procedure call
3450 -- This case is handled in Exp_Ch6.Expand_Actuals
3452 -- The second expression in a 'Read attribute reference
3454 -- The prefix of an address or size attribute reference
3456 -- The following circuit detects these exceptions
3459 Child : Node_Id := N;
3460 Parnt : Node_Id := Parent (N);
3464 if Nkind (Parnt) = N_Unchecked_Expression then
3467 elsif Nkind (Parnt) = N_Object_Renaming_Declaration
3468 or else Nkind (Parnt) = N_Procedure_Call_Statement
3469 or else (Nkind (Parnt) = N_Parameter_Association
3471 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
3475 elsif Nkind (Parnt) = N_Attribute_Reference
3476 and then (Attribute_Name (Parnt) = Name_Address
3478 Attribute_Name (Parnt) = Name_Size)
3479 and then Prefix (Parnt) = Child
3483 elsif Nkind (Parnt) = N_Assignment_Statement
3484 and then Name (Parnt) = Child
3488 -- If the expression is an index of an indexed component,
3489 -- it must be expanded regardless of context.
3491 elsif Nkind (Parnt) = N_Indexed_Component
3492 and then Child /= Prefix (Parnt)
3494 Expand_Packed_Element_Reference (N);
3497 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
3498 and then Name (Parent (Parnt)) = Parnt
3502 elsif Nkind (Parnt) = N_Attribute_Reference
3503 and then Attribute_Name (Parnt) = Name_Read
3504 and then Next (First (Expressions (Parnt))) = Child
3508 elsif (Nkind (Parnt) = N_Indexed_Component
3509 or else Nkind (Parnt) = N_Selected_Component)
3510 and then Prefix (Parnt) = Child
3515 Expand_Packed_Element_Reference (N);
3519 -- Keep looking up tree for unchecked expression, or if we are
3520 -- the prefix of a possible assignment left side.
3523 Parnt := Parent (Child);
3526 end Expand_N_Indexed_Component;
3528 ---------------------
3529 -- Expand_N_Not_In --
3530 ---------------------
3532 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
3533 -- can be done. This avoids needing to duplicate this expansion code.
3535 procedure Expand_N_Not_In (N : Node_Id) is
3536 Loc : constant Source_Ptr := Sloc (N);
3537 Typ : constant Entity_Id := Etype (N);
3538 Cfs : constant Boolean := Comes_From_Source (N);
3545 Left_Opnd => Left_Opnd (N),
3546 Right_Opnd => Right_Opnd (N))));
3548 -- We want this tp appear as coming from source if original does (see
3549 -- tranformations in Expand_N_In).
3551 Set_Comes_From_Source (N, Cfs);
3552 Set_Comes_From_Source (Right_Opnd (N), Cfs);
3554 -- Now analyze tranformed node
3556 Analyze_And_Resolve (N, Typ);
3557 end Expand_N_Not_In;
3563 -- The only replacement required is for the case of a null of type
3564 -- that is an access to protected subprogram. We represent such
3565 -- access values as a record, and so we must replace the occurrence
3566 -- of null by the equivalent record (with a null address and a null
3567 -- pointer in it), so that the backend creates the proper value.
3569 procedure Expand_N_Null (N : Node_Id) is
3570 Loc : constant Source_Ptr := Sloc (N);
3571 Typ : constant Entity_Id := Etype (N);
3575 if Ekind (Typ) = E_Access_Protected_Subprogram_Type then
3577 Make_Aggregate (Loc,
3578 Expressions => New_List (
3579 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
3583 Analyze_And_Resolve (N, Equivalent_Type (Typ));
3585 -- For subsequent semantic analysis, the node must retain its
3586 -- type. Gigi in any case replaces this type by the corresponding
3587 -- record type before processing the node.
3593 when RE_Not_Available =>
3597 ---------------------
3598 -- Expand_N_Op_Abs --
3599 ---------------------
3601 procedure Expand_N_Op_Abs (N : Node_Id) is
3602 Loc : constant Source_Ptr := Sloc (N);
3603 Expr : constant Node_Id := Right_Opnd (N);
3606 Unary_Op_Validity_Checks (N);
3608 -- Deal with software overflow checking
3610 if not Backend_Overflow_Checks_On_Target
3611 and then Is_Signed_Integer_Type (Etype (N))
3612 and then Do_Overflow_Check (N)
3614 -- The only case to worry about is when the argument is
3615 -- equal to the largest negative number, so what we do is
3616 -- to insert the check:
3618 -- [constraint_error when Expr = typ'Base'First]
3620 -- with the usual Duplicate_Subexpr use coding for expr
3623 Make_Raise_Constraint_Error (Loc,
3626 Left_Opnd => Duplicate_Subexpr (Expr),
3628 Make_Attribute_Reference (Loc,
3630 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
3631 Attribute_Name => Name_First)),
3632 Reason => CE_Overflow_Check_Failed));
3635 -- Vax floating-point types case
3637 if Vax_Float (Etype (N)) then
3638 Expand_Vax_Arith (N);
3640 end Expand_N_Op_Abs;
3642 ---------------------
3643 -- Expand_N_Op_Add --
3644 ---------------------
3646 procedure Expand_N_Op_Add (N : Node_Id) is
3647 Typ : constant Entity_Id := Etype (N);
3650 Binary_Op_Validity_Checks (N);
3652 -- N + 0 = 0 + N = N for integer types
3654 if Is_Integer_Type (Typ) then
3655 if Compile_Time_Known_Value (Right_Opnd (N))
3656 and then Expr_Value (Right_Opnd (N)) = Uint_0
3658 Rewrite (N, Left_Opnd (N));
3661 elsif Compile_Time_Known_Value (Left_Opnd (N))
3662 and then Expr_Value (Left_Opnd (N)) = Uint_0
3664 Rewrite (N, Right_Opnd (N));
3669 -- Arithmetic overflow checks for signed integer/fixed point types
3671 if Is_Signed_Integer_Type (Typ)
3672 or else Is_Fixed_Point_Type (Typ)
3674 Apply_Arithmetic_Overflow_Check (N);
3677 -- Vax floating-point types case
3679 elsif Vax_Float (Typ) then
3680 Expand_Vax_Arith (N);
3682 end Expand_N_Op_Add;
3684 ---------------------
3685 -- Expand_N_Op_And --
3686 ---------------------
3688 procedure Expand_N_Op_And (N : Node_Id) is
3689 Typ : constant Entity_Id := Etype (N);
3692 Binary_Op_Validity_Checks (N);
3694 if Is_Array_Type (Etype (N)) then
3695 Expand_Boolean_Operator (N);
3697 elsif Is_Boolean_Type (Etype (N)) then
3698 Adjust_Condition (Left_Opnd (N));
3699 Adjust_Condition (Right_Opnd (N));
3700 Set_Etype (N, Standard_Boolean);
3701 Adjust_Result_Type (N, Typ);
3703 end Expand_N_Op_And;
3705 ------------------------
3706 -- Expand_N_Op_Concat --
3707 ------------------------
3709 Max_Available_String_Operands : Int := -1;
3710 -- This is initialized the first time this routine is called. It records
3711 -- a value of 0,2,3,4,5 depending on what Str_Concat_n procedures are
3712 -- available in the run-time:
3715 -- 2 RE_Str_Concat available, RE_Str_Concat_3 not available
3716 -- 3 RE_Str_Concat/Concat_2 available, RE_Str_Concat_4 not available
3717 -- 4 RE_Str_Concat/Concat_2/3 available, RE_Str_Concat_5 not available
3718 -- 5 All routines including RE_Str_Concat_5 available
3720 Char_Concat_Available : Boolean;
3721 -- Records if the routines RE_Str_Concat_CC/CS/SC are available. True if
3722 -- all three are available, False if any one of these is unavailable.
3724 procedure Expand_N_Op_Concat (N : Node_Id) is
3726 -- List of operands to be concatenated
3729 -- Single operand for concatenation
3732 -- Node which is to be replaced by the result of concatenating
3733 -- the nodes in the list Opnds.
3736 -- Array type of concatenation result type
3739 -- Component type of concatenation represented by Cnode
3742 -- Initialize global variables showing run-time status
3744 if Max_Available_String_Operands < 1 then
3745 if not RTE_Available (RE_Str_Concat) then
3746 Max_Available_String_Operands := 0;
3747 elsif not RTE_Available (RE_Str_Concat_3) then
3748 Max_Available_String_Operands := 2;
3749 elsif not RTE_Available (RE_Str_Concat_4) then
3750 Max_Available_String_Operands := 3;
3751 elsif not RTE_Available (RE_Str_Concat_5) then
3752 Max_Available_String_Operands := 4;
3754 Max_Available_String_Operands := 5;
3757 Char_Concat_Available :=
3758 RTE_Available (RE_Str_Concat_CC)
3760 RTE_Available (RE_Str_Concat_CS)
3762 RTE_Available (RE_Str_Concat_SC);
3765 -- Ensure validity of both operands
3767 Binary_Op_Validity_Checks (N);
3769 -- If we are the left operand of a concatenation higher up the
3770 -- tree, then do nothing for now, since we want to deal with a
3771 -- series of concatenations as a unit.
3773 if Nkind (Parent (N)) = N_Op_Concat
3774 and then N = Left_Opnd (Parent (N))
3779 -- We get here with a concatenation whose left operand may be a
3780 -- concatenation itself with a consistent type. We need to process
3781 -- these concatenation operands from left to right, which means
3782 -- from the deepest node in the tree to the highest node.
3785 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
3786 Cnode := Left_Opnd (Cnode);
3789 -- Now Opnd is the deepest Opnd, and its parents are the concatenation
3790 -- nodes above, so now we process bottom up, doing the operations. We
3791 -- gather a string that is as long as possible up to five operands
3793 -- The outer loop runs more than once if there are more than five
3794 -- concatenations of type Standard.String, the most we handle for
3795 -- this case, or if more than one concatenation type is involved.
3798 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
3799 Set_Parent (Opnds, N);
3801 -- The inner loop gathers concatenation operands. We gather any
3802 -- number of these in the non-string case, or if no concatenation
3803 -- routines are available for string (since in that case we will
3804 -- treat string like any other non-string case). Otherwise we only
3805 -- gather as many operands as can be handled by the available
3806 -- procedures in the run-time library (normally 5, but may be
3807 -- less for the configurable run-time case).
3809 Inner : while Cnode /= N
3810 and then (Base_Type (Etype (Cnode)) /= Standard_String
3812 Max_Available_String_Operands = 0
3814 List_Length (Opnds) <
3815 Max_Available_String_Operands)
3816 and then Base_Type (Etype (Cnode)) =
3817 Base_Type (Etype (Parent (Cnode)))
3819 Cnode := Parent (Cnode);
3820 Append (Right_Opnd (Cnode), Opnds);
3823 -- Here we process the collected operands. First we convert
3824 -- singleton operands to singleton aggregates. This is skipped
3825 -- however for the case of two operands of type String, since
3826 -- we have special routines for these cases.
3828 Atyp := Base_Type (Etype (Cnode));
3829 Ctyp := Base_Type (Component_Type (Etype (Cnode)));
3831 if (List_Length (Opnds) > 2 or else Atyp /= Standard_String)
3832 or else not Char_Concat_Available
3834 Opnd := First (Opnds);
3836 if Base_Type (Etype (Opnd)) = Ctyp then
3838 Make_Aggregate (Sloc (Cnode),
3839 Expressions => New_List (Relocate_Node (Opnd))));
3840 Analyze_And_Resolve (Opnd, Atyp);
3844 exit when No (Opnd);
3848 -- Now call appropriate continuation routine
3850 if Atyp = Standard_String
3851 and then Max_Available_String_Operands > 0
3853 Expand_Concatenate_String (Cnode, Opnds);
3855 Expand_Concatenate_Other (Cnode, Opnds);
3858 exit Outer when Cnode = N;
3859 Cnode := Parent (Cnode);
3861 end Expand_N_Op_Concat;
3863 ------------------------
3864 -- Expand_N_Op_Divide --
3865 ------------------------
3867 procedure Expand_N_Op_Divide (N : Node_Id) is
3868 Loc : constant Source_Ptr := Sloc (N);
3869 Ltyp : constant Entity_Id := Etype (Left_Opnd (N));
3870 Rtyp : constant Entity_Id := Etype (Right_Opnd (N));
3871 Typ : Entity_Id := Etype (N);
3874 Binary_Op_Validity_Checks (N);
3876 -- N / 1 = N for integer types
3878 if Is_Integer_Type (Typ)
3879 and then Compile_Time_Known_Value (Right_Opnd (N))
3880 and then Expr_Value (Right_Opnd (N)) = Uint_1
3882 Rewrite (N, Left_Opnd (N));
3886 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
3887 -- Is_Power_Of_2_For_Shift is set means that we know that our left
3888 -- operand is an unsigned integer, as required for this to work.
3890 if Nkind (Right_Opnd (N)) = N_Op_Expon
3891 and then Is_Power_Of_2_For_Shift (Right_Opnd (N))
3893 -- We cannot do this transformation in configurable run time mode if we
3894 -- have 64-bit -- integers and long shifts are not available.
3898 or else Support_Long_Shifts_On_Target)
3901 Make_Op_Shift_Right (Loc,
3902 Left_Opnd => Left_Opnd (N),
3904 Convert_To (Standard_Natural, Right_Opnd (Right_Opnd (N)))));
3905 Analyze_And_Resolve (N, Typ);
3909 -- Do required fixup of universal fixed operation
3911 if Typ = Universal_Fixed then
3912 Fixup_Universal_Fixed_Operation (N);
3916 -- Divisions with fixed-point results
3918 if Is_Fixed_Point_Type (Typ) then
3920 -- No special processing if Treat_Fixed_As_Integer is set,
3921 -- since from a semantic point of view such operations are
3922 -- simply integer operations and will be treated that way.
3924 if not Treat_Fixed_As_Integer (N) then
3925 if Is_Integer_Type (Rtyp) then
3926 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
3928 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
3932 -- Other cases of division of fixed-point operands. Again we
3933 -- exclude the case where Treat_Fixed_As_Integer is set.
3935 elsif (Is_Fixed_Point_Type (Ltyp) or else
3936 Is_Fixed_Point_Type (Rtyp))
3937 and then not Treat_Fixed_As_Integer (N)
3939 if Is_Integer_Type (Typ) then
3940 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
3942 pragma Assert (Is_Floating_Point_Type (Typ));
3943 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
3946 -- Mixed-mode operations can appear in a non-static universal
3947 -- context, in which case the integer argument must be converted
3950 elsif Typ = Universal_Real
3951 and then Is_Integer_Type (Rtyp)
3953 Rewrite (Right_Opnd (N),
3954 Convert_To (Universal_Real, Relocate_Node (Right_Opnd (N))));
3956 Analyze_And_Resolve (Right_Opnd (N), Universal_Real);
3958 elsif Typ = Universal_Real
3959 and then Is_Integer_Type (Ltyp)
3961 Rewrite (Left_Opnd (N),
3962 Convert_To (Universal_Real, Relocate_Node (Left_Opnd (N))));
3964 Analyze_And_Resolve (Left_Opnd (N), Universal_Real);
3966 -- Non-fixed point cases, do integer zero divide and overflow checks
3968 elsif Is_Integer_Type (Typ) then
3969 Apply_Divide_Check (N);
3971 -- Check for 64-bit division available
3973 if Esize (Ltyp) > 32
3974 and then not Support_64_Bit_Divides_On_Target
3976 Error_Msg_CRT ("64-bit division", N);
3979 -- Deal with Vax_Float
3981 elsif Vax_Float (Typ) then
3982 Expand_Vax_Arith (N);
3985 end Expand_N_Op_Divide;
3987 --------------------
3988 -- Expand_N_Op_Eq --
3989 --------------------
3991 procedure Expand_N_Op_Eq (N : Node_Id) is
3992 Loc : constant Source_Ptr := Sloc (N);
3993 Typ : constant Entity_Id := Etype (N);
3994 Lhs : constant Node_Id := Left_Opnd (N);
3995 Rhs : constant Node_Id := Right_Opnd (N);
3996 Bodies : constant List_Id := New_List;
3997 A_Typ : constant Entity_Id := Etype (Lhs);
3999 Typl : Entity_Id := A_Typ;
4000 Op_Name : Entity_Id;
4003 procedure Build_Equality_Call (Eq : Entity_Id);
4004 -- If a constructed equality exists for the type or for its parent,
4005 -- build and analyze call, adding conversions if the operation is
4008 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
4009 -- Determines whether a type has a subcompoment of an unconstrained
4010 -- Unchecked_Union subtype. Typ is a record type.
4012 -------------------------
4013 -- Build_Equality_Call --
4014 -------------------------
4016 procedure Build_Equality_Call (Eq : Entity_Id) is
4017 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
4018 L_Exp : Node_Id := Relocate_Node (Lhs);
4019 R_Exp : Node_Id := Relocate_Node (Rhs);
4022 if Base_Type (Op_Type) /= Base_Type (A_Typ)
4023 and then not Is_Class_Wide_Type (A_Typ)
4025 L_Exp := OK_Convert_To (Op_Type, L_Exp);
4026 R_Exp := OK_Convert_To (Op_Type, R_Exp);
4029 -- If we have an Unchecked_Union, we need to add the inferred
4030 -- discriminant values as actuals in the function call. At this
4031 -- point, the expansion has determined that both operands have
4032 -- inferable discriminants.
4034 if Is_Unchecked_Union (Op_Type) then
4036 Lhs_Type : constant Node_Id := Etype (L_Exp);
4037 Rhs_Type : constant Node_Id := Etype (R_Exp);
4038 Lhs_Discr_Val : Node_Id;
4039 Rhs_Discr_Val : Node_Id;
4042 -- Per-object constrained selected components require special
4043 -- attention. If the enclosing scope of the component is an
4044 -- Unchecked_Union, we cannot reference its discriminants
4045 -- directly. This is why we use the two extra parameters of
4046 -- the equality function of the enclosing Unchecked_Union.
4048 -- type UU_Type (Discr : Integer := 0) is
4051 -- pragma Unchecked_Union (UU_Type);
4053 -- 1. Unchecked_Union enclosing record:
4055 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
4057 -- Comp : UU_Type (Discr);
4059 -- end Enclosing_UU_Type;
4060 -- pragma Unchecked_Union (Enclosing_UU_Type);
4062 -- Obj1 : Enclosing_UU_Type;
4063 -- Obj2 : Enclosing_UU_Type (1);
4065 -- [. . .] Obj1 = Obj2 [. . .]
4069 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
4071 -- A and B are the formal parameters of the equality function
4072 -- of Enclosing_UU_Type. The function always has two extra
4073 -- formals to capture the inferred discriminant values.
4075 -- 2. Non-Unchecked_Union enclosing record:
4078 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
4081 -- Comp : UU_Type (Discr);
4083 -- end Enclosing_Non_UU_Type;
4085 -- Obj1 : Enclosing_Non_UU_Type;
4086 -- Obj2 : Enclosing_Non_UU_Type (1);
4088 -- ... Obj1 = Obj2 ...
4092 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
4093 -- obj1.discr, obj2.discr)) then
4095 -- In this case we can directly reference the discriminants of
4096 -- the enclosing record.
4100 if Nkind (Lhs) = N_Selected_Component
4101 and then Has_Per_Object_Constraint
4102 (Entity (Selector_Name (Lhs)))
4104 -- Enclosing record is an Unchecked_Union, use formal A
4106 if Is_Unchecked_Union (Scope
4107 (Entity (Selector_Name (Lhs))))
4110 Make_Identifier (Loc,
4113 -- Enclosing record is of a non-Unchecked_Union type, it is
4114 -- possible to reference the discriminant.
4118 Make_Selected_Component (Loc,
4119 Prefix => Prefix (Lhs),
4122 (Get_Discriminant_Value
4123 (First_Discriminant (Lhs_Type),
4125 Stored_Constraint (Lhs_Type))));
4128 -- Comment needed here ???
4131 -- Infer the discriminant value
4135 (Get_Discriminant_Value
4136 (First_Discriminant (Lhs_Type),
4138 Stored_Constraint (Lhs_Type)));
4143 if Nkind (Rhs) = N_Selected_Component
4144 and then Has_Per_Object_Constraint
4145 (Entity (Selector_Name (Rhs)))
4147 if Is_Unchecked_Union
4148 (Scope (Entity (Selector_Name (Rhs))))
4151 Make_Identifier (Loc,
4156 Make_Selected_Component (Loc,
4157 Prefix => Prefix (Rhs),
4159 New_Copy (Get_Discriminant_Value (
4160 First_Discriminant (Rhs_Type),
4162 Stored_Constraint (Rhs_Type))));
4167 New_Copy (Get_Discriminant_Value (
4168 First_Discriminant (Rhs_Type),
4170 Stored_Constraint (Rhs_Type)));
4175 Make_Function_Call (Loc,
4176 Name => New_Reference_To (Eq, Loc),
4177 Parameter_Associations => New_List (
4184 -- Normal case, not an unchecked union
4188 Make_Function_Call (Loc,
4189 Name => New_Reference_To (Eq, Loc),
4190 Parameter_Associations => New_List (L_Exp, R_Exp)));
4193 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4194 end Build_Equality_Call;
4196 ------------------------------------
4197 -- Has_Unconstrained_UU_Component --
4198 ------------------------------------
4200 function Has_Unconstrained_UU_Component
4201 (Typ : Node_Id) return Boolean
4203 Tdef : constant Node_Id :=
4204 Type_Definition (Declaration_Node (Base_Type (Typ)));
4208 function Component_Is_Unconstrained_UU
4209 (Comp : Node_Id) return Boolean;
4210 -- Determines whether the subtype of the component is an
4211 -- unconstrained Unchecked_Union.
4213 function Variant_Is_Unconstrained_UU
4214 (Variant : Node_Id) return Boolean;
4215 -- Determines whether a component of the variant has an unconstrained
4216 -- Unchecked_Union subtype.
4218 -----------------------------------
4219 -- Component_Is_Unconstrained_UU --
4220 -----------------------------------
4222 function Component_Is_Unconstrained_UU
4223 (Comp : Node_Id) return Boolean
4226 if Nkind (Comp) /= N_Component_Declaration then
4231 Sindic : constant Node_Id :=
4232 Subtype_Indication (Component_Definition (Comp));
4235 -- Unconstrained nominal type. In the case of a constraint
4236 -- present, the node kind would have been N_Subtype_Indication.
4238 if Nkind (Sindic) = N_Identifier then
4239 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
4244 end Component_Is_Unconstrained_UU;
4246 ---------------------------------
4247 -- Variant_Is_Unconstrained_UU --
4248 ---------------------------------
4250 function Variant_Is_Unconstrained_UU
4251 (Variant : Node_Id) return Boolean
4253 Clist : constant Node_Id := Component_List (Variant);
4256 if Is_Empty_List (Component_Items (Clist)) then
4260 -- We only need to test one component
4263 Comp : Node_Id := First (Component_Items (Clist));
4266 while Present (Comp) loop
4267 if Component_Is_Unconstrained_UU (Comp) then
4275 -- None of the components withing the variant were of
4276 -- unconstrained Unchecked_Union type.
4279 end Variant_Is_Unconstrained_UU;
4281 -- Start of processing for Has_Unconstrained_UU_Component
4284 if Null_Present (Tdef) then
4288 Clist := Component_List (Tdef);
4289 Vpart := Variant_Part (Clist);
4291 -- Inspect available components
4293 if Present (Component_Items (Clist)) then
4295 Comp : Node_Id := First (Component_Items (Clist));
4298 while Present (Comp) loop
4300 -- One component is sufficent
4302 if Component_Is_Unconstrained_UU (Comp) then
4311 -- Inspect available components withing variants
4313 if Present (Vpart) then
4315 Variant : Node_Id := First (Variants (Vpart));
4318 while Present (Variant) loop
4320 -- One component within a variant is sufficent
4322 if Variant_Is_Unconstrained_UU (Variant) then
4331 -- Neither the available components, nor the components inside the
4332 -- variant parts were of an unconstrained Unchecked_Union subtype.
4335 end Has_Unconstrained_UU_Component;
4337 -- Start of processing for Expand_N_Op_Eq
4340 Binary_Op_Validity_Checks (N);
4342 if Ekind (Typl) = E_Private_Type then
4343 Typl := Underlying_Type (Typl);
4344 elsif Ekind (Typl) = E_Private_Subtype then
4345 Typl := Underlying_Type (Base_Type (Typl));
4350 -- It may happen in error situations that the underlying type is not
4351 -- set. The error will be detected later, here we just defend the
4358 Typl := Base_Type (Typl);
4360 -- Boolean types (requiring handling of non-standard case)
4362 if Is_Boolean_Type (Typl) then
4363 Adjust_Condition (Left_Opnd (N));
4364 Adjust_Condition (Right_Opnd (N));
4365 Set_Etype (N, Standard_Boolean);
4366 Adjust_Result_Type (N, Typ);
4370 elsif Is_Array_Type (Typl) then
4372 -- If we are doing full validity checking, then expand out array
4373 -- comparisons to make sure that we check the array elements.
4375 if Validity_Check_Operands then
4377 Save_Force_Validity_Checks : constant Boolean :=
4378 Force_Validity_Checks;
4380 Force_Validity_Checks := True;
4382 Expand_Array_Equality
4384 Relocate_Node (Lhs),
4385 Relocate_Node (Rhs),
4388 Insert_Actions (N, Bodies);
4389 Analyze_And_Resolve (N, Standard_Boolean);
4390 Force_Validity_Checks := Save_Force_Validity_Checks;
4393 -- Packed case where both operands are known aligned
4395 elsif Is_Bit_Packed_Array (Typl)
4396 and then not Is_Possibly_Unaligned_Object (Lhs)
4397 and then not Is_Possibly_Unaligned_Object (Rhs)
4399 Expand_Packed_Eq (N);
4401 -- Where the component type is elementary we can use a block bit
4402 -- comparison (if supported on the target) exception in the case
4403 -- of floating-point (negative zero issues require element by
4404 -- element comparison), and atomic types (where we must be sure
4405 -- to load elements independently) and possibly unaligned arrays.
4407 elsif Is_Elementary_Type (Component_Type (Typl))
4408 and then not Is_Floating_Point_Type (Component_Type (Typl))
4409 and then not Is_Atomic (Component_Type (Typl))
4410 and then not Is_Possibly_Unaligned_Object (Lhs)
4411 and then not Is_Possibly_Unaligned_Object (Rhs)
4412 and then Support_Composite_Compare_On_Target
4416 -- For composite and floating-point cases, expand equality loop
4417 -- to make sure of using proper comparisons for tagged types,
4418 -- and correctly handling the floating-point case.
4422 Expand_Array_Equality
4424 Relocate_Node (Lhs),
4425 Relocate_Node (Rhs),
4428 Insert_Actions (N, Bodies, Suppress => All_Checks);
4429 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4434 elsif Is_Record_Type (Typl) then
4436 -- For tagged types, use the primitive "="
4438 if Is_Tagged_Type (Typl) then
4440 -- If this is derived from an untagged private type completed
4441 -- with a tagged type, it does not have a full view, so we
4442 -- use the primitive operations of the private type.
4443 -- This check should no longer be necessary when these
4444 -- types receive their full views ???
4446 if Is_Private_Type (A_Typ)
4447 and then not Is_Tagged_Type (A_Typ)
4448 and then Is_Derived_Type (A_Typ)
4449 and then No (Full_View (A_Typ))
4451 -- Search for equality operation, checking that the
4452 -- operands have the same type. Note that we must find
4453 -- a matching entry, or something is very wrong!
4455 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
4457 while Present (Prim) loop
4458 exit when Chars (Node (Prim)) = Name_Op_Eq
4459 and then Etype (First_Formal (Node (Prim))) =
4460 Etype (Next_Formal (First_Formal (Node (Prim))))
4462 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4467 pragma Assert (Present (Prim));
4468 Op_Name := Node (Prim);
4470 -- Find the type's predefined equality or an overriding
4471 -- user-defined equality. The reason for not simply calling
4472 -- Find_Prim_Op here is that there may be a user-defined
4473 -- overloaded equality op that precedes the equality that
4474 -- we want, so we have to explicitly search (e.g., there
4475 -- could be an equality with two different parameter types).
4478 if Is_Class_Wide_Type (Typl) then
4479 Typl := Root_Type (Typl);
4482 Prim := First_Elmt (Primitive_Operations (Typl));
4483 while Present (Prim) loop
4484 exit when Chars (Node (Prim)) = Name_Op_Eq
4485 and then Etype (First_Formal (Node (Prim))) =
4486 Etype (Next_Formal (First_Formal (Node (Prim))))
4488 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
4493 pragma Assert (Present (Prim));
4494 Op_Name := Node (Prim);
4497 Build_Equality_Call (Op_Name);
4499 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
4500 -- predefined equality operator for a type which has a subcomponent
4501 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
4503 elsif Has_Unconstrained_UU_Component (Typl) then
4505 Make_Raise_Program_Error (Loc,
4506 Reason => PE_Unchecked_Union_Restriction));
4508 -- Prevent Gigi from generating incorrect code by rewriting the
4509 -- equality as a standard False.
4512 New_Occurrence_Of (Standard_False, Loc));
4514 elsif Is_Unchecked_Union (Typl) then
4516 -- If we can infer the discriminants of the operands, we make a
4517 -- call to the TSS equality function.
4519 if Has_Inferable_Discriminants (Lhs)
4521 Has_Inferable_Discriminants (Rhs)
4524 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4527 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
4528 -- the predefined equality operator for an Unchecked_Union type
4529 -- if either of the operands lack inferable discriminants.
4532 Make_Raise_Program_Error (Loc,
4533 Reason => PE_Unchecked_Union_Restriction));
4535 -- Prevent Gigi from generating incorrect code by rewriting
4536 -- the equality as a standard False.
4539 New_Occurrence_Of (Standard_False, Loc));
4543 -- If a type support function is present (for complex cases), use it
4545 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
4547 (TSS (Root_Type (Typl), TSS_Composite_Equality));
4549 -- Otherwise expand the component by component equality. Note that
4550 -- we never use block-bit coparisons for records, because of the
4551 -- problems with gaps. The backend will often be able to recombine
4552 -- the separate comparisons that we generate here.
4555 Remove_Side_Effects (Lhs);
4556 Remove_Side_Effects (Rhs);
4558 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
4560 Insert_Actions (N, Bodies, Suppress => All_Checks);
4561 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
4565 -- Test if result is known at compile time
4567 Rewrite_Comparison (N);
4569 -- If we still have comparison for Vax_Float, process it
4571 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
4572 Expand_Vax_Comparison (N);
4577 -----------------------
4578 -- Expand_N_Op_Expon --
4579 -----------------------
4581 procedure Expand_N_Op_Expon (N : Node_Id) is
4582 Loc : constant Source_Ptr := Sloc (N);
4583 Typ : constant Entity_Id := Etype (N);
4584 Rtyp : constant Entity_Id := Root_Type (Typ);
4585 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
4586 Bastyp : constant Node_Id := Etype (Base);
4587 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
4588 Exptyp : constant Entity_Id := Etype (Exp);
4589 Ovflo : constant Boolean := Do_Overflow_Check (N);
4598 Binary_Op_Validity_Checks (N);
4600 -- If either operand is of a private type, then we have the use of
4601 -- an intrinsic operator, and we get rid of the privateness, by using
4602 -- root types of underlying types for the actual operation. Otherwise
4603 -- the private types will cause trouble if we expand multiplications
4604 -- or shifts etc. We also do this transformation if the result type
4605 -- is different from the base type.
4607 if Is_Private_Type (Etype (Base))
4609 Is_Private_Type (Typ)
4611 Is_Private_Type (Exptyp)
4613 Rtyp /= Root_Type (Bastyp)
4616 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
4617 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
4621 Unchecked_Convert_To (Typ,
4623 Left_Opnd => Unchecked_Convert_To (Bt, Base),
4624 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
4625 Analyze_And_Resolve (N, Typ);
4630 -- Test for case of known right argument
4632 if Compile_Time_Known_Value (Exp) then
4633 Expv := Expr_Value (Exp);
4635 -- We only fold small non-negative exponents. You might think we
4636 -- could fold small negative exponents for the real case, but we
4637 -- can't because we are required to raise Constraint_Error for
4638 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
4639 -- See ACVC test C4A012B.
4641 if Expv >= 0 and then Expv <= 4 then
4643 -- X ** 0 = 1 (or 1.0)
4646 if Ekind (Typ) in Integer_Kind then
4647 Xnode := Make_Integer_Literal (Loc, Intval => 1);
4649 Xnode := Make_Real_Literal (Loc, Ureal_1);
4661 Make_Op_Multiply (Loc,
4662 Left_Opnd => Duplicate_Subexpr (Base),
4663 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4665 -- X ** 3 = X * X * X
4669 Make_Op_Multiply (Loc,
4671 Make_Op_Multiply (Loc,
4672 Left_Opnd => Duplicate_Subexpr (Base),
4673 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
4674 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
4677 -- En : constant base'type := base * base;
4683 Make_Defining_Identifier (Loc, New_Internal_Name ('E'));
4685 Insert_Actions (N, New_List (
4686 Make_Object_Declaration (Loc,
4687 Defining_Identifier => Temp,
4688 Constant_Present => True,
4689 Object_Definition => New_Reference_To (Typ, Loc),
4691 Make_Op_Multiply (Loc,
4692 Left_Opnd => Duplicate_Subexpr (Base),
4693 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)))));
4696 Make_Op_Multiply (Loc,
4697 Left_Opnd => New_Reference_To (Temp, Loc),
4698 Right_Opnd => New_Reference_To (Temp, Loc));
4702 Analyze_And_Resolve (N, Typ);
4707 -- Case of (2 ** expression) appearing as an argument of an integer
4708 -- multiplication, or as the right argument of a division of a non-
4709 -- negative integer. In such cases we leave the node untouched, setting
4710 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
4711 -- of the higher level node converts it into a shift.
4713 if Nkind (Base) = N_Integer_Literal
4714 and then Intval (Base) = 2
4715 and then Is_Integer_Type (Root_Type (Exptyp))
4716 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
4717 and then Is_Unsigned_Type (Exptyp)
4719 and then Nkind (Parent (N)) in N_Binary_Op
4722 P : constant Node_Id := Parent (N);
4723 L : constant Node_Id := Left_Opnd (P);
4724 R : constant Node_Id := Right_Opnd (P);
4727 if (Nkind (P) = N_Op_Multiply
4729 ((Is_Integer_Type (Etype (L)) and then R = N)
4731 (Is_Integer_Type (Etype (R)) and then L = N))
4732 and then not Do_Overflow_Check (P))
4735 (Nkind (P) = N_Op_Divide
4736 and then Is_Integer_Type (Etype (L))
4737 and then Is_Unsigned_Type (Etype (L))
4739 and then not Do_Overflow_Check (P))
4741 Set_Is_Power_Of_2_For_Shift (N);
4747 -- Fall through if exponentiation must be done using a runtime routine
4749 -- First deal with modular case
4751 if Is_Modular_Integer_Type (Rtyp) then
4753 -- Non-binary case, we call the special exponentiation routine for
4754 -- the non-binary case, converting the argument to Long_Long_Integer
4755 -- and passing the modulus value. Then the result is converted back
4756 -- to the base type.
4758 if Non_Binary_Modulus (Rtyp) then
4761 Make_Function_Call (Loc,
4762 Name => New_Reference_To (RTE (RE_Exp_Modular), Loc),
4763 Parameter_Associations => New_List (
4764 Convert_To (Standard_Integer, Base),
4765 Make_Integer_Literal (Loc, Modulus (Rtyp)),
4768 -- Binary case, in this case, we call one of two routines, either
4769 -- the unsigned integer case, or the unsigned long long integer
4770 -- case, with a final "and" operation to do the required mod.
4773 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
4774 Ent := RTE (RE_Exp_Unsigned);
4776 Ent := RTE (RE_Exp_Long_Long_Unsigned);
4783 Make_Function_Call (Loc,
4784 Name => New_Reference_To (Ent, Loc),
4785 Parameter_Associations => New_List (
4786 Convert_To (Etype (First_Formal (Ent)), Base),
4789 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
4793 -- Common exit point for modular type case
4795 Analyze_And_Resolve (N, Typ);
4798 -- Signed integer cases, done using either Integer or Long_Long_Integer.
4799 -- It is not worth having routines for Short_[Short_]Integer, since for
4800 -- most machines it would not help, and it would generate more code that
4801 -- might need certification in the HI-E case.
4803 -- In the integer cases, we have two routines, one for when overflow
4804 -- checks are required, and one when they are not required, since
4805 -- there is a real gain in ommitting checks on many machines.
4807 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
4808 or else (Rtyp = Base_Type (Standard_Long_Integer)
4810 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
4811 or else (Rtyp = Universal_Integer)
4813 Etyp := Standard_Long_Long_Integer;
4816 Rent := RE_Exp_Long_Long_Integer;
4818 Rent := RE_Exn_Long_Long_Integer;
4821 elsif Is_Signed_Integer_Type (Rtyp) then
4822 Etyp := Standard_Integer;
4825 Rent := RE_Exp_Integer;
4827 Rent := RE_Exn_Integer;
4830 -- Floating-point cases, always done using Long_Long_Float. We do not
4831 -- need separate routines for the overflow case here, since in the case
4832 -- of floating-point, we generate infinities anyway as a rule (either
4833 -- that or we automatically trap overflow), and if there is an infinity
4834 -- generated and a range check is required, the check will fail anyway.
4837 pragma Assert (Is_Floating_Point_Type (Rtyp));
4838 Etyp := Standard_Long_Long_Float;
4839 Rent := RE_Exn_Long_Long_Float;
4842 -- Common processing for integer cases and floating-point cases.
4843 -- If we are in the right type, we can call runtime routine directly
4846 and then Rtyp /= Universal_Integer
4847 and then Rtyp /= Universal_Real
4850 Make_Function_Call (Loc,
4851 Name => New_Reference_To (RTE (Rent), Loc),
4852 Parameter_Associations => New_List (Base, Exp)));
4854 -- Otherwise we have to introduce conversions (conversions are also
4855 -- required in the universal cases, since the runtime routine is
4856 -- typed using one of the standard types.
4861 Make_Function_Call (Loc,
4862 Name => New_Reference_To (RTE (Rent), Loc),
4863 Parameter_Associations => New_List (
4864 Convert_To (Etyp, Base),
4868 Analyze_And_Resolve (N, Typ);
4872 when RE_Not_Available =>
4874 end Expand_N_Op_Expon;
4876 --------------------
4877 -- Expand_N_Op_Ge --
4878 --------------------
4880 procedure Expand_N_Op_Ge (N : Node_Id) is
4881 Typ : constant Entity_Id := Etype (N);
4882 Op1 : constant Node_Id := Left_Opnd (N);
4883 Op2 : constant Node_Id := Right_Opnd (N);
4884 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4887 Binary_Op_Validity_Checks (N);
4889 if Is_Array_Type (Typ1) then
4890 Expand_Array_Comparison (N);
4894 if Is_Boolean_Type (Typ1) then
4895 Adjust_Condition (Op1);
4896 Adjust_Condition (Op2);
4897 Set_Etype (N, Standard_Boolean);
4898 Adjust_Result_Type (N, Typ);
4901 Rewrite_Comparison (N);
4903 -- If we still have comparison, and Vax_Float type, process it
4905 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
4906 Expand_Vax_Comparison (N);
4911 --------------------
4912 -- Expand_N_Op_Gt --
4913 --------------------
4915 procedure Expand_N_Op_Gt (N : Node_Id) is
4916 Typ : constant Entity_Id := Etype (N);
4917 Op1 : constant Node_Id := Left_Opnd (N);
4918 Op2 : constant Node_Id := Right_Opnd (N);
4919 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4922 Binary_Op_Validity_Checks (N);
4924 if Is_Array_Type (Typ1) then
4925 Expand_Array_Comparison (N);
4929 if Is_Boolean_Type (Typ1) then
4930 Adjust_Condition (Op1);
4931 Adjust_Condition (Op2);
4932 Set_Etype (N, Standard_Boolean);
4933 Adjust_Result_Type (N, Typ);
4936 Rewrite_Comparison (N);
4938 -- If we still have comparison, and Vax_Float type, process it
4940 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
4941 Expand_Vax_Comparison (N);
4946 --------------------
4947 -- Expand_N_Op_Le --
4948 --------------------
4950 procedure Expand_N_Op_Le (N : Node_Id) is
4951 Typ : constant Entity_Id := Etype (N);
4952 Op1 : constant Node_Id := Left_Opnd (N);
4953 Op2 : constant Node_Id := Right_Opnd (N);
4954 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4957 Binary_Op_Validity_Checks (N);
4959 if Is_Array_Type (Typ1) then
4960 Expand_Array_Comparison (N);
4964 if Is_Boolean_Type (Typ1) then
4965 Adjust_Condition (Op1);
4966 Adjust_Condition (Op2);
4967 Set_Etype (N, Standard_Boolean);
4968 Adjust_Result_Type (N, Typ);
4971 Rewrite_Comparison (N);
4973 -- If we still have comparison, and Vax_Float type, process it
4975 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
4976 Expand_Vax_Comparison (N);
4981 --------------------
4982 -- Expand_N_Op_Lt --
4983 --------------------
4985 procedure Expand_N_Op_Lt (N : Node_Id) is
4986 Typ : constant Entity_Id := Etype (N);
4987 Op1 : constant Node_Id := Left_Opnd (N);
4988 Op2 : constant Node_Id := Right_Opnd (N);
4989 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
4992 Binary_Op_Validity_Checks (N);
4994 if Is_Array_Type (Typ1) then
4995 Expand_Array_Comparison (N);
4999 if Is_Boolean_Type (Typ1) then
5000 Adjust_Condition (Op1);
5001 Adjust_Condition (Op2);
5002 Set_Etype (N, Standard_Boolean);
5003 Adjust_Result_Type (N, Typ);
5006 Rewrite_Comparison (N);
5008 -- If we still have comparison, and Vax_Float type, process it
5010 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
5011 Expand_Vax_Comparison (N);
5016 -----------------------
5017 -- Expand_N_Op_Minus --
5018 -----------------------
5020 procedure Expand_N_Op_Minus (N : Node_Id) is
5021 Loc : constant Source_Ptr := Sloc (N);
5022 Typ : constant Entity_Id := Etype (N);
5025 Unary_Op_Validity_Checks (N);
5027 if not Backend_Overflow_Checks_On_Target
5028 and then Is_Signed_Integer_Type (Etype (N))
5029 and then Do_Overflow_Check (N)
5031 -- Software overflow checking expands -expr into (0 - expr)
5034 Make_Op_Subtract (Loc,
5035 Left_Opnd => Make_Integer_Literal (Loc, 0),
5036 Right_Opnd => Right_Opnd (N)));
5038 Analyze_And_Resolve (N, Typ);
5040 -- Vax floating-point types case
5042 elsif Vax_Float (Etype (N)) then
5043 Expand_Vax_Arith (N);
5045 end Expand_N_Op_Minus;
5047 ---------------------
5048 -- Expand_N_Op_Mod --
5049 ---------------------
5051 procedure Expand_N_Op_Mod (N : Node_Id) is
5052 Loc : constant Source_Ptr := Sloc (N);
5053 Typ : constant Entity_Id := Etype (N);
5054 Left : constant Node_Id := Left_Opnd (N);
5055 Right : constant Node_Id := Right_Opnd (N);
5056 DOC : constant Boolean := Do_Overflow_Check (N);
5057 DDC : constant Boolean := Do_Division_Check (N);
5068 Binary_Op_Validity_Checks (N);
5070 Determine_Range (Right, ROK, Rlo, Rhi);
5071 Determine_Range (Left, LOK, Llo, Lhi);
5073 -- Convert mod to rem if operands are known non-negative. We do this
5074 -- since it is quite likely that this will improve the quality of code,
5075 -- (the operation now corresponds to the hardware remainder), and it
5076 -- does not seem likely that it could be harmful.
5078 if LOK and then Llo >= 0
5080 ROK and then Rlo >= 0
5083 Make_Op_Rem (Sloc (N),
5084 Left_Opnd => Left_Opnd (N),
5085 Right_Opnd => Right_Opnd (N)));
5087 -- Instead of reanalyzing the node we do the analysis manually.
5088 -- This avoids anomalies when the replacement is done in an
5089 -- instance and is epsilon more efficient.
5091 Set_Entity (N, Standard_Entity (S_Op_Rem));
5093 Set_Do_Overflow_Check (N, DOC);
5094 Set_Do_Division_Check (N, DDC);
5095 Expand_N_Op_Rem (N);
5098 -- Otherwise, normal mod processing
5101 if Is_Integer_Type (Etype (N)) then
5102 Apply_Divide_Check (N);
5105 -- Apply optimization x mod 1 = 0. We don't really need that with
5106 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5107 -- certainly harmless.
5109 if Is_Integer_Type (Etype (N))
5110 and then Compile_Time_Known_Value (Right)
5111 and then Expr_Value (Right) = Uint_1
5113 Rewrite (N, Make_Integer_Literal (Loc, 0));
5114 Analyze_And_Resolve (N, Typ);
5118 -- Deal with annoying case of largest negative number remainder
5119 -- minus one. Gigi does not handle this case correctly, because
5120 -- it generates a divide instruction which may trap in this case.
5122 -- In fact the check is quite easy, if the right operand is -1,
5123 -- then the mod value is always 0, and we can just ignore the
5124 -- left operand completely in this case.
5126 -- The operand type may be private (e.g. in the expansion of an
5127 -- an intrinsic operation) so we must use the underlying type to
5128 -- get the bounds, and convert the literals explicitly.
5132 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5134 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5136 ((not LOK) or else (Llo = LLB))
5139 Make_Conditional_Expression (Loc,
5140 Expressions => New_List (
5142 Left_Opnd => Duplicate_Subexpr (Right),
5144 Unchecked_Convert_To (Typ,
5145 Make_Integer_Literal (Loc, -1))),
5146 Unchecked_Convert_To (Typ,
5147 Make_Integer_Literal (Loc, Uint_0)),
5148 Relocate_Node (N))));
5150 Set_Analyzed (Next (Next (First (Expressions (N)))));
5151 Analyze_And_Resolve (N, Typ);
5154 end Expand_N_Op_Mod;
5156 --------------------------
5157 -- Expand_N_Op_Multiply --
5158 --------------------------
5160 procedure Expand_N_Op_Multiply (N : Node_Id) is
5161 Loc : constant Source_Ptr := Sloc (N);
5162 Lop : constant Node_Id := Left_Opnd (N);
5163 Rop : constant Node_Id := Right_Opnd (N);
5165 Lp2 : constant Boolean :=
5166 Nkind (Lop) = N_Op_Expon
5167 and then Is_Power_Of_2_For_Shift (Lop);
5169 Rp2 : constant Boolean :=
5170 Nkind (Rop) = N_Op_Expon
5171 and then Is_Power_Of_2_For_Shift (Rop);
5173 Ltyp : constant Entity_Id := Etype (Lop);
5174 Rtyp : constant Entity_Id := Etype (Rop);
5175 Typ : Entity_Id := Etype (N);
5178 Binary_Op_Validity_Checks (N);
5180 -- Special optimizations for integer types
5182 if Is_Integer_Type (Typ) then
5184 -- N * 0 = 0 * N = 0 for integer types
5186 if (Compile_Time_Known_Value (Rop)
5187 and then Expr_Value (Rop) = Uint_0)
5189 (Compile_Time_Known_Value (Lop)
5190 and then Expr_Value (Lop) = Uint_0)
5192 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
5193 Analyze_And_Resolve (N, Typ);
5197 -- N * 1 = 1 * N = N for integer types
5199 -- This optimisation is not done if we are going to
5200 -- rewrite the product 1 * 2 ** N to a shift.
5202 if Compile_Time_Known_Value (Rop)
5203 and then Expr_Value (Rop) = Uint_1
5209 elsif Compile_Time_Known_Value (Lop)
5210 and then Expr_Value (Lop) = Uint_1
5218 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
5219 -- Is_Power_Of_2_For_Shift is set means that we know that our left
5220 -- operand is an integer, as required for this to work.
5225 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
5229 Left_Opnd => Make_Integer_Literal (Loc, 2),
5232 Left_Opnd => Right_Opnd (Lop),
5233 Right_Opnd => Right_Opnd (Rop))));
5234 Analyze_And_Resolve (N, Typ);
5239 Make_Op_Shift_Left (Loc,
5242 Convert_To (Standard_Natural, Right_Opnd (Rop))));
5243 Analyze_And_Resolve (N, Typ);
5247 -- Same processing for the operands the other way round
5251 Make_Op_Shift_Left (Loc,
5254 Convert_To (Standard_Natural, Right_Opnd (Lop))));
5255 Analyze_And_Resolve (N, Typ);
5259 -- Do required fixup of universal fixed operation
5261 if Typ = Universal_Fixed then
5262 Fixup_Universal_Fixed_Operation (N);
5266 -- Multiplications with fixed-point results
5268 if Is_Fixed_Point_Type (Typ) then
5270 -- No special processing if Treat_Fixed_As_Integer is set,
5271 -- since from a semantic point of view such operations are
5272 -- simply integer operations and will be treated that way.
5274 if not Treat_Fixed_As_Integer (N) then
5276 -- Case of fixed * integer => fixed
5278 if Is_Integer_Type (Rtyp) then
5279 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
5281 -- Case of integer * fixed => fixed
5283 elsif Is_Integer_Type (Ltyp) then
5284 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
5286 -- Case of fixed * fixed => fixed
5289 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
5293 -- Other cases of multiplication of fixed-point operands. Again
5294 -- we exclude the cases where Treat_Fixed_As_Integer flag is set.
5296 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
5297 and then not Treat_Fixed_As_Integer (N)
5299 if Is_Integer_Type (Typ) then
5300 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
5302 pragma Assert (Is_Floating_Point_Type (Typ));
5303 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
5306 -- Mixed-mode operations can appear in a non-static universal
5307 -- context, in which case the integer argument must be converted
5310 elsif Typ = Universal_Real
5311 and then Is_Integer_Type (Rtyp)
5313 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
5315 Analyze_And_Resolve (Rop, Universal_Real);
5317 elsif Typ = Universal_Real
5318 and then Is_Integer_Type (Ltyp)
5320 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
5322 Analyze_And_Resolve (Lop, Universal_Real);
5324 -- Non-fixed point cases, check software overflow checking required
5326 elsif Is_Signed_Integer_Type (Etype (N)) then
5327 Apply_Arithmetic_Overflow_Check (N);
5329 -- Deal with VAX float case
5331 elsif Vax_Float (Typ) then
5332 Expand_Vax_Arith (N);
5335 end Expand_N_Op_Multiply;
5337 --------------------
5338 -- Expand_N_Op_Ne --
5339 --------------------
5341 procedure Expand_N_Op_Ne (N : Node_Id) is
5342 Typ : constant Entity_Id := Etype (Left_Opnd (N));
5345 -- Case of elementary type with standard operator
5347 if Is_Elementary_Type (Typ)
5348 and then Sloc (Entity (N)) = Standard_Location
5350 Binary_Op_Validity_Checks (N);
5352 -- Boolean types (requiring handling of non-standard case)
5354 if Is_Boolean_Type (Typ) then
5355 Adjust_Condition (Left_Opnd (N));
5356 Adjust_Condition (Right_Opnd (N));
5357 Set_Etype (N, Standard_Boolean);
5358 Adjust_Result_Type (N, Typ);
5361 Rewrite_Comparison (N);
5363 -- If we still have comparison for Vax_Float, process it
5365 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
5366 Expand_Vax_Comparison (N);
5370 -- For all cases other than elementary types, we rewrite node as the
5371 -- negation of an equality operation, and reanalyze. The equality to be
5372 -- used is defined in the same scope and has the same signature. This
5373 -- signature must be set explicitly since in an instance it may not have
5374 -- the same visibility as in the generic unit. This avoids duplicating
5375 -- or factoring the complex code for record/array equality tests etc.
5379 Loc : constant Source_Ptr := Sloc (N);
5381 Ne : constant Entity_Id := Entity (N);
5384 Binary_Op_Validity_Checks (N);
5390 Left_Opnd => Left_Opnd (N),
5391 Right_Opnd => Right_Opnd (N)));
5392 Set_Paren_Count (Right_Opnd (Neg), 1);
5394 if Scope (Ne) /= Standard_Standard then
5395 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
5398 -- For navigation purposes, the inequality is treated as an
5399 -- implicit reference to the corresponding equality. Preserve the
5400 -- Comes_From_ source flag so that the proper Xref entry is
5403 Preserve_Comes_From_Source (Neg, N);
5404 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
5406 Analyze_And_Resolve (N, Standard_Boolean);
5411 ---------------------
5412 -- Expand_N_Op_Not --
5413 ---------------------
5415 -- If the argument is other than a Boolean array type, there is no
5416 -- special expansion required.
5418 -- For the packed case, we call the special routine in Exp_Pakd, except
5419 -- that if the component size is greater than one, we use the standard
5420 -- routine generating a gruesome loop (it is so peculiar to have packed
5421 -- arrays with non-standard Boolean representations anyway, so it does
5422 -- not matter that we do not handle this case efficiently).
5424 -- For the unpacked case (and for the special packed case where we have
5425 -- non standard Booleans, as discussed above), we generate and insert
5426 -- into the tree the following function definition:
5428 -- function Nnnn (A : arr) is
5431 -- for J in a'range loop
5432 -- B (J) := not A (J);
5437 -- Here arr is the actual subtype of the parameter (and hence always
5438 -- constrained). Then we replace the not with a call to this function.
5440 procedure Expand_N_Op_Not (N : Node_Id) is
5441 Loc : constant Source_Ptr := Sloc (N);
5442 Typ : constant Entity_Id := Etype (N);
5451 Func_Name : Entity_Id;
5452 Loop_Statement : Node_Id;
5455 Unary_Op_Validity_Checks (N);
5457 -- For boolean operand, deal with non-standard booleans
5459 if Is_Boolean_Type (Typ) then
5460 Adjust_Condition (Right_Opnd (N));
5461 Set_Etype (N, Standard_Boolean);
5462 Adjust_Result_Type (N, Typ);
5466 -- Only array types need any other processing
5468 if not Is_Array_Type (Typ) then
5472 -- Case of array operand. If bit packed with a component size of 1,
5473 -- handle it in Exp_Pakd if the operand is known to be aligned.
5475 if Is_Bit_Packed_Array (Typ)
5476 and then Component_Size (Typ) = 1
5477 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
5479 Expand_Packed_Not (N);
5483 -- Case of array operand which is not bit-packed. If the context is
5484 -- a safe assignment, call in-place operation, If context is a larger
5485 -- boolean expression in the context of a safe assignment, expansion is
5486 -- done by enclosing operation.
5488 Opnd := Relocate_Node (Right_Opnd (N));
5489 Convert_To_Actual_Subtype (Opnd);
5490 Arr := Etype (Opnd);
5491 Ensure_Defined (Arr, N);
5493 if Nkind (Parent (N)) = N_Assignment_Statement then
5494 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
5495 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5498 -- Special case the negation of a binary operation
5500 elsif (Nkind (Opnd) = N_Op_And
5501 or else Nkind (Opnd) = N_Op_Or
5502 or else Nkind (Opnd) = N_Op_Xor)
5503 and then Safe_In_Place_Array_Op
5504 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
5506 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
5510 elsif Nkind (Parent (N)) in N_Binary_Op
5511 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
5514 Op1 : constant Node_Id := Left_Opnd (Parent (N));
5515 Op2 : constant Node_Id := Right_Opnd (Parent (N));
5516 Lhs : constant Node_Id := Name (Parent (Parent (N)));
5519 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
5521 and then Nkind (Op2) = N_Op_Not
5523 -- (not A) op (not B) can be reduced to a single call
5528 and then Nkind (Parent (N)) = N_Op_Xor
5530 -- A xor (not B) can also be special-cased
5538 A := Make_Defining_Identifier (Loc, Name_uA);
5539 B := Make_Defining_Identifier (Loc, Name_uB);
5540 J := Make_Defining_Identifier (Loc, Name_uJ);
5543 Make_Indexed_Component (Loc,
5544 Prefix => New_Reference_To (A, Loc),
5545 Expressions => New_List (New_Reference_To (J, Loc)));
5548 Make_Indexed_Component (Loc,
5549 Prefix => New_Reference_To (B, Loc),
5550 Expressions => New_List (New_Reference_To (J, Loc)));
5553 Make_Implicit_Loop_Statement (N,
5554 Identifier => Empty,
5557 Make_Iteration_Scheme (Loc,
5558 Loop_Parameter_Specification =>
5559 Make_Loop_Parameter_Specification (Loc,
5560 Defining_Identifier => J,
5561 Discrete_Subtype_Definition =>
5562 Make_Attribute_Reference (Loc,
5563 Prefix => Make_Identifier (Loc, Chars (A)),
5564 Attribute_Name => Name_Range))),
5566 Statements => New_List (
5567 Make_Assignment_Statement (Loc,
5569 Expression => Make_Op_Not (Loc, A_J))));
5571 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('N'));
5572 Set_Is_Inlined (Func_Name);
5575 Make_Subprogram_Body (Loc,
5577 Make_Function_Specification (Loc,
5578 Defining_Unit_Name => Func_Name,
5579 Parameter_Specifications => New_List (
5580 Make_Parameter_Specification (Loc,
5581 Defining_Identifier => A,
5582 Parameter_Type => New_Reference_To (Typ, Loc))),
5583 Result_Definition => New_Reference_To (Typ, Loc)),
5585 Declarations => New_List (
5586 Make_Object_Declaration (Loc,
5587 Defining_Identifier => B,
5588 Object_Definition => New_Reference_To (Arr, Loc))),
5590 Handled_Statement_Sequence =>
5591 Make_Handled_Sequence_Of_Statements (Loc,
5592 Statements => New_List (
5594 Make_Return_Statement (Loc,
5596 Make_Identifier (Loc, Chars (B)))))));
5599 Make_Function_Call (Loc,
5600 Name => New_Reference_To (Func_Name, Loc),
5601 Parameter_Associations => New_List (Opnd)));
5603 Analyze_And_Resolve (N, Typ);
5604 end Expand_N_Op_Not;
5606 --------------------
5607 -- Expand_N_Op_Or --
5608 --------------------
5610 procedure Expand_N_Op_Or (N : Node_Id) is
5611 Typ : constant Entity_Id := Etype (N);
5614 Binary_Op_Validity_Checks (N);
5616 if Is_Array_Type (Etype (N)) then
5617 Expand_Boolean_Operator (N);
5619 elsif Is_Boolean_Type (Etype (N)) then
5620 Adjust_Condition (Left_Opnd (N));
5621 Adjust_Condition (Right_Opnd (N));
5622 Set_Etype (N, Standard_Boolean);
5623 Adjust_Result_Type (N, Typ);
5627 ----------------------
5628 -- Expand_N_Op_Plus --
5629 ----------------------
5631 procedure Expand_N_Op_Plus (N : Node_Id) is
5633 Unary_Op_Validity_Checks (N);
5634 end Expand_N_Op_Plus;
5636 ---------------------
5637 -- Expand_N_Op_Rem --
5638 ---------------------
5640 procedure Expand_N_Op_Rem (N : Node_Id) is
5641 Loc : constant Source_Ptr := Sloc (N);
5642 Typ : constant Entity_Id := Etype (N);
5644 Left : constant Node_Id := Left_Opnd (N);
5645 Right : constant Node_Id := Right_Opnd (N);
5656 Binary_Op_Validity_Checks (N);
5658 if Is_Integer_Type (Etype (N)) then
5659 Apply_Divide_Check (N);
5662 -- Apply optimization x rem 1 = 0. We don't really need that with
5663 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
5664 -- certainly harmless.
5666 if Is_Integer_Type (Etype (N))
5667 and then Compile_Time_Known_Value (Right)
5668 and then Expr_Value (Right) = Uint_1
5670 Rewrite (N, Make_Integer_Literal (Loc, 0));
5671 Analyze_And_Resolve (N, Typ);
5675 -- Deal with annoying case of largest negative number remainder
5676 -- minus one. Gigi does not handle this case correctly, because
5677 -- it generates a divide instruction which may trap in this case.
5679 -- In fact the check is quite easy, if the right operand is -1,
5680 -- then the remainder is always 0, and we can just ignore the
5681 -- left operand completely in this case.
5683 Determine_Range (Right, ROK, Rlo, Rhi);
5684 Determine_Range (Left, LOK, Llo, Lhi);
5686 -- The operand type may be private (e.g. in the expansion of an
5687 -- an intrinsic operation) so we must use the underlying type to
5688 -- get the bounds, and convert the literals explicitly.
5692 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
5694 -- Now perform the test, generating code only if needed
5696 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
5698 ((not LOK) or else (Llo = LLB))
5701 Make_Conditional_Expression (Loc,
5702 Expressions => New_List (
5704 Left_Opnd => Duplicate_Subexpr (Right),
5706 Unchecked_Convert_To (Typ,
5707 Make_Integer_Literal (Loc, -1))),
5709 Unchecked_Convert_To (Typ,
5710 Make_Integer_Literal (Loc, Uint_0)),
5712 Relocate_Node (N))));
5714 Set_Analyzed (Next (Next (First (Expressions (N)))));
5715 Analyze_And_Resolve (N, Typ);
5717 end Expand_N_Op_Rem;
5719 -----------------------------
5720 -- Expand_N_Op_Rotate_Left --
5721 -----------------------------
5723 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
5725 Binary_Op_Validity_Checks (N);
5726 end Expand_N_Op_Rotate_Left;
5728 ------------------------------
5729 -- Expand_N_Op_Rotate_Right --
5730 ------------------------------
5732 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
5734 Binary_Op_Validity_Checks (N);
5735 end Expand_N_Op_Rotate_Right;
5737 ----------------------------
5738 -- Expand_N_Op_Shift_Left --
5739 ----------------------------
5741 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
5743 Binary_Op_Validity_Checks (N);
5744 end Expand_N_Op_Shift_Left;
5746 -----------------------------
5747 -- Expand_N_Op_Shift_Right --
5748 -----------------------------
5750 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
5752 Binary_Op_Validity_Checks (N);
5753 end Expand_N_Op_Shift_Right;
5755 ----------------------------------------
5756 -- Expand_N_Op_Shift_Right_Arithmetic --
5757 ----------------------------------------
5759 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
5761 Binary_Op_Validity_Checks (N);
5762 end Expand_N_Op_Shift_Right_Arithmetic;
5764 --------------------------
5765 -- Expand_N_Op_Subtract --
5766 --------------------------
5768 procedure Expand_N_Op_Subtract (N : Node_Id) is
5769 Typ : constant Entity_Id := Etype (N);
5772 Binary_Op_Validity_Checks (N);
5774 -- N - 0 = N for integer types
5776 if Is_Integer_Type (Typ)
5777 and then Compile_Time_Known_Value (Right_Opnd (N))
5778 and then Expr_Value (Right_Opnd (N)) = 0
5780 Rewrite (N, Left_Opnd (N));
5784 -- Arithemtic overflow checks for signed integer/fixed point types
5786 if Is_Signed_Integer_Type (Typ)
5787 or else Is_Fixed_Point_Type (Typ)
5789 Apply_Arithmetic_Overflow_Check (N);
5791 -- Vax floating-point types case
5793 elsif Vax_Float (Typ) then
5794 Expand_Vax_Arith (N);
5796 end Expand_N_Op_Subtract;
5798 ---------------------
5799 -- Expand_N_Op_Xor --
5800 ---------------------
5802 procedure Expand_N_Op_Xor (N : Node_Id) is
5803 Typ : constant Entity_Id := Etype (N);
5806 Binary_Op_Validity_Checks (N);
5808 if Is_Array_Type (Etype (N)) then
5809 Expand_Boolean_Operator (N);
5811 elsif Is_Boolean_Type (Etype (N)) then
5812 Adjust_Condition (Left_Opnd (N));
5813 Adjust_Condition (Right_Opnd (N));
5814 Set_Etype (N, Standard_Boolean);
5815 Adjust_Result_Type (N, Typ);
5817 end Expand_N_Op_Xor;
5819 ----------------------
5820 -- Expand_N_Or_Else --
5821 ----------------------
5823 -- Expand into conditional expression if Actions present, and also
5824 -- deal with optimizing case of arguments being True or False.
5826 procedure Expand_N_Or_Else (N : Node_Id) is
5827 Loc : constant Source_Ptr := Sloc (N);
5828 Typ : constant Entity_Id := Etype (N);
5829 Left : constant Node_Id := Left_Opnd (N);
5830 Right : constant Node_Id := Right_Opnd (N);
5834 -- Deal with non-standard booleans
5836 if Is_Boolean_Type (Typ) then
5837 Adjust_Condition (Left);
5838 Adjust_Condition (Right);
5839 Set_Etype (N, Standard_Boolean);
5842 -- Check for cases of left argument is True or False
5844 if Nkind (Left) = N_Identifier then
5846 -- If left argument is False, change (False or else Right) to Right.
5847 -- Any actions associated with Right will be executed unconditionally
5848 -- and can thus be inserted into the tree unconditionally.
5850 if Entity (Left) = Standard_False then
5851 if Present (Actions (N)) then
5852 Insert_Actions (N, Actions (N));
5856 Adjust_Result_Type (N, Typ);
5859 -- If left argument is True, change (True and then Right) to
5860 -- True. In this case we can forget the actions associated with
5861 -- Right, since they will never be executed.
5863 elsif Entity (Left) = Standard_True then
5864 Kill_Dead_Code (Right);
5865 Kill_Dead_Code (Actions (N));
5866 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5867 Adjust_Result_Type (N, Typ);
5872 -- If Actions are present, we expand
5874 -- left or else right
5878 -- if left then True else right end
5880 -- with the actions becoming the Else_Actions of the conditional
5881 -- expression. This conditional expression is then further expanded
5882 -- (and will eventually disappear)
5884 if Present (Actions (N)) then
5885 Actlist := Actions (N);
5887 Make_Conditional_Expression (Loc,
5888 Expressions => New_List (
5890 New_Occurrence_Of (Standard_True, Loc),
5893 Set_Else_Actions (N, Actlist);
5894 Analyze_And_Resolve (N, Standard_Boolean);
5895 Adjust_Result_Type (N, Typ);
5899 -- No actions present, check for cases of right argument True/False
5901 if Nkind (Right) = N_Identifier then
5903 -- Change (Left or else False) to Left. Note that we know there
5904 -- are no actions associated with the True operand, since we
5905 -- just checked for this case above.
5907 if Entity (Right) = Standard_False then
5910 -- Change (Left or else True) to True, making sure to preserve
5911 -- any side effects associated with the Left operand.
5913 elsif Entity (Right) = Standard_True then
5914 Remove_Side_Effects (Left);
5916 (N, New_Occurrence_Of (Standard_True, Loc));
5920 Adjust_Result_Type (N, Typ);
5921 end Expand_N_Or_Else;
5923 -----------------------------------
5924 -- Expand_N_Qualified_Expression --
5925 -----------------------------------
5927 procedure Expand_N_Qualified_Expression (N : Node_Id) is
5928 Operand : constant Node_Id := Expression (N);
5929 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
5932 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
5933 end Expand_N_Qualified_Expression;
5935 ---------------------------------
5936 -- Expand_N_Selected_Component --
5937 ---------------------------------
5939 -- If the selector is a discriminant of a concurrent object, rewrite the
5940 -- prefix to denote the corresponding record type.
5942 procedure Expand_N_Selected_Component (N : Node_Id) is
5943 Loc : constant Source_Ptr := Sloc (N);
5944 Par : constant Node_Id := Parent (N);
5945 P : constant Node_Id := Prefix (N);
5946 Ptyp : Entity_Id := Underlying_Type (Etype (P));
5951 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
5952 -- Gigi needs a temporary for prefixes that depend on a discriminant,
5953 -- unless the context of an assignment can provide size information.
5954 -- Don't we have a general routine that does this???
5956 -----------------------
5957 -- In_Left_Hand_Side --
5958 -----------------------
5960 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
5962 return (Nkind (Parent (Comp)) = N_Assignment_Statement
5963 and then Comp = Name (Parent (Comp)))
5964 or else (Present (Parent (Comp))
5965 and then Nkind (Parent (Comp)) in N_Subexpr
5966 and then In_Left_Hand_Side (Parent (Comp)));
5967 end In_Left_Hand_Side;
5969 -- Start of processing for Expand_N_Selected_Component
5972 -- Insert explicit dereference if required
5974 if Is_Access_Type (Ptyp) then
5975 Insert_Explicit_Dereference (P);
5976 Analyze_And_Resolve (P, Designated_Type (Ptyp));
5978 if Ekind (Etype (P)) = E_Private_Subtype
5979 and then Is_For_Access_Subtype (Etype (P))
5981 Set_Etype (P, Base_Type (Etype (P)));
5987 -- Deal with discriminant check required
5989 if Do_Discriminant_Check (N) then
5991 -- Present the discrminant checking function to the backend,
5992 -- so that it can inline the call to the function.
5995 (Discriminant_Checking_Func
5996 (Original_Record_Component (Entity (Selector_Name (N)))));
5998 -- Now reset the flag and generate the call
6000 Set_Do_Discriminant_Check (N, False);
6001 Generate_Discriminant_Check (N);
6004 -- Gigi cannot handle unchecked conversions that are the prefix of a
6005 -- selected component with discriminants. This must be checked during
6006 -- expansion, because during analysis the type of the selector is not
6007 -- known at the point the prefix is analyzed. If the conversion is the
6008 -- target of an assignment, then we cannot force the evaluation.
6010 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
6011 and then Has_Discriminants (Etype (N))
6012 and then not In_Left_Hand_Side (N)
6014 Force_Evaluation (Prefix (N));
6017 -- Remaining processing applies only if selector is a discriminant
6019 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
6021 -- If the selector is a discriminant of a constrained record type,
6022 -- we may be able to rewrite the expression with the actual value
6023 -- of the discriminant, a useful optimization in some cases.
6025 if Is_Record_Type (Ptyp)
6026 and then Has_Discriminants (Ptyp)
6027 and then Is_Constrained (Ptyp)
6029 -- Do this optimization for discrete types only, and not for
6030 -- access types (access discriminants get us into trouble!)
6032 if not Is_Discrete_Type (Etype (N)) then
6035 -- Don't do this on the left hand of an assignment statement.
6036 -- Normally one would think that references like this would
6037 -- not occur, but they do in generated code, and mean that
6038 -- we really do want to assign the discriminant!
6040 elsif Nkind (Par) = N_Assignment_Statement
6041 and then Name (Par) = N
6045 -- Don't do this optimization for the prefix of an attribute
6046 -- or the operand of an object renaming declaration since these
6047 -- are contexts where we do not want the value anyway.
6049 elsif (Nkind (Par) = N_Attribute_Reference
6050 and then Prefix (Par) = N)
6051 or else Is_Renamed_Object (N)
6055 -- Don't do this optimization if we are within the code for a
6056 -- discriminant check, since the whole point of such a check may
6057 -- be to verify the condition on which the code below depends!
6059 elsif Is_In_Discriminant_Check (N) then
6062 -- Green light to see if we can do the optimization. There is
6063 -- still one condition that inhibits the optimization below
6064 -- but now is the time to check the particular discriminant.
6067 -- Loop through discriminants to find the matching
6068 -- discriminant constraint to see if we can copy it.
6070 Disc := First_Discriminant (Ptyp);
6071 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
6072 Discr_Loop : while Present (Dcon) loop
6074 -- Check if this is the matching discriminant
6076 if Disc = Entity (Selector_Name (N)) then
6078 -- Here we have the matching discriminant. Check for
6079 -- the case of a discriminant of a component that is
6080 -- constrained by an outer discriminant, which cannot
6081 -- be optimized away.
6084 Denotes_Discriminant
6085 (Node (Dcon), Check_Protected => True)
6089 -- In the context of a case statement, the expression
6090 -- may have the base type of the discriminant, and we
6091 -- need to preserve the constraint to avoid spurious
6092 -- errors on missing cases.
6094 elsif Nkind (Parent (N)) = N_Case_Statement
6095 and then Etype (Node (Dcon)) /= Etype (Disc)
6098 Make_Qualified_Expression (Loc,
6100 New_Occurrence_Of (Etype (Disc), Loc),
6102 New_Copy_Tree (Node (Dcon))));
6103 Analyze_And_Resolve (N, Etype (Disc));
6105 -- In case that comes out as a static expression,
6106 -- reset it (a selected component is never static).
6108 Set_Is_Static_Expression (N, False);
6111 -- Otherwise we can just copy the constraint, but the
6112 -- result is certainly not static! In some cases the
6113 -- discriminant constraint has been analyzed in the
6114 -- context of the original subtype indication, but for
6115 -- itypes the constraint might not have been analyzed
6116 -- yet, and this must be done now.
6119 Rewrite (N, New_Copy_Tree (Node (Dcon)));
6120 Analyze_And_Resolve (N);
6121 Set_Is_Static_Expression (N, False);
6127 Next_Discriminant (Disc);
6128 end loop Discr_Loop;
6130 -- Note: the above loop should always find a matching
6131 -- discriminant, but if it does not, we just missed an
6132 -- optimization due to some glitch (perhaps a previous
6133 -- error), so ignore.
6138 -- The only remaining processing is in the case of a discriminant of
6139 -- a concurrent object, where we rewrite the prefix to denote the
6140 -- corresponding record type. If the type is derived and has renamed
6141 -- discriminants, use corresponding discriminant, which is the one
6142 -- that appears in the corresponding record.
6144 if not Is_Concurrent_Type (Ptyp) then
6148 Disc := Entity (Selector_Name (N));
6150 if Is_Derived_Type (Ptyp)
6151 and then Present (Corresponding_Discriminant (Disc))
6153 Disc := Corresponding_Discriminant (Disc);
6157 Make_Selected_Component (Loc,
6159 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
6161 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
6166 end Expand_N_Selected_Component;
6168 --------------------
6169 -- Expand_N_Slice --
6170 --------------------
6172 procedure Expand_N_Slice (N : Node_Id) is
6173 Loc : constant Source_Ptr := Sloc (N);
6174 Typ : constant Entity_Id := Etype (N);
6175 Pfx : constant Node_Id := Prefix (N);
6176 Ptp : Entity_Id := Etype (Pfx);
6178 function Is_Procedure_Actual (N : Node_Id) return Boolean;
6179 -- Check whether the argument is an actual for a procedure call,
6180 -- in which case the expansion of a bit-packed slice is deferred
6181 -- until the call itself is expanded. The reason this is required
6182 -- is that we might have an IN OUT or OUT parameter, and the copy out
6183 -- is essential, and that copy out would be missed if we created a
6184 -- temporary here in Expand_N_Slice. Note that we don't bother
6185 -- to test specifically for an IN OUT or OUT mode parameter, since it
6186 -- is a bit tricky to do, and it is harmless to defer expansion
6187 -- in the IN case, since the call processing will still generate the
6188 -- appropriate copy in operation, which will take care of the slice.
6190 procedure Make_Temporary;
6191 -- Create a named variable for the value of the slice, in
6192 -- cases where the back-end cannot handle it properly, e.g.
6193 -- when packed types or unaligned slices are involved.
6195 -------------------------
6196 -- Is_Procedure_Actual --
6197 -------------------------
6199 function Is_Procedure_Actual (N : Node_Id) return Boolean is
6200 Par : Node_Id := Parent (N);
6204 -- If our parent is a procedure call we can return
6206 if Nkind (Par) = N_Procedure_Call_Statement then
6209 -- If our parent is a type conversion, keep climbing the
6210 -- tree, since a type conversion can be a procedure actual.
6211 -- Also keep climbing if parameter association or a qualified
6212 -- expression, since these are additional cases that do can
6213 -- appear on procedure actuals.
6215 elsif Nkind (Par) = N_Type_Conversion
6216 or else Nkind (Par) = N_Parameter_Association
6217 or else Nkind (Par) = N_Qualified_Expression
6219 Par := Parent (Par);
6221 -- Any other case is not what we are looking for
6227 end Is_Procedure_Actual;
6229 --------------------
6230 -- Make_Temporary --
6231 --------------------
6233 procedure Make_Temporary is
6235 Ent : constant Entity_Id :=
6236 Make_Defining_Identifier (Loc, New_Internal_Name ('T'));
6239 Make_Object_Declaration (Loc,
6240 Defining_Identifier => Ent,
6241 Object_Definition => New_Occurrence_Of (Typ, Loc));
6243 Set_No_Initialization (Decl);
6245 Insert_Actions (N, New_List (
6247 Make_Assignment_Statement (Loc,
6248 Name => New_Occurrence_Of (Ent, Loc),
6249 Expression => Relocate_Node (N))));
6251 Rewrite (N, New_Occurrence_Of (Ent, Loc));
6252 Analyze_And_Resolve (N, Typ);
6255 -- Start of processing for Expand_N_Slice
6258 -- Special handling for access types
6260 if Is_Access_Type (Ptp) then
6262 Ptp := Designated_Type (Ptp);
6265 Make_Explicit_Dereference (Sloc (N),
6266 Prefix => Relocate_Node (Pfx)));
6268 Analyze_And_Resolve (Pfx, Ptp);
6271 -- Range checks are potentially also needed for cases involving
6272 -- a slice indexed by a subtype indication, but Do_Range_Check
6273 -- can currently only be set for expressions ???
6275 if not Index_Checks_Suppressed (Ptp)
6276 and then (not Is_Entity_Name (Pfx)
6277 or else not Index_Checks_Suppressed (Entity (Pfx)))
6278 and then Nkind (Discrete_Range (N)) /= N_Subtype_Indication
6280 Enable_Range_Check (Discrete_Range (N));
6283 -- The remaining case to be handled is packed slices. We can leave
6284 -- packed slices as they are in the following situations:
6286 -- 1. Right or left side of an assignment (we can handle this
6287 -- situation correctly in the assignment statement expansion).
6289 -- 2. Prefix of indexed component (the slide is optimized away
6290 -- in this case, see the start of Expand_N_Slice.
6292 -- 3. Object renaming declaration, since we want the name of
6293 -- the slice, not the value.
6295 -- 4. Argument to procedure call, since copy-in/copy-out handling
6296 -- may be required, and this is handled in the expansion of
6299 -- 5. Prefix of an address attribute (this is an error which
6300 -- is caught elsewhere, and the expansion would intefere
6301 -- with generating the error message).
6303 if not Is_Packed (Typ) then
6305 -- Apply transformation for actuals of a function call,
6306 -- where Expand_Actuals is not used.
6308 if Nkind (Parent (N)) = N_Function_Call
6309 and then Is_Possibly_Unaligned_Slice (N)
6314 elsif Nkind (Parent (N)) = N_Assignment_Statement
6315 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
6316 and then Parent (N) = Name (Parent (Parent (N))))
6320 elsif Nkind (Parent (N)) = N_Indexed_Component
6321 or else Is_Renamed_Object (N)
6322 or else Is_Procedure_Actual (N)
6326 elsif Nkind (Parent (N)) = N_Attribute_Reference
6327 and then Attribute_Name (Parent (N)) = Name_Address
6336 ------------------------------
6337 -- Expand_N_Type_Conversion --
6338 ------------------------------
6340 procedure Expand_N_Type_Conversion (N : Node_Id) is
6341 Loc : constant Source_Ptr := Sloc (N);
6342 Operand : constant Node_Id := Expression (N);
6343 Target_Type : constant Entity_Id := Etype (N);
6344 Operand_Type : Entity_Id := Etype (Operand);
6346 procedure Handle_Changed_Representation;
6347 -- This is called in the case of record and array type conversions
6348 -- to see if there is a change of representation to be handled.
6349 -- Change of representation is actually handled at the assignment
6350 -- statement level, and what this procedure does is rewrite node N
6351 -- conversion as an assignment to temporary. If there is no change
6352 -- of representation, then the conversion node is unchanged.
6354 procedure Real_Range_Check;
6355 -- Handles generation of range check for real target value
6357 -----------------------------------
6358 -- Handle_Changed_Representation --
6359 -----------------------------------
6361 procedure Handle_Changed_Representation is
6370 -- Nothing to do if no change of representation
6372 if Same_Representation (Operand_Type, Target_Type) then
6375 -- The real change of representation work is done by the assignment
6376 -- statement processing. So if this type conversion is appearing as
6377 -- the expression of an assignment statement, nothing needs to be
6378 -- done to the conversion.
6380 elsif Nkind (Parent (N)) = N_Assignment_Statement then
6383 -- Otherwise we need to generate a temporary variable, and do the
6384 -- change of representation assignment into that temporary variable.
6385 -- The conversion is then replaced by a reference to this variable.
6390 -- If type is unconstrained we have to add a constraint,
6391 -- copied from the actual value of the left hand side.
6393 if not Is_Constrained (Target_Type) then
6394 if Has_Discriminants (Operand_Type) then
6395 Disc := First_Discriminant (Operand_Type);
6397 if Disc /= First_Stored_Discriminant (Operand_Type) then
6398 Disc := First_Stored_Discriminant (Operand_Type);
6402 while Present (Disc) loop
6404 Make_Selected_Component (Loc,
6405 Prefix => Duplicate_Subexpr_Move_Checks (Operand),
6407 Make_Identifier (Loc, Chars (Disc))));
6408 Next_Discriminant (Disc);
6411 elsif Is_Array_Type (Operand_Type) then
6412 N_Ix := First_Index (Target_Type);
6415 for J in 1 .. Number_Dimensions (Operand_Type) loop
6417 -- We convert the bounds explicitly. We use an unchecked
6418 -- conversion because bounds checks are done elsewhere.
6423 Unchecked_Convert_To (Etype (N_Ix),
6424 Make_Attribute_Reference (Loc,
6426 Duplicate_Subexpr_No_Checks
6427 (Operand, Name_Req => True),
6428 Attribute_Name => Name_First,
6429 Expressions => New_List (
6430 Make_Integer_Literal (Loc, J)))),
6433 Unchecked_Convert_To (Etype (N_Ix),
6434 Make_Attribute_Reference (Loc,
6436 Duplicate_Subexpr_No_Checks
6437 (Operand, Name_Req => True),
6438 Attribute_Name => Name_Last,
6439 Expressions => New_List (
6440 Make_Integer_Literal (Loc, J))))));
6447 Odef := New_Occurrence_Of (Target_Type, Loc);
6449 if Present (Cons) then
6451 Make_Subtype_Indication (Loc,
6452 Subtype_Mark => Odef,
6454 Make_Index_Or_Discriminant_Constraint (Loc,
6455 Constraints => Cons));
6458 Temp := Make_Defining_Identifier (Loc, New_Internal_Name ('C'));
6460 Make_Object_Declaration (Loc,
6461 Defining_Identifier => Temp,
6462 Object_Definition => Odef);
6464 Set_No_Initialization (Decl, True);
6466 -- Insert required actions. It is essential to suppress checks
6467 -- since we have suppressed default initialization, which means
6468 -- that the variable we create may have no discriminants.
6473 Make_Assignment_Statement (Loc,
6474 Name => New_Occurrence_Of (Temp, Loc),
6475 Expression => Relocate_Node (N))),
6476 Suppress => All_Checks);
6478 Rewrite (N, New_Occurrence_Of (Temp, Loc));
6481 end Handle_Changed_Representation;
6483 ----------------------
6484 -- Real_Range_Check --
6485 ----------------------
6487 -- Case of conversions to floating-point or fixed-point. If range
6488 -- checks are enabled and the target type has a range constraint,
6495 -- Tnn : typ'Base := typ'Base (x);
6496 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
6499 -- This is necessary when there is a conversion of integer to float
6500 -- or to fixed-point to ensure that the correct checks are made. It
6501 -- is not necessary for float to float where it is enough to simply
6502 -- set the Do_Range_Check flag.
6504 procedure Real_Range_Check is
6505 Btyp : constant Entity_Id := Base_Type (Target_Type);
6506 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
6507 Hi : constant Node_Id := Type_High_Bound (Target_Type);
6508 Xtyp : constant Entity_Id := Etype (Operand);
6513 -- Nothing to do if conversion was rewritten
6515 if Nkind (N) /= N_Type_Conversion then
6519 -- Nothing to do if range checks suppressed, or target has the
6520 -- same range as the base type (or is the base type).
6522 if Range_Checks_Suppressed (Target_Type)
6523 or else (Lo = Type_Low_Bound (Btyp)
6525 Hi = Type_High_Bound (Btyp))
6530 -- Nothing to do if expression is an entity on which checks
6531 -- have been suppressed.
6533 if Is_Entity_Name (Operand)
6534 and then Range_Checks_Suppressed (Entity (Operand))
6539 -- Nothing to do if bounds are all static and we can tell that
6540 -- the expression is within the bounds of the target. Note that
6541 -- if the operand is of an unconstrained floating-point type,
6542 -- then we do not trust it to be in range (might be infinite)
6545 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
6546 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
6549 if (not Is_Floating_Point_Type (Xtyp)
6550 or else Is_Constrained (Xtyp))
6551 and then Compile_Time_Known_Value (S_Lo)
6552 and then Compile_Time_Known_Value (S_Hi)
6553 and then Compile_Time_Known_Value (Hi)
6554 and then Compile_Time_Known_Value (Lo)
6557 D_Lov : constant Ureal := Expr_Value_R (Lo);
6558 D_Hiv : constant Ureal := Expr_Value_R (Hi);
6563 if Is_Real_Type (Xtyp) then
6564 S_Lov := Expr_Value_R (S_Lo);
6565 S_Hiv := Expr_Value_R (S_Hi);
6567 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
6568 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
6572 and then S_Lov >= D_Lov
6573 and then S_Hiv <= D_Hiv
6575 Set_Do_Range_Check (Operand, False);
6582 -- For float to float conversions, we are done
6584 if Is_Floating_Point_Type (Xtyp)
6586 Is_Floating_Point_Type (Btyp)
6591 -- Otherwise rewrite the conversion as described above
6593 Conv := Relocate_Node (N);
6595 (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
6596 Set_Etype (Conv, Btyp);
6598 -- Enable overflow except for case of integer to float conversions,
6599 -- where it is never required, since we can never have overflow in
6602 if not Is_Integer_Type (Etype (Operand)) then
6603 Enable_Overflow_Check (Conv);
6607 Make_Defining_Identifier (Loc,
6608 Chars => New_Internal_Name ('T'));
6610 Insert_Actions (N, New_List (
6611 Make_Object_Declaration (Loc,
6612 Defining_Identifier => Tnn,
6613 Object_Definition => New_Occurrence_Of (Btyp, Loc),
6614 Expression => Conv),
6616 Make_Raise_Constraint_Error (Loc,
6621 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6623 Make_Attribute_Reference (Loc,
6624 Attribute_Name => Name_First,
6626 New_Occurrence_Of (Target_Type, Loc))),
6630 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
6632 Make_Attribute_Reference (Loc,
6633 Attribute_Name => Name_Last,
6635 New_Occurrence_Of (Target_Type, Loc)))),
6636 Reason => CE_Range_Check_Failed)));
6638 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
6639 Analyze_And_Resolve (N, Btyp);
6640 end Real_Range_Check;
6642 -- Start of processing for Expand_N_Type_Conversion
6645 -- Nothing at all to do if conversion is to the identical type
6646 -- so remove the conversion completely, it is useless.
6648 if Operand_Type = Target_Type then
6649 Rewrite (N, Relocate_Node (Operand));
6653 -- Nothing to do if this is the second argument of read. This
6654 -- is a "backwards" conversion that will be handled by the
6655 -- specialized code in attribute processing.
6657 if Nkind (Parent (N)) = N_Attribute_Reference
6658 and then Attribute_Name (Parent (N)) = Name_Read
6659 and then Next (First (Expressions (Parent (N)))) = N
6664 -- Here if we may need to expand conversion
6666 -- Special case of converting from non-standard boolean type
6668 if Is_Boolean_Type (Operand_Type)
6669 and then (Nonzero_Is_True (Operand_Type))
6671 Adjust_Condition (Operand);
6672 Set_Etype (Operand, Standard_Boolean);
6673 Operand_Type := Standard_Boolean;
6676 -- Case of converting to an access type
6678 if Is_Access_Type (Target_Type) then
6680 -- Apply an accessibility check if the operand is an
6681 -- access parameter. Note that other checks may still
6682 -- need to be applied below (such as tagged type checks).
6684 if Is_Entity_Name (Operand)
6685 and then Ekind (Entity (Operand)) in Formal_Kind
6686 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
6688 Apply_Accessibility_Check (Operand, Target_Type);
6690 -- If the level of the operand type is statically deeper
6691 -- then the level of the target type, then force Program_Error.
6692 -- Note that this can only occur for cases where the attribute
6693 -- is within the body of an instantiation (otherwise the
6694 -- conversion will already have been rejected as illegal).
6695 -- Note: warnings are issued by the analyzer for the instance
6698 elsif In_Instance_Body
6699 and then Type_Access_Level (Operand_Type) >
6700 Type_Access_Level (Target_Type)
6703 Make_Raise_Program_Error (Sloc (N),
6704 Reason => PE_Accessibility_Check_Failed));
6705 Set_Etype (N, Target_Type);
6707 -- When the operand is a selected access discriminant
6708 -- the check needs to be made against the level of the
6709 -- object denoted by the prefix of the selected name.
6710 -- Force Program_Error for this case as well (this
6711 -- accessibility violation can only happen if within
6712 -- the body of an instantiation).
6714 elsif In_Instance_Body
6715 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
6716 and then Nkind (Operand) = N_Selected_Component
6717 and then Object_Access_Level (Operand) >
6718 Type_Access_Level (Target_Type)
6721 Make_Raise_Program_Error (Sloc (N),
6722 Reason => PE_Accessibility_Check_Failed));
6723 Set_Etype (N, Target_Type);
6727 -- Case of conversions of tagged types and access to tagged types
6729 -- When needed, that is to say when the expression is class-wide,
6730 -- Add runtime a tag check for (strict) downward conversion by using
6731 -- the membership test, generating:
6733 -- [constraint_error when Operand not in Target_Type'Class]
6735 -- or in the access type case
6737 -- [constraint_error
6738 -- when Operand /= null
6739 -- and then Operand.all not in
6740 -- Designated_Type (Target_Type)'Class]
6742 if (Is_Access_Type (Target_Type)
6743 and then Is_Tagged_Type (Designated_Type (Target_Type)))
6744 or else Is_Tagged_Type (Target_Type)
6746 -- Do not do any expansion in the access type case if the
6747 -- parent is a renaming, since this is an error situation
6748 -- which will be caught by Sem_Ch8, and the expansion can
6749 -- intefere with this error check.
6751 if Is_Access_Type (Target_Type)
6752 and then Is_Renamed_Object (N)
6757 -- Oherwise, proceed with processing tagged conversion
6760 Actual_Operand_Type : Entity_Id;
6761 Actual_Target_Type : Entity_Id;
6766 if Is_Access_Type (Target_Type) then
6767 Actual_Operand_Type := Designated_Type (Operand_Type);
6768 Actual_Target_Type := Designated_Type (Target_Type);
6771 Actual_Operand_Type := Operand_Type;
6772 Actual_Target_Type := Target_Type;
6775 if Is_Class_Wide_Type (Actual_Operand_Type)
6776 and then Root_Type (Actual_Operand_Type) /= Actual_Target_Type
6777 and then Is_Ancestor
6778 (Root_Type (Actual_Operand_Type),
6780 and then not Tag_Checks_Suppressed (Actual_Target_Type)
6782 -- The conversion is valid for any descendant of the
6785 Actual_Target_Type := Class_Wide_Type (Actual_Target_Type);
6787 if Is_Access_Type (Target_Type) then
6792 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6793 Right_Opnd => Make_Null (Loc)),
6798 Make_Explicit_Dereference (Loc,
6800 Duplicate_Subexpr_No_Checks (Operand)),
6802 New_Reference_To (Actual_Target_Type, Loc)));
6807 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
6809 New_Reference_To (Actual_Target_Type, Loc));
6813 Make_Raise_Constraint_Error (Loc,
6815 Reason => CE_Tag_Check_Failed));
6821 Make_Unchecked_Type_Conversion (Loc,
6822 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
6823 Expression => Relocate_Node (Expression (N)));
6825 Analyze_And_Resolve (N, Target_Type);
6830 -- Case of other access type conversions
6832 elsif Is_Access_Type (Target_Type) then
6833 Apply_Constraint_Check (Operand, Target_Type);
6835 -- Case of conversions from a fixed-point type
6837 -- These conversions require special expansion and processing, found
6838 -- in the Exp_Fixd package. We ignore cases where Conversion_OK is
6839 -- set, since from a semantic point of view, these are simple integer
6840 -- conversions, which do not need further processing.
6842 elsif Is_Fixed_Point_Type (Operand_Type)
6843 and then not Conversion_OK (N)
6845 -- We should never see universal fixed at this case, since the
6846 -- expansion of the constituent divide or multiply should have
6847 -- eliminated the explicit mention of universal fixed.
6849 pragma Assert (Operand_Type /= Universal_Fixed);
6851 -- Check for special case of the conversion to universal real
6852 -- that occurs as a result of the use of a round attribute.
6853 -- In this case, the real type for the conversion is taken
6854 -- from the target type of the Round attribute and the
6855 -- result must be marked as rounded.
6857 if Target_Type = Universal_Real
6858 and then Nkind (Parent (N)) = N_Attribute_Reference
6859 and then Attribute_Name (Parent (N)) = Name_Round
6861 Set_Rounded_Result (N);
6862 Set_Etype (N, Etype (Parent (N)));
6865 -- Otherwise do correct fixed-conversion, but skip these if the
6866 -- Conversion_OK flag is set, because from a semantic point of
6867 -- view these are simple integer conversions needing no further
6868 -- processing (the backend will simply treat them as integers)
6870 if not Conversion_OK (N) then
6871 if Is_Fixed_Point_Type (Etype (N)) then
6872 Expand_Convert_Fixed_To_Fixed (N);
6875 elsif Is_Integer_Type (Etype (N)) then
6876 Expand_Convert_Fixed_To_Integer (N);
6879 pragma Assert (Is_Floating_Point_Type (Etype (N)));
6880 Expand_Convert_Fixed_To_Float (N);
6885 -- Case of conversions to a fixed-point type
6887 -- These conversions require special expansion and processing, found
6888 -- in the Exp_Fixd package. Again, ignore cases where Conversion_OK
6889 -- is set, since from a semantic point of view, these are simple
6890 -- integer conversions, which do not need further processing.
6892 elsif Is_Fixed_Point_Type (Target_Type)
6893 and then not Conversion_OK (N)
6895 if Is_Integer_Type (Operand_Type) then
6896 Expand_Convert_Integer_To_Fixed (N);
6899 pragma Assert (Is_Floating_Point_Type (Operand_Type));
6900 Expand_Convert_Float_To_Fixed (N);
6904 -- Case of float-to-integer conversions
6906 -- We also handle float-to-fixed conversions with Conversion_OK set
6907 -- since semantically the fixed-point target is treated as though it
6908 -- were an integer in such cases.
6910 elsif Is_Floating_Point_Type (Operand_Type)
6912 (Is_Integer_Type (Target_Type)
6914 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
6916 -- Special processing required if the conversion is the expression
6917 -- of a Truncation attribute reference. In this case we replace:
6919 -- ityp (ftyp'Truncation (x))
6925 -- with the Float_Truncate flag set. This is clearly more efficient
6927 if Nkind (Operand) = N_Attribute_Reference
6928 and then Attribute_Name (Operand) = Name_Truncation
6931 Relocate_Node (First (Expressions (Operand))));
6932 Set_Float_Truncate (N, True);
6935 -- One more check here, gcc is still not able to do conversions of
6936 -- this type with proper overflow checking, and so gigi is doing an
6937 -- approximation of what is required by doing floating-point compares
6938 -- with the end-point. But that can lose precision in some cases, and
6939 -- give a wrong result. Converting the operand to Universal_Real is
6940 -- helpful, but still does not catch all cases with 64-bit integers
6941 -- on targets with only 64-bit floats ???
6943 if Do_Range_Check (Operand) then
6945 Make_Type_Conversion (Loc,
6947 New_Occurrence_Of (Universal_Real, Loc),
6949 Relocate_Node (Operand)));
6951 Set_Etype (Operand, Universal_Real);
6952 Enable_Range_Check (Operand);
6953 Set_Do_Range_Check (Expression (Operand), False);
6956 -- Case of array conversions
6958 -- Expansion of array conversions, add required length/range checks
6959 -- but only do this if there is no change of representation. For
6960 -- handling of this case, see Handle_Changed_Representation.
6962 elsif Is_Array_Type (Target_Type) then
6964 if Is_Constrained (Target_Type) then
6965 Apply_Length_Check (Operand, Target_Type);
6967 Apply_Range_Check (Operand, Target_Type);
6970 Handle_Changed_Representation;
6972 -- Case of conversions of discriminated types
6974 -- Add required discriminant checks if target is constrained. Again
6975 -- this change is skipped if we have a change of representation.
6977 elsif Has_Discriminants (Target_Type)
6978 and then Is_Constrained (Target_Type)
6980 Apply_Discriminant_Check (Operand, Target_Type);
6981 Handle_Changed_Representation;
6983 -- Case of all other record conversions. The only processing required
6984 -- is to check for a change of representation requiring the special
6985 -- assignment processing.
6987 elsif Is_Record_Type (Target_Type) then
6989 -- Ada 2005 (AI-216): Program_Error is raised when converting from
6990 -- a derived Unchecked_Union type to an unconstrained non-Unchecked_
6991 -- Union type if the operand lacks inferable discriminants.
6993 if Is_Derived_Type (Operand_Type)
6994 and then Is_Unchecked_Union (Base_Type (Operand_Type))
6995 and then not Is_Constrained (Target_Type)
6996 and then not Is_Unchecked_Union (Base_Type (Target_Type))
6997 and then not Has_Inferable_Discriminants (Operand)
6999 -- To prevent Gigi from generating illegal code, we make a
7000 -- Program_Error node, but we give it the target type of the
7004 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
7005 Reason => PE_Unchecked_Union_Restriction);
7008 Set_Etype (PE, Target_Type);
7013 Handle_Changed_Representation;
7016 -- Case of conversions of enumeration types
7018 elsif Is_Enumeration_Type (Target_Type) then
7020 -- Special processing is required if there is a change of
7021 -- representation (from enumeration representation clauses)
7023 if not Same_Representation (Target_Type, Operand_Type) then
7025 -- Convert: x(y) to x'val (ytyp'val (y))
7028 Make_Attribute_Reference (Loc,
7029 Prefix => New_Occurrence_Of (Target_Type, Loc),
7030 Attribute_Name => Name_Val,
7031 Expressions => New_List (
7032 Make_Attribute_Reference (Loc,
7033 Prefix => New_Occurrence_Of (Operand_Type, Loc),
7034 Attribute_Name => Name_Pos,
7035 Expressions => New_List (Operand)))));
7037 Analyze_And_Resolve (N, Target_Type);
7040 -- Case of conversions to floating-point
7042 elsif Is_Floating_Point_Type (Target_Type) then
7046 -- At this stage, either the conversion node has been transformed
7047 -- into some other equivalent expression, or left as a conversion
7048 -- that can be handled by Gigi. The conversions that Gigi can handle
7049 -- are the following:
7051 -- Conversions with no change of representation or type
7053 -- Numeric conversions involving integer values, floating-point
7054 -- values, and fixed-point values. Fixed-point values are allowed
7055 -- only if Conversion_OK is set, i.e. if the fixed-point values
7056 -- are to be treated as integers.
7058 -- No other conversions should be passed to Gigi
7060 -- Check: are these rules stated in sinfo??? if so, why restate here???
7062 -- The only remaining step is to generate a range check if we still
7063 -- have a type conversion at this stage and Do_Range_Check is set.
7064 -- For now we do this only for conversions of discrete types.
7066 if Nkind (N) = N_Type_Conversion
7067 and then Is_Discrete_Type (Etype (N))
7070 Expr : constant Node_Id := Expression (N);
7075 if Do_Range_Check (Expr)
7076 and then Is_Discrete_Type (Etype (Expr))
7078 Set_Do_Range_Check (Expr, False);
7080 -- Before we do a range check, we have to deal with treating
7081 -- a fixed-point operand as an integer. The way we do this
7082 -- is simply to do an unchecked conversion to an appropriate
7083 -- integer type large enough to hold the result.
7085 -- This code is not active yet, because we are only dealing
7086 -- with discrete types so far ???
7088 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
7089 and then Treat_Fixed_As_Integer (Expr)
7091 Ftyp := Base_Type (Etype (Expr));
7093 if Esize (Ftyp) >= Esize (Standard_Integer) then
7094 Ityp := Standard_Long_Long_Integer;
7096 Ityp := Standard_Integer;
7099 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
7102 -- Reset overflow flag, since the range check will include
7103 -- dealing with possible overflow, and generate the check
7104 -- If Address is either source or target type, suppress
7105 -- range check to avoid typing anomalies when it is a visible
7108 Set_Do_Overflow_Check (N, False);
7109 if not Is_Descendent_Of_Address (Etype (Expr))
7110 and then not Is_Descendent_Of_Address (Target_Type)
7112 Generate_Range_Check
7113 (Expr, Target_Type, CE_Range_Check_Failed);
7119 -- Final step, if the result is a type conversion involving Vax_Float
7120 -- types, then it is subject for further special processing.
7122 if Nkind (N) = N_Type_Conversion
7123 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
7125 Expand_Vax_Conversion (N);
7128 end Expand_N_Type_Conversion;
7130 -----------------------------------
7131 -- Expand_N_Unchecked_Expression --
7132 -----------------------------------
7134 -- Remove the unchecked expression node from the tree. It's job was simply
7135 -- to make sure that its constituent expression was handled with checks
7136 -- off, and now that that is done, we can remove it from the tree, and
7137 -- indeed must, since gigi does not expect to see these nodes.
7139 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
7140 Exp : constant Node_Id := Expression (N);
7143 Set_Assignment_OK (Exp, Assignment_OK (N) or Assignment_OK (Exp));
7145 end Expand_N_Unchecked_Expression;
7147 ----------------------------------------
7148 -- Expand_N_Unchecked_Type_Conversion --
7149 ----------------------------------------
7151 -- If this cannot be handled by Gigi and we haven't already made
7152 -- a temporary for it, do it now.
7154 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
7155 Target_Type : constant Entity_Id := Etype (N);
7156 Operand : constant Node_Id := Expression (N);
7157 Operand_Type : constant Entity_Id := Etype (Operand);
7160 -- If we have a conversion of a compile time known value to a target
7161 -- type and the value is in range of the target type, then we can simply
7162 -- replace the construct by an integer literal of the correct type. We
7163 -- only apply this to integer types being converted. Possibly it may
7164 -- apply in other cases, but it is too much trouble to worry about.
7166 -- Note that we do not do this transformation if the Kill_Range_Check
7167 -- flag is set, since then the value may be outside the expected range.
7168 -- This happens in the Normalize_Scalars case.
7170 if Is_Integer_Type (Target_Type)
7171 and then Is_Integer_Type (Operand_Type)
7172 and then Compile_Time_Known_Value (Operand)
7173 and then not Kill_Range_Check (N)
7176 Val : constant Uint := Expr_Value (Operand);
7179 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
7181 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
7183 Val >= Expr_Value (Type_Low_Bound (Target_Type))
7185 Val <= Expr_Value (Type_High_Bound (Target_Type))
7187 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
7189 -- If Address is the target type, just set the type
7190 -- to avoid a spurious type error on the literal when
7191 -- Address is a visible integer type.
7193 if Is_Descendent_Of_Address (Target_Type) then
7194 Set_Etype (N, Target_Type);
7196 Analyze_And_Resolve (N, Target_Type);
7204 -- Nothing to do if conversion is safe
7206 if Safe_Unchecked_Type_Conversion (N) then
7210 -- Otherwise force evaluation unless Assignment_OK flag is set (this
7211 -- flag indicates ??? -- more comments needed here)
7213 if Assignment_OK (N) then
7216 Force_Evaluation (N);
7218 end Expand_N_Unchecked_Type_Conversion;
7220 ----------------------------
7221 -- Expand_Record_Equality --
7222 ----------------------------
7224 -- For non-variant records, Equality is expanded when needed into:
7226 -- and then Lhs.Discr1 = Rhs.Discr1
7228 -- and then Lhs.Discrn = Rhs.Discrn
7229 -- and then Lhs.Cmp1 = Rhs.Cmp1
7231 -- and then Lhs.Cmpn = Rhs.Cmpn
7233 -- The expression is folded by the back-end for adjacent fields. This
7234 -- function is called for tagged record in only one occasion: for imple-
7235 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
7236 -- otherwise the primitive "=" is used directly.
7238 function Expand_Record_Equality
7243 Bodies : List_Id) return Node_Id
7245 Loc : constant Source_Ptr := Sloc (Nod);
7250 First_Time : Boolean := True;
7252 function Suitable_Element (C : Entity_Id) return Entity_Id;
7253 -- Return the first field to compare beginning with C, skipping the
7254 -- inherited components.
7256 ----------------------
7257 -- Suitable_Element --
7258 ----------------------
7260 function Suitable_Element (C : Entity_Id) return Entity_Id is
7265 elsif Ekind (C) /= E_Discriminant
7266 and then Ekind (C) /= E_Component
7268 return Suitable_Element (Next_Entity (C));
7270 elsif Is_Tagged_Type (Typ)
7271 and then C /= Original_Record_Component (C)
7273 return Suitable_Element (Next_Entity (C));
7275 elsif Chars (C) = Name_uController
7276 or else Chars (C) = Name_uTag
7278 return Suitable_Element (Next_Entity (C));
7283 end Suitable_Element;
7285 -- Start of processing for Expand_Record_Equality
7288 -- Generates the following code: (assuming that Typ has one Discr and
7289 -- component C2 is also a record)
7292 -- and then Lhs.Discr1 = Rhs.Discr1
7293 -- and then Lhs.C1 = Rhs.C1
7294 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
7296 -- and then Lhs.Cmpn = Rhs.Cmpn
7298 Result := New_Reference_To (Standard_True, Loc);
7299 C := Suitable_Element (First_Entity (Typ));
7301 while Present (C) loop
7309 First_Time := False;
7313 New_Lhs := New_Copy_Tree (Lhs);
7314 New_Rhs := New_Copy_Tree (Rhs);
7318 Expand_Composite_Equality (Nod, Etype (C),
7320 Make_Selected_Component (Loc,
7322 Selector_Name => New_Reference_To (C, Loc)),
7324 Make_Selected_Component (Loc,
7326 Selector_Name => New_Reference_To (C, Loc)),
7329 -- If some (sub)component is an unchecked_union, the whole
7330 -- operation will raise program error.
7332 if Nkind (Check) = N_Raise_Program_Error then
7334 Set_Etype (Result, Standard_Boolean);
7339 Left_Opnd => Result,
7340 Right_Opnd => Check);
7344 C := Suitable_Element (Next_Entity (C));
7348 end Expand_Record_Equality;
7350 -------------------------------------
7351 -- Fixup_Universal_Fixed_Operation --
7352 -------------------------------------
7354 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
7355 Conv : constant Node_Id := Parent (N);
7358 -- We must have a type conversion immediately above us
7360 pragma Assert (Nkind (Conv) = N_Type_Conversion);
7362 -- Normally the type conversion gives our target type. The exception
7363 -- occurs in the case of the Round attribute, where the conversion
7364 -- will be to universal real, and our real type comes from the Round
7365 -- attribute (as well as an indication that we must round the result)
7367 if Nkind (Parent (Conv)) = N_Attribute_Reference
7368 and then Attribute_Name (Parent (Conv)) = Name_Round
7370 Set_Etype (N, Etype (Parent (Conv)));
7371 Set_Rounded_Result (N);
7373 -- Normal case where type comes from conversion above us
7376 Set_Etype (N, Etype (Conv));
7378 end Fixup_Universal_Fixed_Operation;
7380 ------------------------------
7381 -- Get_Allocator_Final_List --
7382 ------------------------------
7384 function Get_Allocator_Final_List
7387 PtrT : Entity_Id) return Entity_Id
7389 Loc : constant Source_Ptr := Sloc (N);
7391 Owner : Entity_Id := PtrT;
7392 -- The entity whose finalisation list must be used to attach the
7393 -- allocated object.
7396 if Ekind (PtrT) = E_Anonymous_Access_Type then
7397 if Nkind (Associated_Node_For_Itype (PtrT))
7398 in N_Subprogram_Specification
7400 -- If the context is an access parameter, we need to create
7401 -- a non-anonymous access type in order to have a usable
7402 -- final list, because there is otherwise no pool to which
7403 -- the allocated object can belong. We create both the type
7404 -- and the finalization chain here, because freezing an
7405 -- internal type does not create such a chain. The Final_Chain
7406 -- that is thus created is shared by the access parameter.
7408 Owner := Make_Defining_Identifier (Loc, New_Internal_Name ('J'));
7410 Make_Full_Type_Declaration (Loc,
7411 Defining_Identifier => Owner,
7413 Make_Access_To_Object_Definition (Loc,
7414 Subtype_Indication =>
7415 New_Occurrence_Of (T, Loc))));
7417 Build_Final_List (N, Owner);
7418 Set_Associated_Final_Chain (PtrT, Associated_Final_Chain (Owner));
7421 -- Case of an access discriminant, or (Ada 2005) of
7422 -- an anonymous access component: find the final list
7423 -- associated with the scope of the type.
7425 Owner := Scope (PtrT);
7429 return Find_Final_List (Owner);
7430 end Get_Allocator_Final_List;
7432 ---------------------------------
7433 -- Has_Inferable_Discriminants --
7434 ---------------------------------
7436 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
7438 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
7439 -- Determines whether the left-most prefix of a selected component is a
7440 -- formal parameter in a subprogram. Assumes N is a selected component.
7442 --------------------------------
7443 -- Prefix_Is_Formal_Parameter --
7444 --------------------------------
7446 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
7447 Sel_Comp : Node_Id := N;
7450 -- Move to the left-most prefix by climbing up the tree
7452 while Present (Parent (Sel_Comp))
7453 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
7455 Sel_Comp := Parent (Sel_Comp);
7458 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
7459 end Prefix_Is_Formal_Parameter;
7461 -- Start of processing for Has_Inferable_Discriminants
7464 -- For identifiers and indexed components, it is sufficent to have a
7465 -- constrained Unchecked_Union nominal subtype.
7467 if Nkind (N) = N_Identifier
7469 Nkind (N) = N_Indexed_Component
7471 return Is_Unchecked_Union (Base_Type (Etype (N)))
7473 Is_Constrained (Etype (N));
7475 -- For selected components, the subtype of the selector must be a
7476 -- constrained Unchecked_Union. If the component is subject to a
7477 -- per-object constraint, then the enclosing object must have inferable
7480 elsif Nkind (N) = N_Selected_Component then
7481 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
7483 -- A small hack. If we have a per-object constrained selected
7484 -- component of a formal parameter, return True since we do not
7485 -- know the actual parameter association yet.
7487 if Prefix_Is_Formal_Parameter (N) then
7491 -- Otherwise, check the enclosing object and the selector
7493 return Has_Inferable_Discriminants (Prefix (N))
7495 Has_Inferable_Discriminants (Selector_Name (N));
7498 -- The call to Has_Inferable_Discriminants will determine whether
7499 -- the selector has a constrained Unchecked_Union nominal type.
7501 return Has_Inferable_Discriminants (Selector_Name (N));
7503 -- A qualified expression has inferable discriminants if its subtype
7504 -- mark is a constrained Unchecked_Union subtype.
7506 elsif Nkind (N) = N_Qualified_Expression then
7507 return Is_Unchecked_Union (Subtype_Mark (N))
7509 Is_Constrained (Subtype_Mark (N));
7514 end Has_Inferable_Discriminants;
7516 -------------------------------
7517 -- Insert_Dereference_Action --
7518 -------------------------------
7520 procedure Insert_Dereference_Action (N : Node_Id) is
7521 Loc : constant Source_Ptr := Sloc (N);
7522 Typ : constant Entity_Id := Etype (N);
7523 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
7524 Pnod : constant Node_Id := Parent (N);
7526 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
7527 -- Return true if type of P is derived from Checked_Pool;
7529 -----------------------------
7530 -- Is_Checked_Storage_Pool --
7531 -----------------------------
7533 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
7542 while T /= Etype (T) loop
7543 if Is_RTE (T, RE_Checked_Pool) then
7551 end Is_Checked_Storage_Pool;
7553 -- Start of processing for Insert_Dereference_Action
7556 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
7558 if not (Is_Checked_Storage_Pool (Pool)
7559 and then Comes_From_Source (Original_Node (Pnod)))
7565 Make_Procedure_Call_Statement (Loc,
7566 Name => New_Reference_To (
7567 Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
7569 Parameter_Associations => New_List (
7573 New_Reference_To (Pool, Loc),
7575 -- Storage_Address. We use the attribute Pool_Address,
7576 -- which uses the pointer itself to find the address of
7577 -- the object, and which handles unconstrained arrays
7578 -- properly by computing the address of the template.
7579 -- i.e. the correct address of the corresponding allocation.
7581 Make_Attribute_Reference (Loc,
7582 Prefix => Duplicate_Subexpr_Move_Checks (N),
7583 Attribute_Name => Name_Pool_Address),
7585 -- Size_In_Storage_Elements
7587 Make_Op_Divide (Loc,
7589 Make_Attribute_Reference (Loc,
7591 Make_Explicit_Dereference (Loc,
7592 Duplicate_Subexpr_Move_Checks (N)),
7593 Attribute_Name => Name_Size),
7595 Make_Integer_Literal (Loc, System_Storage_Unit)),
7599 Make_Attribute_Reference (Loc,
7601 Make_Explicit_Dereference (Loc,
7602 Duplicate_Subexpr_Move_Checks (N)),
7603 Attribute_Name => Name_Alignment))));
7606 when RE_Not_Available =>
7608 end Insert_Dereference_Action;
7610 ------------------------------
7611 -- Make_Array_Comparison_Op --
7612 ------------------------------
7614 -- This is a hand-coded expansion of the following generic function:
7617 -- type elem is (<>);
7618 -- type index is (<>);
7619 -- type a is array (index range <>) of elem;
7621 -- function Gnnn (X : a; Y: a) return boolean is
7622 -- J : index := Y'first;
7625 -- if X'length = 0 then
7628 -- elsif Y'length = 0 then
7632 -- for I in X'range loop
7633 -- if X (I) = Y (J) then
7634 -- if J = Y'last then
7637 -- J := index'succ (J);
7641 -- return X (I) > Y (J);
7645 -- return X'length > Y'length;
7649 -- Note that since we are essentially doing this expansion by hand, we
7650 -- do not need to generate an actual or formal generic part, just the
7651 -- instantiated function itself.
7653 function Make_Array_Comparison_Op
7655 Nod : Node_Id) return Node_Id
7657 Loc : constant Source_Ptr := Sloc (Nod);
7659 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
7660 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
7661 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
7662 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7664 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
7666 Loop_Statement : Node_Id;
7667 Loop_Body : Node_Id;
7670 Final_Expr : Node_Id;
7671 Func_Body : Node_Id;
7672 Func_Name : Entity_Id;
7678 -- if J = Y'last then
7681 -- J := index'succ (J);
7685 Make_Implicit_If_Statement (Nod,
7688 Left_Opnd => New_Reference_To (J, Loc),
7690 Make_Attribute_Reference (Loc,
7691 Prefix => New_Reference_To (Y, Loc),
7692 Attribute_Name => Name_Last)),
7694 Then_Statements => New_List (
7695 Make_Exit_Statement (Loc)),
7699 Make_Assignment_Statement (Loc,
7700 Name => New_Reference_To (J, Loc),
7702 Make_Attribute_Reference (Loc,
7703 Prefix => New_Reference_To (Index, Loc),
7704 Attribute_Name => Name_Succ,
7705 Expressions => New_List (New_Reference_To (J, Loc))))));
7707 -- if X (I) = Y (J) then
7710 -- return X (I) > Y (J);
7714 Make_Implicit_If_Statement (Nod,
7718 Make_Indexed_Component (Loc,
7719 Prefix => New_Reference_To (X, Loc),
7720 Expressions => New_List (New_Reference_To (I, Loc))),
7723 Make_Indexed_Component (Loc,
7724 Prefix => New_Reference_To (Y, Loc),
7725 Expressions => New_List (New_Reference_To (J, Loc)))),
7727 Then_Statements => New_List (Inner_If),
7729 Else_Statements => New_List (
7730 Make_Return_Statement (Loc,
7734 Make_Indexed_Component (Loc,
7735 Prefix => New_Reference_To (X, Loc),
7736 Expressions => New_List (New_Reference_To (I, Loc))),
7739 Make_Indexed_Component (Loc,
7740 Prefix => New_Reference_To (Y, Loc),
7741 Expressions => New_List (
7742 New_Reference_To (J, Loc)))))));
7744 -- for I in X'range loop
7749 Make_Implicit_Loop_Statement (Nod,
7750 Identifier => Empty,
7753 Make_Iteration_Scheme (Loc,
7754 Loop_Parameter_Specification =>
7755 Make_Loop_Parameter_Specification (Loc,
7756 Defining_Identifier => I,
7757 Discrete_Subtype_Definition =>
7758 Make_Attribute_Reference (Loc,
7759 Prefix => New_Reference_To (X, Loc),
7760 Attribute_Name => Name_Range))),
7762 Statements => New_List (Loop_Body));
7764 -- if X'length = 0 then
7766 -- elsif Y'length = 0 then
7769 -- for ... loop ... end loop;
7770 -- return X'length > Y'length;
7774 Make_Attribute_Reference (Loc,
7775 Prefix => New_Reference_To (X, Loc),
7776 Attribute_Name => Name_Length);
7779 Make_Attribute_Reference (Loc,
7780 Prefix => New_Reference_To (Y, Loc),
7781 Attribute_Name => Name_Length);
7785 Left_Opnd => Length1,
7786 Right_Opnd => Length2);
7789 Make_Implicit_If_Statement (Nod,
7793 Make_Attribute_Reference (Loc,
7794 Prefix => New_Reference_To (X, Loc),
7795 Attribute_Name => Name_Length),
7797 Make_Integer_Literal (Loc, 0)),
7801 Make_Return_Statement (Loc,
7802 Expression => New_Reference_To (Standard_False, Loc))),
7804 Elsif_Parts => New_List (
7805 Make_Elsif_Part (Loc,
7809 Make_Attribute_Reference (Loc,
7810 Prefix => New_Reference_To (Y, Loc),
7811 Attribute_Name => Name_Length),
7813 Make_Integer_Literal (Loc, 0)),
7817 Make_Return_Statement (Loc,
7818 Expression => New_Reference_To (Standard_True, Loc))))),
7820 Else_Statements => New_List (
7822 Make_Return_Statement (Loc,
7823 Expression => Final_Expr)));
7827 Formals := New_List (
7828 Make_Parameter_Specification (Loc,
7829 Defining_Identifier => X,
7830 Parameter_Type => New_Reference_To (Typ, Loc)),
7832 Make_Parameter_Specification (Loc,
7833 Defining_Identifier => Y,
7834 Parameter_Type => New_Reference_To (Typ, Loc)));
7836 -- function Gnnn (...) return boolean is
7837 -- J : index := Y'first;
7842 Func_Name := Make_Defining_Identifier (Loc, New_Internal_Name ('G'));
7845 Make_Subprogram_Body (Loc,
7847 Make_Function_Specification (Loc,
7848 Defining_Unit_Name => Func_Name,
7849 Parameter_Specifications => Formals,
7850 Result_Definition => New_Reference_To (Standard_Boolean, Loc)),
7852 Declarations => New_List (
7853 Make_Object_Declaration (Loc,
7854 Defining_Identifier => J,
7855 Object_Definition => New_Reference_To (Index, Loc),
7857 Make_Attribute_Reference (Loc,
7858 Prefix => New_Reference_To (Y, Loc),
7859 Attribute_Name => Name_First))),
7861 Handled_Statement_Sequence =>
7862 Make_Handled_Sequence_Of_Statements (Loc,
7863 Statements => New_List (If_Stat)));
7866 end Make_Array_Comparison_Op;
7868 ---------------------------
7869 -- Make_Boolean_Array_Op --
7870 ---------------------------
7872 -- For logical operations on boolean arrays, expand in line the
7873 -- following, replacing 'and' with 'or' or 'xor' where needed:
7875 -- function Annn (A : typ; B: typ) return typ is
7878 -- for J in A'range loop
7879 -- C (J) := A (J) op B (J);
7884 -- Here typ is the boolean array type
7886 function Make_Boolean_Array_Op
7888 N : Node_Id) return Node_Id
7890 Loc : constant Source_Ptr := Sloc (N);
7892 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
7893 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
7894 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
7895 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
7903 Func_Name : Entity_Id;
7904 Func_Body : Node_Id;
7905 Loop_Statement : Node_Id;
7909 Make_Indexed_Component (Loc,
7910 Prefix => New_Reference_To (A, Loc),
7911 Expressions => New_List (New_Reference_To (J, Loc)));
7914 Make_Indexed_Component (Loc,
7915 Prefix => New_Reference_To (B, Loc),
7916 Expressions => New_List (New_Reference_To (J, Loc)));
7919 Make_Indexed_Component (Loc,
7920 Prefix => New_Reference_To (C, Loc),
7921 Expressions => New_List (New_Reference_To (J, Loc)));
7923 if Nkind (N) = N_Op_And then
7929 elsif Nkind (N) = N_Op_Or then
7943 Make_Implicit_Loop_Statement (N,
7944 Identifier => Empty,
7947 Make_Iteration_Scheme (Loc,
7948 Loop_Parameter_Specification =>
7949 Make_Loop_Parameter_Specification (Loc,
7950 Defining_Identifier => J,
7951 Discrete_Subtype_Definition =>
7952 Make_Attribute_Reference (Loc,
7953 Prefix => New_Reference_To (A, Loc),
7954 Attribute_Name => Name_Range))),
7956 Statements => New_List (
7957 Make_Assignment_Statement (Loc,
7959 Expression => Op)));
7961 Formals := New_List (
7962 Make_Parameter_Specification (Loc,
7963 Defining_Identifier => A,
7964 Parameter_Type => New_Reference_To (Typ, Loc)),
7966 Make_Parameter_Specification (Loc,
7967 Defining_Identifier => B,
7968 Parameter_Type => New_Reference_To (Typ, Loc)));
7971 Make_Defining_Identifier (Loc, New_Internal_Name ('A'));
7972 Set_Is_Inlined (Func_Name);
7975 Make_Subprogram_Body (Loc,
7977 Make_Function_Specification (Loc,
7978 Defining_Unit_Name => Func_Name,
7979 Parameter_Specifications => Formals,
7980 Result_Definition => New_Reference_To (Typ, Loc)),
7982 Declarations => New_List (
7983 Make_Object_Declaration (Loc,
7984 Defining_Identifier => C,
7985 Object_Definition => New_Reference_To (Typ, Loc))),
7987 Handled_Statement_Sequence =>
7988 Make_Handled_Sequence_Of_Statements (Loc,
7989 Statements => New_List (
7991 Make_Return_Statement (Loc,
7992 Expression => New_Reference_To (C, Loc)))));
7995 end Make_Boolean_Array_Op;
7997 ------------------------
7998 -- Rewrite_Comparison --
7999 ------------------------
8001 procedure Rewrite_Comparison (N : Node_Id) is
8003 if Nkind (N) = N_Type_Conversion then
8004 Rewrite_Comparison (Expression (N));
8006 elsif Nkind (N) not in N_Op_Compare then
8011 Typ : constant Entity_Id := Etype (N);
8012 Op1 : constant Node_Id := Left_Opnd (N);
8013 Op2 : constant Node_Id := Right_Opnd (N);
8015 Res : constant Compare_Result := Compile_Time_Compare (Op1, Op2);
8016 -- Res indicates if compare outcome can be compile time determined
8018 True_Result : Boolean;
8019 False_Result : Boolean;
8022 case N_Op_Compare (Nkind (N)) is
8024 True_Result := Res = EQ;
8025 False_Result := Res = LT or else Res = GT or else Res = NE;
8028 True_Result := Res in Compare_GE;
8029 False_Result := Res = LT;
8032 and then Constant_Condition_Warnings
8033 and then Comes_From_Source (Original_Node (N))
8034 and then Nkind (Original_Node (N)) = N_Op_Ge
8035 and then not In_Instance
8036 and then not Warnings_Off (Etype (Left_Opnd (N)))
8037 and then Is_Integer_Type (Etype (Left_Opnd (N)))
8040 ("can never be greater than, could replace by ""'=""?", N);
8044 True_Result := Res = GT;
8045 False_Result := Res in Compare_LE;
8048 True_Result := Res = LT;
8049 False_Result := Res in Compare_GE;
8052 True_Result := Res in Compare_LE;
8053 False_Result := Res = GT;
8056 and then Constant_Condition_Warnings
8057 and then Comes_From_Source (Original_Node (N))
8058 and then Nkind (Original_Node (N)) = N_Op_Le
8059 and then not In_Instance
8060 and then not Warnings_Off (Etype (Left_Opnd (N)))
8061 and then Is_Integer_Type (Etype (Left_Opnd (N)))
8064 ("can never be less than, could replace by ""'=""?", N);
8068 True_Result := Res = NE or else Res = GT or else Res = LT;
8069 False_Result := Res = EQ;
8075 New_Occurrence_Of (Standard_True, Sloc (N))));
8076 Analyze_And_Resolve (N, Typ);
8077 Warn_On_Known_Condition (N);
8079 elsif False_Result then
8082 New_Occurrence_Of (Standard_False, Sloc (N))));
8083 Analyze_And_Resolve (N, Typ);
8084 Warn_On_Known_Condition (N);
8088 end Rewrite_Comparison;
8090 ----------------------------
8091 -- Safe_In_Place_Array_Op --
8092 ----------------------------
8094 function Safe_In_Place_Array_Op
8097 Op2 : Node_Id) return Boolean
8101 function Is_Safe_Operand (Op : Node_Id) return Boolean;
8102 -- Operand is safe if it cannot overlap part of the target of the
8103 -- operation. If the operand and the target are identical, the operand
8104 -- is safe. The operand can be empty in the case of negation.
8106 function Is_Unaliased (N : Node_Id) return Boolean;
8107 -- Check that N is a stand-alone entity
8113 function Is_Unaliased (N : Node_Id) return Boolean is
8117 and then No (Address_Clause (Entity (N)))
8118 and then No (Renamed_Object (Entity (N)));
8121 ---------------------
8122 -- Is_Safe_Operand --
8123 ---------------------
8125 function Is_Safe_Operand (Op : Node_Id) return Boolean is
8130 elsif Is_Entity_Name (Op) then
8131 return Is_Unaliased (Op);
8133 elsif Nkind (Op) = N_Indexed_Component
8134 or else Nkind (Op) = N_Selected_Component
8136 return Is_Unaliased (Prefix (Op));
8138 elsif Nkind (Op) = N_Slice then
8140 Is_Unaliased (Prefix (Op))
8141 and then Entity (Prefix (Op)) /= Target;
8143 elsif Nkind (Op) = N_Op_Not then
8144 return Is_Safe_Operand (Right_Opnd (Op));
8149 end Is_Safe_Operand;
8151 -- Start of processing for Is_Safe_In_Place_Array_Op
8154 -- We skip this processing if the component size is not the
8155 -- same as a system storage unit (since at least for NOT
8156 -- this would cause problems).
8158 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
8161 -- Cannot do in place stuff on Java_VM since cannot pass addresses
8166 -- Cannot do in place stuff if non-standard Boolean representation
8168 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
8171 elsif not Is_Unaliased (Lhs) then
8174 Target := Entity (Lhs);
8177 Is_Safe_Operand (Op1)
8178 and then Is_Safe_Operand (Op2);
8180 end Safe_In_Place_Array_Op;
8182 -----------------------
8183 -- Tagged_Membership --
8184 -----------------------
8186 -- There are two different cases to consider depending on whether
8187 -- the right operand is a class-wide type or not. If not we just
8188 -- compare the actual tag of the left expr to the target type tag:
8190 -- Left_Expr.Tag = Right_Type'Tag;
8192 -- If it is a class-wide type we use the RT function CW_Membership which
8193 -- is usually implemented by looking in the ancestor tables contained in
8194 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
8196 function Tagged_Membership (N : Node_Id) return Node_Id is
8197 Left : constant Node_Id := Left_Opnd (N);
8198 Right : constant Node_Id := Right_Opnd (N);
8199 Loc : constant Source_Ptr := Sloc (N);
8201 Left_Type : Entity_Id;
8202 Right_Type : Entity_Id;
8206 Left_Type := Etype (Left);
8207 Right_Type := Etype (Right);
8209 if Is_Class_Wide_Type (Left_Type) then
8210 Left_Type := Root_Type (Left_Type);
8214 Make_Selected_Component (Loc,
8215 Prefix => Relocate_Node (Left),
8217 New_Reference_To (First_Tag_Component (Left_Type), Loc));
8219 if Is_Class_Wide_Type (Right_Type) then
8221 -- Ada 2005 (AI-251): Class-wide applied to interfaces
8223 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
8225 -- Give support to: "Iface_CW_Typ in Typ'Class"
8227 or else Is_Interface (Left_Type)
8230 Make_Function_Call (Loc,
8231 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
8232 Parameter_Associations => New_List (
8233 Make_Attribute_Reference (Loc,
8235 Attribute_Name => Name_Address),
8238 (Access_Disp_Table (Root_Type (Right_Type)))),
8241 -- Ada 95: Normal case
8245 Make_Function_Call (Loc,
8246 Name => New_Occurrence_Of (RTE (RE_CW_Membership), Loc),
8247 Parameter_Associations => New_List (
8251 (Access_Disp_Table (Root_Type (Right_Type)))),
8258 Left_Opnd => Obj_Tag,
8261 (Node (First_Elmt (Access_Disp_Table (Right_Type))), Loc));
8263 end Tagged_Membership;
8265 ------------------------------
8266 -- Unary_Op_Validity_Checks --
8267 ------------------------------
8269 procedure Unary_Op_Validity_Checks (N : Node_Id) is
8271 if Validity_Checks_On and Validity_Check_Operands then
8272 Ensure_Valid (Right_Opnd (N));
8274 end Unary_Op_Validity_Checks;