1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Exp_Aggr; use Exp_Aggr;
33 with Exp_Atag; use Exp_Atag;
34 with Exp_Ch2; use Exp_Ch2;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch6; use Exp_Ch6;
37 with Exp_Ch7; use Exp_Ch7;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Fixd; use Exp_Fixd;
41 with Exp_Intr; use Exp_Intr;
42 with Exp_Pakd; use Exp_Pakd;
43 with Exp_Tss; use Exp_Tss;
44 with Exp_Util; use Exp_Util;
45 with Exp_VFpt; use Exp_VFpt;
46 with Freeze; use Freeze;
47 with Inline; use Inline;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Par_SCO; use Par_SCO;
54 with Restrict; use Restrict;
55 with Rident; use Rident;
56 with Rtsfind; use Rtsfind;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Res; use Sem_Res;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Sinfo; use Sinfo;
69 with Snames; use Snames;
70 with Stand; use Stand;
71 with SCIL_LL; use SCIL_LL;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Ttypes; use Ttypes;
75 with Uintp; use Uintp;
76 with Urealp; use Urealp;
77 with Validsw; use Validsw;
79 package body Exp_Ch4 is
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Binary_Op_Validity_Checks (N : Node_Id);
86 pragma Inline (Binary_Op_Validity_Checks);
87 -- Performs validity checks for a binary operator
89 procedure Build_Boolean_Array_Proc_Call
93 -- If a boolean array assignment can be done in place, build call to
94 -- corresponding library procedure.
96 function Current_Anonymous_Master return Entity_Id;
97 -- Return the entity of the heterogeneous finalization master belonging to
98 -- the current unit (either function, package or procedure). This master
99 -- services all anonymous access-to-controlled types. If the current unit
100 -- does not have such master, create one.
102 procedure Displace_Allocator_Pointer (N : Node_Id);
103 -- Ada 2005 (AI-251): Subsidiary procedure to Expand_N_Allocator and
104 -- Expand_Allocator_Expression. Allocating class-wide interface objects
105 -- this routine displaces the pointer to the allocated object to reference
106 -- the component referencing the corresponding secondary dispatch table.
108 procedure Expand_Allocator_Expression (N : Node_Id);
109 -- Subsidiary to Expand_N_Allocator, for the case when the expression
110 -- is a qualified expression or an aggregate.
112 procedure Expand_Array_Comparison (N : Node_Id);
113 -- This routine handles expansion of the comparison operators (N_Op_Lt,
114 -- N_Op_Le, N_Op_Gt, N_Op_Ge) when operating on an array type. The basic
115 -- code for these operators is similar, differing only in the details of
116 -- the actual comparison call that is made. Special processing (call a
119 function Expand_Array_Equality
124 Typ : Entity_Id) return Node_Id;
125 -- Expand an array equality into a call to a function implementing this
126 -- equality, and a call to it. Loc is the location for the generated nodes.
127 -- Lhs and Rhs are the array expressions to be compared. Bodies is a list
128 -- on which to attach bodies of local functions that are created in the
129 -- process. It is the responsibility of the caller to insert those bodies
130 -- at the right place. Nod provides the Sloc value for the generated code.
131 -- Normally the types used for the generated equality routine are taken
132 -- from Lhs and Rhs. However, in some situations of generated code, the
133 -- Etype fields of Lhs and Rhs are not set yet. In such cases, Typ supplies
134 -- the type to be used for the formal parameters.
136 procedure Expand_Boolean_Operator (N : Node_Id);
137 -- Common expansion processing for Boolean operators (And, Or, Xor) for the
138 -- case of array type arguments.
140 procedure Expand_Short_Circuit_Operator (N : Node_Id);
141 -- Common expansion processing for short-circuit boolean operators
143 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id);
144 -- Deal with comparison in MINIMIZED/ELIMINATED overflow mode. This is
145 -- where we allow comparison of "out of range" values.
147 function Expand_Composite_Equality
152 Bodies : List_Id) return Node_Id;
153 -- Local recursive function used to expand equality for nested composite
154 -- types. Used by Expand_Record/Array_Equality, Bodies is a list on which
155 -- to attach bodies of local functions that are created in the process.
156 -- It is the responsibility of the caller to insert those bodies at the
157 -- right place. Nod provides the Sloc value for generated code. Lhs and Rhs
158 -- are the left and right sides for the comparison, and Typ is the type of
159 -- the objects to compare.
161 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id);
162 -- Routine to expand concatenation of a sequence of two or more operands
163 -- (in the list Operands) and replace node Cnode with the result of the
164 -- concatenation. The operands can be of any appropriate type, and can
165 -- include both arrays and singleton elements.
167 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id);
168 -- N is an N_In membership test mode, with the overflow check mode set to
169 -- MINIMIZED or ELIMINATED, and the type of the left operand is a signed
170 -- integer type. This is a case where top level processing is required to
171 -- handle overflow checks in subtrees.
173 procedure Fixup_Universal_Fixed_Operation (N : Node_Id);
174 -- N is a N_Op_Divide or N_Op_Multiply node whose result is universal
175 -- fixed. We do not have such a type at runtime, so the purpose of this
176 -- routine is to find the real type by looking up the tree. We also
177 -- determine if the operation must be rounded.
179 function Has_Inferable_Discriminants (N : Node_Id) return Boolean;
180 -- Ada 2005 (AI-216): A view of an Unchecked_Union object has inferable
181 -- discriminants if it has a constrained nominal type, unless the object
182 -- is a component of an enclosing Unchecked_Union object that is subject
183 -- to a per-object constraint and the enclosing object lacks inferable
186 -- An expression of an Unchecked_Union type has inferable discriminants
187 -- if it is either a name of an object with inferable discriminants or a
188 -- qualified expression whose subtype mark denotes a constrained subtype.
190 procedure Insert_Dereference_Action (N : Node_Id);
191 -- N is an expression whose type is an access. When the type of the
192 -- associated storage pool is derived from Checked_Pool, generate a
193 -- call to the 'Dereference' primitive operation.
195 function Make_Array_Comparison_Op
197 Nod : Node_Id) return Node_Id;
198 -- Comparisons between arrays are expanded in line. This function produces
199 -- the body of the implementation of (a > b), where a and b are one-
200 -- dimensional arrays of some discrete type. The original node is then
201 -- expanded into the appropriate call to this function. Nod provides the
202 -- Sloc value for the generated code.
204 function Make_Boolean_Array_Op
206 N : Node_Id) return Node_Id;
207 -- Boolean operations on boolean arrays are expanded in line. This function
208 -- produce the body for the node N, which is (a and b), (a or b), or (a xor
209 -- b). It is used only the normal case and not the packed case. The type
210 -- involved, Typ, is the Boolean array type, and the logical operations in
211 -- the body are simple boolean operations. Note that Typ is always a
212 -- constrained type (the caller has ensured this by using
213 -- Convert_To_Actual_Subtype if necessary).
215 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean;
216 -- For signed arithmetic operations when the current overflow mode is
217 -- MINIMIZED or ELIMINATED, we must call Apply_Arithmetic_Overflow_Checks
218 -- as the first thing we do. We then return. We count on the recursive
219 -- apparatus for overflow checks to call us back with an equivalent
220 -- operation that is in CHECKED mode, avoiding a recursive entry into this
221 -- routine, and that is when we will proceed with the expansion of the
222 -- operator (e.g. converting X+0 to X, or X**2 to X*X). We cannot do
223 -- these optimizations without first making this check, since there may be
224 -- operands further down the tree that are relying on the recursive calls
225 -- triggered by the top level nodes to properly process overflow checking
226 -- and remaining expansion on these nodes. Note that this call back may be
227 -- skipped if the operation is done in Bignum mode but that's fine, since
228 -- the Bignum call takes care of everything.
230 procedure Optimize_Length_Comparison (N : Node_Id);
231 -- Given an expression, if it is of the form X'Length op N (or the other
232 -- way round), where N is known at compile time to be 0 or 1, and X is a
233 -- simple entity, and op is a comparison operator, optimizes it into a
234 -- comparison of First and Last.
236 procedure Process_Transient_Object
239 -- Subsidiary routine to the expansion of expression_with_actions and if
240 -- expressions. Generate all the necessary code to finalize a transient
241 -- controlled object when the enclosing context is elaborated or evaluated.
242 -- Decl denotes the declaration of the transient controlled object which is
243 -- usually the result of a controlled function call. Rel_Node denotes the
244 -- context, either an expression_with_actions or an if expression.
246 procedure Rewrite_Comparison (N : Node_Id);
247 -- If N is the node for a comparison whose outcome can be determined at
248 -- compile time, then the node N can be rewritten with True or False. If
249 -- the outcome cannot be determined at compile time, the call has no
250 -- effect. If N is a type conversion, then this processing is applied to
251 -- its expression. If N is neither comparison nor a type conversion, the
252 -- call has no effect.
254 procedure Tagged_Membership
256 SCIL_Node : out Node_Id;
257 Result : out Node_Id);
258 -- Construct the expression corresponding to the tagged membership test.
259 -- Deals with a second operand being (or not) a class-wide type.
261 function Safe_In_Place_Array_Op
264 Op2 : Node_Id) return Boolean;
265 -- In the context of an assignment, where the right-hand side is a boolean
266 -- operation on arrays, check whether operation can be performed in place.
268 procedure Unary_Op_Validity_Checks (N : Node_Id);
269 pragma Inline (Unary_Op_Validity_Checks);
270 -- Performs validity checks for a unary operator
272 -------------------------------
273 -- Binary_Op_Validity_Checks --
274 -------------------------------
276 procedure Binary_Op_Validity_Checks (N : Node_Id) is
278 if Validity_Checks_On and Validity_Check_Operands then
279 Ensure_Valid (Left_Opnd (N));
280 Ensure_Valid (Right_Opnd (N));
282 end Binary_Op_Validity_Checks;
284 ------------------------------------
285 -- Build_Boolean_Array_Proc_Call --
286 ------------------------------------
288 procedure Build_Boolean_Array_Proc_Call
293 Loc : constant Source_Ptr := Sloc (N);
294 Kind : constant Node_Kind := Nkind (Expression (N));
295 Target : constant Node_Id :=
296 Make_Attribute_Reference (Loc,
298 Attribute_Name => Name_Address);
300 Arg1 : Node_Id := Op1;
301 Arg2 : Node_Id := Op2;
303 Proc_Name : Entity_Id;
306 if Kind = N_Op_Not then
307 if Nkind (Op1) in N_Binary_Op then
309 -- Use negated version of the binary operators
311 if Nkind (Op1) = N_Op_And then
312 Proc_Name := RTE (RE_Vector_Nand);
314 elsif Nkind (Op1) = N_Op_Or then
315 Proc_Name := RTE (RE_Vector_Nor);
317 else pragma Assert (Nkind (Op1) = N_Op_Xor);
318 Proc_Name := RTE (RE_Vector_Xor);
322 Make_Procedure_Call_Statement (Loc,
323 Name => New_Occurrence_Of (Proc_Name, Loc),
325 Parameter_Associations => New_List (
327 Make_Attribute_Reference (Loc,
328 Prefix => Left_Opnd (Op1),
329 Attribute_Name => Name_Address),
331 Make_Attribute_Reference (Loc,
332 Prefix => Right_Opnd (Op1),
333 Attribute_Name => Name_Address),
335 Make_Attribute_Reference (Loc,
336 Prefix => Left_Opnd (Op1),
337 Attribute_Name => Name_Length)));
340 Proc_Name := RTE (RE_Vector_Not);
343 Make_Procedure_Call_Statement (Loc,
344 Name => New_Occurrence_Of (Proc_Name, Loc),
345 Parameter_Associations => New_List (
348 Make_Attribute_Reference (Loc,
350 Attribute_Name => Name_Address),
352 Make_Attribute_Reference (Loc,
354 Attribute_Name => Name_Length)));
358 -- We use the following equivalences:
360 -- (not X) or (not Y) = not (X and Y) = Nand (X, Y)
361 -- (not X) and (not Y) = not (X or Y) = Nor (X, Y)
362 -- (not X) xor (not Y) = X xor Y
363 -- X xor (not Y) = not (X xor Y) = Nxor (X, Y)
365 if Nkind (Op1) = N_Op_Not then
366 Arg1 := Right_Opnd (Op1);
367 Arg2 := Right_Opnd (Op2);
369 if Kind = N_Op_And then
370 Proc_Name := RTE (RE_Vector_Nor);
371 elsif Kind = N_Op_Or then
372 Proc_Name := RTE (RE_Vector_Nand);
374 Proc_Name := RTE (RE_Vector_Xor);
378 if Kind = N_Op_And then
379 Proc_Name := RTE (RE_Vector_And);
380 elsif Kind = N_Op_Or then
381 Proc_Name := RTE (RE_Vector_Or);
382 elsif Nkind (Op2) = N_Op_Not then
383 Proc_Name := RTE (RE_Vector_Nxor);
384 Arg2 := Right_Opnd (Op2);
386 Proc_Name := RTE (RE_Vector_Xor);
391 Make_Procedure_Call_Statement (Loc,
392 Name => New_Occurrence_Of (Proc_Name, Loc),
393 Parameter_Associations => New_List (
395 Make_Attribute_Reference (Loc,
397 Attribute_Name => Name_Address),
398 Make_Attribute_Reference (Loc,
400 Attribute_Name => Name_Address),
401 Make_Attribute_Reference (Loc,
403 Attribute_Name => Name_Length)));
406 Rewrite (N, Call_Node);
410 when RE_Not_Available =>
412 end Build_Boolean_Array_Proc_Call;
414 ------------------------------
415 -- Current_Anonymous_Master --
416 ------------------------------
418 function Current_Anonymous_Master return Entity_Id is
426 Unit_Id := Cunit_Entity (Current_Sem_Unit);
428 -- Find the entity of the current unit
430 if Ekind (Unit_Id) = E_Subprogram_Body then
432 -- When processing subprogram bodies, the proper scope is always that
435 Subp_Body := Unit_Id;
436 while Present (Subp_Body)
437 and then Nkind (Subp_Body) /= N_Subprogram_Body
439 Subp_Body := Parent (Subp_Body);
442 Unit_Id := Corresponding_Spec (Subp_Body);
445 Loc := Sloc (Unit_Id);
446 Unit_Decl := Unit (Cunit (Current_Sem_Unit));
448 -- Find the declarations list of the current unit
450 if Nkind (Unit_Decl) = N_Package_Declaration then
451 Unit_Decl := Specification (Unit_Decl);
452 Decls := Visible_Declarations (Unit_Decl);
455 Decls := New_List (Make_Null_Statement (Loc));
456 Set_Visible_Declarations (Unit_Decl, Decls);
458 elsif Is_Empty_List (Decls) then
459 Append_To (Decls, Make_Null_Statement (Loc));
463 Decls := Declarations (Unit_Decl);
466 Decls := New_List (Make_Null_Statement (Loc));
467 Set_Declarations (Unit_Decl, Decls);
469 elsif Is_Empty_List (Decls) then
470 Append_To (Decls, Make_Null_Statement (Loc));
474 -- The current unit has an existing anonymous master, traverse its
475 -- declarations and locate the entity.
477 if Has_Anonymous_Master (Unit_Id) then
480 Fin_Mas_Id : Entity_Id;
483 Decl := First (Decls);
484 while Present (Decl) loop
486 -- Look for the first variable in the declarations whole type
487 -- is Finalization_Master.
489 if Nkind (Decl) = N_Object_Declaration then
490 Fin_Mas_Id := Defining_Identifier (Decl);
492 if Ekind (Fin_Mas_Id) = E_Variable
493 and then Etype (Fin_Mas_Id) = RTE (RE_Finalization_Master)
502 -- The master was not found even though the unit was labeled as
508 -- Create a new anonymous master
512 First_Decl : constant Node_Id := First (Decls);
514 Fin_Mas_Id : Entity_Id;
517 -- Since the master and its associated initialization is inserted
518 -- at top level, use the scope of the unit when analyzing.
520 Push_Scope (Unit_Id);
522 -- Create the finalization master
525 Make_Defining_Identifier (Loc,
526 Chars => New_External_Name (Chars (Unit_Id), "AM"));
529 -- <Fin_Mas_Id> : Finalization_Master;
532 Make_Object_Declaration (Loc,
533 Defining_Identifier => Fin_Mas_Id,
535 New_Occurrence_Of (RTE (RE_Finalization_Master), Loc));
537 Insert_Before_And_Analyze (First_Decl, Action);
539 -- Mark the unit to prevent the generation of multiple masters
541 Set_Has_Anonymous_Master (Unit_Id);
543 -- Do not set the base pool and mode of operation on .NET/JVM
544 -- since those targets do not support pools and all VM masters
545 -- are heterogeneous by default.
547 if VM_Target = No_VM then
551 -- (<Fin_Mas_Id>, Global_Pool_Object'Unrestricted_Access);
554 Make_Procedure_Call_Statement (Loc,
556 New_Occurrence_Of (RTE (RE_Set_Base_Pool), Loc),
558 Parameter_Associations => New_List (
559 New_Occurrence_Of (Fin_Mas_Id, Loc),
560 Make_Attribute_Reference (Loc,
562 New_Occurrence_Of (RTE (RE_Global_Pool_Object), Loc),
563 Attribute_Name => Name_Unrestricted_Access)));
565 Insert_Before_And_Analyze (First_Decl, Action);
568 -- Set_Is_Heterogeneous (<Fin_Mas_Id>);
571 Make_Procedure_Call_Statement (Loc,
573 New_Occurrence_Of (RTE (RE_Set_Is_Heterogeneous), Loc),
574 Parameter_Associations => New_List (
575 New_Occurrence_Of (Fin_Mas_Id, Loc)));
577 Insert_Before_And_Analyze (First_Decl, Action);
580 -- Restore the original state of the scope stack
587 end Current_Anonymous_Master;
589 --------------------------------
590 -- Displace_Allocator_Pointer --
591 --------------------------------
593 procedure Displace_Allocator_Pointer (N : Node_Id) is
594 Loc : constant Source_Ptr := Sloc (N);
595 Orig_Node : constant Node_Id := Original_Node (N);
601 -- Do nothing in case of VM targets: the virtual machine will handle
602 -- interfaces directly.
604 if not Tagged_Type_Expansion then
608 pragma Assert (Nkind (N) = N_Identifier
609 and then Nkind (Orig_Node) = N_Allocator);
611 PtrT := Etype (Orig_Node);
612 Dtyp := Available_View (Designated_Type (PtrT));
613 Etyp := Etype (Expression (Orig_Node));
615 if Is_Class_Wide_Type (Dtyp) and then Is_Interface (Dtyp) then
617 -- If the type of the allocator expression is not an interface type
618 -- we can generate code to reference the record component containing
619 -- the pointer to the secondary dispatch table.
621 if not Is_Interface (Etyp) then
623 Saved_Typ : constant Entity_Id := Etype (Orig_Node);
626 -- 1) Get access to the allocated object
629 Make_Explicit_Dereference (Loc, Relocate_Node (N)));
633 -- 2) Add the conversion to displace the pointer to reference
634 -- the secondary dispatch table.
636 Rewrite (N, Convert_To (Dtyp, Relocate_Node (N)));
637 Analyze_And_Resolve (N, Dtyp);
639 -- 3) The 'access to the secondary dispatch table will be used
640 -- as the value returned by the allocator.
643 Make_Attribute_Reference (Loc,
644 Prefix => Relocate_Node (N),
645 Attribute_Name => Name_Access));
646 Set_Etype (N, Saved_Typ);
650 -- If the type of the allocator expression is an interface type we
651 -- generate a run-time call to displace "this" to reference the
652 -- component containing the pointer to the secondary dispatch table
653 -- or else raise Constraint_Error if the actual object does not
654 -- implement the target interface. This case corresponds to the
655 -- following example:
657 -- function Op (Obj : Iface_1'Class) return access Iface_2'Class is
659 -- return new Iface_2'Class'(Obj);
664 Unchecked_Convert_To (PtrT,
665 Make_Function_Call (Loc,
666 Name => New_Occurrence_Of (RTE (RE_Displace), Loc),
667 Parameter_Associations => New_List (
668 Unchecked_Convert_To (RTE (RE_Address),
674 (Access_Disp_Table (Etype (Base_Type (Dtyp))))),
676 Analyze_And_Resolve (N, PtrT);
679 end Displace_Allocator_Pointer;
681 ---------------------------------
682 -- Expand_Allocator_Expression --
683 ---------------------------------
685 procedure Expand_Allocator_Expression (N : Node_Id) is
686 Loc : constant Source_Ptr := Sloc (N);
687 Exp : constant Node_Id := Expression (Expression (N));
688 PtrT : constant Entity_Id := Etype (N);
689 DesigT : constant Entity_Id := Designated_Type (PtrT);
691 procedure Apply_Accessibility_Check
693 Built_In_Place : Boolean := False);
694 -- Ada 2005 (AI-344): For an allocator with a class-wide designated
695 -- type, generate an accessibility check to verify that the level of the
696 -- type of the created object is not deeper than the level of the access
697 -- type. If the type of the qualified expression is class-wide, then
698 -- always generate the check (except in the case where it is known to be
699 -- unnecessary, see comment below). Otherwise, only generate the check
700 -- if the level of the qualified expression type is statically deeper
701 -- than the access type.
703 -- Although the static accessibility will generally have been performed
704 -- as a legality check, it won't have been done in cases where the
705 -- allocator appears in generic body, so a run-time check is needed in
706 -- general. One special case is when the access type is declared in the
707 -- same scope as the class-wide allocator, in which case the check can
708 -- never fail, so it need not be generated.
710 -- As an open issue, there seem to be cases where the static level
711 -- associated with the class-wide object's underlying type is not
712 -- sufficient to perform the proper accessibility check, such as for
713 -- allocators in nested subprograms or accept statements initialized by
714 -- class-wide formals when the actual originates outside at a deeper
715 -- static level. The nested subprogram case might require passing
716 -- accessibility levels along with class-wide parameters, and the task
717 -- case seems to be an actual gap in the language rules that needs to
718 -- be fixed by the ARG. ???
720 -------------------------------
721 -- Apply_Accessibility_Check --
722 -------------------------------
724 procedure Apply_Accessibility_Check
726 Built_In_Place : Boolean := False)
728 Pool_Id : constant Entity_Id := Associated_Storage_Pool (PtrT);
736 if Ada_Version >= Ada_2005
737 and then Is_Class_Wide_Type (DesigT)
738 and then (Tagged_Type_Expansion or else VM_Target /= No_VM)
739 and then not Scope_Suppress.Suppress (Accessibility_Check)
741 (Type_Access_Level (Etype (Exp)) > Type_Access_Level (PtrT)
743 (Is_Class_Wide_Type (Etype (Exp))
744 and then Scope (PtrT) /= Current_Scope))
746 -- If the allocator was built in place, Ref is already a reference
747 -- to the access object initialized to the result of the allocator
748 -- (see Exp_Ch6.Make_Build_In_Place_Call_In_Allocator). We call
749 -- Remove_Side_Effects for cases where the build-in-place call may
750 -- still be the prefix of the reference (to avoid generating
751 -- duplicate calls). Otherwise, it is the entity associated with
752 -- the object containing the address of the allocated object.
754 if Built_In_Place then
755 Remove_Side_Effects (Ref);
756 Obj_Ref := New_Copy_Tree (Ref);
758 Obj_Ref := New_Occurrence_Of (Ref, Loc);
761 -- Step 1: Create the object clean up code
765 -- Deallocate the object if the accessibility check fails. This
766 -- is done only on targets or profiles that support deallocation.
770 if RTE_Available (RE_Free) then
771 Free_Stmt := Make_Free_Statement (Loc, New_Copy_Tree (Obj_Ref));
772 Set_Storage_Pool (Free_Stmt, Pool_Id);
774 Append_To (Stmts, Free_Stmt);
776 -- The target or profile cannot deallocate objects
782 -- Finalize the object if applicable. Generate:
784 -- [Deep_]Finalize (Obj_Ref.all);
786 if Needs_Finalization (DesigT) then
790 Make_Explicit_Dereference (Loc, New_Copy (Obj_Ref)),
793 -- When the target or profile supports deallocation, wrap the
794 -- finalization call in a block to ensure proper deallocation
795 -- even if finalization fails. Generate:
805 if Present (Free_Stmt) then
807 Make_Block_Statement (Loc,
808 Handled_Statement_Sequence =>
809 Make_Handled_Sequence_Of_Statements (Loc,
810 Statements => New_List (Fin_Call),
812 Exception_Handlers => New_List (
813 Make_Exception_Handler (Loc,
814 Exception_Choices => New_List (
815 Make_Others_Choice (Loc)),
817 Statements => New_List (
818 New_Copy_Tree (Free_Stmt),
819 Make_Raise_Statement (Loc))))));
822 Prepend_To (Stmts, Fin_Call);
825 -- Signal the accessibility failure through a Program_Error
828 Make_Raise_Program_Error (Loc,
829 Condition => New_Occurrence_Of (Standard_True, Loc),
830 Reason => PE_Accessibility_Check_Failed));
832 -- Step 2: Create the accessibility comparison
834 -- Reference the tag: for a renaming of an access to an interface
835 -- object Obj_Ref already references the tag of the secondary
838 if Nkind (Obj_Ref) in N_Has_Entity
839 and then Present (Entity (Obj_Ref))
840 and then Present (Renamed_Object (Entity (Obj_Ref)))
841 and then Is_Interface (DesigT)
850 Make_Attribute_Reference (Loc,
852 Attribute_Name => Name_Tag);
855 -- For tagged types, determine the accessibility level by looking
856 -- at the type specific data of the dispatch table. Generate:
858 -- Type_Specific_Data (Address (Ref'Tag)).Access_Level
860 if Tagged_Type_Expansion then
861 Cond := Build_Get_Access_Level (Loc, Obj_Ref);
863 -- Use a runtime call to determine the accessibility level when
864 -- compiling on virtual machine targets. Generate:
866 -- Get_Access_Level (Ref'Tag)
870 Make_Function_Call (Loc,
872 New_Occurrence_Of (RTE (RE_Get_Access_Level), Loc),
873 Parameter_Associations => New_List (Obj_Ref));
880 Make_Integer_Literal (Loc, Type_Access_Level (PtrT)));
882 -- Due to the complexity and side effects of the check, utilize an
883 -- if statement instead of the regular Program_Error circuitry.
886 Make_Implicit_If_Statement (N,
888 Then_Statements => Stmts));
890 end Apply_Accessibility_Check;
894 Aggr_In_Place : constant Boolean := Is_Delayed_Aggregate (Exp);
895 Indic : constant Node_Id := Subtype_Mark (Expression (N));
896 T : constant Entity_Id := Entity (Indic);
898 Tag_Assign : Node_Id;
902 TagT : Entity_Id := Empty;
903 -- Type used as source for tag assignment
905 TagR : Node_Id := Empty;
906 -- Target reference for tag assignment
908 -- Start of processing for Expand_Allocator_Expression
911 -- Handle call to C++ constructor
913 if Is_CPP_Constructor_Call (Exp) then
914 Make_CPP_Constructor_Call_In_Allocator
916 Function_Call => Exp);
920 -- In the case of an Ada 2012 allocator whose initial value comes from a
921 -- function call, pass "the accessibility level determined by the point
922 -- of call" (AI05-0234) to the function. Conceptually, this belongs in
923 -- Expand_Call but it couldn't be done there (because the Etype of the
924 -- allocator wasn't set then) so we generate the parameter here. See
925 -- the Boolean variable Defer in (a block within) Expand_Call.
927 if Ada_Version >= Ada_2012 and then Nkind (Exp) = N_Function_Call then
932 if Nkind (Name (Exp)) = N_Explicit_Dereference then
933 Subp := Designated_Type (Etype (Prefix (Name (Exp))));
935 Subp := Entity (Name (Exp));
938 Subp := Ultimate_Alias (Subp);
940 if Present (Extra_Accessibility_Of_Result (Subp)) then
941 Add_Extra_Actual_To_Call
942 (Subprogram_Call => Exp,
943 Extra_Formal => Extra_Accessibility_Of_Result (Subp),
944 Extra_Actual => Dynamic_Accessibility_Level (PtrT));
949 -- Case of tagged type or type requiring finalization
951 if Is_Tagged_Type (T) or else Needs_Finalization (T) then
953 -- Ada 2005 (AI-318-02): If the initialization expression is a call
954 -- to a build-in-place function, then access to the allocated object
955 -- must be passed to the function. Currently we limit such functions
956 -- to those with constrained limited result subtypes, but eventually
957 -- we plan to expand the allowed forms of functions that are treated
958 -- as build-in-place.
960 if Ada_Version >= Ada_2005
961 and then Is_Build_In_Place_Function_Call (Exp)
963 Make_Build_In_Place_Call_In_Allocator (N, Exp);
964 Apply_Accessibility_Check (N, Built_In_Place => True);
968 -- Actions inserted before:
969 -- Temp : constant ptr_T := new T'(Expression);
970 -- Temp._tag = T'tag; -- when not class-wide
971 -- [Deep_]Adjust (Temp.all);
973 -- We analyze by hand the new internal allocator to avoid any
974 -- recursion and inappropriate call to Initialize.
976 -- We don't want to remove side effects when the expression must be
977 -- built in place. In the case of a build-in-place function call,
978 -- that could lead to a duplication of the call, which was already
979 -- substituted for the allocator.
981 if not Aggr_In_Place then
982 Remove_Side_Effects (Exp);
985 Temp := Make_Temporary (Loc, 'P', N);
987 -- For a class wide allocation generate the following code:
989 -- type Equiv_Record is record ... end record;
990 -- implicit subtype CW is <Class_Wide_Subytpe>;
991 -- temp : PtrT := new CW'(CW!(expr));
993 if Is_Class_Wide_Type (T) then
994 Expand_Subtype_From_Expr (Empty, T, Indic, Exp);
996 -- Ada 2005 (AI-251): If the expression is a class-wide interface
997 -- object we generate code to move up "this" to reference the
998 -- base of the object before allocating the new object.
1000 -- Note that Exp'Address is recursively expanded into a call
1001 -- to Base_Address (Exp.Tag)
1003 if Is_Class_Wide_Type (Etype (Exp))
1004 and then Is_Interface (Etype (Exp))
1005 and then Tagged_Type_Expansion
1009 Unchecked_Convert_To (Entity (Indic),
1010 Make_Explicit_Dereference (Loc,
1011 Unchecked_Convert_To (RTE (RE_Tag_Ptr),
1012 Make_Attribute_Reference (Loc,
1014 Attribute_Name => Name_Address)))));
1018 Unchecked_Convert_To (Entity (Indic), Exp));
1021 Analyze_And_Resolve (Expression (N), Entity (Indic));
1024 -- Processing for allocators returning non-interface types
1026 if not Is_Interface (Directly_Designated_Type (PtrT)) then
1027 if Aggr_In_Place then
1029 Make_Object_Declaration (Loc,
1030 Defining_Identifier => Temp,
1031 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1033 Make_Allocator (Loc,
1035 New_Occurrence_Of (Etype (Exp), Loc)));
1037 -- Copy the Comes_From_Source flag for the allocator we just
1038 -- built, since logically this allocator is a replacement of
1039 -- the original allocator node. This is for proper handling of
1040 -- restriction No_Implicit_Heap_Allocations.
1042 Set_Comes_From_Source
1043 (Expression (Temp_Decl), Comes_From_Source (N));
1045 Set_No_Initialization (Expression (Temp_Decl));
1046 Insert_Action (N, Temp_Decl);
1048 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1049 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1051 -- Attach the object to the associated finalization master.
1052 -- This is done manually on .NET/JVM since those compilers do
1053 -- no support pools and can't benefit from internally generated
1054 -- Allocate / Deallocate procedures.
1056 if VM_Target /= No_VM
1057 and then Is_Controlled (DesigT)
1058 and then Present (Finalization_Master (PtrT))
1062 Obj_Ref => New_Occurrence_Of (Temp, Loc),
1067 Node := Relocate_Node (N);
1068 Set_Analyzed (Node);
1071 Make_Object_Declaration (Loc,
1072 Defining_Identifier => Temp,
1073 Constant_Present => True,
1074 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1075 Expression => Node);
1077 Insert_Action (N, Temp_Decl);
1078 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1080 -- Attach the object to the associated finalization master.
1081 -- This is done manually on .NET/JVM since those compilers do
1082 -- no support pools and can't benefit from internally generated
1083 -- Allocate / Deallocate procedures.
1085 if VM_Target /= No_VM
1086 and then Is_Controlled (DesigT)
1087 and then Present (Finalization_Master (PtrT))
1092 New_Occurrence_Of (Temp, Loc),
1097 -- Ada 2005 (AI-251): Handle allocators whose designated type is an
1098 -- interface type. In this case we use the type of the qualified
1099 -- expression to allocate the object.
1103 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'T');
1108 Make_Full_Type_Declaration (Loc,
1109 Defining_Identifier => Def_Id,
1111 Make_Access_To_Object_Definition (Loc,
1112 All_Present => True,
1113 Null_Exclusion_Present => False,
1115 Is_Access_Constant (Etype (N)),
1116 Subtype_Indication =>
1117 New_Occurrence_Of (Etype (Exp), Loc)));
1119 Insert_Action (N, New_Decl);
1121 -- Inherit the allocation-related attributes from the original
1124 Set_Finalization_Master (Def_Id, Finalization_Master (PtrT));
1126 Set_Associated_Storage_Pool (Def_Id,
1127 Associated_Storage_Pool (PtrT));
1129 -- Declare the object using the previous type declaration
1131 if Aggr_In_Place then
1133 Make_Object_Declaration (Loc,
1134 Defining_Identifier => Temp,
1135 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1137 Make_Allocator (Loc,
1138 New_Occurrence_Of (Etype (Exp), Loc)));
1140 -- Copy the Comes_From_Source flag for the allocator we just
1141 -- built, since logically this allocator is a replacement of
1142 -- the original allocator node. This is for proper handling
1143 -- of restriction No_Implicit_Heap_Allocations.
1145 Set_Comes_From_Source
1146 (Expression (Temp_Decl), Comes_From_Source (N));
1148 Set_No_Initialization (Expression (Temp_Decl));
1149 Insert_Action (N, Temp_Decl);
1151 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1152 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1155 Node := Relocate_Node (N);
1156 Set_Analyzed (Node);
1159 Make_Object_Declaration (Loc,
1160 Defining_Identifier => Temp,
1161 Constant_Present => True,
1162 Object_Definition => New_Occurrence_Of (Def_Id, Loc),
1163 Expression => Node);
1165 Insert_Action (N, Temp_Decl);
1166 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1169 -- Generate an additional object containing the address of the
1170 -- returned object. The type of this second object declaration
1171 -- is the correct type required for the common processing that
1172 -- is still performed by this subprogram. The displacement of
1173 -- this pointer to reference the component associated with the
1174 -- interface type will be done at the end of common processing.
1177 Make_Object_Declaration (Loc,
1178 Defining_Identifier => Make_Temporary (Loc, 'P'),
1179 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1181 Unchecked_Convert_To (PtrT,
1182 New_Occurrence_Of (Temp, Loc)));
1184 Insert_Action (N, New_Decl);
1186 Temp_Decl := New_Decl;
1187 Temp := Defining_Identifier (New_Decl);
1191 Apply_Accessibility_Check (Temp);
1193 -- Generate the tag assignment
1195 -- Suppress the tag assignment when VM_Target because VM tags are
1196 -- represented implicitly in objects.
1198 if not Tagged_Type_Expansion then
1201 -- Ada 2005 (AI-251): Suppress the tag assignment with class-wide
1202 -- interface objects because in this case the tag does not change.
1204 elsif Is_Interface (Directly_Designated_Type (Etype (N))) then
1205 pragma Assert (Is_Class_Wide_Type
1206 (Directly_Designated_Type (Etype (N))));
1209 elsif Is_Tagged_Type (T) and then not Is_Class_Wide_Type (T) then
1211 TagR := New_Occurrence_Of (Temp, Loc);
1213 elsif Is_Private_Type (T)
1214 and then Is_Tagged_Type (Underlying_Type (T))
1216 TagT := Underlying_Type (T);
1218 Unchecked_Convert_To (Underlying_Type (T),
1219 Make_Explicit_Dereference (Loc,
1220 Prefix => New_Occurrence_Of (Temp, Loc)));
1223 if Present (TagT) then
1225 Full_T : constant Entity_Id := Underlying_Type (TagT);
1229 Make_Assignment_Statement (Loc,
1231 Make_Selected_Component (Loc,
1235 (First_Tag_Component (Full_T), Loc)),
1238 Unchecked_Convert_To (RTE (RE_Tag),
1241 (First_Elmt (Access_Disp_Table (Full_T))), Loc)));
1244 -- The previous assignment has to be done in any case
1246 Set_Assignment_OK (Name (Tag_Assign));
1247 Insert_Action (N, Tag_Assign);
1250 if Needs_Finalization (DesigT) and then Needs_Finalization (T) then
1252 -- Generate an Adjust call if the object will be moved. In Ada
1253 -- 2005, the object may be inherently limited, in which case
1254 -- there is no Adjust procedure, and the object is built in
1255 -- place. In Ada 95, the object can be limited but not
1256 -- inherently limited if this allocator came from a return
1257 -- statement (we're allocating the result on the secondary
1258 -- stack). In that case, the object will be moved, so we _do_
1261 if not Aggr_In_Place
1262 and then not Is_Limited_View (T)
1266 -- An unchecked conversion is needed in the classwide case
1267 -- because the designated type can be an ancestor of the
1268 -- subtype mark of the allocator.
1272 Unchecked_Convert_To (T,
1273 Make_Explicit_Dereference (Loc,
1274 Prefix => New_Occurrence_Of (Temp, Loc))),
1279 -- Set_Finalize_Address (<PtrT>FM, <T>FD'Unrestricted_Access);
1281 -- Do not generate this call in the following cases:
1283 -- * .NET/JVM - these targets do not support address arithmetic
1284 -- and unchecked conversion, key elements of Finalize_Address.
1286 -- * CodePeer mode - TSS primitive Finalize_Address is not
1287 -- created in this mode.
1289 if VM_Target = No_VM
1290 and then not CodePeer_Mode
1291 and then Present (Finalization_Master (PtrT))
1292 and then Present (Temp_Decl)
1293 and then Nkind (Expression (Temp_Decl)) = N_Allocator
1296 Make_Set_Finalize_Address_Call
1303 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1304 Analyze_And_Resolve (N, PtrT);
1306 -- Ada 2005 (AI-251): Displace the pointer to reference the record
1307 -- component containing the secondary dispatch table of the interface
1310 if Is_Interface (Directly_Designated_Type (PtrT)) then
1311 Displace_Allocator_Pointer (N);
1314 elsif Aggr_In_Place then
1315 Temp := Make_Temporary (Loc, 'P', N);
1317 Make_Object_Declaration (Loc,
1318 Defining_Identifier => Temp,
1319 Object_Definition => New_Occurrence_Of (PtrT, Loc),
1321 Make_Allocator (Loc,
1322 Expression => New_Occurrence_Of (Etype (Exp), Loc)));
1324 -- Copy the Comes_From_Source flag for the allocator we just built,
1325 -- since logically this allocator is a replacement of the original
1326 -- allocator node. This is for proper handling of restriction
1327 -- No_Implicit_Heap_Allocations.
1329 Set_Comes_From_Source
1330 (Expression (Temp_Decl), Comes_From_Source (N));
1332 Set_No_Initialization (Expression (Temp_Decl));
1333 Insert_Action (N, Temp_Decl);
1335 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
1336 Convert_Aggr_In_Allocator (N, Temp_Decl, Exp);
1338 -- Attach the object to the associated finalization master. Thisis
1339 -- done manually on .NET/JVM since those compilers do no support
1340 -- pools and cannot benefit from internally generated Allocate and
1341 -- Deallocate procedures.
1343 if VM_Target /= No_VM
1344 and then Is_Controlled (DesigT)
1345 and then Present (Finalization_Master (PtrT))
1349 (Obj_Ref => New_Occurrence_Of (Temp, Loc),
1353 Rewrite (N, New_Occurrence_Of (Temp, Loc));
1354 Analyze_And_Resolve (N, PtrT);
1356 elsif Is_Access_Type (T) and then Can_Never_Be_Null (T) then
1357 Install_Null_Excluding_Check (Exp);
1359 elsif Is_Access_Type (DesigT)
1360 and then Nkind (Exp) = N_Allocator
1361 and then Nkind (Expression (Exp)) /= N_Qualified_Expression
1363 -- Apply constraint to designated subtype indication
1365 Apply_Constraint_Check (Expression (Exp),
1366 Designated_Type (DesigT),
1367 No_Sliding => True);
1369 if Nkind (Expression (Exp)) = N_Raise_Constraint_Error then
1371 -- Propagate constraint_error to enclosing allocator
1373 Rewrite (Exp, New_Copy (Expression (Exp)));
1377 Build_Allocate_Deallocate_Proc (N, True);
1380 -- type A is access T1;
1381 -- X : A := new T2'(...);
1382 -- T1 and T2 can be different subtypes, and we might need to check
1383 -- both constraints. First check against the type of the qualified
1386 Apply_Constraint_Check (Exp, T, No_Sliding => True);
1388 if Do_Range_Check (Exp) then
1389 Set_Do_Range_Check (Exp, False);
1390 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1393 -- A check is also needed in cases where the designated subtype is
1394 -- constrained and differs from the subtype given in the qualified
1395 -- expression. Note that the check on the qualified expression does
1396 -- not allow sliding, but this check does (a relaxation from Ada 83).
1398 if Is_Constrained (DesigT)
1399 and then not Subtypes_Statically_Match (T, DesigT)
1401 Apply_Constraint_Check
1402 (Exp, DesigT, No_Sliding => False);
1404 if Do_Range_Check (Exp) then
1405 Set_Do_Range_Check (Exp, False);
1406 Generate_Range_Check (Exp, DesigT, CE_Range_Check_Failed);
1410 -- For an access to unconstrained packed array, GIGI needs to see an
1411 -- expression with a constrained subtype in order to compute the
1412 -- proper size for the allocator.
1414 if Is_Array_Type (T)
1415 and then not Is_Constrained (T)
1416 and then Is_Packed (T)
1419 ConstrT : constant Entity_Id := Make_Temporary (Loc, 'A');
1420 Internal_Exp : constant Node_Id := Relocate_Node (Exp);
1423 Make_Subtype_Declaration (Loc,
1424 Defining_Identifier => ConstrT,
1425 Subtype_Indication =>
1426 Make_Subtype_From_Expr (Internal_Exp, T)));
1427 Freeze_Itype (ConstrT, Exp);
1428 Rewrite (Exp, OK_Convert_To (ConstrT, Internal_Exp));
1432 -- Ada 2005 (AI-318-02): If the initialization expression is a call
1433 -- to a build-in-place function, then access to the allocated object
1434 -- must be passed to the function. Currently we limit such functions
1435 -- to those with constrained limited result subtypes, but eventually
1436 -- we plan to expand the allowed forms of functions that are treated
1437 -- as build-in-place.
1439 if Ada_Version >= Ada_2005
1440 and then Is_Build_In_Place_Function_Call (Exp)
1442 Make_Build_In_Place_Call_In_Allocator (N, Exp);
1447 when RE_Not_Available =>
1449 end Expand_Allocator_Expression;
1451 -----------------------------
1452 -- Expand_Array_Comparison --
1453 -----------------------------
1455 -- Expansion is only required in the case of array types. For the unpacked
1456 -- case, an appropriate runtime routine is called. For packed cases, and
1457 -- also in some other cases where a runtime routine cannot be called, the
1458 -- form of the expansion is:
1460 -- [body for greater_nn; boolean_expression]
1462 -- The body is built by Make_Array_Comparison_Op, and the form of the
1463 -- Boolean expression depends on the operator involved.
1465 procedure Expand_Array_Comparison (N : Node_Id) is
1466 Loc : constant Source_Ptr := Sloc (N);
1467 Op1 : Node_Id := Left_Opnd (N);
1468 Op2 : Node_Id := Right_Opnd (N);
1469 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
1470 Ctyp : constant Entity_Id := Component_Type (Typ1);
1473 Func_Body : Node_Id;
1474 Func_Name : Entity_Id;
1478 Byte_Addressable : constant Boolean := System_Storage_Unit = Byte'Size;
1479 -- True for byte addressable target
1481 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean;
1482 -- Returns True if the length of the given operand is known to be less
1483 -- than 4. Returns False if this length is known to be four or greater
1484 -- or is not known at compile time.
1486 ------------------------
1487 -- Length_Less_Than_4 --
1488 ------------------------
1490 function Length_Less_Than_4 (Opnd : Node_Id) return Boolean is
1491 Otyp : constant Entity_Id := Etype (Opnd);
1494 if Ekind (Otyp) = E_String_Literal_Subtype then
1495 return String_Literal_Length (Otyp) < 4;
1499 Ityp : constant Entity_Id := Etype (First_Index (Otyp));
1500 Lo : constant Node_Id := Type_Low_Bound (Ityp);
1501 Hi : constant Node_Id := Type_High_Bound (Ityp);
1506 if Compile_Time_Known_Value (Lo) then
1507 Lov := Expr_Value (Lo);
1512 if Compile_Time_Known_Value (Hi) then
1513 Hiv := Expr_Value (Hi);
1518 return Hiv < Lov + 3;
1521 end Length_Less_Than_4;
1523 -- Start of processing for Expand_Array_Comparison
1526 -- Deal first with unpacked case, where we can call a runtime routine
1527 -- except that we avoid this for targets for which are not addressable
1528 -- by bytes, and for the JVM/CIL, since they do not support direct
1529 -- addressing of array components.
1531 if not Is_Bit_Packed_Array (Typ1)
1532 and then Byte_Addressable
1533 and then VM_Target = No_VM
1535 -- The call we generate is:
1537 -- Compare_Array_xn[_Unaligned]
1538 -- (left'address, right'address, left'length, right'length) <op> 0
1540 -- x = U for unsigned, S for signed
1541 -- n = 8,16,32,64 for component size
1542 -- Add _Unaligned if length < 4 and component size is 8.
1543 -- <op> is the standard comparison operator
1545 if Component_Size (Typ1) = 8 then
1546 if Length_Less_Than_4 (Op1)
1548 Length_Less_Than_4 (Op2)
1550 if Is_Unsigned_Type (Ctyp) then
1551 Comp := RE_Compare_Array_U8_Unaligned;
1553 Comp := RE_Compare_Array_S8_Unaligned;
1557 if Is_Unsigned_Type (Ctyp) then
1558 Comp := RE_Compare_Array_U8;
1560 Comp := RE_Compare_Array_S8;
1564 elsif Component_Size (Typ1) = 16 then
1565 if Is_Unsigned_Type (Ctyp) then
1566 Comp := RE_Compare_Array_U16;
1568 Comp := RE_Compare_Array_S16;
1571 elsif Component_Size (Typ1) = 32 then
1572 if Is_Unsigned_Type (Ctyp) then
1573 Comp := RE_Compare_Array_U32;
1575 Comp := RE_Compare_Array_S32;
1578 else pragma Assert (Component_Size (Typ1) = 64);
1579 if Is_Unsigned_Type (Ctyp) then
1580 Comp := RE_Compare_Array_U64;
1582 Comp := RE_Compare_Array_S64;
1586 Remove_Side_Effects (Op1, Name_Req => True);
1587 Remove_Side_Effects (Op2, Name_Req => True);
1590 Make_Function_Call (Sloc (Op1),
1591 Name => New_Occurrence_Of (RTE (Comp), Loc),
1593 Parameter_Associations => New_List (
1594 Make_Attribute_Reference (Loc,
1595 Prefix => Relocate_Node (Op1),
1596 Attribute_Name => Name_Address),
1598 Make_Attribute_Reference (Loc,
1599 Prefix => Relocate_Node (Op2),
1600 Attribute_Name => Name_Address),
1602 Make_Attribute_Reference (Loc,
1603 Prefix => Relocate_Node (Op1),
1604 Attribute_Name => Name_Length),
1606 Make_Attribute_Reference (Loc,
1607 Prefix => Relocate_Node (Op2),
1608 Attribute_Name => Name_Length))));
1611 Make_Integer_Literal (Sloc (Op2),
1614 Analyze_And_Resolve (Op1, Standard_Integer);
1615 Analyze_And_Resolve (Op2, Standard_Integer);
1619 -- Cases where we cannot make runtime call
1621 -- For (a <= b) we convert to not (a > b)
1623 if Chars (N) = Name_Op_Le then
1629 Right_Opnd => Op2)));
1630 Analyze_And_Resolve (N, Standard_Boolean);
1633 -- For < the Boolean expression is
1634 -- greater__nn (op2, op1)
1636 elsif Chars (N) = Name_Op_Lt then
1637 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1641 Op1 := Right_Opnd (N);
1642 Op2 := Left_Opnd (N);
1644 -- For (a >= b) we convert to not (a < b)
1646 elsif Chars (N) = Name_Op_Ge then
1652 Right_Opnd => Op2)));
1653 Analyze_And_Resolve (N, Standard_Boolean);
1656 -- For > the Boolean expression is
1657 -- greater__nn (op1, op2)
1660 pragma Assert (Chars (N) = Name_Op_Gt);
1661 Func_Body := Make_Array_Comparison_Op (Typ1, N);
1664 Func_Name := Defining_Unit_Name (Specification (Func_Body));
1666 Make_Function_Call (Loc,
1667 Name => New_Occurrence_Of (Func_Name, Loc),
1668 Parameter_Associations => New_List (Op1, Op2));
1670 Insert_Action (N, Func_Body);
1672 Analyze_And_Resolve (N, Standard_Boolean);
1675 when RE_Not_Available =>
1677 end Expand_Array_Comparison;
1679 ---------------------------
1680 -- Expand_Array_Equality --
1681 ---------------------------
1683 -- Expand an equality function for multi-dimensional arrays. Here is an
1684 -- example of such a function for Nb_Dimension = 2
1686 -- function Enn (A : atyp; B : btyp) return boolean is
1688 -- if (A'length (1) = 0 or else A'length (2) = 0)
1690 -- (B'length (1) = 0 or else B'length (2) = 0)
1692 -- return True; -- RM 4.5.2(22)
1695 -- if A'length (1) /= B'length (1)
1697 -- A'length (2) /= B'length (2)
1699 -- return False; -- RM 4.5.2(23)
1703 -- A1 : Index_T1 := A'first (1);
1704 -- B1 : Index_T1 := B'first (1);
1708 -- A2 : Index_T2 := A'first (2);
1709 -- B2 : Index_T2 := B'first (2);
1712 -- if A (A1, A2) /= B (B1, B2) then
1716 -- exit when A2 = A'last (2);
1717 -- A2 := Index_T2'succ (A2);
1718 -- B2 := Index_T2'succ (B2);
1722 -- exit when A1 = A'last (1);
1723 -- A1 := Index_T1'succ (A1);
1724 -- B1 := Index_T1'succ (B1);
1731 -- Note on the formal types used (atyp and btyp). If either of the arrays
1732 -- is of a private type, we use the underlying type, and do an unchecked
1733 -- conversion of the actual. If either of the arrays has a bound depending
1734 -- on a discriminant, then we use the base type since otherwise we have an
1735 -- escaped discriminant in the function.
1737 -- If both arrays are constrained and have the same bounds, we can generate
1738 -- a loop with an explicit iteration scheme using a 'Range attribute over
1741 function Expand_Array_Equality
1746 Typ : Entity_Id) return Node_Id
1748 Loc : constant Source_Ptr := Sloc (Nod);
1749 Decls : constant List_Id := New_List;
1750 Index_List1 : constant List_Id := New_List;
1751 Index_List2 : constant List_Id := New_List;
1755 Func_Name : Entity_Id;
1756 Func_Body : Node_Id;
1758 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
1759 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
1763 -- The parameter types to be used for the formals
1768 Num : Int) return Node_Id;
1769 -- This builds the attribute reference Arr'Nam (Expr)
1771 function Component_Equality (Typ : Entity_Id) return Node_Id;
1772 -- Create one statement to compare corresponding components, designated
1773 -- by a full set of indexes.
1775 function Get_Arg_Type (N : Node_Id) return Entity_Id;
1776 -- Given one of the arguments, computes the appropriate type to be used
1777 -- for that argument in the corresponding function formal
1779 function Handle_One_Dimension
1781 Index : Node_Id) return Node_Id;
1782 -- This procedure returns the following code
1785 -- Bn : Index_T := B'First (N);
1789 -- exit when An = A'Last (N);
1790 -- An := Index_T'Succ (An)
1791 -- Bn := Index_T'Succ (Bn)
1795 -- If both indexes are constrained and identical, the procedure
1796 -- returns a simpler loop:
1798 -- for An in A'Range (N) loop
1802 -- N is the dimension for which we are generating a loop. Index is the
1803 -- N'th index node, whose Etype is Index_Type_n in the above code. The
1804 -- xxx statement is either the loop or declare for the next dimension
1805 -- or if this is the last dimension the comparison of corresponding
1806 -- components of the arrays.
1808 -- The actual way the code works is to return the comparison of
1809 -- corresponding components for the N+1 call. That's neater.
1811 function Test_Empty_Arrays return Node_Id;
1812 -- This function constructs the test for both arrays being empty
1813 -- (A'length (1) = 0 or else A'length (2) = 0 or else ...)
1815 -- (B'length (1) = 0 or else B'length (2) = 0 or else ...)
1817 function Test_Lengths_Correspond return Node_Id;
1818 -- This function constructs the test for arrays having different lengths
1819 -- in at least one index position, in which case the resulting code is:
1821 -- A'length (1) /= B'length (1)
1823 -- A'length (2) /= B'length (2)
1834 Num : Int) return Node_Id
1838 Make_Attribute_Reference (Loc,
1839 Attribute_Name => Nam,
1840 Prefix => New_Occurrence_Of (Arr, Loc),
1841 Expressions => New_List (Make_Integer_Literal (Loc, Num)));
1844 ------------------------
1845 -- Component_Equality --
1846 ------------------------
1848 function Component_Equality (Typ : Entity_Id) return Node_Id is
1853 -- if a(i1...) /= b(j1...) then return false; end if;
1856 Make_Indexed_Component (Loc,
1857 Prefix => Make_Identifier (Loc, Chars (A)),
1858 Expressions => Index_List1);
1861 Make_Indexed_Component (Loc,
1862 Prefix => Make_Identifier (Loc, Chars (B)),
1863 Expressions => Index_List2);
1865 Test := Expand_Composite_Equality
1866 (Nod, Component_Type (Typ), L, R, Decls);
1868 -- If some (sub)component is an unchecked_union, the whole operation
1869 -- will raise program error.
1871 if Nkind (Test) = N_Raise_Program_Error then
1873 -- This node is going to be inserted at a location where a
1874 -- statement is expected: clear its Etype so analysis will set
1875 -- it to the expected Standard_Void_Type.
1877 Set_Etype (Test, Empty);
1882 Make_Implicit_If_Statement (Nod,
1883 Condition => Make_Op_Not (Loc, Right_Opnd => Test),
1884 Then_Statements => New_List (
1885 Make_Simple_Return_Statement (Loc,
1886 Expression => New_Occurrence_Of (Standard_False, Loc))));
1888 end Component_Equality;
1894 function Get_Arg_Type (N : Node_Id) return Entity_Id is
1905 T := Underlying_Type (T);
1907 X := First_Index (T);
1908 while Present (X) loop
1909 if Denotes_Discriminant (Type_Low_Bound (Etype (X)))
1911 Denotes_Discriminant (Type_High_Bound (Etype (X)))
1924 --------------------------
1925 -- Handle_One_Dimension --
1926 ---------------------------
1928 function Handle_One_Dimension
1930 Index : Node_Id) return Node_Id
1932 Need_Separate_Indexes : constant Boolean :=
1933 Ltyp /= Rtyp or else not Is_Constrained (Ltyp);
1934 -- If the index types are identical, and we are working with
1935 -- constrained types, then we can use the same index for both
1938 An : constant Entity_Id := Make_Temporary (Loc, 'A');
1941 Index_T : Entity_Id;
1946 if N > Number_Dimensions (Ltyp) then
1947 return Component_Equality (Ltyp);
1950 -- Case where we generate a loop
1952 Index_T := Base_Type (Etype (Index));
1954 if Need_Separate_Indexes then
1955 Bn := Make_Temporary (Loc, 'B');
1960 Append (New_Occurrence_Of (An, Loc), Index_List1);
1961 Append (New_Occurrence_Of (Bn, Loc), Index_List2);
1963 Stm_List := New_List (
1964 Handle_One_Dimension (N + 1, Next_Index (Index)));
1966 if Need_Separate_Indexes then
1968 -- Generate guard for loop, followed by increments of indexes
1970 Append_To (Stm_List,
1971 Make_Exit_Statement (Loc,
1974 Left_Opnd => New_Occurrence_Of (An, Loc),
1975 Right_Opnd => Arr_Attr (A, Name_Last, N))));
1977 Append_To (Stm_List,
1978 Make_Assignment_Statement (Loc,
1979 Name => New_Occurrence_Of (An, Loc),
1981 Make_Attribute_Reference (Loc,
1982 Prefix => New_Occurrence_Of (Index_T, Loc),
1983 Attribute_Name => Name_Succ,
1984 Expressions => New_List (
1985 New_Occurrence_Of (An, Loc)))));
1987 Append_To (Stm_List,
1988 Make_Assignment_Statement (Loc,
1989 Name => New_Occurrence_Of (Bn, Loc),
1991 Make_Attribute_Reference (Loc,
1992 Prefix => New_Occurrence_Of (Index_T, Loc),
1993 Attribute_Name => Name_Succ,
1994 Expressions => New_List (
1995 New_Occurrence_Of (Bn, Loc)))));
1998 -- If separate indexes, we need a declare block for An and Bn, and a
1999 -- loop without an iteration scheme.
2001 if Need_Separate_Indexes then
2003 Make_Implicit_Loop_Statement (Nod, Statements => Stm_List);
2006 Make_Block_Statement (Loc,
2007 Declarations => New_List (
2008 Make_Object_Declaration (Loc,
2009 Defining_Identifier => An,
2010 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2011 Expression => Arr_Attr (A, Name_First, N)),
2013 Make_Object_Declaration (Loc,
2014 Defining_Identifier => Bn,
2015 Object_Definition => New_Occurrence_Of (Index_T, Loc),
2016 Expression => Arr_Attr (B, Name_First, N))),
2018 Handled_Statement_Sequence =>
2019 Make_Handled_Sequence_Of_Statements (Loc,
2020 Statements => New_List (Loop_Stm)));
2022 -- If no separate indexes, return loop statement with explicit
2023 -- iteration scheme on its own
2027 Make_Implicit_Loop_Statement (Nod,
2028 Statements => Stm_List,
2030 Make_Iteration_Scheme (Loc,
2031 Loop_Parameter_Specification =>
2032 Make_Loop_Parameter_Specification (Loc,
2033 Defining_Identifier => An,
2034 Discrete_Subtype_Definition =>
2035 Arr_Attr (A, Name_Range, N))));
2038 end Handle_One_Dimension;
2040 -----------------------
2041 -- Test_Empty_Arrays --
2042 -----------------------
2044 function Test_Empty_Arrays return Node_Id is
2054 for J in 1 .. Number_Dimensions (Ltyp) loop
2057 Left_Opnd => Arr_Attr (A, Name_Length, J),
2058 Right_Opnd => Make_Integer_Literal (Loc, 0));
2062 Left_Opnd => Arr_Attr (B, Name_Length, J),
2063 Right_Opnd => Make_Integer_Literal (Loc, 0));
2072 Left_Opnd => Relocate_Node (Alist),
2073 Right_Opnd => Atest);
2077 Left_Opnd => Relocate_Node (Blist),
2078 Right_Opnd => Btest);
2085 Right_Opnd => Blist);
2086 end Test_Empty_Arrays;
2088 -----------------------------
2089 -- Test_Lengths_Correspond --
2090 -----------------------------
2092 function Test_Lengths_Correspond return Node_Id is
2098 for J in 1 .. Number_Dimensions (Ltyp) loop
2101 Left_Opnd => Arr_Attr (A, Name_Length, J),
2102 Right_Opnd => Arr_Attr (B, Name_Length, J));
2109 Left_Opnd => Relocate_Node (Result),
2110 Right_Opnd => Rtest);
2115 end Test_Lengths_Correspond;
2117 -- Start of processing for Expand_Array_Equality
2120 Ltyp := Get_Arg_Type (Lhs);
2121 Rtyp := Get_Arg_Type (Rhs);
2123 -- For now, if the argument types are not the same, go to the base type,
2124 -- since the code assumes that the formals have the same type. This is
2125 -- fixable in future ???
2127 if Ltyp /= Rtyp then
2128 Ltyp := Base_Type (Ltyp);
2129 Rtyp := Base_Type (Rtyp);
2130 pragma Assert (Ltyp = Rtyp);
2133 -- Build list of formals for function
2135 Formals := New_List (
2136 Make_Parameter_Specification (Loc,
2137 Defining_Identifier => A,
2138 Parameter_Type => New_Occurrence_Of (Ltyp, Loc)),
2140 Make_Parameter_Specification (Loc,
2141 Defining_Identifier => B,
2142 Parameter_Type => New_Occurrence_Of (Rtyp, Loc)));
2144 Func_Name := Make_Temporary (Loc, 'E');
2146 -- Build statement sequence for function
2149 Make_Subprogram_Body (Loc,
2151 Make_Function_Specification (Loc,
2152 Defining_Unit_Name => Func_Name,
2153 Parameter_Specifications => Formals,
2154 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
2156 Declarations => Decls,
2158 Handled_Statement_Sequence =>
2159 Make_Handled_Sequence_Of_Statements (Loc,
2160 Statements => New_List (
2162 Make_Implicit_If_Statement (Nod,
2163 Condition => Test_Empty_Arrays,
2164 Then_Statements => New_List (
2165 Make_Simple_Return_Statement (Loc,
2167 New_Occurrence_Of (Standard_True, Loc)))),
2169 Make_Implicit_If_Statement (Nod,
2170 Condition => Test_Lengths_Correspond,
2171 Then_Statements => New_List (
2172 Make_Simple_Return_Statement (Loc,
2174 New_Occurrence_Of (Standard_False, Loc)))),
2176 Handle_One_Dimension (1, First_Index (Ltyp)),
2178 Make_Simple_Return_Statement (Loc,
2179 Expression => New_Occurrence_Of (Standard_True, Loc)))));
2181 Set_Has_Completion (Func_Name, True);
2182 Set_Is_Inlined (Func_Name);
2184 -- If the array type is distinct from the type of the arguments, it
2185 -- is the full view of a private type. Apply an unchecked conversion
2186 -- to insure that analysis of the call succeeds.
2196 or else Base_Type (Etype (Lhs)) /= Base_Type (Ltyp)
2198 L := OK_Convert_To (Ltyp, Lhs);
2202 or else Base_Type (Etype (Rhs)) /= Base_Type (Rtyp)
2204 R := OK_Convert_To (Rtyp, Rhs);
2207 Actuals := New_List (L, R);
2210 Append_To (Bodies, Func_Body);
2213 Make_Function_Call (Loc,
2214 Name => New_Occurrence_Of (Func_Name, Loc),
2215 Parameter_Associations => Actuals);
2216 end Expand_Array_Equality;
2218 -----------------------------
2219 -- Expand_Boolean_Operator --
2220 -----------------------------
2222 -- Note that we first get the actual subtypes of the operands, since we
2223 -- always want to deal with types that have bounds.
2225 procedure Expand_Boolean_Operator (N : Node_Id) is
2226 Typ : constant Entity_Id := Etype (N);
2229 -- Special case of bit packed array where both operands are known to be
2230 -- properly aligned. In this case we use an efficient run time routine
2231 -- to carry out the operation (see System.Bit_Ops).
2233 if Is_Bit_Packed_Array (Typ)
2234 and then not Is_Possibly_Unaligned_Object (Left_Opnd (N))
2235 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
2237 Expand_Packed_Boolean_Operator (N);
2241 -- For the normal non-packed case, the general expansion is to build
2242 -- function for carrying out the comparison (use Make_Boolean_Array_Op)
2243 -- and then inserting it into the tree. The original operator node is
2244 -- then rewritten as a call to this function. We also use this in the
2245 -- packed case if either operand is a possibly unaligned object.
2248 Loc : constant Source_Ptr := Sloc (N);
2249 L : constant Node_Id := Relocate_Node (Left_Opnd (N));
2250 R : constant Node_Id := Relocate_Node (Right_Opnd (N));
2251 Func_Body : Node_Id;
2252 Func_Name : Entity_Id;
2255 Convert_To_Actual_Subtype (L);
2256 Convert_To_Actual_Subtype (R);
2257 Ensure_Defined (Etype (L), N);
2258 Ensure_Defined (Etype (R), N);
2259 Apply_Length_Check (R, Etype (L));
2261 if Nkind (N) = N_Op_Xor then
2262 Silly_Boolean_Array_Xor_Test (N, Etype (L));
2265 if Nkind (Parent (N)) = N_Assignment_Statement
2266 and then Safe_In_Place_Array_Op (Name (Parent (N)), L, R)
2268 Build_Boolean_Array_Proc_Call (Parent (N), L, R);
2270 elsif Nkind (Parent (N)) = N_Op_Not
2271 and then Nkind (N) = N_Op_And
2273 Safe_In_Place_Array_Op (Name (Parent (Parent (N))), L, R)
2278 Func_Body := Make_Boolean_Array_Op (Etype (L), N);
2279 Func_Name := Defining_Unit_Name (Specification (Func_Body));
2280 Insert_Action (N, Func_Body);
2282 -- Now rewrite the expression with a call
2285 Make_Function_Call (Loc,
2286 Name => New_Occurrence_Of (Func_Name, Loc),
2287 Parameter_Associations =>
2290 Make_Type_Conversion
2291 (Loc, New_Occurrence_Of (Etype (L), Loc), R))));
2293 Analyze_And_Resolve (N, Typ);
2296 end Expand_Boolean_Operator;
2298 ------------------------------------------------
2299 -- Expand_Compare_Minimize_Eliminate_Overflow --
2300 ------------------------------------------------
2302 procedure Expand_Compare_Minimize_Eliminate_Overflow (N : Node_Id) is
2303 Loc : constant Source_Ptr := Sloc (N);
2305 Result_Type : constant Entity_Id := Etype (N);
2306 -- Capture result type (could be a derived boolean type)
2311 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
2312 -- Entity for Long_Long_Integer'Base
2314 Check : constant Overflow_Mode_Type := Overflow_Check_Mode;
2315 -- Current overflow checking mode
2318 procedure Set_False;
2319 -- These procedures rewrite N with an occurrence of Standard_True or
2320 -- Standard_False, and then makes a call to Warn_On_Known_Condition.
2326 procedure Set_False is
2328 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
2329 Warn_On_Known_Condition (N);
2336 procedure Set_True is
2338 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
2339 Warn_On_Known_Condition (N);
2342 -- Start of processing for Expand_Compare_Minimize_Eliminate_Overflow
2345 -- Nothing to do unless we have a comparison operator with operands
2346 -- that are signed integer types, and we are operating in either
2347 -- MINIMIZED or ELIMINATED overflow checking mode.
2349 if Nkind (N) not in N_Op_Compare
2350 or else Check not in Minimized_Or_Eliminated
2351 or else not Is_Signed_Integer_Type (Etype (Left_Opnd (N)))
2356 -- OK, this is the case we are interested in. First step is to process
2357 -- our operands using the Minimize_Eliminate circuitry which applies
2358 -- this processing to the two operand subtrees.
2360 Minimize_Eliminate_Overflows
2361 (Left_Opnd (N), Llo, Lhi, Top_Level => False);
2362 Minimize_Eliminate_Overflows
2363 (Right_Opnd (N), Rlo, Rhi, Top_Level => False);
2365 -- See if the range information decides the result of the comparison.
2366 -- We can only do this if we in fact have full range information (which
2367 -- won't be the case if either operand is bignum at this stage).
2369 if Llo /= No_Uint and then Rlo /= No_Uint then
2370 case N_Op_Compare (Nkind (N)) is
2372 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2374 elsif Llo > Rhi or else Lhi < Rlo then
2381 elsif Lhi < Rlo then
2388 elsif Lhi <= Rlo then
2395 elsif Lhi <= Rlo then
2402 elsif Lhi < Rlo then
2407 if Llo = Lhi and then Rlo = Rhi and then Llo = Rlo then
2409 elsif Llo > Rhi or else Lhi < Rlo then
2414 -- All done if we did the rewrite
2416 if Nkind (N) not in N_Op_Compare then
2421 -- Otherwise, time to do the comparison
2424 Ltype : constant Entity_Id := Etype (Left_Opnd (N));
2425 Rtype : constant Entity_Id := Etype (Right_Opnd (N));
2428 -- If the two operands have the same signed integer type we are
2429 -- all set, nothing more to do. This is the case where either
2430 -- both operands were unchanged, or we rewrote both of them to
2431 -- be Long_Long_Integer.
2433 -- Note: Entity for the comparison may be wrong, but it's not worth
2434 -- the effort to change it, since the back end does not use it.
2436 if Is_Signed_Integer_Type (Ltype)
2437 and then Base_Type (Ltype) = Base_Type (Rtype)
2441 -- Here if bignums are involved (can only happen in ELIMINATED mode)
2443 elsif Is_RTE (Ltype, RE_Bignum) or else Is_RTE (Rtype, RE_Bignum) then
2445 Left : Node_Id := Left_Opnd (N);
2446 Right : Node_Id := Right_Opnd (N);
2447 -- Bignum references for left and right operands
2450 if not Is_RTE (Ltype, RE_Bignum) then
2451 Left := Convert_To_Bignum (Left);
2452 elsif not Is_RTE (Rtype, RE_Bignum) then
2453 Right := Convert_To_Bignum (Right);
2456 -- We rewrite our node with:
2459 -- Bnn : Result_Type;
2461 -- M : Mark_Id := SS_Mark;
2463 -- Bnn := Big_xx (Left, Right); (xx = EQ, NT etc)
2471 Blk : constant Node_Id := Make_Bignum_Block (Loc);
2472 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
2476 case N_Op_Compare (Nkind (N)) is
2477 when N_Op_Eq => Ent := RE_Big_EQ;
2478 when N_Op_Ge => Ent := RE_Big_GE;
2479 when N_Op_Gt => Ent := RE_Big_GT;
2480 when N_Op_Le => Ent := RE_Big_LE;
2481 when N_Op_Lt => Ent := RE_Big_LT;
2482 when N_Op_Ne => Ent := RE_Big_NE;
2485 -- Insert assignment to Bnn into the bignum block
2488 (First (Statements (Handled_Statement_Sequence (Blk))),
2489 Make_Assignment_Statement (Loc,
2490 Name => New_Occurrence_Of (Bnn, Loc),
2492 Make_Function_Call (Loc,
2494 New_Occurrence_Of (RTE (Ent), Loc),
2495 Parameter_Associations => New_List (Left, Right))));
2497 -- Now do the rewrite with expression actions
2500 Make_Expression_With_Actions (Loc,
2501 Actions => New_List (
2502 Make_Object_Declaration (Loc,
2503 Defining_Identifier => Bnn,
2504 Object_Definition =>
2505 New_Occurrence_Of (Result_Type, Loc)),
2507 Expression => New_Occurrence_Of (Bnn, Loc)));
2508 Analyze_And_Resolve (N, Result_Type);
2512 -- No bignums involved, but types are different, so we must have
2513 -- rewritten one of the operands as a Long_Long_Integer but not
2516 -- If left operand is Long_Long_Integer, convert right operand
2517 -- and we are done (with a comparison of two Long_Long_Integers).
2519 elsif Ltype = LLIB then
2520 Convert_To_And_Rewrite (LLIB, Right_Opnd (N));
2521 Analyze_And_Resolve (Right_Opnd (N), LLIB, Suppress => All_Checks);
2524 -- If right operand is Long_Long_Integer, convert left operand
2525 -- and we are done (with a comparison of two Long_Long_Integers).
2527 -- This is the only remaining possibility
2529 else pragma Assert (Rtype = LLIB);
2530 Convert_To_And_Rewrite (LLIB, Left_Opnd (N));
2531 Analyze_And_Resolve (Left_Opnd (N), LLIB, Suppress => All_Checks);
2535 end Expand_Compare_Minimize_Eliminate_Overflow;
2537 -------------------------------
2538 -- Expand_Composite_Equality --
2539 -------------------------------
2541 -- This function is only called for comparing internal fields of composite
2542 -- types when these fields are themselves composites. This is a special
2543 -- case because it is not possible to respect normal Ada visibility rules.
2545 function Expand_Composite_Equality
2550 Bodies : List_Id) return Node_Id
2552 Loc : constant Source_Ptr := Sloc (Nod);
2553 Full_Type : Entity_Id;
2557 function Find_Primitive_Eq return Node_Id;
2558 -- AI05-0123: Locate primitive equality for type if it exists, and
2559 -- build the corresponding call. If operation is abstract, replace
2560 -- call with an explicit raise. Return Empty if there is no primitive.
2562 -----------------------
2563 -- Find_Primitive_Eq --
2564 -----------------------
2566 function Find_Primitive_Eq return Node_Id is
2571 Prim_E := First_Elmt (Collect_Primitive_Operations (Typ));
2572 while Present (Prim_E) loop
2573 Prim := Node (Prim_E);
2575 -- Locate primitive equality with the right signature
2577 if Chars (Prim) = Name_Op_Eq
2578 and then Etype (First_Formal (Prim)) =
2579 Etype (Next_Formal (First_Formal (Prim)))
2580 and then Etype (Prim) = Standard_Boolean
2582 if Is_Abstract_Subprogram (Prim) then
2584 Make_Raise_Program_Error (Loc,
2585 Reason => PE_Explicit_Raise);
2589 Make_Function_Call (Loc,
2590 Name => New_Occurrence_Of (Prim, Loc),
2591 Parameter_Associations => New_List (Lhs, Rhs));
2598 -- If not found, predefined operation will be used
2601 end Find_Primitive_Eq;
2603 -- Start of processing for Expand_Composite_Equality
2606 if Is_Private_Type (Typ) then
2607 Full_Type := Underlying_Type (Typ);
2612 -- If the private type has no completion the context may be the
2613 -- expansion of a composite equality for a composite type with some
2614 -- still incomplete components. The expression will not be analyzed
2615 -- until the enclosing type is completed, at which point this will be
2616 -- properly expanded, unless there is a bona fide completion error.
2618 if No (Full_Type) then
2619 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2622 Full_Type := Base_Type (Full_Type);
2624 -- When the base type itself is private, use the full view to expand
2625 -- the composite equality.
2627 if Is_Private_Type (Full_Type) then
2628 Full_Type := Underlying_Type (Full_Type);
2631 -- Case of array types
2633 if Is_Array_Type (Full_Type) then
2635 -- If the operand is an elementary type other than a floating-point
2636 -- type, then we can simply use the built-in block bitwise equality,
2637 -- since the predefined equality operators always apply and bitwise
2638 -- equality is fine for all these cases.
2640 if Is_Elementary_Type (Component_Type (Full_Type))
2641 and then not Is_Floating_Point_Type (Component_Type (Full_Type))
2643 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2645 -- For composite component types, and floating-point types, use the
2646 -- expansion. This deals with tagged component types (where we use
2647 -- the applicable equality routine) and floating-point, (where we
2648 -- need to worry about negative zeroes), and also the case of any
2649 -- composite type recursively containing such fields.
2652 return Expand_Array_Equality (Nod, Lhs, Rhs, Bodies, Full_Type);
2655 -- Case of tagged record types
2657 elsif Is_Tagged_Type (Full_Type) then
2659 -- Call the primitive operation "=" of this type
2661 if Is_Class_Wide_Type (Full_Type) then
2662 Full_Type := Root_Type (Full_Type);
2665 -- If this is derived from an untagged private type completed with a
2666 -- tagged type, it does not have a full view, so we use the primitive
2667 -- operations of the private type. This check should no longer be
2668 -- necessary when these types receive their full views ???
2670 if Is_Private_Type (Typ)
2671 and then not Is_Tagged_Type (Typ)
2672 and then not Is_Controlled (Typ)
2673 and then Is_Derived_Type (Typ)
2674 and then No (Full_View (Typ))
2676 Prim := First_Elmt (Collect_Primitive_Operations (Typ));
2678 Prim := First_Elmt (Primitive_Operations (Full_Type));
2682 Eq_Op := Node (Prim);
2683 exit when Chars (Eq_Op) = Name_Op_Eq
2684 and then Etype (First_Formal (Eq_Op)) =
2685 Etype (Next_Formal (First_Formal (Eq_Op)))
2686 and then Base_Type (Etype (Eq_Op)) = Standard_Boolean;
2688 pragma Assert (Present (Prim));
2691 Eq_Op := Node (Prim);
2694 Make_Function_Call (Loc,
2695 Name => New_Occurrence_Of (Eq_Op, Loc),
2696 Parameter_Associations =>
2698 (Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Lhs),
2699 Unchecked_Convert_To (Etype (First_Formal (Eq_Op)), Rhs)));
2701 -- Case of untagged record types
2703 elsif Is_Record_Type (Full_Type) then
2704 Eq_Op := TSS (Full_Type, TSS_Composite_Equality);
2706 if Present (Eq_Op) then
2707 if Etype (First_Formal (Eq_Op)) /= Full_Type then
2709 -- Inherited equality from parent type. Convert the actuals to
2710 -- match signature of operation.
2713 T : constant Entity_Id := Etype (First_Formal (Eq_Op));
2717 Make_Function_Call (Loc,
2718 Name => New_Occurrence_Of (Eq_Op, Loc),
2719 Parameter_Associations => New_List (
2720 OK_Convert_To (T, Lhs),
2721 OK_Convert_To (T, Rhs)));
2725 -- Comparison between Unchecked_Union components
2727 if Is_Unchecked_Union (Full_Type) then
2729 Lhs_Type : Node_Id := Full_Type;
2730 Rhs_Type : Node_Id := Full_Type;
2731 Lhs_Discr_Val : Node_Id;
2732 Rhs_Discr_Val : Node_Id;
2737 if Nkind (Lhs) = N_Selected_Component then
2738 Lhs_Type := Etype (Entity (Selector_Name (Lhs)));
2743 if Nkind (Rhs) = N_Selected_Component then
2744 Rhs_Type := Etype (Entity (Selector_Name (Rhs)));
2747 -- Lhs of the composite equality
2749 if Is_Constrained (Lhs_Type) then
2751 -- Since the enclosing record type can never be an
2752 -- Unchecked_Union (this code is executed for records
2753 -- that do not have variants), we may reference its
2756 if Nkind (Lhs) = N_Selected_Component
2757 and then Has_Per_Object_Constraint
2758 (Entity (Selector_Name (Lhs)))
2761 Make_Selected_Component (Loc,
2762 Prefix => Prefix (Lhs),
2765 (Get_Discriminant_Value
2766 (First_Discriminant (Lhs_Type),
2768 Stored_Constraint (Lhs_Type))));
2773 (Get_Discriminant_Value
2774 (First_Discriminant (Lhs_Type),
2776 Stored_Constraint (Lhs_Type)));
2780 -- It is not possible to infer the discriminant since
2781 -- the subtype is not constrained.
2784 Make_Raise_Program_Error (Loc,
2785 Reason => PE_Unchecked_Union_Restriction);
2788 -- Rhs of the composite equality
2790 if Is_Constrained (Rhs_Type) then
2791 if Nkind (Rhs) = N_Selected_Component
2792 and then Has_Per_Object_Constraint
2793 (Entity (Selector_Name (Rhs)))
2796 Make_Selected_Component (Loc,
2797 Prefix => Prefix (Rhs),
2800 (Get_Discriminant_Value
2801 (First_Discriminant (Rhs_Type),
2803 Stored_Constraint (Rhs_Type))));
2808 (Get_Discriminant_Value
2809 (First_Discriminant (Rhs_Type),
2811 Stored_Constraint (Rhs_Type)));
2816 Make_Raise_Program_Error (Loc,
2817 Reason => PE_Unchecked_Union_Restriction);
2820 -- Call the TSS equality function with the inferred
2821 -- discriminant values.
2824 Make_Function_Call (Loc,
2825 Name => New_Occurrence_Of (Eq_Op, Loc),
2826 Parameter_Associations => New_List (
2835 Make_Function_Call (Loc,
2836 Name => New_Occurrence_Of (Eq_Op, Loc),
2837 Parameter_Associations => New_List (Lhs, Rhs));
2841 -- Equality composes in Ada 2012 for untagged record types. It also
2842 -- composes for bounded strings, because they are part of the
2843 -- predefined environment. We could make it compose for bounded
2844 -- strings by making them tagged, or by making sure all subcomponents
2845 -- are set to the same value, even when not used. Instead, we have
2846 -- this special case in the compiler, because it's more efficient.
2848 elsif Ada_Version >= Ada_2012 or else Is_Bounded_String (Typ) then
2850 -- If no TSS has been created for the type, check whether there is
2851 -- a primitive equality declared for it.
2854 Op : constant Node_Id := Find_Primitive_Eq;
2857 -- Use user-defined primitive if it exists, otherwise use
2858 -- predefined equality.
2860 if Present (Op) then
2863 return Make_Op_Eq (Loc, Lhs, Rhs);
2868 return Expand_Record_Equality (Nod, Full_Type, Lhs, Rhs, Bodies);
2871 -- Non-composite types (always use predefined equality)
2874 return Make_Op_Eq (Loc, Left_Opnd => Lhs, Right_Opnd => Rhs);
2876 end Expand_Composite_Equality;
2878 ------------------------
2879 -- Expand_Concatenate --
2880 ------------------------
2882 procedure Expand_Concatenate (Cnode : Node_Id; Opnds : List_Id) is
2883 Loc : constant Source_Ptr := Sloc (Cnode);
2885 Atyp : constant Entity_Id := Base_Type (Etype (Cnode));
2886 -- Result type of concatenation
2888 Ctyp : constant Entity_Id := Base_Type (Component_Type (Etype (Cnode)));
2889 -- Component type. Elements of this component type can appear as one
2890 -- of the operands of concatenation as well as arrays.
2892 Istyp : constant Entity_Id := Etype (First_Index (Atyp));
2895 Ityp : constant Entity_Id := Base_Type (Istyp);
2896 -- Index type. This is the base type of the index subtype, and is used
2897 -- for all computed bounds (which may be out of range of Istyp in the
2898 -- case of null ranges).
2901 -- This is the type we use to do arithmetic to compute the bounds and
2902 -- lengths of operands. The choice of this type is a little subtle and
2903 -- is discussed in a separate section at the start of the body code.
2905 Concatenation_Error : exception;
2906 -- Raised if concatenation is sure to raise a CE
2908 Result_May_Be_Null : Boolean := True;
2909 -- Reset to False if at least one operand is encountered which is known
2910 -- at compile time to be non-null. Used for handling the special case
2911 -- of setting the high bound to the last operand high bound for a null
2912 -- result, thus ensuring a proper high bound in the super-flat case.
2914 N : constant Nat := List_Length (Opnds);
2915 -- Number of concatenation operands including possibly null operands
2918 -- Number of operands excluding any known to be null, except that the
2919 -- last operand is always retained, in case it provides the bounds for
2923 -- Current operand being processed in the loop through operands. After
2924 -- this loop is complete, always contains the last operand (which is not
2925 -- the same as Operands (NN), since null operands are skipped).
2927 -- Arrays describing the operands, only the first NN entries of each
2928 -- array are set (NN < N when we exclude known null operands).
2930 Is_Fixed_Length : array (1 .. N) of Boolean;
2931 -- True if length of corresponding operand known at compile time
2933 Operands : array (1 .. N) of Node_Id;
2934 -- Set to the corresponding entry in the Opnds list (but note that null
2935 -- operands are excluded, so not all entries in the list are stored).
2937 Fixed_Length : array (1 .. N) of Uint;
2938 -- Set to length of operand. Entries in this array are set only if the
2939 -- corresponding entry in Is_Fixed_Length is True.
2941 Opnd_Low_Bound : array (1 .. N) of Node_Id;
2942 -- Set to lower bound of operand. Either an integer literal in the case
2943 -- where the bound is known at compile time, else actual lower bound.
2944 -- The operand low bound is of type Ityp.
2946 Var_Length : array (1 .. N) of Entity_Id;
2947 -- Set to an entity of type Natural that contains the length of an
2948 -- operand whose length is not known at compile time. Entries in this
2949 -- array are set only if the corresponding entry in Is_Fixed_Length
2950 -- is False. The entity is of type Artyp.
2952 Aggr_Length : array (0 .. N) of Node_Id;
2953 -- The J'th entry in an expression node that represents the total length
2954 -- of operands 1 through J. It is either an integer literal node, or a
2955 -- reference to a constant entity with the right value, so it is fine
2956 -- to just do a Copy_Node to get an appropriate copy. The extra zero'th
2957 -- entry always is set to zero. The length is of type Artyp.
2959 Low_Bound : Node_Id;
2960 -- A tree node representing the low bound of the result (of type Ityp).
2961 -- This is either an integer literal node, or an identifier reference to
2962 -- a constant entity initialized to the appropriate value.
2964 Last_Opnd_Low_Bound : Node_Id;
2965 -- A tree node representing the low bound of the last operand. This
2966 -- need only be set if the result could be null. It is used for the
2967 -- special case of setting the right low bound for a null result.
2968 -- This is of type Ityp.
2970 Last_Opnd_High_Bound : Node_Id;
2971 -- A tree node representing the high bound of the last operand. This
2972 -- need only be set if the result could be null. It is used for the
2973 -- special case of setting the right high bound for a null result.
2974 -- This is of type Ityp.
2976 High_Bound : Node_Id;
2977 -- A tree node representing the high bound of the result (of type Ityp)
2980 -- Result of the concatenation (of type Ityp)
2982 Actions : constant List_Id := New_List;
2983 -- Collect actions to be inserted
2985 Known_Non_Null_Operand_Seen : Boolean;
2986 -- Set True during generation of the assignments of operands into
2987 -- result once an operand known to be non-null has been seen.
2989 function Make_Artyp_Literal (Val : Nat) return Node_Id;
2990 -- This function makes an N_Integer_Literal node that is returned in
2991 -- analyzed form with the type set to Artyp. Importantly this literal
2992 -- is not flagged as static, so that if we do computations with it that
2993 -- result in statically detected out of range conditions, we will not
2994 -- generate error messages but instead warning messages.
2996 function To_Artyp (X : Node_Id) return Node_Id;
2997 -- Given a node of type Ityp, returns the corresponding value of type
2998 -- Artyp. For non-enumeration types, this is a plain integer conversion.
2999 -- For enum types, the Pos of the value is returned.
3001 function To_Ityp (X : Node_Id) return Node_Id;
3002 -- The inverse function (uses Val in the case of enumeration types)
3004 ------------------------
3005 -- Make_Artyp_Literal --
3006 ------------------------
3008 function Make_Artyp_Literal (Val : Nat) return Node_Id is
3009 Result : constant Node_Id := Make_Integer_Literal (Loc, Val);
3011 Set_Etype (Result, Artyp);
3012 Set_Analyzed (Result, True);
3013 Set_Is_Static_Expression (Result, False);
3015 end Make_Artyp_Literal;
3021 function To_Artyp (X : Node_Id) return Node_Id is
3023 if Ityp = Base_Type (Artyp) then
3026 elsif Is_Enumeration_Type (Ityp) then
3028 Make_Attribute_Reference (Loc,
3029 Prefix => New_Occurrence_Of (Ityp, Loc),
3030 Attribute_Name => Name_Pos,
3031 Expressions => New_List (X));
3034 return Convert_To (Artyp, X);
3042 function To_Ityp (X : Node_Id) return Node_Id is
3044 if Is_Enumeration_Type (Ityp) then
3046 Make_Attribute_Reference (Loc,
3047 Prefix => New_Occurrence_Of (Ityp, Loc),
3048 Attribute_Name => Name_Val,
3049 Expressions => New_List (X));
3051 -- Case where we will do a type conversion
3054 if Ityp = Base_Type (Artyp) then
3057 return Convert_To (Ityp, X);
3062 -- Local Declarations
3064 Lib_Level_Target : constant Boolean :=
3065 Nkind (Parent (Cnode)) = N_Object_Declaration
3067 Is_Library_Level_Entity (Defining_Identifier (Parent (Cnode)));
3069 -- If the concatenation declares a library level entity, we call the
3070 -- built-in concatenation routines to prevent code bloat, regardless
3071 -- of optimization level. This is space-efficient, and prevent linking
3072 -- problems when units are compiled with different optimizations.
3074 Opnd_Typ : Entity_Id;
3081 -- Start of processing for Expand_Concatenate
3084 -- Choose an appropriate computational type
3086 -- We will be doing calculations of lengths and bounds in this routine
3087 -- and computing one from the other in some cases, e.g. getting the high
3088 -- bound by adding the length-1 to the low bound.
3090 -- We can't just use the index type, or even its base type for this
3091 -- purpose for two reasons. First it might be an enumeration type which
3092 -- is not suitable for computations of any kind, and second it may
3093 -- simply not have enough range. For example if the index type is
3094 -- -128..+127 then lengths can be up to 256, which is out of range of
3097 -- For enumeration types, we can simply use Standard_Integer, this is
3098 -- sufficient since the actual number of enumeration literals cannot
3099 -- possibly exceed the range of integer (remember we will be doing the
3100 -- arithmetic with POS values, not representation values).
3102 if Is_Enumeration_Type (Ityp) then
3103 Artyp := Standard_Integer;
3105 -- If index type is Positive, we use the standard unsigned type, to give
3106 -- more room on the top of the range, obviating the need for an overflow
3107 -- check when creating the upper bound. This is needed to avoid junk
3108 -- overflow checks in the common case of String types.
3110 -- ??? Disabled for now
3112 -- elsif Istyp = Standard_Positive then
3113 -- Artyp := Standard_Unsigned;
3115 -- For modular types, we use a 32-bit modular type for types whose size
3116 -- is in the range 1-31 bits. For 32-bit unsigned types, we use the
3117 -- identity type, and for larger unsigned types we use 64-bits.
3119 elsif Is_Modular_Integer_Type (Ityp) then
3120 if RM_Size (Ityp) < RM_Size (Standard_Unsigned) then
3121 Artyp := Standard_Unsigned;
3122 elsif RM_Size (Ityp) = RM_Size (Standard_Unsigned) then
3125 Artyp := RTE (RE_Long_Long_Unsigned);
3128 -- Similar treatment for signed types
3131 if RM_Size (Ityp) < RM_Size (Standard_Integer) then
3132 Artyp := Standard_Integer;
3133 elsif RM_Size (Ityp) = RM_Size (Standard_Integer) then
3136 Artyp := Standard_Long_Long_Integer;
3140 -- Supply dummy entry at start of length array
3142 Aggr_Length (0) := Make_Artyp_Literal (0);
3144 -- Go through operands setting up the above arrays
3148 Opnd := Remove_Head (Opnds);
3149 Opnd_Typ := Etype (Opnd);
3151 -- The parent got messed up when we put the operands in a list,
3152 -- so now put back the proper parent for the saved operand, that
3153 -- is to say the concatenation node, to make sure that each operand
3154 -- is seen as a subexpression, e.g. if actions must be inserted.
3156 Set_Parent (Opnd, Cnode);
3158 -- Set will be True when we have setup one entry in the array
3162 -- Singleton element (or character literal) case
3164 if Base_Type (Opnd_Typ) = Ctyp then
3166 Operands (NN) := Opnd;
3167 Is_Fixed_Length (NN) := True;
3168 Fixed_Length (NN) := Uint_1;
3169 Result_May_Be_Null := False;
3171 -- Set low bound of operand (no need to set Last_Opnd_High_Bound
3172 -- since we know that the result cannot be null).
3174 Opnd_Low_Bound (NN) :=
3175 Make_Attribute_Reference (Loc,
3176 Prefix => New_Occurrence_Of (Istyp, Loc),
3177 Attribute_Name => Name_First);
3181 -- String literal case (can only occur for strings of course)
3183 elsif Nkind (Opnd) = N_String_Literal then
3184 Len := String_Literal_Length (Opnd_Typ);
3187 Result_May_Be_Null := False;
3190 -- Capture last operand low and high bound if result could be null
3192 if J = N and then Result_May_Be_Null then
3193 Last_Opnd_Low_Bound :=
3194 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3196 Last_Opnd_High_Bound :=
3197 Make_Op_Subtract (Loc,
3199 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ)),
3200 Right_Opnd => Make_Integer_Literal (Loc, 1));
3203 -- Skip null string literal
3205 if J < N and then Len = 0 then
3210 Operands (NN) := Opnd;
3211 Is_Fixed_Length (NN) := True;
3213 -- Set length and bounds
3215 Fixed_Length (NN) := Len;
3217 Opnd_Low_Bound (NN) :=
3218 New_Copy_Tree (String_Literal_Low_Bound (Opnd_Typ));
3225 -- Check constrained case with known bounds
3227 if Is_Constrained (Opnd_Typ) then
3229 Index : constant Node_Id := First_Index (Opnd_Typ);
3230 Indx_Typ : constant Entity_Id := Etype (Index);
3231 Lo : constant Node_Id := Type_Low_Bound (Indx_Typ);
3232 Hi : constant Node_Id := Type_High_Bound (Indx_Typ);
3235 -- Fixed length constrained array type with known at compile
3236 -- time bounds is last case of fixed length operand.
3238 if Compile_Time_Known_Value (Lo)
3240 Compile_Time_Known_Value (Hi)
3243 Loval : constant Uint := Expr_Value (Lo);
3244 Hival : constant Uint := Expr_Value (Hi);
3245 Len : constant Uint :=
3246 UI_Max (Hival - Loval + 1, Uint_0);
3250 Result_May_Be_Null := False;
3253 -- Capture last operand bounds if result could be null
3255 if J = N and then Result_May_Be_Null then
3256 Last_Opnd_Low_Bound :=
3258 Make_Integer_Literal (Loc, Expr_Value (Lo)));
3260 Last_Opnd_High_Bound :=
3262 Make_Integer_Literal (Loc, Expr_Value (Hi)));
3265 -- Exclude null length case unless last operand
3267 if J < N and then Len = 0 then
3272 Operands (NN) := Opnd;
3273 Is_Fixed_Length (NN) := True;
3274 Fixed_Length (NN) := Len;
3276 Opnd_Low_Bound (NN) :=
3278 (Make_Integer_Literal (Loc, Expr_Value (Lo)));
3285 -- All cases where the length is not known at compile time, or the
3286 -- special case of an operand which is known to be null but has a
3287 -- lower bound other than 1 or is other than a string type.
3292 -- Capture operand bounds
3294 Opnd_Low_Bound (NN) :=
3295 Make_Attribute_Reference (Loc,
3297 Duplicate_Subexpr (Opnd, Name_Req => True),
3298 Attribute_Name => Name_First);
3300 -- Capture last operand bounds if result could be null
3302 if J = N and Result_May_Be_Null then
3303 Last_Opnd_Low_Bound :=
3305 Make_Attribute_Reference (Loc,
3307 Duplicate_Subexpr (Opnd, Name_Req => True),
3308 Attribute_Name => Name_First));
3310 Last_Opnd_High_Bound :=
3312 Make_Attribute_Reference (Loc,
3314 Duplicate_Subexpr (Opnd, Name_Req => True),
3315 Attribute_Name => Name_Last));
3318 -- Capture length of operand in entity
3320 Operands (NN) := Opnd;
3321 Is_Fixed_Length (NN) := False;
3323 Var_Length (NN) := Make_Temporary (Loc, 'L');
3326 Make_Object_Declaration (Loc,
3327 Defining_Identifier => Var_Length (NN),
3328 Constant_Present => True,
3329 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3331 Make_Attribute_Reference (Loc,
3333 Duplicate_Subexpr (Opnd, Name_Req => True),
3334 Attribute_Name => Name_Length)));
3338 -- Set next entry in aggregate length array
3340 -- For first entry, make either integer literal for fixed length
3341 -- or a reference to the saved length for variable length.
3344 if Is_Fixed_Length (1) then
3345 Aggr_Length (1) := Make_Integer_Literal (Loc, Fixed_Length (1));
3347 Aggr_Length (1) := New_Occurrence_Of (Var_Length (1), Loc);
3350 -- If entry is fixed length and only fixed lengths so far, make
3351 -- appropriate new integer literal adding new length.
3353 elsif Is_Fixed_Length (NN)
3354 and then Nkind (Aggr_Length (NN - 1)) = N_Integer_Literal
3357 Make_Integer_Literal (Loc,
3358 Intval => Fixed_Length (NN) + Intval (Aggr_Length (NN - 1)));
3360 -- All other cases, construct an addition node for the length and
3361 -- create an entity initialized to this length.
3364 Ent := Make_Temporary (Loc, 'L');
3366 if Is_Fixed_Length (NN) then
3367 Clen := Make_Integer_Literal (Loc, Fixed_Length (NN));
3369 Clen := New_Occurrence_Of (Var_Length (NN), Loc);
3373 Make_Object_Declaration (Loc,
3374 Defining_Identifier => Ent,
3375 Constant_Present => True,
3376 Object_Definition => New_Occurrence_Of (Artyp, Loc),
3379 Left_Opnd => New_Copy (Aggr_Length (NN - 1)),
3380 Right_Opnd => Clen)));
3382 Aggr_Length (NN) := Make_Identifier (Loc, Chars => Chars (Ent));
3389 -- If we have only skipped null operands, return the last operand
3396 -- If we have only one non-null operand, return it and we are done.
3397 -- There is one case in which this cannot be done, and that is when
3398 -- the sole operand is of the element type, in which case it must be
3399 -- converted to an array, and the easiest way of doing that is to go
3400 -- through the normal general circuit.
3402 if NN = 1 and then Base_Type (Etype (Operands (1))) /= Ctyp then
3403 Result := Operands (1);
3407 -- Cases where we have a real concatenation
3409 -- Next step is to find the low bound for the result array that we
3410 -- will allocate. The rules for this are in (RM 4.5.6(5-7)).
3412 -- If the ultimate ancestor of the index subtype is a constrained array
3413 -- definition, then the lower bound is that of the index subtype as
3414 -- specified by (RM 4.5.3(6)).
3416 -- The right test here is to go to the root type, and then the ultimate
3417 -- ancestor is the first subtype of this root type.
3419 if Is_Constrained (First_Subtype (Root_Type (Atyp))) then
3421 Make_Attribute_Reference (Loc,
3423 New_Occurrence_Of (First_Subtype (Root_Type (Atyp)), Loc),
3424 Attribute_Name => Name_First);
3426 -- If the first operand in the list has known length we know that
3427 -- the lower bound of the result is the lower bound of this operand.
3429 elsif Is_Fixed_Length (1) then
3430 Low_Bound := Opnd_Low_Bound (1);
3432 -- OK, we don't know the lower bound, we have to build a horrible
3433 -- if expression node of the form
3435 -- if Cond1'Length /= 0 then
3438 -- if Opnd2'Length /= 0 then
3443 -- The nesting ends either when we hit an operand whose length is known
3444 -- at compile time, or on reaching the last operand, whose low bound we
3445 -- take unconditionally whether or not it is null. It's easiest to do
3446 -- this with a recursive procedure:
3450 function Get_Known_Bound (J : Nat) return Node_Id;
3451 -- Returns the lower bound determined by operands J .. NN
3453 ---------------------
3454 -- Get_Known_Bound --
3455 ---------------------
3457 function Get_Known_Bound (J : Nat) return Node_Id is
3459 if Is_Fixed_Length (J) or else J = NN then
3460 return New_Copy (Opnd_Low_Bound (J));
3464 Make_If_Expression (Loc,
3465 Expressions => New_List (
3469 New_Occurrence_Of (Var_Length (J), Loc),
3471 Make_Integer_Literal (Loc, 0)),
3473 New_Copy (Opnd_Low_Bound (J)),
3474 Get_Known_Bound (J + 1)));
3476 end Get_Known_Bound;
3479 Ent := Make_Temporary (Loc, 'L');
3482 Make_Object_Declaration (Loc,
3483 Defining_Identifier => Ent,
3484 Constant_Present => True,
3485 Object_Definition => New_Occurrence_Of (Ityp, Loc),
3486 Expression => Get_Known_Bound (1)));
3488 Low_Bound := New_Occurrence_Of (Ent, Loc);
3492 -- Now we can safely compute the upper bound, normally
3493 -- Low_Bound + Length - 1.
3498 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3500 Make_Op_Subtract (Loc,
3501 Left_Opnd => New_Copy (Aggr_Length (NN)),
3502 Right_Opnd => Make_Artyp_Literal (1))));
3504 -- Note that calculation of the high bound may cause overflow in some
3505 -- very weird cases, so in the general case we need an overflow check on
3506 -- the high bound. We can avoid this for the common case of string types
3507 -- and other types whose index is Positive, since we chose a wider range
3508 -- for the arithmetic type.
3510 if Istyp /= Standard_Positive then
3511 Activate_Overflow_Check (High_Bound);
3514 -- Handle the exceptional case where the result is null, in which case
3515 -- case the bounds come from the last operand (so that we get the proper
3516 -- bounds if the last operand is super-flat).
3518 if Result_May_Be_Null then
3520 Make_If_Expression (Loc,
3521 Expressions => New_List (
3523 Left_Opnd => New_Copy (Aggr_Length (NN)),
3524 Right_Opnd => Make_Artyp_Literal (0)),
3525 Last_Opnd_Low_Bound,
3529 Make_If_Expression (Loc,
3530 Expressions => New_List (
3532 Left_Opnd => New_Copy (Aggr_Length (NN)),
3533 Right_Opnd => Make_Artyp_Literal (0)),
3534 Last_Opnd_High_Bound,
3538 -- Here is where we insert the saved up actions
3540 Insert_Actions (Cnode, Actions, Suppress => All_Checks);
3542 -- Now we construct an array object with appropriate bounds. We mark
3543 -- the target as internal to prevent useless initialization when
3544 -- Initialize_Scalars is enabled. Also since this is the actual result
3545 -- entity, we make sure we have debug information for the result.
3547 Ent := Make_Temporary (Loc, 'S');
3548 Set_Is_Internal (Ent);
3549 Set_Needs_Debug_Info (Ent);
3551 -- If the bound is statically known to be out of range, we do not want
3552 -- to abort, we want a warning and a runtime constraint error. Note that
3553 -- we have arranged that the result will not be treated as a static
3554 -- constant, so we won't get an illegality during this insertion.
3556 Insert_Action (Cnode,
3557 Make_Object_Declaration (Loc,
3558 Defining_Identifier => Ent,
3559 Object_Definition =>
3560 Make_Subtype_Indication (Loc,
3561 Subtype_Mark => New_Occurrence_Of (Atyp, Loc),
3563 Make_Index_Or_Discriminant_Constraint (Loc,
3564 Constraints => New_List (
3566 Low_Bound => Low_Bound,
3567 High_Bound => High_Bound))))),
3568 Suppress => All_Checks);
3570 -- If the result of the concatenation appears as the initializing
3571 -- expression of an object declaration, we can just rename the
3572 -- result, rather than copying it.
3574 Set_OK_To_Rename (Ent);
3576 -- Catch the static out of range case now
3578 if Raises_Constraint_Error (High_Bound) then
3579 raise Concatenation_Error;
3582 -- Now we will generate the assignments to do the actual concatenation
3584 -- There is one case in which we will not do this, namely when all the
3585 -- following conditions are met:
3587 -- The result type is Standard.String
3589 -- There are nine or fewer retained (non-null) operands
3591 -- The optimization level is -O0
3593 -- The corresponding System.Concat_n.Str_Concat_n routine is
3594 -- available in the run time.
3596 -- The debug flag gnatd.c is not set
3598 -- If all these conditions are met then we generate a call to the
3599 -- relevant concatenation routine. The purpose of this is to avoid
3600 -- undesirable code bloat at -O0.
3602 if Atyp = Standard_String
3603 and then NN in 2 .. 9
3604 and then (Lib_Level_Target
3606 ((Opt.Optimization_Level = 0 or else Debug_Flag_Dot_CC)
3607 and then not Debug_Flag_Dot_C))
3610 RR : constant array (Nat range 2 .. 9) of RE_Id :=
3621 if RTE_Available (RR (NN)) then
3623 Opnds : constant List_Id :=
3624 New_List (New_Occurrence_Of (Ent, Loc));
3627 for J in 1 .. NN loop
3628 if Is_List_Member (Operands (J)) then
3629 Remove (Operands (J));
3632 if Base_Type (Etype (Operands (J))) = Ctyp then
3634 Make_Aggregate (Loc,
3635 Component_Associations => New_List (
3636 Make_Component_Association (Loc,
3637 Choices => New_List (
3638 Make_Integer_Literal (Loc, 1)),
3639 Expression => Operands (J)))));
3642 Append_To (Opnds, Operands (J));
3646 Insert_Action (Cnode,
3647 Make_Procedure_Call_Statement (Loc,
3648 Name => New_Occurrence_Of (RTE (RR (NN)), Loc),
3649 Parameter_Associations => Opnds));
3651 Result := New_Occurrence_Of (Ent, Loc);
3658 -- Not special case so generate the assignments
3660 Known_Non_Null_Operand_Seen := False;
3662 for J in 1 .. NN loop
3664 Lo : constant Node_Id :=
3666 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3667 Right_Opnd => Aggr_Length (J - 1));
3669 Hi : constant Node_Id :=
3671 Left_Opnd => To_Artyp (New_Copy (Low_Bound)),
3673 Make_Op_Subtract (Loc,
3674 Left_Opnd => Aggr_Length (J),
3675 Right_Opnd => Make_Artyp_Literal (1)));
3678 -- Singleton case, simple assignment
3680 if Base_Type (Etype (Operands (J))) = Ctyp then
3681 Known_Non_Null_Operand_Seen := True;
3682 Insert_Action (Cnode,
3683 Make_Assignment_Statement (Loc,
3685 Make_Indexed_Component (Loc,
3686 Prefix => New_Occurrence_Of (Ent, Loc),
3687 Expressions => New_List (To_Ityp (Lo))),
3688 Expression => Operands (J)),
3689 Suppress => All_Checks);
3691 -- Array case, slice assignment, skipped when argument is fixed
3692 -- length and known to be null.
3694 elsif (not Is_Fixed_Length (J)) or else (Fixed_Length (J) > 0) then
3697 Make_Assignment_Statement (Loc,
3701 New_Occurrence_Of (Ent, Loc),
3704 Low_Bound => To_Ityp (Lo),
3705 High_Bound => To_Ityp (Hi))),
3706 Expression => Operands (J));
3708 if Is_Fixed_Length (J) then
3709 Known_Non_Null_Operand_Seen := True;
3711 elsif not Known_Non_Null_Operand_Seen then
3713 -- Here if operand length is not statically known and no
3714 -- operand known to be non-null has been processed yet.
3715 -- If operand length is 0, we do not need to perform the
3716 -- assignment, and we must avoid the evaluation of the
3717 -- high bound of the slice, since it may underflow if the
3718 -- low bound is Ityp'First.
3721 Make_Implicit_If_Statement (Cnode,
3725 New_Occurrence_Of (Var_Length (J), Loc),
3726 Right_Opnd => Make_Integer_Literal (Loc, 0)),
3727 Then_Statements => New_List (Assign));
3730 Insert_Action (Cnode, Assign, Suppress => All_Checks);
3736 -- Finally we build the result, which is a reference to the array object
3738 Result := New_Occurrence_Of (Ent, Loc);
3741 Rewrite (Cnode, Result);
3742 Analyze_And_Resolve (Cnode, Atyp);
3745 when Concatenation_Error =>
3747 -- Kill warning generated for the declaration of the static out of
3748 -- range high bound, and instead generate a Constraint_Error with
3749 -- an appropriate specific message.
3751 Kill_Dead_Code (Declaration_Node (Entity (High_Bound)));
3752 Apply_Compile_Time_Constraint_Error
3754 Msg => "concatenation result upper bound out of range??",
3755 Reason => CE_Range_Check_Failed);
3756 end Expand_Concatenate;
3758 ---------------------------------------------------
3759 -- Expand_Membership_Minimize_Eliminate_Overflow --
3760 ---------------------------------------------------
3762 procedure Expand_Membership_Minimize_Eliminate_Overflow (N : Node_Id) is
3763 pragma Assert (Nkind (N) = N_In);
3764 -- Despite the name, this routine applies only to N_In, not to
3765 -- N_Not_In. The latter is always rewritten as not (X in Y).
3767 Result_Type : constant Entity_Id := Etype (N);
3768 -- Capture result type, may be a derived boolean type
3770 Loc : constant Source_Ptr := Sloc (N);
3771 Lop : constant Node_Id := Left_Opnd (N);
3772 Rop : constant Node_Id := Right_Opnd (N);
3774 -- Note: there are many referencs to Etype (Lop) and Etype (Rop). It
3775 -- is thus tempting to capture these values, but due to the rewrites
3776 -- that occur as a result of overflow checking, these values change
3777 -- as we go along, and it is safe just to always use Etype explicitly.
3779 Restype : constant Entity_Id := Etype (N);
3783 -- Bounds in Minimize calls, not used currently
3785 LLIB : constant Entity_Id := Base_Type (Standard_Long_Long_Integer);
3786 -- Entity for Long_Long_Integer'Base (Standard should export this???)
3789 Minimize_Eliminate_Overflows (Lop, Lo, Hi, Top_Level => False);
3791 -- If right operand is a subtype name, and the subtype name has no
3792 -- predicate, then we can just replace the right operand with an
3793 -- explicit range T'First .. T'Last, and use the explicit range code.
3795 if Nkind (Rop) /= N_Range
3796 and then No (Predicate_Function (Etype (Rop)))
3799 Rtyp : constant Entity_Id := Etype (Rop);
3804 Make_Attribute_Reference (Loc,
3805 Attribute_Name => Name_First,
3806 Prefix => New_Occurrence_Of (Rtyp, Loc)),
3808 Make_Attribute_Reference (Loc,
3809 Attribute_Name => Name_Last,
3810 Prefix => New_Occurrence_Of (Rtyp, Loc))));
3811 Analyze_And_Resolve (Rop, Rtyp, Suppress => All_Checks);
3815 -- Here for the explicit range case. Note that the bounds of the range
3816 -- have not been processed for minimized or eliminated checks.
3818 if Nkind (Rop) = N_Range then
3819 Minimize_Eliminate_Overflows
3820 (Low_Bound (Rop), Lo, Hi, Top_Level => False);
3821 Minimize_Eliminate_Overflows
3822 (High_Bound (Rop), Lo, Hi, Top_Level => False);
3824 -- We have A in B .. C, treated as A >= B and then A <= C
3828 if Is_RTE (Etype (Lop), RE_Bignum)
3829 or else Is_RTE (Etype (Low_Bound (Rop)), RE_Bignum)
3830 or else Is_RTE (Etype (High_Bound (Rop)), RE_Bignum)
3833 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3834 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3835 L : constant Entity_Id :=
3836 Make_Defining_Identifier (Loc, Name_uL);
3837 Lopnd : constant Node_Id := Convert_To_Bignum (Lop);
3838 Lbound : constant Node_Id :=
3839 Convert_To_Bignum (Low_Bound (Rop));
3840 Hbound : constant Node_Id :=
3841 Convert_To_Bignum (High_Bound (Rop));
3843 -- Now we rewrite the membership test node to look like
3846 -- Bnn : Result_Type;
3848 -- M : Mark_Id := SS_Mark;
3849 -- L : Bignum := Lopnd;
3851 -- Bnn := Big_GE (L, Lbound) and then Big_LE (L, Hbound)
3859 -- Insert declaration of L into declarations of bignum block
3862 (Last (Declarations (Blk)),
3863 Make_Object_Declaration (Loc,
3864 Defining_Identifier => L,
3865 Object_Definition =>
3866 New_Occurrence_Of (RTE (RE_Bignum), Loc),
3867 Expression => Lopnd));
3869 -- Insert assignment to Bnn into expressions of bignum block
3872 (First (Statements (Handled_Statement_Sequence (Blk))),
3873 Make_Assignment_Statement (Loc,
3874 Name => New_Occurrence_Of (Bnn, Loc),
3878 Make_Function_Call (Loc,
3880 New_Occurrence_Of (RTE (RE_Big_GE), Loc),
3881 Parameter_Associations => New_List (
3882 New_Occurrence_Of (L, Loc),
3885 Make_Function_Call (Loc,
3887 New_Occurrence_Of (RTE (RE_Big_LE), Loc),
3888 Parameter_Associations => New_List (
3889 New_Occurrence_Of (L, Loc),
3892 -- Now rewrite the node
3895 Make_Expression_With_Actions (Loc,
3896 Actions => New_List (
3897 Make_Object_Declaration (Loc,
3898 Defining_Identifier => Bnn,
3899 Object_Definition =>
3900 New_Occurrence_Of (Result_Type, Loc)),
3902 Expression => New_Occurrence_Of (Bnn, Loc)));
3903 Analyze_And_Resolve (N, Result_Type);
3907 -- Here if no bignums around
3910 -- Case where types are all the same
3912 if Base_Type (Etype (Lop)) = Base_Type (Etype (Low_Bound (Rop)))
3914 Base_Type (Etype (Lop)) = Base_Type (Etype (High_Bound (Rop)))
3918 -- If types are not all the same, it means that we have rewritten
3919 -- at least one of them to be of type Long_Long_Integer, and we
3920 -- will convert the other operands to Long_Long_Integer.
3923 Convert_To_And_Rewrite (LLIB, Lop);
3924 Set_Analyzed (Lop, False);
3925 Analyze_And_Resolve (Lop, LLIB);
3927 -- For the right operand, avoid unnecessary recursion into
3928 -- this routine, we know that overflow is not possible.
3930 Convert_To_And_Rewrite (LLIB, Low_Bound (Rop));
3931 Convert_To_And_Rewrite (LLIB, High_Bound (Rop));
3932 Set_Analyzed (Rop, False);
3933 Analyze_And_Resolve (Rop, LLIB, Suppress => Overflow_Check);
3936 -- Now the three operands are of the same signed integer type,
3937 -- so we can use the normal expansion routine for membership,
3938 -- setting the flag to prevent recursion into this procedure.
3940 Set_No_Minimize_Eliminate (N);
3944 -- Right operand is a subtype name and the subtype has a predicate. We
3945 -- have to make sure the predicate is checked, and for that we need to
3946 -- use the standard N_In circuitry with appropriate types.
3949 pragma Assert (Present (Predicate_Function (Etype (Rop))));
3951 -- If types are "right", just call Expand_N_In preventing recursion
3953 if Base_Type (Etype (Lop)) = Base_Type (Etype (Rop)) then
3954 Set_No_Minimize_Eliminate (N);
3959 elsif Is_RTE (Etype (Lop), RE_Bignum) then
3961 -- For X in T, we want to rewrite our node as
3964 -- Bnn : Result_Type;
3967 -- M : Mark_Id := SS_Mark;
3968 -- Lnn : Long_Long_Integer'Base
3974 -- if not Bignum_In_LLI_Range (Nnn) then
3977 -- Lnn := From_Bignum (Nnn);
3979 -- Lnn in LLIB (T'Base'First) .. LLIB (T'Base'Last)
3980 -- and then T'Base (Lnn) in T;
3989 -- A bit gruesome, but there doesn't seem to be a simpler way
3992 Blk : constant Node_Id := Make_Bignum_Block (Loc);
3993 Bnn : constant Entity_Id := Make_Temporary (Loc, 'B', N);
3994 Lnn : constant Entity_Id := Make_Temporary (Loc, 'L', N);
3995 Nnn : constant Entity_Id := Make_Temporary (Loc, 'N', N);
3996 T : constant Entity_Id := Etype (Rop);
3997 TB : constant Entity_Id := Base_Type (T);
4001 -- Mark the last membership operation to prevent recursion
4005 Left_Opnd => Convert_To (TB, New_Occurrence_Of (Lnn, Loc)),
4006 Right_Opnd => New_Occurrence_Of (T, Loc));
4007 Set_No_Minimize_Eliminate (Nin);
4009 -- Now decorate the block
4012 (Last (Declarations (Blk)),
4013 Make_Object_Declaration (Loc,
4014 Defining_Identifier => Lnn,
4015 Object_Definition => New_Occurrence_Of (LLIB, Loc)));
4018 (Last (Declarations (Blk)),
4019 Make_Object_Declaration (Loc,
4020 Defining_Identifier => Nnn,
4021 Object_Definition =>
4022 New_Occurrence_Of (RTE (RE_Bignum), Loc)));
4025 (First (Statements (Handled_Statement_Sequence (Blk))),
4027 Make_Assignment_Statement (Loc,
4028 Name => New_Occurrence_Of (Nnn, Loc),
4029 Expression => Relocate_Node (Lop)),
4031 Make_Implicit_If_Statement (N,
4035 Make_Function_Call (Loc,
4038 (RTE (RE_Bignum_In_LLI_Range), Loc),
4039 Parameter_Associations => New_List (
4040 New_Occurrence_Of (Nnn, Loc)))),
4042 Then_Statements => New_List (
4043 Make_Assignment_Statement (Loc,
4044 Name => New_Occurrence_Of (Bnn, Loc),
4046 New_Occurrence_Of (Standard_False, Loc))),
4048 Else_Statements => New_List (
4049 Make_Assignment_Statement (Loc,
4050 Name => New_Occurrence_Of (Lnn, Loc),
4052 Make_Function_Call (Loc,
4054 New_Occurrence_Of (RTE (RE_From_Bignum), Loc),
4055 Parameter_Associations => New_List (
4056 New_Occurrence_Of (Nnn, Loc)))),
4058 Make_Assignment_Statement (Loc,
4059 Name => New_Occurrence_Of (Bnn, Loc),
4064 Left_Opnd => New_Occurrence_Of (Lnn, Loc),
4069 Make_Attribute_Reference (Loc,
4070 Attribute_Name => Name_First,
4072 New_Occurrence_Of (TB, Loc))),
4076 Make_Attribute_Reference (Loc,
4077 Attribute_Name => Name_Last,
4079 New_Occurrence_Of (TB, Loc))))),
4081 Right_Opnd => Nin))))));
4083 -- Now we can do the rewrite
4086 Make_Expression_With_Actions (Loc,
4087 Actions => New_List (
4088 Make_Object_Declaration (Loc,
4089 Defining_Identifier => Bnn,
4090 Object_Definition =>
4091 New_Occurrence_Of (Result_Type, Loc)),
4093 Expression => New_Occurrence_Of (Bnn, Loc)));
4094 Analyze_And_Resolve (N, Result_Type);
4098 -- Not bignum case, but types don't match (this means we rewrote the
4099 -- left operand to be Long_Long_Integer).
4102 pragma Assert (Base_Type (Etype (Lop)) = LLIB);
4104 -- We rewrite the membership test as (where T is the type with
4105 -- the predicate, i.e. the type of the right operand)
4107 -- Lop in LLIB (T'Base'First) .. LLIB (T'Base'Last)
4108 -- and then T'Base (Lop) in T
4111 T : constant Entity_Id := Etype (Rop);
4112 TB : constant Entity_Id := Base_Type (T);
4116 -- The last membership test is marked to prevent recursion
4120 Left_Opnd => Convert_To (TB, Duplicate_Subexpr (Lop)),
4121 Right_Opnd => New_Occurrence_Of (T, Loc));
4122 Set_No_Minimize_Eliminate (Nin);
4124 -- Now do the rewrite
4135 Make_Attribute_Reference (Loc,
4136 Attribute_Name => Name_First,
4137 Prefix => New_Occurrence_Of (TB, Loc))),
4140 Make_Attribute_Reference (Loc,
4141 Attribute_Name => Name_Last,
4142 Prefix => New_Occurrence_Of (TB, Loc))))),
4143 Right_Opnd => Nin));
4144 Set_Analyzed (N, False);
4145 Analyze_And_Resolve (N, Restype);
4149 end Expand_Membership_Minimize_Eliminate_Overflow;
4151 ------------------------
4152 -- Expand_N_Allocator --
4153 ------------------------
4155 procedure Expand_N_Allocator (N : Node_Id) is
4156 Etyp : constant Entity_Id := Etype (Expression (N));
4157 Loc : constant Source_Ptr := Sloc (N);
4158 PtrT : constant Entity_Id := Etype (N);
4160 procedure Rewrite_Coextension (N : Node_Id);
4161 -- Static coextensions have the same lifetime as the entity they
4162 -- constrain. Such occurrences can be rewritten as aliased objects
4163 -- and their unrestricted access used instead of the coextension.
4165 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id;
4166 -- Given a constrained array type E, returns a node representing the
4167 -- code to compute the size in storage elements for the given type.
4168 -- This is done without using the attribute (which malfunctions for
4171 -------------------------
4172 -- Rewrite_Coextension --
4173 -------------------------
4175 procedure Rewrite_Coextension (N : Node_Id) is
4176 Temp_Id : constant Node_Id := Make_Temporary (Loc, 'C');
4177 Temp_Decl : Node_Id;
4181 -- Cnn : aliased Etyp;
4184 Make_Object_Declaration (Loc,
4185 Defining_Identifier => Temp_Id,
4186 Aliased_Present => True,
4187 Object_Definition => New_Occurrence_Of (Etyp, Loc));
4189 if Nkind (Expression (N)) = N_Qualified_Expression then
4190 Set_Expression (Temp_Decl, Expression (Expression (N)));
4193 Insert_Action (N, Temp_Decl);
4195 Make_Attribute_Reference (Loc,
4196 Prefix => New_Occurrence_Of (Temp_Id, Loc),
4197 Attribute_Name => Name_Unrestricted_Access));
4199 Analyze_And_Resolve (N, PtrT);
4200 end Rewrite_Coextension;
4202 ------------------------------
4203 -- Size_In_Storage_Elements --
4204 ------------------------------
4206 function Size_In_Storage_Elements (E : Entity_Id) return Node_Id is
4208 -- Logically this just returns E'Max_Size_In_Storage_Elements.
4209 -- However, the reason for the existence of this function is
4210 -- to construct a test for sizes too large, which means near the
4211 -- 32-bit limit on a 32-bit machine, and precisely the trouble
4212 -- is that we get overflows when sizes are greater than 2**31.
4214 -- So what we end up doing for array types is to use the expression:
4216 -- number-of-elements * component_type'Max_Size_In_Storage_Elements
4218 -- which avoids this problem. All this is a bit bogus, but it does
4219 -- mean we catch common cases of trying to allocate arrays that
4220 -- are too large, and which in the absence of a check results in
4221 -- undetected chaos ???
4223 -- Note in particular that this is a pessimistic estimate in the
4224 -- case of packed array types, where an array element might occupy
4225 -- just a fraction of a storage element???
4232 for J in 1 .. Number_Dimensions (E) loop
4234 Make_Attribute_Reference (Loc,
4235 Prefix => New_Occurrence_Of (E, Loc),
4236 Attribute_Name => Name_Length,
4237 Expressions => New_List (Make_Integer_Literal (Loc, J)));
4244 Make_Op_Multiply (Loc,
4251 Make_Op_Multiply (Loc,
4254 Make_Attribute_Reference (Loc,
4255 Prefix => New_Occurrence_Of (Component_Type (E), Loc),
4256 Attribute_Name => Name_Max_Size_In_Storage_Elements));
4258 end Size_In_Storage_Elements;
4262 Dtyp : constant Entity_Id := Available_View (Designated_Type (PtrT));
4266 Rel_Typ : Entity_Id;
4269 -- Start of processing for Expand_N_Allocator
4272 -- RM E.2.3(22). We enforce that the expected type of an allocator
4273 -- shall not be a remote access-to-class-wide-limited-private type
4275 -- Why is this being done at expansion time, seems clearly wrong ???
4277 Validate_Remote_Access_To_Class_Wide_Type (N);
4279 -- Processing for anonymous access-to-controlled types. These access
4280 -- types receive a special finalization master which appears in the
4281 -- declarations of the enclosing semantic unit. This expansion is done
4282 -- now to ensure that any additional types generated by this routine or
4283 -- Expand_Allocator_Expression inherit the proper type attributes.
4285 if (Ekind (PtrT) = E_Anonymous_Access_Type
4286 or else (Is_Itype (PtrT) and then No (Finalization_Master (PtrT))))
4287 and then Needs_Finalization (Dtyp)
4289 -- Detect the allocation of an anonymous controlled object where the
4290 -- type of the context is named. For example:
4292 -- procedure Proc (Ptr : Named_Access_Typ);
4293 -- Proc (new Designated_Typ);
4295 -- Regardless of the anonymous-to-named access type conversion, the
4296 -- lifetime of the object must be associated with the named access
4297 -- type. Use the finalization-related attributes of this type.
4299 if Nkind_In (Parent (N), N_Type_Conversion,
4300 N_Unchecked_Type_Conversion)
4301 and then Ekind_In (Etype (Parent (N)), E_Access_Subtype,
4303 E_General_Access_Type)
4305 Rel_Typ := Etype (Parent (N));
4310 -- Anonymous access-to-controlled types allocate on the global pool.
4311 -- Do not set this attribute on .NET/JVM since those targets do not
4314 if No (Associated_Storage_Pool (PtrT)) and then VM_Target = No_VM then
4315 if Present (Rel_Typ) then
4316 Set_Associated_Storage_Pool (PtrT,
4317 Associated_Storage_Pool (Rel_Typ));
4319 Set_Associated_Storage_Pool (PtrT,
4320 Get_Global_Pool_For_Access_Type (PtrT));
4324 -- The finalization master must be inserted and analyzed as part of
4325 -- the current semantic unit. Note that the master is updated when
4326 -- analysis changes current units.
4328 if Present (Rel_Typ) then
4329 Set_Finalization_Master (PtrT, Finalization_Master (Rel_Typ));
4331 Set_Finalization_Master (PtrT, Current_Anonymous_Master);
4335 -- Set the storage pool and find the appropriate version of Allocate to
4336 -- call. Do not overwrite the storage pool if it is already set, which
4337 -- can happen for build-in-place function returns (see
4338 -- Exp_Ch4.Expand_N_Extended_Return_Statement).
4340 if No (Storage_Pool (N)) then
4341 Pool := Associated_Storage_Pool (Root_Type (PtrT));
4343 if Present (Pool) then
4344 Set_Storage_Pool (N, Pool);
4346 if Is_RTE (Pool, RE_SS_Pool) then
4347 if VM_Target = No_VM then
4348 Set_Procedure_To_Call (N, RTE (RE_SS_Allocate));
4351 -- In the case of an allocator for a simple storage pool, locate
4352 -- and save a reference to the pool type's Allocate routine.
4354 elsif Present (Get_Rep_Pragma
4355 (Etype (Pool), Name_Simple_Storage_Pool_Type))
4358 Pool_Type : constant Entity_Id := Base_Type (Etype (Pool));
4359 Alloc_Op : Entity_Id;
4361 Alloc_Op := Get_Name_Entity_Id (Name_Allocate);
4362 while Present (Alloc_Op) loop
4363 if Scope (Alloc_Op) = Scope (Pool_Type)
4364 and then Present (First_Formal (Alloc_Op))
4365 and then Etype (First_Formal (Alloc_Op)) = Pool_Type
4367 Set_Procedure_To_Call (N, Alloc_Op);
4370 Alloc_Op := Homonym (Alloc_Op);
4375 elsif Is_Class_Wide_Type (Etype (Pool)) then
4376 Set_Procedure_To_Call (N, RTE (RE_Allocate_Any));
4379 Set_Procedure_To_Call (N,
4380 Find_Prim_Op (Etype (Pool), Name_Allocate));
4385 -- Under certain circumstances we can replace an allocator by an access
4386 -- to statically allocated storage. The conditions, as noted in AARM
4387 -- 3.10 (10c) are as follows:
4389 -- Size and initial value is known at compile time
4390 -- Access type is access-to-constant
4392 -- The allocator is not part of a constraint on a record component,
4393 -- because in that case the inserted actions are delayed until the
4394 -- record declaration is fully analyzed, which is too late for the
4395 -- analysis of the rewritten allocator.
4397 if Is_Access_Constant (PtrT)
4398 and then Nkind (Expression (N)) = N_Qualified_Expression
4399 and then Compile_Time_Known_Value (Expression (Expression (N)))
4400 and then Size_Known_At_Compile_Time
4401 (Etype (Expression (Expression (N))))
4402 and then not Is_Record_Type (Current_Scope)
4404 -- Here we can do the optimization. For the allocator
4408 -- We insert an object declaration
4410 -- Tnn : aliased x := y;
4412 -- and replace the allocator by Tnn'Unrestricted_Access. Tnn is
4413 -- marked as requiring static allocation.
4415 Temp := Make_Temporary (Loc, 'T', Expression (Expression (N)));
4416 Desig := Subtype_Mark (Expression (N));
4418 -- If context is constrained, use constrained subtype directly,
4419 -- so that the constant is not labelled as having a nominally
4420 -- unconstrained subtype.
4422 if Entity (Desig) = Base_Type (Dtyp) then
4423 Desig := New_Occurrence_Of (Dtyp, Loc);
4427 Make_Object_Declaration (Loc,
4428 Defining_Identifier => Temp,
4429 Aliased_Present => True,
4430 Constant_Present => Is_Access_Constant (PtrT),
4431 Object_Definition => Desig,
4432 Expression => Expression (Expression (N))));
4435 Make_Attribute_Reference (Loc,
4436 Prefix => New_Occurrence_Of (Temp, Loc),
4437 Attribute_Name => Name_Unrestricted_Access));
4439 Analyze_And_Resolve (N, PtrT);
4441 -- We set the variable as statically allocated, since we don't want
4442 -- it going on the stack of the current procedure.
4444 Set_Is_Statically_Allocated (Temp);
4448 -- Same if the allocator is an access discriminant for a local object:
4449 -- instead of an allocator we create a local value and constrain the
4450 -- enclosing object with the corresponding access attribute.
4452 if Is_Static_Coextension (N) then
4453 Rewrite_Coextension (N);
4457 -- Check for size too large, we do this because the back end misses
4458 -- proper checks here and can generate rubbish allocation calls when
4459 -- we are near the limit. We only do this for the 32-bit address case
4460 -- since that is from a practical point of view where we see a problem.
4462 if System_Address_Size = 32
4463 and then not Storage_Checks_Suppressed (PtrT)
4464 and then not Storage_Checks_Suppressed (Dtyp)
4465 and then not Storage_Checks_Suppressed (Etyp)
4467 -- The check we want to generate should look like
4469 -- if Etyp'Max_Size_In_Storage_Elements > 3.5 gigabytes then
4470 -- raise Storage_Error;
4473 -- where 3.5 gigabytes is a constant large enough to accommodate any
4474 -- reasonable request for. But we can't do it this way because at
4475 -- least at the moment we don't compute this attribute right, and
4476 -- can silently give wrong results when the result gets large. Since
4477 -- this is all about large results, that's bad, so instead we only
4478 -- apply the check for constrained arrays, and manually compute the
4479 -- value of the attribute ???
4481 if Is_Array_Type (Etyp) and then Is_Constrained (Etyp) then
4483 Make_Raise_Storage_Error (Loc,
4486 Left_Opnd => Size_In_Storage_Elements (Etyp),
4488 Make_Integer_Literal (Loc, Uint_7 * (Uint_2 ** 29))),
4489 Reason => SE_Object_Too_Large));
4493 -- Handle case of qualified expression (other than optimization above)
4494 -- First apply constraint checks, because the bounds or discriminants
4495 -- in the aggregate might not match the subtype mark in the allocator.
4497 if Nkind (Expression (N)) = N_Qualified_Expression then
4498 Apply_Constraint_Check
4499 (Expression (Expression (N)), Etype (Expression (N)));
4501 Expand_Allocator_Expression (N);
4505 -- If the allocator is for a type which requires initialization, and
4506 -- there is no initial value (i.e. operand is a subtype indication
4507 -- rather than a qualified expression), then we must generate a call to
4508 -- the initialization routine using an expressions action node:
4510 -- [Pnnn : constant ptr_T := new (T); Init (Pnnn.all,...); Pnnn]
4512 -- Here ptr_T is the pointer type for the allocator, and T is the
4513 -- subtype of the allocator. A special case arises if the designated
4514 -- type of the access type is a task or contains tasks. In this case
4515 -- the call to Init (Temp.all ...) is replaced by code that ensures
4516 -- that tasks get activated (see Exp_Ch9.Build_Task_Allocate_Block
4517 -- for details). In addition, if the type T is a task T, then the
4518 -- first argument to Init must be converted to the task record type.
4521 T : constant Entity_Id := Entity (Expression (N));
4527 Init_Arg1 : Node_Id;
4528 Temp_Decl : Node_Id;
4529 Temp_Type : Entity_Id;
4532 if No_Initialization (N) then
4534 -- Even though this might be a simple allocation, create a custom
4535 -- Allocate if the context requires it. Since .NET/JVM compilers
4536 -- do not support pools, this step is skipped.
4538 if VM_Target = No_VM
4539 and then Present (Finalization_Master (PtrT))
4541 Build_Allocate_Deallocate_Proc
4543 Is_Allocate => True);
4546 -- Case of no initialization procedure present
4548 elsif not Has_Non_Null_Base_Init_Proc (T) then
4550 -- Case of simple initialization required
4552 if Needs_Simple_Initialization (T) then
4553 Check_Restriction (No_Default_Initialization, N);
4554 Rewrite (Expression (N),
4555 Make_Qualified_Expression (Loc,
4556 Subtype_Mark => New_Occurrence_Of (T, Loc),
4557 Expression => Get_Simple_Init_Val (T, N)));
4559 Analyze_And_Resolve (Expression (Expression (N)), T);
4560 Analyze_And_Resolve (Expression (N), T);
4561 Set_Paren_Count (Expression (Expression (N)), 1);
4562 Expand_N_Allocator (N);
4564 -- No initialization required
4570 -- Case of initialization procedure present, must be called
4573 Check_Restriction (No_Default_Initialization, N);
4575 if not Restriction_Active (No_Default_Initialization) then
4576 Init := Base_Init_Proc (T);
4578 Temp := Make_Temporary (Loc, 'P');
4580 -- Construct argument list for the initialization routine call
4583 Make_Explicit_Dereference (Loc,
4585 New_Occurrence_Of (Temp, Loc));
4587 Set_Assignment_OK (Init_Arg1);
4590 -- The initialization procedure expects a specific type. if the
4591 -- context is access to class wide, indicate that the object
4592 -- being allocated has the right specific type.
4594 if Is_Class_Wide_Type (Dtyp) then
4595 Init_Arg1 := Unchecked_Convert_To (T, Init_Arg1);
4598 -- If designated type is a concurrent type or if it is private
4599 -- type whose definition is a concurrent type, the first
4600 -- argument in the Init routine has to be unchecked conversion
4601 -- to the corresponding record type. If the designated type is
4602 -- a derived type, also convert the argument to its root type.
4604 if Is_Concurrent_Type (T) then
4606 Unchecked_Convert_To (
4607 Corresponding_Record_Type (T), Init_Arg1);
4609 elsif Is_Private_Type (T)
4610 and then Present (Full_View (T))
4611 and then Is_Concurrent_Type (Full_View (T))
4614 Unchecked_Convert_To
4615 (Corresponding_Record_Type (Full_View (T)), Init_Arg1);
4617 elsif Etype (First_Formal (Init)) /= Base_Type (T) then
4619 Ftyp : constant Entity_Id := Etype (First_Formal (Init));
4622 Init_Arg1 := OK_Convert_To (Etype (Ftyp), Init_Arg1);
4623 Set_Etype (Init_Arg1, Ftyp);
4627 Args := New_List (Init_Arg1);
4629 -- For the task case, pass the Master_Id of the access type as
4630 -- the value of the _Master parameter, and _Chain as the value
4631 -- of the _Chain parameter (_Chain will be defined as part of
4632 -- the generated code for the allocator).
4634 -- In Ada 2005, the context may be a function that returns an
4635 -- anonymous access type. In that case the Master_Id has been
4636 -- created when expanding the function declaration.
4638 if Has_Task (T) then
4639 if No (Master_Id (Base_Type (PtrT))) then
4641 -- The designated type was an incomplete type, and the
4642 -- access type did not get expanded. Salvage it now.
4644 if not Restriction_Active (No_Task_Hierarchy) then
4645 if Present (Parent (Base_Type (PtrT))) then
4646 Expand_N_Full_Type_Declaration
4647 (Parent (Base_Type (PtrT)));
4649 -- The only other possibility is an itype. For this
4650 -- case, the master must exist in the context. This is
4651 -- the case when the allocator initializes an access
4652 -- component in an init-proc.
4655 pragma Assert (Is_Itype (PtrT));
4656 Build_Master_Renaming (PtrT, N);
4661 -- If the context of the allocator is a declaration or an
4662 -- assignment, we can generate a meaningful image for it,
4663 -- even though subsequent assignments might remove the
4664 -- connection between task and entity. We build this image
4665 -- when the left-hand side is a simple variable, a simple
4666 -- indexed assignment or a simple selected component.
4668 if Nkind (Parent (N)) = N_Assignment_Statement then
4670 Nam : constant Node_Id := Name (Parent (N));
4673 if Is_Entity_Name (Nam) then
4675 Build_Task_Image_Decls
4678 (Entity (Nam), Sloc (Nam)), T);
4680 elsif Nkind_In (Nam, N_Indexed_Component,
4681 N_Selected_Component)
4682 and then Is_Entity_Name (Prefix (Nam))
4685 Build_Task_Image_Decls
4686 (Loc, Nam, Etype (Prefix (Nam)));
4688 Decls := Build_Task_Image_Decls (Loc, T, T);
4692 elsif Nkind (Parent (N)) = N_Object_Declaration then
4694 Build_Task_Image_Decls
4695 (Loc, Defining_Identifier (Parent (N)), T);
4698 Decls := Build_Task_Image_Decls (Loc, T, T);
4701 if Restriction_Active (No_Task_Hierarchy) then
4703 New_Occurrence_Of (RTE (RE_Library_Task_Level), Loc));
4707 (Master_Id (Base_Type (Root_Type (PtrT))), Loc));
4710 Append_To (Args, Make_Identifier (Loc, Name_uChain));
4712 Decl := Last (Decls);
4714 New_Occurrence_Of (Defining_Identifier (Decl), Loc));
4716 -- Has_Task is false, Decls not used
4722 -- Add discriminants if discriminated type
4725 Dis : Boolean := False;
4729 if Has_Discriminants (T) then
4733 elsif Is_Private_Type (T)
4734 and then Present (Full_View (T))
4735 and then Has_Discriminants (Full_View (T))
4738 Typ := Full_View (T);
4743 -- If the allocated object will be constrained by the
4744 -- default values for discriminants, then build a subtype
4745 -- with those defaults, and change the allocated subtype
4746 -- to that. Note that this happens in fewer cases in Ada
4749 if not Is_Constrained (Typ)
4750 and then Present (Discriminant_Default_Value
4751 (First_Discriminant (Typ)))
4752 and then (Ada_Version < Ada_2005
4754 Object_Type_Has_Constrained_Partial_View
4755 (Typ, Current_Scope))
4757 Typ := Build_Default_Subtype (Typ, N);
4758 Set_Expression (N, New_Occurrence_Of (Typ, Loc));
4761 Discr := First_Elmt (Discriminant_Constraint (Typ));
4762 while Present (Discr) loop
4763 Nod := Node (Discr);
4764 Append (New_Copy_Tree (Node (Discr)), Args);
4766 -- AI-416: when the discriminant constraint is an
4767 -- anonymous access type make sure an accessibility
4768 -- check is inserted if necessary (3.10.2(22.q/2))
4770 if Ada_Version >= Ada_2005
4772 Ekind (Etype (Nod)) = E_Anonymous_Access_Type
4774 Apply_Accessibility_Check
4775 (Nod, Typ, Insert_Node => Nod);
4783 -- We set the allocator as analyzed so that when we analyze
4784 -- the if expression node, we do not get an unwanted recursive
4785 -- expansion of the allocator expression.
4787 Set_Analyzed (N, True);
4788 Nod := Relocate_Node (N);
4790 -- Here is the transformation:
4791 -- input: new Ctrl_Typ
4792 -- output: Temp : constant Ctrl_Typ_Ptr := new Ctrl_Typ;
4793 -- Ctrl_TypIP (Temp.all, ...);
4794 -- [Deep_]Initialize (Temp.all);
4796 -- Here Ctrl_Typ_Ptr is the pointer type for the allocator, and
4797 -- is the subtype of the allocator.
4800 Make_Object_Declaration (Loc,
4801 Defining_Identifier => Temp,
4802 Constant_Present => True,
4803 Object_Definition => New_Occurrence_Of (Temp_Type, Loc),
4806 Set_Assignment_OK (Temp_Decl);
4807 Insert_Action (N, Temp_Decl, Suppress => All_Checks);
4809 Build_Allocate_Deallocate_Proc (Temp_Decl, True);
4811 -- If the designated type is a task type or contains tasks,
4812 -- create block to activate created tasks, and insert
4813 -- declaration for Task_Image variable ahead of call.
4815 if Has_Task (T) then
4817 L : constant List_Id := New_List;
4820 Build_Task_Allocate_Block (L, Nod, Args);
4822 Insert_List_Before (First (Declarations (Blk)), Decls);
4823 Insert_Actions (N, L);
4828 Make_Procedure_Call_Statement (Loc,
4829 Name => New_Occurrence_Of (Init, Loc),
4830 Parameter_Associations => Args));
4833 if Needs_Finalization (T) then
4836 -- [Deep_]Initialize (Init_Arg1);
4840 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4843 if Present (Finalization_Master (PtrT)) then
4845 -- Special processing for .NET/JVM, the allocated object
4846 -- is attached to the finalization master. Generate:
4848 -- Attach (<PtrT>FM, Root_Controlled_Ptr (Init_Arg1));
4850 -- Types derived from [Limited_]Controlled are the only
4851 -- ones considered since they have fields Prev and Next.
4853 if VM_Target /= No_VM then
4854 if Is_Controlled (T) then
4857 (Obj_Ref => New_Copy_Tree (Init_Arg1),
4861 -- Default case, generate:
4863 -- Set_Finalize_Address
4864 -- (<PtrT>FM, <T>FD'Unrestricted_Access);
4866 -- Do not generate this call in CodePeer mode, as TSS
4867 -- primitive Finalize_Address is not created in this
4870 elsif not CodePeer_Mode then
4872 Make_Set_Finalize_Address_Call
4880 Rewrite (N, New_Occurrence_Of (Temp, Loc));
4881 Analyze_And_Resolve (N, PtrT);
4886 -- Ada 2005 (AI-251): If the allocator is for a class-wide interface
4887 -- object that has been rewritten as a reference, we displace "this"
4888 -- to reference properly its secondary dispatch table.
4890 if Nkind (N) = N_Identifier and then Is_Interface (Dtyp) then
4891 Displace_Allocator_Pointer (N);
4895 when RE_Not_Available =>
4897 end Expand_N_Allocator;
4899 -----------------------
4900 -- Expand_N_And_Then --
4901 -----------------------
4903 procedure Expand_N_And_Then (N : Node_Id)
4904 renames Expand_Short_Circuit_Operator;
4906 ------------------------------
4907 -- Expand_N_Case_Expression --
4908 ------------------------------
4910 procedure Expand_N_Case_Expression (N : Node_Id) is
4911 Loc : constant Source_Ptr := Sloc (N);
4912 Typ : constant Entity_Id := Etype (N);
4923 -- Check for MINIMIZED/ELIMINATED overflow mode
4925 if Minimized_Eliminated_Overflow_Check (N) then
4926 Apply_Arithmetic_Overflow_Check (N);
4930 -- If the case expression is a predicate specification, do not
4931 -- expand, because it will be converted to the proper predicate
4932 -- form when building the predicate function.
4934 if Ekind_In (Current_Scope, E_Function, E_Procedure)
4935 and then Is_Predicate_Function (Current_Scope)
4942 -- case X is when A => AX, when B => BX ...
4957 -- However, this expansion is wrong for limited types, and also
4958 -- wrong for unconstrained types (since the bounds may not be the
4959 -- same in all branches). Furthermore it involves an extra copy
4960 -- for large objects. So we take care of this by using the following
4961 -- modified expansion for non-elementary types:
4964 -- type Pnn is access all typ;
4968 -- T := AX'Unrestricted_Access;
4970 -- T := BX'Unrestricted_Access;
4976 Make_Case_Statement (Loc,
4977 Expression => Expression (N),
4978 Alternatives => New_List);
4980 Actions := New_List;
4984 if Is_Elementary_Type (Typ) then
4988 Pnn := Make_Temporary (Loc, 'P');
4990 Make_Full_Type_Declaration (Loc,
4991 Defining_Identifier => Pnn,
4993 Make_Access_To_Object_Definition (Loc,
4994 All_Present => True,
4995 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
4999 Tnn := Make_Temporary (Loc, 'T');
5001 -- Create declaration for target of expression, and indicate that it
5002 -- does not require initialization.
5005 Make_Object_Declaration (Loc,
5006 Defining_Identifier => Tnn,
5007 Object_Definition => New_Occurrence_Of (Ttyp, Loc));
5008 Set_No_Initialization (Decl);
5009 Append_To (Actions, Decl);
5011 -- Now process the alternatives
5013 Alt := First (Alternatives (N));
5014 while Present (Alt) loop
5016 Aexp : Node_Id := Expression (Alt);
5017 Aloc : constant Source_Ptr := Sloc (Aexp);
5021 -- As described above, take Unrestricted_Access for case of non-
5022 -- scalar types, to avoid big copies, and special cases.
5024 if not Is_Elementary_Type (Typ) then
5026 Make_Attribute_Reference (Aloc,
5027 Prefix => Relocate_Node (Aexp),
5028 Attribute_Name => Name_Unrestricted_Access);
5032 Make_Assignment_Statement (Aloc,
5033 Name => New_Occurrence_Of (Tnn, Loc),
5034 Expression => Aexp));
5036 -- Propagate declarations inserted in the node by Insert_Actions
5037 -- (for example, temporaries generated to remove side effects).
5038 -- These actions must remain attached to the alternative, given
5039 -- that they are generated by the corresponding expression.
5041 if Present (Sinfo.Actions (Alt)) then
5042 Prepend_List (Sinfo.Actions (Alt), Stats);
5046 (Alternatives (Cstmt),
5047 Make_Case_Statement_Alternative (Sloc (Alt),
5048 Discrete_Choices => Discrete_Choices (Alt),
5049 Statements => Stats));
5055 Append_To (Actions, Cstmt);
5057 -- Construct and return final expression with actions
5059 if Is_Elementary_Type (Typ) then
5060 Fexp := New_Occurrence_Of (Tnn, Loc);
5063 Make_Explicit_Dereference (Loc,
5064 Prefix => New_Occurrence_Of (Tnn, Loc));
5068 Make_Expression_With_Actions (Loc,
5070 Actions => Actions));
5072 Analyze_And_Resolve (N, Typ);
5073 end Expand_N_Case_Expression;
5075 -----------------------------------
5076 -- Expand_N_Explicit_Dereference --
5077 -----------------------------------
5079 procedure Expand_N_Explicit_Dereference (N : Node_Id) is
5081 -- Insert explicit dereference call for the checked storage pool case
5083 Insert_Dereference_Action (Prefix (N));
5085 -- If the type is an Atomic type for which Atomic_Sync is enabled, then
5086 -- we set the atomic sync flag.
5088 if Is_Atomic (Etype (N))
5089 and then not Atomic_Synchronization_Disabled (Etype (N))
5091 Activate_Atomic_Synchronization (N);
5093 end Expand_N_Explicit_Dereference;
5095 --------------------------------------
5096 -- Expand_N_Expression_With_Actions --
5097 --------------------------------------
5099 procedure Expand_N_Expression_With_Actions (N : Node_Id) is
5101 function Process_Action (Act : Node_Id) return Traverse_Result;
5102 -- Inspect and process a single action of an expression_with_actions for
5103 -- transient controlled objects. If such objects are found, the routine
5104 -- generates code to clean them up when the context of the expression is
5105 -- evaluated or elaborated.
5107 --------------------
5108 -- Process_Action --
5109 --------------------
5111 function Process_Action (Act : Node_Id) return Traverse_Result is
5113 if Nkind (Act) = N_Object_Declaration
5114 and then Is_Finalizable_Transient (Act, N)
5116 Process_Transient_Object (Act, N);
5119 -- Avoid processing temporary function results multiple times when
5120 -- dealing with nested expression_with_actions.
5122 elsif Nkind (Act) = N_Expression_With_Actions then
5125 -- Do not process temporary function results in loops. This is done
5126 -- by Expand_N_Loop_Statement and Build_Finalizer.
5128 elsif Nkind (Act) = N_Loop_Statement then
5135 procedure Process_Single_Action is new Traverse_Proc (Process_Action);
5141 -- Start of processing for Expand_N_Expression_With_Actions
5144 -- Process the actions as described above
5146 Act := First (Actions (N));
5147 while Present (Act) loop
5148 Process_Single_Action (Act);
5152 -- Deal with case where there are no actions. In this case we simply
5153 -- rewrite the node with its expression since we don't need the actions
5154 -- and the specification of this node does not allow a null action list.
5156 -- Note: we use Rewrite instead of Replace, because Codepeer is using
5157 -- the expanded tree and relying on being able to retrieve the original
5158 -- tree in cases like this. This raises a whole lot of issues of whether
5159 -- we have problems elsewhere, which will be addressed in the future???
5161 if Is_Empty_List (Actions (N)) then
5162 Rewrite (N, Relocate_Node (Expression (N)));
5164 end Expand_N_Expression_With_Actions;
5166 ----------------------------
5167 -- Expand_N_If_Expression --
5168 ----------------------------
5170 -- Deal with limited types and condition actions
5172 procedure Expand_N_If_Expression (N : Node_Id) is
5173 procedure Process_Actions (Actions : List_Id);
5174 -- Inspect and process a single action list of an if expression for
5175 -- transient controlled objects. If such objects are found, the routine
5176 -- generates code to clean them up when the context of the expression is
5177 -- evaluated or elaborated.
5179 ---------------------
5180 -- Process_Actions --
5181 ---------------------
5183 procedure Process_Actions (Actions : List_Id) is
5187 Act := First (Actions);
5188 while Present (Act) loop
5189 if Nkind (Act) = N_Object_Declaration
5190 and then Is_Finalizable_Transient (Act, N)
5192 Process_Transient_Object (Act, N);
5197 end Process_Actions;
5201 Loc : constant Source_Ptr := Sloc (N);
5202 Cond : constant Node_Id := First (Expressions (N));
5203 Thenx : constant Node_Id := Next (Cond);
5204 Elsex : constant Node_Id := Next (Thenx);
5205 Typ : constant Entity_Id := Etype (N);
5213 Ptr_Typ : Entity_Id;
5215 -- Start of processing for Expand_N_If_Expression
5218 -- Check for MINIMIZED/ELIMINATED overflow mode
5220 if Minimized_Eliminated_Overflow_Check (N) then
5221 Apply_Arithmetic_Overflow_Check (N);
5225 -- Fold at compile time if condition known. We have already folded
5226 -- static if expressions, but it is possible to fold any case in which
5227 -- the condition is known at compile time, even though the result is
5230 -- Note that we don't do the fold of such cases in Sem_Elab because
5231 -- it can cause infinite loops with the expander adding a conditional
5232 -- expression, and Sem_Elab circuitry removing it repeatedly.
5234 if Compile_Time_Known_Value (Cond) then
5235 if Is_True (Expr_Value (Cond)) then
5237 Actions := Then_Actions (N);
5240 Actions := Else_Actions (N);
5245 if Present (Actions) then
5247 Make_Expression_With_Actions (Loc,
5248 Expression => Relocate_Node (Expr),
5249 Actions => Actions));
5250 Analyze_And_Resolve (N, Typ);
5252 Rewrite (N, Relocate_Node (Expr));
5255 -- Note that the result is never static (legitimate cases of static
5256 -- if expressions were folded in Sem_Eval).
5258 Set_Is_Static_Expression (N, False);
5262 -- If the type is limited or unconstrained, we expand as follows to
5263 -- avoid any possibility of improper copies.
5265 -- Note: it may be possible to avoid this special processing if the
5266 -- back end uses its own mechanisms for handling by-reference types ???
5268 -- type Ptr is access all Typ;
5272 -- Cnn := then-expr'Unrestricted_Access;
5275 -- Cnn := else-expr'Unrestricted_Access;
5278 -- and replace the if expression by a reference to Cnn.all.
5280 -- This special case can be skipped if the back end handles limited
5281 -- types properly and ensures that no incorrect copies are made.
5283 if Is_By_Reference_Type (Typ)
5284 and then not Back_End_Handles_Limited_Types
5286 -- When the "then" or "else" expressions involve controlled function
5287 -- calls, generated temporaries are chained on the corresponding list
5288 -- of actions. These temporaries need to be finalized after the if
5289 -- expression is evaluated.
5291 Process_Actions (Then_Actions (N));
5292 Process_Actions (Else_Actions (N));
5295 -- type Ann is access all Typ;
5297 Ptr_Typ := Make_Temporary (Loc, 'A');
5300 Make_Full_Type_Declaration (Loc,
5301 Defining_Identifier => Ptr_Typ,
5303 Make_Access_To_Object_Definition (Loc,
5304 All_Present => True,
5305 Subtype_Indication => New_Occurrence_Of (Typ, Loc))));
5310 Cnn := Make_Temporary (Loc, 'C', N);
5313 Make_Object_Declaration (Loc,
5314 Defining_Identifier => Cnn,
5315 Object_Definition => New_Occurrence_Of (Ptr_Typ, Loc));
5319 -- Cnn := <Thenx>'Unrestricted_Access;
5321 -- Cnn := <Elsex>'Unrestricted_Access;
5325 Make_Implicit_If_Statement (N,
5326 Condition => Relocate_Node (Cond),
5327 Then_Statements => New_List (
5328 Make_Assignment_Statement (Sloc (Thenx),
5329 Name => New_Occurrence_Of (Cnn, Sloc (Thenx)),
5331 Make_Attribute_Reference (Loc,
5332 Prefix => Relocate_Node (Thenx),
5333 Attribute_Name => Name_Unrestricted_Access))),
5335 Else_Statements => New_List (
5336 Make_Assignment_Statement (Sloc (Elsex),
5337 Name => New_Occurrence_Of (Cnn, Sloc (Elsex)),
5339 Make_Attribute_Reference (Loc,
5340 Prefix => Relocate_Node (Elsex),
5341 Attribute_Name => Name_Unrestricted_Access))));
5344 Make_Explicit_Dereference (Loc,
5345 Prefix => New_Occurrence_Of (Cnn, Loc));
5347 -- For other types, we only need to expand if there are other actions
5348 -- associated with either branch.
5350 elsif Present (Then_Actions (N)) or else Present (Else_Actions (N)) then
5352 -- We now wrap the actions into the appropriate expression
5354 if Present (Then_Actions (N)) then
5356 Make_Expression_With_Actions (Sloc (Thenx),
5357 Actions => Then_Actions (N),
5358 Expression => Relocate_Node (Thenx)));
5360 Set_Then_Actions (N, No_List);
5361 Analyze_And_Resolve (Thenx, Typ);
5364 if Present (Else_Actions (N)) then
5366 Make_Expression_With_Actions (Sloc (Elsex),
5367 Actions => Else_Actions (N),
5368 Expression => Relocate_Node (Elsex)));
5370 Set_Else_Actions (N, No_List);
5371 Analyze_And_Resolve (Elsex, Typ);
5376 -- If no actions then no expansion needed, gigi will handle it using the
5377 -- same approach as a C conditional expression.
5383 -- Fall through here for either the limited expansion, or the case of
5384 -- inserting actions for non-limited types. In both these cases, we must
5385 -- move the SLOC of the parent If statement to the newly created one and
5386 -- change it to the SLOC of the expression which, after expansion, will
5387 -- correspond to what is being evaluated.
5389 if Present (Parent (N)) and then Nkind (Parent (N)) = N_If_Statement then
5390 Set_Sloc (New_If, Sloc (Parent (N)));
5391 Set_Sloc (Parent (N), Loc);
5394 -- Make sure Then_Actions and Else_Actions are appropriately moved
5395 -- to the new if statement.
5397 if Present (Then_Actions (N)) then
5399 (First (Then_Statements (New_If)), Then_Actions (N));
5402 if Present (Else_Actions (N)) then
5404 (First (Else_Statements (New_If)), Else_Actions (N));
5407 Insert_Action (N, Decl);
5408 Insert_Action (N, New_If);
5410 Analyze_And_Resolve (N, Typ);
5411 end Expand_N_If_Expression;
5417 procedure Expand_N_In (N : Node_Id) is
5418 Loc : constant Source_Ptr := Sloc (N);
5419 Restyp : constant Entity_Id := Etype (N);
5420 Lop : constant Node_Id := Left_Opnd (N);
5421 Rop : constant Node_Id := Right_Opnd (N);
5422 Static : constant Boolean := Is_OK_Static_Expression (N);
5427 procedure Substitute_Valid_Check;
5428 -- Replaces node N by Lop'Valid. This is done when we have an explicit
5429 -- test for the left operand being in range of its subtype.
5431 ----------------------------
5432 -- Substitute_Valid_Check --
5433 ----------------------------
5435 procedure Substitute_Valid_Check is
5438 Make_Attribute_Reference (Loc,
5439 Prefix => Relocate_Node (Lop),
5440 Attribute_Name => Name_Valid));
5442 Analyze_And_Resolve (N, Restyp);
5444 -- Give warning unless overflow checking is MINIMIZED or ELIMINATED,
5445 -- in which case, this usage makes sense, and in any case, we have
5446 -- actually eliminated the danger of optimization above.
5448 if Overflow_Check_Mode not in Minimized_Or_Eliminated then
5450 ("??explicit membership test may be optimized away", N);
5451 Error_Msg_N -- CODEFIX
5452 ("\??use ''Valid attribute instead", N);
5456 end Substitute_Valid_Check;
5458 -- Start of processing for Expand_N_In
5461 -- If set membership case, expand with separate procedure
5463 if Present (Alternatives (N)) then
5464 Expand_Set_Membership (N);
5468 -- Not set membership, proceed with expansion
5470 Ltyp := Etype (Left_Opnd (N));
5471 Rtyp := Etype (Right_Opnd (N));
5473 -- If MINIMIZED/ELIMINATED overflow mode and type is a signed integer
5474 -- type, then expand with a separate procedure. Note the use of the
5475 -- flag No_Minimize_Eliminate to prevent infinite recursion.
5477 if Overflow_Check_Mode in Minimized_Or_Eliminated
5478 and then Is_Signed_Integer_Type (Ltyp)
5479 and then not No_Minimize_Eliminate (N)
5481 Expand_Membership_Minimize_Eliminate_Overflow (N);
5485 -- Check case of explicit test for an expression in range of its
5486 -- subtype. This is suspicious usage and we replace it with a 'Valid
5487 -- test and give a warning for scalar types.
5489 if Is_Scalar_Type (Ltyp)
5491 -- Only relevant for source comparisons
5493 and then Comes_From_Source (N)
5495 -- In floating-point this is a standard way to check for finite values
5496 -- and using 'Valid would typically be a pessimization.
5498 and then not Is_Floating_Point_Type (Ltyp)
5500 -- Don't give the message unless right operand is a type entity and
5501 -- the type of the left operand matches this type. Note that this
5502 -- eliminates the cases where MINIMIZED/ELIMINATED mode overflow
5503 -- checks have changed the type of the left operand.
5505 and then Nkind (Rop) in N_Has_Entity
5506 and then Ltyp = Entity (Rop)
5508 -- Skip in VM mode, where we have no sense of invalid values. The
5509 -- warning still seems relevant, but not important enough to worry.
5511 and then VM_Target = No_VM
5513 -- Skip this for predicated types, where such expressions are a
5514 -- reasonable way of testing if something meets the predicate.
5516 and then not Present (Predicate_Function (Ltyp))
5518 Substitute_Valid_Check;
5522 -- Do validity check on operands
5524 if Validity_Checks_On and Validity_Check_Operands then
5525 Ensure_Valid (Left_Opnd (N));
5526 Validity_Check_Range (Right_Opnd (N));
5529 -- Case of explicit range
5531 if Nkind (Rop) = N_Range then
5533 Lo : constant Node_Id := Low_Bound (Rop);
5534 Hi : constant Node_Id := High_Bound (Rop);
5536 Lo_Orig : constant Node_Id := Original_Node (Lo);
5537 Hi_Orig : constant Node_Id := Original_Node (Hi);
5539 Lcheck : Compare_Result;
5540 Ucheck : Compare_Result;
5542 Warn1 : constant Boolean :=
5543 Constant_Condition_Warnings
5544 and then Comes_From_Source (N)
5545 and then not In_Instance;
5546 -- This must be true for any of the optimization warnings, we
5547 -- clearly want to give them only for source with the flag on. We
5548 -- also skip these warnings in an instance since it may be the
5549 -- case that different instantiations have different ranges.
5551 Warn2 : constant Boolean :=
5553 and then Nkind (Original_Node (Rop)) = N_Range
5554 and then Is_Integer_Type (Etype (Lo));
5555 -- For the case where only one bound warning is elided, we also
5556 -- insist on an explicit range and an integer type. The reason is
5557 -- that the use of enumeration ranges including an end point is
5558 -- common, as is the use of a subtype name, one of whose bounds is
5559 -- the same as the type of the expression.
5562 -- If test is explicit x'First .. x'Last, replace by valid check
5564 -- Could use some individual comments for this complex test ???
5566 if Is_Scalar_Type (Ltyp)
5568 -- And left operand is X'First where X matches left operand
5569 -- type (this eliminates cases of type mismatch, including
5570 -- the cases where ELIMINATED/MINIMIZED mode has changed the
5571 -- type of the left operand.
5573 and then Nkind (Lo_Orig) = N_Attribute_Reference
5574 and then Attribute_Name (Lo_Orig) = Name_First
5575 and then Nkind (Prefix (Lo_Orig)) in N_Has_Entity
5576 and then Entity (Prefix (Lo_Orig)) = Ltyp
5578 -- Same tests for right operand
5580 and then Nkind (Hi_Orig) = N_Attribute_Reference
5581 and then Attribute_Name (Hi_Orig) = Name_Last
5582 and then Nkind (Prefix (Hi_Orig)) in N_Has_Entity
5583 and then Entity (Prefix (Hi_Orig)) = Ltyp
5585 -- Relevant only for source cases
5587 and then Comes_From_Source (N)
5589 -- Omit for VM cases, where we don't have invalid values
5591 and then VM_Target = No_VM
5593 Substitute_Valid_Check;
5597 -- If bounds of type are known at compile time, and the end points
5598 -- are known at compile time and identical, this is another case
5599 -- for substituting a valid test. We only do this for discrete
5600 -- types, since it won't arise in practice for float types.
5602 if Comes_From_Source (N)
5603 and then Is_Discrete_Type (Ltyp)
5604 and then Compile_Time_Known_Value (Type_High_Bound (Ltyp))
5605 and then Compile_Time_Known_Value (Type_Low_Bound (Ltyp))
5606 and then Compile_Time_Known_Value (Lo)
5607 and then Compile_Time_Known_Value (Hi)
5608 and then Expr_Value (Type_High_Bound (Ltyp)) = Expr_Value (Hi)
5609 and then Expr_Value (Type_Low_Bound (Ltyp)) = Expr_Value (Lo)
5611 -- Kill warnings in instances, since they may be cases where we
5612 -- have a test in the generic that makes sense with some types
5613 -- and not with other types.
5615 and then not In_Instance
5617 Substitute_Valid_Check;
5621 -- If we have an explicit range, do a bit of optimization based on
5622 -- range analysis (we may be able to kill one or both checks).
5624 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => False);
5625 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => False);
5627 -- If either check is known to fail, replace result by False since
5628 -- the other check does not matter. Preserve the static flag for
5629 -- legality checks, because we are constant-folding beyond RM 4.9.
5631 if Lcheck = LT or else Ucheck = GT then
5633 Error_Msg_N ("?c?range test optimized away", N);
5634 Error_Msg_N ("\?c?value is known to be out of range", N);
5637 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5638 Analyze_And_Resolve (N, Restyp);
5639 Set_Is_Static_Expression (N, Static);
5642 -- If both checks are known to succeed, replace result by True,
5643 -- since we know we are in range.
5645 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5647 Error_Msg_N ("?c?range test optimized away", N);
5648 Error_Msg_N ("\?c?value is known to be in range", N);
5651 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5652 Analyze_And_Resolve (N, Restyp);
5653 Set_Is_Static_Expression (N, Static);
5656 -- If lower bound check succeeds and upper bound check is not
5657 -- known to succeed or fail, then replace the range check with
5658 -- a comparison against the upper bound.
5660 elsif Lcheck in Compare_GE then
5661 if Warn2 and then not In_Instance then
5662 Error_Msg_N ("??lower bound test optimized away", Lo);
5663 Error_Msg_N ("\??value is known to be in range", Lo);
5669 Right_Opnd => High_Bound (Rop)));
5670 Analyze_And_Resolve (N, Restyp);
5673 -- If upper bound check succeeds and lower bound check is not
5674 -- known to succeed or fail, then replace the range check with
5675 -- a comparison against the lower bound.
5677 elsif Ucheck in Compare_LE then
5678 if Warn2 and then not In_Instance then
5679 Error_Msg_N ("??upper bound test optimized away", Hi);
5680 Error_Msg_N ("\??value is known to be in range", Hi);
5686 Right_Opnd => Low_Bound (Rop)));
5687 Analyze_And_Resolve (N, Restyp);
5691 -- We couldn't optimize away the range check, but there is one
5692 -- more issue. If we are checking constant conditionals, then we
5693 -- see if we can determine the outcome assuming everything is
5694 -- valid, and if so give an appropriate warning.
5696 if Warn1 and then not Assume_No_Invalid_Values then
5697 Lcheck := Compile_Time_Compare (Lop, Lo, Assume_Valid => True);
5698 Ucheck := Compile_Time_Compare (Lop, Hi, Assume_Valid => True);
5700 -- Result is out of range for valid value
5702 if Lcheck = LT or else Ucheck = GT then
5704 ("?c?value can only be in range if it is invalid", N);
5706 -- Result is in range for valid value
5708 elsif Lcheck in Compare_GE and then Ucheck in Compare_LE then
5710 ("?c?value can only be out of range if it is invalid", N);
5712 -- Lower bound check succeeds if value is valid
5714 elsif Warn2 and then Lcheck in Compare_GE then
5716 ("?c?lower bound check only fails if it is invalid", Lo);
5718 -- Upper bound check succeeds if value is valid
5720 elsif Warn2 and then Ucheck in Compare_LE then
5722 ("?c?upper bound check only fails for invalid values", Hi);
5727 -- For all other cases of an explicit range, nothing to be done
5731 -- Here right operand is a subtype mark
5735 Typ : Entity_Id := Etype (Rop);
5736 Is_Acc : constant Boolean := Is_Access_Type (Typ);
5737 Cond : Node_Id := Empty;
5739 Obj : Node_Id := Lop;
5740 SCIL_Node : Node_Id;
5743 Remove_Side_Effects (Obj);
5745 -- For tagged type, do tagged membership operation
5747 if Is_Tagged_Type (Typ) then
5749 -- No expansion will be performed when VM_Target, as the VM
5750 -- back-ends will handle the membership tests directly (tags
5751 -- are not explicitly represented in Java objects, so the
5752 -- normal tagged membership expansion is not what we want).
5754 if Tagged_Type_Expansion then
5755 Tagged_Membership (N, SCIL_Node, New_N);
5757 Analyze_And_Resolve (N, Restyp);
5759 -- Update decoration of relocated node referenced by the
5762 if Generate_SCIL and then Present (SCIL_Node) then
5763 Set_SCIL_Node (N, SCIL_Node);
5769 -- If type is scalar type, rewrite as x in t'First .. t'Last.
5770 -- This reason we do this is that the bounds may have the wrong
5771 -- type if they come from the original type definition. Also this
5772 -- way we get all the processing above for an explicit range.
5774 -- Don't do this for predicated types, since in this case we
5775 -- want to check the predicate.
5777 elsif Is_Scalar_Type (Typ) then
5778 if No (Predicate_Function (Typ)) then
5782 Make_Attribute_Reference (Loc,
5783 Attribute_Name => Name_First,
5784 Prefix => New_Occurrence_Of (Typ, Loc)),
5787 Make_Attribute_Reference (Loc,
5788 Attribute_Name => Name_Last,
5789 Prefix => New_Occurrence_Of (Typ, Loc))));
5790 Analyze_And_Resolve (N, Restyp);
5795 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
5796 -- a membership test if the subtype mark denotes a constrained
5797 -- Unchecked_Union subtype and the expression lacks inferable
5800 elsif Is_Unchecked_Union (Base_Type (Typ))
5801 and then Is_Constrained (Typ)
5802 and then not Has_Inferable_Discriminants (Lop)
5805 Make_Raise_Program_Error (Loc,
5806 Reason => PE_Unchecked_Union_Restriction));
5808 -- Prevent Gigi from generating incorrect code by rewriting the
5809 -- test as False. What is this undocumented thing about ???
5811 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5815 -- Here we have a non-scalar type
5818 Typ := Designated_Type (Typ);
5821 if not Is_Constrained (Typ) then
5822 Rewrite (N, New_Occurrence_Of (Standard_True, Loc));
5823 Analyze_And_Resolve (N, Restyp);
5825 -- For the constrained array case, we have to check the subscripts
5826 -- for an exact match if the lengths are non-zero (the lengths
5827 -- must match in any case).
5829 elsif Is_Array_Type (Typ) then
5830 Check_Subscripts : declare
5831 function Build_Attribute_Reference
5834 Dim : Nat) return Node_Id;
5835 -- Build attribute reference E'Nam (Dim)
5837 -------------------------------
5838 -- Build_Attribute_Reference --
5839 -------------------------------
5841 function Build_Attribute_Reference
5844 Dim : Nat) return Node_Id
5848 Make_Attribute_Reference (Loc,
5850 Attribute_Name => Nam,
5851 Expressions => New_List (
5852 Make_Integer_Literal (Loc, Dim)));
5853 end Build_Attribute_Reference;
5855 -- Start of processing for Check_Subscripts
5858 for J in 1 .. Number_Dimensions (Typ) loop
5859 Evolve_And_Then (Cond,
5862 Build_Attribute_Reference
5863 (Duplicate_Subexpr_No_Checks (Obj),
5866 Build_Attribute_Reference
5867 (New_Occurrence_Of (Typ, Loc), Name_First, J)));
5869 Evolve_And_Then (Cond,
5872 Build_Attribute_Reference
5873 (Duplicate_Subexpr_No_Checks (Obj),
5876 Build_Attribute_Reference
5877 (New_Occurrence_Of (Typ, Loc), Name_Last, J)));
5886 Right_Opnd => Make_Null (Loc)),
5887 Right_Opnd => Cond);
5891 Analyze_And_Resolve (N, Restyp);
5892 end Check_Subscripts;
5894 -- These are the cases where constraint checks may be required,
5895 -- e.g. records with possible discriminants
5898 -- Expand the test into a series of discriminant comparisons.
5899 -- The expression that is built is the negation of the one that
5900 -- is used for checking discriminant constraints.
5902 Obj := Relocate_Node (Left_Opnd (N));
5904 if Has_Discriminants (Typ) then
5905 Cond := Make_Op_Not (Loc,
5906 Right_Opnd => Build_Discriminant_Checks (Obj, Typ));
5909 Cond := Make_Or_Else (Loc,
5913 Right_Opnd => Make_Null (Loc)),
5914 Right_Opnd => Cond);
5918 Cond := New_Occurrence_Of (Standard_True, Loc);
5922 Analyze_And_Resolve (N, Restyp);
5925 -- Ada 2012 (AI05-0149): Handle membership tests applied to an
5926 -- expression of an anonymous access type. This can involve an
5927 -- accessibility test and a tagged type membership test in the
5928 -- case of tagged designated types.
5930 if Ada_Version >= Ada_2012
5932 and then Ekind (Ltyp) = E_Anonymous_Access_Type
5935 Expr_Entity : Entity_Id := Empty;
5937 Param_Level : Node_Id;
5938 Type_Level : Node_Id;
5941 if Is_Entity_Name (Lop) then
5942 Expr_Entity := Param_Entity (Lop);
5944 if not Present (Expr_Entity) then
5945 Expr_Entity := Entity (Lop);
5949 -- If a conversion of the anonymous access value to the
5950 -- tested type would be illegal, then the result is False.
5952 if not Valid_Conversion
5953 (Lop, Rtyp, Lop, Report_Errs => False)
5955 Rewrite (N, New_Occurrence_Of (Standard_False, Loc));
5956 Analyze_And_Resolve (N, Restyp);
5958 -- Apply an accessibility check if the access object has an
5959 -- associated access level and when the level of the type is
5960 -- less deep than the level of the access parameter. This
5961 -- only occur for access parameters and stand-alone objects
5962 -- of an anonymous access type.
5965 if Present (Expr_Entity)
5968 (Effective_Extra_Accessibility (Expr_Entity))
5969 and then UI_Gt (Object_Access_Level (Lop),
5970 Type_Access_Level (Rtyp))
5974 (Effective_Extra_Accessibility (Expr_Entity), Loc);
5977 Make_Integer_Literal (Loc, Type_Access_Level (Rtyp));
5979 -- Return True only if the accessibility level of the
5980 -- expression entity is not deeper than the level of
5981 -- the tested access type.
5985 Left_Opnd => Relocate_Node (N),
5986 Right_Opnd => Make_Op_Le (Loc,
5987 Left_Opnd => Param_Level,
5988 Right_Opnd => Type_Level)));
5990 Analyze_And_Resolve (N);
5993 -- If the designated type is tagged, do tagged membership
5996 -- *** NOTE: we have to check not null before doing the
5997 -- tagged membership test (but maybe that can be done
5998 -- inside Tagged_Membership?).
6000 if Is_Tagged_Type (Typ) then
6003 Left_Opnd => Relocate_Node (N),
6007 Right_Opnd => Make_Null (Loc))));
6009 -- No expansion will be performed when VM_Target, as
6010 -- the VM back-ends will handle the membership tests
6011 -- directly (tags are not explicitly represented in
6012 -- Java objects, so the normal tagged membership
6013 -- expansion is not what we want).
6015 if Tagged_Type_Expansion then
6017 -- Note that we have to pass Original_Node, because
6018 -- the membership test might already have been
6019 -- rewritten by earlier parts of membership test.
6022 (Original_Node (N), SCIL_Node, New_N);
6024 -- Update decoration of relocated node referenced
6025 -- by the SCIL node.
6027 if Generate_SCIL and then Present (SCIL_Node) then
6028 Set_SCIL_Node (New_N, SCIL_Node);
6033 Left_Opnd => Relocate_Node (N),
6034 Right_Opnd => New_N));
6036 Analyze_And_Resolve (N, Restyp);
6045 -- At this point, we have done the processing required for the basic
6046 -- membership test, but not yet dealt with the predicate.
6050 -- If a predicate is present, then we do the predicate test, but we
6051 -- most certainly want to omit this if we are within the predicate
6052 -- function itself, since otherwise we have an infinite recursion.
6053 -- The check should also not be emitted when testing against a range
6054 -- (the check is only done when the right operand is a subtype; see
6055 -- RM12-4.5.2 (28.1/3-30/3)).
6058 PFunc : constant Entity_Id := Predicate_Function (Rtyp);
6062 and then Current_Scope /= PFunc
6063 and then Nkind (Rop) /= N_Range
6067 Left_Opnd => Relocate_Node (N),
6068 Right_Opnd => Make_Predicate_Call (Rtyp, Lop, Mem => True)));
6070 -- Analyze new expression, mark left operand as analyzed to
6071 -- avoid infinite recursion adding predicate calls. Similarly,
6072 -- suppress further range checks on the call.
6074 Set_Analyzed (Left_Opnd (N));
6075 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6077 -- All done, skip attempt at compile time determination of result
6084 --------------------------------
6085 -- Expand_N_Indexed_Component --
6086 --------------------------------
6088 procedure Expand_N_Indexed_Component (N : Node_Id) is
6089 Loc : constant Source_Ptr := Sloc (N);
6090 Typ : constant Entity_Id := Etype (N);
6091 P : constant Node_Id := Prefix (N);
6092 T : constant Entity_Id := Etype (P);
6096 -- A special optimization, if we have an indexed component that is
6097 -- selecting from a slice, then we can eliminate the slice, since, for
6098 -- example, x (i .. j)(k) is identical to x(k). The only difference is
6099 -- the range check required by the slice. The range check for the slice
6100 -- itself has already been generated. The range check for the
6101 -- subscripting operation is ensured by converting the subject to
6102 -- the subtype of the slice.
6104 -- This optimization not only generates better code, avoiding slice
6105 -- messing especially in the packed case, but more importantly bypasses
6106 -- some problems in handling this peculiar case, for example, the issue
6107 -- of dealing specially with object renamings.
6109 if Nkind (P) = N_Slice then
6111 Make_Indexed_Component (Loc,
6112 Prefix => Prefix (P),
6113 Expressions => New_List (
6115 (Etype (First_Index (Etype (P))),
6116 First (Expressions (N))))));
6117 Analyze_And_Resolve (N, Typ);
6121 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
6122 -- function, then additional actuals must be passed.
6124 if Ada_Version >= Ada_2005
6125 and then Is_Build_In_Place_Function_Call (P)
6127 Make_Build_In_Place_Call_In_Anonymous_Context (P);
6130 -- If the prefix is an access type, then we unconditionally rewrite if
6131 -- as an explicit dereference. This simplifies processing for several
6132 -- cases, including packed array cases and certain cases in which checks
6133 -- must be generated. We used to try to do this only when it was
6134 -- necessary, but it cleans up the code to do it all the time.
6136 if Is_Access_Type (T) then
6137 Insert_Explicit_Dereference (P);
6138 Analyze_And_Resolve (P, Designated_Type (T));
6139 Atp := Designated_Type (T);
6144 -- Generate index and validity checks
6146 Generate_Index_Checks (N);
6148 if Validity_Checks_On and then Validity_Check_Subscripts then
6149 Apply_Subscript_Validity_Checks (N);
6152 -- If selecting from an array with atomic components, and atomic sync
6153 -- is not suppressed for this array type, set atomic sync flag.
6155 if (Has_Atomic_Components (Atp)
6156 and then not Atomic_Synchronization_Disabled (Atp))
6157 or else (Is_Atomic (Typ)
6158 and then not Atomic_Synchronization_Disabled (Typ))
6160 Activate_Atomic_Synchronization (N);
6163 -- All done for the non-packed case
6165 if not Is_Packed (Etype (Prefix (N))) then
6169 -- For packed arrays that are not bit-packed (i.e. the case of an array
6170 -- with one or more index types with a non-contiguous enumeration type),
6171 -- we can always use the normal packed element get circuit.
6173 if not Is_Bit_Packed_Array (Etype (Prefix (N))) then
6174 Expand_Packed_Element_Reference (N);
6178 -- For a reference to a component of a bit packed array, we have to
6179 -- convert it to a reference to the corresponding Packed_Array_Type.
6180 -- We only want to do this for simple references, and not for:
6182 -- Left side of assignment, or prefix of left side of assignment, or
6183 -- prefix of the prefix, to handle packed arrays of packed arrays,
6184 -- This case is handled in Exp_Ch5.Expand_N_Assignment_Statement
6186 -- Renaming objects in renaming associations
6187 -- This case is handled when a use of the renamed variable occurs
6189 -- Actual parameters for a procedure call
6190 -- This case is handled in Exp_Ch6.Expand_Actuals
6192 -- The second expression in a 'Read attribute reference
6194 -- The prefix of an address or bit or size attribute reference
6196 -- The following circuit detects these exceptions
6199 Child : Node_Id := N;
6200 Parnt : Node_Id := Parent (N);
6204 if Nkind (Parnt) = N_Unchecked_Expression then
6207 elsif Nkind_In (Parnt, N_Object_Renaming_Declaration,
6208 N_Procedure_Call_Statement)
6209 or else (Nkind (Parnt) = N_Parameter_Association
6211 Nkind (Parent (Parnt)) = N_Procedure_Call_Statement)
6215 elsif Nkind (Parnt) = N_Attribute_Reference
6216 and then Nam_In (Attribute_Name (Parnt), Name_Address,
6219 and then Prefix (Parnt) = Child
6223 elsif Nkind (Parnt) = N_Assignment_Statement
6224 and then Name (Parnt) = Child
6228 -- If the expression is an index of an indexed component, it must
6229 -- be expanded regardless of context.
6231 elsif Nkind (Parnt) = N_Indexed_Component
6232 and then Child /= Prefix (Parnt)
6234 Expand_Packed_Element_Reference (N);
6237 elsif Nkind (Parent (Parnt)) = N_Assignment_Statement
6238 and then Name (Parent (Parnt)) = Parnt
6242 elsif Nkind (Parnt) = N_Attribute_Reference
6243 and then Attribute_Name (Parnt) = Name_Read
6244 and then Next (First (Expressions (Parnt))) = Child
6248 elsif Nkind_In (Parnt, N_Indexed_Component, N_Selected_Component)
6249 and then Prefix (Parnt) = Child
6254 Expand_Packed_Element_Reference (N);
6258 -- Keep looking up tree for unchecked expression, or if we are the
6259 -- prefix of a possible assignment left side.
6262 Parnt := Parent (Child);
6265 end Expand_N_Indexed_Component;
6267 ---------------------
6268 -- Expand_N_Not_In --
6269 ---------------------
6271 -- Replace a not in b by not (a in b) so that the expansions for (a in b)
6272 -- can be done. This avoids needing to duplicate this expansion code.
6274 procedure Expand_N_Not_In (N : Node_Id) is
6275 Loc : constant Source_Ptr := Sloc (N);
6276 Typ : constant Entity_Id := Etype (N);
6277 Cfs : constant Boolean := Comes_From_Source (N);
6284 Left_Opnd => Left_Opnd (N),
6285 Right_Opnd => Right_Opnd (N))));
6287 -- If this is a set membership, preserve list of alternatives
6289 Set_Alternatives (Right_Opnd (N), Alternatives (Original_Node (N)));
6291 -- We want this to appear as coming from source if original does (see
6292 -- transformations in Expand_N_In).
6294 Set_Comes_From_Source (N, Cfs);
6295 Set_Comes_From_Source (Right_Opnd (N), Cfs);
6297 -- Now analyze transformed node
6299 Analyze_And_Resolve (N, Typ);
6300 end Expand_N_Not_In;
6306 -- The only replacement required is for the case of a null of a type that
6307 -- is an access to protected subprogram, or a subtype thereof. We represent
6308 -- such access values as a record, and so we must replace the occurrence of
6309 -- null by the equivalent record (with a null address and a null pointer in
6310 -- it), so that the backend creates the proper value.
6312 procedure Expand_N_Null (N : Node_Id) is
6313 Loc : constant Source_Ptr := Sloc (N);
6314 Typ : constant Entity_Id := Base_Type (Etype (N));
6318 if Is_Access_Protected_Subprogram_Type (Typ) then
6320 Make_Aggregate (Loc,
6321 Expressions => New_List (
6322 New_Occurrence_Of (RTE (RE_Null_Address), Loc),
6326 Analyze_And_Resolve (N, Equivalent_Type (Typ));
6328 -- For subsequent semantic analysis, the node must retain its type.
6329 -- Gigi in any case replaces this type by the corresponding record
6330 -- type before processing the node.
6336 when RE_Not_Available =>
6340 ---------------------
6341 -- Expand_N_Op_Abs --
6342 ---------------------
6344 procedure Expand_N_Op_Abs (N : Node_Id) is
6345 Loc : constant Source_Ptr := Sloc (N);
6346 Expr : constant Node_Id := Right_Opnd (N);
6349 Unary_Op_Validity_Checks (N);
6351 -- Check for MINIMIZED/ELIMINATED overflow mode
6353 if Minimized_Eliminated_Overflow_Check (N) then
6354 Apply_Arithmetic_Overflow_Check (N);
6358 -- Deal with software overflow checking
6360 if not Backend_Overflow_Checks_On_Target
6361 and then Is_Signed_Integer_Type (Etype (N))
6362 and then Do_Overflow_Check (N)
6364 -- The only case to worry about is when the argument is equal to the
6365 -- largest negative number, so what we do is to insert the check:
6367 -- [constraint_error when Expr = typ'Base'First]
6369 -- with the usual Duplicate_Subexpr use coding for expr
6372 Make_Raise_Constraint_Error (Loc,
6375 Left_Opnd => Duplicate_Subexpr (Expr),
6377 Make_Attribute_Reference (Loc,
6379 New_Occurrence_Of (Base_Type (Etype (Expr)), Loc),
6380 Attribute_Name => Name_First)),
6381 Reason => CE_Overflow_Check_Failed));
6384 -- Vax floating-point types case
6386 if Vax_Float (Etype (N)) then
6387 Expand_Vax_Arith (N);
6389 end Expand_N_Op_Abs;
6391 ---------------------
6392 -- Expand_N_Op_Add --
6393 ---------------------
6395 procedure Expand_N_Op_Add (N : Node_Id) is
6396 Typ : constant Entity_Id := Etype (N);
6399 Binary_Op_Validity_Checks (N);
6401 -- Check for MINIMIZED/ELIMINATED overflow mode
6403 if Minimized_Eliminated_Overflow_Check (N) then
6404 Apply_Arithmetic_Overflow_Check (N);
6408 -- N + 0 = 0 + N = N for integer types
6410 if Is_Integer_Type (Typ) then
6411 if Compile_Time_Known_Value (Right_Opnd (N))
6412 and then Expr_Value (Right_Opnd (N)) = Uint_0
6414 Rewrite (N, Left_Opnd (N));
6417 elsif Compile_Time_Known_Value (Left_Opnd (N))
6418 and then Expr_Value (Left_Opnd (N)) = Uint_0
6420 Rewrite (N, Right_Opnd (N));
6425 -- Arithmetic overflow checks for signed integer/fixed point types
6427 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
6428 Apply_Arithmetic_Overflow_Check (N);
6431 -- Vax floating-point types case
6433 elsif Vax_Float (Typ) then
6434 Expand_Vax_Arith (N);
6436 end Expand_N_Op_Add;
6438 ---------------------
6439 -- Expand_N_Op_And --
6440 ---------------------
6442 procedure Expand_N_Op_And (N : Node_Id) is
6443 Typ : constant Entity_Id := Etype (N);
6446 Binary_Op_Validity_Checks (N);
6448 if Is_Array_Type (Etype (N)) then
6449 Expand_Boolean_Operator (N);
6451 elsif Is_Boolean_Type (Etype (N)) then
6452 Adjust_Condition (Left_Opnd (N));
6453 Adjust_Condition (Right_Opnd (N));
6454 Set_Etype (N, Standard_Boolean);
6455 Adjust_Result_Type (N, Typ);
6457 elsif Is_Intrinsic_Subprogram (Entity (N)) then
6458 Expand_Intrinsic_Call (N, Entity (N));
6461 end Expand_N_Op_And;
6463 ------------------------
6464 -- Expand_N_Op_Concat --
6465 ------------------------
6467 procedure Expand_N_Op_Concat (N : Node_Id) is
6469 -- List of operands to be concatenated
6472 -- Node which is to be replaced by the result of concatenating the nodes
6473 -- in the list Opnds.
6476 -- Ensure validity of both operands
6478 Binary_Op_Validity_Checks (N);
6480 -- If we are the left operand of a concatenation higher up the tree,
6481 -- then do nothing for now, since we want to deal with a series of
6482 -- concatenations as a unit.
6484 if Nkind (Parent (N)) = N_Op_Concat
6485 and then N = Left_Opnd (Parent (N))
6490 -- We get here with a concatenation whose left operand may be a
6491 -- concatenation itself with a consistent type. We need to process
6492 -- these concatenation operands from left to right, which means
6493 -- from the deepest node in the tree to the highest node.
6496 while Nkind (Left_Opnd (Cnode)) = N_Op_Concat loop
6497 Cnode := Left_Opnd (Cnode);
6500 -- Now Cnode is the deepest concatenation, and its parents are the
6501 -- concatenation nodes above, so now we process bottom up, doing the
6504 -- The outer loop runs more than once if more than one concatenation
6505 -- type is involved.
6508 Opnds := New_List (Left_Opnd (Cnode), Right_Opnd (Cnode));
6509 Set_Parent (Opnds, N);
6511 -- The inner loop gathers concatenation operands
6513 Inner : while Cnode /= N
6514 and then Base_Type (Etype (Cnode)) =
6515 Base_Type (Etype (Parent (Cnode)))
6517 Cnode := Parent (Cnode);
6518 Append (Right_Opnd (Cnode), Opnds);
6521 Expand_Concatenate (Cnode, Opnds);
6523 exit Outer when Cnode = N;
6524 Cnode := Parent (Cnode);
6526 end Expand_N_Op_Concat;
6528 ------------------------
6529 -- Expand_N_Op_Divide --
6530 ------------------------
6532 procedure Expand_N_Op_Divide (N : Node_Id) is
6533 Loc : constant Source_Ptr := Sloc (N);
6534 Lopnd : constant Node_Id := Left_Opnd (N);
6535 Ropnd : constant Node_Id := Right_Opnd (N);
6536 Ltyp : constant Entity_Id := Etype (Lopnd);
6537 Rtyp : constant Entity_Id := Etype (Ropnd);
6538 Typ : Entity_Id := Etype (N);
6539 Rknow : constant Boolean := Is_Integer_Type (Typ)
6541 Compile_Time_Known_Value (Ropnd);
6545 Binary_Op_Validity_Checks (N);
6547 -- Check for MINIMIZED/ELIMINATED overflow mode
6549 if Minimized_Eliminated_Overflow_Check (N) then
6550 Apply_Arithmetic_Overflow_Check (N);
6554 -- Otherwise proceed with expansion of division
6557 Rval := Expr_Value (Ropnd);
6560 -- N / 1 = N for integer types
6562 if Rknow and then Rval = Uint_1 then
6567 -- Convert x / 2 ** y to Shift_Right (x, y). Note that the fact that
6568 -- Is_Power_Of_2_For_Shift is set means that we know that our left
6569 -- operand is an unsigned integer, as required for this to work.
6571 if Nkind (Ropnd) = N_Op_Expon
6572 and then Is_Power_Of_2_For_Shift (Ropnd)
6574 -- We cannot do this transformation in configurable run time mode if we
6575 -- have 64-bit integers and long shifts are not available.
6577 and then (Esize (Ltyp) <= 32 or else Support_Long_Shifts_On_Target)
6580 Make_Op_Shift_Right (Loc,
6583 Convert_To (Standard_Natural, Right_Opnd (Ropnd))));
6584 Analyze_And_Resolve (N, Typ);
6588 -- Do required fixup of universal fixed operation
6590 if Typ = Universal_Fixed then
6591 Fixup_Universal_Fixed_Operation (N);
6595 -- Divisions with fixed-point results
6597 if Is_Fixed_Point_Type (Typ) then
6599 -- No special processing if Treat_Fixed_As_Integer is set, since
6600 -- from a semantic point of view such operations are simply integer
6601 -- operations and will be treated that way.
6603 if not Treat_Fixed_As_Integer (N) then
6604 if Is_Integer_Type (Rtyp) then
6605 Expand_Divide_Fixed_By_Integer_Giving_Fixed (N);
6607 Expand_Divide_Fixed_By_Fixed_Giving_Fixed (N);
6611 -- Other cases of division of fixed-point operands. Again we exclude the
6612 -- case where Treat_Fixed_As_Integer is set.
6614 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
6615 and then not Treat_Fixed_As_Integer (N)
6617 if Is_Integer_Type (Typ) then
6618 Expand_Divide_Fixed_By_Fixed_Giving_Integer (N);
6620 pragma Assert (Is_Floating_Point_Type (Typ));
6621 Expand_Divide_Fixed_By_Fixed_Giving_Float (N);
6624 -- Mixed-mode operations can appear in a non-static universal context,
6625 -- in which case the integer argument must be converted explicitly.
6627 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
6629 Convert_To (Universal_Real, Relocate_Node (Ropnd)));
6631 Analyze_And_Resolve (Ropnd, Universal_Real);
6633 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
6635 Convert_To (Universal_Real, Relocate_Node (Lopnd)));
6637 Analyze_And_Resolve (Lopnd, Universal_Real);
6639 -- Non-fixed point cases, do integer zero divide and overflow checks
6641 elsif Is_Integer_Type (Typ) then
6642 Apply_Divide_Checks (N);
6644 -- Deal with Vax_Float
6646 elsif Vax_Float (Typ) then
6647 Expand_Vax_Arith (N);
6650 end Expand_N_Op_Divide;
6652 --------------------
6653 -- Expand_N_Op_Eq --
6654 --------------------
6656 procedure Expand_N_Op_Eq (N : Node_Id) is
6657 Loc : constant Source_Ptr := Sloc (N);
6658 Typ : constant Entity_Id := Etype (N);
6659 Lhs : constant Node_Id := Left_Opnd (N);
6660 Rhs : constant Node_Id := Right_Opnd (N);
6661 Bodies : constant List_Id := New_List;
6662 A_Typ : constant Entity_Id := Etype (Lhs);
6664 Typl : Entity_Id := A_Typ;
6665 Op_Name : Entity_Id;
6668 procedure Build_Equality_Call (Eq : Entity_Id);
6669 -- If a constructed equality exists for the type or for its parent,
6670 -- build and analyze call, adding conversions if the operation is
6673 function Has_Unconstrained_UU_Component (Typ : Node_Id) return Boolean;
6674 -- Determines whether a type has a subcomponent of an unconstrained
6675 -- Unchecked_Union subtype. Typ is a record type.
6677 -------------------------
6678 -- Build_Equality_Call --
6679 -------------------------
6681 procedure Build_Equality_Call (Eq : Entity_Id) is
6682 Op_Type : constant Entity_Id := Etype (First_Formal (Eq));
6683 L_Exp : Node_Id := Relocate_Node (Lhs);
6684 R_Exp : Node_Id := Relocate_Node (Rhs);
6687 -- Adjust operands if necessary to comparison type
6689 if Base_Type (Op_Type) /= Base_Type (A_Typ)
6690 and then not Is_Class_Wide_Type (A_Typ)
6692 L_Exp := OK_Convert_To (Op_Type, L_Exp);
6693 R_Exp := OK_Convert_To (Op_Type, R_Exp);
6696 -- If we have an Unchecked_Union, we need to add the inferred
6697 -- discriminant values as actuals in the function call. At this
6698 -- point, the expansion has determined that both operands have
6699 -- inferable discriminants.
6701 if Is_Unchecked_Union (Op_Type) then
6703 Lhs_Type : constant Node_Id := Etype (L_Exp);
6704 Rhs_Type : constant Node_Id := Etype (R_Exp);
6706 Lhs_Discr_Vals : Elist_Id;
6707 -- List of inferred discriminant values for left operand.
6709 Rhs_Discr_Vals : Elist_Id;
6710 -- List of inferred discriminant values for right operand.
6715 Lhs_Discr_Vals := New_Elmt_List;
6716 Rhs_Discr_Vals := New_Elmt_List;
6718 -- Per-object constrained selected components require special
6719 -- attention. If the enclosing scope of the component is an
6720 -- Unchecked_Union, we cannot reference its discriminants
6721 -- directly. This is why we use the extra parameters of the
6722 -- equality function of the enclosing Unchecked_Union.
6724 -- type UU_Type (Discr : Integer := 0) is
6727 -- pragma Unchecked_Union (UU_Type);
6729 -- 1. Unchecked_Union enclosing record:
6731 -- type Enclosing_UU_Type (Discr : Integer := 0) is record
6733 -- Comp : UU_Type (Discr);
6735 -- end Enclosing_UU_Type;
6736 -- pragma Unchecked_Union (Enclosing_UU_Type);
6738 -- Obj1 : Enclosing_UU_Type;
6739 -- Obj2 : Enclosing_UU_Type (1);
6741 -- [. . .] Obj1 = Obj2 [. . .]
6745 -- if not (uu_typeEQ (obj1.comp, obj2.comp, a, b)) then
6747 -- A and B are the formal parameters of the equality function
6748 -- of Enclosing_UU_Type. The function always has two extra
6749 -- formals to capture the inferred discriminant values for
6750 -- each discriminant of the type.
6752 -- 2. Non-Unchecked_Union enclosing record:
6755 -- Enclosing_Non_UU_Type (Discr : Integer := 0)
6758 -- Comp : UU_Type (Discr);
6760 -- end Enclosing_Non_UU_Type;
6762 -- Obj1 : Enclosing_Non_UU_Type;
6763 -- Obj2 : Enclosing_Non_UU_Type (1);
6765 -- ... Obj1 = Obj2 ...
6769 -- if not (uu_typeEQ (obj1.comp, obj2.comp,
6770 -- obj1.discr, obj2.discr)) then
6772 -- In this case we can directly reference the discriminants of
6773 -- the enclosing record.
6775 -- Process left operand of equality
6777 if Nkind (Lhs) = N_Selected_Component
6779 Has_Per_Object_Constraint (Entity (Selector_Name (Lhs)))
6781 -- If enclosing record is an Unchecked_Union, use formals
6782 -- corresponding to each discriminant. The name of the
6783 -- formal is that of the discriminant, with added suffix,
6784 -- see Exp_Ch3.Build_Record_Equality for details.
6786 if Is_Unchecked_Union (Scope (Entity (Selector_Name (Lhs))))
6790 (Scope (Entity (Selector_Name (Lhs))));
6791 while Present (Discr) loop
6793 Make_Identifier (Loc,
6794 Chars => New_External_Name (Chars (Discr), 'A')),
6795 To => Lhs_Discr_Vals);
6796 Next_Discriminant (Discr);
6799 -- If enclosing record is of a non-Unchecked_Union type, it
6800 -- is possible to reference its discriminants directly.
6803 Discr := First_Discriminant (Lhs_Type);
6804 while Present (Discr) loop
6806 Make_Selected_Component (Loc,
6807 Prefix => Prefix (Lhs),
6810 (Get_Discriminant_Value (Discr,
6812 Stored_Constraint (Lhs_Type)))),
6813 To => Lhs_Discr_Vals);
6814 Next_Discriminant (Discr);
6818 -- Otherwise operand is on object with a constrained type.
6819 -- Infer the discriminant values from the constraint.
6823 Discr := First_Discriminant (Lhs_Type);
6824 while Present (Discr) loop
6827 (Get_Discriminant_Value (Discr,
6829 Stored_Constraint (Lhs_Type))),
6830 To => Lhs_Discr_Vals);
6831 Next_Discriminant (Discr);
6835 -- Similar processing for right operand of equality
6837 if Nkind (Rhs) = N_Selected_Component
6839 Has_Per_Object_Constraint (Entity (Selector_Name (Rhs)))
6841 if Is_Unchecked_Union
6842 (Scope (Entity (Selector_Name (Rhs))))
6846 (Scope (Entity (Selector_Name (Rhs))));
6847 while Present (Discr) loop
6849 Make_Identifier (Loc,
6850 Chars => New_External_Name (Chars (Discr), 'B')),
6851 To => Rhs_Discr_Vals);
6852 Next_Discriminant (Discr);
6856 Discr := First_Discriminant (Rhs_Type);
6857 while Present (Discr) loop
6859 Make_Selected_Component (Loc,
6860 Prefix => Prefix (Rhs),
6862 New_Copy (Get_Discriminant_Value
6865 Stored_Constraint (Rhs_Type)))),
6866 To => Rhs_Discr_Vals);
6867 Next_Discriminant (Discr);
6872 Discr := First_Discriminant (Rhs_Type);
6873 while Present (Discr) loop
6875 New_Copy (Get_Discriminant_Value
6878 Stored_Constraint (Rhs_Type))),
6879 To => Rhs_Discr_Vals);
6880 Next_Discriminant (Discr);
6884 -- Now merge the list of discriminant values so that values
6885 -- of corresponding discriminants are adjacent.
6893 Params := New_List (L_Exp, R_Exp);
6894 L_Elmt := First_Elmt (Lhs_Discr_Vals);
6895 R_Elmt := First_Elmt (Rhs_Discr_Vals);
6896 while Present (L_Elmt) loop
6897 Append_To (Params, Node (L_Elmt));
6898 Append_To (Params, Node (R_Elmt));
6904 Make_Function_Call (Loc,
6905 Name => New_Occurrence_Of (Eq, Loc),
6906 Parameter_Associations => Params));
6910 -- Normal case, not an unchecked union
6914 Make_Function_Call (Loc,
6915 Name => New_Occurrence_Of (Eq, Loc),
6916 Parameter_Associations => New_List (L_Exp, R_Exp)));
6919 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
6920 end Build_Equality_Call;
6922 ------------------------------------
6923 -- Has_Unconstrained_UU_Component --
6924 ------------------------------------
6926 function Has_Unconstrained_UU_Component
6927 (Typ : Node_Id) return Boolean
6929 Tdef : constant Node_Id :=
6930 Type_Definition (Declaration_Node (Base_Type (Typ)));
6934 function Component_Is_Unconstrained_UU
6935 (Comp : Node_Id) return Boolean;
6936 -- Determines whether the subtype of the component is an
6937 -- unconstrained Unchecked_Union.
6939 function Variant_Is_Unconstrained_UU
6940 (Variant : Node_Id) return Boolean;
6941 -- Determines whether a component of the variant has an unconstrained
6942 -- Unchecked_Union subtype.
6944 -----------------------------------
6945 -- Component_Is_Unconstrained_UU --
6946 -----------------------------------
6948 function Component_Is_Unconstrained_UU
6949 (Comp : Node_Id) return Boolean
6952 if Nkind (Comp) /= N_Component_Declaration then
6957 Sindic : constant Node_Id :=
6958 Subtype_Indication (Component_Definition (Comp));
6961 -- Unconstrained nominal type. In the case of a constraint
6962 -- present, the node kind would have been N_Subtype_Indication.
6964 if Nkind (Sindic) = N_Identifier then
6965 return Is_Unchecked_Union (Base_Type (Etype (Sindic)));
6970 end Component_Is_Unconstrained_UU;
6972 ---------------------------------
6973 -- Variant_Is_Unconstrained_UU --
6974 ---------------------------------
6976 function Variant_Is_Unconstrained_UU
6977 (Variant : Node_Id) return Boolean
6979 Clist : constant Node_Id := Component_List (Variant);
6982 if Is_Empty_List (Component_Items (Clist)) then
6986 -- We only need to test one component
6989 Comp : Node_Id := First (Component_Items (Clist));
6992 while Present (Comp) loop
6993 if Component_Is_Unconstrained_UU (Comp) then
7001 -- None of the components withing the variant were of
7002 -- unconstrained Unchecked_Union type.
7005 end Variant_Is_Unconstrained_UU;
7007 -- Start of processing for Has_Unconstrained_UU_Component
7010 if Null_Present (Tdef) then
7014 Clist := Component_List (Tdef);
7015 Vpart := Variant_Part (Clist);
7017 -- Inspect available components
7019 if Present (Component_Items (Clist)) then
7021 Comp : Node_Id := First (Component_Items (Clist));
7024 while Present (Comp) loop
7026 -- One component is sufficient
7028 if Component_Is_Unconstrained_UU (Comp) then
7037 -- Inspect available components withing variants
7039 if Present (Vpart) then
7041 Variant : Node_Id := First (Variants (Vpart));
7044 while Present (Variant) loop
7046 -- One component within a variant is sufficient
7048 if Variant_Is_Unconstrained_UU (Variant) then
7057 -- Neither the available components, nor the components inside the
7058 -- variant parts were of an unconstrained Unchecked_Union subtype.
7061 end Has_Unconstrained_UU_Component;
7063 -- Start of processing for Expand_N_Op_Eq
7066 Binary_Op_Validity_Checks (N);
7068 -- Deal with private types
7070 if Ekind (Typl) = E_Private_Type then
7071 Typl := Underlying_Type (Typl);
7072 elsif Ekind (Typl) = E_Private_Subtype then
7073 Typl := Underlying_Type (Base_Type (Typl));
7078 -- It may happen in error situations that the underlying type is not
7079 -- set. The error will be detected later, here we just defend the
7086 Typl := Base_Type (Typl);
7088 -- Equality between variant records results in a call to a routine
7089 -- that has conditional tests of the discriminant value(s), and hence
7090 -- violates the No_Implicit_Conditionals restriction.
7092 if Has_Variant_Part (Typl) then
7097 Check_Restriction (Msg, No_Implicit_Conditionals, N);
7101 ("\comparison of variant records tests discriminants", N);
7107 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7108 -- means we no longer have a comparison operation, we are all done.
7110 Expand_Compare_Minimize_Eliminate_Overflow (N);
7112 if Nkind (N) /= N_Op_Eq then
7116 -- Boolean types (requiring handling of non-standard case)
7118 if Is_Boolean_Type (Typl) then
7119 Adjust_Condition (Left_Opnd (N));
7120 Adjust_Condition (Right_Opnd (N));
7121 Set_Etype (N, Standard_Boolean);
7122 Adjust_Result_Type (N, Typ);
7126 elsif Is_Array_Type (Typl) then
7128 -- If we are doing full validity checking, and it is possible for the
7129 -- array elements to be invalid then expand out array comparisons to
7130 -- make sure that we check the array elements.
7132 if Validity_Check_Operands
7133 and then not Is_Known_Valid (Component_Type (Typl))
7136 Save_Force_Validity_Checks : constant Boolean :=
7137 Force_Validity_Checks;
7139 Force_Validity_Checks := True;
7141 Expand_Array_Equality
7143 Relocate_Node (Lhs),
7144 Relocate_Node (Rhs),
7147 Insert_Actions (N, Bodies);
7148 Analyze_And_Resolve (N, Standard_Boolean);
7149 Force_Validity_Checks := Save_Force_Validity_Checks;
7152 -- Packed case where both operands are known aligned
7154 elsif Is_Bit_Packed_Array (Typl)
7155 and then not Is_Possibly_Unaligned_Object (Lhs)
7156 and then not Is_Possibly_Unaligned_Object (Rhs)
7158 Expand_Packed_Eq (N);
7160 -- Where the component type is elementary we can use a block bit
7161 -- comparison (if supported on the target) exception in the case
7162 -- of floating-point (negative zero issues require element by
7163 -- element comparison), and atomic types (where we must be sure
7164 -- to load elements independently) and possibly unaligned arrays.
7166 elsif Is_Elementary_Type (Component_Type (Typl))
7167 and then not Is_Floating_Point_Type (Component_Type (Typl))
7168 and then not Is_Atomic (Component_Type (Typl))
7169 and then not Is_Possibly_Unaligned_Object (Lhs)
7170 and then not Is_Possibly_Unaligned_Object (Rhs)
7171 and then Support_Composite_Compare_On_Target
7175 -- For composite and floating-point cases, expand equality loop to
7176 -- make sure of using proper comparisons for tagged types, and
7177 -- correctly handling the floating-point case.
7181 Expand_Array_Equality
7183 Relocate_Node (Lhs),
7184 Relocate_Node (Rhs),
7187 Insert_Actions (N, Bodies, Suppress => All_Checks);
7188 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7193 elsif Is_Record_Type (Typl) then
7195 -- For tagged types, use the primitive "="
7197 if Is_Tagged_Type (Typl) then
7199 -- No need to do anything else compiling under restriction
7200 -- No_Dispatching_Calls. During the semantic analysis we
7201 -- already notified such violation.
7203 if Restriction_Active (No_Dispatching_Calls) then
7207 -- If this is derived from an untagged private type completed with
7208 -- a tagged type, it does not have a full view, so we use the
7209 -- primitive operations of the private type. This check should no
7210 -- longer be necessary when these types get their full views???
7212 if Is_Private_Type (A_Typ)
7213 and then not Is_Tagged_Type (A_Typ)
7214 and then Is_Derived_Type (A_Typ)
7215 and then No (Full_View (A_Typ))
7217 -- Search for equality operation, checking that the operands
7218 -- have the same type. Note that we must find a matching entry,
7219 -- or something is very wrong.
7221 Prim := First_Elmt (Collect_Primitive_Operations (A_Typ));
7223 while Present (Prim) loop
7224 exit when Chars (Node (Prim)) = Name_Op_Eq
7225 and then Etype (First_Formal (Node (Prim))) =
7226 Etype (Next_Formal (First_Formal (Node (Prim))))
7228 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7233 pragma Assert (Present (Prim));
7234 Op_Name := Node (Prim);
7236 -- Find the type's predefined equality or an overriding
7237 -- user- defined equality. The reason for not simply calling
7238 -- Find_Prim_Op here is that there may be a user-defined
7239 -- overloaded equality op that precedes the equality that we want,
7240 -- so we have to explicitly search (e.g., there could be an
7241 -- equality with two different parameter types).
7244 if Is_Class_Wide_Type (Typl) then
7245 Typl := Root_Type (Typl);
7248 Prim := First_Elmt (Primitive_Operations (Typl));
7249 while Present (Prim) loop
7250 exit when Chars (Node (Prim)) = Name_Op_Eq
7251 and then Etype (First_Formal (Node (Prim))) =
7252 Etype (Next_Formal (First_Formal (Node (Prim))))
7254 Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7259 pragma Assert (Present (Prim));
7260 Op_Name := Node (Prim);
7263 Build_Equality_Call (Op_Name);
7265 -- Ada 2005 (AI-216): Program_Error is raised when evaluating the
7266 -- predefined equality operator for a type which has a subcomponent
7267 -- of an Unchecked_Union type whose nominal subtype is unconstrained.
7269 elsif Has_Unconstrained_UU_Component (Typl) then
7271 Make_Raise_Program_Error (Loc,
7272 Reason => PE_Unchecked_Union_Restriction));
7274 -- Prevent Gigi from generating incorrect code by rewriting the
7275 -- equality as a standard False. (is this documented somewhere???)
7278 New_Occurrence_Of (Standard_False, Loc));
7280 elsif Is_Unchecked_Union (Typl) then
7282 -- If we can infer the discriminants of the operands, we make a
7283 -- call to the TSS equality function.
7285 if Has_Inferable_Discriminants (Lhs)
7287 Has_Inferable_Discriminants (Rhs)
7290 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7293 -- Ada 2005 (AI-216): Program_Error is raised when evaluating
7294 -- the predefined equality operator for an Unchecked_Union type
7295 -- if either of the operands lack inferable discriminants.
7298 Make_Raise_Program_Error (Loc,
7299 Reason => PE_Unchecked_Union_Restriction));
7301 -- Prevent Gigi from generating incorrect code by rewriting
7302 -- the equality as a standard False (documented where???).
7305 New_Occurrence_Of (Standard_False, Loc));
7309 -- If a type support function is present (for complex cases), use it
7311 elsif Present (TSS (Root_Type (Typl), TSS_Composite_Equality)) then
7313 (TSS (Root_Type (Typl), TSS_Composite_Equality));
7315 -- When comparing two Bounded_Strings, use the primitive equality of
7316 -- the root Super_String type.
7318 elsif Is_Bounded_String (Typl) then
7320 First_Elmt (Collect_Primitive_Operations (Root_Type (Typl)));
7322 while Present (Prim) loop
7323 exit when Chars (Node (Prim)) = Name_Op_Eq
7324 and then Etype (First_Formal (Node (Prim))) =
7325 Etype (Next_Formal (First_Formal (Node (Prim))))
7326 and then Base_Type (Etype (Node (Prim))) = Standard_Boolean;
7331 -- A Super_String type should always have a primitive equality
7333 pragma Assert (Present (Prim));
7334 Build_Equality_Call (Node (Prim));
7336 -- Otherwise expand the component by component equality. Note that
7337 -- we never use block-bit comparisons for records, because of the
7338 -- problems with gaps. The backend will often be able to recombine
7339 -- the separate comparisons that we generate here.
7342 Remove_Side_Effects (Lhs);
7343 Remove_Side_Effects (Rhs);
7345 Expand_Record_Equality (N, Typl, Lhs, Rhs, Bodies));
7347 Insert_Actions (N, Bodies, Suppress => All_Checks);
7348 Analyze_And_Resolve (N, Standard_Boolean, Suppress => All_Checks);
7352 -- Test if result is known at compile time
7354 Rewrite_Comparison (N);
7356 -- If we still have comparison for Vax_Float, process it
7358 if Vax_Float (Typl) and then Nkind (N) in N_Op_Compare then
7359 Expand_Vax_Comparison (N);
7363 Optimize_Length_Comparison (N);
7366 -----------------------
7367 -- Expand_N_Op_Expon --
7368 -----------------------
7370 procedure Expand_N_Op_Expon (N : Node_Id) is
7371 Loc : constant Source_Ptr := Sloc (N);
7372 Typ : constant Entity_Id := Etype (N);
7373 Rtyp : constant Entity_Id := Root_Type (Typ);
7374 Base : constant Node_Id := Relocate_Node (Left_Opnd (N));
7375 Bastyp : constant Node_Id := Etype (Base);
7376 Exp : constant Node_Id := Relocate_Node (Right_Opnd (N));
7377 Exptyp : constant Entity_Id := Etype (Exp);
7378 Ovflo : constant Boolean := Do_Overflow_Check (N);
7387 Binary_Op_Validity_Checks (N);
7389 -- CodePeer wants to see the unexpanded N_Op_Expon node
7391 if CodePeer_Mode then
7395 -- If either operand is of a private type, then we have the use of an
7396 -- intrinsic operator, and we get rid of the privateness, by using root
7397 -- types of underlying types for the actual operation. Otherwise the
7398 -- private types will cause trouble if we expand multiplications or
7399 -- shifts etc. We also do this transformation if the result type is
7400 -- different from the base type.
7402 if Is_Private_Type (Etype (Base))
7403 or else Is_Private_Type (Typ)
7404 or else Is_Private_Type (Exptyp)
7405 or else Rtyp /= Root_Type (Bastyp)
7408 Bt : constant Entity_Id := Root_Type (Underlying_Type (Bastyp));
7409 Et : constant Entity_Id := Root_Type (Underlying_Type (Exptyp));
7412 Unchecked_Convert_To (Typ,
7414 Left_Opnd => Unchecked_Convert_To (Bt, Base),
7415 Right_Opnd => Unchecked_Convert_To (Et, Exp))));
7416 Analyze_And_Resolve (N, Typ);
7421 -- Check for MINIMIZED/ELIMINATED overflow mode
7423 if Minimized_Eliminated_Overflow_Check (N) then
7424 Apply_Arithmetic_Overflow_Check (N);
7428 -- Test for case of known right argument where we can replace the
7429 -- exponentiation by an equivalent expression using multiplication.
7431 -- Note: use CRT_Safe version of Compile_Time_Known_Value because in
7432 -- configurable run-time mode, we may not have the exponentiation
7433 -- routine available, and we don't want the legality of the program
7434 -- to depend on how clever the compiler is in knowing values.
7436 if CRT_Safe_Compile_Time_Known_Value (Exp) then
7437 Expv := Expr_Value (Exp);
7439 -- We only fold small non-negative exponents. You might think we
7440 -- could fold small negative exponents for the real case, but we
7441 -- can't because we are required to raise Constraint_Error for
7442 -- the case of 0.0 ** (negative) even if Machine_Overflows = False.
7443 -- See ACVC test C4A012B.
7445 if Expv >= 0 and then Expv <= 4 then
7447 -- X ** 0 = 1 (or 1.0)
7451 -- Call Remove_Side_Effects to ensure that any side effects
7452 -- in the ignored left operand (in particular function calls
7453 -- to user defined functions) are properly executed.
7455 Remove_Side_Effects (Base);
7457 if Ekind (Typ) in Integer_Kind then
7458 Xnode := Make_Integer_Literal (Loc, Intval => 1);
7460 Xnode := Make_Real_Literal (Loc, Ureal_1);
7472 Make_Op_Multiply (Loc,
7473 Left_Opnd => Duplicate_Subexpr (Base),
7474 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7476 -- X ** 3 = X * X * X
7480 Make_Op_Multiply (Loc,
7482 Make_Op_Multiply (Loc,
7483 Left_Opnd => Duplicate_Subexpr (Base),
7484 Right_Opnd => Duplicate_Subexpr_No_Checks (Base)),
7485 Right_Opnd => Duplicate_Subexpr_No_Checks (Base));
7490 -- En : constant base'type := base * base;
7495 pragma Assert (Expv = 4);
7496 Temp := Make_Temporary (Loc, 'E', Base);
7499 Make_Expression_With_Actions (Loc,
7500 Actions => New_List (
7501 Make_Object_Declaration (Loc,
7502 Defining_Identifier => Temp,
7503 Constant_Present => True,
7504 Object_Definition => New_Occurrence_Of (Typ, Loc),
7506 Make_Op_Multiply (Loc,
7508 Duplicate_Subexpr (Base),
7510 Duplicate_Subexpr_No_Checks (Base)))),
7513 Make_Op_Multiply (Loc,
7514 Left_Opnd => New_Occurrence_Of (Temp, Loc),
7515 Right_Opnd => New_Occurrence_Of (Temp, Loc)));
7519 Analyze_And_Resolve (N, Typ);
7524 -- Case of (2 ** expression) appearing as an argument of an integer
7525 -- multiplication, or as the right argument of a division of a non-
7526 -- negative integer. In such cases we leave the node untouched, setting
7527 -- the flag Is_Natural_Power_Of_2_for_Shift set, then the expansion
7528 -- of the higher level node converts it into a shift.
7530 -- Another case is 2 ** N in any other context. We simply convert
7531 -- this to 1 * 2 ** N, and then the above transformation applies.
7533 -- Note: this transformation is not applicable for a modular type with
7534 -- a non-binary modulus in the multiplication case, since we get a wrong
7535 -- result if the shift causes an overflow before the modular reduction.
7537 -- Note: we used to check that Exptyp was an unsigned type. But that is
7538 -- an unnecessary check, since if Exp is negative, we have a run-time
7539 -- error that is either caught (so we get the right result) or we have
7540 -- suppressed the check, in which case the code is erroneous anyway.
7542 if Nkind (Base) = N_Integer_Literal
7543 and then CRT_Safe_Compile_Time_Known_Value (Base)
7544 and then Expr_Value (Base) = Uint_2
7545 and then Is_Integer_Type (Root_Type (Exptyp))
7546 and then Esize (Root_Type (Exptyp)) <= Esize (Standard_Integer)
7549 -- First the multiply and divide cases
7551 if Nkind_In (Parent (N), N_Op_Divide, N_Op_Multiply) then
7553 P : constant Node_Id := Parent (N);
7554 L : constant Node_Id := Left_Opnd (P);
7555 R : constant Node_Id := Right_Opnd (P);
7558 if (Nkind (P) = N_Op_Multiply
7559 and then not Non_Binary_Modulus (Typ)
7561 ((Is_Integer_Type (Etype (L)) and then R = N)
7563 (Is_Integer_Type (Etype (R)) and then L = N))
7564 and then not Do_Overflow_Check (P))
7566 (Nkind (P) = N_Op_Divide
7567 and then Is_Integer_Type (Etype (L))
7568 and then Is_Unsigned_Type (Etype (L))
7570 and then not Do_Overflow_Check (P))
7572 Set_Is_Power_Of_2_For_Shift (N);
7577 -- Now the other cases
7579 elsif not Non_Binary_Modulus (Typ) then
7581 Make_Op_Multiply (Loc,
7582 Left_Opnd => Make_Integer_Literal (Loc, 1),
7583 Right_Opnd => Relocate_Node (N)));
7584 Analyze_And_Resolve (N, Typ);
7589 -- Fall through if exponentiation must be done using a runtime routine
7591 -- First deal with modular case
7593 if Is_Modular_Integer_Type (Rtyp) then
7595 -- Non-binary case, we call the special exponentiation routine for
7596 -- the non-binary case, converting the argument to Long_Long_Integer
7597 -- and passing the modulus value. Then the result is converted back
7598 -- to the base type.
7600 if Non_Binary_Modulus (Rtyp) then
7603 Make_Function_Call (Loc,
7604 Name => New_Occurrence_Of (RTE (RE_Exp_Modular), Loc),
7605 Parameter_Associations => New_List (
7606 Convert_To (RTE (RE_Unsigned), Base),
7607 Make_Integer_Literal (Loc, Modulus (Rtyp)),
7610 -- Binary case, in this case, we call one of two routines, either the
7611 -- unsigned integer case, or the unsigned long long integer case,
7612 -- with a final "and" operation to do the required mod.
7615 if UI_To_Int (Esize (Rtyp)) <= Standard_Integer_Size then
7616 Ent := RTE (RE_Exp_Unsigned);
7618 Ent := RTE (RE_Exp_Long_Long_Unsigned);
7625 Make_Function_Call (Loc,
7626 Name => New_Occurrence_Of (Ent, Loc),
7627 Parameter_Associations => New_List (
7628 Convert_To (Etype (First_Formal (Ent)), Base),
7631 Make_Integer_Literal (Loc, Modulus (Rtyp) - 1))));
7635 -- Common exit point for modular type case
7637 Analyze_And_Resolve (N, Typ);
7640 -- Signed integer cases, done using either Integer or Long_Long_Integer.
7641 -- It is not worth having routines for Short_[Short_]Integer, since for
7642 -- most machines it would not help, and it would generate more code that
7643 -- might need certification when a certified run time is required.
7645 -- In the integer cases, we have two routines, one for when overflow
7646 -- checks are required, and one when they are not required, since there
7647 -- is a real gain in omitting checks on many machines.
7649 elsif Rtyp = Base_Type (Standard_Long_Long_Integer)
7650 or else (Rtyp = Base_Type (Standard_Long_Integer)
7652 Esize (Standard_Long_Integer) > Esize (Standard_Integer))
7653 or else Rtyp = Universal_Integer
7655 Etyp := Standard_Long_Long_Integer;
7657 -- Overflow checking is the only choice on the AAMP target, where
7658 -- arithmetic instructions check overflow automatically, so only
7659 -- one version of the exponentiation unit is needed.
7661 if Ovflo or AAMP_On_Target then
7662 Rent := RE_Exp_Long_Long_Integer;
7664 Rent := RE_Exn_Long_Long_Integer;
7667 elsif Is_Signed_Integer_Type (Rtyp) then
7668 Etyp := Standard_Integer;
7670 -- Overflow checking is the only choice on the AAMP target, where
7671 -- arithmetic instructions check overflow automatically, so only
7672 -- one version of the exponentiation unit is needed.
7674 if Ovflo or AAMP_On_Target then
7675 Rent := RE_Exp_Integer;
7677 Rent := RE_Exn_Integer;
7680 -- Floating-point cases, always done using Long_Long_Float. We do not
7681 -- need separate routines for the overflow case here, since in the case
7682 -- of floating-point, we generate infinities anyway as a rule (either
7683 -- that or we automatically trap overflow), and if there is an infinity
7684 -- generated and a range check is required, the check will fail anyway.
7687 pragma Assert (Is_Floating_Point_Type (Rtyp));
7688 Etyp := Standard_Long_Long_Float;
7689 Rent := RE_Exn_Long_Long_Float;
7692 -- Common processing for integer cases and floating-point cases.
7693 -- If we are in the right type, we can call runtime routine directly
7696 and then Rtyp /= Universal_Integer
7697 and then Rtyp /= Universal_Real
7700 Make_Function_Call (Loc,
7701 Name => New_Occurrence_Of (RTE (Rent), Loc),
7702 Parameter_Associations => New_List (Base, Exp)));
7704 -- Otherwise we have to introduce conversions (conversions are also
7705 -- required in the universal cases, since the runtime routine is
7706 -- typed using one of the standard types).
7711 Make_Function_Call (Loc,
7712 Name => New_Occurrence_Of (RTE (Rent), Loc),
7713 Parameter_Associations => New_List (
7714 Convert_To (Etyp, Base),
7718 Analyze_And_Resolve (N, Typ);
7722 when RE_Not_Available =>
7724 end Expand_N_Op_Expon;
7726 --------------------
7727 -- Expand_N_Op_Ge --
7728 --------------------
7730 procedure Expand_N_Op_Ge (N : Node_Id) is
7731 Typ : constant Entity_Id := Etype (N);
7732 Op1 : constant Node_Id := Left_Opnd (N);
7733 Op2 : constant Node_Id := Right_Opnd (N);
7734 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7737 Binary_Op_Validity_Checks (N);
7739 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7740 -- means we no longer have a comparison operation, we are all done.
7742 Expand_Compare_Minimize_Eliminate_Overflow (N);
7744 if Nkind (N) /= N_Op_Ge then
7750 if Is_Array_Type (Typ1) then
7751 Expand_Array_Comparison (N);
7755 -- Deal with boolean operands
7757 if Is_Boolean_Type (Typ1) then
7758 Adjust_Condition (Op1);
7759 Adjust_Condition (Op2);
7760 Set_Etype (N, Standard_Boolean);
7761 Adjust_Result_Type (N, Typ);
7764 Rewrite_Comparison (N);
7766 -- If we still have comparison, and Vax_Float type, process it
7768 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7769 Expand_Vax_Comparison (N);
7773 Optimize_Length_Comparison (N);
7776 --------------------
7777 -- Expand_N_Op_Gt --
7778 --------------------
7780 procedure Expand_N_Op_Gt (N : Node_Id) is
7781 Typ : constant Entity_Id := Etype (N);
7782 Op1 : constant Node_Id := Left_Opnd (N);
7783 Op2 : constant Node_Id := Right_Opnd (N);
7784 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7787 Binary_Op_Validity_Checks (N);
7789 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7790 -- means we no longer have a comparison operation, we are all done.
7792 Expand_Compare_Minimize_Eliminate_Overflow (N);
7794 if Nkind (N) /= N_Op_Gt then
7798 -- Deal with array type operands
7800 if Is_Array_Type (Typ1) then
7801 Expand_Array_Comparison (N);
7805 -- Deal with boolean type operands
7807 if Is_Boolean_Type (Typ1) then
7808 Adjust_Condition (Op1);
7809 Adjust_Condition (Op2);
7810 Set_Etype (N, Standard_Boolean);
7811 Adjust_Result_Type (N, Typ);
7814 Rewrite_Comparison (N);
7816 -- If we still have comparison, and Vax_Float type, process it
7818 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7819 Expand_Vax_Comparison (N);
7823 Optimize_Length_Comparison (N);
7826 --------------------
7827 -- Expand_N_Op_Le --
7828 --------------------
7830 procedure Expand_N_Op_Le (N : Node_Id) is
7831 Typ : constant Entity_Id := Etype (N);
7832 Op1 : constant Node_Id := Left_Opnd (N);
7833 Op2 : constant Node_Id := Right_Opnd (N);
7834 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7837 Binary_Op_Validity_Checks (N);
7839 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7840 -- means we no longer have a comparison operation, we are all done.
7842 Expand_Compare_Minimize_Eliminate_Overflow (N);
7844 if Nkind (N) /= N_Op_Le then
7848 -- Deal with array type operands
7850 if Is_Array_Type (Typ1) then
7851 Expand_Array_Comparison (N);
7855 -- Deal with Boolean type operands
7857 if Is_Boolean_Type (Typ1) then
7858 Adjust_Condition (Op1);
7859 Adjust_Condition (Op2);
7860 Set_Etype (N, Standard_Boolean);
7861 Adjust_Result_Type (N, Typ);
7864 Rewrite_Comparison (N);
7866 -- If we still have comparison, and Vax_Float type, process it
7868 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7869 Expand_Vax_Comparison (N);
7873 Optimize_Length_Comparison (N);
7876 --------------------
7877 -- Expand_N_Op_Lt --
7878 --------------------
7880 procedure Expand_N_Op_Lt (N : Node_Id) is
7881 Typ : constant Entity_Id := Etype (N);
7882 Op1 : constant Node_Id := Left_Opnd (N);
7883 Op2 : constant Node_Id := Right_Opnd (N);
7884 Typ1 : constant Entity_Id := Base_Type (Etype (Op1));
7887 Binary_Op_Validity_Checks (N);
7889 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if that
7890 -- means we no longer have a comparison operation, we are all done.
7892 Expand_Compare_Minimize_Eliminate_Overflow (N);
7894 if Nkind (N) /= N_Op_Lt then
7898 -- Deal with array type operands
7900 if Is_Array_Type (Typ1) then
7901 Expand_Array_Comparison (N);
7905 -- Deal with Boolean type operands
7907 if Is_Boolean_Type (Typ1) then
7908 Adjust_Condition (Op1);
7909 Adjust_Condition (Op2);
7910 Set_Etype (N, Standard_Boolean);
7911 Adjust_Result_Type (N, Typ);
7914 Rewrite_Comparison (N);
7916 -- If we still have comparison, and Vax_Float type, process it
7918 if Vax_Float (Typ1) and then Nkind (N) in N_Op_Compare then
7919 Expand_Vax_Comparison (N);
7923 Optimize_Length_Comparison (N);
7926 -----------------------
7927 -- Expand_N_Op_Minus --
7928 -----------------------
7930 procedure Expand_N_Op_Minus (N : Node_Id) is
7931 Loc : constant Source_Ptr := Sloc (N);
7932 Typ : constant Entity_Id := Etype (N);
7935 Unary_Op_Validity_Checks (N);
7937 -- Check for MINIMIZED/ELIMINATED overflow mode
7939 if Minimized_Eliminated_Overflow_Check (N) then
7940 Apply_Arithmetic_Overflow_Check (N);
7944 if not Backend_Overflow_Checks_On_Target
7945 and then Is_Signed_Integer_Type (Etype (N))
7946 and then Do_Overflow_Check (N)
7948 -- Software overflow checking expands -expr into (0 - expr)
7951 Make_Op_Subtract (Loc,
7952 Left_Opnd => Make_Integer_Literal (Loc, 0),
7953 Right_Opnd => Right_Opnd (N)));
7955 Analyze_And_Resolve (N, Typ);
7957 -- Vax floating-point types case
7959 elsif Vax_Float (Etype (N)) then
7960 Expand_Vax_Arith (N);
7962 end Expand_N_Op_Minus;
7964 ---------------------
7965 -- Expand_N_Op_Mod --
7966 ---------------------
7968 procedure Expand_N_Op_Mod (N : Node_Id) is
7969 Loc : constant Source_Ptr := Sloc (N);
7970 Typ : constant Entity_Id := Etype (N);
7971 DDC : constant Boolean := Do_Division_Check (N);
7984 pragma Warnings (Off, Lhi);
7987 Binary_Op_Validity_Checks (N);
7989 -- Check for MINIMIZED/ELIMINATED overflow mode
7991 if Minimized_Eliminated_Overflow_Check (N) then
7992 Apply_Arithmetic_Overflow_Check (N);
7996 if Is_Integer_Type (Etype (N)) then
7997 Apply_Divide_Checks (N);
7999 -- All done if we don't have a MOD any more, which can happen as a
8000 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8002 if Nkind (N) /= N_Op_Mod then
8007 -- Proceed with expansion of mod operator
8009 Left := Left_Opnd (N);
8010 Right := Right_Opnd (N);
8012 Determine_Range (Right, ROK, Rlo, Rhi, Assume_Valid => True);
8013 Determine_Range (Left, LOK, Llo, Lhi, Assume_Valid => True);
8015 -- Convert mod to rem if operands are both known to be non-negative, or
8016 -- both known to be non-positive (these are the cases in which rem and
8017 -- mod are the same, see (RM 4.5.5(28-30)). We do this since it is quite
8018 -- likely that this will improve the quality of code, (the operation now
8019 -- corresponds to the hardware remainder), and it does not seem likely
8020 -- that it could be harmful. It also avoids some cases of the elaborate
8021 -- expansion in Modify_Tree_For_C mode below (since Ada rem = C %).
8024 and then ((Llo >= 0 and then Rlo >= 0)
8026 (Lhi <= 0 and then Rhi <= 0))
8029 Make_Op_Rem (Sloc (N),
8030 Left_Opnd => Left_Opnd (N),
8031 Right_Opnd => Right_Opnd (N)));
8033 -- Instead of reanalyzing the node we do the analysis manually. This
8034 -- avoids anomalies when the replacement is done in an instance and
8035 -- is epsilon more efficient.
8037 Set_Entity (N, Standard_Entity (S_Op_Rem));
8039 Set_Do_Division_Check (N, DDC);
8040 Expand_N_Op_Rem (N);
8044 -- Otherwise, normal mod processing
8047 -- Apply optimization x mod 1 = 0. We don't really need that with
8048 -- gcc, but it is useful with other back ends (e.g. AAMP), and is
8049 -- certainly harmless.
8051 if Is_Integer_Type (Etype (N))
8052 and then Compile_Time_Known_Value (Right)
8053 and then Expr_Value (Right) = Uint_1
8055 -- Call Remove_Side_Effects to ensure that any side effects in
8056 -- the ignored left operand (in particular function calls to
8057 -- user defined functions) are properly executed.
8059 Remove_Side_Effects (Left);
8061 Rewrite (N, Make_Integer_Literal (Loc, 0));
8062 Analyze_And_Resolve (N, Typ);
8066 -- If we still have a mod operator and we are in Modify_Tree_For_C
8067 -- mode, and we have a signed integer type, then here is where we do
8068 -- the rewrite in terms of Rem. Note this rewrite bypasses the need
8069 -- for the special handling of the annoying case of largest negative
8070 -- number mod minus one.
8072 if Nkind (N) = N_Op_Mod
8073 and then Is_Signed_Integer_Type (Typ)
8074 and then Modify_Tree_For_C
8076 -- In the general case, we expand A mod B as
8078 -- Tnn : constant typ := A rem B;
8080 -- (if (A >= 0) = (B >= 0) then Tnn
8081 -- elsif Tnn = 0 then 0
8084 -- The comparison can be written simply as A >= 0 if we know that
8085 -- B >= 0 which is a very common case.
8087 -- An important optimization is when B is known at compile time
8088 -- to be 2**K for some constant. In this case we can simply AND
8089 -- the left operand with the bit string 2**K-1 (i.e. K 1-bits)
8090 -- and that works for both the positive and negative cases.
8093 P2 : constant Nat := Power_Of_Two (Right);
8098 Unchecked_Convert_To (Typ,
8101 Unchecked_Convert_To
8102 (Corresponding_Unsigned_Type (Typ), Left),
8104 Make_Integer_Literal (Loc, 2 ** P2 - 1))));
8105 Analyze_And_Resolve (N, Typ);
8110 -- Here for the full rewrite
8113 Tnn : constant Entity_Id := Make_Temporary (Sloc (N), 'T', N);
8119 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
8120 Right_Opnd => Make_Integer_Literal (Loc, 0));
8122 if not LOK or else Rlo < 0 then
8128 Left_Opnd => Duplicate_Subexpr_No_Checks (Right),
8129 Right_Opnd => Make_Integer_Literal (Loc, 0)));
8133 Make_Object_Declaration (Loc,
8134 Defining_Identifier => Tnn,
8135 Constant_Present => True,
8136 Object_Definition => New_Occurrence_Of (Typ, Loc),
8140 Right_Opnd => Right)));
8143 Make_If_Expression (Loc,
8144 Expressions => New_List (
8146 New_Occurrence_Of (Tnn, Loc),
8147 Make_If_Expression (Loc,
8149 Expressions => New_List (
8151 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8152 Right_Opnd => Make_Integer_Literal (Loc, 0)),
8153 Make_Integer_Literal (Loc, 0),
8155 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
8157 Duplicate_Subexpr_No_Checks (Right)))))));
8159 Analyze_And_Resolve (N, Typ);
8164 -- Deal with annoying case of largest negative number mod minus one.
8165 -- Gigi may not handle this case correctly, because on some targets,
8166 -- the mod value is computed using a divide instruction which gives
8167 -- an overflow trap for this case.
8169 -- It would be a bit more efficient to figure out which targets
8170 -- this is really needed for, but in practice it is reasonable
8171 -- to do the following special check in all cases, since it means
8172 -- we get a clearer message, and also the overhead is minimal given
8173 -- that division is expensive in any case.
8175 -- In fact the check is quite easy, if the right operand is -1, then
8176 -- the mod value is always 0, and we can just ignore the left operand
8177 -- completely in this case.
8179 -- This only applies if we still have a mod operator. Skip if we
8180 -- have already rewritten this (e.g. in the case of eliminated
8181 -- overflow checks which have driven us into bignum mode).
8183 if Nkind (N) = N_Op_Mod then
8185 -- The operand type may be private (e.g. in the expansion of an
8186 -- intrinsic operation) so we must use the underlying type to get
8187 -- the bounds, and convert the literals explicitly.
8191 (Type_Low_Bound (Base_Type (Underlying_Type (Etype (Left)))));
8193 if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
8194 and then ((not LOK) or else (Llo = LLB))
8197 Make_If_Expression (Loc,
8198 Expressions => New_List (
8200 Left_Opnd => Duplicate_Subexpr (Right),
8202 Unchecked_Convert_To (Typ,
8203 Make_Integer_Literal (Loc, -1))),
8204 Unchecked_Convert_To (Typ,
8205 Make_Integer_Literal (Loc, Uint_0)),
8206 Relocate_Node (N))));
8208 Set_Analyzed (Next (Next (First (Expressions (N)))));
8209 Analyze_And_Resolve (N, Typ);
8213 end Expand_N_Op_Mod;
8215 --------------------------
8216 -- Expand_N_Op_Multiply --
8217 --------------------------
8219 procedure Expand_N_Op_Multiply (N : Node_Id) is
8220 Loc : constant Source_Ptr := Sloc (N);
8221 Lop : constant Node_Id := Left_Opnd (N);
8222 Rop : constant Node_Id := Right_Opnd (N);
8224 Lp2 : constant Boolean :=
8225 Nkind (Lop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Lop);
8226 Rp2 : constant Boolean :=
8227 Nkind (Rop) = N_Op_Expon and then Is_Power_Of_2_For_Shift (Rop);
8229 Ltyp : constant Entity_Id := Etype (Lop);
8230 Rtyp : constant Entity_Id := Etype (Rop);
8231 Typ : Entity_Id := Etype (N);
8234 Binary_Op_Validity_Checks (N);
8236 -- Check for MINIMIZED/ELIMINATED overflow mode
8238 if Minimized_Eliminated_Overflow_Check (N) then
8239 Apply_Arithmetic_Overflow_Check (N);
8243 -- Special optimizations for integer types
8245 if Is_Integer_Type (Typ) then
8247 -- N * 0 = 0 for integer types
8249 if Compile_Time_Known_Value (Rop)
8250 and then Expr_Value (Rop) = Uint_0
8252 -- Call Remove_Side_Effects to ensure that any side effects in
8253 -- the ignored left operand (in particular function calls to
8254 -- user defined functions) are properly executed.
8256 Remove_Side_Effects (Lop);
8258 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8259 Analyze_And_Resolve (N, Typ);
8263 -- Similar handling for 0 * N = 0
8265 if Compile_Time_Known_Value (Lop)
8266 and then Expr_Value (Lop) = Uint_0
8268 Remove_Side_Effects (Rop);
8269 Rewrite (N, Make_Integer_Literal (Loc, Uint_0));
8270 Analyze_And_Resolve (N, Typ);
8274 -- N * 1 = 1 * N = N for integer types
8276 -- This optimisation is not done if we are going to
8277 -- rewrite the product 1 * 2 ** N to a shift.
8279 if Compile_Time_Known_Value (Rop)
8280 and then Expr_Value (Rop) = Uint_1
8286 elsif Compile_Time_Known_Value (Lop)
8287 and then Expr_Value (Lop) = Uint_1
8295 -- Convert x * 2 ** y to Shift_Left (x, y). Note that the fact that
8296 -- Is_Power_Of_2_For_Shift is set means that we know that our left
8297 -- operand is an integer, as required for this to work.
8302 -- Convert 2 ** A * 2 ** B into 2 ** (A + B)
8306 Left_Opnd => Make_Integer_Literal (Loc, 2),
8309 Left_Opnd => Right_Opnd (Lop),
8310 Right_Opnd => Right_Opnd (Rop))));
8311 Analyze_And_Resolve (N, Typ);
8315 -- If the result is modular, perform the reduction of the result
8318 if Is_Modular_Integer_Type (Typ)
8319 and then not Non_Binary_Modulus (Typ)
8324 Make_Op_Shift_Left (Loc,
8327 Convert_To (Standard_Natural, Right_Opnd (Rop))),
8329 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8333 Make_Op_Shift_Left (Loc,
8336 Convert_To (Standard_Natural, Right_Opnd (Rop))));
8339 Analyze_And_Resolve (N, Typ);
8343 -- Same processing for the operands the other way round
8346 if Is_Modular_Integer_Type (Typ)
8347 and then not Non_Binary_Modulus (Typ)
8352 Make_Op_Shift_Left (Loc,
8355 Convert_To (Standard_Natural, Right_Opnd (Lop))),
8357 Make_Integer_Literal (Loc, Modulus (Typ) - 1)));
8361 Make_Op_Shift_Left (Loc,
8364 Convert_To (Standard_Natural, Right_Opnd (Lop))));
8367 Analyze_And_Resolve (N, Typ);
8371 -- Do required fixup of universal fixed operation
8373 if Typ = Universal_Fixed then
8374 Fixup_Universal_Fixed_Operation (N);
8378 -- Multiplications with fixed-point results
8380 if Is_Fixed_Point_Type (Typ) then
8382 -- No special processing if Treat_Fixed_As_Integer is set, since from
8383 -- a semantic point of view such operations are simply integer
8384 -- operations and will be treated that way.
8386 if not Treat_Fixed_As_Integer (N) then
8388 -- Case of fixed * integer => fixed
8390 if Is_Integer_Type (Rtyp) then
8391 Expand_Multiply_Fixed_By_Integer_Giving_Fixed (N);
8393 -- Case of integer * fixed => fixed
8395 elsif Is_Integer_Type (Ltyp) then
8396 Expand_Multiply_Integer_By_Fixed_Giving_Fixed (N);
8398 -- Case of fixed * fixed => fixed
8401 Expand_Multiply_Fixed_By_Fixed_Giving_Fixed (N);
8405 -- Other cases of multiplication of fixed-point operands. Again we
8406 -- exclude the cases where Treat_Fixed_As_Integer flag is set.
8408 elsif (Is_Fixed_Point_Type (Ltyp) or else Is_Fixed_Point_Type (Rtyp))
8409 and then not Treat_Fixed_As_Integer (N)
8411 if Is_Integer_Type (Typ) then
8412 Expand_Multiply_Fixed_By_Fixed_Giving_Integer (N);
8414 pragma Assert (Is_Floating_Point_Type (Typ));
8415 Expand_Multiply_Fixed_By_Fixed_Giving_Float (N);
8418 -- Mixed-mode operations can appear in a non-static universal context,
8419 -- in which case the integer argument must be converted explicitly.
8421 elsif Typ = Universal_Real and then Is_Integer_Type (Rtyp) then
8422 Rewrite (Rop, Convert_To (Universal_Real, Relocate_Node (Rop)));
8423 Analyze_And_Resolve (Rop, Universal_Real);
8425 elsif Typ = Universal_Real and then Is_Integer_Type (Ltyp) then
8426 Rewrite (Lop, Convert_To (Universal_Real, Relocate_Node (Lop)));
8427 Analyze_And_Resolve (Lop, Universal_Real);
8429 -- Non-fixed point cases, check software overflow checking required
8431 elsif Is_Signed_Integer_Type (Etype (N)) then
8432 Apply_Arithmetic_Overflow_Check (N);
8434 -- Deal with VAX float case
8436 elsif Vax_Float (Typ) then
8437 Expand_Vax_Arith (N);
8440 end Expand_N_Op_Multiply;
8442 --------------------
8443 -- Expand_N_Op_Ne --
8444 --------------------
8446 procedure Expand_N_Op_Ne (N : Node_Id) is
8447 Typ : constant Entity_Id := Etype (Left_Opnd (N));
8450 -- Case of elementary type with standard operator
8452 if Is_Elementary_Type (Typ)
8453 and then Sloc (Entity (N)) = Standard_Location
8455 Binary_Op_Validity_Checks (N);
8457 -- Deal with overflow checks in MINIMIZED/ELIMINATED mode and if
8458 -- means we no longer have a /= operation, we are all done.
8460 Expand_Compare_Minimize_Eliminate_Overflow (N);
8462 if Nkind (N) /= N_Op_Ne then
8466 -- Boolean types (requiring handling of non-standard case)
8468 if Is_Boolean_Type (Typ) then
8469 Adjust_Condition (Left_Opnd (N));
8470 Adjust_Condition (Right_Opnd (N));
8471 Set_Etype (N, Standard_Boolean);
8472 Adjust_Result_Type (N, Typ);
8475 Rewrite_Comparison (N);
8477 -- If we still have comparison for Vax_Float, process it
8479 if Vax_Float (Typ) and then Nkind (N) in N_Op_Compare then
8480 Expand_Vax_Comparison (N);
8484 -- For all cases other than elementary types, we rewrite node as the
8485 -- negation of an equality operation, and reanalyze. The equality to be
8486 -- used is defined in the same scope and has the same signature. This
8487 -- signature must be set explicitly since in an instance it may not have
8488 -- the same visibility as in the generic unit. This avoids duplicating
8489 -- or factoring the complex code for record/array equality tests etc.
8493 Loc : constant Source_Ptr := Sloc (N);
8495 Ne : constant Entity_Id := Entity (N);
8498 Binary_Op_Validity_Checks (N);
8504 Left_Opnd => Left_Opnd (N),
8505 Right_Opnd => Right_Opnd (N)));
8506 Set_Paren_Count (Right_Opnd (Neg), 1);
8508 if Scope (Ne) /= Standard_Standard then
8509 Set_Entity (Right_Opnd (Neg), Corresponding_Equality (Ne));
8512 -- For navigation purposes, we want to treat the inequality as an
8513 -- implicit reference to the corresponding equality. Preserve the
8514 -- Comes_From_ source flag to generate proper Xref entries.
8516 Preserve_Comes_From_Source (Neg, N);
8517 Preserve_Comes_From_Source (Right_Opnd (Neg), N);
8519 Analyze_And_Resolve (N, Standard_Boolean);
8523 Optimize_Length_Comparison (N);
8526 ---------------------
8527 -- Expand_N_Op_Not --
8528 ---------------------
8530 -- If the argument is other than a Boolean array type, there is no special
8531 -- expansion required, except for VMS operations on signed integers.
8533 -- For the packed case, we call the special routine in Exp_Pakd, except
8534 -- that if the component size is greater than one, we use the standard
8535 -- routine generating a gruesome loop (it is so peculiar to have packed
8536 -- arrays with non-standard Boolean representations anyway, so it does not
8537 -- matter that we do not handle this case efficiently).
8539 -- For the unpacked case (and for the special packed case where we have non
8540 -- standard Booleans, as discussed above), we generate and insert into the
8541 -- tree the following function definition:
8543 -- function Nnnn (A : arr) is
8546 -- for J in a'range loop
8547 -- B (J) := not A (J);
8552 -- Here arr is the actual subtype of the parameter (and hence always
8553 -- constrained). Then we replace the not with a call to this function.
8555 procedure Expand_N_Op_Not (N : Node_Id) is
8556 Loc : constant Source_Ptr := Sloc (N);
8557 Typ : constant Entity_Id := Etype (N);
8566 Func_Name : Entity_Id;
8567 Loop_Statement : Node_Id;
8570 Unary_Op_Validity_Checks (N);
8572 -- For boolean operand, deal with non-standard booleans
8574 if Is_Boolean_Type (Typ) then
8575 Adjust_Condition (Right_Opnd (N));
8576 Set_Etype (N, Standard_Boolean);
8577 Adjust_Result_Type (N, Typ);
8581 -- For the VMS "not" on signed integer types, use conversion to and from
8582 -- a predefined modular type.
8584 if Is_VMS_Operator (Entity (N)) then
8590 -- If this is a derived type, retrieve original VMS type so that
8591 -- the proper sized type is used for intermediate values.
8593 if Is_Derived_Type (Typ) then
8594 Rtyp := First_Subtype (Etype (Typ));
8599 -- The proper unsigned type must have a size compatible with the
8600 -- operand, to prevent misalignment.
8602 if RM_Size (Rtyp) <= 8 then
8603 Utyp := RTE (RE_Unsigned_8);
8605 elsif RM_Size (Rtyp) <= 16 then
8606 Utyp := RTE (RE_Unsigned_16);
8608 elsif RM_Size (Rtyp) = RM_Size (Standard_Unsigned) then
8609 Utyp := RTE (RE_Unsigned_32);
8612 Utyp := RTE (RE_Long_Long_Unsigned);
8616 Unchecked_Convert_To (Typ,
8618 Unchecked_Convert_To (Utyp, Right_Opnd (N)))));
8619 Analyze_And_Resolve (N, Typ);
8624 -- Only array types need any other processing
8626 if not Is_Array_Type (Typ) then
8630 -- Case of array operand. If bit packed with a component size of 1,
8631 -- handle it in Exp_Pakd if the operand is known to be aligned.
8633 if Is_Bit_Packed_Array (Typ)
8634 and then Component_Size (Typ) = 1
8635 and then not Is_Possibly_Unaligned_Object (Right_Opnd (N))
8637 Expand_Packed_Not (N);
8641 -- Case of array operand which is not bit-packed. If the context is
8642 -- a safe assignment, call in-place operation, If context is a larger
8643 -- boolean expression in the context of a safe assignment, expansion is
8644 -- done by enclosing operation.
8646 Opnd := Relocate_Node (Right_Opnd (N));
8647 Convert_To_Actual_Subtype (Opnd);
8648 Arr := Etype (Opnd);
8649 Ensure_Defined (Arr, N);
8650 Silly_Boolean_Array_Not_Test (N, Arr);
8652 if Nkind (Parent (N)) = N_Assignment_Statement then
8653 if Safe_In_Place_Array_Op (Name (Parent (N)), N, Empty) then
8654 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8657 -- Special case the negation of a binary operation
8659 elsif Nkind_In (Opnd, N_Op_And, N_Op_Or, N_Op_Xor)
8660 and then Safe_In_Place_Array_Op
8661 (Name (Parent (N)), Left_Opnd (Opnd), Right_Opnd (Opnd))
8663 Build_Boolean_Array_Proc_Call (Parent (N), Opnd, Empty);
8667 elsif Nkind (Parent (N)) in N_Binary_Op
8668 and then Nkind (Parent (Parent (N))) = N_Assignment_Statement
8671 Op1 : constant Node_Id := Left_Opnd (Parent (N));
8672 Op2 : constant Node_Id := Right_Opnd (Parent (N));
8673 Lhs : constant Node_Id := Name (Parent (Parent (N)));
8676 if Safe_In_Place_Array_Op (Lhs, Op1, Op2) then
8678 -- (not A) op (not B) can be reduced to a single call
8680 if N = Op1 and then Nkind (Op2) = N_Op_Not then
8683 elsif N = Op2 and then Nkind (Op1) = N_Op_Not then
8686 -- A xor (not B) can also be special-cased
8688 elsif N = Op2 and then Nkind (Parent (N)) = N_Op_Xor then
8695 A := Make_Defining_Identifier (Loc, Name_uA);
8696 B := Make_Defining_Identifier (Loc, Name_uB);
8697 J := Make_Defining_Identifier (Loc, Name_uJ);
8700 Make_Indexed_Component (Loc,
8701 Prefix => New_Occurrence_Of (A, Loc),
8702 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8705 Make_Indexed_Component (Loc,
8706 Prefix => New_Occurrence_Of (B, Loc),
8707 Expressions => New_List (New_Occurrence_Of (J, Loc)));
8710 Make_Implicit_Loop_Statement (N,
8711 Identifier => Empty,
8714 Make_Iteration_Scheme (Loc,
8715 Loop_Parameter_Specification =>
8716 Make_Loop_Parameter_Specification (Loc,
8717 Defining_Identifier => J,
8718 Discrete_Subtype_Definition =>
8719 Make_Attribute_Reference (Loc,
8720 Prefix => Make_Identifier (Loc, Chars (A)),
8721 Attribute_Name => Name_Range))),
8723 Statements => New_List (
8724 Make_Assignment_Statement (Loc,
8726 Expression => Make_Op_Not (Loc, A_J))));
8728 Func_Name := Make_Temporary (Loc, 'N');
8729 Set_Is_Inlined (Func_Name);
8732 Make_Subprogram_Body (Loc,
8734 Make_Function_Specification (Loc,
8735 Defining_Unit_Name => Func_Name,
8736 Parameter_Specifications => New_List (
8737 Make_Parameter_Specification (Loc,
8738 Defining_Identifier => A,
8739 Parameter_Type => New_Occurrence_Of (Typ, Loc))),
8740 Result_Definition => New_Occurrence_Of (Typ, Loc)),
8742 Declarations => New_List (
8743 Make_Object_Declaration (Loc,
8744 Defining_Identifier => B,
8745 Object_Definition => New_Occurrence_Of (Arr, Loc))),
8747 Handled_Statement_Sequence =>
8748 Make_Handled_Sequence_Of_Statements (Loc,
8749 Statements => New_List (
8751 Make_Simple_Return_Statement (Loc,
8752 Expression => Make_Identifier (Loc, Chars (B)))))));
8755 Make_Function_Call (Loc,
8756 Name => New_Occurrence_Of (Func_Name, Loc),
8757 Parameter_Associations => New_List (Opnd)));
8759 Analyze_And_Resolve (N, Typ);
8760 end Expand_N_Op_Not;
8762 --------------------
8763 -- Expand_N_Op_Or --
8764 --------------------
8766 procedure Expand_N_Op_Or (N : Node_Id) is
8767 Typ : constant Entity_Id := Etype (N);
8770 Binary_Op_Validity_Checks (N);
8772 if Is_Array_Type (Etype (N)) then
8773 Expand_Boolean_Operator (N);
8775 elsif Is_Boolean_Type (Etype (N)) then
8776 Adjust_Condition (Left_Opnd (N));
8777 Adjust_Condition (Right_Opnd (N));
8778 Set_Etype (N, Standard_Boolean);
8779 Adjust_Result_Type (N, Typ);
8781 elsif Is_Intrinsic_Subprogram (Entity (N)) then
8782 Expand_Intrinsic_Call (N, Entity (N));
8787 ----------------------
8788 -- Expand_N_Op_Plus --
8789 ----------------------
8791 procedure Expand_N_Op_Plus (N : Node_Id) is
8793 Unary_Op_Validity_Checks (N);
8795 -- Check for MINIMIZED/ELIMINATED overflow mode
8797 if Minimized_Eliminated_Overflow_Check (N) then
8798 Apply_Arithmetic_Overflow_Check (N);
8801 end Expand_N_Op_Plus;
8803 ---------------------
8804 -- Expand_N_Op_Rem --
8805 ---------------------
8807 procedure Expand_N_Op_Rem (N : Node_Id) is
8808 Loc : constant Source_Ptr := Sloc (N);
8809 Typ : constant Entity_Id := Etype (N);
8820 -- Set if corresponding operand can be negative
8822 pragma Unreferenced (Hi);
8825 Binary_Op_Validity_Checks (N);
8827 -- Check for MINIMIZED/ELIMINATED overflow mode
8829 if Minimized_Eliminated_Overflow_Check (N) then
8830 Apply_Arithmetic_Overflow_Check (N);
8834 if Is_Integer_Type (Etype (N)) then
8835 Apply_Divide_Checks (N);
8837 -- All done if we don't have a REM any more, which can happen as a
8838 -- result of overflow expansion in MINIMIZED or ELIMINATED modes.
8840 if Nkind (N) /= N_Op_Rem then
8845 -- Proceed with expansion of REM
8847 Left := Left_Opnd (N);
8848 Right := Right_Opnd (N);
8850 -- Apply optimization x rem 1 = 0. We don't really need that with gcc,
8851 -- but it is useful with other back ends (e.g. AAMP), and is certainly
8854 if Is_Integer_Type (Etype (N))
8855 and then Compile_Time_Known_Value (Right)
8856 and then Expr_Value (Right) = Uint_1
8858 -- Call Remove_Side_Effects to ensure that any side effects in the
8859 -- ignored left operand (in particular function calls to user defined
8860 -- functions) are properly executed.
8862 Remove_Side_Effects (Left);
8864 Rewrite (N, Make_Integer_Literal (Loc, 0));
8865 Analyze_And_Resolve (N, Typ);
8869 -- Deal with annoying case of largest negative number remainder minus
8870 -- one. Gigi may not handle this case correctly, because on some
8871 -- targets, the mod value is computed using a divide instruction
8872 -- which gives an overflow trap for this case.
8874 -- It would be a bit more efficient to figure out which targets this
8875 -- is really needed for, but in practice it is reasonable to do the
8876 -- following special check in all cases, since it means we get a clearer
8877 -- message, and also the overhead is minimal given that division is
8878 -- expensive in any case.
8880 -- In fact the check is quite easy, if the right operand is -1, then
8881 -- the remainder is always 0, and we can just ignore the left operand
8882 -- completely in this case.
8884 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
8885 Lneg := (not OK) or else Lo < 0;
8887 Determine_Range (Left, OK, Lo, Hi, Assume_Valid => True);
8888 Rneg := (not OK) or else Lo < 0;
8890 -- We won't mess with trying to find out if the left operand can really
8891 -- be the largest negative number (that's a pain in the case of private
8892 -- types and this is really marginal). We will just assume that we need
8893 -- the test if the left operand can be negative at all.
8895 if Lneg and Rneg then
8897 Make_If_Expression (Loc,
8898 Expressions => New_List (
8900 Left_Opnd => Duplicate_Subexpr (Right),
8902 Unchecked_Convert_To (Typ, Make_Integer_Literal (Loc, -1))),
8904 Unchecked_Convert_To (Typ,
8905 Make_Integer_Literal (Loc, Uint_0)),
8907 Relocate_Node (N))));
8909 Set_Analyzed (Next (Next (First (Expressions (N)))));
8910 Analyze_And_Resolve (N, Typ);
8912 end Expand_N_Op_Rem;
8914 -----------------------------
8915 -- Expand_N_Op_Rotate_Left --
8916 -----------------------------
8918 procedure Expand_N_Op_Rotate_Left (N : Node_Id) is
8920 Binary_Op_Validity_Checks (N);
8922 -- If we are in Modify_Tree_For_C mode, there is no rotate left in C,
8923 -- so we rewrite in terms of logical shifts
8925 -- Shift_Left (Num, Bits) or Shift_Right (num, Esize - Bits)
8927 -- where Bits is the shift count mod Esize (the mod operation here
8928 -- deals with ludicrous large shift counts, which are apparently OK).
8930 -- What about non-binary modulus ???
8933 Loc : constant Source_Ptr := Sloc (N);
8934 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8935 Typ : constant Entity_Id := Etype (N);
8938 if Modify_Tree_For_C then
8939 Rewrite (Right_Opnd (N),
8941 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8942 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8944 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8949 Make_Op_Shift_Left (Loc,
8950 Left_Opnd => Left_Opnd (N),
8951 Right_Opnd => Right_Opnd (N)),
8954 Make_Op_Shift_Right (Loc,
8955 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
8957 Make_Op_Subtract (Loc,
8958 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
8960 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
8962 Analyze_And_Resolve (N, Typ);
8965 end Expand_N_Op_Rotate_Left;
8967 ------------------------------
8968 -- Expand_N_Op_Rotate_Right --
8969 ------------------------------
8971 procedure Expand_N_Op_Rotate_Right (N : Node_Id) is
8973 Binary_Op_Validity_Checks (N);
8975 -- If we are in Modify_Tree_For_C mode, there is no rotate right in C,
8976 -- so we rewrite in terms of logical shifts
8978 -- Shift_Right (Num, Bits) or Shift_Left (num, Esize - Bits)
8980 -- where Bits is the shift count mod Esize (the mod operation here
8981 -- deals with ludicrous large shift counts, which are apparently OK).
8983 -- What about non-binary modulus ???
8986 Loc : constant Source_Ptr := Sloc (N);
8987 Rtp : constant Entity_Id := Etype (Right_Opnd (N));
8988 Typ : constant Entity_Id := Etype (N);
8991 Rewrite (Right_Opnd (N),
8993 Left_Opnd => Relocate_Node (Right_Opnd (N)),
8994 Right_Opnd => Make_Integer_Literal (Loc, Esize (Typ))));
8996 Analyze_And_Resolve (Right_Opnd (N), Rtp);
8998 if Modify_Tree_For_C then
9002 Make_Op_Shift_Right (Loc,
9003 Left_Opnd => Left_Opnd (N),
9004 Right_Opnd => Right_Opnd (N)),
9007 Make_Op_Shift_Left (Loc,
9008 Left_Opnd => Duplicate_Subexpr_No_Checks (Left_Opnd (N)),
9010 Make_Op_Subtract (Loc,
9011 Left_Opnd => Make_Integer_Literal (Loc, Esize (Typ)),
9013 Duplicate_Subexpr_No_Checks (Right_Opnd (N))))));
9015 Analyze_And_Resolve (N, Typ);
9018 end Expand_N_Op_Rotate_Right;
9020 ----------------------------
9021 -- Expand_N_Op_Shift_Left --
9022 ----------------------------
9024 -- Note: nothing in this routine depends on left as opposed to right shifts
9025 -- so we share the routine for expanding shift right operations.
9027 procedure Expand_N_Op_Shift_Left (N : Node_Id) is
9029 Binary_Op_Validity_Checks (N);
9031 -- If we are in Modify_Tree_For_C mode, then ensure that the right
9032 -- operand is not greater than the word size (since that would not
9033 -- be defined properly by the corresponding C shift operator).
9035 if Modify_Tree_For_C then
9037 Right : constant Node_Id := Right_Opnd (N);
9038 Loc : constant Source_Ptr := Sloc (Right);
9039 Typ : constant Entity_Id := Etype (N);
9040 Siz : constant Uint := Esize (Typ);
9047 if Compile_Time_Known_Value (Right) then
9048 if Expr_Value (Right) >= Siz then
9049 Rewrite (N, Make_Integer_Literal (Loc, 0));
9050 Analyze_And_Resolve (N, Typ);
9053 -- Not compile time known, find range
9056 Determine_Range (Right, OK, Lo, Hi, Assume_Valid => True);
9058 -- Nothing to do if known to be OK range, otherwise expand
9060 if not OK or else Hi >= Siz then
9062 -- Prevent recursion on copy of shift node
9064 Orig := Relocate_Node (N);
9065 Set_Analyzed (Orig);
9067 -- Now do the rewrite
9070 Make_If_Expression (Loc,
9071 Expressions => New_List (
9073 Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
9074 Right_Opnd => Make_Integer_Literal (Loc, Siz)),
9075 Make_Integer_Literal (Loc, 0),
9077 Analyze_And_Resolve (N, Typ);
9082 end Expand_N_Op_Shift_Left;
9084 -----------------------------
9085 -- Expand_N_Op_Shift_Right --
9086 -----------------------------
9088 procedure Expand_N_Op_Shift_Right (N : Node_Id) is
9090 -- Share shift left circuit
9092 Expand_N_Op_Shift_Left (N);
9093 end Expand_N_Op_Shift_Right;
9095 ----------------------------------------
9096 -- Expand_N_Op_Shift_Right_Arithmetic --
9097 ----------------------------------------
9099 procedure Expand_N_Op_Shift_Right_Arithmetic (N : Node_Id) is
9101 Binary_Op_Validity_Checks (N);
9103 -- If we are in Modify_Tree_For_C mode, there is no shift right
9104 -- arithmetic in C, so we rewrite in terms of logical shifts.
9106 -- Shift_Right (Num, Bits) or
9108 -- then not (Shift_Right (Mask, bits))
9111 -- Here Mask is all 1 bits (2**size - 1), and Sign is 2**(size - 1)
9113 -- Note: in almost all C compilers it would work to just shift a
9114 -- signed integer right, but it's undefined and we cannot rely on it.
9116 -- Note: the above works fine for shift counts greater than or equal
9117 -- to the word size, since in this case (not (Shift_Right (Mask, bits)))
9118 -- generates all 1'bits.
9120 -- What about non-binary modulus ???
9123 Loc : constant Source_Ptr := Sloc (N);
9124 Typ : constant Entity_Id := Etype (N);
9125 Sign : constant Uint := 2 ** (Esize (Typ) - 1);
9126 Mask : constant Uint := (2 ** Esize (Typ)) - 1;
9127 Left : constant Node_Id := Left_Opnd (N);
9128 Right : constant Node_Id := Right_Opnd (N);
9132 if Modify_Tree_For_C then
9134 -- Here if not (Shift_Right (Mask, bits)) can be computed at
9135 -- compile time as a single constant.
9137 if Compile_Time_Known_Value (Right) then
9139 Val : constant Uint := Expr_Value (Right);
9142 if Val >= Esize (Typ) then
9143 Maskx := Make_Integer_Literal (Loc, Mask);
9147 Make_Integer_Literal (Loc,
9148 Intval => Mask - (Mask / (2 ** Expr_Value (Right))));
9156 Make_Op_Shift_Right (Loc,
9157 Left_Opnd => Make_Integer_Literal (Loc, Mask),
9158 Right_Opnd => Duplicate_Subexpr_No_Checks (Right)));
9161 -- Now do the rewrite
9166 Make_Op_Shift_Right (Loc,
9168 Right_Opnd => Right),
9170 Make_If_Expression (Loc,
9171 Expressions => New_List (
9173 Left_Opnd => Duplicate_Subexpr_No_Checks (Left),
9174 Right_Opnd => Make_Integer_Literal (Loc, Sign)),
9176 Make_Integer_Literal (Loc, 0)))));
9177 Analyze_And_Resolve (N, Typ);
9180 end Expand_N_Op_Shift_Right_Arithmetic;
9182 --------------------------
9183 -- Expand_N_Op_Subtract --
9184 --------------------------
9186 procedure Expand_N_Op_Subtract (N : Node_Id) is
9187 Typ : constant Entity_Id := Etype (N);
9190 Binary_Op_Validity_Checks (N);
9192 -- Check for MINIMIZED/ELIMINATED overflow mode
9194 if Minimized_Eliminated_Overflow_Check (N) then
9195 Apply_Arithmetic_Overflow_Check (N);
9199 -- N - 0 = N for integer types
9201 if Is_Integer_Type (Typ)
9202 and then Compile_Time_Known_Value (Right_Opnd (N))
9203 and then Expr_Value (Right_Opnd (N)) = 0
9205 Rewrite (N, Left_Opnd (N));
9209 -- Arithmetic overflow checks for signed integer/fixed point types
9211 if Is_Signed_Integer_Type (Typ) or else Is_Fixed_Point_Type (Typ) then
9212 Apply_Arithmetic_Overflow_Check (N);
9214 -- VAX floating-point types case
9216 elsif Vax_Float (Typ) then
9217 Expand_Vax_Arith (N);
9219 end Expand_N_Op_Subtract;
9221 ---------------------
9222 -- Expand_N_Op_Xor --
9223 ---------------------
9225 procedure Expand_N_Op_Xor (N : Node_Id) is
9226 Typ : constant Entity_Id := Etype (N);
9229 Binary_Op_Validity_Checks (N);
9231 if Is_Array_Type (Etype (N)) then
9232 Expand_Boolean_Operator (N);
9234 elsif Is_Boolean_Type (Etype (N)) then
9235 Adjust_Condition (Left_Opnd (N));
9236 Adjust_Condition (Right_Opnd (N));
9237 Set_Etype (N, Standard_Boolean);
9238 Adjust_Result_Type (N, Typ);
9240 elsif Is_Intrinsic_Subprogram (Entity (N)) then
9241 Expand_Intrinsic_Call (N, Entity (N));
9244 end Expand_N_Op_Xor;
9246 ----------------------
9247 -- Expand_N_Or_Else --
9248 ----------------------
9250 procedure Expand_N_Or_Else (N : Node_Id)
9251 renames Expand_Short_Circuit_Operator;
9253 -----------------------------------
9254 -- Expand_N_Qualified_Expression --
9255 -----------------------------------
9257 procedure Expand_N_Qualified_Expression (N : Node_Id) is
9258 Operand : constant Node_Id := Expression (N);
9259 Target_Type : constant Entity_Id := Entity (Subtype_Mark (N));
9262 -- Do validity check if validity checking operands
9264 if Validity_Checks_On and Validity_Check_Operands then
9265 Ensure_Valid (Operand);
9268 -- Apply possible constraint check
9270 Apply_Constraint_Check (Operand, Target_Type, No_Sliding => True);
9272 if Do_Range_Check (Operand) then
9273 Set_Do_Range_Check (Operand, False);
9274 Generate_Range_Check (Operand, Target_Type, CE_Range_Check_Failed);
9276 end Expand_N_Qualified_Expression;
9278 ------------------------------------
9279 -- Expand_N_Quantified_Expression --
9280 ------------------------------------
9284 -- for all X in range => Cond
9289 -- for X in range loop
9296 -- Similarly, an existentially quantified expression:
9298 -- for some X in range => Cond
9303 -- for X in range loop
9310 -- In both cases, the iteration may be over a container in which case it is
9311 -- given by an iterator specification, not a loop parameter specification.
9313 procedure Expand_N_Quantified_Expression (N : Node_Id) is
9314 Actions : constant List_Id := New_List;
9315 For_All : constant Boolean := All_Present (N);
9316 Iter_Spec : constant Node_Id := Iterator_Specification (N);
9317 Loc : constant Source_Ptr := Sloc (N);
9318 Loop_Spec : constant Node_Id := Loop_Parameter_Specification (N);
9325 -- Create the declaration of the flag which tracks the status of the
9326 -- quantified expression. Generate:
9328 -- Flag : Boolean := (True | False);
9330 Flag := Make_Temporary (Loc, 'T', N);
9333 Make_Object_Declaration (Loc,
9334 Defining_Identifier => Flag,
9335 Object_Definition => New_Occurrence_Of (Standard_Boolean, Loc),
9337 New_Occurrence_Of (Boolean_Literals (For_All), Loc)));
9339 -- Construct the circuitry which tracks the status of the quantified
9340 -- expression. Generate:
9342 -- if [not] Cond then
9343 -- Flag := (False | True);
9347 Cond := Relocate_Node (Condition (N));
9350 Cond := Make_Op_Not (Loc, Cond);
9354 Make_Implicit_If_Statement (N,
9356 Then_Statements => New_List (
9357 Make_Assignment_Statement (Loc,
9358 Name => New_Occurrence_Of (Flag, Loc),
9360 New_Occurrence_Of (Boolean_Literals (not For_All), Loc)),
9361 Make_Exit_Statement (Loc))));
9363 -- Build the loop equivalent of the quantified expression
9365 if Present (Iter_Spec) then
9367 Make_Iteration_Scheme (Loc,
9368 Iterator_Specification => Iter_Spec);
9371 Make_Iteration_Scheme (Loc,
9372 Loop_Parameter_Specification => Loop_Spec);
9376 Make_Loop_Statement (Loc,
9377 Iteration_Scheme => Scheme,
9378 Statements => Stmts,
9379 End_Label => Empty));
9381 -- Transform the quantified expression
9384 Make_Expression_With_Actions (Loc,
9385 Expression => New_Occurrence_Of (Flag, Loc),
9386 Actions => Actions));
9387 Analyze_And_Resolve (N, Standard_Boolean);
9388 end Expand_N_Quantified_Expression;
9390 ---------------------------------
9391 -- Expand_N_Selected_Component --
9392 ---------------------------------
9394 procedure Expand_N_Selected_Component (N : Node_Id) is
9395 Loc : constant Source_Ptr := Sloc (N);
9396 Par : constant Node_Id := Parent (N);
9397 P : constant Node_Id := Prefix (N);
9398 S : constant Node_Id := Selector_Name (N);
9399 Ptyp : Entity_Id := Underlying_Type (Etype (P));
9405 function In_Left_Hand_Side (Comp : Node_Id) return Boolean;
9406 -- Gigi needs a temporary for prefixes that depend on a discriminant,
9407 -- unless the context of an assignment can provide size information.
9408 -- Don't we have a general routine that does this???
9410 function Is_Subtype_Declaration return Boolean;
9411 -- The replacement of a discriminant reference by its value is required
9412 -- if this is part of the initialization of an temporary generated by a
9413 -- change of representation. This shows up as the construction of a
9414 -- discriminant constraint for a subtype declared at the same point as
9415 -- the entity in the prefix of the selected component. We recognize this
9416 -- case when the context of the reference is:
9417 -- subtype ST is T(Obj.D);
9418 -- where the entity for Obj comes from source, and ST has the same sloc.
9420 -----------------------
9421 -- In_Left_Hand_Side --
9422 -----------------------
9424 function In_Left_Hand_Side (Comp : Node_Id) return Boolean is
9426 return (Nkind (Parent (Comp)) = N_Assignment_Statement
9427 and then Comp = Name (Parent (Comp)))
9428 or else (Present (Parent (Comp))
9429 and then Nkind (Parent (Comp)) in N_Subexpr
9430 and then In_Left_Hand_Side (Parent (Comp)));
9431 end In_Left_Hand_Side;
9433 -----------------------------
9434 -- Is_Subtype_Declaration --
9435 -----------------------------
9437 function Is_Subtype_Declaration return Boolean is
9438 Par : constant Node_Id := Parent (N);
9441 Nkind (Par) = N_Index_Or_Discriminant_Constraint
9442 and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
9443 and then Comes_From_Source (Entity (Prefix (N)))
9444 and then Sloc (Par) = Sloc (Entity (Prefix (N)));
9445 end Is_Subtype_Declaration;
9447 -- Start of processing for Expand_N_Selected_Component
9450 -- Insert explicit dereference if required
9452 if Is_Access_Type (Ptyp) then
9454 -- First set prefix type to proper access type, in case it currently
9455 -- has a private (non-access) view of this type.
9457 Set_Etype (P, Ptyp);
9459 Insert_Explicit_Dereference (P);
9460 Analyze_And_Resolve (P, Designated_Type (Ptyp));
9462 if Ekind (Etype (P)) = E_Private_Subtype
9463 and then Is_For_Access_Subtype (Etype (P))
9465 Set_Etype (P, Base_Type (Etype (P)));
9471 -- Deal with discriminant check required
9473 if Do_Discriminant_Check (N) then
9474 if Present (Discriminant_Checking_Func
9475 (Original_Record_Component (Entity (S))))
9477 -- Present the discriminant checking function to the backend, so
9478 -- that it can inline the call to the function.
9481 (Discriminant_Checking_Func
9482 (Original_Record_Component (Entity (S))));
9484 -- Now reset the flag and generate the call
9486 Set_Do_Discriminant_Check (N, False);
9487 Generate_Discriminant_Check (N);
9489 -- In the case of Unchecked_Union, no discriminant checking is
9490 -- actually performed.
9493 Set_Do_Discriminant_Check (N, False);
9497 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9498 -- function, then additional actuals must be passed.
9500 if Ada_Version >= Ada_2005
9501 and then Is_Build_In_Place_Function_Call (P)
9503 Make_Build_In_Place_Call_In_Anonymous_Context (P);
9506 -- Gigi cannot handle unchecked conversions that are the prefix of a
9507 -- selected component with discriminants. This must be checked during
9508 -- expansion, because during analysis the type of the selector is not
9509 -- known at the point the prefix is analyzed. If the conversion is the
9510 -- target of an assignment, then we cannot force the evaluation.
9512 if Nkind (Prefix (N)) = N_Unchecked_Type_Conversion
9513 and then Has_Discriminants (Etype (N))
9514 and then not In_Left_Hand_Side (N)
9516 Force_Evaluation (Prefix (N));
9519 -- Remaining processing applies only if selector is a discriminant
9521 if Ekind (Entity (Selector_Name (N))) = E_Discriminant then
9523 -- If the selector is a discriminant of a constrained record type,
9524 -- we may be able to rewrite the expression with the actual value
9525 -- of the discriminant, a useful optimization in some cases.
9527 if Is_Record_Type (Ptyp)
9528 and then Has_Discriminants (Ptyp)
9529 and then Is_Constrained (Ptyp)
9531 -- Do this optimization for discrete types only, and not for
9532 -- access types (access discriminants get us into trouble).
9534 if not Is_Discrete_Type (Etype (N)) then
9537 -- Don't do this on the left hand of an assignment statement.
9538 -- Normally one would think that references like this would not
9539 -- occur, but they do in generated code, and mean that we really
9540 -- do want to assign the discriminant.
9542 elsif Nkind (Par) = N_Assignment_Statement
9543 and then Name (Par) = N
9547 -- Don't do this optimization for the prefix of an attribute or
9548 -- the name of an object renaming declaration since these are
9549 -- contexts where we do not want the value anyway.
9551 elsif (Nkind (Par) = N_Attribute_Reference
9552 and then Prefix (Par) = N)
9553 or else Is_Renamed_Object (N)
9557 -- Don't do this optimization if we are within the code for a
9558 -- discriminant check, since the whole point of such a check may
9559 -- be to verify the condition on which the code below depends.
9561 elsif Is_In_Discriminant_Check (N) then
9564 -- Green light to see if we can do the optimization. There is
9565 -- still one condition that inhibits the optimization below but
9566 -- now is the time to check the particular discriminant.
9569 -- Loop through discriminants to find the matching discriminant
9570 -- constraint to see if we can copy it.
9572 Disc := First_Discriminant (Ptyp);
9573 Dcon := First_Elmt (Discriminant_Constraint (Ptyp));
9574 Discr_Loop : while Present (Dcon) loop
9575 Dval := Node (Dcon);
9577 -- Check if this is the matching discriminant and if the
9578 -- discriminant value is simple enough to make sense to
9579 -- copy. We don't want to copy complex expressions, and
9580 -- indeed to do so can cause trouble (before we put in
9581 -- this guard, a discriminant expression containing an
9582 -- AND THEN was copied, causing problems for coverage
9585 -- However, if the reference is part of the initialization
9586 -- code generated for an object declaration, we must use
9587 -- the discriminant value from the subtype constraint,
9588 -- because the selected component may be a reference to the
9589 -- object being initialized, whose discriminant is not yet
9590 -- set. This only happens in complex cases involving changes
9591 -- or representation.
9593 if Disc = Entity (Selector_Name (N))
9594 and then (Is_Entity_Name (Dval)
9595 or else Compile_Time_Known_Value (Dval)
9596 or else Is_Subtype_Declaration)
9598 -- Here we have the matching discriminant. Check for
9599 -- the case of a discriminant of a component that is
9600 -- constrained by an outer discriminant, which cannot
9601 -- be optimized away.
9603 if Denotes_Discriminant
9604 (Dval, Check_Concurrent => True)
9608 elsif Nkind (Original_Node (Dval)) = N_Selected_Component
9610 Denotes_Discriminant
9611 (Selector_Name (Original_Node (Dval)), True)
9615 -- Do not retrieve value if constraint is not static. It
9616 -- is generally not useful, and the constraint may be a
9617 -- rewritten outer discriminant in which case it is in
9620 elsif Is_Entity_Name (Dval)
9622 Nkind (Parent (Entity (Dval))) = N_Object_Declaration
9623 and then Present (Expression (Parent (Entity (Dval))))
9625 Is_Static_Expression
9626 (Expression (Parent (Entity (Dval))))
9630 -- In the context of a case statement, the expression may
9631 -- have the base type of the discriminant, and we need to
9632 -- preserve the constraint to avoid spurious errors on
9635 elsif Nkind (Parent (N)) = N_Case_Statement
9636 and then Etype (Dval) /= Etype (Disc)
9639 Make_Qualified_Expression (Loc,
9641 New_Occurrence_Of (Etype (Disc), Loc),
9643 New_Copy_Tree (Dval)));
9644 Analyze_And_Resolve (N, Etype (Disc));
9646 -- In case that comes out as a static expression,
9647 -- reset it (a selected component is never static).
9649 Set_Is_Static_Expression (N, False);
9652 -- Otherwise we can just copy the constraint, but the
9653 -- result is certainly not static. In some cases the
9654 -- discriminant constraint has been analyzed in the
9655 -- context of the original subtype indication, but for
9656 -- itypes the constraint might not have been analyzed
9657 -- yet, and this must be done now.
9660 Rewrite (N, New_Copy_Tree (Dval));
9661 Analyze_And_Resolve (N);
9662 Set_Is_Static_Expression (N, False);
9668 Next_Discriminant (Disc);
9669 end loop Discr_Loop;
9671 -- Note: the above loop should always find a matching
9672 -- discriminant, but if it does not, we just missed an
9673 -- optimization due to some glitch (perhaps a previous
9674 -- error), so ignore.
9679 -- The only remaining processing is in the case of a discriminant of
9680 -- a concurrent object, where we rewrite the prefix to denote the
9681 -- corresponding record type. If the type is derived and has renamed
9682 -- discriminants, use corresponding discriminant, which is the one
9683 -- that appears in the corresponding record.
9685 if not Is_Concurrent_Type (Ptyp) then
9689 Disc := Entity (Selector_Name (N));
9691 if Is_Derived_Type (Ptyp)
9692 and then Present (Corresponding_Discriminant (Disc))
9694 Disc := Corresponding_Discriminant (Disc);
9698 Make_Selected_Component (Loc,
9700 Unchecked_Convert_To (Corresponding_Record_Type (Ptyp),
9702 Selector_Name => Make_Identifier (Loc, Chars (Disc)));
9708 -- Set Atomic_Sync_Required if necessary for atomic component
9710 if Nkind (N) = N_Selected_Component then
9712 E : constant Entity_Id := Entity (Selector_Name (N));
9716 -- If component is atomic, but type is not, setting depends on
9717 -- disable/enable state for the component.
9719 if Is_Atomic (E) and then not Is_Atomic (Etype (E)) then
9720 Set := not Atomic_Synchronization_Disabled (E);
9722 -- If component is not atomic, but its type is atomic, setting
9723 -- depends on disable/enable state for the type.
9725 elsif not Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9726 Set := not Atomic_Synchronization_Disabled (Etype (E));
9728 -- If both component and type are atomic, we disable if either
9729 -- component or its type have sync disabled.
9731 elsif Is_Atomic (E) and then Is_Atomic (Etype (E)) then
9732 Set := (not Atomic_Synchronization_Disabled (E))
9734 (not Atomic_Synchronization_Disabled (Etype (E)));
9740 -- Set flag if required
9743 Activate_Atomic_Synchronization (N);
9747 end Expand_N_Selected_Component;
9749 --------------------
9750 -- Expand_N_Slice --
9751 --------------------
9753 procedure Expand_N_Slice (N : Node_Id) is
9754 Loc : constant Source_Ptr := Sloc (N);
9755 Typ : constant Entity_Id := Etype (N);
9757 function Is_Procedure_Actual (N : Node_Id) return Boolean;
9758 -- Check whether the argument is an actual for a procedure call, in
9759 -- which case the expansion of a bit-packed slice is deferred until the
9760 -- call itself is expanded. The reason this is required is that we might
9761 -- have an IN OUT or OUT parameter, and the copy out is essential, and
9762 -- that copy out would be missed if we created a temporary here in
9763 -- Expand_N_Slice. Note that we don't bother to test specifically for an
9764 -- IN OUT or OUT mode parameter, since it is a bit tricky to do, and it
9765 -- is harmless to defer expansion in the IN case, since the call
9766 -- processing will still generate the appropriate copy in operation,
9767 -- which will take care of the slice.
9769 procedure Make_Temporary_For_Slice;
9770 -- Create a named variable for the value of the slice, in cases where
9771 -- the back-end cannot handle it properly, e.g. when packed types or
9772 -- unaligned slices are involved.
9774 -------------------------
9775 -- Is_Procedure_Actual --
9776 -------------------------
9778 function Is_Procedure_Actual (N : Node_Id) return Boolean is
9779 Par : Node_Id := Parent (N);
9783 -- If our parent is a procedure call we can return
9785 if Nkind (Par) = N_Procedure_Call_Statement then
9788 -- If our parent is a type conversion, keep climbing the tree,
9789 -- since a type conversion can be a procedure actual. Also keep
9790 -- climbing if parameter association or a qualified expression,
9791 -- since these are additional cases that do can appear on
9792 -- procedure actuals.
9794 elsif Nkind_In (Par, N_Type_Conversion,
9795 N_Parameter_Association,
9796 N_Qualified_Expression)
9798 Par := Parent (Par);
9800 -- Any other case is not what we are looking for
9806 end Is_Procedure_Actual;
9808 ------------------------------
9809 -- Make_Temporary_For_Slice --
9810 ------------------------------
9812 procedure Make_Temporary_For_Slice is
9813 Ent : constant Entity_Id := Make_Temporary (Loc, 'T', N);
9818 Make_Object_Declaration (Loc,
9819 Defining_Identifier => Ent,
9820 Object_Definition => New_Occurrence_Of (Typ, Loc));
9822 Set_No_Initialization (Decl);
9824 Insert_Actions (N, New_List (
9826 Make_Assignment_Statement (Loc,
9827 Name => New_Occurrence_Of (Ent, Loc),
9828 Expression => Relocate_Node (N))));
9830 Rewrite (N, New_Occurrence_Of (Ent, Loc));
9831 Analyze_And_Resolve (N, Typ);
9832 end Make_Temporary_For_Slice;
9836 Pref : constant Node_Id := Prefix (N);
9837 Pref_Typ : Entity_Id := Etype (Pref);
9839 -- Start of processing for Expand_N_Slice
9842 -- Special handling for access types
9844 if Is_Access_Type (Pref_Typ) then
9845 Pref_Typ := Designated_Type (Pref_Typ);
9848 Make_Explicit_Dereference (Sloc (N),
9849 Prefix => Relocate_Node (Pref)));
9851 Analyze_And_Resolve (Pref, Pref_Typ);
9854 -- Ada 2005 (AI-318-02): If the prefix is a call to a build-in-place
9855 -- function, then additional actuals must be passed.
9857 if Ada_Version >= Ada_2005
9858 and then Is_Build_In_Place_Function_Call (Pref)
9860 Make_Build_In_Place_Call_In_Anonymous_Context (Pref);
9863 -- The remaining case to be handled is packed slices. We can leave
9864 -- packed slices as they are in the following situations:
9866 -- 1. Right or left side of an assignment (we can handle this
9867 -- situation correctly in the assignment statement expansion).
9869 -- 2. Prefix of indexed component (the slide is optimized away in this
9870 -- case, see the start of Expand_N_Slice.)
9872 -- 3. Object renaming declaration, since we want the name of the
9873 -- slice, not the value.
9875 -- 4. Argument to procedure call, since copy-in/copy-out handling may
9876 -- be required, and this is handled in the expansion of call
9879 -- 5. Prefix of an address attribute (this is an error which is caught
9880 -- elsewhere, and the expansion would interfere with generating the
9883 if not Is_Packed (Typ) then
9885 -- Apply transformation for actuals of a function call, where
9886 -- Expand_Actuals is not used.
9888 if Nkind (Parent (N)) = N_Function_Call
9889 and then Is_Possibly_Unaligned_Slice (N)
9891 Make_Temporary_For_Slice;
9894 elsif Nkind (Parent (N)) = N_Assignment_Statement
9895 or else (Nkind (Parent (Parent (N))) = N_Assignment_Statement
9896 and then Parent (N) = Name (Parent (Parent (N))))
9900 elsif Nkind (Parent (N)) = N_Indexed_Component
9901 or else Is_Renamed_Object (N)
9902 or else Is_Procedure_Actual (N)
9906 elsif Nkind (Parent (N)) = N_Attribute_Reference
9907 and then Attribute_Name (Parent (N)) = Name_Address
9912 Make_Temporary_For_Slice;
9916 ------------------------------
9917 -- Expand_N_Type_Conversion --
9918 ------------------------------
9920 procedure Expand_N_Type_Conversion (N : Node_Id) is
9921 Loc : constant Source_Ptr := Sloc (N);
9922 Operand : constant Node_Id := Expression (N);
9923 Target_Type : constant Entity_Id := Etype (N);
9924 Operand_Type : Entity_Id := Etype (Operand);
9926 procedure Handle_Changed_Representation;
9927 -- This is called in the case of record and array type conversions to
9928 -- see if there is a change of representation to be handled. Change of
9929 -- representation is actually handled at the assignment statement level,
9930 -- and what this procedure does is rewrite node N conversion as an
9931 -- assignment to temporary. If there is no change of representation,
9932 -- then the conversion node is unchanged.
9934 procedure Raise_Accessibility_Error;
9935 -- Called when we know that an accessibility check will fail. Rewrites
9936 -- node N to an appropriate raise statement and outputs warning msgs.
9937 -- The Etype of the raise node is set to Target_Type.
9939 procedure Real_Range_Check;
9940 -- Handles generation of range check for real target value
9942 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean;
9943 -- True iff Present (Effective_Extra_Accessibility (Id)) successfully
9944 -- evaluates to True.
9946 -----------------------------------
9947 -- Handle_Changed_Representation --
9948 -----------------------------------
9950 procedure Handle_Changed_Representation is
9959 -- Nothing else to do if no change of representation
9961 if Same_Representation (Operand_Type, Target_Type) then
9964 -- The real change of representation work is done by the assignment
9965 -- statement processing. So if this type conversion is appearing as
9966 -- the expression of an assignment statement, nothing needs to be
9967 -- done to the conversion.
9969 elsif Nkind (Parent (N)) = N_Assignment_Statement then
9972 -- Otherwise we need to generate a temporary variable, and do the
9973 -- change of representation assignment into that temporary variable.
9974 -- The conversion is then replaced by a reference to this variable.
9979 -- If type is unconstrained we have to add a constraint, copied
9980 -- from the actual value of the left hand side.
9982 if not Is_Constrained (Target_Type) then
9983 if Has_Discriminants (Operand_Type) then
9984 Disc := First_Discriminant (Operand_Type);
9986 if Disc /= First_Stored_Discriminant (Operand_Type) then
9987 Disc := First_Stored_Discriminant (Operand_Type);
9991 while Present (Disc) loop
9993 Make_Selected_Component (Loc,
9995 Duplicate_Subexpr_Move_Checks (Operand),
9997 Make_Identifier (Loc, Chars (Disc))));
9998 Next_Discriminant (Disc);
10001 elsif Is_Array_Type (Operand_Type) then
10002 N_Ix := First_Index (Target_Type);
10005 for J in 1 .. Number_Dimensions (Operand_Type) loop
10007 -- We convert the bounds explicitly. We use an unchecked
10008 -- conversion because bounds checks are done elsewhere.
10013 Unchecked_Convert_To (Etype (N_Ix),
10014 Make_Attribute_Reference (Loc,
10016 Duplicate_Subexpr_No_Checks
10017 (Operand, Name_Req => True),
10018 Attribute_Name => Name_First,
10019 Expressions => New_List (
10020 Make_Integer_Literal (Loc, J)))),
10023 Unchecked_Convert_To (Etype (N_Ix),
10024 Make_Attribute_Reference (Loc,
10026 Duplicate_Subexpr_No_Checks
10027 (Operand, Name_Req => True),
10028 Attribute_Name => Name_Last,
10029 Expressions => New_List (
10030 Make_Integer_Literal (Loc, J))))));
10037 Odef := New_Occurrence_Of (Target_Type, Loc);
10039 if Present (Cons) then
10041 Make_Subtype_Indication (Loc,
10042 Subtype_Mark => Odef,
10044 Make_Index_Or_Discriminant_Constraint (Loc,
10045 Constraints => Cons));
10048 Temp := Make_Temporary (Loc, 'C');
10050 Make_Object_Declaration (Loc,
10051 Defining_Identifier => Temp,
10052 Object_Definition => Odef);
10054 Set_No_Initialization (Decl, True);
10056 -- Insert required actions. It is essential to suppress checks
10057 -- since we have suppressed default initialization, which means
10058 -- that the variable we create may have no discriminants.
10063 Make_Assignment_Statement (Loc,
10064 Name => New_Occurrence_Of (Temp, Loc),
10065 Expression => Relocate_Node (N))),
10066 Suppress => All_Checks);
10068 Rewrite (N, New_Occurrence_Of (Temp, Loc));
10071 end Handle_Changed_Representation;
10073 -------------------------------
10074 -- Raise_Accessibility_Error --
10075 -------------------------------
10077 procedure Raise_Accessibility_Error is
10079 Error_Msg_Warn := SPARK_Mode /= On;
10081 Make_Raise_Program_Error (Sloc (N),
10082 Reason => PE_Accessibility_Check_Failed));
10083 Set_Etype (N, Target_Type);
10085 Error_Msg_N ("<<accessibility check failure", N);
10086 Error_Msg_NE ("\<<& [", N, Standard_Program_Error);
10087 end Raise_Accessibility_Error;
10089 ----------------------
10090 -- Real_Range_Check --
10091 ----------------------
10093 -- Case of conversions to floating-point or fixed-point. If range checks
10094 -- are enabled and the target type has a range constraint, we convert:
10100 -- Tnn : typ'Base := typ'Base (x);
10101 -- [constraint_error when Tnn < typ'First or else Tnn > typ'Last]
10104 -- This is necessary when there is a conversion of integer to float or
10105 -- to fixed-point to ensure that the correct checks are made. It is not
10106 -- necessary for float to float where it is enough to simply set the
10107 -- Do_Range_Check flag.
10109 procedure Real_Range_Check is
10110 Btyp : constant Entity_Id := Base_Type (Target_Type);
10111 Lo : constant Node_Id := Type_Low_Bound (Target_Type);
10112 Hi : constant Node_Id := Type_High_Bound (Target_Type);
10113 Xtyp : constant Entity_Id := Etype (Operand);
10118 -- Nothing to do if conversion was rewritten
10120 if Nkind (N) /= N_Type_Conversion then
10124 -- Nothing to do if range checks suppressed, or target has the same
10125 -- range as the base type (or is the base type).
10127 if Range_Checks_Suppressed (Target_Type)
10128 or else (Lo = Type_Low_Bound (Btyp)
10130 Hi = Type_High_Bound (Btyp))
10135 -- Nothing to do if expression is an entity on which checks have been
10138 if Is_Entity_Name (Operand)
10139 and then Range_Checks_Suppressed (Entity (Operand))
10144 -- Nothing to do if bounds are all static and we can tell that the
10145 -- expression is within the bounds of the target. Note that if the
10146 -- operand is of an unconstrained floating-point type, then we do
10147 -- not trust it to be in range (might be infinite)
10150 S_Lo : constant Node_Id := Type_Low_Bound (Xtyp);
10151 S_Hi : constant Node_Id := Type_High_Bound (Xtyp);
10154 if (not Is_Floating_Point_Type (Xtyp)
10155 or else Is_Constrained (Xtyp))
10156 and then Compile_Time_Known_Value (S_Lo)
10157 and then Compile_Time_Known_Value (S_Hi)
10158 and then Compile_Time_Known_Value (Hi)
10159 and then Compile_Time_Known_Value (Lo)
10162 D_Lov : constant Ureal := Expr_Value_R (Lo);
10163 D_Hiv : constant Ureal := Expr_Value_R (Hi);
10168 if Is_Real_Type (Xtyp) then
10169 S_Lov := Expr_Value_R (S_Lo);
10170 S_Hiv := Expr_Value_R (S_Hi);
10172 S_Lov := UR_From_Uint (Expr_Value (S_Lo));
10173 S_Hiv := UR_From_Uint (Expr_Value (S_Hi));
10177 and then S_Lov >= D_Lov
10178 and then S_Hiv <= D_Hiv
10180 Set_Do_Range_Check (Operand, False);
10187 -- For float to float conversions, we are done
10189 if Is_Floating_Point_Type (Xtyp)
10191 Is_Floating_Point_Type (Btyp)
10196 -- Otherwise rewrite the conversion as described above
10198 Conv := Relocate_Node (N);
10199 Rewrite (Subtype_Mark (Conv), New_Occurrence_Of (Btyp, Loc));
10200 Set_Etype (Conv, Btyp);
10202 -- Enable overflow except for case of integer to float conversions,
10203 -- where it is never required, since we can never have overflow in
10206 if not Is_Integer_Type (Etype (Operand)) then
10207 Enable_Overflow_Check (Conv);
10210 Tnn := Make_Temporary (Loc, 'T', Conv);
10212 Insert_Actions (N, New_List (
10213 Make_Object_Declaration (Loc,
10214 Defining_Identifier => Tnn,
10215 Object_Definition => New_Occurrence_Of (Btyp, Loc),
10216 Constant_Present => True,
10217 Expression => Conv),
10219 Make_Raise_Constraint_Error (Loc,
10224 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10226 Make_Attribute_Reference (Loc,
10227 Attribute_Name => Name_First,
10229 New_Occurrence_Of (Target_Type, Loc))),
10233 Left_Opnd => New_Occurrence_Of (Tnn, Loc),
10235 Make_Attribute_Reference (Loc,
10236 Attribute_Name => Name_Last,
10238 New_Occurrence_Of (Target_Type, Loc)))),
10239 Reason => CE_Range_Check_Failed)));
10241 Rewrite (N, New_Occurrence_Of (Tnn, Loc));
10242 Analyze_And_Resolve (N, Btyp);
10243 end Real_Range_Check;
10245 -----------------------------
10246 -- Has_Extra_Accessibility --
10247 -----------------------------
10249 -- Returns true for a formal of an anonymous access type or for
10250 -- an Ada 2012-style stand-alone object of an anonymous access type.
10252 function Has_Extra_Accessibility (Id : Entity_Id) return Boolean is
10254 if Is_Formal (Id) or else Ekind_In (Id, E_Constant, E_Variable) then
10255 return Present (Effective_Extra_Accessibility (Id));
10259 end Has_Extra_Accessibility;
10261 -- Start of processing for Expand_N_Type_Conversion
10264 -- First remove check marks put by the semantic analysis on the type
10265 -- conversion between array types. We need these checks, and they will
10266 -- be generated by this expansion routine, but we do not depend on these
10267 -- flags being set, and since we do intend to expand the checks in the
10268 -- front end, we don't want them on the tree passed to the back end.
10270 if Is_Array_Type (Target_Type) then
10271 if Is_Constrained (Target_Type) then
10272 Set_Do_Length_Check (N, False);
10274 Set_Do_Range_Check (Operand, False);
10278 -- Nothing at all to do if conversion is to the identical type so remove
10279 -- the conversion completely, it is useless, except that it may carry
10280 -- an Assignment_OK attribute, which must be propagated to the operand.
10282 if Operand_Type = Target_Type then
10283 if Assignment_OK (N) then
10284 Set_Assignment_OK (Operand);
10287 Rewrite (N, Relocate_Node (Operand));
10291 -- Nothing to do if this is the second argument of read. This is a
10292 -- "backwards" conversion that will be handled by the specialized code
10293 -- in attribute processing.
10295 if Nkind (Parent (N)) = N_Attribute_Reference
10296 and then Attribute_Name (Parent (N)) = Name_Read
10297 and then Next (First (Expressions (Parent (N)))) = N
10302 -- Check for case of converting to a type that has an invariant
10303 -- associated with it. This required an invariant check. We convert
10309 -- do invariant_check (typ (expr)) in typ (expr);
10311 -- using Duplicate_Subexpr to avoid multiple side effects
10313 -- Note: the Comes_From_Source check, and then the resetting of this
10314 -- flag prevents what would otherwise be an infinite recursion.
10316 if Has_Invariants (Target_Type)
10317 and then Present (Invariant_Procedure (Target_Type))
10318 and then Comes_From_Source (N)
10320 Set_Comes_From_Source (N, False);
10322 Make_Expression_With_Actions (Loc,
10323 Actions => New_List (
10324 Make_Invariant_Call (Duplicate_Subexpr (N))),
10325 Expression => Duplicate_Subexpr_No_Checks (N)));
10326 Analyze_And_Resolve (N, Target_Type);
10330 -- Here if we may need to expand conversion
10332 -- If the operand of the type conversion is an arithmetic operation on
10333 -- signed integers, and the based type of the signed integer type in
10334 -- question is smaller than Standard.Integer, we promote both of the
10335 -- operands to type Integer.
10337 -- For example, if we have
10339 -- target-type (opnd1 + opnd2)
10341 -- and opnd1 and opnd2 are of type short integer, then we rewrite
10344 -- target-type (integer(opnd1) + integer(opnd2))
10346 -- We do this because we are always allowed to compute in a larger type
10347 -- if we do the right thing with the result, and in this case we are
10348 -- going to do a conversion which will do an appropriate check to make
10349 -- sure that things are in range of the target type in any case. This
10350 -- avoids some unnecessary intermediate overflows.
10352 -- We might consider a similar transformation in the case where the
10353 -- target is a real type or a 64-bit integer type, and the operand
10354 -- is an arithmetic operation using a 32-bit integer type. However,
10355 -- we do not bother with this case, because it could cause significant
10356 -- inefficiencies on 32-bit machines. On a 64-bit machine it would be
10357 -- much cheaper, but we don't want different behavior on 32-bit and
10358 -- 64-bit machines. Note that the exclusion of the 64-bit case also
10359 -- handles the configurable run-time cases where 64-bit arithmetic
10360 -- may simply be unavailable.
10362 -- Note: this circuit is partially redundant with respect to the circuit
10363 -- in Checks.Apply_Arithmetic_Overflow_Check, but we catch more cases in
10364 -- the processing here. Also we still need the Checks circuit, since we
10365 -- have to be sure not to generate junk overflow checks in the first
10366 -- place, since it would be trick to remove them here.
10368 if Integer_Promotion_Possible (N) then
10370 -- All conditions met, go ahead with transformation
10378 Make_Type_Conversion (Loc,
10379 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10380 Expression => Relocate_Node (Right_Opnd (Operand)));
10382 Opnd := New_Op_Node (Nkind (Operand), Loc);
10383 Set_Right_Opnd (Opnd, R);
10385 if Nkind (Operand) in N_Binary_Op then
10387 Make_Type_Conversion (Loc,
10388 Subtype_Mark => New_Occurrence_Of (Standard_Integer, Loc),
10389 Expression => Relocate_Node (Left_Opnd (Operand)));
10391 Set_Left_Opnd (Opnd, L);
10395 Make_Type_Conversion (Loc,
10396 Subtype_Mark => Relocate_Node (Subtype_Mark (N)),
10397 Expression => Opnd));
10399 Analyze_And_Resolve (N, Target_Type);
10404 -- Do validity check if validity checking operands
10406 if Validity_Checks_On and Validity_Check_Operands then
10407 Ensure_Valid (Operand);
10410 -- Special case of converting from non-standard boolean type
10412 if Is_Boolean_Type (Operand_Type)
10413 and then (Nonzero_Is_True (Operand_Type))
10415 Adjust_Condition (Operand);
10416 Set_Etype (Operand, Standard_Boolean);
10417 Operand_Type := Standard_Boolean;
10420 -- Case of converting to an access type
10422 if Is_Access_Type (Target_Type) then
10424 -- Apply an accessibility check when the conversion operand is an
10425 -- access parameter (or a renaming thereof), unless conversion was
10426 -- expanded from an Unchecked_ or Unrestricted_Access attribute.
10427 -- Note that other checks may still need to be applied below (such
10428 -- as tagged type checks).
10430 if Is_Entity_Name (Operand)
10431 and then Has_Extra_Accessibility (Entity (Operand))
10432 and then Ekind (Etype (Operand)) = E_Anonymous_Access_Type
10433 and then (Nkind (Original_Node (N)) /= N_Attribute_Reference
10434 or else Attribute_Name (Original_Node (N)) = Name_Access)
10436 Apply_Accessibility_Check
10437 (Operand, Target_Type, Insert_Node => Operand);
10439 -- If the level of the operand type is statically deeper than the
10440 -- level of the target type, then force Program_Error. Note that this
10441 -- can only occur for cases where the attribute is within the body of
10442 -- an instantiation (otherwise the conversion will already have been
10443 -- rejected as illegal). Note: warnings are issued by the analyzer
10444 -- for the instance cases.
10446 elsif In_Instance_Body
10447 and then Type_Access_Level (Operand_Type) >
10448 Type_Access_Level (Target_Type)
10450 Raise_Accessibility_Error;
10452 -- When the operand is a selected access discriminant the check needs
10453 -- to be made against the level of the object denoted by the prefix
10454 -- of the selected name. Force Program_Error for this case as well
10455 -- (this accessibility violation can only happen if within the body
10456 -- of an instantiation).
10458 elsif In_Instance_Body
10459 and then Ekind (Operand_Type) = E_Anonymous_Access_Type
10460 and then Nkind (Operand) = N_Selected_Component
10461 and then Object_Access_Level (Operand) >
10462 Type_Access_Level (Target_Type)
10464 Raise_Accessibility_Error;
10469 -- Case of conversions of tagged types and access to tagged types
10471 -- When needed, that is to say when the expression is class-wide, Add
10472 -- runtime a tag check for (strict) downward conversion by using the
10473 -- membership test, generating:
10475 -- [constraint_error when Operand not in Target_Type'Class]
10477 -- or in the access type case
10479 -- [constraint_error
10480 -- when Operand /= null
10481 -- and then Operand.all not in
10482 -- Designated_Type (Target_Type)'Class]
10484 if (Is_Access_Type (Target_Type)
10485 and then Is_Tagged_Type (Designated_Type (Target_Type)))
10486 or else Is_Tagged_Type (Target_Type)
10488 -- Do not do any expansion in the access type case if the parent is a
10489 -- renaming, since this is an error situation which will be caught by
10490 -- Sem_Ch8, and the expansion can interfere with this error check.
10492 if Is_Access_Type (Target_Type) and then Is_Renamed_Object (N) then
10496 -- Otherwise, proceed with processing tagged conversion
10498 Tagged_Conversion : declare
10499 Actual_Op_Typ : Entity_Id;
10500 Actual_Targ_Typ : Entity_Id;
10501 Make_Conversion : Boolean := False;
10502 Root_Op_Typ : Entity_Id;
10504 procedure Make_Tag_Check (Targ_Typ : Entity_Id);
10505 -- Create a membership check to test whether Operand is a member
10506 -- of Targ_Typ. If the original Target_Type is an access, include
10507 -- a test for null value. The check is inserted at N.
10509 --------------------
10510 -- Make_Tag_Check --
10511 --------------------
10513 procedure Make_Tag_Check (Targ_Typ : Entity_Id) is
10518 -- [Constraint_Error
10519 -- when Operand /= null
10520 -- and then Operand.all not in Targ_Typ]
10522 if Is_Access_Type (Target_Type) then
10524 Make_And_Then (Loc,
10527 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10528 Right_Opnd => Make_Null (Loc)),
10533 Make_Explicit_Dereference (Loc,
10534 Prefix => Duplicate_Subexpr_No_Checks (Operand)),
10535 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc)));
10538 -- [Constraint_Error when Operand not in Targ_Typ]
10543 Left_Opnd => Duplicate_Subexpr_No_Checks (Operand),
10544 Right_Opnd => New_Occurrence_Of (Targ_Typ, Loc));
10548 Make_Raise_Constraint_Error (Loc,
10550 Reason => CE_Tag_Check_Failed));
10551 end Make_Tag_Check;
10553 -- Start of processing for Tagged_Conversion
10556 -- Handle entities from the limited view
10558 if Is_Access_Type (Operand_Type) then
10560 Available_View (Designated_Type (Operand_Type));
10562 Actual_Op_Typ := Operand_Type;
10565 if Is_Access_Type (Target_Type) then
10567 Available_View (Designated_Type (Target_Type));
10569 Actual_Targ_Typ := Target_Type;
10572 Root_Op_Typ := Root_Type (Actual_Op_Typ);
10574 -- Ada 2005 (AI-251): Handle interface type conversion
10576 if Is_Interface (Actual_Op_Typ) then
10577 Expand_Interface_Conversion (N);
10581 if not Tag_Checks_Suppressed (Actual_Targ_Typ) then
10583 -- Create a runtime tag check for a downward class-wide type
10586 if Is_Class_Wide_Type (Actual_Op_Typ)
10587 and then Actual_Op_Typ /= Actual_Targ_Typ
10588 and then Root_Op_Typ /= Actual_Targ_Typ
10589 and then Is_Ancestor (Root_Op_Typ, Actual_Targ_Typ,
10590 Use_Full_View => True)
10592 Make_Tag_Check (Class_Wide_Type (Actual_Targ_Typ));
10593 Make_Conversion := True;
10596 -- AI05-0073: If the result subtype of the function is defined
10597 -- by an access_definition designating a specific tagged type
10598 -- T, a check is made that the result value is null or the tag
10599 -- of the object designated by the result value identifies T.
10600 -- Constraint_Error is raised if this check fails.
10602 if Nkind (Parent (N)) = N_Simple_Return_Statement then
10605 Func_Typ : Entity_Id;
10608 -- Climb scope stack looking for the enclosing function
10610 Func := Current_Scope;
10611 while Present (Func)
10612 and then Ekind (Func) /= E_Function
10614 Func := Scope (Func);
10617 -- The function's return subtype must be defined using
10618 -- an access definition.
10620 if Nkind (Result_Definition (Parent (Func))) =
10621 N_Access_Definition
10623 Func_Typ := Directly_Designated_Type (Etype (Func));
10625 -- The return subtype denotes a specific tagged type,
10626 -- in other words, a non class-wide type.
10628 if Is_Tagged_Type (Func_Typ)
10629 and then not Is_Class_Wide_Type (Func_Typ)
10631 Make_Tag_Check (Actual_Targ_Typ);
10632 Make_Conversion := True;
10638 -- We have generated a tag check for either a class-wide type
10639 -- conversion or for AI05-0073.
10641 if Make_Conversion then
10646 Make_Unchecked_Type_Conversion (Loc,
10647 Subtype_Mark => New_Occurrence_Of (Target_Type, Loc),
10648 Expression => Relocate_Node (Expression (N)));
10650 Analyze_And_Resolve (N, Target_Type);
10654 end Tagged_Conversion;
10656 -- Case of other access type conversions
10658 elsif Is_Access_Type (Target_Type) then
10659 Apply_Constraint_Check (Operand, Target_Type);
10661 -- Case of conversions from a fixed-point type
10663 -- These conversions require special expansion and processing, found in
10664 -- the Exp_Fixd package. We ignore cases where Conversion_OK is set,
10665 -- since from a semantic point of view, these are simple integer
10666 -- conversions, which do not need further processing.
10668 elsif Is_Fixed_Point_Type (Operand_Type)
10669 and then not Conversion_OK (N)
10671 -- We should never see universal fixed at this case, since the
10672 -- expansion of the constituent divide or multiply should have
10673 -- eliminated the explicit mention of universal fixed.
10675 pragma Assert (Operand_Type /= Universal_Fixed);
10677 -- Check for special case of the conversion to universal real that
10678 -- occurs as a result of the use of a round attribute. In this case,
10679 -- the real type for the conversion is taken from the target type of
10680 -- the Round attribute and the result must be marked as rounded.
10682 if Target_Type = Universal_Real
10683 and then Nkind (Parent (N)) = N_Attribute_Reference
10684 and then Attribute_Name (Parent (N)) = Name_Round
10686 Set_Rounded_Result (N);
10687 Set_Etype (N, Etype (Parent (N)));
10690 -- Otherwise do correct fixed-conversion, but skip these if the
10691 -- Conversion_OK flag is set, because from a semantic point of view
10692 -- these are simple integer conversions needing no further processing
10693 -- (the backend will simply treat them as integers).
10695 if not Conversion_OK (N) then
10696 if Is_Fixed_Point_Type (Etype (N)) then
10697 Expand_Convert_Fixed_To_Fixed (N);
10700 elsif Is_Integer_Type (Etype (N)) then
10701 Expand_Convert_Fixed_To_Integer (N);
10704 pragma Assert (Is_Floating_Point_Type (Etype (N)));
10705 Expand_Convert_Fixed_To_Float (N);
10710 -- Case of conversions to a fixed-point type
10712 -- These conversions require special expansion and processing, found in
10713 -- the Exp_Fixd package. Again, ignore cases where Conversion_OK is set,
10714 -- since from a semantic point of view, these are simple integer
10715 -- conversions, which do not need further processing.
10717 elsif Is_Fixed_Point_Type (Target_Type)
10718 and then not Conversion_OK (N)
10720 if Is_Integer_Type (Operand_Type) then
10721 Expand_Convert_Integer_To_Fixed (N);
10724 pragma Assert (Is_Floating_Point_Type (Operand_Type));
10725 Expand_Convert_Float_To_Fixed (N);
10729 -- Case of float-to-integer conversions
10731 -- We also handle float-to-fixed conversions with Conversion_OK set
10732 -- since semantically the fixed-point target is treated as though it
10733 -- were an integer in such cases.
10735 elsif Is_Floating_Point_Type (Operand_Type)
10737 (Is_Integer_Type (Target_Type)
10739 (Is_Fixed_Point_Type (Target_Type) and then Conversion_OK (N)))
10741 -- One more check here, gcc is still not able to do conversions of
10742 -- this type with proper overflow checking, and so gigi is doing an
10743 -- approximation of what is required by doing floating-point compares
10744 -- with the end-point. But that can lose precision in some cases, and
10745 -- give a wrong result. Converting the operand to Universal_Real is
10746 -- helpful, but still does not catch all cases with 64-bit integers
10747 -- on targets with only 64-bit floats.
10749 -- The above comment seems obsoleted by Apply_Float_Conversion_Check
10750 -- Can this code be removed ???
10752 if Do_Range_Check (Operand) then
10754 Make_Type_Conversion (Loc,
10756 New_Occurrence_Of (Universal_Real, Loc),
10758 Relocate_Node (Operand)));
10760 Set_Etype (Operand, Universal_Real);
10761 Enable_Range_Check (Operand);
10762 Set_Do_Range_Check (Expression (Operand), False);
10765 -- Case of array conversions
10767 -- Expansion of array conversions, add required length/range checks but
10768 -- only do this if there is no change of representation. For handling of
10769 -- this case, see Handle_Changed_Representation.
10771 elsif Is_Array_Type (Target_Type) then
10772 if Is_Constrained (Target_Type) then
10773 Apply_Length_Check (Operand, Target_Type);
10775 Apply_Range_Check (Operand, Target_Type);
10778 Handle_Changed_Representation;
10780 -- Case of conversions of discriminated types
10782 -- Add required discriminant checks if target is constrained. Again this
10783 -- change is skipped if we have a change of representation.
10785 elsif Has_Discriminants (Target_Type)
10786 and then Is_Constrained (Target_Type)
10788 Apply_Discriminant_Check (Operand, Target_Type);
10789 Handle_Changed_Representation;
10791 -- Case of all other record conversions. The only processing required
10792 -- is to check for a change of representation requiring the special
10793 -- assignment processing.
10795 elsif Is_Record_Type (Target_Type) then
10797 -- Ada 2005 (AI-216): Program_Error is raised when converting from
10798 -- a derived Unchecked_Union type to an unconstrained type that is
10799 -- not Unchecked_Union if the operand lacks inferable discriminants.
10801 if Is_Derived_Type (Operand_Type)
10802 and then Is_Unchecked_Union (Base_Type (Operand_Type))
10803 and then not Is_Constrained (Target_Type)
10804 and then not Is_Unchecked_Union (Base_Type (Target_Type))
10805 and then not Has_Inferable_Discriminants (Operand)
10807 -- To prevent Gigi from generating illegal code, we generate a
10808 -- Program_Error node, but we give it the target type of the
10809 -- conversion (is this requirement documented somewhere ???)
10812 PE : constant Node_Id := Make_Raise_Program_Error (Loc,
10813 Reason => PE_Unchecked_Union_Restriction);
10816 Set_Etype (PE, Target_Type);
10821 Handle_Changed_Representation;
10824 -- Case of conversions of enumeration types
10826 elsif Is_Enumeration_Type (Target_Type) then
10828 -- Special processing is required if there is a change of
10829 -- representation (from enumeration representation clauses).
10831 if not Same_Representation (Target_Type, Operand_Type) then
10833 -- Convert: x(y) to x'val (ytyp'val (y))
10836 Make_Attribute_Reference (Loc,
10837 Prefix => New_Occurrence_Of (Target_Type, Loc),
10838 Attribute_Name => Name_Val,
10839 Expressions => New_List (
10840 Make_Attribute_Reference (Loc,
10841 Prefix => New_Occurrence_Of (Operand_Type, Loc),
10842 Attribute_Name => Name_Pos,
10843 Expressions => New_List (Operand)))));
10845 Analyze_And_Resolve (N, Target_Type);
10848 -- Case of conversions to floating-point
10850 elsif Is_Floating_Point_Type (Target_Type) then
10854 -- At this stage, either the conversion node has been transformed into
10855 -- some other equivalent expression, or left as a conversion that can be
10856 -- handled by Gigi, in the following cases:
10858 -- Conversions with no change of representation or type
10860 -- Numeric conversions involving integer, floating- and fixed-point
10861 -- values. Fixed-point values are allowed only if Conversion_OK is
10862 -- set, i.e. if the fixed-point values are to be treated as integers.
10864 -- No other conversions should be passed to Gigi
10866 -- Check: are these rules stated in sinfo??? if so, why restate here???
10868 -- The only remaining step is to generate a range check if we still have
10869 -- a type conversion at this stage and Do_Range_Check is set. For now we
10870 -- do this only for conversions of discrete types.
10872 if Nkind (N) = N_Type_Conversion
10873 and then Is_Discrete_Type (Etype (N))
10876 Expr : constant Node_Id := Expression (N);
10881 if Do_Range_Check (Expr)
10882 and then Is_Discrete_Type (Etype (Expr))
10884 Set_Do_Range_Check (Expr, False);
10886 -- Before we do a range check, we have to deal with treating a
10887 -- fixed-point operand as an integer. The way we do this is
10888 -- simply to do an unchecked conversion to an appropriate
10889 -- integer type large enough to hold the result.
10891 -- This code is not active yet, because we are only dealing
10892 -- with discrete types so far ???
10894 if Nkind (Expr) in N_Has_Treat_Fixed_As_Integer
10895 and then Treat_Fixed_As_Integer (Expr)
10897 Ftyp := Base_Type (Etype (Expr));
10899 if Esize (Ftyp) >= Esize (Standard_Integer) then
10900 Ityp := Standard_Long_Long_Integer;
10902 Ityp := Standard_Integer;
10905 Rewrite (Expr, Unchecked_Convert_To (Ityp, Expr));
10908 -- Reset overflow flag, since the range check will include
10909 -- dealing with possible overflow, and generate the check. If
10910 -- Address is either a source type or target type, suppress
10911 -- range check to avoid typing anomalies when it is a visible
10914 Set_Do_Overflow_Check (N, False);
10915 if not Is_Descendent_Of_Address (Etype (Expr))
10916 and then not Is_Descendent_Of_Address (Target_Type)
10918 Generate_Range_Check
10919 (Expr, Target_Type, CE_Range_Check_Failed);
10925 -- Final step, if the result is a type conversion involving Vax_Float
10926 -- types, then it is subject for further special processing.
10928 if Nkind (N) = N_Type_Conversion
10929 and then (Vax_Float (Operand_Type) or else Vax_Float (Target_Type))
10931 Expand_Vax_Conversion (N);
10935 -- Here at end of processing
10938 -- Apply predicate check if required. Note that we can't just call
10939 -- Apply_Predicate_Check here, because the type looks right after
10940 -- the conversion and it would omit the check. The Comes_From_Source
10941 -- guard is necessary to prevent infinite recursions when we generate
10942 -- internal conversions for the purpose of checking predicates.
10944 if Present (Predicate_Function (Target_Type))
10945 and then Target_Type /= Operand_Type
10946 and then Comes_From_Source (N)
10949 New_Expr : constant Node_Id := Duplicate_Subexpr (N);
10952 -- Avoid infinite recursion on the subsequent expansion of
10953 -- of the copy of the original type conversion.
10955 Set_Comes_From_Source (New_Expr, False);
10956 Insert_Action (N, Make_Predicate_Check (Target_Type, New_Expr));
10959 end Expand_N_Type_Conversion;
10961 -----------------------------------
10962 -- Expand_N_Unchecked_Expression --
10963 -----------------------------------
10965 -- Remove the unchecked expression node from the tree. Its job was simply
10966 -- to make sure that its constituent expression was handled with checks
10967 -- off, and now that that is done, we can remove it from the tree, and
10968 -- indeed must, since Gigi does not expect to see these nodes.
10970 procedure Expand_N_Unchecked_Expression (N : Node_Id) is
10971 Exp : constant Node_Id := Expression (N);
10973 Set_Assignment_OK (Exp, Assignment_OK (N) or else Assignment_OK (Exp));
10975 end Expand_N_Unchecked_Expression;
10977 ----------------------------------------
10978 -- Expand_N_Unchecked_Type_Conversion --
10979 ----------------------------------------
10981 -- If this cannot be handled by Gigi and we haven't already made a
10982 -- temporary for it, do it now.
10984 procedure Expand_N_Unchecked_Type_Conversion (N : Node_Id) is
10985 Target_Type : constant Entity_Id := Etype (N);
10986 Operand : constant Node_Id := Expression (N);
10987 Operand_Type : constant Entity_Id := Etype (Operand);
10990 -- Nothing at all to do if conversion is to the identical type so remove
10991 -- the conversion completely, it is useless, except that it may carry
10992 -- an Assignment_OK indication which must be propagated to the operand.
10994 if Operand_Type = Target_Type then
10996 -- Code duplicates Expand_N_Unchecked_Expression above, factor???
10998 if Assignment_OK (N) then
10999 Set_Assignment_OK (Operand);
11002 Rewrite (N, Relocate_Node (Operand));
11006 -- If we have a conversion of a compile time known value to a target
11007 -- type and the value is in range of the target type, then we can simply
11008 -- replace the construct by an integer literal of the correct type. We
11009 -- only apply this to integer types being converted. Possibly it may
11010 -- apply in other cases, but it is too much trouble to worry about.
11012 -- Note that we do not do this transformation if the Kill_Range_Check
11013 -- flag is set, since then the value may be outside the expected range.
11014 -- This happens in the Normalize_Scalars case.
11016 -- We also skip this if either the target or operand type is biased
11017 -- because in this case, the unchecked conversion is supposed to
11018 -- preserve the bit pattern, not the integer value.
11020 if Is_Integer_Type (Target_Type)
11021 and then not Has_Biased_Representation (Target_Type)
11022 and then Is_Integer_Type (Operand_Type)
11023 and then not Has_Biased_Representation (Operand_Type)
11024 and then Compile_Time_Known_Value (Operand)
11025 and then not Kill_Range_Check (N)
11028 Val : constant Uint := Expr_Value (Operand);
11031 if Compile_Time_Known_Value (Type_Low_Bound (Target_Type))
11033 Compile_Time_Known_Value (Type_High_Bound (Target_Type))
11035 Val >= Expr_Value (Type_Low_Bound (Target_Type))
11037 Val <= Expr_Value (Type_High_Bound (Target_Type))
11039 Rewrite (N, Make_Integer_Literal (Sloc (N), Val));
11041 -- If Address is the target type, just set the type to avoid a
11042 -- spurious type error on the literal when Address is a visible
11045 if Is_Descendent_Of_Address (Target_Type) then
11046 Set_Etype (N, Target_Type);
11048 Analyze_And_Resolve (N, Target_Type);
11056 -- Nothing to do if conversion is safe
11058 if Safe_Unchecked_Type_Conversion (N) then
11062 -- Otherwise force evaluation unless Assignment_OK flag is set (this
11063 -- flag indicates ??? More comments needed here)
11065 if Assignment_OK (N) then
11068 Force_Evaluation (N);
11070 end Expand_N_Unchecked_Type_Conversion;
11072 ----------------------------
11073 -- Expand_Record_Equality --
11074 ----------------------------
11076 -- For non-variant records, Equality is expanded when needed into:
11078 -- and then Lhs.Discr1 = Rhs.Discr1
11080 -- and then Lhs.Discrn = Rhs.Discrn
11081 -- and then Lhs.Cmp1 = Rhs.Cmp1
11083 -- and then Lhs.Cmpn = Rhs.Cmpn
11085 -- The expression is folded by the back-end for adjacent fields. This
11086 -- function is called for tagged record in only one occasion: for imple-
11087 -- menting predefined primitive equality (see Predefined_Primitives_Bodies)
11088 -- otherwise the primitive "=" is used directly.
11090 function Expand_Record_Equality
11095 Bodies : List_Id) return Node_Id
11097 Loc : constant Source_Ptr := Sloc (Nod);
11102 First_Time : Boolean := True;
11104 function Element_To_Compare (C : Entity_Id) return Entity_Id;
11105 -- Return the next discriminant or component to compare, starting with
11106 -- C, skipping inherited components.
11108 ------------------------
11109 -- Element_To_Compare --
11110 ------------------------
11112 function Element_To_Compare (C : Entity_Id) return Entity_Id is
11118 -- Exit loop when the next element to be compared is found, or
11119 -- there is no more such element.
11121 exit when No (Comp);
11123 exit when Ekind_In (Comp, E_Discriminant, E_Component)
11126 -- Skip inherited components
11128 -- Note: for a tagged type, we always generate the "=" primitive
11129 -- for the base type (not on the first subtype), so the test for
11130 -- Comp /= Original_Record_Component (Comp) is True for
11131 -- inherited components only.
11133 (Is_Tagged_Type (Typ)
11134 and then Comp /= Original_Record_Component (Comp))
11138 or else Chars (Comp) = Name_uTag
11140 -- The .NET/JVM version of type Root_Controlled contains two
11141 -- fields which should not be considered part of the object. To
11142 -- achieve proper equiality between two controlled objects on
11143 -- .NET/JVM, skip _Parent whenever it has type Root_Controlled.
11145 or else (Chars (Comp) = Name_uParent
11146 and then VM_Target /= No_VM
11147 and then Etype (Comp) = RTE (RE_Root_Controlled))
11149 -- Skip interface elements (secondary tags???)
11151 or else Is_Interface (Etype (Comp)));
11153 Next_Entity (Comp);
11157 end Element_To_Compare;
11159 -- Start of processing for Expand_Record_Equality
11162 -- Generates the following code: (assuming that Typ has one Discr and
11163 -- component C2 is also a record)
11166 -- and then Lhs.Discr1 = Rhs.Discr1
11167 -- and then Lhs.C1 = Rhs.C1
11168 -- and then Lhs.C2.C1=Rhs.C2.C1 and then ... Lhs.C2.Cn=Rhs.C2.Cn
11170 -- and then Lhs.Cmpn = Rhs.Cmpn
11172 Result := New_Occurrence_Of (Standard_True, Loc);
11173 C := Element_To_Compare (First_Entity (Typ));
11174 while Present (C) loop
11182 First_Time := False;
11186 New_Lhs := New_Copy_Tree (Lhs);
11187 New_Rhs := New_Copy_Tree (Rhs);
11191 Expand_Composite_Equality (Nod, Etype (C),
11193 Make_Selected_Component (Loc,
11195 Selector_Name => New_Occurrence_Of (C, Loc)),
11197 Make_Selected_Component (Loc,
11199 Selector_Name => New_Occurrence_Of (C, Loc)),
11202 -- If some (sub)component is an unchecked_union, the whole
11203 -- operation will raise program error.
11205 if Nkind (Check) = N_Raise_Program_Error then
11207 Set_Etype (Result, Standard_Boolean);
11211 Make_And_Then (Loc,
11212 Left_Opnd => Result,
11213 Right_Opnd => Check);
11217 C := Element_To_Compare (Next_Entity (C));
11221 end Expand_Record_Equality;
11223 ---------------------------
11224 -- Expand_Set_Membership --
11225 ---------------------------
11227 procedure Expand_Set_Membership (N : Node_Id) is
11228 Lop : constant Node_Id := Left_Opnd (N);
11232 function Make_Cond (Alt : Node_Id) return Node_Id;
11233 -- If the alternative is a subtype mark, create a simple membership
11234 -- test. Otherwise create an equality test for it.
11240 function Make_Cond (Alt : Node_Id) return Node_Id is
11242 L : constant Node_Id := New_Copy (Lop);
11243 R : constant Node_Id := Relocate_Node (Alt);
11246 if (Is_Entity_Name (Alt) and then Is_Type (Entity (Alt)))
11247 or else Nkind (Alt) = N_Range
11250 Make_In (Sloc (Alt),
11255 Make_Op_Eq (Sloc (Alt),
11263 -- Start of processing for Expand_Set_Membership
11266 Remove_Side_Effects (Lop);
11268 Alt := Last (Alternatives (N));
11269 Res := Make_Cond (Alt);
11272 while Present (Alt) loop
11274 Make_Or_Else (Sloc (Alt),
11275 Left_Opnd => Make_Cond (Alt),
11276 Right_Opnd => Res);
11281 Analyze_And_Resolve (N, Standard_Boolean);
11282 end Expand_Set_Membership;
11284 -----------------------------------
11285 -- Expand_Short_Circuit_Operator --
11286 -----------------------------------
11288 -- Deal with special expansion if actions are present for the right operand
11289 -- and deal with optimizing case of arguments being True or False. We also
11290 -- deal with the special case of non-standard boolean values.
11292 procedure Expand_Short_Circuit_Operator (N : Node_Id) is
11293 Loc : constant Source_Ptr := Sloc (N);
11294 Typ : constant Entity_Id := Etype (N);
11295 Left : constant Node_Id := Left_Opnd (N);
11296 Right : constant Node_Id := Right_Opnd (N);
11297 LocR : constant Source_Ptr := Sloc (Right);
11300 Shortcut_Value : constant Boolean := Nkind (N) = N_Or_Else;
11301 Shortcut_Ent : constant Entity_Id := Boolean_Literals (Shortcut_Value);
11302 -- If Left = Shortcut_Value then Right need not be evaluated
11305 -- Deal with non-standard booleans
11307 if Is_Boolean_Type (Typ) then
11308 Adjust_Condition (Left);
11309 Adjust_Condition (Right);
11310 Set_Etype (N, Standard_Boolean);
11313 -- Check for cases where left argument is known to be True or False
11315 if Compile_Time_Known_Value (Left) then
11317 -- Mark SCO for left condition as compile time known
11319 if Generate_SCO and then Comes_From_Source (Left) then
11320 Set_SCO_Condition (Left, Expr_Value_E (Left) = Standard_True);
11323 -- Rewrite True AND THEN Right / False OR ELSE Right to Right.
11324 -- Any actions associated with Right will be executed unconditionally
11325 -- and can thus be inserted into the tree unconditionally.
11327 if Expr_Value_E (Left) /= Shortcut_Ent then
11328 if Present (Actions (N)) then
11329 Insert_Actions (N, Actions (N));
11332 Rewrite (N, Right);
11334 -- Rewrite False AND THEN Right / True OR ELSE Right to Left.
11335 -- In this case we can forget the actions associated with Right,
11336 -- since they will never be executed.
11339 Kill_Dead_Code (Right);
11340 Kill_Dead_Code (Actions (N));
11341 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11344 Adjust_Result_Type (N, Typ);
11348 -- If Actions are present for the right operand, we have to do some
11349 -- special processing. We can't just let these actions filter back into
11350 -- code preceding the short circuit (which is what would have happened
11351 -- if we had not trapped them in the short-circuit form), since they
11352 -- must only be executed if the right operand of the short circuit is
11353 -- executed and not otherwise.
11355 if Present (Actions (N)) then
11356 Actlist := Actions (N);
11358 -- We now use an Expression_With_Actions node for the right operand
11359 -- of the short-circuit form. Note that this solves the traceability
11360 -- problems for coverage analysis.
11363 Make_Expression_With_Actions (LocR,
11364 Expression => Relocate_Node (Right),
11365 Actions => Actlist));
11366 Set_Actions (N, No_List);
11367 Analyze_And_Resolve (Right, Standard_Boolean);
11369 Adjust_Result_Type (N, Typ);
11373 -- No actions present, check for cases of right argument True/False
11375 if Compile_Time_Known_Value (Right) then
11377 -- Mark SCO for left condition as compile time known
11379 if Generate_SCO and then Comes_From_Source (Right) then
11380 Set_SCO_Condition (Right, Expr_Value_E (Right) = Standard_True);
11383 -- Change (Left and then True), (Left or else False) to Left.
11384 -- Note that we know there are no actions associated with the right
11385 -- operand, since we just checked for this case above.
11387 if Expr_Value_E (Right) /= Shortcut_Ent then
11390 -- Change (Left and then False), (Left or else True) to Right,
11391 -- making sure to preserve any side effects associated with the Left
11395 Remove_Side_Effects (Left);
11396 Rewrite (N, New_Occurrence_Of (Shortcut_Ent, Loc));
11400 Adjust_Result_Type (N, Typ);
11401 end Expand_Short_Circuit_Operator;
11403 -------------------------------------
11404 -- Fixup_Universal_Fixed_Operation --
11405 -------------------------------------
11407 procedure Fixup_Universal_Fixed_Operation (N : Node_Id) is
11408 Conv : constant Node_Id := Parent (N);
11411 -- We must have a type conversion immediately above us
11413 pragma Assert (Nkind (Conv) = N_Type_Conversion);
11415 -- Normally the type conversion gives our target type. The exception
11416 -- occurs in the case of the Round attribute, where the conversion
11417 -- will be to universal real, and our real type comes from the Round
11418 -- attribute (as well as an indication that we must round the result)
11420 if Nkind (Parent (Conv)) = N_Attribute_Reference
11421 and then Attribute_Name (Parent (Conv)) = Name_Round
11423 Set_Etype (N, Etype (Parent (Conv)));
11424 Set_Rounded_Result (N);
11426 -- Normal case where type comes from conversion above us
11429 Set_Etype (N, Etype (Conv));
11431 end Fixup_Universal_Fixed_Operation;
11433 ---------------------------------
11434 -- Has_Inferable_Discriminants --
11435 ---------------------------------
11437 function Has_Inferable_Discriminants (N : Node_Id) return Boolean is
11439 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean;
11440 -- Determines whether the left-most prefix of a selected component is a
11441 -- formal parameter in a subprogram. Assumes N is a selected component.
11443 --------------------------------
11444 -- Prefix_Is_Formal_Parameter --
11445 --------------------------------
11447 function Prefix_Is_Formal_Parameter (N : Node_Id) return Boolean is
11448 Sel_Comp : Node_Id;
11451 -- Move to the left-most prefix by climbing up the tree
11454 while Present (Parent (Sel_Comp))
11455 and then Nkind (Parent (Sel_Comp)) = N_Selected_Component
11457 Sel_Comp := Parent (Sel_Comp);
11460 return Ekind (Entity (Prefix (Sel_Comp))) in Formal_Kind;
11461 end Prefix_Is_Formal_Parameter;
11463 -- Start of processing for Has_Inferable_Discriminants
11466 -- For selected components, the subtype of the selector must be a
11467 -- constrained Unchecked_Union. If the component is subject to a
11468 -- per-object constraint, then the enclosing object must have inferable
11471 if Nkind (N) = N_Selected_Component then
11472 if Has_Per_Object_Constraint (Entity (Selector_Name (N))) then
11474 -- A small hack. If we have a per-object constrained selected
11475 -- component of a formal parameter, return True since we do not
11476 -- know the actual parameter association yet.
11478 if Prefix_Is_Formal_Parameter (N) then
11481 -- Otherwise, check the enclosing object and the selector
11484 return Has_Inferable_Discriminants (Prefix (N))
11485 and then Has_Inferable_Discriminants (Selector_Name (N));
11488 -- The call to Has_Inferable_Discriminants will determine whether
11489 -- the selector has a constrained Unchecked_Union nominal type.
11492 return Has_Inferable_Discriminants (Selector_Name (N));
11495 -- A qualified expression has inferable discriminants if its subtype
11496 -- mark is a constrained Unchecked_Union subtype.
11498 elsif Nkind (N) = N_Qualified_Expression then
11499 return Is_Unchecked_Union (Etype (Subtype_Mark (N)))
11500 and then Is_Constrained (Etype (Subtype_Mark (N)));
11502 -- For all other names, it is sufficient to have a constrained
11503 -- Unchecked_Union nominal subtype.
11506 return Is_Unchecked_Union (Base_Type (Etype (N)))
11507 and then Is_Constrained (Etype (N));
11509 end Has_Inferable_Discriminants;
11511 -------------------------------
11512 -- Insert_Dereference_Action --
11513 -------------------------------
11515 procedure Insert_Dereference_Action (N : Node_Id) is
11517 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean;
11518 -- Return true if type of P is derived from Checked_Pool;
11520 -----------------------------
11521 -- Is_Checked_Storage_Pool --
11522 -----------------------------
11524 function Is_Checked_Storage_Pool (P : Entity_Id) return Boolean is
11533 while T /= Etype (T) loop
11534 if Is_RTE (T, RE_Checked_Pool) then
11542 end Is_Checked_Storage_Pool;
11546 Typ : constant Entity_Id := Etype (N);
11547 Desig : constant Entity_Id := Available_View (Designated_Type (Typ));
11548 Loc : constant Source_Ptr := Sloc (N);
11549 Pool : constant Entity_Id := Associated_Storage_Pool (Typ);
11550 Pnod : constant Node_Id := Parent (N);
11558 -- Start of processing for Insert_Dereference_Action
11561 pragma Assert (Nkind (Pnod) = N_Explicit_Dereference);
11563 -- Do not re-expand a dereference which has already been processed by
11566 if Has_Dereference_Action (Pnod) then
11569 -- Do not perform this type of expansion for internally-generated
11572 elsif not Comes_From_Source (Original_Node (Pnod)) then
11575 -- A dereference action is only applicable to objects which have been
11576 -- allocated on a checked pool.
11578 elsif not Is_Checked_Storage_Pool (Pool) then
11582 -- Extract the address of the dereferenced object. Generate:
11584 -- Addr : System.Address := <N>'Pool_Address;
11586 Addr := Make_Temporary (Loc, 'P');
11589 Make_Object_Declaration (Loc,
11590 Defining_Identifier => Addr,
11591 Object_Definition =>
11592 New_Occurrence_Of (RTE (RE_Address), Loc),
11594 Make_Attribute_Reference (Loc,
11595 Prefix => Duplicate_Subexpr_Move_Checks (N),
11596 Attribute_Name => Name_Pool_Address)));
11598 -- Calculate the size of the dereferenced object. Generate:
11600 -- Size : Storage_Count := <N>.all'Size / Storage_Unit;
11603 Make_Explicit_Dereference (Loc,
11604 Prefix => Duplicate_Subexpr_Move_Checks (N));
11605 Set_Has_Dereference_Action (Deref);
11607 Size := Make_Temporary (Loc, 'S');
11610 Make_Object_Declaration (Loc,
11611 Defining_Identifier => Size,
11613 Object_Definition =>
11614 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11617 Make_Op_Divide (Loc,
11619 Make_Attribute_Reference (Loc,
11621 Attribute_Name => Name_Size),
11623 Make_Integer_Literal (Loc, System_Storage_Unit))));
11625 -- Calculate the alignment of the dereferenced object. Generate:
11626 -- Alig : constant Storage_Count := <N>.all'Alignment;
11629 Make_Explicit_Dereference (Loc,
11630 Prefix => Duplicate_Subexpr_Move_Checks (N));
11631 Set_Has_Dereference_Action (Deref);
11633 Alig := Make_Temporary (Loc, 'A');
11636 Make_Object_Declaration (Loc,
11637 Defining_Identifier => Alig,
11638 Object_Definition =>
11639 New_Occurrence_Of (RTE (RE_Storage_Count), Loc),
11641 Make_Attribute_Reference (Loc,
11643 Attribute_Name => Name_Alignment)));
11645 -- A dereference of a controlled object requires special processing. The
11646 -- finalization machinery requests additional space from the underlying
11647 -- pool to allocate and hide two pointers. As a result, a checked pool
11648 -- may mark the wrong memory as valid. Since checked pools do not have
11649 -- knowledge of hidden pointers, we have to bring the two pointers back
11650 -- in view in order to restore the original state of the object.
11652 if Needs_Finalization (Desig) then
11654 -- Adjust the address and size of the dereferenced object. Generate:
11655 -- Adjust_Controlled_Dereference (Addr, Size, Alig);
11658 Make_Procedure_Call_Statement (Loc,
11660 New_Occurrence_Of (RTE (RE_Adjust_Controlled_Dereference), Loc),
11661 Parameter_Associations => New_List (
11662 New_Occurrence_Of (Addr, Loc),
11663 New_Occurrence_Of (Size, Loc),
11664 New_Occurrence_Of (Alig, Loc)));
11666 -- Class-wide types complicate things because we cannot determine
11667 -- statically whether the actual object is truly controlled. We must
11668 -- generate a runtime check to detect this property. Generate:
11670 -- if Needs_Finalization (<N>.all'Tag) then
11674 if Is_Class_Wide_Type (Desig) then
11676 Make_Explicit_Dereference (Loc,
11677 Prefix => Duplicate_Subexpr_Move_Checks (N));
11678 Set_Has_Dereference_Action (Deref);
11681 Make_Implicit_If_Statement (N,
11683 Make_Function_Call (Loc,
11685 New_Occurrence_Of (RTE (RE_Needs_Finalization), Loc),
11686 Parameter_Associations => New_List (
11687 Make_Attribute_Reference (Loc,
11689 Attribute_Name => Name_Tag))),
11690 Then_Statements => New_List (Stmt));
11693 Insert_Action (N, Stmt);
11697 -- Dereference (Pool, Addr, Size, Alig);
11700 Make_Procedure_Call_Statement (Loc,
11703 (Find_Prim_Op (Etype (Pool), Name_Dereference), Loc),
11704 Parameter_Associations => New_List (
11705 New_Occurrence_Of (Pool, Loc),
11706 New_Occurrence_Of (Addr, Loc),
11707 New_Occurrence_Of (Size, Loc),
11708 New_Occurrence_Of (Alig, Loc))));
11710 -- Mark the explicit dereference as processed to avoid potential
11711 -- infinite expansion.
11713 Set_Has_Dereference_Action (Pnod);
11716 when RE_Not_Available =>
11718 end Insert_Dereference_Action;
11720 --------------------------------
11721 -- Integer_Promotion_Possible --
11722 --------------------------------
11724 function Integer_Promotion_Possible (N : Node_Id) return Boolean is
11725 Operand : constant Node_Id := Expression (N);
11726 Operand_Type : constant Entity_Id := Etype (Operand);
11727 Root_Operand_Type : constant Entity_Id := Root_Type (Operand_Type);
11730 pragma Assert (Nkind (N) = N_Type_Conversion);
11734 -- We only do the transformation for source constructs. We assume
11735 -- that the expander knows what it is doing when it generates code.
11737 Comes_From_Source (N)
11739 -- If the operand type is Short_Integer or Short_Short_Integer,
11740 -- then we will promote to Integer, which is available on all
11741 -- targets, and is sufficient to ensure no intermediate overflow.
11742 -- Furthermore it is likely to be as efficient or more efficient
11743 -- than using the smaller type for the computation so we do this
11744 -- unconditionally.
11747 (Root_Operand_Type = Base_Type (Standard_Short_Integer)
11749 Root_Operand_Type = Base_Type (Standard_Short_Short_Integer))
11751 -- Test for interesting operation, which includes addition,
11752 -- division, exponentiation, multiplication, subtraction, absolute
11753 -- value and unary negation. Unary "+" is omitted since it is a
11754 -- no-op and thus can't overflow.
11756 and then Nkind_In (Operand, N_Op_Abs,
11763 end Integer_Promotion_Possible;
11765 ------------------------------
11766 -- Make_Array_Comparison_Op --
11767 ------------------------------
11769 -- This is a hand-coded expansion of the following generic function:
11772 -- type elem is (<>);
11773 -- type index is (<>);
11774 -- type a is array (index range <>) of elem;
11776 -- function Gnnn (X : a; Y: a) return boolean is
11777 -- J : index := Y'first;
11780 -- if X'length = 0 then
11783 -- elsif Y'length = 0 then
11787 -- for I in X'range loop
11788 -- if X (I) = Y (J) then
11789 -- if J = Y'last then
11792 -- J := index'succ (J);
11796 -- return X (I) > Y (J);
11800 -- return X'length > Y'length;
11804 -- Note that since we are essentially doing this expansion by hand, we
11805 -- do not need to generate an actual or formal generic part, just the
11806 -- instantiated function itself.
11808 function Make_Array_Comparison_Op
11810 Nod : Node_Id) return Node_Id
11812 Loc : constant Source_Ptr := Sloc (Nod);
11814 X : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uX);
11815 Y : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uY);
11816 I : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uI);
11817 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
11819 Index : constant Entity_Id := Base_Type (Etype (First_Index (Typ)));
11821 Loop_Statement : Node_Id;
11822 Loop_Body : Node_Id;
11824 Inner_If : Node_Id;
11825 Final_Expr : Node_Id;
11826 Func_Body : Node_Id;
11827 Func_Name : Entity_Id;
11833 -- if J = Y'last then
11836 -- J := index'succ (J);
11840 Make_Implicit_If_Statement (Nod,
11843 Left_Opnd => New_Occurrence_Of (J, Loc),
11845 Make_Attribute_Reference (Loc,
11846 Prefix => New_Occurrence_Of (Y, Loc),
11847 Attribute_Name => Name_Last)),
11849 Then_Statements => New_List (
11850 Make_Exit_Statement (Loc)),
11854 Make_Assignment_Statement (Loc,
11855 Name => New_Occurrence_Of (J, Loc),
11857 Make_Attribute_Reference (Loc,
11858 Prefix => New_Occurrence_Of (Index, Loc),
11859 Attribute_Name => Name_Succ,
11860 Expressions => New_List (New_Occurrence_Of (J, Loc))))));
11862 -- if X (I) = Y (J) then
11865 -- return X (I) > Y (J);
11869 Make_Implicit_If_Statement (Nod,
11873 Make_Indexed_Component (Loc,
11874 Prefix => New_Occurrence_Of (X, Loc),
11875 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11878 Make_Indexed_Component (Loc,
11879 Prefix => New_Occurrence_Of (Y, Loc),
11880 Expressions => New_List (New_Occurrence_Of (J, Loc)))),
11882 Then_Statements => New_List (Inner_If),
11884 Else_Statements => New_List (
11885 Make_Simple_Return_Statement (Loc,
11889 Make_Indexed_Component (Loc,
11890 Prefix => New_Occurrence_Of (X, Loc),
11891 Expressions => New_List (New_Occurrence_Of (I, Loc))),
11894 Make_Indexed_Component (Loc,
11895 Prefix => New_Occurrence_Of (Y, Loc),
11896 Expressions => New_List (
11897 New_Occurrence_Of (J, Loc)))))));
11899 -- for I in X'range loop
11904 Make_Implicit_Loop_Statement (Nod,
11905 Identifier => Empty,
11907 Iteration_Scheme =>
11908 Make_Iteration_Scheme (Loc,
11909 Loop_Parameter_Specification =>
11910 Make_Loop_Parameter_Specification (Loc,
11911 Defining_Identifier => I,
11912 Discrete_Subtype_Definition =>
11913 Make_Attribute_Reference (Loc,
11914 Prefix => New_Occurrence_Of (X, Loc),
11915 Attribute_Name => Name_Range))),
11917 Statements => New_List (Loop_Body));
11919 -- if X'length = 0 then
11921 -- elsif Y'length = 0 then
11924 -- for ... loop ... end loop;
11925 -- return X'length > Y'length;
11929 Make_Attribute_Reference (Loc,
11930 Prefix => New_Occurrence_Of (X, Loc),
11931 Attribute_Name => Name_Length);
11934 Make_Attribute_Reference (Loc,
11935 Prefix => New_Occurrence_Of (Y, Loc),
11936 Attribute_Name => Name_Length);
11940 Left_Opnd => Length1,
11941 Right_Opnd => Length2);
11944 Make_Implicit_If_Statement (Nod,
11948 Make_Attribute_Reference (Loc,
11949 Prefix => New_Occurrence_Of (X, Loc),
11950 Attribute_Name => Name_Length),
11952 Make_Integer_Literal (Loc, 0)),
11956 Make_Simple_Return_Statement (Loc,
11957 Expression => New_Occurrence_Of (Standard_False, Loc))),
11959 Elsif_Parts => New_List (
11960 Make_Elsif_Part (Loc,
11964 Make_Attribute_Reference (Loc,
11965 Prefix => New_Occurrence_Of (Y, Loc),
11966 Attribute_Name => Name_Length),
11968 Make_Integer_Literal (Loc, 0)),
11972 Make_Simple_Return_Statement (Loc,
11973 Expression => New_Occurrence_Of (Standard_True, Loc))))),
11975 Else_Statements => New_List (
11977 Make_Simple_Return_Statement (Loc,
11978 Expression => Final_Expr)));
11982 Formals := New_List (
11983 Make_Parameter_Specification (Loc,
11984 Defining_Identifier => X,
11985 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
11987 Make_Parameter_Specification (Loc,
11988 Defining_Identifier => Y,
11989 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
11991 -- function Gnnn (...) return boolean is
11992 -- J : index := Y'first;
11997 Func_Name := Make_Temporary (Loc, 'G');
12000 Make_Subprogram_Body (Loc,
12002 Make_Function_Specification (Loc,
12003 Defining_Unit_Name => Func_Name,
12004 Parameter_Specifications => Formals,
12005 Result_Definition => New_Occurrence_Of (Standard_Boolean, Loc)),
12007 Declarations => New_List (
12008 Make_Object_Declaration (Loc,
12009 Defining_Identifier => J,
12010 Object_Definition => New_Occurrence_Of (Index, Loc),
12012 Make_Attribute_Reference (Loc,
12013 Prefix => New_Occurrence_Of (Y, Loc),
12014 Attribute_Name => Name_First))),
12016 Handled_Statement_Sequence =>
12017 Make_Handled_Sequence_Of_Statements (Loc,
12018 Statements => New_List (If_Stat)));
12021 end Make_Array_Comparison_Op;
12023 ---------------------------
12024 -- Make_Boolean_Array_Op --
12025 ---------------------------
12027 -- For logical operations on boolean arrays, expand in line the following,
12028 -- replacing 'and' with 'or' or 'xor' where needed:
12030 -- function Annn (A : typ; B: typ) return typ is
12033 -- for J in A'range loop
12034 -- C (J) := A (J) op B (J);
12039 -- Here typ is the boolean array type
12041 function Make_Boolean_Array_Op
12043 N : Node_Id) return Node_Id
12045 Loc : constant Source_Ptr := Sloc (N);
12047 A : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uA);
12048 B : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uB);
12049 C : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uC);
12050 J : constant Entity_Id := Make_Defining_Identifier (Loc, Name_uJ);
12058 Func_Name : Entity_Id;
12059 Func_Body : Node_Id;
12060 Loop_Statement : Node_Id;
12064 Make_Indexed_Component (Loc,
12065 Prefix => New_Occurrence_Of (A, Loc),
12066 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12069 Make_Indexed_Component (Loc,
12070 Prefix => New_Occurrence_Of (B, Loc),
12071 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12074 Make_Indexed_Component (Loc,
12075 Prefix => New_Occurrence_Of (C, Loc),
12076 Expressions => New_List (New_Occurrence_Of (J, Loc)));
12078 if Nkind (N) = N_Op_And then
12082 Right_Opnd => B_J);
12084 elsif Nkind (N) = N_Op_Or then
12088 Right_Opnd => B_J);
12094 Right_Opnd => B_J);
12098 Make_Implicit_Loop_Statement (N,
12099 Identifier => Empty,
12101 Iteration_Scheme =>
12102 Make_Iteration_Scheme (Loc,
12103 Loop_Parameter_Specification =>
12104 Make_Loop_Parameter_Specification (Loc,
12105 Defining_Identifier => J,
12106 Discrete_Subtype_Definition =>
12107 Make_Attribute_Reference (Loc,
12108 Prefix => New_Occurrence_Of (A, Loc),
12109 Attribute_Name => Name_Range))),
12111 Statements => New_List (
12112 Make_Assignment_Statement (Loc,
12114 Expression => Op)));
12116 Formals := New_List (
12117 Make_Parameter_Specification (Loc,
12118 Defining_Identifier => A,
12119 Parameter_Type => New_Occurrence_Of (Typ, Loc)),
12121 Make_Parameter_Specification (Loc,
12122 Defining_Identifier => B,
12123 Parameter_Type => New_Occurrence_Of (Typ, Loc)));
12125 Func_Name := Make_Temporary (Loc, 'A');
12126 Set_Is_Inlined (Func_Name);
12129 Make_Subprogram_Body (Loc,
12131 Make_Function_Specification (Loc,
12132 Defining_Unit_Name => Func_Name,
12133 Parameter_Specifications => Formals,
12134 Result_Definition => New_Occurrence_Of (Typ, Loc)),
12136 Declarations => New_List (
12137 Make_Object_Declaration (Loc,
12138 Defining_Identifier => C,
12139 Object_Definition => New_Occurrence_Of (Typ, Loc))),
12141 Handled_Statement_Sequence =>
12142 Make_Handled_Sequence_Of_Statements (Loc,
12143 Statements => New_List (
12145 Make_Simple_Return_Statement (Loc,
12146 Expression => New_Occurrence_Of (C, Loc)))));
12149 end Make_Boolean_Array_Op;
12151 -----------------------------------------
12152 -- Minimized_Eliminated_Overflow_Check --
12153 -----------------------------------------
12155 function Minimized_Eliminated_Overflow_Check (N : Node_Id) return Boolean is
12158 Is_Signed_Integer_Type (Etype (N))
12159 and then Overflow_Check_Mode in Minimized_Or_Eliminated;
12160 end Minimized_Eliminated_Overflow_Check;
12162 --------------------------------
12163 -- Optimize_Length_Comparison --
12164 --------------------------------
12166 procedure Optimize_Length_Comparison (N : Node_Id) is
12167 Loc : constant Source_Ptr := Sloc (N);
12168 Typ : constant Entity_Id := Etype (N);
12173 -- First and Last attribute reference nodes, which end up as left and
12174 -- right operands of the optimized result.
12177 -- True for comparison operand of zero
12180 -- Comparison operand, set only if Is_Zero is false
12183 -- Entity whose length is being compared
12186 -- Integer_Literal node for length attribute expression, or Empty
12187 -- if there is no such expression present.
12190 -- Type of array index to which 'Length is applied
12192 Op : Node_Kind := Nkind (N);
12193 -- Kind of comparison operator, gets flipped if operands backwards
12195 function Is_Optimizable (N : Node_Id) return Boolean;
12196 -- Tests N to see if it is an optimizable comparison value (defined as
12197 -- constant zero or one, or something else where the value is known to
12198 -- be positive and in the range of 32-bits, and where the corresponding
12199 -- Length value is also known to be 32-bits. If result is true, sets
12200 -- Is_Zero, Ityp, and Comp accordingly.
12202 function Is_Entity_Length (N : Node_Id) return Boolean;
12203 -- Tests if N is a length attribute applied to a simple entity. If so,
12204 -- returns True, and sets Ent to the entity, and Index to the integer
12205 -- literal provided as an attribute expression, or to Empty if none.
12206 -- Also returns True if the expression is a generated type conversion
12207 -- whose expression is of the desired form. This latter case arises
12208 -- when Apply_Universal_Integer_Attribute_Check installs a conversion
12209 -- to check for being in range, which is not needed in this context.
12210 -- Returns False if neither condition holds.
12212 function Prepare_64 (N : Node_Id) return Node_Id;
12213 -- Given a discrete expression, returns a Long_Long_Integer typed
12214 -- expression representing the underlying value of the expression.
12215 -- This is done with an unchecked conversion to the result type. We
12216 -- use unchecked conversion to handle the enumeration type case.
12218 ----------------------
12219 -- Is_Entity_Length --
12220 ----------------------
12222 function Is_Entity_Length (N : Node_Id) return Boolean is
12224 if Nkind (N) = N_Attribute_Reference
12225 and then Attribute_Name (N) = Name_Length
12226 and then Is_Entity_Name (Prefix (N))
12228 Ent := Entity (Prefix (N));
12230 if Present (Expressions (N)) then
12231 Index := First (Expressions (N));
12238 elsif Nkind (N) = N_Type_Conversion
12239 and then not Comes_From_Source (N)
12241 return Is_Entity_Length (Expression (N));
12246 end Is_Entity_Length;
12248 --------------------
12249 -- Is_Optimizable --
12250 --------------------
12252 function Is_Optimizable (N : Node_Id) return Boolean is
12260 if Compile_Time_Known_Value (N) then
12261 Val := Expr_Value (N);
12263 if Val = Uint_0 then
12268 elsif Val = Uint_1 then
12275 -- Here we have to make sure of being within 32-bits
12277 Determine_Range (N, OK, Lo, Hi, Assume_Valid => True);
12280 or else Lo < Uint_1
12281 or else Hi > UI_From_Int (Int'Last)
12286 -- Comparison value was within range, so now we must check the index
12287 -- value to make sure it is also within 32-bits.
12289 Indx := First_Index (Etype (Ent));
12291 if Present (Index) then
12292 for J in 2 .. UI_To_Int (Intval (Index)) loop
12297 Ityp := Etype (Indx);
12299 if Esize (Ityp) > 32 then
12306 end Is_Optimizable;
12312 function Prepare_64 (N : Node_Id) return Node_Id is
12314 return Unchecked_Convert_To (Standard_Long_Long_Integer, N);
12317 -- Start of processing for Optimize_Length_Comparison
12320 -- Nothing to do if not a comparison
12322 if Op not in N_Op_Compare then
12326 -- Nothing to do if special -gnatd.P debug flag set
12328 if Debug_Flag_Dot_PP then
12332 -- Ent'Length op 0/1
12334 if Is_Entity_Length (Left_Opnd (N))
12335 and then Is_Optimizable (Right_Opnd (N))
12339 -- 0/1 op Ent'Length
12341 elsif Is_Entity_Length (Right_Opnd (N))
12342 and then Is_Optimizable (Left_Opnd (N))
12344 -- Flip comparison to opposite sense
12347 when N_Op_Lt => Op := N_Op_Gt;
12348 when N_Op_Le => Op := N_Op_Ge;
12349 when N_Op_Gt => Op := N_Op_Lt;
12350 when N_Op_Ge => Op := N_Op_Le;
12351 when others => null;
12354 -- Else optimization not possible
12360 -- Fall through if we will do the optimization
12362 -- Cases to handle:
12364 -- X'Length = 0 => X'First > X'Last
12365 -- X'Length = 1 => X'First = X'Last
12366 -- X'Length = n => X'First + (n - 1) = X'Last
12368 -- X'Length /= 0 => X'First <= X'Last
12369 -- X'Length /= 1 => X'First /= X'Last
12370 -- X'Length /= n => X'First + (n - 1) /= X'Last
12372 -- X'Length >= 0 => always true, warn
12373 -- X'Length >= 1 => X'First <= X'Last
12374 -- X'Length >= n => X'First + (n - 1) <= X'Last
12376 -- X'Length > 0 => X'First <= X'Last
12377 -- X'Length > 1 => X'First < X'Last
12378 -- X'Length > n => X'First + (n - 1) < X'Last
12380 -- X'Length <= 0 => X'First > X'Last (warn, could be =)
12381 -- X'Length <= 1 => X'First >= X'Last
12382 -- X'Length <= n => X'First + (n - 1) >= X'Last
12384 -- X'Length < 0 => always false (warn)
12385 -- X'Length < 1 => X'First > X'Last
12386 -- X'Length < n => X'First + (n - 1) > X'Last
12388 -- Note: for the cases of n (not constant 0,1), we require that the
12389 -- corresponding index type be integer or shorter (i.e. not 64-bit),
12390 -- and the same for the comparison value. Then we do the comparison
12391 -- using 64-bit arithmetic (actually long long integer), so that we
12392 -- cannot have overflow intefering with the result.
12394 -- First deal with warning cases
12403 Convert_To (Typ, New_Occurrence_Of (Standard_True, Loc)));
12404 Analyze_And_Resolve (N, Typ);
12405 Warn_On_Known_Condition (N);
12412 Convert_To (Typ, New_Occurrence_Of (Standard_False, Loc)));
12413 Analyze_And_Resolve (N, Typ);
12414 Warn_On_Known_Condition (N);
12418 if Constant_Condition_Warnings
12419 and then Comes_From_Source (Original_Node (N))
12421 Error_Msg_N ("could replace by ""'=""?c?", N);
12431 -- Build the First reference we will use
12434 Make_Attribute_Reference (Loc,
12435 Prefix => New_Occurrence_Of (Ent, Loc),
12436 Attribute_Name => Name_First);
12438 if Present (Index) then
12439 Set_Expressions (Left, New_List (New_Copy (Index)));
12442 -- If general value case, then do the addition of (n - 1), and
12443 -- also add the needed conversions to type Long_Long_Integer.
12445 if Present (Comp) then
12448 Left_Opnd => Prepare_64 (Left),
12450 Make_Op_Subtract (Loc,
12451 Left_Opnd => Prepare_64 (Comp),
12452 Right_Opnd => Make_Integer_Literal (Loc, 1)));
12455 -- Build the Last reference we will use
12458 Make_Attribute_Reference (Loc,
12459 Prefix => New_Occurrence_Of (Ent, Loc),
12460 Attribute_Name => Name_Last);
12462 if Present (Index) then
12463 Set_Expressions (Right, New_List (New_Copy (Index)));
12466 -- If general operand, convert Last reference to Long_Long_Integer
12468 if Present (Comp) then
12469 Right := Prepare_64 (Right);
12472 -- Check for cases to optimize
12474 -- X'Length = 0 => X'First > X'Last
12475 -- X'Length < 1 => X'First > X'Last
12476 -- X'Length < n => X'First + (n - 1) > X'Last
12478 if (Is_Zero and then Op = N_Op_Eq)
12479 or else (not Is_Zero and then Op = N_Op_Lt)
12484 Right_Opnd => Right);
12486 -- X'Length = 1 => X'First = X'Last
12487 -- X'Length = n => X'First + (n - 1) = X'Last
12489 elsif not Is_Zero and then Op = N_Op_Eq then
12493 Right_Opnd => Right);
12495 -- X'Length /= 0 => X'First <= X'Last
12496 -- X'Length > 0 => X'First <= X'Last
12498 elsif Is_Zero and (Op = N_Op_Ne or else Op = N_Op_Gt) then
12502 Right_Opnd => Right);
12504 -- X'Length /= 1 => X'First /= X'Last
12505 -- X'Length /= n => X'First + (n - 1) /= X'Last
12507 elsif not Is_Zero and then Op = N_Op_Ne then
12511 Right_Opnd => Right);
12513 -- X'Length >= 1 => X'First <= X'Last
12514 -- X'Length >= n => X'First + (n - 1) <= X'Last
12516 elsif not Is_Zero and then Op = N_Op_Ge then
12520 Right_Opnd => Right);
12522 -- X'Length > 1 => X'First < X'Last
12523 -- X'Length > n => X'First + (n = 1) < X'Last
12525 elsif not Is_Zero and then Op = N_Op_Gt then
12529 Right_Opnd => Right);
12531 -- X'Length <= 1 => X'First >= X'Last
12532 -- X'Length <= n => X'First + (n - 1) >= X'Last
12534 elsif not Is_Zero and then Op = N_Op_Le then
12538 Right_Opnd => Right);
12540 -- Should not happen at this stage
12543 raise Program_Error;
12546 -- Rewrite and finish up
12548 Rewrite (N, Result);
12549 Analyze_And_Resolve (N, Typ);
12551 end Optimize_Length_Comparison;
12553 ------------------------------
12554 -- Process_Transient_Object --
12555 ------------------------------
12557 procedure Process_Transient_Object
12559 Rel_Node : Node_Id)
12561 Loc : constant Source_Ptr := Sloc (Decl);
12562 Obj_Id : constant Entity_Id := Defining_Identifier (Decl);
12563 Obj_Typ : constant Node_Id := Etype (Obj_Id);
12564 Desig_Typ : Entity_Id;
12566 Fin_Stmts : List_Id;
12567 Ptr_Id : Entity_Id;
12568 Temp_Id : Entity_Id;
12569 Temp_Ins : Node_Id;
12571 Hook_Context : constant Node_Id := Find_Hook_Context (Rel_Node);
12572 -- Node on which to insert the hook pointer (as an action): the
12573 -- innermost enclosing non-transient scope.
12575 Finalization_Context : Node_Id;
12576 -- Node after which to insert finalization actions
12578 Finalize_Always : Boolean;
12579 -- If False, call to finalizer includes a test of whether the hook
12580 -- pointer is null.
12582 In_Cond_Expr : constant Boolean :=
12583 Within_Case_Or_If_Expression (Rel_Node);
12586 -- Step 0: determine where to attach finalization actions in the tree
12588 -- Special case for Boolean EWAs: capture expression in a temporary,
12589 -- whose declaration will serve as the context around which to insert
12590 -- finalization code. The finalization thus remains local to the
12591 -- specific condition being evaluated.
12593 if Is_Boolean_Type (Etype (Rel_Node)) then
12595 -- In this case, the finalization context is chosen so that we know
12596 -- at finalization point that the hook pointer is never null, so no
12597 -- need for a test, we can call the finalizer unconditionally, except
12598 -- in the case where the object is created in a specific branch of a
12599 -- conditional expression.
12604 Nkind_In (Original_Node (Rel_Node), N_Case_Expression,
12608 Loc : constant Source_Ptr := Sloc (Rel_Node);
12609 Temp : constant Entity_Id := Make_Temporary (Loc, 'E', Rel_Node);
12612 Append_To (Actions (Rel_Node),
12613 Make_Object_Declaration (Loc,
12614 Defining_Identifier => Temp,
12615 Constant_Present => True,
12616 Object_Definition =>
12617 New_Occurrence_Of (Etype (Rel_Node), Loc),
12618 Expression => Expression (Rel_Node)));
12619 Finalization_Context := Last (Actions (Rel_Node));
12621 Analyze (Last (Actions (Rel_Node)));
12623 Set_Expression (Rel_Node, New_Occurrence_Of (Temp, Loc));
12624 Analyze (Expression (Rel_Node));
12628 Finalize_Always := False;
12629 Finalization_Context := Hook_Context;
12632 -- Step 1: Create the access type which provides a reference to the
12633 -- transient controlled object.
12635 if Is_Access_Type (Obj_Typ) then
12636 Desig_Typ := Directly_Designated_Type (Obj_Typ);
12638 Desig_Typ := Obj_Typ;
12641 Desig_Typ := Base_Type (Desig_Typ);
12644 -- Ann : access [all] <Desig_Typ>;
12646 Ptr_Id := Make_Temporary (Loc, 'A');
12648 Insert_Action (Hook_Context,
12649 Make_Full_Type_Declaration (Loc,
12650 Defining_Identifier => Ptr_Id,
12652 Make_Access_To_Object_Definition (Loc,
12653 All_Present => Ekind (Obj_Typ) = E_General_Access_Type,
12654 Subtype_Indication => New_Occurrence_Of (Desig_Typ, Loc))));
12656 -- Step 2: Create a temporary which acts as a hook to the transient
12657 -- controlled object. Generate:
12659 -- Temp : Ptr_Id := null;
12661 Temp_Id := Make_Temporary (Loc, 'T');
12663 Insert_Action (Hook_Context,
12664 Make_Object_Declaration (Loc,
12665 Defining_Identifier => Temp_Id,
12666 Object_Definition => New_Occurrence_Of (Ptr_Id, Loc)));
12668 -- Mark the temporary as created for the purposes of exporting the
12669 -- transient controlled object out of the expression_with_action or if
12670 -- expression. This signals the machinery in Build_Finalizer to treat
12671 -- this case specially.
12673 Set_Status_Flag_Or_Transient_Decl (Temp_Id, Decl);
12675 -- Step 3: Hook the transient object to the temporary
12677 -- This must be inserted right after the object declaration, so that
12678 -- the assignment is executed if, and only if, the object is actually
12679 -- created (whereas the declaration of the hook pointer, and the
12680 -- finalization call, may be inserted at an outer level, and may
12681 -- remain unused for some executions, if the actual creation of
12682 -- the object is conditional).
12684 -- The use of unchecked conversion / unrestricted access is needed to
12685 -- avoid an accessibility violation. Note that the finalization code is
12686 -- structured in such a way that the "hook" is processed only when it
12687 -- points to an existing object.
12689 if Is_Access_Type (Obj_Typ) then
12691 Unchecked_Convert_To (Ptr_Id, New_Occurrence_Of (Obj_Id, Loc));
12694 Make_Attribute_Reference (Loc,
12695 Prefix => New_Occurrence_Of (Obj_Id, Loc),
12696 Attribute_Name => Name_Unrestricted_Access);
12700 -- Temp := Ptr_Id (Obj_Id);
12702 -- Temp := Obj_Id'Unrestricted_Access;
12704 -- When the transient object is initialized by an aggregate, the hook
12705 -- must capture the object after the last component assignment takes
12706 -- place. Only then is the object fully initialized.
12708 if Ekind (Obj_Id) = E_Variable
12709 and then Present (Last_Aggregate_Assignment (Obj_Id))
12711 Temp_Ins := Last_Aggregate_Assignment (Obj_Id);
12713 -- Otherwise the hook seizes the related object immediately
12719 Insert_After_And_Analyze (Temp_Ins,
12720 Make_Assignment_Statement (Loc,
12721 Name => New_Occurrence_Of (Temp_Id, Loc),
12722 Expression => Expr));
12724 -- Step 4: Finalize the transient controlled object after the context
12725 -- has been evaluated/elaborated. Generate:
12727 -- if Temp /= null then
12728 -- [Deep_]Finalize (Temp.all);
12732 -- When the node is part of a return statement, there is no need to
12733 -- insert a finalization call, as the general finalization mechanism
12734 -- (see Build_Finalizer) would take care of the transient controlled
12735 -- object on subprogram exit. Note that it would also be impossible to
12736 -- insert the finalization code after the return statement as this will
12737 -- render it unreachable.
12739 if Nkind (Finalization_Context) /= N_Simple_Return_Statement then
12740 Fin_Stmts := New_List (
12743 Make_Explicit_Dereference (Loc,
12744 Prefix => New_Occurrence_Of (Temp_Id, Loc)),
12747 Make_Assignment_Statement (Loc,
12748 Name => New_Occurrence_Of (Temp_Id, Loc),
12749 Expression => Make_Null (Loc)));
12751 if not Finalize_Always then
12752 Fin_Stmts := New_List (
12753 Make_Implicit_If_Statement (Decl,
12756 Left_Opnd => New_Occurrence_Of (Temp_Id, Loc),
12757 Right_Opnd => Make_Null (Loc)),
12758 Then_Statements => Fin_Stmts));
12761 Insert_Actions_After (Finalization_Context, Fin_Stmts);
12763 end Process_Transient_Object;
12765 ------------------------
12766 -- Rewrite_Comparison --
12767 ------------------------
12769 procedure Rewrite_Comparison (N : Node_Id) is
12770 Warning_Generated : Boolean := False;
12771 -- Set to True if first pass with Assume_Valid generates a warning in
12772 -- which case we skip the second pass to avoid warning overloaded.
12775 -- Set to Standard_True or Standard_False
12778 if Nkind (N) = N_Type_Conversion then
12779 Rewrite_Comparison (Expression (N));
12782 elsif Nkind (N) not in N_Op_Compare then
12786 -- Now start looking at the comparison in detail. We potentially go
12787 -- through this loop twice. The first time, Assume_Valid is set False
12788 -- in the call to Compile_Time_Compare. If this call results in a
12789 -- clear result of always True or Always False, that's decisive and
12790 -- we are done. Otherwise we repeat the processing with Assume_Valid
12791 -- set to True to generate additional warnings. We can skip that step
12792 -- if Constant_Condition_Warnings is False.
12794 for AV in False .. True loop
12796 Typ : constant Entity_Id := Etype (N);
12797 Op1 : constant Node_Id := Left_Opnd (N);
12798 Op2 : constant Node_Id := Right_Opnd (N);
12800 Res : constant Compare_Result :=
12801 Compile_Time_Compare (Op1, Op2, Assume_Valid => AV);
12802 -- Res indicates if compare outcome can be compile time determined
12804 True_Result : Boolean;
12805 False_Result : Boolean;
12808 case N_Op_Compare (Nkind (N)) is
12810 True_Result := Res = EQ;
12811 False_Result := Res = LT or else Res = GT or else Res = NE;
12814 True_Result := Res in Compare_GE;
12815 False_Result := Res = LT;
12818 and then Constant_Condition_Warnings
12819 and then Comes_From_Source (Original_Node (N))
12820 and then Nkind (Original_Node (N)) = N_Op_Ge
12821 and then not In_Instance
12822 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12823 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12826 ("can never be greater than, could replace by ""'=""?c?",
12828 Warning_Generated := True;
12832 True_Result := Res = GT;
12833 False_Result := Res in Compare_LE;
12836 True_Result := Res = LT;
12837 False_Result := Res in Compare_GE;
12840 True_Result := Res in Compare_LE;
12841 False_Result := Res = GT;
12844 and then Constant_Condition_Warnings
12845 and then Comes_From_Source (Original_Node (N))
12846 and then Nkind (Original_Node (N)) = N_Op_Le
12847 and then not In_Instance
12848 and then Is_Integer_Type (Etype (Left_Opnd (N)))
12849 and then not Has_Warnings_Off (Etype (Left_Opnd (N)))
12852 ("can never be less than, could replace by ""'=""?c?", N);
12853 Warning_Generated := True;
12857 True_Result := Res = NE or else Res = GT or else Res = LT;
12858 False_Result := Res = EQ;
12861 -- If this is the first iteration, then we actually convert the
12862 -- comparison into True or False, if the result is certain.
12865 if True_Result or False_Result then
12866 Result := Boolean_Literals (True_Result);
12869 New_Occurrence_Of (Result, Sloc (N))));
12870 Analyze_And_Resolve (N, Typ);
12871 Warn_On_Known_Condition (N);
12875 -- If this is the second iteration (AV = True), and the original
12876 -- node comes from source and we are not in an instance, then give
12877 -- a warning if we know result would be True or False. Note: we
12878 -- know Constant_Condition_Warnings is set if we get here.
12880 elsif Comes_From_Source (Original_Node (N))
12881 and then not In_Instance
12883 if True_Result then
12885 ("condition can only be False if invalid values present??",
12887 elsif False_Result then
12889 ("condition can only be True if invalid values present??",
12895 -- Skip second iteration if not warning on constant conditions or
12896 -- if the first iteration already generated a warning of some kind or
12897 -- if we are in any case assuming all values are valid (so that the
12898 -- first iteration took care of the valid case).
12900 exit when not Constant_Condition_Warnings;
12901 exit when Warning_Generated;
12902 exit when Assume_No_Invalid_Values;
12904 end Rewrite_Comparison;
12906 ----------------------------
12907 -- Safe_In_Place_Array_Op --
12908 ----------------------------
12910 function Safe_In_Place_Array_Op
12913 Op2 : Node_Id) return Boolean
12915 Target : Entity_Id;
12917 function Is_Safe_Operand (Op : Node_Id) return Boolean;
12918 -- Operand is safe if it cannot overlap part of the target of the
12919 -- operation. If the operand and the target are identical, the operand
12920 -- is safe. The operand can be empty in the case of negation.
12922 function Is_Unaliased (N : Node_Id) return Boolean;
12923 -- Check that N is a stand-alone entity
12929 function Is_Unaliased (N : Node_Id) return Boolean is
12933 and then No (Address_Clause (Entity (N)))
12934 and then No (Renamed_Object (Entity (N)));
12937 ---------------------
12938 -- Is_Safe_Operand --
12939 ---------------------
12941 function Is_Safe_Operand (Op : Node_Id) return Boolean is
12946 elsif Is_Entity_Name (Op) then
12947 return Is_Unaliased (Op);
12949 elsif Nkind_In (Op, N_Indexed_Component, N_Selected_Component) then
12950 return Is_Unaliased (Prefix (Op));
12952 elsif Nkind (Op) = N_Slice then
12954 Is_Unaliased (Prefix (Op))
12955 and then Entity (Prefix (Op)) /= Target;
12957 elsif Nkind (Op) = N_Op_Not then
12958 return Is_Safe_Operand (Right_Opnd (Op));
12963 end Is_Safe_Operand;
12965 -- Start of processing for Safe_In_Place_Array_Op
12968 -- Skip this processing if the component size is different from system
12969 -- storage unit (since at least for NOT this would cause problems).
12971 if Component_Size (Etype (Lhs)) /= System_Storage_Unit then
12974 -- Cannot do in place stuff on VM_Target since cannot pass addresses
12976 elsif VM_Target /= No_VM then
12979 -- Cannot do in place stuff if non-standard Boolean representation
12981 elsif Has_Non_Standard_Rep (Component_Type (Etype (Lhs))) then
12984 elsif not Is_Unaliased (Lhs) then
12988 Target := Entity (Lhs);
12989 return Is_Safe_Operand (Op1) and then Is_Safe_Operand (Op2);
12991 end Safe_In_Place_Array_Op;
12993 -----------------------
12994 -- Tagged_Membership --
12995 -----------------------
12997 -- There are two different cases to consider depending on whether the right
12998 -- operand is a class-wide type or not. If not we just compare the actual
12999 -- tag of the left expr to the target type tag:
13001 -- Left_Expr.Tag = Right_Type'Tag;
13003 -- If it is a class-wide type we use the RT function CW_Membership which is
13004 -- usually implemented by looking in the ancestor tables contained in the
13005 -- dispatch table pointed by Left_Expr.Tag for Typ'Tag
13007 -- Ada 2005 (AI-251): If it is a class-wide interface type we use the RT
13008 -- function IW_Membership which is usually implemented by looking in the
13009 -- table of abstract interface types plus the ancestor table contained in
13010 -- the dispatch table pointed by Left_Expr.Tag for Typ'Tag
13012 procedure Tagged_Membership
13014 SCIL_Node : out Node_Id;
13015 Result : out Node_Id)
13017 Left : constant Node_Id := Left_Opnd (N);
13018 Right : constant Node_Id := Right_Opnd (N);
13019 Loc : constant Source_Ptr := Sloc (N);
13021 Full_R_Typ : Entity_Id;
13022 Left_Type : Entity_Id;
13023 New_Node : Node_Id;
13024 Right_Type : Entity_Id;
13028 SCIL_Node := Empty;
13030 -- Handle entities from the limited view
13032 Left_Type := Available_View (Etype (Left));
13033 Right_Type := Available_View (Etype (Right));
13035 -- In the case where the type is an access type, the test is applied
13036 -- using the designated types (needed in Ada 2012 for implicit anonymous
13037 -- access conversions, for AI05-0149).
13039 if Is_Access_Type (Right_Type) then
13040 Left_Type := Designated_Type (Left_Type);
13041 Right_Type := Designated_Type (Right_Type);
13044 if Is_Class_Wide_Type (Left_Type) then
13045 Left_Type := Root_Type (Left_Type);
13048 if Is_Class_Wide_Type (Right_Type) then
13049 Full_R_Typ := Underlying_Type (Root_Type (Right_Type));
13051 Full_R_Typ := Underlying_Type (Right_Type);
13055 Make_Selected_Component (Loc,
13056 Prefix => Relocate_Node (Left),
13058 New_Occurrence_Of (First_Tag_Component (Left_Type), Loc));
13060 if Is_Class_Wide_Type (Right_Type) then
13062 -- No need to issue a run-time check if we statically know that the
13063 -- result of this membership test is always true. For example,
13064 -- considering the following declarations:
13066 -- type Iface is interface;
13067 -- type T is tagged null record;
13068 -- type DT is new T and Iface with null record;
13073 -- These membership tests are always true:
13076 -- Obj2 in T'Class;
13077 -- Obj2 in Iface'Class;
13079 -- We do not need to handle cases where the membership is illegal.
13082 -- Obj1 in DT'Class; -- Compile time error
13083 -- Obj1 in Iface'Class; -- Compile time error
13085 if not Is_Class_Wide_Type (Left_Type)
13086 and then (Is_Ancestor (Etype (Right_Type), Left_Type,
13087 Use_Full_View => True)
13088 or else (Is_Interface (Etype (Right_Type))
13089 and then Interface_Present_In_Ancestor
13091 Iface => Etype (Right_Type))))
13093 Result := New_Occurrence_Of (Standard_True, Loc);
13097 -- Ada 2005 (AI-251): Class-wide applied to interfaces
13099 if Is_Interface (Etype (Class_Wide_Type (Right_Type)))
13101 -- Support to: "Iface_CW_Typ in Typ'Class"
13103 or else Is_Interface (Left_Type)
13105 -- Issue error if IW_Membership operation not available in a
13106 -- configurable run time setting.
13108 if not RTE_Available (RE_IW_Membership) then
13110 ("dynamic membership test on interface types", N);
13116 Make_Function_Call (Loc,
13117 Name => New_Occurrence_Of (RTE (RE_IW_Membership), Loc),
13118 Parameter_Associations => New_List (
13119 Make_Attribute_Reference (Loc,
13121 Attribute_Name => Name_Address),
13122 New_Occurrence_Of (
13123 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))),
13126 -- Ada 95: Normal case
13129 Build_CW_Membership (Loc,
13130 Obj_Tag_Node => Obj_Tag,
13132 New_Occurrence_Of (
13133 Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc),
13135 New_Node => New_Node);
13137 -- Generate the SCIL node for this class-wide membership test.
13138 -- Done here because the previous call to Build_CW_Membership
13139 -- relocates Obj_Tag.
13141 if Generate_SCIL then
13142 SCIL_Node := Make_SCIL_Membership_Test (Sloc (N));
13143 Set_SCIL_Entity (SCIL_Node, Etype (Right_Type));
13144 Set_SCIL_Tag_Value (SCIL_Node, Obj_Tag);
13147 Result := New_Node;
13150 -- Right_Type is not a class-wide type
13153 -- No need to check the tag of the object if Right_Typ is abstract
13155 if Is_Abstract_Type (Right_Type) then
13156 Result := New_Occurrence_Of (Standard_False, Loc);
13161 Left_Opnd => Obj_Tag,
13164 (Node (First_Elmt (Access_Disp_Table (Full_R_Typ))), Loc));
13167 end Tagged_Membership;
13169 ------------------------------
13170 -- Unary_Op_Validity_Checks --
13171 ------------------------------
13173 procedure Unary_Op_Validity_Checks (N : Node_Id) is
13175 if Validity_Checks_On and Validity_Check_Operands then
13176 Ensure_Valid (Right_Opnd (N));
13178 end Unary_Op_Validity_Checks;